xref: /linux/fs/ext4/super.c (revision 357fdad075abb976ee43f2dd450c40dcef4b7e2d)
1ac27a0ecSDave Kleikamp /*
2617ba13bSMingming Cao  *  linux/fs/ext4/super.c
3ac27a0ecSDave Kleikamp  *
4ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
5ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
6ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
7ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
8ac27a0ecSDave Kleikamp  *
9ac27a0ecSDave Kleikamp  *  from
10ac27a0ecSDave Kleikamp  *
11ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
12ac27a0ecSDave Kleikamp  *
13ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
14ac27a0ecSDave Kleikamp  *
15ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
16ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
17ac27a0ecSDave Kleikamp  */
18ac27a0ecSDave Kleikamp 
19ac27a0ecSDave Kleikamp #include <linux/module.h>
20ac27a0ecSDave Kleikamp #include <linux/string.h>
21ac27a0ecSDave Kleikamp #include <linux/fs.h>
22ac27a0ecSDave Kleikamp #include <linux/time.h>
23c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
24ac27a0ecSDave Kleikamp #include <linux/slab.h>
25ac27a0ecSDave Kleikamp #include <linux/init.h>
26ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
2766114cadSTejun Heo #include <linux/backing-dev.h>
28ac27a0ecSDave Kleikamp #include <linux/parser.h>
29ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
30a5694255SChristoph Hellwig #include <linux/exportfs.h>
31ac27a0ecSDave Kleikamp #include <linux/vfs.h>
32ac27a0ecSDave Kleikamp #include <linux/random.h>
33ac27a0ecSDave Kleikamp #include <linux/mount.h>
34ac27a0ecSDave Kleikamp #include <linux/namei.h>
35ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
36ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
373197ebdbSTheodore Ts'o #include <linux/ctype.h>
381330593eSVignesh Babu #include <linux/log2.h>
39717d50e4SAndreas Dilger #include <linux/crc16.h>
40ef510424SDan Williams #include <linux/dax.h>
417abc52c2SDan Magenheimer #include <linux/cleancache.h>
427c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
43ac27a0ecSDave Kleikamp 
44bfff6873SLukas Czerner #include <linux/kthread.h>
45bfff6873SLukas Czerner #include <linux/freezer.h>
46bfff6873SLukas Czerner 
473dcf5451SChristoph Hellwig #include "ext4.h"
484a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
493dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
50ac27a0ecSDave Kleikamp #include "xattr.h"
51ac27a0ecSDave Kleikamp #include "acl.h"
523661d286STheodore Ts'o #include "mballoc.h"
530c9ec4beSDarrick J. Wong #include "fsmap.h"
54ac27a0ecSDave Kleikamp 
559bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
569bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
579bffad1eSTheodore Ts'o 
580b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
590b75a840SLukas Czerner static struct mutex ext4_li_mtx;
60e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
619f6200bbSTheodore Ts'o 
62617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
63ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
642adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
65e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
66617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
67617ba13bSMingming Cao 					struct ext4_super_block *es);
68617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
69617ba13bSMingming Cao 				   struct ext4_super_block *es);
70617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
71617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
72617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
73c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
74c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
75152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
76152a0836SAl Viro 		       const char *dev_name, void *data);
772035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
782035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
79d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
80bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
81bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
828f1f7453SEric Sandeen static void ext4_clear_request_list(void);
83c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
84c6cb7e77SEric Whitney 					    unsigned int journal_inum);
85ac27a0ecSDave Kleikamp 
86e74031fdSJan Kara /*
87e74031fdSJan Kara  * Lock ordering
88e74031fdSJan Kara  *
89e74031fdSJan Kara  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
90e74031fdSJan Kara  * i_mmap_rwsem (inode->i_mmap_rwsem)!
91e74031fdSJan Kara  *
92e74031fdSJan Kara  * page fault path:
93e74031fdSJan Kara  * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
94e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
95e74031fdSJan Kara  *
96e74031fdSJan Kara  * buffered write path:
97e74031fdSJan Kara  * sb_start_write -> i_mutex -> mmap_sem
98e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
99e74031fdSJan Kara  *   i_data_sem (rw)
100e74031fdSJan Kara  *
101e74031fdSJan Kara  * truncate:
102e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
103e74031fdSJan Kara  *   i_mmap_rwsem (w) -> page lock
104e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
105e74031fdSJan Kara  *   transaction start -> i_data_sem (rw)
106e74031fdSJan Kara  *
107e74031fdSJan Kara  * direct IO:
108e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) -> mmap_sem
109e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) ->
110e74031fdSJan Kara  *   transaction start -> i_data_sem (rw)
111e74031fdSJan Kara  *
112e74031fdSJan Kara  * writepages:
113e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
114e74031fdSJan Kara  */
115e74031fdSJan Kara 
116c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1172035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1182035e776STheodore Ts'o 	.owner		= THIS_MODULE,
1192035e776STheodore Ts'o 	.name		= "ext2",
1202035e776STheodore Ts'o 	.mount		= ext4_mount,
1212035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
1222035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
1232035e776STheodore Ts'o };
1247f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
125fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1262035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1272035e776STheodore Ts'o #else
1282035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1292035e776STheodore Ts'o #endif
1302035e776STheodore Ts'o 
1312035e776STheodore Ts'o 
132ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
133ba69f9abSJan Kara 	.owner		= THIS_MODULE,
134ba69f9abSJan Kara 	.name		= "ext3",
135152a0836SAl Viro 	.mount		= ext4_mount,
136ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
137ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
138ba69f9abSJan Kara };
1397f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
140fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
141ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
142bd81d8eeSLaurent Vivier 
143d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
144d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
145d25425f8SDarrick J. Wong {
146e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
147d25425f8SDarrick J. Wong 		return 1;
148d25425f8SDarrick J. Wong 
149d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
150d25425f8SDarrick J. Wong }
151d25425f8SDarrick J. Wong 
152a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
153a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
154a9c47317SDarrick J. Wong {
155a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
156a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
157a9c47317SDarrick J. Wong 	__u32 csum;
158a9c47317SDarrick J. Wong 
159a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
160a9c47317SDarrick J. Wong 
161a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
162a9c47317SDarrick J. Wong }
163a9c47317SDarrick J. Wong 
164c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb,
165a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
166a9c47317SDarrick J. Wong {
1679aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
168a9c47317SDarrick J. Wong 		return 1;
169a9c47317SDarrick J. Wong 
170a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
171a9c47317SDarrick J. Wong }
172a9c47317SDarrick J. Wong 
17306db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
174a9c47317SDarrick J. Wong {
17506db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
17606db49e6STheodore Ts'o 
1779aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
178a9c47317SDarrick J. Wong 		return;
179a9c47317SDarrick J. Wong 
180a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
181a9c47317SDarrick J. Wong }
182a9c47317SDarrick J. Wong 
1839933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
1849933fc0aSTheodore Ts'o {
1859933fc0aSTheodore Ts'o 	void *ret;
1869933fc0aSTheodore Ts'o 
1878be04b93SJoe Perches 	ret = kmalloc(size, flags | __GFP_NOWARN);
1889933fc0aSTheodore Ts'o 	if (!ret)
1899933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
1909933fc0aSTheodore Ts'o 	return ret;
1919933fc0aSTheodore Ts'o }
1929933fc0aSTheodore Ts'o 
1939933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
1949933fc0aSTheodore Ts'o {
1959933fc0aSTheodore Ts'o 	void *ret;
1969933fc0aSTheodore Ts'o 
1978be04b93SJoe Perches 	ret = kzalloc(size, flags | __GFP_NOWARN);
1989933fc0aSTheodore Ts'o 	if (!ret)
1999933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2009933fc0aSTheodore Ts'o 	return ret;
2019933fc0aSTheodore Ts'o }
2029933fc0aSTheodore Ts'o 
2038fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
2048fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
205bd81d8eeSLaurent Vivier {
2063a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
2078fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2088fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
209bd81d8eeSLaurent Vivier }
210bd81d8eeSLaurent Vivier 
2118fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
2128fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
213bd81d8eeSLaurent Vivier {
2145272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
2158fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2168fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
217bd81d8eeSLaurent Vivier }
218bd81d8eeSLaurent Vivier 
2198fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
2208fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
221bd81d8eeSLaurent Vivier {
2225272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
2238fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2248fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
225bd81d8eeSLaurent Vivier }
226bd81d8eeSLaurent Vivier 
227021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
228560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
229560671a0SAneesh Kumar K.V {
230560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
231560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
232560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
233560671a0SAneesh Kumar K.V }
234560671a0SAneesh Kumar K.V 
235560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
236560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
237560671a0SAneesh Kumar K.V {
238560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
239560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
240560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
241560671a0SAneesh Kumar K.V }
242560671a0SAneesh Kumar K.V 
243560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
244560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
245560671a0SAneesh Kumar K.V {
246560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
247560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
248560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
249560671a0SAneesh Kumar K.V }
250560671a0SAneesh Kumar K.V 
251560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
252560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
253560671a0SAneesh Kumar K.V {
254560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
255560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
256560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
257560671a0SAneesh Kumar K.V }
258560671a0SAneesh Kumar K.V 
2598fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2608fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
261bd81d8eeSLaurent Vivier {
2623a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2638fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2648fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
265bd81d8eeSLaurent Vivier }
266bd81d8eeSLaurent Vivier 
2678fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2688fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
269bd81d8eeSLaurent Vivier {
2705272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2718fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2728fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
273bd81d8eeSLaurent Vivier }
274bd81d8eeSLaurent Vivier 
2758fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2768fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
277bd81d8eeSLaurent Vivier {
2785272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2798fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2808fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
281bd81d8eeSLaurent Vivier }
282bd81d8eeSLaurent Vivier 
283021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
284560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
285560671a0SAneesh Kumar K.V {
286560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
287560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
288560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
289560671a0SAneesh Kumar K.V }
290560671a0SAneesh Kumar K.V 
291560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
292560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
293560671a0SAneesh Kumar K.V {
294560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
295560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
296560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
297560671a0SAneesh Kumar K.V }
298560671a0SAneesh Kumar K.V 
299560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
300560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
301560671a0SAneesh Kumar K.V {
302560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
303560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
304560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
305560671a0SAneesh Kumar K.V }
306560671a0SAneesh Kumar K.V 
307560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
308560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
309560671a0SAneesh Kumar K.V {
310560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
311560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
312560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
313560671a0SAneesh Kumar K.V }
314560671a0SAneesh Kumar K.V 
315d3d1faf6SCurt Wohlgemuth 
3161c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3171c13d5c0STheodore Ts'o 			    unsigned int line)
3181c13d5c0STheodore Ts'o {
3191c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3201c13d5c0STheodore Ts'o 
3211c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3221b46617bSTheodore Ts'o 	if (bdev_read_only(sb->s_bdev))
3231b46617bSTheodore Ts'o 		return;
3241c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3251c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
3261c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3271c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3281c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3291c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3301c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3311c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3321c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3331c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3341c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3351c13d5c0STheodore Ts'o 	}
33666e61a9eSTheodore Ts'o 	/*
33766e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
33866e61a9eSTheodore Ts'o 	 * started already
33966e61a9eSTheodore Ts'o 	 */
34066e61a9eSTheodore Ts'o 	if (!es->s_error_count)
34166e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
342ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
3431c13d5c0STheodore Ts'o }
3441c13d5c0STheodore Ts'o 
3451c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3461c13d5c0STheodore Ts'o 			    unsigned int line)
3471c13d5c0STheodore Ts'o {
3481c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3491c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3501c13d5c0STheodore Ts'o }
3511c13d5c0STheodore Ts'o 
352bdfe0cbdSTheodore Ts'o /*
353bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
354bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
355bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
356bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
357bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
358bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
359bdfe0cbdSTheodore Ts'o  */
360bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
361bdfe0cbdSTheodore Ts'o {
362bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
363bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
364bdfe0cbdSTheodore Ts'o 
365bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
366bdfe0cbdSTheodore Ts'o }
367bdfe0cbdSTheodore Ts'o 
36818aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
36918aadd47SBobi Jam {
37018aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
37118aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
37218aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
3735d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
37418aadd47SBobi Jam 
3755d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
376a0154344SDaeho Jeong 
377a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
378a0154344SDaeho Jeong 
37918aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
3805d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
3815d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
3825d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
38318aadd47SBobi Jam 		list_del_init(&jce->jce_list);
38418aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
38518aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
38618aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
38718aadd47SBobi Jam 	}
38818aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
38918aadd47SBobi Jam }
3901c13d5c0STheodore Ts'o 
391ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
392ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
393ac27a0ecSDave Kleikamp  *
394ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
395617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
396ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
397ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
398ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
399ac27a0ecSDave Kleikamp  * write out the superblock safely.
400ac27a0ecSDave Kleikamp  *
401dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
402d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
403ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
404ac27a0ecSDave Kleikamp  */
405ac27a0ecSDave Kleikamp 
406617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
407ac27a0ecSDave Kleikamp {
408bc98a42cSDavid Howells 	if (sb_rdonly(sb))
409ac27a0ecSDave Kleikamp 		return;
410ac27a0ecSDave Kleikamp 
411ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
412617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
413ac27a0ecSDave Kleikamp 
4144ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
415ac27a0ecSDave Kleikamp 		if (journal)
416dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
417ac27a0ecSDave Kleikamp 	}
418ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
419b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
4204418e141SDmitry Monakhov 		/*
4214418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
4224418e141SDmitry Monakhov 		 * before ->s_flags update
4234418e141SDmitry Monakhov 		 */
4244418e141SDmitry Monakhov 		smp_wmb();
425ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
426ac27a0ecSDave Kleikamp 	}
4274327ba52SDaeho Jeong 	if (test_opt(sb, ERRORS_PANIC)) {
4284327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
4294327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
4304327ba52SDaeho Jeong 			return;
431617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
432ac27a0ecSDave Kleikamp 			sb->s_id);
433ac27a0ecSDave Kleikamp 	}
4344327ba52SDaeho Jeong }
435ac27a0ecSDave Kleikamp 
436efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
437efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
438efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
439efbed4dcSTheodore Ts'o 
44012062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
441c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
442ac27a0ecSDave Kleikamp {
4430ff2ea7dSJoe Perches 	struct va_format vaf;
444ac27a0ecSDave Kleikamp 	va_list args;
445ac27a0ecSDave Kleikamp 
4460db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
4470db1ff22STheodore Ts'o 		return;
4480db1ff22STheodore Ts'o 
449efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
450ac27a0ecSDave Kleikamp 		va_start(args, fmt);
4510ff2ea7dSJoe Perches 		vaf.fmt = fmt;
4520ff2ea7dSJoe Perches 		vaf.va = &args;
453efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
454efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4550ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
456ac27a0ecSDave Kleikamp 		va_end(args);
457efbed4dcSTheodore Ts'o 	}
458f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
459617ba13bSMingming Cao 	ext4_handle_error(sb);
460ac27a0ecSDave Kleikamp }
461ac27a0ecSDave Kleikamp 
462e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
463c398eda0STheodore Ts'o 			unsigned int line, ext4_fsblk_t block,
464273df556SFrank Mayhar 			const char *fmt, ...)
465273df556SFrank Mayhar {
466273df556SFrank Mayhar 	va_list args;
467f7c21177STheodore Ts'o 	struct va_format vaf;
4681c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
469273df556SFrank Mayhar 
4700db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
4710db1ff22STheodore Ts'o 		return;
4720db1ff22STheodore Ts'o 
4731c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4741c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
475efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
476273df556SFrank Mayhar 		va_start(args, fmt);
477f7c21177STheodore Ts'o 		vaf.fmt = fmt;
478f7c21177STheodore Ts'o 		vaf.va = &args;
479c398eda0STheodore Ts'o 		if (block)
480d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
481d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
482d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
483d9ee81daSJoe Perches 			       block, current->comm, &vaf);
484d9ee81daSJoe Perches 		else
485d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
486d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
487d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
488d9ee81daSJoe Perches 			       current->comm, &vaf);
489273df556SFrank Mayhar 		va_end(args);
490efbed4dcSTheodore Ts'o 	}
491efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
492273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
493273df556SFrank Mayhar }
494273df556SFrank Mayhar 
495e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
496f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
497f7c21177STheodore Ts'o 		       const char *fmt, ...)
498273df556SFrank Mayhar {
499273df556SFrank Mayhar 	va_list args;
500f7c21177STheodore Ts'o 	struct va_format vaf;
5011c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
502496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
503273df556SFrank Mayhar 	char pathname[80], *path;
504273df556SFrank Mayhar 
5050db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5060db1ff22STheodore Ts'o 		return;
5070db1ff22STheodore Ts'o 
5081c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
5091c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
510efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
5119bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
512f9a62d09SDan Carpenter 		if (IS_ERR(path))
513273df556SFrank Mayhar 			path = "(unknown)";
514f7c21177STheodore Ts'o 		va_start(args, fmt);
515f7c21177STheodore Ts'o 		vaf.fmt = fmt;
516f7c21177STheodore Ts'o 		vaf.va = &args;
517d9ee81daSJoe Perches 		if (block)
518d9ee81daSJoe Perches 			printk(KERN_CRIT
519d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
520d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
521d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
522d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
523d9ee81daSJoe Perches 		else
524d9ee81daSJoe Perches 			printk(KERN_CRIT
525d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
526d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
527d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
528d9ee81daSJoe Perches 			       current->comm, path, &vaf);
529273df556SFrank Mayhar 		va_end(args);
530efbed4dcSTheodore Ts'o 	}
531efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
532273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
533273df556SFrank Mayhar }
534273df556SFrank Mayhar 
535722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
536ac27a0ecSDave Kleikamp 			      char nbuf[16])
537ac27a0ecSDave Kleikamp {
538ac27a0ecSDave Kleikamp 	char *errstr = NULL;
539ac27a0ecSDave Kleikamp 
540ac27a0ecSDave Kleikamp 	switch (errno) {
5416a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
5426a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
5436a797d27SDarrick J. Wong 		break;
5446a797d27SDarrick J. Wong 	case -EFSBADCRC:
5456a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
5466a797d27SDarrick J. Wong 		break;
547ac27a0ecSDave Kleikamp 	case -EIO:
548ac27a0ecSDave Kleikamp 		errstr = "IO failure";
549ac27a0ecSDave Kleikamp 		break;
550ac27a0ecSDave Kleikamp 	case -ENOMEM:
551ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
552ac27a0ecSDave Kleikamp 		break;
553ac27a0ecSDave Kleikamp 	case -EROFS:
55478f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
55578f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
556ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
557ac27a0ecSDave Kleikamp 		else
558ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
559ac27a0ecSDave Kleikamp 		break;
560ac27a0ecSDave Kleikamp 	default:
561ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
562ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
563ac27a0ecSDave Kleikamp 		 * NULL. */
564ac27a0ecSDave Kleikamp 		if (nbuf) {
565ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
566ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
567ac27a0ecSDave Kleikamp 				errstr = nbuf;
568ac27a0ecSDave Kleikamp 		}
569ac27a0ecSDave Kleikamp 		break;
570ac27a0ecSDave Kleikamp 	}
571ac27a0ecSDave Kleikamp 
572ac27a0ecSDave Kleikamp 	return errstr;
573ac27a0ecSDave Kleikamp }
574ac27a0ecSDave Kleikamp 
575617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
576ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
577ac27a0ecSDave Kleikamp 
578c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
579c398eda0STheodore Ts'o 		      unsigned int line, int errno)
580ac27a0ecSDave Kleikamp {
581ac27a0ecSDave Kleikamp 	char nbuf[16];
582ac27a0ecSDave Kleikamp 	const char *errstr;
583ac27a0ecSDave Kleikamp 
5840db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
5850db1ff22STheodore Ts'o 		return;
5860db1ff22STheodore Ts'o 
587ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
588ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
589ac27a0ecSDave Kleikamp 	 * an error. */
590bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
591ac27a0ecSDave Kleikamp 		return;
592ac27a0ecSDave Kleikamp 
593efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
594617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
595c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
596c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
597efbed4dcSTheodore Ts'o 	}
598ac27a0ecSDave Kleikamp 
599efbed4dcSTheodore Ts'o 	save_error_info(sb, function, line);
600617ba13bSMingming Cao 	ext4_handle_error(sb);
601ac27a0ecSDave Kleikamp }
602ac27a0ecSDave Kleikamp 
603ac27a0ecSDave Kleikamp /*
604617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
605ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
606ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
607ac27a0ecSDave Kleikamp  *
608ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
609ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
610ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
611ac27a0ecSDave Kleikamp  */
612ac27a0ecSDave Kleikamp 
613c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
614c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
615ac27a0ecSDave Kleikamp {
616651e1c3bSJoe Perches 	struct va_format vaf;
617ac27a0ecSDave Kleikamp 	va_list args;
618ac27a0ecSDave Kleikamp 
6190db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
6200db1ff22STheodore Ts'o 		return;
6210db1ff22STheodore Ts'o 
6221c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
623ac27a0ecSDave Kleikamp 	va_start(args, fmt);
624651e1c3bSJoe Perches 	vaf.fmt = fmt;
625651e1c3bSJoe Perches 	vaf.va = &args;
626651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
627651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
628ac27a0ecSDave Kleikamp 	va_end(args);
629ac27a0ecSDave Kleikamp 
630bc98a42cSDavid Howells 	if (sb_rdonly(sb) == 0) {
631b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
6324ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
6334418e141SDmitry Monakhov 		/*
6344418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
6354418e141SDmitry Monakhov 		 * before ->s_flags update
6364418e141SDmitry Monakhov 		 */
6374418e141SDmitry Monakhov 		smp_wmb();
6384418e141SDmitry Monakhov 		sb->s_flags |= MS_RDONLY;
639ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
640dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
6411c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
6421c13d5c0STheodore Ts'o 	}
6434327ba52SDaeho Jeong 	if (test_opt(sb, ERRORS_PANIC)) {
6444327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
6454327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
6464327ba52SDaeho Jeong 			return;
6471c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
648ac27a0ecSDave Kleikamp 	}
6494327ba52SDaeho Jeong }
650ac27a0ecSDave Kleikamp 
651e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
652e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
653b31e1552SEric Sandeen {
6540ff2ea7dSJoe Perches 	struct va_format vaf;
655b31e1552SEric Sandeen 	va_list args;
656b31e1552SEric Sandeen 
657efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
658efbed4dcSTheodore Ts'o 		return;
659efbed4dcSTheodore Ts'o 
660b31e1552SEric Sandeen 	va_start(args, fmt);
6610ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6620ff2ea7dSJoe Perches 	vaf.va = &args;
6630ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
664b31e1552SEric Sandeen 	va_end(args);
665b31e1552SEric Sandeen }
666b31e1552SEric Sandeen 
667b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb)					\
668b03a2f7eSAndreas Dilger 		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
669b03a2f7eSAndreas Dilger 			     "EXT4-fs warning")
670b03a2f7eSAndreas Dilger 
67112062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
672c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
673ac27a0ecSDave Kleikamp {
6740ff2ea7dSJoe Perches 	struct va_format vaf;
675ac27a0ecSDave Kleikamp 	va_list args;
676ac27a0ecSDave Kleikamp 
677b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
678efbed4dcSTheodore Ts'o 		return;
679efbed4dcSTheodore Ts'o 
680ac27a0ecSDave Kleikamp 	va_start(args, fmt);
6810ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6820ff2ea7dSJoe Perches 	vaf.va = &args;
6830ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
6840ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
685ac27a0ecSDave Kleikamp 	va_end(args);
686ac27a0ecSDave Kleikamp }
687ac27a0ecSDave Kleikamp 
688b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
689b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
690b03a2f7eSAndreas Dilger {
691b03a2f7eSAndreas Dilger 	struct va_format vaf;
692b03a2f7eSAndreas Dilger 	va_list args;
693b03a2f7eSAndreas Dilger 
694b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
695b03a2f7eSAndreas Dilger 		return;
696b03a2f7eSAndreas Dilger 
697b03a2f7eSAndreas Dilger 	va_start(args, fmt);
698b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
699b03a2f7eSAndreas Dilger 	vaf.va = &args;
700b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
701b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
702b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
703b03a2f7eSAndreas Dilger 	va_end(args);
704b03a2f7eSAndreas Dilger }
705b03a2f7eSAndreas Dilger 
706e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
707e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
708e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
709e29136f8STheodore Ts'o 			     const char *fmt, ...)
7105d1b1b3fSAneesh Kumar K.V __releases(bitlock)
7115d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
7125d1b1b3fSAneesh Kumar K.V {
7130ff2ea7dSJoe Perches 	struct va_format vaf;
7145d1b1b3fSAneesh Kumar K.V 	va_list args;
7155d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
7165d1b1b3fSAneesh Kumar K.V 
7170db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7180db1ff22STheodore Ts'o 		return;
7190db1ff22STheodore Ts'o 
7201c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
7211c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
7221c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
7230ff2ea7dSJoe Perches 
724efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
7255d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
7260ff2ea7dSJoe Perches 		vaf.fmt = fmt;
7270ff2ea7dSJoe Perches 		vaf.va = &args;
72821149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
729e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
730e29136f8STheodore Ts'o 		if (ino)
7310ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
732e29136f8STheodore Ts'o 		if (block)
733efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
734efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
7350ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
7365d1b1b3fSAneesh Kumar K.V 		va_end(args);
737efbed4dcSTheodore Ts'o 	}
7385d1b1b3fSAneesh Kumar K.V 
7395d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
740e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
7415d1b1b3fSAneesh Kumar K.V 		return;
7425d1b1b3fSAneesh Kumar K.V 	}
7431c13d5c0STheodore Ts'o 
7445d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
7455d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
7465d1b1b3fSAneesh Kumar K.V 	/*
7475d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
7485d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
7495d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
7505d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
7515d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
75225985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
7535d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
7545d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
7555d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
7565d1b1b3fSAneesh Kumar K.V 	 */
7575d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
7585d1b1b3fSAneesh Kumar K.V 	return;
7595d1b1b3fSAneesh Kumar K.V }
7605d1b1b3fSAneesh Kumar K.V 
761617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
762ac27a0ecSDave Kleikamp {
763617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
764ac27a0ecSDave Kleikamp 
765617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
766ac27a0ecSDave Kleikamp 		return;
767ac27a0ecSDave Kleikamp 
76812062dddSEric Sandeen 	ext4_warning(sb,
769ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
770ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
771617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
772ac27a0ecSDave Kleikamp 
773617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
774617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
775617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
776ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
777ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
778ac27a0ecSDave Kleikamp 
779ac27a0ecSDave Kleikamp 	/*
780ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
781ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
782ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
783ac27a0ecSDave Kleikamp 	 */
784ac27a0ecSDave Kleikamp }
785ac27a0ecSDave Kleikamp 
786ac27a0ecSDave Kleikamp /*
787ac27a0ecSDave Kleikamp  * Open the external journal device
788ac27a0ecSDave Kleikamp  */
789b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
790ac27a0ecSDave Kleikamp {
791ac27a0ecSDave Kleikamp 	struct block_device *bdev;
792ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
793ac27a0ecSDave Kleikamp 
794d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
795ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
796ac27a0ecSDave Kleikamp 		goto fail;
797ac27a0ecSDave Kleikamp 	return bdev;
798ac27a0ecSDave Kleikamp 
799ac27a0ecSDave Kleikamp fail:
800b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
801ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
802ac27a0ecSDave Kleikamp 	return NULL;
803ac27a0ecSDave Kleikamp }
804ac27a0ecSDave Kleikamp 
805ac27a0ecSDave Kleikamp /*
806ac27a0ecSDave Kleikamp  * Release the journal device
807ac27a0ecSDave Kleikamp  */
8084385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
809ac27a0ecSDave Kleikamp {
8104385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
811ac27a0ecSDave Kleikamp }
812ac27a0ecSDave Kleikamp 
8134385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
814ac27a0ecSDave Kleikamp {
815ac27a0ecSDave Kleikamp 	struct block_device *bdev;
816ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
817ac27a0ecSDave Kleikamp 	if (bdev) {
8184385bab1SAl Viro 		ext4_blkdev_put(bdev);
819ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
820ac27a0ecSDave Kleikamp 	}
821ac27a0ecSDave Kleikamp }
822ac27a0ecSDave Kleikamp 
823ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
824ac27a0ecSDave Kleikamp {
825617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
826ac27a0ecSDave Kleikamp }
827ac27a0ecSDave Kleikamp 
828617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
829ac27a0ecSDave Kleikamp {
830ac27a0ecSDave Kleikamp 	struct list_head *l;
831ac27a0ecSDave Kleikamp 
832b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
833ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
834ac27a0ecSDave Kleikamp 
835ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
836ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
837ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
838ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
839ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
840ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
841ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
842ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
843ac27a0ecSDave Kleikamp 	}
844ac27a0ecSDave Kleikamp }
845ac27a0ecSDave Kleikamp 
846957153fcSJan Kara #ifdef CONFIG_QUOTA
847957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
848957153fcSJan Kara 
849957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
850957153fcSJan Kara {
851957153fcSJan Kara 	int type;
852957153fcSJan Kara 
853957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
854957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
855957153fcSJan Kara 		ext4_quota_off(sb, type);
856957153fcSJan Kara }
857957153fcSJan Kara #else
858957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
859957153fcSJan Kara {
860957153fcSJan Kara }
861957153fcSJan Kara #endif
862957153fcSJan Kara 
863617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
864ac27a0ecSDave Kleikamp {
865617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
866617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
86797abd7d4STheodore Ts'o 	int aborted = 0;
868ef2cabf7SHidehiro Kawai 	int i, err;
869ac27a0ecSDave Kleikamp 
870857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
871957153fcSJan Kara 	ext4_quota_off_umount(sb);
872e0ccfd95SChristoph Hellwig 
8732e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
8742e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
8754c0425ffSMingming Cao 
8760390131bSFrank Mayhar 	if (sbi->s_journal) {
87797abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
878ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
87947b4a50bSJan Kara 		sbi->s_journal = NULL;
88097abd7d4STheodore Ts'o 		if ((err < 0) && !aborted)
881c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
8820390131bSFrank Mayhar 	}
883d4edac31SJosef Bacik 
884ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
885d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
8869105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
887d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
888d4edac31SJosef Bacik 	ext4_mb_release(sb);
889d4edac31SJosef Bacik 	ext4_ext_release(sb);
890d4edac31SJosef Bacik 
891bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
892e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
893ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
894ac27a0ecSDave Kleikamp 	}
895bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
896a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
897a8e25a83SArtem Bityutskiy 
898ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
899ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
900b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
901b93b41d4SAl Viro 	kvfree(sbi->s_flex_groups);
90257042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
903ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
904ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
90557042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
906c8585c6fSDaeho Jeong 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
907ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
908a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
909ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
910ac27a0ecSDave Kleikamp #endif
911ac27a0ecSDave Kleikamp 
912ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
913ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
914ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
915ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
916ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
917ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
918ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
919ac27a0ecSDave Kleikamp 
92089d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
921f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
922ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
923ac27a0ecSDave Kleikamp 		/*
924ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
925ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
926ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
927ac27a0ecSDave Kleikamp 		 */
928ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
929f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
930617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
931ac27a0ecSDave Kleikamp 	}
932dec214d0STahsin Erdogan 	if (sbi->s_ea_inode_cache) {
933dec214d0STahsin Erdogan 		ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
934dec214d0STahsin Erdogan 		sbi->s_ea_inode_cache = NULL;
935dec214d0STahsin Erdogan 	}
93647387409STahsin Erdogan 	if (sbi->s_ea_block_cache) {
93747387409STahsin Erdogan 		ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
93847387409STahsin Erdogan 		sbi->s_ea_block_cache = NULL;
9399c191f70ST Makphaibulchoke 	}
940c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
941c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
9429060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
943ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
9443197ebdbSTheodore Ts'o 	/*
9453197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
9463197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
9473197ebdbSTheodore Ts'o 	 */
9483197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
9493197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
9500441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
9510441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
952705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
9535e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
954ac27a0ecSDave Kleikamp 	kfree(sbi);
955ac27a0ecSDave Kleikamp }
956ac27a0ecSDave Kleikamp 
957e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
958ac27a0ecSDave Kleikamp 
959ac27a0ecSDave Kleikamp /*
960ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
961ac27a0ecSDave Kleikamp  */
962617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
963ac27a0ecSDave Kleikamp {
964617ba13bSMingming Cao 	struct ext4_inode_info *ei;
965ac27a0ecSDave Kleikamp 
966e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
967ac27a0ecSDave Kleikamp 	if (!ei)
968ac27a0ecSDave Kleikamp 		return NULL;
9690b8e58a1SAndreas Dilger 
970ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
971202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
972c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
973c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
9749a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
9759a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
976edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
977eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
978edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
979dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
980d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
9819d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
9827e731bc9STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = 0;
983d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
984a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
985a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
98696c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
987a9e7f447SDmitry Monakhov #endif
9888aefcd55STheodore Ts'o 	ei->jinode = NULL;
9892e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
990744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
991b436b9beSJan Kara 	ei->i_sync_tid = 0;
992b436b9beSJan Kara 	ei->i_datasync_tid = 0;
993e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
9942e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
995ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
996ac27a0ecSDave Kleikamp }
997ac27a0ecSDave Kleikamp 
9987ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
9997ff9c073STheodore Ts'o {
10007ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
10017ff9c073STheodore Ts'o 
10027ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
10037ff9c073STheodore Ts'o 	return drop;
10047ff9c073STheodore Ts'o }
10057ff9c073STheodore Ts'o 
1006fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
1007fa0d7e3dSNick Piggin {
1008fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
1009fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1010fa0d7e3dSNick Piggin }
1011fa0d7e3dSNick Piggin 
1012617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1013ac27a0ecSDave Kleikamp {
10149f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1015b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1016b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1017b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
10189f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
10199f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
10209f7dd93dSVasily Averin 				true);
10219f7dd93dSVasily Averin 		dump_stack();
10229f7dd93dSVasily Averin 	}
1023fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
1024ac27a0ecSDave Kleikamp }
1025ac27a0ecSDave Kleikamp 
102651cc5068SAlexey Dobriyan static void init_once(void *foo)
1027ac27a0ecSDave Kleikamp {
1028617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1029ac27a0ecSDave Kleikamp 
1030ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1031ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
10320e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1033ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1034ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1035ac27a0ecSDave Kleikamp }
1036ac27a0ecSDave Kleikamp 
1037e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1038ac27a0ecSDave Kleikamp {
1039617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
1040617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
1041ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
10425d097056SVladimir Davydov 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
104320c2df83SPaul Mundt 					     init_once);
1044617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1045ac27a0ecSDave Kleikamp 		return -ENOMEM;
1046ac27a0ecSDave Kleikamp 	return 0;
1047ac27a0ecSDave Kleikamp }
1048ac27a0ecSDave Kleikamp 
1049ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1050ac27a0ecSDave Kleikamp {
10518c0a8537SKirill A. Shutemov 	/*
10528c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
10538c0a8537SKirill A. Shutemov 	 * destroy cache.
10548c0a8537SKirill A. Shutemov 	 */
10558c0a8537SKirill A. Shutemov 	rcu_barrier();
1056617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1057ac27a0ecSDave Kleikamp }
1058ac27a0ecSDave Kleikamp 
10590930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1060ac27a0ecSDave Kleikamp {
10610930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1062dbd5768fSJan Kara 	clear_inode(inode);
10639f754758SChristoph Hellwig 	dquot_drop(inode);
1064c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
106551865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
10668aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
10678aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
10688aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
10698aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
10708aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
10718aefcd55STheodore Ts'o 	}
1072b7236e21STheodore Ts'o #ifdef CONFIG_EXT4_FS_ENCRYPTION
1073a7550b30SJaegeuk Kim 	fscrypt_put_encryption_info(inode, NULL);
1074b7236e21STheodore Ts'o #endif
1075ac27a0ecSDave Kleikamp }
1076ac27a0ecSDave Kleikamp 
10771b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
10781b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1079ac27a0ecSDave Kleikamp {
1080ac27a0ecSDave Kleikamp 	struct inode *inode;
1081ac27a0ecSDave Kleikamp 
1082617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1083ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1084617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1085ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1086ac27a0ecSDave Kleikamp 
1087ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1088ac27a0ecSDave Kleikamp 	 *
1089617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1090ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1091ac27a0ecSDave Kleikamp 	 *
1092ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1093ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1094ac27a0ecSDave Kleikamp 	 */
1095f4bb2981STheodore Ts'o 	inode = ext4_iget_normal(sb, ino);
10961d1fe1eeSDavid Howells 	if (IS_ERR(inode))
10971d1fe1eeSDavid Howells 		return ERR_CAST(inode);
10981d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1099ac27a0ecSDave Kleikamp 		iput(inode);
1100ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1101ac27a0ecSDave Kleikamp 	}
11021b961ac0SChristoph Hellwig 
11031b961ac0SChristoph Hellwig 	return inode;
1104ac27a0ecSDave Kleikamp }
11051b961ac0SChristoph Hellwig 
11061b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
11071b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
11081b961ac0SChristoph Hellwig {
11091b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
11101b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
11111b961ac0SChristoph Hellwig }
11121b961ac0SChristoph Hellwig 
11131b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
11141b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
11151b961ac0SChristoph Hellwig {
11161b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
11171b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1118ac27a0ecSDave Kleikamp }
1119ac27a0ecSDave Kleikamp 
1120c39a7f84SToshiyuki Okajima /*
1121c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1122c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1123c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1124c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1125c39a7f84SToshiyuki Okajima  */
11260b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
11270b8e58a1SAndreas Dilger 				 gfp_t wait)
1128c39a7f84SToshiyuki Okajima {
1129c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1130c39a7f84SToshiyuki Okajima 
1131c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1132c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1133c39a7f84SToshiyuki Okajima 		return 0;
1134c39a7f84SToshiyuki Okajima 	if (journal)
1135c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1136d0164adcSMel Gorman 						wait & ~__GFP_DIRECT_RECLAIM);
1137c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1138c39a7f84SToshiyuki Okajima }
1139c39a7f84SToshiyuki Okajima 
1140a7550b30SJaegeuk Kim #ifdef CONFIG_EXT4_FS_ENCRYPTION
1141a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1142a7550b30SJaegeuk Kim {
1143a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1144a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1145a7550b30SJaegeuk Kim }
1146a7550b30SJaegeuk Kim 
1147a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1148a7550b30SJaegeuk Kim 							void *fs_data)
1149a7550b30SJaegeuk Kim {
11502f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1151c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1152a7550b30SJaegeuk Kim 
11539ce0151aSEric Biggers 	/*
11549ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
11559ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
11569ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
11579ce0151aSEric Biggers 	 * the filename "lost+found" itself.
11589ce0151aSEric Biggers 	 */
11599ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
11609ce0151aSEric Biggers 		return -EPERM;
1161a7550b30SJaegeuk Kim 
116294840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
116394840e3cSEric Biggers 	if (res)
116494840e3cSEric Biggers 		return res;
116594840e3cSEric Biggers 
11662f8f5e76SEric Biggers 	/*
11672f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
11682f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
11692f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
11702f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
11712f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
11722f8f5e76SEric Biggers 	 */
11732f8f5e76SEric Biggers 
11742f8f5e76SEric Biggers 	if (handle) {
11752f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
11762f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
11772f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
11782f8f5e76SEric Biggers 					    ctx, len, 0);
1179a7550b30SJaegeuk Kim 		if (!res) {
1180a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1181a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1182a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1183a3caa24bSJan Kara 			/*
1184a3caa24bSJan Kara 			 * Update inode->i_flags - e.g. S_DAX may get disabled
1185a3caa24bSJan Kara 			 */
1186a3caa24bSJan Kara 			ext4_set_inode_flags(inode);
1187a7550b30SJaegeuk Kim 		}
1188a7550b30SJaegeuk Kim 		return res;
1189a7550b30SJaegeuk Kim 	}
1190a7550b30SJaegeuk Kim 
1191b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1192b8cb5a54STahsin Erdogan 	if (res)
1193b8cb5a54STahsin Erdogan 		return res;
11942f8f5e76SEric Biggers retry:
1195af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1196af65207cSTahsin Erdogan 				     &credits);
1197dec214d0STahsin Erdogan 	if (res)
1198dec214d0STahsin Erdogan 		return res;
1199dec214d0STahsin Erdogan 
1200c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1201a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1202a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1203a7550b30SJaegeuk Kim 
12042f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
12052f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
12062f8f5e76SEric Biggers 				    ctx, len, 0);
1207a7550b30SJaegeuk Kim 	if (!res) {
1208a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1209a3caa24bSJan Kara 		/* Update inode->i_flags - e.g. S_DAX may get disabled */
1210a3caa24bSJan Kara 		ext4_set_inode_flags(inode);
1211a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1212a7550b30SJaegeuk Kim 		if (res)
1213a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1214a7550b30SJaegeuk Kim 	}
1215a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
12162f8f5e76SEric Biggers 
12172f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
12182f8f5e76SEric Biggers 		goto retry;
1219a7550b30SJaegeuk Kim 	if (!res)
1220a7550b30SJaegeuk Kim 		res = res2;
1221a7550b30SJaegeuk Kim 	return res;
1222a7550b30SJaegeuk Kim }
1223a7550b30SJaegeuk Kim 
1224c250b7ddSEric Biggers static bool ext4_dummy_context(struct inode *inode)
1225a7550b30SJaegeuk Kim {
1226a7550b30SJaegeuk Kim 	return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
1227a7550b30SJaegeuk Kim }
1228a7550b30SJaegeuk Kim 
1229a7550b30SJaegeuk Kim static unsigned ext4_max_namelen(struct inode *inode)
1230a7550b30SJaegeuk Kim {
1231a7550b30SJaegeuk Kim 	return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
1232a7550b30SJaegeuk Kim 		EXT4_NAME_LEN;
1233a7550b30SJaegeuk Kim }
1234a7550b30SJaegeuk Kim 
12356f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1236a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1237a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1238a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1239a7550b30SJaegeuk Kim 	.dummy_context		= ext4_dummy_context,
1240a7550b30SJaegeuk Kim 	.is_encrypted		= ext4_encrypted_inode,
1241a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1242a7550b30SJaegeuk Kim 	.max_namelen		= ext4_max_namelen,
1243a7550b30SJaegeuk Kim };
1244a7550b30SJaegeuk Kim #else
12456f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1246a7550b30SJaegeuk Kim 	.is_encrypted		= ext4_encrypted_inode,
1247a7550b30SJaegeuk Kim };
1248a7550b30SJaegeuk Kim #endif
1249a7550b30SJaegeuk Kim 
1250ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1251d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1252689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1253ac27a0ecSDave Kleikamp 
1254617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1255617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1256617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1257617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1258617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
12596f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
12608c54ca9cSAl Viro 			 const struct path *path);
1261617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1262617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1263ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1264617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1265ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
12667c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
12677c319d32SAditya Kali 			     unsigned int flags);
12687c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
12698f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid);
1270ac27a0ecSDave Kleikamp 
127196c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
127296c7e0d9SJan Kara {
127396c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
127496c7e0d9SJan Kara }
127596c7e0d9SJan Kara 
127661e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
127760e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1278617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1279617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1280617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1281617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1282a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1283a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1284a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1285040cb378SLi Xi 	.get_projid		= ext4_get_projid,
12867a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
12878f0e8746STheodore Ts'o 	.get_next_id		= ext4_get_next_id,
1288ac27a0ecSDave Kleikamp };
1289ac27a0ecSDave Kleikamp 
12900d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1291617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1292ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1293287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
12940a240339SJan Kara 	.get_state	= dquot_get_state,
1295287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1296287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
12976332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
12986332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1299ac27a0ecSDave Kleikamp };
1300ac27a0ecSDave Kleikamp #endif
1301ac27a0ecSDave Kleikamp 
1302ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1303617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1304617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1305617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1306617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
13077ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
13080930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1309617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1310617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1311c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1312c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1313617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1314617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1315617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1316ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1317617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1318617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
131996c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1320ac27a0ecSDave Kleikamp #endif
1321c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1322ac27a0ecSDave Kleikamp };
1323ac27a0ecSDave Kleikamp 
132439655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
13251b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
13261b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1327617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1328ac27a0ecSDave Kleikamp };
1329ac27a0ecSDave Kleikamp 
1330ac27a0ecSDave Kleikamp enum {
1331ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1332ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
133372578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1334ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
133572578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1336ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1337ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1338ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
13396ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1340ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
13415a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1342ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
134349da9392SJan Kara 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
13441449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
1345670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
13461449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
13475328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1348744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1349fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1350cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1351ac27a0ecSDave Kleikamp };
1352ac27a0ecSDave Kleikamp 
1353a447c093SSteven Whitehouse static const match_table_t tokens = {
1354ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1355ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1356ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1357ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1358ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1359ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1360ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1361ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1362ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1363ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1364ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1365ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1366ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1367ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
136872578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
136972578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1370ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1371ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1372ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1373ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1374e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
13755a916be1STheodore Ts'o 	{Opt_noload, "noload"},
137672578c33STheodore Ts'o 	{Opt_removed, "nobh"},
137772578c33STheodore Ts'o 	{Opt_removed, "bh"},
1378ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
137930773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
138030773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1381ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1382ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1383818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1384c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1385818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1386ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1387ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1388ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1389ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
13905bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
13915bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1392ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1393ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1394ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1395ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1396ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1397ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
13985a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1399ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1400ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1401ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1402ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
140349da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1404ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
140506705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
140606705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
140725ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1408923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1409c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
141064769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1411a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1412a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1413670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1414dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
141536ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
141636ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
14176fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
14186fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1419240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1420b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1421afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
142206705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
142306705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1424744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1425744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
14265328e635SEric Sandeen 	{Opt_discard, "discard"},
14275328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1428fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1429fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1430fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1431df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
14326ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1433cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1434cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1435c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1436c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1437c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1438c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1439c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1440f3f12faaSJosef Bacik 	{Opt_err, NULL},
1441ac27a0ecSDave Kleikamp };
1442ac27a0ecSDave Kleikamp 
1443617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1444ac27a0ecSDave Kleikamp {
1445617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1446ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1447ac27a0ecSDave Kleikamp 
1448ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1449ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
14500b8e58a1SAndreas Dilger 
1451ac27a0ecSDave Kleikamp 	options += 3;
14520b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1453ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1454ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
14554776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1456ac27a0ecSDave Kleikamp 		       (char *) *data);
1457ac27a0ecSDave Kleikamp 		return 1;
1458ac27a0ecSDave Kleikamp 	}
1459ac27a0ecSDave Kleikamp 	if (*options == ',')
1460ac27a0ecSDave Kleikamp 		options++;
1461ac27a0ecSDave Kleikamp 	*data = (void *) options;
14620b8e58a1SAndreas Dilger 
1463ac27a0ecSDave Kleikamp 	return sb_block;
1464ac27a0ecSDave Kleikamp }
1465ac27a0ecSDave Kleikamp 
1466b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1467d6006186SEric Biggers static const char deprecated_msg[] =
1468d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1469437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1470b3881f74STheodore Ts'o 
147156c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
147256c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
147356c50f11SDmitry Monakhov {
147456c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
147556c50f11SDmitry Monakhov 	char *qname;
147603dafb5fSChen Gang 	int ret = -1;
147756c50f11SDmitry Monakhov 
147856c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
147956c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
148056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
148156c50f11SDmitry Monakhov 			"Cannot change journaled "
148256c50f11SDmitry Monakhov 			"quota options when quota turned on");
148357f73c2cSTheodore Ts'o 		return -1;
148456c50f11SDmitry Monakhov 	}
1485e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1486c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1487c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1488c325a67cSTheodore Ts'o 		return 1;
1489262b4662SJan Kara 	}
149056c50f11SDmitry Monakhov 	qname = match_strdup(args);
149156c50f11SDmitry Monakhov 	if (!qname) {
149256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
149356c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
149457f73c2cSTheodore Ts'o 		return -1;
149556c50f11SDmitry Monakhov 	}
149603dafb5fSChen Gang 	if (sbi->s_qf_names[qtype]) {
149703dafb5fSChen Gang 		if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
149803dafb5fSChen Gang 			ret = 1;
149903dafb5fSChen Gang 		else
150056c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
150103dafb5fSChen Gang 				 "%s quota file already specified",
150203dafb5fSChen Gang 				 QTYPE2NAME(qtype));
150303dafb5fSChen Gang 		goto errout;
150456c50f11SDmitry Monakhov 	}
150503dafb5fSChen Gang 	if (strchr(qname, '/')) {
150656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
150756c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
150803dafb5fSChen Gang 		goto errout;
150956c50f11SDmitry Monakhov 	}
151003dafb5fSChen Gang 	sbi->s_qf_names[qtype] = qname;
1511fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
151256c50f11SDmitry Monakhov 	return 1;
151303dafb5fSChen Gang errout:
151403dafb5fSChen Gang 	kfree(qname);
151503dafb5fSChen Gang 	return ret;
151656c50f11SDmitry Monakhov }
151756c50f11SDmitry Monakhov 
151856c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
151956c50f11SDmitry Monakhov {
152056c50f11SDmitry Monakhov 
152156c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
152256c50f11SDmitry Monakhov 
152356c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
152456c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
152556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
152656c50f11SDmitry Monakhov 			" when quota turned on");
152757f73c2cSTheodore Ts'o 		return -1;
152856c50f11SDmitry Monakhov 	}
152903dafb5fSChen Gang 	kfree(sbi->s_qf_names[qtype]);
153056c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
153156c50f11SDmitry Monakhov 	return 1;
153256c50f11SDmitry Monakhov }
153356c50f11SDmitry Monakhov #endif
153456c50f11SDmitry Monakhov 
153526092bf5STheodore Ts'o #define MOPT_SET	0x0001
153626092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
153726092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
153826092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
153926092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
154026092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
154126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
154226092bf5STheodore Ts'o #define MOPT_Q		0
154326092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
154426092bf5STheodore Ts'o #else
154526092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
154626092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
154726092bf5STheodore Ts'o #endif
154826092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
15498dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
15508dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
15518dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1552ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
155326092bf5STheodore Ts'o 
155426092bf5STheodore Ts'o static const struct mount_opts {
155526092bf5STheodore Ts'o 	int	token;
155626092bf5STheodore Ts'o 	int	mount_opt;
155726092bf5STheodore Ts'o 	int	flags;
155826092bf5STheodore Ts'o } ext4_mount_opts[] = {
155926092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
156026092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
156126092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
156226092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
156326092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
156426092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
15658dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
15668dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
15678dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
15688dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
156926092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
157026092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
15718dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
15728dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
15738dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
157459d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1575c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1576c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
15778dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
15781e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
157926092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
15808dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
15811e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
15828dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
158326092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
158426092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
158526092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
15868dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
15877915a861SAles Novak 	 MOPT_NO_EXT2},
15888dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
15897915a861SAles Novak 	 MOPT_NO_EXT2},
159026092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
159126092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
159226092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
159326092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
159426092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
159526092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
159626092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
159726092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
159826092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
159926092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1600923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
160126092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
16020efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
16030efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
16045ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
16055ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
16065ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
16078dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
16088dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
16098dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
16108dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
161126092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
161226092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
161326092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
161426092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
161526092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
161626092bf5STheodore Ts'o #else
161726092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
161826092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
161926092bf5STheodore Ts'o #endif
162026092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
162126092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1622670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
162326092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
162426092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
162526092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
162626092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
162726092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
162849da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
162949da9392SJan Kara 							MOPT_SET | MOPT_Q},
163026092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
163149da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
163249da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
163326092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
163426092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
163526092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
163626092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
163726092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
163826092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
163926092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1640df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
16416ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1642cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
164326092bf5STheodore Ts'o 	{Opt_err, 0, 0}
164426092bf5STheodore Ts'o };
164526092bf5STheodore Ts'o 
164626092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
164726092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
164826092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
164926092bf5STheodore Ts'o {
165026092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
165126092bf5STheodore Ts'o 	const struct mount_opts *m;
165208cefc7aSEric W. Biederman 	kuid_t uid;
165308cefc7aSEric W. Biederman 	kgid_t gid;
165426092bf5STheodore Ts'o 	int arg = 0;
165526092bf5STheodore Ts'o 
165657f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
165757f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
165857f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
165957f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
166057f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
166157f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
166257f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
166357f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
166457f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
166557f73c2cSTheodore Ts'o #endif
166626092bf5STheodore Ts'o 	switch (token) {
1667f7048605STheodore Ts'o 	case Opt_noacl:
1668f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1669f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1670f7048605STheodore Ts'o 		break;
167126092bf5STheodore Ts'o 	case Opt_sb:
167226092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
167326092bf5STheodore Ts'o 	case Opt_removed:
16745f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
167526092bf5STheodore Ts'o 		return 1;
167626092bf5STheodore Ts'o 	case Opt_abort:
167726092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
167826092bf5STheodore Ts'o 		return 1;
167926092bf5STheodore Ts'o 	case Opt_i_version:
1680*357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
168126092bf5STheodore Ts'o 		return 1;
1682a26f4992STheodore Ts'o 	case Opt_lazytime:
1683a26f4992STheodore Ts'o 		sb->s_flags |= MS_LAZYTIME;
1684a26f4992STheodore Ts'o 		return 1;
1685a26f4992STheodore Ts'o 	case Opt_nolazytime:
1686a26f4992STheodore Ts'o 		sb->s_flags &= ~MS_LAZYTIME;
1687a26f4992STheodore Ts'o 		return 1;
168826092bf5STheodore Ts'o 	}
168926092bf5STheodore Ts'o 
16905f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
16915f3633e3SJan Kara 		if (token == m->token)
16925f3633e3SJan Kara 			break;
16935f3633e3SJan Kara 
16945f3633e3SJan Kara 	if (m->token == Opt_err) {
16955f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
16965f3633e3SJan Kara 			 "or missing value", opt);
16975f3633e3SJan Kara 		return -1;
16985f3633e3SJan Kara 	}
16995f3633e3SJan Kara 
17008dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
17018dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
17028dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
17038dc0aa8cSTheodore Ts'o 		return -1;
17048dc0aa8cSTheodore Ts'o 	}
17058dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
17068dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
17078dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
17088dc0aa8cSTheodore Ts'o 		return -1;
17098dc0aa8cSTheodore Ts'o 	}
17108dc0aa8cSTheodore Ts'o 
1711ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
17120efb3b23SJan Kara 		return -1;
171326092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
171426092bf5STheodore Ts'o 		return -1;
1715c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1716c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
171726092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
17181e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
17191e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1720c93cf2d7SDmitry Monakhov 		} else
1721c93cf2d7SDmitry Monakhov 			return -1;
1722c93cf2d7SDmitry Monakhov 	}
172326092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
172426092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
172526092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
172626092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
172726092bf5STheodore Ts'o 			 "options when quota turned on");
172826092bf5STheodore Ts'o 		return -1;
172926092bf5STheodore Ts'o 	}
173026092bf5STheodore Ts'o 
173126092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
173226092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
173326092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
173426092bf5STheodore Ts'o 		if (arg == 0)
173526092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
173626092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
1737670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
1738670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
173926092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
174026092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
174126092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
174226092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
174326092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1744e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1745e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1746e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1747e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
174826092bf5STheodore Ts'o 			return -1;
174926092bf5STheodore Ts'o 		}
175026092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
175126092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
175226092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
175326092bf5STheodore Ts'o 		if (!args->from)
175426092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
175526092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1756df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1757df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
175826092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
175926092bf5STheodore Ts'o 		sbi->s_stripe = arg;
17600efb3b23SJan Kara 	} else if (token == Opt_resuid) {
17610efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
17620efb3b23SJan Kara 		if (!uid_valid(uid)) {
17635f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
17640efb3b23SJan Kara 			return -1;
17650efb3b23SJan Kara 		}
17660efb3b23SJan Kara 		sbi->s_resuid = uid;
17670efb3b23SJan Kara 	} else if (token == Opt_resgid) {
17680efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
17690efb3b23SJan Kara 		if (!gid_valid(gid)) {
17705f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
17710efb3b23SJan Kara 			return -1;
17720efb3b23SJan Kara 		}
17730efb3b23SJan Kara 		sbi->s_resgid = gid;
17740efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
17750efb3b23SJan Kara 		if (is_remount) {
17760efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
17770efb3b23SJan Kara 				 "Cannot specify journal on remount");
17780efb3b23SJan Kara 			return -1;
17790efb3b23SJan Kara 		}
17800efb3b23SJan Kara 		*journal_devnum = arg;
1781ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1782ad4eec61SEric Sandeen 		char *journal_path;
1783ad4eec61SEric Sandeen 		struct inode *journal_inode;
1784ad4eec61SEric Sandeen 		struct path path;
1785ad4eec61SEric Sandeen 		int error;
1786ad4eec61SEric Sandeen 
1787ad4eec61SEric Sandeen 		if (is_remount) {
1788ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1789ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
1790ad4eec61SEric Sandeen 			return -1;
1791ad4eec61SEric Sandeen 		}
1792ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
1793ad4eec61SEric Sandeen 		if (!journal_path) {
1794ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1795ad4eec61SEric Sandeen 				"journal device string");
1796ad4eec61SEric Sandeen 			return -1;
1797ad4eec61SEric Sandeen 		}
1798ad4eec61SEric Sandeen 
1799ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1800ad4eec61SEric Sandeen 		if (error) {
1801ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
1802ad4eec61SEric Sandeen 				"journal device path: error %d", error);
1803ad4eec61SEric Sandeen 			kfree(journal_path);
1804ad4eec61SEric Sandeen 			return -1;
1805ad4eec61SEric Sandeen 		}
1806ad4eec61SEric Sandeen 
18072b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
1808ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
1809ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1810ad4eec61SEric Sandeen 				"is not a block device", journal_path);
1811ad4eec61SEric Sandeen 			path_put(&path);
1812ad4eec61SEric Sandeen 			kfree(journal_path);
1813ad4eec61SEric Sandeen 			return -1;
1814ad4eec61SEric Sandeen 		}
1815ad4eec61SEric Sandeen 
1816ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1817ad4eec61SEric Sandeen 		path_put(&path);
1818ad4eec61SEric Sandeen 		kfree(journal_path);
18190efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
18200efb3b23SJan Kara 		if (arg > 7) {
18215f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
18220efb3b23SJan Kara 				 " (must be 0-7)");
18230efb3b23SJan Kara 			return -1;
18240efb3b23SJan Kara 		}
18250efb3b23SJan Kara 		*journal_ioprio =
18260efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
18276ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
18286ddb2447STheodore Ts'o #ifdef CONFIG_EXT4_FS_ENCRYPTION
18296ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
18306ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
18316ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
18326ddb2447STheodore Ts'o #else
18336ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
18346ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
18356ddb2447STheodore Ts'o #endif
183626092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
183726092bf5STheodore Ts'o 		if (is_remount) {
183826092bf5STheodore Ts'o 			if (!sbi->s_journal)
183926092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
18405f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
184126092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
184226092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
184326092bf5STheodore Ts'o 				return -1;
184426092bf5STheodore Ts'o 			}
184526092bf5STheodore Ts'o 		} else {
184626092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
184726092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
184826092bf5STheodore Ts'o 		}
184926092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
185026092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
185126092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
185226092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
18535f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
18545f3633e3SJan Kara 				 "quota options when quota turned on");
185526092bf5STheodore Ts'o 			return -1;
185626092bf5STheodore Ts'o 		}
1857e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
1858c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
1859c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
1860262b4662SJan Kara 				 "when QUOTA feature is enabled");
1861c325a67cSTheodore Ts'o 			return 1;
1862262b4662SJan Kara 		}
186326092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
186426092bf5STheodore Ts'o #endif
1865923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
1866ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
1867ef83b6e8SDan Williams 		ext4_msg(sb, KERN_WARNING,
1868ef83b6e8SDan Williams 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
1869ef83b6e8SDan Williams 			sbi->s_mount_opt |= m->mount_opt;
1870ef83b6e8SDan Williams #else
1871923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
1872923ae0ffSRoss Zwisler 		return -1;
1873923ae0ffSRoss Zwisler #endif
18747915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
18757915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
18767915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
18777915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
187826092bf5STheodore Ts'o 	} else {
187926092bf5STheodore Ts'o 		if (!args->from)
188026092bf5STheodore Ts'o 			arg = 1;
188126092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
188226092bf5STheodore Ts'o 			arg = !arg;
188326092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
188426092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
188526092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
188626092bf5STheodore Ts'o 			WARN_ON(1);
188726092bf5STheodore Ts'o 			return -1;
188826092bf5STheodore Ts'o 		}
188926092bf5STheodore Ts'o 		if (arg != 0)
189026092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
189126092bf5STheodore Ts'o 		else
189226092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
189326092bf5STheodore Ts'o 	}
189426092bf5STheodore Ts'o 	return 1;
189526092bf5STheodore Ts'o }
189626092bf5STheodore Ts'o 
1897ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1898c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1899b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1900661aa520SEric Sandeen 			 int is_remount)
1901ac27a0ecSDave Kleikamp {
1902617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1903ac27a0ecSDave Kleikamp 	char *p;
1904ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
190526092bf5STheodore Ts'o 	int token;
1906ac27a0ecSDave Kleikamp 
1907ac27a0ecSDave Kleikamp 	if (!options)
1908ac27a0ecSDave Kleikamp 		return 1;
1909ac27a0ecSDave Kleikamp 
1910ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1911ac27a0ecSDave Kleikamp 		if (!*p)
1912ac27a0ecSDave Kleikamp 			continue;
191315121c18SEric Sandeen 		/*
191415121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
191515121c18SEric Sandeen 		 * found; some options take optional arguments.
191615121c18SEric Sandeen 		 */
1917caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
1918ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
191926092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
192026092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
1921ac27a0ecSDave Kleikamp 			return 0;
1922ac27a0ecSDave Kleikamp 	}
1923ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
192449da9392SJan Kara 	/*
192549da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
192649da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
192749da9392SJan Kara 	 * to support legacy quotas in quota files.
192849da9392SJan Kara 	 */
192949da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
193049da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
193149da9392SJan Kara 			 "Cannot enable project quota enforcement.");
193249da9392SJan Kara 		return 0;
193349da9392SJan Kara 	}
193449da9392SJan Kara 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1935482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1936fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1937ac27a0ecSDave Kleikamp 
1938482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1939fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1940ac27a0ecSDave Kleikamp 
194156c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1942b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1943b31e1552SEric Sandeen 					"format mixing");
1944ac27a0ecSDave Kleikamp 			return 0;
1945ac27a0ecSDave Kleikamp 		}
1946ac27a0ecSDave Kleikamp 
1947ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1948b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1949b31e1552SEric Sandeen 					"not specified");
1950ac27a0ecSDave Kleikamp 			return 0;
1951ac27a0ecSDave Kleikamp 		}
1952ac27a0ecSDave Kleikamp 	}
1953ac27a0ecSDave Kleikamp #endif
1954261cb20cSJan Kara 	if (test_opt(sb, DIOREAD_NOLOCK)) {
1955261cb20cSJan Kara 		int blocksize =
1956261cb20cSJan Kara 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
1957261cb20cSJan Kara 
195809cbfeafSKirill A. Shutemov 		if (blocksize < PAGE_SIZE) {
1959261cb20cSJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
1960261cb20cSJan Kara 				 "dioread_nolock if block size != PAGE_SIZE");
1961261cb20cSJan Kara 			return 0;
1962261cb20cSJan Kara 		}
1963261cb20cSJan Kara 	}
1964ac27a0ecSDave Kleikamp 	return 1;
1965ac27a0ecSDave Kleikamp }
1966ac27a0ecSDave Kleikamp 
19672adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
19682adf6da8STheodore Ts'o 					   struct super_block *sb)
19692adf6da8STheodore Ts'o {
19702adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
19712adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
19722adf6da8STheodore Ts'o 
19732adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
19742adf6da8STheodore Ts'o 		char *fmtname = "";
19752adf6da8STheodore Ts'o 
19762adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
19772adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
19782adf6da8STheodore Ts'o 			fmtname = "vfsold";
19792adf6da8STheodore Ts'o 			break;
19802adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
19812adf6da8STheodore Ts'o 			fmtname = "vfsv0";
19822adf6da8STheodore Ts'o 			break;
19832adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
19842adf6da8STheodore Ts'o 			fmtname = "vfsv1";
19852adf6da8STheodore Ts'o 			break;
19862adf6da8STheodore Ts'o 		}
19872adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
19882adf6da8STheodore Ts'o 	}
19892adf6da8STheodore Ts'o 
19902adf6da8STheodore Ts'o 	if (sbi->s_qf_names[USRQUOTA])
1991a068acf2SKees Cook 		seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
19922adf6da8STheodore Ts'o 
19932adf6da8STheodore Ts'o 	if (sbi->s_qf_names[GRPQUOTA])
1994a068acf2SKees Cook 		seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
19952adf6da8STheodore Ts'o #endif
19962adf6da8STheodore Ts'o }
19972adf6da8STheodore Ts'o 
19985a916be1STheodore Ts'o static const char *token2str(int token)
19995a916be1STheodore Ts'o {
200050df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
20015a916be1STheodore Ts'o 
20025a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
20035a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
20045a916be1STheodore Ts'o 			break;
20055a916be1STheodore Ts'o 	return t->pattern;
20065a916be1STheodore Ts'o }
20075a916be1STheodore Ts'o 
20082adf6da8STheodore Ts'o /*
20092adf6da8STheodore Ts'o  * Show an option if
20102adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
20112adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
20122adf6da8STheodore Ts'o  */
201366acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
201466acdcf4STheodore Ts'o 			      int nodefs)
20152adf6da8STheodore Ts'o {
20162adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
20172adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
201866acdcf4STheodore Ts'o 	int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
20195a916be1STheodore Ts'o 	const struct mount_opts *m;
202066acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
20212adf6da8STheodore Ts'o 
202266acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
202366acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
20242adf6da8STheodore Ts'o 
20252adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
20265a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
20275a916be1STheodore Ts'o 
20285a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
20295a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
20305a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
20315a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
20325a916be1STheodore Ts'o 			continue;
203366acdcf4STheodore Ts'o 		if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
20345a916be1STheodore Ts'o 			continue; /* skip if same as the default */
20355a916be1STheodore Ts'o 		if ((want_set &&
20365a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
20375a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
20385a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
20395a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
20405a916be1STheodore Ts'o 	}
20415a916be1STheodore Ts'o 
204208cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
20435a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
204408cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
204508cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
204608cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
20475a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
204808cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
204908cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
205066acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
20515a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
20525a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
20532adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
20545a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
20552adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
20565a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
205766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
20585a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
205966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
20605a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
206166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
20625a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2063*357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
20645a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
206566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
20665a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
206766acdcf4STheodore Ts'o 	if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
20682adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
20695a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
20702adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
20715a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
20722adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
20735a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
20745a916be1STheodore Ts'o 	}
207566acdcf4STheodore Ts'o 	if (nodefs ||
207666acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
20775a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
20782adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
20792adf6da8STheodore Ts'o 
208066acdcf4STheodore Ts'o 	if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
208166acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
20825a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2083df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2084df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
20857915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
20867915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
20872adf6da8STheodore Ts'o 
20882adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
20892adf6da8STheodore Ts'o 	return 0;
20902adf6da8STheodore Ts'o }
20912adf6da8STheodore Ts'o 
209266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
209366acdcf4STheodore Ts'o {
209466acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
209566acdcf4STheodore Ts'o }
209666acdcf4STheodore Ts'o 
2097ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
209866acdcf4STheodore Ts'o {
209966acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
210066acdcf4STheodore Ts'o 	int rc;
210166acdcf4STheodore Ts'o 
2102bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
210366acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
210466acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
210566acdcf4STheodore Ts'o 	return rc;
210666acdcf4STheodore Ts'o }
210766acdcf4STheodore Ts'o 
2108617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2109ac27a0ecSDave Kleikamp 			    int read_only)
2110ac27a0ecSDave Kleikamp {
2111617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2112ac27a0ecSDave Kleikamp 	int res = 0;
2113ac27a0ecSDave Kleikamp 
2114617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2115b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2116b31e1552SEric Sandeen 			 "forcing read-only mode");
2117ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
2118ac27a0ecSDave Kleikamp 	}
2119ac27a0ecSDave Kleikamp 	if (read_only)
2120281b5995STheodore Ts'o 		goto done;
2121617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2122b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2123b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2124c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2125b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2126b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2127b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2128ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2129ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2130ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2131b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2132b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2133b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2134ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
2135ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
2136ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
2137b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2138b31e1552SEric Sandeen 			 "warning: checktime reached, "
2139b31e1552SEric Sandeen 			 "running e2fsck is recommended");
21400390131bSFrank Mayhar 	if (!sbi->s_journal)
2141216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2142ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2143617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2144e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
2145ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
2146617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
21470390131bSFrank Mayhar 	if (sbi->s_journal)
2148e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2149ac27a0ecSDave Kleikamp 
2150e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
2151281b5995STheodore Ts'o done:
2152ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2153a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2154a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2155ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2156ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2157617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2158617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2159a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2160ac27a0ecSDave Kleikamp 
21617abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2162ac27a0ecSDave Kleikamp 	return res;
2163ac27a0ecSDave Kleikamp }
2164ac27a0ecSDave Kleikamp 
2165117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2166117fff10STheodore Ts'o {
2167117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2168117fff10STheodore Ts'o 	struct flex_groups *new_groups;
2169117fff10STheodore Ts'o 	int size;
2170117fff10STheodore Ts'o 
2171117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2172117fff10STheodore Ts'o 		return 0;
2173117fff10STheodore Ts'o 
2174117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2175117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2176117fff10STheodore Ts'o 		return 0;
2177117fff10STheodore Ts'o 
2178117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
2179a7c3e901SMichal Hocko 	new_groups = kvzalloc(size, GFP_KERNEL);
2180117fff10STheodore Ts'o 	if (!new_groups) {
2181117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
2182117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
2183117fff10STheodore Ts'o 		return -ENOMEM;
2184117fff10STheodore Ts'o 	}
2185117fff10STheodore Ts'o 
2186117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
2187117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
2188117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
2189117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
2190b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
2191117fff10STheodore Ts'o 	}
2192117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
2193117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
2194117fff10STheodore Ts'o 	return 0;
2195117fff10STheodore Ts'o }
2196117fff10STheodore Ts'o 
2197772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2198772cb7c8SJose R. Santos {
2199772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2200772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
2201772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2202117fff10STheodore Ts'o 	int i, err;
2203772cb7c8SJose R. Santos 
2204503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2205d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2206772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2207772cb7c8SJose R. Santos 		return 1;
2208772cb7c8SJose R. Santos 	}
2209772cb7c8SJose R. Santos 
2210117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2211117fff10STheodore Ts'o 	if (err)
2212772cb7c8SJose R. Santos 		goto failed;
2213772cb7c8SJose R. Santos 
2214772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
221588b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2216772cb7c8SJose R. Santos 
2217772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
22187ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
22197ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
222090ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
222124aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
22227ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
22237ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2224772cb7c8SJose R. Santos 	}
2225772cb7c8SJose R. Santos 
2226772cb7c8SJose R. Santos 	return 1;
2227772cb7c8SJose R. Santos failed:
2228772cb7c8SJose R. Santos 	return 0;
2229772cb7c8SJose R. Santos }
2230772cb7c8SJose R. Santos 
2231e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2232717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2233717d50e4SAndreas Dilger {
2234b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2235717d50e4SAndreas Dilger 	__u16 crc = 0;
2236717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2237e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2238717d50e4SAndreas Dilger 
22399aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2240feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2241feb0ab32SDarrick J. Wong 		__u32 csum32;
2242b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2243feb0ab32SDarrick J. Wong 
2244feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2245feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2246b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2247b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2248b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2249b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2250b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2251b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2252b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2253feb0ab32SDarrick J. Wong 
2254feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2255feb0ab32SDarrick J. Wong 		goto out;
2256feb0ab32SDarrick J. Wong 	}
2257feb0ab32SDarrick J. Wong 
2258feb0ab32SDarrick J. Wong 	/* old crc16 code */
2259e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2260813d32f9SDarrick J. Wong 		return 0;
2261813d32f9SDarrick J. Wong 
2262717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2263717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2264717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2265717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2266717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2267e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2268717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2269717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2270717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2271717d50e4SAndreas Dilger 				offset);
2272717d50e4SAndreas Dilger 
2273feb0ab32SDarrick J. Wong out:
2274717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2275717d50e4SAndreas Dilger }
2276717d50e4SAndreas Dilger 
2277feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2278717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2279717d50e4SAndreas Dilger {
2280feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2281e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2282717d50e4SAndreas Dilger 		return 0;
2283717d50e4SAndreas Dilger 
2284717d50e4SAndreas Dilger 	return 1;
2285717d50e4SAndreas Dilger }
2286717d50e4SAndreas Dilger 
2287feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2288feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2289feb0ab32SDarrick J. Wong {
2290feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2291feb0ab32SDarrick J. Wong 		return;
2292e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2293feb0ab32SDarrick J. Wong }
2294feb0ab32SDarrick J. Wong 
2295ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2296bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2297829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2298bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2299ac27a0ecSDave Kleikamp {
2300617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2301617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2302617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2303bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2304bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2305bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2306ce421581SJose R. Santos 	int flexbg_flag = 0;
2307bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2308ac27a0ecSDave Kleikamp 
2309e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2310ce421581SJose R. Santos 		flexbg_flag = 1;
2311ce421581SJose R. Santos 
2312617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2313ac27a0ecSDave Kleikamp 
2314197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2315197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2316197cd65aSAkinobu Mita 
2317ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2318bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2319ac27a0ecSDave Kleikamp 		else
2320ac27a0ecSDave Kleikamp 			last_block = first_block +
2321617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2322ac27a0ecSDave Kleikamp 
2323bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2324bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2325bfff6873SLukas Czerner 			grp = i;
2326bfff6873SLukas Czerner 
23278fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2328829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2329829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2330829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2331829fa70dSTheodore Ts'o 				 "superblock", i);
2332829fa70dSTheodore Ts'o 		}
23332b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2334b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2335a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2336b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2337ac27a0ecSDave Kleikamp 			return 0;
2338ac27a0ecSDave Kleikamp 		}
23398fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2340829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2341829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2342829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2343829fa70dSTheodore Ts'o 				 "superblock", i);
2344829fa70dSTheodore Ts'o 		}
23452b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2346b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2347a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2348b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2349ac27a0ecSDave Kleikamp 			return 0;
2350ac27a0ecSDave Kleikamp 		}
23518fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2352829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2353829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2354829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2355829fa70dSTheodore Ts'o 				 "superblock", i);
2356829fa70dSTheodore Ts'o 		}
2357bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
23582b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2359b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2360a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2361b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2362ac27a0ecSDave Kleikamp 			return 0;
2363ac27a0ecSDave Kleikamp 		}
2364955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2365feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2366b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2367b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2368e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2369fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2370bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2371955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2372717d50e4SAndreas Dilger 				return 0;
2373717d50e4SAndreas Dilger 			}
23747ee1ec4cSLi Zefan 		}
2375955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2376ce421581SJose R. Santos 		if (!flexbg_flag)
2377617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2378ac27a0ecSDave Kleikamp 	}
2379bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2380bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2381ac27a0ecSDave Kleikamp 	return 1;
2382ac27a0ecSDave Kleikamp }
2383ac27a0ecSDave Kleikamp 
2384617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2385ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2386ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2387ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2388ac27a0ecSDave Kleikamp  *
2389ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2390ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2391ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2392ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2393ac27a0ecSDave Kleikamp  *
2394ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2395ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2396ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2397617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2398ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2399ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2400ac27a0ecSDave Kleikamp  */
2401617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2402617ba13bSMingming Cao 				struct ext4_super_block *es)
2403ac27a0ecSDave Kleikamp {
2404ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
24052c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2406ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
240795f1fda4Szhangyi (F) 	int quota_update = 0;
2408ac27a0ecSDave Kleikamp 	int i;
2409ac27a0ecSDave Kleikamp #endif
2410ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2411ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2412ac27a0ecSDave Kleikamp 		return;
2413ac27a0ecSDave Kleikamp 	}
2414ac27a0ecSDave Kleikamp 
2415a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2416b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2417b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2418a8f48a95SEric Sandeen 		return;
2419a8f48a95SEric Sandeen 	}
2420a8f48a95SEric Sandeen 
2421d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2422d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2423d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2424d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2425d39195c3SAmir Goldstein 		return;
2426d39195c3SAmir Goldstein 	}
2427d39195c3SAmir Goldstein 
2428617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2429c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
2430c25f9bc6SEric Sandeen 		if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
243184474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2432ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2433ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2434c25f9bc6SEric Sandeen 		}
2435ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2436ac27a0ecSDave Kleikamp 		return;
2437ac27a0ecSDave Kleikamp 	}
2438ac27a0ecSDave Kleikamp 
2439ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2440b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2441ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2442ac27a0ecSDave Kleikamp 	}
2443ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2444ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2445ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
244695f1fda4Szhangyi (F) 
244795f1fda4Szhangyi (F) 	/*
244895f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
244995f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
245095f1fda4Szhangyi (F) 	 */
245195f1fda4Szhangyi (F) 	if (ext4_has_feature_quota(sb) && (s_flags & MS_RDONLY)) {
245295f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
245395f1fda4Szhangyi (F) 
245495f1fda4Szhangyi (F) 		if (!ret)
245595f1fda4Szhangyi (F) 			quota_update = 1;
245695f1fda4Szhangyi (F) 		else
245795f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
245895f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
245995f1fda4Szhangyi (F) 	}
246095f1fda4Szhangyi (F) 
246195f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2462a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2463617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2464617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
246595f1fda4Szhangyi (F) 
246695f1fda4Szhangyi (F) 			if (!ret)
246795f1fda4Szhangyi (F) 				quota_update = 1;
246895f1fda4Szhangyi (F) 			else
2469b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2470b31e1552SEric Sandeen 					"Cannot turn on journaled "
247195f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2472ac27a0ecSDave Kleikamp 		}
2473ac27a0ecSDave Kleikamp 	}
2474ac27a0ecSDave Kleikamp #endif
2475ac27a0ecSDave Kleikamp 
2476ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2477ac27a0ecSDave Kleikamp 		struct inode *inode;
2478ac27a0ecSDave Kleikamp 
2479c65d5c6cSVegard Nossum 		/*
2480c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2481c65d5c6cSVegard Nossum 		 * so, skip the rest.
2482c65d5c6cSVegard Nossum 		 */
2483c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2484c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2485c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2486c65d5c6cSVegard Nossum 			break;
2487c65d5c6cSVegard Nossum 		}
2488c65d5c6cSVegard Nossum 
248997bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
249097bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2491ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2492ac27a0ecSDave Kleikamp 			break;
2493ac27a0ecSDave Kleikamp 		}
2494ac27a0ecSDave Kleikamp 
2495617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2496871a2931SChristoph Hellwig 		dquot_initialize(inode);
2497ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2498566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2499b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2500b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
250146e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2502e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2503ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
25045955102cSAl Viro 			inode_lock(inode);
250555f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
25062c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
25072c98eb5eSTheodore Ts'o 			if (ret)
25082c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
25095955102cSAl Viro 			inode_unlock(inode);
2510ac27a0ecSDave Kleikamp 			nr_truncates++;
2511ac27a0ecSDave Kleikamp 		} else {
2512566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2513b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2514b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
251546e665e9SHarvey Harrison 					__func__, inode->i_ino);
2516ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2517ac27a0ecSDave Kleikamp 				  inode->i_ino);
2518ac27a0ecSDave Kleikamp 			nr_orphans++;
2519ac27a0ecSDave Kleikamp 		}
2520ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2521ac27a0ecSDave Kleikamp 	}
2522ac27a0ecSDave Kleikamp 
2523ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2524ac27a0ecSDave Kleikamp 
2525ac27a0ecSDave Kleikamp 	if (nr_orphans)
2526b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2527b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2528ac27a0ecSDave Kleikamp 	if (nr_truncates)
2529b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2530b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2531ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
253295f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
253395f1fda4Szhangyi (F) 	if (quota_update) {
2534a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
253595f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2536287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2537ac27a0ecSDave Kleikamp 		}
253895f1fda4Szhangyi (F) 	}
2539ac27a0ecSDave Kleikamp #endif
2540ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2541ac27a0ecSDave Kleikamp }
25420b8e58a1SAndreas Dilger 
2543cd2291a4SEric Sandeen /*
2544cd2291a4SEric Sandeen  * Maximal extent format file size.
2545cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2546cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2547cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2548cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2549cd2291a4SEric Sandeen  *
2550f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2551f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2552f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2553f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2554f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2555f17722f9SLukas Czerner  *
2556cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2557cd2291a4SEric Sandeen  */
2558f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2559cd2291a4SEric Sandeen {
2560cd2291a4SEric Sandeen 	loff_t res;
2561cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2562cd2291a4SEric Sandeen 
2563cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2564f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2565cd2291a4SEric Sandeen 		/*
256690c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2567cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2568cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2569cd2291a4SEric Sandeen 		 */
2570cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2571cd2291a4SEric Sandeen 
2572cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2573cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2574cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2575cd2291a4SEric Sandeen 	}
2576cd2291a4SEric Sandeen 
2577f17722f9SLukas Czerner 	/*
2578f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2579f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2580f17722f9SLukas Czerner 	 * size
2581f17722f9SLukas Czerner 	 */
2582f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2583cd2291a4SEric Sandeen 	res <<= blkbits;
2584cd2291a4SEric Sandeen 
2585cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2586cd2291a4SEric Sandeen 	if (res > upper_limit)
2587cd2291a4SEric Sandeen 		res = upper_limit;
2588cd2291a4SEric Sandeen 
2589cd2291a4SEric Sandeen 	return res;
2590cd2291a4SEric Sandeen }
2591ac27a0ecSDave Kleikamp 
2592ac27a0ecSDave Kleikamp /*
2593cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
25940fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
25950fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2596ac27a0ecSDave Kleikamp  */
2597f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2598ac27a0ecSDave Kleikamp {
2599617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
26000fc1b451SAneesh Kumar K.V 	int meta_blocks;
26010fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
26020b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
26030b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
26040b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
26050b8e58a1SAndreas Dilger 	 *
26060b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
26070b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
26080fc1b451SAneesh Kumar K.V 	 */
26090fc1b451SAneesh Kumar K.V 
2610f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
26110fc1b451SAneesh Kumar K.V 		/*
261290c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
26130b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
26140b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
26150fc1b451SAneesh Kumar K.V 		 */
26160fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
26170fc1b451SAneesh Kumar K.V 
26180fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
26190fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
26200fc1b451SAneesh Kumar K.V 
26210fc1b451SAneesh Kumar K.V 	} else {
26228180a562SAneesh Kumar K.V 		/*
26238180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
26248180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
26258180a562SAneesh Kumar K.V 		 * represent total number of blocks in
26268180a562SAneesh Kumar K.V 		 * file system block size
26278180a562SAneesh Kumar K.V 		 */
26280fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
26290fc1b451SAneesh Kumar K.V 
26300fc1b451SAneesh Kumar K.V 	}
26310fc1b451SAneesh Kumar K.V 
26320fc1b451SAneesh Kumar K.V 	/* indirect blocks */
26330fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
26340fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
26350fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
26360fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
26370fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
26380fc1b451SAneesh Kumar K.V 
26390fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
26400fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2641ac27a0ecSDave Kleikamp 
2642ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2643ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2644ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2645ac27a0ecSDave Kleikamp 	res <<= bits;
2646ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2647ac27a0ecSDave Kleikamp 		res = upper_limit;
26480fc1b451SAneesh Kumar K.V 
26490fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
26500fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
26510fc1b451SAneesh Kumar K.V 
2652ac27a0ecSDave Kleikamp 	return res;
2653ac27a0ecSDave Kleikamp }
2654ac27a0ecSDave Kleikamp 
2655617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
265670bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2657ac27a0ecSDave Kleikamp {
2658617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2659fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2660ac27a0ecSDave Kleikamp 	int has_super = 0;
2661ac27a0ecSDave Kleikamp 
2662ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2663ac27a0ecSDave Kleikamp 
2664e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
266570bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2666ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2667617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2668ac27a0ecSDave Kleikamp 		has_super = 1;
26690b8e58a1SAndreas Dilger 
2670bd63f6b0SDarrick J. Wong 	/*
2671bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2672bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2673bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2674bd63f6b0SDarrick J. Wong 	 * compensate.
2675bd63f6b0SDarrick J. Wong 	 */
2676bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
2677bd63f6b0SDarrick J. Wong 	    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) == 0)
2678bd63f6b0SDarrick J. Wong 		has_super++;
2679bd63f6b0SDarrick J. Wong 
2680617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2681ac27a0ecSDave Kleikamp }
2682ac27a0ecSDave Kleikamp 
2683c9de560dSAlex Tomas /**
2684c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2685c9de560dSAlex Tomas  * @sbi: In memory super block info
2686c9de560dSAlex Tomas  *
2687c9de560dSAlex Tomas  * If we have specified it via mount option, then
2688c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2689c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2690c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2691c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2692c9de560dSAlex Tomas  *
2693c9de560dSAlex Tomas  */
2694c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2695c9de560dSAlex Tomas {
2696c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2697c9de560dSAlex Tomas 	unsigned long stripe_width =
2698c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
26993eb08658SDan Ehrenberg 	int ret;
2700c9de560dSAlex Tomas 
2701c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
27023eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
27035469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
27043eb08658SDan Ehrenberg 		ret = stripe_width;
27055469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
27063eb08658SDan Ehrenberg 		ret = stride;
27073eb08658SDan Ehrenberg 	else
27083eb08658SDan Ehrenberg 		ret = 0;
2709c9de560dSAlex Tomas 
27103eb08658SDan Ehrenberg 	/*
27113eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
27123eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
27133eb08658SDan Ehrenberg 	 */
27143eb08658SDan Ehrenberg 	if (ret <= 1)
27153eb08658SDan Ehrenberg 		ret = 0;
2716c9de560dSAlex Tomas 
27173eb08658SDan Ehrenberg 	return ret;
2718c9de560dSAlex Tomas }
2719ac27a0ecSDave Kleikamp 
2720a13fb1a4SEric Sandeen /*
2721a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2722a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2723a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2724a13fb1a4SEric Sandeen  * 0 if it cannot be.
2725a13fb1a4SEric Sandeen  */
2726a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2727a13fb1a4SEric Sandeen {
2728e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2729a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2730a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2731a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2732a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2733a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2734a13fb1a4SEric Sandeen 		return 0;
2735a13fb1a4SEric Sandeen 	}
2736a13fb1a4SEric Sandeen 
2737a13fb1a4SEric Sandeen 	if (readonly)
2738a13fb1a4SEric Sandeen 		return 1;
2739a13fb1a4SEric Sandeen 
2740e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
27412cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
27422cb5cc8bSDarrick J. Wong 		sb->s_flags |= MS_RDONLY;
27432cb5cc8bSDarrick J. Wong 		return 1;
27442cb5cc8bSDarrick J. Wong 	}
27452cb5cc8bSDarrick J. Wong 
2746a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2747e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
2748a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2749a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2750a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2751a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2752a13fb1a4SEric Sandeen 		return 0;
2753a13fb1a4SEric Sandeen 	}
2754a13fb1a4SEric Sandeen 	/*
2755a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2756a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2757a13fb1a4SEric Sandeen 	 */
2758e2b911c5SDarrick J. Wong 	if (ext4_has_feature_huge_file(sb)) {
2759a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2760a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2761a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2762a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2763a13fb1a4SEric Sandeen 			return 0;
2764a13fb1a4SEric Sandeen 		}
2765a13fb1a4SEric Sandeen 	}
2766e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
2767bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2768bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2769bab08ab9STheodore Ts'o 			 "extents feature\n");
2770bab08ab9STheodore Ts'o 		return 0;
2771bab08ab9STheodore Ts'o 	}
27727c319d32SAditya Kali 
27737c319d32SAditya Kali #ifndef CONFIG_QUOTA
2774e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !readonly) {
27757c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
27767c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
27777c319d32SAditya Kali 			 "without CONFIG_QUOTA");
27787c319d32SAditya Kali 		return 0;
27797c319d32SAditya Kali 	}
2780689c958cSLi Xi 	if (ext4_has_feature_project(sb) && !readonly) {
2781689c958cSLi Xi 		ext4_msg(sb, KERN_ERR,
2782689c958cSLi Xi 			 "Filesystem with project quota feature cannot be mounted RDWR "
2783689c958cSLi Xi 			 "without CONFIG_QUOTA");
2784689c958cSLi Xi 		return 0;
2785689c958cSLi Xi 	}
27867c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2787a13fb1a4SEric Sandeen 	return 1;
2788a13fb1a4SEric Sandeen }
2789a13fb1a4SEric Sandeen 
279066e61a9eSTheodore Ts'o /*
279166e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
279266e61a9eSTheodore Ts'o  * on the file system
279366e61a9eSTheodore Ts'o  */
279466e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
279566e61a9eSTheodore Ts'o {
279666e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
279766e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
279866e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
279966e61a9eSTheodore Ts'o 
280066e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
280166e61a9eSTheodore Ts'o 	es = sbi->s_es;
280266e61a9eSTheodore Ts'o 
280366e61a9eSTheodore Ts'o 	if (es->s_error_count)
2804ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
2805ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
280666e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
280766e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
2808ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %u: %.*s:%d",
280966e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
281066e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
281166e61a9eSTheodore Ts'o 		       es->s_first_error_func,
281266e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
281366e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
2814651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
281566e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
281666e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
2817651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
281866e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
2819651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
282066e61a9eSTheodore Ts'o 	}
282166e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
2822ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %u: %.*s:%d",
282366e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
282466e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
282566e61a9eSTheodore Ts'o 		       es->s_last_error_func,
282666e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
282766e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
2828651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
282966e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
283066e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
2831651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
283266e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
2833651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
283466e61a9eSTheodore Ts'o 	}
283566e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
283666e61a9eSTheodore Ts'o }
283766e61a9eSTheodore Ts'o 
2838bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2839bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2840bfff6873SLukas Czerner {
2841bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2842bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2843bfff6873SLukas Czerner 	struct super_block *sb;
2844bfff6873SLukas Czerner 	unsigned long timeout = 0;
2845bfff6873SLukas Czerner 	int ret = 0;
2846bfff6873SLukas Czerner 
2847bfff6873SLukas Czerner 	sb = elr->lr_super;
2848bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2849bfff6873SLukas Czerner 
2850bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2851bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2852bfff6873SLukas Czerner 		if (!gdp) {
2853bfff6873SLukas Czerner 			ret = 1;
2854bfff6873SLukas Czerner 			break;
2855bfff6873SLukas Czerner 		}
2856bfff6873SLukas Czerner 
2857bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2858bfff6873SLukas Czerner 			break;
2859bfff6873SLukas Czerner 	}
2860bfff6873SLukas Czerner 
28617f511862STheodore Ts'o 	if (group >= ngroups)
2862bfff6873SLukas Czerner 		ret = 1;
2863bfff6873SLukas Czerner 
2864bfff6873SLukas Czerner 	if (!ret) {
2865bfff6873SLukas Czerner 		timeout = jiffies;
2866bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2867bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2868bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
286951ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
287051ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2871bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2872bfff6873SLukas Czerner 		}
2873bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2874bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2875bfff6873SLukas Czerner 	}
2876bfff6873SLukas Czerner 	return ret;
2877bfff6873SLukas Czerner }
2878bfff6873SLukas Czerner 
2879bfff6873SLukas Czerner /*
2880bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
28814ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2882bfff6873SLukas Czerner  */
2883bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2884bfff6873SLukas Czerner {
2885bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2886bfff6873SLukas Czerner 
2887bfff6873SLukas Czerner 	if (!elr)
2888bfff6873SLukas Czerner 		return;
2889bfff6873SLukas Czerner 
2890bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2891bfff6873SLukas Czerner 
2892bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2893bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2894bfff6873SLukas Czerner 	kfree(elr);
2895bfff6873SLukas Czerner }
2896bfff6873SLukas Czerner 
2897bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2898bfff6873SLukas Czerner {
28991bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
29001bb933fbSLukas Czerner 	if (!ext4_li_info) {
29011bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2902bfff6873SLukas Czerner 		return;
29031bb933fbSLukas Czerner 	}
2904bfff6873SLukas Czerner 
2905bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
29061bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2907bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
29081bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2909bfff6873SLukas Czerner }
2910bfff6873SLukas Czerner 
29118f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
29128f1f7453SEric Sandeen 
2913bfff6873SLukas Czerner /*
2914bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2915bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2916bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2917bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2918bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2919bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2920bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2921bfff6873SLukas Czerner  */
2922bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2923bfff6873SLukas Czerner {
2924bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2925bfff6873SLukas Czerner 	struct list_head *pos, *n;
2926bfff6873SLukas Czerner 	struct ext4_li_request *elr;
29274ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2928bfff6873SLukas Czerner 
2929bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2930bfff6873SLukas Czerner 
2931bfff6873SLukas Czerner cont_thread:
2932bfff6873SLukas Czerner 	while (true) {
2933bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2934bfff6873SLukas Czerner 
2935bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2936bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2937bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2938bfff6873SLukas Czerner 			goto exit_thread;
2939bfff6873SLukas Czerner 		}
2940bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2941e22834f0SDmitry Monakhov 			int err = 0;
2942e22834f0SDmitry Monakhov 			int progress = 0;
2943bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2944bfff6873SLukas Czerner 					 lr_request);
2945bfff6873SLukas Czerner 
2946e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
2947e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
2948e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
2949e22834f0SDmitry Monakhov 				continue;
2950e22834f0SDmitry Monakhov 			}
2951e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
2952e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
2953e22834f0SDmitry Monakhov 					progress = 1;
2954e22834f0SDmitry Monakhov 					/*
2955e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
2956e22834f0SDmitry Monakhov 					 * be removed from the list, it is
2957e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
2958e22834f0SDmitry Monakhov 					 */
2959e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
2960e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
2961e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
2962e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
2963e22834f0SDmitry Monakhov 					n = pos->next;
2964e22834f0SDmitry Monakhov 				}
2965e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
2966e22834f0SDmitry Monakhov 			}
2967b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
2968e22834f0SDmitry Monakhov 			if (err) {
2969bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
2970bfff6873SLukas Czerner 				continue;
2971bfff6873SLukas Czerner 			}
2972e22834f0SDmitry Monakhov 			if (!progress) {
2973e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
2974e22834f0SDmitry Monakhov 					(prandom_u32()
2975e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
2976b2c78cd0STheodore Ts'o 			}
2977bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2978bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2979bfff6873SLukas Czerner 		}
2980bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2981bfff6873SLukas Czerner 
2982a0acae0eSTejun Heo 		try_to_freeze();
2983bfff6873SLukas Czerner 
29844ed5c033SLukas Czerner 		cur = jiffies;
29854ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2986f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2987bfff6873SLukas Czerner 			cond_resched();
2988bfff6873SLukas Czerner 			continue;
2989bfff6873SLukas Czerner 		}
2990bfff6873SLukas Czerner 
29914ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
29924ed5c033SLukas Czerner 
29938f1f7453SEric Sandeen 		if (kthread_should_stop()) {
29948f1f7453SEric Sandeen 			ext4_clear_request_list();
29958f1f7453SEric Sandeen 			goto exit_thread;
29968f1f7453SEric Sandeen 		}
2997bfff6873SLukas Czerner 	}
2998bfff6873SLukas Czerner 
2999bfff6873SLukas Czerner exit_thread:
3000bfff6873SLukas Czerner 	/*
3001bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3002bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3003bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3004bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3005bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3006bfff6873SLukas Czerner 	 * new one.
3007bfff6873SLukas Czerner 	 */
3008bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3009bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3010bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3011bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3012bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3013bfff6873SLukas Czerner 		goto cont_thread;
3014bfff6873SLukas Czerner 	}
3015bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3016bfff6873SLukas Czerner 	kfree(ext4_li_info);
3017bfff6873SLukas Czerner 	ext4_li_info = NULL;
3018bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3019bfff6873SLukas Czerner 
3020bfff6873SLukas Czerner 	return 0;
3021bfff6873SLukas Czerner }
3022bfff6873SLukas Czerner 
3023bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3024bfff6873SLukas Czerner {
3025bfff6873SLukas Czerner 	struct list_head *pos, *n;
3026bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3027bfff6873SLukas Czerner 
3028bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3029bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3030bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3031bfff6873SLukas Czerner 				 lr_request);
3032bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3033bfff6873SLukas Czerner 	}
3034bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3035bfff6873SLukas Czerner }
3036bfff6873SLukas Czerner 
3037bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3038bfff6873SLukas Czerner {
30398f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
30408f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
30418f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
30428f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3043bfff6873SLukas Czerner 		ext4_clear_request_list();
3044bfff6873SLukas Czerner 		kfree(ext4_li_info);
3045bfff6873SLukas Czerner 		ext4_li_info = NULL;
304692b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3047bfff6873SLukas Czerner 				 "initialization thread\n",
3048bfff6873SLukas Czerner 				 err);
3049bfff6873SLukas Czerner 		return err;
3050bfff6873SLukas Czerner 	}
3051bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3052bfff6873SLukas Czerner 	return 0;
3053bfff6873SLukas Czerner }
3054bfff6873SLukas Czerner 
3055bfff6873SLukas Czerner /*
3056bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3057bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3058bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3059bfff6873SLukas Czerner  * groups and return total number of groups.
3060bfff6873SLukas Czerner  */
3061bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3062bfff6873SLukas Czerner {
3063bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3064bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3065bfff6873SLukas Czerner 
3066bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3067bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3068bfff6873SLukas Czerner 		if (!gdp)
3069bfff6873SLukas Czerner 			continue;
3070bfff6873SLukas Czerner 
3071bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3072bfff6873SLukas Czerner 			break;
3073bfff6873SLukas Czerner 	}
3074bfff6873SLukas Czerner 
3075bfff6873SLukas Czerner 	return group;
3076bfff6873SLukas Czerner }
3077bfff6873SLukas Czerner 
3078bfff6873SLukas Czerner static int ext4_li_info_new(void)
3079bfff6873SLukas Czerner {
3080bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3081bfff6873SLukas Czerner 
3082bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3083bfff6873SLukas Czerner 	if (!eli)
3084bfff6873SLukas Czerner 		return -ENOMEM;
3085bfff6873SLukas Czerner 
3086bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3087bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3088bfff6873SLukas Czerner 
3089bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3090bfff6873SLukas Czerner 
3091bfff6873SLukas Czerner 	ext4_li_info = eli;
3092bfff6873SLukas Czerner 
3093bfff6873SLukas Czerner 	return 0;
3094bfff6873SLukas Czerner }
3095bfff6873SLukas Czerner 
3096bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3097bfff6873SLukas Czerner 					    ext4_group_t start)
3098bfff6873SLukas Czerner {
3099bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3100bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3101bfff6873SLukas Czerner 
3102bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3103bfff6873SLukas Czerner 	if (!elr)
3104bfff6873SLukas Czerner 		return NULL;
3105bfff6873SLukas Czerner 
3106bfff6873SLukas Czerner 	elr->lr_super = sb;
3107bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3108bfff6873SLukas Czerner 	elr->lr_next_group = start;
3109bfff6873SLukas Czerner 
3110bfff6873SLukas Czerner 	/*
3111bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3112bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3113bfff6873SLukas Czerner 	 * better.
3114bfff6873SLukas Czerner 	 */
3115dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3116dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3117bfff6873SLukas Czerner 	return elr;
3118bfff6873SLukas Czerner }
3119bfff6873SLukas Czerner 
31207f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3121bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3122bfff6873SLukas Czerner {
3123bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
31247f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
3125bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
31266c5a6cb9SAndrew Morton 	int ret = 0;
3127bfff6873SLukas Czerner 
31287f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
312951ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
313051ce6511SLukas Czerner 		/*
313151ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
313251ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
313351ce6511SLukas Czerner 		 */
313451ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
31357f511862STheodore Ts'o 		goto out;
313651ce6511SLukas Czerner 	}
3137bfff6873SLukas Czerner 
3138bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
313955ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
31407f511862STheodore Ts'o 		goto out;
3141bfff6873SLukas Czerner 
3142bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
31437f511862STheodore Ts'o 	if (!elr) {
31447f511862STheodore Ts'o 		ret = -ENOMEM;
31457f511862STheodore Ts'o 		goto out;
31467f511862STheodore Ts'o 	}
3147bfff6873SLukas Czerner 
3148bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3149bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3150bfff6873SLukas Czerner 		if (ret)
3151bfff6873SLukas Czerner 			goto out;
3152bfff6873SLukas Czerner 	}
3153bfff6873SLukas Czerner 
3154bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3155bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3156bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3157bfff6873SLukas Czerner 
3158bfff6873SLukas Czerner 	sbi->s_li_request = elr;
315946e4690bSTao Ma 	/*
316046e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
316146e4690bSTao Ma 	 * the request_list and the removal and free of it is
316246e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
316346e4690bSTao Ma 	 */
316446e4690bSTao Ma 	elr = NULL;
3165bfff6873SLukas Czerner 
3166bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3167bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3168bfff6873SLukas Czerner 		if (ret)
3169bfff6873SLukas Czerner 			goto out;
3170bfff6873SLukas Czerner 	}
3171bfff6873SLukas Czerner out:
3172bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3173beed5ecbSNicolas Kaiser 	if (ret)
3174bfff6873SLukas Czerner 		kfree(elr);
3175bfff6873SLukas Czerner 	return ret;
3176bfff6873SLukas Czerner }
3177bfff6873SLukas Czerner 
3178bfff6873SLukas Czerner /*
3179bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3180bfff6873SLukas Czerner  * module unload.
3181bfff6873SLukas Czerner  */
3182bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3183bfff6873SLukas Czerner {
3184bfff6873SLukas Czerner 	/*
3185bfff6873SLukas Czerner 	 * If thread exited earlier
3186bfff6873SLukas Czerner 	 * there's nothing to be done.
3187bfff6873SLukas Czerner 	 */
31888f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3189bfff6873SLukas Czerner 		return;
3190bfff6873SLukas Czerner 
31918f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3192bfff6873SLukas Czerner }
3193bfff6873SLukas Czerner 
319425ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
319525ed6e8aSDarrick J. Wong {
319625ed6e8aSDarrick J. Wong 	int ret = 1;
319725ed6e8aSDarrick J. Wong 	int compat, incompat;
319825ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
319925ed6e8aSDarrick J. Wong 
32009aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3201db9ee220SDarrick J. Wong 		/* journal checksum v3 */
320225ed6e8aSDarrick J. Wong 		compat = 0;
3203db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
320425ed6e8aSDarrick J. Wong 	} else {
320525ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
320625ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
320725ed6e8aSDarrick J. Wong 		incompat = 0;
320825ed6e8aSDarrick J. Wong 	}
320925ed6e8aSDarrick J. Wong 
3210feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3211feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3212feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3213feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
321425ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
321525ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
321625ed6e8aSDarrick J. Wong 				compat, 0,
321725ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
321825ed6e8aSDarrick J. Wong 				incompat);
321925ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
322025ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
322125ed6e8aSDarrick J. Wong 				compat, 0,
322225ed6e8aSDarrick J. Wong 				incompat);
322325ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
322425ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
322525ed6e8aSDarrick J. Wong 	} else {
3226feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3227feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
322825ed6e8aSDarrick J. Wong 	}
322925ed6e8aSDarrick J. Wong 
323025ed6e8aSDarrick J. Wong 	return ret;
323125ed6e8aSDarrick J. Wong }
323225ed6e8aSDarrick J. Wong 
3233952fc18eSTheodore Ts'o /*
3234952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3235952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3236952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3237952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3238952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3239952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3240952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3241952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3242952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3243952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3244952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3245952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3246952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3247952fc18eSTheodore Ts'o  */
3248952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3249952fc18eSTheodore Ts'o 			  char *buf)
3250952fc18eSTheodore Ts'o {
3251952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3252952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3253952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3254952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3255952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3256952fc18eSTheodore Ts'o 
3257e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
32580548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
32590548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
32600548bbb8STheodore Ts'o 
3261952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3262952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3263952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3264952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3265952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3266952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3267952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3268952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3269952fc18eSTheodore Ts'o 			count++;
3270952fc18eSTheodore Ts'o 		}
3271952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3272952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3273952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3274952fc18eSTheodore Ts'o 			count++;
3275952fc18eSTheodore Ts'o 		}
3276952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3277952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3278952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3279952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3280952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3281952fc18eSTheodore Ts'o 				count++;
3282952fc18eSTheodore Ts'o 			}
3283952fc18eSTheodore Ts'o 		if (i != grp)
3284952fc18eSTheodore Ts'o 			continue;
3285952fc18eSTheodore Ts'o 		s = 0;
3286952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3287952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3288952fc18eSTheodore Ts'o 			count++;
3289952fc18eSTheodore Ts'o 		}
3290c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3291c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3292c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3293c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3294c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3295952fc18eSTheodore Ts'o 		}
3296c48ae41bSTheodore Ts'o 		count += j;
3297c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3298c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3299952fc18eSTheodore Ts'o 	}
3300952fc18eSTheodore Ts'o 	if (!count)
3301952fc18eSTheodore Ts'o 		return 0;
3302952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3303952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3304952fc18eSTheodore Ts'o }
3305952fc18eSTheodore Ts'o 
3306952fc18eSTheodore Ts'o /*
3307952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3308952fc18eSTheodore Ts'o  */
3309952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3310952fc18eSTheodore Ts'o {
3311952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3312952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
33133c816dedSEric Whitney 	struct inode *j_inode;
33143c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3315952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3316952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
33174fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3318952fc18eSTheodore Ts'o 
3319952fc18eSTheodore Ts'o 	if (!buf)
3320952fc18eSTheodore Ts'o 		return -ENOMEM;
3321952fc18eSTheodore Ts'o 
3322952fc18eSTheodore Ts'o 	/*
3323952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3324952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3325952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3326952fc18eSTheodore Ts'o 	 */
3327952fc18eSTheodore Ts'o 
3328952fc18eSTheodore Ts'o 	/*
3329952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3330952fc18eSTheodore Ts'o 	 */
3331952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3332952fc18eSTheodore Ts'o 
3333952fc18eSTheodore Ts'o 	/*
3334952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3335952fc18eSTheodore Ts'o 	 */
3336952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3337952fc18eSTheodore Ts'o 		int blks;
3338952fc18eSTheodore Ts'o 
3339952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3340952fc18eSTheodore Ts'o 		overhead += blks;
3341952fc18eSTheodore Ts'o 		if (blks)
3342952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3343952fc18eSTheodore Ts'o 		cond_resched();
3344952fc18eSTheodore Ts'o 	}
33453c816dedSEric Whitney 
33463c816dedSEric Whitney 	/*
33473c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
33483c816dedSEric Whitney 	 * loaded or not
33493c816dedSEric Whitney 	 */
3350b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3351810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
33523c816dedSEric Whitney 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal) {
33533c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
33543c816dedSEric Whitney 		if (j_inode) {
33553c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
33563c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
33573c816dedSEric Whitney 			iput(j_inode);
33583c816dedSEric Whitney 		} else {
33593c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
33603c816dedSEric Whitney 		}
33613c816dedSEric Whitney 	}
3362952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3363952fc18eSTheodore Ts'o 	smp_wmb();
3364952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3365952fc18eSTheodore Ts'o 	return 0;
3366952fc18eSTheodore Ts'o }
3367952fc18eSTheodore Ts'o 
3368b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
336927dd4385SLukas Czerner {
337027dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3371b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
337227dd4385SLukas Czerner 
337327dd4385SLukas Czerner 	/*
337430fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
337530fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
337630fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
337730fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
337830fac0f7SJan Kara 	 */
3379e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3380b5799018STheodore Ts'o 		return;
338130fac0f7SJan Kara 	/*
338227dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
338327dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
338427dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3385556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
338627dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
338727dd4385SLukas Czerner 	 * very rare.
338827dd4385SLukas Czerner 	 */
3389b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3390b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
339127dd4385SLukas Czerner 
339227dd4385SLukas Czerner 	do_div(resv_clusters, 50);
339327dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
339427dd4385SLukas Czerner 
3395b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
339627dd4385SLukas Czerner }
339727dd4385SLukas Czerner 
3398617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3399ac27a0ecSDave Kleikamp {
34005e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3401d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3402ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3403617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
34045aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3405617ba13bSMingming Cao 	ext4_fsblk_t block;
3406617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
340770bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3408ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3409ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3410ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3411ac27a0ecSDave Kleikamp 	struct inode *root;
34120390131bSFrank Mayhar 	const char *descr;
3413dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3414281b5995STheodore Ts'o 	int blocksize, clustersize;
34154ec11028STheodore Ts'o 	unsigned int db_count;
34164ec11028STheodore Ts'o 	unsigned int i;
3417281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3418bd81d8eeSLaurent Vivier 	__u64 blocks_count;
341907aa2ea1SLukas Czerner 	int err = 0;
3420b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3421bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3422ac27a0ecSDave Kleikamp 
34235aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
34245aee0f8aSTheodore Ts'o 		goto out_free_base;
3425705895b6SPekka Enberg 
3426aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3427705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3428705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
34295aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
34305aee0f8aSTheodore Ts'o 		goto out_free_base;
34315aee0f8aSTheodore Ts'o 
3432ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
34332c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3434240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3435d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3436f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3437f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3438f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3439ac27a0ecSDave Kleikamp 
34409f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3441ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
34429f6200bbSTheodore Ts'o 
344307aa2ea1SLukas Czerner 	/* -EINVAL is default */
3444dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3445617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3446ac27a0ecSDave Kleikamp 	if (!blocksize) {
3447b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3448ac27a0ecSDave Kleikamp 		goto out_fail;
3449ac27a0ecSDave Kleikamp 	}
3450ac27a0ecSDave Kleikamp 
3451ac27a0ecSDave Kleikamp 	/*
3452617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3453ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3454ac27a0ecSDave Kleikamp 	 */
3455617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
345670bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
345770bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3458ac27a0ecSDave Kleikamp 	} else {
345970bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3460ac27a0ecSDave Kleikamp 	}
3461ac27a0ecSDave Kleikamp 
3462a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3463b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3464ac27a0ecSDave Kleikamp 		goto out_fail;
3465ac27a0ecSDave Kleikamp 	}
3466ac27a0ecSDave Kleikamp 	/*
3467ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3468617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3469ac27a0ecSDave Kleikamp 	 */
34702716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3471ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3472ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3473617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3474617ba13bSMingming Cao 		goto cantfind_ext4;
3475afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3476ac27a0ecSDave Kleikamp 
3477feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3478e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3479e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3480363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3481feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3482feb0ab32SDarrick J. Wong 
3483d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3484d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3485d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3486d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3487d25425f8SDarrick J. Wong 		silent = 1;
3488d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3489d25425f8SDarrick J. Wong 	}
3490d25425f8SDarrick J. Wong 
34910441984aSDarrick J. Wong 	/* Load the checksum driver */
3492dec214d0STahsin Erdogan 	if (ext4_has_feature_metadata_csum(sb) ||
3493dec214d0STahsin Erdogan 	    ext4_has_feature_ea_inode(sb)) {
34940441984aSDarrick J. Wong 		sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
34950441984aSDarrick J. Wong 		if (IS_ERR(sbi->s_chksum_driver)) {
34960441984aSDarrick J. Wong 			ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
34970441984aSDarrick J. Wong 			ret = PTR_ERR(sbi->s_chksum_driver);
34980441984aSDarrick J. Wong 			sbi->s_chksum_driver = NULL;
34990441984aSDarrick J. Wong 			goto failed_mount;
35000441984aSDarrick J. Wong 		}
35010441984aSDarrick J. Wong 	}
35020441984aSDarrick J. Wong 
3503a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3504a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3505a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3506a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3507a9c47317SDarrick J. Wong 		silent = 1;
35086a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3509a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3510a9c47317SDarrick J. Wong 	}
3511a9c47317SDarrick J. Wong 
3512a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3513e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
35148c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3515dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3516a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3517a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3518a9c47317SDarrick J. Wong 
3519ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3520ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3521fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3522617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3523fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
352487f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3525fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3526617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3527fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3528ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3529fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
353003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3531fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
35322e7842b8SHugh Dickins #endif
353398c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
353498c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
353598c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
353698c1a759SDarrick J. Wong 
3537617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3538fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3539617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3540fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3541617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3542fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3543ac27a0ecSDave Kleikamp 
3544617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3545fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3546bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3547fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3548bb4f397aSAneesh Kumar K.V 	else
3549fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
355045f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3551fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
35528b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3553fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3554ac27a0ecSDave Kleikamp 
355508cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
355608cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
355730773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
355830773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
355930773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3560ac27a0ecSDave Kleikamp 
35618b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3562fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3563ac27a0ecSDave Kleikamp 
35641e2462f9SMingming Cao 	/*
3565dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3566dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3567dd919b98SAneesh Kumar K.V 	 */
3568bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
35698b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3570fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3571dd919b98SAneesh Kumar K.V 
357251ce6511SLukas Czerner 	/*
357351ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
357451ce6511SLukas Czerner 	 * no mount option specified.
357551ce6511SLukas Czerner 	 */
357651ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
357751ce6511SLukas Czerner 
35785aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
35795aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
35805aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
35815aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
35825aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
35835aee0f8aSTheodore Ts'o 			goto failed_mount;
35845aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
35855aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
35868b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
35878b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
35885aee0f8aSTheodore Ts'o 				 s_mount_opts);
35895aee0f8aSTheodore Ts'o 		}
35905aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
35918b67f04aSTheodore Ts'o 	}
35925a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3593b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3594661aa520SEric Sandeen 			   &journal_ioprio, 0))
3595ac27a0ecSDave Kleikamp 		goto failed_mount;
3596ac27a0ecSDave Kleikamp 
359756889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
359856889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
359956889787STheodore Ts'o 			    "with data=journal disables delayed "
360056889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
360156889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
360256889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
360356889787STheodore Ts'o 				 "both data=journal and delalloc");
360456889787STheodore Ts'o 			goto failed_mount;
360556889787STheodore Ts'o 		}
360656889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
360756889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
36086ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
360956889787STheodore Ts'o 			goto failed_mount;
361056889787STheodore Ts'o 		}
3611923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3612923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3613923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3614923ae0ffSRoss Zwisler 			goto failed_mount;
3615923ae0ffSRoss Zwisler 		}
361673b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
361773b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
361873b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
361973b92a2aSSergey Karamov 				 "instead of data journaling mode");
362073b92a2aSSergey Karamov 		}
362156889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
362256889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3623001e4a87STejun Heo 	} else {
3624001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
362556889787STheodore Ts'o 	}
362656889787STheodore Ts'o 
3627ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3628482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3629ac27a0ecSDave Kleikamp 
3630617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3631e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
3632e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
3633e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
3634b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3635b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3636b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3637469108ffSTheodore Tso 
3638ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3639ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3640e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
3641ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3642ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3643ed3654ebSTheodore Ts'o 			goto failed_mount;
3644ed3654ebSTheodore Ts'o 		}
3645dec214d0STahsin Erdogan 
3646dec214d0STahsin Erdogan 		/*
3647dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
3648dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
3649dec214d0STahsin Erdogan 		 */
3650dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
3651dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
3652dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
3653dec214d0STahsin Erdogan 			goto failed_mount;
3654dec214d0STahsin Erdogan 		}
3655ed3654ebSTheodore Ts'o 	}
3656ed3654ebSTheodore Ts'o 
36572035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
36582035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
36592035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
36602035e776STheodore Ts'o 				 "using the ext4 subsystem");
36612035e776STheodore Ts'o 		else {
36622035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
36632035e776STheodore Ts'o 				 "to feature incompatibilities");
36642035e776STheodore Ts'o 			goto failed_mount;
36652035e776STheodore Ts'o 		}
36662035e776STheodore Ts'o 	}
36672035e776STheodore Ts'o 
36682035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
36692035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
36702035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
36712035e776STheodore Ts'o 				 "using the ext4 subsystem");
36722035e776STheodore Ts'o 		else {
36732035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
36742035e776STheodore Ts'o 				 "to feature incompatibilities");
36752035e776STheodore Ts'o 			goto failed_mount;
36762035e776STheodore Ts'o 		}
36772035e776STheodore Ts'o 	}
36782035e776STheodore Ts'o 
3679469108ffSTheodore Tso 	/*
3680ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3681ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3682ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3683ac27a0ecSDave Kleikamp 	 */
3684bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
3685ac27a0ecSDave Kleikamp 		goto failed_mount;
3686a13fb1a4SEric Sandeen 
3687261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3688617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3689617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3690b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
36918cdf3372STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
36928cdf3372STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
36938cdf3372STheodore Ts'o 		goto failed_mount;
36948cdf3372STheodore Ts'o 	}
36958cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
36968cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
36978cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
36988cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
36998cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
3700ac27a0ecSDave Kleikamp 		goto failed_mount;
3701ac27a0ecSDave Kleikamp 	}
3702ac27a0ecSDave Kleikamp 
37035b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
37045b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
37055b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
37065b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
37075b9554dcSTheodore Ts'o 		goto failed_mount;
37085b9554dcSTheodore Ts'o 	}
37095b9554dcSTheodore Ts'o 
3710923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
371187eefeb4SToshi Kani 		err = bdev_dax_supported(sb, blocksize);
371287eefeb4SToshi Kani 		if (err)
3713923ae0ffSRoss Zwisler 			goto failed_mount;
3714923ae0ffSRoss Zwisler 	}
3715923ae0ffSRoss Zwisler 
3716e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
37176ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
37186ddb2447STheodore Ts'o 			 es->s_encryption_level);
37196ddb2447STheodore Ts'o 		goto failed_mount;
37206ddb2447STheodore Ts'o 	}
37216ddb2447STheodore Ts'o 
3722ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3723ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3724ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3725b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3726ce40733cSAneesh Kumar K.V 					blocksize);
3727ac27a0ecSDave Kleikamp 			goto failed_mount;
3728ac27a0ecSDave Kleikamp 		}
3729ac27a0ecSDave Kleikamp 
3730ac27a0ecSDave Kleikamp 		brelse(bh);
373170bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
373270bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3733a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
3734ac27a0ecSDave Kleikamp 		if (!bh) {
3735b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3736b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3737ac27a0ecSDave Kleikamp 			goto failed_mount;
3738ac27a0ecSDave Kleikamp 		}
37392716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
3740ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3741617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3742b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3743b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3744ac27a0ecSDave Kleikamp 			goto failed_mount;
3745ac27a0ecSDave Kleikamp 		}
3746ac27a0ecSDave Kleikamp 	}
3747ac27a0ecSDave Kleikamp 
3748e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
3749f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3750f287a1a5STheodore Ts'o 						      has_huge_files);
3751f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3752ac27a0ecSDave Kleikamp 
3753617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3754617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3755617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3756ac27a0ecSDave Kleikamp 	} else {
3757ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3758ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3759617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
37601330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3761ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3762b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3763b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3764ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3765ac27a0ecSDave Kleikamp 			goto failed_mount;
3766ac27a0ecSDave Kleikamp 		}
3767ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3768ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3769ac27a0ecSDave Kleikamp 	}
37700b8e58a1SAndreas Dilger 
37710d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
3772e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
37738fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
37740d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3775d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3776b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3777b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
37780d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
37790d1ee42fSAlexandre Ratchov 			goto failed_mount;
37800d1ee42fSAlexandre Ratchov 		}
37810d1ee42fSAlexandre Ratchov 	} else
37820d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
37830b8e58a1SAndreas Dilger 
3784ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3785ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
37860b8e58a1SAndreas Dilger 
3787617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3788ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3789617ba13bSMingming Cao 		goto cantfind_ext4;
3790cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
3791cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
3792cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
3793cd6bb35bSTheodore Ts'o 			 sbi->s_blocks_per_group);
3794cd6bb35bSTheodore Ts'o 		goto failed_mount;
3795cd6bb35bSTheodore Ts'o 	}
3796ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3797ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
37980d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3799ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3800ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3801e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3802e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
38030b8e58a1SAndreas Dilger 
3804ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3805ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3806ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3807e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
3808f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
3809f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
3810f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3811f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3812f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
3813bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
381423301410STheodore Ts'o 				es->s_flags |=
381523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3816f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3817f99b2589STheodore Ts'o #else
3818bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
381923301410STheodore Ts'o 				es->s_flags |=
382023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3821f99b2589STheodore Ts'o #endif
3822f99b2589STheodore Ts'o 		}
382323301410STheodore Ts'o 	}
3824ac27a0ecSDave Kleikamp 
3825281b5995STheodore Ts'o 	/* Handle clustersize */
3826281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3827e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
3828281b5995STheodore Ts'o 	if (has_bigalloc) {
3829281b5995STheodore Ts'o 		if (clustersize < blocksize) {
3830281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3831281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
3832281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
3833281b5995STheodore Ts'o 			goto failed_mount;
3834281b5995STheodore Ts'o 		}
38358cdf3372STheodore Ts'o 		if (le32_to_cpu(es->s_log_cluster_size) >
38368cdf3372STheodore Ts'o 		    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
38378cdf3372STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
38388cdf3372STheodore Ts'o 				 "Invalid log cluster size: %u",
38398cdf3372STheodore Ts'o 				 le32_to_cpu(es->s_log_cluster_size));
38408cdf3372STheodore Ts'o 			goto failed_mount;
38418cdf3372STheodore Ts'o 		}
3842281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3843281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
3844281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
3845281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
3846281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
3847281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3848281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
3849281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3850281b5995STheodore Ts'o 			goto failed_mount;
3851281b5995STheodore Ts'o 		}
3852281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
3853281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3854281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3855281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
3856281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
3857281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3858281b5995STheodore Ts'o 			goto failed_mount;
3859281b5995STheodore Ts'o 		}
3860281b5995STheodore Ts'o 	} else {
3861281b5995STheodore Ts'o 		if (clustersize != blocksize) {
3862281b5995STheodore Ts'o 			ext4_warning(sb, "fragment/cluster size (%d) != "
3863281b5995STheodore Ts'o 				     "block size (%d)", clustersize,
3864281b5995STheodore Ts'o 				     blocksize);
3865281b5995STheodore Ts'o 			clustersize = blocksize;
3866281b5995STheodore Ts'o 		}
3867ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
3868b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3869b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
3870ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
3871ac27a0ecSDave Kleikamp 			goto failed_mount;
3872ac27a0ecSDave Kleikamp 		}
3873281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3874281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
3875281b5995STheodore Ts'o 	}
3876281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
3877281b5995STheodore Ts'o 
3878960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
3879960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
3880960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
3881960fd856STheodore Ts'o 
3882bf43d84bSEric Sandeen 	/*
3883bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3884bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3885bf43d84bSEric Sandeen 	 */
38865a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
388730ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
38885a9ae68aSDarrick J. Wong 	if (err) {
3889b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3890bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3891ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
389290c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
3893ac27a0ecSDave Kleikamp 		goto failed_mount;
3894ac27a0ecSDave Kleikamp 	}
3895ac27a0ecSDave Kleikamp 
3896617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3897617ba13bSMingming Cao 		goto cantfind_ext4;
3898e7c95593SEric Sandeen 
38990f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
39000f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
39010f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3902b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3903b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
39040f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
39050f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
39060f2ddca6SFrom: Thiemo Nagel 	}
39070f2ddca6SFrom: Thiemo Nagel 
39084ec11028STheodore Ts'o 	/*
39094ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
39104ec11028STheodore Ts'o 	 * of the filesystem.
39114ec11028STheodore Ts'o 	 */
39124ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3913b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
3914b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3915e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
39164ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3917e7c95593SEric Sandeen 		goto failed_mount;
3918e7c95593SEric Sandeen 	}
3919bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3920bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3921bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3922bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
39234ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3924b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
39254ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3926b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
39274ec11028STheodore Ts'o 		       ext4_blocks_count(es),
39284ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
39294ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
39304ec11028STheodore Ts'o 		goto failed_mount;
39314ec11028STheodore Ts'o 	}
3932bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3933fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3934fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3935617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3936617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
39373a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
39382ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
39393a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
39403a4b77cdSEryu Guan 				 "first meta block group too large: %u "
39413a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
39423a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
39433a4b77cdSEryu Guan 			goto failed_mount;
39443a4b77cdSEryu Guan 		}
39453a4b77cdSEryu Guan 	}
3946a7c3e901SMichal Hocko 	sbi->s_group_desc = kvmalloc(db_count *
3947f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
3948ac27a0ecSDave Kleikamp 					  GFP_KERNEL);
3949ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3950b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
39512cde417dSTheodore Ts'o 		ret = -ENOMEM;
3952ac27a0ecSDave Kleikamp 		goto failed_mount;
3953ac27a0ecSDave Kleikamp 	}
3954ac27a0ecSDave Kleikamp 
3955705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3956ac27a0ecSDave Kleikamp 
395785c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
395885c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
395985c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
396085c8f176SAndrew Perepechko 		sb_breadahead(sb, block);
396185c8f176SAndrew Perepechko 	}
396285c8f176SAndrew Perepechko 
3963ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
396470bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3965a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
3966ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3967b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3968b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3969ac27a0ecSDave Kleikamp 			db_count = i;
3970ac27a0ecSDave Kleikamp 			goto failed_mount2;
3971ac27a0ecSDave Kleikamp 		}
3972ac27a0ecSDave Kleikamp 	}
3973829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
3974b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
39756a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
3976f9ae9cf5STheodore Ts'o 		goto failed_mount2;
3977ac27a0ecSDave Kleikamp 	}
3978772cb7c8SJose R. Santos 
3979f9ae9cf5STheodore Ts'o 	sbi->s_gdb_count = db_count;
3980ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3981ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3982ac27a0ecSDave Kleikamp 
398304ecddb7SJan Mrazek 	setup_timer(&sbi->s_err_report, print_daily_error_info,
398404ecddb7SJan Mrazek 		(unsigned long) sb);
398504496411STao Ma 
3986a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
3987eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
3988ce7e010aSTheodore Ts'o 		goto failed_mount3;
3989ce7e010aSTheodore Ts'o 
3990c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
399167a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
3992c9de560dSAlex Tomas 
3993f9ae9cf5STheodore Ts'o 	/*
3994f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
3995f9ae9cf5STheodore Ts'o 	 */
3996f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
3997617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3998617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3999a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4000ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4001617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4002e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
40031fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4004262b4662SJan Kara 	else
4005262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4006689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4007ac27a0ecSDave Kleikamp #endif
400885787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4009f2fa2ffcSAneesh Kumar K.V 
4010ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
40113b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4012ac27a0ecSDave Kleikamp 
4013ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4014ac27a0ecSDave Kleikamp 
4015ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4016e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4017ac27a0ecSDave Kleikamp 
4018bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4019c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
402050460fe8SDarrick J. Wong 			goto failed_mount3a;
4021c5e06d10SJohann Lombardi 
4022ac27a0ecSDave Kleikamp 	/*
4023ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4024ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4025ac27a0ecSDave Kleikamp 	 */
4026e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
40274753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
40284753d8a2STheodore Ts'o 		if (err)
402950460fe8SDarrick J. Wong 			goto failed_mount3a;
4030bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4031e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4032b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4033b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4034744692dcSJiaying Zhang 		goto failed_mount_wq;
4035ac27a0ecSDave Kleikamp 	} else {
40361e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
40371e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
40381e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
40391e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
40401e381f60SDmitry Monakhov 			goto failed_mount_wq;
40411e381f60SDmitry Monakhov 		}
40421e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
40431e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
40441e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
40451e381f60SDmitry Monakhov 			goto failed_mount_wq;
40461e381f60SDmitry Monakhov 		}
40471e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
40481e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
40491e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
40501e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
40511e381f60SDmitry Monakhov 			goto failed_mount_wq;
40521e381f60SDmitry Monakhov 		}
40531e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
40541e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
40551e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
40561e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
40571e381f60SDmitry Monakhov 			goto failed_mount_wq;
40581e381f60SDmitry Monakhov 		}
40591e381f60SDmitry Monakhov 		sbi->s_def_mount_opt &= EXT4_MOUNT_JOURNAL_CHECKSUM;
40601e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4061fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
40620390131bSFrank Mayhar 		sbi->s_journal = NULL;
40630390131bSFrank Mayhar 		needs_recovery = 0;
40640390131bSFrank Mayhar 		goto no_journal;
4065ac27a0ecSDave Kleikamp 	}
4066ac27a0ecSDave Kleikamp 
4067e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4068eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4069eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4070b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4071744692dcSJiaying Zhang 		goto failed_mount_wq;
4072eb40a09cSJose R. Santos 	}
4073eb40a09cSJose R. Santos 
407425ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
407525ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
407625ed6e8aSDarrick J. Wong 			 "feature set");
407725ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4078d4da6c9cSLinus Torvalds 	}
4079818d276cSGirish Shilamkar 
4080ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4081ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4082ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4083ac27a0ecSDave Kleikamp 	case 0:
4084ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
408563f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
408663f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
408763f57933SAndrew Morton 		 */
4088dab291afSMingming Cao 		if (jbd2_journal_check_available_features
4089dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
4090fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
4091ac27a0ecSDave Kleikamp 		else
4092fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
4093ac27a0ecSDave Kleikamp 		break;
4094ac27a0ecSDave Kleikamp 
4095617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4096617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4097dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4098dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4099b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4100b31e1552SEric Sandeen 			       "requested data journaling mode");
4101744692dcSJiaying Zhang 			goto failed_mount_wq;
4102ac27a0ecSDave Kleikamp 		}
4103ac27a0ecSDave Kleikamp 	default:
4104ac27a0ecSDave Kleikamp 		break;
4105ac27a0ecSDave Kleikamp 	}
4106ab04df78SJan Kara 
4107ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4108ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4109ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4110ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4111ab04df78SJan Kara 		goto failed_mount_wq;
4112ab04df78SJan Kara 	}
4113ab04df78SJan Kara 
4114b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4115ac27a0ecSDave Kleikamp 
411618aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
411718aadd47SBobi Jam 
4118ce7e010aSTheodore Ts'o no_journal:
4119cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
412047387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
412147387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4122cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4123cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
41249c191f70ST Makphaibulchoke 			goto failed_mount_wq;
41259c191f70ST Makphaibulchoke 		}
41269c191f70ST Makphaibulchoke 
4127dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4128dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4129dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4130dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4131dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4132dec214d0STahsin Erdogan 				goto failed_mount_wq;
4133dec214d0STahsin Erdogan 			}
4134dec214d0STahsin Erdogan 		}
4135cdb7ee4cSTahsin Erdogan 	}
4136dec214d0STahsin Erdogan 
4137e2b911c5SDarrick J. Wong 	if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
413809cbfeafSKirill A. Shutemov 	    (blocksize != PAGE_SIZE)) {
41391cb767cdSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
41401cb767cdSTheodore Ts'o 			 "Unsupported blocksize for fs encryption");
41411cb767cdSTheodore Ts'o 		goto failed_mount_wq;
41421cb767cdSTheodore Ts'o 	}
41431cb767cdSTheodore Ts'o 
4144bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4145e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4146e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
41476ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
41486ddb2447STheodore Ts'o 	}
41496ddb2447STheodore Ts'o 
4150fd89d5f2STejun Heo 	/*
4151952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4152952fc18eSTheodore Ts'o 	 * superblock if present.
4153952fc18eSTheodore Ts'o 	 */
4154952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4155952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4156952fc18eSTheodore Ts'o 	else {
415707aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
415807aa2ea1SLukas Czerner 		if (err)
4159952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4160952fc18eSTheodore Ts'o 	}
4161952fc18eSTheodore Ts'o 
4162952fc18eSTheodore Ts'o 	/*
4163fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4164fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4165fd89d5f2STejun Heo 	 */
41662e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
41672e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
41682e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
41692e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
417007aa2ea1SLukas Czerner 		ret = -ENOMEM;
41712e8fa54eSJan Kara 		goto failed_mount4;
41722e8fa54eSJan Kara 	}
41732e8fa54eSJan Kara 
4174ac27a0ecSDave Kleikamp 	/*
4175dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4176ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4177ac27a0ecSDave Kleikamp 	 */
4178ac27a0ecSDave Kleikamp 
41791d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
41801d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4181b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
41821d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
418332a9bb57SManish Katiyar 		root = NULL;
4184ac27a0ecSDave Kleikamp 		goto failed_mount4;
4185ac27a0ecSDave Kleikamp 	}
4186ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4187b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
418894bf608aSAl Viro 		iput(root);
4189ac27a0ecSDave Kleikamp 		goto failed_mount4;
4190ac27a0ecSDave Kleikamp 	}
419148fde701SAl Viro 	sb->s_root = d_make_root(root);
41921d1fe1eeSDavid Howells 	if (!sb->s_root) {
4193b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
41941d1fe1eeSDavid Howells 		ret = -ENOMEM;
41951d1fe1eeSDavid Howells 		goto failed_mount4;
41961d1fe1eeSDavid Howells 	}
4197ac27a0ecSDave Kleikamp 
4198bc98a42cSDavid Howells 	if (ext4_setup_super(sb, es, sb_rdonly(sb)))
41997e84b621SEric Sandeen 		sb->s_flags |= MS_RDONLY;
4200ef7f3835SKalpak Shah 
4201ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
4202670e9875STheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE &&
4203670e9875STheodore Ts'o 	    sbi->s_want_extra_isize == 0) {
4204ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4205ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
4206e2b911c5SDarrick J. Wong 		if (ext4_has_feature_extra_isize(sb)) {
4207ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4208ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
4209ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4210ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
4211ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4212ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
4213ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4214ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
4215ef7f3835SKalpak Shah 		}
4216ef7f3835SKalpak Shah 	}
4217ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
4218ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
4219ef7f3835SKalpak Shah 							sbi->s_inode_size) {
4220ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4221ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
4222b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
4223b31e1552SEric Sandeen 			 "available");
4224ef7f3835SKalpak Shah 	}
4225ef7f3835SKalpak Shah 
4226b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
422727dd4385SLukas Czerner 
42286fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
42296fd058f7STheodore Ts'o 	if (err) {
4230b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4231fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4232f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4233f9ae9cf5STheodore Ts'o 	}
4234f9ae9cf5STheodore Ts'o 
4235f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4236f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4237f9ae9cf5STheodore Ts'o 	if (err) {
4238f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4239f9ae9cf5STheodore Ts'o 			 err);
4240dcf2d804STao Ma 		goto failed_mount5;
4241c2774d84SAneesh Kumar K.V 	}
4242c2774d84SAneesh Kumar K.V 
4243d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4244d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4245d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
4246908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4247908c7f19STejun Heo 				  GFP_KERNEL);
4248d5e03cbbSTheodore Ts'o 	if (!err) {
4249d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4250d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
4251908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4252908c7f19STejun Heo 					  GFP_KERNEL);
4253d5e03cbbSTheodore Ts'o 	}
4254d5e03cbbSTheodore Ts'o 	if (!err)
4255d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4256908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4257d5e03cbbSTheodore Ts'o 	if (!err)
4258908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4259908c7f19STejun Heo 					  GFP_KERNEL);
4260c8585c6fSDaeho Jeong 	if (!err)
4261c8585c6fSDaeho Jeong 		err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem);
4262c8585c6fSDaeho Jeong 
4263d5e03cbbSTheodore Ts'o 	if (err) {
4264d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4265d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4266d5e03cbbSTheodore Ts'o 	}
4267d5e03cbbSTheodore Ts'o 
4268e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4269d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4270d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4271d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4272d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4273d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4274d5e03cbbSTheodore Ts'o 		}
4275d5e03cbbSTheodore Ts'o 
4276bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4277bfff6873SLukas Czerner 	if (err)
4278dcf2d804STao Ma 		goto failed_mount6;
4279bfff6873SLukas Czerner 
4280b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4281dcf2d804STao Ma 	if (err)
4282dcf2d804STao Ma 		goto failed_mount7;
42833197ebdbSTheodore Ts'o 
42849b2ff357SJan Kara #ifdef CONFIG_QUOTA
42859b2ff357SJan Kara 	/* Enable quota usage during mount. */
4286bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
42879b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
42889b2ff357SJan Kara 		if (err)
42899b2ff357SJan Kara 			goto failed_mount8;
42909b2ff357SJan Kara 	}
42919b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
42929b2ff357SJan Kara 
4293617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4294617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4295617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
42960390131bSFrank Mayhar 	if (needs_recovery) {
4297b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4298617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
42990390131bSFrank Mayhar 	}
43000390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
43010390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
43020390131bSFrank Mayhar 			descr = " journalled data mode";
43030390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
43040390131bSFrank Mayhar 			descr = " ordered data mode";
43050390131bSFrank Mayhar 		else
43060390131bSFrank Mayhar 			descr = " writeback data mode";
43070390131bSFrank Mayhar 	} else
43080390131bSFrank Mayhar 		descr = "out journal";
43090390131bSFrank Mayhar 
431079add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
431179add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
431279add3a3SLukas Czerner 		if (!blk_queue_discard(q))
431379add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
431479add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
431579add3a3SLukas Czerner 				 "the device does not support discard");
431679add3a3SLukas Czerner 	}
431779add3a3SLukas Czerner 
4318e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4319d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
43205aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
43215aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
43225aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
43238b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4324ac27a0ecSDave Kleikamp 
432566e61a9eSTheodore Ts'o 	if (es->s_error_count)
432666e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4327ac27a0ecSDave Kleikamp 
4328efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4329efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4330efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4331efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4332efbed4dcSTheodore Ts'o 
4333d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4334ac27a0ecSDave Kleikamp 	return 0;
4335ac27a0ecSDave Kleikamp 
4336617ba13bSMingming Cao cantfind_ext4:
4337ac27a0ecSDave Kleikamp 	if (!silent)
4338b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4339ac27a0ecSDave Kleikamp 	goto failed_mount;
4340ac27a0ecSDave Kleikamp 
434172ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
434272ba7450STheodore Ts'o failed_mount8:
4343ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
434472ba7450STheodore Ts'o #endif
4345dcf2d804STao Ma failed_mount7:
4346dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4347dcf2d804STao Ma failed_mount6:
4348f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
4349d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
4350b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
4351d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4352d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4353d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4354d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
435500764937SAzat Khuzhin failed_mount5:
4356f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4357f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4358f9ae9cf5STheodore Ts'o failed_mount4a:
435994bf608aSAl Viro 	dput(sb->s_root);
436032a9bb57SManish Katiyar 	sb->s_root = NULL;
436194bf608aSAl Viro failed_mount4:
4362b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
43632e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
43642e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
43654c0425ffSMingming Cao failed_mount_wq:
4366dec214d0STahsin Erdogan 	if (sbi->s_ea_inode_cache) {
4367dec214d0STahsin Erdogan 		ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4368dec214d0STahsin Erdogan 		sbi->s_ea_inode_cache = NULL;
4369dec214d0STahsin Erdogan 	}
437047387409STahsin Erdogan 	if (sbi->s_ea_block_cache) {
437147387409STahsin Erdogan 		ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
437247387409STahsin Erdogan 		sbi->s_ea_block_cache = NULL;
437382939d79SJan Kara 	}
43740390131bSFrank Mayhar 	if (sbi->s_journal) {
4375dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
437647b4a50bSJan Kara 		sbi->s_journal = NULL;
43770390131bSFrank Mayhar 	}
437850460fe8SDarrick J. Wong failed_mount3a:
4379d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4380eb68d0e2SZheng Liu failed_mount3:
43819105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4382c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4383c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4384ac27a0ecSDave Kleikamp failed_mount2:
4385ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4386ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4387b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4388ac27a0ecSDave Kleikamp failed_mount:
43890441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
43900441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4391ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4392a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
4393ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4394ac27a0ecSDave Kleikamp #endif
4395617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4396ac27a0ecSDave Kleikamp 	brelse(bh);
4397ac27a0ecSDave Kleikamp out_fail:
4398ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4399f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
44005aee0f8aSTheodore Ts'o out_free_base:
4401ac27a0ecSDave Kleikamp 	kfree(sbi);
4402d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
44035e405595SDan Williams 	fs_put_dax(dax_dev);
440407aa2ea1SLukas Czerner 	return err ? err : ret;
4405ac27a0ecSDave Kleikamp }
4406ac27a0ecSDave Kleikamp 
4407ac27a0ecSDave Kleikamp /*
4408ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4409ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4410ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4411ac27a0ecSDave Kleikamp  */
4412617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4413ac27a0ecSDave Kleikamp {
4414617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4415ac27a0ecSDave Kleikamp 
4416ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
441730773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
441830773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4419ac27a0ecSDave Kleikamp 
4420a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4421ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4422dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4423ac27a0ecSDave Kleikamp 	else
4424dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
44255bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
44265bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
44275bf5683aSHidehiro Kawai 	else
44285bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4429a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4430ac27a0ecSDave Kleikamp }
4431ac27a0ecSDave Kleikamp 
4432c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4433ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4434ac27a0ecSDave Kleikamp {
4435ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4436ac27a0ecSDave Kleikamp 
4437c6cb7e77SEric Whitney 	/*
4438c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4439c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4440c6cb7e77SEric Whitney 	 * will try to delete it.
4441c6cb7e77SEric Whitney 	 */
44421d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
44431d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4444b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4445ac27a0ecSDave Kleikamp 		return NULL;
4446ac27a0ecSDave Kleikamp 	}
4447ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4448ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4449ac27a0ecSDave Kleikamp 		iput(journal_inode);
4450b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4451ac27a0ecSDave Kleikamp 		return NULL;
4452ac27a0ecSDave Kleikamp 	}
4453ac27a0ecSDave Kleikamp 
4454e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4455ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
44561d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4457b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4458ac27a0ecSDave Kleikamp 		iput(journal_inode);
4459ac27a0ecSDave Kleikamp 		return NULL;
4460ac27a0ecSDave Kleikamp 	}
4461c6cb7e77SEric Whitney 	return journal_inode;
4462c6cb7e77SEric Whitney }
4463c6cb7e77SEric Whitney 
4464c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4465c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4466c6cb7e77SEric Whitney {
4467c6cb7e77SEric Whitney 	struct inode *journal_inode;
4468c6cb7e77SEric Whitney 	journal_t *journal;
4469c6cb7e77SEric Whitney 
4470c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4471c6cb7e77SEric Whitney 
4472c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4473c6cb7e77SEric Whitney 	if (!journal_inode)
4474c6cb7e77SEric Whitney 		return NULL;
4475ac27a0ecSDave Kleikamp 
4476dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4477ac27a0ecSDave Kleikamp 	if (!journal) {
4478b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4479ac27a0ecSDave Kleikamp 		iput(journal_inode);
4480ac27a0ecSDave Kleikamp 		return NULL;
4481ac27a0ecSDave Kleikamp 	}
4482ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4483617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4484ac27a0ecSDave Kleikamp 	return journal;
4485ac27a0ecSDave Kleikamp }
4486ac27a0ecSDave Kleikamp 
4487617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4488ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4489ac27a0ecSDave Kleikamp {
4490ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4491ac27a0ecSDave Kleikamp 	journal_t *journal;
4492617ba13bSMingming Cao 	ext4_fsblk_t start;
4493617ba13bSMingming Cao 	ext4_fsblk_t len;
4494ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4495617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4496ac27a0ecSDave Kleikamp 	unsigned long offset;
4497617ba13bSMingming Cao 	struct ext4_super_block *es;
4498ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4499ac27a0ecSDave Kleikamp 
4500e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
45010390131bSFrank Mayhar 
4502b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4503ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4504ac27a0ecSDave Kleikamp 		return NULL;
4505ac27a0ecSDave Kleikamp 
4506ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4507e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4508ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4509b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4510b31e1552SEric Sandeen 			"blocksize too small for journal device");
4511ac27a0ecSDave Kleikamp 		goto out_bdev;
4512ac27a0ecSDave Kleikamp 	}
4513ac27a0ecSDave Kleikamp 
4514617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4515617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4516ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4517ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4518b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4519b31e1552SEric Sandeen 		       "external journal");
4520ac27a0ecSDave Kleikamp 		goto out_bdev;
4521ac27a0ecSDave Kleikamp 	}
4522ac27a0ecSDave Kleikamp 
45232716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4524617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4525ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4526617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4527b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4528b31e1552SEric Sandeen 					"bad superblock");
4529ac27a0ecSDave Kleikamp 		brelse(bh);
4530ac27a0ecSDave Kleikamp 		goto out_bdev;
4531ac27a0ecSDave Kleikamp 	}
4532ac27a0ecSDave Kleikamp 
4533df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4534df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4535df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4536df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4537df4763beSDarrick J. Wong 				       "corrupt superblock");
4538df4763beSDarrick J. Wong 		brelse(bh);
4539df4763beSDarrick J. Wong 		goto out_bdev;
4540df4763beSDarrick J. Wong 	}
4541df4763beSDarrick J. Wong 
4542617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4543b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4544ac27a0ecSDave Kleikamp 		brelse(bh);
4545ac27a0ecSDave Kleikamp 		goto out_bdev;
4546ac27a0ecSDave Kleikamp 	}
4547ac27a0ecSDave Kleikamp 
4548bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4549ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4550ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4551ac27a0ecSDave Kleikamp 
4552dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4553ac27a0ecSDave Kleikamp 					start, len, blocksize);
4554ac27a0ecSDave Kleikamp 	if (!journal) {
4555b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4556ac27a0ecSDave Kleikamp 		goto out_bdev;
4557ac27a0ecSDave Kleikamp 	}
4558ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4559dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4560ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4561ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4562b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4563ac27a0ecSDave Kleikamp 		goto out_journal;
4564ac27a0ecSDave Kleikamp 	}
4565ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4566b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4567b31e1552SEric Sandeen 					"user (unsupported) - %d",
4568ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4569ac27a0ecSDave Kleikamp 		goto out_journal;
4570ac27a0ecSDave Kleikamp 	}
4571617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4572617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4573ac27a0ecSDave Kleikamp 	return journal;
45740b8e58a1SAndreas Dilger 
4575ac27a0ecSDave Kleikamp out_journal:
4576dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4577ac27a0ecSDave Kleikamp out_bdev:
4578617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4579ac27a0ecSDave Kleikamp 	return NULL;
4580ac27a0ecSDave Kleikamp }
4581ac27a0ecSDave Kleikamp 
4582617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4583617ba13bSMingming Cao 			     struct ext4_super_block *es,
4584ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4585ac27a0ecSDave Kleikamp {
4586ac27a0ecSDave Kleikamp 	journal_t *journal;
4587ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4588ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4589ac27a0ecSDave Kleikamp 	int err = 0;
4590ac27a0ecSDave Kleikamp 	int really_read_only;
4591ac27a0ecSDave Kleikamp 
4592e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
45930390131bSFrank Mayhar 
4594ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4595ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4596b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4597b31e1552SEric Sandeen 			"numbers have changed");
4598ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4599ac27a0ecSDave Kleikamp 	} else
4600ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4601ac27a0ecSDave Kleikamp 
4602ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4603ac27a0ecSDave Kleikamp 
4604ac27a0ecSDave Kleikamp 	/*
4605ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4606ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4607ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4608ac27a0ecSDave Kleikamp 	 */
4609e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4610bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
4611b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4612b31e1552SEric Sandeen 					"required on readonly filesystem");
4613ac27a0ecSDave Kleikamp 			if (really_read_only) {
4614b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4615b31e1552SEric Sandeen 					"unavailable, cannot proceed");
4616ac27a0ecSDave Kleikamp 				return -EROFS;
4617ac27a0ecSDave Kleikamp 			}
4618b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4619b31e1552SEric Sandeen 			       "be enabled during recovery");
4620ac27a0ecSDave Kleikamp 		}
4621ac27a0ecSDave Kleikamp 	}
4622ac27a0ecSDave Kleikamp 
4623ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4624b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4625b31e1552SEric Sandeen 		       "and inode journals!");
4626ac27a0ecSDave Kleikamp 		return -EINVAL;
4627ac27a0ecSDave Kleikamp 	}
4628ac27a0ecSDave Kleikamp 
4629ac27a0ecSDave Kleikamp 	if (journal_inum) {
4630617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4631ac27a0ecSDave Kleikamp 			return -EINVAL;
4632ac27a0ecSDave Kleikamp 	} else {
4633617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4634ac27a0ecSDave Kleikamp 			return -EINVAL;
4635ac27a0ecSDave Kleikamp 	}
4636ac27a0ecSDave Kleikamp 
463790576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4638b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
46394776004fSTheodore Ts'o 
4640e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
4641dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
46421c13d5c0STheodore Ts'o 	if (!err) {
46431c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
46441c13d5c0STheodore Ts'o 		if (save)
46451c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
46461c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4647dab291afSMingming Cao 		err = jbd2_journal_load(journal);
46481c13d5c0STheodore Ts'o 		if (save)
46491c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
46501c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
46511c13d5c0STheodore Ts'o 		kfree(save);
46521c13d5c0STheodore Ts'o 	}
4653ac27a0ecSDave Kleikamp 
4654ac27a0ecSDave Kleikamp 	if (err) {
4655b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4656dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4657ac27a0ecSDave Kleikamp 		return err;
4658ac27a0ecSDave Kleikamp 	}
4659ac27a0ecSDave Kleikamp 
4660617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4661617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4662ac27a0ecSDave Kleikamp 
4663c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4664ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4665ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4666ac27a0ecSDave Kleikamp 
4667ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4668e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4669ac27a0ecSDave Kleikamp 	}
4670ac27a0ecSDave Kleikamp 
4671ac27a0ecSDave Kleikamp 	return 0;
4672ac27a0ecSDave Kleikamp }
4673ac27a0ecSDave Kleikamp 
4674e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4675ac27a0ecSDave Kleikamp {
4676e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4677617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4678c4be0c1dSTakashi Sato 	int error = 0;
4679ac27a0ecSDave Kleikamp 
4680bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4681c4be0c1dSTakashi Sato 		return error;
468271290b36STheodore Ts'o 	/*
468371290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
468471290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
468571290b36STheodore Ts'o 	 * write time when we are mounting the root file system
468671290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
468771290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
468871290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
468971290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
469071290b36STheodore Ts'o 	 * to complain and force a full file system check.
469171290b36STheodore Ts'o 	 */
469271290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4693ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4694f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4695afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4696afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4697afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4698afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4699f613dfcbSTheodore Ts'o 	else
4700f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4701f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
4702d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
470357042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
470457042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
470557042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
4706d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
47077f93cff9STheodore Ts'o 		es->s_free_inodes_count =
47087f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
47095d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
4710ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
471106db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
47121566a48aSTheodore Ts'o 	if (sync)
47134743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
47144743f839SPranay Kr. Srivastava 	if (buffer_write_io_error(sbh)) {
47154743f839SPranay Kr. Srivastava 		/*
47164743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
47174743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
47184743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
47194743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
47204743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
47214743f839SPranay Kr. Srivastava 		 * write and hope for the best.
47224743f839SPranay Kr. Srivastava 		 */
47234743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
47244743f839SPranay Kr. Srivastava 		       "superblock detected");
47254743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
47264743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
47274743f839SPranay Kr. Srivastava 	}
4728ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4729914258bfSTheodore Ts'o 	if (sync) {
47301566a48aSTheodore Ts'o 		unlock_buffer(sbh);
4731564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
473200473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
4733c4be0c1dSTakashi Sato 		if (error)
4734c4be0c1dSTakashi Sato 			return error;
4735c4be0c1dSTakashi Sato 
4736c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4737c4be0c1dSTakashi Sato 		if (error) {
4738b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4739b31e1552SEric Sandeen 			       "superblock");
4740914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4741914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4742914258bfSTheodore Ts'o 		}
4743914258bfSTheodore Ts'o 	}
4744c4be0c1dSTakashi Sato 	return error;
4745ac27a0ecSDave Kleikamp }
4746ac27a0ecSDave Kleikamp 
4747ac27a0ecSDave Kleikamp /*
4748ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4749ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4750ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4751ac27a0ecSDave Kleikamp  */
4752617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4753617ba13bSMingming Cao 					struct ext4_super_block *es)
4754ac27a0ecSDave Kleikamp {
4755617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4756ac27a0ecSDave Kleikamp 
4757e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
47580390131bSFrank Mayhar 		BUG_ON(journal != NULL);
47590390131bSFrank Mayhar 		return;
47600390131bSFrank Mayhar 	}
4761dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
47627ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
47637ffe1ea8SHidehiro Kawai 		goto out;
47647ffe1ea8SHidehiro Kawai 
4765bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
4766e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
4767e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4768ac27a0ecSDave Kleikamp 	}
47697ffe1ea8SHidehiro Kawai 
47707ffe1ea8SHidehiro Kawai out:
4771dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4772ac27a0ecSDave Kleikamp }
4773ac27a0ecSDave Kleikamp 
4774ac27a0ecSDave Kleikamp /*
4775ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4776ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4777ac27a0ecSDave Kleikamp  * main filesystem now.
4778ac27a0ecSDave Kleikamp  */
4779617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4780617ba13bSMingming Cao 				   struct ext4_super_block *es)
4781ac27a0ecSDave Kleikamp {
4782ac27a0ecSDave Kleikamp 	journal_t *journal;
4783ac27a0ecSDave Kleikamp 	int j_errno;
4784ac27a0ecSDave Kleikamp 	const char *errstr;
4785ac27a0ecSDave Kleikamp 
4786e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
47870390131bSFrank Mayhar 
4788617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4789ac27a0ecSDave Kleikamp 
4790ac27a0ecSDave Kleikamp 	/*
4791ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4792617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4793ac27a0ecSDave Kleikamp 	 */
4794ac27a0ecSDave Kleikamp 
4795dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4796ac27a0ecSDave Kleikamp 	if (j_errno) {
4797ac27a0ecSDave Kleikamp 		char nbuf[16];
4798ac27a0ecSDave Kleikamp 
4799617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
480012062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4801ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
480212062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4803ac27a0ecSDave Kleikamp 
4804617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4805617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4806e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4807ac27a0ecSDave Kleikamp 
4808dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4809d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
4810ac27a0ecSDave Kleikamp 	}
4811ac27a0ecSDave Kleikamp }
4812ac27a0ecSDave Kleikamp 
4813ac27a0ecSDave Kleikamp /*
4814ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4815ac27a0ecSDave Kleikamp  * and wait on the commit.
4816ac27a0ecSDave Kleikamp  */
4817617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4818ac27a0ecSDave Kleikamp {
4819ac27a0ecSDave Kleikamp 	journal_t *journal;
4820ac27a0ecSDave Kleikamp 
4821bc98a42cSDavid Howells 	if (sb_rdonly(sb))
4822ac27a0ecSDave Kleikamp 		return 0;
4823ac27a0ecSDave Kleikamp 
4824617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4825b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
4826ac27a0ecSDave Kleikamp }
4827ac27a0ecSDave Kleikamp 
4828617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4829ac27a0ecSDave Kleikamp {
483014ce0cb4STheodore Ts'o 	int ret = 0;
48319eddacf9SJan Kara 	tid_t target;
483206a407f1SDmitry Monakhov 	bool needs_barrier = false;
48338d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4834ac27a0ecSDave Kleikamp 
48350db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
48360db1ff22STheodore Ts'o 		return 0;
48370db1ff22STheodore Ts'o 
48389bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
48392e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
4840a1177825SJan Kara 	/*
4841a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
4842a1177825SJan Kara 	 * no dirty dquots
4843a1177825SJan Kara 	 */
4844a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
484506a407f1SDmitry Monakhov 	/*
484606a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
484706a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
484806a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
484906a407f1SDmitry Monakhov 	 */
4850bda32530STheodore Ts'o 	if (sbi->s_journal) {
485106a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
485206a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
485306a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
485406a407f1SDmitry Monakhov 			needs_barrier = true;
485506a407f1SDmitry Monakhov 
48568d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4857ac27a0ecSDave Kleikamp 			if (wait)
4858bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
4859bda32530STheodore Ts'o 							   target);
48600390131bSFrank Mayhar 		}
4861bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
4862bda32530STheodore Ts'o 		needs_barrier = true;
486306a407f1SDmitry Monakhov 	if (needs_barrier) {
486406a407f1SDmitry Monakhov 		int err;
486506a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
486606a407f1SDmitry Monakhov 		if (!ret)
486706a407f1SDmitry Monakhov 			ret = err;
486806a407f1SDmitry Monakhov 	}
486906a407f1SDmitry Monakhov 
487006a407f1SDmitry Monakhov 	return ret;
487106a407f1SDmitry Monakhov }
487206a407f1SDmitry Monakhov 
4873ac27a0ecSDave Kleikamp /*
4874ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4875ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4876be4f27d3SYongqiang Yang  *
4877be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
48788e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
48798e8ad8a5SJan Kara  * modifications.
4880ac27a0ecSDave Kleikamp  */
4881c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4882ac27a0ecSDave Kleikamp {
4883c4be0c1dSTakashi Sato 	int error = 0;
4884c4be0c1dSTakashi Sato 	journal_t *journal;
4885ac27a0ecSDave Kleikamp 
4886bc98a42cSDavid Howells 	if (sb_rdonly(sb))
48879ca92389STheodore Ts'o 		return 0;
48889ca92389STheodore Ts'o 
4889c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4890ac27a0ecSDave Kleikamp 
4891bb044576STheodore Ts'o 	if (journal) {
4892ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
4893dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
48947ffe1ea8SHidehiro Kawai 
48957ffe1ea8SHidehiro Kawai 		/*
4896bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
4897bb044576STheodore Ts'o 		 * flush the journal.
48987ffe1ea8SHidehiro Kawai 		 */
4899c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
49006b0310fbSEric Sandeen 		if (error < 0)
49016b0310fbSEric Sandeen 			goto out;
4902ac27a0ecSDave Kleikamp 
4903ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
4904e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
4905c642dc9eSEric Sandeen 	}
4906c642dc9eSEric Sandeen 
4907e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
49086b0310fbSEric Sandeen out:
4909bb044576STheodore Ts'o 	if (journal)
49108e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
4911bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
49126b0310fbSEric Sandeen 	return error;
4913ac27a0ecSDave Kleikamp }
4914ac27a0ecSDave Kleikamp 
4915ac27a0ecSDave Kleikamp /*
4916ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4917ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4918ac27a0ecSDave Kleikamp  */
4919c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4920ac27a0ecSDave Kleikamp {
4921bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
49229ca92389STheodore Ts'o 		return 0;
49239ca92389STheodore Ts'o 
4924c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
49259ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
4926e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
4927c642dc9eSEric Sandeen 	}
4928c642dc9eSEric Sandeen 
4929e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4930c4be0c1dSTakashi Sato 	return 0;
4931ac27a0ecSDave Kleikamp }
4932ac27a0ecSDave Kleikamp 
4933673c6100STheodore Ts'o /*
4934673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4935673c6100STheodore Ts'o  */
4936673c6100STheodore Ts'o struct ext4_mount_options {
4937673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4938a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
493908cefc7aSEric W. Biederman 	kuid_t s_resuid;
494008cefc7aSEric W. Biederman 	kgid_t s_resgid;
4941673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4942673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4943673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4944673c6100STheodore Ts'o 	int s_jquota_fmt;
4945a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
4946673c6100STheodore Ts'o #endif
4947673c6100STheodore Ts'o };
4948673c6100STheodore Ts'o 
4949617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4950ac27a0ecSDave Kleikamp {
4951617ba13bSMingming Cao 	struct ext4_super_block *es;
4952617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4953ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4954617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4955c79d967dSChristoph Hellwig 	int enable_quota = 0;
49568a266467STheodore Ts'o 	ext4_group_t g;
4957b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4958c5e06d10SJohann Lombardi 	int err = 0;
4959ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
496003dafb5fSChen Gang 	int i, j;
4961ac27a0ecSDave Kleikamp #endif
4962d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4963ac27a0ecSDave Kleikamp 
4964ac27a0ecSDave Kleikamp 	/* Store the original options */
4965ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4966ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4967a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4968ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4969ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4970ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
497130773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
497230773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4973ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4974ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4975a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
497603dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
497703dafb5fSChen Gang 			old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
497803dafb5fSChen Gang 							 GFP_KERNEL);
497903dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
498003dafb5fSChen Gang 				for (j = 0; j < i; j++)
498103dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
49823e36a163SWei Yongjun 				kfree(orig_data);
498303dafb5fSChen Gang 				return -ENOMEM;
498403dafb5fSChen Gang 			}
498503dafb5fSChen Gang 		} else
498603dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
4987ac27a0ecSDave Kleikamp #endif
4988b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4989b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4990ac27a0ecSDave Kleikamp 
4991661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
4992ac27a0ecSDave Kleikamp 		err = -EINVAL;
4993ac27a0ecSDave Kleikamp 		goto restore_opts;
4994ac27a0ecSDave Kleikamp 	}
4995ac27a0ecSDave Kleikamp 
49966b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
49976b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
49986b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
49992d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
50002d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5001c6d3d56dSDarrick J. Wong 	}
5002c6d3d56dSDarrick J. Wong 
50036ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
50046ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
50056ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
50066ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
50076ae6514bSPiotr Sarna 			err = -EINVAL;
50086ae6514bSPiotr Sarna 			goto restore_opts;
50096ae6514bSPiotr Sarna 		}
50106ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
50116ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
50126ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
50136ae6514bSPiotr Sarna 			err = -EINVAL;
50146ae6514bSPiotr Sarna 			goto restore_opts;
50156ae6514bSPiotr Sarna 		}
5016923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
5017923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
5018923ae0ffSRoss Zwisler 				 "both data=journal and dax");
5019923ae0ffSRoss Zwisler 			err = -EINVAL;
5020923ae0ffSRoss Zwisler 			goto restore_opts;
5021923ae0ffSRoss Zwisler 		}
5022ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5023ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5024ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5025ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5026ab04df78SJan Kara 			err = -EINVAL;
5027ab04df78SJan Kara 			goto restore_opts;
5028ab04df78SJan Kara 		}
5029923ae0ffSRoss Zwisler 	}
5030923ae0ffSRoss Zwisler 
5031cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5032cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5033cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5034cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5035cdb7ee4cSTahsin Erdogan 	}
5036cdb7ee4cSTahsin Erdogan 
5037923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
5038923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
5039923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
5040923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
50416ae6514bSPiotr Sarna 	}
50426ae6514bSPiotr Sarna 
50434ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
5044c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
5045ac27a0ecSDave Kleikamp 
5046ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
5047482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
5048ac27a0ecSDave Kleikamp 
5049ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5050ac27a0ecSDave Kleikamp 
5051b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5052617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5053b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5054b3881f74STheodore Ts'o 	}
5055ac27a0ecSDave Kleikamp 
5056a2fd66d0STheodore Ts'o 	if (*flags & MS_LAZYTIME)
5057a2fd66d0STheodore Ts'o 		sb->s_flags |= MS_LAZYTIME;
5058a2fd66d0STheodore Ts'o 
5059bc98a42cSDavid Howells 	if ((bool)(*flags & MS_RDONLY) != sb_rdonly(sb)) {
50604ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5061ac27a0ecSDave Kleikamp 			err = -EROFS;
5062ac27a0ecSDave Kleikamp 			goto restore_opts;
5063ac27a0ecSDave Kleikamp 		}
5064ac27a0ecSDave Kleikamp 
5065ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
506638c03b34STheodore Ts'o 			err = sync_filesystem(sb);
506738c03b34STheodore Ts'o 			if (err < 0)
506838c03b34STheodore Ts'o 				goto restore_opts;
50690f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
50700f0dd62fSChristoph Hellwig 			if (err < 0)
5071c79d967dSChristoph Hellwig 				goto restore_opts;
5072c79d967dSChristoph Hellwig 
5073ac27a0ecSDave Kleikamp 			/*
5074ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5075ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5076ac27a0ecSDave Kleikamp 			 */
5077ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
5078ac27a0ecSDave Kleikamp 
5079ac27a0ecSDave Kleikamp 			/*
5080ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5081ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5082ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5083ac27a0ecSDave Kleikamp 			 */
5084617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5085617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5086ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5087ac27a0ecSDave Kleikamp 
5088a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5089617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
5090ac27a0ecSDave Kleikamp 		} else {
5091a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5092e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
50932cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5094ac27a0ecSDave Kleikamp 				err = -EROFS;
5095ac27a0ecSDave Kleikamp 				goto restore_opts;
5096ac27a0ecSDave Kleikamp 			}
5097ead6596bSEric Sandeen 			/*
50988a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
50990b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
51008a266467STheodore Ts'o 			 */
51018a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
51028a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
51038a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
51048a266467STheodore Ts'o 
5105feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5106b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5107b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5108e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
51098a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
51106a797d27SDarrick J. Wong 					err = -EFSBADCRC;
51118a266467STheodore Ts'o 					goto restore_opts;
51128a266467STheodore Ts'o 				}
51138a266467STheodore Ts'o 			}
51148a266467STheodore Ts'o 
51158a266467STheodore Ts'o 			/*
5116ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5117ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5118ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5119ead6596bSEric Sandeen 			 */
5120ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5121b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5122ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5123ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5124b31e1552SEric Sandeen 				       "umount/remount instead");
5125ead6596bSEric Sandeen 				err = -EINVAL;
5126ead6596bSEric Sandeen 				goto restore_opts;
5127ead6596bSEric Sandeen 			}
5128ead6596bSEric Sandeen 
5129ac27a0ecSDave Kleikamp 			/*
5130ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5131ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5132ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5133ac27a0ecSDave Kleikamp 			 * the partition.)
5134ac27a0ecSDave Kleikamp 			 */
51350390131bSFrank Mayhar 			if (sbi->s_journal)
5136617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5137ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5138617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
5139ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
5140e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5141c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5142c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5143c5e06d10SJohann Lombardi 					err = -EROFS;
5144c5e06d10SJohann Lombardi 					goto restore_opts;
5145c5e06d10SJohann Lombardi 				}
5146c79d967dSChristoph Hellwig 			enable_quota = 1;
5147ac27a0ecSDave Kleikamp 		}
5148ac27a0ecSDave Kleikamp 	}
5149bfff6873SLukas Czerner 
5150bfff6873SLukas Czerner 	/*
5151bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5152bfff6873SLukas Czerner 	 * current settings
5153bfff6873SLukas Czerner 	 */
5154bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5155bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5156bfff6873SLukas Czerner 	else {
5157bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5158bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5159bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5160bfff6873SLukas Czerner 	}
5161bfff6873SLukas Czerner 
51626fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5163d096ad0fSMichael Tokarev 	if (sbi->s_journal == NULL && !(old_sb_flags & MS_RDONLY))
5164e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
51650390131bSFrank Mayhar 
5166ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5167ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5168a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5169ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
51707c319d32SAditya Kali 	if (enable_quota) {
51717c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
51720f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5173e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
51747c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
517507724f98STheodore Ts'o 			if (err)
51767c319d32SAditya Kali 				goto restore_opts;
51777c319d32SAditya Kali 		}
51787c319d32SAditya Kali 	}
51797c319d32SAditya Kali #endif
5180d4c402d9SCurt Wohlgemuth 
5181a26f4992STheodore Ts'o 	*flags = (*flags & ~MS_LAZYTIME) | (sb->s_flags & MS_LAZYTIME);
5182d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5183d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5184ac27a0ecSDave Kleikamp 	return 0;
51850b8e58a1SAndreas Dilger 
5186ac27a0ecSDave Kleikamp restore_opts:
5187ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5188ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5189a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5190ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5191ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5192ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
519330773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
519430773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5195ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5196ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5197a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
5198ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
5199ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
5200ac27a0ecSDave Kleikamp 	}
5201ac27a0ecSDave Kleikamp #endif
5202d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5203ac27a0ecSDave Kleikamp 	return err;
5204ac27a0ecSDave Kleikamp }
5205ac27a0ecSDave Kleikamp 
5206689c958cSLi Xi #ifdef CONFIG_QUOTA
5207689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5208689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5209689c958cSLi Xi {
5210689c958cSLi Xi 	struct kqid qid;
5211689c958cSLi Xi 	struct dquot *dquot;
5212689c958cSLi Xi 	u64 limit;
5213689c958cSLi Xi 	u64 curblock;
5214689c958cSLi Xi 
5215689c958cSLi Xi 	qid = make_kqid_projid(projid);
5216689c958cSLi Xi 	dquot = dqget(sb, qid);
5217689c958cSLi Xi 	if (IS_ERR(dquot))
5218689c958cSLi Xi 		return PTR_ERR(dquot);
52197b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5220689c958cSLi Xi 
5221689c958cSLi Xi 	limit = (dquot->dq_dqb.dqb_bsoftlimit ?
5222689c958cSLi Xi 		 dquot->dq_dqb.dqb_bsoftlimit :
5223689c958cSLi Xi 		 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
5224689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5225689c958cSLi Xi 		curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
5226689c958cSLi Xi 		buf->f_blocks = limit;
5227689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5228689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5229689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5230689c958cSLi Xi 	}
5231689c958cSLi Xi 
5232689c958cSLi Xi 	limit = dquot->dq_dqb.dqb_isoftlimit ?
5233689c958cSLi Xi 		dquot->dq_dqb.dqb_isoftlimit :
5234689c958cSLi Xi 		dquot->dq_dqb.dqb_ihardlimit;
5235689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5236689c958cSLi Xi 		buf->f_files = limit;
5237689c958cSLi Xi 		buf->f_ffree =
5238689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5239689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5240689c958cSLi Xi 	}
5241689c958cSLi Xi 
52427b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5243689c958cSLi Xi 	dqput(dquot);
5244689c958cSLi Xi 	return 0;
5245689c958cSLi Xi }
5246689c958cSLi Xi #endif
5247689c958cSLi Xi 
5248617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5249ac27a0ecSDave Kleikamp {
5250ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5251617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5252617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
525327dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5254960cc398SPekka Enberg 	u64 fsid;
5255d02a9391SKazuya Mio 	s64 bfree;
525627dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5257ac27a0ecSDave Kleikamp 
5258952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5259952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5260ac27a0ecSDave Kleikamp 
5261617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5262ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5263b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
526457042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
526557042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5266d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
526757042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
526827dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
526927dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
527027dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5271ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5272ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
527352d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5274617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5275960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5276960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5277960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5278960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
52790b8e58a1SAndreas Dilger 
5280689c958cSLi Xi #ifdef CONFIG_QUOTA
5281689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5282689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5283689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5284689c958cSLi Xi #endif
5285ac27a0ecSDave Kleikamp 	return 0;
5286ac27a0ecSDave Kleikamp }
5287ac27a0ecSDave Kleikamp 
5288ac27a0ecSDave Kleikamp 
5289ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5290ac27a0ecSDave Kleikamp 
5291bc8230eeSJan Kara /*
5292bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5293bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5294bc8230eeSJan Kara  */
5295ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5296ac27a0ecSDave Kleikamp {
52974c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5298ac27a0ecSDave Kleikamp }
5299ac27a0ecSDave Kleikamp 
5300617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5301ac27a0ecSDave Kleikamp {
5302ac27a0ecSDave Kleikamp 	int ret, err;
5303ac27a0ecSDave Kleikamp 	handle_t *handle;
5304ac27a0ecSDave Kleikamp 	struct inode *inode;
5305ac27a0ecSDave Kleikamp 
5306ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
53079924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5308617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5309ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5310ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5311ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5312617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5313ac27a0ecSDave Kleikamp 	if (!ret)
5314ac27a0ecSDave Kleikamp 		ret = err;
5315ac27a0ecSDave Kleikamp 	return ret;
5316ac27a0ecSDave Kleikamp }
5317ac27a0ecSDave Kleikamp 
5318617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5319ac27a0ecSDave Kleikamp {
5320ac27a0ecSDave Kleikamp 	int ret, err;
5321ac27a0ecSDave Kleikamp 	handle_t *handle;
5322ac27a0ecSDave Kleikamp 
53239924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5324617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5325ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5326ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5327ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5328617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5329ac27a0ecSDave Kleikamp 	if (!ret)
5330ac27a0ecSDave Kleikamp 		ret = err;
5331ac27a0ecSDave Kleikamp 	return ret;
5332ac27a0ecSDave Kleikamp }
5333ac27a0ecSDave Kleikamp 
5334617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5335ac27a0ecSDave Kleikamp {
5336ac27a0ecSDave Kleikamp 	int ret, err;
5337ac27a0ecSDave Kleikamp 	handle_t *handle;
5338ac27a0ecSDave Kleikamp 
53399924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5340617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
53419c3013e9SJan Kara 	if (IS_ERR(handle)) {
53429c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
53439c3013e9SJan Kara 		dquot_release(dquot);
5344ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
53459c3013e9SJan Kara 	}
5346ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5347617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5348ac27a0ecSDave Kleikamp 	if (!ret)
5349ac27a0ecSDave Kleikamp 		ret = err;
5350ac27a0ecSDave Kleikamp 	return ret;
5351ac27a0ecSDave Kleikamp }
5352ac27a0ecSDave Kleikamp 
5353617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5354ac27a0ecSDave Kleikamp {
5355262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5356262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5357262b4662SJan Kara 
53582c8be6b2SJan Kara 	/* Are we journaling quotas? */
5359e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5360262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5361ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5362617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5363ac27a0ecSDave Kleikamp 	} else {
5364ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5365ac27a0ecSDave Kleikamp 	}
5366ac27a0ecSDave Kleikamp }
5367ac27a0ecSDave Kleikamp 
5368617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5369ac27a0ecSDave Kleikamp {
5370ac27a0ecSDave Kleikamp 	int ret, err;
5371ac27a0ecSDave Kleikamp 	handle_t *handle;
5372ac27a0ecSDave Kleikamp 
5373ac27a0ecSDave Kleikamp 	/* Data block + inode block */
53742b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5375ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5376ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5377ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5378617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5379ac27a0ecSDave Kleikamp 	if (!ret)
5380ac27a0ecSDave Kleikamp 		ret = err;
5381ac27a0ecSDave Kleikamp 	return ret;
5382ac27a0ecSDave Kleikamp }
5383ac27a0ecSDave Kleikamp 
5384ac27a0ecSDave Kleikamp /*
5385ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5386ac27a0ecSDave Kleikamp  * the quota file and such...
5387ac27a0ecSDave Kleikamp  */
5388617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5389ac27a0ecSDave Kleikamp {
5390287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
5391617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5392ac27a0ecSDave Kleikamp }
5393ac27a0ecSDave Kleikamp 
5394daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5395daf647d2STheodore Ts'o {
5396daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5397daf647d2STheodore Ts'o 
5398daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5399daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5400daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5401daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5402daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5403daf647d2STheodore Ts'o 	 */
5404daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5405daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5406daf647d2STheodore Ts'o }
5407daf647d2STheodore Ts'o 
5408ac27a0ecSDave Kleikamp /*
5409ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5410ac27a0ecSDave Kleikamp  */
5411617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
54128c54ca9cSAl Viro 			 const struct path *path)
5413ac27a0ecSDave Kleikamp {
5414ac27a0ecSDave Kleikamp 	int err;
5415ac27a0ecSDave Kleikamp 
5416ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5417ac27a0ecSDave Kleikamp 		return -EINVAL;
54180623543bSJan Kara 
5419ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5420d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5421ac27a0ecSDave Kleikamp 		return -EXDEV;
54220623543bSJan Kara 	/* Journaling quota? */
54230623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
54242b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5425f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5426b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5427b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5428b31e1552SEric Sandeen 				"Journaled quota will not work");
542991389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
543091389240SJan Kara 	} else {
543191389240SJan Kara 		/*
543291389240SJan Kara 		 * Clear the flag just in case mount options changed since
543391389240SJan Kara 		 * last time.
543491389240SJan Kara 		 */
543591389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
54360623543bSJan Kara 	}
54370623543bSJan Kara 
54380623543bSJan Kara 	/*
54390623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
54400623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
54410623543bSJan Kara 	 */
54420390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
54432b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
54440623543bSJan Kara 		/*
54450623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
54460623543bSJan Kara 		 * otherwise be livelocked...
54470623543bSJan Kara 		 */
54480623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
54497ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
54500623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5451f00c9e44SJan Kara 		if (err)
54527ffe1ea8SHidehiro Kawai 			return err;
54537ffe1ea8SHidehiro Kawai 	}
5454957153fcSJan Kara 
5455daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5456daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5457957153fcSJan Kara 	if (err) {
5458daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5459daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5460957153fcSJan Kara 	} else {
5461957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5462957153fcSJan Kara 		handle_t *handle;
5463957153fcSJan Kara 
546461a92987SJan Kara 		/*
546561a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
546661a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
546761a92987SJan Kara 		 * are already enabled and this is not a hard failure.
546861a92987SJan Kara 		 */
5469957153fcSJan Kara 		inode_lock(inode);
5470957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5471957153fcSJan Kara 		if (IS_ERR(handle))
5472957153fcSJan Kara 			goto unlock_inode;
5473957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5474957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5475957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
5476957153fcSJan Kara 		ext4_mark_inode_dirty(handle, inode);
5477957153fcSJan Kara 		ext4_journal_stop(handle);
5478957153fcSJan Kara 	unlock_inode:
5479957153fcSJan Kara 		inode_unlock(inode);
5480957153fcSJan Kara 	}
5481daf647d2STheodore Ts'o 	return err;
5482ac27a0ecSDave Kleikamp }
5483ac27a0ecSDave Kleikamp 
54847c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
54857c319d32SAditya Kali 			     unsigned int flags)
54867c319d32SAditya Kali {
54877c319d32SAditya Kali 	int err;
54887c319d32SAditya Kali 	struct inode *qf_inode;
5489a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
54907c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5491689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5492689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
54937c319d32SAditya Kali 	};
54947c319d32SAditya Kali 
5495e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
54967c319d32SAditya Kali 
54977c319d32SAditya Kali 	if (!qf_inums[type])
54987c319d32SAditya Kali 		return -EPERM;
54997c319d32SAditya Kali 
55007c319d32SAditya Kali 	qf_inode = ext4_iget(sb, qf_inums[type]);
55017c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
55027c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
55037c319d32SAditya Kali 		return PTR_ERR(qf_inode);
55047c319d32SAditya Kali 	}
55057c319d32SAditya Kali 
5506bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5507bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5508daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
55097c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
55107c319d32SAditya Kali 	iput(qf_inode);
5511daf647d2STheodore Ts'o 	if (err)
5512daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
55137c319d32SAditya Kali 
55147c319d32SAditya Kali 	return err;
55157c319d32SAditya Kali }
55167c319d32SAditya Kali 
55177c319d32SAditya Kali /* Enable usage tracking for all quota types. */
55187c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
55197c319d32SAditya Kali {
55207c319d32SAditya Kali 	int type, err = 0;
5521a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
55227c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5523689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5524689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
55257c319d32SAditya Kali 	};
552649da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
552749da9392SJan Kara 		test_opt(sb, USRQUOTA),
552849da9392SJan Kara 		test_opt(sb, GRPQUOTA),
552949da9392SJan Kara 		test_opt(sb, PRJQUOTA),
553049da9392SJan Kara 	};
55317c319d32SAditya Kali 
553291389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
5533a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
55347c319d32SAditya Kali 		if (qf_inums[type]) {
55357c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
553649da9392SJan Kara 				DQUOT_USAGE_ENABLED |
553749da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
55387c319d32SAditya Kali 			if (err) {
553995f1fda4Szhangyi (F) 				for (type--; type >= 0; type--)
554095f1fda4Szhangyi (F) 					dquot_quota_off(sb, type);
554195f1fda4Szhangyi (F) 
55427c319d32SAditya Kali 				ext4_warning(sb,
554372ba7450STheodore Ts'o 					"Failed to enable quota tracking "
554472ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
554572ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
55467c319d32SAditya Kali 				return err;
55477c319d32SAditya Kali 			}
55487c319d32SAditya Kali 		}
55497c319d32SAditya Kali 	}
55507c319d32SAditya Kali 	return 0;
55517c319d32SAditya Kali }
55527c319d32SAditya Kali 
5553ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5554ca0e05e4SDmitry Monakhov {
555521f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
555621f97697SJan Kara 	handle_t *handle;
5557957153fcSJan Kara 	int err;
555821f97697SJan Kara 
555987009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
556087009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
556187009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5562ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5563ca0e05e4SDmitry Monakhov 
5564957153fcSJan Kara 	if (!inode || !igrab(inode))
55650b268590SAmir Goldstein 		goto out;
55660b268590SAmir Goldstein 
5567957153fcSJan Kara 	err = dquot_quota_off(sb, type);
5568964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
5569957153fcSJan Kara 		goto out_put;
5570957153fcSJan Kara 
5571957153fcSJan Kara 	inode_lock(inode);
557261a92987SJan Kara 	/*
557361a92987SJan Kara 	 * Update modification times of quota files when userspace can
557461a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
557561a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
557661a92987SJan Kara 	 */
55779924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
557821f97697SJan Kara 	if (IS_ERR(handle))
5579957153fcSJan Kara 		goto out_unlock;
5580957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
5581957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
5582eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
558321f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
558421f97697SJan Kara 	ext4_journal_stop(handle);
5585957153fcSJan Kara out_unlock:
5586957153fcSJan Kara 	inode_unlock(inode);
5587957153fcSJan Kara out_put:
5588964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
5589957153fcSJan Kara 	iput(inode);
5590957153fcSJan Kara 	return err;
559121f97697SJan Kara out:
5592ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5593ca0e05e4SDmitry Monakhov }
5594ca0e05e4SDmitry Monakhov 
5595ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5596ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5597ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5598ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5599617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5600ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5601ac27a0ecSDave Kleikamp {
5602ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5603725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5604ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5605ac27a0ecSDave Kleikamp 	int tocopy;
5606ac27a0ecSDave Kleikamp 	size_t toread;
5607ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5608ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5609ac27a0ecSDave Kleikamp 
5610ac27a0ecSDave Kleikamp 	if (off > i_size)
5611ac27a0ecSDave Kleikamp 		return 0;
5612ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5613ac27a0ecSDave Kleikamp 		len = i_size-off;
5614ac27a0ecSDave Kleikamp 	toread = len;
5615ac27a0ecSDave Kleikamp 	while (toread > 0) {
5616ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5617ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
56181c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
56191c215028STheodore Ts'o 		if (IS_ERR(bh))
56201c215028STheodore Ts'o 			return PTR_ERR(bh);
5621ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5622ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5623ac27a0ecSDave Kleikamp 		else
5624ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5625ac27a0ecSDave Kleikamp 		brelse(bh);
5626ac27a0ecSDave Kleikamp 		offset = 0;
5627ac27a0ecSDave Kleikamp 		toread -= tocopy;
5628ac27a0ecSDave Kleikamp 		data += tocopy;
5629ac27a0ecSDave Kleikamp 		blk++;
5630ac27a0ecSDave Kleikamp 	}
5631ac27a0ecSDave Kleikamp 	return len;
5632ac27a0ecSDave Kleikamp }
5633ac27a0ecSDave Kleikamp 
5634ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5635ac27a0ecSDave Kleikamp  * enough credits) */
5636617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5637ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5638ac27a0ecSDave Kleikamp {
5639ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5640725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
56411c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
5642c5e298aeSTheodore Ts'o 	int retries = 0;
5643ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5644ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5645ac27a0ecSDave Kleikamp 
56460390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5647b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5648b31e1552SEric Sandeen 			" cancelled because transaction is not started",
56499c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
56509c3013e9SJan Kara 		return -EIO;
56519c3013e9SJan Kara 	}
565267eeb568SDmitry Monakhov 	/*
565367eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
565467eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
565567eeb568SDmitry Monakhov 	 */
565667eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
565767eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
565867eeb568SDmitry Monakhov 			" cancelled because not block aligned",
565967eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
566067eeb568SDmitry Monakhov 		return -EIO;
566167eeb568SDmitry Monakhov 	}
566267eeb568SDmitry Monakhov 
5663c5e298aeSTheodore Ts'o 	do {
5664c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
5665c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
5666c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
5667c5e298aeSTheodore Ts'o 	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
5668c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
56691c215028STheodore Ts'o 	if (IS_ERR(bh))
56701c215028STheodore Ts'o 		return PTR_ERR(bh);
5671ac27a0ecSDave Kleikamp 	if (!bh)
5672ac27a0ecSDave Kleikamp 		goto out;
56735d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
5674617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5675ac27a0ecSDave Kleikamp 	if (err) {
5676ac27a0ecSDave Kleikamp 		brelse(bh);
56771c215028STheodore Ts'o 		return err;
5678ac27a0ecSDave Kleikamp 	}
5679ac27a0ecSDave Kleikamp 	lock_buffer(bh);
568067eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5681ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5682ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
56830390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5684ac27a0ecSDave Kleikamp 	brelse(bh);
5685ac27a0ecSDave Kleikamp out:
568667eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
568767eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
5688617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
5689617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
569021f97697SJan Kara 	}
569167eeb568SDmitry Monakhov 	return len;
5692ac27a0ecSDave Kleikamp }
5693ac27a0ecSDave Kleikamp 
56948f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid)
56958f0e8746STheodore Ts'o {
56968f0e8746STheodore Ts'o 	const struct quota_format_ops	*ops;
56978f0e8746STheodore Ts'o 
56988f0e8746STheodore Ts'o 	if (!sb_has_quota_loaded(sb, qid->type))
56998f0e8746STheodore Ts'o 		return -ESRCH;
57008f0e8746STheodore Ts'o 	ops = sb_dqopt(sb)->ops[qid->type];
57018f0e8746STheodore Ts'o 	if (!ops || !ops->get_next_id)
57028f0e8746STheodore Ts'o 		return -ENOSYS;
57038f0e8746STheodore Ts'o 	return dquot_get_next_id(sb, qid);
57048f0e8746STheodore Ts'o }
5705ac27a0ecSDave Kleikamp #endif
5706ac27a0ecSDave Kleikamp 
5707152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5708152a0836SAl Viro 		       const char *dev_name, void *data)
5709ac27a0ecSDave Kleikamp {
5710152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
5711ac27a0ecSDave Kleikamp }
5712ac27a0ecSDave Kleikamp 
5713c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
571424b58424STheodore Ts'o static inline void register_as_ext2(void)
571524b58424STheodore Ts'o {
571624b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
571724b58424STheodore Ts'o 	if (err)
571824b58424STheodore Ts'o 		printk(KERN_WARNING
571924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
572024b58424STheodore Ts'o }
572124b58424STheodore Ts'o 
572224b58424STheodore Ts'o static inline void unregister_as_ext2(void)
572324b58424STheodore Ts'o {
572424b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
572524b58424STheodore Ts'o }
57262035e776STheodore Ts'o 
57272035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
57282035e776STheodore Ts'o {
5729e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
57302035e776STheodore Ts'o 		return 0;
5731bc98a42cSDavid Howells 	if (sb_rdonly(sb))
57322035e776STheodore Ts'o 		return 1;
5733e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
57342035e776STheodore Ts'o 		return 0;
57352035e776STheodore Ts'o 	return 1;
57362035e776STheodore Ts'o }
573724b58424STheodore Ts'o #else
573824b58424STheodore Ts'o static inline void register_as_ext2(void) { }
573924b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
57402035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
574124b58424STheodore Ts'o #endif
574224b58424STheodore Ts'o 
574324b58424STheodore Ts'o static inline void register_as_ext3(void)
574424b58424STheodore Ts'o {
574524b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
574624b58424STheodore Ts'o 	if (err)
574724b58424STheodore Ts'o 		printk(KERN_WARNING
574824b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
574924b58424STheodore Ts'o }
575024b58424STheodore Ts'o 
575124b58424STheodore Ts'o static inline void unregister_as_ext3(void)
575224b58424STheodore Ts'o {
575324b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
575424b58424STheodore Ts'o }
57552035e776STheodore Ts'o 
57562035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
57572035e776STheodore Ts'o {
5758e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
57592035e776STheodore Ts'o 		return 0;
5760e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
57612035e776STheodore Ts'o 		return 0;
5762bc98a42cSDavid Howells 	if (sb_rdonly(sb))
57632035e776STheodore Ts'o 		return 1;
5764e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
57652035e776STheodore Ts'o 		return 0;
57662035e776STheodore Ts'o 	return 1;
57672035e776STheodore Ts'o }
576824b58424STheodore Ts'o 
576903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
5770ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
577103010a33STheodore Ts'o 	.name		= "ext4",
5772152a0836SAl Viro 	.mount		= ext4_mount,
5773ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
5774ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
5775ac27a0ecSDave Kleikamp };
57767f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
5777ac27a0ecSDave Kleikamp 
5778e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
5779e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5780e9e3bcecSEric Sandeen 
57815dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
5782ac27a0ecSDave Kleikamp {
5783e9e3bcecSEric Sandeen 	int i, err;
5784c9de560dSAlex Tomas 
5785e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
578607c0c5d8SAl Viro 	ext4_li_info = NULL;
578707c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
578807c0c5d8SAl Viro 
57899a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
579012e9b892SDmitry Monakhov 	ext4_check_flag_values();
5791e9e3bcecSEric Sandeen 
5792e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
5793e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
5794e9e3bcecSEric Sandeen 
579551865fdaSZheng Liu 	err = ext4_init_es();
57966fd058f7STheodore Ts'o 	if (err)
57976fd058f7STheodore Ts'o 		return err;
579851865fdaSZheng Liu 
579951865fdaSZheng Liu 	err = ext4_init_pageio();
580051865fdaSZheng Liu 	if (err)
5801b5799018STheodore Ts'o 		goto out5;
580251865fdaSZheng Liu 
58035dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
5804bd2d0210STheodore Ts'o 	if (err)
5805b5799018STheodore Ts'o 		goto out4;
5806857ac889SLukas Czerner 
5807b5799018STheodore Ts'o 	err = ext4_init_sysfs();
5808dd68314cSTheodore Ts'o 	if (err)
5809b5799018STheodore Ts'o 		goto out3;
5810857ac889SLukas Czerner 
58115dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
5812ac27a0ecSDave Kleikamp 	if (err)
5813c9de560dSAlex Tomas 		goto out2;
5814ac27a0ecSDave Kleikamp 	err = init_inodecache();
5815ac27a0ecSDave Kleikamp 	if (err)
5816ac27a0ecSDave Kleikamp 		goto out1;
581724b58424STheodore Ts'o 	register_as_ext3();
58182035e776STheodore Ts'o 	register_as_ext2();
581903010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
5820ac27a0ecSDave Kleikamp 	if (err)
5821ac27a0ecSDave Kleikamp 		goto out;
5822bfff6873SLukas Czerner 
5823ac27a0ecSDave Kleikamp 	return 0;
5824ac27a0ecSDave Kleikamp out:
582524b58424STheodore Ts'o 	unregister_as_ext2();
582624b58424STheodore Ts'o 	unregister_as_ext3();
5827ac27a0ecSDave Kleikamp 	destroy_inodecache();
5828ac27a0ecSDave Kleikamp out1:
58295dabfc78STheodore Ts'o 	ext4_exit_mballoc();
58309c191f70ST Makphaibulchoke out2:
5831b5799018STheodore Ts'o 	ext4_exit_sysfs();
5832b5799018STheodore Ts'o out3:
5833dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5834b5799018STheodore Ts'o out4:
58355dabfc78STheodore Ts'o 	ext4_exit_pageio();
5836b5799018STheodore Ts'o out5:
583751865fdaSZheng Liu 	ext4_exit_es();
583851865fdaSZheng Liu 
5839ac27a0ecSDave Kleikamp 	return err;
5840ac27a0ecSDave Kleikamp }
5841ac27a0ecSDave Kleikamp 
58425dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5843ac27a0ecSDave Kleikamp {
5844bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
584524b58424STheodore Ts'o 	unregister_as_ext2();
584624b58424STheodore Ts'o 	unregister_as_ext3();
584703010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5848ac27a0ecSDave Kleikamp 	destroy_inodecache();
58495dabfc78STheodore Ts'o 	ext4_exit_mballoc();
5850b5799018STheodore Ts'o 	ext4_exit_sysfs();
58515dabfc78STheodore Ts'o 	ext4_exit_system_zone();
58525dabfc78STheodore Ts'o 	ext4_exit_pageio();
5853dd12ed14SEric Sandeen 	ext4_exit_es();
5854ac27a0ecSDave Kleikamp }
5855ac27a0ecSDave Kleikamp 
5856ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
585783982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5858ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
58595dabfc78STheodore Ts'o module_init(ext4_init_fs)
58605dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5861