xref: /linux/fs/ext4/super.c (revision d3922a777f9b4c4df898d326fa940f239af4f9b6)
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>
24dab291afSMingming Cao #include <linux/jbd2.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.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>
379f6200bbSTheodore Ts'o #include <linux/proc_fs.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
417abc52c2SDan Magenheimer #include <linux/cleancache.h>
42ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
43ac27a0ecSDave Kleikamp 
44bfff6873SLukas Czerner #include <linux/kthread.h>
45bfff6873SLukas Czerner #include <linux/freezer.h>
46bfff6873SLukas Czerner 
473dcf5451SChristoph Hellwig #include "ext4.h"
484a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
493dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
50ac27a0ecSDave Kleikamp #include "xattr.h"
51ac27a0ecSDave Kleikamp #include "acl.h"
523661d286STheodore Ts'o #include "mballoc.h"
53ac27a0ecSDave Kleikamp 
549bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
559bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
569bffad1eSTheodore Ts'o 
571f109d5aSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root;
583197ebdbSTheodore Ts'o static struct kset *ext4_kset;
590b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
600b75a840SLukas Czerner static struct mutex ext4_li_mtx;
610b75a840SLukas Czerner static struct ext4_features *ext4_feat;
629f6200bbSTheodore Ts'o 
63617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
64ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
652adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
66e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
67617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
68617ba13bSMingming Cao 					struct ext4_super_block *es);
69617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
70617ba13bSMingming Cao 				   struct ext4_super_block *es);
71617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
7206a407f1SDmitry Monakhov static int ext4_sync_fs_nojournal(struct super_block *sb, int wait);
73617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
74617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
75c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
76c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
77152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
78152a0836SAl Viro 		       const char *dev_name, void *data);
792035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
802035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
81d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
82bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
83bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
848f1f7453SEric Sandeen static void ext4_clear_request_list(void);
8527dd4385SLukas Czerner static int ext4_reserve_clusters(struct ext4_sb_info *, ext4_fsblk_t);
86ac27a0ecSDave Kleikamp 
872035e776STheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
882035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
892035e776STheodore Ts'o 	.owner		= THIS_MODULE,
902035e776STheodore Ts'o 	.name		= "ext2",
912035e776STheodore Ts'o 	.mount		= ext4_mount,
922035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
932035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
942035e776STheodore Ts'o };
957f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
96fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
972035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
982035e776STheodore Ts'o #else
992035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1002035e776STheodore Ts'o #endif
1012035e776STheodore Ts'o 
1022035e776STheodore Ts'o 
103ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
104ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
105ba69f9abSJan Kara 	.owner		= THIS_MODULE,
106ba69f9abSJan Kara 	.name		= "ext3",
107152a0836SAl Viro 	.mount		= ext4_mount,
108ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
109ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
110ba69f9abSJan Kara };
1117f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
112fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
113ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
114ba69f9abSJan Kara #else
115ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0)
116ba69f9abSJan Kara #endif
117bd81d8eeSLaurent Vivier 
118d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
119d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
120d25425f8SDarrick J. Wong {
121d25425f8SDarrick J. Wong 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
122d25425f8SDarrick J. Wong 					EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
123d25425f8SDarrick J. Wong 		return 1;
124d25425f8SDarrick J. Wong 
125d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
126d25425f8SDarrick J. Wong }
127d25425f8SDarrick J. Wong 
128a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
129a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
130a9c47317SDarrick J. Wong {
131a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
132a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
133a9c47317SDarrick J. Wong 	__u32 csum;
134a9c47317SDarrick J. Wong 
135a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
136a9c47317SDarrick J. Wong 
137a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
138a9c47317SDarrick J. Wong }
139a9c47317SDarrick J. Wong 
140a9c47317SDarrick J. Wong int ext4_superblock_csum_verify(struct super_block *sb,
141a9c47317SDarrick J. Wong 				struct ext4_super_block *es)
142a9c47317SDarrick J. Wong {
143a9c47317SDarrick J. Wong 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
144a9c47317SDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
145a9c47317SDarrick J. Wong 		return 1;
146a9c47317SDarrick J. Wong 
147a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
148a9c47317SDarrick J. Wong }
149a9c47317SDarrick J. Wong 
15006db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
151a9c47317SDarrick J. Wong {
15206db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
15306db49e6STheodore Ts'o 
154a9c47317SDarrick J. Wong 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
155a9c47317SDarrick J. Wong 		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
156a9c47317SDarrick J. Wong 		return;
157a9c47317SDarrick J. Wong 
158a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
159a9c47317SDarrick J. Wong }
160a9c47317SDarrick J. Wong 
1619933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
1629933fc0aSTheodore Ts'o {
1639933fc0aSTheodore Ts'o 	void *ret;
1649933fc0aSTheodore Ts'o 
1659933fc0aSTheodore Ts'o 	ret = kmalloc(size, flags);
1669933fc0aSTheodore Ts'o 	if (!ret)
1679933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
1689933fc0aSTheodore Ts'o 	return ret;
1699933fc0aSTheodore Ts'o }
1709933fc0aSTheodore Ts'o 
1719933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
1729933fc0aSTheodore Ts'o {
1739933fc0aSTheodore Ts'o 	void *ret;
1749933fc0aSTheodore Ts'o 
175db9481c0SMathias Krause 	ret = kzalloc(size, flags);
1769933fc0aSTheodore Ts'o 	if (!ret)
1779933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
1789933fc0aSTheodore Ts'o 	return ret;
1799933fc0aSTheodore Ts'o }
1809933fc0aSTheodore Ts'o 
1819933fc0aSTheodore Ts'o void ext4_kvfree(void *ptr)
1829933fc0aSTheodore Ts'o {
1839933fc0aSTheodore Ts'o 	if (is_vmalloc_addr(ptr))
1849933fc0aSTheodore Ts'o 		vfree(ptr);
1859933fc0aSTheodore Ts'o 	else
1869933fc0aSTheodore Ts'o 		kfree(ptr);
1879933fc0aSTheodore Ts'o 
1889933fc0aSTheodore Ts'o }
1899933fc0aSTheodore Ts'o 
1908fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
1918fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
192bd81d8eeSLaurent Vivier {
1933a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
1948fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1958fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
196bd81d8eeSLaurent Vivier }
197bd81d8eeSLaurent Vivier 
1988fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
1998fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
200bd81d8eeSLaurent Vivier {
2015272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
2028fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2038fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
204bd81d8eeSLaurent Vivier }
205bd81d8eeSLaurent Vivier 
2068fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
2078fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
208bd81d8eeSLaurent Vivier {
2095272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
2108fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2118fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
212bd81d8eeSLaurent Vivier }
213bd81d8eeSLaurent Vivier 
214021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
215560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
216560671a0SAneesh Kumar K.V {
217560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
218560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
219560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
220560671a0SAneesh Kumar K.V }
221560671a0SAneesh Kumar K.V 
222560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
223560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
224560671a0SAneesh Kumar K.V {
225560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
226560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
227560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
228560671a0SAneesh Kumar K.V }
229560671a0SAneesh Kumar K.V 
230560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
231560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
232560671a0SAneesh Kumar K.V {
233560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
234560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
235560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
236560671a0SAneesh Kumar K.V }
237560671a0SAneesh Kumar K.V 
238560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
239560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
240560671a0SAneesh Kumar K.V {
241560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
242560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
243560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
244560671a0SAneesh Kumar K.V }
245560671a0SAneesh Kumar K.V 
2468fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2478fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
248bd81d8eeSLaurent Vivier {
2493a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2508fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2518fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
252bd81d8eeSLaurent Vivier }
253bd81d8eeSLaurent Vivier 
2548fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2558fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
256bd81d8eeSLaurent Vivier {
2575272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2588fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2598fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
260bd81d8eeSLaurent Vivier }
261bd81d8eeSLaurent Vivier 
2628fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2638fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
264bd81d8eeSLaurent Vivier {
2655272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2668fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2678fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
268bd81d8eeSLaurent Vivier }
269bd81d8eeSLaurent Vivier 
270021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
271560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
272560671a0SAneesh Kumar K.V {
273560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
274560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
275560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
276560671a0SAneesh Kumar K.V }
277560671a0SAneesh Kumar K.V 
278560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
279560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
280560671a0SAneesh Kumar K.V {
281560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
282560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
283560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
284560671a0SAneesh Kumar K.V }
285560671a0SAneesh Kumar K.V 
286560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
287560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
288560671a0SAneesh Kumar K.V {
289560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
290560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
291560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
292560671a0SAneesh Kumar K.V }
293560671a0SAneesh Kumar K.V 
294560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
295560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
296560671a0SAneesh Kumar K.V {
297560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
298560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
299560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
300560671a0SAneesh Kumar K.V }
301560671a0SAneesh Kumar K.V 
302d3d1faf6SCurt Wohlgemuth 
3031c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3041c13d5c0STheodore Ts'o 			    unsigned int line)
3051c13d5c0STheodore Ts'o {
3061c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3071c13d5c0STheodore Ts'o 
3081c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3091c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3101c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
3111c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3121c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3131c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3141c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3151c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3161c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3171c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3181c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3191c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3201c13d5c0STheodore Ts'o 	}
32166e61a9eSTheodore Ts'o 	/*
32266e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
32366e61a9eSTheodore Ts'o 	 * started already
32466e61a9eSTheodore Ts'o 	 */
32566e61a9eSTheodore Ts'o 	if (!es->s_error_count)
32666e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
327ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
3281c13d5c0STheodore Ts'o }
3291c13d5c0STheodore Ts'o 
3301c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3311c13d5c0STheodore Ts'o 			    unsigned int line)
3321c13d5c0STheodore Ts'o {
3331c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3341c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3351c13d5c0STheodore Ts'o }
3361c13d5c0STheodore Ts'o 
3377c2e7087STheodore Ts'o /*
3387c2e7087STheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
3397c2e7087STheodore Ts'o  * structures, including the bdi structure, without telling anyone
3407c2e7087STheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
3417c2e7087STheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
3427c2e7087STheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
3437c2e7087STheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
3447c2e7087STheodore Ts'o  */
3457c2e7087STheodore Ts'o static int block_device_ejected(struct super_block *sb)
3467c2e7087STheodore Ts'o {
3477c2e7087STheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
3487c2e7087STheodore Ts'o 	struct backing_dev_info *bdi = bd_inode->i_mapping->backing_dev_info;
3497c2e7087STheodore Ts'o 
3507c2e7087STheodore Ts'o 	return bdi->dev == NULL;
3517c2e7087STheodore Ts'o }
3527c2e7087STheodore Ts'o 
35318aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
35418aadd47SBobi Jam {
35518aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
35618aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
35718aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
3585d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
35918aadd47SBobi Jam 
3605d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
36118aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
3625d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
3635d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
3645d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
36518aadd47SBobi Jam 		list_del_init(&jce->jce_list);
36618aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
36718aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
36818aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
36918aadd47SBobi Jam 	}
37018aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
37118aadd47SBobi Jam }
3721c13d5c0STheodore Ts'o 
373ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
374ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
375ac27a0ecSDave Kleikamp  *
376ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
377617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
378ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
379ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
380ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
381ac27a0ecSDave Kleikamp  * write out the superblock safely.
382ac27a0ecSDave Kleikamp  *
383dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
384d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
385ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
386ac27a0ecSDave Kleikamp  */
387ac27a0ecSDave Kleikamp 
388617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
389ac27a0ecSDave Kleikamp {
390ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
391ac27a0ecSDave Kleikamp 		return;
392ac27a0ecSDave Kleikamp 
393ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
394617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
395ac27a0ecSDave Kleikamp 
3964ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
397ac27a0ecSDave Kleikamp 		if (journal)
398dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
399ac27a0ecSDave Kleikamp 	}
400ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
401b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
4024418e141SDmitry Monakhov 		/*
4034418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
4044418e141SDmitry Monakhov 		 * before ->s_flags update
4054418e141SDmitry Monakhov 		 */
4064418e141SDmitry Monakhov 		smp_wmb();
407ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
408ac27a0ecSDave Kleikamp 	}
409ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
410617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
411ac27a0ecSDave Kleikamp 			sb->s_id);
412ac27a0ecSDave Kleikamp }
413ac27a0ecSDave Kleikamp 
41412062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
415c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
416ac27a0ecSDave Kleikamp {
4170ff2ea7dSJoe Perches 	struct va_format vaf;
418ac27a0ecSDave Kleikamp 	va_list args;
419ac27a0ecSDave Kleikamp 
420ac27a0ecSDave Kleikamp 	va_start(args, fmt);
4210ff2ea7dSJoe Perches 	vaf.fmt = fmt;
4220ff2ea7dSJoe Perches 	vaf.va = &args;
4230ff2ea7dSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4240ff2ea7dSJoe Perches 	       sb->s_id, function, line, current->comm, &vaf);
425ac27a0ecSDave Kleikamp 	va_end(args);
426f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
427ac27a0ecSDave Kleikamp 
428617ba13bSMingming Cao 	ext4_handle_error(sb);
429ac27a0ecSDave Kleikamp }
430ac27a0ecSDave Kleikamp 
431c398eda0STheodore Ts'o void ext4_error_inode(struct inode *inode, const char *function,
432c398eda0STheodore Ts'o 		      unsigned int line, ext4_fsblk_t block,
433273df556SFrank Mayhar 		      const char *fmt, ...)
434273df556SFrank Mayhar {
435273df556SFrank Mayhar 	va_list args;
436f7c21177STheodore Ts'o 	struct va_format vaf;
4371c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
438273df556SFrank Mayhar 
4391c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4401c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
4411c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
442273df556SFrank Mayhar 	va_start(args, fmt);
443f7c21177STheodore Ts'o 	vaf.fmt = fmt;
444f7c21177STheodore Ts'o 	vaf.va = &args;
445c398eda0STheodore Ts'o 	if (block)
446d9ee81daSJoe Perches 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
447d9ee81daSJoe Perches 		       "inode #%lu: block %llu: comm %s: %pV\n",
448d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
449d9ee81daSJoe Perches 		       block, current->comm, &vaf);
450d9ee81daSJoe Perches 	else
451d9ee81daSJoe Perches 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
452d9ee81daSJoe Perches 		       "inode #%lu: comm %s: %pV\n",
453d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
454d9ee81daSJoe Perches 		       current->comm, &vaf);
455273df556SFrank Mayhar 	va_end(args);
456273df556SFrank Mayhar 
457273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
458273df556SFrank Mayhar }
459273df556SFrank Mayhar 
460c398eda0STheodore Ts'o void ext4_error_file(struct file *file, const char *function,
461f7c21177STheodore Ts'o 		     unsigned int line, ext4_fsblk_t block,
462f7c21177STheodore Ts'o 		     const char *fmt, ...)
463273df556SFrank Mayhar {
464273df556SFrank Mayhar 	va_list args;
465f7c21177STheodore Ts'o 	struct va_format vaf;
4661c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
467496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
468273df556SFrank Mayhar 	char pathname[80], *path;
469273df556SFrank Mayhar 
4701c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
4711c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4721c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
473273df556SFrank Mayhar 	path = d_path(&(file->f_path), pathname, sizeof(pathname));
474f9a62d09SDan Carpenter 	if (IS_ERR(path))
475273df556SFrank Mayhar 		path = "(unknown)";
476f7c21177STheodore Ts'o 	va_start(args, fmt);
477f7c21177STheodore Ts'o 	vaf.fmt = fmt;
478f7c21177STheodore Ts'o 	vaf.va = &args;
479d9ee81daSJoe Perches 	if (block)
480d9ee81daSJoe Perches 		printk(KERN_CRIT
481d9ee81daSJoe Perches 		       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
482d9ee81daSJoe Perches 		       "block %llu: comm %s: path %s: %pV\n",
483d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
484d9ee81daSJoe Perches 		       block, current->comm, path, &vaf);
485d9ee81daSJoe Perches 	else
486d9ee81daSJoe Perches 		printk(KERN_CRIT
487d9ee81daSJoe Perches 		       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
488d9ee81daSJoe Perches 		       "comm %s: path %s: %pV\n",
489d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
490d9ee81daSJoe Perches 		       current->comm, path, &vaf);
491273df556SFrank Mayhar 	va_end(args);
492273df556SFrank Mayhar 
493273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
494273df556SFrank Mayhar }
495273df556SFrank Mayhar 
496722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
497ac27a0ecSDave Kleikamp 			      char nbuf[16])
498ac27a0ecSDave Kleikamp {
499ac27a0ecSDave Kleikamp 	char *errstr = NULL;
500ac27a0ecSDave Kleikamp 
501ac27a0ecSDave Kleikamp 	switch (errno) {
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 
546617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
547c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
548c398eda0STheodore Ts'o 	       sb->s_id, function, line, errstr);
5491c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
550ac27a0ecSDave Kleikamp 
551617ba13bSMingming Cao 	ext4_handle_error(sb);
552ac27a0ecSDave Kleikamp }
553ac27a0ecSDave Kleikamp 
554ac27a0ecSDave Kleikamp /*
555617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
556ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
557ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
558ac27a0ecSDave Kleikamp  *
559ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
560ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
561ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
562ac27a0ecSDave Kleikamp  */
563ac27a0ecSDave Kleikamp 
564c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
565c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
566ac27a0ecSDave Kleikamp {
567ac27a0ecSDave Kleikamp 	va_list args;
568ac27a0ecSDave Kleikamp 
5691c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
570ac27a0ecSDave Kleikamp 	va_start(args, fmt);
571c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
572c398eda0STheodore Ts'o 	       function, line);
573ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
574ac27a0ecSDave Kleikamp 	printk("\n");
575ac27a0ecSDave Kleikamp 	va_end(args);
576ac27a0ecSDave Kleikamp 
5771c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
578b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
5794ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
5804418e141SDmitry Monakhov 		/*
5814418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
5824418e141SDmitry Monakhov 		 * before ->s_flags update
5834418e141SDmitry Monakhov 		 */
5844418e141SDmitry Monakhov 		smp_wmb();
5854418e141SDmitry Monakhov 		sb->s_flags |= MS_RDONLY;
586ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
587dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
5881c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
5891c13d5c0STheodore Ts'o 	}
5901c13d5c0STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC))
5911c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
592ac27a0ecSDave Kleikamp }
593ac27a0ecSDave Kleikamp 
5940ff2ea7dSJoe Perches void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
595b31e1552SEric Sandeen {
5960ff2ea7dSJoe Perches 	struct va_format vaf;
597b31e1552SEric Sandeen 	va_list args;
598b31e1552SEric Sandeen 
599b31e1552SEric Sandeen 	va_start(args, fmt);
6000ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6010ff2ea7dSJoe Perches 	vaf.va = &args;
6020ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
603b31e1552SEric Sandeen 	va_end(args);
604b31e1552SEric Sandeen }
605b31e1552SEric Sandeen 
60612062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
607c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
608ac27a0ecSDave Kleikamp {
6090ff2ea7dSJoe Perches 	struct va_format vaf;
610ac27a0ecSDave Kleikamp 	va_list args;
611ac27a0ecSDave Kleikamp 
612ac27a0ecSDave Kleikamp 	va_start(args, fmt);
6130ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6140ff2ea7dSJoe Perches 	vaf.va = &args;
6150ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
6160ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
617ac27a0ecSDave Kleikamp 	va_end(args);
618ac27a0ecSDave Kleikamp }
619ac27a0ecSDave Kleikamp 
620e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
621e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
622e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
623e29136f8STheodore Ts'o 			     const char *fmt, ...)
6245d1b1b3fSAneesh Kumar K.V __releases(bitlock)
6255d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
6265d1b1b3fSAneesh Kumar K.V {
6270ff2ea7dSJoe Perches 	struct va_format vaf;
6285d1b1b3fSAneesh Kumar K.V 	va_list args;
6295d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
6305d1b1b3fSAneesh Kumar K.V 
6311c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
6321c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
6331c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
6340ff2ea7dSJoe Perches 
6355d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
6360ff2ea7dSJoe Perches 
6370ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6380ff2ea7dSJoe Perches 	vaf.va = &args;
63921149d61SRobin Dong 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
640e29136f8STheodore Ts'o 	       sb->s_id, function, line, grp);
641e29136f8STheodore Ts'o 	if (ino)
6420ff2ea7dSJoe Perches 		printk(KERN_CONT "inode %lu: ", ino);
643e29136f8STheodore Ts'o 	if (block)
6440ff2ea7dSJoe Perches 		printk(KERN_CONT "block %llu:", (unsigned long long) block);
6450ff2ea7dSJoe Perches 	printk(KERN_CONT "%pV\n", &vaf);
6465d1b1b3fSAneesh Kumar K.V 	va_end(args);
6475d1b1b3fSAneesh Kumar K.V 
6485d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
649e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
6505d1b1b3fSAneesh Kumar K.V 		return;
6515d1b1b3fSAneesh Kumar K.V 	}
6521c13d5c0STheodore Ts'o 
6535d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
6545d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
6555d1b1b3fSAneesh Kumar K.V 	/*
6565d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
6575d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
6585d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
6595d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
6605d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
66125985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
6625d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
6635d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
6645d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
6655d1b1b3fSAneesh Kumar K.V 	 */
6665d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
6675d1b1b3fSAneesh Kumar K.V 	return;
6685d1b1b3fSAneesh Kumar K.V }
6695d1b1b3fSAneesh Kumar K.V 
670617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
671ac27a0ecSDave Kleikamp {
672617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
673ac27a0ecSDave Kleikamp 
674617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
675ac27a0ecSDave Kleikamp 		return;
676ac27a0ecSDave Kleikamp 
67712062dddSEric Sandeen 	ext4_warning(sb,
678ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
679ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
680617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
681ac27a0ecSDave Kleikamp 
682617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
683617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
684617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
685ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
686ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
687ac27a0ecSDave Kleikamp 
688ac27a0ecSDave Kleikamp 	/*
689ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
690ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
691ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
692ac27a0ecSDave Kleikamp 	 */
693ac27a0ecSDave Kleikamp }
694ac27a0ecSDave Kleikamp 
695ac27a0ecSDave Kleikamp /*
696ac27a0ecSDave Kleikamp  * Open the external journal device
697ac27a0ecSDave Kleikamp  */
698b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
699ac27a0ecSDave Kleikamp {
700ac27a0ecSDave Kleikamp 	struct block_device *bdev;
701ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
702ac27a0ecSDave Kleikamp 
703d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
704ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
705ac27a0ecSDave Kleikamp 		goto fail;
706ac27a0ecSDave Kleikamp 	return bdev;
707ac27a0ecSDave Kleikamp 
708ac27a0ecSDave Kleikamp fail:
709b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
710ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
711ac27a0ecSDave Kleikamp 	return NULL;
712ac27a0ecSDave Kleikamp }
713ac27a0ecSDave Kleikamp 
714ac27a0ecSDave Kleikamp /*
715ac27a0ecSDave Kleikamp  * Release the journal device
716ac27a0ecSDave Kleikamp  */
7174385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
718ac27a0ecSDave Kleikamp {
7194385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
720ac27a0ecSDave Kleikamp }
721ac27a0ecSDave Kleikamp 
7224385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
723ac27a0ecSDave Kleikamp {
724ac27a0ecSDave Kleikamp 	struct block_device *bdev;
725ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
726ac27a0ecSDave Kleikamp 	if (bdev) {
7274385bab1SAl Viro 		ext4_blkdev_put(bdev);
728ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
729ac27a0ecSDave Kleikamp 	}
730ac27a0ecSDave Kleikamp }
731ac27a0ecSDave Kleikamp 
732ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
733ac27a0ecSDave Kleikamp {
734617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
735ac27a0ecSDave Kleikamp }
736ac27a0ecSDave Kleikamp 
737617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
738ac27a0ecSDave Kleikamp {
739ac27a0ecSDave Kleikamp 	struct list_head *l;
740ac27a0ecSDave Kleikamp 
741b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
742ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
743ac27a0ecSDave Kleikamp 
744ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
745ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
746ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
747ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
748ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
749ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
750ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
751ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
752ac27a0ecSDave Kleikamp 	}
753ac27a0ecSDave Kleikamp }
754ac27a0ecSDave Kleikamp 
755617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
756ac27a0ecSDave Kleikamp {
757617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
758617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
759ef2cabf7SHidehiro Kawai 	int i, err;
760ac27a0ecSDave Kleikamp 
761857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
762e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
763e0ccfd95SChristoph Hellwig 
7642e8fa54eSJan Kara 	flush_workqueue(sbi->unrsv_conversion_wq);
7652e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
7662e8fa54eSJan Kara 	destroy_workqueue(sbi->unrsv_conversion_wq);
7672e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
7684c0425ffSMingming Cao 
7690390131bSFrank Mayhar 	if (sbi->s_journal) {
770ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
77147b4a50bSJan Kara 		sbi->s_journal = NULL;
772ef2cabf7SHidehiro Kawai 		if (err < 0)
773c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
7740390131bSFrank Mayhar 	}
775d4edac31SJosef Bacik 
776*d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
777a1c6c569SSergey Senozhatsky 	del_timer(&sbi->s_err_report);
778d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
779d4edac31SJosef Bacik 	ext4_mb_release(sb);
780d4edac31SJosef Bacik 	ext4_ext_release(sb);
781d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
782d4edac31SJosef Bacik 
783ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
784617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
785ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
786ac27a0ecSDave Kleikamp 	}
78758c5873aSArtem Bityutskiy 	if (!(sb->s_flags & MS_RDONLY))
788a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
789a8e25a83SArtem Bityutskiy 
790240799cdSTheodore Ts'o 	if (sbi->s_proc) {
79166acdcf4STheodore Ts'o 		remove_proc_entry("options", sbi->s_proc);
7929f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
793240799cdSTheodore Ts'o 	}
7943197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
795ac27a0ecSDave Kleikamp 
796ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
797ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
798f18a5f21STheodore Ts'o 	ext4_kvfree(sbi->s_group_desc);
7999933fc0aSTheodore Ts'o 	ext4_kvfree(sbi->s_flex_groups);
80057042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
801ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
802ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
80357042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
8041ac6466fSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_extent_cache_cnt);
805ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
806ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
807ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
808ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
809ac27a0ecSDave Kleikamp #endif
810ac27a0ecSDave Kleikamp 
811ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
812ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
813ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
814ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
815ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
816ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
817ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
818ac27a0ecSDave Kleikamp 
819f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
820ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
821ac27a0ecSDave Kleikamp 		/*
822ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
823ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
824ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
825ac27a0ecSDave Kleikamp 		 */
826ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
827f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
828617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
829ac27a0ecSDave Kleikamp 	}
830c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
831c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
832ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
8333197ebdbSTheodore Ts'o 	/*
8343197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
8353197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
8363197ebdbSTheodore Ts'o 	 */
8373197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
8383197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
8390441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
8400441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
841705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
842ac27a0ecSDave Kleikamp 	kfree(sbi);
843ac27a0ecSDave Kleikamp }
844ac27a0ecSDave Kleikamp 
845e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
846ac27a0ecSDave Kleikamp 
847ac27a0ecSDave Kleikamp /*
848ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
849ac27a0ecSDave Kleikamp  */
850617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
851ac27a0ecSDave Kleikamp {
852617ba13bSMingming Cao 	struct ext4_inode_info *ei;
853ac27a0ecSDave Kleikamp 
854e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
855ac27a0ecSDave Kleikamp 	if (!ei)
856ac27a0ecSDave Kleikamp 		return NULL;
8570b8e58a1SAndreas Dilger 
858ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
859c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
860c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
8619a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
8629a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
86374cd15cdSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_lru);
86474cd15cdSZheng Liu 	ei->i_es_lru_nr = 0;
865*d3922a77SZheng Liu 	ei->i_touch_when = 0;
866d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
867d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
868d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
8699d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
8707e731bc9STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = 0;
871d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
872a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
873a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
874a9e7f447SDmitry Monakhov #endif
8758aefcd55STheodore Ts'o 	ei->jinode = NULL;
8762e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
8772e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_unrsv_conversion_list);
878744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
879b436b9beSJan Kara 	ei->i_sync_tid = 0;
880b436b9beSJan Kara 	ei->i_datasync_tid = 0;
881f7ad6d2eSTheodore Ts'o 	atomic_set(&ei->i_ioend_count, 0);
882e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
8832e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
8842e8fa54eSJan Kara 	INIT_WORK(&ei->i_unrsv_conversion_work, ext4_end_io_unrsv_work);
8850b8e58a1SAndreas Dilger 
886ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
887ac27a0ecSDave Kleikamp }
888ac27a0ecSDave Kleikamp 
8897ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
8907ff9c073STheodore Ts'o {
8917ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
8927ff9c073STheodore Ts'o 
8937ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
8947ff9c073STheodore Ts'o 	return drop;
8957ff9c073STheodore Ts'o }
8967ff9c073STheodore Ts'o 
897fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
898fa0d7e3dSNick Piggin {
899fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
900fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
901fa0d7e3dSNick Piggin }
902fa0d7e3dSNick Piggin 
903617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
904ac27a0ecSDave Kleikamp {
9059f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
906b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
907b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
908b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
9099f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
9109f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
9119f7dd93dSVasily Averin 				true);
9129f7dd93dSVasily Averin 		dump_stack();
9139f7dd93dSVasily Averin 	}
914fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
915ac27a0ecSDave Kleikamp }
916ac27a0ecSDave Kleikamp 
91751cc5068SAlexey Dobriyan static void init_once(void *foo)
918ac27a0ecSDave Kleikamp {
919617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
920ac27a0ecSDave Kleikamp 
921ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
922ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
9230e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
924ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
925ac27a0ecSDave Kleikamp }
926ac27a0ecSDave Kleikamp 
927ac27a0ecSDave Kleikamp static int init_inodecache(void)
928ac27a0ecSDave Kleikamp {
929617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
930617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
931ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
932ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
93320c2df83SPaul Mundt 					     init_once);
934617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
935ac27a0ecSDave Kleikamp 		return -ENOMEM;
936ac27a0ecSDave Kleikamp 	return 0;
937ac27a0ecSDave Kleikamp }
938ac27a0ecSDave Kleikamp 
939ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
940ac27a0ecSDave Kleikamp {
9418c0a8537SKirill A. Shutemov 	/*
9428c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
9438c0a8537SKirill A. Shutemov 	 * destroy cache.
9448c0a8537SKirill A. Shutemov 	 */
9458c0a8537SKirill A. Shutemov 	rcu_barrier();
946617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
947ac27a0ecSDave Kleikamp }
948ac27a0ecSDave Kleikamp 
9490930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
950ac27a0ecSDave Kleikamp {
9510930fcc1SAl Viro 	invalidate_inode_buffers(inode);
952dbd5768fSJan Kara 	clear_inode(inode);
9539f754758SChristoph Hellwig 	dquot_drop(inode);
954c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
95551865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
95674cd15cdSZheng Liu 	ext4_es_lru_del(inode);
9578aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
9588aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
9598aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
9608aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
9618aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
9628aefcd55STheodore Ts'o 	}
963ac27a0ecSDave Kleikamp }
964ac27a0ecSDave Kleikamp 
9651b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
9661b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
967ac27a0ecSDave Kleikamp {
968ac27a0ecSDave Kleikamp 	struct inode *inode;
969ac27a0ecSDave Kleikamp 
970617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
971ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
972617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
973ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
974ac27a0ecSDave Kleikamp 
975ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
976ac27a0ecSDave Kleikamp 	 *
977617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
978ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
979ac27a0ecSDave Kleikamp 	 *
980ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
981ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
982ac27a0ecSDave Kleikamp 	 */
9831d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
9841d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9851d1fe1eeSDavid Howells 		return ERR_CAST(inode);
9861d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
987ac27a0ecSDave Kleikamp 		iput(inode);
988ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
989ac27a0ecSDave Kleikamp 	}
9901b961ac0SChristoph Hellwig 
9911b961ac0SChristoph Hellwig 	return inode;
992ac27a0ecSDave Kleikamp }
9931b961ac0SChristoph Hellwig 
9941b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
9951b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
9961b961ac0SChristoph Hellwig {
9971b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
9981b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
9991b961ac0SChristoph Hellwig }
10001b961ac0SChristoph Hellwig 
10011b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
10021b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10031b961ac0SChristoph Hellwig {
10041b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
10051b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1006ac27a0ecSDave Kleikamp }
1007ac27a0ecSDave Kleikamp 
1008c39a7f84SToshiyuki Okajima /*
1009c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1010c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1011c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1012c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1013c39a7f84SToshiyuki Okajima  */
10140b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
10150b8e58a1SAndreas Dilger 				 gfp_t wait)
1016c39a7f84SToshiyuki Okajima {
1017c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1018c39a7f84SToshiyuki Okajima 
1019c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1020c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1021c39a7f84SToshiyuki Okajima 		return 0;
1022c39a7f84SToshiyuki Okajima 	if (journal)
1023c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1024c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1025c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1026c39a7f84SToshiyuki Okajima }
1027c39a7f84SToshiyuki Okajima 
1028ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1029ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1030ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1031ac27a0ecSDave Kleikamp 
1032617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1033617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1034617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1035617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1036617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
10376f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1038f00c9e44SJan Kara 			 struct path *path);
10397c319d32SAditya Kali static int ext4_quota_on_sysfile(struct super_block *sb, int type,
10407c319d32SAditya Kali 				 int format_id);
1041ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
10427c319d32SAditya Kali static int ext4_quota_off_sysfile(struct super_block *sb, int type);
1043617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1044617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1045ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1046617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1047ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
10487c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
10497c319d32SAditya Kali 			     unsigned int flags);
10507c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1051ac27a0ecSDave Kleikamp 
105261e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
105360e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1054617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1055617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1056617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1057617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1058a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1059a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1060a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1061ac27a0ecSDave Kleikamp };
1062ac27a0ecSDave Kleikamp 
10630d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1064617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1065ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1066287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
1067287a8095SChristoph Hellwig 	.get_info	= dquot_get_dqinfo,
1068287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1069287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1070287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1071ac27a0ecSDave Kleikamp };
10727c319d32SAditya Kali 
10737c319d32SAditya Kali static const struct quotactl_ops ext4_qctl_sysfile_operations = {
10747c319d32SAditya Kali 	.quota_on_meta	= ext4_quota_on_sysfile,
10757c319d32SAditya Kali 	.quota_off	= ext4_quota_off_sysfile,
10767c319d32SAditya Kali 	.quota_sync	= dquot_quota_sync,
10777c319d32SAditya Kali 	.get_info	= dquot_get_dqinfo,
10787c319d32SAditya Kali 	.set_info	= dquot_set_dqinfo,
10797c319d32SAditya Kali 	.get_dqblk	= dquot_get_dqblk,
10807c319d32SAditya Kali 	.set_dqblk	= dquot_set_dqblk
10817c319d32SAditya Kali };
1082ac27a0ecSDave Kleikamp #endif
1083ac27a0ecSDave Kleikamp 
1084ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1085617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1086617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1087617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1088617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
10897ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
10900930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1091617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1092617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1093c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1094c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1095617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1096617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1097617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1098ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1099617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1100617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1101ac27a0ecSDave Kleikamp #endif
1102c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1103ac27a0ecSDave Kleikamp };
1104ac27a0ecSDave Kleikamp 
11059ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
11069ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
11079ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
11089ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
11099ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
11107ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
11110930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
111206a407f1SDmitry Monakhov 	.sync_fs	= ext4_sync_fs_nojournal,
11139ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
11149ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
11159ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
11169ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
11179ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
11189ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
11199ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
11209ca92389STheodore Ts'o #endif
11219ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
11229ca92389STheodore Ts'o };
11239ca92389STheodore Ts'o 
112439655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
11251b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
11261b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1127617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1128ac27a0ecSDave Kleikamp };
1129ac27a0ecSDave Kleikamp 
1130ac27a0ecSDave Kleikamp enum {
1131ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1132ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
113372578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1134ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
113572578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
113630773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
113743e625d8SEric Sandeen 	Opt_journal_dev, Opt_journal_checksum, Opt_journal_async_commit,
1138ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1139296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1140ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
11415a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1142ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1143661aa520SEric Sandeen 	Opt_usrquota, Opt_grpquota, Opt_i_version,
11441449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
11451449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
11465328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1147744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1148fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1149df981d03STheodore Ts'o 	Opt_max_dir_size_kb,
1150ac27a0ecSDave Kleikamp };
1151ac27a0ecSDave Kleikamp 
1152a447c093SSteven Whitehouse static const match_table_t tokens = {
1153ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1154ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1155ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1156ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1157ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1158ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1159ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1160ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1161ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1162ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1163ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1164ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1165ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1166ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
116772578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
116872578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1169ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1170ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1171ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1172ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1173e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
11745a916be1STheodore Ts'o 	{Opt_noload, "noload"},
117572578c33STheodore Ts'o 	{Opt_removed, "nobh"},
117672578c33STheodore Ts'o 	{Opt_removed, "bh"},
1177ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
117830773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
117930773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1180ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1181818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1182818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1183ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1184ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1185ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1186ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
11875bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
11885bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1189ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1190ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1191ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1192ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1193ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1194ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
11955a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1196ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1197ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1198ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1199ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1200ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
120106705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
120206705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
120325ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1204c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
120564769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1206dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
120736ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
120836ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
12096fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
12106fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1211240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1212b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1213afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
121406705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
121506705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1216744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1217744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
12185328e635SEric Sandeen 	{Opt_discard, "discard"},
12195328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1220fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1221fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1222fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1223df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1224c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1225c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1226c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1227c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1228c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1229f3f12faaSJosef Bacik 	{Opt_err, NULL},
1230ac27a0ecSDave Kleikamp };
1231ac27a0ecSDave Kleikamp 
1232617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1233ac27a0ecSDave Kleikamp {
1234617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1235ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1236ac27a0ecSDave Kleikamp 
1237ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1238ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
12390b8e58a1SAndreas Dilger 
1240ac27a0ecSDave Kleikamp 	options += 3;
12410b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1242ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1243ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
12444776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1245ac27a0ecSDave Kleikamp 		       (char *) *data);
1246ac27a0ecSDave Kleikamp 		return 1;
1247ac27a0ecSDave Kleikamp 	}
1248ac27a0ecSDave Kleikamp 	if (*options == ',')
1249ac27a0ecSDave Kleikamp 		options++;
1250ac27a0ecSDave Kleikamp 	*data = (void *) options;
12510b8e58a1SAndreas Dilger 
1252ac27a0ecSDave Kleikamp 	return sb_block;
1253ac27a0ecSDave Kleikamp }
1254ac27a0ecSDave Kleikamp 
1255b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1256437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1257437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1258b3881f74STheodore Ts'o 
125956c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
126056c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
126156c50f11SDmitry Monakhov {
126256c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
126356c50f11SDmitry Monakhov 	char *qname;
126403dafb5fSChen Gang 	int ret = -1;
126556c50f11SDmitry Monakhov 
126656c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
126756c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
126856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
126956c50f11SDmitry Monakhov 			"Cannot change journaled "
127056c50f11SDmitry Monakhov 			"quota options when quota turned on");
127157f73c2cSTheodore Ts'o 		return -1;
127256c50f11SDmitry Monakhov 	}
1273262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1274262b4662SJan Kara 		ext4_msg(sb, KERN_ERR, "Cannot set journaled quota options "
1275262b4662SJan Kara 			 "when QUOTA feature is enabled");
1276262b4662SJan Kara 		return -1;
1277262b4662SJan Kara 	}
127856c50f11SDmitry Monakhov 	qname = match_strdup(args);
127956c50f11SDmitry Monakhov 	if (!qname) {
128056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
128156c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
128257f73c2cSTheodore Ts'o 		return -1;
128356c50f11SDmitry Monakhov 	}
128403dafb5fSChen Gang 	if (sbi->s_qf_names[qtype]) {
128503dafb5fSChen Gang 		if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
128603dafb5fSChen Gang 			ret = 1;
128703dafb5fSChen Gang 		else
128856c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
128903dafb5fSChen Gang 				 "%s quota file already specified",
129003dafb5fSChen Gang 				 QTYPE2NAME(qtype));
129103dafb5fSChen Gang 		goto errout;
129256c50f11SDmitry Monakhov 	}
129303dafb5fSChen Gang 	if (strchr(qname, '/')) {
129456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
129556c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
129603dafb5fSChen Gang 		goto errout;
129756c50f11SDmitry Monakhov 	}
129803dafb5fSChen Gang 	sbi->s_qf_names[qtype] = qname;
1299fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
130056c50f11SDmitry Monakhov 	return 1;
130103dafb5fSChen Gang errout:
130203dafb5fSChen Gang 	kfree(qname);
130303dafb5fSChen Gang 	return ret;
130456c50f11SDmitry Monakhov }
130556c50f11SDmitry Monakhov 
130656c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
130756c50f11SDmitry Monakhov {
130856c50f11SDmitry Monakhov 
130956c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
131056c50f11SDmitry Monakhov 
131156c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
131256c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
131356c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
131456c50f11SDmitry Monakhov 			" when quota turned on");
131557f73c2cSTheodore Ts'o 		return -1;
131656c50f11SDmitry Monakhov 	}
131703dafb5fSChen Gang 	kfree(sbi->s_qf_names[qtype]);
131856c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
131956c50f11SDmitry Monakhov 	return 1;
132056c50f11SDmitry Monakhov }
132156c50f11SDmitry Monakhov #endif
132256c50f11SDmitry Monakhov 
132326092bf5STheodore Ts'o #define MOPT_SET	0x0001
132426092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
132526092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
132626092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
132726092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
132826092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
132926092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
133026092bf5STheodore Ts'o #define MOPT_Q		0
133126092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
133226092bf5STheodore Ts'o #else
133326092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
133426092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
133526092bf5STheodore Ts'o #endif
133626092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
13378dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
13388dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
13398dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
134026092bf5STheodore Ts'o 
134126092bf5STheodore Ts'o static const struct mount_opts {
134226092bf5STheodore Ts'o 	int	token;
134326092bf5STheodore Ts'o 	int	mount_opt;
134426092bf5STheodore Ts'o 	int	flags;
134526092bf5STheodore Ts'o } ext4_mount_opts[] = {
134626092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
134726092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
134826092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
134926092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
135026092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
135126092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
13528dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
13538dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
13548dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
13558dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
135626092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
135726092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
13588dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
13598dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
13608dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
13618dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR | MOPT_EXPLICIT},
13628dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
13638dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
136426092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
13658dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
13668dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
13678dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
136826092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
136926092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
137026092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
13718dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
13728dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_SET},
13738dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
13748dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_CLEAR},
137526092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
137626092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
137726092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
137826092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
137926092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
138026092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
138126092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
138226092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
138326092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
138426092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
138526092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
13860efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
13870efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
13880efb3b23SJan Kara 	{Opt_journal_dev, 0, MOPT_GTE0},
13890efb3b23SJan Kara 	{Opt_journal_ioprio, 0, MOPT_GTE0},
13908dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
13918dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
13928dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
13938dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
139426092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
139526092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
139626092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
139726092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
139826092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
139926092bf5STheodore Ts'o #else
140026092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
140126092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
140226092bf5STheodore Ts'o #endif
140326092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
140426092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
140526092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
140626092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
140726092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
140826092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
140926092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
141026092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
141126092bf5STheodore Ts'o 		       EXT4_MOUNT_GRPQUOTA), MOPT_CLEAR | MOPT_Q},
141226092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
141326092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
141426092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
141526092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
141626092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
141726092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
141826092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1419df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
142026092bf5STheodore Ts'o 	{Opt_err, 0, 0}
142126092bf5STheodore Ts'o };
142226092bf5STheodore Ts'o 
142326092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
142426092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
142526092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
142626092bf5STheodore Ts'o {
142726092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
142826092bf5STheodore Ts'o 	const struct mount_opts *m;
142908cefc7aSEric W. Biederman 	kuid_t uid;
143008cefc7aSEric W. Biederman 	kgid_t gid;
143126092bf5STheodore Ts'o 	int arg = 0;
143226092bf5STheodore Ts'o 
143357f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
143457f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
143557f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
143657f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
143757f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
143857f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
143957f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
144057f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
144157f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
144257f73c2cSTheodore Ts'o #endif
144326092bf5STheodore Ts'o 	switch (token) {
1444f7048605STheodore Ts'o 	case Opt_noacl:
1445f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1446f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1447f7048605STheodore Ts'o 		break;
144826092bf5STheodore Ts'o 	case Opt_sb:
144926092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
145026092bf5STheodore Ts'o 	case Opt_removed:
14515f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
145226092bf5STheodore Ts'o 		return 1;
145326092bf5STheodore Ts'o 	case Opt_abort:
145426092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
145526092bf5STheodore Ts'o 		return 1;
145626092bf5STheodore Ts'o 	case Opt_i_version:
145726092bf5STheodore Ts'o 		sb->s_flags |= MS_I_VERSION;
145826092bf5STheodore Ts'o 		return 1;
145926092bf5STheodore Ts'o 	}
146026092bf5STheodore Ts'o 
14615f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
14625f3633e3SJan Kara 		if (token == m->token)
14635f3633e3SJan Kara 			break;
14645f3633e3SJan Kara 
14655f3633e3SJan Kara 	if (m->token == Opt_err) {
14665f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
14675f3633e3SJan Kara 			 "or missing value", opt);
14685f3633e3SJan Kara 		return -1;
14695f3633e3SJan Kara 	}
14705f3633e3SJan Kara 
14718dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
14728dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
14738dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
14748dc0aa8cSTheodore Ts'o 		return -1;
14758dc0aa8cSTheodore Ts'o 	}
14768dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
14778dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
14788dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
14798dc0aa8cSTheodore Ts'o 		return -1;
14808dc0aa8cSTheodore Ts'o 	}
14818dc0aa8cSTheodore Ts'o 
14820efb3b23SJan Kara 	if (args->from && match_int(args, &arg))
14830efb3b23SJan Kara 		return -1;
148426092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
148526092bf5STheodore Ts'o 		return -1;
148626092bf5STheodore Ts'o 	if (m->flags & MOPT_EXPLICIT)
148726092bf5STheodore Ts'o 		set_opt2(sb, EXPLICIT_DELALLOC);
148826092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
148926092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
149026092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
149126092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
149226092bf5STheodore Ts'o 			 "options when quota turned on");
149326092bf5STheodore Ts'o 		return -1;
149426092bf5STheodore Ts'o 	}
149526092bf5STheodore Ts'o 
149626092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
149726092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
149826092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
149926092bf5STheodore Ts'o 		if (arg == 0)
150026092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
150126092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
150226092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
150326092bf5STheodore Ts'o 		if (arg == 0)
150426092bf5STheodore Ts'o 			arg = EXT4_DEF_MAX_BATCH_TIME;
150526092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
150626092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
150726092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
150826092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1509e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1510e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1511e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1512e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
151326092bf5STheodore Ts'o 			return -1;
151426092bf5STheodore Ts'o 		}
151526092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
151626092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
151726092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
151826092bf5STheodore Ts'o 		if (!args->from)
151926092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
152026092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1521df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1522df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
152326092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
152426092bf5STheodore Ts'o 		sbi->s_stripe = arg;
15250efb3b23SJan Kara 	} else if (token == Opt_resuid) {
15260efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
15270efb3b23SJan Kara 		if (!uid_valid(uid)) {
15285f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
15290efb3b23SJan Kara 			return -1;
15300efb3b23SJan Kara 		}
15310efb3b23SJan Kara 		sbi->s_resuid = uid;
15320efb3b23SJan Kara 	} else if (token == Opt_resgid) {
15330efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
15340efb3b23SJan Kara 		if (!gid_valid(gid)) {
15355f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
15360efb3b23SJan Kara 			return -1;
15370efb3b23SJan Kara 		}
15380efb3b23SJan Kara 		sbi->s_resgid = gid;
15390efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
15400efb3b23SJan Kara 		if (is_remount) {
15410efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
15420efb3b23SJan Kara 				 "Cannot specify journal on remount");
15430efb3b23SJan Kara 			return -1;
15440efb3b23SJan Kara 		}
15450efb3b23SJan Kara 		*journal_devnum = arg;
15460efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
15470efb3b23SJan Kara 		if (arg > 7) {
15485f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
15490efb3b23SJan Kara 				 " (must be 0-7)");
15500efb3b23SJan Kara 			return -1;
15510efb3b23SJan Kara 		}
15520efb3b23SJan Kara 		*journal_ioprio =
15530efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
155426092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
155526092bf5STheodore Ts'o 		if (is_remount) {
155626092bf5STheodore Ts'o 			if (!sbi->s_journal)
155726092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
15585f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
155926092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
156026092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
156126092bf5STheodore Ts'o 				return -1;
156226092bf5STheodore Ts'o 			}
156326092bf5STheodore Ts'o 		} else {
156426092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
156526092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
156626092bf5STheodore Ts'o 		}
156726092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
156826092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
156926092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
157026092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
15715f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
15725f3633e3SJan Kara 				 "quota options when quota turned on");
157326092bf5STheodore Ts'o 			return -1;
157426092bf5STheodore Ts'o 		}
1575262b4662SJan Kara 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
1576262b4662SJan Kara 					       EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1577262b4662SJan Kara 			ext4_msg(sb, KERN_ERR,
1578262b4662SJan Kara 				 "Cannot set journaled quota options "
1579262b4662SJan Kara 				 "when QUOTA feature is enabled");
1580262b4662SJan Kara 			return -1;
1581262b4662SJan Kara 		}
158226092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
158326092bf5STheodore Ts'o #endif
158426092bf5STheodore Ts'o 	} else {
158526092bf5STheodore Ts'o 		if (!args->from)
158626092bf5STheodore Ts'o 			arg = 1;
158726092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
158826092bf5STheodore Ts'o 			arg = !arg;
158926092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
159026092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
159126092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
159226092bf5STheodore Ts'o 			WARN_ON(1);
159326092bf5STheodore Ts'o 			return -1;
159426092bf5STheodore Ts'o 		}
159526092bf5STheodore Ts'o 		if (arg != 0)
159626092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
159726092bf5STheodore Ts'o 		else
159826092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
159926092bf5STheodore Ts'o 	}
160026092bf5STheodore Ts'o 	return 1;
160126092bf5STheodore Ts'o }
160226092bf5STheodore Ts'o 
1603ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1604c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1605b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1606661aa520SEric Sandeen 			 int is_remount)
1607ac27a0ecSDave Kleikamp {
1608617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1609ac27a0ecSDave Kleikamp 	char *p;
1610ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
161126092bf5STheodore Ts'o 	int token;
1612ac27a0ecSDave Kleikamp 
1613ac27a0ecSDave Kleikamp 	if (!options)
1614ac27a0ecSDave Kleikamp 		return 1;
1615ac27a0ecSDave Kleikamp 
1616ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1617ac27a0ecSDave Kleikamp 		if (!*p)
1618ac27a0ecSDave Kleikamp 			continue;
161915121c18SEric Sandeen 		/*
162015121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
162115121c18SEric Sandeen 		 * found; some options take optional arguments.
162215121c18SEric Sandeen 		 */
1623caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
1624ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
162526092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
162626092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
1627ac27a0ecSDave Kleikamp 			return 0;
1628ac27a0ecSDave Kleikamp 	}
1629ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1630262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1631262b4662SJan Kara 	    (test_opt(sb, USRQUOTA) || test_opt(sb, GRPQUOTA))) {
1632262b4662SJan Kara 		ext4_msg(sb, KERN_ERR, "Cannot set quota options when QUOTA "
1633262b4662SJan Kara 			 "feature is enabled");
1634262b4662SJan Kara 		return 0;
1635262b4662SJan Kara 	}
1636ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1637482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1638fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1639ac27a0ecSDave Kleikamp 
1640482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1641fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1642ac27a0ecSDave Kleikamp 
164356c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1644b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1645b31e1552SEric Sandeen 					"format mixing");
1646ac27a0ecSDave Kleikamp 			return 0;
1647ac27a0ecSDave Kleikamp 		}
1648ac27a0ecSDave Kleikamp 
1649ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1650b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1651b31e1552SEric Sandeen 					"not specified");
1652ac27a0ecSDave Kleikamp 			return 0;
1653ac27a0ecSDave Kleikamp 		}
1654ac27a0ecSDave Kleikamp 	} else {
1655ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1656b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
16572c8be6b2SJan Kara 					"specified with no journaling "
1658b31e1552SEric Sandeen 					"enabled");
1659ac27a0ecSDave Kleikamp 			return 0;
1660ac27a0ecSDave Kleikamp 		}
1661ac27a0ecSDave Kleikamp 	}
1662ac27a0ecSDave Kleikamp #endif
1663261cb20cSJan Kara 	if (test_opt(sb, DIOREAD_NOLOCK)) {
1664261cb20cSJan Kara 		int blocksize =
1665261cb20cSJan Kara 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
1666261cb20cSJan Kara 
1667261cb20cSJan Kara 		if (blocksize < PAGE_CACHE_SIZE) {
1668261cb20cSJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
1669261cb20cSJan Kara 				 "dioread_nolock if block size != PAGE_SIZE");
1670261cb20cSJan Kara 			return 0;
1671261cb20cSJan Kara 		}
1672261cb20cSJan Kara 	}
1673ac27a0ecSDave Kleikamp 	return 1;
1674ac27a0ecSDave Kleikamp }
1675ac27a0ecSDave Kleikamp 
16762adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
16772adf6da8STheodore Ts'o 					   struct super_block *sb)
16782adf6da8STheodore Ts'o {
16792adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
16802adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
16812adf6da8STheodore Ts'o 
16822adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
16832adf6da8STheodore Ts'o 		char *fmtname = "";
16842adf6da8STheodore Ts'o 
16852adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
16862adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
16872adf6da8STheodore Ts'o 			fmtname = "vfsold";
16882adf6da8STheodore Ts'o 			break;
16892adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
16902adf6da8STheodore Ts'o 			fmtname = "vfsv0";
16912adf6da8STheodore Ts'o 			break;
16922adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
16932adf6da8STheodore Ts'o 			fmtname = "vfsv1";
16942adf6da8STheodore Ts'o 			break;
16952adf6da8STheodore Ts'o 		}
16962adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
16972adf6da8STheodore Ts'o 	}
16982adf6da8STheodore Ts'o 
16992adf6da8STheodore Ts'o 	if (sbi->s_qf_names[USRQUOTA])
17002adf6da8STheodore Ts'o 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
17012adf6da8STheodore Ts'o 
17022adf6da8STheodore Ts'o 	if (sbi->s_qf_names[GRPQUOTA])
17032adf6da8STheodore Ts'o 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
17042adf6da8STheodore Ts'o 
17052adf6da8STheodore Ts'o 	if (test_opt(sb, USRQUOTA))
17062adf6da8STheodore Ts'o 		seq_puts(seq, ",usrquota");
17072adf6da8STheodore Ts'o 
17082adf6da8STheodore Ts'o 	if (test_opt(sb, GRPQUOTA))
17092adf6da8STheodore Ts'o 		seq_puts(seq, ",grpquota");
17102adf6da8STheodore Ts'o #endif
17112adf6da8STheodore Ts'o }
17122adf6da8STheodore Ts'o 
17135a916be1STheodore Ts'o static const char *token2str(int token)
17145a916be1STheodore Ts'o {
171550df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
17165a916be1STheodore Ts'o 
17175a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
17185a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
17195a916be1STheodore Ts'o 			break;
17205a916be1STheodore Ts'o 	return t->pattern;
17215a916be1STheodore Ts'o }
17225a916be1STheodore Ts'o 
17232adf6da8STheodore Ts'o /*
17242adf6da8STheodore Ts'o  * Show an option if
17252adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
17262adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
17272adf6da8STheodore Ts'o  */
172866acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
172966acdcf4STheodore Ts'o 			      int nodefs)
17302adf6da8STheodore Ts'o {
17312adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
17322adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
173366acdcf4STheodore Ts'o 	int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
17345a916be1STheodore Ts'o 	const struct mount_opts *m;
173566acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
17362adf6da8STheodore Ts'o 
173766acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
173866acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
17392adf6da8STheodore Ts'o 
17402adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
17415a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
17425a916be1STheodore Ts'o 
17435a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
17445a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
17455a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
17465a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
17475a916be1STheodore Ts'o 			continue;
174866acdcf4STheodore Ts'o 		if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
17495a916be1STheodore Ts'o 			continue; /* skip if same as the default */
17505a916be1STheodore Ts'o 		if ((want_set &&
17515a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
17525a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
17535a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
17545a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
17555a916be1STheodore Ts'o 	}
17565a916be1STheodore Ts'o 
175708cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
17585a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
175908cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
176008cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
176108cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
17625a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
176308cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
176408cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
176566acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
17665a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
17675a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
17682adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
17695a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
17702adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
17715a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
177266acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
17735a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
177466acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
17755a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
177666acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
17775a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
17782adf6da8STheodore Ts'o 	if (sb->s_flags & MS_I_VERSION)
17795a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
178066acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
17815a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
178266acdcf4STheodore Ts'o 	if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
17832adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
17845a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
17852adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
17865a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
17872adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
17885a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
17895a916be1STheodore Ts'o 	}
179066acdcf4STheodore Ts'o 	if (nodefs ||
179166acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
17925a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
17932adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
17942adf6da8STheodore Ts'o 
179566acdcf4STheodore Ts'o 	if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
179666acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
17975a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
1798df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
1799df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
18002adf6da8STheodore Ts'o 
18012adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
18022adf6da8STheodore Ts'o 	return 0;
18032adf6da8STheodore Ts'o }
18042adf6da8STheodore Ts'o 
180566acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
180666acdcf4STheodore Ts'o {
180766acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
180866acdcf4STheodore Ts'o }
180966acdcf4STheodore Ts'o 
181066acdcf4STheodore Ts'o static int options_seq_show(struct seq_file *seq, void *offset)
181166acdcf4STheodore Ts'o {
181266acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
181366acdcf4STheodore Ts'o 	int rc;
181466acdcf4STheodore Ts'o 
181566acdcf4STheodore Ts'o 	seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
181666acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
181766acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
181866acdcf4STheodore Ts'o 	return rc;
181966acdcf4STheodore Ts'o }
182066acdcf4STheodore Ts'o 
182166acdcf4STheodore Ts'o static int options_open_fs(struct inode *inode, struct file *file)
182266acdcf4STheodore Ts'o {
1823d9dda78bSAl Viro 	return single_open(file, options_seq_show, PDE_DATA(inode));
182466acdcf4STheodore Ts'o }
182566acdcf4STheodore Ts'o 
182666acdcf4STheodore Ts'o static const struct file_operations ext4_seq_options_fops = {
182766acdcf4STheodore Ts'o 	.owner = THIS_MODULE,
182866acdcf4STheodore Ts'o 	.open = options_open_fs,
182966acdcf4STheodore Ts'o 	.read = seq_read,
183066acdcf4STheodore Ts'o 	.llseek = seq_lseek,
183166acdcf4STheodore Ts'o 	.release = single_release,
183266acdcf4STheodore Ts'o };
183366acdcf4STheodore Ts'o 
1834617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1835ac27a0ecSDave Kleikamp 			    int read_only)
1836ac27a0ecSDave Kleikamp {
1837617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1838ac27a0ecSDave Kleikamp 	int res = 0;
1839ac27a0ecSDave Kleikamp 
1840617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1841b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1842b31e1552SEric Sandeen 			 "forcing read-only mode");
1843ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1844ac27a0ecSDave Kleikamp 	}
1845ac27a0ecSDave Kleikamp 	if (read_only)
1846281b5995STheodore Ts'o 		goto done;
1847617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1848b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1849b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1850617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1851b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1852b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1853b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1854ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1855ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1856ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1857b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1858b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1859b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1860ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1861ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1862ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1863b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1864b31e1552SEric Sandeen 			 "warning: checktime reached, "
1865b31e1552SEric Sandeen 			 "running e2fsck is recommended");
18660390131bSFrank Mayhar 	if (!sbi->s_journal)
1867216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1868ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1869617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1870e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1871ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1872617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
18730390131bSFrank Mayhar 	if (sbi->s_journal)
1874617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1875ac27a0ecSDave Kleikamp 
1876e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1877281b5995STheodore Ts'o done:
1878ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1879a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1880a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1881ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1882ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1883617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1884617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1885a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
1886ac27a0ecSDave Kleikamp 
18877abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
1888ac27a0ecSDave Kleikamp 	return res;
1889ac27a0ecSDave Kleikamp }
1890ac27a0ecSDave Kleikamp 
1891117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
1892117fff10STheodore Ts'o {
1893117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1894117fff10STheodore Ts'o 	struct flex_groups *new_groups;
1895117fff10STheodore Ts'o 	int size;
1896117fff10STheodore Ts'o 
1897117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
1898117fff10STheodore Ts'o 		return 0;
1899117fff10STheodore Ts'o 
1900117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
1901117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
1902117fff10STheodore Ts'o 		return 0;
1903117fff10STheodore Ts'o 
1904117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
1905117fff10STheodore Ts'o 	new_groups = ext4_kvzalloc(size, GFP_KERNEL);
1906117fff10STheodore Ts'o 	if (!new_groups) {
1907117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
1908117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
1909117fff10STheodore Ts'o 		return -ENOMEM;
1910117fff10STheodore Ts'o 	}
1911117fff10STheodore Ts'o 
1912117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
1913117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
1914117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
1915117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
1916117fff10STheodore Ts'o 		ext4_kvfree(sbi->s_flex_groups);
1917117fff10STheodore Ts'o 	}
1918117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
1919117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
1920117fff10STheodore Ts'o 	return 0;
1921117fff10STheodore Ts'o }
1922117fff10STheodore Ts'o 
1923772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1924772cb7c8SJose R. Santos {
1925772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1926772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1927772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1928117fff10STheodore Ts'o 	int i, err;
1929772cb7c8SJose R. Santos 
1930503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1931d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
1932772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1933772cb7c8SJose R. Santos 		return 1;
1934772cb7c8SJose R. Santos 	}
1935772cb7c8SJose R. Santos 
1936117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
1937117fff10STheodore Ts'o 	if (err)
1938772cb7c8SJose R. Santos 		goto failed;
1939772cb7c8SJose R. Santos 
1940772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
194188b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1942772cb7c8SJose R. Santos 
1943772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
19447ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
19457ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
194690ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
194724aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
19487ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
19497ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
1950772cb7c8SJose R. Santos 	}
1951772cb7c8SJose R. Santos 
1952772cb7c8SJose R. Santos 	return 1;
1953772cb7c8SJose R. Santos failed:
1954772cb7c8SJose R. Santos 	return 0;
1955772cb7c8SJose R. Santos }
1956772cb7c8SJose R. Santos 
1957feb0ab32SDarrick J. Wong static __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1958717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
1959717d50e4SAndreas Dilger {
1960feb0ab32SDarrick J. Wong 	int offset;
1961717d50e4SAndreas Dilger 	__u16 crc = 0;
1962717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
1963717d50e4SAndreas Dilger 
1964feb0ab32SDarrick J. Wong 	if ((sbi->s_es->s_feature_ro_compat &
1965feb0ab32SDarrick J. Wong 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))) {
1966feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
1967d6a77105STheodore Ts'o 		__le16 save_csum;
1968feb0ab32SDarrick J. Wong 		__u32 csum32;
1969feb0ab32SDarrick J. Wong 
1970d6a77105STheodore Ts'o 		save_csum = gdp->bg_checksum;
1971feb0ab32SDarrick J. Wong 		gdp->bg_checksum = 0;
1972feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
1973feb0ab32SDarrick J. Wong 				     sizeof(le_group));
1974feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp,
1975feb0ab32SDarrick J. Wong 				     sbi->s_desc_size);
1976d6a77105STheodore Ts'o 		gdp->bg_checksum = save_csum;
1977feb0ab32SDarrick J. Wong 
1978feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
1979feb0ab32SDarrick J. Wong 		goto out;
1980feb0ab32SDarrick J. Wong 	}
1981feb0ab32SDarrick J. Wong 
1982feb0ab32SDarrick J. Wong 	/* old crc16 code */
1983feb0ab32SDarrick J. Wong 	offset = offsetof(struct ext4_group_desc, bg_checksum);
1984feb0ab32SDarrick J. Wong 
1985717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1986717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1987717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
1988717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
1989717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
1990717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_incompat &
1991717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1992717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1993717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
1994717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
1995717d50e4SAndreas Dilger 				offset);
1996717d50e4SAndreas Dilger 
1997feb0ab32SDarrick J. Wong out:
1998717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1999717d50e4SAndreas Dilger }
2000717d50e4SAndreas Dilger 
2001feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2002717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2003717d50e4SAndreas Dilger {
2004feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2005feb0ab32SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(EXT4_SB(sb),
2006feb0ab32SDarrick J. Wong 						      block_group, gdp)))
2007717d50e4SAndreas Dilger 		return 0;
2008717d50e4SAndreas Dilger 
2009717d50e4SAndreas Dilger 	return 1;
2010717d50e4SAndreas Dilger }
2011717d50e4SAndreas Dilger 
2012feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2013feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2014feb0ab32SDarrick J. Wong {
2015feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2016feb0ab32SDarrick J. Wong 		return;
2017feb0ab32SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(EXT4_SB(sb), block_group, gdp);
2018feb0ab32SDarrick J. Wong }
2019feb0ab32SDarrick J. Wong 
2020ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2021bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2022bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2023ac27a0ecSDave Kleikamp {
2024617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2025617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2026617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2027bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2028bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2029bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2030ce421581SJose R. Santos 	int flexbg_flag = 0;
2031bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2032ac27a0ecSDave Kleikamp 
2033ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2034ce421581SJose R. Santos 		flexbg_flag = 1;
2035ce421581SJose R. Santos 
2036617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2037ac27a0ecSDave Kleikamp 
2038197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2039197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2040197cd65aSAkinobu Mita 
2041ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2042bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2043ac27a0ecSDave Kleikamp 		else
2044ac27a0ecSDave Kleikamp 			last_block = first_block +
2045617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2046ac27a0ecSDave Kleikamp 
2047bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2048bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2049bfff6873SLukas Czerner 			grp = i;
2050bfff6873SLukas Czerner 
20518fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
20522b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2053b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2054a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2055b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2056ac27a0ecSDave Kleikamp 			return 0;
2057ac27a0ecSDave Kleikamp 		}
20588fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
20592b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2060b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2061a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2062b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2063ac27a0ecSDave Kleikamp 			return 0;
2064ac27a0ecSDave Kleikamp 		}
20658fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2066bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
20672b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2068b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2069a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2070b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2071ac27a0ecSDave Kleikamp 			return 0;
2072ac27a0ecSDave Kleikamp 		}
2073955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2074feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2075b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2076b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2077fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2078fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
20797ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2080955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2081717d50e4SAndreas Dilger 				return 0;
2082717d50e4SAndreas Dilger 			}
20837ee1ec4cSLi Zefan 		}
2084955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2085ce421581SJose R. Santos 		if (!flexbg_flag)
2086617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2087ac27a0ecSDave Kleikamp 	}
2088bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2089bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2090ac27a0ecSDave Kleikamp 
20915dee5437STheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
20925dee5437STheodore Ts'o 				   EXT4_C2B(sbi, ext4_count_free_clusters(sb)));
2093617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
2094ac27a0ecSDave Kleikamp 	return 1;
2095ac27a0ecSDave Kleikamp }
2096ac27a0ecSDave Kleikamp 
2097617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2098ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2099ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2100ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2101ac27a0ecSDave Kleikamp  *
2102ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2103ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2104ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2105ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2106ac27a0ecSDave Kleikamp  *
2107ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2108ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2109ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2110617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2111ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2112ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2113ac27a0ecSDave Kleikamp  */
2114617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2115617ba13bSMingming Cao 				struct ext4_super_block *es)
2116ac27a0ecSDave Kleikamp {
2117ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
2118ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
2119ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2120ac27a0ecSDave Kleikamp 	int i;
2121ac27a0ecSDave Kleikamp #endif
2122ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2123ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2124ac27a0ecSDave Kleikamp 		return;
2125ac27a0ecSDave Kleikamp 	}
2126ac27a0ecSDave Kleikamp 
2127a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2128b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2129b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2130a8f48a95SEric Sandeen 		return;
2131a8f48a95SEric Sandeen 	}
2132a8f48a95SEric Sandeen 
2133d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2134d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2135d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2136d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2137d39195c3SAmir Goldstein 		return;
2138d39195c3SAmir Goldstein 	}
2139d39195c3SAmir Goldstein 
2140617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2141c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
2142c25f9bc6SEric Sandeen 		if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
2143ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
2144ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2145ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2146c25f9bc6SEric Sandeen 		}
2147ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2148ac27a0ecSDave Kleikamp 		return;
2149ac27a0ecSDave Kleikamp 	}
2150ac27a0ecSDave Kleikamp 
2151ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2152b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2153ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2154ac27a0ecSDave Kleikamp 	}
2155ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2156ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2157ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2158ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2159ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2160617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2161617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2162ac27a0ecSDave Kleikamp 			if (ret < 0)
2163b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2164b31e1552SEric Sandeen 					"Cannot turn on journaled "
2165b31e1552SEric Sandeen 					"quota: error %d", ret);
2166ac27a0ecSDave Kleikamp 		}
2167ac27a0ecSDave Kleikamp 	}
2168ac27a0ecSDave Kleikamp #endif
2169ac27a0ecSDave Kleikamp 
2170ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2171ac27a0ecSDave Kleikamp 		struct inode *inode;
2172ac27a0ecSDave Kleikamp 
217397bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
217497bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2175ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2176ac27a0ecSDave Kleikamp 			break;
2177ac27a0ecSDave Kleikamp 		}
2178ac27a0ecSDave Kleikamp 
2179617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2180871a2931SChristoph Hellwig 		dquot_initialize(inode);
2181ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2182566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2183b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2184b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
218546e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2186e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2187ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2188721e3ebaSTheodore Ts'o 			mutex_lock(&inode->i_mutex);
218955f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
2190617ba13bSMingming Cao 			ext4_truncate(inode);
2191721e3ebaSTheodore Ts'o 			mutex_unlock(&inode->i_mutex);
2192ac27a0ecSDave Kleikamp 			nr_truncates++;
2193ac27a0ecSDave Kleikamp 		} else {
2194566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2195b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2196b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
219746e665e9SHarvey Harrison 					__func__, inode->i_ino);
2198ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2199ac27a0ecSDave Kleikamp 				  inode->i_ino);
2200ac27a0ecSDave Kleikamp 			nr_orphans++;
2201ac27a0ecSDave Kleikamp 		}
2202ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2203ac27a0ecSDave Kleikamp 	}
2204ac27a0ecSDave Kleikamp 
2205ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2206ac27a0ecSDave Kleikamp 
2207ac27a0ecSDave Kleikamp 	if (nr_orphans)
2208b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2209b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2210ac27a0ecSDave Kleikamp 	if (nr_truncates)
2211b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2212b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2213ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2214ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2215ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2216ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2217287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2218ac27a0ecSDave Kleikamp 	}
2219ac27a0ecSDave Kleikamp #endif
2220ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2221ac27a0ecSDave Kleikamp }
22220b8e58a1SAndreas Dilger 
2223cd2291a4SEric Sandeen /*
2224cd2291a4SEric Sandeen  * Maximal extent format file size.
2225cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2226cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2227cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2228cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2229cd2291a4SEric Sandeen  *
2230f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2231f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2232f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2233f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2234f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2235f17722f9SLukas Czerner  *
2236cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2237cd2291a4SEric Sandeen  */
2238f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2239cd2291a4SEric Sandeen {
2240cd2291a4SEric Sandeen 	loff_t res;
2241cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2242cd2291a4SEric Sandeen 
2243cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2244f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2245cd2291a4SEric Sandeen 		/*
224690c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2247cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2248cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2249cd2291a4SEric Sandeen 		 */
2250cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2251cd2291a4SEric Sandeen 
2252cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2253cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2254cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2255cd2291a4SEric Sandeen 	}
2256cd2291a4SEric Sandeen 
2257f17722f9SLukas Czerner 	/*
2258f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2259f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2260f17722f9SLukas Czerner 	 * size
2261f17722f9SLukas Czerner 	 */
2262f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2263cd2291a4SEric Sandeen 	res <<= blkbits;
2264cd2291a4SEric Sandeen 
2265cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2266cd2291a4SEric Sandeen 	if (res > upper_limit)
2267cd2291a4SEric Sandeen 		res = upper_limit;
2268cd2291a4SEric Sandeen 
2269cd2291a4SEric Sandeen 	return res;
2270cd2291a4SEric Sandeen }
2271ac27a0ecSDave Kleikamp 
2272ac27a0ecSDave Kleikamp /*
2273cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
22740fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
22750fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2276ac27a0ecSDave Kleikamp  */
2277f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2278ac27a0ecSDave Kleikamp {
2279617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
22800fc1b451SAneesh Kumar K.V 	int meta_blocks;
22810fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
22820b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
22830b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
22840b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
22850b8e58a1SAndreas Dilger 	 *
22860b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
22870b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
22880fc1b451SAneesh Kumar K.V 	 */
22890fc1b451SAneesh Kumar K.V 
2290f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
22910fc1b451SAneesh Kumar K.V 		/*
229290c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
22930b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
22940b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
22950fc1b451SAneesh Kumar K.V 		 */
22960fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
22970fc1b451SAneesh Kumar K.V 
22980fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
22990fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
23000fc1b451SAneesh Kumar K.V 
23010fc1b451SAneesh Kumar K.V 	} else {
23028180a562SAneesh Kumar K.V 		/*
23038180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
23048180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
23058180a562SAneesh Kumar K.V 		 * represent total number of blocks in
23068180a562SAneesh Kumar K.V 		 * file system block size
23078180a562SAneesh Kumar K.V 		 */
23080fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
23090fc1b451SAneesh Kumar K.V 
23100fc1b451SAneesh Kumar K.V 	}
23110fc1b451SAneesh Kumar K.V 
23120fc1b451SAneesh Kumar K.V 	/* indirect blocks */
23130fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
23140fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
23150fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
23160fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
23170fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
23180fc1b451SAneesh Kumar K.V 
23190fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
23200fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2321ac27a0ecSDave Kleikamp 
2322ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2323ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2324ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2325ac27a0ecSDave Kleikamp 	res <<= bits;
2326ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2327ac27a0ecSDave Kleikamp 		res = upper_limit;
23280fc1b451SAneesh Kumar K.V 
23290fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
23300fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
23310fc1b451SAneesh Kumar K.V 
2332ac27a0ecSDave Kleikamp 	return res;
2333ac27a0ecSDave Kleikamp }
2334ac27a0ecSDave Kleikamp 
2335617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
233670bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2337ac27a0ecSDave Kleikamp {
2338617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2339fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2340ac27a0ecSDave Kleikamp 	int has_super = 0;
2341ac27a0ecSDave Kleikamp 
2342ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2343ac27a0ecSDave Kleikamp 
2344617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2345ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
234670bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2347ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2348617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2349ac27a0ecSDave Kleikamp 		has_super = 1;
23500b8e58a1SAndreas Dilger 
2351617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2352ac27a0ecSDave Kleikamp }
2353ac27a0ecSDave Kleikamp 
2354c9de560dSAlex Tomas /**
2355c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2356c9de560dSAlex Tomas  * @sbi: In memory super block info
2357c9de560dSAlex Tomas  *
2358c9de560dSAlex Tomas  * If we have specified it via mount option, then
2359c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2360c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2361c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2362c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2363c9de560dSAlex Tomas  *
2364c9de560dSAlex Tomas  */
2365c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2366c9de560dSAlex Tomas {
2367c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2368c9de560dSAlex Tomas 	unsigned long stripe_width =
2369c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
23703eb08658SDan Ehrenberg 	int ret;
2371c9de560dSAlex Tomas 
2372c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
23733eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
23743eb08658SDan Ehrenberg 	else if (stripe_width <= sbi->s_blocks_per_group)
23753eb08658SDan Ehrenberg 		ret = stripe_width;
23763eb08658SDan Ehrenberg 	else if (stride <= sbi->s_blocks_per_group)
23773eb08658SDan Ehrenberg 		ret = stride;
23783eb08658SDan Ehrenberg 	else
23793eb08658SDan Ehrenberg 		ret = 0;
2380c9de560dSAlex Tomas 
23813eb08658SDan Ehrenberg 	/*
23823eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
23833eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
23843eb08658SDan Ehrenberg 	 */
23853eb08658SDan Ehrenberg 	if (ret <= 1)
23863eb08658SDan Ehrenberg 		ret = 0;
2387c9de560dSAlex Tomas 
23883eb08658SDan Ehrenberg 	return ret;
2389c9de560dSAlex Tomas }
2390ac27a0ecSDave Kleikamp 
23913197ebdbSTheodore Ts'o /* sysfs supprt */
23923197ebdbSTheodore Ts'o 
23933197ebdbSTheodore Ts'o struct ext4_attr {
23943197ebdbSTheodore Ts'o 	struct attribute attr;
23953197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
23963197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
23973197ebdbSTheodore Ts'o 			 const char *, size_t);
2398f2d50a65SJan Kara 	union {
23993197ebdbSTheodore Ts'o 		int offset;
2400f2d50a65SJan Kara 		int deprecated_val;
2401f2d50a65SJan Kara 	} u;
24023197ebdbSTheodore Ts'o };
24033197ebdbSTheodore Ts'o 
240427dd4385SLukas Czerner static int parse_strtoull(const char *buf,
240527dd4385SLukas Czerner 		unsigned long long max, unsigned long long *value)
240627dd4385SLukas Czerner {
240727dd4385SLukas Czerner 	int ret;
240827dd4385SLukas Czerner 
240927dd4385SLukas Czerner 	ret = kstrtoull(skip_spaces(buf), 0, value);
241027dd4385SLukas Czerner 	if (!ret && *value > max)
241127dd4385SLukas Czerner 		ret = -EINVAL;
241227dd4385SLukas Czerner 	return ret;
241327dd4385SLukas Czerner }
241427dd4385SLukas Czerner 
24153197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
24163197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
24173197ebdbSTheodore Ts'o 					      char *buf)
24183197ebdbSTheodore Ts'o {
24193197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
24207b415bf6SAditya Kali 		(s64) EXT4_C2B(sbi,
24217b415bf6SAditya Kali 			percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
24223197ebdbSTheodore Ts'o }
24233197ebdbSTheodore Ts'o 
24243197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
24253197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
24263197ebdbSTheodore Ts'o {
24273197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24283197ebdbSTheodore Ts'o 
2429f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2430f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24313197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
24323197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
24333197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
24343197ebdbSTheodore Ts'o }
24353197ebdbSTheodore Ts'o 
24363197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
24373197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
24383197ebdbSTheodore Ts'o {
24393197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24403197ebdbSTheodore Ts'o 
2441f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2442f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24433197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2444a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
24453197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2446a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
24473197ebdbSTheodore Ts'o }
24483197ebdbSTheodore Ts'o 
24493197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
24503197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
24513197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
24523197ebdbSTheodore Ts'o {
24533197ebdbSTheodore Ts'o 	unsigned long t;
2454e1091b15SLukas Czerner 	int ret;
24553197ebdbSTheodore Ts'o 
2456e1091b15SLukas Czerner 	ret = kstrtoul(skip_spaces(buf), 0, &t);
2457e1091b15SLukas Czerner 	if (ret)
2458e1091b15SLukas Czerner 		return ret;
24593197ebdbSTheodore Ts'o 
2460e1091b15SLukas Czerner 	if (t && (!is_power_of_2(t) || t > 0x40000000))
24613197ebdbSTheodore Ts'o 		return -EINVAL;
24623197ebdbSTheodore Ts'o 
24633197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
24643197ebdbSTheodore Ts'o 	return count;
24653197ebdbSTheodore Ts'o }
24663197ebdbSTheodore Ts'o 
24673197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
24683197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
24693197ebdbSTheodore Ts'o {
2470f2d50a65SJan Kara 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
24713197ebdbSTheodore Ts'o 
24723197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
24733197ebdbSTheodore Ts'o }
24743197ebdbSTheodore Ts'o 
24753197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
24763197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
24773197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
24783197ebdbSTheodore Ts'o {
2479f2d50a65SJan Kara 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
24803197ebdbSTheodore Ts'o 	unsigned long t;
2481e1091b15SLukas Czerner 	int ret;
24823197ebdbSTheodore Ts'o 
2483e1091b15SLukas Czerner 	ret = kstrtoul(skip_spaces(buf), 0, &t);
2484e1091b15SLukas Czerner 	if (ret)
2485e1091b15SLukas Czerner 		return ret;
24863197ebdbSTheodore Ts'o 	*ui = t;
24873197ebdbSTheodore Ts'o 	return count;
24883197ebdbSTheodore Ts'o }
24893197ebdbSTheodore Ts'o 
249027dd4385SLukas Czerner static ssize_t reserved_clusters_show(struct ext4_attr *a,
249127dd4385SLukas Czerner 				  struct ext4_sb_info *sbi, char *buf)
249227dd4385SLukas Czerner {
249327dd4385SLukas Czerner 	return snprintf(buf, PAGE_SIZE, "%llu\n",
249427dd4385SLukas Czerner 		(unsigned long long) atomic64_read(&sbi->s_resv_clusters));
249527dd4385SLukas Czerner }
249627dd4385SLukas Czerner 
249727dd4385SLukas Czerner static ssize_t reserved_clusters_store(struct ext4_attr *a,
249827dd4385SLukas Czerner 				   struct ext4_sb_info *sbi,
249927dd4385SLukas Czerner 				   const char *buf, size_t count)
250027dd4385SLukas Czerner {
250127dd4385SLukas Czerner 	unsigned long long val;
250227dd4385SLukas Czerner 	int ret;
250327dd4385SLukas Czerner 
250427dd4385SLukas Czerner 	if (parse_strtoull(buf, -1ULL, &val))
250527dd4385SLukas Czerner 		return -EINVAL;
250627dd4385SLukas Czerner 	ret = ext4_reserve_clusters(sbi, val);
250727dd4385SLukas Czerner 
250827dd4385SLukas Czerner 	return ret ? ret : count;
250927dd4385SLukas Czerner }
251027dd4385SLukas Czerner 
25112c0544b2STheodore Ts'o static ssize_t trigger_test_error(struct ext4_attr *a,
25122c0544b2STheodore Ts'o 				  struct ext4_sb_info *sbi,
25132c0544b2STheodore Ts'o 				  const char *buf, size_t count)
25142c0544b2STheodore Ts'o {
25152c0544b2STheodore Ts'o 	int len = count;
25162c0544b2STheodore Ts'o 
25172c0544b2STheodore Ts'o 	if (!capable(CAP_SYS_ADMIN))
25182c0544b2STheodore Ts'o 		return -EPERM;
25192c0544b2STheodore Ts'o 
25202c0544b2STheodore Ts'o 	if (len && buf[len-1] == '\n')
25212c0544b2STheodore Ts'o 		len--;
25222c0544b2STheodore Ts'o 
25232c0544b2STheodore Ts'o 	if (len)
25242c0544b2STheodore Ts'o 		ext4_error(sbi->s_sb, "%.*s", len, buf);
25252c0544b2STheodore Ts'o 	return count;
25262c0544b2STheodore Ts'o }
25272c0544b2STheodore Ts'o 
2528f2d50a65SJan Kara static ssize_t sbi_deprecated_show(struct ext4_attr *a,
2529f2d50a65SJan Kara 				   struct ext4_sb_info *sbi, char *buf)
2530f2d50a65SJan Kara {
2531f2d50a65SJan Kara 	return snprintf(buf, PAGE_SIZE, "%d\n", a->u.deprecated_val);
2532f2d50a65SJan Kara }
2533f2d50a65SJan Kara 
25343197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
25353197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
25363197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
25373197ebdbSTheodore Ts'o 	.show	= _show,					\
25383197ebdbSTheodore Ts'o 	.store	= _store,					\
2539f2d50a65SJan Kara 	.u = {							\
25403197ebdbSTheodore Ts'o 		.offset = offsetof(struct ext4_sb_info, _elname),\
2541f2d50a65SJan Kara 	},							\
25423197ebdbSTheodore Ts'o }
25433197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
25443197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
25453197ebdbSTheodore Ts'o 
2546857ac889SLukas Czerner #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
25473197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
25483197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
25493197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
25503197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
25513197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
2552f2d50a65SJan Kara #define EXT4_DEPRECATED_ATTR(_name, _val)	\
2553f2d50a65SJan Kara static struct ext4_attr ext4_attr_##_name = {			\
2554f2d50a65SJan Kara 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
2555f2d50a65SJan Kara 	.show	= sbi_deprecated_show,				\
2556f2d50a65SJan Kara 	.u = {							\
2557f2d50a65SJan Kara 		.deprecated_val = _val,				\
2558f2d50a65SJan Kara 	},							\
2559f2d50a65SJan Kara }
25603197ebdbSTheodore Ts'o 
25613197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
25623197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
25633197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
256427dd4385SLukas Czerner EXT4_RW_ATTR(reserved_clusters);
25653197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
25663197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
256711013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
25683197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
25693197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
25703197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
25713197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
25723197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
25733197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2574f2d50a65SJan Kara EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
257567a5da56SZheng Liu EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
25762c0544b2STheodore Ts'o EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
25773197ebdbSTheodore Ts'o 
25783197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
25793197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
25803197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
25813197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
258227dd4385SLukas Czerner 	ATTR_LIST(reserved_clusters),
25833197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
258411013911SAndreas Dilger 	ATTR_LIST(inode_goal),
25853197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
25863197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
25873197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
25883197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
25893197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
25903197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
259155138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
259267a5da56SZheng Liu 	ATTR_LIST(extent_max_zeroout_kb),
25932c0544b2STheodore Ts'o 	ATTR_LIST(trigger_fs_error),
25943197ebdbSTheodore Ts'o 	NULL,
25953197ebdbSTheodore Ts'o };
25963197ebdbSTheodore Ts'o 
2597857ac889SLukas Czerner /* Features this copy of ext4 supports */
2598857ac889SLukas Czerner EXT4_INFO_ATTR(lazy_itable_init);
259927ee40dfSLukas Czerner EXT4_INFO_ATTR(batched_discard);
26005e7bbef1STheodore Ts'o EXT4_INFO_ATTR(meta_bg_resize);
2601857ac889SLukas Czerner 
2602857ac889SLukas Czerner static struct attribute *ext4_feat_attrs[] = {
2603857ac889SLukas Czerner 	ATTR_LIST(lazy_itable_init),
260427ee40dfSLukas Czerner 	ATTR_LIST(batched_discard),
26055e7bbef1STheodore Ts'o 	ATTR_LIST(meta_bg_resize),
2606857ac889SLukas Czerner 	NULL,
2607857ac889SLukas Czerner };
2608857ac889SLukas Czerner 
26093197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
26103197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
26113197ebdbSTheodore Ts'o {
26123197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26133197ebdbSTheodore Ts'o 						s_kobj);
26143197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
26153197ebdbSTheodore Ts'o 
26163197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
26173197ebdbSTheodore Ts'o }
26183197ebdbSTheodore Ts'o 
26193197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
26203197ebdbSTheodore Ts'o 			       struct attribute *attr,
26213197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
26223197ebdbSTheodore Ts'o {
26233197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26243197ebdbSTheodore Ts'o 						s_kobj);
26253197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
26263197ebdbSTheodore Ts'o 
26273197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
26283197ebdbSTheodore Ts'o }
26293197ebdbSTheodore Ts'o 
26303197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
26313197ebdbSTheodore Ts'o {
26323197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26333197ebdbSTheodore Ts'o 						s_kobj);
26343197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
26353197ebdbSTheodore Ts'o }
26363197ebdbSTheodore Ts'o 
263752cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = {
26383197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
26393197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
26403197ebdbSTheodore Ts'o };
26413197ebdbSTheodore Ts'o 
26423197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
26433197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
26443197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
26453197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
26463197ebdbSTheodore Ts'o };
26473197ebdbSTheodore Ts'o 
2648857ac889SLukas Czerner static void ext4_feat_release(struct kobject *kobj)
2649857ac889SLukas Czerner {
2650857ac889SLukas Czerner 	complete(&ext4_feat->f_kobj_unregister);
2651857ac889SLukas Czerner }
2652857ac889SLukas Czerner 
2653857ac889SLukas Czerner static struct kobj_type ext4_feat_ktype = {
2654857ac889SLukas Czerner 	.default_attrs	= ext4_feat_attrs,
2655857ac889SLukas Czerner 	.sysfs_ops	= &ext4_attr_ops,
2656857ac889SLukas Czerner 	.release	= ext4_feat_release,
2657857ac889SLukas Czerner };
2658857ac889SLukas Czerner 
2659a13fb1a4SEric Sandeen /*
2660a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2661a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2662a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2663a13fb1a4SEric Sandeen  * 0 if it cannot be.
2664a13fb1a4SEric Sandeen  */
2665a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2666a13fb1a4SEric Sandeen {
2667a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2668a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2669a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2670a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2671a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2672a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2673a13fb1a4SEric Sandeen 		return 0;
2674a13fb1a4SEric Sandeen 	}
2675a13fb1a4SEric Sandeen 
2676a13fb1a4SEric Sandeen 	if (readonly)
2677a13fb1a4SEric Sandeen 		return 1;
2678a13fb1a4SEric Sandeen 
2679a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2680a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2681a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2682a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2683a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2684a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2685a13fb1a4SEric Sandeen 		return 0;
2686a13fb1a4SEric Sandeen 	}
2687a13fb1a4SEric Sandeen 	/*
2688a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2689a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2690a13fb1a4SEric Sandeen 	 */
2691a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2692a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2693a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2694a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2695a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2696a13fb1a4SEric Sandeen 			return 0;
2697a13fb1a4SEric Sandeen 		}
2698a13fb1a4SEric Sandeen 	}
2699bab08ab9STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2700bab08ab9STheodore Ts'o 	    !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2701bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2702bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2703bab08ab9STheodore Ts'o 			 "extents feature\n");
2704bab08ab9STheodore Ts'o 		return 0;
2705bab08ab9STheodore Ts'o 	}
27067c319d32SAditya Kali 
27077c319d32SAditya Kali #ifndef CONFIG_QUOTA
27087c319d32SAditya Kali 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
27097c319d32SAditya Kali 	    !readonly) {
27107c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
27117c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
27127c319d32SAditya Kali 			 "without CONFIG_QUOTA");
27137c319d32SAditya Kali 		return 0;
27147c319d32SAditya Kali 	}
27157c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2716a13fb1a4SEric Sandeen 	return 1;
2717a13fb1a4SEric Sandeen }
2718a13fb1a4SEric Sandeen 
271966e61a9eSTheodore Ts'o /*
272066e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
272166e61a9eSTheodore Ts'o  * on the file system
272266e61a9eSTheodore Ts'o  */
272366e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
272466e61a9eSTheodore Ts'o {
272566e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
272666e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
272766e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
272866e61a9eSTheodore Ts'o 
272966e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
273066e61a9eSTheodore Ts'o 	es = sbi->s_es;
273166e61a9eSTheodore Ts'o 
273266e61a9eSTheodore Ts'o 	if (es->s_error_count)
273366e61a9eSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count: %u",
273466e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
273566e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
273666e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
273766e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
273866e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
273966e61a9eSTheodore Ts'o 		       es->s_first_error_func,
274066e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
274166e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
274266e61a9eSTheodore Ts'o 			printk(": inode %u",
274366e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
274466e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
274566e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
274666e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
274766e61a9eSTheodore Ts'o 		printk("\n");
274866e61a9eSTheodore Ts'o 	}
274966e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
275066e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
275166e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
275266e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
275366e61a9eSTheodore Ts'o 		       es->s_last_error_func,
275466e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
275566e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
275666e61a9eSTheodore Ts'o 			printk(": inode %u",
275766e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
275866e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
275966e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
276066e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
276166e61a9eSTheodore Ts'o 		printk("\n");
276266e61a9eSTheodore Ts'o 	}
276366e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
276466e61a9eSTheodore Ts'o }
276566e61a9eSTheodore Ts'o 
2766bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2767bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2768bfff6873SLukas Czerner {
2769bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2770bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2771bfff6873SLukas Czerner 	struct super_block *sb;
2772bfff6873SLukas Czerner 	unsigned long timeout = 0;
2773bfff6873SLukas Czerner 	int ret = 0;
2774bfff6873SLukas Czerner 
2775bfff6873SLukas Czerner 	sb = elr->lr_super;
2776bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2777bfff6873SLukas Czerner 
27788e8ad8a5SJan Kara 	sb_start_write(sb);
2779bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2780bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2781bfff6873SLukas Czerner 		if (!gdp) {
2782bfff6873SLukas Czerner 			ret = 1;
2783bfff6873SLukas Czerner 			break;
2784bfff6873SLukas Czerner 		}
2785bfff6873SLukas Czerner 
2786bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2787bfff6873SLukas Czerner 			break;
2788bfff6873SLukas Czerner 	}
2789bfff6873SLukas Czerner 
27907f511862STheodore Ts'o 	if (group >= ngroups)
2791bfff6873SLukas Czerner 		ret = 1;
2792bfff6873SLukas Czerner 
2793bfff6873SLukas Czerner 	if (!ret) {
2794bfff6873SLukas Czerner 		timeout = jiffies;
2795bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2796bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2797bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
279851ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
279951ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2800bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2801bfff6873SLukas Czerner 		}
2802bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2803bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2804bfff6873SLukas Czerner 	}
28058e8ad8a5SJan Kara 	sb_end_write(sb);
2806bfff6873SLukas Czerner 
2807bfff6873SLukas Czerner 	return ret;
2808bfff6873SLukas Czerner }
2809bfff6873SLukas Czerner 
2810bfff6873SLukas Czerner /*
2811bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
28124ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2813bfff6873SLukas Czerner  */
2814bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2815bfff6873SLukas Czerner {
2816bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2817bfff6873SLukas Czerner 
2818bfff6873SLukas Czerner 	if (!elr)
2819bfff6873SLukas Czerner 		return;
2820bfff6873SLukas Czerner 
2821bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2822bfff6873SLukas Czerner 
2823bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2824bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2825bfff6873SLukas Czerner 	kfree(elr);
2826bfff6873SLukas Czerner }
2827bfff6873SLukas Czerner 
2828bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2829bfff6873SLukas Czerner {
28301bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
28311bb933fbSLukas Czerner 	if (!ext4_li_info) {
28321bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2833bfff6873SLukas Czerner 		return;
28341bb933fbSLukas Czerner 	}
2835bfff6873SLukas Czerner 
2836bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
28371bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2838bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
28391bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2840bfff6873SLukas Czerner }
2841bfff6873SLukas Czerner 
28428f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
28438f1f7453SEric Sandeen 
2844bfff6873SLukas Czerner /*
2845bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2846bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2847bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2848bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2849bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2850bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2851bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2852bfff6873SLukas Czerner  */
2853bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2854bfff6873SLukas Czerner {
2855bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2856bfff6873SLukas Czerner 	struct list_head *pos, *n;
2857bfff6873SLukas Czerner 	struct ext4_li_request *elr;
28584ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2859bfff6873SLukas Czerner 
2860bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2861bfff6873SLukas Czerner 
2862bfff6873SLukas Czerner cont_thread:
2863bfff6873SLukas Czerner 	while (true) {
2864bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2865bfff6873SLukas Czerner 
2866bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2867bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2868bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2869bfff6873SLukas Czerner 			goto exit_thread;
2870bfff6873SLukas Czerner 		}
2871bfff6873SLukas Czerner 
2872bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2873bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2874bfff6873SLukas Czerner 					 lr_request);
2875bfff6873SLukas Czerner 
2876b2c78cd0STheodore Ts'o 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
2877b2c78cd0STheodore Ts'o 				if (ext4_run_li_request(elr) != 0) {
2878b2c78cd0STheodore Ts'o 					/* error, remove the lazy_init job */
2879bfff6873SLukas Czerner 					ext4_remove_li_request(elr);
2880bfff6873SLukas Czerner 					continue;
2881bfff6873SLukas Czerner 				}
2882b2c78cd0STheodore Ts'o 			}
2883bfff6873SLukas Czerner 
2884bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2885bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2886bfff6873SLukas Czerner 		}
2887bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2888bfff6873SLukas Czerner 
2889a0acae0eSTejun Heo 		try_to_freeze();
2890bfff6873SLukas Czerner 
28914ed5c033SLukas Czerner 		cur = jiffies;
28924ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2893f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2894bfff6873SLukas Czerner 			cond_resched();
2895bfff6873SLukas Czerner 			continue;
2896bfff6873SLukas Czerner 		}
2897bfff6873SLukas Czerner 
28984ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
28994ed5c033SLukas Czerner 
29008f1f7453SEric Sandeen 		if (kthread_should_stop()) {
29018f1f7453SEric Sandeen 			ext4_clear_request_list();
29028f1f7453SEric Sandeen 			goto exit_thread;
29038f1f7453SEric Sandeen 		}
2904bfff6873SLukas Czerner 	}
2905bfff6873SLukas Czerner 
2906bfff6873SLukas Czerner exit_thread:
2907bfff6873SLukas Czerner 	/*
2908bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
2909bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
2910bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
2911bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
2912bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
2913bfff6873SLukas Czerner 	 * new one.
2914bfff6873SLukas Czerner 	 */
2915bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2916bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
2917bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
2918bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2919bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2920bfff6873SLukas Czerner 		goto cont_thread;
2921bfff6873SLukas Czerner 	}
2922bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
2923bfff6873SLukas Czerner 	kfree(ext4_li_info);
2924bfff6873SLukas Czerner 	ext4_li_info = NULL;
2925bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2926bfff6873SLukas Czerner 
2927bfff6873SLukas Czerner 	return 0;
2928bfff6873SLukas Czerner }
2929bfff6873SLukas Czerner 
2930bfff6873SLukas Czerner static void ext4_clear_request_list(void)
2931bfff6873SLukas Czerner {
2932bfff6873SLukas Czerner 	struct list_head *pos, *n;
2933bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2934bfff6873SLukas Czerner 
2935bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2936bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2937bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
2938bfff6873SLukas Czerner 				 lr_request);
2939bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
2940bfff6873SLukas Czerner 	}
2941bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2942bfff6873SLukas Czerner }
2943bfff6873SLukas Czerner 
2944bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
2945bfff6873SLukas Czerner {
29468f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
29478f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
29488f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
29498f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
2950bfff6873SLukas Czerner 		ext4_clear_request_list();
2951bfff6873SLukas Czerner 		kfree(ext4_li_info);
2952bfff6873SLukas Czerner 		ext4_li_info = NULL;
295392b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
2954bfff6873SLukas Czerner 				 "initialization thread\n",
2955bfff6873SLukas Czerner 				 err);
2956bfff6873SLukas Czerner 		return err;
2957bfff6873SLukas Czerner 	}
2958bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
2959bfff6873SLukas Czerner 	return 0;
2960bfff6873SLukas Czerner }
2961bfff6873SLukas Czerner 
2962bfff6873SLukas Czerner /*
2963bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
2964bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
2965bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
2966bfff6873SLukas Czerner  * groups and return total number of groups.
2967bfff6873SLukas Czerner  */
2968bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2969bfff6873SLukas Czerner {
2970bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2971bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2972bfff6873SLukas Czerner 
2973bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
2974bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2975bfff6873SLukas Czerner 		if (!gdp)
2976bfff6873SLukas Czerner 			continue;
2977bfff6873SLukas Czerner 
2978bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2979bfff6873SLukas Czerner 			break;
2980bfff6873SLukas Czerner 	}
2981bfff6873SLukas Czerner 
2982bfff6873SLukas Czerner 	return group;
2983bfff6873SLukas Czerner }
2984bfff6873SLukas Czerner 
2985bfff6873SLukas Czerner static int ext4_li_info_new(void)
2986bfff6873SLukas Czerner {
2987bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
2988bfff6873SLukas Czerner 
2989bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2990bfff6873SLukas Czerner 	if (!eli)
2991bfff6873SLukas Czerner 		return -ENOMEM;
2992bfff6873SLukas Czerner 
2993bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
2994bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
2995bfff6873SLukas Czerner 
2996bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
2997bfff6873SLukas Czerner 
2998bfff6873SLukas Czerner 	ext4_li_info = eli;
2999bfff6873SLukas Czerner 
3000bfff6873SLukas Czerner 	return 0;
3001bfff6873SLukas Czerner }
3002bfff6873SLukas Czerner 
3003bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3004bfff6873SLukas Czerner 					    ext4_group_t start)
3005bfff6873SLukas Czerner {
3006bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3007bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3008bfff6873SLukas Czerner 	unsigned long rnd;
3009bfff6873SLukas Czerner 
3010bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3011bfff6873SLukas Czerner 	if (!elr)
3012bfff6873SLukas Czerner 		return NULL;
3013bfff6873SLukas Czerner 
3014bfff6873SLukas Czerner 	elr->lr_super = sb;
3015bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3016bfff6873SLukas Czerner 	elr->lr_next_group = start;
3017bfff6873SLukas Czerner 
3018bfff6873SLukas Czerner 	/*
3019bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3020bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3021bfff6873SLukas Czerner 	 * better.
3022bfff6873SLukas Czerner 	 */
3023bfff6873SLukas Czerner 	get_random_bytes(&rnd, sizeof(rnd));
3024bfff6873SLukas Czerner 	elr->lr_next_sched = jiffies + (unsigned long)rnd %
3025bfff6873SLukas Czerner 			     (EXT4_DEF_LI_MAX_START_DELAY * HZ);
3026bfff6873SLukas Czerner 
3027bfff6873SLukas Czerner 	return elr;
3028bfff6873SLukas Czerner }
3029bfff6873SLukas Czerner 
30307f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3031bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3032bfff6873SLukas Czerner {
3033bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
30347f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
3035bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
30366c5a6cb9SAndrew Morton 	int ret = 0;
3037bfff6873SLukas Czerner 
30387f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
303951ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
304051ce6511SLukas Czerner 		/*
304151ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
304251ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
304351ce6511SLukas Czerner 		 */
304451ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
30457f511862STheodore Ts'o 		goto out;
304651ce6511SLukas Czerner 	}
3047bfff6873SLukas Czerner 
3048bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
3049bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
305055ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
30517f511862STheodore Ts'o 		goto out;
3052bfff6873SLukas Czerner 
3053bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
30547f511862STheodore Ts'o 	if (!elr) {
30557f511862STheodore Ts'o 		ret = -ENOMEM;
30567f511862STheodore Ts'o 		goto out;
30577f511862STheodore Ts'o 	}
3058bfff6873SLukas Czerner 
3059bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3060bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3061bfff6873SLukas Czerner 		if (ret)
3062bfff6873SLukas Czerner 			goto out;
3063bfff6873SLukas Czerner 	}
3064bfff6873SLukas Czerner 
3065bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3066bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3067bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3068bfff6873SLukas Czerner 
3069bfff6873SLukas Czerner 	sbi->s_li_request = elr;
307046e4690bSTao Ma 	/*
307146e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
307246e4690bSTao Ma 	 * the request_list and the removal and free of it is
307346e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
307446e4690bSTao Ma 	 */
307546e4690bSTao Ma 	elr = NULL;
3076bfff6873SLukas Czerner 
3077bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3078bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3079bfff6873SLukas Czerner 		if (ret)
3080bfff6873SLukas Czerner 			goto out;
3081bfff6873SLukas Czerner 	}
3082bfff6873SLukas Czerner out:
3083bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3084beed5ecbSNicolas Kaiser 	if (ret)
3085bfff6873SLukas Czerner 		kfree(elr);
3086bfff6873SLukas Czerner 	return ret;
3087bfff6873SLukas Czerner }
3088bfff6873SLukas Czerner 
3089bfff6873SLukas Czerner /*
3090bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3091bfff6873SLukas Czerner  * module unload.
3092bfff6873SLukas Czerner  */
3093bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3094bfff6873SLukas Czerner {
3095bfff6873SLukas Czerner 	/*
3096bfff6873SLukas Czerner 	 * If thread exited earlier
3097bfff6873SLukas Czerner 	 * there's nothing to be done.
3098bfff6873SLukas Czerner 	 */
30998f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3100bfff6873SLukas Czerner 		return;
3101bfff6873SLukas Czerner 
31028f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3103bfff6873SLukas Czerner }
3104bfff6873SLukas Czerner 
310525ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
310625ed6e8aSDarrick J. Wong {
310725ed6e8aSDarrick J. Wong 	int ret = 1;
310825ed6e8aSDarrick J. Wong 	int compat, incompat;
310925ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
311025ed6e8aSDarrick J. Wong 
311125ed6e8aSDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
311225ed6e8aSDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
311325ed6e8aSDarrick J. Wong 		/* journal checksum v2 */
311425ed6e8aSDarrick J. Wong 		compat = 0;
311525ed6e8aSDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V2;
311625ed6e8aSDarrick J. Wong 	} else {
311725ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
311825ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
311925ed6e8aSDarrick J. Wong 		incompat = 0;
312025ed6e8aSDarrick J. Wong 	}
312125ed6e8aSDarrick J. Wong 
312225ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
312325ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
312425ed6e8aSDarrick J. Wong 				compat, 0,
312525ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
312625ed6e8aSDarrick J. Wong 				incompat);
312725ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
312825ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
312925ed6e8aSDarrick J. Wong 				compat, 0,
313025ed6e8aSDarrick J. Wong 				incompat);
313125ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
313225ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
313325ed6e8aSDarrick J. Wong 	} else {
313425ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal,
313525ed6e8aSDarrick J. Wong 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
313625ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
313725ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_CSUM_V2);
313825ed6e8aSDarrick J. Wong 	}
313925ed6e8aSDarrick J. Wong 
314025ed6e8aSDarrick J. Wong 	return ret;
314125ed6e8aSDarrick J. Wong }
314225ed6e8aSDarrick J. Wong 
3143952fc18eSTheodore Ts'o /*
3144952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3145952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3146952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3147952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3148952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3149952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3150952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3151952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3152952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3153952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3154952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3155952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3156952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3157952fc18eSTheodore Ts'o  */
3158952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3159952fc18eSTheodore Ts'o 			  char *buf)
3160952fc18eSTheodore Ts'o {
3161952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3162952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3163952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3164952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3165952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3166952fc18eSTheodore Ts'o 
31670548bbb8STheodore Ts'o 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC))
31680548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
31690548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
31700548bbb8STheodore Ts'o 
3171952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3172952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3173952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3174952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3175952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3176952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3177952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3178952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3179952fc18eSTheodore Ts'o 			count++;
3180952fc18eSTheodore Ts'o 		}
3181952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3182952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3183952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3184952fc18eSTheodore Ts'o 			count++;
3185952fc18eSTheodore Ts'o 		}
3186952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3187952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3188952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3189952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3190952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3191952fc18eSTheodore Ts'o 				count++;
3192952fc18eSTheodore Ts'o 			}
3193952fc18eSTheodore Ts'o 		if (i != grp)
3194952fc18eSTheodore Ts'o 			continue;
3195952fc18eSTheodore Ts'o 		s = 0;
3196952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3197952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3198952fc18eSTheodore Ts'o 			count++;
3199952fc18eSTheodore Ts'o 		}
3200952fc18eSTheodore Ts'o 		for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) {
3201952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3202952fc18eSTheodore Ts'o 			count++;
3203952fc18eSTheodore Ts'o 		}
3204952fc18eSTheodore Ts'o 	}
3205952fc18eSTheodore Ts'o 	if (!count)
3206952fc18eSTheodore Ts'o 		return 0;
3207952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3208952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3209952fc18eSTheodore Ts'o }
3210952fc18eSTheodore Ts'o 
3211952fc18eSTheodore Ts'o /*
3212952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3213952fc18eSTheodore Ts'o  */
3214952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3215952fc18eSTheodore Ts'o {
3216952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3217952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
3218952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3219952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
3220952fc18eSTheodore Ts'o 	char *buf = (char *) get_zeroed_page(GFP_KERNEL);
3221952fc18eSTheodore Ts'o 
3222952fc18eSTheodore Ts'o 	if (!buf)
3223952fc18eSTheodore Ts'o 		return -ENOMEM;
3224952fc18eSTheodore Ts'o 
3225952fc18eSTheodore Ts'o 	/*
3226952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3227952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3228952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3229952fc18eSTheodore Ts'o 	 */
3230952fc18eSTheodore Ts'o 
3231952fc18eSTheodore Ts'o 	/*
3232952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3233952fc18eSTheodore Ts'o 	 */
3234952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3235952fc18eSTheodore Ts'o 
3236952fc18eSTheodore Ts'o 	/*
3237952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3238952fc18eSTheodore Ts'o 	 */
3239952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3240952fc18eSTheodore Ts'o 		int blks;
3241952fc18eSTheodore Ts'o 
3242952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3243952fc18eSTheodore Ts'o 		overhead += blks;
3244952fc18eSTheodore Ts'o 		if (blks)
3245952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3246952fc18eSTheodore Ts'o 		cond_resched();
3247952fc18eSTheodore Ts'o 	}
32480875a2b4SEric Sandeen 	/* Add the journal blocks as well */
32490875a2b4SEric Sandeen 	if (sbi->s_journal)
3250810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
32510875a2b4SEric Sandeen 
3252952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3253952fc18eSTheodore Ts'o 	smp_wmb();
3254952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3255952fc18eSTheodore Ts'o 	return 0;
3256952fc18eSTheodore Ts'o }
3257952fc18eSTheodore Ts'o 
325827dd4385SLukas Czerner 
325927dd4385SLukas Czerner static ext4_fsblk_t ext4_calculate_resv_clusters(struct ext4_sb_info *sbi)
326027dd4385SLukas Czerner {
326127dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
326227dd4385SLukas Czerner 
326327dd4385SLukas Czerner 	/*
326427dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
326527dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
326627dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
326727dd4385SLukas Czerner 	 * uninitialized extents in delalloc path. In most cases such
326827dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
326927dd4385SLukas Czerner 	 * very rare.
327027dd4385SLukas Czerner 	 */
327127dd4385SLukas Czerner 	resv_clusters = ext4_blocks_count(sbi->s_es) >> sbi->s_cluster_bits;
327227dd4385SLukas Czerner 
327327dd4385SLukas Czerner 	do_div(resv_clusters, 50);
327427dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
327527dd4385SLukas Czerner 
327627dd4385SLukas Czerner 	return resv_clusters;
327727dd4385SLukas Czerner }
327827dd4385SLukas Czerner 
327927dd4385SLukas Czerner 
328027dd4385SLukas Czerner static int ext4_reserve_clusters(struct ext4_sb_info *sbi, ext4_fsblk_t count)
328127dd4385SLukas Czerner {
328227dd4385SLukas Czerner 	ext4_fsblk_t clusters = ext4_blocks_count(sbi->s_es) >>
328327dd4385SLukas Czerner 				sbi->s_cluster_bits;
328427dd4385SLukas Czerner 
328527dd4385SLukas Czerner 	if (count >= clusters)
328627dd4385SLukas Czerner 		return -EINVAL;
328727dd4385SLukas Czerner 
328827dd4385SLukas Czerner 	atomic64_set(&sbi->s_resv_clusters, count);
328927dd4385SLukas Czerner 	return 0;
329027dd4385SLukas Czerner }
329127dd4385SLukas Czerner 
3292617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3293ac27a0ecSDave Kleikamp {
3294d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3295ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3296617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
3297617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
3298617ba13bSMingming Cao 	ext4_fsblk_t block;
3299617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
330070bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3301ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3302ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3303ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3304ac27a0ecSDave Kleikamp 	struct inode *root;
33059f6200bbSTheodore Ts'o 	char *cp;
33060390131bSFrank Mayhar 	const char *descr;
3307dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3308281b5995STheodore Ts'o 	int blocksize, clustersize;
33094ec11028STheodore Ts'o 	unsigned int db_count;
33104ec11028STheodore Ts'o 	unsigned int i;
3311281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3312bd81d8eeSLaurent Vivier 	__u64 blocks_count;
331307aa2ea1SLukas Czerner 	int err = 0;
3314b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3315bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3316ac27a0ecSDave Kleikamp 
3317ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3318ac27a0ecSDave Kleikamp 	if (!sbi)
3319dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3320705895b6SPekka Enberg 
3321705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3322705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3323705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
3324705895b6SPekka Enberg 		kfree(sbi);
3325dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3326705895b6SPekka Enberg 	}
3327ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
33282c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3329240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3330d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3331f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3332f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3333f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3334ac27a0ecSDave Kleikamp 
33359f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
33369f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
33379f6200bbSTheodore Ts'o 		*cp = '!';
33389f6200bbSTheodore Ts'o 
333907aa2ea1SLukas Czerner 	/* -EINVAL is default */
3340dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3341617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3342ac27a0ecSDave Kleikamp 	if (!blocksize) {
3343b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3344ac27a0ecSDave Kleikamp 		goto out_fail;
3345ac27a0ecSDave Kleikamp 	}
3346ac27a0ecSDave Kleikamp 
3347ac27a0ecSDave Kleikamp 	/*
3348617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3349ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3350ac27a0ecSDave Kleikamp 	 */
3351617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
335270bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
335370bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3354ac27a0ecSDave Kleikamp 	} else {
335570bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3356ac27a0ecSDave Kleikamp 	}
3357ac27a0ecSDave Kleikamp 
335870bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
3359b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3360ac27a0ecSDave Kleikamp 		goto out_fail;
3361ac27a0ecSDave Kleikamp 	}
3362ac27a0ecSDave Kleikamp 	/*
3363ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3364617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3365ac27a0ecSDave Kleikamp 	 */
33662716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3367ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3368ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3369617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3370617ba13bSMingming Cao 		goto cantfind_ext4;
3371afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3372ac27a0ecSDave Kleikamp 
3373feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3374feb0ab32SDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3375feb0ab32SDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
3376feb0ab32SDarrick J. Wong 	    EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
3377feb0ab32SDarrick J. Wong 		ext4_warning(sb, KERN_INFO "metadata_csum and uninit_bg are "
3378feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3379feb0ab32SDarrick J. Wong 
3380d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3381d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3382d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3383d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3384d25425f8SDarrick J. Wong 		silent = 1;
3385d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3386d25425f8SDarrick J. Wong 	}
3387d25425f8SDarrick J. Wong 
33880441984aSDarrick J. Wong 	/* Load the checksum driver */
33890441984aSDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
33900441984aSDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
33910441984aSDarrick J. Wong 		sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
33920441984aSDarrick J. Wong 		if (IS_ERR(sbi->s_chksum_driver)) {
33930441984aSDarrick J. Wong 			ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
33940441984aSDarrick J. Wong 			ret = PTR_ERR(sbi->s_chksum_driver);
33950441984aSDarrick J. Wong 			sbi->s_chksum_driver = NULL;
33960441984aSDarrick J. Wong 			goto failed_mount;
33970441984aSDarrick J. Wong 		}
33980441984aSDarrick J. Wong 	}
33990441984aSDarrick J. Wong 
3400a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3401a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3402a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3403a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3404a9c47317SDarrick J. Wong 		silent = 1;
3405a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3406a9c47317SDarrick J. Wong 	}
3407a9c47317SDarrick J. Wong 
3408a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3409a9c47317SDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3410a9c47317SDarrick J. Wong 			EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
3411a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3412a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3413a9c47317SDarrick J. Wong 
3414ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3415ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3416fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3417617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3418fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
341987f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3420fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3421617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3422fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3423ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3424fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
342503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3426fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
34272e7842b8SHugh Dickins #endif
3428617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3429fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3430617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3431fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3432617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3433fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3434ac27a0ecSDave Kleikamp 
3435617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3436fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3437bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3438fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3439bb4f397aSAneesh Kumar K.V 	else
3440fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
34418b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
3442fd8c37ecSTheodore Ts'o 		set_opt(sb, BLOCK_VALIDITY);
34438b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3444fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3445ac27a0ecSDave Kleikamp 
344608cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
344708cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
344830773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
344930773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
345030773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3451ac27a0ecSDave Kleikamp 
34528b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3453fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3454ac27a0ecSDave Kleikamp 
34551e2462f9SMingming Cao 	/*
3456dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3457dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3458dd919b98SAneesh Kumar K.V 	 */
3459bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
34608b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3461fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3462dd919b98SAneesh Kumar K.V 
346351ce6511SLukas Czerner 	/*
346451ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
346551ce6511SLukas Czerner 	 * no mount option specified.
346651ce6511SLukas Czerner 	 */
346751ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
346851ce6511SLukas Czerner 
34698b67f04aSTheodore Ts'o 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
3470661aa520SEric Sandeen 			   &journal_devnum, &journal_ioprio, 0)) {
34718b67f04aSTheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
34728b67f04aSTheodore Ts'o 			 "failed to parse options in superblock: %s",
34738b67f04aSTheodore Ts'o 			 sbi->s_es->s_mount_opts);
34748b67f04aSTheodore Ts'o 	}
34755a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3476b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3477661aa520SEric Sandeen 			   &journal_ioprio, 0))
3478ac27a0ecSDave Kleikamp 		goto failed_mount;
3479ac27a0ecSDave Kleikamp 
348056889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
348156889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
348256889787STheodore Ts'o 			    "with data=journal disables delayed "
348356889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
348456889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
348556889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
348656889787STheodore Ts'o 				 "both data=journal and delalloc");
348756889787STheodore Ts'o 			goto failed_mount;
348856889787STheodore Ts'o 		}
348956889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
349056889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
349156889787STheodore Ts'o 				 "both data=journal and delalloc");
349256889787STheodore Ts'o 			goto failed_mount;
349356889787STheodore Ts'o 		}
349456889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
349556889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
349656889787STheodore Ts'o 	}
349756889787STheodore Ts'o 
3498ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3499482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3500ac27a0ecSDave Kleikamp 
3501617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3502617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3503617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3504617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
3505b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3506b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3507b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3508469108ffSTheodore Tso 
35092035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
35102035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
35112035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
35122035e776STheodore Ts'o 				 "using the ext4 subsystem");
35132035e776STheodore Ts'o 		else {
35142035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
35152035e776STheodore Ts'o 				 "to feature incompatibilities");
35162035e776STheodore Ts'o 			goto failed_mount;
35172035e776STheodore Ts'o 		}
35182035e776STheodore Ts'o 	}
35192035e776STheodore Ts'o 
35202035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
35212035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
35222035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
35232035e776STheodore Ts'o 				 "using the ext4 subsystem");
35242035e776STheodore Ts'o 		else {
35252035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
35262035e776STheodore Ts'o 				 "to feature incompatibilities");
35272035e776STheodore Ts'o 			goto failed_mount;
35282035e776STheodore Ts'o 		}
35292035e776STheodore Ts'o 	}
35302035e776STheodore Ts'o 
3531469108ffSTheodore Tso 	/*
3532ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3533ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3534ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3535ac27a0ecSDave Kleikamp 	 */
3536a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3537ac27a0ecSDave Kleikamp 		goto failed_mount;
3538a13fb1a4SEric Sandeen 
3539261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3540617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3541617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3542b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3543b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
3544ac27a0ecSDave Kleikamp 		goto failed_mount;
3545ac27a0ecSDave Kleikamp 	}
3546ac27a0ecSDave Kleikamp 
3547ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3548ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3549ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3550b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3551ce40733cSAneesh Kumar K.V 					blocksize);
3552ac27a0ecSDave Kleikamp 			goto failed_mount;
3553ac27a0ecSDave Kleikamp 		}
3554ac27a0ecSDave Kleikamp 
3555ac27a0ecSDave Kleikamp 		brelse(bh);
355670bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
355770bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
355870bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
3559ac27a0ecSDave Kleikamp 		if (!bh) {
3560b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3561b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3562ac27a0ecSDave Kleikamp 			goto failed_mount;
3563ac27a0ecSDave Kleikamp 		}
35642716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
3565ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3566617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3567b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3568b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3569ac27a0ecSDave Kleikamp 			goto failed_mount;
3570ac27a0ecSDave Kleikamp 		}
3571ac27a0ecSDave Kleikamp 	}
3572ac27a0ecSDave Kleikamp 
3573a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3574a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
3575f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3576f287a1a5STheodore Ts'o 						      has_huge_files);
3577f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3578ac27a0ecSDave Kleikamp 
3579617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3580617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3581617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3582ac27a0ecSDave Kleikamp 	} else {
3583ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3584ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3585617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
35861330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3587ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3588b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3589b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3590ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3591ac27a0ecSDave Kleikamp 			goto failed_mount;
3592ac27a0ecSDave Kleikamp 		}
3593ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3594ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3595ac27a0ecSDave Kleikamp 	}
35960b8e58a1SAndreas Dilger 
35970d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
35980d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
35998fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
36000d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3601d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3602b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3603b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
36040d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
36050d1ee42fSAlexandre Ratchov 			goto failed_mount;
36060d1ee42fSAlexandre Ratchov 		}
36070d1ee42fSAlexandre Ratchov 	} else
36080d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
36090b8e58a1SAndreas Dilger 
3610ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3611ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3612b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
3613617ba13bSMingming Cao 		goto cantfind_ext4;
36140b8e58a1SAndreas Dilger 
3615617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3616ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3617617ba13bSMingming Cao 		goto cantfind_ext4;
3618ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3619ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
36200d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3621ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3622ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3623e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3624e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
36250b8e58a1SAndreas Dilger 
362668911009SLukas Czerner 	/* Do we have standard group size of blocksize * 8 blocks ? */
362768911009SLukas Czerner 	if (sbi->s_blocks_per_group == blocksize << 3)
362868911009SLukas Czerner 		set_opt2(sb, STD_GROUP_SIZE);
362968911009SLukas Czerner 
3630ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3631ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3632ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3633f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
3634f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
3635f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3636f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3637f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
3638f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3639f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3640f99b2589STheodore Ts'o #else
3641f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3642f99b2589STheodore Ts'o #endif
3643f99b2589STheodore Ts'o 	}
3644ac27a0ecSDave Kleikamp 
3645281b5995STheodore Ts'o 	/* Handle clustersize */
3646281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3647281b5995STheodore Ts'o 	has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3648281b5995STheodore Ts'o 				EXT4_FEATURE_RO_COMPAT_BIGALLOC);
3649281b5995STheodore Ts'o 	if (has_bigalloc) {
3650281b5995STheodore Ts'o 		if (clustersize < blocksize) {
3651281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3652281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
3653281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
3654281b5995STheodore Ts'o 			goto failed_mount;
3655281b5995STheodore Ts'o 		}
3656281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3657281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
3658281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
3659281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
3660281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
3661281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3662281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
3663281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3664281b5995STheodore Ts'o 			goto failed_mount;
3665281b5995STheodore Ts'o 		}
3666281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
3667281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3668281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3669281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
3670281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
3671281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3672281b5995STheodore Ts'o 			goto failed_mount;
3673281b5995STheodore Ts'o 		}
3674281b5995STheodore Ts'o 	} else {
3675281b5995STheodore Ts'o 		if (clustersize != blocksize) {
3676281b5995STheodore Ts'o 			ext4_warning(sb, "fragment/cluster size (%d) != "
3677281b5995STheodore Ts'o 				     "block size (%d)", clustersize,
3678281b5995STheodore Ts'o 				     blocksize);
3679281b5995STheodore Ts'o 			clustersize = blocksize;
3680281b5995STheodore Ts'o 		}
3681ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
3682b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3683b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
3684ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
3685ac27a0ecSDave Kleikamp 			goto failed_mount;
3686ac27a0ecSDave Kleikamp 		}
3687281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3688281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
3689281b5995STheodore Ts'o 	}
3690281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
3691281b5995STheodore Ts'o 
3692ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
3693b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3694b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
3695ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
3696ac27a0ecSDave Kleikamp 		goto failed_mount;
3697ac27a0ecSDave Kleikamp 	}
3698ac27a0ecSDave Kleikamp 
3699bf43d84bSEric Sandeen 	/*
3700bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3701bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3702bf43d84bSEric Sandeen 	 */
37035a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
370430ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
37055a9ae68aSDarrick J. Wong 	if (err) {
3706b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3707bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3708ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
370990c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
3710ac27a0ecSDave Kleikamp 		goto failed_mount;
3711ac27a0ecSDave Kleikamp 	}
3712ac27a0ecSDave Kleikamp 
3713617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3714617ba13bSMingming Cao 		goto cantfind_ext4;
3715e7c95593SEric Sandeen 
37160f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
37170f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
37180f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3719b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3720b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
37210f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
37220f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
37230f2ddca6SFrom: Thiemo Nagel 	}
37240f2ddca6SFrom: Thiemo Nagel 
37254ec11028STheodore Ts'o 	/*
37264ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
37274ec11028STheodore Ts'o 	 * of the filesystem.
37284ec11028STheodore Ts'o 	 */
37294ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3730b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
3731b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3732e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
37334ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3734e7c95593SEric Sandeen 		goto failed_mount;
3735e7c95593SEric Sandeen 	}
3736bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3737bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3738bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3739bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
37404ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3741b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
37424ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3743b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
37444ec11028STheodore Ts'o 		       ext4_blocks_count(es),
37454ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
37464ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
37474ec11028STheodore Ts'o 		goto failed_mount;
37484ec11028STheodore Ts'o 	}
3749bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3750fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3751fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3752617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3753617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3754f18a5f21STheodore Ts'o 	sbi->s_group_desc = ext4_kvmalloc(db_count *
3755f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
3756ac27a0ecSDave Kleikamp 					  GFP_KERNEL);
3757ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3758b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
37592cde417dSTheodore Ts'o 		ret = -ENOMEM;
3760ac27a0ecSDave Kleikamp 		goto failed_mount;
3761ac27a0ecSDave Kleikamp 	}
3762ac27a0ecSDave Kleikamp 
37639f6200bbSTheodore Ts'o 	if (ext4_proc_root)
37649f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
3765240799cdSTheodore Ts'o 
376666acdcf4STheodore Ts'o 	if (sbi->s_proc)
376766acdcf4STheodore Ts'o 		proc_create_data("options", S_IRUGO, sbi->s_proc,
376866acdcf4STheodore Ts'o 				 &ext4_seq_options_fops, sb);
376966acdcf4STheodore Ts'o 
3770705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3771ac27a0ecSDave Kleikamp 
3772ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
377370bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3774ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
3775ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3776b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3777b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3778ac27a0ecSDave Kleikamp 			db_count = i;
3779ac27a0ecSDave Kleikamp 			goto failed_mount2;
3780ac27a0ecSDave Kleikamp 		}
3781ac27a0ecSDave Kleikamp 	}
3782bfff6873SLukas Czerner 	if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
3783b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
3784ac27a0ecSDave Kleikamp 		goto failed_mount2;
3785ac27a0ecSDave Kleikamp 	}
3786772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
3787772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
3788b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3789b31e1552SEric Sandeen 			       "unable to initialize "
3790b31e1552SEric Sandeen 			       "flex_bg meta info!");
3791772cb7c8SJose R. Santos 			goto failed_mount2;
3792772cb7c8SJose R. Santos 		}
3793772cb7c8SJose R. Santos 
3794ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
3795ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3796ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3797ac27a0ecSDave Kleikamp 
379804496411STao Ma 	init_timer(&sbi->s_err_report);
379904496411STao Ma 	sbi->s_err_report.function = print_daily_error_info;
380004496411STao Ma 	sbi->s_err_report.data = (unsigned long) sb;
380104496411STao Ma 
3802a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
3803*d3922a77SZheng Liu 	ext4_es_register_shrinker(sbi);
3804a75ae78fSDmitry Monakhov 
380557042651STheodore Ts'o 	err = percpu_counter_init(&sbi->s_freeclusters_counter,
38065dee5437STheodore Ts'o 			ext4_count_free_clusters(sb));
3807ce7e010aSTheodore Ts'o 	if (!err) {
3808ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
3809ce7e010aSTheodore Ts'o 				ext4_count_free_inodes(sb));
3810ce7e010aSTheodore Ts'o 	}
3811ce7e010aSTheodore Ts'o 	if (!err) {
3812ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
3813ce7e010aSTheodore Ts'o 				ext4_count_dirs(sb));
3814ce7e010aSTheodore Ts'o 	}
3815ce7e010aSTheodore Ts'o 	if (!err) {
381657042651STheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0);
3817ce7e010aSTheodore Ts'o 	}
38181ac6466fSTheodore Ts'o 	if (!err) {
38191ac6466fSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_extent_cache_cnt, 0);
38201ac6466fSTheodore Ts'o 	}
3821ce7e010aSTheodore Ts'o 	if (err) {
3822ce7e010aSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
3823ce7e010aSTheodore Ts'o 		goto failed_mount3;
3824ce7e010aSTheodore Ts'o 	}
3825ce7e010aSTheodore Ts'o 
3826c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
382767a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
3828c9de560dSAlex Tomas 
3829ac27a0ecSDave Kleikamp 	/*
3830ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
3831ac27a0ecSDave Kleikamp 	 */
38329ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
38339ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
3834617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
38359ca92389STheodore Ts'o 	else
38369ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
3837617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3838617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3839ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3840617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
3841262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
38427c319d32SAditya Kali 		sb->s_qcop = &ext4_qctl_sysfile_operations;
3843262b4662SJan Kara 	else
3844262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
3845ac27a0ecSDave Kleikamp #endif
3846f2fa2ffcSAneesh Kumar K.V 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3847f2fa2ffcSAneesh Kumar K.V 
3848ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
38493b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
3850ac27a0ecSDave Kleikamp 
3851ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3852ac27a0ecSDave Kleikamp 
3853ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3854617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
3855617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
3856ac27a0ecSDave Kleikamp 
3857c5e06d10SJohann Lombardi 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
3858c5e06d10SJohann Lombardi 	    !(sb->s_flags & MS_RDONLY))
3859c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
3860c5e06d10SJohann Lombardi 			goto failed_mount3;
3861c5e06d10SJohann Lombardi 
3862ac27a0ecSDave Kleikamp 	/*
3863ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3864ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3865ac27a0ecSDave Kleikamp 	 */
3866ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
3867617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3868617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
3869ac27a0ecSDave Kleikamp 			goto failed_mount3;
38700390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
38710390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3872b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3873b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3874744692dcSJiaying Zhang 		goto failed_mount_wq;
3875ac27a0ecSDave Kleikamp 	} else {
3876fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
38770390131bSFrank Mayhar 		sbi->s_journal = NULL;
38780390131bSFrank Mayhar 		needs_recovery = 0;
38790390131bSFrank Mayhar 		goto no_journal;
3880ac27a0ecSDave Kleikamp 	}
3881ac27a0ecSDave Kleikamp 
3882f32aaf2dSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT) &&
3883eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3884eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3885b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3886744692dcSJiaying Zhang 		goto failed_mount_wq;
3887eb40a09cSJose R. Santos 	}
3888eb40a09cSJose R. Santos 
388925ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
389025ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
389125ed6e8aSDarrick J. Wong 			 "feature set");
389225ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
3893d4da6c9cSLinus Torvalds 	}
3894818d276cSGirish Shilamkar 
3895ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3896ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3897ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3898ac27a0ecSDave Kleikamp 	case 0:
3899ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
390063f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
390163f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
390263f57933SAndrew Morton 		 */
3903dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3904dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3905fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3906ac27a0ecSDave Kleikamp 		else
3907fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3908ac27a0ecSDave Kleikamp 		break;
3909ac27a0ecSDave Kleikamp 
3910617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
3911617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
3912dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
3913dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
3914b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
3915b31e1552SEric Sandeen 			       "requested data journaling mode");
3916744692dcSJiaying Zhang 			goto failed_mount_wq;
3917ac27a0ecSDave Kleikamp 		}
3918ac27a0ecSDave Kleikamp 	default:
3919ac27a0ecSDave Kleikamp 		break;
3920ac27a0ecSDave Kleikamp 	}
3921b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3922ac27a0ecSDave Kleikamp 
392318aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
392418aadd47SBobi Jam 
3925ce7e010aSTheodore Ts'o 	/*
3926ce7e010aSTheodore Ts'o 	 * The journal may have updated the bg summary counts, so we
3927ce7e010aSTheodore Ts'o 	 * need to update the global counters.
3928ce7e010aSTheodore Ts'o 	 */
392957042651STheodore Ts'o 	percpu_counter_set(&sbi->s_freeclusters_counter,
39305dee5437STheodore Ts'o 			   ext4_count_free_clusters(sb));
3931ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_freeinodes_counter,
393284061e07SDmitry Monakhov 			   ext4_count_free_inodes(sb));
3933ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_dirs_counter,
393484061e07SDmitry Monakhov 			   ext4_count_dirs(sb));
393557042651STheodore Ts'o 	percpu_counter_set(&sbi->s_dirtyclusters_counter, 0);
3936206f7ab4SChristoph Hellwig 
3937ce7e010aSTheodore Ts'o no_journal:
3938fd89d5f2STejun Heo 	/*
3939952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
3940952fc18eSTheodore Ts'o 	 * superblock if present.
3941952fc18eSTheodore Ts'o 	 */
3942952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
3943952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
3944952fc18eSTheodore Ts'o 	else {
394507aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
394607aa2ea1SLukas Czerner 		if (err)
3947952fc18eSTheodore Ts'o 			goto failed_mount_wq;
3948952fc18eSTheodore Ts'o 	}
3949952fc18eSTheodore Ts'o 
3950952fc18eSTheodore Ts'o 	/*
3951fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
3952fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
3953fd89d5f2STejun Heo 	 */
39542e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
39552e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
39562e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
39572e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
395807aa2ea1SLukas Czerner 		ret = -ENOMEM;
39592e8fa54eSJan Kara 		goto failed_mount4;
39602e8fa54eSJan Kara 	}
39612e8fa54eSJan Kara 
39622e8fa54eSJan Kara 	EXT4_SB(sb)->unrsv_conversion_wq =
39632e8fa54eSJan Kara 		alloc_workqueue("ext4-unrsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
39642e8fa54eSJan Kara 	if (!EXT4_SB(sb)->unrsv_conversion_wq) {
39652e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
39662e8fa54eSJan Kara 		ret = -ENOMEM;
39672e8fa54eSJan Kara 		goto failed_mount4;
39684c0425ffSMingming Cao 	}
39694c0425ffSMingming Cao 
3970ac27a0ecSDave Kleikamp 	/*
3971dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
3972ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
3973ac27a0ecSDave Kleikamp 	 */
3974ac27a0ecSDave Kleikamp 
39751d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
39761d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
3977b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
39781d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
397932a9bb57SManish Katiyar 		root = NULL;
3980ac27a0ecSDave Kleikamp 		goto failed_mount4;
3981ac27a0ecSDave Kleikamp 	}
3982ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
3983b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
398494bf608aSAl Viro 		iput(root);
3985ac27a0ecSDave Kleikamp 		goto failed_mount4;
3986ac27a0ecSDave Kleikamp 	}
398748fde701SAl Viro 	sb->s_root = d_make_root(root);
39881d1fe1eeSDavid Howells 	if (!sb->s_root) {
3989b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
39901d1fe1eeSDavid Howells 		ret = -ENOMEM;
39911d1fe1eeSDavid Howells 		goto failed_mount4;
39921d1fe1eeSDavid Howells 	}
3993ac27a0ecSDave Kleikamp 
39947e84b621SEric Sandeen 	if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
39957e84b621SEric Sandeen 		sb->s_flags |= MS_RDONLY;
3996ef7f3835SKalpak Shah 
3997ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
3998ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3999ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4000ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
4001ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4002ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
4003ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4004ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
4005ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4006ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
4007ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4008ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
4009ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4010ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
4011ef7f3835SKalpak Shah 		}
4012ef7f3835SKalpak Shah 	}
4013ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
4014ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
4015ef7f3835SKalpak Shah 							sbi->s_inode_size) {
4016ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4017ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
4018b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
4019b31e1552SEric Sandeen 			 "available");
4020ef7f3835SKalpak Shah 	}
4021ef7f3835SKalpak Shah 
402227dd4385SLukas Czerner 	err = ext4_reserve_clusters(sbi, ext4_calculate_resv_clusters(sbi));
402327dd4385SLukas Czerner 	if (err) {
402427dd4385SLukas Czerner 		ext4_msg(sb, KERN_ERR, "failed to reserve %llu clusters for "
402527dd4385SLukas Czerner 			 "reserved pool", ext4_calculate_resv_clusters(sbi));
402627dd4385SLukas Czerner 		goto failed_mount4a;
402727dd4385SLukas Czerner 	}
402827dd4385SLukas Czerner 
40296fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
40306fd058f7STheodore Ts'o 	if (err) {
4031b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4032fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
403394bf608aSAl Viro 		goto failed_mount4a;
40346fd058f7STheodore Ts'o 	}
40356fd058f7STheodore Ts'o 
4036c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
40379d99012fSAkira Fujita 	err = ext4_mb_init(sb);
4038c2774d84SAneesh Kumar K.V 	if (err) {
4039421f91d2SUwe Kleine-König 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4040c2774d84SAneesh Kumar K.V 			 err);
4041dcf2d804STao Ma 		goto failed_mount5;
4042c2774d84SAneesh Kumar K.V 	}
4043c2774d84SAneesh Kumar K.V 
4044bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4045bfff6873SLukas Czerner 	if (err)
4046dcf2d804STao Ma 		goto failed_mount6;
4047bfff6873SLukas Czerner 
40483197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
40493197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
40503197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
40513197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
4052dcf2d804STao Ma 	if (err)
4053dcf2d804STao Ma 		goto failed_mount7;
40543197ebdbSTheodore Ts'o 
40559b2ff357SJan Kara #ifdef CONFIG_QUOTA
40569b2ff357SJan Kara 	/* Enable quota usage during mount. */
40579b2ff357SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
40589b2ff357SJan Kara 	    !(sb->s_flags & MS_RDONLY)) {
40599b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
40609b2ff357SJan Kara 		if (err)
40619b2ff357SJan Kara 			goto failed_mount8;
40629b2ff357SJan Kara 	}
40639b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
40649b2ff357SJan Kara 
4065617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4066617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4067617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
40680390131bSFrank Mayhar 	if (needs_recovery) {
4069b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4070617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
40710390131bSFrank Mayhar 	}
40720390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
40730390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
40740390131bSFrank Mayhar 			descr = " journalled data mode";
40750390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
40760390131bSFrank Mayhar 			descr = " ordered data mode";
40770390131bSFrank Mayhar 		else
40780390131bSFrank Mayhar 			descr = " writeback data mode";
40790390131bSFrank Mayhar 	} else
40800390131bSFrank Mayhar 		descr = "out journal";
40810390131bSFrank Mayhar 
408279add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
408379add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
408479add3a3SLukas Czerner 		if (!blk_queue_discard(q))
408579add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
408679add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
408779add3a3SLukas Czerner 				 "the device does not support discard");
408879add3a3SLukas Czerner 	}
408979add3a3SLukas Czerner 
4090d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
40918b67f04aSTheodore Ts'o 		 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
40928b67f04aSTheodore Ts'o 		 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4093ac27a0ecSDave Kleikamp 
409466e61a9eSTheodore Ts'o 	if (es->s_error_count)
409566e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4096ac27a0ecSDave Kleikamp 
4097d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4098ac27a0ecSDave Kleikamp 	return 0;
4099ac27a0ecSDave Kleikamp 
4100617ba13bSMingming Cao cantfind_ext4:
4101ac27a0ecSDave Kleikamp 	if (!silent)
4102b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4103ac27a0ecSDave Kleikamp 	goto failed_mount;
4104ac27a0ecSDave Kleikamp 
410572ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
410672ba7450STheodore Ts'o failed_mount8:
410772ba7450STheodore Ts'o 	kobject_del(&sbi->s_kobj);
410872ba7450STheodore Ts'o #endif
4109dcf2d804STao Ma failed_mount7:
4110dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4111dcf2d804STao Ma failed_mount6:
4112dcf2d804STao Ma 	ext4_mb_release(sb);
411394bf608aSAl Viro failed_mount5:
411494bf608aSAl Viro 	ext4_ext_release(sb);
4115dcf2d804STao Ma 	ext4_release_system_zone(sb);
411694bf608aSAl Viro failed_mount4a:
411794bf608aSAl Viro 	dput(sb->s_root);
411832a9bb57SManish Katiyar 	sb->s_root = NULL;
411994bf608aSAl Viro failed_mount4:
4120b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
41212e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
41222e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
41232e8fa54eSJan Kara 	if (EXT4_SB(sb)->unrsv_conversion_wq)
41242e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->unrsv_conversion_wq);
41254c0425ffSMingming Cao failed_mount_wq:
41260390131bSFrank Mayhar 	if (sbi->s_journal) {
4127dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
412847b4a50bSJan Kara 		sbi->s_journal = NULL;
41290390131bSFrank Mayhar 	}
4130ac27a0ecSDave Kleikamp failed_mount3:
4131*d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
413204496411STao Ma 	del_timer(&sbi->s_err_report);
41339933fc0aSTheodore Ts'o 	if (sbi->s_flex_groups)
41349933fc0aSTheodore Ts'o 		ext4_kvfree(sbi->s_flex_groups);
413557042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4136ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4137ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
413857042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
41391ac6466fSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_extent_cache_cnt);
4140c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4141c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4142ac27a0ecSDave Kleikamp failed_mount2:
4143ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4144ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4145f18a5f21STheodore Ts'o 	ext4_kvfree(sbi->s_group_desc);
4146ac27a0ecSDave Kleikamp failed_mount:
41470441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
41480441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4149240799cdSTheodore Ts'o 	if (sbi->s_proc) {
415066acdcf4STheodore Ts'o 		remove_proc_entry("options", sbi->s_proc);
41519f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
4152240799cdSTheodore Ts'o 	}
4153ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4154ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4155ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4156ac27a0ecSDave Kleikamp #endif
4157617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4158ac27a0ecSDave Kleikamp 	brelse(bh);
4159ac27a0ecSDave Kleikamp out_fail:
4160ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4161f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
4162ac27a0ecSDave Kleikamp 	kfree(sbi);
4163dcc7dae3SCyrill Gorcunov out_free_orig:
4164d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
416507aa2ea1SLukas Czerner 	return err ? err : ret;
4166ac27a0ecSDave Kleikamp }
4167ac27a0ecSDave Kleikamp 
4168ac27a0ecSDave Kleikamp /*
4169ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4170ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4171ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4172ac27a0ecSDave Kleikamp  */
4173617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4174ac27a0ecSDave Kleikamp {
4175617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4176ac27a0ecSDave Kleikamp 
4177ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
417830773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
417930773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4180ac27a0ecSDave Kleikamp 
4181a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4182ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4183dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4184ac27a0ecSDave Kleikamp 	else
4185dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
41865bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
41875bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
41885bf5683aSHidehiro Kawai 	else
41895bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4190a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4191ac27a0ecSDave Kleikamp }
4192ac27a0ecSDave Kleikamp 
4193617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
4194ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
4195ac27a0ecSDave Kleikamp {
4196ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4197ac27a0ecSDave Kleikamp 	journal_t *journal;
4198ac27a0ecSDave Kleikamp 
41990390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
42000390131bSFrank Mayhar 
4201ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
4202ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
4203ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
4204ac27a0ecSDave Kleikamp 
42051d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
42061d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4207b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4208ac27a0ecSDave Kleikamp 		return NULL;
4209ac27a0ecSDave Kleikamp 	}
4210ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4211ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4212ac27a0ecSDave Kleikamp 		iput(journal_inode);
4213b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4214ac27a0ecSDave Kleikamp 		return NULL;
4215ac27a0ecSDave Kleikamp 	}
4216ac27a0ecSDave Kleikamp 
4217e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4218ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
42191d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4220b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4221ac27a0ecSDave Kleikamp 		iput(journal_inode);
4222ac27a0ecSDave Kleikamp 		return NULL;
4223ac27a0ecSDave Kleikamp 	}
4224ac27a0ecSDave Kleikamp 
4225dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4226ac27a0ecSDave Kleikamp 	if (!journal) {
4227b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4228ac27a0ecSDave Kleikamp 		iput(journal_inode);
4229ac27a0ecSDave Kleikamp 		return NULL;
4230ac27a0ecSDave Kleikamp 	}
4231ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4232617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4233ac27a0ecSDave Kleikamp 	return journal;
4234ac27a0ecSDave Kleikamp }
4235ac27a0ecSDave Kleikamp 
4236617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4237ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4238ac27a0ecSDave Kleikamp {
4239ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4240ac27a0ecSDave Kleikamp 	journal_t *journal;
4241617ba13bSMingming Cao 	ext4_fsblk_t start;
4242617ba13bSMingming Cao 	ext4_fsblk_t len;
4243ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4244617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4245ac27a0ecSDave Kleikamp 	unsigned long offset;
4246617ba13bSMingming Cao 	struct ext4_super_block *es;
4247ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4248ac27a0ecSDave Kleikamp 
42490390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
42500390131bSFrank Mayhar 
4251b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4252ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4253ac27a0ecSDave Kleikamp 		return NULL;
4254ac27a0ecSDave Kleikamp 
4255ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4256e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4257ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4258b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4259b31e1552SEric Sandeen 			"blocksize too small for journal device");
4260ac27a0ecSDave Kleikamp 		goto out_bdev;
4261ac27a0ecSDave Kleikamp 	}
4262ac27a0ecSDave Kleikamp 
4263617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4264617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4265ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4266ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4267b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4268b31e1552SEric Sandeen 		       "external journal");
4269ac27a0ecSDave Kleikamp 		goto out_bdev;
4270ac27a0ecSDave Kleikamp 	}
4271ac27a0ecSDave Kleikamp 
42722716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4273617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4274ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4275617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4276b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4277b31e1552SEric Sandeen 					"bad superblock");
4278ac27a0ecSDave Kleikamp 		brelse(bh);
4279ac27a0ecSDave Kleikamp 		goto out_bdev;
4280ac27a0ecSDave Kleikamp 	}
4281ac27a0ecSDave Kleikamp 
4282617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4283b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4284ac27a0ecSDave Kleikamp 		brelse(bh);
4285ac27a0ecSDave Kleikamp 		goto out_bdev;
4286ac27a0ecSDave Kleikamp 	}
4287ac27a0ecSDave Kleikamp 
4288bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4289ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4290ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4291ac27a0ecSDave Kleikamp 
4292dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4293ac27a0ecSDave Kleikamp 					start, len, blocksize);
4294ac27a0ecSDave Kleikamp 	if (!journal) {
4295b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4296ac27a0ecSDave Kleikamp 		goto out_bdev;
4297ac27a0ecSDave Kleikamp 	}
4298ac27a0ecSDave Kleikamp 	journal->j_private = sb;
42999f203507STheodore Ts'o 	ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4300ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4301ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4302b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4303ac27a0ecSDave Kleikamp 		goto out_journal;
4304ac27a0ecSDave Kleikamp 	}
4305ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4306b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4307b31e1552SEric Sandeen 					"user (unsupported) - %d",
4308ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4309ac27a0ecSDave Kleikamp 		goto out_journal;
4310ac27a0ecSDave Kleikamp 	}
4311617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4312617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4313ac27a0ecSDave Kleikamp 	return journal;
43140b8e58a1SAndreas Dilger 
4315ac27a0ecSDave Kleikamp out_journal:
4316dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4317ac27a0ecSDave Kleikamp out_bdev:
4318617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4319ac27a0ecSDave Kleikamp 	return NULL;
4320ac27a0ecSDave Kleikamp }
4321ac27a0ecSDave Kleikamp 
4322617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4323617ba13bSMingming Cao 			     struct ext4_super_block *es,
4324ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4325ac27a0ecSDave Kleikamp {
4326ac27a0ecSDave Kleikamp 	journal_t *journal;
4327ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4328ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4329ac27a0ecSDave Kleikamp 	int err = 0;
4330ac27a0ecSDave Kleikamp 	int really_read_only;
4331ac27a0ecSDave Kleikamp 
43320390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
43330390131bSFrank Mayhar 
4334ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4335ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4336b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4337b31e1552SEric Sandeen 			"numbers have changed");
4338ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4339ac27a0ecSDave Kleikamp 	} else
4340ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4341ac27a0ecSDave Kleikamp 
4342ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4343ac27a0ecSDave Kleikamp 
4344ac27a0ecSDave Kleikamp 	/*
4345ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4346ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4347ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4348ac27a0ecSDave Kleikamp 	 */
4349617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
4350ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
4351b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4352b31e1552SEric Sandeen 					"required on readonly filesystem");
4353ac27a0ecSDave Kleikamp 			if (really_read_only) {
4354b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4355b31e1552SEric Sandeen 					"unavailable, cannot proceed");
4356ac27a0ecSDave Kleikamp 				return -EROFS;
4357ac27a0ecSDave Kleikamp 			}
4358b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4359b31e1552SEric Sandeen 			       "be enabled during recovery");
4360ac27a0ecSDave Kleikamp 		}
4361ac27a0ecSDave Kleikamp 	}
4362ac27a0ecSDave Kleikamp 
4363ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4364b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4365b31e1552SEric Sandeen 		       "and inode journals!");
4366ac27a0ecSDave Kleikamp 		return -EINVAL;
4367ac27a0ecSDave Kleikamp 	}
4368ac27a0ecSDave Kleikamp 
4369ac27a0ecSDave Kleikamp 	if (journal_inum) {
4370617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4371ac27a0ecSDave Kleikamp 			return -EINVAL;
4372ac27a0ecSDave Kleikamp 	} else {
4373617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4374ac27a0ecSDave Kleikamp 			return -EINVAL;
4375ac27a0ecSDave Kleikamp 	}
4376ac27a0ecSDave Kleikamp 
437790576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4378b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
43794776004fSTheodore Ts'o 
4380617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
4381dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
43821c13d5c0STheodore Ts'o 	if (!err) {
43831c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
43841c13d5c0STheodore Ts'o 		if (save)
43851c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
43861c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4387dab291afSMingming Cao 		err = jbd2_journal_load(journal);
43881c13d5c0STheodore Ts'o 		if (save)
43891c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
43901c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
43911c13d5c0STheodore Ts'o 		kfree(save);
43921c13d5c0STheodore Ts'o 	}
4393ac27a0ecSDave Kleikamp 
4394ac27a0ecSDave Kleikamp 	if (err) {
4395b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4396dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4397ac27a0ecSDave Kleikamp 		return err;
4398ac27a0ecSDave Kleikamp 	}
4399ac27a0ecSDave Kleikamp 
4400617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4401617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4402ac27a0ecSDave Kleikamp 
4403c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4404ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4405ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4406ac27a0ecSDave Kleikamp 
4407ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4408e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4409ac27a0ecSDave Kleikamp 	}
4410ac27a0ecSDave Kleikamp 
4411ac27a0ecSDave Kleikamp 	return 0;
4412ac27a0ecSDave Kleikamp }
4413ac27a0ecSDave Kleikamp 
4414e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4415ac27a0ecSDave Kleikamp {
4416e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4417617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4418c4be0c1dSTakashi Sato 	int error = 0;
4419ac27a0ecSDave Kleikamp 
44207c2e7087STheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4421c4be0c1dSTakashi Sato 		return error;
4422914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
4423914258bfSTheodore Ts'o 		/*
4424914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
4425914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
4426914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
4427914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
4428914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
4429914258bfSTheodore Ts'o 		 * write and hope for the best.
4430914258bfSTheodore Ts'o 		 */
4431b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
4432b31e1552SEric Sandeen 		       "superblock detected");
4433914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
4434914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
4435914258bfSTheodore Ts'o 	}
443671290b36STheodore Ts'o 	/*
443771290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
443871290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
443971290b36STheodore Ts'o 	 * write time when we are mounting the root file system
444071290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
444171290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
444271290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
444371290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
444471290b36STheodore Ts'o 	 * to complain and force a full file system check.
444571290b36STheodore Ts'o 	 */
444671290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4447ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4448f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4449afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4450afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4451afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4452afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4453f613dfcbSTheodore Ts'o 	else
4454f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4455f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
445657042651STheodore Ts'o 	ext4_free_blocks_count_set(es,
445757042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
445857042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
44597f93cff9STheodore Ts'o 	es->s_free_inodes_count =
44607f93cff9STheodore Ts'o 		cpu_to_le32(percpu_counter_sum_positive(
44615d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
4462ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
446306db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
4464ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4465914258bfSTheodore Ts'o 	if (sync) {
4466c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
4467c4be0c1dSTakashi Sato 		if (error)
4468c4be0c1dSTakashi Sato 			return error;
4469c4be0c1dSTakashi Sato 
4470c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4471c4be0c1dSTakashi Sato 		if (error) {
4472b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4473b31e1552SEric Sandeen 			       "superblock");
4474914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4475914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4476914258bfSTheodore Ts'o 		}
4477914258bfSTheodore Ts'o 	}
4478c4be0c1dSTakashi Sato 	return error;
4479ac27a0ecSDave Kleikamp }
4480ac27a0ecSDave Kleikamp 
4481ac27a0ecSDave Kleikamp /*
4482ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4483ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4484ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4485ac27a0ecSDave Kleikamp  */
4486617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4487617ba13bSMingming Cao 					struct ext4_super_block *es)
4488ac27a0ecSDave Kleikamp {
4489617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4490ac27a0ecSDave Kleikamp 
44910390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
44920390131bSFrank Mayhar 		BUG_ON(journal != NULL);
44930390131bSFrank Mayhar 		return;
44940390131bSFrank Mayhar 	}
4495dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
44967ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
44977ffe1ea8SHidehiro Kawai 		goto out;
44987ffe1ea8SHidehiro Kawai 
4499617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
4500ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4501617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4502e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4503ac27a0ecSDave Kleikamp 	}
45047ffe1ea8SHidehiro Kawai 
45057ffe1ea8SHidehiro Kawai out:
4506dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4507ac27a0ecSDave Kleikamp }
4508ac27a0ecSDave Kleikamp 
4509ac27a0ecSDave Kleikamp /*
4510ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4511ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4512ac27a0ecSDave Kleikamp  * main filesystem now.
4513ac27a0ecSDave Kleikamp  */
4514617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4515617ba13bSMingming Cao 				   struct ext4_super_block *es)
4516ac27a0ecSDave Kleikamp {
4517ac27a0ecSDave Kleikamp 	journal_t *journal;
4518ac27a0ecSDave Kleikamp 	int j_errno;
4519ac27a0ecSDave Kleikamp 	const char *errstr;
4520ac27a0ecSDave Kleikamp 
45210390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
45220390131bSFrank Mayhar 
4523617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4524ac27a0ecSDave Kleikamp 
4525ac27a0ecSDave Kleikamp 	/*
4526ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4527617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4528ac27a0ecSDave Kleikamp 	 */
4529ac27a0ecSDave Kleikamp 
4530dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4531ac27a0ecSDave Kleikamp 	if (j_errno) {
4532ac27a0ecSDave Kleikamp 		char nbuf[16];
4533ac27a0ecSDave Kleikamp 
4534617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
453512062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4536ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
453712062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4538ac27a0ecSDave Kleikamp 
4539617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4540617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4541e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4542ac27a0ecSDave Kleikamp 
4543dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4544d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
4545ac27a0ecSDave Kleikamp 	}
4546ac27a0ecSDave Kleikamp }
4547ac27a0ecSDave Kleikamp 
4548ac27a0ecSDave Kleikamp /*
4549ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4550ac27a0ecSDave Kleikamp  * and wait on the commit.
4551ac27a0ecSDave Kleikamp  */
4552617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4553ac27a0ecSDave Kleikamp {
4554ac27a0ecSDave Kleikamp 	journal_t *journal;
4555ac27a0ecSDave Kleikamp 
4556ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4557ac27a0ecSDave Kleikamp 		return 0;
4558ac27a0ecSDave Kleikamp 
4559617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4560b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
4561ac27a0ecSDave Kleikamp }
4562ac27a0ecSDave Kleikamp 
4563617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4564ac27a0ecSDave Kleikamp {
456514ce0cb4STheodore Ts'o 	int ret = 0;
45669eddacf9SJan Kara 	tid_t target;
456706a407f1SDmitry Monakhov 	bool needs_barrier = false;
45688d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4569ac27a0ecSDave Kleikamp 
45709bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
45712e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
45722e8fa54eSJan Kara 	flush_workqueue(sbi->unrsv_conversion_wq);
4573a1177825SJan Kara 	/*
4574a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
4575a1177825SJan Kara 	 * no dirty dquots
4576a1177825SJan Kara 	 */
4577a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
457806a407f1SDmitry Monakhov 	/*
457906a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
458006a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
458106a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
458206a407f1SDmitry Monakhov 	 */
458306a407f1SDmitry Monakhov 	target = jbd2_get_latest_transaction(sbi->s_journal);
458406a407f1SDmitry Monakhov 	if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
458506a407f1SDmitry Monakhov 	    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
458606a407f1SDmitry Monakhov 		needs_barrier = true;
458706a407f1SDmitry Monakhov 
45888d5d02e6SMingming Cao 	if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4589ac27a0ecSDave Kleikamp 		if (wait)
459006a407f1SDmitry Monakhov 			ret = jbd2_log_wait_commit(sbi->s_journal, target);
45910390131bSFrank Mayhar 	}
459206a407f1SDmitry Monakhov 	if (needs_barrier) {
459306a407f1SDmitry Monakhov 		int err;
459406a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
459506a407f1SDmitry Monakhov 		if (!ret)
459606a407f1SDmitry Monakhov 			ret = err;
459706a407f1SDmitry Monakhov 	}
459806a407f1SDmitry Monakhov 
459906a407f1SDmitry Monakhov 	return ret;
460006a407f1SDmitry Monakhov }
460106a407f1SDmitry Monakhov 
460206a407f1SDmitry Monakhov static int ext4_sync_fs_nojournal(struct super_block *sb, int wait)
460306a407f1SDmitry Monakhov {
460406a407f1SDmitry Monakhov 	int ret = 0;
460506a407f1SDmitry Monakhov 
460606a407f1SDmitry Monakhov 	trace_ext4_sync_fs(sb, wait);
460706a407f1SDmitry Monakhov 	flush_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
460806a407f1SDmitry Monakhov 	flush_workqueue(EXT4_SB(sb)->unrsv_conversion_wq);
460906a407f1SDmitry Monakhov 	dquot_writeback_dquots(sb, -1);
461006a407f1SDmitry Monakhov 	if (wait && test_opt(sb, BARRIER))
461106a407f1SDmitry Monakhov 		ret = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
461206a407f1SDmitry Monakhov 
461314ce0cb4STheodore Ts'o 	return ret;
4614ac27a0ecSDave Kleikamp }
4615ac27a0ecSDave Kleikamp 
4616ac27a0ecSDave Kleikamp /*
4617ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4618ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4619be4f27d3SYongqiang Yang  *
4620be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
46218e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
46228e8ad8a5SJan Kara  * modifications.
4623ac27a0ecSDave Kleikamp  */
4624c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4625ac27a0ecSDave Kleikamp {
4626c4be0c1dSTakashi Sato 	int error = 0;
4627c4be0c1dSTakashi Sato 	journal_t *journal;
4628ac27a0ecSDave Kleikamp 
46299ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
46309ca92389STheodore Ts'o 		return 0;
46319ca92389STheodore Ts'o 
4632c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4633ac27a0ecSDave Kleikamp 
4634ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
4635dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
46367ffe1ea8SHidehiro Kawai 
46377ffe1ea8SHidehiro Kawai 	/*
46389ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
46399ca92389STheodore Ts'o 	 * the journal.
46407ffe1ea8SHidehiro Kawai 	 */
4641c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
46426b0310fbSEric Sandeen 	if (error < 0)
46436b0310fbSEric Sandeen 		goto out;
4644ac27a0ecSDave Kleikamp 
4645ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
4646617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4647e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
46486b0310fbSEric Sandeen out:
46498e8ad8a5SJan Kara 	/* we rely on upper layer to stop further updates */
46506b0310fbSEric Sandeen 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
46516b0310fbSEric Sandeen 	return error;
4652ac27a0ecSDave Kleikamp }
4653ac27a0ecSDave Kleikamp 
4654ac27a0ecSDave Kleikamp /*
4655ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4656ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4657ac27a0ecSDave Kleikamp  */
4658c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4659ac27a0ecSDave Kleikamp {
46609ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
46619ca92389STheodore Ts'o 		return 0;
46629ca92389STheodore Ts'o 
46639ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
4664617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4665e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4666c4be0c1dSTakashi Sato 	return 0;
4667ac27a0ecSDave Kleikamp }
4668ac27a0ecSDave Kleikamp 
4669673c6100STheodore Ts'o /*
4670673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4671673c6100STheodore Ts'o  */
4672673c6100STheodore Ts'o struct ext4_mount_options {
4673673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4674a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
467508cefc7aSEric W. Biederman 	kuid_t s_resuid;
467608cefc7aSEric W. Biederman 	kgid_t s_resgid;
4677673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4678673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4679673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4680673c6100STheodore Ts'o 	int s_jquota_fmt;
4681673c6100STheodore Ts'o 	char *s_qf_names[MAXQUOTAS];
4682673c6100STheodore Ts'o #endif
4683673c6100STheodore Ts'o };
4684673c6100STheodore Ts'o 
4685617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4686ac27a0ecSDave Kleikamp {
4687617ba13bSMingming Cao 	struct ext4_super_block *es;
4688617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4689ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4690617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4691c79d967dSChristoph Hellwig 	int enable_quota = 0;
46928a266467STheodore Ts'o 	ext4_group_t g;
4693b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4694c5e06d10SJohann Lombardi 	int err = 0;
4695ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
469603dafb5fSChen Gang 	int i, j;
4697ac27a0ecSDave Kleikamp #endif
4698d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4699ac27a0ecSDave Kleikamp 
4700ac27a0ecSDave Kleikamp 	/* Store the original options */
4701ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4702ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4703a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4704ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4705ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4706ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
470730773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
470830773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4709ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4710ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4711ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
471203dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
471303dafb5fSChen Gang 			old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
471403dafb5fSChen Gang 							 GFP_KERNEL);
471503dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
471603dafb5fSChen Gang 				for (j = 0; j < i; j++)
471703dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
47183e36a163SWei Yongjun 				kfree(orig_data);
471903dafb5fSChen Gang 				return -ENOMEM;
472003dafb5fSChen Gang 			}
472103dafb5fSChen Gang 		} else
472203dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
4723ac27a0ecSDave Kleikamp #endif
4724b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4725b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4726ac27a0ecSDave Kleikamp 
4727ac27a0ecSDave Kleikamp 	/*
4728ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
4729ac27a0ecSDave Kleikamp 	 */
4730661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
4731ac27a0ecSDave Kleikamp 		err = -EINVAL;
4732ac27a0ecSDave Kleikamp 		goto restore_opts;
4733ac27a0ecSDave Kleikamp 	}
4734ac27a0ecSDave Kleikamp 
47354ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4736c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4737ac27a0ecSDave Kleikamp 
4738ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4739482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4740ac27a0ecSDave Kleikamp 
4741ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4742ac27a0ecSDave Kleikamp 
4743b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4744617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4745b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4746b3881f74STheodore Ts'o 	}
4747ac27a0ecSDave Kleikamp 
4748661aa520SEric Sandeen 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
47494ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4750ac27a0ecSDave Kleikamp 			err = -EROFS;
4751ac27a0ecSDave Kleikamp 			goto restore_opts;
4752ac27a0ecSDave Kleikamp 		}
4753ac27a0ecSDave Kleikamp 
4754ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
47550f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
47560f0dd62fSChristoph Hellwig 			if (err < 0)
4757c79d967dSChristoph Hellwig 				goto restore_opts;
4758c79d967dSChristoph Hellwig 
4759ac27a0ecSDave Kleikamp 			/*
4760ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4761ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4762ac27a0ecSDave Kleikamp 			 */
4763ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4764ac27a0ecSDave Kleikamp 
4765ac27a0ecSDave Kleikamp 			/*
4766ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4767ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4768ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4769ac27a0ecSDave Kleikamp 			 */
4770617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4771617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4772ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4773ac27a0ecSDave Kleikamp 
4774a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4775617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4776ac27a0ecSDave Kleikamp 		} else {
4777a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4778a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
4779ac27a0ecSDave Kleikamp 				err = -EROFS;
4780ac27a0ecSDave Kleikamp 				goto restore_opts;
4781ac27a0ecSDave Kleikamp 			}
4782ead6596bSEric Sandeen 			/*
47838a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
47840b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
47858a266467STheodore Ts'o 			 */
47868a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
47878a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
47888a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
47898a266467STheodore Ts'o 
4790feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
4791b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4792b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
47938a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
47948a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
47958a266467STheodore Ts'o 					err = -EINVAL;
47968a266467STheodore Ts'o 					goto restore_opts;
47978a266467STheodore Ts'o 				}
47988a266467STheodore Ts'o 			}
47998a266467STheodore Ts'o 
48008a266467STheodore Ts'o 			/*
4801ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4802ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4803ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4804ead6596bSEric Sandeen 			 */
4805ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4806b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4807ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4808ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4809b31e1552SEric Sandeen 				       "umount/remount instead");
4810ead6596bSEric Sandeen 				err = -EINVAL;
4811ead6596bSEric Sandeen 				goto restore_opts;
4812ead6596bSEric Sandeen 			}
4813ead6596bSEric Sandeen 
4814ac27a0ecSDave Kleikamp 			/*
4815ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4816ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4817ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4818ac27a0ecSDave Kleikamp 			 * the partition.)
4819ac27a0ecSDave Kleikamp 			 */
48200390131bSFrank Mayhar 			if (sbi->s_journal)
4821617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4822ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4823617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4824ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4825c5e06d10SJohann Lombardi 			if (EXT4_HAS_INCOMPAT_FEATURE(sb,
4826c5e06d10SJohann Lombardi 						     EXT4_FEATURE_INCOMPAT_MMP))
4827c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
4828c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
4829c5e06d10SJohann Lombardi 					err = -EROFS;
4830c5e06d10SJohann Lombardi 					goto restore_opts;
4831c5e06d10SJohann Lombardi 				}
4832c79d967dSChristoph Hellwig 			enable_quota = 1;
4833ac27a0ecSDave Kleikamp 		}
4834ac27a0ecSDave Kleikamp 	}
4835bfff6873SLukas Czerner 
4836bfff6873SLukas Czerner 	/*
4837bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4838bfff6873SLukas Czerner 	 * current settings
4839bfff6873SLukas Czerner 	 */
4840bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4841bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4842bfff6873SLukas Czerner 	else {
4843bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4844bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4845bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4846bfff6873SLukas Czerner 	}
4847bfff6873SLukas Czerner 
48486fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
4849d096ad0fSMichael Tokarev 	if (sbi->s_journal == NULL && !(old_sb_flags & MS_RDONLY))
4850e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
48510390131bSFrank Mayhar 
4852ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4853ac27a0ecSDave Kleikamp 	/* Release old quota file names */
4854ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4855ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
48567c319d32SAditya Kali 	if (enable_quota) {
48577c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
48580f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
48597c319d32SAditya Kali 		else if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
48607c319d32SAditya Kali 					EXT4_FEATURE_RO_COMPAT_QUOTA)) {
48617c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
486207724f98STheodore Ts'o 			if (err)
48637c319d32SAditya Kali 				goto restore_opts;
48647c319d32SAditya Kali 		}
48657c319d32SAditya Kali 	}
48667c319d32SAditya Kali #endif
4867d4c402d9SCurt Wohlgemuth 
4868d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4869d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4870ac27a0ecSDave Kleikamp 	return 0;
48710b8e58a1SAndreas Dilger 
4872ac27a0ecSDave Kleikamp restore_opts:
4873ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
4874ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
4875a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
4876ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
4877ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
4878ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
487930773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
488030773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
4881ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4882ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
4883ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
4884ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4885ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
4886ac27a0ecSDave Kleikamp 	}
4887ac27a0ecSDave Kleikamp #endif
4888d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4889ac27a0ecSDave Kleikamp 	return err;
4890ac27a0ecSDave Kleikamp }
4891ac27a0ecSDave Kleikamp 
4892617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
4893ac27a0ecSDave Kleikamp {
4894ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
4895617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4896617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
489727dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
4898960cc398SPekka Enberg 	u64 fsid;
4899d02a9391SKazuya Mio 	s64 bfree;
490027dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
4901ac27a0ecSDave Kleikamp 
4902952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
4903952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
4904ac27a0ecSDave Kleikamp 
4905617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
4906ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
4907b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
490857042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
490957042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
4910d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
491157042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
491227dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
491327dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
491427dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
4915ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
4916ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
491752d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
4918617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
4919960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
4920960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
4921960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
4922960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
49230b8e58a1SAndreas Dilger 
4924ac27a0ecSDave Kleikamp 	return 0;
4925ac27a0ecSDave Kleikamp }
4926ac27a0ecSDave Kleikamp 
49270b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
49280b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
4929ac27a0ecSDave Kleikamp  * Process 1                         Process 2
4930617ba13bSMingming Cao  * ext4_create()                     quota_sync()
4931dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
4932871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
4933dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
4934ac27a0ecSDave Kleikamp  *
4935ac27a0ecSDave Kleikamp  */
4936ac27a0ecSDave Kleikamp 
4937ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4938ac27a0ecSDave Kleikamp 
4939ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
4940ac27a0ecSDave Kleikamp {
49414c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
4942ac27a0ecSDave Kleikamp }
4943ac27a0ecSDave Kleikamp 
4944617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
4945ac27a0ecSDave Kleikamp {
4946ac27a0ecSDave Kleikamp 	int ret, err;
4947ac27a0ecSDave Kleikamp 	handle_t *handle;
4948ac27a0ecSDave Kleikamp 	struct inode *inode;
4949ac27a0ecSDave Kleikamp 
4950ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
49519924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
4952617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
4953ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4954ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4955ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
4956617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4957ac27a0ecSDave Kleikamp 	if (!ret)
4958ac27a0ecSDave Kleikamp 		ret = err;
4959ac27a0ecSDave Kleikamp 	return ret;
4960ac27a0ecSDave Kleikamp }
4961ac27a0ecSDave Kleikamp 
4962617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
4963ac27a0ecSDave Kleikamp {
4964ac27a0ecSDave Kleikamp 	int ret, err;
4965ac27a0ecSDave Kleikamp 	handle_t *handle;
4966ac27a0ecSDave Kleikamp 
49679924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
4968617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
4969ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4970ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4971ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
4972617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4973ac27a0ecSDave Kleikamp 	if (!ret)
4974ac27a0ecSDave Kleikamp 		ret = err;
4975ac27a0ecSDave Kleikamp 	return ret;
4976ac27a0ecSDave Kleikamp }
4977ac27a0ecSDave Kleikamp 
4978617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
4979ac27a0ecSDave Kleikamp {
4980ac27a0ecSDave Kleikamp 	int ret, err;
4981ac27a0ecSDave Kleikamp 	handle_t *handle;
4982ac27a0ecSDave Kleikamp 
49839924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
4984617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
49859c3013e9SJan Kara 	if (IS_ERR(handle)) {
49869c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
49879c3013e9SJan Kara 		dquot_release(dquot);
4988ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
49899c3013e9SJan Kara 	}
4990ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
4991617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4992ac27a0ecSDave Kleikamp 	if (!ret)
4993ac27a0ecSDave Kleikamp 		ret = err;
4994ac27a0ecSDave Kleikamp 	return ret;
4995ac27a0ecSDave Kleikamp }
4996ac27a0ecSDave Kleikamp 
4997617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
4998ac27a0ecSDave Kleikamp {
4999262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5000262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5001262b4662SJan Kara 
50022c8be6b2SJan Kara 	/* Are we journaling quotas? */
5003262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) ||
5004262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5005ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5006617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5007ac27a0ecSDave Kleikamp 	} else {
5008ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5009ac27a0ecSDave Kleikamp 	}
5010ac27a0ecSDave Kleikamp }
5011ac27a0ecSDave Kleikamp 
5012617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5013ac27a0ecSDave Kleikamp {
5014ac27a0ecSDave Kleikamp 	int ret, err;
5015ac27a0ecSDave Kleikamp 	handle_t *handle;
5016ac27a0ecSDave Kleikamp 
5017ac27a0ecSDave Kleikamp 	/* Data block + inode block */
50189924a92aSTheodore Ts'o 	handle = ext4_journal_start(sb->s_root->d_inode, EXT4_HT_QUOTA, 2);
5019ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5020ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5021ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5022617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5023ac27a0ecSDave Kleikamp 	if (!ret)
5024ac27a0ecSDave Kleikamp 		ret = err;
5025ac27a0ecSDave Kleikamp 	return ret;
5026ac27a0ecSDave Kleikamp }
5027ac27a0ecSDave Kleikamp 
5028ac27a0ecSDave Kleikamp /*
5029ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5030ac27a0ecSDave Kleikamp  * the quota file and such...
5031ac27a0ecSDave Kleikamp  */
5032617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5033ac27a0ecSDave Kleikamp {
5034287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
5035617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5036ac27a0ecSDave Kleikamp }
5037ac27a0ecSDave Kleikamp 
5038ac27a0ecSDave Kleikamp /*
5039ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5040ac27a0ecSDave Kleikamp  */
5041617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
5042f00c9e44SJan Kara 			 struct path *path)
5043ac27a0ecSDave Kleikamp {
5044ac27a0ecSDave Kleikamp 	int err;
5045ac27a0ecSDave Kleikamp 
5046ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5047ac27a0ecSDave Kleikamp 		return -EINVAL;
50480623543bSJan Kara 
5049ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5050d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5051ac27a0ecSDave Kleikamp 		return -EXDEV;
50520623543bSJan Kara 	/* Journaling quota? */
50530623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
50542b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5055f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5056b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5057b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5058b31e1552SEric Sandeen 				"Journaled quota will not work");
50590623543bSJan Kara 	}
50600623543bSJan Kara 
50610623543bSJan Kara 	/*
50620623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
50630623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
50640623543bSJan Kara 	 */
50650390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
5066f00c9e44SJan Kara 	    ext4_should_journal_data(path->dentry->d_inode)) {
50670623543bSJan Kara 		/*
50680623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
50690623543bSJan Kara 		 * otherwise be livelocked...
50700623543bSJan Kara 		 */
50710623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
50727ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
50730623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5074f00c9e44SJan Kara 		if (err)
50757ffe1ea8SHidehiro Kawai 			return err;
50767ffe1ea8SHidehiro Kawai 	}
50770623543bSJan Kara 
5078f00c9e44SJan Kara 	return dquot_quota_on(sb, type, format_id, path);
5079ac27a0ecSDave Kleikamp }
5080ac27a0ecSDave Kleikamp 
50817c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
50827c319d32SAditya Kali 			     unsigned int flags)
50837c319d32SAditya Kali {
50847c319d32SAditya Kali 	int err;
50857c319d32SAditya Kali 	struct inode *qf_inode;
50867c319d32SAditya Kali 	unsigned long qf_inums[MAXQUOTAS] = {
50877c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
50887c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
50897c319d32SAditya Kali 	};
50907c319d32SAditya Kali 
50917c319d32SAditya Kali 	BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA));
50927c319d32SAditya Kali 
50937c319d32SAditya Kali 	if (!qf_inums[type])
50947c319d32SAditya Kali 		return -EPERM;
50957c319d32SAditya Kali 
50967c319d32SAditya Kali 	qf_inode = ext4_iget(sb, qf_inums[type]);
50977c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
50987c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
50997c319d32SAditya Kali 		return PTR_ERR(qf_inode);
51007c319d32SAditya Kali 	}
51017c319d32SAditya Kali 
5102bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5103bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
51047c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
51057c319d32SAditya Kali 	iput(qf_inode);
51067c319d32SAditya Kali 
51077c319d32SAditya Kali 	return err;
51087c319d32SAditya Kali }
51097c319d32SAditya Kali 
51107c319d32SAditya Kali /* Enable usage tracking for all quota types. */
51117c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
51127c319d32SAditya Kali {
51137c319d32SAditya Kali 	int type, err = 0;
51147c319d32SAditya Kali 	unsigned long qf_inums[MAXQUOTAS] = {
51157c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
51167c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
51177c319d32SAditya Kali 	};
51187c319d32SAditya Kali 
51197c319d32SAditya Kali 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
51207c319d32SAditya Kali 	for (type = 0; type < MAXQUOTAS; type++) {
51217c319d32SAditya Kali 		if (qf_inums[type]) {
51227c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
51237c319d32SAditya Kali 						DQUOT_USAGE_ENABLED);
51247c319d32SAditya Kali 			if (err) {
51257c319d32SAditya Kali 				ext4_warning(sb,
512672ba7450STheodore Ts'o 					"Failed to enable quota tracking "
512772ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
512872ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
51297c319d32SAditya Kali 				return err;
51307c319d32SAditya Kali 			}
51317c319d32SAditya Kali 		}
51327c319d32SAditya Kali 	}
51337c319d32SAditya Kali 	return 0;
51347c319d32SAditya Kali }
51357c319d32SAditya Kali 
51367c319d32SAditya Kali /*
51377c319d32SAditya Kali  * quota_on function that is used when QUOTA feature is set.
51387c319d32SAditya Kali  */
51397c319d32SAditya Kali static int ext4_quota_on_sysfile(struct super_block *sb, int type,
51407c319d32SAditya Kali 				 int format_id)
51417c319d32SAditya Kali {
51427c319d32SAditya Kali 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
51437c319d32SAditya Kali 		return -EINVAL;
51447c319d32SAditya Kali 
51457c319d32SAditya Kali 	/*
51467c319d32SAditya Kali 	 * USAGE was enabled at mount time. Only need to enable LIMITS now.
51477c319d32SAditya Kali 	 */
51487c319d32SAditya Kali 	return ext4_quota_enable(sb, type, format_id, DQUOT_LIMITS_ENABLED);
51497c319d32SAditya Kali }
51507c319d32SAditya Kali 
5151ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5152ca0e05e4SDmitry Monakhov {
515321f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
515421f97697SJan Kara 	handle_t *handle;
515521f97697SJan Kara 
515687009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
515787009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
515887009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5159ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5160ca0e05e4SDmitry Monakhov 
51610b268590SAmir Goldstein 	if (!inode)
51620b268590SAmir Goldstein 		goto out;
51630b268590SAmir Goldstein 
516421f97697SJan Kara 	/* Update modification times of quota files when userspace can
516521f97697SJan Kara 	 * start looking at them */
51669924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
516721f97697SJan Kara 	if (IS_ERR(handle))
516821f97697SJan Kara 		goto out;
516921f97697SJan Kara 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
517021f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
517121f97697SJan Kara 	ext4_journal_stop(handle);
517221f97697SJan Kara 
517321f97697SJan Kara out:
5174ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5175ca0e05e4SDmitry Monakhov }
5176ca0e05e4SDmitry Monakhov 
51777c319d32SAditya Kali /*
51787c319d32SAditya Kali  * quota_off function that is used when QUOTA feature is set.
51797c319d32SAditya Kali  */
51807c319d32SAditya Kali static int ext4_quota_off_sysfile(struct super_block *sb, int type)
51817c319d32SAditya Kali {
51827c319d32SAditya Kali 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
51837c319d32SAditya Kali 		return -EINVAL;
51847c319d32SAditya Kali 
51857c319d32SAditya Kali 	/* Disable only the limits. */
51867c319d32SAditya Kali 	return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
51877c319d32SAditya Kali }
51887c319d32SAditya Kali 
5189ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5190ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5191ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5192ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5193617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5194ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5195ac27a0ecSDave Kleikamp {
5196ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5197725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5198ac27a0ecSDave Kleikamp 	int err = 0;
5199ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5200ac27a0ecSDave Kleikamp 	int tocopy;
5201ac27a0ecSDave Kleikamp 	size_t toread;
5202ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5203ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5204ac27a0ecSDave Kleikamp 
5205ac27a0ecSDave Kleikamp 	if (off > i_size)
5206ac27a0ecSDave Kleikamp 		return 0;
5207ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5208ac27a0ecSDave Kleikamp 		len = i_size-off;
5209ac27a0ecSDave Kleikamp 	toread = len;
5210ac27a0ecSDave Kleikamp 	while (toread > 0) {
5211ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5212ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
5213617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
5214ac27a0ecSDave Kleikamp 		if (err)
5215ac27a0ecSDave Kleikamp 			return err;
5216ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5217ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5218ac27a0ecSDave Kleikamp 		else
5219ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5220ac27a0ecSDave Kleikamp 		brelse(bh);
5221ac27a0ecSDave Kleikamp 		offset = 0;
5222ac27a0ecSDave Kleikamp 		toread -= tocopy;
5223ac27a0ecSDave Kleikamp 		data += tocopy;
5224ac27a0ecSDave Kleikamp 		blk++;
5225ac27a0ecSDave Kleikamp 	}
5226ac27a0ecSDave Kleikamp 	return len;
5227ac27a0ecSDave Kleikamp }
5228ac27a0ecSDave Kleikamp 
5229ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5230ac27a0ecSDave Kleikamp  * enough credits) */
5231617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5232ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5233ac27a0ecSDave Kleikamp {
5234ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5235725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5236ac27a0ecSDave Kleikamp 	int err = 0;
5237ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5238ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5239ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5240ac27a0ecSDave Kleikamp 
52410390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5242b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5243b31e1552SEric Sandeen 			" cancelled because transaction is not started",
52449c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
52459c3013e9SJan Kara 		return -EIO;
52469c3013e9SJan Kara 	}
524767eeb568SDmitry Monakhov 	/*
524867eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
524967eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
525067eeb568SDmitry Monakhov 	 */
525167eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
525267eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
525367eeb568SDmitry Monakhov 			" cancelled because not block aligned",
525467eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
525567eeb568SDmitry Monakhov 		return -EIO;
525667eeb568SDmitry Monakhov 	}
525767eeb568SDmitry Monakhov 
5258617ba13bSMingming Cao 	bh = ext4_bread(handle, inode, blk, 1, &err);
5259ac27a0ecSDave Kleikamp 	if (!bh)
5260ac27a0ecSDave Kleikamp 		goto out;
5261617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5262ac27a0ecSDave Kleikamp 	if (err) {
5263ac27a0ecSDave Kleikamp 		brelse(bh);
5264ac27a0ecSDave Kleikamp 		goto out;
5265ac27a0ecSDave Kleikamp 	}
5266ac27a0ecSDave Kleikamp 	lock_buffer(bh);
526767eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5268ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5269ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
52700390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5271ac27a0ecSDave Kleikamp 	brelse(bh);
5272ac27a0ecSDave Kleikamp out:
52730b7f7cefSJan Kara 	if (err)
5274ac27a0ecSDave Kleikamp 		return err;
527567eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
527667eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
5277617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
5278617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
527921f97697SJan Kara 	}
528067eeb568SDmitry Monakhov 	return len;
5281ac27a0ecSDave Kleikamp }
5282ac27a0ecSDave Kleikamp 
5283ac27a0ecSDave Kleikamp #endif
5284ac27a0ecSDave Kleikamp 
5285152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5286152a0836SAl Viro 		       const char *dev_name, void *data)
5287ac27a0ecSDave Kleikamp {
5288152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
5289ac27a0ecSDave Kleikamp }
5290ac27a0ecSDave Kleikamp 
529137f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
529224b58424STheodore Ts'o static inline void register_as_ext2(void)
529324b58424STheodore Ts'o {
529424b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
529524b58424STheodore Ts'o 	if (err)
529624b58424STheodore Ts'o 		printk(KERN_WARNING
529724b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
529824b58424STheodore Ts'o }
529924b58424STheodore Ts'o 
530024b58424STheodore Ts'o static inline void unregister_as_ext2(void)
530124b58424STheodore Ts'o {
530224b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
530324b58424STheodore Ts'o }
53042035e776STheodore Ts'o 
53052035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
53062035e776STheodore Ts'o {
53072035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
53082035e776STheodore Ts'o 		return 0;
53092035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
53102035e776STheodore Ts'o 		return 1;
53112035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
53122035e776STheodore Ts'o 		return 0;
53132035e776STheodore Ts'o 	return 1;
53142035e776STheodore Ts'o }
531524b58424STheodore Ts'o #else
531624b58424STheodore Ts'o static inline void register_as_ext2(void) { }
531724b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
53182035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
531924b58424STheodore Ts'o #endif
532024b58424STheodore Ts'o 
532137f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
532224b58424STheodore Ts'o static inline void register_as_ext3(void)
532324b58424STheodore Ts'o {
532424b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
532524b58424STheodore Ts'o 	if (err)
532624b58424STheodore Ts'o 		printk(KERN_WARNING
532724b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
532824b58424STheodore Ts'o }
532924b58424STheodore Ts'o 
533024b58424STheodore Ts'o static inline void unregister_as_ext3(void)
533124b58424STheodore Ts'o {
533224b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
533324b58424STheodore Ts'o }
53342035e776STheodore Ts'o 
53352035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
53362035e776STheodore Ts'o {
53372035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
53382035e776STheodore Ts'o 		return 0;
53392035e776STheodore Ts'o 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
53402035e776STheodore Ts'o 		return 0;
53412035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
53422035e776STheodore Ts'o 		return 1;
53432035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
53442035e776STheodore Ts'o 		return 0;
53452035e776STheodore Ts'o 	return 1;
53462035e776STheodore Ts'o }
534724b58424STheodore Ts'o #else
534824b58424STheodore Ts'o static inline void register_as_ext3(void) { }
534924b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
53502035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
535124b58424STheodore Ts'o #endif
535224b58424STheodore Ts'o 
535303010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
5354ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
535503010a33STheodore Ts'o 	.name		= "ext4",
5356152a0836SAl Viro 	.mount		= ext4_mount,
5357ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
5358ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
5359ac27a0ecSDave Kleikamp };
53607f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
5361ac27a0ecSDave Kleikamp 
53628f021222SLukas Czerner static int __init ext4_init_feat_adverts(void)
5363857ac889SLukas Czerner {
5364857ac889SLukas Czerner 	struct ext4_features *ef;
5365857ac889SLukas Czerner 	int ret = -ENOMEM;
5366857ac889SLukas Czerner 
5367857ac889SLukas Czerner 	ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
5368857ac889SLukas Czerner 	if (!ef)
5369857ac889SLukas Czerner 		goto out;
5370857ac889SLukas Czerner 
5371857ac889SLukas Czerner 	ef->f_kobj.kset = ext4_kset;
5372857ac889SLukas Czerner 	init_completion(&ef->f_kobj_unregister);
5373857ac889SLukas Czerner 	ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
5374857ac889SLukas Czerner 				   "features");
5375857ac889SLukas Czerner 	if (ret) {
5376857ac889SLukas Czerner 		kfree(ef);
5377857ac889SLukas Czerner 		goto out;
5378857ac889SLukas Czerner 	}
5379857ac889SLukas Czerner 
5380857ac889SLukas Czerner 	ext4_feat = ef;
5381857ac889SLukas Czerner 	ret = 0;
5382857ac889SLukas Czerner out:
5383857ac889SLukas Czerner 	return ret;
5384857ac889SLukas Czerner }
5385857ac889SLukas Czerner 
53868f021222SLukas Czerner static void ext4_exit_feat_adverts(void)
53878f021222SLukas Czerner {
53888f021222SLukas Czerner 	kobject_put(&ext4_feat->f_kobj);
53898f021222SLukas Czerner 	wait_for_completion(&ext4_feat->f_kobj_unregister);
53908f021222SLukas Czerner 	kfree(ext4_feat);
53918f021222SLukas Czerner }
53928f021222SLukas Czerner 
5393e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
5394e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5395e9e3bcecSEric Sandeen struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
5396e9e3bcecSEric Sandeen 
53975dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
5398ac27a0ecSDave Kleikamp {
5399e9e3bcecSEric Sandeen 	int i, err;
5400c9de560dSAlex Tomas 
540107c0c5d8SAl Viro 	ext4_li_info = NULL;
540207c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
540307c0c5d8SAl Viro 
54049a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
540512e9b892SDmitry Monakhov 	ext4_check_flag_values();
5406e9e3bcecSEric Sandeen 
5407e9e3bcecSEric Sandeen 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
5408e9e3bcecSEric Sandeen 		mutex_init(&ext4__aio_mutex[i]);
5409e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
5410e9e3bcecSEric Sandeen 	}
5411e9e3bcecSEric Sandeen 
541251865fdaSZheng Liu 	err = ext4_init_es();
54136fd058f7STheodore Ts'o 	if (err)
54146fd058f7STheodore Ts'o 		return err;
541551865fdaSZheng Liu 
541651865fdaSZheng Liu 	err = ext4_init_pageio();
541751865fdaSZheng Liu 	if (err)
541851865fdaSZheng Liu 		goto out7;
541951865fdaSZheng Liu 
54205dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
5421bd2d0210STheodore Ts'o 	if (err)
5422d44651d0SFabrice Jouhaud 		goto out6;
54233197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
54240e376b1eSTheodore Ts'o 	if (!ext4_kset) {
54250e376b1eSTheodore Ts'o 		err = -ENOMEM;
5426dd68314cSTheodore Ts'o 		goto out5;
54270e376b1eSTheodore Ts'o 	}
5428d44651d0SFabrice Jouhaud 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
5429857ac889SLukas Czerner 
5430857ac889SLukas Czerner 	err = ext4_init_feat_adverts();
5431dd68314cSTheodore Ts'o 	if (err)
5432dd68314cSTheodore Ts'o 		goto out4;
5433857ac889SLukas Czerner 
54345dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
5435ac27a0ecSDave Kleikamp 	if (err)
54366fd058f7STheodore Ts'o 		goto out3;
5437c9de560dSAlex Tomas 
54385dabfc78STheodore Ts'o 	err = ext4_init_xattr();
5439c9de560dSAlex Tomas 	if (err)
5440c9de560dSAlex Tomas 		goto out2;
5441ac27a0ecSDave Kleikamp 	err = init_inodecache();
5442ac27a0ecSDave Kleikamp 	if (err)
5443ac27a0ecSDave Kleikamp 		goto out1;
544424b58424STheodore Ts'o 	register_as_ext3();
54452035e776STheodore Ts'o 	register_as_ext2();
544603010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
5447ac27a0ecSDave Kleikamp 	if (err)
5448ac27a0ecSDave Kleikamp 		goto out;
5449bfff6873SLukas Czerner 
5450ac27a0ecSDave Kleikamp 	return 0;
5451ac27a0ecSDave Kleikamp out:
545224b58424STheodore Ts'o 	unregister_as_ext2();
545324b58424STheodore Ts'o 	unregister_as_ext3();
5454ac27a0ecSDave Kleikamp 	destroy_inodecache();
5455ac27a0ecSDave Kleikamp out1:
54565dabfc78STheodore Ts'o 	ext4_exit_xattr();
5457c9de560dSAlex Tomas out2:
54585dabfc78STheodore Ts'o 	ext4_exit_mballoc();
54596fd058f7STheodore Ts'o out3:
54608f021222SLukas Czerner 	ext4_exit_feat_adverts();
54616fd058f7STheodore Ts'o out4:
5462d44651d0SFabrice Jouhaud 	if (ext4_proc_root)
5463dd68314cSTheodore Ts'o 		remove_proc_entry("fs/ext4", NULL);
5464dd68314cSTheodore Ts'o 	kset_unregister(ext4_kset);
5465d44651d0SFabrice Jouhaud out5:
5466dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5467d44651d0SFabrice Jouhaud out6:
54685dabfc78STheodore Ts'o 	ext4_exit_pageio();
546951865fdaSZheng Liu out7:
547051865fdaSZheng Liu 	ext4_exit_es();
547151865fdaSZheng Liu 
5472ac27a0ecSDave Kleikamp 	return err;
5473ac27a0ecSDave Kleikamp }
5474ac27a0ecSDave Kleikamp 
54755dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5476ac27a0ecSDave Kleikamp {
5477bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
547824b58424STheodore Ts'o 	unregister_as_ext2();
547924b58424STheodore Ts'o 	unregister_as_ext3();
548003010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5481ac27a0ecSDave Kleikamp 	destroy_inodecache();
54825dabfc78STheodore Ts'o 	ext4_exit_xattr();
54835dabfc78STheodore Ts'o 	ext4_exit_mballoc();
54848f021222SLukas Czerner 	ext4_exit_feat_adverts();
54859f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
54863197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
54875dabfc78STheodore Ts'o 	ext4_exit_system_zone();
54885dabfc78STheodore Ts'o 	ext4_exit_pageio();
5489ac27a0ecSDave Kleikamp }
5490ac27a0ecSDave Kleikamp 
5491ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
549283982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5493ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
54945dabfc78STheodore Ts'o module_init(ext4_init_fs)
54955dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5496