xref: /linux/fs/ext4/super.c (revision cd6bb35bf7f6d7d922509bf50265383a0ceabe96)
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>
407abc52c2SDan Magenheimer #include <linux/cleancache.h>
41ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
42ac27a0ecSDave Kleikamp 
43bfff6873SLukas Czerner #include <linux/kthread.h>
44bfff6873SLukas Czerner #include <linux/freezer.h>
45bfff6873SLukas Czerner 
463dcf5451SChristoph Hellwig #include "ext4.h"
474a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
483dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
49ac27a0ecSDave Kleikamp #include "xattr.h"
50ac27a0ecSDave Kleikamp #include "acl.h"
513661d286STheodore Ts'o #include "mballoc.h"
52ac27a0ecSDave Kleikamp 
539bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
549bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
559bffad1eSTheodore Ts'o 
560b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
570b75a840SLukas Czerner static struct mutex ext4_li_mtx;
58e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
599f6200bbSTheodore Ts'o 
60617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
61ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
622adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
63e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
64617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
65617ba13bSMingming Cao 					struct ext4_super_block *es);
66617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
67617ba13bSMingming Cao 				   struct ext4_super_block *es);
68617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
69617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
70617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
71c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
72c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
73152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
74152a0836SAl Viro 		       const char *dev_name, void *data);
752035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
762035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
77d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
78bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
79bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
808f1f7453SEric Sandeen static void ext4_clear_request_list(void);
81c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
82c6cb7e77SEric Whitney 					    unsigned int journal_inum);
83ac27a0ecSDave Kleikamp 
84e74031fdSJan Kara /*
85e74031fdSJan Kara  * Lock ordering
86e74031fdSJan Kara  *
87e74031fdSJan Kara  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
88e74031fdSJan Kara  * i_mmap_rwsem (inode->i_mmap_rwsem)!
89e74031fdSJan Kara  *
90e74031fdSJan Kara  * page fault path:
91e74031fdSJan Kara  * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
92e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
93e74031fdSJan Kara  *
94e74031fdSJan Kara  * buffered write path:
95e74031fdSJan Kara  * sb_start_write -> i_mutex -> mmap_sem
96e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
97e74031fdSJan Kara  *   i_data_sem (rw)
98e74031fdSJan Kara  *
99e74031fdSJan Kara  * truncate:
100e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
101e74031fdSJan Kara  *   i_mmap_rwsem (w) -> page lock
102e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
103e74031fdSJan Kara  *   transaction start -> i_data_sem (rw)
104e74031fdSJan Kara  *
105e74031fdSJan Kara  * direct IO:
106e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) -> mmap_sem
107e74031fdSJan Kara  * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) ->
108e74031fdSJan Kara  *   transaction start -> i_data_sem (rw)
109e74031fdSJan Kara  *
110e74031fdSJan Kara  * writepages:
111e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
112e74031fdSJan Kara  */
113e74031fdSJan Kara 
114c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1152035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1162035e776STheodore Ts'o 	.owner		= THIS_MODULE,
1172035e776STheodore Ts'o 	.name		= "ext2",
1182035e776STheodore Ts'o 	.mount		= ext4_mount,
1192035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
1202035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
1212035e776STheodore Ts'o };
1227f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
123fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1242035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1252035e776STheodore Ts'o #else
1262035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1272035e776STheodore Ts'o #endif
1282035e776STheodore Ts'o 
1292035e776STheodore Ts'o 
130ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
131ba69f9abSJan Kara 	.owner		= THIS_MODULE,
132ba69f9abSJan Kara 	.name		= "ext3",
133152a0836SAl Viro 	.mount		= ext4_mount,
134ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
135ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
136ba69f9abSJan Kara };
1377f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
138fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
139ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
140bd81d8eeSLaurent Vivier 
141d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
142d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
143d25425f8SDarrick J. Wong {
144e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
145d25425f8SDarrick J. Wong 		return 1;
146d25425f8SDarrick J. Wong 
147d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
148d25425f8SDarrick J. Wong }
149d25425f8SDarrick J. Wong 
150a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
151a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
152a9c47317SDarrick J. Wong {
153a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
154a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
155a9c47317SDarrick J. Wong 	__u32 csum;
156a9c47317SDarrick J. Wong 
157a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
158a9c47317SDarrick J. Wong 
159a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
160a9c47317SDarrick J. Wong }
161a9c47317SDarrick J. Wong 
162c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb,
163a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
164a9c47317SDarrick J. Wong {
1659aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
166a9c47317SDarrick J. Wong 		return 1;
167a9c47317SDarrick J. Wong 
168a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
169a9c47317SDarrick J. Wong }
170a9c47317SDarrick J. Wong 
17106db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
172a9c47317SDarrick J. Wong {
17306db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
17406db49e6STheodore Ts'o 
1759aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
176a9c47317SDarrick J. Wong 		return;
177a9c47317SDarrick J. Wong 
178a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
179a9c47317SDarrick J. Wong }
180a9c47317SDarrick J. Wong 
1819933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
1829933fc0aSTheodore Ts'o {
1839933fc0aSTheodore Ts'o 	void *ret;
1849933fc0aSTheodore Ts'o 
1858be04b93SJoe Perches 	ret = kmalloc(size, flags | __GFP_NOWARN);
1869933fc0aSTheodore Ts'o 	if (!ret)
1879933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
1889933fc0aSTheodore Ts'o 	return ret;
1899933fc0aSTheodore Ts'o }
1909933fc0aSTheodore Ts'o 
1919933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
1929933fc0aSTheodore Ts'o {
1939933fc0aSTheodore Ts'o 	void *ret;
1949933fc0aSTheodore Ts'o 
1958be04b93SJoe Perches 	ret = kzalloc(size, flags | __GFP_NOWARN);
1969933fc0aSTheodore Ts'o 	if (!ret)
1979933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
1989933fc0aSTheodore Ts'o 	return ret;
1999933fc0aSTheodore Ts'o }
2009933fc0aSTheodore Ts'o 
2018fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
2028fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
203bd81d8eeSLaurent Vivier {
2043a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
2058fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2068fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
207bd81d8eeSLaurent Vivier }
208bd81d8eeSLaurent Vivier 
2098fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
2108fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
211bd81d8eeSLaurent Vivier {
2125272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
2138fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2148fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
215bd81d8eeSLaurent Vivier }
216bd81d8eeSLaurent Vivier 
2178fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
2188fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
219bd81d8eeSLaurent Vivier {
2205272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
2218fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2228fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
223bd81d8eeSLaurent Vivier }
224bd81d8eeSLaurent Vivier 
225021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
226560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
227560671a0SAneesh Kumar K.V {
228560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
229560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
230560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
231560671a0SAneesh Kumar K.V }
232560671a0SAneesh Kumar K.V 
233560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
234560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
235560671a0SAneesh Kumar K.V {
236560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
237560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
238560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
239560671a0SAneesh Kumar K.V }
240560671a0SAneesh Kumar K.V 
241560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
242560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
243560671a0SAneesh Kumar K.V {
244560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
245560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
246560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
247560671a0SAneesh Kumar K.V }
248560671a0SAneesh Kumar K.V 
249560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
250560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
251560671a0SAneesh Kumar K.V {
252560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
253560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
254560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
255560671a0SAneesh Kumar K.V }
256560671a0SAneesh Kumar K.V 
2578fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2588fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
259bd81d8eeSLaurent Vivier {
2603a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2618fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2628fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
263bd81d8eeSLaurent Vivier }
264bd81d8eeSLaurent Vivier 
2658fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2668fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
267bd81d8eeSLaurent Vivier {
2685272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2698fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2708fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
271bd81d8eeSLaurent Vivier }
272bd81d8eeSLaurent Vivier 
2738fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2748fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
275bd81d8eeSLaurent Vivier {
2765272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2778fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2788fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
279bd81d8eeSLaurent Vivier }
280bd81d8eeSLaurent Vivier 
281021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
282560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
283560671a0SAneesh Kumar K.V {
284560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
285560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
286560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
287560671a0SAneesh Kumar K.V }
288560671a0SAneesh Kumar K.V 
289560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
290560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
291560671a0SAneesh Kumar K.V {
292560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
293560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
294560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
295560671a0SAneesh Kumar K.V }
296560671a0SAneesh Kumar K.V 
297560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
298560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
299560671a0SAneesh Kumar K.V {
300560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
301560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
302560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
303560671a0SAneesh Kumar K.V }
304560671a0SAneesh Kumar K.V 
305560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
306560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
307560671a0SAneesh Kumar K.V {
308560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
309560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
310560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
311560671a0SAneesh Kumar K.V }
312560671a0SAneesh Kumar K.V 
313d3d1faf6SCurt Wohlgemuth 
3141c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3151c13d5c0STheodore Ts'o 			    unsigned int line)
3161c13d5c0STheodore Ts'o {
3171c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3181c13d5c0STheodore Ts'o 
3191c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3201b46617bSTheodore Ts'o 	if (bdev_read_only(sb->s_bdev))
3211b46617bSTheodore Ts'o 		return;
3221c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3231c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
3241c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3251c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3261c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3271c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3281c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3291c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3301c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3311c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3321c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3331c13d5c0STheodore Ts'o 	}
33466e61a9eSTheodore Ts'o 	/*
33566e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
33666e61a9eSTheodore Ts'o 	 * started already
33766e61a9eSTheodore Ts'o 	 */
33866e61a9eSTheodore Ts'o 	if (!es->s_error_count)
33966e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
340ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
3411c13d5c0STheodore Ts'o }
3421c13d5c0STheodore Ts'o 
3431c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3441c13d5c0STheodore Ts'o 			    unsigned int line)
3451c13d5c0STheodore Ts'o {
3461c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3471c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3481c13d5c0STheodore Ts'o }
3491c13d5c0STheodore Ts'o 
350bdfe0cbdSTheodore Ts'o /*
351bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
352bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
353bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
354bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
355bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
356bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
357bdfe0cbdSTheodore Ts'o  */
358bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
359bdfe0cbdSTheodore Ts'o {
360bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
361bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
362bdfe0cbdSTheodore Ts'o 
363bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
364bdfe0cbdSTheodore Ts'o }
365bdfe0cbdSTheodore Ts'o 
36618aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
36718aadd47SBobi Jam {
36818aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
36918aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
37018aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
3715d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
37218aadd47SBobi Jam 
3735d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
37418aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
3755d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
3765d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
3775d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
37818aadd47SBobi Jam 		list_del_init(&jce->jce_list);
37918aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
38018aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
38118aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
38218aadd47SBobi Jam 	}
38318aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
38418aadd47SBobi Jam }
3851c13d5c0STheodore Ts'o 
386ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
387ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
388ac27a0ecSDave Kleikamp  *
389ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
390617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
391ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
392ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
393ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
394ac27a0ecSDave Kleikamp  * write out the superblock safely.
395ac27a0ecSDave Kleikamp  *
396dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
397d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
398ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
399ac27a0ecSDave Kleikamp  */
400ac27a0ecSDave Kleikamp 
401617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
402ac27a0ecSDave Kleikamp {
403ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
404ac27a0ecSDave Kleikamp 		return;
405ac27a0ecSDave Kleikamp 
406ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
407617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
408ac27a0ecSDave Kleikamp 
4094ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
410ac27a0ecSDave Kleikamp 		if (journal)
411dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
412ac27a0ecSDave Kleikamp 	}
413ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
414b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
4154418e141SDmitry Monakhov 		/*
4164418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
4174418e141SDmitry Monakhov 		 * before ->s_flags update
4184418e141SDmitry Monakhov 		 */
4194418e141SDmitry Monakhov 		smp_wmb();
420ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
421ac27a0ecSDave Kleikamp 	}
4224327ba52SDaeho Jeong 	if (test_opt(sb, ERRORS_PANIC)) {
4234327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
4244327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
4254327ba52SDaeho Jeong 			return;
426617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
427ac27a0ecSDave Kleikamp 			sb->s_id);
428ac27a0ecSDave Kleikamp 	}
4294327ba52SDaeho Jeong }
430ac27a0ecSDave Kleikamp 
431efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
432efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
433efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
434efbed4dcSTheodore Ts'o 
43512062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
436c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
437ac27a0ecSDave Kleikamp {
4380ff2ea7dSJoe Perches 	struct va_format vaf;
439ac27a0ecSDave Kleikamp 	va_list args;
440ac27a0ecSDave Kleikamp 
441efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
442ac27a0ecSDave Kleikamp 		va_start(args, fmt);
4430ff2ea7dSJoe Perches 		vaf.fmt = fmt;
4440ff2ea7dSJoe Perches 		vaf.va = &args;
445efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
446efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4470ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
448ac27a0ecSDave Kleikamp 		va_end(args);
449efbed4dcSTheodore Ts'o 	}
450f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
451617ba13bSMingming Cao 	ext4_handle_error(sb);
452ac27a0ecSDave Kleikamp }
453ac27a0ecSDave Kleikamp 
454e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
455c398eda0STheodore Ts'o 			unsigned int line, ext4_fsblk_t block,
456273df556SFrank Mayhar 			const char *fmt, ...)
457273df556SFrank Mayhar {
458273df556SFrank Mayhar 	va_list args;
459f7c21177STheodore Ts'o 	struct va_format vaf;
4601c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
461273df556SFrank Mayhar 
4621c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4631c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
464efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
465273df556SFrank Mayhar 		va_start(args, fmt);
466f7c21177STheodore Ts'o 		vaf.fmt = fmt;
467f7c21177STheodore Ts'o 		vaf.va = &args;
468c398eda0STheodore Ts'o 		if (block)
469d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
470d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
471d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
472d9ee81daSJoe Perches 			       block, current->comm, &vaf);
473d9ee81daSJoe Perches 		else
474d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
475d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
476d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
477d9ee81daSJoe Perches 			       current->comm, &vaf);
478273df556SFrank Mayhar 		va_end(args);
479efbed4dcSTheodore Ts'o 	}
480efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
481273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
482273df556SFrank Mayhar }
483273df556SFrank Mayhar 
484e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
485f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
486f7c21177STheodore Ts'o 		       const char *fmt, ...)
487273df556SFrank Mayhar {
488273df556SFrank Mayhar 	va_list args;
489f7c21177STheodore Ts'o 	struct va_format vaf;
4901c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
491496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
492273df556SFrank Mayhar 	char pathname[80], *path;
493273df556SFrank Mayhar 
4941c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
4951c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
496efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
4979bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
498f9a62d09SDan Carpenter 		if (IS_ERR(path))
499273df556SFrank Mayhar 			path = "(unknown)";
500f7c21177STheodore Ts'o 		va_start(args, fmt);
501f7c21177STheodore Ts'o 		vaf.fmt = fmt;
502f7c21177STheodore Ts'o 		vaf.va = &args;
503d9ee81daSJoe Perches 		if (block)
504d9ee81daSJoe Perches 			printk(KERN_CRIT
505d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
506d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
507d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
508d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
509d9ee81daSJoe Perches 		else
510d9ee81daSJoe Perches 			printk(KERN_CRIT
511d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
512d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
513d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
514d9ee81daSJoe Perches 			       current->comm, path, &vaf);
515273df556SFrank Mayhar 		va_end(args);
516efbed4dcSTheodore Ts'o 	}
517efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
518273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
519273df556SFrank Mayhar }
520273df556SFrank Mayhar 
521722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
522ac27a0ecSDave Kleikamp 			      char nbuf[16])
523ac27a0ecSDave Kleikamp {
524ac27a0ecSDave Kleikamp 	char *errstr = NULL;
525ac27a0ecSDave Kleikamp 
526ac27a0ecSDave Kleikamp 	switch (errno) {
5276a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
5286a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
5296a797d27SDarrick J. Wong 		break;
5306a797d27SDarrick J. Wong 	case -EFSBADCRC:
5316a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
5326a797d27SDarrick J. Wong 		break;
533ac27a0ecSDave Kleikamp 	case -EIO:
534ac27a0ecSDave Kleikamp 		errstr = "IO failure";
535ac27a0ecSDave Kleikamp 		break;
536ac27a0ecSDave Kleikamp 	case -ENOMEM:
537ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
538ac27a0ecSDave Kleikamp 		break;
539ac27a0ecSDave Kleikamp 	case -EROFS:
54078f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
54178f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
542ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
543ac27a0ecSDave Kleikamp 		else
544ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
545ac27a0ecSDave Kleikamp 		break;
546ac27a0ecSDave Kleikamp 	default:
547ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
548ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
549ac27a0ecSDave Kleikamp 		 * NULL. */
550ac27a0ecSDave Kleikamp 		if (nbuf) {
551ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
552ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
553ac27a0ecSDave Kleikamp 				errstr = nbuf;
554ac27a0ecSDave Kleikamp 		}
555ac27a0ecSDave Kleikamp 		break;
556ac27a0ecSDave Kleikamp 	}
557ac27a0ecSDave Kleikamp 
558ac27a0ecSDave Kleikamp 	return errstr;
559ac27a0ecSDave Kleikamp }
560ac27a0ecSDave Kleikamp 
561617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
562ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
563ac27a0ecSDave Kleikamp 
564c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
565c398eda0STheodore Ts'o 		      unsigned int line, int errno)
566ac27a0ecSDave Kleikamp {
567ac27a0ecSDave Kleikamp 	char nbuf[16];
568ac27a0ecSDave Kleikamp 	const char *errstr;
569ac27a0ecSDave Kleikamp 
570ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
571ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
572ac27a0ecSDave Kleikamp 	 * an error. */
573ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
574ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
575ac27a0ecSDave Kleikamp 		return;
576ac27a0ecSDave Kleikamp 
577efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
578617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
579c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
580c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
581efbed4dcSTheodore Ts'o 	}
582ac27a0ecSDave Kleikamp 
583efbed4dcSTheodore Ts'o 	save_error_info(sb, function, line);
584617ba13bSMingming Cao 	ext4_handle_error(sb);
585ac27a0ecSDave Kleikamp }
586ac27a0ecSDave Kleikamp 
587ac27a0ecSDave Kleikamp /*
588617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
589ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
590ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
591ac27a0ecSDave Kleikamp  *
592ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
593ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
594ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
595ac27a0ecSDave Kleikamp  */
596ac27a0ecSDave Kleikamp 
597c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
598c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
599ac27a0ecSDave Kleikamp {
600651e1c3bSJoe Perches 	struct va_format vaf;
601ac27a0ecSDave Kleikamp 	va_list args;
602ac27a0ecSDave Kleikamp 
6031c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
604ac27a0ecSDave Kleikamp 	va_start(args, fmt);
605651e1c3bSJoe Perches 	vaf.fmt = fmt;
606651e1c3bSJoe Perches 	vaf.va = &args;
607651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
608651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
609ac27a0ecSDave Kleikamp 	va_end(args);
610ac27a0ecSDave Kleikamp 
6111c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
612b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
6134ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
6144418e141SDmitry Monakhov 		/*
6154418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
6164418e141SDmitry Monakhov 		 * before ->s_flags update
6174418e141SDmitry Monakhov 		 */
6184418e141SDmitry Monakhov 		smp_wmb();
6194418e141SDmitry Monakhov 		sb->s_flags |= MS_RDONLY;
620ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
621dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
6221c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
6231c13d5c0STheodore Ts'o 	}
6244327ba52SDaeho Jeong 	if (test_opt(sb, ERRORS_PANIC)) {
6254327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
6264327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
6274327ba52SDaeho Jeong 			return;
6281c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
629ac27a0ecSDave Kleikamp 	}
6304327ba52SDaeho Jeong }
631ac27a0ecSDave Kleikamp 
632e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
633e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
634b31e1552SEric Sandeen {
6350ff2ea7dSJoe Perches 	struct va_format vaf;
636b31e1552SEric Sandeen 	va_list args;
637b31e1552SEric Sandeen 
638efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
639efbed4dcSTheodore Ts'o 		return;
640efbed4dcSTheodore Ts'o 
641b31e1552SEric Sandeen 	va_start(args, fmt);
6420ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6430ff2ea7dSJoe Perches 	vaf.va = &args;
6440ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
645b31e1552SEric Sandeen 	va_end(args);
646b31e1552SEric Sandeen }
647b31e1552SEric Sandeen 
648b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb)					\
649b03a2f7eSAndreas Dilger 		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
650b03a2f7eSAndreas Dilger 			     "EXT4-fs warning")
651b03a2f7eSAndreas Dilger 
65212062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
653c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
654ac27a0ecSDave Kleikamp {
6550ff2ea7dSJoe Perches 	struct va_format vaf;
656ac27a0ecSDave Kleikamp 	va_list args;
657ac27a0ecSDave Kleikamp 
658b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
659efbed4dcSTheodore Ts'o 		return;
660efbed4dcSTheodore Ts'o 
661ac27a0ecSDave Kleikamp 	va_start(args, fmt);
6620ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6630ff2ea7dSJoe Perches 	vaf.va = &args;
6640ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
6650ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
666ac27a0ecSDave Kleikamp 	va_end(args);
667ac27a0ecSDave Kleikamp }
668ac27a0ecSDave Kleikamp 
669b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
670b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
671b03a2f7eSAndreas Dilger {
672b03a2f7eSAndreas Dilger 	struct va_format vaf;
673b03a2f7eSAndreas Dilger 	va_list args;
674b03a2f7eSAndreas Dilger 
675b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
676b03a2f7eSAndreas Dilger 		return;
677b03a2f7eSAndreas Dilger 
678b03a2f7eSAndreas Dilger 	va_start(args, fmt);
679b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
680b03a2f7eSAndreas Dilger 	vaf.va = &args;
681b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
682b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
683b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
684b03a2f7eSAndreas Dilger 	va_end(args);
685b03a2f7eSAndreas Dilger }
686b03a2f7eSAndreas Dilger 
687e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
688e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
689e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
690e29136f8STheodore Ts'o 			     const char *fmt, ...)
6915d1b1b3fSAneesh Kumar K.V __releases(bitlock)
6925d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
6935d1b1b3fSAneesh Kumar K.V {
6940ff2ea7dSJoe Perches 	struct va_format vaf;
6955d1b1b3fSAneesh Kumar K.V 	va_list args;
6965d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
6975d1b1b3fSAneesh Kumar K.V 
6981c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
6991c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
7001c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
7010ff2ea7dSJoe Perches 
702efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
7035d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
7040ff2ea7dSJoe Perches 		vaf.fmt = fmt;
7050ff2ea7dSJoe Perches 		vaf.va = &args;
70621149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
707e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
708e29136f8STheodore Ts'o 		if (ino)
7090ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
710e29136f8STheodore Ts'o 		if (block)
711efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
712efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
7130ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
7145d1b1b3fSAneesh Kumar K.V 		va_end(args);
715efbed4dcSTheodore Ts'o 	}
7165d1b1b3fSAneesh Kumar K.V 
7175d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
718e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
7195d1b1b3fSAneesh Kumar K.V 		return;
7205d1b1b3fSAneesh Kumar K.V 	}
7211c13d5c0STheodore Ts'o 
7225d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
7235d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
7245d1b1b3fSAneesh Kumar K.V 	/*
7255d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
7265d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
7275d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
7285d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
7295d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
73025985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
7315d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
7325d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
7335d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
7345d1b1b3fSAneesh Kumar K.V 	 */
7355d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
7365d1b1b3fSAneesh Kumar K.V 	return;
7375d1b1b3fSAneesh Kumar K.V }
7385d1b1b3fSAneesh Kumar K.V 
739617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
740ac27a0ecSDave Kleikamp {
741617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
742ac27a0ecSDave Kleikamp 
743617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
744ac27a0ecSDave Kleikamp 		return;
745ac27a0ecSDave Kleikamp 
74612062dddSEric Sandeen 	ext4_warning(sb,
747ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
748ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
749617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
750ac27a0ecSDave Kleikamp 
751617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
752617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
753617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
754ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
755ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
756ac27a0ecSDave Kleikamp 
757ac27a0ecSDave Kleikamp 	/*
758ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
759ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
760ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
761ac27a0ecSDave Kleikamp 	 */
762ac27a0ecSDave Kleikamp }
763ac27a0ecSDave Kleikamp 
764ac27a0ecSDave Kleikamp /*
765ac27a0ecSDave Kleikamp  * Open the external journal device
766ac27a0ecSDave Kleikamp  */
767b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
768ac27a0ecSDave Kleikamp {
769ac27a0ecSDave Kleikamp 	struct block_device *bdev;
770ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
771ac27a0ecSDave Kleikamp 
772d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
773ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
774ac27a0ecSDave Kleikamp 		goto fail;
775ac27a0ecSDave Kleikamp 	return bdev;
776ac27a0ecSDave Kleikamp 
777ac27a0ecSDave Kleikamp fail:
778b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
779ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
780ac27a0ecSDave Kleikamp 	return NULL;
781ac27a0ecSDave Kleikamp }
782ac27a0ecSDave Kleikamp 
783ac27a0ecSDave Kleikamp /*
784ac27a0ecSDave Kleikamp  * Release the journal device
785ac27a0ecSDave Kleikamp  */
7864385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
787ac27a0ecSDave Kleikamp {
7884385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
789ac27a0ecSDave Kleikamp }
790ac27a0ecSDave Kleikamp 
7914385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
792ac27a0ecSDave Kleikamp {
793ac27a0ecSDave Kleikamp 	struct block_device *bdev;
794ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
795ac27a0ecSDave Kleikamp 	if (bdev) {
7964385bab1SAl Viro 		ext4_blkdev_put(bdev);
797ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
798ac27a0ecSDave Kleikamp 	}
799ac27a0ecSDave Kleikamp }
800ac27a0ecSDave Kleikamp 
801ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
802ac27a0ecSDave Kleikamp {
803617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
804ac27a0ecSDave Kleikamp }
805ac27a0ecSDave Kleikamp 
806617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
807ac27a0ecSDave Kleikamp {
808ac27a0ecSDave Kleikamp 	struct list_head *l;
809ac27a0ecSDave Kleikamp 
810b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
811ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
812ac27a0ecSDave Kleikamp 
813ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
814ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
815ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
816ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
817ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
818ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
819ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
820ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
821ac27a0ecSDave Kleikamp 	}
822ac27a0ecSDave Kleikamp }
823ac27a0ecSDave Kleikamp 
824617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
825ac27a0ecSDave Kleikamp {
826617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
827617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
828ef2cabf7SHidehiro Kawai 	int i, err;
829ac27a0ecSDave Kleikamp 
830857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
831e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
832e0ccfd95SChristoph Hellwig 
8332e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
8342e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
8354c0425ffSMingming Cao 
8360390131bSFrank Mayhar 	if (sbi->s_journal) {
837ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
83847b4a50bSJan Kara 		sbi->s_journal = NULL;
839ef2cabf7SHidehiro Kawai 		if (err < 0)
840c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
8410390131bSFrank Mayhar 	}
842d4edac31SJosef Bacik 
843ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
844d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
8459105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
846d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
847d4edac31SJosef Bacik 	ext4_mb_release(sb);
848d4edac31SJosef Bacik 	ext4_ext_release(sb);
849d4edac31SJosef Bacik 
850ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
851e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
852ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
853ac27a0ecSDave Kleikamp 	}
85458c5873aSArtem Bityutskiy 	if (!(sb->s_flags & MS_RDONLY))
855a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
856a8e25a83SArtem Bityutskiy 
857ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
858ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
859b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
860b93b41d4SAl Viro 	kvfree(sbi->s_flex_groups);
86157042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
862ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
863ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
86457042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
865c8585c6fSDaeho Jeong 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
866ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
867ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
868a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
869ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
870ac27a0ecSDave Kleikamp #endif
871ac27a0ecSDave Kleikamp 
872ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
873ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
874ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
875ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
876ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
877ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
878ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
879ac27a0ecSDave Kleikamp 
88089d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
881f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
882ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
883ac27a0ecSDave Kleikamp 		/*
884ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
885ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
886ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
887ac27a0ecSDave Kleikamp 		 */
888ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
889f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
890617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
891ac27a0ecSDave Kleikamp 	}
8929c191f70ST Makphaibulchoke 	if (sbi->s_mb_cache) {
8939c191f70ST Makphaibulchoke 		ext4_xattr_destroy_cache(sbi->s_mb_cache);
8949c191f70ST Makphaibulchoke 		sbi->s_mb_cache = NULL;
8959c191f70ST Makphaibulchoke 	}
896c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
897c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
898ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
8993197ebdbSTheodore Ts'o 	/*
9003197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
9013197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
9023197ebdbSTheodore Ts'o 	 */
9033197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
9043197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
9050441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
9060441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
907705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
908ac27a0ecSDave Kleikamp 	kfree(sbi);
909ac27a0ecSDave Kleikamp }
910ac27a0ecSDave Kleikamp 
911e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
912ac27a0ecSDave Kleikamp 
913ac27a0ecSDave Kleikamp /*
914ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
915ac27a0ecSDave Kleikamp  */
916617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
917ac27a0ecSDave Kleikamp {
918617ba13bSMingming Cao 	struct ext4_inode_info *ei;
919ac27a0ecSDave Kleikamp 
920e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
921ac27a0ecSDave Kleikamp 	if (!ei)
922ac27a0ecSDave Kleikamp 		return NULL;
9230b8e58a1SAndreas Dilger 
924ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
925202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
926c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
927c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
9289a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
9299a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
930edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
931eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
932edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
933dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
934d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
935d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
936d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
9379d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
9387e731bc9STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = 0;
939d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
940a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
941a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
94296c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
943a9e7f447SDmitry Monakhov #endif
9448aefcd55STheodore Ts'o 	ei->jinode = NULL;
9452e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
946744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
947b436b9beSJan Kara 	ei->i_sync_tid = 0;
948b436b9beSJan Kara 	ei->i_datasync_tid = 0;
949e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
9502e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
951ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
952ac27a0ecSDave Kleikamp }
953ac27a0ecSDave Kleikamp 
9547ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
9557ff9c073STheodore Ts'o {
9567ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
9577ff9c073STheodore Ts'o 
9587ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
9597ff9c073STheodore Ts'o 	return drop;
9607ff9c073STheodore Ts'o }
9617ff9c073STheodore Ts'o 
962fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
963fa0d7e3dSNick Piggin {
964fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
965fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
966fa0d7e3dSNick Piggin }
967fa0d7e3dSNick Piggin 
968617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
969ac27a0ecSDave Kleikamp {
9709f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
971b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
972b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
973b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
9749f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
9759f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
9769f7dd93dSVasily Averin 				true);
9779f7dd93dSVasily Averin 		dump_stack();
9789f7dd93dSVasily Averin 	}
979fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
980ac27a0ecSDave Kleikamp }
981ac27a0ecSDave Kleikamp 
98251cc5068SAlexey Dobriyan static void init_once(void *foo)
983ac27a0ecSDave Kleikamp {
984617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
985ac27a0ecSDave Kleikamp 
986ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
987ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
9880e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
989ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
990ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
991ac27a0ecSDave Kleikamp }
992ac27a0ecSDave Kleikamp 
993e67bc2b3SFabian Frederick static int __init init_inodecache(void)
994ac27a0ecSDave Kleikamp {
995617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
996617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
997ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
9985d097056SVladimir Davydov 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
99920c2df83SPaul Mundt 					     init_once);
1000617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1001ac27a0ecSDave Kleikamp 		return -ENOMEM;
1002ac27a0ecSDave Kleikamp 	return 0;
1003ac27a0ecSDave Kleikamp }
1004ac27a0ecSDave Kleikamp 
1005ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1006ac27a0ecSDave Kleikamp {
10078c0a8537SKirill A. Shutemov 	/*
10088c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
10098c0a8537SKirill A. Shutemov 	 * destroy cache.
10108c0a8537SKirill A. Shutemov 	 */
10118c0a8537SKirill A. Shutemov 	rcu_barrier();
1012617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1013ac27a0ecSDave Kleikamp }
1014ac27a0ecSDave Kleikamp 
10150930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1016ac27a0ecSDave Kleikamp {
10170930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1018dbd5768fSJan Kara 	clear_inode(inode);
10199f754758SChristoph Hellwig 	dquot_drop(inode);
1020c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
102151865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
10228aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
10238aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
10248aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
10258aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
10268aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
10278aefcd55STheodore Ts'o 	}
1028b7236e21STheodore Ts'o #ifdef CONFIG_EXT4_FS_ENCRYPTION
1029a7550b30SJaegeuk Kim 	fscrypt_put_encryption_info(inode, NULL);
1030b7236e21STheodore Ts'o #endif
1031ac27a0ecSDave Kleikamp }
1032ac27a0ecSDave Kleikamp 
10331b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
10341b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1035ac27a0ecSDave Kleikamp {
1036ac27a0ecSDave Kleikamp 	struct inode *inode;
1037ac27a0ecSDave Kleikamp 
1038617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1039ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1040617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1041ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1042ac27a0ecSDave Kleikamp 
1043ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1044ac27a0ecSDave Kleikamp 	 *
1045617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1046ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1047ac27a0ecSDave Kleikamp 	 *
1048ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1049ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1050ac27a0ecSDave Kleikamp 	 */
1051f4bb2981STheodore Ts'o 	inode = ext4_iget_normal(sb, ino);
10521d1fe1eeSDavid Howells 	if (IS_ERR(inode))
10531d1fe1eeSDavid Howells 		return ERR_CAST(inode);
10541d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1055ac27a0ecSDave Kleikamp 		iput(inode);
1056ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1057ac27a0ecSDave Kleikamp 	}
10581b961ac0SChristoph Hellwig 
10591b961ac0SChristoph Hellwig 	return inode;
1060ac27a0ecSDave Kleikamp }
10611b961ac0SChristoph Hellwig 
10621b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
10631b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10641b961ac0SChristoph Hellwig {
10651b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
10661b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
10671b961ac0SChristoph Hellwig }
10681b961ac0SChristoph Hellwig 
10691b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
10701b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10711b961ac0SChristoph Hellwig {
10721b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
10731b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1074ac27a0ecSDave Kleikamp }
1075ac27a0ecSDave Kleikamp 
1076c39a7f84SToshiyuki Okajima /*
1077c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1078c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1079c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1080c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1081c39a7f84SToshiyuki Okajima  */
10820b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
10830b8e58a1SAndreas Dilger 				 gfp_t wait)
1084c39a7f84SToshiyuki Okajima {
1085c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1086c39a7f84SToshiyuki Okajima 
1087c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1088c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1089c39a7f84SToshiyuki Okajima 		return 0;
1090c39a7f84SToshiyuki Okajima 	if (journal)
1091c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1092d0164adcSMel Gorman 						wait & ~__GFP_DIRECT_RECLAIM);
1093c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1094c39a7f84SToshiyuki Okajima }
1095c39a7f84SToshiyuki Okajima 
1096a7550b30SJaegeuk Kim #ifdef CONFIG_EXT4_FS_ENCRYPTION
1097a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1098a7550b30SJaegeuk Kim {
1099a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1100a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1101a7550b30SJaegeuk Kim }
1102a7550b30SJaegeuk Kim 
1103a7550b30SJaegeuk Kim static int ext4_key_prefix(struct inode *inode, u8 **key)
1104a7550b30SJaegeuk Kim {
1105a7550b30SJaegeuk Kim 	*key = EXT4_SB(inode->i_sb)->key_prefix;
1106a7550b30SJaegeuk Kim 	return EXT4_SB(inode->i_sb)->key_prefix_size;
1107a7550b30SJaegeuk Kim }
1108a7550b30SJaegeuk Kim 
1109a7550b30SJaegeuk Kim static int ext4_prepare_context(struct inode *inode)
1110a7550b30SJaegeuk Kim {
1111a7550b30SJaegeuk Kim 	return ext4_convert_inline_data(inode);
1112a7550b30SJaegeuk Kim }
1113a7550b30SJaegeuk Kim 
1114a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1115a7550b30SJaegeuk Kim 							void *fs_data)
1116a7550b30SJaegeuk Kim {
1117a7550b30SJaegeuk Kim 	handle_t *handle;
1118a7550b30SJaegeuk Kim 	int res, res2;
1119a7550b30SJaegeuk Kim 
1120a7550b30SJaegeuk Kim 	/* fs_data is null when internally used. */
1121a7550b30SJaegeuk Kim 	if (fs_data) {
1122a7550b30SJaegeuk Kim 		res  = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1123a7550b30SJaegeuk Kim 				EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx,
1124a7550b30SJaegeuk Kim 				len, 0);
1125a7550b30SJaegeuk Kim 		if (!res) {
1126a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1127a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1128a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1129a7550b30SJaegeuk Kim 		}
1130a7550b30SJaegeuk Kim 		return res;
1131a7550b30SJaegeuk Kim 	}
1132a7550b30SJaegeuk Kim 
1133a7550b30SJaegeuk Kim 	handle = ext4_journal_start(inode, EXT4_HT_MISC,
1134a7550b30SJaegeuk Kim 			ext4_jbd2_credits_xattr(inode));
1135a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1136a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1137a7550b30SJaegeuk Kim 
1138a7550b30SJaegeuk Kim 	res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1139a7550b30SJaegeuk Kim 			EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx,
1140a7550b30SJaegeuk Kim 			len, 0);
1141a7550b30SJaegeuk Kim 	if (!res) {
1142a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1143a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1144a7550b30SJaegeuk Kim 		if (res)
1145a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1146a7550b30SJaegeuk Kim 	}
1147a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
1148a7550b30SJaegeuk Kim 	if (!res)
1149a7550b30SJaegeuk Kim 		res = res2;
1150a7550b30SJaegeuk Kim 	return res;
1151a7550b30SJaegeuk Kim }
1152a7550b30SJaegeuk Kim 
1153a7550b30SJaegeuk Kim static int ext4_dummy_context(struct inode *inode)
1154a7550b30SJaegeuk Kim {
1155a7550b30SJaegeuk Kim 	return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
1156a7550b30SJaegeuk Kim }
1157a7550b30SJaegeuk Kim 
1158a7550b30SJaegeuk Kim static unsigned ext4_max_namelen(struct inode *inode)
1159a7550b30SJaegeuk Kim {
1160a7550b30SJaegeuk Kim 	return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
1161a7550b30SJaegeuk Kim 		EXT4_NAME_LEN;
1162a7550b30SJaegeuk Kim }
1163a7550b30SJaegeuk Kim 
1164a7550b30SJaegeuk Kim static struct fscrypt_operations ext4_cryptops = {
1165a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1166a7550b30SJaegeuk Kim 	.key_prefix		= ext4_key_prefix,
1167a7550b30SJaegeuk Kim 	.prepare_context	= ext4_prepare_context,
1168a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1169a7550b30SJaegeuk Kim 	.dummy_context		= ext4_dummy_context,
1170a7550b30SJaegeuk Kim 	.is_encrypted		= ext4_encrypted_inode,
1171a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1172a7550b30SJaegeuk Kim 	.max_namelen		= ext4_max_namelen,
1173a7550b30SJaegeuk Kim };
1174a7550b30SJaegeuk Kim #else
1175a7550b30SJaegeuk Kim static struct fscrypt_operations ext4_cryptops = {
1176a7550b30SJaegeuk Kim 	.is_encrypted		= ext4_encrypted_inode,
1177a7550b30SJaegeuk Kim };
1178a7550b30SJaegeuk Kim #endif
1179a7550b30SJaegeuk Kim 
1180ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1181689c958cSLi Xi static char *quotatypes[] = INITQFNAMES;
1182689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1183ac27a0ecSDave Kleikamp 
1184617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1185617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1186617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1187617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1188617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
11896f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1190f00c9e44SJan Kara 			 struct path *path);
1191ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
1192617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1193617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1194ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1195617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1196ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
11977c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
11987c319d32SAditya Kali 			     unsigned int flags);
11997c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
12008f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid);
1201ac27a0ecSDave Kleikamp 
120296c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
120396c7e0d9SJan Kara {
120496c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
120596c7e0d9SJan Kara }
120696c7e0d9SJan Kara 
120761e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
120860e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1209617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1210617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1211617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1212617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1213a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1214a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1215a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1216040cb378SLi Xi 	.get_projid	= ext4_get_projid,
12178f0e8746STheodore Ts'o 	.get_next_id	= ext4_get_next_id,
1218ac27a0ecSDave Kleikamp };
1219ac27a0ecSDave Kleikamp 
12200d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1221617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1222ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1223287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
12240a240339SJan Kara 	.get_state	= dquot_get_state,
1225287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1226287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
12276332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
12286332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1229ac27a0ecSDave Kleikamp };
1230ac27a0ecSDave Kleikamp #endif
1231ac27a0ecSDave Kleikamp 
1232ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1233617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1234617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1235617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1236617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
12377ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
12380930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1239617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1240617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1241c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1242c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1243617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1244617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1245617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1246ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1247617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1248617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
124996c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1250ac27a0ecSDave Kleikamp #endif
1251c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1252ac27a0ecSDave Kleikamp };
1253ac27a0ecSDave Kleikamp 
125439655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
12551b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
12561b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1257617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1258ac27a0ecSDave Kleikamp };
1259ac27a0ecSDave Kleikamp 
1260ac27a0ecSDave Kleikamp enum {
1261ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1262ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
126372578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1264ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
126572578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1266ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1267ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1268ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
12696ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1270ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
12715a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1272ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
127349da9392SJan Kara 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
12741449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
1275a26f4992STheodore Ts'o 	Opt_lazytime, Opt_nolazytime,
12761449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
12775328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1278744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1279fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1280c6d3d56dSDarrick J. Wong 	Opt_max_dir_size_kb, Opt_nojournal_checksum,
1281ac27a0ecSDave Kleikamp };
1282ac27a0ecSDave Kleikamp 
1283a447c093SSteven Whitehouse static const match_table_t tokens = {
1284ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1285ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1286ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1287ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1288ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1289ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1290ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1291ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1292ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1293ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1294ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1295ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1296ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1297ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
129872578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
129972578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1300ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1301ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1302ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1303ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1304e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
13055a916be1STheodore Ts'o 	{Opt_noload, "noload"},
130672578c33STheodore Ts'o 	{Opt_removed, "nobh"},
130772578c33STheodore Ts'o 	{Opt_removed, "bh"},
1308ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
130930773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
131030773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1311ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1312ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1313818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1314c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1315818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1316ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1317ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1318ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1319ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
13205bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
13215bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1322ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1323ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1324ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1325ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1326ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1327ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
13285a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1329ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1330ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1331ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1332ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
133349da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1334ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
133506705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
133606705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
133725ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1338923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1339c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
134064769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1341a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1342a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1343dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
134436ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
134536ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
13466fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
13476fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1348240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1349b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1350afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
135106705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
135206705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1353744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1354744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
13555328e635SEric Sandeen 	{Opt_discard, "discard"},
13565328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1357fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1358fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1359fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1360df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
13616ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1362c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1363c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1364c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1365c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1366c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1367f3f12faaSJosef Bacik 	{Opt_err, NULL},
1368ac27a0ecSDave Kleikamp };
1369ac27a0ecSDave Kleikamp 
1370617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1371ac27a0ecSDave Kleikamp {
1372617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1373ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1374ac27a0ecSDave Kleikamp 
1375ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1376ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
13770b8e58a1SAndreas Dilger 
1378ac27a0ecSDave Kleikamp 	options += 3;
13790b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1380ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1381ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
13824776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1383ac27a0ecSDave Kleikamp 		       (char *) *data);
1384ac27a0ecSDave Kleikamp 		return 1;
1385ac27a0ecSDave Kleikamp 	}
1386ac27a0ecSDave Kleikamp 	if (*options == ',')
1387ac27a0ecSDave Kleikamp 		options++;
1388ac27a0ecSDave Kleikamp 	*data = (void *) options;
13890b8e58a1SAndreas Dilger 
1390ac27a0ecSDave Kleikamp 	return sb_block;
1391ac27a0ecSDave Kleikamp }
1392ac27a0ecSDave Kleikamp 
1393b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1394437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1395437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1396b3881f74STheodore Ts'o 
139756c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
139856c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
139956c50f11SDmitry Monakhov {
140056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
140156c50f11SDmitry Monakhov 	char *qname;
140203dafb5fSChen Gang 	int ret = -1;
140356c50f11SDmitry Monakhov 
140456c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
140556c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
140656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
140756c50f11SDmitry Monakhov 			"Cannot change journaled "
140856c50f11SDmitry Monakhov 			"quota options when quota turned on");
140957f73c2cSTheodore Ts'o 		return -1;
141056c50f11SDmitry Monakhov 	}
1411e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1412c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1413c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1414c325a67cSTheodore Ts'o 		return 1;
1415262b4662SJan Kara 	}
141656c50f11SDmitry Monakhov 	qname = match_strdup(args);
141756c50f11SDmitry Monakhov 	if (!qname) {
141856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
141956c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
142057f73c2cSTheodore Ts'o 		return -1;
142156c50f11SDmitry Monakhov 	}
142203dafb5fSChen Gang 	if (sbi->s_qf_names[qtype]) {
142303dafb5fSChen Gang 		if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
142403dafb5fSChen Gang 			ret = 1;
142503dafb5fSChen Gang 		else
142656c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
142703dafb5fSChen Gang 				 "%s quota file already specified",
142803dafb5fSChen Gang 				 QTYPE2NAME(qtype));
142903dafb5fSChen Gang 		goto errout;
143056c50f11SDmitry Monakhov 	}
143103dafb5fSChen Gang 	if (strchr(qname, '/')) {
143256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
143356c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
143403dafb5fSChen Gang 		goto errout;
143556c50f11SDmitry Monakhov 	}
143603dafb5fSChen Gang 	sbi->s_qf_names[qtype] = qname;
1437fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
143856c50f11SDmitry Monakhov 	return 1;
143903dafb5fSChen Gang errout:
144003dafb5fSChen Gang 	kfree(qname);
144103dafb5fSChen Gang 	return ret;
144256c50f11SDmitry Monakhov }
144356c50f11SDmitry Monakhov 
144456c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
144556c50f11SDmitry Monakhov {
144656c50f11SDmitry Monakhov 
144756c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
144856c50f11SDmitry Monakhov 
144956c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
145056c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
145156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
145256c50f11SDmitry Monakhov 			" when quota turned on");
145357f73c2cSTheodore Ts'o 		return -1;
145456c50f11SDmitry Monakhov 	}
145503dafb5fSChen Gang 	kfree(sbi->s_qf_names[qtype]);
145656c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
145756c50f11SDmitry Monakhov 	return 1;
145856c50f11SDmitry Monakhov }
145956c50f11SDmitry Monakhov #endif
146056c50f11SDmitry Monakhov 
146126092bf5STheodore Ts'o #define MOPT_SET	0x0001
146226092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
146326092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
146426092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
146526092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
146626092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
146726092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
146826092bf5STheodore Ts'o #define MOPT_Q		0
146926092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
147026092bf5STheodore Ts'o #else
147126092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
147226092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
147326092bf5STheodore Ts'o #endif
147426092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
14758dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
14768dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
14778dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1478ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
147926092bf5STheodore Ts'o 
148026092bf5STheodore Ts'o static const struct mount_opts {
148126092bf5STheodore Ts'o 	int	token;
148226092bf5STheodore Ts'o 	int	mount_opt;
148326092bf5STheodore Ts'o 	int	flags;
148426092bf5STheodore Ts'o } ext4_mount_opts[] = {
148526092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
148626092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
148726092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
148826092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
148926092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
149026092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
14918dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
14928dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
14938dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
14948dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
149526092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
149626092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
14978dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
14988dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
14998dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
150059d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1501c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1502c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
15038dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
15041e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
150526092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
15068dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
15071e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
15088dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
150926092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
151026092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
151126092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
15128dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
15137915a861SAles Novak 	 MOPT_NO_EXT2},
15148dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
15157915a861SAles Novak 	 MOPT_NO_EXT2},
151626092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
151726092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
151826092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
151926092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
152026092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
152126092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
152226092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
152326092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
152426092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
152526092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1526923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
152726092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
15280efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
15290efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
15305ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
15315ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
15325ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
15338dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
15348dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
15358dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
15368dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
153726092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
153826092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
153926092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
154026092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
154126092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
154226092bf5STheodore Ts'o #else
154326092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
154426092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
154526092bf5STheodore Ts'o #endif
154626092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
154726092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
154826092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
154926092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
155026092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
155126092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
155226092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
155349da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
155449da9392SJan Kara 							MOPT_SET | MOPT_Q},
155526092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
155649da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
155749da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
155826092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
155926092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
156026092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
156126092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
156226092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
156326092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
156426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1565df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
15666ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
156726092bf5STheodore Ts'o 	{Opt_err, 0, 0}
156826092bf5STheodore Ts'o };
156926092bf5STheodore Ts'o 
157026092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
157126092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
157226092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
157326092bf5STheodore Ts'o {
157426092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
157526092bf5STheodore Ts'o 	const struct mount_opts *m;
157608cefc7aSEric W. Biederman 	kuid_t uid;
157708cefc7aSEric W. Biederman 	kgid_t gid;
157826092bf5STheodore Ts'o 	int arg = 0;
157926092bf5STheodore Ts'o 
158057f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
158157f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
158257f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
158357f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
158457f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
158557f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
158657f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
158757f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
158857f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
158957f73c2cSTheodore Ts'o #endif
159026092bf5STheodore Ts'o 	switch (token) {
1591f7048605STheodore Ts'o 	case Opt_noacl:
1592f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1593f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1594f7048605STheodore Ts'o 		break;
159526092bf5STheodore Ts'o 	case Opt_sb:
159626092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
159726092bf5STheodore Ts'o 	case Opt_removed:
15985f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
159926092bf5STheodore Ts'o 		return 1;
160026092bf5STheodore Ts'o 	case Opt_abort:
160126092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
160226092bf5STheodore Ts'o 		return 1;
160326092bf5STheodore Ts'o 	case Opt_i_version:
160426092bf5STheodore Ts'o 		sb->s_flags |= MS_I_VERSION;
160526092bf5STheodore Ts'o 		return 1;
1606a26f4992STheodore Ts'o 	case Opt_lazytime:
1607a26f4992STheodore Ts'o 		sb->s_flags |= MS_LAZYTIME;
1608a26f4992STheodore Ts'o 		return 1;
1609a26f4992STheodore Ts'o 	case Opt_nolazytime:
1610a26f4992STheodore Ts'o 		sb->s_flags &= ~MS_LAZYTIME;
1611a26f4992STheodore Ts'o 		return 1;
161226092bf5STheodore Ts'o 	}
161326092bf5STheodore Ts'o 
16145f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
16155f3633e3SJan Kara 		if (token == m->token)
16165f3633e3SJan Kara 			break;
16175f3633e3SJan Kara 
16185f3633e3SJan Kara 	if (m->token == Opt_err) {
16195f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
16205f3633e3SJan Kara 			 "or missing value", opt);
16215f3633e3SJan Kara 		return -1;
16225f3633e3SJan Kara 	}
16235f3633e3SJan Kara 
16248dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
16258dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
16268dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
16278dc0aa8cSTheodore Ts'o 		return -1;
16288dc0aa8cSTheodore Ts'o 	}
16298dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
16308dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
16318dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
16328dc0aa8cSTheodore Ts'o 		return -1;
16338dc0aa8cSTheodore Ts'o 	}
16348dc0aa8cSTheodore Ts'o 
1635ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
16360efb3b23SJan Kara 		return -1;
163726092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
163826092bf5STheodore Ts'o 		return -1;
1639c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1640c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
164126092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
16421e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
16431e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1644c93cf2d7SDmitry Monakhov 		} else
1645c93cf2d7SDmitry Monakhov 			return -1;
1646c93cf2d7SDmitry Monakhov 	}
164726092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
164826092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
164926092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
165026092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
165126092bf5STheodore Ts'o 			 "options when quota turned on");
165226092bf5STheodore Ts'o 		return -1;
165326092bf5STheodore Ts'o 	}
165426092bf5STheodore Ts'o 
165526092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
165626092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
165726092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
165826092bf5STheodore Ts'o 		if (arg == 0)
165926092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
166026092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
166126092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
166226092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
166326092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
166426092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
166526092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1666e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1667e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1668e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1669e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
167026092bf5STheodore Ts'o 			return -1;
167126092bf5STheodore Ts'o 		}
167226092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
167326092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
167426092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
167526092bf5STheodore Ts'o 		if (!args->from)
167626092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
167726092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1678df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1679df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
168026092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
168126092bf5STheodore Ts'o 		sbi->s_stripe = arg;
16820efb3b23SJan Kara 	} else if (token == Opt_resuid) {
16830efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
16840efb3b23SJan Kara 		if (!uid_valid(uid)) {
16855f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
16860efb3b23SJan Kara 			return -1;
16870efb3b23SJan Kara 		}
16880efb3b23SJan Kara 		sbi->s_resuid = uid;
16890efb3b23SJan Kara 	} else if (token == Opt_resgid) {
16900efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
16910efb3b23SJan Kara 		if (!gid_valid(gid)) {
16925f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
16930efb3b23SJan Kara 			return -1;
16940efb3b23SJan Kara 		}
16950efb3b23SJan Kara 		sbi->s_resgid = gid;
16960efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
16970efb3b23SJan Kara 		if (is_remount) {
16980efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
16990efb3b23SJan Kara 				 "Cannot specify journal on remount");
17000efb3b23SJan Kara 			return -1;
17010efb3b23SJan Kara 		}
17020efb3b23SJan Kara 		*journal_devnum = arg;
1703ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1704ad4eec61SEric Sandeen 		char *journal_path;
1705ad4eec61SEric Sandeen 		struct inode *journal_inode;
1706ad4eec61SEric Sandeen 		struct path path;
1707ad4eec61SEric Sandeen 		int error;
1708ad4eec61SEric Sandeen 
1709ad4eec61SEric Sandeen 		if (is_remount) {
1710ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1711ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
1712ad4eec61SEric Sandeen 			return -1;
1713ad4eec61SEric Sandeen 		}
1714ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
1715ad4eec61SEric Sandeen 		if (!journal_path) {
1716ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1717ad4eec61SEric Sandeen 				"journal device string");
1718ad4eec61SEric Sandeen 			return -1;
1719ad4eec61SEric Sandeen 		}
1720ad4eec61SEric Sandeen 
1721ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1722ad4eec61SEric Sandeen 		if (error) {
1723ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
1724ad4eec61SEric Sandeen 				"journal device path: error %d", error);
1725ad4eec61SEric Sandeen 			kfree(journal_path);
1726ad4eec61SEric Sandeen 			return -1;
1727ad4eec61SEric Sandeen 		}
1728ad4eec61SEric Sandeen 
17292b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
1730ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
1731ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1732ad4eec61SEric Sandeen 				"is not a block device", journal_path);
1733ad4eec61SEric Sandeen 			path_put(&path);
1734ad4eec61SEric Sandeen 			kfree(journal_path);
1735ad4eec61SEric Sandeen 			return -1;
1736ad4eec61SEric Sandeen 		}
1737ad4eec61SEric Sandeen 
1738ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1739ad4eec61SEric Sandeen 		path_put(&path);
1740ad4eec61SEric Sandeen 		kfree(journal_path);
17410efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
17420efb3b23SJan Kara 		if (arg > 7) {
17435f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
17440efb3b23SJan Kara 				 " (must be 0-7)");
17450efb3b23SJan Kara 			return -1;
17460efb3b23SJan Kara 		}
17470efb3b23SJan Kara 		*journal_ioprio =
17480efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
17496ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
17506ddb2447STheodore Ts'o #ifdef CONFIG_EXT4_FS_ENCRYPTION
17516ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
17526ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
17536ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
17546ddb2447STheodore Ts'o #else
17556ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
17566ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
17576ddb2447STheodore Ts'o #endif
175826092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
175926092bf5STheodore Ts'o 		if (is_remount) {
176026092bf5STheodore Ts'o 			if (!sbi->s_journal)
176126092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
17625f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
176326092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
176426092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
176526092bf5STheodore Ts'o 				return -1;
176626092bf5STheodore Ts'o 			}
176726092bf5STheodore Ts'o 		} else {
176826092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
176926092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
177026092bf5STheodore Ts'o 		}
177126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
177226092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
177326092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
177426092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
17755f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
17765f3633e3SJan Kara 				 "quota options when quota turned on");
177726092bf5STheodore Ts'o 			return -1;
177826092bf5STheodore Ts'o 		}
1779e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
1780c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
1781c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
1782262b4662SJan Kara 				 "when QUOTA feature is enabled");
1783c325a67cSTheodore Ts'o 			return 1;
1784262b4662SJan Kara 		}
178526092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
178626092bf5STheodore Ts'o #endif
1787923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
1788ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
1789ef83b6e8SDan Williams 		ext4_msg(sb, KERN_WARNING,
1790ef83b6e8SDan Williams 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
1791ef83b6e8SDan Williams 			sbi->s_mount_opt |= m->mount_opt;
1792ef83b6e8SDan Williams #else
1793923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
1794923ae0ffSRoss Zwisler 		return -1;
1795923ae0ffSRoss Zwisler #endif
17967915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
17977915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
17987915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
17997915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
180026092bf5STheodore Ts'o 	} else {
180126092bf5STheodore Ts'o 		if (!args->from)
180226092bf5STheodore Ts'o 			arg = 1;
180326092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
180426092bf5STheodore Ts'o 			arg = !arg;
180526092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
180626092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
180726092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
180826092bf5STheodore Ts'o 			WARN_ON(1);
180926092bf5STheodore Ts'o 			return -1;
181026092bf5STheodore Ts'o 		}
181126092bf5STheodore Ts'o 		if (arg != 0)
181226092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
181326092bf5STheodore Ts'o 		else
181426092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
181526092bf5STheodore Ts'o 	}
181626092bf5STheodore Ts'o 	return 1;
181726092bf5STheodore Ts'o }
181826092bf5STheodore Ts'o 
1819ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1820c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1821b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1822661aa520SEric Sandeen 			 int is_remount)
1823ac27a0ecSDave Kleikamp {
1824617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1825ac27a0ecSDave Kleikamp 	char *p;
1826ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
182726092bf5STheodore Ts'o 	int token;
1828ac27a0ecSDave Kleikamp 
1829ac27a0ecSDave Kleikamp 	if (!options)
1830ac27a0ecSDave Kleikamp 		return 1;
1831ac27a0ecSDave Kleikamp 
1832ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1833ac27a0ecSDave Kleikamp 		if (!*p)
1834ac27a0ecSDave Kleikamp 			continue;
183515121c18SEric Sandeen 		/*
183615121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
183715121c18SEric Sandeen 		 * found; some options take optional arguments.
183815121c18SEric Sandeen 		 */
1839caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
1840ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
184126092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
184226092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
1843ac27a0ecSDave Kleikamp 			return 0;
1844ac27a0ecSDave Kleikamp 	}
1845ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
184649da9392SJan Kara 	/*
184749da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
184849da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
184949da9392SJan Kara 	 * to support legacy quotas in quota files.
185049da9392SJan Kara 	 */
185149da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
185249da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
185349da9392SJan Kara 			 "Cannot enable project quota enforcement.");
185449da9392SJan Kara 		return 0;
185549da9392SJan Kara 	}
185649da9392SJan Kara 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1857482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1858fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1859ac27a0ecSDave Kleikamp 
1860482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1861fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1862ac27a0ecSDave Kleikamp 
186356c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1864b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1865b31e1552SEric Sandeen 					"format mixing");
1866ac27a0ecSDave Kleikamp 			return 0;
1867ac27a0ecSDave Kleikamp 		}
1868ac27a0ecSDave Kleikamp 
1869ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1870b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1871b31e1552SEric Sandeen 					"not specified");
1872ac27a0ecSDave Kleikamp 			return 0;
1873ac27a0ecSDave Kleikamp 		}
1874ac27a0ecSDave Kleikamp 	}
1875ac27a0ecSDave Kleikamp #endif
1876261cb20cSJan Kara 	if (test_opt(sb, DIOREAD_NOLOCK)) {
1877261cb20cSJan Kara 		int blocksize =
1878261cb20cSJan Kara 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
1879261cb20cSJan Kara 
188009cbfeafSKirill A. Shutemov 		if (blocksize < PAGE_SIZE) {
1881261cb20cSJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
1882261cb20cSJan Kara 				 "dioread_nolock if block size != PAGE_SIZE");
1883261cb20cSJan Kara 			return 0;
1884261cb20cSJan Kara 		}
1885261cb20cSJan Kara 	}
1886d4f76107SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
1887d4f76107SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
1888d4f76107SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with journal_async_commit "
1889d4f76107SJan Kara 			 "in data=ordered mode");
1890d4f76107SJan Kara 		return 0;
1891d4f76107SJan Kara 	}
1892ac27a0ecSDave Kleikamp 	return 1;
1893ac27a0ecSDave Kleikamp }
1894ac27a0ecSDave Kleikamp 
18952adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
18962adf6da8STheodore Ts'o 					   struct super_block *sb)
18972adf6da8STheodore Ts'o {
18982adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
18992adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
19002adf6da8STheodore Ts'o 
19012adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
19022adf6da8STheodore Ts'o 		char *fmtname = "";
19032adf6da8STheodore Ts'o 
19042adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
19052adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
19062adf6da8STheodore Ts'o 			fmtname = "vfsold";
19072adf6da8STheodore Ts'o 			break;
19082adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
19092adf6da8STheodore Ts'o 			fmtname = "vfsv0";
19102adf6da8STheodore Ts'o 			break;
19112adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
19122adf6da8STheodore Ts'o 			fmtname = "vfsv1";
19132adf6da8STheodore Ts'o 			break;
19142adf6da8STheodore Ts'o 		}
19152adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
19162adf6da8STheodore Ts'o 	}
19172adf6da8STheodore Ts'o 
19182adf6da8STheodore Ts'o 	if (sbi->s_qf_names[USRQUOTA])
1919a068acf2SKees Cook 		seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
19202adf6da8STheodore Ts'o 
19212adf6da8STheodore Ts'o 	if (sbi->s_qf_names[GRPQUOTA])
1922a068acf2SKees Cook 		seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
19232adf6da8STheodore Ts'o #endif
19242adf6da8STheodore Ts'o }
19252adf6da8STheodore Ts'o 
19265a916be1STheodore Ts'o static const char *token2str(int token)
19275a916be1STheodore Ts'o {
192850df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
19295a916be1STheodore Ts'o 
19305a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
19315a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
19325a916be1STheodore Ts'o 			break;
19335a916be1STheodore Ts'o 	return t->pattern;
19345a916be1STheodore Ts'o }
19355a916be1STheodore Ts'o 
19362adf6da8STheodore Ts'o /*
19372adf6da8STheodore Ts'o  * Show an option if
19382adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
19392adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
19402adf6da8STheodore Ts'o  */
194166acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
194266acdcf4STheodore Ts'o 			      int nodefs)
19432adf6da8STheodore Ts'o {
19442adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
19452adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
194666acdcf4STheodore Ts'o 	int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
19475a916be1STheodore Ts'o 	const struct mount_opts *m;
194866acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
19492adf6da8STheodore Ts'o 
195066acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
195166acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
19522adf6da8STheodore Ts'o 
19532adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
19545a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
19555a916be1STheodore Ts'o 
19565a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
19575a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
19585a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
19595a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
19605a916be1STheodore Ts'o 			continue;
196166acdcf4STheodore Ts'o 		if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
19625a916be1STheodore Ts'o 			continue; /* skip if same as the default */
19635a916be1STheodore Ts'o 		if ((want_set &&
19645a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
19655a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
19665a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
19675a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
19685a916be1STheodore Ts'o 	}
19695a916be1STheodore Ts'o 
197008cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
19715a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
197208cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
197308cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
197408cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
19755a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
197608cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
197708cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
197866acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
19795a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
19805a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
19812adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
19825a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
19832adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
19845a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
198566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
19865a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
198766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
19885a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
198966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
19905a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
19912adf6da8STheodore Ts'o 	if (sb->s_flags & MS_I_VERSION)
19925a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
199366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
19945a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
199566acdcf4STheodore Ts'o 	if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
19962adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
19975a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
19982adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
19995a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
20002adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
20015a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
20025a916be1STheodore Ts'o 	}
200366acdcf4STheodore Ts'o 	if (nodefs ||
200466acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
20055a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
20062adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
20072adf6da8STheodore Ts'o 
200866acdcf4STheodore Ts'o 	if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
200966acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
20105a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2011df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2012df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
20137915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
20147915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
20152adf6da8STheodore Ts'o 
20162adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
20172adf6da8STheodore Ts'o 	return 0;
20182adf6da8STheodore Ts'o }
20192adf6da8STheodore Ts'o 
202066acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
202166acdcf4STheodore Ts'o {
202266acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
202366acdcf4STheodore Ts'o }
202466acdcf4STheodore Ts'o 
2025ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
202666acdcf4STheodore Ts'o {
202766acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
202866acdcf4STheodore Ts'o 	int rc;
202966acdcf4STheodore Ts'o 
203066acdcf4STheodore Ts'o 	seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
203166acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
203266acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
203366acdcf4STheodore Ts'o 	return rc;
203466acdcf4STheodore Ts'o }
203566acdcf4STheodore Ts'o 
2036617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2037ac27a0ecSDave Kleikamp 			    int read_only)
2038ac27a0ecSDave Kleikamp {
2039617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2040ac27a0ecSDave Kleikamp 	int res = 0;
2041ac27a0ecSDave Kleikamp 
2042617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2043b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2044b31e1552SEric Sandeen 			 "forcing read-only mode");
2045ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
2046ac27a0ecSDave Kleikamp 	}
2047ac27a0ecSDave Kleikamp 	if (read_only)
2048281b5995STheodore Ts'o 		goto done;
2049617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2050b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2051b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2052c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2053b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2054b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2055b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2056ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2057ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2058ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2059b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2060b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2061b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2062ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
2063ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
2064ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
2065b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2066b31e1552SEric Sandeen 			 "warning: checktime reached, "
2067b31e1552SEric Sandeen 			 "running e2fsck is recommended");
20680390131bSFrank Mayhar 	if (!sbi->s_journal)
2069216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2070ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2071617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2072e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
2073ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
2074617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
20750390131bSFrank Mayhar 	if (sbi->s_journal)
2076e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2077ac27a0ecSDave Kleikamp 
2078e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
2079281b5995STheodore Ts'o done:
2080ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2081a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2082a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2083ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2084ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2085617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2086617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2087a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2088ac27a0ecSDave Kleikamp 
20897abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2090ac27a0ecSDave Kleikamp 	return res;
2091ac27a0ecSDave Kleikamp }
2092ac27a0ecSDave Kleikamp 
2093117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2094117fff10STheodore Ts'o {
2095117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2096117fff10STheodore Ts'o 	struct flex_groups *new_groups;
2097117fff10STheodore Ts'o 	int size;
2098117fff10STheodore Ts'o 
2099117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2100117fff10STheodore Ts'o 		return 0;
2101117fff10STheodore Ts'o 
2102117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2103117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2104117fff10STheodore Ts'o 		return 0;
2105117fff10STheodore Ts'o 
2106117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
2107117fff10STheodore Ts'o 	new_groups = ext4_kvzalloc(size, GFP_KERNEL);
2108117fff10STheodore Ts'o 	if (!new_groups) {
2109117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
2110117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
2111117fff10STheodore Ts'o 		return -ENOMEM;
2112117fff10STheodore Ts'o 	}
2113117fff10STheodore Ts'o 
2114117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
2115117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
2116117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
2117117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
2118b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
2119117fff10STheodore Ts'o 	}
2120117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
2121117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
2122117fff10STheodore Ts'o 	return 0;
2123117fff10STheodore Ts'o }
2124117fff10STheodore Ts'o 
2125772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2126772cb7c8SJose R. Santos {
2127772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2128772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
2129772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2130117fff10STheodore Ts'o 	int i, err;
2131772cb7c8SJose R. Santos 
2132503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2133d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2134772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2135772cb7c8SJose R. Santos 		return 1;
2136772cb7c8SJose R. Santos 	}
2137772cb7c8SJose R. Santos 
2138117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2139117fff10STheodore Ts'o 	if (err)
2140772cb7c8SJose R. Santos 		goto failed;
2141772cb7c8SJose R. Santos 
2142772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
214388b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2144772cb7c8SJose R. Santos 
2145772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
21467ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
21477ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
214890ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
214924aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
21507ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
21517ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2152772cb7c8SJose R. Santos 	}
2153772cb7c8SJose R. Santos 
2154772cb7c8SJose R. Santos 	return 1;
2155772cb7c8SJose R. Santos failed:
2156772cb7c8SJose R. Santos 	return 0;
2157772cb7c8SJose R. Santos }
2158772cb7c8SJose R. Santos 
2159e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2160717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2161717d50e4SAndreas Dilger {
2162b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2163717d50e4SAndreas Dilger 	__u16 crc = 0;
2164717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2165e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2166717d50e4SAndreas Dilger 
21679aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2168feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2169feb0ab32SDarrick J. Wong 		__u32 csum32;
2170b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2171feb0ab32SDarrick J. Wong 
2172feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2173feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2174b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2175b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2176b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2177b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2178b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2179b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2180b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2181feb0ab32SDarrick J. Wong 
2182feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2183feb0ab32SDarrick J. Wong 		goto out;
2184feb0ab32SDarrick J. Wong 	}
2185feb0ab32SDarrick J. Wong 
2186feb0ab32SDarrick J. Wong 	/* old crc16 code */
2187e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2188813d32f9SDarrick J. Wong 		return 0;
2189813d32f9SDarrick J. Wong 
2190717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2191717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2192717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2193717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2194717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2195e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2196717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2197717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2198717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2199717d50e4SAndreas Dilger 				offset);
2200717d50e4SAndreas Dilger 
2201feb0ab32SDarrick J. Wong out:
2202717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2203717d50e4SAndreas Dilger }
2204717d50e4SAndreas Dilger 
2205feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2206717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2207717d50e4SAndreas Dilger {
2208feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2209e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2210717d50e4SAndreas Dilger 		return 0;
2211717d50e4SAndreas Dilger 
2212717d50e4SAndreas Dilger 	return 1;
2213717d50e4SAndreas Dilger }
2214717d50e4SAndreas Dilger 
2215feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2216feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2217feb0ab32SDarrick J. Wong {
2218feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2219feb0ab32SDarrick J. Wong 		return;
2220e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2221feb0ab32SDarrick J. Wong }
2222feb0ab32SDarrick J. Wong 
2223ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2224bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2225829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2226bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2227ac27a0ecSDave Kleikamp {
2228617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2229617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2230617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2231bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2232bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2233bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2234ce421581SJose R. Santos 	int flexbg_flag = 0;
2235bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2236ac27a0ecSDave Kleikamp 
2237e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2238ce421581SJose R. Santos 		flexbg_flag = 1;
2239ce421581SJose R. Santos 
2240617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2241ac27a0ecSDave Kleikamp 
2242197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2243197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2244197cd65aSAkinobu Mita 
2245ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2246bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2247ac27a0ecSDave Kleikamp 		else
2248ac27a0ecSDave Kleikamp 			last_block = first_block +
2249617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2250ac27a0ecSDave Kleikamp 
2251bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2252bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2253bfff6873SLukas Czerner 			grp = i;
2254bfff6873SLukas Czerner 
22558fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2256829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2257829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2258829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2259829fa70dSTheodore Ts'o 				 "superblock", i);
2260829fa70dSTheodore Ts'o 		}
22612b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2262b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2263a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2264b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2265ac27a0ecSDave Kleikamp 			return 0;
2266ac27a0ecSDave Kleikamp 		}
22678fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2268829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2269829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2270829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2271829fa70dSTheodore Ts'o 				 "superblock", i);
2272829fa70dSTheodore Ts'o 		}
22732b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2274b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2275a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2276b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2277ac27a0ecSDave Kleikamp 			return 0;
2278ac27a0ecSDave Kleikamp 		}
22798fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2280829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2281829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2282829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2283829fa70dSTheodore Ts'o 				 "superblock", i);
2284829fa70dSTheodore Ts'o 		}
2285bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
22862b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2287b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2288a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2289b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2290ac27a0ecSDave Kleikamp 			return 0;
2291ac27a0ecSDave Kleikamp 		}
2292955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2293feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2294b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2295b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2296e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2297fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
22987ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2299955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2300717d50e4SAndreas Dilger 				return 0;
2301717d50e4SAndreas Dilger 			}
23027ee1ec4cSLi Zefan 		}
2303955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2304ce421581SJose R. Santos 		if (!flexbg_flag)
2305617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2306ac27a0ecSDave Kleikamp 	}
2307bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2308bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2309ac27a0ecSDave Kleikamp 	return 1;
2310ac27a0ecSDave Kleikamp }
2311ac27a0ecSDave Kleikamp 
2312617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2313ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2314ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2315ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2316ac27a0ecSDave Kleikamp  *
2317ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2318ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2319ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2320ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2321ac27a0ecSDave Kleikamp  *
2322ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2323ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2324ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2325617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2326ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2327ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2328ac27a0ecSDave Kleikamp  */
2329617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2330617ba13bSMingming Cao 				struct ext4_super_block *es)
2331ac27a0ecSDave Kleikamp {
2332ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
23332c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2334ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2335ac27a0ecSDave Kleikamp 	int i;
2336ac27a0ecSDave Kleikamp #endif
2337ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2338ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2339ac27a0ecSDave Kleikamp 		return;
2340ac27a0ecSDave Kleikamp 	}
2341ac27a0ecSDave Kleikamp 
2342a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2343b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2344b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2345a8f48a95SEric Sandeen 		return;
2346a8f48a95SEric Sandeen 	}
2347a8f48a95SEric Sandeen 
2348d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2349d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2350d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2351d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2352d39195c3SAmir Goldstein 		return;
2353d39195c3SAmir Goldstein 	}
2354d39195c3SAmir Goldstein 
2355617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2356c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
2357c25f9bc6SEric Sandeen 		if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
235884474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2359ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2360ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2361c25f9bc6SEric Sandeen 		}
2362ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2363ac27a0ecSDave Kleikamp 		return;
2364ac27a0ecSDave Kleikamp 	}
2365ac27a0ecSDave Kleikamp 
2366ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2367b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2368ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2369ac27a0ecSDave Kleikamp 	}
2370ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2371ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2372ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2373ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2374a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2375617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2376617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2377ac27a0ecSDave Kleikamp 			if (ret < 0)
2378b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2379b31e1552SEric Sandeen 					"Cannot turn on journaled "
2380b31e1552SEric Sandeen 					"quota: error %d", ret);
2381ac27a0ecSDave Kleikamp 		}
2382ac27a0ecSDave Kleikamp 	}
2383ac27a0ecSDave Kleikamp #endif
2384ac27a0ecSDave Kleikamp 
2385ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2386ac27a0ecSDave Kleikamp 		struct inode *inode;
2387ac27a0ecSDave Kleikamp 
2388c65d5c6cSVegard Nossum 		/*
2389c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2390c65d5c6cSVegard Nossum 		 * so, skip the rest.
2391c65d5c6cSVegard Nossum 		 */
2392c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2393c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2394c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2395c65d5c6cSVegard Nossum 			break;
2396c65d5c6cSVegard Nossum 		}
2397c65d5c6cSVegard Nossum 
239897bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
239997bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2400ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2401ac27a0ecSDave Kleikamp 			break;
2402ac27a0ecSDave Kleikamp 		}
2403ac27a0ecSDave Kleikamp 
2404617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2405871a2931SChristoph Hellwig 		dquot_initialize(inode);
2406ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2407566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2408b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2409b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
241046e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2411e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2412ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
24135955102cSAl Viro 			inode_lock(inode);
241455f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
24152c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
24162c98eb5eSTheodore Ts'o 			if (ret)
24172c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
24185955102cSAl Viro 			inode_unlock(inode);
2419ac27a0ecSDave Kleikamp 			nr_truncates++;
2420ac27a0ecSDave Kleikamp 		} else {
2421566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2422b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2423b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
242446e665e9SHarvey Harrison 					__func__, inode->i_ino);
2425ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2426ac27a0ecSDave Kleikamp 				  inode->i_ino);
2427ac27a0ecSDave Kleikamp 			nr_orphans++;
2428ac27a0ecSDave Kleikamp 		}
2429ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2430ac27a0ecSDave Kleikamp 	}
2431ac27a0ecSDave Kleikamp 
2432ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2433ac27a0ecSDave Kleikamp 
2434ac27a0ecSDave Kleikamp 	if (nr_orphans)
2435b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2436b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2437ac27a0ecSDave Kleikamp 	if (nr_truncates)
2438b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2439b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2440ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2441ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2442a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2443ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2444287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2445ac27a0ecSDave Kleikamp 	}
2446ac27a0ecSDave Kleikamp #endif
2447ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2448ac27a0ecSDave Kleikamp }
24490b8e58a1SAndreas Dilger 
2450cd2291a4SEric Sandeen /*
2451cd2291a4SEric Sandeen  * Maximal extent format file size.
2452cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2453cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2454cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2455cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2456cd2291a4SEric Sandeen  *
2457f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2458f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2459f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2460f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2461f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2462f17722f9SLukas Czerner  *
2463cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2464cd2291a4SEric Sandeen  */
2465f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2466cd2291a4SEric Sandeen {
2467cd2291a4SEric Sandeen 	loff_t res;
2468cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2469cd2291a4SEric Sandeen 
2470cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2471f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2472cd2291a4SEric Sandeen 		/*
247390c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2474cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2475cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2476cd2291a4SEric Sandeen 		 */
2477cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2478cd2291a4SEric Sandeen 
2479cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2480cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2481cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2482cd2291a4SEric Sandeen 	}
2483cd2291a4SEric Sandeen 
2484f17722f9SLukas Czerner 	/*
2485f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2486f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2487f17722f9SLukas Czerner 	 * size
2488f17722f9SLukas Czerner 	 */
2489f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2490cd2291a4SEric Sandeen 	res <<= blkbits;
2491cd2291a4SEric Sandeen 
2492cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2493cd2291a4SEric Sandeen 	if (res > upper_limit)
2494cd2291a4SEric Sandeen 		res = upper_limit;
2495cd2291a4SEric Sandeen 
2496cd2291a4SEric Sandeen 	return res;
2497cd2291a4SEric Sandeen }
2498ac27a0ecSDave Kleikamp 
2499ac27a0ecSDave Kleikamp /*
2500cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
25010fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
25020fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2503ac27a0ecSDave Kleikamp  */
2504f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2505ac27a0ecSDave Kleikamp {
2506617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
25070fc1b451SAneesh Kumar K.V 	int meta_blocks;
25080fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
25090b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
25100b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
25110b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
25120b8e58a1SAndreas Dilger 	 *
25130b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
25140b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
25150fc1b451SAneesh Kumar K.V 	 */
25160fc1b451SAneesh Kumar K.V 
2517f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
25180fc1b451SAneesh Kumar K.V 		/*
251990c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
25200b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
25210b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
25220fc1b451SAneesh Kumar K.V 		 */
25230fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
25240fc1b451SAneesh Kumar K.V 
25250fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
25260fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
25270fc1b451SAneesh Kumar K.V 
25280fc1b451SAneesh Kumar K.V 	} else {
25298180a562SAneesh Kumar K.V 		/*
25308180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
25318180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
25328180a562SAneesh Kumar K.V 		 * represent total number of blocks in
25338180a562SAneesh Kumar K.V 		 * file system block size
25348180a562SAneesh Kumar K.V 		 */
25350fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
25360fc1b451SAneesh Kumar K.V 
25370fc1b451SAneesh Kumar K.V 	}
25380fc1b451SAneesh Kumar K.V 
25390fc1b451SAneesh Kumar K.V 	/* indirect blocks */
25400fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
25410fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
25420fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
25430fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
25440fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
25450fc1b451SAneesh Kumar K.V 
25460fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
25470fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2548ac27a0ecSDave Kleikamp 
2549ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2550ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2551ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2552ac27a0ecSDave Kleikamp 	res <<= bits;
2553ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2554ac27a0ecSDave Kleikamp 		res = upper_limit;
25550fc1b451SAneesh Kumar K.V 
25560fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
25570fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
25580fc1b451SAneesh Kumar K.V 
2559ac27a0ecSDave Kleikamp 	return res;
2560ac27a0ecSDave Kleikamp }
2561ac27a0ecSDave Kleikamp 
2562617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
256370bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2564ac27a0ecSDave Kleikamp {
2565617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2566fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2567ac27a0ecSDave Kleikamp 	int has_super = 0;
2568ac27a0ecSDave Kleikamp 
2569ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2570ac27a0ecSDave Kleikamp 
2571e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
257270bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2573ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2574617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2575ac27a0ecSDave Kleikamp 		has_super = 1;
25760b8e58a1SAndreas Dilger 
2577bd63f6b0SDarrick J. Wong 	/*
2578bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2579bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2580bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2581bd63f6b0SDarrick J. Wong 	 * compensate.
2582bd63f6b0SDarrick J. Wong 	 */
2583bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
2584bd63f6b0SDarrick J. Wong 	    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) == 0)
2585bd63f6b0SDarrick J. Wong 		has_super++;
2586bd63f6b0SDarrick J. Wong 
2587617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2588ac27a0ecSDave Kleikamp }
2589ac27a0ecSDave Kleikamp 
2590c9de560dSAlex Tomas /**
2591c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2592c9de560dSAlex Tomas  * @sbi: In memory super block info
2593c9de560dSAlex Tomas  *
2594c9de560dSAlex Tomas  * If we have specified it via mount option, then
2595c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2596c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2597c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2598c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2599c9de560dSAlex Tomas  *
2600c9de560dSAlex Tomas  */
2601c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2602c9de560dSAlex Tomas {
2603c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2604c9de560dSAlex Tomas 	unsigned long stripe_width =
2605c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
26063eb08658SDan Ehrenberg 	int ret;
2607c9de560dSAlex Tomas 
2608c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
26093eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
26103eb08658SDan Ehrenberg 	else if (stripe_width <= sbi->s_blocks_per_group)
26113eb08658SDan Ehrenberg 		ret = stripe_width;
26123eb08658SDan Ehrenberg 	else if (stride <= sbi->s_blocks_per_group)
26133eb08658SDan Ehrenberg 		ret = stride;
26143eb08658SDan Ehrenberg 	else
26153eb08658SDan Ehrenberg 		ret = 0;
2616c9de560dSAlex Tomas 
26173eb08658SDan Ehrenberg 	/*
26183eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
26193eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
26203eb08658SDan Ehrenberg 	 */
26213eb08658SDan Ehrenberg 	if (ret <= 1)
26223eb08658SDan Ehrenberg 		ret = 0;
2623c9de560dSAlex Tomas 
26243eb08658SDan Ehrenberg 	return ret;
2625c9de560dSAlex Tomas }
2626ac27a0ecSDave Kleikamp 
2627a13fb1a4SEric Sandeen /*
2628a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2629a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2630a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2631a13fb1a4SEric Sandeen  * 0 if it cannot be.
2632a13fb1a4SEric Sandeen  */
2633a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2634a13fb1a4SEric Sandeen {
2635e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2636a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2637a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2638a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2639a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2640a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2641a13fb1a4SEric Sandeen 		return 0;
2642a13fb1a4SEric Sandeen 	}
2643a13fb1a4SEric Sandeen 
2644a13fb1a4SEric Sandeen 	if (readonly)
2645a13fb1a4SEric Sandeen 		return 1;
2646a13fb1a4SEric Sandeen 
2647e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
26482cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
26492cb5cc8bSDarrick J. Wong 		sb->s_flags |= MS_RDONLY;
26502cb5cc8bSDarrick J. Wong 		return 1;
26512cb5cc8bSDarrick J. Wong 	}
26522cb5cc8bSDarrick J. Wong 
2653a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2654e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
2655a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2656a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2657a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2658a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2659a13fb1a4SEric Sandeen 		return 0;
2660a13fb1a4SEric Sandeen 	}
2661a13fb1a4SEric Sandeen 	/*
2662a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2663a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2664a13fb1a4SEric Sandeen 	 */
2665e2b911c5SDarrick J. Wong 	if (ext4_has_feature_huge_file(sb)) {
2666a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2667a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2668a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2669a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2670a13fb1a4SEric Sandeen 			return 0;
2671a13fb1a4SEric Sandeen 		}
2672a13fb1a4SEric Sandeen 	}
2673e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
2674bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2675bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2676bab08ab9STheodore Ts'o 			 "extents feature\n");
2677bab08ab9STheodore Ts'o 		return 0;
2678bab08ab9STheodore Ts'o 	}
26797c319d32SAditya Kali 
26807c319d32SAditya Kali #ifndef CONFIG_QUOTA
2681e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !readonly) {
26827c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
26837c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
26847c319d32SAditya Kali 			 "without CONFIG_QUOTA");
26857c319d32SAditya Kali 		return 0;
26867c319d32SAditya Kali 	}
2687689c958cSLi Xi 	if (ext4_has_feature_project(sb) && !readonly) {
2688689c958cSLi Xi 		ext4_msg(sb, KERN_ERR,
2689689c958cSLi Xi 			 "Filesystem with project quota feature cannot be mounted RDWR "
2690689c958cSLi Xi 			 "without CONFIG_QUOTA");
2691689c958cSLi Xi 		return 0;
2692689c958cSLi Xi 	}
26937c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2694a13fb1a4SEric Sandeen 	return 1;
2695a13fb1a4SEric Sandeen }
2696a13fb1a4SEric Sandeen 
269766e61a9eSTheodore Ts'o /*
269866e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
269966e61a9eSTheodore Ts'o  * on the file system
270066e61a9eSTheodore Ts'o  */
270166e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
270266e61a9eSTheodore Ts'o {
270366e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
270466e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
270566e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
270666e61a9eSTheodore Ts'o 
270766e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
270866e61a9eSTheodore Ts'o 	es = sbi->s_es;
270966e61a9eSTheodore Ts'o 
271066e61a9eSTheodore Ts'o 	if (es->s_error_count)
2711ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
2712ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
271366e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
271466e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
2715ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %u: %.*s:%d",
271666e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
271766e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
271866e61a9eSTheodore Ts'o 		       es->s_first_error_func,
271966e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
272066e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
2721651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
272266e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
272366e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
2724651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
272566e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
2726651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
272766e61a9eSTheodore Ts'o 	}
272866e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
2729ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %u: %.*s:%d",
273066e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
273166e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
273266e61a9eSTheodore Ts'o 		       es->s_last_error_func,
273366e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
273466e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
2735651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
273666e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
273766e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
2738651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
273966e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
2740651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
274166e61a9eSTheodore Ts'o 	}
274266e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
274366e61a9eSTheodore Ts'o }
274466e61a9eSTheodore Ts'o 
2745bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2746bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2747bfff6873SLukas Czerner {
2748bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2749bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2750bfff6873SLukas Czerner 	struct super_block *sb;
2751bfff6873SLukas Czerner 	unsigned long timeout = 0;
2752bfff6873SLukas Czerner 	int ret = 0;
2753bfff6873SLukas Czerner 
2754bfff6873SLukas Czerner 	sb = elr->lr_super;
2755bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2756bfff6873SLukas Czerner 
2757bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2758bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2759bfff6873SLukas Czerner 		if (!gdp) {
2760bfff6873SLukas Czerner 			ret = 1;
2761bfff6873SLukas Czerner 			break;
2762bfff6873SLukas Czerner 		}
2763bfff6873SLukas Czerner 
2764bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2765bfff6873SLukas Czerner 			break;
2766bfff6873SLukas Czerner 	}
2767bfff6873SLukas Czerner 
27687f511862STheodore Ts'o 	if (group >= ngroups)
2769bfff6873SLukas Czerner 		ret = 1;
2770bfff6873SLukas Czerner 
2771bfff6873SLukas Czerner 	if (!ret) {
2772bfff6873SLukas Czerner 		timeout = jiffies;
2773bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2774bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2775bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
277651ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
277751ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2778bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2779bfff6873SLukas Czerner 		}
2780bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2781bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2782bfff6873SLukas Czerner 	}
2783bfff6873SLukas Czerner 	return ret;
2784bfff6873SLukas Czerner }
2785bfff6873SLukas Czerner 
2786bfff6873SLukas Czerner /*
2787bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
27884ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2789bfff6873SLukas Czerner  */
2790bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2791bfff6873SLukas Czerner {
2792bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2793bfff6873SLukas Czerner 
2794bfff6873SLukas Czerner 	if (!elr)
2795bfff6873SLukas Czerner 		return;
2796bfff6873SLukas Czerner 
2797bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2798bfff6873SLukas Czerner 
2799bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2800bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2801bfff6873SLukas Czerner 	kfree(elr);
2802bfff6873SLukas Czerner }
2803bfff6873SLukas Czerner 
2804bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2805bfff6873SLukas Czerner {
28061bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
28071bb933fbSLukas Czerner 	if (!ext4_li_info) {
28081bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2809bfff6873SLukas Czerner 		return;
28101bb933fbSLukas Czerner 	}
2811bfff6873SLukas Czerner 
2812bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
28131bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2814bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
28151bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2816bfff6873SLukas Czerner }
2817bfff6873SLukas Czerner 
28188f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
28198f1f7453SEric Sandeen 
2820bfff6873SLukas Czerner /*
2821bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2822bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2823bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2824bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2825bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2826bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2827bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2828bfff6873SLukas Czerner  */
2829bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2830bfff6873SLukas Czerner {
2831bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2832bfff6873SLukas Czerner 	struct list_head *pos, *n;
2833bfff6873SLukas Czerner 	struct ext4_li_request *elr;
28344ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2835bfff6873SLukas Czerner 
2836bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2837bfff6873SLukas Czerner 
2838bfff6873SLukas Czerner cont_thread:
2839bfff6873SLukas Czerner 	while (true) {
2840bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2841bfff6873SLukas Czerner 
2842bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2843bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2844bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2845bfff6873SLukas Czerner 			goto exit_thread;
2846bfff6873SLukas Czerner 		}
2847bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2848e22834f0SDmitry Monakhov 			int err = 0;
2849e22834f0SDmitry Monakhov 			int progress = 0;
2850bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2851bfff6873SLukas Czerner 					 lr_request);
2852bfff6873SLukas Czerner 
2853e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
2854e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
2855e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
2856e22834f0SDmitry Monakhov 				continue;
2857e22834f0SDmitry Monakhov 			}
2858e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
2859e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
2860e22834f0SDmitry Monakhov 					progress = 1;
2861e22834f0SDmitry Monakhov 					/*
2862e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
2863e22834f0SDmitry Monakhov 					 * be removed from the list, it is
2864e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
2865e22834f0SDmitry Monakhov 					 */
2866e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
2867e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
2868e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
2869e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
2870e22834f0SDmitry Monakhov 					n = pos->next;
2871e22834f0SDmitry Monakhov 				}
2872e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
2873e22834f0SDmitry Monakhov 			}
2874b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
2875e22834f0SDmitry Monakhov 			if (err) {
2876bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
2877bfff6873SLukas Czerner 				continue;
2878bfff6873SLukas Czerner 			}
2879e22834f0SDmitry Monakhov 			if (!progress) {
2880e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
2881e22834f0SDmitry Monakhov 					(prandom_u32()
2882e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
2883b2c78cd0STheodore Ts'o 			}
2884bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2885bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2886bfff6873SLukas Czerner 		}
2887bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2888bfff6873SLukas Czerner 
2889a0acae0eSTejun Heo 		try_to_freeze();
2890bfff6873SLukas Czerner 
28914ed5c033SLukas Czerner 		cur = jiffies;
28924ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2893f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2894bfff6873SLukas Czerner 			cond_resched();
2895bfff6873SLukas Czerner 			continue;
2896bfff6873SLukas Czerner 		}
2897bfff6873SLukas Czerner 
28984ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
28994ed5c033SLukas Czerner 
29008f1f7453SEric Sandeen 		if (kthread_should_stop()) {
29018f1f7453SEric Sandeen 			ext4_clear_request_list();
29028f1f7453SEric Sandeen 			goto exit_thread;
29038f1f7453SEric Sandeen 		}
2904bfff6873SLukas Czerner 	}
2905bfff6873SLukas Czerner 
2906bfff6873SLukas Czerner exit_thread:
2907bfff6873SLukas Czerner 	/*
2908bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
2909bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
2910bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
2911bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
2912bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
2913bfff6873SLukas Czerner 	 * new one.
2914bfff6873SLukas Czerner 	 */
2915bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2916bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
2917bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
2918bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2919bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2920bfff6873SLukas Czerner 		goto cont_thread;
2921bfff6873SLukas Czerner 	}
2922bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
2923bfff6873SLukas Czerner 	kfree(ext4_li_info);
2924bfff6873SLukas Czerner 	ext4_li_info = NULL;
2925bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2926bfff6873SLukas Czerner 
2927bfff6873SLukas Czerner 	return 0;
2928bfff6873SLukas Czerner }
2929bfff6873SLukas Czerner 
2930bfff6873SLukas Czerner static void ext4_clear_request_list(void)
2931bfff6873SLukas Czerner {
2932bfff6873SLukas Czerner 	struct list_head *pos, *n;
2933bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2934bfff6873SLukas Czerner 
2935bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2936bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2937bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
2938bfff6873SLukas Czerner 				 lr_request);
2939bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
2940bfff6873SLukas Czerner 	}
2941bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2942bfff6873SLukas Czerner }
2943bfff6873SLukas Czerner 
2944bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
2945bfff6873SLukas Czerner {
29468f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
29478f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
29488f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
29498f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
2950bfff6873SLukas Czerner 		ext4_clear_request_list();
2951bfff6873SLukas Czerner 		kfree(ext4_li_info);
2952bfff6873SLukas Czerner 		ext4_li_info = NULL;
295392b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
2954bfff6873SLukas Czerner 				 "initialization thread\n",
2955bfff6873SLukas Czerner 				 err);
2956bfff6873SLukas Czerner 		return err;
2957bfff6873SLukas Czerner 	}
2958bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
2959bfff6873SLukas Czerner 	return 0;
2960bfff6873SLukas Czerner }
2961bfff6873SLukas Czerner 
2962bfff6873SLukas Czerner /*
2963bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
2964bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
2965bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
2966bfff6873SLukas Czerner  * groups and return total number of groups.
2967bfff6873SLukas Czerner  */
2968bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2969bfff6873SLukas Czerner {
2970bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2971bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2972bfff6873SLukas Czerner 
2973bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
2974bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2975bfff6873SLukas Czerner 		if (!gdp)
2976bfff6873SLukas Czerner 			continue;
2977bfff6873SLukas Czerner 
2978bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2979bfff6873SLukas Czerner 			break;
2980bfff6873SLukas Czerner 	}
2981bfff6873SLukas Czerner 
2982bfff6873SLukas Czerner 	return group;
2983bfff6873SLukas Czerner }
2984bfff6873SLukas Czerner 
2985bfff6873SLukas Czerner static int ext4_li_info_new(void)
2986bfff6873SLukas Czerner {
2987bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
2988bfff6873SLukas Czerner 
2989bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2990bfff6873SLukas Czerner 	if (!eli)
2991bfff6873SLukas Czerner 		return -ENOMEM;
2992bfff6873SLukas Czerner 
2993bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
2994bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
2995bfff6873SLukas Czerner 
2996bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
2997bfff6873SLukas Czerner 
2998bfff6873SLukas Czerner 	ext4_li_info = eli;
2999bfff6873SLukas Czerner 
3000bfff6873SLukas Czerner 	return 0;
3001bfff6873SLukas Czerner }
3002bfff6873SLukas Czerner 
3003bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3004bfff6873SLukas Czerner 					    ext4_group_t start)
3005bfff6873SLukas Czerner {
3006bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3007bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3008bfff6873SLukas Czerner 
3009bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3010bfff6873SLukas Czerner 	if (!elr)
3011bfff6873SLukas Czerner 		return NULL;
3012bfff6873SLukas Czerner 
3013bfff6873SLukas Czerner 	elr->lr_super = sb;
3014bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3015bfff6873SLukas Czerner 	elr->lr_next_group = start;
3016bfff6873SLukas Czerner 
3017bfff6873SLukas Czerner 	/*
3018bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3019bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3020bfff6873SLukas Czerner 	 * better.
3021bfff6873SLukas Czerner 	 */
3022dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3023dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3024bfff6873SLukas Czerner 	return elr;
3025bfff6873SLukas Czerner }
3026bfff6873SLukas Czerner 
30277f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3028bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3029bfff6873SLukas Czerner {
3030bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
30317f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
3032bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
30336c5a6cb9SAndrew Morton 	int ret = 0;
3034bfff6873SLukas Czerner 
30357f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
303651ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
303751ce6511SLukas Czerner 		/*
303851ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
303951ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
304051ce6511SLukas Czerner 		 */
304151ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
30427f511862STheodore Ts'o 		goto out;
304351ce6511SLukas Czerner 	}
3044bfff6873SLukas Czerner 
3045bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
3046bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
304755ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
30487f511862STheodore Ts'o 		goto out;
3049bfff6873SLukas Czerner 
3050bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
30517f511862STheodore Ts'o 	if (!elr) {
30527f511862STheodore Ts'o 		ret = -ENOMEM;
30537f511862STheodore Ts'o 		goto out;
30547f511862STheodore Ts'o 	}
3055bfff6873SLukas Czerner 
3056bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3057bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3058bfff6873SLukas Czerner 		if (ret)
3059bfff6873SLukas Czerner 			goto out;
3060bfff6873SLukas Czerner 	}
3061bfff6873SLukas Czerner 
3062bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3063bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3064bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3065bfff6873SLukas Czerner 
3066bfff6873SLukas Czerner 	sbi->s_li_request = elr;
306746e4690bSTao Ma 	/*
306846e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
306946e4690bSTao Ma 	 * the request_list and the removal and free of it is
307046e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
307146e4690bSTao Ma 	 */
307246e4690bSTao Ma 	elr = NULL;
3073bfff6873SLukas Czerner 
3074bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3075bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3076bfff6873SLukas Czerner 		if (ret)
3077bfff6873SLukas Czerner 			goto out;
3078bfff6873SLukas Czerner 	}
3079bfff6873SLukas Czerner out:
3080bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3081beed5ecbSNicolas Kaiser 	if (ret)
3082bfff6873SLukas Czerner 		kfree(elr);
3083bfff6873SLukas Czerner 	return ret;
3084bfff6873SLukas Czerner }
3085bfff6873SLukas Czerner 
3086bfff6873SLukas Czerner /*
3087bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3088bfff6873SLukas Czerner  * module unload.
3089bfff6873SLukas Czerner  */
3090bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3091bfff6873SLukas Czerner {
3092bfff6873SLukas Czerner 	/*
3093bfff6873SLukas Czerner 	 * If thread exited earlier
3094bfff6873SLukas Czerner 	 * there's nothing to be done.
3095bfff6873SLukas Czerner 	 */
30968f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3097bfff6873SLukas Czerner 		return;
3098bfff6873SLukas Czerner 
30998f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3100bfff6873SLukas Czerner }
3101bfff6873SLukas Czerner 
310225ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
310325ed6e8aSDarrick J. Wong {
310425ed6e8aSDarrick J. Wong 	int ret = 1;
310525ed6e8aSDarrick J. Wong 	int compat, incompat;
310625ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
310725ed6e8aSDarrick J. Wong 
31089aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3109db9ee220SDarrick J. Wong 		/* journal checksum v3 */
311025ed6e8aSDarrick J. Wong 		compat = 0;
3111db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
311225ed6e8aSDarrick J. Wong 	} else {
311325ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
311425ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
311525ed6e8aSDarrick J. Wong 		incompat = 0;
311625ed6e8aSDarrick J. Wong 	}
311725ed6e8aSDarrick J. Wong 
3118feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3119feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3120feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3121feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
312225ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
312325ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
312425ed6e8aSDarrick J. Wong 				compat, 0,
312525ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
312625ed6e8aSDarrick J. Wong 				incompat);
312725ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
312825ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
312925ed6e8aSDarrick J. Wong 				compat, 0,
313025ed6e8aSDarrick J. Wong 				incompat);
313125ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
313225ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
313325ed6e8aSDarrick J. Wong 	} else {
3134feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3135feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
313625ed6e8aSDarrick J. Wong 	}
313725ed6e8aSDarrick J. Wong 
313825ed6e8aSDarrick J. Wong 	return ret;
313925ed6e8aSDarrick J. Wong }
314025ed6e8aSDarrick J. Wong 
3141952fc18eSTheodore Ts'o /*
3142952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3143952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3144952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3145952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3146952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3147952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3148952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3149952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3150952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3151952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3152952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3153952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3154952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3155952fc18eSTheodore Ts'o  */
3156952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3157952fc18eSTheodore Ts'o 			  char *buf)
3158952fc18eSTheodore Ts'o {
3159952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3160952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3161952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3162952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3163952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3164952fc18eSTheodore Ts'o 
3165e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
31660548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
31670548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
31680548bbb8STheodore Ts'o 
3169952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3170952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3171952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3172952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3173952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3174952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3175952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3176952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3177952fc18eSTheodore Ts'o 			count++;
3178952fc18eSTheodore Ts'o 		}
3179952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3180952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3181952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3182952fc18eSTheodore Ts'o 			count++;
3183952fc18eSTheodore Ts'o 		}
3184952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3185952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3186952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3187952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3188952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3189952fc18eSTheodore Ts'o 				count++;
3190952fc18eSTheodore Ts'o 			}
3191952fc18eSTheodore Ts'o 		if (i != grp)
3192952fc18eSTheodore Ts'o 			continue;
3193952fc18eSTheodore Ts'o 		s = 0;
3194952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3195952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3196952fc18eSTheodore Ts'o 			count++;
3197952fc18eSTheodore Ts'o 		}
3198952fc18eSTheodore Ts'o 		for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) {
3199952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3200952fc18eSTheodore Ts'o 			count++;
3201952fc18eSTheodore Ts'o 		}
3202952fc18eSTheodore Ts'o 	}
3203952fc18eSTheodore Ts'o 	if (!count)
3204952fc18eSTheodore Ts'o 		return 0;
3205952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3206952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3207952fc18eSTheodore Ts'o }
3208952fc18eSTheodore Ts'o 
3209952fc18eSTheodore Ts'o /*
3210952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3211952fc18eSTheodore Ts'o  */
3212952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3213952fc18eSTheodore Ts'o {
3214952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3215952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
32163c816dedSEric Whitney 	struct inode *j_inode;
32173c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3218952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3219952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
32204fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3221952fc18eSTheodore Ts'o 
3222952fc18eSTheodore Ts'o 	if (!buf)
3223952fc18eSTheodore Ts'o 		return -ENOMEM;
3224952fc18eSTheodore Ts'o 
3225952fc18eSTheodore Ts'o 	/*
3226952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3227952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3228952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3229952fc18eSTheodore Ts'o 	 */
3230952fc18eSTheodore Ts'o 
3231952fc18eSTheodore Ts'o 	/*
3232952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3233952fc18eSTheodore Ts'o 	 */
3234952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3235952fc18eSTheodore Ts'o 
3236952fc18eSTheodore Ts'o 	/*
3237952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3238952fc18eSTheodore Ts'o 	 */
3239952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3240952fc18eSTheodore Ts'o 		int blks;
3241952fc18eSTheodore Ts'o 
3242952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3243952fc18eSTheodore Ts'o 		overhead += blks;
3244952fc18eSTheodore Ts'o 		if (blks)
3245952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3246952fc18eSTheodore Ts'o 		cond_resched();
3247952fc18eSTheodore Ts'o 	}
32483c816dedSEric Whitney 
32493c816dedSEric Whitney 	/*
32503c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
32513c816dedSEric Whitney 	 * loaded or not
32523c816dedSEric Whitney 	 */
3253b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3254810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
32553c816dedSEric Whitney 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal) {
32563c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
32573c816dedSEric Whitney 		if (j_inode) {
32583c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
32593c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
32603c816dedSEric Whitney 			iput(j_inode);
32613c816dedSEric Whitney 		} else {
32623c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
32633c816dedSEric Whitney 		}
32643c816dedSEric Whitney 	}
3265952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3266952fc18eSTheodore Ts'o 	smp_wmb();
3267952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3268952fc18eSTheodore Ts'o 	return 0;
3269952fc18eSTheodore Ts'o }
3270952fc18eSTheodore Ts'o 
3271b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
327227dd4385SLukas Czerner {
327327dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3274b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
327527dd4385SLukas Czerner 
327627dd4385SLukas Czerner 	/*
327730fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
327830fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
327930fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
328030fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
328130fac0f7SJan Kara 	 */
3282e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3283b5799018STheodore Ts'o 		return;
328430fac0f7SJan Kara 	/*
328527dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
328627dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
328727dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3288556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
328927dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
329027dd4385SLukas Czerner 	 * very rare.
329127dd4385SLukas Czerner 	 */
3292b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3293b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
329427dd4385SLukas Czerner 
329527dd4385SLukas Czerner 	do_div(resv_clusters, 50);
329627dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
329727dd4385SLukas Czerner 
3298b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
329927dd4385SLukas Czerner }
330027dd4385SLukas Czerner 
3301617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3302ac27a0ecSDave Kleikamp {
3303d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3304ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3305617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
33065aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3307617ba13bSMingming Cao 	ext4_fsblk_t block;
3308617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
330970bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3310ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3311ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3312ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3313ac27a0ecSDave Kleikamp 	struct inode *root;
33140390131bSFrank Mayhar 	const char *descr;
3315dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3316281b5995STheodore Ts'o 	int blocksize, clustersize;
33174ec11028STheodore Ts'o 	unsigned int db_count;
33184ec11028STheodore Ts'o 	unsigned int i;
3319281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3320bd81d8eeSLaurent Vivier 	__u64 blocks_count;
332107aa2ea1SLukas Czerner 	int err = 0;
3322b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3323bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3324ac27a0ecSDave Kleikamp 
33255aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
33265aee0f8aSTheodore Ts'o 		goto out_free_base;
3327705895b6SPekka Enberg 
3328705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3329705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
33305aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
33315aee0f8aSTheodore Ts'o 		goto out_free_base;
33325aee0f8aSTheodore Ts'o 
3333ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
33342c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3335240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3336d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3337f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3338f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3339f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3340ac27a0ecSDave Kleikamp 
33419f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3342ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
33439f6200bbSTheodore Ts'o 
334407aa2ea1SLukas Czerner 	/* -EINVAL is default */
3345dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3346617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3347ac27a0ecSDave Kleikamp 	if (!blocksize) {
3348b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3349ac27a0ecSDave Kleikamp 		goto out_fail;
3350ac27a0ecSDave Kleikamp 	}
3351ac27a0ecSDave Kleikamp 
3352ac27a0ecSDave Kleikamp 	/*
3353617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3354ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3355ac27a0ecSDave Kleikamp 	 */
3356617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
335770bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
335870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3359ac27a0ecSDave Kleikamp 	} else {
336070bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3361ac27a0ecSDave Kleikamp 	}
3362ac27a0ecSDave Kleikamp 
3363a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3364b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3365ac27a0ecSDave Kleikamp 		goto out_fail;
3366ac27a0ecSDave Kleikamp 	}
3367ac27a0ecSDave Kleikamp 	/*
3368ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3369617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3370ac27a0ecSDave Kleikamp 	 */
33712716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3372ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3373ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3374617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3375617ba13bSMingming Cao 		goto cantfind_ext4;
3376afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3377ac27a0ecSDave Kleikamp 
3378feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3379e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3380e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3381363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3382feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3383feb0ab32SDarrick J. Wong 
3384d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3385d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3386d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3387d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3388d25425f8SDarrick J. Wong 		silent = 1;
3389d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3390d25425f8SDarrick J. Wong 	}
3391d25425f8SDarrick J. Wong 
33920441984aSDarrick J. Wong 	/* Load the checksum driver */
3393e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb)) {
33940441984aSDarrick J. Wong 		sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
33950441984aSDarrick J. Wong 		if (IS_ERR(sbi->s_chksum_driver)) {
33960441984aSDarrick J. Wong 			ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
33970441984aSDarrick J. Wong 			ret = PTR_ERR(sbi->s_chksum_driver);
33980441984aSDarrick J. Wong 			sbi->s_chksum_driver = NULL;
33990441984aSDarrick J. Wong 			goto failed_mount;
34000441984aSDarrick J. Wong 		}
34010441984aSDarrick J. Wong 	}
34020441984aSDarrick J. Wong 
3403a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3404a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3405a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3406a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3407a9c47317SDarrick J. Wong 		silent = 1;
34086a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3409a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3410a9c47317SDarrick J. Wong 	}
3411a9c47317SDarrick J. Wong 
3412a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3413e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
34148c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
34158c81bd8fSDarrick J. Wong 	else if (ext4_has_metadata_csum(sb))
3416a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3417a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3418a9c47317SDarrick J. Wong 
3419ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3420ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3421fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3422617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3423fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
342487f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3425fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3426617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3427fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3428ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3429fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
343003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3431fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
34322e7842b8SHugh Dickins #endif
343398c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
343498c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
343598c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
343698c1a759SDarrick J. Wong 
3437617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3438fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3439617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3440fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3441617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3442fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3443ac27a0ecSDave Kleikamp 
3444617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3445fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3446bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3447fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3448bb4f397aSAneesh Kumar K.V 	else
3449fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
345045f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3451fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
34528b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3453fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3454ac27a0ecSDave Kleikamp 
345508cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
345608cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
345730773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
345830773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
345930773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3460ac27a0ecSDave Kleikamp 
34618b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3462fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3463ac27a0ecSDave Kleikamp 
34641e2462f9SMingming Cao 	/*
3465dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3466dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3467dd919b98SAneesh Kumar K.V 	 */
3468bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
34698b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3470fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3471dd919b98SAneesh Kumar K.V 
347251ce6511SLukas Czerner 	/*
347351ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
347451ce6511SLukas Czerner 	 * no mount option specified.
347551ce6511SLukas Czerner 	 */
347651ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
347751ce6511SLukas Czerner 
34785aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
34795aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
34805aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
34815aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
34825aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
34835aee0f8aSTheodore Ts'o 			goto failed_mount;
34845aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
34855aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
34868b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
34878b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
34885aee0f8aSTheodore Ts'o 				 s_mount_opts);
34895aee0f8aSTheodore Ts'o 		}
34905aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
34918b67f04aSTheodore Ts'o 	}
34925a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3493b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3494661aa520SEric Sandeen 			   &journal_ioprio, 0))
3495ac27a0ecSDave Kleikamp 		goto failed_mount;
3496ac27a0ecSDave Kleikamp 
349756889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
349856889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
349956889787STheodore Ts'o 			    "with data=journal disables delayed "
350056889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
350156889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
350256889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
350356889787STheodore Ts'o 				 "both data=journal and delalloc");
350456889787STheodore Ts'o 			goto failed_mount;
350556889787STheodore Ts'o 		}
350656889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
350756889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
35086ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
350956889787STheodore Ts'o 			goto failed_mount;
351056889787STheodore Ts'o 		}
3511923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3512923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3513923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3514923ae0ffSRoss Zwisler 			goto failed_mount;
3515923ae0ffSRoss Zwisler 		}
351656889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
351756889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3518001e4a87STejun Heo 	} else {
3519001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
352056889787STheodore Ts'o 	}
352156889787STheodore Ts'o 
3522ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3523482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3524ac27a0ecSDave Kleikamp 
3525617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3526e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
3527e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
3528e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
3529b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3530b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3531b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3532469108ffSTheodore Tso 
3533ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3534ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3535e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
3536ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3537ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3538ed3654ebSTheodore Ts'o 			goto failed_mount;
3539ed3654ebSTheodore Ts'o 		}
3540ed3654ebSTheodore Ts'o 	}
3541ed3654ebSTheodore Ts'o 
35422035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
35432035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
35442035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
35452035e776STheodore Ts'o 				 "using the ext4 subsystem");
35462035e776STheodore Ts'o 		else {
35472035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
35482035e776STheodore Ts'o 				 "to feature incompatibilities");
35492035e776STheodore Ts'o 			goto failed_mount;
35502035e776STheodore Ts'o 		}
35512035e776STheodore Ts'o 	}
35522035e776STheodore Ts'o 
35532035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
35542035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
35552035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
35562035e776STheodore Ts'o 				 "using the ext4 subsystem");
35572035e776STheodore Ts'o 		else {
35582035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
35592035e776STheodore Ts'o 				 "to feature incompatibilities");
35602035e776STheodore Ts'o 			goto failed_mount;
35612035e776STheodore Ts'o 		}
35622035e776STheodore Ts'o 	}
35632035e776STheodore Ts'o 
3564469108ffSTheodore Tso 	/*
3565ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3566ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3567ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3568ac27a0ecSDave Kleikamp 	 */
3569a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3570ac27a0ecSDave Kleikamp 		goto failed_mount;
3571a13fb1a4SEric Sandeen 
3572261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3573617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3574617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3575b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
35769e47a4c9STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
35779e47a4c9STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
35789e47a4c9STheodore Ts'o 		goto failed_mount;
35799e47a4c9STheodore Ts'o 	}
35809e47a4c9STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
35819e47a4c9STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
35829e47a4c9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
35839e47a4c9STheodore Ts'o 			 "Invalid log block size: %u",
35849e47a4c9STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
3585ac27a0ecSDave Kleikamp 		goto failed_mount;
3586ac27a0ecSDave Kleikamp 	}
3587ac27a0ecSDave Kleikamp 
35885b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
35895b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
35905b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
35915b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
35925b9554dcSTheodore Ts'o 		goto failed_mount;
35935b9554dcSTheodore Ts'o 	}
35945b9554dcSTheodore Ts'o 
3595923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
359687eefeb4SToshi Kani 		err = bdev_dax_supported(sb, blocksize);
359787eefeb4SToshi Kani 		if (err)
3598923ae0ffSRoss Zwisler 			goto failed_mount;
3599923ae0ffSRoss Zwisler 	}
3600923ae0ffSRoss Zwisler 
3601e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
36026ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
36036ddb2447STheodore Ts'o 			 es->s_encryption_level);
36046ddb2447STheodore Ts'o 		goto failed_mount;
36056ddb2447STheodore Ts'o 	}
36066ddb2447STheodore Ts'o 
3607ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3608ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3609ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3610b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3611ce40733cSAneesh Kumar K.V 					blocksize);
3612ac27a0ecSDave Kleikamp 			goto failed_mount;
3613ac27a0ecSDave Kleikamp 		}
3614ac27a0ecSDave Kleikamp 
3615ac27a0ecSDave Kleikamp 		brelse(bh);
361670bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
361770bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3618a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
3619ac27a0ecSDave Kleikamp 		if (!bh) {
3620b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3621b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3622ac27a0ecSDave Kleikamp 			goto failed_mount;
3623ac27a0ecSDave Kleikamp 		}
36242716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
3625ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3626617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3627b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3628b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3629ac27a0ecSDave Kleikamp 			goto failed_mount;
3630ac27a0ecSDave Kleikamp 		}
3631ac27a0ecSDave Kleikamp 	}
3632ac27a0ecSDave Kleikamp 
3633e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
3634f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3635f287a1a5STheodore Ts'o 						      has_huge_files);
3636f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3637ac27a0ecSDave Kleikamp 
3638617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3639617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3640617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3641ac27a0ecSDave Kleikamp 	} else {
3642ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3643ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3644617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
36451330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3646ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3647b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3648b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3649ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3650ac27a0ecSDave Kleikamp 			goto failed_mount;
3651ac27a0ecSDave Kleikamp 		}
3652ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3653ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3654ac27a0ecSDave Kleikamp 	}
36550b8e58a1SAndreas Dilger 
36560d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
3657e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
36588fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
36590d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3660d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3661b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3662b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
36630d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
36640d1ee42fSAlexandre Ratchov 			goto failed_mount;
36650d1ee42fSAlexandre Ratchov 		}
36660d1ee42fSAlexandre Ratchov 	} else
36670d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
36680b8e58a1SAndreas Dilger 
3669ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3670ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
36710b8e58a1SAndreas Dilger 
3672617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3673ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3674617ba13bSMingming Cao 		goto cantfind_ext4;
3675*cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
3676*cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
3677*cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
3678*cd6bb35bSTheodore Ts'o 			 sbi->s_blocks_per_group);
3679*cd6bb35bSTheodore Ts'o 		goto failed_mount;
3680*cd6bb35bSTheodore Ts'o 	}
3681ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3682ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
36830d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3684ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3685ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3686e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3687e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
36880b8e58a1SAndreas Dilger 
3689ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3690ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3691ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3692e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
3693f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
3694f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
3695f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3696f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3697f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
369823301410STheodore Ts'o 			if (!(sb->s_flags & MS_RDONLY))
369923301410STheodore Ts'o 				es->s_flags |=
370023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3701f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3702f99b2589STheodore Ts'o #else
370323301410STheodore Ts'o 			if (!(sb->s_flags & MS_RDONLY))
370423301410STheodore Ts'o 				es->s_flags |=
370523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3706f99b2589STheodore Ts'o #endif
3707f99b2589STheodore Ts'o 		}
370823301410STheodore Ts'o 	}
3709ac27a0ecSDave Kleikamp 
3710281b5995STheodore Ts'o 	/* Handle clustersize */
3711281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3712e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
3713281b5995STheodore Ts'o 	if (has_bigalloc) {
3714281b5995STheodore Ts'o 		if (clustersize < blocksize) {
3715281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3716281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
3717281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
3718281b5995STheodore Ts'o 			goto failed_mount;
3719281b5995STheodore Ts'o 		}
37209e47a4c9STheodore Ts'o 		if (le32_to_cpu(es->s_log_cluster_size) >
37219e47a4c9STheodore Ts'o 		    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
37229e47a4c9STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
37239e47a4c9STheodore Ts'o 				 "Invalid log cluster size: %u",
37249e47a4c9STheodore Ts'o 				 le32_to_cpu(es->s_log_cluster_size));
37259e47a4c9STheodore Ts'o 			goto failed_mount;
37269e47a4c9STheodore Ts'o 		}
3727281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3728281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
3729281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
3730281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
3731281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
3732281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3733281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
3734281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3735281b5995STheodore Ts'o 			goto failed_mount;
3736281b5995STheodore Ts'o 		}
3737281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
3738281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3739281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3740281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
3741281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
3742281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3743281b5995STheodore Ts'o 			goto failed_mount;
3744281b5995STheodore Ts'o 		}
3745281b5995STheodore Ts'o 	} else {
3746281b5995STheodore Ts'o 		if (clustersize != blocksize) {
3747281b5995STheodore Ts'o 			ext4_warning(sb, "fragment/cluster size (%d) != "
3748281b5995STheodore Ts'o 				     "block size (%d)", clustersize,
3749281b5995STheodore Ts'o 				     blocksize);
3750281b5995STheodore Ts'o 			clustersize = blocksize;
3751281b5995STheodore Ts'o 		}
3752ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
3753b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3754b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
3755ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
3756ac27a0ecSDave Kleikamp 			goto failed_mount;
3757ac27a0ecSDave Kleikamp 		}
3758281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3759281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
3760281b5995STheodore Ts'o 	}
3761281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
3762281b5995STheodore Ts'o 
3763960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
3764960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
3765960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
3766960fd856STheodore Ts'o 
3767bf43d84bSEric Sandeen 	/*
3768bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3769bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3770bf43d84bSEric Sandeen 	 */
37715a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
377230ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
37735a9ae68aSDarrick J. Wong 	if (err) {
3774b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3775bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3776ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
377790c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
3778ac27a0ecSDave Kleikamp 		goto failed_mount;
3779ac27a0ecSDave Kleikamp 	}
3780ac27a0ecSDave Kleikamp 
3781617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3782617ba13bSMingming Cao 		goto cantfind_ext4;
3783e7c95593SEric Sandeen 
37840f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
37850f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
37860f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3787b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3788b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
37890f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
37900f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
37910f2ddca6SFrom: Thiemo Nagel 	}
37920f2ddca6SFrom: Thiemo Nagel 
37934ec11028STheodore Ts'o 	/*
37944ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
37954ec11028STheodore Ts'o 	 * of the filesystem.
37964ec11028STheodore Ts'o 	 */
37974ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3798b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
3799b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3800e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
38014ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3802e7c95593SEric Sandeen 		goto failed_mount;
3803e7c95593SEric Sandeen 	}
3804bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3805bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3806bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3807bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
38084ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3809b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
38104ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3811b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
38124ec11028STheodore Ts'o 		       ext4_blocks_count(es),
38134ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
38144ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
38154ec11028STheodore Ts'o 		goto failed_mount;
38164ec11028STheodore Ts'o 	}
3817bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3818fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3819fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3820617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3821617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3822f18a5f21STheodore Ts'o 	sbi->s_group_desc = ext4_kvmalloc(db_count *
3823f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
3824ac27a0ecSDave Kleikamp 					  GFP_KERNEL);
3825ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3826b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
38272cde417dSTheodore Ts'o 		ret = -ENOMEM;
3828ac27a0ecSDave Kleikamp 		goto failed_mount;
3829ac27a0ecSDave Kleikamp 	}
3830ac27a0ecSDave Kleikamp 
3831705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3832ac27a0ecSDave Kleikamp 
3833ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
383470bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3835a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
3836ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3837b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3838b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3839ac27a0ecSDave Kleikamp 			db_count = i;
3840ac27a0ecSDave Kleikamp 			goto failed_mount2;
3841ac27a0ecSDave Kleikamp 		}
3842ac27a0ecSDave Kleikamp 	}
3843829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
3844b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
38456a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
3846f9ae9cf5STheodore Ts'o 		goto failed_mount2;
3847ac27a0ecSDave Kleikamp 	}
3848772cb7c8SJose R. Santos 
3849f9ae9cf5STheodore Ts'o 	sbi->s_gdb_count = db_count;
3850ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3851ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3852ac27a0ecSDave Kleikamp 
385304ecddb7SJan Mrazek 	setup_timer(&sbi->s_err_report, print_daily_error_info,
385404ecddb7SJan Mrazek 		(unsigned long) sb);
385504496411STao Ma 
3856a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
3857eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
3858ce7e010aSTheodore Ts'o 		goto failed_mount3;
3859ce7e010aSTheodore Ts'o 
3860c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
386167a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
3862c9de560dSAlex Tomas 
3863f9ae9cf5STheodore Ts'o 	/*
3864f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
3865f9ae9cf5STheodore Ts'o 	 */
3866f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
3867617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3868617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3869a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
3870ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3871617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
3872e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
38731fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
3874262b4662SJan Kara 	else
3875262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
3876689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
3877ac27a0ecSDave Kleikamp #endif
3878f2fa2ffcSAneesh Kumar K.V 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3879f2fa2ffcSAneesh Kumar K.V 
3880ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
38813b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
3882ac27a0ecSDave Kleikamp 
3883ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3884ac27a0ecSDave Kleikamp 
3885ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3886e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
3887ac27a0ecSDave Kleikamp 
3888e2b911c5SDarrick J. Wong 	if (ext4_has_feature_mmp(sb) && !(sb->s_flags & MS_RDONLY))
3889c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
389050460fe8SDarrick J. Wong 			goto failed_mount3a;
3891c5e06d10SJohann Lombardi 
3892ac27a0ecSDave Kleikamp 	/*
3893ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3894ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3895ac27a0ecSDave Kleikamp 	 */
3896e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
3897617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
389850460fe8SDarrick J. Wong 			goto failed_mount3a;
38990390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
3900e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
3901b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3902b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3903744692dcSJiaying Zhang 		goto failed_mount_wq;
3904ac27a0ecSDave Kleikamp 	} else {
39051e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
39061e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
39071e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
39081e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
39091e381f60SDmitry Monakhov 			goto failed_mount_wq;
39101e381f60SDmitry Monakhov 		}
39111e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
39121e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
39131e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
39141e381f60SDmitry Monakhov 			goto failed_mount_wq;
39151e381f60SDmitry Monakhov 		}
39161e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
39171e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
39181e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
39191e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
39201e381f60SDmitry Monakhov 			goto failed_mount_wq;
39211e381f60SDmitry Monakhov 		}
39221e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
39231e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
39241e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
39251e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
39261e381f60SDmitry Monakhov 			goto failed_mount_wq;
39271e381f60SDmitry Monakhov 		}
39281e381f60SDmitry Monakhov 		sbi->s_def_mount_opt &= EXT4_MOUNT_JOURNAL_CHECKSUM;
39291e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
3930fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
39310390131bSFrank Mayhar 		sbi->s_journal = NULL;
39320390131bSFrank Mayhar 		needs_recovery = 0;
39330390131bSFrank Mayhar 		goto no_journal;
3934ac27a0ecSDave Kleikamp 	}
3935ac27a0ecSDave Kleikamp 
3936e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
3937eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3938eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3939b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3940744692dcSJiaying Zhang 		goto failed_mount_wq;
3941eb40a09cSJose R. Santos 	}
3942eb40a09cSJose R. Santos 
394325ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
394425ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
394525ed6e8aSDarrick J. Wong 			 "feature set");
394625ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
3947d4da6c9cSLinus Torvalds 	}
3948818d276cSGirish Shilamkar 
3949ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3950ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3951ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3952ac27a0ecSDave Kleikamp 	case 0:
3953ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
395463f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
395563f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
395663f57933SAndrew Morton 		 */
3957dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3958dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3959fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3960ac27a0ecSDave Kleikamp 		else
3961fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3962ac27a0ecSDave Kleikamp 		break;
3963ac27a0ecSDave Kleikamp 
3964617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
3965617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
3966dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
3967dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
3968b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
3969b31e1552SEric Sandeen 			       "requested data journaling mode");
3970744692dcSJiaying Zhang 			goto failed_mount_wq;
3971ac27a0ecSDave Kleikamp 		}
3972ac27a0ecSDave Kleikamp 	default:
3973ac27a0ecSDave Kleikamp 		break;
3974ac27a0ecSDave Kleikamp 	}
3975b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3976ac27a0ecSDave Kleikamp 
397718aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
397818aadd47SBobi Jam 
3979ce7e010aSTheodore Ts'o no_journal:
398082939d79SJan Kara 	sbi->s_mb_cache = ext4_xattr_create_cache();
39819c191f70ST Makphaibulchoke 	if (!sbi->s_mb_cache) {
39829c191f70ST Makphaibulchoke 		ext4_msg(sb, KERN_ERR, "Failed to create an mb_cache");
39839c191f70ST Makphaibulchoke 		goto failed_mount_wq;
39849c191f70ST Makphaibulchoke 	}
39859c191f70ST Makphaibulchoke 
3986e2b911c5SDarrick J. Wong 	if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
398709cbfeafSKirill A. Shutemov 	    (blocksize != PAGE_SIZE)) {
39881cb767cdSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39891cb767cdSTheodore Ts'o 			 "Unsupported blocksize for fs encryption");
39901cb767cdSTheodore Ts'o 		goto failed_mount_wq;
39911cb767cdSTheodore Ts'o 	}
39921cb767cdSTheodore Ts'o 
3993e2b911c5SDarrick J. Wong 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !(sb->s_flags & MS_RDONLY) &&
3994e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
3995e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
39966ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
39976ddb2447STheodore Ts'o 	}
39986ddb2447STheodore Ts'o 
3999fd89d5f2STejun Heo 	/*
4000952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4001952fc18eSTheodore Ts'o 	 * superblock if present.
4002952fc18eSTheodore Ts'o 	 */
4003952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4004952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4005952fc18eSTheodore Ts'o 	else {
400607aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
400707aa2ea1SLukas Czerner 		if (err)
4008952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4009952fc18eSTheodore Ts'o 	}
4010952fc18eSTheodore Ts'o 
4011952fc18eSTheodore Ts'o 	/*
4012fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4013fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4014fd89d5f2STejun Heo 	 */
40152e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
40162e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
40172e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
40182e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
401907aa2ea1SLukas Czerner 		ret = -ENOMEM;
40202e8fa54eSJan Kara 		goto failed_mount4;
40212e8fa54eSJan Kara 	}
40222e8fa54eSJan Kara 
4023ac27a0ecSDave Kleikamp 	/*
4024dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4025ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4026ac27a0ecSDave Kleikamp 	 */
4027ac27a0ecSDave Kleikamp 
40281d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
40291d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4030b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
40311d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
403232a9bb57SManish Katiyar 		root = NULL;
4033ac27a0ecSDave Kleikamp 		goto failed_mount4;
4034ac27a0ecSDave Kleikamp 	}
4035ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4036b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
403794bf608aSAl Viro 		iput(root);
4038ac27a0ecSDave Kleikamp 		goto failed_mount4;
4039ac27a0ecSDave Kleikamp 	}
404048fde701SAl Viro 	sb->s_root = d_make_root(root);
40411d1fe1eeSDavid Howells 	if (!sb->s_root) {
4042b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
40431d1fe1eeSDavid Howells 		ret = -ENOMEM;
40441d1fe1eeSDavid Howells 		goto failed_mount4;
40451d1fe1eeSDavid Howells 	}
4046ac27a0ecSDave Kleikamp 
40477e84b621SEric Sandeen 	if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
40487e84b621SEric Sandeen 		sb->s_flags |= MS_RDONLY;
4049ef7f3835SKalpak Shah 
4050ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
4051ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4052ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4053ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
4054e2b911c5SDarrick J. Wong 		if (ext4_has_feature_extra_isize(sb)) {
4055ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4056ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
4057ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4058ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
4059ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4060ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
4061ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4062ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
4063ef7f3835SKalpak Shah 		}
4064ef7f3835SKalpak Shah 	}
4065ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
4066ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
4067ef7f3835SKalpak Shah 							sbi->s_inode_size) {
4068ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4069ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
4070b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
4071b31e1552SEric Sandeen 			 "available");
4072ef7f3835SKalpak Shah 	}
4073ef7f3835SKalpak Shah 
4074b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
407527dd4385SLukas Czerner 
40766fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
40776fd058f7STheodore Ts'o 	if (err) {
4078b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4079fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4080f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4081f9ae9cf5STheodore Ts'o 	}
4082f9ae9cf5STheodore Ts'o 
4083f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4084f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4085f9ae9cf5STheodore Ts'o 	if (err) {
4086f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4087f9ae9cf5STheodore Ts'o 			 err);
4088dcf2d804STao Ma 		goto failed_mount5;
4089c2774d84SAneesh Kumar K.V 	}
4090c2774d84SAneesh Kumar K.V 
4091d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4092d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4093d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
4094908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4095908c7f19STejun Heo 				  GFP_KERNEL);
4096d5e03cbbSTheodore Ts'o 	if (!err) {
4097d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4098d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
4099908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4100908c7f19STejun Heo 					  GFP_KERNEL);
4101d5e03cbbSTheodore Ts'o 	}
4102d5e03cbbSTheodore Ts'o 	if (!err)
4103d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4104908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4105d5e03cbbSTheodore Ts'o 	if (!err)
4106908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4107908c7f19STejun Heo 					  GFP_KERNEL);
4108c8585c6fSDaeho Jeong 	if (!err)
4109c8585c6fSDaeho Jeong 		err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem);
4110c8585c6fSDaeho Jeong 
4111d5e03cbbSTheodore Ts'o 	if (err) {
4112d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4113d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4114d5e03cbbSTheodore Ts'o 	}
4115d5e03cbbSTheodore Ts'o 
4116e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4117d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4118d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4119d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4120d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4121d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4122d5e03cbbSTheodore Ts'o 		}
4123d5e03cbbSTheodore Ts'o 
4124bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4125bfff6873SLukas Czerner 	if (err)
4126dcf2d804STao Ma 		goto failed_mount6;
4127bfff6873SLukas Czerner 
4128b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4129dcf2d804STao Ma 	if (err)
4130dcf2d804STao Ma 		goto failed_mount7;
41313197ebdbSTheodore Ts'o 
41329b2ff357SJan Kara #ifdef CONFIG_QUOTA
41339b2ff357SJan Kara 	/* Enable quota usage during mount. */
4134e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !(sb->s_flags & MS_RDONLY)) {
41359b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
41369b2ff357SJan Kara 		if (err)
41379b2ff357SJan Kara 			goto failed_mount8;
41389b2ff357SJan Kara 	}
41399b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
41409b2ff357SJan Kara 
4141617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4142617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4143617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
41440390131bSFrank Mayhar 	if (needs_recovery) {
4145b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4146617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
41470390131bSFrank Mayhar 	}
41480390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
41490390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
41500390131bSFrank Mayhar 			descr = " journalled data mode";
41510390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
41520390131bSFrank Mayhar 			descr = " ordered data mode";
41530390131bSFrank Mayhar 		else
41540390131bSFrank Mayhar 			descr = " writeback data mode";
41550390131bSFrank Mayhar 	} else
41560390131bSFrank Mayhar 		descr = "out journal";
41570390131bSFrank Mayhar 
415879add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
415979add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
416079add3a3SLukas Czerner 		if (!blk_queue_discard(q))
416179add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
416279add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
416379add3a3SLukas Czerner 				 "the device does not support discard");
416479add3a3SLukas Czerner 	}
416579add3a3SLukas Czerner 
4166e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4167d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
41685aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
41695aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
41705aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
41718b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4172ac27a0ecSDave Kleikamp 
417366e61a9eSTheodore Ts'o 	if (es->s_error_count)
417466e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4175ac27a0ecSDave Kleikamp 
4176efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4177efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4178efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4179efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4180efbed4dcSTheodore Ts'o 
4181d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4182a7550b30SJaegeuk Kim #ifdef CONFIG_EXT4_FS_ENCRYPTION
4183a7550b30SJaegeuk Kim 	memcpy(sbi->key_prefix, EXT4_KEY_DESC_PREFIX,
4184a7550b30SJaegeuk Kim 				EXT4_KEY_DESC_PREFIX_SIZE);
4185a7550b30SJaegeuk Kim 	sbi->key_prefix_size = EXT4_KEY_DESC_PREFIX_SIZE;
4186a7550b30SJaegeuk Kim #endif
4187ac27a0ecSDave Kleikamp 	return 0;
4188ac27a0ecSDave Kleikamp 
4189617ba13bSMingming Cao cantfind_ext4:
4190ac27a0ecSDave Kleikamp 	if (!silent)
4191b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4192ac27a0ecSDave Kleikamp 	goto failed_mount;
4193ac27a0ecSDave Kleikamp 
419472ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
419572ba7450STheodore Ts'o failed_mount8:
4196ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
419772ba7450STheodore Ts'o #endif
4198dcf2d804STao Ma failed_mount7:
4199dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4200dcf2d804STao Ma failed_mount6:
4201f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
4202d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
4203b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
4204d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4205d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4206d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4207d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
420800764937SAzat Khuzhin failed_mount5:
4209f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4210f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4211f9ae9cf5STheodore Ts'o failed_mount4a:
421294bf608aSAl Viro 	dput(sb->s_root);
421332a9bb57SManish Katiyar 	sb->s_root = NULL;
421494bf608aSAl Viro failed_mount4:
4215b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
42162e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
42172e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
42184c0425ffSMingming Cao failed_mount_wq:
421982939d79SJan Kara 	if (sbi->s_mb_cache) {
422082939d79SJan Kara 		ext4_xattr_destroy_cache(sbi->s_mb_cache);
422182939d79SJan Kara 		sbi->s_mb_cache = NULL;
422282939d79SJan Kara 	}
42230390131bSFrank Mayhar 	if (sbi->s_journal) {
4224dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
422547b4a50bSJan Kara 		sbi->s_journal = NULL;
42260390131bSFrank Mayhar 	}
422750460fe8SDarrick J. Wong failed_mount3a:
4228d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4229eb68d0e2SZheng Liu failed_mount3:
42309105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4231c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4232c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4233ac27a0ecSDave Kleikamp failed_mount2:
4234ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4235ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4236b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4237ac27a0ecSDave Kleikamp failed_mount:
42380441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
42390441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4240ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4241a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
4242ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4243ac27a0ecSDave Kleikamp #endif
4244617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4245ac27a0ecSDave Kleikamp 	brelse(bh);
4246ac27a0ecSDave Kleikamp out_fail:
4247ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4248f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
42495aee0f8aSTheodore Ts'o out_free_base:
4250ac27a0ecSDave Kleikamp 	kfree(sbi);
4251d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
425207aa2ea1SLukas Czerner 	return err ? err : ret;
4253ac27a0ecSDave Kleikamp }
4254ac27a0ecSDave Kleikamp 
4255ac27a0ecSDave Kleikamp /*
4256ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4257ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4258ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4259ac27a0ecSDave Kleikamp  */
4260617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4261ac27a0ecSDave Kleikamp {
4262617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4263ac27a0ecSDave Kleikamp 
4264ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
426530773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
426630773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4267ac27a0ecSDave Kleikamp 
4268a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4269ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4270dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4271ac27a0ecSDave Kleikamp 	else
4272dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
42735bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
42745bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
42755bf5683aSHidehiro Kawai 	else
42765bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4277a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4278ac27a0ecSDave Kleikamp }
4279ac27a0ecSDave Kleikamp 
4280c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4281ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4282ac27a0ecSDave Kleikamp {
4283ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4284ac27a0ecSDave Kleikamp 
4285c6cb7e77SEric Whitney 	/*
4286c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4287c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4288c6cb7e77SEric Whitney 	 * will try to delete it.
4289c6cb7e77SEric Whitney 	 */
42901d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
42911d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4292b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4293ac27a0ecSDave Kleikamp 		return NULL;
4294ac27a0ecSDave Kleikamp 	}
4295ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4296ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4297ac27a0ecSDave Kleikamp 		iput(journal_inode);
4298b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4299ac27a0ecSDave Kleikamp 		return NULL;
4300ac27a0ecSDave Kleikamp 	}
4301ac27a0ecSDave Kleikamp 
4302e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4303ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
43041d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4305b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4306ac27a0ecSDave Kleikamp 		iput(journal_inode);
4307ac27a0ecSDave Kleikamp 		return NULL;
4308ac27a0ecSDave Kleikamp 	}
4309c6cb7e77SEric Whitney 	return journal_inode;
4310c6cb7e77SEric Whitney }
4311c6cb7e77SEric Whitney 
4312c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4313c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4314c6cb7e77SEric Whitney {
4315c6cb7e77SEric Whitney 	struct inode *journal_inode;
4316c6cb7e77SEric Whitney 	journal_t *journal;
4317c6cb7e77SEric Whitney 
4318c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4319c6cb7e77SEric Whitney 
4320c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4321c6cb7e77SEric Whitney 	if (!journal_inode)
4322c6cb7e77SEric Whitney 		return NULL;
4323ac27a0ecSDave Kleikamp 
4324dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4325ac27a0ecSDave Kleikamp 	if (!journal) {
4326b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4327ac27a0ecSDave Kleikamp 		iput(journal_inode);
4328ac27a0ecSDave Kleikamp 		return NULL;
4329ac27a0ecSDave Kleikamp 	}
4330ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4331617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4332ac27a0ecSDave Kleikamp 	return journal;
4333ac27a0ecSDave Kleikamp }
4334ac27a0ecSDave Kleikamp 
4335617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4336ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4337ac27a0ecSDave Kleikamp {
4338ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4339ac27a0ecSDave Kleikamp 	journal_t *journal;
4340617ba13bSMingming Cao 	ext4_fsblk_t start;
4341617ba13bSMingming Cao 	ext4_fsblk_t len;
4342ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4343617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4344ac27a0ecSDave Kleikamp 	unsigned long offset;
4345617ba13bSMingming Cao 	struct ext4_super_block *es;
4346ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4347ac27a0ecSDave Kleikamp 
4348e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
43490390131bSFrank Mayhar 
4350b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4351ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4352ac27a0ecSDave Kleikamp 		return NULL;
4353ac27a0ecSDave Kleikamp 
4354ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4355e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4356ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4357b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4358b31e1552SEric Sandeen 			"blocksize too small for journal device");
4359ac27a0ecSDave Kleikamp 		goto out_bdev;
4360ac27a0ecSDave Kleikamp 	}
4361ac27a0ecSDave Kleikamp 
4362617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4363617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4364ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4365ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4366b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4367b31e1552SEric Sandeen 		       "external journal");
4368ac27a0ecSDave Kleikamp 		goto out_bdev;
4369ac27a0ecSDave Kleikamp 	}
4370ac27a0ecSDave Kleikamp 
43712716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4372617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4373ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4374617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4375b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4376b31e1552SEric Sandeen 					"bad superblock");
4377ac27a0ecSDave Kleikamp 		brelse(bh);
4378ac27a0ecSDave Kleikamp 		goto out_bdev;
4379ac27a0ecSDave Kleikamp 	}
4380ac27a0ecSDave Kleikamp 
4381df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4382df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4383df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4384df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4385df4763beSDarrick J. Wong 				       "corrupt superblock");
4386df4763beSDarrick J. Wong 		brelse(bh);
4387df4763beSDarrick J. Wong 		goto out_bdev;
4388df4763beSDarrick J. Wong 	}
4389df4763beSDarrick J. Wong 
4390617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4391b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4392ac27a0ecSDave Kleikamp 		brelse(bh);
4393ac27a0ecSDave Kleikamp 		goto out_bdev;
4394ac27a0ecSDave Kleikamp 	}
4395ac27a0ecSDave Kleikamp 
4396bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4397ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4398ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4399ac27a0ecSDave Kleikamp 
4400dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4401ac27a0ecSDave Kleikamp 					start, len, blocksize);
4402ac27a0ecSDave Kleikamp 	if (!journal) {
4403b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4404ac27a0ecSDave Kleikamp 		goto out_bdev;
4405ac27a0ecSDave Kleikamp 	}
4406ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4407dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4408ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4409ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4410b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4411ac27a0ecSDave Kleikamp 		goto out_journal;
4412ac27a0ecSDave Kleikamp 	}
4413ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4414b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4415b31e1552SEric Sandeen 					"user (unsupported) - %d",
4416ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4417ac27a0ecSDave Kleikamp 		goto out_journal;
4418ac27a0ecSDave Kleikamp 	}
4419617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4420617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4421ac27a0ecSDave Kleikamp 	return journal;
44220b8e58a1SAndreas Dilger 
4423ac27a0ecSDave Kleikamp out_journal:
4424dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4425ac27a0ecSDave Kleikamp out_bdev:
4426617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4427ac27a0ecSDave Kleikamp 	return NULL;
4428ac27a0ecSDave Kleikamp }
4429ac27a0ecSDave Kleikamp 
4430617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4431617ba13bSMingming Cao 			     struct ext4_super_block *es,
4432ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4433ac27a0ecSDave Kleikamp {
4434ac27a0ecSDave Kleikamp 	journal_t *journal;
4435ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4436ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4437ac27a0ecSDave Kleikamp 	int err = 0;
4438ac27a0ecSDave Kleikamp 	int really_read_only;
4439ac27a0ecSDave Kleikamp 
4440e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
44410390131bSFrank Mayhar 
4442ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4443ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4444b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4445b31e1552SEric Sandeen 			"numbers have changed");
4446ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4447ac27a0ecSDave Kleikamp 	} else
4448ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4449ac27a0ecSDave Kleikamp 
4450ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4451ac27a0ecSDave Kleikamp 
4452ac27a0ecSDave Kleikamp 	/*
4453ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4454ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4455ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4456ac27a0ecSDave Kleikamp 	 */
4457e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4458ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
4459b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4460b31e1552SEric Sandeen 					"required on readonly filesystem");
4461ac27a0ecSDave Kleikamp 			if (really_read_only) {
4462b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4463b31e1552SEric Sandeen 					"unavailable, cannot proceed");
4464ac27a0ecSDave Kleikamp 				return -EROFS;
4465ac27a0ecSDave Kleikamp 			}
4466b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4467b31e1552SEric Sandeen 			       "be enabled during recovery");
4468ac27a0ecSDave Kleikamp 		}
4469ac27a0ecSDave Kleikamp 	}
4470ac27a0ecSDave Kleikamp 
4471ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4472b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4473b31e1552SEric Sandeen 		       "and inode journals!");
4474ac27a0ecSDave Kleikamp 		return -EINVAL;
4475ac27a0ecSDave Kleikamp 	}
4476ac27a0ecSDave Kleikamp 
4477ac27a0ecSDave Kleikamp 	if (journal_inum) {
4478617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4479ac27a0ecSDave Kleikamp 			return -EINVAL;
4480ac27a0ecSDave Kleikamp 	} else {
4481617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4482ac27a0ecSDave Kleikamp 			return -EINVAL;
4483ac27a0ecSDave Kleikamp 	}
4484ac27a0ecSDave Kleikamp 
448590576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4486b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
44874776004fSTheodore Ts'o 
4488e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
4489dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
44901c13d5c0STheodore Ts'o 	if (!err) {
44911c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
44921c13d5c0STheodore Ts'o 		if (save)
44931c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
44941c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4495dab291afSMingming Cao 		err = jbd2_journal_load(journal);
44961c13d5c0STheodore Ts'o 		if (save)
44971c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
44981c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
44991c13d5c0STheodore Ts'o 		kfree(save);
45001c13d5c0STheodore Ts'o 	}
4501ac27a0ecSDave Kleikamp 
4502ac27a0ecSDave Kleikamp 	if (err) {
4503b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4504dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4505ac27a0ecSDave Kleikamp 		return err;
4506ac27a0ecSDave Kleikamp 	}
4507ac27a0ecSDave Kleikamp 
4508617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4509617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4510ac27a0ecSDave Kleikamp 
4511c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4512ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4513ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4514ac27a0ecSDave Kleikamp 
4515ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4516e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4517ac27a0ecSDave Kleikamp 	}
4518ac27a0ecSDave Kleikamp 
4519ac27a0ecSDave Kleikamp 	return 0;
4520ac27a0ecSDave Kleikamp }
4521ac27a0ecSDave Kleikamp 
4522e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4523ac27a0ecSDave Kleikamp {
4524e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4525617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4526c4be0c1dSTakashi Sato 	int error = 0;
4527ac27a0ecSDave Kleikamp 
4528bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4529c4be0c1dSTakashi Sato 		return error;
453071290b36STheodore Ts'o 	/*
453171290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
453271290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
453371290b36STheodore Ts'o 	 * write time when we are mounting the root file system
453471290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
453571290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
453671290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
453771290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
453871290b36STheodore Ts'o 	 * to complain and force a full file system check.
453971290b36STheodore Ts'o 	 */
454071290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4541ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4542f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4543afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4544afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4545afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4546afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4547f613dfcbSTheodore Ts'o 	else
4548f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4549f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
4550d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
455157042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
455257042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
455357042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
4554d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
45557f93cff9STheodore Ts'o 		es->s_free_inodes_count =
45567f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
45575d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
4558ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
455906db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
45601566a48aSTheodore Ts'o 	if (sync)
45614743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
45624743f839SPranay Kr. Srivastava 	if (buffer_write_io_error(sbh)) {
45634743f839SPranay Kr. Srivastava 		/*
45644743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
45654743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
45664743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
45674743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
45684743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
45694743f839SPranay Kr. Srivastava 		 * write and hope for the best.
45704743f839SPranay Kr. Srivastava 		 */
45714743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
45724743f839SPranay Kr. Srivastava 		       "superblock detected");
45734743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
45744743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
45754743f839SPranay Kr. Srivastava 	}
4576ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4577914258bfSTheodore Ts'o 	if (sync) {
45781566a48aSTheodore Ts'o 		unlock_buffer(sbh);
4579564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
4580564bc402SDaeho Jeong 			test_opt(sb, BARRIER) ? WRITE_FUA : WRITE_SYNC);
4581c4be0c1dSTakashi Sato 		if (error)
4582c4be0c1dSTakashi Sato 			return error;
4583c4be0c1dSTakashi Sato 
4584c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4585c4be0c1dSTakashi Sato 		if (error) {
4586b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4587b31e1552SEric Sandeen 			       "superblock");
4588914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4589914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4590914258bfSTheodore Ts'o 		}
4591914258bfSTheodore Ts'o 	}
4592c4be0c1dSTakashi Sato 	return error;
4593ac27a0ecSDave Kleikamp }
4594ac27a0ecSDave Kleikamp 
4595ac27a0ecSDave Kleikamp /*
4596ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4597ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4598ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4599ac27a0ecSDave Kleikamp  */
4600617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4601617ba13bSMingming Cao 					struct ext4_super_block *es)
4602ac27a0ecSDave Kleikamp {
4603617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4604ac27a0ecSDave Kleikamp 
4605e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
46060390131bSFrank Mayhar 		BUG_ON(journal != NULL);
46070390131bSFrank Mayhar 		return;
46080390131bSFrank Mayhar 	}
4609dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
46107ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
46117ffe1ea8SHidehiro Kawai 		goto out;
46127ffe1ea8SHidehiro Kawai 
4613e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb) &&
4614ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4615e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
4616e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4617ac27a0ecSDave Kleikamp 	}
46187ffe1ea8SHidehiro Kawai 
46197ffe1ea8SHidehiro Kawai out:
4620dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4621ac27a0ecSDave Kleikamp }
4622ac27a0ecSDave Kleikamp 
4623ac27a0ecSDave Kleikamp /*
4624ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4625ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4626ac27a0ecSDave Kleikamp  * main filesystem now.
4627ac27a0ecSDave Kleikamp  */
4628617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4629617ba13bSMingming Cao 				   struct ext4_super_block *es)
4630ac27a0ecSDave Kleikamp {
4631ac27a0ecSDave Kleikamp 	journal_t *journal;
4632ac27a0ecSDave Kleikamp 	int j_errno;
4633ac27a0ecSDave Kleikamp 	const char *errstr;
4634ac27a0ecSDave Kleikamp 
4635e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
46360390131bSFrank Mayhar 
4637617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4638ac27a0ecSDave Kleikamp 
4639ac27a0ecSDave Kleikamp 	/*
4640ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4641617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4642ac27a0ecSDave Kleikamp 	 */
4643ac27a0ecSDave Kleikamp 
4644dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4645ac27a0ecSDave Kleikamp 	if (j_errno) {
4646ac27a0ecSDave Kleikamp 		char nbuf[16];
4647ac27a0ecSDave Kleikamp 
4648617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
464912062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4650ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
465112062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4652ac27a0ecSDave Kleikamp 
4653617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4654617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4655e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4656ac27a0ecSDave Kleikamp 
4657dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4658d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
4659ac27a0ecSDave Kleikamp 	}
4660ac27a0ecSDave Kleikamp }
4661ac27a0ecSDave Kleikamp 
4662ac27a0ecSDave Kleikamp /*
4663ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4664ac27a0ecSDave Kleikamp  * and wait on the commit.
4665ac27a0ecSDave Kleikamp  */
4666617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4667ac27a0ecSDave Kleikamp {
4668ac27a0ecSDave Kleikamp 	journal_t *journal;
4669ac27a0ecSDave Kleikamp 
4670ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4671ac27a0ecSDave Kleikamp 		return 0;
4672ac27a0ecSDave Kleikamp 
4673617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4674b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
4675ac27a0ecSDave Kleikamp }
4676ac27a0ecSDave Kleikamp 
4677617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4678ac27a0ecSDave Kleikamp {
467914ce0cb4STheodore Ts'o 	int ret = 0;
46809eddacf9SJan Kara 	tid_t target;
468106a407f1SDmitry Monakhov 	bool needs_barrier = false;
46828d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4683ac27a0ecSDave Kleikamp 
46849bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
46852e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
4686a1177825SJan Kara 	/*
4687a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
4688a1177825SJan Kara 	 * no dirty dquots
4689a1177825SJan Kara 	 */
4690a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
469106a407f1SDmitry Monakhov 	/*
469206a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
469306a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
469406a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
469506a407f1SDmitry Monakhov 	 */
4696bda32530STheodore Ts'o 	if (sbi->s_journal) {
469706a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
469806a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
469906a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
470006a407f1SDmitry Monakhov 			needs_barrier = true;
470106a407f1SDmitry Monakhov 
47028d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4703ac27a0ecSDave Kleikamp 			if (wait)
4704bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
4705bda32530STheodore Ts'o 							   target);
47060390131bSFrank Mayhar 		}
4707bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
4708bda32530STheodore Ts'o 		needs_barrier = true;
470906a407f1SDmitry Monakhov 	if (needs_barrier) {
471006a407f1SDmitry Monakhov 		int err;
471106a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
471206a407f1SDmitry Monakhov 		if (!ret)
471306a407f1SDmitry Monakhov 			ret = err;
471406a407f1SDmitry Monakhov 	}
471506a407f1SDmitry Monakhov 
471606a407f1SDmitry Monakhov 	return ret;
471706a407f1SDmitry Monakhov }
471806a407f1SDmitry Monakhov 
4719ac27a0ecSDave Kleikamp /*
4720ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4721ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4722be4f27d3SYongqiang Yang  *
4723be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
47248e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
47258e8ad8a5SJan Kara  * modifications.
4726ac27a0ecSDave Kleikamp  */
4727c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4728ac27a0ecSDave Kleikamp {
4729c4be0c1dSTakashi Sato 	int error = 0;
4730c4be0c1dSTakashi Sato 	journal_t *journal;
4731ac27a0ecSDave Kleikamp 
47329ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
47339ca92389STheodore Ts'o 		return 0;
47349ca92389STheodore Ts'o 
4735c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4736ac27a0ecSDave Kleikamp 
4737bb044576STheodore Ts'o 	if (journal) {
4738ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
4739dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
47407ffe1ea8SHidehiro Kawai 
47417ffe1ea8SHidehiro Kawai 		/*
4742bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
4743bb044576STheodore Ts'o 		 * flush the journal.
47447ffe1ea8SHidehiro Kawai 		 */
4745c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
47466b0310fbSEric Sandeen 		if (error < 0)
47476b0310fbSEric Sandeen 			goto out;
4748ac27a0ecSDave Kleikamp 
4749ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
4750e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
4751c642dc9eSEric Sandeen 	}
4752c642dc9eSEric Sandeen 
4753e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
47546b0310fbSEric Sandeen out:
4755bb044576STheodore Ts'o 	if (journal)
47568e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
4757bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
47586b0310fbSEric Sandeen 	return error;
4759ac27a0ecSDave Kleikamp }
4760ac27a0ecSDave Kleikamp 
4761ac27a0ecSDave Kleikamp /*
4762ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4763ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4764ac27a0ecSDave Kleikamp  */
4765c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4766ac27a0ecSDave Kleikamp {
47679ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
47689ca92389STheodore Ts'o 		return 0;
47699ca92389STheodore Ts'o 
4770c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
47719ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
4772e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
4773c642dc9eSEric Sandeen 	}
4774c642dc9eSEric Sandeen 
4775e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4776c4be0c1dSTakashi Sato 	return 0;
4777ac27a0ecSDave Kleikamp }
4778ac27a0ecSDave Kleikamp 
4779673c6100STheodore Ts'o /*
4780673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4781673c6100STheodore Ts'o  */
4782673c6100STheodore Ts'o struct ext4_mount_options {
4783673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4784a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
478508cefc7aSEric W. Biederman 	kuid_t s_resuid;
478608cefc7aSEric W. Biederman 	kgid_t s_resgid;
4787673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4788673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4789673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4790673c6100STheodore Ts'o 	int s_jquota_fmt;
4791a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
4792673c6100STheodore Ts'o #endif
4793673c6100STheodore Ts'o };
4794673c6100STheodore Ts'o 
4795617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4796ac27a0ecSDave Kleikamp {
4797617ba13bSMingming Cao 	struct ext4_super_block *es;
4798617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4799ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4800617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4801c79d967dSChristoph Hellwig 	int enable_quota = 0;
48028a266467STheodore Ts'o 	ext4_group_t g;
4803b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4804c5e06d10SJohann Lombardi 	int err = 0;
4805ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
480603dafb5fSChen Gang 	int i, j;
4807ac27a0ecSDave Kleikamp #endif
4808d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4809ac27a0ecSDave Kleikamp 
4810ac27a0ecSDave Kleikamp 	/* Store the original options */
4811ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4812ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4813a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4814ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4815ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4816ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
481730773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
481830773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4819ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4820ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4821a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
482203dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
482303dafb5fSChen Gang 			old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
482403dafb5fSChen Gang 							 GFP_KERNEL);
482503dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
482603dafb5fSChen Gang 				for (j = 0; j < i; j++)
482703dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
48283e36a163SWei Yongjun 				kfree(orig_data);
482903dafb5fSChen Gang 				return -ENOMEM;
483003dafb5fSChen Gang 			}
483103dafb5fSChen Gang 		} else
483203dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
4833ac27a0ecSDave Kleikamp #endif
4834b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4835b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4836ac27a0ecSDave Kleikamp 
4837661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
4838ac27a0ecSDave Kleikamp 		err = -EINVAL;
4839ac27a0ecSDave Kleikamp 		goto restore_opts;
4840ac27a0ecSDave Kleikamp 	}
4841ac27a0ecSDave Kleikamp 
48426b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
48436b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
48446b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
48452d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
48462d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
4847c6d3d56dSDarrick J. Wong 	}
4848c6d3d56dSDarrick J. Wong 
48496ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
48506ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
48516ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
48526ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
48536ae6514bSPiotr Sarna 			err = -EINVAL;
48546ae6514bSPiotr Sarna 			goto restore_opts;
48556ae6514bSPiotr Sarna 		}
48566ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
48576ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
48586ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
48596ae6514bSPiotr Sarna 			err = -EINVAL;
48606ae6514bSPiotr Sarna 			goto restore_opts;
48616ae6514bSPiotr Sarna 		}
4862923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
4863923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4864923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4865923ae0ffSRoss Zwisler 			err = -EINVAL;
4866923ae0ffSRoss Zwisler 			goto restore_opts;
4867923ae0ffSRoss Zwisler 		}
4868923ae0ffSRoss Zwisler 	}
4869923ae0ffSRoss Zwisler 
4870923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
4871923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
4872923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
4873923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
48746ae6514bSPiotr Sarna 	}
48756ae6514bSPiotr Sarna 
48764ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4877c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4878ac27a0ecSDave Kleikamp 
4879ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4880482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4881ac27a0ecSDave Kleikamp 
4882ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4883ac27a0ecSDave Kleikamp 
4884b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4885617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4886b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4887b3881f74STheodore Ts'o 	}
4888ac27a0ecSDave Kleikamp 
4889a2fd66d0STheodore Ts'o 	if (*flags & MS_LAZYTIME)
4890a2fd66d0STheodore Ts'o 		sb->s_flags |= MS_LAZYTIME;
4891a2fd66d0STheodore Ts'o 
4892661aa520SEric Sandeen 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
48934ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4894ac27a0ecSDave Kleikamp 			err = -EROFS;
4895ac27a0ecSDave Kleikamp 			goto restore_opts;
4896ac27a0ecSDave Kleikamp 		}
4897ac27a0ecSDave Kleikamp 
4898ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
489938c03b34STheodore Ts'o 			err = sync_filesystem(sb);
490038c03b34STheodore Ts'o 			if (err < 0)
490138c03b34STheodore Ts'o 				goto restore_opts;
49020f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
49030f0dd62fSChristoph Hellwig 			if (err < 0)
4904c79d967dSChristoph Hellwig 				goto restore_opts;
4905c79d967dSChristoph Hellwig 
4906ac27a0ecSDave Kleikamp 			/*
4907ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4908ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4909ac27a0ecSDave Kleikamp 			 */
4910ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4911ac27a0ecSDave Kleikamp 
4912ac27a0ecSDave Kleikamp 			/*
4913ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4914ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4915ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4916ac27a0ecSDave Kleikamp 			 */
4917617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4918617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4919ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4920ac27a0ecSDave Kleikamp 
4921a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4922617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4923ac27a0ecSDave Kleikamp 		} else {
4924a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4925e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
49262cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
4927ac27a0ecSDave Kleikamp 				err = -EROFS;
4928ac27a0ecSDave Kleikamp 				goto restore_opts;
4929ac27a0ecSDave Kleikamp 			}
4930ead6596bSEric Sandeen 			/*
49318a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
49320b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
49338a266467STheodore Ts'o 			 */
49348a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
49358a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
49368a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
49378a266467STheodore Ts'o 
4938feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
4939b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4940b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
4941e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
49428a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
49436a797d27SDarrick J. Wong 					err = -EFSBADCRC;
49448a266467STheodore Ts'o 					goto restore_opts;
49458a266467STheodore Ts'o 				}
49468a266467STheodore Ts'o 			}
49478a266467STheodore Ts'o 
49488a266467STheodore Ts'o 			/*
4949ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4950ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4951ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4952ead6596bSEric Sandeen 			 */
4953ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4954b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4955ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4956ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4957b31e1552SEric Sandeen 				       "umount/remount instead");
4958ead6596bSEric Sandeen 				err = -EINVAL;
4959ead6596bSEric Sandeen 				goto restore_opts;
4960ead6596bSEric Sandeen 			}
4961ead6596bSEric Sandeen 
4962ac27a0ecSDave Kleikamp 			/*
4963ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4964ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4965ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4966ac27a0ecSDave Kleikamp 			 * the partition.)
4967ac27a0ecSDave Kleikamp 			 */
49680390131bSFrank Mayhar 			if (sbi->s_journal)
4969617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4970ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4971617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4972ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4973e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
4974c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
4975c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
4976c5e06d10SJohann Lombardi 					err = -EROFS;
4977c5e06d10SJohann Lombardi 					goto restore_opts;
4978c5e06d10SJohann Lombardi 				}
4979c79d967dSChristoph Hellwig 			enable_quota = 1;
4980ac27a0ecSDave Kleikamp 		}
4981ac27a0ecSDave Kleikamp 	}
4982bfff6873SLukas Czerner 
4983bfff6873SLukas Czerner 	/*
4984bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4985bfff6873SLukas Czerner 	 * current settings
4986bfff6873SLukas Czerner 	 */
4987bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4988bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4989bfff6873SLukas Czerner 	else {
4990bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4991bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4992bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4993bfff6873SLukas Czerner 	}
4994bfff6873SLukas Czerner 
49956fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
4996d096ad0fSMichael Tokarev 	if (sbi->s_journal == NULL && !(old_sb_flags & MS_RDONLY))
4997e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
49980390131bSFrank Mayhar 
4999ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5000ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5001a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5002ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
50037c319d32SAditya Kali 	if (enable_quota) {
50047c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
50050f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5006e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
50077c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
500807724f98STheodore Ts'o 			if (err)
50097c319d32SAditya Kali 				goto restore_opts;
50107c319d32SAditya Kali 		}
50117c319d32SAditya Kali 	}
50127c319d32SAditya Kali #endif
5013d4c402d9SCurt Wohlgemuth 
5014a26f4992STheodore Ts'o 	*flags = (*flags & ~MS_LAZYTIME) | (sb->s_flags & MS_LAZYTIME);
5015d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5016d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5017ac27a0ecSDave Kleikamp 	return 0;
50180b8e58a1SAndreas Dilger 
5019ac27a0ecSDave Kleikamp restore_opts:
5020ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5021ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5022a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5023ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5024ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5025ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
502630773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
502730773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5028ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5029ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5030a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
5031ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
5032ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
5033ac27a0ecSDave Kleikamp 	}
5034ac27a0ecSDave Kleikamp #endif
5035d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5036ac27a0ecSDave Kleikamp 	return err;
5037ac27a0ecSDave Kleikamp }
5038ac27a0ecSDave Kleikamp 
5039689c958cSLi Xi #ifdef CONFIG_QUOTA
5040689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5041689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5042689c958cSLi Xi {
5043689c958cSLi Xi 	struct kqid qid;
5044689c958cSLi Xi 	struct dquot *dquot;
5045689c958cSLi Xi 	u64 limit;
5046689c958cSLi Xi 	u64 curblock;
5047689c958cSLi Xi 
5048689c958cSLi Xi 	qid = make_kqid_projid(projid);
5049689c958cSLi Xi 	dquot = dqget(sb, qid);
5050689c958cSLi Xi 	if (IS_ERR(dquot))
5051689c958cSLi Xi 		return PTR_ERR(dquot);
5052689c958cSLi Xi 	spin_lock(&dq_data_lock);
5053689c958cSLi Xi 
5054689c958cSLi Xi 	limit = (dquot->dq_dqb.dqb_bsoftlimit ?
5055689c958cSLi Xi 		 dquot->dq_dqb.dqb_bsoftlimit :
5056689c958cSLi Xi 		 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
5057689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5058689c958cSLi Xi 		curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
5059689c958cSLi Xi 		buf->f_blocks = limit;
5060689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5061689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5062689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5063689c958cSLi Xi 	}
5064689c958cSLi Xi 
5065689c958cSLi Xi 	limit = dquot->dq_dqb.dqb_isoftlimit ?
5066689c958cSLi Xi 		dquot->dq_dqb.dqb_isoftlimit :
5067689c958cSLi Xi 		dquot->dq_dqb.dqb_ihardlimit;
5068689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5069689c958cSLi Xi 		buf->f_files = limit;
5070689c958cSLi Xi 		buf->f_ffree =
5071689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5072689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5073689c958cSLi Xi 	}
5074689c958cSLi Xi 
5075689c958cSLi Xi 	spin_unlock(&dq_data_lock);
5076689c958cSLi Xi 	dqput(dquot);
5077689c958cSLi Xi 	return 0;
5078689c958cSLi Xi }
5079689c958cSLi Xi #endif
5080689c958cSLi Xi 
5081617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5082ac27a0ecSDave Kleikamp {
5083ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5084617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5085617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
508627dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5087960cc398SPekka Enberg 	u64 fsid;
5088d02a9391SKazuya Mio 	s64 bfree;
508927dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5090ac27a0ecSDave Kleikamp 
5091952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5092952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5093ac27a0ecSDave Kleikamp 
5094617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5095ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5096b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
509757042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
509857042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5099d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
510057042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
510127dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
510227dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
510327dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5104ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5105ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
510652d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5107617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5108960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5109960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5110960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5111960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
51120b8e58a1SAndreas Dilger 
5113689c958cSLi Xi #ifdef CONFIG_QUOTA
5114689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5115689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5116689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5117689c958cSLi Xi #endif
5118ac27a0ecSDave Kleikamp 	return 0;
5119ac27a0ecSDave Kleikamp }
5120ac27a0ecSDave Kleikamp 
51210b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
51220b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
5123ac27a0ecSDave Kleikamp  * Process 1                         Process 2
5124617ba13bSMingming Cao  * ext4_create()                     quota_sync()
5125dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
5126871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
5127dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
5128ac27a0ecSDave Kleikamp  *
5129ac27a0ecSDave Kleikamp  */
5130ac27a0ecSDave Kleikamp 
5131ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5132ac27a0ecSDave Kleikamp 
5133ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5134ac27a0ecSDave Kleikamp {
51354c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5136ac27a0ecSDave Kleikamp }
5137ac27a0ecSDave Kleikamp 
5138617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5139ac27a0ecSDave Kleikamp {
5140ac27a0ecSDave Kleikamp 	int ret, err;
5141ac27a0ecSDave Kleikamp 	handle_t *handle;
5142ac27a0ecSDave Kleikamp 	struct inode *inode;
5143ac27a0ecSDave Kleikamp 
5144ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
51459924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5146617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5147ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5148ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5149ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5150617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5151ac27a0ecSDave Kleikamp 	if (!ret)
5152ac27a0ecSDave Kleikamp 		ret = err;
5153ac27a0ecSDave Kleikamp 	return ret;
5154ac27a0ecSDave Kleikamp }
5155ac27a0ecSDave Kleikamp 
5156617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5157ac27a0ecSDave Kleikamp {
5158ac27a0ecSDave Kleikamp 	int ret, err;
5159ac27a0ecSDave Kleikamp 	handle_t *handle;
5160ac27a0ecSDave Kleikamp 
51619924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5162617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5163ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5164ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5165ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5166617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5167ac27a0ecSDave Kleikamp 	if (!ret)
5168ac27a0ecSDave Kleikamp 		ret = err;
5169ac27a0ecSDave Kleikamp 	return ret;
5170ac27a0ecSDave Kleikamp }
5171ac27a0ecSDave Kleikamp 
5172617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5173ac27a0ecSDave Kleikamp {
5174ac27a0ecSDave Kleikamp 	int ret, err;
5175ac27a0ecSDave Kleikamp 	handle_t *handle;
5176ac27a0ecSDave Kleikamp 
51779924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5178617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
51799c3013e9SJan Kara 	if (IS_ERR(handle)) {
51809c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
51819c3013e9SJan Kara 		dquot_release(dquot);
5182ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
51839c3013e9SJan Kara 	}
5184ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5185617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5186ac27a0ecSDave Kleikamp 	if (!ret)
5187ac27a0ecSDave Kleikamp 		ret = err;
5188ac27a0ecSDave Kleikamp 	return ret;
5189ac27a0ecSDave Kleikamp }
5190ac27a0ecSDave Kleikamp 
5191617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5192ac27a0ecSDave Kleikamp {
5193262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5194262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5195262b4662SJan Kara 
51962c8be6b2SJan Kara 	/* Are we journaling quotas? */
5197e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5198262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5199ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5200617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5201ac27a0ecSDave Kleikamp 	} else {
5202ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5203ac27a0ecSDave Kleikamp 	}
5204ac27a0ecSDave Kleikamp }
5205ac27a0ecSDave Kleikamp 
5206617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5207ac27a0ecSDave Kleikamp {
5208ac27a0ecSDave Kleikamp 	int ret, err;
5209ac27a0ecSDave Kleikamp 	handle_t *handle;
5210ac27a0ecSDave Kleikamp 
5211ac27a0ecSDave Kleikamp 	/* Data block + inode block */
52122b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5213ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5214ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5215ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5216617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5217ac27a0ecSDave Kleikamp 	if (!ret)
5218ac27a0ecSDave Kleikamp 		ret = err;
5219ac27a0ecSDave Kleikamp 	return ret;
5220ac27a0ecSDave Kleikamp }
5221ac27a0ecSDave Kleikamp 
5222ac27a0ecSDave Kleikamp /*
5223ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5224ac27a0ecSDave Kleikamp  * the quota file and such...
5225ac27a0ecSDave Kleikamp  */
5226617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5227ac27a0ecSDave Kleikamp {
5228287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
5229617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5230ac27a0ecSDave Kleikamp }
5231ac27a0ecSDave Kleikamp 
5232daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5233daf647d2STheodore Ts'o {
5234daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5235daf647d2STheodore Ts'o 
5236daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5237daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5238daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5239daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5240daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5241daf647d2STheodore Ts'o 	 */
5242daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5243daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5244daf647d2STheodore Ts'o }
5245daf647d2STheodore Ts'o 
5246ac27a0ecSDave Kleikamp /*
5247ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5248ac27a0ecSDave Kleikamp  */
5249617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
5250f00c9e44SJan Kara 			 struct path *path)
5251ac27a0ecSDave Kleikamp {
5252ac27a0ecSDave Kleikamp 	int err;
5253ac27a0ecSDave Kleikamp 
5254ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5255ac27a0ecSDave Kleikamp 		return -EINVAL;
52560623543bSJan Kara 
5257ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5258d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5259ac27a0ecSDave Kleikamp 		return -EXDEV;
52600623543bSJan Kara 	/* Journaling quota? */
52610623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
52622b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5263f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5264b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5265b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5266b31e1552SEric Sandeen 				"Journaled quota will not work");
52670623543bSJan Kara 	}
52680623543bSJan Kara 
52690623543bSJan Kara 	/*
52700623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
52710623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
52720623543bSJan Kara 	 */
52730390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
52742b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
52750623543bSJan Kara 		/*
52760623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
52770623543bSJan Kara 		 * otherwise be livelocked...
52780623543bSJan Kara 		 */
52790623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
52807ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
52810623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5282f00c9e44SJan Kara 		if (err)
52837ffe1ea8SHidehiro Kawai 			return err;
52847ffe1ea8SHidehiro Kawai 	}
5285daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5286daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5287daf647d2STheodore Ts'o 	if (err)
5288daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5289daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5290daf647d2STheodore Ts'o 	return err;
5291ac27a0ecSDave Kleikamp }
5292ac27a0ecSDave Kleikamp 
52937c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
52947c319d32SAditya Kali 			     unsigned int flags)
52957c319d32SAditya Kali {
52967c319d32SAditya Kali 	int err;
52977c319d32SAditya Kali 	struct inode *qf_inode;
5298a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
52997c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5300689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5301689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
53027c319d32SAditya Kali 	};
53037c319d32SAditya Kali 
5304e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
53057c319d32SAditya Kali 
53067c319d32SAditya Kali 	if (!qf_inums[type])
53077c319d32SAditya Kali 		return -EPERM;
53087c319d32SAditya Kali 
53097c319d32SAditya Kali 	qf_inode = ext4_iget(sb, qf_inums[type]);
53107c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
53117c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
53127c319d32SAditya Kali 		return PTR_ERR(qf_inode);
53137c319d32SAditya Kali 	}
53147c319d32SAditya Kali 
5315bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5316bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5317daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
53187c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
53197c319d32SAditya Kali 	iput(qf_inode);
5320daf647d2STheodore Ts'o 	if (err)
5321daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
53227c319d32SAditya Kali 
53237c319d32SAditya Kali 	return err;
53247c319d32SAditya Kali }
53257c319d32SAditya Kali 
53267c319d32SAditya Kali /* Enable usage tracking for all quota types. */
53277c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
53287c319d32SAditya Kali {
53297c319d32SAditya Kali 	int type, err = 0;
5330a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
53317c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5332689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5333689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
53347c319d32SAditya Kali 	};
533549da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
533649da9392SJan Kara 		test_opt(sb, USRQUOTA),
533749da9392SJan Kara 		test_opt(sb, GRPQUOTA),
533849da9392SJan Kara 		test_opt(sb, PRJQUOTA),
533949da9392SJan Kara 	};
53407c319d32SAditya Kali 
53417c319d32SAditya Kali 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
5342a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
53437c319d32SAditya Kali 		if (qf_inums[type]) {
53447c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
534549da9392SJan Kara 				DQUOT_USAGE_ENABLED |
534649da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
53477c319d32SAditya Kali 			if (err) {
53487c319d32SAditya Kali 				ext4_warning(sb,
534972ba7450STheodore Ts'o 					"Failed to enable quota tracking "
535072ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
535172ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
53527c319d32SAditya Kali 				return err;
53537c319d32SAditya Kali 			}
53547c319d32SAditya Kali 		}
53557c319d32SAditya Kali 	}
53567c319d32SAditya Kali 	return 0;
53577c319d32SAditya Kali }
53587c319d32SAditya Kali 
5359ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5360ca0e05e4SDmitry Monakhov {
536121f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
536221f97697SJan Kara 	handle_t *handle;
536321f97697SJan Kara 
536487009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
536587009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
536687009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5367ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5368ca0e05e4SDmitry Monakhov 
53690b268590SAmir Goldstein 	if (!inode)
53700b268590SAmir Goldstein 		goto out;
53710b268590SAmir Goldstein 
537221f97697SJan Kara 	/* Update modification times of quota files when userspace can
537321f97697SJan Kara 	 * start looking at them */
53749924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
537521f97697SJan Kara 	if (IS_ERR(handle))
537621f97697SJan Kara 		goto out;
5377eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
537821f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
537921f97697SJan Kara 	ext4_journal_stop(handle);
538021f97697SJan Kara 
538121f97697SJan Kara out:
5382ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5383ca0e05e4SDmitry Monakhov }
5384ca0e05e4SDmitry Monakhov 
5385ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5386ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5387ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5388ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5389617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5390ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5391ac27a0ecSDave Kleikamp {
5392ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5393725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5394ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5395ac27a0ecSDave Kleikamp 	int tocopy;
5396ac27a0ecSDave Kleikamp 	size_t toread;
5397ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5398ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5399ac27a0ecSDave Kleikamp 
5400ac27a0ecSDave Kleikamp 	if (off > i_size)
5401ac27a0ecSDave Kleikamp 		return 0;
5402ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5403ac27a0ecSDave Kleikamp 		len = i_size-off;
5404ac27a0ecSDave Kleikamp 	toread = len;
5405ac27a0ecSDave Kleikamp 	while (toread > 0) {
5406ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5407ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
54081c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
54091c215028STheodore Ts'o 		if (IS_ERR(bh))
54101c215028STheodore Ts'o 			return PTR_ERR(bh);
5411ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5412ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5413ac27a0ecSDave Kleikamp 		else
5414ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5415ac27a0ecSDave Kleikamp 		brelse(bh);
5416ac27a0ecSDave Kleikamp 		offset = 0;
5417ac27a0ecSDave Kleikamp 		toread -= tocopy;
5418ac27a0ecSDave Kleikamp 		data += tocopy;
5419ac27a0ecSDave Kleikamp 		blk++;
5420ac27a0ecSDave Kleikamp 	}
5421ac27a0ecSDave Kleikamp 	return len;
5422ac27a0ecSDave Kleikamp }
5423ac27a0ecSDave Kleikamp 
5424ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5425ac27a0ecSDave Kleikamp  * enough credits) */
5426617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5427ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5428ac27a0ecSDave Kleikamp {
5429ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5430725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
54311c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
5432c5e298aeSTheodore Ts'o 	int retries = 0;
5433ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5434ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5435ac27a0ecSDave Kleikamp 
54360390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5437b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5438b31e1552SEric Sandeen 			" cancelled because transaction is not started",
54399c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
54409c3013e9SJan Kara 		return -EIO;
54419c3013e9SJan Kara 	}
544267eeb568SDmitry Monakhov 	/*
544367eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
544467eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
544567eeb568SDmitry Monakhov 	 */
544667eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
544767eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
544867eeb568SDmitry Monakhov 			" cancelled because not block aligned",
544967eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
545067eeb568SDmitry Monakhov 		return -EIO;
545167eeb568SDmitry Monakhov 	}
545267eeb568SDmitry Monakhov 
5453c5e298aeSTheodore Ts'o 	do {
5454c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
5455c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
5456c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
5457c5e298aeSTheodore Ts'o 	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
5458c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
54591c215028STheodore Ts'o 	if (IS_ERR(bh))
54601c215028STheodore Ts'o 		return PTR_ERR(bh);
5461ac27a0ecSDave Kleikamp 	if (!bh)
5462ac27a0ecSDave Kleikamp 		goto out;
54635d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
5464617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5465ac27a0ecSDave Kleikamp 	if (err) {
5466ac27a0ecSDave Kleikamp 		brelse(bh);
54671c215028STheodore Ts'o 		return err;
5468ac27a0ecSDave Kleikamp 	}
5469ac27a0ecSDave Kleikamp 	lock_buffer(bh);
547067eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5471ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5472ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
54730390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5474ac27a0ecSDave Kleikamp 	brelse(bh);
5475ac27a0ecSDave Kleikamp out:
547667eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
547767eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
5478617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
5479617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
548021f97697SJan Kara 	}
548167eeb568SDmitry Monakhov 	return len;
5482ac27a0ecSDave Kleikamp }
5483ac27a0ecSDave Kleikamp 
54848f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid)
54858f0e8746STheodore Ts'o {
54868f0e8746STheodore Ts'o 	const struct quota_format_ops	*ops;
54878f0e8746STheodore Ts'o 
54888f0e8746STheodore Ts'o 	if (!sb_has_quota_loaded(sb, qid->type))
54898f0e8746STheodore Ts'o 		return -ESRCH;
54908f0e8746STheodore Ts'o 	ops = sb_dqopt(sb)->ops[qid->type];
54918f0e8746STheodore Ts'o 	if (!ops || !ops->get_next_id)
54928f0e8746STheodore Ts'o 		return -ENOSYS;
54938f0e8746STheodore Ts'o 	return dquot_get_next_id(sb, qid);
54948f0e8746STheodore Ts'o }
5495ac27a0ecSDave Kleikamp #endif
5496ac27a0ecSDave Kleikamp 
5497152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5498152a0836SAl Viro 		       const char *dev_name, void *data)
5499ac27a0ecSDave Kleikamp {
5500152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
5501ac27a0ecSDave Kleikamp }
5502ac27a0ecSDave Kleikamp 
5503c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
550424b58424STheodore Ts'o static inline void register_as_ext2(void)
550524b58424STheodore Ts'o {
550624b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
550724b58424STheodore Ts'o 	if (err)
550824b58424STheodore Ts'o 		printk(KERN_WARNING
550924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
551024b58424STheodore Ts'o }
551124b58424STheodore Ts'o 
551224b58424STheodore Ts'o static inline void unregister_as_ext2(void)
551324b58424STheodore Ts'o {
551424b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
551524b58424STheodore Ts'o }
55162035e776STheodore Ts'o 
55172035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
55182035e776STheodore Ts'o {
5519e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
55202035e776STheodore Ts'o 		return 0;
55212035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
55222035e776STheodore Ts'o 		return 1;
5523e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
55242035e776STheodore Ts'o 		return 0;
55252035e776STheodore Ts'o 	return 1;
55262035e776STheodore Ts'o }
552724b58424STheodore Ts'o #else
552824b58424STheodore Ts'o static inline void register_as_ext2(void) { }
552924b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
55302035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
553124b58424STheodore Ts'o #endif
553224b58424STheodore Ts'o 
553324b58424STheodore Ts'o static inline void register_as_ext3(void)
553424b58424STheodore Ts'o {
553524b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
553624b58424STheodore Ts'o 	if (err)
553724b58424STheodore Ts'o 		printk(KERN_WARNING
553824b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
553924b58424STheodore Ts'o }
554024b58424STheodore Ts'o 
554124b58424STheodore Ts'o static inline void unregister_as_ext3(void)
554224b58424STheodore Ts'o {
554324b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
554424b58424STheodore Ts'o }
55452035e776STheodore Ts'o 
55462035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
55472035e776STheodore Ts'o {
5548e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
55492035e776STheodore Ts'o 		return 0;
5550e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
55512035e776STheodore Ts'o 		return 0;
55522035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
55532035e776STheodore Ts'o 		return 1;
5554e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
55552035e776STheodore Ts'o 		return 0;
55562035e776STheodore Ts'o 	return 1;
55572035e776STheodore Ts'o }
555824b58424STheodore Ts'o 
555903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
5560ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
556103010a33STheodore Ts'o 	.name		= "ext4",
5562152a0836SAl Viro 	.mount		= ext4_mount,
5563ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
5564ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
5565ac27a0ecSDave Kleikamp };
55667f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
5567ac27a0ecSDave Kleikamp 
5568e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
5569e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5570e9e3bcecSEric Sandeen 
55715dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
5572ac27a0ecSDave Kleikamp {
5573e9e3bcecSEric Sandeen 	int i, err;
5574c9de560dSAlex Tomas 
5575e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
557607c0c5d8SAl Viro 	ext4_li_info = NULL;
557707c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
557807c0c5d8SAl Viro 
55799a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
558012e9b892SDmitry Monakhov 	ext4_check_flag_values();
5581e9e3bcecSEric Sandeen 
5582e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
5583e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
5584e9e3bcecSEric Sandeen 
558551865fdaSZheng Liu 	err = ext4_init_es();
55866fd058f7STheodore Ts'o 	if (err)
55876fd058f7STheodore Ts'o 		return err;
558851865fdaSZheng Liu 
558951865fdaSZheng Liu 	err = ext4_init_pageio();
559051865fdaSZheng Liu 	if (err)
5591b5799018STheodore Ts'o 		goto out5;
559251865fdaSZheng Liu 
55935dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
5594bd2d0210STheodore Ts'o 	if (err)
5595b5799018STheodore Ts'o 		goto out4;
5596857ac889SLukas Czerner 
5597b5799018STheodore Ts'o 	err = ext4_init_sysfs();
5598dd68314cSTheodore Ts'o 	if (err)
5599b5799018STheodore Ts'o 		goto out3;
5600857ac889SLukas Czerner 
56015dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
5602ac27a0ecSDave Kleikamp 	if (err)
5603c9de560dSAlex Tomas 		goto out2;
5604ac27a0ecSDave Kleikamp 	err = init_inodecache();
5605ac27a0ecSDave Kleikamp 	if (err)
5606ac27a0ecSDave Kleikamp 		goto out1;
560724b58424STheodore Ts'o 	register_as_ext3();
56082035e776STheodore Ts'o 	register_as_ext2();
560903010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
5610ac27a0ecSDave Kleikamp 	if (err)
5611ac27a0ecSDave Kleikamp 		goto out;
5612bfff6873SLukas Czerner 
5613ac27a0ecSDave Kleikamp 	return 0;
5614ac27a0ecSDave Kleikamp out:
561524b58424STheodore Ts'o 	unregister_as_ext2();
561624b58424STheodore Ts'o 	unregister_as_ext3();
5617ac27a0ecSDave Kleikamp 	destroy_inodecache();
5618ac27a0ecSDave Kleikamp out1:
56195dabfc78STheodore Ts'o 	ext4_exit_mballoc();
56209c191f70ST Makphaibulchoke out2:
5621b5799018STheodore Ts'o 	ext4_exit_sysfs();
5622b5799018STheodore Ts'o out3:
5623dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5624b5799018STheodore Ts'o out4:
56255dabfc78STheodore Ts'o 	ext4_exit_pageio();
5626b5799018STheodore Ts'o out5:
562751865fdaSZheng Liu 	ext4_exit_es();
562851865fdaSZheng Liu 
5629ac27a0ecSDave Kleikamp 	return err;
5630ac27a0ecSDave Kleikamp }
5631ac27a0ecSDave Kleikamp 
56325dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5633ac27a0ecSDave Kleikamp {
5634bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
563524b58424STheodore Ts'o 	unregister_as_ext2();
563624b58424STheodore Ts'o 	unregister_as_ext3();
563703010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5638ac27a0ecSDave Kleikamp 	destroy_inodecache();
56395dabfc78STheodore Ts'o 	ext4_exit_mballoc();
5640b5799018STheodore Ts'o 	ext4_exit_sysfs();
56415dabfc78STheodore Ts'o 	ext4_exit_system_zone();
56425dabfc78STheodore Ts'o 	ext4_exit_pageio();
5643dd12ed14SEric Sandeen 	ext4_exit_es();
5644ac27a0ecSDave Kleikamp }
5645ac27a0ecSDave Kleikamp 
5646ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
564783982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5648ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
56495dabfc78STheodore Ts'o module_init(ext4_init_fs)
56505dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5651