xref: /linux/fs/ext4/super.c (revision 1e381f60dad913625956ed3a9c5714d9fc7e865f)
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;
589c191f70ST Makphaibulchoke static int ext4_mballoc_ready;
59e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
609f6200bbSTheodore Ts'o 
61617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
62ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
632adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
64e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
65617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
66617ba13bSMingming Cao 					struct ext4_super_block *es);
67617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
68617ba13bSMingming Cao 				   struct ext4_super_block *es);
69617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
70617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
71617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
72c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
73c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
74152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
75152a0836SAl Viro 		       const char *dev_name, void *data);
762035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
772035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
78d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
79bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
80bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
818f1f7453SEric Sandeen static void ext4_clear_request_list(void);
82ac27a0ecSDave Kleikamp 
83c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
842035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
852035e776STheodore Ts'o 	.owner		= THIS_MODULE,
862035e776STheodore Ts'o 	.name		= "ext2",
872035e776STheodore Ts'o 	.mount		= ext4_mount,
882035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
892035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
902035e776STheodore Ts'o };
917f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
92fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
932035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
942035e776STheodore Ts'o #else
952035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
962035e776STheodore Ts'o #endif
972035e776STheodore Ts'o 
982035e776STheodore Ts'o 
99ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
100ba69f9abSJan Kara 	.owner		= THIS_MODULE,
101ba69f9abSJan Kara 	.name		= "ext3",
102152a0836SAl Viro 	.mount		= ext4_mount,
103ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
104ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
105ba69f9abSJan Kara };
1067f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
107fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
108ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
109bd81d8eeSLaurent Vivier 
110d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
111d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
112d25425f8SDarrick J. Wong {
113e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
114d25425f8SDarrick J. Wong 		return 1;
115d25425f8SDarrick J. Wong 
116d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
117d25425f8SDarrick J. Wong }
118d25425f8SDarrick J. Wong 
119a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
120a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
121a9c47317SDarrick J. Wong {
122a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
123a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
124a9c47317SDarrick J. Wong 	__u32 csum;
125a9c47317SDarrick J. Wong 
126a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
127a9c47317SDarrick J. Wong 
128a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
129a9c47317SDarrick J. Wong }
130a9c47317SDarrick J. Wong 
131c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb,
132a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
133a9c47317SDarrick J. Wong {
1349aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
135a9c47317SDarrick J. Wong 		return 1;
136a9c47317SDarrick J. Wong 
137a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
138a9c47317SDarrick J. Wong }
139a9c47317SDarrick J. Wong 
14006db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
141a9c47317SDarrick J. Wong {
14206db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
14306db49e6STheodore Ts'o 
1449aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
145a9c47317SDarrick J. Wong 		return;
146a9c47317SDarrick J. Wong 
147a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
148a9c47317SDarrick J. Wong }
149a9c47317SDarrick J. Wong 
1509933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
1519933fc0aSTheodore Ts'o {
1529933fc0aSTheodore Ts'o 	void *ret;
1539933fc0aSTheodore Ts'o 
1548be04b93SJoe Perches 	ret = kmalloc(size, flags | __GFP_NOWARN);
1559933fc0aSTheodore Ts'o 	if (!ret)
1569933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
1579933fc0aSTheodore Ts'o 	return ret;
1589933fc0aSTheodore Ts'o }
1599933fc0aSTheodore Ts'o 
1609933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
1619933fc0aSTheodore Ts'o {
1629933fc0aSTheodore Ts'o 	void *ret;
1639933fc0aSTheodore Ts'o 
1648be04b93SJoe Perches 	ret = kzalloc(size, flags | __GFP_NOWARN);
1659933fc0aSTheodore Ts'o 	if (!ret)
1669933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
1679933fc0aSTheodore Ts'o 	return ret;
1689933fc0aSTheodore Ts'o }
1699933fc0aSTheodore Ts'o 
1708fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
1718fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
172bd81d8eeSLaurent Vivier {
1733a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
1748fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1758fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
176bd81d8eeSLaurent Vivier }
177bd81d8eeSLaurent Vivier 
1788fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
1798fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
180bd81d8eeSLaurent Vivier {
1815272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
1828fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1838fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
184bd81d8eeSLaurent Vivier }
185bd81d8eeSLaurent Vivier 
1868fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
1878fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
188bd81d8eeSLaurent Vivier {
1895272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
1908fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1918fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
192bd81d8eeSLaurent Vivier }
193bd81d8eeSLaurent Vivier 
194021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
195560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
196560671a0SAneesh Kumar K.V {
197560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
198560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
199560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
200560671a0SAneesh Kumar K.V }
201560671a0SAneesh Kumar K.V 
202560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
203560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
204560671a0SAneesh Kumar K.V {
205560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
206560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
207560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
208560671a0SAneesh Kumar K.V }
209560671a0SAneesh Kumar K.V 
210560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
211560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
212560671a0SAneesh Kumar K.V {
213560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
214560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
215560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
216560671a0SAneesh Kumar K.V }
217560671a0SAneesh Kumar K.V 
218560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
219560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
220560671a0SAneesh Kumar K.V {
221560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
222560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
223560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
224560671a0SAneesh Kumar K.V }
225560671a0SAneesh Kumar K.V 
2268fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2278fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
228bd81d8eeSLaurent Vivier {
2293a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2308fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2318fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
232bd81d8eeSLaurent Vivier }
233bd81d8eeSLaurent Vivier 
2348fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2358fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
236bd81d8eeSLaurent Vivier {
2375272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2388fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2398fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
240bd81d8eeSLaurent Vivier }
241bd81d8eeSLaurent Vivier 
2428fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2438fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
244bd81d8eeSLaurent Vivier {
2455272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2468fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2478fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
248bd81d8eeSLaurent Vivier }
249bd81d8eeSLaurent Vivier 
250021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
251560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
252560671a0SAneesh Kumar K.V {
253560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
254560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
255560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
256560671a0SAneesh Kumar K.V }
257560671a0SAneesh Kumar K.V 
258560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
259560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
260560671a0SAneesh Kumar K.V {
261560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
262560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
263560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
264560671a0SAneesh Kumar K.V }
265560671a0SAneesh Kumar K.V 
266560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
267560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
268560671a0SAneesh Kumar K.V {
269560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
270560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
271560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
272560671a0SAneesh Kumar K.V }
273560671a0SAneesh Kumar K.V 
274560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
275560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
276560671a0SAneesh Kumar K.V {
277560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
278560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
279560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
280560671a0SAneesh Kumar K.V }
281560671a0SAneesh Kumar K.V 
282d3d1faf6SCurt Wohlgemuth 
2831c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
2841c13d5c0STheodore Ts'o 			    unsigned int line)
2851c13d5c0STheodore Ts'o {
2861c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
2871c13d5c0STheodore Ts'o 
2881c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2891b46617bSTheodore Ts'o 	if (bdev_read_only(sb->s_bdev))
2901b46617bSTheodore Ts'o 		return;
2911c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2921c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
2931c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
2941c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
2951c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
2961c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
2971c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
2981c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
2991c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3001c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3011c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3021c13d5c0STheodore Ts'o 	}
30366e61a9eSTheodore Ts'o 	/*
30466e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
30566e61a9eSTheodore Ts'o 	 * started already
30666e61a9eSTheodore Ts'o 	 */
30766e61a9eSTheodore Ts'o 	if (!es->s_error_count)
30866e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
309ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
3101c13d5c0STheodore Ts'o }
3111c13d5c0STheodore Ts'o 
3121c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3131c13d5c0STheodore Ts'o 			    unsigned int line)
3141c13d5c0STheodore Ts'o {
3151c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3161c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3171c13d5c0STheodore Ts'o }
3181c13d5c0STheodore Ts'o 
319bdfe0cbdSTheodore Ts'o /*
320bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
321bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
322bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
323bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
324bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
325bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
326bdfe0cbdSTheodore Ts'o  */
327bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
328bdfe0cbdSTheodore Ts'o {
329bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
330bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
331bdfe0cbdSTheodore Ts'o 
332bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
333bdfe0cbdSTheodore Ts'o }
334bdfe0cbdSTheodore Ts'o 
33518aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
33618aadd47SBobi Jam {
33718aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
33818aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
33918aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
3405d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
34118aadd47SBobi Jam 
3425d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
34318aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
3445d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
3455d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
3465d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
34718aadd47SBobi Jam 		list_del_init(&jce->jce_list);
34818aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
34918aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
35018aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
35118aadd47SBobi Jam 	}
35218aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
35318aadd47SBobi Jam }
3541c13d5c0STheodore Ts'o 
355ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
356ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
357ac27a0ecSDave Kleikamp  *
358ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
359617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
360ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
361ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
362ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
363ac27a0ecSDave Kleikamp  * write out the superblock safely.
364ac27a0ecSDave Kleikamp  *
365dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
366d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
367ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
368ac27a0ecSDave Kleikamp  */
369ac27a0ecSDave Kleikamp 
370617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
371ac27a0ecSDave Kleikamp {
372ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
373ac27a0ecSDave Kleikamp 		return;
374ac27a0ecSDave Kleikamp 
375ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
376617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
377ac27a0ecSDave Kleikamp 
3784ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
379ac27a0ecSDave Kleikamp 		if (journal)
380dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
381ac27a0ecSDave Kleikamp 	}
382ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
383b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
3844418e141SDmitry Monakhov 		/*
3854418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
3864418e141SDmitry Monakhov 		 * before ->s_flags update
3874418e141SDmitry Monakhov 		 */
3884418e141SDmitry Monakhov 		smp_wmb();
389ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
390ac27a0ecSDave Kleikamp 	}
3914327ba52SDaeho Jeong 	if (test_opt(sb, ERRORS_PANIC)) {
3924327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
3934327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
3944327ba52SDaeho Jeong 			return;
395617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
396ac27a0ecSDave Kleikamp 			sb->s_id);
397ac27a0ecSDave Kleikamp 	}
3984327ba52SDaeho Jeong }
399ac27a0ecSDave Kleikamp 
400efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
401efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
402efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
403efbed4dcSTheodore Ts'o 
40412062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
405c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
406ac27a0ecSDave Kleikamp {
4070ff2ea7dSJoe Perches 	struct va_format vaf;
408ac27a0ecSDave Kleikamp 	va_list args;
409ac27a0ecSDave Kleikamp 
410efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
411ac27a0ecSDave Kleikamp 		va_start(args, fmt);
4120ff2ea7dSJoe Perches 		vaf.fmt = fmt;
4130ff2ea7dSJoe Perches 		vaf.va = &args;
414efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
415efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4160ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
417ac27a0ecSDave Kleikamp 		va_end(args);
418efbed4dcSTheodore Ts'o 	}
419f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
420617ba13bSMingming Cao 	ext4_handle_error(sb);
421ac27a0ecSDave Kleikamp }
422ac27a0ecSDave Kleikamp 
423e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
424c398eda0STheodore Ts'o 			unsigned int line, ext4_fsblk_t block,
425273df556SFrank Mayhar 			const char *fmt, ...)
426273df556SFrank Mayhar {
427273df556SFrank Mayhar 	va_list args;
428f7c21177STheodore Ts'o 	struct va_format vaf;
4291c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
430273df556SFrank Mayhar 
4311c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4321c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
433efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
434273df556SFrank Mayhar 		va_start(args, fmt);
435f7c21177STheodore Ts'o 		vaf.fmt = fmt;
436f7c21177STheodore Ts'o 		vaf.va = &args;
437c398eda0STheodore Ts'o 		if (block)
438d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
439d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
440d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
441d9ee81daSJoe Perches 			       block, current->comm, &vaf);
442d9ee81daSJoe Perches 		else
443d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
444d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
445d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
446d9ee81daSJoe Perches 			       current->comm, &vaf);
447273df556SFrank Mayhar 		va_end(args);
448efbed4dcSTheodore Ts'o 	}
449efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
450273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
451273df556SFrank Mayhar }
452273df556SFrank Mayhar 
453e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
454f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
455f7c21177STheodore Ts'o 		       const char *fmt, ...)
456273df556SFrank Mayhar {
457273df556SFrank Mayhar 	va_list args;
458f7c21177STheodore Ts'o 	struct va_format vaf;
4591c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
460496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
461273df556SFrank Mayhar 	char pathname[80], *path;
462273df556SFrank Mayhar 
4631c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
4641c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
465efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
4669bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
467f9a62d09SDan Carpenter 		if (IS_ERR(path))
468273df556SFrank Mayhar 			path = "(unknown)";
469f7c21177STheodore Ts'o 		va_start(args, fmt);
470f7c21177STheodore Ts'o 		vaf.fmt = fmt;
471f7c21177STheodore Ts'o 		vaf.va = &args;
472d9ee81daSJoe Perches 		if (block)
473d9ee81daSJoe Perches 			printk(KERN_CRIT
474d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
475d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
476d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
477d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
478d9ee81daSJoe Perches 		else
479d9ee81daSJoe Perches 			printk(KERN_CRIT
480d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
481d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
482d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
483d9ee81daSJoe Perches 			       current->comm, path, &vaf);
484273df556SFrank Mayhar 		va_end(args);
485efbed4dcSTheodore Ts'o 	}
486efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
487273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
488273df556SFrank Mayhar }
489273df556SFrank Mayhar 
490722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
491ac27a0ecSDave Kleikamp 			      char nbuf[16])
492ac27a0ecSDave Kleikamp {
493ac27a0ecSDave Kleikamp 	char *errstr = NULL;
494ac27a0ecSDave Kleikamp 
495ac27a0ecSDave Kleikamp 	switch (errno) {
4966a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
4976a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
4986a797d27SDarrick J. Wong 		break;
4996a797d27SDarrick J. Wong 	case -EFSBADCRC:
5006a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
5016a797d27SDarrick J. Wong 		break;
502ac27a0ecSDave Kleikamp 	case -EIO:
503ac27a0ecSDave Kleikamp 		errstr = "IO failure";
504ac27a0ecSDave Kleikamp 		break;
505ac27a0ecSDave Kleikamp 	case -ENOMEM:
506ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
507ac27a0ecSDave Kleikamp 		break;
508ac27a0ecSDave Kleikamp 	case -EROFS:
50978f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
51078f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
511ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
512ac27a0ecSDave Kleikamp 		else
513ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
514ac27a0ecSDave Kleikamp 		break;
515ac27a0ecSDave Kleikamp 	default:
516ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
517ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
518ac27a0ecSDave Kleikamp 		 * NULL. */
519ac27a0ecSDave Kleikamp 		if (nbuf) {
520ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
521ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
522ac27a0ecSDave Kleikamp 				errstr = nbuf;
523ac27a0ecSDave Kleikamp 		}
524ac27a0ecSDave Kleikamp 		break;
525ac27a0ecSDave Kleikamp 	}
526ac27a0ecSDave Kleikamp 
527ac27a0ecSDave Kleikamp 	return errstr;
528ac27a0ecSDave Kleikamp }
529ac27a0ecSDave Kleikamp 
530617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
531ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
532ac27a0ecSDave Kleikamp 
533c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
534c398eda0STheodore Ts'o 		      unsigned int line, int errno)
535ac27a0ecSDave Kleikamp {
536ac27a0ecSDave Kleikamp 	char nbuf[16];
537ac27a0ecSDave Kleikamp 	const char *errstr;
538ac27a0ecSDave Kleikamp 
539ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
540ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
541ac27a0ecSDave Kleikamp 	 * an error. */
542ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
543ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
544ac27a0ecSDave Kleikamp 		return;
545ac27a0ecSDave Kleikamp 
546efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
547617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
548c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
549c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
550efbed4dcSTheodore Ts'o 	}
551ac27a0ecSDave Kleikamp 
552efbed4dcSTheodore Ts'o 	save_error_info(sb, function, line);
553617ba13bSMingming Cao 	ext4_handle_error(sb);
554ac27a0ecSDave Kleikamp }
555ac27a0ecSDave Kleikamp 
556ac27a0ecSDave Kleikamp /*
557617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
558ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
559ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
560ac27a0ecSDave Kleikamp  *
561ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
562ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
563ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
564ac27a0ecSDave Kleikamp  */
565ac27a0ecSDave Kleikamp 
566c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
567c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
568ac27a0ecSDave Kleikamp {
569ac27a0ecSDave Kleikamp 	va_list args;
570ac27a0ecSDave Kleikamp 
5711c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
572ac27a0ecSDave Kleikamp 	va_start(args, fmt);
573c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
574c398eda0STheodore Ts'o 	       function, line);
575ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
576ac27a0ecSDave Kleikamp 	printk("\n");
577ac27a0ecSDave Kleikamp 	va_end(args);
578ac27a0ecSDave Kleikamp 
5791c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
580b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
5814ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
5824418e141SDmitry Monakhov 		/*
5834418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
5844418e141SDmitry Monakhov 		 * before ->s_flags update
5854418e141SDmitry Monakhov 		 */
5864418e141SDmitry Monakhov 		smp_wmb();
5874418e141SDmitry Monakhov 		sb->s_flags |= MS_RDONLY;
588ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
589dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
5901c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
5911c13d5c0STheodore Ts'o 	}
5924327ba52SDaeho Jeong 	if (test_opt(sb, ERRORS_PANIC)) {
5934327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
5944327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
5954327ba52SDaeho Jeong 			return;
5961c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
597ac27a0ecSDave Kleikamp 	}
5984327ba52SDaeho Jeong }
599ac27a0ecSDave Kleikamp 
600e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
601e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
602b31e1552SEric Sandeen {
6030ff2ea7dSJoe Perches 	struct va_format vaf;
604b31e1552SEric Sandeen 	va_list args;
605b31e1552SEric Sandeen 
606efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
607efbed4dcSTheodore Ts'o 		return;
608efbed4dcSTheodore Ts'o 
609b31e1552SEric Sandeen 	va_start(args, fmt);
6100ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6110ff2ea7dSJoe Perches 	vaf.va = &args;
6120ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
613b31e1552SEric Sandeen 	va_end(args);
614b31e1552SEric Sandeen }
615b31e1552SEric Sandeen 
616b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb)					\
617b03a2f7eSAndreas Dilger 		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
618b03a2f7eSAndreas Dilger 			     "EXT4-fs warning")
619b03a2f7eSAndreas Dilger 
62012062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
621c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
622ac27a0ecSDave Kleikamp {
6230ff2ea7dSJoe Perches 	struct va_format vaf;
624ac27a0ecSDave Kleikamp 	va_list args;
625ac27a0ecSDave Kleikamp 
626b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
627efbed4dcSTheodore Ts'o 		return;
628efbed4dcSTheodore Ts'o 
629ac27a0ecSDave Kleikamp 	va_start(args, fmt);
6300ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6310ff2ea7dSJoe Perches 	vaf.va = &args;
6320ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
6330ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
634ac27a0ecSDave Kleikamp 	va_end(args);
635ac27a0ecSDave Kleikamp }
636ac27a0ecSDave Kleikamp 
637b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
638b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
639b03a2f7eSAndreas Dilger {
640b03a2f7eSAndreas Dilger 	struct va_format vaf;
641b03a2f7eSAndreas Dilger 	va_list args;
642b03a2f7eSAndreas Dilger 
643b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
644b03a2f7eSAndreas Dilger 		return;
645b03a2f7eSAndreas Dilger 
646b03a2f7eSAndreas Dilger 	va_start(args, fmt);
647b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
648b03a2f7eSAndreas Dilger 	vaf.va = &args;
649b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
650b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
651b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
652b03a2f7eSAndreas Dilger 	va_end(args);
653b03a2f7eSAndreas Dilger }
654b03a2f7eSAndreas Dilger 
655e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
656e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
657e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
658e29136f8STheodore Ts'o 			     const char *fmt, ...)
6595d1b1b3fSAneesh Kumar K.V __releases(bitlock)
6605d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
6615d1b1b3fSAneesh Kumar K.V {
6620ff2ea7dSJoe Perches 	struct va_format vaf;
6635d1b1b3fSAneesh Kumar K.V 	va_list args;
6645d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
6655d1b1b3fSAneesh Kumar K.V 
6661c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
6671c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
6681c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
6690ff2ea7dSJoe Perches 
670efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
6715d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
6720ff2ea7dSJoe Perches 		vaf.fmt = fmt;
6730ff2ea7dSJoe Perches 		vaf.va = &args;
67421149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
675e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
676e29136f8STheodore Ts'o 		if (ino)
6770ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
678e29136f8STheodore Ts'o 		if (block)
679efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
680efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
6810ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
6825d1b1b3fSAneesh Kumar K.V 		va_end(args);
683efbed4dcSTheodore Ts'o 	}
6845d1b1b3fSAneesh Kumar K.V 
6855d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
686e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
6875d1b1b3fSAneesh Kumar K.V 		return;
6885d1b1b3fSAneesh Kumar K.V 	}
6891c13d5c0STheodore Ts'o 
6905d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
6915d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
6925d1b1b3fSAneesh Kumar K.V 	/*
6935d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
6945d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
6955d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
6965d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
6975d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
69825985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
6995d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
7005d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
7015d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
7025d1b1b3fSAneesh Kumar K.V 	 */
7035d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
7045d1b1b3fSAneesh Kumar K.V 	return;
7055d1b1b3fSAneesh Kumar K.V }
7065d1b1b3fSAneesh Kumar K.V 
707617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
708ac27a0ecSDave Kleikamp {
709617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
710ac27a0ecSDave Kleikamp 
711617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
712ac27a0ecSDave Kleikamp 		return;
713ac27a0ecSDave Kleikamp 
71412062dddSEric Sandeen 	ext4_warning(sb,
715ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
716ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
717617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
718ac27a0ecSDave Kleikamp 
719617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
720617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
721617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
722ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
723ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
724ac27a0ecSDave Kleikamp 
725ac27a0ecSDave Kleikamp 	/*
726ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
727ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
728ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
729ac27a0ecSDave Kleikamp 	 */
730ac27a0ecSDave Kleikamp }
731ac27a0ecSDave Kleikamp 
732ac27a0ecSDave Kleikamp /*
733ac27a0ecSDave Kleikamp  * Open the external journal device
734ac27a0ecSDave Kleikamp  */
735b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
736ac27a0ecSDave Kleikamp {
737ac27a0ecSDave Kleikamp 	struct block_device *bdev;
738ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
739ac27a0ecSDave Kleikamp 
740d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
741ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
742ac27a0ecSDave Kleikamp 		goto fail;
743ac27a0ecSDave Kleikamp 	return bdev;
744ac27a0ecSDave Kleikamp 
745ac27a0ecSDave Kleikamp fail:
746b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
747ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
748ac27a0ecSDave Kleikamp 	return NULL;
749ac27a0ecSDave Kleikamp }
750ac27a0ecSDave Kleikamp 
751ac27a0ecSDave Kleikamp /*
752ac27a0ecSDave Kleikamp  * Release the journal device
753ac27a0ecSDave Kleikamp  */
7544385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
755ac27a0ecSDave Kleikamp {
7564385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
757ac27a0ecSDave Kleikamp }
758ac27a0ecSDave Kleikamp 
7594385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
760ac27a0ecSDave Kleikamp {
761ac27a0ecSDave Kleikamp 	struct block_device *bdev;
762ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
763ac27a0ecSDave Kleikamp 	if (bdev) {
7644385bab1SAl Viro 		ext4_blkdev_put(bdev);
765ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
766ac27a0ecSDave Kleikamp 	}
767ac27a0ecSDave Kleikamp }
768ac27a0ecSDave Kleikamp 
769ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
770ac27a0ecSDave Kleikamp {
771617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
772ac27a0ecSDave Kleikamp }
773ac27a0ecSDave Kleikamp 
774617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
775ac27a0ecSDave Kleikamp {
776ac27a0ecSDave Kleikamp 	struct list_head *l;
777ac27a0ecSDave Kleikamp 
778b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
779ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
780ac27a0ecSDave Kleikamp 
781ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
782ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
783ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
784ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
785ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
786ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
787ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
788ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
789ac27a0ecSDave Kleikamp 	}
790ac27a0ecSDave Kleikamp }
791ac27a0ecSDave Kleikamp 
792617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
793ac27a0ecSDave Kleikamp {
794617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
795617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
796ef2cabf7SHidehiro Kawai 	int i, err;
797ac27a0ecSDave Kleikamp 
798857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
799e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
800e0ccfd95SChristoph Hellwig 
8012e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
8022e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
8034c0425ffSMingming Cao 
8040390131bSFrank Mayhar 	if (sbi->s_journal) {
805ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
80647b4a50bSJan Kara 		sbi->s_journal = NULL;
807ef2cabf7SHidehiro Kawai 		if (err < 0)
808c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
8090390131bSFrank Mayhar 	}
810d4edac31SJosef Bacik 
811ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
812d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
8139105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
814d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
815d4edac31SJosef Bacik 	ext4_mb_release(sb);
816d4edac31SJosef Bacik 	ext4_ext_release(sb);
817d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
818d4edac31SJosef Bacik 
819ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
820e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
821ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
822ac27a0ecSDave Kleikamp 	}
82358c5873aSArtem Bityutskiy 	if (!(sb->s_flags & MS_RDONLY))
824a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
825a8e25a83SArtem Bityutskiy 
826ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
827ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
828b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
829b93b41d4SAl Viro 	kvfree(sbi->s_flex_groups);
83057042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
831ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
832ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
83357042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
834ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
835ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
836a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
837ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
838ac27a0ecSDave Kleikamp #endif
839ac27a0ecSDave Kleikamp 
840ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
841ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
842ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
843ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
844ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
845ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
846ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
847ac27a0ecSDave Kleikamp 
84889d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
849f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
850ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
851ac27a0ecSDave Kleikamp 		/*
852ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
853ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
854ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
855ac27a0ecSDave Kleikamp 		 */
856ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
857f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
858617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
859ac27a0ecSDave Kleikamp 	}
8609c191f70ST Makphaibulchoke 	if (sbi->s_mb_cache) {
8619c191f70ST Makphaibulchoke 		ext4_xattr_destroy_cache(sbi->s_mb_cache);
8629c191f70ST Makphaibulchoke 		sbi->s_mb_cache = NULL;
8639c191f70ST Makphaibulchoke 	}
864c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
865c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
866ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
8673197ebdbSTheodore Ts'o 	/*
8683197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
8693197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
8703197ebdbSTheodore Ts'o 	 */
8713197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
8723197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
8730441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
8740441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
875705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
876ac27a0ecSDave Kleikamp 	kfree(sbi);
877ac27a0ecSDave Kleikamp }
878ac27a0ecSDave Kleikamp 
879e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
880ac27a0ecSDave Kleikamp 
881ac27a0ecSDave Kleikamp /*
882ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
883ac27a0ecSDave Kleikamp  */
884617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
885ac27a0ecSDave Kleikamp {
886617ba13bSMingming Cao 	struct ext4_inode_info *ei;
887ac27a0ecSDave Kleikamp 
888e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
889ac27a0ecSDave Kleikamp 	if (!ei)
890ac27a0ecSDave Kleikamp 		return NULL;
8910b8e58a1SAndreas Dilger 
892ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
893202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
894c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
895c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
8969a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
8979a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
898edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
899eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
900edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
901dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
902d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
903d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
904d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
9059d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
9067e731bc9STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = 0;
907d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
908a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
909a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
91096c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
911a9e7f447SDmitry Monakhov #endif
9128aefcd55STheodore Ts'o 	ei->jinode = NULL;
9132e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
914744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
915b436b9beSJan Kara 	ei->i_sync_tid = 0;
916b436b9beSJan Kara 	ei->i_datasync_tid = 0;
917f7ad6d2eSTheodore Ts'o 	atomic_set(&ei->i_ioend_count, 0);
918e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
9192e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
920b30ab0e0SMichael Halcrow #ifdef CONFIG_EXT4_FS_ENCRYPTION
921b7236e21STheodore Ts'o 	ei->i_crypt_info = NULL;
922b30ab0e0SMichael Halcrow #endif
923ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
924ac27a0ecSDave Kleikamp }
925ac27a0ecSDave Kleikamp 
9267ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
9277ff9c073STheodore Ts'o {
9287ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
9297ff9c073STheodore Ts'o 
9307ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
9317ff9c073STheodore Ts'o 	return drop;
9327ff9c073STheodore Ts'o }
9337ff9c073STheodore Ts'o 
934fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
935fa0d7e3dSNick Piggin {
936fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
937fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
938fa0d7e3dSNick Piggin }
939fa0d7e3dSNick Piggin 
940617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
941ac27a0ecSDave Kleikamp {
9429f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
943b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
944b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
945b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
9469f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
9479f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
9489f7dd93dSVasily Averin 				true);
9499f7dd93dSVasily Averin 		dump_stack();
9509f7dd93dSVasily Averin 	}
951fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
952ac27a0ecSDave Kleikamp }
953ac27a0ecSDave Kleikamp 
95451cc5068SAlexey Dobriyan static void init_once(void *foo)
955ac27a0ecSDave Kleikamp {
956617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
957ac27a0ecSDave Kleikamp 
958ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
959ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
9600e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
961ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
962ac27a0ecSDave Kleikamp }
963ac27a0ecSDave Kleikamp 
964e67bc2b3SFabian Frederick static int __init init_inodecache(void)
965ac27a0ecSDave Kleikamp {
966617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
967617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
968ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
969ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
97020c2df83SPaul Mundt 					     init_once);
971617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
972ac27a0ecSDave Kleikamp 		return -ENOMEM;
973ac27a0ecSDave Kleikamp 	return 0;
974ac27a0ecSDave Kleikamp }
975ac27a0ecSDave Kleikamp 
976ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
977ac27a0ecSDave Kleikamp {
9788c0a8537SKirill A. Shutemov 	/*
9798c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
9808c0a8537SKirill A. Shutemov 	 * destroy cache.
9818c0a8537SKirill A. Shutemov 	 */
9828c0a8537SKirill A. Shutemov 	rcu_barrier();
983617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
984ac27a0ecSDave Kleikamp }
985ac27a0ecSDave Kleikamp 
9860930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
987ac27a0ecSDave Kleikamp {
9880930fcc1SAl Viro 	invalidate_inode_buffers(inode);
989dbd5768fSJan Kara 	clear_inode(inode);
9909f754758SChristoph Hellwig 	dquot_drop(inode);
991c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
99251865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
9938aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
9948aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
9958aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
9968aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
9978aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
9988aefcd55STheodore Ts'o 	}
999b7236e21STheodore Ts'o #ifdef CONFIG_EXT4_FS_ENCRYPTION
1000b7236e21STheodore Ts'o 	if (EXT4_I(inode)->i_crypt_info)
1001c936e1ecSTheodore Ts'o 		ext4_free_encryption_info(inode, EXT4_I(inode)->i_crypt_info);
1002b7236e21STheodore Ts'o #endif
1003ac27a0ecSDave Kleikamp }
1004ac27a0ecSDave Kleikamp 
10051b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
10061b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1007ac27a0ecSDave Kleikamp {
1008ac27a0ecSDave Kleikamp 	struct inode *inode;
1009ac27a0ecSDave Kleikamp 
1010617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1011ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1012617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1013ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1014ac27a0ecSDave Kleikamp 
1015ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1016ac27a0ecSDave Kleikamp 	 *
1017617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1018ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1019ac27a0ecSDave Kleikamp 	 *
1020ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1021ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1022ac27a0ecSDave Kleikamp 	 */
1023f4bb2981STheodore Ts'o 	inode = ext4_iget_normal(sb, ino);
10241d1fe1eeSDavid Howells 	if (IS_ERR(inode))
10251d1fe1eeSDavid Howells 		return ERR_CAST(inode);
10261d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1027ac27a0ecSDave Kleikamp 		iput(inode);
1028ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1029ac27a0ecSDave Kleikamp 	}
10301b961ac0SChristoph Hellwig 
10311b961ac0SChristoph Hellwig 	return inode;
1032ac27a0ecSDave Kleikamp }
10331b961ac0SChristoph Hellwig 
10341b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
10351b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10361b961ac0SChristoph Hellwig {
10371b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
10381b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
10391b961ac0SChristoph Hellwig }
10401b961ac0SChristoph Hellwig 
10411b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
10421b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10431b961ac0SChristoph Hellwig {
10441b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
10451b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1046ac27a0ecSDave Kleikamp }
1047ac27a0ecSDave Kleikamp 
1048c39a7f84SToshiyuki Okajima /*
1049c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1050c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1051c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1052c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1053c39a7f84SToshiyuki Okajima  */
10540b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
10550b8e58a1SAndreas Dilger 				 gfp_t wait)
1056c39a7f84SToshiyuki Okajima {
1057c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1058c39a7f84SToshiyuki Okajima 
1059c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1060c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1061c39a7f84SToshiyuki Okajima 		return 0;
1062c39a7f84SToshiyuki Okajima 	if (journal)
1063c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1064c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1065c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1066c39a7f84SToshiyuki Okajima }
1067c39a7f84SToshiyuki Okajima 
1068ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1069ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1070ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1071ac27a0ecSDave Kleikamp 
1072617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1073617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1074617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1075617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1076617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
10776f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1078f00c9e44SJan Kara 			 struct path *path);
1079ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
1080617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1081617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1082ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1083617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1084ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
10857c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
10867c319d32SAditya Kali 			     unsigned int flags);
10877c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1088ac27a0ecSDave Kleikamp 
108996c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
109096c7e0d9SJan Kara {
109196c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
109296c7e0d9SJan Kara }
109396c7e0d9SJan Kara 
109461e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
109560e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1096617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1097617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1098617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1099617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1100a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1101a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1102a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1103ac27a0ecSDave Kleikamp };
1104ac27a0ecSDave Kleikamp 
11050d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1106617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1107ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1108287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
11090a240339SJan Kara 	.get_state	= dquot_get_state,
1110287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1111287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1112287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1113ac27a0ecSDave Kleikamp };
1114ac27a0ecSDave Kleikamp #endif
1115ac27a0ecSDave Kleikamp 
1116ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1117617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1118617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1119617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1120617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
11217ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
11220930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1123617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1124617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1125c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1126c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1127617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1128617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1129617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1130ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1131617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1132617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
113396c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1134ac27a0ecSDave Kleikamp #endif
1135c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1136ac27a0ecSDave Kleikamp };
1137ac27a0ecSDave Kleikamp 
113839655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
11391b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
11401b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1141617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1142ac27a0ecSDave Kleikamp };
1143ac27a0ecSDave Kleikamp 
1144ac27a0ecSDave Kleikamp enum {
1145ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1146ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
114772578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1148ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
114972578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1150ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1151ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1152ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
11536ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1154ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
11555a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1156ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1157923ae0ffSRoss Zwisler 	Opt_usrquota, Opt_grpquota, Opt_i_version, Opt_dax,
11581449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
1159a26f4992STheodore Ts'o 	Opt_lazytime, Opt_nolazytime,
11601449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
11615328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1162744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1163fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1164c6d3d56dSDarrick J. Wong 	Opt_max_dir_size_kb, Opt_nojournal_checksum,
1165ac27a0ecSDave Kleikamp };
1166ac27a0ecSDave Kleikamp 
1167a447c093SSteven Whitehouse static const match_table_t tokens = {
1168ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1169ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1170ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1171ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1172ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1173ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1174ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1175ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1176ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1177ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1178ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1179ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1180ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1181ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
118272578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
118372578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1184ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1185ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1186ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1187ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1188e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
11895a916be1STheodore Ts'o 	{Opt_noload, "noload"},
119072578c33STheodore Ts'o 	{Opt_removed, "nobh"},
119172578c33STheodore Ts'o 	{Opt_removed, "bh"},
1192ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
119330773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
119430773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1195ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1196ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1197818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1198c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1199818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1200ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1201ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1202ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1203ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
12045bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
12055bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1206ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1207ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1208ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1209ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1210ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1211ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
12125a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1213ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1214ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1215ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1216ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1217ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
121806705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
121906705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
122025ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1221923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1222c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
122364769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1224a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1225a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1226dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
122736ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
122836ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
12296fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
12306fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1231240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1232b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1233afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
123406705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
123506705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1236744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1237744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
12385328e635SEric Sandeen 	{Opt_discard, "discard"},
12395328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1240fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1241fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1242fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1243df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
12446ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1245c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1246c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1247c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1248c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1249c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1250f3f12faaSJosef Bacik 	{Opt_err, NULL},
1251ac27a0ecSDave Kleikamp };
1252ac27a0ecSDave Kleikamp 
1253617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1254ac27a0ecSDave Kleikamp {
1255617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1256ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1257ac27a0ecSDave Kleikamp 
1258ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1259ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
12600b8e58a1SAndreas Dilger 
1261ac27a0ecSDave Kleikamp 	options += 3;
12620b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1263ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1264ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
12654776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1266ac27a0ecSDave Kleikamp 		       (char *) *data);
1267ac27a0ecSDave Kleikamp 		return 1;
1268ac27a0ecSDave Kleikamp 	}
1269ac27a0ecSDave Kleikamp 	if (*options == ',')
1270ac27a0ecSDave Kleikamp 		options++;
1271ac27a0ecSDave Kleikamp 	*data = (void *) options;
12720b8e58a1SAndreas Dilger 
1273ac27a0ecSDave Kleikamp 	return sb_block;
1274ac27a0ecSDave Kleikamp }
1275ac27a0ecSDave Kleikamp 
1276b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1277437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1278437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1279b3881f74STheodore Ts'o 
128056c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
128156c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
128256c50f11SDmitry Monakhov {
128356c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
128456c50f11SDmitry Monakhov 	char *qname;
128503dafb5fSChen Gang 	int ret = -1;
128656c50f11SDmitry Monakhov 
128756c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
128856c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
128956c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
129056c50f11SDmitry Monakhov 			"Cannot change journaled "
129156c50f11SDmitry Monakhov 			"quota options when quota turned on");
129257f73c2cSTheodore Ts'o 		return -1;
129356c50f11SDmitry Monakhov 	}
1294e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1295262b4662SJan Kara 		ext4_msg(sb, KERN_ERR, "Cannot set journaled quota options "
1296262b4662SJan Kara 			 "when QUOTA feature is enabled");
1297262b4662SJan Kara 		return -1;
1298262b4662SJan Kara 	}
129956c50f11SDmitry Monakhov 	qname = match_strdup(args);
130056c50f11SDmitry Monakhov 	if (!qname) {
130156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
130256c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
130357f73c2cSTheodore Ts'o 		return -1;
130456c50f11SDmitry Monakhov 	}
130503dafb5fSChen Gang 	if (sbi->s_qf_names[qtype]) {
130603dafb5fSChen Gang 		if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
130703dafb5fSChen Gang 			ret = 1;
130803dafb5fSChen Gang 		else
130956c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
131003dafb5fSChen Gang 				 "%s quota file already specified",
131103dafb5fSChen Gang 				 QTYPE2NAME(qtype));
131203dafb5fSChen Gang 		goto errout;
131356c50f11SDmitry Monakhov 	}
131403dafb5fSChen Gang 	if (strchr(qname, '/')) {
131556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
131656c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
131703dafb5fSChen Gang 		goto errout;
131856c50f11SDmitry Monakhov 	}
131903dafb5fSChen Gang 	sbi->s_qf_names[qtype] = qname;
1320fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
132156c50f11SDmitry Monakhov 	return 1;
132203dafb5fSChen Gang errout:
132303dafb5fSChen Gang 	kfree(qname);
132403dafb5fSChen Gang 	return ret;
132556c50f11SDmitry Monakhov }
132656c50f11SDmitry Monakhov 
132756c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
132856c50f11SDmitry Monakhov {
132956c50f11SDmitry Monakhov 
133056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
133156c50f11SDmitry Monakhov 
133256c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
133356c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
133456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
133556c50f11SDmitry Monakhov 			" when quota turned on");
133657f73c2cSTheodore Ts'o 		return -1;
133756c50f11SDmitry Monakhov 	}
133803dafb5fSChen Gang 	kfree(sbi->s_qf_names[qtype]);
133956c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
134056c50f11SDmitry Monakhov 	return 1;
134156c50f11SDmitry Monakhov }
134256c50f11SDmitry Monakhov #endif
134356c50f11SDmitry Monakhov 
134426092bf5STheodore Ts'o #define MOPT_SET	0x0001
134526092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
134626092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
134726092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
134826092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
134926092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
135026092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
135126092bf5STheodore Ts'o #define MOPT_Q		0
135226092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
135326092bf5STheodore Ts'o #else
135426092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
135526092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
135626092bf5STheodore Ts'o #endif
135726092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
13588dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
13598dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
13608dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1361ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
136226092bf5STheodore Ts'o 
136326092bf5STheodore Ts'o static const struct mount_opts {
136426092bf5STheodore Ts'o 	int	token;
136526092bf5STheodore Ts'o 	int	mount_opt;
136626092bf5STheodore Ts'o 	int	flags;
136726092bf5STheodore Ts'o } ext4_mount_opts[] = {
136826092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
136926092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
137026092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
137126092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
137226092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
137326092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
13748dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
13758dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
13768dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
13778dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
137826092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
137926092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
13808dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
13818dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
13828dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
138359d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1384c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1385c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
13868dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1387*1e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
138826092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
13898dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
1390*1e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
13918dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
139226092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
139326092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
139426092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
13958dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
13968dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_SET},
13978dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
13988dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_CLEAR},
139926092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
140026092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
140126092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
140226092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
140326092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
140426092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
140526092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
140626092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
140726092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
140826092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1409923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
141026092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
14110efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
14120efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
14135ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
14145ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
14155ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
14168dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
14178dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
14188dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
14198dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
142026092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
142126092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
142226092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
142326092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
142426092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
142526092bf5STheodore Ts'o #else
142626092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
142726092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
142826092bf5STheodore Ts'o #endif
142926092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
143026092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
143126092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
143226092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
143326092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
143426092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
143526092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
143626092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
143726092bf5STheodore Ts'o 		       EXT4_MOUNT_GRPQUOTA), MOPT_CLEAR | MOPT_Q},
143826092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
143926092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
144026092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
144126092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
144226092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
144326092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
144426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1445df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
14466ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
144726092bf5STheodore Ts'o 	{Opt_err, 0, 0}
144826092bf5STheodore Ts'o };
144926092bf5STheodore Ts'o 
145026092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
145126092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
145226092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
145326092bf5STheodore Ts'o {
145426092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
145526092bf5STheodore Ts'o 	const struct mount_opts *m;
145608cefc7aSEric W. Biederman 	kuid_t uid;
145708cefc7aSEric W. Biederman 	kgid_t gid;
145826092bf5STheodore Ts'o 	int arg = 0;
145926092bf5STheodore Ts'o 
146057f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
146157f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
146257f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
146357f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
146457f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
146557f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
146657f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
146757f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
146857f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
146957f73c2cSTheodore Ts'o #endif
147026092bf5STheodore Ts'o 	switch (token) {
1471f7048605STheodore Ts'o 	case Opt_noacl:
1472f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1473f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1474f7048605STheodore Ts'o 		break;
147526092bf5STheodore Ts'o 	case Opt_sb:
147626092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
147726092bf5STheodore Ts'o 	case Opt_removed:
14785f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
147926092bf5STheodore Ts'o 		return 1;
148026092bf5STheodore Ts'o 	case Opt_abort:
148126092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
148226092bf5STheodore Ts'o 		return 1;
148326092bf5STheodore Ts'o 	case Opt_i_version:
148426092bf5STheodore Ts'o 		sb->s_flags |= MS_I_VERSION;
148526092bf5STheodore Ts'o 		return 1;
1486a26f4992STheodore Ts'o 	case Opt_lazytime:
1487a26f4992STheodore Ts'o 		sb->s_flags |= MS_LAZYTIME;
1488a26f4992STheodore Ts'o 		return 1;
1489a26f4992STheodore Ts'o 	case Opt_nolazytime:
1490a26f4992STheodore Ts'o 		sb->s_flags &= ~MS_LAZYTIME;
1491a26f4992STheodore Ts'o 		return 1;
149226092bf5STheodore Ts'o 	}
149326092bf5STheodore Ts'o 
14945f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
14955f3633e3SJan Kara 		if (token == m->token)
14965f3633e3SJan Kara 			break;
14975f3633e3SJan Kara 
14985f3633e3SJan Kara 	if (m->token == Opt_err) {
14995f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
15005f3633e3SJan Kara 			 "or missing value", opt);
15015f3633e3SJan Kara 		return -1;
15025f3633e3SJan Kara 	}
15035f3633e3SJan Kara 
15048dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
15058dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
15068dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
15078dc0aa8cSTheodore Ts'o 		return -1;
15088dc0aa8cSTheodore Ts'o 	}
15098dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
15108dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
15118dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
15128dc0aa8cSTheodore Ts'o 		return -1;
15138dc0aa8cSTheodore Ts'o 	}
15148dc0aa8cSTheodore Ts'o 
1515ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
15160efb3b23SJan Kara 		return -1;
151726092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
151826092bf5STheodore Ts'o 		return -1;
1519c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1520c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
152126092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
1522*1e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
1523*1e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1524c93cf2d7SDmitry Monakhov 		} else
1525c93cf2d7SDmitry Monakhov 			return -1;
1526c93cf2d7SDmitry Monakhov 	}
152726092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
152826092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
152926092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
153026092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
153126092bf5STheodore Ts'o 			 "options when quota turned on");
153226092bf5STheodore Ts'o 		return -1;
153326092bf5STheodore Ts'o 	}
153426092bf5STheodore Ts'o 
153526092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
153626092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
153726092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
153826092bf5STheodore Ts'o 		if (arg == 0)
153926092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
154026092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
154126092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
154226092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
154326092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
154426092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
154526092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1546e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1547e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1548e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1549e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
155026092bf5STheodore Ts'o 			return -1;
155126092bf5STheodore Ts'o 		}
155226092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
155326092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
155426092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
155526092bf5STheodore Ts'o 		if (!args->from)
155626092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
155726092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1558df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1559df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
156026092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
156126092bf5STheodore Ts'o 		sbi->s_stripe = arg;
15620efb3b23SJan Kara 	} else if (token == Opt_resuid) {
15630efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
15640efb3b23SJan Kara 		if (!uid_valid(uid)) {
15655f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
15660efb3b23SJan Kara 			return -1;
15670efb3b23SJan Kara 		}
15680efb3b23SJan Kara 		sbi->s_resuid = uid;
15690efb3b23SJan Kara 	} else if (token == Opt_resgid) {
15700efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
15710efb3b23SJan Kara 		if (!gid_valid(gid)) {
15725f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
15730efb3b23SJan Kara 			return -1;
15740efb3b23SJan Kara 		}
15750efb3b23SJan Kara 		sbi->s_resgid = gid;
15760efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
15770efb3b23SJan Kara 		if (is_remount) {
15780efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
15790efb3b23SJan Kara 				 "Cannot specify journal on remount");
15800efb3b23SJan Kara 			return -1;
15810efb3b23SJan Kara 		}
15820efb3b23SJan Kara 		*journal_devnum = arg;
1583ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1584ad4eec61SEric Sandeen 		char *journal_path;
1585ad4eec61SEric Sandeen 		struct inode *journal_inode;
1586ad4eec61SEric Sandeen 		struct path path;
1587ad4eec61SEric Sandeen 		int error;
1588ad4eec61SEric Sandeen 
1589ad4eec61SEric Sandeen 		if (is_remount) {
1590ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1591ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
1592ad4eec61SEric Sandeen 			return -1;
1593ad4eec61SEric Sandeen 		}
1594ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
1595ad4eec61SEric Sandeen 		if (!journal_path) {
1596ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1597ad4eec61SEric Sandeen 				"journal device string");
1598ad4eec61SEric Sandeen 			return -1;
1599ad4eec61SEric Sandeen 		}
1600ad4eec61SEric Sandeen 
1601ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1602ad4eec61SEric Sandeen 		if (error) {
1603ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
1604ad4eec61SEric Sandeen 				"journal device path: error %d", error);
1605ad4eec61SEric Sandeen 			kfree(journal_path);
1606ad4eec61SEric Sandeen 			return -1;
1607ad4eec61SEric Sandeen 		}
1608ad4eec61SEric Sandeen 
16092b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
1610ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
1611ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1612ad4eec61SEric Sandeen 				"is not a block device", journal_path);
1613ad4eec61SEric Sandeen 			path_put(&path);
1614ad4eec61SEric Sandeen 			kfree(journal_path);
1615ad4eec61SEric Sandeen 			return -1;
1616ad4eec61SEric Sandeen 		}
1617ad4eec61SEric Sandeen 
1618ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1619ad4eec61SEric Sandeen 		path_put(&path);
1620ad4eec61SEric Sandeen 		kfree(journal_path);
16210efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
16220efb3b23SJan Kara 		if (arg > 7) {
16235f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
16240efb3b23SJan Kara 				 " (must be 0-7)");
16250efb3b23SJan Kara 			return -1;
16260efb3b23SJan Kara 		}
16270efb3b23SJan Kara 		*journal_ioprio =
16280efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
16296ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
16306ddb2447STheodore Ts'o #ifdef CONFIG_EXT4_FS_ENCRYPTION
16316ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
16326ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
16336ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
16346ddb2447STheodore Ts'o #else
16356ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
16366ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
16376ddb2447STheodore Ts'o #endif
163826092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
163926092bf5STheodore Ts'o 		if (is_remount) {
164026092bf5STheodore Ts'o 			if (!sbi->s_journal)
164126092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
16425f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
164326092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
164426092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
164526092bf5STheodore Ts'o 				return -1;
164626092bf5STheodore Ts'o 			}
164726092bf5STheodore Ts'o 		} else {
164826092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
164926092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
165026092bf5STheodore Ts'o 		}
165126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
165226092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
165326092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
165426092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
16555f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
16565f3633e3SJan Kara 				 "quota options when quota turned on");
165726092bf5STheodore Ts'o 			return -1;
165826092bf5STheodore Ts'o 		}
1659e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
1660262b4662SJan Kara 			ext4_msg(sb, KERN_ERR,
1661262b4662SJan Kara 				 "Cannot set journaled quota options "
1662262b4662SJan Kara 				 "when QUOTA feature is enabled");
1663262b4662SJan Kara 			return -1;
1664262b4662SJan Kara 		}
166526092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
166626092bf5STheodore Ts'o #endif
1667923ae0ffSRoss Zwisler #ifndef CONFIG_FS_DAX
1668923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
1669923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
1670923ae0ffSRoss Zwisler 		return -1;
1671923ae0ffSRoss Zwisler #endif
167226092bf5STheodore Ts'o 	} else {
167326092bf5STheodore Ts'o 		if (!args->from)
167426092bf5STheodore Ts'o 			arg = 1;
167526092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
167626092bf5STheodore Ts'o 			arg = !arg;
167726092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
167826092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
167926092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
168026092bf5STheodore Ts'o 			WARN_ON(1);
168126092bf5STheodore Ts'o 			return -1;
168226092bf5STheodore Ts'o 		}
168326092bf5STheodore Ts'o 		if (arg != 0)
168426092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
168526092bf5STheodore Ts'o 		else
168626092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
168726092bf5STheodore Ts'o 	}
168826092bf5STheodore Ts'o 	return 1;
168926092bf5STheodore Ts'o }
169026092bf5STheodore Ts'o 
1691ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1692c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1693b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1694661aa520SEric Sandeen 			 int is_remount)
1695ac27a0ecSDave Kleikamp {
1696617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1697ac27a0ecSDave Kleikamp 	char *p;
1698ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
169926092bf5STheodore Ts'o 	int token;
1700ac27a0ecSDave Kleikamp 
1701ac27a0ecSDave Kleikamp 	if (!options)
1702ac27a0ecSDave Kleikamp 		return 1;
1703ac27a0ecSDave Kleikamp 
1704ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1705ac27a0ecSDave Kleikamp 		if (!*p)
1706ac27a0ecSDave Kleikamp 			continue;
170715121c18SEric Sandeen 		/*
170815121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
170915121c18SEric Sandeen 		 * found; some options take optional arguments.
171015121c18SEric Sandeen 		 */
1711caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
1712ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
171326092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
171426092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
1715ac27a0ecSDave Kleikamp 			return 0;
1716ac27a0ecSDave Kleikamp 	}
1717ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1718e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) &&
1719262b4662SJan Kara 	    (test_opt(sb, USRQUOTA) || test_opt(sb, GRPQUOTA))) {
1720262b4662SJan Kara 		ext4_msg(sb, KERN_ERR, "Cannot set quota options when QUOTA "
1721262b4662SJan Kara 			 "feature is enabled");
1722262b4662SJan Kara 		return 0;
1723262b4662SJan Kara 	}
1724ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1725482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1726fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1727ac27a0ecSDave Kleikamp 
1728482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1729fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1730ac27a0ecSDave Kleikamp 
173156c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1732b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1733b31e1552SEric Sandeen 					"format mixing");
1734ac27a0ecSDave Kleikamp 			return 0;
1735ac27a0ecSDave Kleikamp 		}
1736ac27a0ecSDave Kleikamp 
1737ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1738b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1739b31e1552SEric Sandeen 					"not specified");
1740ac27a0ecSDave Kleikamp 			return 0;
1741ac27a0ecSDave Kleikamp 		}
1742ac27a0ecSDave Kleikamp 	}
1743ac27a0ecSDave Kleikamp #endif
1744261cb20cSJan Kara 	if (test_opt(sb, DIOREAD_NOLOCK)) {
1745261cb20cSJan Kara 		int blocksize =
1746261cb20cSJan Kara 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
1747261cb20cSJan Kara 
1748261cb20cSJan Kara 		if (blocksize < PAGE_CACHE_SIZE) {
1749261cb20cSJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
1750261cb20cSJan Kara 				 "dioread_nolock if block size != PAGE_SIZE");
1751261cb20cSJan Kara 			return 0;
1752261cb20cSJan Kara 		}
1753261cb20cSJan Kara 	}
1754d4f76107SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
1755d4f76107SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
1756d4f76107SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with journal_async_commit "
1757d4f76107SJan Kara 			 "in data=ordered mode");
1758d4f76107SJan Kara 		return 0;
1759d4f76107SJan Kara 	}
1760ac27a0ecSDave Kleikamp 	return 1;
1761ac27a0ecSDave Kleikamp }
1762ac27a0ecSDave Kleikamp 
17632adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
17642adf6da8STheodore Ts'o 					   struct super_block *sb)
17652adf6da8STheodore Ts'o {
17662adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
17672adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
17682adf6da8STheodore Ts'o 
17692adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
17702adf6da8STheodore Ts'o 		char *fmtname = "";
17712adf6da8STheodore Ts'o 
17722adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
17732adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
17742adf6da8STheodore Ts'o 			fmtname = "vfsold";
17752adf6da8STheodore Ts'o 			break;
17762adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
17772adf6da8STheodore Ts'o 			fmtname = "vfsv0";
17782adf6da8STheodore Ts'o 			break;
17792adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
17802adf6da8STheodore Ts'o 			fmtname = "vfsv1";
17812adf6da8STheodore Ts'o 			break;
17822adf6da8STheodore Ts'o 		}
17832adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
17842adf6da8STheodore Ts'o 	}
17852adf6da8STheodore Ts'o 
17862adf6da8STheodore Ts'o 	if (sbi->s_qf_names[USRQUOTA])
1787a068acf2SKees Cook 		seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
17882adf6da8STheodore Ts'o 
17892adf6da8STheodore Ts'o 	if (sbi->s_qf_names[GRPQUOTA])
1790a068acf2SKees Cook 		seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
17912adf6da8STheodore Ts'o #endif
17922adf6da8STheodore Ts'o }
17932adf6da8STheodore Ts'o 
17945a916be1STheodore Ts'o static const char *token2str(int token)
17955a916be1STheodore Ts'o {
179650df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
17975a916be1STheodore Ts'o 
17985a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
17995a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
18005a916be1STheodore Ts'o 			break;
18015a916be1STheodore Ts'o 	return t->pattern;
18025a916be1STheodore Ts'o }
18035a916be1STheodore Ts'o 
18042adf6da8STheodore Ts'o /*
18052adf6da8STheodore Ts'o  * Show an option if
18062adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
18072adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
18082adf6da8STheodore Ts'o  */
180966acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
181066acdcf4STheodore Ts'o 			      int nodefs)
18112adf6da8STheodore Ts'o {
18122adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
18132adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
181466acdcf4STheodore Ts'o 	int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
18155a916be1STheodore Ts'o 	const struct mount_opts *m;
181666acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
18172adf6da8STheodore Ts'o 
181866acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
181966acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
18202adf6da8STheodore Ts'o 
18212adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
18225a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
18235a916be1STheodore Ts'o 
18245a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
18255a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
18265a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
18275a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
18285a916be1STheodore Ts'o 			continue;
182966acdcf4STheodore Ts'o 		if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
18305a916be1STheodore Ts'o 			continue; /* skip if same as the default */
18315a916be1STheodore Ts'o 		if ((want_set &&
18325a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
18335a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
18345a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
18355a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
18365a916be1STheodore Ts'o 	}
18375a916be1STheodore Ts'o 
183808cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
18395a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
184008cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
184108cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
184208cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
18435a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
184408cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
184508cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
184666acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
18475a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
18485a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
18492adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
18505a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
18512adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
18525a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
185366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
18545a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
185566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
18565a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
185766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
18585a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
18592adf6da8STheodore Ts'o 	if (sb->s_flags & MS_I_VERSION)
18605a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
186166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
18625a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
186366acdcf4STheodore Ts'o 	if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
18642adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
18655a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
18662adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
18675a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
18682adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
18695a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
18705a916be1STheodore Ts'o 	}
187166acdcf4STheodore Ts'o 	if (nodefs ||
187266acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
18735a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
18742adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
18752adf6da8STheodore Ts'o 
187666acdcf4STheodore Ts'o 	if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
187766acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
18785a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
1879df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
1880df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
18812adf6da8STheodore Ts'o 
18822adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
18832adf6da8STheodore Ts'o 	return 0;
18842adf6da8STheodore Ts'o }
18852adf6da8STheodore Ts'o 
188666acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
188766acdcf4STheodore Ts'o {
188866acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
188966acdcf4STheodore Ts'o }
189066acdcf4STheodore Ts'o 
1891ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
189266acdcf4STheodore Ts'o {
189366acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
189466acdcf4STheodore Ts'o 	int rc;
189566acdcf4STheodore Ts'o 
189666acdcf4STheodore Ts'o 	seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
189766acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
189866acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
189966acdcf4STheodore Ts'o 	return rc;
190066acdcf4STheodore Ts'o }
190166acdcf4STheodore Ts'o 
1902617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1903ac27a0ecSDave Kleikamp 			    int read_only)
1904ac27a0ecSDave Kleikamp {
1905617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1906ac27a0ecSDave Kleikamp 	int res = 0;
1907ac27a0ecSDave Kleikamp 
1908617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1909b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1910b31e1552SEric Sandeen 			 "forcing read-only mode");
1911ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1912ac27a0ecSDave Kleikamp 	}
1913ac27a0ecSDave Kleikamp 	if (read_only)
1914281b5995STheodore Ts'o 		goto done;
1915617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1916b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1917b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1918c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
1919b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1920b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1921b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1922ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1923ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1924ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1925b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1926b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1927b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1928ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1929ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1930ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1931b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1932b31e1552SEric Sandeen 			 "warning: checktime reached, "
1933b31e1552SEric Sandeen 			 "running e2fsck is recommended");
19340390131bSFrank Mayhar 	if (!sbi->s_journal)
1935216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1936ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1937617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1938e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1939ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1940617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
19410390131bSFrank Mayhar 	if (sbi->s_journal)
1942e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
1943ac27a0ecSDave Kleikamp 
1944e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1945281b5995STheodore Ts'o done:
1946ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1947a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1948a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1949ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1950ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1951617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1952617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1953a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
1954ac27a0ecSDave Kleikamp 
19557abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
1956ac27a0ecSDave Kleikamp 	return res;
1957ac27a0ecSDave Kleikamp }
1958ac27a0ecSDave Kleikamp 
1959117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
1960117fff10STheodore Ts'o {
1961117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1962117fff10STheodore Ts'o 	struct flex_groups *new_groups;
1963117fff10STheodore Ts'o 	int size;
1964117fff10STheodore Ts'o 
1965117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
1966117fff10STheodore Ts'o 		return 0;
1967117fff10STheodore Ts'o 
1968117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
1969117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
1970117fff10STheodore Ts'o 		return 0;
1971117fff10STheodore Ts'o 
1972117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
1973117fff10STheodore Ts'o 	new_groups = ext4_kvzalloc(size, GFP_KERNEL);
1974117fff10STheodore Ts'o 	if (!new_groups) {
1975117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
1976117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
1977117fff10STheodore Ts'o 		return -ENOMEM;
1978117fff10STheodore Ts'o 	}
1979117fff10STheodore Ts'o 
1980117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
1981117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
1982117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
1983117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
1984b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
1985117fff10STheodore Ts'o 	}
1986117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
1987117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
1988117fff10STheodore Ts'o 	return 0;
1989117fff10STheodore Ts'o }
1990117fff10STheodore Ts'o 
1991772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1992772cb7c8SJose R. Santos {
1993772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1994772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1995772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1996117fff10STheodore Ts'o 	int i, err;
1997772cb7c8SJose R. Santos 
1998503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1999d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2000772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2001772cb7c8SJose R. Santos 		return 1;
2002772cb7c8SJose R. Santos 	}
2003772cb7c8SJose R. Santos 
2004117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2005117fff10STheodore Ts'o 	if (err)
2006772cb7c8SJose R. Santos 		goto failed;
2007772cb7c8SJose R. Santos 
2008772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
200988b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2010772cb7c8SJose R. Santos 
2011772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
20127ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
20137ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
201490ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
201524aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
20167ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
20177ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2018772cb7c8SJose R. Santos 	}
2019772cb7c8SJose R. Santos 
2020772cb7c8SJose R. Santos 	return 1;
2021772cb7c8SJose R. Santos failed:
2022772cb7c8SJose R. Santos 	return 0;
2023772cb7c8SJose R. Santos }
2024772cb7c8SJose R. Santos 
2025e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2026717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2027717d50e4SAndreas Dilger {
2028feb0ab32SDarrick J. Wong 	int offset;
2029717d50e4SAndreas Dilger 	__u16 crc = 0;
2030717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2031e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2032717d50e4SAndreas Dilger 
20339aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2034feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2035d6a77105STheodore Ts'o 		__le16 save_csum;
2036feb0ab32SDarrick J. Wong 		__u32 csum32;
2037feb0ab32SDarrick J. Wong 
2038d6a77105STheodore Ts'o 		save_csum = gdp->bg_checksum;
2039feb0ab32SDarrick J. Wong 		gdp->bg_checksum = 0;
2040feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2041feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2042feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp,
2043feb0ab32SDarrick J. Wong 				     sbi->s_desc_size);
2044d6a77105STheodore Ts'o 		gdp->bg_checksum = save_csum;
2045feb0ab32SDarrick J. Wong 
2046feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2047feb0ab32SDarrick J. Wong 		goto out;
2048feb0ab32SDarrick J. Wong 	}
2049feb0ab32SDarrick J. Wong 
2050feb0ab32SDarrick J. Wong 	/* old crc16 code */
2051e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2052813d32f9SDarrick J. Wong 		return 0;
2053813d32f9SDarrick J. Wong 
2054feb0ab32SDarrick J. Wong 	offset = offsetof(struct ext4_group_desc, bg_checksum);
2055feb0ab32SDarrick J. Wong 
2056717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2057717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2058717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2059717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2060717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2061e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2062717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2063717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2064717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2065717d50e4SAndreas Dilger 				offset);
2066717d50e4SAndreas Dilger 
2067feb0ab32SDarrick J. Wong out:
2068717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2069717d50e4SAndreas Dilger }
2070717d50e4SAndreas Dilger 
2071feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2072717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2073717d50e4SAndreas Dilger {
2074feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2075e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2076717d50e4SAndreas Dilger 		return 0;
2077717d50e4SAndreas Dilger 
2078717d50e4SAndreas Dilger 	return 1;
2079717d50e4SAndreas Dilger }
2080717d50e4SAndreas Dilger 
2081feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2082feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2083feb0ab32SDarrick J. Wong {
2084feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2085feb0ab32SDarrick J. Wong 		return;
2086e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2087feb0ab32SDarrick J. Wong }
2088feb0ab32SDarrick J. Wong 
2089ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2090bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2091bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2092ac27a0ecSDave Kleikamp {
2093617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2094617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2095617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2096bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2097bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2098bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2099ce421581SJose R. Santos 	int flexbg_flag = 0;
2100bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2101ac27a0ecSDave Kleikamp 
2102e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2103ce421581SJose R. Santos 		flexbg_flag = 1;
2104ce421581SJose R. Santos 
2105617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2106ac27a0ecSDave Kleikamp 
2107197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2108197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2109197cd65aSAkinobu Mita 
2110ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2111bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2112ac27a0ecSDave Kleikamp 		else
2113ac27a0ecSDave Kleikamp 			last_block = first_block +
2114617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2115ac27a0ecSDave Kleikamp 
2116bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2117bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2118bfff6873SLukas Czerner 			grp = i;
2119bfff6873SLukas Czerner 
21208fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
21212b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2122b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2123a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2124b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2125ac27a0ecSDave Kleikamp 			return 0;
2126ac27a0ecSDave Kleikamp 		}
21278fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
21282b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2129b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2130a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2131b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2132ac27a0ecSDave Kleikamp 			return 0;
2133ac27a0ecSDave Kleikamp 		}
21348fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2135bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
21362b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2137b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2138a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2139b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2140ac27a0ecSDave Kleikamp 			return 0;
2141ac27a0ecSDave Kleikamp 		}
2142955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2143feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2144b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2145b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2146e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2147fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
21487ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2149955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2150717d50e4SAndreas Dilger 				return 0;
2151717d50e4SAndreas Dilger 			}
21527ee1ec4cSLi Zefan 		}
2153955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2154ce421581SJose R. Santos 		if (!flexbg_flag)
2155617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2156ac27a0ecSDave Kleikamp 	}
2157bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2158bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2159ac27a0ecSDave Kleikamp 	return 1;
2160ac27a0ecSDave Kleikamp }
2161ac27a0ecSDave Kleikamp 
2162617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2163ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2164ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2165ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2166ac27a0ecSDave Kleikamp  *
2167ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2168ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2169ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2170ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2171ac27a0ecSDave Kleikamp  *
2172ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2173ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2174ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2175617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2176ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2177ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2178ac27a0ecSDave Kleikamp  */
2179617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2180617ba13bSMingming Cao 				struct ext4_super_block *es)
2181ac27a0ecSDave Kleikamp {
2182ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
2183ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
2184ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2185ac27a0ecSDave Kleikamp 	int i;
2186ac27a0ecSDave Kleikamp #endif
2187ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2188ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2189ac27a0ecSDave Kleikamp 		return;
2190ac27a0ecSDave Kleikamp 	}
2191ac27a0ecSDave Kleikamp 
2192a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2193b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2194b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2195a8f48a95SEric Sandeen 		return;
2196a8f48a95SEric Sandeen 	}
2197a8f48a95SEric Sandeen 
2198d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2199d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2200d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2201d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2202d39195c3SAmir Goldstein 		return;
2203d39195c3SAmir Goldstein 	}
2204d39195c3SAmir Goldstein 
2205617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2206c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
2207c25f9bc6SEric Sandeen 		if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
220884474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2209ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2210ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2211c25f9bc6SEric Sandeen 		}
2212ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2213ac27a0ecSDave Kleikamp 		return;
2214ac27a0ecSDave Kleikamp 	}
2215ac27a0ecSDave Kleikamp 
2216ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2217b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2218ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2219ac27a0ecSDave Kleikamp 	}
2220ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2221ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2222ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2223ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2224a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2225617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2226617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2227ac27a0ecSDave Kleikamp 			if (ret < 0)
2228b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2229b31e1552SEric Sandeen 					"Cannot turn on journaled "
2230b31e1552SEric Sandeen 					"quota: error %d", ret);
2231ac27a0ecSDave Kleikamp 		}
2232ac27a0ecSDave Kleikamp 	}
2233ac27a0ecSDave Kleikamp #endif
2234ac27a0ecSDave Kleikamp 
2235ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2236ac27a0ecSDave Kleikamp 		struct inode *inode;
2237ac27a0ecSDave Kleikamp 
223897bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
223997bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2240ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2241ac27a0ecSDave Kleikamp 			break;
2242ac27a0ecSDave Kleikamp 		}
2243ac27a0ecSDave Kleikamp 
2244617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2245871a2931SChristoph Hellwig 		dquot_initialize(inode);
2246ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2247566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2248b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2249b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
225046e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2251e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2252ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2253721e3ebaSTheodore Ts'o 			mutex_lock(&inode->i_mutex);
225455f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
2255617ba13bSMingming Cao 			ext4_truncate(inode);
2256721e3ebaSTheodore Ts'o 			mutex_unlock(&inode->i_mutex);
2257ac27a0ecSDave Kleikamp 			nr_truncates++;
2258ac27a0ecSDave Kleikamp 		} else {
2259566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2260b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2261b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
226246e665e9SHarvey Harrison 					__func__, inode->i_ino);
2263ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2264ac27a0ecSDave Kleikamp 				  inode->i_ino);
2265ac27a0ecSDave Kleikamp 			nr_orphans++;
2266ac27a0ecSDave Kleikamp 		}
2267ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2268ac27a0ecSDave Kleikamp 	}
2269ac27a0ecSDave Kleikamp 
2270ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2271ac27a0ecSDave Kleikamp 
2272ac27a0ecSDave Kleikamp 	if (nr_orphans)
2273b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2274b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2275ac27a0ecSDave Kleikamp 	if (nr_truncates)
2276b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2277b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2278ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2279ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2280a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2281ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2282287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2283ac27a0ecSDave Kleikamp 	}
2284ac27a0ecSDave Kleikamp #endif
2285ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2286ac27a0ecSDave Kleikamp }
22870b8e58a1SAndreas Dilger 
2288cd2291a4SEric Sandeen /*
2289cd2291a4SEric Sandeen  * Maximal extent format file size.
2290cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2291cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2292cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2293cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2294cd2291a4SEric Sandeen  *
2295f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2296f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2297f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2298f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2299f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2300f17722f9SLukas Czerner  *
2301cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2302cd2291a4SEric Sandeen  */
2303f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2304cd2291a4SEric Sandeen {
2305cd2291a4SEric Sandeen 	loff_t res;
2306cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2307cd2291a4SEric Sandeen 
2308cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2309f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2310cd2291a4SEric Sandeen 		/*
231190c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2312cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2313cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2314cd2291a4SEric Sandeen 		 */
2315cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2316cd2291a4SEric Sandeen 
2317cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2318cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2319cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2320cd2291a4SEric Sandeen 	}
2321cd2291a4SEric Sandeen 
2322f17722f9SLukas Czerner 	/*
2323f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2324f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2325f17722f9SLukas Czerner 	 * size
2326f17722f9SLukas Czerner 	 */
2327f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2328cd2291a4SEric Sandeen 	res <<= blkbits;
2329cd2291a4SEric Sandeen 
2330cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2331cd2291a4SEric Sandeen 	if (res > upper_limit)
2332cd2291a4SEric Sandeen 		res = upper_limit;
2333cd2291a4SEric Sandeen 
2334cd2291a4SEric Sandeen 	return res;
2335cd2291a4SEric Sandeen }
2336ac27a0ecSDave Kleikamp 
2337ac27a0ecSDave Kleikamp /*
2338cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
23390fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
23400fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2341ac27a0ecSDave Kleikamp  */
2342f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2343ac27a0ecSDave Kleikamp {
2344617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
23450fc1b451SAneesh Kumar K.V 	int meta_blocks;
23460fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
23470b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
23480b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
23490b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
23500b8e58a1SAndreas Dilger 	 *
23510b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
23520b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
23530fc1b451SAneesh Kumar K.V 	 */
23540fc1b451SAneesh Kumar K.V 
2355f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
23560fc1b451SAneesh Kumar K.V 		/*
235790c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
23580b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
23590b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
23600fc1b451SAneesh Kumar K.V 		 */
23610fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
23620fc1b451SAneesh Kumar K.V 
23630fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
23640fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
23650fc1b451SAneesh Kumar K.V 
23660fc1b451SAneesh Kumar K.V 	} else {
23678180a562SAneesh Kumar K.V 		/*
23688180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
23698180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
23708180a562SAneesh Kumar K.V 		 * represent total number of blocks in
23718180a562SAneesh Kumar K.V 		 * file system block size
23728180a562SAneesh Kumar K.V 		 */
23730fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
23740fc1b451SAneesh Kumar K.V 
23750fc1b451SAneesh Kumar K.V 	}
23760fc1b451SAneesh Kumar K.V 
23770fc1b451SAneesh Kumar K.V 	/* indirect blocks */
23780fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
23790fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
23800fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
23810fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
23820fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
23830fc1b451SAneesh Kumar K.V 
23840fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
23850fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2386ac27a0ecSDave Kleikamp 
2387ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2388ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2389ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2390ac27a0ecSDave Kleikamp 	res <<= bits;
2391ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2392ac27a0ecSDave Kleikamp 		res = upper_limit;
23930fc1b451SAneesh Kumar K.V 
23940fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
23950fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
23960fc1b451SAneesh Kumar K.V 
2397ac27a0ecSDave Kleikamp 	return res;
2398ac27a0ecSDave Kleikamp }
2399ac27a0ecSDave Kleikamp 
2400617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
240170bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2402ac27a0ecSDave Kleikamp {
2403617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2404fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2405ac27a0ecSDave Kleikamp 	int has_super = 0;
2406ac27a0ecSDave Kleikamp 
2407ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2408ac27a0ecSDave Kleikamp 
2409e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
241070bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2411ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2412617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2413ac27a0ecSDave Kleikamp 		has_super = 1;
24140b8e58a1SAndreas Dilger 
2415bd63f6b0SDarrick J. Wong 	/*
2416bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2417bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2418bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2419bd63f6b0SDarrick J. Wong 	 * compensate.
2420bd63f6b0SDarrick J. Wong 	 */
2421bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
2422bd63f6b0SDarrick J. Wong 	    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) == 0)
2423bd63f6b0SDarrick J. Wong 		has_super++;
2424bd63f6b0SDarrick J. Wong 
2425617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2426ac27a0ecSDave Kleikamp }
2427ac27a0ecSDave Kleikamp 
2428c9de560dSAlex Tomas /**
2429c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2430c9de560dSAlex Tomas  * @sbi: In memory super block info
2431c9de560dSAlex Tomas  *
2432c9de560dSAlex Tomas  * If we have specified it via mount option, then
2433c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2434c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2435c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2436c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2437c9de560dSAlex Tomas  *
2438c9de560dSAlex Tomas  */
2439c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2440c9de560dSAlex Tomas {
2441c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2442c9de560dSAlex Tomas 	unsigned long stripe_width =
2443c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
24443eb08658SDan Ehrenberg 	int ret;
2445c9de560dSAlex Tomas 
2446c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
24473eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
24483eb08658SDan Ehrenberg 	else if (stripe_width <= sbi->s_blocks_per_group)
24493eb08658SDan Ehrenberg 		ret = stripe_width;
24503eb08658SDan Ehrenberg 	else if (stride <= sbi->s_blocks_per_group)
24513eb08658SDan Ehrenberg 		ret = stride;
24523eb08658SDan Ehrenberg 	else
24533eb08658SDan Ehrenberg 		ret = 0;
2454c9de560dSAlex Tomas 
24553eb08658SDan Ehrenberg 	/*
24563eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
24573eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
24583eb08658SDan Ehrenberg 	 */
24593eb08658SDan Ehrenberg 	if (ret <= 1)
24603eb08658SDan Ehrenberg 		ret = 0;
2461c9de560dSAlex Tomas 
24623eb08658SDan Ehrenberg 	return ret;
2463c9de560dSAlex Tomas }
2464ac27a0ecSDave Kleikamp 
2465a13fb1a4SEric Sandeen /*
2466a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2467a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2468a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2469a13fb1a4SEric Sandeen  * 0 if it cannot be.
2470a13fb1a4SEric Sandeen  */
2471a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2472a13fb1a4SEric Sandeen {
2473e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2474a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2475a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2476a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2477a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2478a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2479a13fb1a4SEric Sandeen 		return 0;
2480a13fb1a4SEric Sandeen 	}
2481a13fb1a4SEric Sandeen 
2482a13fb1a4SEric Sandeen 	if (readonly)
2483a13fb1a4SEric Sandeen 		return 1;
2484a13fb1a4SEric Sandeen 
2485e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
24862cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
24872cb5cc8bSDarrick J. Wong 		sb->s_flags |= MS_RDONLY;
24882cb5cc8bSDarrick J. Wong 		return 1;
24892cb5cc8bSDarrick J. Wong 	}
24902cb5cc8bSDarrick J. Wong 
2491a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2492e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
2493a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2494a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2495a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2496a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2497a13fb1a4SEric Sandeen 		return 0;
2498a13fb1a4SEric Sandeen 	}
2499a13fb1a4SEric Sandeen 	/*
2500a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2501a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2502a13fb1a4SEric Sandeen 	 */
2503e2b911c5SDarrick J. Wong 	if (ext4_has_feature_huge_file(sb)) {
2504a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2505a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2506a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2507a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2508a13fb1a4SEric Sandeen 			return 0;
2509a13fb1a4SEric Sandeen 		}
2510a13fb1a4SEric Sandeen 	}
2511e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
2512bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2513bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2514bab08ab9STheodore Ts'o 			 "extents feature\n");
2515bab08ab9STheodore Ts'o 		return 0;
2516bab08ab9STheodore Ts'o 	}
25177c319d32SAditya Kali 
25187c319d32SAditya Kali #ifndef CONFIG_QUOTA
2519e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !readonly) {
25207c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
25217c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
25227c319d32SAditya Kali 			 "without CONFIG_QUOTA");
25237c319d32SAditya Kali 		return 0;
25247c319d32SAditya Kali 	}
25257c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2526a13fb1a4SEric Sandeen 	return 1;
2527a13fb1a4SEric Sandeen }
2528a13fb1a4SEric Sandeen 
252966e61a9eSTheodore Ts'o /*
253066e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
253166e61a9eSTheodore Ts'o  * on the file system
253266e61a9eSTheodore Ts'o  */
253366e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
253466e61a9eSTheodore Ts'o {
253566e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
253666e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
253766e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
253866e61a9eSTheodore Ts'o 
253966e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
254066e61a9eSTheodore Ts'o 	es = sbi->s_es;
254166e61a9eSTheodore Ts'o 
254266e61a9eSTheodore Ts'o 	if (es->s_error_count)
2543ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
2544ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
254566e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
254666e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
2547ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %u: %.*s:%d",
254866e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
254966e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
255066e61a9eSTheodore Ts'o 		       es->s_first_error_func,
255166e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
255266e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
255366e61a9eSTheodore Ts'o 			printk(": inode %u",
255466e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
255566e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
255666e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
255766e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
255866e61a9eSTheodore Ts'o 		printk("\n");
255966e61a9eSTheodore Ts'o 	}
256066e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
2561ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %u: %.*s:%d",
256266e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
256366e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
256466e61a9eSTheodore Ts'o 		       es->s_last_error_func,
256566e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
256666e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
256766e61a9eSTheodore Ts'o 			printk(": inode %u",
256866e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
256966e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
257066e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
257166e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
257266e61a9eSTheodore Ts'o 		printk("\n");
257366e61a9eSTheodore Ts'o 	}
257466e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
257566e61a9eSTheodore Ts'o }
257666e61a9eSTheodore Ts'o 
2577bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2578bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2579bfff6873SLukas Czerner {
2580bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2581bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2582bfff6873SLukas Czerner 	struct super_block *sb;
2583bfff6873SLukas Czerner 	unsigned long timeout = 0;
2584bfff6873SLukas Czerner 	int ret = 0;
2585bfff6873SLukas Czerner 
2586bfff6873SLukas Czerner 	sb = elr->lr_super;
2587bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2588bfff6873SLukas Czerner 
25898e8ad8a5SJan Kara 	sb_start_write(sb);
2590bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2591bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2592bfff6873SLukas Czerner 		if (!gdp) {
2593bfff6873SLukas Czerner 			ret = 1;
2594bfff6873SLukas Czerner 			break;
2595bfff6873SLukas Czerner 		}
2596bfff6873SLukas Czerner 
2597bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2598bfff6873SLukas Czerner 			break;
2599bfff6873SLukas Czerner 	}
2600bfff6873SLukas Czerner 
26017f511862STheodore Ts'o 	if (group >= ngroups)
2602bfff6873SLukas Czerner 		ret = 1;
2603bfff6873SLukas Czerner 
2604bfff6873SLukas Czerner 	if (!ret) {
2605bfff6873SLukas Czerner 		timeout = jiffies;
2606bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2607bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2608bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
260951ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
261051ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2611bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2612bfff6873SLukas Czerner 		}
2613bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2614bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2615bfff6873SLukas Czerner 	}
26168e8ad8a5SJan Kara 	sb_end_write(sb);
2617bfff6873SLukas Czerner 
2618bfff6873SLukas Czerner 	return ret;
2619bfff6873SLukas Czerner }
2620bfff6873SLukas Czerner 
2621bfff6873SLukas Czerner /*
2622bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
26234ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2624bfff6873SLukas Czerner  */
2625bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2626bfff6873SLukas Czerner {
2627bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2628bfff6873SLukas Czerner 
2629bfff6873SLukas Czerner 	if (!elr)
2630bfff6873SLukas Czerner 		return;
2631bfff6873SLukas Czerner 
2632bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2633bfff6873SLukas Czerner 
2634bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2635bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2636bfff6873SLukas Czerner 	kfree(elr);
2637bfff6873SLukas Czerner }
2638bfff6873SLukas Czerner 
2639bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2640bfff6873SLukas Czerner {
26411bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
26421bb933fbSLukas Czerner 	if (!ext4_li_info) {
26431bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2644bfff6873SLukas Czerner 		return;
26451bb933fbSLukas Czerner 	}
2646bfff6873SLukas Czerner 
2647bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
26481bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2649bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
26501bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2651bfff6873SLukas Czerner }
2652bfff6873SLukas Czerner 
26538f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
26548f1f7453SEric Sandeen 
2655bfff6873SLukas Czerner /*
2656bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2657bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2658bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2659bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2660bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2661bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2662bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2663bfff6873SLukas Czerner  */
2664bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2665bfff6873SLukas Czerner {
2666bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2667bfff6873SLukas Czerner 	struct list_head *pos, *n;
2668bfff6873SLukas Czerner 	struct ext4_li_request *elr;
26694ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2670bfff6873SLukas Czerner 
2671bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2672bfff6873SLukas Czerner 
2673bfff6873SLukas Czerner cont_thread:
2674bfff6873SLukas Czerner 	while (true) {
2675bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2676bfff6873SLukas Czerner 
2677bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2678bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2679bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2680bfff6873SLukas Czerner 			goto exit_thread;
2681bfff6873SLukas Czerner 		}
2682bfff6873SLukas Czerner 
2683bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2684bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2685bfff6873SLukas Czerner 					 lr_request);
2686bfff6873SLukas Czerner 
2687b2c78cd0STheodore Ts'o 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
2688b2c78cd0STheodore Ts'o 				if (ext4_run_li_request(elr) != 0) {
2689b2c78cd0STheodore Ts'o 					/* error, remove the lazy_init job */
2690bfff6873SLukas Czerner 					ext4_remove_li_request(elr);
2691bfff6873SLukas Czerner 					continue;
2692bfff6873SLukas Czerner 				}
2693b2c78cd0STheodore Ts'o 			}
2694bfff6873SLukas Czerner 
2695bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2696bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2697bfff6873SLukas Czerner 		}
2698bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2699bfff6873SLukas Czerner 
2700a0acae0eSTejun Heo 		try_to_freeze();
2701bfff6873SLukas Czerner 
27024ed5c033SLukas Czerner 		cur = jiffies;
27034ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2704f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2705bfff6873SLukas Czerner 			cond_resched();
2706bfff6873SLukas Czerner 			continue;
2707bfff6873SLukas Czerner 		}
2708bfff6873SLukas Czerner 
27094ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
27104ed5c033SLukas Czerner 
27118f1f7453SEric Sandeen 		if (kthread_should_stop()) {
27128f1f7453SEric Sandeen 			ext4_clear_request_list();
27138f1f7453SEric Sandeen 			goto exit_thread;
27148f1f7453SEric Sandeen 		}
2715bfff6873SLukas Czerner 	}
2716bfff6873SLukas Czerner 
2717bfff6873SLukas Czerner exit_thread:
2718bfff6873SLukas Czerner 	/*
2719bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
2720bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
2721bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
2722bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
2723bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
2724bfff6873SLukas Czerner 	 * new one.
2725bfff6873SLukas Czerner 	 */
2726bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2727bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
2728bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
2729bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2730bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2731bfff6873SLukas Czerner 		goto cont_thread;
2732bfff6873SLukas Czerner 	}
2733bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
2734bfff6873SLukas Czerner 	kfree(ext4_li_info);
2735bfff6873SLukas Czerner 	ext4_li_info = NULL;
2736bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2737bfff6873SLukas Czerner 
2738bfff6873SLukas Czerner 	return 0;
2739bfff6873SLukas Czerner }
2740bfff6873SLukas Czerner 
2741bfff6873SLukas Czerner static void ext4_clear_request_list(void)
2742bfff6873SLukas Czerner {
2743bfff6873SLukas Czerner 	struct list_head *pos, *n;
2744bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2745bfff6873SLukas Czerner 
2746bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2747bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2748bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
2749bfff6873SLukas Czerner 				 lr_request);
2750bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
2751bfff6873SLukas Czerner 	}
2752bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2753bfff6873SLukas Czerner }
2754bfff6873SLukas Czerner 
2755bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
2756bfff6873SLukas Czerner {
27578f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
27588f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
27598f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
27608f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
2761bfff6873SLukas Czerner 		ext4_clear_request_list();
2762bfff6873SLukas Czerner 		kfree(ext4_li_info);
2763bfff6873SLukas Czerner 		ext4_li_info = NULL;
276492b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
2765bfff6873SLukas Czerner 				 "initialization thread\n",
2766bfff6873SLukas Czerner 				 err);
2767bfff6873SLukas Czerner 		return err;
2768bfff6873SLukas Czerner 	}
2769bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
2770bfff6873SLukas Czerner 	return 0;
2771bfff6873SLukas Czerner }
2772bfff6873SLukas Czerner 
2773bfff6873SLukas Czerner /*
2774bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
2775bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
2776bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
2777bfff6873SLukas Czerner  * groups and return total number of groups.
2778bfff6873SLukas Czerner  */
2779bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2780bfff6873SLukas Czerner {
2781bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2782bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2783bfff6873SLukas Czerner 
2784bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
2785bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2786bfff6873SLukas Czerner 		if (!gdp)
2787bfff6873SLukas Czerner 			continue;
2788bfff6873SLukas Czerner 
2789bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2790bfff6873SLukas Czerner 			break;
2791bfff6873SLukas Czerner 	}
2792bfff6873SLukas Czerner 
2793bfff6873SLukas Czerner 	return group;
2794bfff6873SLukas Czerner }
2795bfff6873SLukas Czerner 
2796bfff6873SLukas Czerner static int ext4_li_info_new(void)
2797bfff6873SLukas Czerner {
2798bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
2799bfff6873SLukas Czerner 
2800bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2801bfff6873SLukas Czerner 	if (!eli)
2802bfff6873SLukas Czerner 		return -ENOMEM;
2803bfff6873SLukas Czerner 
2804bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
2805bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
2806bfff6873SLukas Czerner 
2807bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
2808bfff6873SLukas Czerner 
2809bfff6873SLukas Czerner 	ext4_li_info = eli;
2810bfff6873SLukas Czerner 
2811bfff6873SLukas Czerner 	return 0;
2812bfff6873SLukas Czerner }
2813bfff6873SLukas Czerner 
2814bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
2815bfff6873SLukas Czerner 					    ext4_group_t start)
2816bfff6873SLukas Czerner {
2817bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2818bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2819bfff6873SLukas Czerner 
2820bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
2821bfff6873SLukas Czerner 	if (!elr)
2822bfff6873SLukas Czerner 		return NULL;
2823bfff6873SLukas Czerner 
2824bfff6873SLukas Czerner 	elr->lr_super = sb;
2825bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
2826bfff6873SLukas Czerner 	elr->lr_next_group = start;
2827bfff6873SLukas Czerner 
2828bfff6873SLukas Czerner 	/*
2829bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
2830bfff6873SLukas Czerner 	 * spread the inode table initialization requests
2831bfff6873SLukas Czerner 	 * better.
2832bfff6873SLukas Czerner 	 */
2833dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
2834dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
2835bfff6873SLukas Czerner 	return elr;
2836bfff6873SLukas Czerner }
2837bfff6873SLukas Czerner 
28387f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
2839bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
2840bfff6873SLukas Czerner {
2841bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
28427f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
2843bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
28446c5a6cb9SAndrew Morton 	int ret = 0;
2845bfff6873SLukas Czerner 
28467f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
284751ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
284851ce6511SLukas Czerner 		/*
284951ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
285051ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
285151ce6511SLukas Czerner 		 */
285251ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
28537f511862STheodore Ts'o 		goto out;
285451ce6511SLukas Czerner 	}
2855bfff6873SLukas Czerner 
2856bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
2857bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
285855ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
28597f511862STheodore Ts'o 		goto out;
2860bfff6873SLukas Czerner 
2861bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
28627f511862STheodore Ts'o 	if (!elr) {
28637f511862STheodore Ts'o 		ret = -ENOMEM;
28647f511862STheodore Ts'o 		goto out;
28657f511862STheodore Ts'o 	}
2866bfff6873SLukas Czerner 
2867bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
2868bfff6873SLukas Czerner 		ret = ext4_li_info_new();
2869bfff6873SLukas Czerner 		if (ret)
2870bfff6873SLukas Czerner 			goto out;
2871bfff6873SLukas Czerner 	}
2872bfff6873SLukas Czerner 
2873bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2874bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
2875bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2876bfff6873SLukas Czerner 
2877bfff6873SLukas Czerner 	sbi->s_li_request = elr;
287846e4690bSTao Ma 	/*
287946e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
288046e4690bSTao Ma 	 * the request_list and the removal and free of it is
288146e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
288246e4690bSTao Ma 	 */
288346e4690bSTao Ma 	elr = NULL;
2884bfff6873SLukas Czerner 
2885bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
2886bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
2887bfff6873SLukas Czerner 		if (ret)
2888bfff6873SLukas Czerner 			goto out;
2889bfff6873SLukas Czerner 	}
2890bfff6873SLukas Czerner out:
2891bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2892beed5ecbSNicolas Kaiser 	if (ret)
2893bfff6873SLukas Czerner 		kfree(elr);
2894bfff6873SLukas Czerner 	return ret;
2895bfff6873SLukas Czerner }
2896bfff6873SLukas Czerner 
2897bfff6873SLukas Czerner /*
2898bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
2899bfff6873SLukas Czerner  * module unload.
2900bfff6873SLukas Czerner  */
2901bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
2902bfff6873SLukas Czerner {
2903bfff6873SLukas Czerner 	/*
2904bfff6873SLukas Czerner 	 * If thread exited earlier
2905bfff6873SLukas Czerner 	 * there's nothing to be done.
2906bfff6873SLukas Czerner 	 */
29078f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
2908bfff6873SLukas Czerner 		return;
2909bfff6873SLukas Czerner 
29108f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
2911bfff6873SLukas Czerner }
2912bfff6873SLukas Czerner 
291325ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
291425ed6e8aSDarrick J. Wong {
291525ed6e8aSDarrick J. Wong 	int ret = 1;
291625ed6e8aSDarrick J. Wong 	int compat, incompat;
291725ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
291825ed6e8aSDarrick J. Wong 
29199aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
2920db9ee220SDarrick J. Wong 		/* journal checksum v3 */
292125ed6e8aSDarrick J. Wong 		compat = 0;
2922db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
292325ed6e8aSDarrick J. Wong 	} else {
292425ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
292525ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
292625ed6e8aSDarrick J. Wong 		incompat = 0;
292725ed6e8aSDarrick J. Wong 	}
292825ed6e8aSDarrick J. Wong 
2929feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
2930feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2931feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
2932feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
293325ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
293425ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
293525ed6e8aSDarrick J. Wong 				compat, 0,
293625ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
293725ed6e8aSDarrick J. Wong 				incompat);
293825ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
293925ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
294025ed6e8aSDarrick J. Wong 				compat, 0,
294125ed6e8aSDarrick J. Wong 				incompat);
294225ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
294325ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
294425ed6e8aSDarrick J. Wong 	} else {
2945feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2946feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
294725ed6e8aSDarrick J. Wong 	}
294825ed6e8aSDarrick J. Wong 
294925ed6e8aSDarrick J. Wong 	return ret;
295025ed6e8aSDarrick J. Wong }
295125ed6e8aSDarrick J. Wong 
2952952fc18eSTheodore Ts'o /*
2953952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
2954952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
2955952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
2956952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
2957952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
2958952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
2959952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
2960952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
2961952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
2962952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
2963952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
2964952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
2965952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
2966952fc18eSTheodore Ts'o  */
2967952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
2968952fc18eSTheodore Ts'o 			  char *buf)
2969952fc18eSTheodore Ts'o {
2970952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
2971952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
2972952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
2973952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
2974952fc18eSTheodore Ts'o 	int			s, j, count = 0;
2975952fc18eSTheodore Ts'o 
2976e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
29770548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
29780548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
29790548bbb8STheodore Ts'o 
2980952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
2981952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
2982952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
2983952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
2984952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2985952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
2986952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
2987952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
2988952fc18eSTheodore Ts'o 			count++;
2989952fc18eSTheodore Ts'o 		}
2990952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
2991952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
2992952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
2993952fc18eSTheodore Ts'o 			count++;
2994952fc18eSTheodore Ts'o 		}
2995952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
2996952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
2997952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
2998952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
2999952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3000952fc18eSTheodore Ts'o 				count++;
3001952fc18eSTheodore Ts'o 			}
3002952fc18eSTheodore Ts'o 		if (i != grp)
3003952fc18eSTheodore Ts'o 			continue;
3004952fc18eSTheodore Ts'o 		s = 0;
3005952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3006952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3007952fc18eSTheodore Ts'o 			count++;
3008952fc18eSTheodore Ts'o 		}
3009952fc18eSTheodore Ts'o 		for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) {
3010952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3011952fc18eSTheodore Ts'o 			count++;
3012952fc18eSTheodore Ts'o 		}
3013952fc18eSTheodore Ts'o 	}
3014952fc18eSTheodore Ts'o 	if (!count)
3015952fc18eSTheodore Ts'o 		return 0;
3016952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3017952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3018952fc18eSTheodore Ts'o }
3019952fc18eSTheodore Ts'o 
3020952fc18eSTheodore Ts'o /*
3021952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3022952fc18eSTheodore Ts'o  */
3023952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3024952fc18eSTheodore Ts'o {
3025952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3026952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
3027952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3028952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
30294fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3030952fc18eSTheodore Ts'o 
3031952fc18eSTheodore Ts'o 	if (!buf)
3032952fc18eSTheodore Ts'o 		return -ENOMEM;
3033952fc18eSTheodore Ts'o 
3034952fc18eSTheodore Ts'o 	/*
3035952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3036952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3037952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3038952fc18eSTheodore Ts'o 	 */
3039952fc18eSTheodore Ts'o 
3040952fc18eSTheodore Ts'o 	/*
3041952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3042952fc18eSTheodore Ts'o 	 */
3043952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3044952fc18eSTheodore Ts'o 
3045952fc18eSTheodore Ts'o 	/*
3046952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3047952fc18eSTheodore Ts'o 	 */
3048952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3049952fc18eSTheodore Ts'o 		int blks;
3050952fc18eSTheodore Ts'o 
3051952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3052952fc18eSTheodore Ts'o 		overhead += blks;
3053952fc18eSTheodore Ts'o 		if (blks)
3054952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3055952fc18eSTheodore Ts'o 		cond_resched();
3056952fc18eSTheodore Ts'o 	}
3057b003b524SEric Sandeen 	/* Add the internal journal blocks as well */
3058b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3059810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
30600875a2b4SEric Sandeen 
3061952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3062952fc18eSTheodore Ts'o 	smp_wmb();
3063952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3064952fc18eSTheodore Ts'o 	return 0;
3065952fc18eSTheodore Ts'o }
3066952fc18eSTheodore Ts'o 
3067b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
306827dd4385SLukas Czerner {
306927dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3070b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
307127dd4385SLukas Czerner 
307227dd4385SLukas Czerner 	/*
307330fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
307430fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
307530fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
307630fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
307730fac0f7SJan Kara 	 */
3078e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3079b5799018STheodore Ts'o 		return;
308030fac0f7SJan Kara 	/*
308127dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
308227dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
308327dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3084556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
308527dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
308627dd4385SLukas Czerner 	 * very rare.
308727dd4385SLukas Czerner 	 */
3088b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3089b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
309027dd4385SLukas Czerner 
309127dd4385SLukas Czerner 	do_div(resv_clusters, 50);
309227dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
309327dd4385SLukas Czerner 
3094b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
309527dd4385SLukas Czerner }
309627dd4385SLukas Czerner 
3097617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3098ac27a0ecSDave Kleikamp {
3099d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3100ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3101617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
3102617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
3103617ba13bSMingming Cao 	ext4_fsblk_t block;
3104617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
310570bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3106ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3107ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3108ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3109ac27a0ecSDave Kleikamp 	struct inode *root;
31100390131bSFrank Mayhar 	const char *descr;
3111dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3112281b5995STheodore Ts'o 	int blocksize, clustersize;
31134ec11028STheodore Ts'o 	unsigned int db_count;
31144ec11028STheodore Ts'o 	unsigned int i;
3115281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3116bd81d8eeSLaurent Vivier 	__u64 blocks_count;
311707aa2ea1SLukas Czerner 	int err = 0;
3118b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3119bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3120ac27a0ecSDave Kleikamp 
3121ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3122ac27a0ecSDave Kleikamp 	if (!sbi)
3123dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3124705895b6SPekka Enberg 
3125705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3126705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3127705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
3128705895b6SPekka Enberg 		kfree(sbi);
3129dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3130705895b6SPekka Enberg 	}
3131ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
31322c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3133240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3134d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3135f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3136f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3137f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3138ac27a0ecSDave Kleikamp 
31399f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3140ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
31419f6200bbSTheodore Ts'o 
314207aa2ea1SLukas Czerner 	/* -EINVAL is default */
3143dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3144617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3145ac27a0ecSDave Kleikamp 	if (!blocksize) {
3146b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3147ac27a0ecSDave Kleikamp 		goto out_fail;
3148ac27a0ecSDave Kleikamp 	}
3149ac27a0ecSDave Kleikamp 
3150ac27a0ecSDave Kleikamp 	/*
3151617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3152ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3153ac27a0ecSDave Kleikamp 	 */
3154617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
315570bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
315670bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3157ac27a0ecSDave Kleikamp 	} else {
315870bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3159ac27a0ecSDave Kleikamp 	}
3160ac27a0ecSDave Kleikamp 
3161a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3162b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3163ac27a0ecSDave Kleikamp 		goto out_fail;
3164ac27a0ecSDave Kleikamp 	}
3165ac27a0ecSDave Kleikamp 	/*
3166ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3167617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3168ac27a0ecSDave Kleikamp 	 */
31692716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3170ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3171ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3172617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3173617ba13bSMingming Cao 		goto cantfind_ext4;
3174afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3175ac27a0ecSDave Kleikamp 
3176feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3177e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3178e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3179363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3180feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3181feb0ab32SDarrick J. Wong 
3182d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3183d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3184d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3185d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3186d25425f8SDarrick J. Wong 		silent = 1;
3187d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3188d25425f8SDarrick J. Wong 	}
3189d25425f8SDarrick J. Wong 
31900441984aSDarrick J. Wong 	/* Load the checksum driver */
3191e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb)) {
31920441984aSDarrick J. Wong 		sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
31930441984aSDarrick J. Wong 		if (IS_ERR(sbi->s_chksum_driver)) {
31940441984aSDarrick J. Wong 			ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
31950441984aSDarrick J. Wong 			ret = PTR_ERR(sbi->s_chksum_driver);
31960441984aSDarrick J. Wong 			sbi->s_chksum_driver = NULL;
31970441984aSDarrick J. Wong 			goto failed_mount;
31980441984aSDarrick J. Wong 		}
31990441984aSDarrick J. Wong 	}
32000441984aSDarrick J. Wong 
3201a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3202a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3203a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3204a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3205a9c47317SDarrick J. Wong 		silent = 1;
32066a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3207a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3208a9c47317SDarrick J. Wong 	}
3209a9c47317SDarrick J. Wong 
3210a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3211e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
32128c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
32138c81bd8fSDarrick J. Wong 	else if (ext4_has_metadata_csum(sb))
3214a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3215a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3216a9c47317SDarrick J. Wong 
3217ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3218ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3219fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3220617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3221fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
322287f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3223fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3224617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3225fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3226ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3227fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
322803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3229fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
32302e7842b8SHugh Dickins #endif
323198c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
323298c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
323398c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
323498c1a759SDarrick J. Wong 
3235617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3236fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3237617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3238fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3239617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3240fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3241ac27a0ecSDave Kleikamp 
3242617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3243fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3244bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3245fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3246bb4f397aSAneesh Kumar K.V 	else
3247fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
324845f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3249fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
32508b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3251fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3252ac27a0ecSDave Kleikamp 
325308cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
325408cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
325530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
325630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
325730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3258ac27a0ecSDave Kleikamp 
32598b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3260fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3261ac27a0ecSDave Kleikamp 
32621e2462f9SMingming Cao 	/*
3263dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3264dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3265dd919b98SAneesh Kumar K.V 	 */
3266bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
32678b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3268fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3269dd919b98SAneesh Kumar K.V 
327051ce6511SLukas Czerner 	/*
327151ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
327251ce6511SLukas Czerner 	 * no mount option specified.
327351ce6511SLukas Czerner 	 */
327451ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
327551ce6511SLukas Czerner 
32768b67f04aSTheodore Ts'o 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
3277661aa520SEric Sandeen 			   &journal_devnum, &journal_ioprio, 0)) {
32788b67f04aSTheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
32798b67f04aSTheodore Ts'o 			 "failed to parse options in superblock: %s",
32808b67f04aSTheodore Ts'o 			 sbi->s_es->s_mount_opts);
32818b67f04aSTheodore Ts'o 	}
32825a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3283b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3284661aa520SEric Sandeen 			   &journal_ioprio, 0))
3285ac27a0ecSDave Kleikamp 		goto failed_mount;
3286ac27a0ecSDave Kleikamp 
328756889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
328856889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
328956889787STheodore Ts'o 			    "with data=journal disables delayed "
329056889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
329156889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
329256889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
329356889787STheodore Ts'o 				 "both data=journal and delalloc");
329456889787STheodore Ts'o 			goto failed_mount;
329556889787STheodore Ts'o 		}
329656889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
329756889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
32986ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
329956889787STheodore Ts'o 			goto failed_mount;
330056889787STheodore Ts'o 		}
3301923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3302923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3303923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3304923ae0ffSRoss Zwisler 			goto failed_mount;
3305923ae0ffSRoss Zwisler 		}
330656889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
330756889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3308001e4a87STejun Heo 	} else {
3309001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
331056889787STheodore Ts'o 	}
331156889787STheodore Ts'o 
3312ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3313482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3314ac27a0ecSDave Kleikamp 
3315617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3316e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
3317e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
3318e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
3319b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3320b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3321b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3322469108ffSTheodore Tso 
3323ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3324ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3325e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
3326ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3327ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3328ed3654ebSTheodore Ts'o 			goto failed_mount;
3329ed3654ebSTheodore Ts'o 		}
3330ed3654ebSTheodore Ts'o 	}
3331ed3654ebSTheodore Ts'o 
33322035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
33332035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
33342035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
33352035e776STheodore Ts'o 				 "using the ext4 subsystem");
33362035e776STheodore Ts'o 		else {
33372035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
33382035e776STheodore Ts'o 				 "to feature incompatibilities");
33392035e776STheodore Ts'o 			goto failed_mount;
33402035e776STheodore Ts'o 		}
33412035e776STheodore Ts'o 	}
33422035e776STheodore Ts'o 
33432035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
33442035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
33452035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
33462035e776STheodore Ts'o 				 "using the ext4 subsystem");
33472035e776STheodore Ts'o 		else {
33482035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
33492035e776STheodore Ts'o 				 "to feature incompatibilities");
33502035e776STheodore Ts'o 			goto failed_mount;
33512035e776STheodore Ts'o 		}
33522035e776STheodore Ts'o 	}
33532035e776STheodore Ts'o 
3354469108ffSTheodore Tso 	/*
3355ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3356ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3357ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3358ac27a0ecSDave Kleikamp 	 */
3359a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3360ac27a0ecSDave Kleikamp 		goto failed_mount;
3361a13fb1a4SEric Sandeen 
3362261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3363617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3364617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3365b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3366b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
3367ac27a0ecSDave Kleikamp 		goto failed_mount;
3368ac27a0ecSDave Kleikamp 	}
3369ac27a0ecSDave Kleikamp 
3370923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
3371923ae0ffSRoss Zwisler 		if (blocksize != PAGE_SIZE) {
3372923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR,
3373923ae0ffSRoss Zwisler 					"error: unsupported blocksize for dax");
3374923ae0ffSRoss Zwisler 			goto failed_mount;
3375923ae0ffSRoss Zwisler 		}
3376923ae0ffSRoss Zwisler 		if (!sb->s_bdev->bd_disk->fops->direct_access) {
3377923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR,
3378923ae0ffSRoss Zwisler 					"error: device does not support dax");
3379923ae0ffSRoss Zwisler 			goto failed_mount;
3380923ae0ffSRoss Zwisler 		}
3381923ae0ffSRoss Zwisler 	}
3382923ae0ffSRoss Zwisler 
3383e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
33846ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
33856ddb2447STheodore Ts'o 			 es->s_encryption_level);
33866ddb2447STheodore Ts'o 		goto failed_mount;
33876ddb2447STheodore Ts'o 	}
33886ddb2447STheodore Ts'o 
3389ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3390ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3391ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3392b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3393ce40733cSAneesh Kumar K.V 					blocksize);
3394ac27a0ecSDave Kleikamp 			goto failed_mount;
3395ac27a0ecSDave Kleikamp 		}
3396ac27a0ecSDave Kleikamp 
3397ac27a0ecSDave Kleikamp 		brelse(bh);
339870bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
339970bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3400a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
3401ac27a0ecSDave Kleikamp 		if (!bh) {
3402b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3403b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3404ac27a0ecSDave Kleikamp 			goto failed_mount;
3405ac27a0ecSDave Kleikamp 		}
34062716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
3407ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3408617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3409b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3410b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3411ac27a0ecSDave Kleikamp 			goto failed_mount;
3412ac27a0ecSDave Kleikamp 		}
3413ac27a0ecSDave Kleikamp 	}
3414ac27a0ecSDave Kleikamp 
3415e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
3416f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3417f287a1a5STheodore Ts'o 						      has_huge_files);
3418f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3419ac27a0ecSDave Kleikamp 
3420617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3421617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3422617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3423ac27a0ecSDave Kleikamp 	} else {
3424ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3425ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3426617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
34271330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3428ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3429b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3430b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3431ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3432ac27a0ecSDave Kleikamp 			goto failed_mount;
3433ac27a0ecSDave Kleikamp 		}
3434ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3435ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3436ac27a0ecSDave Kleikamp 	}
34370b8e58a1SAndreas Dilger 
34380d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
3439e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
34408fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
34410d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3442d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3443b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3444b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
34450d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
34460d1ee42fSAlexandre Ratchov 			goto failed_mount;
34470d1ee42fSAlexandre Ratchov 		}
34480d1ee42fSAlexandre Ratchov 	} else
34490d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
34500b8e58a1SAndreas Dilger 
3451ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3452ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3453b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
3454617ba13bSMingming Cao 		goto cantfind_ext4;
34550b8e58a1SAndreas Dilger 
3456617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3457ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3458617ba13bSMingming Cao 		goto cantfind_ext4;
3459ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3460ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
34610d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3462ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3463ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3464e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3465e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
34660b8e58a1SAndreas Dilger 
3467ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3468ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3469ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3470e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
3471f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
3472f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
3473f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3474f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3475f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
347623301410STheodore Ts'o 			if (!(sb->s_flags & MS_RDONLY))
347723301410STheodore Ts'o 				es->s_flags |=
347823301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3479f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3480f99b2589STheodore Ts'o #else
348123301410STheodore Ts'o 			if (!(sb->s_flags & MS_RDONLY))
348223301410STheodore Ts'o 				es->s_flags |=
348323301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3484f99b2589STheodore Ts'o #endif
3485f99b2589STheodore Ts'o 		}
348623301410STheodore Ts'o 	}
3487ac27a0ecSDave Kleikamp 
3488281b5995STheodore Ts'o 	/* Handle clustersize */
3489281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3490e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
3491281b5995STheodore Ts'o 	if (has_bigalloc) {
3492281b5995STheodore Ts'o 		if (clustersize < blocksize) {
3493281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3494281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
3495281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
3496281b5995STheodore Ts'o 			goto failed_mount;
3497281b5995STheodore Ts'o 		}
3498281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3499281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
3500281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
3501281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
3502281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
3503281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3504281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
3505281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3506281b5995STheodore Ts'o 			goto failed_mount;
3507281b5995STheodore Ts'o 		}
3508281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
3509281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3510281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3511281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
3512281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
3513281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3514281b5995STheodore Ts'o 			goto failed_mount;
3515281b5995STheodore Ts'o 		}
3516281b5995STheodore Ts'o 	} else {
3517281b5995STheodore Ts'o 		if (clustersize != blocksize) {
3518281b5995STheodore Ts'o 			ext4_warning(sb, "fragment/cluster size (%d) != "
3519281b5995STheodore Ts'o 				     "block size (%d)", clustersize,
3520281b5995STheodore Ts'o 				     blocksize);
3521281b5995STheodore Ts'o 			clustersize = blocksize;
3522281b5995STheodore Ts'o 		}
3523ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
3524b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3525b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
3526ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
3527ac27a0ecSDave Kleikamp 			goto failed_mount;
3528ac27a0ecSDave Kleikamp 		}
3529281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3530281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
3531281b5995STheodore Ts'o 	}
3532281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
3533281b5995STheodore Ts'o 
3534ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
3535b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3536b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
3537ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
3538ac27a0ecSDave Kleikamp 		goto failed_mount;
3539ac27a0ecSDave Kleikamp 	}
3540ac27a0ecSDave Kleikamp 
3541960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
3542960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
3543960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
3544960fd856STheodore Ts'o 
3545bf43d84bSEric Sandeen 	/*
3546bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3547bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3548bf43d84bSEric Sandeen 	 */
35495a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
355030ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
35515a9ae68aSDarrick J. Wong 	if (err) {
3552b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3553bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3554ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
355590c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
3556ac27a0ecSDave Kleikamp 		goto failed_mount;
3557ac27a0ecSDave Kleikamp 	}
3558ac27a0ecSDave Kleikamp 
3559617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3560617ba13bSMingming Cao 		goto cantfind_ext4;
3561e7c95593SEric Sandeen 
35620f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
35630f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
35640f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3565b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3566b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
35670f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
35680f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
35690f2ddca6SFrom: Thiemo Nagel 	}
35700f2ddca6SFrom: Thiemo Nagel 
35714ec11028STheodore Ts'o 	/*
35724ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
35734ec11028STheodore Ts'o 	 * of the filesystem.
35744ec11028STheodore Ts'o 	 */
35754ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3576b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
3577b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3578e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
35794ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3580e7c95593SEric Sandeen 		goto failed_mount;
3581e7c95593SEric Sandeen 	}
3582bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3583bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3584bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3585bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
35864ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3587b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
35884ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3589b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
35904ec11028STheodore Ts'o 		       ext4_blocks_count(es),
35914ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
35924ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
35934ec11028STheodore Ts'o 		goto failed_mount;
35944ec11028STheodore Ts'o 	}
3595bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3596fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3597fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3598617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3599617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3600f18a5f21STheodore Ts'o 	sbi->s_group_desc = ext4_kvmalloc(db_count *
3601f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
3602ac27a0ecSDave Kleikamp 					  GFP_KERNEL);
3603ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3604b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
36052cde417dSTheodore Ts'o 		ret = -ENOMEM;
3606ac27a0ecSDave Kleikamp 		goto failed_mount;
3607ac27a0ecSDave Kleikamp 	}
3608ac27a0ecSDave Kleikamp 
3609705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3610ac27a0ecSDave Kleikamp 
3611ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
361270bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3613a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
3614ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3615b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3616b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3617ac27a0ecSDave Kleikamp 			db_count = i;
3618ac27a0ecSDave Kleikamp 			goto failed_mount2;
3619ac27a0ecSDave Kleikamp 		}
3620ac27a0ecSDave Kleikamp 	}
3621bfff6873SLukas Czerner 	if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
3622b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
36236a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
3624f9ae9cf5STheodore Ts'o 		goto failed_mount2;
3625ac27a0ecSDave Kleikamp 	}
3626772cb7c8SJose R. Santos 
3627f9ae9cf5STheodore Ts'o 	sbi->s_gdb_count = db_count;
3628ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3629ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3630ac27a0ecSDave Kleikamp 
363104ecddb7SJan Mrazek 	setup_timer(&sbi->s_err_report, print_daily_error_info,
363204ecddb7SJan Mrazek 		(unsigned long) sb);
363304496411STao Ma 
3634a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
3635eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
3636ce7e010aSTheodore Ts'o 		goto failed_mount3;
3637ce7e010aSTheodore Ts'o 
3638c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
363967a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
3640c9de560dSAlex Tomas 
3641f9ae9cf5STheodore Ts'o 	/*
3642f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
3643f9ae9cf5STheodore Ts'o 	 */
3644f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
3645617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3646617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3647ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3648617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
3649e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
36501fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
3651262b4662SJan Kara 	else
3652262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
365396c7e0d9SJan Kara 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
3654ac27a0ecSDave Kleikamp #endif
3655f2fa2ffcSAneesh Kumar K.V 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3656f2fa2ffcSAneesh Kumar K.V 
3657ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
36583b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
3659ac27a0ecSDave Kleikamp 
3660ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3661ac27a0ecSDave Kleikamp 
3662ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3663e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
3664ac27a0ecSDave Kleikamp 
3665e2b911c5SDarrick J. Wong 	if (ext4_has_feature_mmp(sb) && !(sb->s_flags & MS_RDONLY))
3666c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
366750460fe8SDarrick J. Wong 			goto failed_mount3a;
3668c5e06d10SJohann Lombardi 
3669ac27a0ecSDave Kleikamp 	/*
3670ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3671ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3672ac27a0ecSDave Kleikamp 	 */
3673e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
3674617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
367550460fe8SDarrick J. Wong 			goto failed_mount3a;
36760390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
3677e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
3678b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3679b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3680744692dcSJiaying Zhang 		goto failed_mount_wq;
3681ac27a0ecSDave Kleikamp 	} else {
3682*1e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
3683*1e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
3684*1e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
3685*1e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
3686*1e381f60SDmitry Monakhov 			goto failed_mount_wq;
3687*1e381f60SDmitry Monakhov 		}
3688*1e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3689*1e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
3690*1e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
3691*1e381f60SDmitry Monakhov 			goto failed_mount_wq;
3692*1e381f60SDmitry Monakhov 		}
3693*1e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
3694*1e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
3695*1e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
3696*1e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
3697*1e381f60SDmitry Monakhov 			goto failed_mount_wq;
3698*1e381f60SDmitry Monakhov 		}
3699*1e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
3700*1e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
3701*1e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
3702*1e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
3703*1e381f60SDmitry Monakhov 			goto failed_mount_wq;
3704*1e381f60SDmitry Monakhov 		}
3705*1e381f60SDmitry Monakhov 		sbi->s_def_mount_opt &= EXT4_MOUNT_JOURNAL_CHECKSUM;
3706*1e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
3707fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
37080390131bSFrank Mayhar 		sbi->s_journal = NULL;
37090390131bSFrank Mayhar 		needs_recovery = 0;
37100390131bSFrank Mayhar 		goto no_journal;
3711ac27a0ecSDave Kleikamp 	}
3712ac27a0ecSDave Kleikamp 
3713e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
3714eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3715eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3716b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3717744692dcSJiaying Zhang 		goto failed_mount_wq;
3718eb40a09cSJose R. Santos 	}
3719eb40a09cSJose R. Santos 
372025ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
372125ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
372225ed6e8aSDarrick J. Wong 			 "feature set");
372325ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
3724d4da6c9cSLinus Torvalds 	}
3725818d276cSGirish Shilamkar 
3726ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3727ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3728ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3729ac27a0ecSDave Kleikamp 	case 0:
3730ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
373163f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
373263f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
373363f57933SAndrew Morton 		 */
3734dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3735dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3736fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3737ac27a0ecSDave Kleikamp 		else
3738fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3739ac27a0ecSDave Kleikamp 		break;
3740ac27a0ecSDave Kleikamp 
3741617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
3742617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
3743dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
3744dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
3745b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
3746b31e1552SEric Sandeen 			       "requested data journaling mode");
3747744692dcSJiaying Zhang 			goto failed_mount_wq;
3748ac27a0ecSDave Kleikamp 		}
3749ac27a0ecSDave Kleikamp 	default:
3750ac27a0ecSDave Kleikamp 		break;
3751ac27a0ecSDave Kleikamp 	}
3752b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3753ac27a0ecSDave Kleikamp 
375418aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
375518aadd47SBobi Jam 
3756ce7e010aSTheodore Ts'o no_journal:
37579c191f70ST Makphaibulchoke 	if (ext4_mballoc_ready) {
37589c191f70ST Makphaibulchoke 		sbi->s_mb_cache = ext4_xattr_create_cache(sb->s_id);
37599c191f70ST Makphaibulchoke 		if (!sbi->s_mb_cache) {
37609c191f70ST Makphaibulchoke 			ext4_msg(sb, KERN_ERR, "Failed to create an mb_cache");
37619c191f70ST Makphaibulchoke 			goto failed_mount_wq;
37629c191f70ST Makphaibulchoke 		}
37639c191f70ST Makphaibulchoke 	}
37649c191f70ST Makphaibulchoke 
3765e2b911c5SDarrick J. Wong 	if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
37661cb767cdSTheodore Ts'o 	    (blocksize != PAGE_CACHE_SIZE)) {
37671cb767cdSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
37681cb767cdSTheodore Ts'o 			 "Unsupported blocksize for fs encryption");
37691cb767cdSTheodore Ts'o 		goto failed_mount_wq;
37701cb767cdSTheodore Ts'o 	}
37711cb767cdSTheodore Ts'o 
3772e2b911c5SDarrick J. Wong 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !(sb->s_flags & MS_RDONLY) &&
3773e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
3774e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
37756ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
37766ddb2447STheodore Ts'o 	}
37776ddb2447STheodore Ts'o 
3778fd89d5f2STejun Heo 	/*
3779952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
3780952fc18eSTheodore Ts'o 	 * superblock if present.
3781952fc18eSTheodore Ts'o 	 */
3782952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
3783952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
3784952fc18eSTheodore Ts'o 	else {
378507aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
378607aa2ea1SLukas Czerner 		if (err)
3787952fc18eSTheodore Ts'o 			goto failed_mount_wq;
3788952fc18eSTheodore Ts'o 	}
3789952fc18eSTheodore Ts'o 
3790952fc18eSTheodore Ts'o 	/*
3791fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
3792fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
3793fd89d5f2STejun Heo 	 */
37942e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
37952e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
37962e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
37972e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
379807aa2ea1SLukas Czerner 		ret = -ENOMEM;
37992e8fa54eSJan Kara 		goto failed_mount4;
38002e8fa54eSJan Kara 	}
38012e8fa54eSJan Kara 
3802ac27a0ecSDave Kleikamp 	/*
3803dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
3804ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
3805ac27a0ecSDave Kleikamp 	 */
3806ac27a0ecSDave Kleikamp 
38071d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
38081d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
3809b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
38101d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
381132a9bb57SManish Katiyar 		root = NULL;
3812ac27a0ecSDave Kleikamp 		goto failed_mount4;
3813ac27a0ecSDave Kleikamp 	}
3814ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
3815b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
381694bf608aSAl Viro 		iput(root);
3817ac27a0ecSDave Kleikamp 		goto failed_mount4;
3818ac27a0ecSDave Kleikamp 	}
381948fde701SAl Viro 	sb->s_root = d_make_root(root);
38201d1fe1eeSDavid Howells 	if (!sb->s_root) {
3821b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
38221d1fe1eeSDavid Howells 		ret = -ENOMEM;
38231d1fe1eeSDavid Howells 		goto failed_mount4;
38241d1fe1eeSDavid Howells 	}
3825ac27a0ecSDave Kleikamp 
38267e84b621SEric Sandeen 	if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
38277e84b621SEric Sandeen 		sb->s_flags |= MS_RDONLY;
3828ef7f3835SKalpak Shah 
3829ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
3830ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3831ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3832ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
3833e2b911c5SDarrick J. Wong 		if (ext4_has_feature_extra_isize(sb)) {
3834ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3835ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
3836ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3837ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
3838ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3839ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
3840ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3841ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
3842ef7f3835SKalpak Shah 		}
3843ef7f3835SKalpak Shah 	}
3844ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
3845ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3846ef7f3835SKalpak Shah 							sbi->s_inode_size) {
3847ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3848ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
3849b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
3850b31e1552SEric Sandeen 			 "available");
3851ef7f3835SKalpak Shah 	}
3852ef7f3835SKalpak Shah 
3853b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
385427dd4385SLukas Czerner 
38556fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
38566fd058f7STheodore Ts'o 	if (err) {
3857b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
3858fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
3859f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
3860f9ae9cf5STheodore Ts'o 	}
3861f9ae9cf5STheodore Ts'o 
3862f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
3863f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
3864f9ae9cf5STheodore Ts'o 	if (err) {
3865f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
3866f9ae9cf5STheodore Ts'o 			 err);
3867dcf2d804STao Ma 		goto failed_mount5;
3868c2774d84SAneesh Kumar K.V 	}
3869c2774d84SAneesh Kumar K.V 
3870d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
3871d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
3872d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
3873908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
3874908c7f19STejun Heo 				  GFP_KERNEL);
3875d5e03cbbSTheodore Ts'o 	if (!err) {
3876d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
3877d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
3878908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
3879908c7f19STejun Heo 					  GFP_KERNEL);
3880d5e03cbbSTheodore Ts'o 	}
3881d5e03cbbSTheodore Ts'o 	if (!err)
3882d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
3883908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
3884d5e03cbbSTheodore Ts'o 	if (!err)
3885908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
3886908c7f19STejun Heo 					  GFP_KERNEL);
3887d5e03cbbSTheodore Ts'o 	if (err) {
3888d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
3889d5e03cbbSTheodore Ts'o 		goto failed_mount6;
3890d5e03cbbSTheodore Ts'o 	}
3891d5e03cbbSTheodore Ts'o 
3892e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
3893d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
3894d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3895d5e03cbbSTheodore Ts'o 			       "unable to initialize "
3896d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
3897d5e03cbbSTheodore Ts'o 			goto failed_mount6;
3898d5e03cbbSTheodore Ts'o 		}
3899d5e03cbbSTheodore Ts'o 
3900bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
3901bfff6873SLukas Czerner 	if (err)
3902dcf2d804STao Ma 		goto failed_mount6;
3903bfff6873SLukas Czerner 
3904b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
3905dcf2d804STao Ma 	if (err)
3906dcf2d804STao Ma 		goto failed_mount7;
39073197ebdbSTheodore Ts'o 
39089b2ff357SJan Kara #ifdef CONFIG_QUOTA
39099b2ff357SJan Kara 	/* Enable quota usage during mount. */
3910e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !(sb->s_flags & MS_RDONLY)) {
39119b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
39129b2ff357SJan Kara 		if (err)
39139b2ff357SJan Kara 			goto failed_mount8;
39149b2ff357SJan Kara 	}
39159b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
39169b2ff357SJan Kara 
3917617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3918617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
3919617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
39200390131bSFrank Mayhar 	if (needs_recovery) {
3921b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
3922617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
39230390131bSFrank Mayhar 	}
39240390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
39250390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
39260390131bSFrank Mayhar 			descr = " journalled data mode";
39270390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
39280390131bSFrank Mayhar 			descr = " ordered data mode";
39290390131bSFrank Mayhar 		else
39300390131bSFrank Mayhar 			descr = " writeback data mode";
39310390131bSFrank Mayhar 	} else
39320390131bSFrank Mayhar 		descr = "out journal";
39330390131bSFrank Mayhar 
393479add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
393579add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
393679add3a3SLukas Czerner 		if (!blk_queue_discard(q))
393779add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
393879add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
393979add3a3SLukas Czerner 				 "the device does not support discard");
394079add3a3SLukas Czerner 	}
394179add3a3SLukas Czerner 
3942e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
3943d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
39448b67f04aSTheodore Ts'o 			 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
39458b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
3946ac27a0ecSDave Kleikamp 
394766e61a9eSTheodore Ts'o 	if (es->s_error_count)
394866e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
3949ac27a0ecSDave Kleikamp 
3950efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
3951efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
3952efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
3953efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
3954efbed4dcSTheodore Ts'o 
3955d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
3956ac27a0ecSDave Kleikamp 	return 0;
3957ac27a0ecSDave Kleikamp 
3958617ba13bSMingming Cao cantfind_ext4:
3959ac27a0ecSDave Kleikamp 	if (!silent)
3960b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
3961ac27a0ecSDave Kleikamp 	goto failed_mount;
3962ac27a0ecSDave Kleikamp 
396372ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
396472ba7450STheodore Ts'o failed_mount8:
3965ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
396672ba7450STheodore Ts'o #endif
3967dcf2d804STao Ma failed_mount7:
3968dcf2d804STao Ma 	ext4_unregister_li_request(sb);
3969dcf2d804STao Ma failed_mount6:
3970f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
3971d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
3972b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
3973d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
3974d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
3975d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
3976d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
397700764937SAzat Khuzhin failed_mount5:
3978f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
3979f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
3980f9ae9cf5STheodore Ts'o failed_mount4a:
398194bf608aSAl Viro 	dput(sb->s_root);
398232a9bb57SManish Katiyar 	sb->s_root = NULL;
398394bf608aSAl Viro failed_mount4:
3984b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
39852e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
39862e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
39874c0425ffSMingming Cao failed_mount_wq:
39880390131bSFrank Mayhar 	if (sbi->s_journal) {
3989dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
399047b4a50bSJan Kara 		sbi->s_journal = NULL;
39910390131bSFrank Mayhar 	}
399250460fe8SDarrick J. Wong failed_mount3a:
3993d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
3994eb68d0e2SZheng Liu failed_mount3:
39959105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
3996c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
3997c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
3998ac27a0ecSDave Kleikamp failed_mount2:
3999ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4000ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4001b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4002ac27a0ecSDave Kleikamp failed_mount:
40030441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
40040441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4005ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4006a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
4007ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4008ac27a0ecSDave Kleikamp #endif
4009617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4010ac27a0ecSDave Kleikamp 	brelse(bh);
4011ac27a0ecSDave Kleikamp out_fail:
4012ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4013f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
4014ac27a0ecSDave Kleikamp 	kfree(sbi);
4015dcc7dae3SCyrill Gorcunov out_free_orig:
4016d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
401707aa2ea1SLukas Czerner 	return err ? err : ret;
4018ac27a0ecSDave Kleikamp }
4019ac27a0ecSDave Kleikamp 
4020ac27a0ecSDave Kleikamp /*
4021ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4022ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4023ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4024ac27a0ecSDave Kleikamp  */
4025617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4026ac27a0ecSDave Kleikamp {
4027617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4028ac27a0ecSDave Kleikamp 
4029ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
403030773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
403130773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4032ac27a0ecSDave Kleikamp 
4033a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4034ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4035dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4036ac27a0ecSDave Kleikamp 	else
4037dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
40385bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
40395bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
40405bf5683aSHidehiro Kawai 	else
40415bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4042a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4043ac27a0ecSDave Kleikamp }
4044ac27a0ecSDave Kleikamp 
4045617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
4046ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
4047ac27a0ecSDave Kleikamp {
4048ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4049ac27a0ecSDave Kleikamp 	journal_t *journal;
4050ac27a0ecSDave Kleikamp 
4051e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
40520390131bSFrank Mayhar 
4053ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
4054ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
4055ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
4056ac27a0ecSDave Kleikamp 
40571d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
40581d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4059b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4060ac27a0ecSDave Kleikamp 		return NULL;
4061ac27a0ecSDave Kleikamp 	}
4062ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4063ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4064ac27a0ecSDave Kleikamp 		iput(journal_inode);
4065b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4066ac27a0ecSDave Kleikamp 		return NULL;
4067ac27a0ecSDave Kleikamp 	}
4068ac27a0ecSDave Kleikamp 
4069e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4070ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
40711d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4072b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4073ac27a0ecSDave Kleikamp 		iput(journal_inode);
4074ac27a0ecSDave Kleikamp 		return NULL;
4075ac27a0ecSDave Kleikamp 	}
4076ac27a0ecSDave Kleikamp 
4077dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4078ac27a0ecSDave Kleikamp 	if (!journal) {
4079b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4080ac27a0ecSDave Kleikamp 		iput(journal_inode);
4081ac27a0ecSDave Kleikamp 		return NULL;
4082ac27a0ecSDave Kleikamp 	}
4083ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4084617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4085ac27a0ecSDave Kleikamp 	return journal;
4086ac27a0ecSDave Kleikamp }
4087ac27a0ecSDave Kleikamp 
4088617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4089ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4090ac27a0ecSDave Kleikamp {
4091ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4092ac27a0ecSDave Kleikamp 	journal_t *journal;
4093617ba13bSMingming Cao 	ext4_fsblk_t start;
4094617ba13bSMingming Cao 	ext4_fsblk_t len;
4095ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4096617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4097ac27a0ecSDave Kleikamp 	unsigned long offset;
4098617ba13bSMingming Cao 	struct ext4_super_block *es;
4099ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4100ac27a0ecSDave Kleikamp 
4101e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
41020390131bSFrank Mayhar 
4103b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4104ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4105ac27a0ecSDave Kleikamp 		return NULL;
4106ac27a0ecSDave Kleikamp 
4107ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4108e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4109ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4110b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4111b31e1552SEric Sandeen 			"blocksize too small for journal device");
4112ac27a0ecSDave Kleikamp 		goto out_bdev;
4113ac27a0ecSDave Kleikamp 	}
4114ac27a0ecSDave Kleikamp 
4115617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4116617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4117ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4118ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4119b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4120b31e1552SEric Sandeen 		       "external journal");
4121ac27a0ecSDave Kleikamp 		goto out_bdev;
4122ac27a0ecSDave Kleikamp 	}
4123ac27a0ecSDave Kleikamp 
41242716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4125617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4126ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4127617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4128b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4129b31e1552SEric Sandeen 					"bad superblock");
4130ac27a0ecSDave Kleikamp 		brelse(bh);
4131ac27a0ecSDave Kleikamp 		goto out_bdev;
4132ac27a0ecSDave Kleikamp 	}
4133ac27a0ecSDave Kleikamp 
4134df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4135df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4136df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4137df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4138df4763beSDarrick J. Wong 				       "corrupt superblock");
4139df4763beSDarrick J. Wong 		brelse(bh);
4140df4763beSDarrick J. Wong 		goto out_bdev;
4141df4763beSDarrick J. Wong 	}
4142df4763beSDarrick J. Wong 
4143617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4144b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4145ac27a0ecSDave Kleikamp 		brelse(bh);
4146ac27a0ecSDave Kleikamp 		goto out_bdev;
4147ac27a0ecSDave Kleikamp 	}
4148ac27a0ecSDave Kleikamp 
4149bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4150ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4151ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4152ac27a0ecSDave Kleikamp 
4153dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4154ac27a0ecSDave Kleikamp 					start, len, blocksize);
4155ac27a0ecSDave Kleikamp 	if (!journal) {
4156b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4157ac27a0ecSDave Kleikamp 		goto out_bdev;
4158ac27a0ecSDave Kleikamp 	}
4159ac27a0ecSDave Kleikamp 	journal->j_private = sb;
41609f203507STheodore Ts'o 	ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4161ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4162ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4163b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4164ac27a0ecSDave Kleikamp 		goto out_journal;
4165ac27a0ecSDave Kleikamp 	}
4166ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4167b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4168b31e1552SEric Sandeen 					"user (unsupported) - %d",
4169ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4170ac27a0ecSDave Kleikamp 		goto out_journal;
4171ac27a0ecSDave Kleikamp 	}
4172617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4173617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4174ac27a0ecSDave Kleikamp 	return journal;
41750b8e58a1SAndreas Dilger 
4176ac27a0ecSDave Kleikamp out_journal:
4177dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4178ac27a0ecSDave Kleikamp out_bdev:
4179617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4180ac27a0ecSDave Kleikamp 	return NULL;
4181ac27a0ecSDave Kleikamp }
4182ac27a0ecSDave Kleikamp 
4183617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4184617ba13bSMingming Cao 			     struct ext4_super_block *es,
4185ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4186ac27a0ecSDave Kleikamp {
4187ac27a0ecSDave Kleikamp 	journal_t *journal;
4188ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4189ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4190ac27a0ecSDave Kleikamp 	int err = 0;
4191ac27a0ecSDave Kleikamp 	int really_read_only;
4192ac27a0ecSDave Kleikamp 
4193e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
41940390131bSFrank Mayhar 
4195ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4196ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4197b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4198b31e1552SEric Sandeen 			"numbers have changed");
4199ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4200ac27a0ecSDave Kleikamp 	} else
4201ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4202ac27a0ecSDave Kleikamp 
4203ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4204ac27a0ecSDave Kleikamp 
4205ac27a0ecSDave Kleikamp 	/*
4206ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4207ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4208ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4209ac27a0ecSDave Kleikamp 	 */
4210e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4211ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
4212b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4213b31e1552SEric Sandeen 					"required on readonly filesystem");
4214ac27a0ecSDave Kleikamp 			if (really_read_only) {
4215b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4216b31e1552SEric Sandeen 					"unavailable, cannot proceed");
4217ac27a0ecSDave Kleikamp 				return -EROFS;
4218ac27a0ecSDave Kleikamp 			}
4219b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4220b31e1552SEric Sandeen 			       "be enabled during recovery");
4221ac27a0ecSDave Kleikamp 		}
4222ac27a0ecSDave Kleikamp 	}
4223ac27a0ecSDave Kleikamp 
4224ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4225b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4226b31e1552SEric Sandeen 		       "and inode journals!");
4227ac27a0ecSDave Kleikamp 		return -EINVAL;
4228ac27a0ecSDave Kleikamp 	}
4229ac27a0ecSDave Kleikamp 
4230ac27a0ecSDave Kleikamp 	if (journal_inum) {
4231617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4232ac27a0ecSDave Kleikamp 			return -EINVAL;
4233ac27a0ecSDave Kleikamp 	} else {
4234617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4235ac27a0ecSDave Kleikamp 			return -EINVAL;
4236ac27a0ecSDave Kleikamp 	}
4237ac27a0ecSDave Kleikamp 
423890576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4239b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
42404776004fSTheodore Ts'o 
4241e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
4242dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
42431c13d5c0STheodore Ts'o 	if (!err) {
42441c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
42451c13d5c0STheodore Ts'o 		if (save)
42461c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
42471c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4248dab291afSMingming Cao 		err = jbd2_journal_load(journal);
42491c13d5c0STheodore Ts'o 		if (save)
42501c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
42511c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
42521c13d5c0STheodore Ts'o 		kfree(save);
42531c13d5c0STheodore Ts'o 	}
4254ac27a0ecSDave Kleikamp 
4255ac27a0ecSDave Kleikamp 	if (err) {
4256b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4257dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4258ac27a0ecSDave Kleikamp 		return err;
4259ac27a0ecSDave Kleikamp 	}
4260ac27a0ecSDave Kleikamp 
4261617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4262617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4263ac27a0ecSDave Kleikamp 
4264c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4265ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4266ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4267ac27a0ecSDave Kleikamp 
4268ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4269e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4270ac27a0ecSDave Kleikamp 	}
4271ac27a0ecSDave Kleikamp 
4272ac27a0ecSDave Kleikamp 	return 0;
4273ac27a0ecSDave Kleikamp }
4274ac27a0ecSDave Kleikamp 
4275e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4276ac27a0ecSDave Kleikamp {
4277e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4278617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4279c4be0c1dSTakashi Sato 	int error = 0;
4280ac27a0ecSDave Kleikamp 
4281bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4282c4be0c1dSTakashi Sato 		return error;
4283914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
4284914258bfSTheodore Ts'o 		/*
4285914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
4286914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
4287914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
4288914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
4289914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
4290914258bfSTheodore Ts'o 		 * write and hope for the best.
4291914258bfSTheodore Ts'o 		 */
4292b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
4293b31e1552SEric Sandeen 		       "superblock detected");
4294914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
4295914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
4296914258bfSTheodore Ts'o 	}
429771290b36STheodore Ts'o 	/*
429871290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
429971290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
430071290b36STheodore Ts'o 	 * write time when we are mounting the root file system
430171290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
430271290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
430371290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
430471290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
430571290b36STheodore Ts'o 	 * to complain and force a full file system check.
430671290b36STheodore Ts'o 	 */
430771290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4308ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4309f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4310afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4311afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4312afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4313afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4314f613dfcbSTheodore Ts'o 	else
4315f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4316f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
4317d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
431857042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
431957042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
432057042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
4321d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
43227f93cff9STheodore Ts'o 		es->s_free_inodes_count =
43237f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
43245d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
4325ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
432606db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
4327ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4328914258bfSTheodore Ts'o 	if (sync) {
4329564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
4330564bc402SDaeho Jeong 			test_opt(sb, BARRIER) ? WRITE_FUA : WRITE_SYNC);
4331c4be0c1dSTakashi Sato 		if (error)
4332c4be0c1dSTakashi Sato 			return error;
4333c4be0c1dSTakashi Sato 
4334c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4335c4be0c1dSTakashi Sato 		if (error) {
4336b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4337b31e1552SEric Sandeen 			       "superblock");
4338914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4339914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4340914258bfSTheodore Ts'o 		}
4341914258bfSTheodore Ts'o 	}
4342c4be0c1dSTakashi Sato 	return error;
4343ac27a0ecSDave Kleikamp }
4344ac27a0ecSDave Kleikamp 
4345ac27a0ecSDave Kleikamp /*
4346ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4347ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4348ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4349ac27a0ecSDave Kleikamp  */
4350617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4351617ba13bSMingming Cao 					struct ext4_super_block *es)
4352ac27a0ecSDave Kleikamp {
4353617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4354ac27a0ecSDave Kleikamp 
4355e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
43560390131bSFrank Mayhar 		BUG_ON(journal != NULL);
43570390131bSFrank Mayhar 		return;
43580390131bSFrank Mayhar 	}
4359dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
43607ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
43617ffe1ea8SHidehiro Kawai 		goto out;
43627ffe1ea8SHidehiro Kawai 
4363e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb) &&
4364ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4365e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
4366e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4367ac27a0ecSDave Kleikamp 	}
43687ffe1ea8SHidehiro Kawai 
43697ffe1ea8SHidehiro Kawai out:
4370dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4371ac27a0ecSDave Kleikamp }
4372ac27a0ecSDave Kleikamp 
4373ac27a0ecSDave Kleikamp /*
4374ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4375ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4376ac27a0ecSDave Kleikamp  * main filesystem now.
4377ac27a0ecSDave Kleikamp  */
4378617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4379617ba13bSMingming Cao 				   struct ext4_super_block *es)
4380ac27a0ecSDave Kleikamp {
4381ac27a0ecSDave Kleikamp 	journal_t *journal;
4382ac27a0ecSDave Kleikamp 	int j_errno;
4383ac27a0ecSDave Kleikamp 	const char *errstr;
4384ac27a0ecSDave Kleikamp 
4385e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
43860390131bSFrank Mayhar 
4387617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4388ac27a0ecSDave Kleikamp 
4389ac27a0ecSDave Kleikamp 	/*
4390ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4391617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4392ac27a0ecSDave Kleikamp 	 */
4393ac27a0ecSDave Kleikamp 
4394dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4395ac27a0ecSDave Kleikamp 	if (j_errno) {
4396ac27a0ecSDave Kleikamp 		char nbuf[16];
4397ac27a0ecSDave Kleikamp 
4398617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
439912062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4400ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
440112062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4402ac27a0ecSDave Kleikamp 
4403617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4404617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4405e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4406ac27a0ecSDave Kleikamp 
4407dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4408d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
4409ac27a0ecSDave Kleikamp 	}
4410ac27a0ecSDave Kleikamp }
4411ac27a0ecSDave Kleikamp 
4412ac27a0ecSDave Kleikamp /*
4413ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4414ac27a0ecSDave Kleikamp  * and wait on the commit.
4415ac27a0ecSDave Kleikamp  */
4416617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4417ac27a0ecSDave Kleikamp {
4418ac27a0ecSDave Kleikamp 	journal_t *journal;
4419ac27a0ecSDave Kleikamp 
4420ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4421ac27a0ecSDave Kleikamp 		return 0;
4422ac27a0ecSDave Kleikamp 
4423617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4424b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
4425ac27a0ecSDave Kleikamp }
4426ac27a0ecSDave Kleikamp 
4427617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4428ac27a0ecSDave Kleikamp {
442914ce0cb4STheodore Ts'o 	int ret = 0;
44309eddacf9SJan Kara 	tid_t target;
443106a407f1SDmitry Monakhov 	bool needs_barrier = false;
44328d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4433ac27a0ecSDave Kleikamp 
44349bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
44352e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
4436a1177825SJan Kara 	/*
4437a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
4438a1177825SJan Kara 	 * no dirty dquots
4439a1177825SJan Kara 	 */
4440a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
444106a407f1SDmitry Monakhov 	/*
444206a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
444306a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
444406a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
444506a407f1SDmitry Monakhov 	 */
4446bda32530STheodore Ts'o 	if (sbi->s_journal) {
444706a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
444806a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
444906a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
445006a407f1SDmitry Monakhov 			needs_barrier = true;
445106a407f1SDmitry Monakhov 
44528d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4453ac27a0ecSDave Kleikamp 			if (wait)
4454bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
4455bda32530STheodore Ts'o 							   target);
44560390131bSFrank Mayhar 		}
4457bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
4458bda32530STheodore Ts'o 		needs_barrier = true;
445906a407f1SDmitry Monakhov 	if (needs_barrier) {
446006a407f1SDmitry Monakhov 		int err;
446106a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
446206a407f1SDmitry Monakhov 		if (!ret)
446306a407f1SDmitry Monakhov 			ret = err;
446406a407f1SDmitry Monakhov 	}
446506a407f1SDmitry Monakhov 
446606a407f1SDmitry Monakhov 	return ret;
446706a407f1SDmitry Monakhov }
446806a407f1SDmitry Monakhov 
4469ac27a0ecSDave Kleikamp /*
4470ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4471ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4472be4f27d3SYongqiang Yang  *
4473be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
44748e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
44758e8ad8a5SJan Kara  * modifications.
4476ac27a0ecSDave Kleikamp  */
4477c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4478ac27a0ecSDave Kleikamp {
4479c4be0c1dSTakashi Sato 	int error = 0;
4480c4be0c1dSTakashi Sato 	journal_t *journal;
4481ac27a0ecSDave Kleikamp 
44829ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
44839ca92389STheodore Ts'o 		return 0;
44849ca92389STheodore Ts'o 
4485c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4486ac27a0ecSDave Kleikamp 
4487bb044576STheodore Ts'o 	if (journal) {
4488ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
4489dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
44907ffe1ea8SHidehiro Kawai 
44917ffe1ea8SHidehiro Kawai 		/*
4492bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
4493bb044576STheodore Ts'o 		 * flush the journal.
44947ffe1ea8SHidehiro Kawai 		 */
4495c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
44966b0310fbSEric Sandeen 		if (error < 0)
44976b0310fbSEric Sandeen 			goto out;
4498ac27a0ecSDave Kleikamp 
4499ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
4500e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
4501c642dc9eSEric Sandeen 	}
4502c642dc9eSEric Sandeen 
4503e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
45046b0310fbSEric Sandeen out:
4505bb044576STheodore Ts'o 	if (journal)
45068e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
4507bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
45086b0310fbSEric Sandeen 	return error;
4509ac27a0ecSDave Kleikamp }
4510ac27a0ecSDave Kleikamp 
4511ac27a0ecSDave Kleikamp /*
4512ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4513ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4514ac27a0ecSDave Kleikamp  */
4515c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4516ac27a0ecSDave Kleikamp {
45179ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
45189ca92389STheodore Ts'o 		return 0;
45199ca92389STheodore Ts'o 
4520c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
45219ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
4522e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
4523c642dc9eSEric Sandeen 	}
4524c642dc9eSEric Sandeen 
4525e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4526c4be0c1dSTakashi Sato 	return 0;
4527ac27a0ecSDave Kleikamp }
4528ac27a0ecSDave Kleikamp 
4529673c6100STheodore Ts'o /*
4530673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4531673c6100STheodore Ts'o  */
4532673c6100STheodore Ts'o struct ext4_mount_options {
4533673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4534a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
453508cefc7aSEric W. Biederman 	kuid_t s_resuid;
453608cefc7aSEric W. Biederman 	kgid_t s_resgid;
4537673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4538673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4539673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4540673c6100STheodore Ts'o 	int s_jquota_fmt;
4541a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
4542673c6100STheodore Ts'o #endif
4543673c6100STheodore Ts'o };
4544673c6100STheodore Ts'o 
4545617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4546ac27a0ecSDave Kleikamp {
4547617ba13bSMingming Cao 	struct ext4_super_block *es;
4548617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4549ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4550617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4551c79d967dSChristoph Hellwig 	int enable_quota = 0;
45528a266467STheodore Ts'o 	ext4_group_t g;
4553b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4554c5e06d10SJohann Lombardi 	int err = 0;
4555ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
455603dafb5fSChen Gang 	int i, j;
4557ac27a0ecSDave Kleikamp #endif
4558d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4559ac27a0ecSDave Kleikamp 
4560ac27a0ecSDave Kleikamp 	/* Store the original options */
4561ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4562ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4563a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4564ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4565ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4566ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
456730773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
456830773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4569ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4570ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4571a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
457203dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
457303dafb5fSChen Gang 			old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
457403dafb5fSChen Gang 							 GFP_KERNEL);
457503dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
457603dafb5fSChen Gang 				for (j = 0; j < i; j++)
457703dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
45783e36a163SWei Yongjun 				kfree(orig_data);
457903dafb5fSChen Gang 				return -ENOMEM;
458003dafb5fSChen Gang 			}
458103dafb5fSChen Gang 		} else
458203dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
4583ac27a0ecSDave Kleikamp #endif
4584b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4585b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4586ac27a0ecSDave Kleikamp 
4587661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
4588ac27a0ecSDave Kleikamp 		err = -EINVAL;
4589ac27a0ecSDave Kleikamp 		goto restore_opts;
4590ac27a0ecSDave Kleikamp 	}
4591ac27a0ecSDave Kleikamp 
45926b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
45936b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
45946b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
45952d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
45962d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
4597c6d3d56dSDarrick J. Wong 	}
4598c6d3d56dSDarrick J. Wong 
45996ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
46006ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
46016ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
46026ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
46036ae6514bSPiotr Sarna 			err = -EINVAL;
46046ae6514bSPiotr Sarna 			goto restore_opts;
46056ae6514bSPiotr Sarna 		}
46066ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
46076ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
46086ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
46096ae6514bSPiotr Sarna 			err = -EINVAL;
46106ae6514bSPiotr Sarna 			goto restore_opts;
46116ae6514bSPiotr Sarna 		}
4612923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
4613923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4614923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4615923ae0ffSRoss Zwisler 			err = -EINVAL;
4616923ae0ffSRoss Zwisler 			goto restore_opts;
4617923ae0ffSRoss Zwisler 		}
4618923ae0ffSRoss Zwisler 	}
4619923ae0ffSRoss Zwisler 
4620923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
4621923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
4622923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
4623923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
46246ae6514bSPiotr Sarna 	}
46256ae6514bSPiotr Sarna 
46264ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4627c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4628ac27a0ecSDave Kleikamp 
4629ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4630482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4631ac27a0ecSDave Kleikamp 
4632ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4633ac27a0ecSDave Kleikamp 
4634b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4635617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4636b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4637b3881f74STheodore Ts'o 	}
4638ac27a0ecSDave Kleikamp 
4639a2fd66d0STheodore Ts'o 	if (*flags & MS_LAZYTIME)
4640a2fd66d0STheodore Ts'o 		sb->s_flags |= MS_LAZYTIME;
4641a2fd66d0STheodore Ts'o 
4642661aa520SEric Sandeen 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
46434ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4644ac27a0ecSDave Kleikamp 			err = -EROFS;
4645ac27a0ecSDave Kleikamp 			goto restore_opts;
4646ac27a0ecSDave Kleikamp 		}
4647ac27a0ecSDave Kleikamp 
4648ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
464938c03b34STheodore Ts'o 			err = sync_filesystem(sb);
465038c03b34STheodore Ts'o 			if (err < 0)
465138c03b34STheodore Ts'o 				goto restore_opts;
46520f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
46530f0dd62fSChristoph Hellwig 			if (err < 0)
4654c79d967dSChristoph Hellwig 				goto restore_opts;
4655c79d967dSChristoph Hellwig 
4656ac27a0ecSDave Kleikamp 			/*
4657ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4658ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4659ac27a0ecSDave Kleikamp 			 */
4660ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4661ac27a0ecSDave Kleikamp 
4662ac27a0ecSDave Kleikamp 			/*
4663ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4664ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4665ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4666ac27a0ecSDave Kleikamp 			 */
4667617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4668617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4669ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4670ac27a0ecSDave Kleikamp 
4671a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4672617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4673ac27a0ecSDave Kleikamp 		} else {
4674a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4675e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
46762cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
4677ac27a0ecSDave Kleikamp 				err = -EROFS;
4678ac27a0ecSDave Kleikamp 				goto restore_opts;
4679ac27a0ecSDave Kleikamp 			}
4680ead6596bSEric Sandeen 			/*
46818a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
46820b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
46838a266467STheodore Ts'o 			 */
46848a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
46858a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
46868a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
46878a266467STheodore Ts'o 
4688feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
4689b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4690b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
4691e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
46928a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
46936a797d27SDarrick J. Wong 					err = -EFSBADCRC;
46948a266467STheodore Ts'o 					goto restore_opts;
46958a266467STheodore Ts'o 				}
46968a266467STheodore Ts'o 			}
46978a266467STheodore Ts'o 
46988a266467STheodore Ts'o 			/*
4699ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4700ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4701ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4702ead6596bSEric Sandeen 			 */
4703ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4704b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4705ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4706ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4707b31e1552SEric Sandeen 				       "umount/remount instead");
4708ead6596bSEric Sandeen 				err = -EINVAL;
4709ead6596bSEric Sandeen 				goto restore_opts;
4710ead6596bSEric Sandeen 			}
4711ead6596bSEric Sandeen 
4712ac27a0ecSDave Kleikamp 			/*
4713ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4714ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4715ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4716ac27a0ecSDave Kleikamp 			 * the partition.)
4717ac27a0ecSDave Kleikamp 			 */
47180390131bSFrank Mayhar 			if (sbi->s_journal)
4719617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4720ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4721617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4722ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4723e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
4724c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
4725c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
4726c5e06d10SJohann Lombardi 					err = -EROFS;
4727c5e06d10SJohann Lombardi 					goto restore_opts;
4728c5e06d10SJohann Lombardi 				}
4729c79d967dSChristoph Hellwig 			enable_quota = 1;
4730ac27a0ecSDave Kleikamp 		}
4731ac27a0ecSDave Kleikamp 	}
4732bfff6873SLukas Czerner 
4733bfff6873SLukas Czerner 	/*
4734bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4735bfff6873SLukas Czerner 	 * current settings
4736bfff6873SLukas Czerner 	 */
4737bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4738bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4739bfff6873SLukas Czerner 	else {
4740bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4741bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4742bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4743bfff6873SLukas Czerner 	}
4744bfff6873SLukas Czerner 
47456fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
4746d096ad0fSMichael Tokarev 	if (sbi->s_journal == NULL && !(old_sb_flags & MS_RDONLY))
4747e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
47480390131bSFrank Mayhar 
4749ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4750ac27a0ecSDave Kleikamp 	/* Release old quota file names */
4751a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
4752ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
47537c319d32SAditya Kali 	if (enable_quota) {
47547c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
47550f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
4756e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
47577c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
475807724f98STheodore Ts'o 			if (err)
47597c319d32SAditya Kali 				goto restore_opts;
47607c319d32SAditya Kali 		}
47617c319d32SAditya Kali 	}
47627c319d32SAditya Kali #endif
4763d4c402d9SCurt Wohlgemuth 
4764a26f4992STheodore Ts'o 	*flags = (*flags & ~MS_LAZYTIME) | (sb->s_flags & MS_LAZYTIME);
4765d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4766d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4767ac27a0ecSDave Kleikamp 	return 0;
47680b8e58a1SAndreas Dilger 
4769ac27a0ecSDave Kleikamp restore_opts:
4770ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
4771ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
4772a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
4773ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
4774ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
4775ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
477630773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
477730773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
4778ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4779ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
4780a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
4781ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4782ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
4783ac27a0ecSDave Kleikamp 	}
4784ac27a0ecSDave Kleikamp #endif
4785d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4786ac27a0ecSDave Kleikamp 	return err;
4787ac27a0ecSDave Kleikamp }
4788ac27a0ecSDave Kleikamp 
4789617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
4790ac27a0ecSDave Kleikamp {
4791ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
4792617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4793617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
479427dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
4795960cc398SPekka Enberg 	u64 fsid;
4796d02a9391SKazuya Mio 	s64 bfree;
479727dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
4798ac27a0ecSDave Kleikamp 
4799952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
4800952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
4801ac27a0ecSDave Kleikamp 
4802617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
4803ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
4804b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
480557042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
480657042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
4807d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
480857042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
480927dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
481027dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
481127dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
4812ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
4813ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
481452d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
4815617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
4816960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
4817960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
4818960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
4819960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
48200b8e58a1SAndreas Dilger 
4821ac27a0ecSDave Kleikamp 	return 0;
4822ac27a0ecSDave Kleikamp }
4823ac27a0ecSDave Kleikamp 
48240b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
48250b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
4826ac27a0ecSDave Kleikamp  * Process 1                         Process 2
4827617ba13bSMingming Cao  * ext4_create()                     quota_sync()
4828dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
4829871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
4830dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
4831ac27a0ecSDave Kleikamp  *
4832ac27a0ecSDave Kleikamp  */
4833ac27a0ecSDave Kleikamp 
4834ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4835ac27a0ecSDave Kleikamp 
4836ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
4837ac27a0ecSDave Kleikamp {
48384c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
4839ac27a0ecSDave Kleikamp }
4840ac27a0ecSDave Kleikamp 
4841617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
4842ac27a0ecSDave Kleikamp {
4843ac27a0ecSDave Kleikamp 	int ret, err;
4844ac27a0ecSDave Kleikamp 	handle_t *handle;
4845ac27a0ecSDave Kleikamp 	struct inode *inode;
4846ac27a0ecSDave Kleikamp 
4847ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
48489924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
4849617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
4850ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4851ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4852ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
4853617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4854ac27a0ecSDave Kleikamp 	if (!ret)
4855ac27a0ecSDave Kleikamp 		ret = err;
4856ac27a0ecSDave Kleikamp 	return ret;
4857ac27a0ecSDave Kleikamp }
4858ac27a0ecSDave Kleikamp 
4859617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
4860ac27a0ecSDave Kleikamp {
4861ac27a0ecSDave Kleikamp 	int ret, err;
4862ac27a0ecSDave Kleikamp 	handle_t *handle;
4863ac27a0ecSDave Kleikamp 
48649924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
4865617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
4866ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4867ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4868ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
4869617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4870ac27a0ecSDave Kleikamp 	if (!ret)
4871ac27a0ecSDave Kleikamp 		ret = err;
4872ac27a0ecSDave Kleikamp 	return ret;
4873ac27a0ecSDave Kleikamp }
4874ac27a0ecSDave Kleikamp 
4875617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
4876ac27a0ecSDave Kleikamp {
4877ac27a0ecSDave Kleikamp 	int ret, err;
4878ac27a0ecSDave Kleikamp 	handle_t *handle;
4879ac27a0ecSDave Kleikamp 
48809924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
4881617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
48829c3013e9SJan Kara 	if (IS_ERR(handle)) {
48839c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
48849c3013e9SJan Kara 		dquot_release(dquot);
4885ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
48869c3013e9SJan Kara 	}
4887ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
4888617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4889ac27a0ecSDave Kleikamp 	if (!ret)
4890ac27a0ecSDave Kleikamp 		ret = err;
4891ac27a0ecSDave Kleikamp 	return ret;
4892ac27a0ecSDave Kleikamp }
4893ac27a0ecSDave Kleikamp 
4894617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
4895ac27a0ecSDave Kleikamp {
4896262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
4897262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4898262b4662SJan Kara 
48992c8be6b2SJan Kara 	/* Are we journaling quotas? */
4900e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
4901262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
4902ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
4903617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
4904ac27a0ecSDave Kleikamp 	} else {
4905ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
4906ac27a0ecSDave Kleikamp 	}
4907ac27a0ecSDave Kleikamp }
4908ac27a0ecSDave Kleikamp 
4909617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
4910ac27a0ecSDave Kleikamp {
4911ac27a0ecSDave Kleikamp 	int ret, err;
4912ac27a0ecSDave Kleikamp 	handle_t *handle;
4913ac27a0ecSDave Kleikamp 
4914ac27a0ecSDave Kleikamp 	/* Data block + inode block */
49152b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
4916ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4917ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4918ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
4919617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4920ac27a0ecSDave Kleikamp 	if (!ret)
4921ac27a0ecSDave Kleikamp 		ret = err;
4922ac27a0ecSDave Kleikamp 	return ret;
4923ac27a0ecSDave Kleikamp }
4924ac27a0ecSDave Kleikamp 
4925ac27a0ecSDave Kleikamp /*
4926ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
4927ac27a0ecSDave Kleikamp  * the quota file and such...
4928ac27a0ecSDave Kleikamp  */
4929617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
4930ac27a0ecSDave Kleikamp {
4931287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
4932617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
4933ac27a0ecSDave Kleikamp }
4934ac27a0ecSDave Kleikamp 
4935ac27a0ecSDave Kleikamp /*
4936ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
4937ac27a0ecSDave Kleikamp  */
4938617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
4939f00c9e44SJan Kara 			 struct path *path)
4940ac27a0ecSDave Kleikamp {
4941ac27a0ecSDave Kleikamp 	int err;
4942ac27a0ecSDave Kleikamp 
4943ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
4944ac27a0ecSDave Kleikamp 		return -EINVAL;
49450623543bSJan Kara 
4946ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
4947d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
4948ac27a0ecSDave Kleikamp 		return -EXDEV;
49490623543bSJan Kara 	/* Journaling quota? */
49500623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
49512b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
4952f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
4953b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
4954b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
4955b31e1552SEric Sandeen 				"Journaled quota will not work");
49560623543bSJan Kara 	}
49570623543bSJan Kara 
49580623543bSJan Kara 	/*
49590623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
49600623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
49610623543bSJan Kara 	 */
49620390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
49632b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
49640623543bSJan Kara 		/*
49650623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
49660623543bSJan Kara 		 * otherwise be livelocked...
49670623543bSJan Kara 		 */
49680623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
49697ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
49700623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
4971f00c9e44SJan Kara 		if (err)
49727ffe1ea8SHidehiro Kawai 			return err;
49737ffe1ea8SHidehiro Kawai 	}
49740623543bSJan Kara 
4975f00c9e44SJan Kara 	return dquot_quota_on(sb, type, format_id, path);
4976ac27a0ecSDave Kleikamp }
4977ac27a0ecSDave Kleikamp 
49787c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
49797c319d32SAditya Kali 			     unsigned int flags)
49807c319d32SAditya Kali {
49817c319d32SAditya Kali 	int err;
49827c319d32SAditya Kali 	struct inode *qf_inode;
4983a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
49847c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
49857c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
49867c319d32SAditya Kali 	};
49877c319d32SAditya Kali 
4988e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
49897c319d32SAditya Kali 
49907c319d32SAditya Kali 	if (!qf_inums[type])
49917c319d32SAditya Kali 		return -EPERM;
49927c319d32SAditya Kali 
49937c319d32SAditya Kali 	qf_inode = ext4_iget(sb, qf_inums[type]);
49947c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
49957c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
49967c319d32SAditya Kali 		return PTR_ERR(qf_inode);
49977c319d32SAditya Kali 	}
49987c319d32SAditya Kali 
4999bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5000bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
50017c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
50027c319d32SAditya Kali 	iput(qf_inode);
50037c319d32SAditya Kali 
50047c319d32SAditya Kali 	return err;
50057c319d32SAditya Kali }
50067c319d32SAditya Kali 
50077c319d32SAditya Kali /* Enable usage tracking for all quota types. */
50087c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
50097c319d32SAditya Kali {
50107c319d32SAditya Kali 	int type, err = 0;
5011a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
50127c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
50137c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
50147c319d32SAditya Kali 	};
50157c319d32SAditya Kali 
50167c319d32SAditya Kali 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
5017a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
50187c319d32SAditya Kali 		if (qf_inums[type]) {
50197c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
50207c319d32SAditya Kali 						DQUOT_USAGE_ENABLED);
50217c319d32SAditya Kali 			if (err) {
50227c319d32SAditya Kali 				ext4_warning(sb,
502372ba7450STheodore Ts'o 					"Failed to enable quota tracking "
502472ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
502572ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
50267c319d32SAditya Kali 				return err;
50277c319d32SAditya Kali 			}
50287c319d32SAditya Kali 		}
50297c319d32SAditya Kali 	}
50307c319d32SAditya Kali 	return 0;
50317c319d32SAditya Kali }
50327c319d32SAditya Kali 
5033ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5034ca0e05e4SDmitry Monakhov {
503521f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
503621f97697SJan Kara 	handle_t *handle;
503721f97697SJan Kara 
503887009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
503987009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
504087009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5041ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5042ca0e05e4SDmitry Monakhov 
50430b268590SAmir Goldstein 	if (!inode)
50440b268590SAmir Goldstein 		goto out;
50450b268590SAmir Goldstein 
504621f97697SJan Kara 	/* Update modification times of quota files when userspace can
504721f97697SJan Kara 	 * start looking at them */
50489924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
504921f97697SJan Kara 	if (IS_ERR(handle))
505021f97697SJan Kara 		goto out;
505121f97697SJan Kara 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
505221f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
505321f97697SJan Kara 	ext4_journal_stop(handle);
505421f97697SJan Kara 
505521f97697SJan Kara out:
5056ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5057ca0e05e4SDmitry Monakhov }
5058ca0e05e4SDmitry Monakhov 
5059ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5060ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5061ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5062ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5063617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5064ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5065ac27a0ecSDave Kleikamp {
5066ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5067725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5068ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5069ac27a0ecSDave Kleikamp 	int tocopy;
5070ac27a0ecSDave Kleikamp 	size_t toread;
5071ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5072ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5073ac27a0ecSDave Kleikamp 
5074ac27a0ecSDave Kleikamp 	if (off > i_size)
5075ac27a0ecSDave Kleikamp 		return 0;
5076ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5077ac27a0ecSDave Kleikamp 		len = i_size-off;
5078ac27a0ecSDave Kleikamp 	toread = len;
5079ac27a0ecSDave Kleikamp 	while (toread > 0) {
5080ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5081ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
50821c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
50831c215028STheodore Ts'o 		if (IS_ERR(bh))
50841c215028STheodore Ts'o 			return PTR_ERR(bh);
5085ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5086ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5087ac27a0ecSDave Kleikamp 		else
5088ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5089ac27a0ecSDave Kleikamp 		brelse(bh);
5090ac27a0ecSDave Kleikamp 		offset = 0;
5091ac27a0ecSDave Kleikamp 		toread -= tocopy;
5092ac27a0ecSDave Kleikamp 		data += tocopy;
5093ac27a0ecSDave Kleikamp 		blk++;
5094ac27a0ecSDave Kleikamp 	}
5095ac27a0ecSDave Kleikamp 	return len;
5096ac27a0ecSDave Kleikamp }
5097ac27a0ecSDave Kleikamp 
5098ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5099ac27a0ecSDave Kleikamp  * enough credits) */
5100617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5101ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5102ac27a0ecSDave Kleikamp {
5103ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5104725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
51051c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
5106c5e298aeSTheodore Ts'o 	int retries = 0;
5107ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5108ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5109ac27a0ecSDave Kleikamp 
51100390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5111b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5112b31e1552SEric Sandeen 			" cancelled because transaction is not started",
51139c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
51149c3013e9SJan Kara 		return -EIO;
51159c3013e9SJan Kara 	}
511667eeb568SDmitry Monakhov 	/*
511767eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
511867eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
511967eeb568SDmitry Monakhov 	 */
512067eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
512167eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
512267eeb568SDmitry Monakhov 			" cancelled because not block aligned",
512367eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
512467eeb568SDmitry Monakhov 		return -EIO;
512567eeb568SDmitry Monakhov 	}
512667eeb568SDmitry Monakhov 
5127c5e298aeSTheodore Ts'o 	do {
5128c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
5129c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
5130c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
5131c5e298aeSTheodore Ts'o 	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
5132c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
51331c215028STheodore Ts'o 	if (IS_ERR(bh))
51341c215028STheodore Ts'o 		return PTR_ERR(bh);
5135ac27a0ecSDave Kleikamp 	if (!bh)
5136ac27a0ecSDave Kleikamp 		goto out;
51375d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
5138617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5139ac27a0ecSDave Kleikamp 	if (err) {
5140ac27a0ecSDave Kleikamp 		brelse(bh);
51411c215028STheodore Ts'o 		return err;
5142ac27a0ecSDave Kleikamp 	}
5143ac27a0ecSDave Kleikamp 	lock_buffer(bh);
514467eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5145ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5146ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
51470390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5148ac27a0ecSDave Kleikamp 	brelse(bh);
5149ac27a0ecSDave Kleikamp out:
515067eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
515167eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
5152617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
5153617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
515421f97697SJan Kara 	}
515567eeb568SDmitry Monakhov 	return len;
5156ac27a0ecSDave Kleikamp }
5157ac27a0ecSDave Kleikamp 
5158ac27a0ecSDave Kleikamp #endif
5159ac27a0ecSDave Kleikamp 
5160152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5161152a0836SAl Viro 		       const char *dev_name, void *data)
5162ac27a0ecSDave Kleikamp {
5163152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
5164ac27a0ecSDave Kleikamp }
5165ac27a0ecSDave Kleikamp 
5166c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
516724b58424STheodore Ts'o static inline void register_as_ext2(void)
516824b58424STheodore Ts'o {
516924b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
517024b58424STheodore Ts'o 	if (err)
517124b58424STheodore Ts'o 		printk(KERN_WARNING
517224b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
517324b58424STheodore Ts'o }
517424b58424STheodore Ts'o 
517524b58424STheodore Ts'o static inline void unregister_as_ext2(void)
517624b58424STheodore Ts'o {
517724b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
517824b58424STheodore Ts'o }
51792035e776STheodore Ts'o 
51802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
51812035e776STheodore Ts'o {
5182e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
51832035e776STheodore Ts'o 		return 0;
51842035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
51852035e776STheodore Ts'o 		return 1;
5186e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
51872035e776STheodore Ts'o 		return 0;
51882035e776STheodore Ts'o 	return 1;
51892035e776STheodore Ts'o }
519024b58424STheodore Ts'o #else
519124b58424STheodore Ts'o static inline void register_as_ext2(void) { }
519224b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
51932035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
519424b58424STheodore Ts'o #endif
519524b58424STheodore Ts'o 
519624b58424STheodore Ts'o static inline void register_as_ext3(void)
519724b58424STheodore Ts'o {
519824b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
519924b58424STheodore Ts'o 	if (err)
520024b58424STheodore Ts'o 		printk(KERN_WARNING
520124b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
520224b58424STheodore Ts'o }
520324b58424STheodore Ts'o 
520424b58424STheodore Ts'o static inline void unregister_as_ext3(void)
520524b58424STheodore Ts'o {
520624b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
520724b58424STheodore Ts'o }
52082035e776STheodore Ts'o 
52092035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
52102035e776STheodore Ts'o {
5211e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
52122035e776STheodore Ts'o 		return 0;
5213e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
52142035e776STheodore Ts'o 		return 0;
52152035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
52162035e776STheodore Ts'o 		return 1;
5217e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
52182035e776STheodore Ts'o 		return 0;
52192035e776STheodore Ts'o 	return 1;
52202035e776STheodore Ts'o }
522124b58424STheodore Ts'o 
522203010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
5223ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
522403010a33STheodore Ts'o 	.name		= "ext4",
5225152a0836SAl Viro 	.mount		= ext4_mount,
5226ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
5227ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
5228ac27a0ecSDave Kleikamp };
52297f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
5230ac27a0ecSDave Kleikamp 
5231e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
5232e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5233e9e3bcecSEric Sandeen struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
5234e9e3bcecSEric Sandeen 
52355dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
5236ac27a0ecSDave Kleikamp {
5237e9e3bcecSEric Sandeen 	int i, err;
5238c9de560dSAlex Tomas 
5239e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
524007c0c5d8SAl Viro 	ext4_li_info = NULL;
524107c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
524207c0c5d8SAl Viro 
52439a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
524412e9b892SDmitry Monakhov 	ext4_check_flag_values();
5245e9e3bcecSEric Sandeen 
5246e9e3bcecSEric Sandeen 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
5247e9e3bcecSEric Sandeen 		mutex_init(&ext4__aio_mutex[i]);
5248e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
5249e9e3bcecSEric Sandeen 	}
5250e9e3bcecSEric Sandeen 
525151865fdaSZheng Liu 	err = ext4_init_es();
52526fd058f7STheodore Ts'o 	if (err)
52536fd058f7STheodore Ts'o 		return err;
525451865fdaSZheng Liu 
525551865fdaSZheng Liu 	err = ext4_init_pageio();
525651865fdaSZheng Liu 	if (err)
5257b5799018STheodore Ts'o 		goto out5;
525851865fdaSZheng Liu 
52595dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
5260bd2d0210STheodore Ts'o 	if (err)
5261b5799018STheodore Ts'o 		goto out4;
5262857ac889SLukas Czerner 
5263b5799018STheodore Ts'o 	err = ext4_init_sysfs();
5264dd68314cSTheodore Ts'o 	if (err)
5265b5799018STheodore Ts'o 		goto out3;
5266857ac889SLukas Czerner 
52675dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
5268ac27a0ecSDave Kleikamp 	if (err)
5269c9de560dSAlex Tomas 		goto out2;
52709c191f70ST Makphaibulchoke 	else
52719c191f70ST Makphaibulchoke 		ext4_mballoc_ready = 1;
5272ac27a0ecSDave Kleikamp 	err = init_inodecache();
5273ac27a0ecSDave Kleikamp 	if (err)
5274ac27a0ecSDave Kleikamp 		goto out1;
527524b58424STheodore Ts'o 	register_as_ext3();
52762035e776STheodore Ts'o 	register_as_ext2();
527703010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
5278ac27a0ecSDave Kleikamp 	if (err)
5279ac27a0ecSDave Kleikamp 		goto out;
5280bfff6873SLukas Czerner 
5281ac27a0ecSDave Kleikamp 	return 0;
5282ac27a0ecSDave Kleikamp out:
528324b58424STheodore Ts'o 	unregister_as_ext2();
528424b58424STheodore Ts'o 	unregister_as_ext3();
5285ac27a0ecSDave Kleikamp 	destroy_inodecache();
5286ac27a0ecSDave Kleikamp out1:
52879c191f70ST Makphaibulchoke 	ext4_mballoc_ready = 0;
52885dabfc78STheodore Ts'o 	ext4_exit_mballoc();
52899c191f70ST Makphaibulchoke out2:
5290b5799018STheodore Ts'o 	ext4_exit_sysfs();
5291b5799018STheodore Ts'o out3:
5292dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5293b5799018STheodore Ts'o out4:
52945dabfc78STheodore Ts'o 	ext4_exit_pageio();
5295b5799018STheodore Ts'o out5:
529651865fdaSZheng Liu 	ext4_exit_es();
529751865fdaSZheng Liu 
5298ac27a0ecSDave Kleikamp 	return err;
5299ac27a0ecSDave Kleikamp }
5300ac27a0ecSDave Kleikamp 
53015dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5302ac27a0ecSDave Kleikamp {
5303e298e73bSChao Yu 	ext4_exit_crypto();
5304bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
530524b58424STheodore Ts'o 	unregister_as_ext2();
530624b58424STheodore Ts'o 	unregister_as_ext3();
530703010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5308ac27a0ecSDave Kleikamp 	destroy_inodecache();
53095dabfc78STheodore Ts'o 	ext4_exit_mballoc();
5310b5799018STheodore Ts'o 	ext4_exit_sysfs();
53115dabfc78STheodore Ts'o 	ext4_exit_system_zone();
53125dabfc78STheodore Ts'o 	ext4_exit_pageio();
5313dd12ed14SEric Sandeen 	ext4_exit_es();
5314ac27a0ecSDave Kleikamp }
5315ac27a0ecSDave Kleikamp 
5316ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
531783982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5318ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
53195dabfc78STheodore Ts'o module_init(ext4_init_fs)
53205dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5321