xref: /linux/fs/ext4/super.c (revision 244adf6426ee31a83f397b700d964cff12a247d3)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao  *  linux/fs/ext4/super.c
4ac27a0ecSDave Kleikamp  *
5ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
6ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
7ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
8ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
9ac27a0ecSDave Kleikamp  *
10ac27a0ecSDave Kleikamp  *  from
11ac27a0ecSDave Kleikamp  *
12ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
13ac27a0ecSDave Kleikamp  *
14ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
15ac27a0ecSDave Kleikamp  *
16ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
17ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
18ac27a0ecSDave Kleikamp  */
19ac27a0ecSDave Kleikamp 
20ac27a0ecSDave Kleikamp #include <linux/module.h>
21ac27a0ecSDave Kleikamp #include <linux/string.h>
22ac27a0ecSDave Kleikamp #include <linux/fs.h>
23ac27a0ecSDave Kleikamp #include <linux/time.h>
24c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
2866114cadSTejun Heo #include <linux/backing-dev.h>
29ac27a0ecSDave Kleikamp #include <linux/parser.h>
30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
31a5694255SChristoph Hellwig #include <linux/exportfs.h>
32ac27a0ecSDave Kleikamp #include <linux/vfs.h>
33ac27a0ecSDave Kleikamp #include <linux/random.h>
34ac27a0ecSDave Kleikamp #include <linux/mount.h>
35ac27a0ecSDave Kleikamp #include <linux/namei.h>
36ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
37ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
41ef510424SDan Williams #include <linux/dax.h>
427abc52c2SDan Magenheimer #include <linux/cleancache.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
44ee73f9a5SJeff Layton #include <linux/iversion.h>
45c83ad55eSGabriel Krisman Bertazi #include <linux/unicode.h>
46ac27a0ecSDave Kleikamp 
47bfff6873SLukas Czerner #include <linux/kthread.h>
48bfff6873SLukas Czerner #include <linux/freezer.h>
49bfff6873SLukas Czerner 
503dcf5451SChristoph Hellwig #include "ext4.h"
514a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
523dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
53ac27a0ecSDave Kleikamp #include "xattr.h"
54ac27a0ecSDave Kleikamp #include "acl.h"
553661d286STheodore Ts'o #include "mballoc.h"
560c9ec4beSDarrick J. Wong #include "fsmap.h"
57ac27a0ecSDave Kleikamp 
589bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
599bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
609bffad1eSTheodore Ts'o 
610b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
620b75a840SLukas Czerner static struct mutex ext4_li_mtx;
63e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
649f6200bbSTheodore Ts'o 
65617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
66ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
672adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
68e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
69617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
70617ba13bSMingming Cao 					struct ext4_super_block *es);
71617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
72617ba13bSMingming Cao 				   struct ext4_super_block *es);
73617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
74617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
75617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
76c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
78152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
79152a0836SAl Viro 		       const char *dev_name, void *data);
802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
82d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
83bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
84bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
858f1f7453SEric Sandeen static void ext4_clear_request_list(void);
86c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
87c6cb7e77SEric Whitney 					    unsigned int journal_inum);
88ac27a0ecSDave Kleikamp 
89e74031fdSJan Kara /*
90e74031fdSJan Kara  * Lock ordering
91e74031fdSJan Kara  *
92e74031fdSJan Kara  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
93e74031fdSJan Kara  * i_mmap_rwsem (inode->i_mmap_rwsem)!
94e74031fdSJan Kara  *
95e74031fdSJan Kara  * page fault path:
96e74031fdSJan Kara  * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
97e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
98e74031fdSJan Kara  *
99e74031fdSJan Kara  * buffered write path:
100e74031fdSJan Kara  * sb_start_write -> i_mutex -> mmap_sem
101e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
102e74031fdSJan Kara  *   i_data_sem (rw)
103e74031fdSJan Kara  *
104e74031fdSJan Kara  * truncate:
1051d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> i_mmap_rwsem (w) -> page lock
1061d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> transaction start ->
1071d39834fSNikolay Borisov  *   i_data_sem (rw)
108e74031fdSJan Kara  *
109e74031fdSJan Kara  * direct IO:
1101d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> mmap_sem
1111d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
112e74031fdSJan Kara  *
113e74031fdSJan Kara  * writepages:
114e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
115e74031fdSJan Kara  */
116e74031fdSJan Kara 
117c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1182035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1192035e776STheodore Ts'o 	.owner		= THIS_MODULE,
1202035e776STheodore Ts'o 	.name		= "ext2",
1212035e776STheodore Ts'o 	.mount		= ext4_mount,
1222035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
1232035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
1242035e776STheodore Ts'o };
1257f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
126fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1272035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1282035e776STheodore Ts'o #else
1292035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1302035e776STheodore Ts'o #endif
1312035e776STheodore Ts'o 
1322035e776STheodore Ts'o 
133ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
134ba69f9abSJan Kara 	.owner		= THIS_MODULE,
135ba69f9abSJan Kara 	.name		= "ext3",
136152a0836SAl Viro 	.mount		= ext4_mount,
137ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
138ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
139ba69f9abSJan Kara };
1407f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
141fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
142ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
143bd81d8eeSLaurent Vivier 
144fb265c9cSTheodore Ts'o /*
145fb265c9cSTheodore Ts'o  * This works like sb_bread() except it uses ERR_PTR for error
146fb265c9cSTheodore Ts'o  * returns.  Currently with sb_bread it's impossible to distinguish
147fb265c9cSTheodore Ts'o  * between ENOMEM and EIO situations (since both result in a NULL
148fb265c9cSTheodore Ts'o  * return.
149fb265c9cSTheodore Ts'o  */
150fb265c9cSTheodore Ts'o struct buffer_head *
151fb265c9cSTheodore Ts'o ext4_sb_bread(struct super_block *sb, sector_t block, int op_flags)
152fb265c9cSTheodore Ts'o {
153fb265c9cSTheodore Ts'o 	struct buffer_head *bh = sb_getblk(sb, block);
154fb265c9cSTheodore Ts'o 
155fb265c9cSTheodore Ts'o 	if (bh == NULL)
156fb265c9cSTheodore Ts'o 		return ERR_PTR(-ENOMEM);
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 
3381c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3391c13d5c0STheodore Ts'o 			    unsigned int line)
3401c13d5c0STheodore Ts'o {
3411c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3421c13d5c0STheodore Ts'o 
3431c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3441b46617bSTheodore Ts'o 	if (bdev_read_only(sb->s_bdev))
3451b46617bSTheodore Ts'o 		return;
3461c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3476a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_last_error_time);
3481c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3491c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
350878520acSTheodore Ts'o 	if (es->s_last_error_errcode == 0)
351878520acSTheodore Ts'o 		es->s_last_error_errcode = EXT4_ERR_EFSCORRUPTED;
3521c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3531c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3546a0678a7SArnd Bergmann 		es->s_first_error_time_hi = es->s_last_error_time_hi;
3551c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3561c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3571c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3581c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3591c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
360878520acSTheodore Ts'o 		es->s_first_error_errcode = es->s_last_error_errcode;
3611c13d5c0STheodore Ts'o 	}
36266e61a9eSTheodore Ts'o 	/*
36366e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
36466e61a9eSTheodore Ts'o 	 * started already
36566e61a9eSTheodore Ts'o 	 */
36666e61a9eSTheodore Ts'o 	if (!es->s_error_count)
36766e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
368ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
3691c13d5c0STheodore Ts'o }
3701c13d5c0STheodore Ts'o 
3711c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3721c13d5c0STheodore Ts'o 			    unsigned int line)
3731c13d5c0STheodore Ts'o {
3741c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3751c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3761c13d5c0STheodore Ts'o }
3771c13d5c0STheodore Ts'o 
378bdfe0cbdSTheodore Ts'o /*
379bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
380bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
381bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
382bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
383bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
384bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
385bdfe0cbdSTheodore Ts'o  */
386bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
387bdfe0cbdSTheodore Ts'o {
388bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
389bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
390bdfe0cbdSTheodore Ts'o 
391bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
392bdfe0cbdSTheodore Ts'o }
393bdfe0cbdSTheodore Ts'o 
39418aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
39518aadd47SBobi Jam {
39618aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
39718aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
39818aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
3995d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
40018aadd47SBobi Jam 
4015d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
402a0154344SDaeho Jeong 
403a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
404a0154344SDaeho Jeong 
40518aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
4065d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
4075d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
4085d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
40918aadd47SBobi Jam 		list_del_init(&jce->jce_list);
41018aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
41118aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
41218aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
41318aadd47SBobi Jam 	}
41418aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
41518aadd47SBobi Jam }
4161c13d5c0STheodore Ts'o 
4171dc1097fSJan Kara static bool system_going_down(void)
4181dc1097fSJan Kara {
4191dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4201dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
4211dc1097fSJan Kara }
4221dc1097fSJan Kara 
423ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
424ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
425ac27a0ecSDave Kleikamp  *
426ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
427617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
428ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
429ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
430ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
431ac27a0ecSDave Kleikamp  * write out the superblock safely.
432ac27a0ecSDave Kleikamp  *
433dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
434d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
435ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
436ac27a0ecSDave Kleikamp  */
437ac27a0ecSDave Kleikamp 
438617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
439ac27a0ecSDave Kleikamp {
440327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
441327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
442327eaf73STheodore Ts'o 
443bc98a42cSDavid Howells 	if (sb_rdonly(sb))
444ac27a0ecSDave Kleikamp 		return;
445ac27a0ecSDave Kleikamp 
446ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
447617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
448ac27a0ecSDave Kleikamp 
4494ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
450ac27a0ecSDave Kleikamp 		if (journal)
451dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
452ac27a0ecSDave Kleikamp 	}
4531dc1097fSJan Kara 	/*
4541dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4551dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
4561dc1097fSJan Kara 	 * disabled.
4571dc1097fSJan Kara 	 */
4581dc1097fSJan Kara 	if (test_opt(sb, ERRORS_RO) || system_going_down()) {
459b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
4604418e141SDmitry Monakhov 		/*
4614418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
4624418e141SDmitry Monakhov 		 * before ->s_flags update
4634418e141SDmitry Monakhov 		 */
4644418e141SDmitry Monakhov 		smp_wmb();
4651751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4661dc1097fSJan Kara 	} else if (test_opt(sb, ERRORS_PANIC)) {
4674327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
4684327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
4694327ba52SDaeho Jeong 			return;
470617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
471ac27a0ecSDave Kleikamp 			sb->s_id);
472ac27a0ecSDave Kleikamp 	}
4734327ba52SDaeho Jeong }
474ac27a0ecSDave Kleikamp 
475efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
476efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
477efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
478efbed4dcSTheodore Ts'o 
47912062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
480c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
481ac27a0ecSDave Kleikamp {
4820ff2ea7dSJoe Perches 	struct va_format vaf;
483ac27a0ecSDave Kleikamp 	va_list args;
484ac27a0ecSDave Kleikamp 
4850db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
4860db1ff22STheodore Ts'o 		return;
4870db1ff22STheodore Ts'o 
488ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
489efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
490ac27a0ecSDave Kleikamp 		va_start(args, fmt);
4910ff2ea7dSJoe Perches 		vaf.fmt = fmt;
4920ff2ea7dSJoe Perches 		vaf.va = &args;
493efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
494efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4950ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
496ac27a0ecSDave Kleikamp 		va_end(args);
497efbed4dcSTheodore Ts'o 	}
498f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
499617ba13bSMingming Cao 	ext4_handle_error(sb);
500ac27a0ecSDave Kleikamp }
501ac27a0ecSDave Kleikamp 
502e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
503c398eda0STheodore Ts'o 			unsigned int line, ext4_fsblk_t block,
504273df556SFrank Mayhar 			const char *fmt, ...)
505273df556SFrank Mayhar {
506273df556SFrank Mayhar 	va_list args;
507f7c21177STheodore Ts'o 	struct va_format vaf;
5081c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
509273df556SFrank Mayhar 
5100db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5110db1ff22STheodore Ts'o 		return;
5120db1ff22STheodore Ts'o 
513ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
5141c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
5151c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
516efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
517273df556SFrank Mayhar 		va_start(args, fmt);
518f7c21177STheodore Ts'o 		vaf.fmt = fmt;
519f7c21177STheodore Ts'o 		vaf.va = &args;
520c398eda0STheodore Ts'o 		if (block)
521d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
522d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
523d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
524d9ee81daSJoe Perches 			       block, current->comm, &vaf);
525d9ee81daSJoe Perches 		else
526d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
527d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
528d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
529d9ee81daSJoe Perches 			       current->comm, &vaf);
530273df556SFrank Mayhar 		va_end(args);
531efbed4dcSTheodore Ts'o 	}
532efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
533273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
534273df556SFrank Mayhar }
535273df556SFrank Mayhar 
536e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
537f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
538f7c21177STheodore Ts'o 		       const char *fmt, ...)
539273df556SFrank Mayhar {
540273df556SFrank Mayhar 	va_list args;
541f7c21177STheodore Ts'o 	struct va_format vaf;
5421c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
543496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
544273df556SFrank Mayhar 	char pathname[80], *path;
545273df556SFrank Mayhar 
5460db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5470db1ff22STheodore Ts'o 		return;
5480db1ff22STheodore Ts'o 
549ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
5501c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
5511c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
552efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
5539bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
554f9a62d09SDan Carpenter 		if (IS_ERR(path))
555273df556SFrank Mayhar 			path = "(unknown)";
556f7c21177STheodore Ts'o 		va_start(args, fmt);
557f7c21177STheodore Ts'o 		vaf.fmt = fmt;
558f7c21177STheodore Ts'o 		vaf.va = &args;
559d9ee81daSJoe Perches 		if (block)
560d9ee81daSJoe Perches 			printk(KERN_CRIT
561d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
562d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
563d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
564d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
565d9ee81daSJoe Perches 		else
566d9ee81daSJoe Perches 			printk(KERN_CRIT
567d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
568d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
569d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
570d9ee81daSJoe Perches 			       current->comm, path, &vaf);
571273df556SFrank Mayhar 		va_end(args);
572efbed4dcSTheodore Ts'o 	}
573efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
574273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
575273df556SFrank Mayhar }
576273df556SFrank Mayhar 
577722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
578ac27a0ecSDave Kleikamp 			      char nbuf[16])
579ac27a0ecSDave Kleikamp {
580ac27a0ecSDave Kleikamp 	char *errstr = NULL;
581ac27a0ecSDave Kleikamp 
582ac27a0ecSDave Kleikamp 	switch (errno) {
5836a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
5846a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
5856a797d27SDarrick J. Wong 		break;
5866a797d27SDarrick J. Wong 	case -EFSBADCRC:
5876a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
5886a797d27SDarrick J. Wong 		break;
589ac27a0ecSDave Kleikamp 	case -EIO:
590ac27a0ecSDave Kleikamp 		errstr = "IO failure";
591ac27a0ecSDave Kleikamp 		break;
592ac27a0ecSDave Kleikamp 	case -ENOMEM:
593ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
594ac27a0ecSDave Kleikamp 		break;
595ac27a0ecSDave Kleikamp 	case -EROFS:
59678f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
59778f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
598ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
599ac27a0ecSDave Kleikamp 		else
600ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
601ac27a0ecSDave Kleikamp 		break;
602ac27a0ecSDave Kleikamp 	default:
603ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
604ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
605ac27a0ecSDave Kleikamp 		 * NULL. */
606ac27a0ecSDave Kleikamp 		if (nbuf) {
607ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
608ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
609ac27a0ecSDave Kleikamp 				errstr = nbuf;
610ac27a0ecSDave Kleikamp 		}
611ac27a0ecSDave Kleikamp 		break;
612ac27a0ecSDave Kleikamp 	}
613ac27a0ecSDave Kleikamp 
614ac27a0ecSDave Kleikamp 	return errstr;
615ac27a0ecSDave Kleikamp }
616ac27a0ecSDave Kleikamp 
617878520acSTheodore Ts'o void ext4_set_errno(struct super_block *sb, int err)
618878520acSTheodore Ts'o {
619878520acSTheodore Ts'o 	if (err < 0)
620878520acSTheodore Ts'o 		err = -err;
621878520acSTheodore Ts'o 
622878520acSTheodore Ts'o 	switch (err) {
623878520acSTheodore Ts'o 	case EIO:
624878520acSTheodore Ts'o 		err = EXT4_ERR_EIO;
625878520acSTheodore Ts'o 		break;
626878520acSTheodore Ts'o 	case ENOMEM:
627878520acSTheodore Ts'o 		err = EXT4_ERR_ENOMEM;
628878520acSTheodore Ts'o 		break;
629878520acSTheodore Ts'o 	case EFSBADCRC:
630878520acSTheodore Ts'o 		err = EXT4_ERR_EFSBADCRC;
631878520acSTheodore Ts'o 		break;
632878520acSTheodore Ts'o 	case EFSCORRUPTED:
633878520acSTheodore Ts'o 		err = EXT4_ERR_EFSCORRUPTED;
634878520acSTheodore Ts'o 		break;
635878520acSTheodore Ts'o 	case ENOSPC:
636878520acSTheodore Ts'o 		err = EXT4_ERR_ENOSPC;
637878520acSTheodore Ts'o 		break;
638878520acSTheodore Ts'o 	case ENOKEY:
639878520acSTheodore Ts'o 		err = EXT4_ERR_ENOKEY;
640878520acSTheodore Ts'o 		break;
641878520acSTheodore Ts'o 	case EROFS:
642878520acSTheodore Ts'o 		err = EXT4_ERR_EROFS;
643878520acSTheodore Ts'o 		break;
644878520acSTheodore Ts'o 	case EFBIG:
645878520acSTheodore Ts'o 		err = EXT4_ERR_EFBIG;
646878520acSTheodore Ts'o 		break;
647878520acSTheodore Ts'o 	case EEXIST:
648878520acSTheodore Ts'o 		err = EXT4_ERR_EEXIST;
649878520acSTheodore Ts'o 		break;
650878520acSTheodore Ts'o 	case ERANGE:
651878520acSTheodore Ts'o 		err = EXT4_ERR_ERANGE;
652878520acSTheodore Ts'o 		break;
653878520acSTheodore Ts'o 	case EOVERFLOW:
654878520acSTheodore Ts'o 		err = EXT4_ERR_EOVERFLOW;
655878520acSTheodore Ts'o 		break;
656878520acSTheodore Ts'o 	case EBUSY:
657878520acSTheodore Ts'o 		err = EXT4_ERR_EBUSY;
658878520acSTheodore Ts'o 		break;
659878520acSTheodore Ts'o 	case ENOTDIR:
660878520acSTheodore Ts'o 		err = EXT4_ERR_ENOTDIR;
661878520acSTheodore Ts'o 		break;
662878520acSTheodore Ts'o 	case ENOTEMPTY:
663878520acSTheodore Ts'o 		err = EXT4_ERR_ENOTEMPTY;
664878520acSTheodore Ts'o 		break;
665878520acSTheodore Ts'o 	case ESHUTDOWN:
666878520acSTheodore Ts'o 		err = EXT4_ERR_ESHUTDOWN;
667878520acSTheodore Ts'o 		break;
668878520acSTheodore Ts'o 	case EFAULT:
669878520acSTheodore Ts'o 		err = EXT4_ERR_EFAULT;
670878520acSTheodore Ts'o 		break;
671878520acSTheodore Ts'o 	default:
672878520acSTheodore Ts'o 		err = EXT4_ERR_UNKNOWN;
673878520acSTheodore Ts'o 	}
674878520acSTheodore Ts'o 	EXT4_SB(sb)->s_es->s_last_error_errcode = err;
675878520acSTheodore Ts'o }
676878520acSTheodore Ts'o 
677617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
678ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
679ac27a0ecSDave Kleikamp 
680c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
681c398eda0STheodore Ts'o 		      unsigned int line, int errno)
682ac27a0ecSDave Kleikamp {
683ac27a0ecSDave Kleikamp 	char nbuf[16];
684ac27a0ecSDave Kleikamp 	const char *errstr;
685ac27a0ecSDave Kleikamp 
6860db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
6870db1ff22STheodore Ts'o 		return;
6880db1ff22STheodore Ts'o 
689ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
690ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
691ac27a0ecSDave Kleikamp 	 * an error. */
692bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
693ac27a0ecSDave Kleikamp 		return;
694ac27a0ecSDave Kleikamp 
695efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
696617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
697c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
698c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
699efbed4dcSTheodore Ts'o 	}
700ac27a0ecSDave Kleikamp 
701878520acSTheodore Ts'o 	ext4_set_errno(sb, -errno);
702efbed4dcSTheodore Ts'o 	save_error_info(sb, function, line);
703617ba13bSMingming Cao 	ext4_handle_error(sb);
704ac27a0ecSDave Kleikamp }
705ac27a0ecSDave Kleikamp 
706ac27a0ecSDave Kleikamp /*
707617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
708ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
709ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
710ac27a0ecSDave Kleikamp  *
711ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
712ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
713ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
714ac27a0ecSDave Kleikamp  */
715ac27a0ecSDave Kleikamp 
716c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
717c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
718ac27a0ecSDave Kleikamp {
719651e1c3bSJoe Perches 	struct va_format vaf;
720ac27a0ecSDave Kleikamp 	va_list args;
721ac27a0ecSDave Kleikamp 
7220db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7230db1ff22STheodore Ts'o 		return;
7240db1ff22STheodore Ts'o 
7251c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
726ac27a0ecSDave Kleikamp 	va_start(args, fmt);
727651e1c3bSJoe Perches 	vaf.fmt = fmt;
728651e1c3bSJoe Perches 	vaf.va = &args;
729651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
730651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
731ac27a0ecSDave Kleikamp 	va_end(args);
732ac27a0ecSDave Kleikamp 
733bc98a42cSDavid Howells 	if (sb_rdonly(sb) == 0) {
734b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
7354ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
7364418e141SDmitry Monakhov 		/*
7374418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
7384418e141SDmitry Monakhov 		 * before ->s_flags update
7394418e141SDmitry Monakhov 		 */
7404418e141SDmitry Monakhov 		smp_wmb();
7411751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
742ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
743dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
7441c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
7451c13d5c0STheodore Ts'o 	}
7462c1d0e36SJan Kara 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
7474327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
7484327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
7494327ba52SDaeho Jeong 			return;
7501c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
751ac27a0ecSDave Kleikamp 	}
7524327ba52SDaeho Jeong }
753ac27a0ecSDave Kleikamp 
754e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
755e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
756b31e1552SEric Sandeen {
7570ff2ea7dSJoe Perches 	struct va_format vaf;
758b31e1552SEric Sandeen 	va_list args;
759b31e1552SEric Sandeen 
760efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
761efbed4dcSTheodore Ts'o 		return;
762efbed4dcSTheodore Ts'o 
763b31e1552SEric Sandeen 	va_start(args, fmt);
7640ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7650ff2ea7dSJoe Perches 	vaf.va = &args;
7660ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
767b31e1552SEric Sandeen 	va_end(args);
768b31e1552SEric Sandeen }
769b31e1552SEric Sandeen 
770b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb)					\
771b03a2f7eSAndreas Dilger 		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
772b03a2f7eSAndreas Dilger 			     "EXT4-fs warning")
773b03a2f7eSAndreas Dilger 
77412062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
775c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
776ac27a0ecSDave Kleikamp {
7770ff2ea7dSJoe Perches 	struct va_format vaf;
778ac27a0ecSDave Kleikamp 	va_list args;
779ac27a0ecSDave Kleikamp 
780b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
781efbed4dcSTheodore Ts'o 		return;
782efbed4dcSTheodore Ts'o 
783ac27a0ecSDave Kleikamp 	va_start(args, fmt);
7840ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7850ff2ea7dSJoe Perches 	vaf.va = &args;
7860ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
7870ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
788ac27a0ecSDave Kleikamp 	va_end(args);
789ac27a0ecSDave Kleikamp }
790ac27a0ecSDave Kleikamp 
791b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
792b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
793b03a2f7eSAndreas Dilger {
794b03a2f7eSAndreas Dilger 	struct va_format vaf;
795b03a2f7eSAndreas Dilger 	va_list args;
796b03a2f7eSAndreas Dilger 
797b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
798b03a2f7eSAndreas Dilger 		return;
799b03a2f7eSAndreas Dilger 
800b03a2f7eSAndreas Dilger 	va_start(args, fmt);
801b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
802b03a2f7eSAndreas Dilger 	vaf.va = &args;
803b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
804b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
805b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
806b03a2f7eSAndreas Dilger 	va_end(args);
807b03a2f7eSAndreas Dilger }
808b03a2f7eSAndreas Dilger 
809e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
810e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
811e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
812e29136f8STheodore Ts'o 			     const char *fmt, ...)
8135d1b1b3fSAneesh Kumar K.V __releases(bitlock)
8145d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
8155d1b1b3fSAneesh Kumar K.V {
8160ff2ea7dSJoe Perches 	struct va_format vaf;
8175d1b1b3fSAneesh Kumar K.V 	va_list args;
8185d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
8195d1b1b3fSAneesh Kumar K.V 
8200db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
8210db1ff22STheodore Ts'o 		return;
8220db1ff22STheodore Ts'o 
823ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
8241c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
8251c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
8261c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
8270ff2ea7dSJoe Perches 
828efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
8295d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
8300ff2ea7dSJoe Perches 		vaf.fmt = fmt;
8310ff2ea7dSJoe Perches 		vaf.va = &args;
83221149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
833e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
834e29136f8STheodore Ts'o 		if (ino)
8350ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
836e29136f8STheodore Ts'o 		if (block)
837efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
838efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
8390ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
8405d1b1b3fSAneesh Kumar K.V 		va_end(args);
841efbed4dcSTheodore Ts'o 	}
8425d1b1b3fSAneesh Kumar K.V 
843327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
844327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
845327eaf73STheodore Ts'o 
8465d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
847e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
8485d1b1b3fSAneesh Kumar K.V 		return;
8495d1b1b3fSAneesh Kumar K.V 	}
8501c13d5c0STheodore Ts'o 
8515d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
85206f29cc8SZhouyi Zhou 	ext4_commit_super(sb, 1);
8535d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
8545d1b1b3fSAneesh Kumar K.V 	/*
8555d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
8565d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
8575d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
8585d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
8595d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
86025985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
8615d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
8625d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
8635d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
8645d1b1b3fSAneesh Kumar K.V 	 */
8655d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
8665d1b1b3fSAneesh Kumar K.V 	return;
8675d1b1b3fSAneesh Kumar K.V }
8685d1b1b3fSAneesh Kumar K.V 
869db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
870db79e6d1SWang Shilong 				     ext4_group_t group,
871db79e6d1SWang Shilong 				     unsigned int flags)
872db79e6d1SWang Shilong {
873db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
874db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
875db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
8769af0b3d1SWang Shilong 	int ret;
877db79e6d1SWang Shilong 
8789af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
8799af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
8809af0b3d1SWang Shilong 					    &grp->bb_state);
8819af0b3d1SWang Shilong 		if (!ret)
882db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
883db79e6d1SWang Shilong 					   grp->bb_free);
884db79e6d1SWang Shilong 	}
885db79e6d1SWang Shilong 
8869af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
8879af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
8889af0b3d1SWang Shilong 					    &grp->bb_state);
8899af0b3d1SWang Shilong 		if (!ret && gdp) {
890db79e6d1SWang Shilong 			int count;
891db79e6d1SWang Shilong 
892db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
893db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
894db79e6d1SWang Shilong 					   count);
895db79e6d1SWang Shilong 		}
896db79e6d1SWang Shilong 	}
897db79e6d1SWang Shilong }
898db79e6d1SWang Shilong 
899617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
900ac27a0ecSDave Kleikamp {
901617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
902ac27a0ecSDave Kleikamp 
903617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
904ac27a0ecSDave Kleikamp 		return;
905ac27a0ecSDave Kleikamp 
90612062dddSEric Sandeen 	ext4_warning(sb,
907ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
908ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
909617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
910ac27a0ecSDave Kleikamp 
911617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
912617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
913617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
914ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
915ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
916ac27a0ecSDave Kleikamp 
917ac27a0ecSDave Kleikamp 	/*
918ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
919ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
920ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
921ac27a0ecSDave Kleikamp 	 */
922ac27a0ecSDave Kleikamp }
923ac27a0ecSDave Kleikamp 
924ac27a0ecSDave Kleikamp /*
925ac27a0ecSDave Kleikamp  * Open the external journal device
926ac27a0ecSDave Kleikamp  */
927b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
928ac27a0ecSDave Kleikamp {
929ac27a0ecSDave Kleikamp 	struct block_device *bdev;
930ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
931ac27a0ecSDave Kleikamp 
932d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
933ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
934ac27a0ecSDave Kleikamp 		goto fail;
935ac27a0ecSDave Kleikamp 	return bdev;
936ac27a0ecSDave Kleikamp 
937ac27a0ecSDave Kleikamp fail:
938b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
939ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
940ac27a0ecSDave Kleikamp 	return NULL;
941ac27a0ecSDave Kleikamp }
942ac27a0ecSDave Kleikamp 
943ac27a0ecSDave Kleikamp /*
944ac27a0ecSDave Kleikamp  * Release the journal device
945ac27a0ecSDave Kleikamp  */
9464385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
947ac27a0ecSDave Kleikamp {
9484385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
949ac27a0ecSDave Kleikamp }
950ac27a0ecSDave Kleikamp 
9514385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
952ac27a0ecSDave Kleikamp {
953ac27a0ecSDave Kleikamp 	struct block_device *bdev;
954ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
955ac27a0ecSDave Kleikamp 	if (bdev) {
9564385bab1SAl Viro 		ext4_blkdev_put(bdev);
957ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
958ac27a0ecSDave Kleikamp 	}
959ac27a0ecSDave Kleikamp }
960ac27a0ecSDave Kleikamp 
961ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
962ac27a0ecSDave Kleikamp {
963617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
964ac27a0ecSDave Kleikamp }
965ac27a0ecSDave Kleikamp 
966617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
967ac27a0ecSDave Kleikamp {
968ac27a0ecSDave Kleikamp 	struct list_head *l;
969ac27a0ecSDave Kleikamp 
970b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
971ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
972ac27a0ecSDave Kleikamp 
973ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
974ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
975ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
976ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
977ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
978ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
979ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
980ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
981ac27a0ecSDave Kleikamp 	}
982ac27a0ecSDave Kleikamp }
983ac27a0ecSDave Kleikamp 
984957153fcSJan Kara #ifdef CONFIG_QUOTA
985957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
986957153fcSJan Kara 
987957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
988957153fcSJan Kara {
989957153fcSJan Kara 	int type;
990957153fcSJan Kara 
991957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
992957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
993957153fcSJan Kara 		ext4_quota_off(sb, type);
994957153fcSJan Kara }
99533458eabSTheodore Ts'o 
99633458eabSTheodore Ts'o /*
99733458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
99833458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
99933458eabSTheodore Ts'o  */
100033458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
100133458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
100233458eabSTheodore Ts'o 				int type)
100333458eabSTheodore Ts'o {
100433458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
100533458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
100633458eabSTheodore Ts'o }
1007957153fcSJan Kara #else
1008957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1009957153fcSJan Kara {
1010957153fcSJan Kara }
1011957153fcSJan Kara #endif
1012957153fcSJan Kara 
1013617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
1014ac27a0ecSDave Kleikamp {
1015617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1016617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
101797abd7d4STheodore Ts'o 	int aborted = 0;
1018ef2cabf7SHidehiro Kawai 	int i, err;
1019ac27a0ecSDave Kleikamp 
1020857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1021957153fcSJan Kara 	ext4_quota_off_umount(sb);
1022e0ccfd95SChristoph Hellwig 
10232e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
10244c0425ffSMingming Cao 
10250390131bSFrank Mayhar 	if (sbi->s_journal) {
102697abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1027ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
102847b4a50bSJan Kara 		sbi->s_journal = NULL;
1029878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
1030878520acSTheodore Ts'o 			ext4_set_errno(sb, -err);
1031c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
10320390131bSFrank Mayhar 		}
1033878520acSTheodore Ts'o 	}
1034d4edac31SJosef Bacik 
1035ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
1036d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
10379105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
1038d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1039d4edac31SJosef Bacik 	ext4_mb_release(sb);
1040d4edac31SJosef Bacik 	ext4_ext_release(sb);
1041d4edac31SJosef Bacik 
1042bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1043e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
1044ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1045ac27a0ecSDave Kleikamp 	}
1046bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
1047a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
1048a8e25a83SArtem Bityutskiy 
1049ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
1050ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
1051b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
1052b93b41d4SAl Viro 	kvfree(sbi->s_flex_groups);
105357042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1054ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1055ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
105657042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1057c8585c6fSDaeho Jeong 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
1058ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1059a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
106033458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1061ac27a0ecSDave Kleikamp #endif
1062ac27a0ecSDave Kleikamp 
1063ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1064ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1065ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1066ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1067ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1068ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1069ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
1070ac27a0ecSDave Kleikamp 
107189d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1072f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1073ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
1074ac27a0ecSDave Kleikamp 		/*
1075ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1076ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1077ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1078ac27a0ecSDave Kleikamp 		 */
1079ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
1080f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
1081617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1082ac27a0ecSDave Kleikamp 	}
108350c15df6SChengguang Xu 
1084dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1085dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
108650c15df6SChengguang Xu 
108747387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
108847387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
108950c15df6SChengguang Xu 
1090c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
1091c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
10929060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1093ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
10943197ebdbSTheodore Ts'o 	/*
10953197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
10963197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
10973197ebdbSTheodore Ts'o 	 */
10983197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
10993197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
11000441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
11010441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1102705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
11035e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1104c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1105c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
1106c83ad55eSGabriel Krisman Bertazi #endif
1107ac27a0ecSDave Kleikamp 	kfree(sbi);
1108ac27a0ecSDave Kleikamp }
1109ac27a0ecSDave Kleikamp 
1110e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1111ac27a0ecSDave Kleikamp 
1112ac27a0ecSDave Kleikamp /*
1113ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1114ac27a0ecSDave Kleikamp  */
1115617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1116ac27a0ecSDave Kleikamp {
1117617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1118ac27a0ecSDave Kleikamp 
1119e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1120ac27a0ecSDave Kleikamp 	if (!ei)
1121ac27a0ecSDave Kleikamp 		return NULL;
11220b8e58a1SAndreas Dilger 
1123ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1124202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1125c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
1126c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
11279a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
11289a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1129edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1130eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1131edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1132dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1133d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1134d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
11351dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1136a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1137a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
113896c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1139a9e7f447SDmitry Monakhov #endif
11408aefcd55STheodore Ts'o 	ei->jinode = NULL;
11412e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1142744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1143b436b9beSJan Kara 	ei->i_sync_tid = 0;
1144b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1145e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
11462e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1147ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1148ac27a0ecSDave Kleikamp }
1149ac27a0ecSDave Kleikamp 
11507ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
11517ff9c073STheodore Ts'o {
11527ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
11537ff9c073STheodore Ts'o 
115429b3692eSEric Biggers 	if (!drop)
115529b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
115629b3692eSEric Biggers 
11577ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
11587ff9c073STheodore Ts'o 	return drop;
11597ff9c073STheodore Ts'o }
11607ff9c073STheodore Ts'o 
116194053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1162fa0d7e3dSNick Piggin {
11632c58d548SEric Biggers 	fscrypt_free_inode(inode);
1164fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1165fa0d7e3dSNick Piggin }
1166fa0d7e3dSNick Piggin 
1167617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1168ac27a0ecSDave Kleikamp {
11699f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1170b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1171b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1172b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
11739f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
11749f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
11759f7dd93dSVasily Averin 				true);
11769f7dd93dSVasily Averin 		dump_stack();
11779f7dd93dSVasily Averin 	}
1178ac27a0ecSDave Kleikamp }
1179ac27a0ecSDave Kleikamp 
118051cc5068SAlexey Dobriyan static void init_once(void *foo)
1181ac27a0ecSDave Kleikamp {
1182617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1183ac27a0ecSDave Kleikamp 
1184ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1185ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
11860e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1187ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1188ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1189ac27a0ecSDave Kleikamp }
1190ac27a0ecSDave Kleikamp 
1191e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1192ac27a0ecSDave Kleikamp {
1193f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1194f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1195f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1196f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1197f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1198f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
119920c2df83SPaul Mundt 				init_once);
1200617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1201ac27a0ecSDave Kleikamp 		return -ENOMEM;
1202ac27a0ecSDave Kleikamp 	return 0;
1203ac27a0ecSDave Kleikamp }
1204ac27a0ecSDave Kleikamp 
1205ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1206ac27a0ecSDave Kleikamp {
12078c0a8537SKirill A. Shutemov 	/*
12088c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
12098c0a8537SKirill A. Shutemov 	 * destroy cache.
12108c0a8537SKirill A. Shutemov 	 */
12118c0a8537SKirill A. Shutemov 	rcu_barrier();
1212617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1213ac27a0ecSDave Kleikamp }
1214ac27a0ecSDave Kleikamp 
12150930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1216ac27a0ecSDave Kleikamp {
12170930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1218dbd5768fSJan Kara 	clear_inode(inode);
1219c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
122051865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1221f4c2d372SJan Kara 	dquot_drop(inode);
12228aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
12238aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
12248aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
12258aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
12268aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
12278aefcd55STheodore Ts'o 	}
12283d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1229c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1230ac27a0ecSDave Kleikamp }
1231ac27a0ecSDave Kleikamp 
12321b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
12331b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1234ac27a0ecSDave Kleikamp {
1235ac27a0ecSDave Kleikamp 	struct inode *inode;
1236ac27a0ecSDave Kleikamp 
12378a363970STheodore Ts'o 	/*
1238ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1239ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1240ac27a0ecSDave Kleikamp 	 */
12418a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
12421d1fe1eeSDavid Howells 	if (IS_ERR(inode))
12431d1fe1eeSDavid Howells 		return ERR_CAST(inode);
12441d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1245ac27a0ecSDave Kleikamp 		iput(inode);
1246ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1247ac27a0ecSDave Kleikamp 	}
12481b961ac0SChristoph Hellwig 
12491b961ac0SChristoph Hellwig 	return inode;
1250ac27a0ecSDave Kleikamp }
12511b961ac0SChristoph Hellwig 
12521b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
12531b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12541b961ac0SChristoph Hellwig {
12551b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
12561b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
12571b961ac0SChristoph Hellwig }
12581b961ac0SChristoph Hellwig 
12591b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
12601b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12611b961ac0SChristoph Hellwig {
12621b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
12631b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1264ac27a0ecSDave Kleikamp }
1265ac27a0ecSDave Kleikamp 
1266fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1267fde87268STheodore Ts'o {
1268fde87268STheodore Ts'o 	struct writeback_control wbc = {
1269fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1270fde87268STheodore Ts'o 	};
1271fde87268STheodore Ts'o 
1272fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1273fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1274fde87268STheodore Ts'o }
1275fde87268STheodore Ts'o 
1276c39a7f84SToshiyuki Okajima /*
1277c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1278c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1279c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1280c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1281c39a7f84SToshiyuki Okajima  */
12820b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
12830b8e58a1SAndreas Dilger 				 gfp_t wait)
1284c39a7f84SToshiyuki Okajima {
1285c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1286c39a7f84SToshiyuki Okajima 
1287c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1288c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1289c39a7f84SToshiyuki Okajima 		return 0;
1290c39a7f84SToshiyuki Okajima 	if (journal)
1291c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1292d0164adcSMel Gorman 						wait & ~__GFP_DIRECT_RECLAIM);
1293c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1294c39a7f84SToshiyuki Okajima }
1295c39a7f84SToshiyuki Okajima 
1296643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1297a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1298a7550b30SJaegeuk Kim {
1299a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1300a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1301a7550b30SJaegeuk Kim }
1302a7550b30SJaegeuk Kim 
1303a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1304a7550b30SJaegeuk Kim 							void *fs_data)
1305a7550b30SJaegeuk Kim {
13062f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1307c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1308a7550b30SJaegeuk Kim 
13099ce0151aSEric Biggers 	/*
13109ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
13119ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
13129ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
13139ce0151aSEric Biggers 	 * the filename "lost+found" itself.
13149ce0151aSEric Biggers 	 */
13159ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
13169ce0151aSEric Biggers 		return -EPERM;
1317a7550b30SJaegeuk Kim 
13187d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
13197d3e06a8SRoss Zwisler 		return -EINVAL;
13207d3e06a8SRoss Zwisler 
132194840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
132294840e3cSEric Biggers 	if (res)
132394840e3cSEric Biggers 		return res;
132494840e3cSEric Biggers 
13252f8f5e76SEric Biggers 	/*
13262f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
13272f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
13282f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
13292f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
13302f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
13312f8f5e76SEric Biggers 	 */
13322f8f5e76SEric Biggers 
13332f8f5e76SEric Biggers 	if (handle) {
13342f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
13352f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
13362f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13372f8f5e76SEric Biggers 					    ctx, len, 0);
1338a7550b30SJaegeuk Kim 		if (!res) {
1339a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1340a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1341a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1342a3caa24bSJan Kara 			/*
13432ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13442ee6a576SEric Biggers 			 * S_DAX may be disabled
1345a3caa24bSJan Kara 			 */
1346a3caa24bSJan Kara 			ext4_set_inode_flags(inode);
1347a7550b30SJaegeuk Kim 		}
1348a7550b30SJaegeuk Kim 		return res;
1349a7550b30SJaegeuk Kim 	}
1350a7550b30SJaegeuk Kim 
1351b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1352b8cb5a54STahsin Erdogan 	if (res)
1353b8cb5a54STahsin Erdogan 		return res;
13542f8f5e76SEric Biggers retry:
1355af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1356af65207cSTahsin Erdogan 				     &credits);
1357dec214d0STahsin Erdogan 	if (res)
1358dec214d0STahsin Erdogan 		return res;
1359dec214d0STahsin Erdogan 
1360c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1361a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1362a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1363a7550b30SJaegeuk Kim 
13642f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
13652f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13662f8f5e76SEric Biggers 				    ctx, len, 0);
1367a7550b30SJaegeuk Kim 	if (!res) {
1368a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
13692ee6a576SEric Biggers 		/*
13702ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13712ee6a576SEric Biggers 		 * S_DAX may be disabled
13722ee6a576SEric Biggers 		 */
1373a3caa24bSJan Kara 		ext4_set_inode_flags(inode);
1374a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1375a7550b30SJaegeuk Kim 		if (res)
1376a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1377a7550b30SJaegeuk Kim 	}
1378a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
13792f8f5e76SEric Biggers 
13802f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
13812f8f5e76SEric Biggers 		goto retry;
1382a7550b30SJaegeuk Kim 	if (!res)
1383a7550b30SJaegeuk Kim 		res = res2;
1384a7550b30SJaegeuk Kim 	return res;
1385a7550b30SJaegeuk Kim }
1386a7550b30SJaegeuk Kim 
1387c250b7ddSEric Biggers static bool ext4_dummy_context(struct inode *inode)
1388a7550b30SJaegeuk Kim {
1389a7550b30SJaegeuk Kim 	return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
1390a7550b30SJaegeuk Kim }
1391a7550b30SJaegeuk Kim 
1392b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb)
1393b925acb8SEric Biggers {
1394b925acb8SEric Biggers 	return ext4_has_feature_stable_inodes(sb);
1395b925acb8SEric Biggers }
1396b925acb8SEric Biggers 
1397b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1398b925acb8SEric Biggers 				       int *ino_bits_ret, int *lblk_bits_ret)
1399b925acb8SEric Biggers {
1400b925acb8SEric Biggers 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1401b925acb8SEric Biggers 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1402b925acb8SEric Biggers }
1403b925acb8SEric Biggers 
14046f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1405a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1406a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1407a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1408a7550b30SJaegeuk Kim 	.dummy_context		= ext4_dummy_context,
1409a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1410e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1411b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1412b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1413a7550b30SJaegeuk Kim };
1414a7550b30SJaegeuk Kim #endif
1415a7550b30SJaegeuk Kim 
1416ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1417d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1418689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1419ac27a0ecSDave Kleikamp 
1420617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1421617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1422617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1423617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1424617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
14256f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
14268c54ca9cSAl Viro 			 const struct path *path);
1427617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1428617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1429ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1430617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1431ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
14327c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
14337c319d32SAditya Kali 			     unsigned int flags);
14347c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1435ac27a0ecSDave Kleikamp 
143696c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
143796c7e0d9SJan Kara {
143896c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
143996c7e0d9SJan Kara }
144096c7e0d9SJan Kara 
144161e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
144260e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1443617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1444617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1445617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1446617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1447a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1448a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1449a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1450040cb378SLi Xi 	.get_projid		= ext4_get_projid,
14517a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1452ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1453ac27a0ecSDave Kleikamp };
1454ac27a0ecSDave Kleikamp 
14550d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1456617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1457ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1458287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
14590a240339SJan Kara 	.get_state	= dquot_get_state,
1460287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1461287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
14626332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
14636332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1464ac27a0ecSDave Kleikamp };
1465ac27a0ecSDave Kleikamp #endif
1466ac27a0ecSDave Kleikamp 
1467ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1468617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
146994053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1470617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1471617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1472617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
14737ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
14740930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1475617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1476617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1477c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1478c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1479617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1480617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1481617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1482ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1483617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1484617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
148596c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1486ac27a0ecSDave Kleikamp #endif
1487c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1488ac27a0ecSDave Kleikamp };
1489ac27a0ecSDave Kleikamp 
149039655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
14911b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
14921b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1493617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1494fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1495ac27a0ecSDave Kleikamp };
1496ac27a0ecSDave Kleikamp 
1497ac27a0ecSDave Kleikamp enum {
1498ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1499ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
150072578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1501ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
150272578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1503ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1504ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1505ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
15066ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1507ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
15085a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1509ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
151049da9392SJan Kara 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
1511327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1512327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1513670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
15141449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
15155328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1516744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1517fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1518cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1519ac27a0ecSDave Kleikamp };
1520ac27a0ecSDave Kleikamp 
1521a447c093SSteven Whitehouse static const match_table_t tokens = {
1522ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1523ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1524ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1525ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1526ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1527ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1528ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1529ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1530ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1531ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1532ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1533ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1534ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1535ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
153672578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
153772578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1538ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1539ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1540ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1541ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1542e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
15435a916be1STheodore Ts'o 	{Opt_noload, "noload"},
154472578c33STheodore Ts'o 	{Opt_removed, "nobh"},
154572578c33STheodore Ts'o 	{Opt_removed, "bh"},
1546ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
154730773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
154830773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1549ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1550ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1551818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1552c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1553818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1554ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1555ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1556ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1557ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
15585bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
15595bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1560ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1561ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1562ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1563ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1564ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1565ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
15665a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1567ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1568ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1569ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1570ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
157149da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1572ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
157306705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
157406705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
157525ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1576923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1577c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
157864769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1579327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1580327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1581a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1582a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1583670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1584dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
158536ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
158636ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15876fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15886fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1589240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1590b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1591afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
159206705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
159306705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1594744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1595*244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1596744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
15975328e635SEric Sandeen 	{Opt_discard, "discard"},
15985328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1599fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1600fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1601fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1602df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
16036ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1604cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1605cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1606c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1607c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1608c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1609c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1610c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1611f3f12faaSJosef Bacik 	{Opt_err, NULL},
1612ac27a0ecSDave Kleikamp };
1613ac27a0ecSDave Kleikamp 
1614617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1615ac27a0ecSDave Kleikamp {
1616617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1617ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1618ac27a0ecSDave Kleikamp 
1619ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1620ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
16210b8e58a1SAndreas Dilger 
1622ac27a0ecSDave Kleikamp 	options += 3;
16230b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1624ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1625ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
16264776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1627ac27a0ecSDave Kleikamp 		       (char *) *data);
1628ac27a0ecSDave Kleikamp 		return 1;
1629ac27a0ecSDave Kleikamp 	}
1630ac27a0ecSDave Kleikamp 	if (*options == ',')
1631ac27a0ecSDave Kleikamp 		options++;
1632ac27a0ecSDave Kleikamp 	*data = (void *) options;
16330b8e58a1SAndreas Dilger 
1634ac27a0ecSDave Kleikamp 	return sb_block;
1635ac27a0ecSDave Kleikamp }
1636ac27a0ecSDave Kleikamp 
1637b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1638d6006186SEric Biggers static const char deprecated_msg[] =
1639d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1640437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1641b3881f74STheodore Ts'o 
164256c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
164356c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
164456c50f11SDmitry Monakhov {
164556c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
164633458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
164703dafb5fSChen Gang 	int ret = -1;
164856c50f11SDmitry Monakhov 
164933458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
165056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
165156c50f11SDmitry Monakhov 			"Cannot change journaled "
165256c50f11SDmitry Monakhov 			"quota options when quota turned on");
165357f73c2cSTheodore Ts'o 		return -1;
165456c50f11SDmitry Monakhov 	}
1655e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1656c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1657c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1658c325a67cSTheodore Ts'o 		return 1;
1659262b4662SJan Kara 	}
166056c50f11SDmitry Monakhov 	qname = match_strdup(args);
166156c50f11SDmitry Monakhov 	if (!qname) {
166256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
166356c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
166457f73c2cSTheodore Ts'o 		return -1;
166556c50f11SDmitry Monakhov 	}
166633458eabSTheodore Ts'o 	if (old_qname) {
166733458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
166803dafb5fSChen Gang 			ret = 1;
166903dafb5fSChen Gang 		else
167056c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
167103dafb5fSChen Gang 				 "%s quota file already specified",
167203dafb5fSChen Gang 				 QTYPE2NAME(qtype));
167303dafb5fSChen Gang 		goto errout;
167456c50f11SDmitry Monakhov 	}
167503dafb5fSChen Gang 	if (strchr(qname, '/')) {
167656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
167756c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
167803dafb5fSChen Gang 		goto errout;
167956c50f11SDmitry Monakhov 	}
168033458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1681fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
168256c50f11SDmitry Monakhov 	return 1;
168303dafb5fSChen Gang errout:
168403dafb5fSChen Gang 	kfree(qname);
168503dafb5fSChen Gang 	return ret;
168656c50f11SDmitry Monakhov }
168756c50f11SDmitry Monakhov 
168856c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
168956c50f11SDmitry Monakhov {
169056c50f11SDmitry Monakhov 
169156c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
169233458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
169356c50f11SDmitry Monakhov 
169433458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
169556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
169656c50f11SDmitry Monakhov 			" when quota turned on");
169757f73c2cSTheodore Ts'o 		return -1;
169856c50f11SDmitry Monakhov 	}
169933458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
170033458eabSTheodore Ts'o 	synchronize_rcu();
170133458eabSTheodore Ts'o 	kfree(old_qname);
170256c50f11SDmitry Monakhov 	return 1;
170356c50f11SDmitry Monakhov }
170456c50f11SDmitry Monakhov #endif
170556c50f11SDmitry Monakhov 
170626092bf5STheodore Ts'o #define MOPT_SET	0x0001
170726092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
170826092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
170926092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
171026092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
171126092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
171226092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
171326092bf5STheodore Ts'o #define MOPT_Q		0
171426092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
171526092bf5STheodore Ts'o #else
171626092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
171726092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
171826092bf5STheodore Ts'o #endif
171926092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
17208dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
17218dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
17228dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1723ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
172426092bf5STheodore Ts'o 
172526092bf5STheodore Ts'o static const struct mount_opts {
172626092bf5STheodore Ts'o 	int	token;
172726092bf5STheodore Ts'o 	int	mount_opt;
172826092bf5STheodore Ts'o 	int	flags;
172926092bf5STheodore Ts'o } ext4_mount_opts[] = {
173026092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
173126092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
173226092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
173326092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
173426092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
173526092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
17368dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
17378dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
17388dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
17398dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
174026092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
174126092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
17428dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
17438dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17448dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
174559d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1746327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1747327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1748c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1749c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
17508dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
17511e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
175226092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
17538dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
17541e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17558dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
175626092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
175726092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
175826092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
17598dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
17607915a861SAles Novak 	 MOPT_NO_EXT2},
17618dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17627915a861SAles Novak 	 MOPT_NO_EXT2},
176326092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
176426092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
176526092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
176626092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
176726092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
176826092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
176926092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
177026092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
177126092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
177226092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1773923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
177426092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17750efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17760efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17775ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17785ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17795ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17808dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17818dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17828dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
17838dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
178426092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
178526092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
178626092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
178726092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
178826092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
178926092bf5STheodore Ts'o #else
179026092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
179126092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
179226092bf5STheodore Ts'o #endif
179326092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
179426092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1795670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
179626092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
179726092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
179826092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
179926092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
180026092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
180149da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
180249da9392SJan Kara 							MOPT_SET | MOPT_Q},
180326092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
180449da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
180549da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
180626092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
180726092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
180826092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
180926092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
181026092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
181126092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
181226092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1813df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
18146ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1815cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
181626092bf5STheodore Ts'o 	{Opt_err, 0, 0}
181726092bf5STheodore Ts'o };
181826092bf5STheodore Ts'o 
1819c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1820c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1821c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1822c83ad55eSGabriel Krisman Bertazi 	char *name;
1823c83ad55eSGabriel Krisman Bertazi 	char *version;
1824c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1825c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1826c83ad55eSGabriel Krisman Bertazi };
1827c83ad55eSGabriel Krisman Bertazi 
1828c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1829c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1830c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1831c83ad55eSGabriel Krisman Bertazi {
1832c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1833c83ad55eSGabriel Krisman Bertazi 	int i;
1834c83ad55eSGabriel Krisman Bertazi 
1835c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1836c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1837c83ad55eSGabriel Krisman Bertazi 			break;
1838c83ad55eSGabriel Krisman Bertazi 
1839c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1840c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1841c83ad55eSGabriel Krisman Bertazi 
1842c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1843c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1844c83ad55eSGabriel Krisman Bertazi 
1845c83ad55eSGabriel Krisman Bertazi 	return 0;
1846c83ad55eSGabriel Krisman Bertazi }
1847c83ad55eSGabriel Krisman Bertazi #endif
1848c83ad55eSGabriel Krisman Bertazi 
184926092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
185026092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
185126092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
185226092bf5STheodore Ts'o {
185326092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
185426092bf5STheodore Ts'o 	const struct mount_opts *m;
185508cefc7aSEric W. Biederman 	kuid_t uid;
185608cefc7aSEric W. Biederman 	kgid_t gid;
185726092bf5STheodore Ts'o 	int arg = 0;
185826092bf5STheodore Ts'o 
185957f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
186057f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
186157f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
186257f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
186357f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
186457f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
186557f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
186657f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
186757f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
186857f73c2cSTheodore Ts'o #endif
186926092bf5STheodore Ts'o 	switch (token) {
1870f7048605STheodore Ts'o 	case Opt_noacl:
1871f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1872f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1873f7048605STheodore Ts'o 		break;
187426092bf5STheodore Ts'o 	case Opt_sb:
187526092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
187626092bf5STheodore Ts'o 	case Opt_removed:
18775f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
187826092bf5STheodore Ts'o 		return 1;
187926092bf5STheodore Ts'o 	case Opt_abort:
188026092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
188126092bf5STheodore Ts'o 		return 1;
188226092bf5STheodore Ts'o 	case Opt_i_version:
1883357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
188426092bf5STheodore Ts'o 		return 1;
1885a26f4992STheodore Ts'o 	case Opt_lazytime:
18861751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1887a26f4992STheodore Ts'o 		return 1;
1888a26f4992STheodore Ts'o 	case Opt_nolazytime:
18891751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1890a26f4992STheodore Ts'o 		return 1;
189126092bf5STheodore Ts'o 	}
189226092bf5STheodore Ts'o 
18935f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
18945f3633e3SJan Kara 		if (token == m->token)
18955f3633e3SJan Kara 			break;
18965f3633e3SJan Kara 
18975f3633e3SJan Kara 	if (m->token == Opt_err) {
18985f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
18995f3633e3SJan Kara 			 "or missing value", opt);
19005f3633e3SJan Kara 		return -1;
19015f3633e3SJan Kara 	}
19025f3633e3SJan Kara 
19038dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
19048dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19058dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
19068dc0aa8cSTheodore Ts'o 		return -1;
19078dc0aa8cSTheodore Ts'o 	}
19088dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
19098dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19108dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
19118dc0aa8cSTheodore Ts'o 		return -1;
19128dc0aa8cSTheodore Ts'o 	}
19138dc0aa8cSTheodore Ts'o 
1914ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
19150efb3b23SJan Kara 		return -1;
191626092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
191726092bf5STheodore Ts'o 		return -1;
1918c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1919c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
192026092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
19211e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
19221e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1923c93cf2d7SDmitry Monakhov 		} else
1924c93cf2d7SDmitry Monakhov 			return -1;
1925c93cf2d7SDmitry Monakhov 	}
192626092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
192726092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
192826092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
192926092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
193026092bf5STheodore Ts'o 			 "options when quota turned on");
193126092bf5STheodore Ts'o 		return -1;
193226092bf5STheodore Ts'o 	}
193326092bf5STheodore Ts'o 
193426092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
193526092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
193626092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
193726092bf5STheodore Ts'o 		if (arg == 0)
193826092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
19399ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
19409ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
19419ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
19429ba55543Szhangyi (F) 				 "must be smaller than %d",
19439ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
19449ba55543Szhangyi (F) 			return -1;
19459ba55543Szhangyi (F) 		}
194626092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
1947670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
19489803387cSTheodore Ts'o 		if ((arg & 1) ||
19499803387cSTheodore Ts'o 		    (arg < 4) ||
19509803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
19519803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
19529803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
19539803387cSTheodore Ts'o 			return -1;
19549803387cSTheodore Ts'o 		}
1955670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
195626092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
195726092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
195826092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
195926092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
196026092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1961e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1962e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1963e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1964e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
196526092bf5STheodore Ts'o 			return -1;
196626092bf5STheodore Ts'o 		}
196726092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
196826092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
196926092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
197026092bf5STheodore Ts'o 		if (!args->from)
197126092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
197226092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1973df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1974df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
197526092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
197626092bf5STheodore Ts'o 		sbi->s_stripe = arg;
19770efb3b23SJan Kara 	} else if (token == Opt_resuid) {
19780efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
19790efb3b23SJan Kara 		if (!uid_valid(uid)) {
19805f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
19810efb3b23SJan Kara 			return -1;
19820efb3b23SJan Kara 		}
19830efb3b23SJan Kara 		sbi->s_resuid = uid;
19840efb3b23SJan Kara 	} else if (token == Opt_resgid) {
19850efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
19860efb3b23SJan Kara 		if (!gid_valid(gid)) {
19875f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
19880efb3b23SJan Kara 			return -1;
19890efb3b23SJan Kara 		}
19900efb3b23SJan Kara 		sbi->s_resgid = gid;
19910efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
19920efb3b23SJan Kara 		if (is_remount) {
19930efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
19940efb3b23SJan Kara 				 "Cannot specify journal on remount");
19950efb3b23SJan Kara 			return -1;
19960efb3b23SJan Kara 		}
19970efb3b23SJan Kara 		*journal_devnum = arg;
1998ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1999ad4eec61SEric Sandeen 		char *journal_path;
2000ad4eec61SEric Sandeen 		struct inode *journal_inode;
2001ad4eec61SEric Sandeen 		struct path path;
2002ad4eec61SEric Sandeen 		int error;
2003ad4eec61SEric Sandeen 
2004ad4eec61SEric Sandeen 		if (is_remount) {
2005ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2006ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2007ad4eec61SEric Sandeen 			return -1;
2008ad4eec61SEric Sandeen 		}
2009ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2010ad4eec61SEric Sandeen 		if (!journal_path) {
2011ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2012ad4eec61SEric Sandeen 				"journal device string");
2013ad4eec61SEric Sandeen 			return -1;
2014ad4eec61SEric Sandeen 		}
2015ad4eec61SEric Sandeen 
2016ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2017ad4eec61SEric Sandeen 		if (error) {
2018ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2019ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2020ad4eec61SEric Sandeen 			kfree(journal_path);
2021ad4eec61SEric Sandeen 			return -1;
2022ad4eec61SEric Sandeen 		}
2023ad4eec61SEric Sandeen 
20242b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2025ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2026ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2027ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2028ad4eec61SEric Sandeen 			path_put(&path);
2029ad4eec61SEric Sandeen 			kfree(journal_path);
2030ad4eec61SEric Sandeen 			return -1;
2031ad4eec61SEric Sandeen 		}
2032ad4eec61SEric Sandeen 
2033ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2034ad4eec61SEric Sandeen 		path_put(&path);
2035ad4eec61SEric Sandeen 		kfree(journal_path);
20360efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
20370efb3b23SJan Kara 		if (arg > 7) {
20385f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
20390efb3b23SJan Kara 				 " (must be 0-7)");
20400efb3b23SJan Kara 			return -1;
20410efb3b23SJan Kara 		}
20420efb3b23SJan Kara 		*journal_ioprio =
20430efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
20446ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2045643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
20466ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
20476ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
20486ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
20496ddb2447STheodore Ts'o #else
20506ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
20516ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
20526ddb2447STheodore Ts'o #endif
205326092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
205426092bf5STheodore Ts'o 		if (is_remount) {
205526092bf5STheodore Ts'o 			if (!sbi->s_journal)
205626092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
20575f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
205826092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
205926092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
206026092bf5STheodore Ts'o 				return -1;
206126092bf5STheodore Ts'o 			}
206226092bf5STheodore Ts'o 		} else {
206326092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
206426092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
206526092bf5STheodore Ts'o 		}
206626092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
206726092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
206826092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
206926092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
20705f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
20715f3633e3SJan Kara 				 "quota options when quota turned on");
207226092bf5STheodore Ts'o 			return -1;
207326092bf5STheodore Ts'o 		}
2074e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2075c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2076c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2077262b4662SJan Kara 				 "when QUOTA feature is enabled");
2078c325a67cSTheodore Ts'o 			return 1;
2079262b4662SJan Kara 		}
208026092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
208126092bf5STheodore Ts'o #endif
2082923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
2083ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2084ef83b6e8SDan Williams 		ext4_msg(sb, KERN_WARNING,
2085ef83b6e8SDan Williams 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2086ef83b6e8SDan Williams 		sbi->s_mount_opt |= m->mount_opt;
2087ef83b6e8SDan Williams #else
2088923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
2089923ae0ffSRoss Zwisler 		return -1;
2090923ae0ffSRoss Zwisler #endif
20917915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
20927915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
20937915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
20947915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
209526092bf5STheodore Ts'o 	} else {
209626092bf5STheodore Ts'o 		if (!args->from)
209726092bf5STheodore Ts'o 			arg = 1;
209826092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
209926092bf5STheodore Ts'o 			arg = !arg;
210026092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
210126092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
210226092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
210326092bf5STheodore Ts'o 			WARN_ON(1);
210426092bf5STheodore Ts'o 			return -1;
210526092bf5STheodore Ts'o 		}
210626092bf5STheodore Ts'o 		if (arg != 0)
210726092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
210826092bf5STheodore Ts'o 		else
210926092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
211026092bf5STheodore Ts'o 	}
211126092bf5STheodore Ts'o 	return 1;
211226092bf5STheodore Ts'o }
211326092bf5STheodore Ts'o 
2114ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2115c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2116b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2117661aa520SEric Sandeen 			 int is_remount)
2118ac27a0ecSDave Kleikamp {
21191e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
212033458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2121ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
212226092bf5STheodore Ts'o 	int token;
2123ac27a0ecSDave Kleikamp 
2124ac27a0ecSDave Kleikamp 	if (!options)
2125ac27a0ecSDave Kleikamp 		return 1;
2126ac27a0ecSDave Kleikamp 
2127ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2128ac27a0ecSDave Kleikamp 		if (!*p)
2129ac27a0ecSDave Kleikamp 			continue;
213015121c18SEric Sandeen 		/*
213115121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
213215121c18SEric Sandeen 		 * found; some options take optional arguments.
213315121c18SEric Sandeen 		 */
2134caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2135ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
213626092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
213726092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2138ac27a0ecSDave Kleikamp 			return 0;
2139ac27a0ecSDave Kleikamp 	}
2140ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
214149da9392SJan Kara 	/*
214249da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
214349da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
214449da9392SJan Kara 	 * to support legacy quotas in quota files.
214549da9392SJan Kara 	 */
214649da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
214749da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
214849da9392SJan Kara 			 "Cannot enable project quota enforcement.");
214949da9392SJan Kara 		return 0;
215049da9392SJan Kara 	}
215133458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
215233458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
215333458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
215433458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2155fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2156ac27a0ecSDave Kleikamp 
215733458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2158fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2159ac27a0ecSDave Kleikamp 
216056c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2161b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2162b31e1552SEric Sandeen 					"format mixing");
2163ac27a0ecSDave Kleikamp 			return 0;
2164ac27a0ecSDave Kleikamp 		}
2165ac27a0ecSDave Kleikamp 
2166ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2167b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2168b31e1552SEric Sandeen 					"not specified");
2169ac27a0ecSDave Kleikamp 			return 0;
2170ac27a0ecSDave Kleikamp 		}
2171ac27a0ecSDave Kleikamp 	}
2172ac27a0ecSDave Kleikamp #endif
2173ac27a0ecSDave Kleikamp 	return 1;
2174ac27a0ecSDave Kleikamp }
2175ac27a0ecSDave Kleikamp 
21762adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
21772adf6da8STheodore Ts'o 					   struct super_block *sb)
21782adf6da8STheodore Ts'o {
21792adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
21802adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
218133458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
21822adf6da8STheodore Ts'o 
21832adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
21842adf6da8STheodore Ts'o 		char *fmtname = "";
21852adf6da8STheodore Ts'o 
21862adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
21872adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
21882adf6da8STheodore Ts'o 			fmtname = "vfsold";
21892adf6da8STheodore Ts'o 			break;
21902adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
21912adf6da8STheodore Ts'o 			fmtname = "vfsv0";
21922adf6da8STheodore Ts'o 			break;
21932adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
21942adf6da8STheodore Ts'o 			fmtname = "vfsv1";
21952adf6da8STheodore Ts'o 			break;
21962adf6da8STheodore Ts'o 		}
21972adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
21982adf6da8STheodore Ts'o 	}
21992adf6da8STheodore Ts'o 
220033458eabSTheodore Ts'o 	rcu_read_lock();
220133458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
220233458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
220333458eabSTheodore Ts'o 	if (usr_qf_name)
220433458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
220533458eabSTheodore Ts'o 	if (grp_qf_name)
220633458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
220733458eabSTheodore Ts'o 	rcu_read_unlock();
22082adf6da8STheodore Ts'o #endif
22092adf6da8STheodore Ts'o }
22102adf6da8STheodore Ts'o 
22115a916be1STheodore Ts'o static const char *token2str(int token)
22125a916be1STheodore Ts'o {
221350df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
22145a916be1STheodore Ts'o 
22155a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
22165a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
22175a916be1STheodore Ts'o 			break;
22185a916be1STheodore Ts'o 	return t->pattern;
22195a916be1STheodore Ts'o }
22205a916be1STheodore Ts'o 
22212adf6da8STheodore Ts'o /*
22222adf6da8STheodore Ts'o  * Show an option if
22232adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
22242adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
22252adf6da8STheodore Ts'o  */
222666acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
222766acdcf4STheodore Ts'o 			      int nodefs)
22282adf6da8STheodore Ts'o {
22292adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
22302adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
223168afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
22325a916be1STheodore Ts'o 	const struct mount_opts *m;
223366acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
22342adf6da8STheodore Ts'o 
223566acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
223666acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
22372adf6da8STheodore Ts'o 
22382adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
22395a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
22405a916be1STheodore Ts'o 
22415a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
22425a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
22435a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
22445a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
22455a916be1STheodore Ts'o 			continue;
224668afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
22475a916be1STheodore Ts'o 			continue; /* skip if same as the default */
22485a916be1STheodore Ts'o 		if ((want_set &&
22495a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
22505a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
22515a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
22525a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
22535a916be1STheodore Ts'o 	}
22545a916be1STheodore Ts'o 
225508cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
22565a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
225708cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
225808cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
225908cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
22605a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
226108cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
226208cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
226366acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
22645a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
22655a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
22662adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
22675a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
22682adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
22695a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
227066acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
22715a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
227266acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
22735a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
227466acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
22755a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2276357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
22775a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
227866acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
22795a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
228068afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
228168afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
22822adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
22835a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
22842adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
22855a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
22862adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
22875a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
22885a916be1STheodore Ts'o 	}
228966acdcf4STheodore Ts'o 	if (nodefs ||
229066acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
22915a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
22922adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
22932adf6da8STheodore Ts'o 
2294ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
229566acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
22965a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2297df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2298df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
22997915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
23007915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2301338affb5SEric Biggers 	if (DUMMY_ENCRYPTION_ENABLED(sbi))
2302338affb5SEric Biggers 		SEQ_OPTS_PUTS("test_dummy_encryption");
23032adf6da8STheodore Ts'o 
23042adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
23052adf6da8STheodore Ts'o 	return 0;
23062adf6da8STheodore Ts'o }
23072adf6da8STheodore Ts'o 
230866acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
230966acdcf4STheodore Ts'o {
231066acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
231166acdcf4STheodore Ts'o }
231266acdcf4STheodore Ts'o 
2313ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
231466acdcf4STheodore Ts'o {
231566acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
231666acdcf4STheodore Ts'o 	int rc;
231766acdcf4STheodore Ts'o 
2318bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
231966acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
232066acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
232166acdcf4STheodore Ts'o 	return rc;
232266acdcf4STheodore Ts'o }
232366acdcf4STheodore Ts'o 
2324617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2325ac27a0ecSDave Kleikamp 			    int read_only)
2326ac27a0ecSDave Kleikamp {
2327617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2328c89128a0SJaegeuk Kim 	int err = 0;
2329ac27a0ecSDave Kleikamp 
2330617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2331b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2332b31e1552SEric Sandeen 			 "forcing read-only mode");
2333c89128a0SJaegeuk Kim 		err = -EROFS;
2334ac27a0ecSDave Kleikamp 	}
2335ac27a0ecSDave Kleikamp 	if (read_only)
2336281b5995STheodore Ts'o 		goto done;
2337617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2338b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2339b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2340c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2341b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2342b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2343b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2344ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2345ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2346ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2347b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2348b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2349b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2350ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
23516a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
23526a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2353b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2354b31e1552SEric Sandeen 			 "warning: checktime reached, "
2355b31e1552SEric Sandeen 			 "running e2fsck is recommended");
23560390131bSFrank Mayhar 	if (!sbi->s_journal)
2357216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2358ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2359617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2360e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
23616a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
23620390131bSFrank Mayhar 	if (sbi->s_journal)
2363e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2364ac27a0ecSDave Kleikamp 
2365c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2366281b5995STheodore Ts'o done:
2367ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2368a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2369a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2370ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2371ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2372617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2373617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2374a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2375ac27a0ecSDave Kleikamp 
23767abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2377c89128a0SJaegeuk Kim 	return err;
2378ac27a0ecSDave Kleikamp }
2379ac27a0ecSDave Kleikamp 
2380117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2381117fff10STheodore Ts'o {
2382117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2383117fff10STheodore Ts'o 	struct flex_groups *new_groups;
2384117fff10STheodore Ts'o 	int size;
2385117fff10STheodore Ts'o 
2386117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2387117fff10STheodore Ts'o 		return 0;
2388117fff10STheodore Ts'o 
2389117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2390117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2391117fff10STheodore Ts'o 		return 0;
2392117fff10STheodore Ts'o 
2393117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
2394a7c3e901SMichal Hocko 	new_groups = kvzalloc(size, GFP_KERNEL);
2395117fff10STheodore Ts'o 	if (!new_groups) {
2396117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
2397117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
2398117fff10STheodore Ts'o 		return -ENOMEM;
2399117fff10STheodore Ts'o 	}
2400117fff10STheodore Ts'o 
2401117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
2402117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
2403117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
2404117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
2405b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
2406117fff10STheodore Ts'o 	}
2407117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
2408117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
2409117fff10STheodore Ts'o 	return 0;
2410117fff10STheodore Ts'o }
2411117fff10STheodore Ts'o 
2412772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2413772cb7c8SJose R. Santos {
2414772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2415772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
2416772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2417117fff10STheodore Ts'o 	int i, err;
2418772cb7c8SJose R. Santos 
2419503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2420d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2421772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2422772cb7c8SJose R. Santos 		return 1;
2423772cb7c8SJose R. Santos 	}
2424772cb7c8SJose R. Santos 
2425117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2426117fff10STheodore Ts'o 	if (err)
2427772cb7c8SJose R. Santos 		goto failed;
2428772cb7c8SJose R. Santos 
2429772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
243088b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2431772cb7c8SJose R. Santos 
2432772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
24337ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
24347ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
243590ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
243624aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
24377ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
24387ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2439772cb7c8SJose R. Santos 	}
2440772cb7c8SJose R. Santos 
2441772cb7c8SJose R. Santos 	return 1;
2442772cb7c8SJose R. Santos failed:
2443772cb7c8SJose R. Santos 	return 0;
2444772cb7c8SJose R. Santos }
2445772cb7c8SJose R. Santos 
2446e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2447717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2448717d50e4SAndreas Dilger {
2449b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2450717d50e4SAndreas Dilger 	__u16 crc = 0;
2451717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2452e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2453717d50e4SAndreas Dilger 
24549aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2455feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2456feb0ab32SDarrick J. Wong 		__u32 csum32;
2457b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2458feb0ab32SDarrick J. Wong 
2459feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2460feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2461b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2462b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2463b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2464b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2465b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2466b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2467b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2468feb0ab32SDarrick J. Wong 
2469feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2470feb0ab32SDarrick J. Wong 		goto out;
2471feb0ab32SDarrick J. Wong 	}
2472feb0ab32SDarrick J. Wong 
2473feb0ab32SDarrick J. Wong 	/* old crc16 code */
2474e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2475813d32f9SDarrick J. Wong 		return 0;
2476813d32f9SDarrick J. Wong 
2477717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2478717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2479717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2480717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2481717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2482e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2483717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2484717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2485717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2486717d50e4SAndreas Dilger 				offset);
2487717d50e4SAndreas Dilger 
2488feb0ab32SDarrick J. Wong out:
2489717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2490717d50e4SAndreas Dilger }
2491717d50e4SAndreas Dilger 
2492feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2493717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2494717d50e4SAndreas Dilger {
2495feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2496e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2497717d50e4SAndreas Dilger 		return 0;
2498717d50e4SAndreas Dilger 
2499717d50e4SAndreas Dilger 	return 1;
2500717d50e4SAndreas Dilger }
2501717d50e4SAndreas Dilger 
2502feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2503feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2504feb0ab32SDarrick J. Wong {
2505feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2506feb0ab32SDarrick J. Wong 		return;
2507e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2508feb0ab32SDarrick J. Wong }
2509feb0ab32SDarrick J. Wong 
2510ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2511bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2512829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2513bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2514ac27a0ecSDave Kleikamp {
2515617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2516617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2517617ba13bSMingming Cao 	ext4_fsblk_t last_block;
251844de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2519bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2520bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2521bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2522ce421581SJose R. Santos 	int flexbg_flag = 0;
2523bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2524ac27a0ecSDave Kleikamp 
2525e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2526ce421581SJose R. Santos 		flexbg_flag = 1;
2527ce421581SJose R. Santos 
2528617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2529ac27a0ecSDave Kleikamp 
2530197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2531197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2532197cd65aSAkinobu Mita 
2533ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2534bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2535ac27a0ecSDave Kleikamp 		else
2536ac27a0ecSDave Kleikamp 			last_block = first_block +
2537617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2538ac27a0ecSDave Kleikamp 
2539bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2540bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2541bfff6873SLukas Czerner 			grp = i;
2542bfff6873SLukas Czerner 
25438fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2544829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2545829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2546829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2547829fa70dSTheodore Ts'o 				 "superblock", i);
254818db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
254918db4b4eSTheodore Ts'o 				return 0;
2550829fa70dSTheodore Ts'o 		}
255177260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
255277260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
255377260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
255477260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
255577260807STheodore Ts'o 				 "block group descriptors", i);
255677260807STheodore Ts'o 			if (!sb_rdonly(sb))
255777260807STheodore Ts'o 				return 0;
255877260807STheodore Ts'o 		}
25592b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2560b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2561a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2562b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2563ac27a0ecSDave Kleikamp 			return 0;
2564ac27a0ecSDave Kleikamp 		}
25658fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2566829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2567829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2568829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2569829fa70dSTheodore Ts'o 				 "superblock", i);
257018db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
257118db4b4eSTheodore Ts'o 				return 0;
2572829fa70dSTheodore Ts'o 		}
257377260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
257477260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
257577260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
257677260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
257777260807STheodore Ts'o 				 "block group descriptors", i);
257877260807STheodore Ts'o 			if (!sb_rdonly(sb))
257977260807STheodore Ts'o 				return 0;
258077260807STheodore Ts'o 		}
25812b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2582b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2583a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2584b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2585ac27a0ecSDave Kleikamp 			return 0;
2586ac27a0ecSDave Kleikamp 		}
25878fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2588829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2589829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2590829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2591829fa70dSTheodore Ts'o 				 "superblock", i);
259218db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
259318db4b4eSTheodore Ts'o 				return 0;
2594829fa70dSTheodore Ts'o 		}
259577260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
259677260807STheodore Ts'o 		    inode_table <= last_bg_block) {
259777260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
259877260807STheodore Ts'o 				 "Inode table for group %u overlaps "
259977260807STheodore Ts'o 				 "block group descriptors", i);
260077260807STheodore Ts'o 			if (!sb_rdonly(sb))
260177260807STheodore Ts'o 				return 0;
260277260807STheodore Ts'o 		}
2603bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
26042b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2605b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2606a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2607b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2608ac27a0ecSDave Kleikamp 			return 0;
2609ac27a0ecSDave Kleikamp 		}
2610955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2611feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2612b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2613b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2614e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2615fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2616bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2617955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2618717d50e4SAndreas Dilger 				return 0;
2619717d50e4SAndreas Dilger 			}
26207ee1ec4cSLi Zefan 		}
2621955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2622ce421581SJose R. Santos 		if (!flexbg_flag)
2623617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2624ac27a0ecSDave Kleikamp 	}
2625bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2626bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2627ac27a0ecSDave Kleikamp 	return 1;
2628ac27a0ecSDave Kleikamp }
2629ac27a0ecSDave Kleikamp 
2630617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2631ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2632ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2633ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2634ac27a0ecSDave Kleikamp  *
2635ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2636ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2637ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2638ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2639ac27a0ecSDave Kleikamp  *
2640ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2641ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2642ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2643617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2644ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2645ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2646ac27a0ecSDave Kleikamp  */
2647617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2648617ba13bSMingming Cao 				struct ext4_super_block *es)
2649ac27a0ecSDave Kleikamp {
2650ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
26512c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2652ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
265395f1fda4Szhangyi (F) 	int quota_update = 0;
2654ac27a0ecSDave Kleikamp 	int i;
2655ac27a0ecSDave Kleikamp #endif
2656ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2657ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2658ac27a0ecSDave Kleikamp 		return;
2659ac27a0ecSDave Kleikamp 	}
2660ac27a0ecSDave Kleikamp 
2661a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2662b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2663b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2664a8f48a95SEric Sandeen 		return;
2665a8f48a95SEric Sandeen 	}
2666a8f48a95SEric Sandeen 
2667d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2668d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2669d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2670d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2671d39195c3SAmir Goldstein 		return;
2672d39195c3SAmir Goldstein 	}
2673d39195c3SAmir Goldstein 
2674617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2675c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
26761751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
267784474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2678ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2679ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2680c25f9bc6SEric Sandeen 		}
2681ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2682ac27a0ecSDave Kleikamp 		return;
2683ac27a0ecSDave Kleikamp 	}
2684ac27a0ecSDave Kleikamp 
26851751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2686b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
26871751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2688ac27a0ecSDave Kleikamp 	}
2689ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2690ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
26911751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
269295f1fda4Szhangyi (F) 
269395f1fda4Szhangyi (F) 	/*
269495f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
269595f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
269695f1fda4Szhangyi (F) 	 */
26971751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
269895f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
269995f1fda4Szhangyi (F) 
270095f1fda4Szhangyi (F) 		if (!ret)
270195f1fda4Szhangyi (F) 			quota_update = 1;
270295f1fda4Szhangyi (F) 		else
270395f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
270495f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
270595f1fda4Szhangyi (F) 	}
270695f1fda4Szhangyi (F) 
270795f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2708a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2709617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2710617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
271195f1fda4Szhangyi (F) 
271295f1fda4Szhangyi (F) 			if (!ret)
271395f1fda4Szhangyi (F) 				quota_update = 1;
271495f1fda4Szhangyi (F) 			else
2715b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2716b31e1552SEric Sandeen 					"Cannot turn on journaled "
271795f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2718ac27a0ecSDave Kleikamp 		}
2719ac27a0ecSDave Kleikamp 	}
2720ac27a0ecSDave Kleikamp #endif
2721ac27a0ecSDave Kleikamp 
2722ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2723ac27a0ecSDave Kleikamp 		struct inode *inode;
2724ac27a0ecSDave Kleikamp 
2725c65d5c6cSVegard Nossum 		/*
2726c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2727c65d5c6cSVegard Nossum 		 * so, skip the rest.
2728c65d5c6cSVegard Nossum 		 */
2729c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2730c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2731c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2732c65d5c6cSVegard Nossum 			break;
2733c65d5c6cSVegard Nossum 		}
2734c65d5c6cSVegard Nossum 
273597bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
273697bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2737ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2738ac27a0ecSDave Kleikamp 			break;
2739ac27a0ecSDave Kleikamp 		}
2740ac27a0ecSDave Kleikamp 
2741617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2742871a2931SChristoph Hellwig 		dquot_initialize(inode);
2743ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2744566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2745b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2746b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
274746e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2748e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2749ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
27505955102cSAl Viro 			inode_lock(inode);
275155f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
27522c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
27532c98eb5eSTheodore Ts'o 			if (ret)
27542c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
27555955102cSAl Viro 			inode_unlock(inode);
2756ac27a0ecSDave Kleikamp 			nr_truncates++;
2757ac27a0ecSDave Kleikamp 		} else {
2758566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2759b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2760b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
276146e665e9SHarvey Harrison 					__func__, inode->i_ino);
2762ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2763ac27a0ecSDave Kleikamp 				  inode->i_ino);
2764ac27a0ecSDave Kleikamp 			nr_orphans++;
2765ac27a0ecSDave Kleikamp 		}
2766ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2767ac27a0ecSDave Kleikamp 	}
2768ac27a0ecSDave Kleikamp 
2769ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2770ac27a0ecSDave Kleikamp 
2771ac27a0ecSDave Kleikamp 	if (nr_orphans)
2772b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2773b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2774ac27a0ecSDave Kleikamp 	if (nr_truncates)
2775b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2776b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2777ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
277895f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
277995f1fda4Szhangyi (F) 	if (quota_update) {
2780a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
278195f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2782287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2783ac27a0ecSDave Kleikamp 		}
278495f1fda4Szhangyi (F) 	}
2785ac27a0ecSDave Kleikamp #endif
27861751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2787ac27a0ecSDave Kleikamp }
27880b8e58a1SAndreas Dilger 
2789cd2291a4SEric Sandeen /*
2790cd2291a4SEric Sandeen  * Maximal extent format file size.
2791cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2792cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2793cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2794cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2795cd2291a4SEric Sandeen  *
2796f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2797f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2798f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2799f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2800f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2801f17722f9SLukas Czerner  *
2802cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2803cd2291a4SEric Sandeen  */
2804f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2805cd2291a4SEric Sandeen {
2806cd2291a4SEric Sandeen 	loff_t res;
2807cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2808cd2291a4SEric Sandeen 
280972deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
281072deb455SChristoph Hellwig 
281172deb455SChristoph Hellwig 	if (!has_huge_files) {
2812cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2813cd2291a4SEric Sandeen 
2814cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2815cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2816cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2817cd2291a4SEric Sandeen 	}
2818cd2291a4SEric Sandeen 
2819f17722f9SLukas Czerner 	/*
2820f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2821f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2822f17722f9SLukas Czerner 	 * size
2823f17722f9SLukas Czerner 	 */
2824f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2825cd2291a4SEric Sandeen 	res <<= blkbits;
2826cd2291a4SEric Sandeen 
2827cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2828cd2291a4SEric Sandeen 	if (res > upper_limit)
2829cd2291a4SEric Sandeen 		res = upper_limit;
2830cd2291a4SEric Sandeen 
2831cd2291a4SEric Sandeen 	return res;
2832cd2291a4SEric Sandeen }
2833ac27a0ecSDave Kleikamp 
2834ac27a0ecSDave Kleikamp /*
2835cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
28360fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
28370fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2838ac27a0ecSDave Kleikamp  */
2839f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2840ac27a0ecSDave Kleikamp {
2841617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
28420fc1b451SAneesh Kumar K.V 	int meta_blocks;
28430fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
28440b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
28450b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
28460b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
28470b8e58a1SAndreas Dilger 	 *
28480b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
28490b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
28500fc1b451SAneesh Kumar K.V 	 */
28510fc1b451SAneesh Kumar K.V 
285272deb455SChristoph Hellwig 	if (!has_huge_files) {
28530fc1b451SAneesh Kumar K.V 		/*
285472deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
285572deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
285672deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
28570fc1b451SAneesh Kumar K.V 		 */
28580fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
28590fc1b451SAneesh Kumar K.V 
28600fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
28610fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
28620fc1b451SAneesh Kumar K.V 
28630fc1b451SAneesh Kumar K.V 	} else {
28648180a562SAneesh Kumar K.V 		/*
28658180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
28668180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
28678180a562SAneesh Kumar K.V 		 * represent total number of blocks in
28688180a562SAneesh Kumar K.V 		 * file system block size
28698180a562SAneesh Kumar K.V 		 */
28700fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
28710fc1b451SAneesh Kumar K.V 
28720fc1b451SAneesh Kumar K.V 	}
28730fc1b451SAneesh Kumar K.V 
28740fc1b451SAneesh Kumar K.V 	/* indirect blocks */
28750fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
28760fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
28770fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
28780fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
28790fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
28800fc1b451SAneesh Kumar K.V 
28810fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
28820fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2883ac27a0ecSDave Kleikamp 
2884ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2885ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2886ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2887ac27a0ecSDave Kleikamp 	res <<= bits;
2888ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2889ac27a0ecSDave Kleikamp 		res = upper_limit;
28900fc1b451SAneesh Kumar K.V 
28910fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
28920fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
28930fc1b451SAneesh Kumar K.V 
2894ac27a0ecSDave Kleikamp 	return res;
2895ac27a0ecSDave Kleikamp }
2896ac27a0ecSDave Kleikamp 
2897617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
289870bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2899ac27a0ecSDave Kleikamp {
2900617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2901fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2902ac27a0ecSDave Kleikamp 	int has_super = 0;
2903ac27a0ecSDave Kleikamp 
2904ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2905ac27a0ecSDave Kleikamp 
2906e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
290770bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2908ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2909617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2910ac27a0ecSDave Kleikamp 		has_super = 1;
29110b8e58a1SAndreas Dilger 
2912bd63f6b0SDarrick J. Wong 	/*
2913bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2914bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2915bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2916bd63f6b0SDarrick J. Wong 	 * compensate.
2917bd63f6b0SDarrick J. Wong 	 */
2918bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
291949598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
2920bd63f6b0SDarrick J. Wong 		has_super++;
2921bd63f6b0SDarrick J. Wong 
2922617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2923ac27a0ecSDave Kleikamp }
2924ac27a0ecSDave Kleikamp 
2925c9de560dSAlex Tomas /**
2926c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2927c9de560dSAlex Tomas  * @sbi: In memory super block info
2928c9de560dSAlex Tomas  *
2929c9de560dSAlex Tomas  * If we have specified it via mount option, then
2930c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2931c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2932c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2933c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2934c9de560dSAlex Tomas  *
2935c9de560dSAlex Tomas  */
2936c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2937c9de560dSAlex Tomas {
2938c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2939c9de560dSAlex Tomas 	unsigned long stripe_width =
2940c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
29413eb08658SDan Ehrenberg 	int ret;
2942c9de560dSAlex Tomas 
2943c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
29443eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
29455469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
29463eb08658SDan Ehrenberg 		ret = stripe_width;
29475469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
29483eb08658SDan Ehrenberg 		ret = stride;
29493eb08658SDan Ehrenberg 	else
29503eb08658SDan Ehrenberg 		ret = 0;
2951c9de560dSAlex Tomas 
29523eb08658SDan Ehrenberg 	/*
29533eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
29543eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
29553eb08658SDan Ehrenberg 	 */
29563eb08658SDan Ehrenberg 	if (ret <= 1)
29573eb08658SDan Ehrenberg 		ret = 0;
2958c9de560dSAlex Tomas 
29593eb08658SDan Ehrenberg 	return ret;
2960c9de560dSAlex Tomas }
2961ac27a0ecSDave Kleikamp 
2962a13fb1a4SEric Sandeen /*
2963a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2964a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2965a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2966a13fb1a4SEric Sandeen  * 0 if it cannot be.
2967a13fb1a4SEric Sandeen  */
2968a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2969a13fb1a4SEric Sandeen {
2970e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2971a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2972a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2973a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2974a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2975a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2976a13fb1a4SEric Sandeen 		return 0;
2977a13fb1a4SEric Sandeen 	}
2978a13fb1a4SEric Sandeen 
2979c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
2980c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
2981c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
2982c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
2983c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
2984c83ad55eSGabriel Krisman Bertazi 		return 0;
2985c83ad55eSGabriel Krisman Bertazi 	}
2986c83ad55eSGabriel Krisman Bertazi #endif
2987c83ad55eSGabriel Krisman Bertazi 
2988a13fb1a4SEric Sandeen 	if (readonly)
2989a13fb1a4SEric Sandeen 		return 1;
2990a13fb1a4SEric Sandeen 
2991e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
29922cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
29931751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
29942cb5cc8bSDarrick J. Wong 		return 1;
29952cb5cc8bSDarrick J. Wong 	}
29962cb5cc8bSDarrick J. Wong 
2997a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2998e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
2999a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3000a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3001a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3002a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3003a13fb1a4SEric Sandeen 		return 0;
3004a13fb1a4SEric Sandeen 	}
3005e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3006bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3007bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3008bab08ab9STheodore Ts'o 			 "extents feature\n");
3009bab08ab9STheodore Ts'o 		return 0;
3010bab08ab9STheodore Ts'o 	}
30117c319d32SAditya Kali 
30127c319d32SAditya Kali #ifndef CONFIG_QUOTA
3013e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !readonly) {
30147c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
30157c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
30167c319d32SAditya Kali 			 "without CONFIG_QUOTA");
30177c319d32SAditya Kali 		return 0;
30187c319d32SAditya Kali 	}
3019689c958cSLi Xi 	if (ext4_has_feature_project(sb) && !readonly) {
3020689c958cSLi Xi 		ext4_msg(sb, KERN_ERR,
3021689c958cSLi Xi 			 "Filesystem with project quota feature cannot be mounted RDWR "
3022689c958cSLi Xi 			 "without CONFIG_QUOTA");
3023689c958cSLi Xi 		return 0;
3024689c958cSLi Xi 	}
30257c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3026a13fb1a4SEric Sandeen 	return 1;
3027a13fb1a4SEric Sandeen }
3028a13fb1a4SEric Sandeen 
302966e61a9eSTheodore Ts'o /*
303066e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
303166e61a9eSTheodore Ts'o  * on the file system
303266e61a9eSTheodore Ts'o  */
3033235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
303466e61a9eSTheodore Ts'o {
3035235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3036235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3037235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
303866e61a9eSTheodore Ts'o 
303966e61a9eSTheodore Ts'o 	if (es->s_error_count)
3040ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3041ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
304266e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
304366e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
30446a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
30456a0678a7SArnd Bergmann 		       sb->s_id,
30466a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
304766e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
304866e61a9eSTheodore Ts'o 		       es->s_first_error_func,
304966e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
305066e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3051651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
305266e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
305366e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3054651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
305566e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3056651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
305766e61a9eSTheodore Ts'o 	}
305866e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
30596a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
30606a0678a7SArnd Bergmann 		       sb->s_id,
30616a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
306266e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
306366e61a9eSTheodore Ts'o 		       es->s_last_error_func,
306466e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
306566e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3066651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
306766e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
306866e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3069651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
307066e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3071651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
307266e61a9eSTheodore Ts'o 	}
307366e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
307466e61a9eSTheodore Ts'o }
307566e61a9eSTheodore Ts'o 
3076bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3077bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3078bfff6873SLukas Czerner {
3079bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3080bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3081bfff6873SLukas Czerner 	struct super_block *sb;
3082bfff6873SLukas Czerner 	unsigned long timeout = 0;
3083bfff6873SLukas Czerner 	int ret = 0;
3084bfff6873SLukas Czerner 
3085bfff6873SLukas Czerner 	sb = elr->lr_super;
3086bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3087bfff6873SLukas Czerner 
3088bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3089bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3090bfff6873SLukas Czerner 		if (!gdp) {
3091bfff6873SLukas Czerner 			ret = 1;
3092bfff6873SLukas Czerner 			break;
3093bfff6873SLukas Czerner 		}
3094bfff6873SLukas Czerner 
3095bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3096bfff6873SLukas Czerner 			break;
3097bfff6873SLukas Czerner 	}
3098bfff6873SLukas Czerner 
30997f511862STheodore Ts'o 	if (group >= ngroups)
3100bfff6873SLukas Czerner 		ret = 1;
3101bfff6873SLukas Czerner 
3102bfff6873SLukas Czerner 	if (!ret) {
3103bfff6873SLukas Czerner 		timeout = jiffies;
3104bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3105bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3106bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
310751ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
310851ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3109bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3110bfff6873SLukas Czerner 		}
3111bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3112bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3113bfff6873SLukas Czerner 	}
3114bfff6873SLukas Czerner 	return ret;
3115bfff6873SLukas Czerner }
3116bfff6873SLukas Czerner 
3117bfff6873SLukas Czerner /*
3118bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
31194ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3120bfff6873SLukas Czerner  */
3121bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3122bfff6873SLukas Czerner {
3123bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3124bfff6873SLukas Czerner 
3125bfff6873SLukas Czerner 	if (!elr)
3126bfff6873SLukas Czerner 		return;
3127bfff6873SLukas Czerner 
3128bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3129bfff6873SLukas Czerner 
3130bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3131bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3132bfff6873SLukas Czerner 	kfree(elr);
3133bfff6873SLukas Czerner }
3134bfff6873SLukas Czerner 
3135bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3136bfff6873SLukas Czerner {
31371bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
31381bb933fbSLukas Czerner 	if (!ext4_li_info) {
31391bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3140bfff6873SLukas Czerner 		return;
31411bb933fbSLukas Czerner 	}
3142bfff6873SLukas Czerner 
3143bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
31441bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3145bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
31461bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3147bfff6873SLukas Czerner }
3148bfff6873SLukas Czerner 
31498f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
31508f1f7453SEric Sandeen 
3151bfff6873SLukas Czerner /*
3152bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3153bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3154bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3155bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3156bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3157bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3158bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3159bfff6873SLukas Czerner  */
3160bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3161bfff6873SLukas Czerner {
3162bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3163bfff6873SLukas Czerner 	struct list_head *pos, *n;
3164bfff6873SLukas Czerner 	struct ext4_li_request *elr;
31654ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3166bfff6873SLukas Czerner 
3167bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3168bfff6873SLukas Czerner 
3169bfff6873SLukas Czerner cont_thread:
3170bfff6873SLukas Czerner 	while (true) {
3171bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3172bfff6873SLukas Czerner 
3173bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3174bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3175bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3176bfff6873SLukas Czerner 			goto exit_thread;
3177bfff6873SLukas Czerner 		}
3178bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3179e22834f0SDmitry Monakhov 			int err = 0;
3180e22834f0SDmitry Monakhov 			int progress = 0;
3181bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3182bfff6873SLukas Czerner 					 lr_request);
3183bfff6873SLukas Czerner 
3184e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3185e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3186e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3187e22834f0SDmitry Monakhov 				continue;
3188e22834f0SDmitry Monakhov 			}
3189e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3190e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3191e22834f0SDmitry Monakhov 					progress = 1;
3192e22834f0SDmitry Monakhov 					/*
3193e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3194e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3195e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3196e22834f0SDmitry Monakhov 					 */
3197e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3198e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3199e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3200e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3201e22834f0SDmitry Monakhov 					n = pos->next;
3202e22834f0SDmitry Monakhov 				}
3203e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3204e22834f0SDmitry Monakhov 			}
3205b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3206e22834f0SDmitry Monakhov 			if (err) {
3207bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3208bfff6873SLukas Czerner 				continue;
3209bfff6873SLukas Czerner 			}
3210e22834f0SDmitry Monakhov 			if (!progress) {
3211e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3212e22834f0SDmitry Monakhov 					(prandom_u32()
3213e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3214b2c78cd0STheodore Ts'o 			}
3215bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3216bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3217bfff6873SLukas Czerner 		}
3218bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3219bfff6873SLukas Czerner 
3220a0acae0eSTejun Heo 		try_to_freeze();
3221bfff6873SLukas Czerner 
32224ed5c033SLukas Czerner 		cur = jiffies;
32234ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3224f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3225bfff6873SLukas Czerner 			cond_resched();
3226bfff6873SLukas Czerner 			continue;
3227bfff6873SLukas Czerner 		}
3228bfff6873SLukas Czerner 
32294ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
32304ed5c033SLukas Czerner 
32318f1f7453SEric Sandeen 		if (kthread_should_stop()) {
32328f1f7453SEric Sandeen 			ext4_clear_request_list();
32338f1f7453SEric Sandeen 			goto exit_thread;
32348f1f7453SEric Sandeen 		}
3235bfff6873SLukas Czerner 	}
3236bfff6873SLukas Czerner 
3237bfff6873SLukas Czerner exit_thread:
3238bfff6873SLukas Czerner 	/*
3239bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3240bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3241bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3242bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3243bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3244bfff6873SLukas Czerner 	 * new one.
3245bfff6873SLukas Czerner 	 */
3246bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3247bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3248bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3249bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3250bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3251bfff6873SLukas Czerner 		goto cont_thread;
3252bfff6873SLukas Czerner 	}
3253bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3254bfff6873SLukas Czerner 	kfree(ext4_li_info);
3255bfff6873SLukas Czerner 	ext4_li_info = NULL;
3256bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3257bfff6873SLukas Czerner 
3258bfff6873SLukas Czerner 	return 0;
3259bfff6873SLukas Czerner }
3260bfff6873SLukas Czerner 
3261bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3262bfff6873SLukas Czerner {
3263bfff6873SLukas Czerner 	struct list_head *pos, *n;
3264bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3265bfff6873SLukas Czerner 
3266bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3267bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3268bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3269bfff6873SLukas Czerner 				 lr_request);
3270bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3271bfff6873SLukas Czerner 	}
3272bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3273bfff6873SLukas Czerner }
3274bfff6873SLukas Czerner 
3275bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3276bfff6873SLukas Czerner {
32778f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
32788f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
32798f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
32808f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3281bfff6873SLukas Czerner 		ext4_clear_request_list();
3282bfff6873SLukas Czerner 		kfree(ext4_li_info);
3283bfff6873SLukas Czerner 		ext4_li_info = NULL;
328492b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3285bfff6873SLukas Czerner 				 "initialization thread\n",
3286bfff6873SLukas Czerner 				 err);
3287bfff6873SLukas Czerner 		return err;
3288bfff6873SLukas Czerner 	}
3289bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3290bfff6873SLukas Czerner 	return 0;
3291bfff6873SLukas Czerner }
3292bfff6873SLukas Czerner 
3293bfff6873SLukas Czerner /*
3294bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3295bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3296bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3297bfff6873SLukas Czerner  * groups and return total number of groups.
3298bfff6873SLukas Czerner  */
3299bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3300bfff6873SLukas Czerner {
3301bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3302bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3303bfff6873SLukas Czerner 
33048844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
33058844618dSTheodore Ts'o 		return ngroups;
33068844618dSTheodore Ts'o 
3307bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3308bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3309bfff6873SLukas Czerner 		if (!gdp)
3310bfff6873SLukas Czerner 			continue;
3311bfff6873SLukas Czerner 
331250122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3313bfff6873SLukas Czerner 			break;
3314bfff6873SLukas Czerner 	}
3315bfff6873SLukas Czerner 
3316bfff6873SLukas Czerner 	return group;
3317bfff6873SLukas Czerner }
3318bfff6873SLukas Czerner 
3319bfff6873SLukas Czerner static int ext4_li_info_new(void)
3320bfff6873SLukas Czerner {
3321bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3322bfff6873SLukas Czerner 
3323bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3324bfff6873SLukas Czerner 	if (!eli)
3325bfff6873SLukas Czerner 		return -ENOMEM;
3326bfff6873SLukas Czerner 
3327bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3328bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3329bfff6873SLukas Czerner 
3330bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3331bfff6873SLukas Czerner 
3332bfff6873SLukas Czerner 	ext4_li_info = eli;
3333bfff6873SLukas Czerner 
3334bfff6873SLukas Czerner 	return 0;
3335bfff6873SLukas Czerner }
3336bfff6873SLukas Czerner 
3337bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3338bfff6873SLukas Czerner 					    ext4_group_t start)
3339bfff6873SLukas Czerner {
3340bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3341bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3342bfff6873SLukas Czerner 
3343bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3344bfff6873SLukas Czerner 	if (!elr)
3345bfff6873SLukas Czerner 		return NULL;
3346bfff6873SLukas Czerner 
3347bfff6873SLukas Czerner 	elr->lr_super = sb;
3348bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3349bfff6873SLukas Czerner 	elr->lr_next_group = start;
3350bfff6873SLukas Czerner 
3351bfff6873SLukas Czerner 	/*
3352bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3353bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3354bfff6873SLukas Czerner 	 * better.
3355bfff6873SLukas Czerner 	 */
3356dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3357dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3358bfff6873SLukas Czerner 	return elr;
3359bfff6873SLukas Czerner }
3360bfff6873SLukas Czerner 
33617f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3362bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3363bfff6873SLukas Czerner {
3364bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
33657f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
336649598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
33676c5a6cb9SAndrew Morton 	int ret = 0;
3368bfff6873SLukas Czerner 
33697f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
337051ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
337151ce6511SLukas Czerner 		/*
337251ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
337351ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
337451ce6511SLukas Czerner 		 */
337551ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
33767f511862STheodore Ts'o 		goto out;
337751ce6511SLukas Czerner 	}
3378bfff6873SLukas Czerner 
3379bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
338055ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
33817f511862STheodore Ts'o 		goto out;
3382bfff6873SLukas Czerner 
3383bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
33847f511862STheodore Ts'o 	if (!elr) {
33857f511862STheodore Ts'o 		ret = -ENOMEM;
33867f511862STheodore Ts'o 		goto out;
33877f511862STheodore Ts'o 	}
3388bfff6873SLukas Czerner 
3389bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3390bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3391bfff6873SLukas Czerner 		if (ret)
3392bfff6873SLukas Czerner 			goto out;
3393bfff6873SLukas Czerner 	}
3394bfff6873SLukas Czerner 
3395bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3396bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3397bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3398bfff6873SLukas Czerner 
3399bfff6873SLukas Czerner 	sbi->s_li_request = elr;
340046e4690bSTao Ma 	/*
340146e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
340246e4690bSTao Ma 	 * the request_list and the removal and free of it is
340346e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
340446e4690bSTao Ma 	 */
340546e4690bSTao Ma 	elr = NULL;
3406bfff6873SLukas Czerner 
3407bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3408bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3409bfff6873SLukas Czerner 		if (ret)
3410bfff6873SLukas Czerner 			goto out;
3411bfff6873SLukas Czerner 	}
3412bfff6873SLukas Czerner out:
3413bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3414beed5ecbSNicolas Kaiser 	if (ret)
3415bfff6873SLukas Czerner 		kfree(elr);
3416bfff6873SLukas Czerner 	return ret;
3417bfff6873SLukas Czerner }
3418bfff6873SLukas Czerner 
3419bfff6873SLukas Czerner /*
3420bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3421bfff6873SLukas Czerner  * module unload.
3422bfff6873SLukas Czerner  */
3423bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3424bfff6873SLukas Czerner {
3425bfff6873SLukas Czerner 	/*
3426bfff6873SLukas Czerner 	 * If thread exited earlier
3427bfff6873SLukas Czerner 	 * there's nothing to be done.
3428bfff6873SLukas Czerner 	 */
34298f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3430bfff6873SLukas Czerner 		return;
3431bfff6873SLukas Czerner 
34328f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3433bfff6873SLukas Czerner }
3434bfff6873SLukas Czerner 
343525ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
343625ed6e8aSDarrick J. Wong {
343725ed6e8aSDarrick J. Wong 	int ret = 1;
343825ed6e8aSDarrick J. Wong 	int compat, incompat;
343925ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
344025ed6e8aSDarrick J. Wong 
34419aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3442db9ee220SDarrick J. Wong 		/* journal checksum v3 */
344325ed6e8aSDarrick J. Wong 		compat = 0;
3444db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
344525ed6e8aSDarrick J. Wong 	} else {
344625ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
344725ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
344825ed6e8aSDarrick J. Wong 		incompat = 0;
344925ed6e8aSDarrick J. Wong 	}
345025ed6e8aSDarrick J. Wong 
3451feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3452feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3453feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3454feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
345525ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
345625ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
345725ed6e8aSDarrick J. Wong 				compat, 0,
345825ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
345925ed6e8aSDarrick J. Wong 				incompat);
346025ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
346125ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
346225ed6e8aSDarrick J. Wong 				compat, 0,
346325ed6e8aSDarrick J. Wong 				incompat);
346425ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
346525ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
346625ed6e8aSDarrick J. Wong 	} else {
3467feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3468feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
346925ed6e8aSDarrick J. Wong 	}
347025ed6e8aSDarrick J. Wong 
347125ed6e8aSDarrick J. Wong 	return ret;
347225ed6e8aSDarrick J. Wong }
347325ed6e8aSDarrick J. Wong 
3474952fc18eSTheodore Ts'o /*
3475952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3476952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3477952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3478952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3479952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3480952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3481952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3482952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3483952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3484952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3485952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3486952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3487952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3488952fc18eSTheodore Ts'o  */
3489952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3490952fc18eSTheodore Ts'o 			  char *buf)
3491952fc18eSTheodore Ts'o {
3492952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3493952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3494952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3495952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3496952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3497952fc18eSTheodore Ts'o 
3498e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
34990548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
35000548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
35010548bbb8STheodore Ts'o 
3502952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3503952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3504952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3505952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3506952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3507952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3508952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3509952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3510952fc18eSTheodore Ts'o 			count++;
3511952fc18eSTheodore Ts'o 		}
3512952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3513952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3514952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3515952fc18eSTheodore Ts'o 			count++;
3516952fc18eSTheodore Ts'o 		}
3517952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3518952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3519952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3520952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3521952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3522952fc18eSTheodore Ts'o 				count++;
3523952fc18eSTheodore Ts'o 			}
3524952fc18eSTheodore Ts'o 		if (i != grp)
3525952fc18eSTheodore Ts'o 			continue;
3526952fc18eSTheodore Ts'o 		s = 0;
3527952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3528952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3529952fc18eSTheodore Ts'o 			count++;
3530952fc18eSTheodore Ts'o 		}
3531c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3532c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3533c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3534c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3535c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3536952fc18eSTheodore Ts'o 		}
3537c48ae41bSTheodore Ts'o 		count += j;
3538c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3539c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3540952fc18eSTheodore Ts'o 	}
3541952fc18eSTheodore Ts'o 	if (!count)
3542952fc18eSTheodore Ts'o 		return 0;
3543952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3544952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3545952fc18eSTheodore Ts'o }
3546952fc18eSTheodore Ts'o 
3547952fc18eSTheodore Ts'o /*
3548952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3549952fc18eSTheodore Ts'o  */
3550952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3551952fc18eSTheodore Ts'o {
3552952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3553952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
35543c816dedSEric Whitney 	struct inode *j_inode;
35553c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3556952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3557952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
35584fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3559952fc18eSTheodore Ts'o 
3560952fc18eSTheodore Ts'o 	if (!buf)
3561952fc18eSTheodore Ts'o 		return -ENOMEM;
3562952fc18eSTheodore Ts'o 
3563952fc18eSTheodore Ts'o 	/*
3564952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3565952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3566952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3567952fc18eSTheodore Ts'o 	 */
3568952fc18eSTheodore Ts'o 
3569952fc18eSTheodore Ts'o 	/*
3570952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3571952fc18eSTheodore Ts'o 	 */
3572952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3573952fc18eSTheodore Ts'o 
3574952fc18eSTheodore Ts'o 	/*
3575952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3576952fc18eSTheodore Ts'o 	 */
3577952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3578952fc18eSTheodore Ts'o 		int blks;
3579952fc18eSTheodore Ts'o 
3580952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3581952fc18eSTheodore Ts'o 		overhead += blks;
3582952fc18eSTheodore Ts'o 		if (blks)
3583952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3584952fc18eSTheodore Ts'o 		cond_resched();
3585952fc18eSTheodore Ts'o 	}
35863c816dedSEric Whitney 
35873c816dedSEric Whitney 	/*
35883c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
35893c816dedSEric Whitney 	 * loaded or not
35903c816dedSEric Whitney 	 */
3591b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3592810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
35933c816dedSEric Whitney 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal) {
35943c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
35953c816dedSEric Whitney 		if (j_inode) {
35963c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
35973c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
35983c816dedSEric Whitney 			iput(j_inode);
35993c816dedSEric Whitney 		} else {
36003c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
36013c816dedSEric Whitney 		}
36023c816dedSEric Whitney 	}
3603952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3604952fc18eSTheodore Ts'o 	smp_wmb();
3605952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3606952fc18eSTheodore Ts'o 	return 0;
3607952fc18eSTheodore Ts'o }
3608952fc18eSTheodore Ts'o 
3609b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
361027dd4385SLukas Czerner {
361127dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3612b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
361327dd4385SLukas Czerner 
361427dd4385SLukas Czerner 	/*
361530fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
361630fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
361730fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
361830fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
361930fac0f7SJan Kara 	 */
3620e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3621b5799018STheodore Ts'o 		return;
362230fac0f7SJan Kara 	/*
362327dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
362427dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
362527dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3626556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
362727dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
362827dd4385SLukas Czerner 	 * very rare.
362927dd4385SLukas Czerner 	 */
3630b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3631b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
363227dd4385SLukas Czerner 
363327dd4385SLukas Czerner 	do_div(resv_clusters, 50);
363427dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
363527dd4385SLukas Czerner 
3636b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
363727dd4385SLukas Czerner }
363827dd4385SLukas Czerner 
3639617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3640ac27a0ecSDave Kleikamp {
36415e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3642d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3643ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3644617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
36455aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3646617ba13bSMingming Cao 	ext4_fsblk_t block;
3647617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
364870bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3649ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3650ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3651ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3652ac27a0ecSDave Kleikamp 	struct inode *root;
36530390131bSFrank Mayhar 	const char *descr;
3654dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3655281b5995STheodore Ts'o 	int blocksize, clustersize;
36564ec11028STheodore Ts'o 	unsigned int db_count;
36574ec11028STheodore Ts'o 	unsigned int i;
3658281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3659bd81d8eeSLaurent Vivier 	__u64 blocks_count;
366007aa2ea1SLukas Czerner 	int err = 0;
3661b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3662bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3663ac27a0ecSDave Kleikamp 
36645aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
36655aee0f8aSTheodore Ts'o 		goto out_free_base;
3666705895b6SPekka Enberg 
3667aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3668705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3669705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
36705aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
36715aee0f8aSTheodore Ts'o 		goto out_free_base;
36725aee0f8aSTheodore Ts'o 
3673ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
36742c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3675240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3676d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3677f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3678f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3679dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3680ac27a0ecSDave Kleikamp 
36819f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3682ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
36839f6200bbSTheodore Ts'o 
368407aa2ea1SLukas Czerner 	/* -EINVAL is default */
3685dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3686617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3687ac27a0ecSDave Kleikamp 	if (!blocksize) {
3688b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3689ac27a0ecSDave Kleikamp 		goto out_fail;
3690ac27a0ecSDave Kleikamp 	}
3691ac27a0ecSDave Kleikamp 
3692ac27a0ecSDave Kleikamp 	/*
3693617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3694ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3695ac27a0ecSDave Kleikamp 	 */
3696617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
369770bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
369870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3699ac27a0ecSDave Kleikamp 	} else {
370070bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3701ac27a0ecSDave Kleikamp 	}
3702ac27a0ecSDave Kleikamp 
3703a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3704b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3705ac27a0ecSDave Kleikamp 		goto out_fail;
3706ac27a0ecSDave Kleikamp 	}
3707ac27a0ecSDave Kleikamp 	/*
3708ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3709617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3710ac27a0ecSDave Kleikamp 	 */
37112716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3712ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3713ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3714617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3715617ba13bSMingming Cao 		goto cantfind_ext4;
3716afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3717ac27a0ecSDave Kleikamp 
3718feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3719e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3720e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3721363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3722feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3723feb0ab32SDarrick J. Wong 
3724d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3725d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3726d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3727d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3728d25425f8SDarrick J. Wong 		silent = 1;
3729d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3730d25425f8SDarrick J. Wong 	}
3731d25425f8SDarrick J. Wong 
37320441984aSDarrick J. Wong 	/* Load the checksum driver */
37330441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
37340441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
37350441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
37360441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
37370441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
37380441984aSDarrick J. Wong 		goto failed_mount;
37390441984aSDarrick J. Wong 	}
37400441984aSDarrick J. Wong 
3741a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3742a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3743a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3744a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3745a9c47317SDarrick J. Wong 		silent = 1;
37466a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3747a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3748a9c47317SDarrick J. Wong 	}
3749a9c47317SDarrick J. Wong 
3750a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3751e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
37528c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3753dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3754a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3755a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3756a9c47317SDarrick J. Wong 
3757ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3758ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3759fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3760617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3761fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
376287f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3763fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3764617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3765fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3766ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3767fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
3768*244adf64STheodore Ts'o 	set_opt(sb, DIOREAD_NOLOCK);
376903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3770fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
37712e7842b8SHugh Dickins #endif
377298c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
377398c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
377498c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
377598c1a759SDarrick J. Wong 
3776617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3777fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3778617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3779fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3780617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3781fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3782ac27a0ecSDave Kleikamp 
3783617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3784fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3785bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3786fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3787bb4f397aSAneesh Kumar K.V 	else
3788fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
378945f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3790fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
37918b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3792fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3793ac27a0ecSDave Kleikamp 
379408cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
379508cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
379630773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
379730773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
379830773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3799ac27a0ecSDave Kleikamp 
38008b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3801fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3802ac27a0ecSDave Kleikamp 
38031e2462f9SMingming Cao 	/*
3804dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3805dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3806dd919b98SAneesh Kumar K.V 	 */
3807bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
38088b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3809fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3810dd919b98SAneesh Kumar K.V 
381151ce6511SLukas Czerner 	/*
381251ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
381351ce6511SLukas Czerner 	 * no mount option specified.
381451ce6511SLukas Czerner 	 */
381551ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
381651ce6511SLukas Czerner 
38179803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
38189803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
38199803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
38209803387cSTheodore Ts'o 	} else {
38219803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
38229803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
38239803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
38249803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
38259803387cSTheodore Ts'o 				 sbi->s_first_ino);
38269803387cSTheodore Ts'o 			goto failed_mount;
38279803387cSTheodore Ts'o 		}
38289803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
38299803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
38309803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
38319803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
38329803387cSTheodore Ts'o 			       "unsupported inode size: %d",
38339803387cSTheodore Ts'o 			       sbi->s_inode_size);
38349803387cSTheodore Ts'o 			goto failed_mount;
38359803387cSTheodore Ts'o 		}
38369803387cSTheodore Ts'o 		/*
38379803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
38389803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
38399803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
38409803387cSTheodore Ts'o 		 * for all three.
38419803387cSTheodore Ts'o 		 */
38429803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
38439803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
38449803387cSTheodore Ts'o 			sb->s_time_gran = 1;
38459803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
38469803387cSTheodore Ts'o 		} else {
38479803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
38489803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
38499803387cSTheodore Ts'o 		}
38509803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
38519803387cSTheodore Ts'o 	}
38529803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
38539803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
38549803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
38559803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
38569803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
38579803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
38589803387cSTheodore Ts'o 
38599803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
38609803387cSTheodore Ts'o 			if (v > max) {
38619803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
38629803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
38639803387cSTheodore Ts'o 				goto failed_mount;
38649803387cSTheodore Ts'o 			}
38659803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
38669803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
38679803387cSTheodore Ts'o 
38689803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
38699803387cSTheodore Ts'o 			if (v > max) {
38709803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
38719803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
38729803387cSTheodore Ts'o 				goto failed_mount;
38739803387cSTheodore Ts'o 			}
38749803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
38759803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
38769803387cSTheodore Ts'o 		}
38779803387cSTheodore Ts'o 	}
38789803387cSTheodore Ts'o 
38795aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
38805aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
38815aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
38825aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
38835aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
38845aee0f8aSTheodore Ts'o 			goto failed_mount;
38855aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
38865aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
38878b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
38888b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
38895aee0f8aSTheodore Ts'o 				 s_mount_opts);
38905aee0f8aSTheodore Ts'o 		}
38915aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
38928b67f04aSTheodore Ts'o 	}
38935a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3894b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3895661aa520SEric Sandeen 			   &journal_ioprio, 0))
3896ac27a0ecSDave Kleikamp 		goto failed_mount;
3897ac27a0ecSDave Kleikamp 
3898c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
3899c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
3900c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
3901c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
3902c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
3903c83ad55eSGabriel Krisman Bertazi 
3904c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
3905c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3906c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
3907c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3908c83ad55eSGabriel Krisman Bertazi 		}
3909c83ad55eSGabriel Krisman Bertazi 
3910c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
3911c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
3912c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3913c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
3914c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3915c83ad55eSGabriel Krisman Bertazi 		}
3916c83ad55eSGabriel Krisman Bertazi 
3917c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
3918c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
3919c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3920c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
3921c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
3922c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
3923c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
3924c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3925c83ad55eSGabriel Krisman Bertazi 		}
3926c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
3927c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
3928c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
3929c83ad55eSGabriel Krisman Bertazi 
3930c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
3931c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
3932c83ad55eSGabriel Krisman Bertazi 	}
3933c83ad55eSGabriel Krisman Bertazi #endif
3934c83ad55eSGabriel Krisman Bertazi 
393556889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
3936*244adf64STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
3937*244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
393856889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
393956889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
394056889787STheodore Ts'o 				 "both data=journal and delalloc");
394156889787STheodore Ts'o 			goto failed_mount;
394256889787STheodore Ts'o 		}
394356889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
394456889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
39456ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
394656889787STheodore Ts'o 			goto failed_mount;
394756889787STheodore Ts'o 		}
3948923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3949923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3950923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3951923ae0ffSRoss Zwisler 			goto failed_mount;
3952923ae0ffSRoss Zwisler 		}
395373b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
395473b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
395573b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
395673b92a2aSSergey Karamov 				 "instead of data journaling mode");
395773b92a2aSSergey Karamov 		}
395856889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
395956889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3960001e4a87STejun Heo 	} else {
3961001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
396256889787STheodore Ts'o 	}
396356889787STheodore Ts'o 
39641751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
39651751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
3966ac27a0ecSDave Kleikamp 
3967617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3968e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
3969e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
3970e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
3971b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3972b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3973b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3974469108ffSTheodore Tso 
3975ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3976ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3977e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
3978ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3979ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3980ed3654ebSTheodore Ts'o 			goto failed_mount;
3981ed3654ebSTheodore Ts'o 		}
3982dec214d0STahsin Erdogan 
3983dec214d0STahsin Erdogan 		/*
3984dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
3985dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
3986dec214d0STahsin Erdogan 		 */
3987dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
3988dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
3989dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
3990dec214d0STahsin Erdogan 			goto failed_mount;
3991dec214d0STahsin Erdogan 		}
3992ed3654ebSTheodore Ts'o 	}
3993ed3654ebSTheodore Ts'o 
39942035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
39952035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
39962035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
39972035e776STheodore Ts'o 				 "using the ext4 subsystem");
39982035e776STheodore Ts'o 		else {
39990d9366d6SEric Sandeen 			/*
40000d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
40010d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
40020d9366d6SEric Sandeen 			 */
40030d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
40040d9366d6SEric Sandeen 				goto failed_mount;
40052035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
40062035e776STheodore Ts'o 				 "to feature incompatibilities");
40072035e776STheodore Ts'o 			goto failed_mount;
40082035e776STheodore Ts'o 		}
40092035e776STheodore Ts'o 	}
40102035e776STheodore Ts'o 
40112035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
40122035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
40132035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
40142035e776STheodore Ts'o 				 "using the ext4 subsystem");
40152035e776STheodore Ts'o 		else {
40160d9366d6SEric Sandeen 			/*
40170d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
40180d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
40190d9366d6SEric Sandeen 			 */
40200d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
40210d9366d6SEric Sandeen 				goto failed_mount;
40222035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
40232035e776STheodore Ts'o 				 "to feature incompatibilities");
40242035e776STheodore Ts'o 			goto failed_mount;
40252035e776STheodore Ts'o 		}
40262035e776STheodore Ts'o 	}
40272035e776STheodore Ts'o 
4028469108ffSTheodore Tso 	/*
4029ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4030ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4031ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4032ac27a0ecSDave Kleikamp 	 */
4033bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4034ac27a0ecSDave Kleikamp 		goto failed_mount;
4035a13fb1a4SEric Sandeen 
4036261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
4037617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
4038617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
4039b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
40408cdf3372STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
40418cdf3372STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
40428cdf3372STheodore Ts'o 		goto failed_mount;
40438cdf3372STheodore Ts'o 	}
40448cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
40458cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
40468cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
40478cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
40488cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4049ac27a0ecSDave Kleikamp 		goto failed_mount;
4050ac27a0ecSDave Kleikamp 	}
4051bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4052bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4053bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4054bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4055bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4056bfe0a5f4STheodore Ts'o 		goto failed_mount;
4057bfe0a5f4STheodore Ts'o 	}
4058ac27a0ecSDave Kleikamp 
40595b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
40605b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
40615b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
40625b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
40635b9554dcSTheodore Ts'o 		goto failed_mount;
40645b9554dcSTheodore Ts'o 	}
40655b9554dcSTheodore Ts'o 
4066923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
4067559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4068559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4069559db4c6SRoss Zwisler 					" that may contain inline data");
4070361d24d4SEric Sandeen 			goto failed_mount;
4071559db4c6SRoss Zwisler 		}
407280660f20SDave Jiang 		if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
407324f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4074361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4075361d24d4SEric Sandeen 			goto failed_mount;
407624f3478dSDan Williams 		}
4077923ae0ffSRoss Zwisler 	}
4078923ae0ffSRoss Zwisler 
4079e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
40806ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
40816ddb2447STheodore Ts'o 			 es->s_encryption_level);
40826ddb2447STheodore Ts'o 		goto failed_mount;
40836ddb2447STheodore Ts'o 	}
40846ddb2447STheodore Ts'o 
4085ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4086ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4087ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4088b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4089ce40733cSAneesh Kumar K.V 					blocksize);
4090ac27a0ecSDave Kleikamp 			goto failed_mount;
4091ac27a0ecSDave Kleikamp 		}
4092ac27a0ecSDave Kleikamp 
4093ac27a0ecSDave Kleikamp 		brelse(bh);
409470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
409570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4096a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4097ac27a0ecSDave Kleikamp 		if (!bh) {
4098b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4099b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4100ac27a0ecSDave Kleikamp 			goto failed_mount;
4101ac27a0ecSDave Kleikamp 		}
41022716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4103ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4104617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4105b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4106b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4107ac27a0ecSDave Kleikamp 			goto failed_mount;
4108ac27a0ecSDave Kleikamp 		}
4109ac27a0ecSDave Kleikamp 	}
4110ac27a0ecSDave Kleikamp 
4111e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4112f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4113f287a1a5STheodore Ts'o 						      has_huge_files);
4114f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4115ac27a0ecSDave Kleikamp 
41160d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4117e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
41188fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
41190d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4120d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4121b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4122b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
41230d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
41240d1ee42fSAlexandre Ratchov 			goto failed_mount;
41250d1ee42fSAlexandre Ratchov 		}
41260d1ee42fSAlexandre Ratchov 	} else
41270d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
41280b8e58a1SAndreas Dilger 
4129ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4130ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
41310b8e58a1SAndreas Dilger 
4132617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4133ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4134617ba13bSMingming Cao 		goto cantfind_ext4;
4135cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4136cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4137cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4138cd6bb35bSTheodore Ts'o 			 sbi->s_blocks_per_group);
4139cd6bb35bSTheodore Ts'o 		goto failed_mount;
4140cd6bb35bSTheodore Ts'o 	}
4141ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4142ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
41430d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4144ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4145ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4146e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4147e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
41480b8e58a1SAndreas Dilger 
4149ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4150ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4151ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4152e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4153f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4154f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4155f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4156f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4157f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4158bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
415923301410STheodore Ts'o 				es->s_flags |=
416023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4161f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4162f99b2589STheodore Ts'o #else
4163bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
416423301410STheodore Ts'o 				es->s_flags |=
416523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4166f99b2589STheodore Ts'o #endif
4167f99b2589STheodore Ts'o 		}
416823301410STheodore Ts'o 	}
4169ac27a0ecSDave Kleikamp 
4170281b5995STheodore Ts'o 	/* Handle clustersize */
4171281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4172e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
4173281b5995STheodore Ts'o 	if (has_bigalloc) {
4174281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4175281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4176281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4177281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4178281b5995STheodore Ts'o 			goto failed_mount;
4179281b5995STheodore Ts'o 		}
4180281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4181281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4182281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4183281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4184281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4185281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4186281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4187281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4188281b5995STheodore Ts'o 			goto failed_mount;
4189281b5995STheodore Ts'o 		}
4190281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4191281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4192281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4193281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4194281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4195281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4196281b5995STheodore Ts'o 			goto failed_mount;
4197281b5995STheodore Ts'o 		}
4198281b5995STheodore Ts'o 	} else {
4199281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4200bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4201bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4202bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4203bfe0a5f4STheodore Ts'o 			goto failed_mount;
4204281b5995STheodore Ts'o 		}
4205ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4206b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4207b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4208ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4209ac27a0ecSDave Kleikamp 			goto failed_mount;
4210ac27a0ecSDave Kleikamp 		}
4211281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4212281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4213281b5995STheodore Ts'o 	}
4214281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4215281b5995STheodore Ts'o 
4216960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4217960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4218960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4219960fd856STheodore Ts'o 
4220bf43d84bSEric Sandeen 	/*
4221bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4222bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4223bf43d84bSEric Sandeen 	 */
42245a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
422530ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
42265a9ae68aSDarrick J. Wong 	if (err) {
4227b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4228bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4229ac27a0ecSDave Kleikamp 		goto failed_mount;
4230ac27a0ecSDave Kleikamp 	}
4231ac27a0ecSDave Kleikamp 
4232617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4233617ba13bSMingming Cao 		goto cantfind_ext4;
4234e7c95593SEric Sandeen 
42350f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
42360f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
42370f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4238b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4239b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
42400f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
42410f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
42420f2ddca6SFrom: Thiemo Nagel 	}
42430f2ddca6SFrom: Thiemo Nagel 
42444ec11028STheodore Ts'o 	/*
42454ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
42464ec11028STheodore Ts'o 	 * of the filesystem.
42474ec11028STheodore Ts'o 	 */
42484ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4249b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4250b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4251e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
42524ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4253e7c95593SEric Sandeen 		goto failed_mount;
4254e7c95593SEric Sandeen 	}
4255bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4256bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4257bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4258bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4259bfe0a5f4STheodore Ts'o 		goto failed_mount;
4260bfe0a5f4STheodore Ts'o 	}
4261bfe0a5f4STheodore Ts'o 
4262bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4263bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4264bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4265bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
42664ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4267b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
42684ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4269b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
42704ec11028STheodore Ts'o 		       ext4_blocks_count(es),
42714ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
42724ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
42734ec11028STheodore Ts'o 		goto failed_mount;
42744ec11028STheodore Ts'o 	}
4275bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4276fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4277fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
42789e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
42799e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
42809e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
42819e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
42829e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
42839e463084STheodore Ts'o 		ret = -EINVAL;
42849e463084STheodore Ts'o 		goto failed_mount;
42859e463084STheodore Ts'o 	}
4286617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4287617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
42883a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
42892ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
42903a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
42913a4b77cdSEryu Guan 				 "first meta block group too large: %u "
42923a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
42933a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
42943a4b77cdSEryu Guan 			goto failed_mount;
42953a4b77cdSEryu Guan 		}
42963a4b77cdSEryu Guan 	}
4297344476e1SKees Cook 	sbi->s_group_desc = kvmalloc_array(db_count,
4298f18a5f21STheodore Ts'o 					   sizeof(struct buffer_head *),
4299ac27a0ecSDave Kleikamp 					   GFP_KERNEL);
4300ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4301b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
43022cde417dSTheodore Ts'o 		ret = -ENOMEM;
4303ac27a0ecSDave Kleikamp 		goto failed_mount;
4304ac27a0ecSDave Kleikamp 	}
4305ac27a0ecSDave Kleikamp 
4306705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4307ac27a0ecSDave Kleikamp 
430885c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
430985c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
431085c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
431185c8f176SAndrew Perepechko 		sb_breadahead(sb, block);
431285c8f176SAndrew Perepechko 	}
431385c8f176SAndrew Perepechko 
4314ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
431570bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
4316a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
4317ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
4318b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4319b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4320ac27a0ecSDave Kleikamp 			db_count = i;
4321ac27a0ecSDave Kleikamp 			goto failed_mount2;
4322ac27a0ecSDave Kleikamp 		}
4323ac27a0ecSDave Kleikamp 	}
432444de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4325829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4326b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
43276a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4328f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4329ac27a0ecSDave Kleikamp 	}
4330772cb7c8SJose R. Santos 
4331235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
433204496411STao Ma 
4333a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4334eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4335ce7e010aSTheodore Ts'o 		goto failed_mount3;
4336ce7e010aSTheodore Ts'o 
4337c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
433867a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4339c9de560dSAlex Tomas 
4340f9ae9cf5STheodore Ts'o 	/*
4341f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4342f9ae9cf5STheodore Ts'o 	 */
4343f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4344617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4345617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4346643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4347a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4348ffcc4182SEric Biggers #endif
4349c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4350c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4351c93d8f88SEric Biggers #endif
4352ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4353617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4354e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
43551fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4356262b4662SJan Kara 	else
4357262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4358689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4359ac27a0ecSDave Kleikamp #endif
436085787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4361f2fa2ffcSAneesh Kumar K.V 
4362ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
43633b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4364ac27a0ecSDave Kleikamp 
4365ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4366ac27a0ecSDave Kleikamp 
4367ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4368e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4369ac27a0ecSDave Kleikamp 
4370bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4371c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
437250460fe8SDarrick J. Wong 			goto failed_mount3a;
4373c5e06d10SJohann Lombardi 
4374ac27a0ecSDave Kleikamp 	/*
4375ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4376ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4377ac27a0ecSDave Kleikamp 	 */
4378e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
43794753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
43804753d8a2STheodore Ts'o 		if (err)
438150460fe8SDarrick J. Wong 			goto failed_mount3a;
4382bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4383e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4384b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4385b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4386744692dcSJiaying Zhang 		goto failed_mount_wq;
4387ac27a0ecSDave Kleikamp 	} else {
43881e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
43891e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
43901e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43911e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
43921e381f60SDmitry Monakhov 			goto failed_mount_wq;
43931e381f60SDmitry Monakhov 		}
43941e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
43951e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43961e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
43971e381f60SDmitry Monakhov 			goto failed_mount_wq;
43981e381f60SDmitry Monakhov 		}
43991e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
44001e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44011e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
44021e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
44031e381f60SDmitry Monakhov 			goto failed_mount_wq;
44041e381f60SDmitry Monakhov 		}
44051e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
44061e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
44071e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44081e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
44091e381f60SDmitry Monakhov 			goto failed_mount_wq;
44101e381f60SDmitry Monakhov 		}
441150b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
44121e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4413fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
44140390131bSFrank Mayhar 		sbi->s_journal = NULL;
44150390131bSFrank Mayhar 		needs_recovery = 0;
44160390131bSFrank Mayhar 		goto no_journal;
4417ac27a0ecSDave Kleikamp 	}
4418ac27a0ecSDave Kleikamp 
4419e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4420eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4421eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4422b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4423744692dcSJiaying Zhang 		goto failed_mount_wq;
4424eb40a09cSJose R. Santos 	}
4425eb40a09cSJose R. Santos 
442625ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
442725ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
442825ed6e8aSDarrick J. Wong 			 "feature set");
442925ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4430d4da6c9cSLinus Torvalds 	}
4431818d276cSGirish Shilamkar 
4432ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4433ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4434ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4435ac27a0ecSDave Kleikamp 	case 0:
4436ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
443763f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
443863f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
443963f57933SAndrew Morton 		 */
4440dab291afSMingming Cao 		if (jbd2_journal_check_available_features
444127f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4442fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
444327f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
444427f394a7STyson Nottingham 		} else {
4445fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
444627f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
444727f394a7STyson Nottingham 		}
4448ac27a0ecSDave Kleikamp 		break;
4449ac27a0ecSDave Kleikamp 
4450617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4451617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4452dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4453dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4454b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4455b31e1552SEric Sandeen 			       "requested data journaling mode");
4456744692dcSJiaying Zhang 			goto failed_mount_wq;
4457ac27a0ecSDave Kleikamp 		}
4458ac27a0ecSDave Kleikamp 	default:
4459ac27a0ecSDave Kleikamp 		break;
4460ac27a0ecSDave Kleikamp 	}
4461ab04df78SJan Kara 
4462ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4463ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4464ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4465ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4466ab04df78SJan Kara 		goto failed_mount_wq;
4467ab04df78SJan Kara 	}
4468ab04df78SJan Kara 
4469b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4470ac27a0ecSDave Kleikamp 
447118aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
447218aadd47SBobi Jam 
4473ce7e010aSTheodore Ts'o no_journal:
4474cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
447547387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
447647387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4477cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4478cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
44799c191f70ST Makphaibulchoke 			goto failed_mount_wq;
44809c191f70ST Makphaibulchoke 		}
44819c191f70ST Makphaibulchoke 
4482dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4483dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4484dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4485dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4486dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4487dec214d0STahsin Erdogan 				goto failed_mount_wq;
4488dec214d0STahsin Erdogan 			}
4489dec214d0STahsin Erdogan 		}
4490cdb7ee4cSTahsin Erdogan 	}
4491dec214d0STahsin Erdogan 
4492c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4493c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4494c93d8f88SEric Biggers 		goto failed_mount_wq;
4495c93d8f88SEric Biggers 	}
4496c93d8f88SEric Biggers 
4497bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4498e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4499e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
45006ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
45016ddb2447STheodore Ts'o 	}
45026ddb2447STheodore Ts'o 
4503fd89d5f2STejun Heo 	/*
4504952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4505952fc18eSTheodore Ts'o 	 * superblock if present.
4506952fc18eSTheodore Ts'o 	 */
4507952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4508952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4509952fc18eSTheodore Ts'o 	else {
451007aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
451107aa2ea1SLukas Czerner 		if (err)
4512952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4513952fc18eSTheodore Ts'o 	}
4514952fc18eSTheodore Ts'o 
4515952fc18eSTheodore Ts'o 	/*
4516fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4517fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4518fd89d5f2STejun Heo 	 */
45192e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
45202e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
45212e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
45222e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
452307aa2ea1SLukas Czerner 		ret = -ENOMEM;
45242e8fa54eSJan Kara 		goto failed_mount4;
45252e8fa54eSJan Kara 	}
45262e8fa54eSJan Kara 
4527ac27a0ecSDave Kleikamp 	/*
4528dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4529ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4530ac27a0ecSDave Kleikamp 	 */
4531ac27a0ecSDave Kleikamp 
45328a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
45331d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4534b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
45351d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
453632a9bb57SManish Katiyar 		root = NULL;
4537ac27a0ecSDave Kleikamp 		goto failed_mount4;
4538ac27a0ecSDave Kleikamp 	}
4539ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4540b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
454194bf608aSAl Viro 		iput(root);
4542ac27a0ecSDave Kleikamp 		goto failed_mount4;
4543ac27a0ecSDave Kleikamp 	}
4544b886ee3eSGabriel Krisman Bertazi 
4545b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4546b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4547b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4548b886ee3eSGabriel Krisman Bertazi #endif
4549b886ee3eSGabriel Krisman Bertazi 
455048fde701SAl Viro 	sb->s_root = d_make_root(root);
45511d1fe1eeSDavid Howells 	if (!sb->s_root) {
4552b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
45531d1fe1eeSDavid Howells 		ret = -ENOMEM;
45541d1fe1eeSDavid Howells 		goto failed_mount4;
45551d1fe1eeSDavid Howells 	}
4556ac27a0ecSDave Kleikamp 
4557c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4558c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
45591751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4560c89128a0SJaegeuk Kim 		ret = 0;
4561c89128a0SJaegeuk Kim 	} else if (ret)
4562c89128a0SJaegeuk Kim 		goto failed_mount4a;
4563ef7f3835SKalpak Shah 
4564b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
456527dd4385SLukas Czerner 
45666fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
45676fd058f7STheodore Ts'o 	if (err) {
4568b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4569fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4570f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4571f9ae9cf5STheodore Ts'o 	}
4572f9ae9cf5STheodore Ts'o 
4573f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4574f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4575f9ae9cf5STheodore Ts'o 	if (err) {
4576f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4577f9ae9cf5STheodore Ts'o 			 err);
4578dcf2d804STao Ma 		goto failed_mount5;
4579c2774d84SAneesh Kumar K.V 	}
4580c2774d84SAneesh Kumar K.V 
4581d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4582d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4583d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
45844274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4585908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4586908c7f19STejun Heo 				  GFP_KERNEL);
4587d5e03cbbSTheodore Ts'o 	if (!err) {
4588d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4589d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
45904274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4591908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4592908c7f19STejun Heo 					  GFP_KERNEL);
4593d5e03cbbSTheodore Ts'o 	}
4594d5e03cbbSTheodore Ts'o 	if (!err)
4595d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4596908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4597d5e03cbbSTheodore Ts'o 	if (!err)
4598908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4599908c7f19STejun Heo 					  GFP_KERNEL);
4600c8585c6fSDaeho Jeong 	if (!err)
4601c8585c6fSDaeho Jeong 		err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem);
4602c8585c6fSDaeho Jeong 
4603d5e03cbbSTheodore Ts'o 	if (err) {
4604d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4605d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4606d5e03cbbSTheodore Ts'o 	}
4607d5e03cbbSTheodore Ts'o 
4608e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4609d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4610d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4611d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4612d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4613d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4614d5e03cbbSTheodore Ts'o 		}
4615d5e03cbbSTheodore Ts'o 
4616bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4617bfff6873SLukas Czerner 	if (err)
4618dcf2d804STao Ma 		goto failed_mount6;
4619bfff6873SLukas Czerner 
4620b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4621dcf2d804STao Ma 	if (err)
4622dcf2d804STao Ma 		goto failed_mount7;
46233197ebdbSTheodore Ts'o 
46249b2ff357SJan Kara #ifdef CONFIG_QUOTA
46259b2ff357SJan Kara 	/* Enable quota usage during mount. */
4626bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
46279b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
46289b2ff357SJan Kara 		if (err)
46299b2ff357SJan Kara 			goto failed_mount8;
46309b2ff357SJan Kara 	}
46319b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
46329b2ff357SJan Kara 
4633617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4634617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4635617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
46360390131bSFrank Mayhar 	if (needs_recovery) {
4637b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4638617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
46390390131bSFrank Mayhar 	}
46400390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
46410390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
46420390131bSFrank Mayhar 			descr = " journalled data mode";
46430390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
46440390131bSFrank Mayhar 			descr = " ordered data mode";
46450390131bSFrank Mayhar 		else
46460390131bSFrank Mayhar 			descr = " writeback data mode";
46470390131bSFrank Mayhar 	} else
46480390131bSFrank Mayhar 		descr = "out journal";
46490390131bSFrank Mayhar 
465079add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
465179add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
465279add3a3SLukas Czerner 		if (!blk_queue_discard(q))
465379add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
465479add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
465579add3a3SLukas Czerner 				 "the device does not support discard");
465679add3a3SLukas Czerner 	}
465779add3a3SLukas Czerner 
4658e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4659d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
46605aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
46615aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
46625aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
46638b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4664ac27a0ecSDave Kleikamp 
466566e61a9eSTheodore Ts'o 	if (es->s_error_count)
466666e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4667ac27a0ecSDave Kleikamp 
4668efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4669efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4670efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4671efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4672efbed4dcSTheodore Ts'o 
4673d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4674ac27a0ecSDave Kleikamp 	return 0;
4675ac27a0ecSDave Kleikamp 
4676617ba13bSMingming Cao cantfind_ext4:
4677ac27a0ecSDave Kleikamp 	if (!silent)
4678b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4679ac27a0ecSDave Kleikamp 	goto failed_mount;
4680ac27a0ecSDave Kleikamp 
468172ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
468272ba7450STheodore Ts'o failed_mount8:
4683ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
468472ba7450STheodore Ts'o #endif
4685dcf2d804STao Ma failed_mount7:
4686dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4687dcf2d804STao Ma failed_mount6:
4688f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
4689d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
4690b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
4691d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4692d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4693d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4694d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4695af18e35bSVasily Averin 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
469600764937SAzat Khuzhin failed_mount5:
4697f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4698f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4699f9ae9cf5STheodore Ts'o failed_mount4a:
470094bf608aSAl Viro 	dput(sb->s_root);
470132a9bb57SManish Katiyar 	sb->s_root = NULL;
470294bf608aSAl Viro failed_mount4:
4703b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
47042e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
47052e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
47064c0425ffSMingming Cao failed_mount_wq:
4707dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4708dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
470950c15df6SChengguang Xu 
471047387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
471147387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
471250c15df6SChengguang Xu 
47130390131bSFrank Mayhar 	if (sbi->s_journal) {
4714dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
471547b4a50bSJan Kara 		sbi->s_journal = NULL;
47160390131bSFrank Mayhar 	}
471750460fe8SDarrick J. Wong failed_mount3a:
4718d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4719eb68d0e2SZheng Liu failed_mount3:
47209105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4721c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4722c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4723ac27a0ecSDave Kleikamp failed_mount2:
4724ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4725ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4726b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4727ac27a0ecSDave Kleikamp failed_mount:
47280441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
47290441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4730c83ad55eSGabriel Krisman Bertazi 
4731c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4732c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4733c83ad55eSGabriel Krisman Bertazi #endif
4734c83ad55eSGabriel Krisman Bertazi 
4735ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4736a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
47370ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4738ac27a0ecSDave Kleikamp #endif
4739617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4740ac27a0ecSDave Kleikamp 	brelse(bh);
4741ac27a0ecSDave Kleikamp out_fail:
4742ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4743f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
47445aee0f8aSTheodore Ts'o out_free_base:
4745ac27a0ecSDave Kleikamp 	kfree(sbi);
4746d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
47475e405595SDan Williams 	fs_put_dax(dax_dev);
474807aa2ea1SLukas Czerner 	return err ? err : ret;
4749ac27a0ecSDave Kleikamp }
4750ac27a0ecSDave Kleikamp 
4751ac27a0ecSDave Kleikamp /*
4752ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4753ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4754ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4755ac27a0ecSDave Kleikamp  */
4756617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4757ac27a0ecSDave Kleikamp {
4758617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4759ac27a0ecSDave Kleikamp 
4760ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
476130773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
476230773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4763ac27a0ecSDave Kleikamp 
4764a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4765ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4766dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4767ac27a0ecSDave Kleikamp 	else
4768dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
47695bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
47705bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
47715bf5683aSHidehiro Kawai 	else
47725bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4773a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4774ac27a0ecSDave Kleikamp }
4775ac27a0ecSDave Kleikamp 
4776c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4777ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4778ac27a0ecSDave Kleikamp {
4779ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4780ac27a0ecSDave Kleikamp 
4781c6cb7e77SEric Whitney 	/*
4782c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4783c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4784c6cb7e77SEric Whitney 	 * will try to delete it.
4785c6cb7e77SEric Whitney 	 */
47868a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
47871d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4788b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4789ac27a0ecSDave Kleikamp 		return NULL;
4790ac27a0ecSDave Kleikamp 	}
4791ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4792ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4793ac27a0ecSDave Kleikamp 		iput(journal_inode);
4794b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4795ac27a0ecSDave Kleikamp 		return NULL;
4796ac27a0ecSDave Kleikamp 	}
4797ac27a0ecSDave Kleikamp 
4798e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4799ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
48001d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4801b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4802ac27a0ecSDave Kleikamp 		iput(journal_inode);
4803ac27a0ecSDave Kleikamp 		return NULL;
4804ac27a0ecSDave Kleikamp 	}
4805c6cb7e77SEric Whitney 	return journal_inode;
4806c6cb7e77SEric Whitney }
4807c6cb7e77SEric Whitney 
4808c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4809c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4810c6cb7e77SEric Whitney {
4811c6cb7e77SEric Whitney 	struct inode *journal_inode;
4812c6cb7e77SEric Whitney 	journal_t *journal;
4813c6cb7e77SEric Whitney 
4814c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4815c6cb7e77SEric Whitney 
4816c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4817c6cb7e77SEric Whitney 	if (!journal_inode)
4818c6cb7e77SEric Whitney 		return NULL;
4819ac27a0ecSDave Kleikamp 
4820dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4821ac27a0ecSDave Kleikamp 	if (!journal) {
4822b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4823ac27a0ecSDave Kleikamp 		iput(journal_inode);
4824ac27a0ecSDave Kleikamp 		return NULL;
4825ac27a0ecSDave Kleikamp 	}
4826ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4827617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4828ac27a0ecSDave Kleikamp 	return journal;
4829ac27a0ecSDave Kleikamp }
4830ac27a0ecSDave Kleikamp 
4831617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4832ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4833ac27a0ecSDave Kleikamp {
4834ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4835ac27a0ecSDave Kleikamp 	journal_t *journal;
4836617ba13bSMingming Cao 	ext4_fsblk_t start;
4837617ba13bSMingming Cao 	ext4_fsblk_t len;
4838ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4839617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4840ac27a0ecSDave Kleikamp 	unsigned long offset;
4841617ba13bSMingming Cao 	struct ext4_super_block *es;
4842ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4843ac27a0ecSDave Kleikamp 
4844e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
48450390131bSFrank Mayhar 
4846b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4847ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4848ac27a0ecSDave Kleikamp 		return NULL;
4849ac27a0ecSDave Kleikamp 
4850ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4851e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4852ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4853b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4854b31e1552SEric Sandeen 			"blocksize too small for journal device");
4855ac27a0ecSDave Kleikamp 		goto out_bdev;
4856ac27a0ecSDave Kleikamp 	}
4857ac27a0ecSDave Kleikamp 
4858617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4859617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4860ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4861ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4862b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4863b31e1552SEric Sandeen 		       "external journal");
4864ac27a0ecSDave Kleikamp 		goto out_bdev;
4865ac27a0ecSDave Kleikamp 	}
4866ac27a0ecSDave Kleikamp 
48672716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4868617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4869ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4870617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4871b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4872b31e1552SEric Sandeen 					"bad superblock");
4873ac27a0ecSDave Kleikamp 		brelse(bh);
4874ac27a0ecSDave Kleikamp 		goto out_bdev;
4875ac27a0ecSDave Kleikamp 	}
4876ac27a0ecSDave Kleikamp 
4877df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4878df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4879df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4880df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4881df4763beSDarrick J. Wong 				       "corrupt superblock");
4882df4763beSDarrick J. Wong 		brelse(bh);
4883df4763beSDarrick J. Wong 		goto out_bdev;
4884df4763beSDarrick J. Wong 	}
4885df4763beSDarrick J. Wong 
4886617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4887b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4888ac27a0ecSDave Kleikamp 		brelse(bh);
4889ac27a0ecSDave Kleikamp 		goto out_bdev;
4890ac27a0ecSDave Kleikamp 	}
4891ac27a0ecSDave Kleikamp 
4892bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4893ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4894ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4895ac27a0ecSDave Kleikamp 
4896dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4897ac27a0ecSDave Kleikamp 					start, len, blocksize);
4898ac27a0ecSDave Kleikamp 	if (!journal) {
4899b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4900ac27a0ecSDave Kleikamp 		goto out_bdev;
4901ac27a0ecSDave Kleikamp 	}
4902ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4903dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4904ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4905ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4906b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4907ac27a0ecSDave Kleikamp 		goto out_journal;
4908ac27a0ecSDave Kleikamp 	}
4909ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4910b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4911b31e1552SEric Sandeen 					"user (unsupported) - %d",
4912ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4913ac27a0ecSDave Kleikamp 		goto out_journal;
4914ac27a0ecSDave Kleikamp 	}
4915617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4916617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4917ac27a0ecSDave Kleikamp 	return journal;
49180b8e58a1SAndreas Dilger 
4919ac27a0ecSDave Kleikamp out_journal:
4920dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4921ac27a0ecSDave Kleikamp out_bdev:
4922617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4923ac27a0ecSDave Kleikamp 	return NULL;
4924ac27a0ecSDave Kleikamp }
4925ac27a0ecSDave Kleikamp 
4926617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4927617ba13bSMingming Cao 			     struct ext4_super_block *es,
4928ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4929ac27a0ecSDave Kleikamp {
4930ac27a0ecSDave Kleikamp 	journal_t *journal;
4931ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4932ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4933ac27a0ecSDave Kleikamp 	int err = 0;
4934ac27a0ecSDave Kleikamp 	int really_read_only;
4935ac27a0ecSDave Kleikamp 
4936e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
49370390131bSFrank Mayhar 
4938ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4939ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4940b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4941b31e1552SEric Sandeen 			"numbers have changed");
4942ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4943ac27a0ecSDave Kleikamp 	} else
4944ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4945ac27a0ecSDave Kleikamp 
4946ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4947ac27a0ecSDave Kleikamp 
4948ac27a0ecSDave Kleikamp 	/*
4949ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4950ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4951ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4952ac27a0ecSDave Kleikamp 	 */
4953e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4954bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
4955b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4956b31e1552SEric Sandeen 					"required on readonly filesystem");
4957ac27a0ecSDave Kleikamp 			if (really_read_only) {
4958b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4959d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
4960d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
4961ac27a0ecSDave Kleikamp 				return -EROFS;
4962ac27a0ecSDave Kleikamp 			}
4963b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4964b31e1552SEric Sandeen 			       "be enabled during recovery");
4965ac27a0ecSDave Kleikamp 		}
4966ac27a0ecSDave Kleikamp 	}
4967ac27a0ecSDave Kleikamp 
4968ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4969b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4970b31e1552SEric Sandeen 		       "and inode journals!");
4971ac27a0ecSDave Kleikamp 		return -EINVAL;
4972ac27a0ecSDave Kleikamp 	}
4973ac27a0ecSDave Kleikamp 
4974ac27a0ecSDave Kleikamp 	if (journal_inum) {
4975617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4976ac27a0ecSDave Kleikamp 			return -EINVAL;
4977ac27a0ecSDave Kleikamp 	} else {
4978617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4979ac27a0ecSDave Kleikamp 			return -EINVAL;
4980ac27a0ecSDave Kleikamp 	}
4981ac27a0ecSDave Kleikamp 
498290576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4983b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
49844776004fSTheodore Ts'o 
4985e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
4986dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
49871c13d5c0STheodore Ts'o 	if (!err) {
49881c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
49891c13d5c0STheodore Ts'o 		if (save)
49901c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
49911c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4992dab291afSMingming Cao 		err = jbd2_journal_load(journal);
49931c13d5c0STheodore Ts'o 		if (save)
49941c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
49951c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
49961c13d5c0STheodore Ts'o 		kfree(save);
49971c13d5c0STheodore Ts'o 	}
4998ac27a0ecSDave Kleikamp 
4999ac27a0ecSDave Kleikamp 	if (err) {
5000b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5001dab291afSMingming Cao 		jbd2_journal_destroy(journal);
5002ac27a0ecSDave Kleikamp 		return err;
5003ac27a0ecSDave Kleikamp 	}
5004ac27a0ecSDave Kleikamp 
5005617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
5006617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
5007ac27a0ecSDave Kleikamp 
5008c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5009ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5010ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5011ac27a0ecSDave Kleikamp 
5012ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5013e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5014ac27a0ecSDave Kleikamp 	}
5015ac27a0ecSDave Kleikamp 
5016ac27a0ecSDave Kleikamp 	return 0;
5017ac27a0ecSDave Kleikamp }
5018ac27a0ecSDave Kleikamp 
5019e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5020ac27a0ecSDave Kleikamp {
5021e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5022617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5023c4be0c1dSTakashi Sato 	int error = 0;
5024ac27a0ecSDave Kleikamp 
5025bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5026c4be0c1dSTakashi Sato 		return error;
5027a17712c8SJon Derrick 
5028a17712c8SJon Derrick 	/*
5029a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
5030a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
5031a17712c8SJon Derrick 	 */
5032a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
5033a17712c8SJon Derrick 		return error;
5034a17712c8SJon Derrick 
503571290b36STheodore Ts'o 	/*
503671290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
503771290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
503871290b36STheodore Ts'o 	 * write time when we are mounting the root file system
503971290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
504071290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
504171290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
504271290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
504371290b36STheodore Ts'o 	 * to complain and force a full file system check.
504471290b36STheodore Ts'o 	 */
50451751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
50466a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5047f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5048afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5049afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5050dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5051dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5052afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5053f613dfcbSTheodore Ts'o 	else
5054f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5055f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5056d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
505757042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
505857042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
505957042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5060d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
50617f93cff9STheodore Ts'o 		es->s_free_inodes_count =
50627f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
50635d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5064ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
506506db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
50661566a48aSTheodore Ts'o 	if (sync)
50674743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5068e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
50694743f839SPranay Kr. Srivastava 		/*
50704743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
50714743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
50724743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
50734743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
50744743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
50754743f839SPranay Kr. Srivastava 		 * write and hope for the best.
50764743f839SPranay Kr. Srivastava 		 */
50774743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
50784743f839SPranay Kr. Srivastava 		       "superblock detected");
50794743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
50804743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
50814743f839SPranay Kr. Srivastava 	}
5082ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5083914258bfSTheodore Ts'o 	if (sync) {
50841566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5085564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
508600473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5087c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5088b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5089b31e1552SEric Sandeen 			       "superblock");
5090914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5091914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5092914258bfSTheodore Ts'o 		}
5093914258bfSTheodore Ts'o 	}
5094c4be0c1dSTakashi Sato 	return error;
5095ac27a0ecSDave Kleikamp }
5096ac27a0ecSDave Kleikamp 
5097ac27a0ecSDave Kleikamp /*
5098ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5099ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5100ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5101ac27a0ecSDave Kleikamp  */
5102617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
5103617ba13bSMingming Cao 					struct ext4_super_block *es)
5104ac27a0ecSDave Kleikamp {
5105617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5106ac27a0ecSDave Kleikamp 
5107e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
51080390131bSFrank Mayhar 		BUG_ON(journal != NULL);
51090390131bSFrank Mayhar 		return;
51100390131bSFrank Mayhar 	}
5111dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
51127ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
51137ffe1ea8SHidehiro Kawai 		goto out;
51147ffe1ea8SHidehiro Kawai 
5115bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5116e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5117e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5118ac27a0ecSDave Kleikamp 	}
51197ffe1ea8SHidehiro Kawai 
51207ffe1ea8SHidehiro Kawai out:
5121dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
5122ac27a0ecSDave Kleikamp }
5123ac27a0ecSDave Kleikamp 
5124ac27a0ecSDave Kleikamp /*
5125ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5126ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5127ac27a0ecSDave Kleikamp  * main filesystem now.
5128ac27a0ecSDave Kleikamp  */
5129617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
5130617ba13bSMingming Cao 				   struct ext4_super_block *es)
5131ac27a0ecSDave Kleikamp {
5132ac27a0ecSDave Kleikamp 	journal_t *journal;
5133ac27a0ecSDave Kleikamp 	int j_errno;
5134ac27a0ecSDave Kleikamp 	const char *errstr;
5135ac27a0ecSDave Kleikamp 
5136e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
51370390131bSFrank Mayhar 
5138617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5139ac27a0ecSDave Kleikamp 
5140ac27a0ecSDave Kleikamp 	/*
5141ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5142617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5143ac27a0ecSDave Kleikamp 	 */
5144ac27a0ecSDave Kleikamp 
5145dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5146ac27a0ecSDave Kleikamp 	if (j_errno) {
5147ac27a0ecSDave Kleikamp 		char nbuf[16];
5148ac27a0ecSDave Kleikamp 
5149617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
515012062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5151ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
515212062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5153ac27a0ecSDave Kleikamp 
5154617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5155617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5156e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5157ac27a0ecSDave Kleikamp 
5158dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5159d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5160ac27a0ecSDave Kleikamp 	}
5161ac27a0ecSDave Kleikamp }
5162ac27a0ecSDave Kleikamp 
5163ac27a0ecSDave Kleikamp /*
5164ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5165ac27a0ecSDave Kleikamp  * and wait on the commit.
5166ac27a0ecSDave Kleikamp  */
5167617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5168ac27a0ecSDave Kleikamp {
5169ac27a0ecSDave Kleikamp 	journal_t *journal;
5170ac27a0ecSDave Kleikamp 
5171bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5172ac27a0ecSDave Kleikamp 		return 0;
5173ac27a0ecSDave Kleikamp 
5174617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5175b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5176ac27a0ecSDave Kleikamp }
5177ac27a0ecSDave Kleikamp 
5178617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5179ac27a0ecSDave Kleikamp {
518014ce0cb4STheodore Ts'o 	int ret = 0;
51819eddacf9SJan Kara 	tid_t target;
518206a407f1SDmitry Monakhov 	bool needs_barrier = false;
51838d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5184ac27a0ecSDave Kleikamp 
518549598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
51860db1ff22STheodore Ts'o 		return 0;
51870db1ff22STheodore Ts'o 
51889bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
51892e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5190a1177825SJan Kara 	/*
5191a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5192a1177825SJan Kara 	 * no dirty dquots
5193a1177825SJan Kara 	 */
5194a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
519506a407f1SDmitry Monakhov 	/*
519606a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
519706a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
519806a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
519906a407f1SDmitry Monakhov 	 */
5200bda32530STheodore Ts'o 	if (sbi->s_journal) {
520106a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
520206a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
520306a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
520406a407f1SDmitry Monakhov 			needs_barrier = true;
520506a407f1SDmitry Monakhov 
52068d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5207ac27a0ecSDave Kleikamp 			if (wait)
5208bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5209bda32530STheodore Ts'o 							   target);
52100390131bSFrank Mayhar 		}
5211bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5212bda32530STheodore Ts'o 		needs_barrier = true;
521306a407f1SDmitry Monakhov 	if (needs_barrier) {
521406a407f1SDmitry Monakhov 		int err;
521506a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
521606a407f1SDmitry Monakhov 		if (!ret)
521706a407f1SDmitry Monakhov 			ret = err;
521806a407f1SDmitry Monakhov 	}
521906a407f1SDmitry Monakhov 
522006a407f1SDmitry Monakhov 	return ret;
522106a407f1SDmitry Monakhov }
522206a407f1SDmitry Monakhov 
5223ac27a0ecSDave Kleikamp /*
5224ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5225ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5226be4f27d3SYongqiang Yang  *
5227be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
52288e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
52298e8ad8a5SJan Kara  * modifications.
5230ac27a0ecSDave Kleikamp  */
5231c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5232ac27a0ecSDave Kleikamp {
5233c4be0c1dSTakashi Sato 	int error = 0;
5234c4be0c1dSTakashi Sato 	journal_t *journal;
5235ac27a0ecSDave Kleikamp 
5236bc98a42cSDavid Howells 	if (sb_rdonly(sb))
52379ca92389STheodore Ts'o 		return 0;
52389ca92389STheodore Ts'o 
5239c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5240ac27a0ecSDave Kleikamp 
5241bb044576STheodore Ts'o 	if (journal) {
5242ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5243dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
52447ffe1ea8SHidehiro Kawai 
52457ffe1ea8SHidehiro Kawai 		/*
5246bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5247bb044576STheodore Ts'o 		 * flush the journal.
52487ffe1ea8SHidehiro Kawai 		 */
5249c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
52506b0310fbSEric Sandeen 		if (error < 0)
52516b0310fbSEric Sandeen 			goto out;
5252ac27a0ecSDave Kleikamp 
5253ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5254e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5255c642dc9eSEric Sandeen 	}
5256c642dc9eSEric Sandeen 
5257e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
52586b0310fbSEric Sandeen out:
5259bb044576STheodore Ts'o 	if (journal)
52608e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5261bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
52626b0310fbSEric Sandeen 	return error;
5263ac27a0ecSDave Kleikamp }
5264ac27a0ecSDave Kleikamp 
5265ac27a0ecSDave Kleikamp /*
5266ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5267ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5268ac27a0ecSDave Kleikamp  */
5269c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5270ac27a0ecSDave Kleikamp {
5271bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
52729ca92389STheodore Ts'o 		return 0;
52739ca92389STheodore Ts'o 
5274c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
52759ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5276e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5277c642dc9eSEric Sandeen 	}
5278c642dc9eSEric Sandeen 
5279e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5280c4be0c1dSTakashi Sato 	return 0;
5281ac27a0ecSDave Kleikamp }
5282ac27a0ecSDave Kleikamp 
5283673c6100STheodore Ts'o /*
5284673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5285673c6100STheodore Ts'o  */
5286673c6100STheodore Ts'o struct ext4_mount_options {
5287673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5288a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
528908cefc7aSEric W. Biederman 	kuid_t s_resuid;
529008cefc7aSEric W. Biederman 	kgid_t s_resgid;
5291673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5292673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5293673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5294673c6100STheodore Ts'o 	int s_jquota_fmt;
5295a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5296673c6100STheodore Ts'o #endif
5297673c6100STheodore Ts'o };
5298673c6100STheodore Ts'o 
5299617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5300ac27a0ecSDave Kleikamp {
5301617ba13bSMingming Cao 	struct ext4_super_block *es;
5302617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5303ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5304617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5305c79d967dSChristoph Hellwig 	int enable_quota = 0;
53068a266467STheodore Ts'o 	ext4_group_t g;
5307b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5308c5e06d10SJohann Lombardi 	int err = 0;
5309ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
531003dafb5fSChen Gang 	int i, j;
531133458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5312ac27a0ecSDave Kleikamp #endif
5313d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5314ac27a0ecSDave Kleikamp 
531521ac738eSChengguang Xu 	if (data && !orig_data)
531621ac738eSChengguang Xu 		return -ENOMEM;
531721ac738eSChengguang Xu 
5318ac27a0ecSDave Kleikamp 	/* Store the original options */
5319ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5320ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5321a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5322ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5323ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5324ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
532530773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
532630773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5327ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5328ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5329a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
533003dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
533133458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
533233458eabSTheodore Ts'o 
533333458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
533403dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
533503dafb5fSChen Gang 				for (j = 0; j < i; j++)
533603dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
53373e36a163SWei Yongjun 				kfree(orig_data);
533803dafb5fSChen Gang 				return -ENOMEM;
533903dafb5fSChen Gang 			}
534003dafb5fSChen Gang 		} else
534103dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5342ac27a0ecSDave Kleikamp #endif
5343b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5344b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5345ac27a0ecSDave Kleikamp 
5346661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5347ac27a0ecSDave Kleikamp 		err = -EINVAL;
5348ac27a0ecSDave Kleikamp 		goto restore_opts;
5349ac27a0ecSDave Kleikamp 	}
5350ac27a0ecSDave Kleikamp 
53516b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
53526b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
53536b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
53542d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
53552d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5356c6d3d56dSDarrick J. Wong 	}
5357c6d3d56dSDarrick J. Wong 
53586ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
53596ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
53606ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53616ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
53626ae6514bSPiotr Sarna 			err = -EINVAL;
53636ae6514bSPiotr Sarna 			goto restore_opts;
53646ae6514bSPiotr Sarna 		}
53656ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
53666ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53676ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
53686ae6514bSPiotr Sarna 			err = -EINVAL;
53696ae6514bSPiotr Sarna 			goto restore_opts;
53706ae6514bSPiotr Sarna 		}
5371923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
5372923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
5373923ae0ffSRoss Zwisler 				 "both data=journal and dax");
5374923ae0ffSRoss Zwisler 			err = -EINVAL;
5375923ae0ffSRoss Zwisler 			goto restore_opts;
5376923ae0ffSRoss Zwisler 		}
5377ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5378ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5379ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5380ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5381ab04df78SJan Kara 			err = -EINVAL;
5382ab04df78SJan Kara 			goto restore_opts;
5383ab04df78SJan Kara 		}
5384923ae0ffSRoss Zwisler 	}
5385923ae0ffSRoss Zwisler 
5386cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5387cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5388cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5389cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5390cdb7ee4cSTahsin Erdogan 	}
5391cdb7ee4cSTahsin Erdogan 
5392923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
5393923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
5394923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
5395923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
53966ae6514bSPiotr Sarna 	}
53976ae6514bSPiotr Sarna 
53984ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
5399c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
5400ac27a0ecSDave Kleikamp 
54011751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
54021751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5403ac27a0ecSDave Kleikamp 
5404ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5405ac27a0ecSDave Kleikamp 
5406b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5407617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5408b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5409b3881f74STheodore Ts'o 	}
5410ac27a0ecSDave Kleikamp 
54111751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
54121751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5413a2fd66d0STheodore Ts'o 
54141751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
54154ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5416ac27a0ecSDave Kleikamp 			err = -EROFS;
5417ac27a0ecSDave Kleikamp 			goto restore_opts;
5418ac27a0ecSDave Kleikamp 		}
5419ac27a0ecSDave Kleikamp 
54201751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
542138c03b34STheodore Ts'o 			err = sync_filesystem(sb);
542238c03b34STheodore Ts'o 			if (err < 0)
542338c03b34STheodore Ts'o 				goto restore_opts;
54240f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
54250f0dd62fSChristoph Hellwig 			if (err < 0)
5426c79d967dSChristoph Hellwig 				goto restore_opts;
5427c79d967dSChristoph Hellwig 
5428ac27a0ecSDave Kleikamp 			/*
5429ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5430ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5431ac27a0ecSDave Kleikamp 			 */
54321751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5433ac27a0ecSDave Kleikamp 
5434ac27a0ecSDave Kleikamp 			/*
5435ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5436ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5437ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5438ac27a0ecSDave Kleikamp 			 */
5439617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5440617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5441ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5442ac27a0ecSDave Kleikamp 
5443a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5444617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
54452dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
54462dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5447ac27a0ecSDave Kleikamp 		} else {
5448a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5449e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
54502cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5451ac27a0ecSDave Kleikamp 				err = -EROFS;
5452ac27a0ecSDave Kleikamp 				goto restore_opts;
5453ac27a0ecSDave Kleikamp 			}
5454ead6596bSEric Sandeen 			/*
54558a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
54560b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
54578a266467STheodore Ts'o 			 */
54588a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
54598a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
54608a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
54618a266467STheodore Ts'o 
5462feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5463b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5464b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5465e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
54668a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
54676a797d27SDarrick J. Wong 					err = -EFSBADCRC;
54688a266467STheodore Ts'o 					goto restore_opts;
54698a266467STheodore Ts'o 				}
54708a266467STheodore Ts'o 			}
54718a266467STheodore Ts'o 
54728a266467STheodore Ts'o 			/*
5473ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5474ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5475ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5476ead6596bSEric Sandeen 			 */
5477ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5478b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5479ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5480ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5481b31e1552SEric Sandeen 				       "umount/remount instead");
5482ead6596bSEric Sandeen 				err = -EINVAL;
5483ead6596bSEric Sandeen 				goto restore_opts;
5484ead6596bSEric Sandeen 			}
5485ead6596bSEric Sandeen 
5486ac27a0ecSDave Kleikamp 			/*
5487ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5488ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5489ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5490ac27a0ecSDave Kleikamp 			 * the partition.)
5491ac27a0ecSDave Kleikamp 			 */
54920390131bSFrank Mayhar 			if (sbi->s_journal)
5493617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5494ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5495c89128a0SJaegeuk Kim 
5496c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5497c89128a0SJaegeuk Kim 			if (err)
5498c89128a0SJaegeuk Kim 				goto restore_opts;
5499c89128a0SJaegeuk Kim 
55001751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5501e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5502c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5503c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5504c5e06d10SJohann Lombardi 					err = -EROFS;
5505c5e06d10SJohann Lombardi 					goto restore_opts;
5506c5e06d10SJohann Lombardi 				}
5507c79d967dSChristoph Hellwig 			enable_quota = 1;
5508ac27a0ecSDave Kleikamp 		}
5509ac27a0ecSDave Kleikamp 	}
5510bfff6873SLukas Czerner 
5511bfff6873SLukas Czerner 	/*
5512bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5513bfff6873SLukas Czerner 	 * current settings
5514bfff6873SLukas Czerner 	 */
5515bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5516bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5517bfff6873SLukas Czerner 	else {
5518bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5519bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5520bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5521bfff6873SLukas Czerner 	}
5522bfff6873SLukas Czerner 
55236fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5524c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5525c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5526c89128a0SJaegeuk Kim 		if (err)
5527c89128a0SJaegeuk Kim 			goto restore_opts;
5528c89128a0SJaegeuk Kim 	}
55290390131bSFrank Mayhar 
5530ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5531ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5532a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5533ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
55347c319d32SAditya Kali 	if (enable_quota) {
55357c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
55360f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5537e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
55387c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
553907724f98STheodore Ts'o 			if (err)
55407c319d32SAditya Kali 				goto restore_opts;
55417c319d32SAditya Kali 		}
55427c319d32SAditya Kali 	}
55437c319d32SAditya Kali #endif
5544d4c402d9SCurt Wohlgemuth 
55451751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5546d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5547d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5548ac27a0ecSDave Kleikamp 	return 0;
55490b8e58a1SAndreas Dilger 
5550ac27a0ecSDave Kleikamp restore_opts:
5551ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5552ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5553a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5554ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5555ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5556ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
555730773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
555830773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5559ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5560ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5561a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
556233458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
556333458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5564ac27a0ecSDave Kleikamp 	}
556533458eabSTheodore Ts'o 	synchronize_rcu();
556633458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
556733458eabSTheodore Ts'o 		kfree(to_free[i]);
5568ac27a0ecSDave Kleikamp #endif
5569d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5570ac27a0ecSDave Kleikamp 	return err;
5571ac27a0ecSDave Kleikamp }
5572ac27a0ecSDave Kleikamp 
5573689c958cSLi Xi #ifdef CONFIG_QUOTA
5574689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5575689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5576689c958cSLi Xi {
5577689c958cSLi Xi 	struct kqid qid;
5578689c958cSLi Xi 	struct dquot *dquot;
5579689c958cSLi Xi 	u64 limit;
5580689c958cSLi Xi 	u64 curblock;
5581689c958cSLi Xi 
5582689c958cSLi Xi 	qid = make_kqid_projid(projid);
5583689c958cSLi Xi 	dquot = dqget(sb, qid);
5584689c958cSLi Xi 	if (IS_ERR(dquot))
5585689c958cSLi Xi 		return PTR_ERR(dquot);
55867b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5587689c958cSLi Xi 
5588689c958cSLi Xi 	limit = (dquot->dq_dqb.dqb_bsoftlimit ?
5589689c958cSLi Xi 		 dquot->dq_dqb.dqb_bsoftlimit :
5590689c958cSLi Xi 		 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
5591689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5592f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5593f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5594689c958cSLi Xi 		buf->f_blocks = limit;
5595689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5596689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5597689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5598689c958cSLi Xi 	}
5599689c958cSLi Xi 
5600689c958cSLi Xi 	limit = dquot->dq_dqb.dqb_isoftlimit ?
5601689c958cSLi Xi 		dquot->dq_dqb.dqb_isoftlimit :
5602689c958cSLi Xi 		dquot->dq_dqb.dqb_ihardlimit;
5603689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5604689c958cSLi Xi 		buf->f_files = limit;
5605689c958cSLi Xi 		buf->f_ffree =
5606689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5607689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5608689c958cSLi Xi 	}
5609689c958cSLi Xi 
56107b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5611689c958cSLi Xi 	dqput(dquot);
5612689c958cSLi Xi 	return 0;
5613689c958cSLi Xi }
5614689c958cSLi Xi #endif
5615689c958cSLi Xi 
5616617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5617ac27a0ecSDave Kleikamp {
5618ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5619617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5620617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
562127dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5622960cc398SPekka Enberg 	u64 fsid;
5623d02a9391SKazuya Mio 	s64 bfree;
562427dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5625ac27a0ecSDave Kleikamp 
5626952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5627952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5628ac27a0ecSDave Kleikamp 
5629617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5630ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5631b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
563257042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
563357042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5634d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
563557042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
563627dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
563727dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
563827dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5639ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5640ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
564152d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5642617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5643960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5644960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5645960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5646960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
56470b8e58a1SAndreas Dilger 
5648689c958cSLi Xi #ifdef CONFIG_QUOTA
5649689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5650689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5651689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5652689c958cSLi Xi #endif
5653ac27a0ecSDave Kleikamp 	return 0;
5654ac27a0ecSDave Kleikamp }
5655ac27a0ecSDave Kleikamp 
5656ac27a0ecSDave Kleikamp 
5657ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5658ac27a0ecSDave Kleikamp 
5659bc8230eeSJan Kara /*
5660bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5661bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5662bc8230eeSJan Kara  */
5663ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5664ac27a0ecSDave Kleikamp {
56654c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5666ac27a0ecSDave Kleikamp }
5667ac27a0ecSDave Kleikamp 
5668617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5669ac27a0ecSDave Kleikamp {
5670ac27a0ecSDave Kleikamp 	int ret, err;
5671ac27a0ecSDave Kleikamp 	handle_t *handle;
5672ac27a0ecSDave Kleikamp 	struct inode *inode;
5673ac27a0ecSDave Kleikamp 
5674ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
56759924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5676617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5677ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5678ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5679ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5680617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5681ac27a0ecSDave Kleikamp 	if (!ret)
5682ac27a0ecSDave Kleikamp 		ret = err;
5683ac27a0ecSDave Kleikamp 	return ret;
5684ac27a0ecSDave Kleikamp }
5685ac27a0ecSDave Kleikamp 
5686617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5687ac27a0ecSDave Kleikamp {
5688ac27a0ecSDave Kleikamp 	int ret, err;
5689ac27a0ecSDave Kleikamp 	handle_t *handle;
5690ac27a0ecSDave Kleikamp 
56919924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5692617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5693ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5694ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5695ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5696617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5697ac27a0ecSDave Kleikamp 	if (!ret)
5698ac27a0ecSDave Kleikamp 		ret = err;
5699ac27a0ecSDave Kleikamp 	return ret;
5700ac27a0ecSDave Kleikamp }
5701ac27a0ecSDave Kleikamp 
5702617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5703ac27a0ecSDave Kleikamp {
5704ac27a0ecSDave Kleikamp 	int ret, err;
5705ac27a0ecSDave Kleikamp 	handle_t *handle;
5706ac27a0ecSDave Kleikamp 
57079924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5708617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
57099c3013e9SJan Kara 	if (IS_ERR(handle)) {
57109c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
57119c3013e9SJan Kara 		dquot_release(dquot);
5712ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
57139c3013e9SJan Kara 	}
5714ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5715617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5716ac27a0ecSDave Kleikamp 	if (!ret)
5717ac27a0ecSDave Kleikamp 		ret = err;
5718ac27a0ecSDave Kleikamp 	return ret;
5719ac27a0ecSDave Kleikamp }
5720ac27a0ecSDave Kleikamp 
5721617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5722ac27a0ecSDave Kleikamp {
5723262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5724262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5725262b4662SJan Kara 
57262c8be6b2SJan Kara 	/* Are we journaling quotas? */
5727e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5728262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5729ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5730617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5731ac27a0ecSDave Kleikamp 	} else {
5732ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5733ac27a0ecSDave Kleikamp 	}
5734ac27a0ecSDave Kleikamp }
5735ac27a0ecSDave Kleikamp 
5736617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5737ac27a0ecSDave Kleikamp {
5738ac27a0ecSDave Kleikamp 	int ret, err;
5739ac27a0ecSDave Kleikamp 	handle_t *handle;
5740ac27a0ecSDave Kleikamp 
5741ac27a0ecSDave Kleikamp 	/* Data block + inode block */
57422b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5743ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5744ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5745ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5746617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5747ac27a0ecSDave Kleikamp 	if (!ret)
5748ac27a0ecSDave Kleikamp 		ret = err;
5749ac27a0ecSDave Kleikamp 	return ret;
5750ac27a0ecSDave Kleikamp }
5751ac27a0ecSDave Kleikamp 
5752ac27a0ecSDave Kleikamp /*
5753ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5754ac27a0ecSDave Kleikamp  * the quota file and such...
5755ac27a0ecSDave Kleikamp  */
5756617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5757ac27a0ecSDave Kleikamp {
575833458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5759617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5760ac27a0ecSDave Kleikamp }
5761ac27a0ecSDave Kleikamp 
5762daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5763daf647d2STheodore Ts'o {
5764daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5765daf647d2STheodore Ts'o 
5766daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5767daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5768daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5769daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5770daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5771daf647d2STheodore Ts'o 	 */
5772daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5773daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5774daf647d2STheodore Ts'o }
5775daf647d2STheodore Ts'o 
5776ac27a0ecSDave Kleikamp /*
5777ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5778ac27a0ecSDave Kleikamp  */
5779617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
57808c54ca9cSAl Viro 			 const struct path *path)
5781ac27a0ecSDave Kleikamp {
5782ac27a0ecSDave Kleikamp 	int err;
5783ac27a0ecSDave Kleikamp 
5784ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5785ac27a0ecSDave Kleikamp 		return -EINVAL;
57860623543bSJan Kara 
5787ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5788d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5789ac27a0ecSDave Kleikamp 		return -EXDEV;
57900623543bSJan Kara 	/* Journaling quota? */
57910623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
57922b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5793f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5794b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5795b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5796b31e1552SEric Sandeen 				"Journaled quota will not work");
579791389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
579891389240SJan Kara 	} else {
579991389240SJan Kara 		/*
580091389240SJan Kara 		 * Clear the flag just in case mount options changed since
580191389240SJan Kara 		 * last time.
580291389240SJan Kara 		 */
580391389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
58040623543bSJan Kara 	}
58050623543bSJan Kara 
58060623543bSJan Kara 	/*
58070623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
58080623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
58090623543bSJan Kara 	 */
58100390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
58112b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
58120623543bSJan Kara 		/*
58130623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
58140623543bSJan Kara 		 * otherwise be livelocked...
58150623543bSJan Kara 		 */
58160623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
58177ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
58180623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5819f00c9e44SJan Kara 		if (err)
58207ffe1ea8SHidehiro Kawai 			return err;
58217ffe1ea8SHidehiro Kawai 	}
5822957153fcSJan Kara 
5823daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5824daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5825957153fcSJan Kara 	if (err) {
5826daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5827daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5828957153fcSJan Kara 	} else {
5829957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5830957153fcSJan Kara 		handle_t *handle;
5831957153fcSJan Kara 
583261a92987SJan Kara 		/*
583361a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
583461a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
583561a92987SJan Kara 		 * are already enabled and this is not a hard failure.
583661a92987SJan Kara 		 */
5837957153fcSJan Kara 		inode_lock(inode);
5838957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5839957153fcSJan Kara 		if (IS_ERR(handle))
5840957153fcSJan Kara 			goto unlock_inode;
5841957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5842957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5843957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
5844957153fcSJan Kara 		ext4_mark_inode_dirty(handle, inode);
5845957153fcSJan Kara 		ext4_journal_stop(handle);
5846957153fcSJan Kara 	unlock_inode:
5847957153fcSJan Kara 		inode_unlock(inode);
5848957153fcSJan Kara 	}
5849daf647d2STheodore Ts'o 	return err;
5850ac27a0ecSDave Kleikamp }
5851ac27a0ecSDave Kleikamp 
58527c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
58537c319d32SAditya Kali 			     unsigned int flags)
58547c319d32SAditya Kali {
58557c319d32SAditya Kali 	int err;
58567c319d32SAditya Kali 	struct inode *qf_inode;
5857a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58587c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5859689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5860689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58617c319d32SAditya Kali 	};
58627c319d32SAditya Kali 
5863e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
58647c319d32SAditya Kali 
58657c319d32SAditya Kali 	if (!qf_inums[type])
58667c319d32SAditya Kali 		return -EPERM;
58677c319d32SAditya Kali 
58688a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
58697c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
58707c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
58717c319d32SAditya Kali 		return PTR_ERR(qf_inode);
58727c319d32SAditya Kali 	}
58737c319d32SAditya Kali 
5874bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5875bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5876daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
58777212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
5878daf647d2STheodore Ts'o 	if (err)
5879daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
588061157b24SPan Bian 	iput(qf_inode);
58817c319d32SAditya Kali 
58827c319d32SAditya Kali 	return err;
58837c319d32SAditya Kali }
58847c319d32SAditya Kali 
58857c319d32SAditya Kali /* Enable usage tracking for all quota types. */
58867c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
58877c319d32SAditya Kali {
58887c319d32SAditya Kali 	int type, err = 0;
5889a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58907c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5891689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5892689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58937c319d32SAditya Kali 	};
589449da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
589549da9392SJan Kara 		test_opt(sb, USRQUOTA),
589649da9392SJan Kara 		test_opt(sb, GRPQUOTA),
589749da9392SJan Kara 		test_opt(sb, PRJQUOTA),
589849da9392SJan Kara 	};
58997c319d32SAditya Kali 
590091389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
5901a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
59027c319d32SAditya Kali 		if (qf_inums[type]) {
59037c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
590449da9392SJan Kara 				DQUOT_USAGE_ENABLED |
590549da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
59067c319d32SAditya Kali 			if (err) {
59077c319d32SAditya Kali 				ext4_warning(sb,
590872ba7450STheodore Ts'o 					"Failed to enable quota tracking "
590972ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
591072ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
59117f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
59127f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
59137f144fd0SJunichi Uekawa 
59147c319d32SAditya Kali 				return err;
59157c319d32SAditya Kali 			}
59167c319d32SAditya Kali 		}
59177c319d32SAditya Kali 	}
59187c319d32SAditya Kali 	return 0;
59197c319d32SAditya Kali }
59207c319d32SAditya Kali 
5921ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5922ca0e05e4SDmitry Monakhov {
592321f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
592421f97697SJan Kara 	handle_t *handle;
5925957153fcSJan Kara 	int err;
592621f97697SJan Kara 
592787009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
592887009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
592987009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5930ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5931ca0e05e4SDmitry Monakhov 
5932957153fcSJan Kara 	if (!inode || !igrab(inode))
59330b268590SAmir Goldstein 		goto out;
59340b268590SAmir Goldstein 
5935957153fcSJan Kara 	err = dquot_quota_off(sb, type);
5936964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
5937957153fcSJan Kara 		goto out_put;
5938957153fcSJan Kara 
5939957153fcSJan Kara 	inode_lock(inode);
594061a92987SJan Kara 	/*
594161a92987SJan Kara 	 * Update modification times of quota files when userspace can
594261a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
594361a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
594461a92987SJan Kara 	 */
59459924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
594621f97697SJan Kara 	if (IS_ERR(handle))
5947957153fcSJan Kara 		goto out_unlock;
5948957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
5949957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
5950eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
595121f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
595221f97697SJan Kara 	ext4_journal_stop(handle);
5953957153fcSJan Kara out_unlock:
5954957153fcSJan Kara 	inode_unlock(inode);
5955957153fcSJan Kara out_put:
5956964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
5957957153fcSJan Kara 	iput(inode);
5958957153fcSJan Kara 	return err;
595921f97697SJan Kara out:
5960ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5961ca0e05e4SDmitry Monakhov }
5962ca0e05e4SDmitry Monakhov 
5963ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5964ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5965ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5966ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5967617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5968ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5969ac27a0ecSDave Kleikamp {
5970ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5971725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5972ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5973ac27a0ecSDave Kleikamp 	int tocopy;
5974ac27a0ecSDave Kleikamp 	size_t toread;
5975ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5976ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5977ac27a0ecSDave Kleikamp 
5978ac27a0ecSDave Kleikamp 	if (off > i_size)
5979ac27a0ecSDave Kleikamp 		return 0;
5980ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5981ac27a0ecSDave Kleikamp 		len = i_size-off;
5982ac27a0ecSDave Kleikamp 	toread = len;
5983ac27a0ecSDave Kleikamp 	while (toread > 0) {
5984ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5985ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
59861c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
59871c215028STheodore Ts'o 		if (IS_ERR(bh))
59881c215028STheodore Ts'o 			return PTR_ERR(bh);
5989ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5990ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5991ac27a0ecSDave Kleikamp 		else
5992ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5993ac27a0ecSDave Kleikamp 		brelse(bh);
5994ac27a0ecSDave Kleikamp 		offset = 0;
5995ac27a0ecSDave Kleikamp 		toread -= tocopy;
5996ac27a0ecSDave Kleikamp 		data += tocopy;
5997ac27a0ecSDave Kleikamp 		blk++;
5998ac27a0ecSDave Kleikamp 	}
5999ac27a0ecSDave Kleikamp 	return len;
6000ac27a0ecSDave Kleikamp }
6001ac27a0ecSDave Kleikamp 
6002ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6003ac27a0ecSDave Kleikamp  * enough credits) */
6004617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6005ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6006ac27a0ecSDave Kleikamp {
6007ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6008725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
60091c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
6010c5e298aeSTheodore Ts'o 	int retries = 0;
6011ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6012ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6013ac27a0ecSDave Kleikamp 
60140390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6015b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6016b31e1552SEric Sandeen 			" cancelled because transaction is not started",
60179c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
60189c3013e9SJan Kara 		return -EIO;
60199c3013e9SJan Kara 	}
602067eeb568SDmitry Monakhov 	/*
602167eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
602267eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
602367eeb568SDmitry Monakhov 	 */
602467eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
602567eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
602667eeb568SDmitry Monakhov 			" cancelled because not block aligned",
602767eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
602867eeb568SDmitry Monakhov 		return -EIO;
602967eeb568SDmitry Monakhov 	}
603067eeb568SDmitry Monakhov 
6031c5e298aeSTheodore Ts'o 	do {
6032c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6033c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6034c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
6035c5e298aeSTheodore Ts'o 	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
6036c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
60371c215028STheodore Ts'o 	if (IS_ERR(bh))
60381c215028STheodore Ts'o 		return PTR_ERR(bh);
6039ac27a0ecSDave Kleikamp 	if (!bh)
6040ac27a0ecSDave Kleikamp 		goto out;
60415d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6042617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6043ac27a0ecSDave Kleikamp 	if (err) {
6044ac27a0ecSDave Kleikamp 		brelse(bh);
60451c215028STheodore Ts'o 		return err;
6046ac27a0ecSDave Kleikamp 	}
6047ac27a0ecSDave Kleikamp 	lock_buffer(bh);
604867eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6049ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6050ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
60510390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6052ac27a0ecSDave Kleikamp 	brelse(bh);
6053ac27a0ecSDave Kleikamp out:
605467eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
605567eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6056617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
6057617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
605821f97697SJan Kara 	}
605967eeb568SDmitry Monakhov 	return len;
6060ac27a0ecSDave Kleikamp }
6061ac27a0ecSDave Kleikamp #endif
6062ac27a0ecSDave Kleikamp 
6063152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6064152a0836SAl Viro 		       const char *dev_name, void *data)
6065ac27a0ecSDave Kleikamp {
6066152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6067ac27a0ecSDave Kleikamp }
6068ac27a0ecSDave Kleikamp 
6069c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
607024b58424STheodore Ts'o static inline void register_as_ext2(void)
607124b58424STheodore Ts'o {
607224b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
607324b58424STheodore Ts'o 	if (err)
607424b58424STheodore Ts'o 		printk(KERN_WARNING
607524b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
607624b58424STheodore Ts'o }
607724b58424STheodore Ts'o 
607824b58424STheodore Ts'o static inline void unregister_as_ext2(void)
607924b58424STheodore Ts'o {
608024b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
608124b58424STheodore Ts'o }
60822035e776STheodore Ts'o 
60832035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
60842035e776STheodore Ts'o {
6085e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
60862035e776STheodore Ts'o 		return 0;
6087bc98a42cSDavid Howells 	if (sb_rdonly(sb))
60882035e776STheodore Ts'o 		return 1;
6089e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
60902035e776STheodore Ts'o 		return 0;
60912035e776STheodore Ts'o 	return 1;
60922035e776STheodore Ts'o }
609324b58424STheodore Ts'o #else
609424b58424STheodore Ts'o static inline void register_as_ext2(void) { }
609524b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
60962035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
609724b58424STheodore Ts'o #endif
609824b58424STheodore Ts'o 
609924b58424STheodore Ts'o static inline void register_as_ext3(void)
610024b58424STheodore Ts'o {
610124b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
610224b58424STheodore Ts'o 	if (err)
610324b58424STheodore Ts'o 		printk(KERN_WARNING
610424b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
610524b58424STheodore Ts'o }
610624b58424STheodore Ts'o 
610724b58424STheodore Ts'o static inline void unregister_as_ext3(void)
610824b58424STheodore Ts'o {
610924b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
611024b58424STheodore Ts'o }
61112035e776STheodore Ts'o 
61122035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
61132035e776STheodore Ts'o {
6114e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
61152035e776STheodore Ts'o 		return 0;
6116e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
61172035e776STheodore Ts'o 		return 0;
6118bc98a42cSDavid Howells 	if (sb_rdonly(sb))
61192035e776STheodore Ts'o 		return 1;
6120e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
61212035e776STheodore Ts'o 		return 0;
61222035e776STheodore Ts'o 	return 1;
61232035e776STheodore Ts'o }
612424b58424STheodore Ts'o 
612503010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6126ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
612703010a33STheodore Ts'o 	.name		= "ext4",
6128152a0836SAl Viro 	.mount		= ext4_mount,
6129ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6130ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6131ac27a0ecSDave Kleikamp };
61327f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6133ac27a0ecSDave Kleikamp 
6134e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6135e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6136e9e3bcecSEric Sandeen 
61375dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6138ac27a0ecSDave Kleikamp {
6139e9e3bcecSEric Sandeen 	int i, err;
6140c9de560dSAlex Tomas 
6141e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
614207c0c5d8SAl Viro 	ext4_li_info = NULL;
614307c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
614407c0c5d8SAl Viro 
61459a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
614612e9b892SDmitry Monakhov 	ext4_check_flag_values();
6147e9e3bcecSEric Sandeen 
6148e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6149e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6150e9e3bcecSEric Sandeen 
615151865fdaSZheng Liu 	err = ext4_init_es();
61526fd058f7STheodore Ts'o 	if (err)
61536fd058f7STheodore Ts'o 		return err;
615451865fdaSZheng Liu 
61551dc0aa46SEric Whitney 	err = ext4_init_pending();
61561dc0aa46SEric Whitney 	if (err)
615722cfe4b4SEric Biggers 		goto out7;
615822cfe4b4SEric Biggers 
615922cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
616022cfe4b4SEric Biggers 	if (err)
61611dc0aa46SEric Whitney 		goto out6;
61621dc0aa46SEric Whitney 
616351865fdaSZheng Liu 	err = ext4_init_pageio();
616451865fdaSZheng Liu 	if (err)
6165b5799018STheodore Ts'o 		goto out5;
616651865fdaSZheng Liu 
61675dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6168bd2d0210STheodore Ts'o 	if (err)
6169b5799018STheodore Ts'o 		goto out4;
6170857ac889SLukas Czerner 
6171b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6172dd68314cSTheodore Ts'o 	if (err)
6173b5799018STheodore Ts'o 		goto out3;
6174857ac889SLukas Czerner 
61755dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6176ac27a0ecSDave Kleikamp 	if (err)
6177c9de560dSAlex Tomas 		goto out2;
6178ac27a0ecSDave Kleikamp 	err = init_inodecache();
6179ac27a0ecSDave Kleikamp 	if (err)
6180ac27a0ecSDave Kleikamp 		goto out1;
618124b58424STheodore Ts'o 	register_as_ext3();
61822035e776STheodore Ts'o 	register_as_ext2();
618303010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6184ac27a0ecSDave Kleikamp 	if (err)
6185ac27a0ecSDave Kleikamp 		goto out;
6186bfff6873SLukas Czerner 
6187ac27a0ecSDave Kleikamp 	return 0;
6188ac27a0ecSDave Kleikamp out:
618924b58424STheodore Ts'o 	unregister_as_ext2();
619024b58424STheodore Ts'o 	unregister_as_ext3();
6191ac27a0ecSDave Kleikamp 	destroy_inodecache();
6192ac27a0ecSDave Kleikamp out1:
61935dabfc78STheodore Ts'o 	ext4_exit_mballoc();
61949c191f70ST Makphaibulchoke out2:
6195b5799018STheodore Ts'o 	ext4_exit_sysfs();
6196b5799018STheodore Ts'o out3:
6197dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6198b5799018STheodore Ts'o out4:
61995dabfc78STheodore Ts'o 	ext4_exit_pageio();
6200b5799018STheodore Ts'o out5:
620122cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
62021dc0aa46SEric Whitney out6:
620322cfe4b4SEric Biggers 	ext4_exit_pending();
620422cfe4b4SEric Biggers out7:
620551865fdaSZheng Liu 	ext4_exit_es();
620651865fdaSZheng Liu 
6207ac27a0ecSDave Kleikamp 	return err;
6208ac27a0ecSDave Kleikamp }
6209ac27a0ecSDave Kleikamp 
62105dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6211ac27a0ecSDave Kleikamp {
6212bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
621324b58424STheodore Ts'o 	unregister_as_ext2();
621424b58424STheodore Ts'o 	unregister_as_ext3();
621503010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6216ac27a0ecSDave Kleikamp 	destroy_inodecache();
62175dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6218b5799018STheodore Ts'o 	ext4_exit_sysfs();
62195dabfc78STheodore Ts'o 	ext4_exit_system_zone();
62205dabfc78STheodore Ts'o 	ext4_exit_pageio();
622122cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6222dd12ed14SEric Sandeen 	ext4_exit_es();
62231dc0aa46SEric Whitney 	ext4_exit_pending();
6224ac27a0ecSDave Kleikamp }
6225ac27a0ecSDave Kleikamp 
6226ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
622783982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6228ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
62297ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
62305dabfc78STheodore Ts'o module_init(ext4_init_fs)
62315dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6232