xref: /linux/fs/ext4/super.c (revision bc0b75f77a944b482293972eb8fd5c88c576eb46)
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"
486f91bc5fSEric Gouriou #include "ext4_extents.h"
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);
72617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
73ac27a0ecSDave Kleikamp 				     char nbuf[16]);
74617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
75617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
76c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
78152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
79152a0836SAl Viro 		       const char *dev_name, void *data);
802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
82d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
83bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
84bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
858f1f7453SEric Sandeen static void ext4_clear_request_list(void);
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 };
952035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
962035e776STheodore Ts'o #else
972035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
982035e776STheodore Ts'o #endif
992035e776STheodore Ts'o 
1002035e776STheodore Ts'o 
101ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
102ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
103ba69f9abSJan Kara 	.owner		= THIS_MODULE,
104ba69f9abSJan Kara 	.name		= "ext3",
105152a0836SAl Viro 	.mount		= ext4_mount,
106ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
107ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
108ba69f9abSJan Kara };
109ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
110ba69f9abSJan Kara #else
111ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0)
112ba69f9abSJan Kara #endif
113bd81d8eeSLaurent Vivier 
114d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
115d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
116d25425f8SDarrick J. Wong {
117d25425f8SDarrick J. Wong 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
118d25425f8SDarrick J. Wong 					EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
119d25425f8SDarrick J. Wong 		return 1;
120d25425f8SDarrick J. Wong 
121d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
122d25425f8SDarrick J. Wong }
123d25425f8SDarrick J. Wong 
124a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
125a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
126a9c47317SDarrick J. Wong {
127a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
128a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
129a9c47317SDarrick J. Wong 	__u32 csum;
130a9c47317SDarrick J. Wong 
131a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
132a9c47317SDarrick J. Wong 
133a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
134a9c47317SDarrick J. Wong }
135a9c47317SDarrick J. Wong 
136a9c47317SDarrick J. Wong int ext4_superblock_csum_verify(struct super_block *sb,
137a9c47317SDarrick J. Wong 				struct ext4_super_block *es)
138a9c47317SDarrick J. Wong {
139a9c47317SDarrick J. Wong 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
140a9c47317SDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
141a9c47317SDarrick J. Wong 		return 1;
142a9c47317SDarrick J. Wong 
143a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
144a9c47317SDarrick J. Wong }
145a9c47317SDarrick J. Wong 
146a9c47317SDarrick J. Wong void ext4_superblock_csum_set(struct super_block *sb,
147a9c47317SDarrick J. Wong 			      struct ext4_super_block *es)
148a9c47317SDarrick J. Wong {
149a9c47317SDarrick J. Wong 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
150a9c47317SDarrick J. Wong 		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
151a9c47317SDarrick J. Wong 		return;
152a9c47317SDarrick J. Wong 
153a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
154a9c47317SDarrick J. Wong }
155a9c47317SDarrick J. Wong 
1569933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
1579933fc0aSTheodore Ts'o {
1589933fc0aSTheodore Ts'o 	void *ret;
1599933fc0aSTheodore Ts'o 
1609933fc0aSTheodore Ts'o 	ret = kmalloc(size, flags);
1619933fc0aSTheodore Ts'o 	if (!ret)
1629933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
1639933fc0aSTheodore Ts'o 	return ret;
1649933fc0aSTheodore Ts'o }
1659933fc0aSTheodore Ts'o 
1669933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
1679933fc0aSTheodore Ts'o {
1689933fc0aSTheodore Ts'o 	void *ret;
1699933fc0aSTheodore Ts'o 
170db9481c0SMathias Krause 	ret = kzalloc(size, flags);
1719933fc0aSTheodore Ts'o 	if (!ret)
1729933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
1739933fc0aSTheodore Ts'o 	return ret;
1749933fc0aSTheodore Ts'o }
1759933fc0aSTheodore Ts'o 
1769933fc0aSTheodore Ts'o void ext4_kvfree(void *ptr)
1779933fc0aSTheodore Ts'o {
1789933fc0aSTheodore Ts'o 	if (is_vmalloc_addr(ptr))
1799933fc0aSTheodore Ts'o 		vfree(ptr);
1809933fc0aSTheodore Ts'o 	else
1819933fc0aSTheodore Ts'o 		kfree(ptr);
1829933fc0aSTheodore Ts'o 
1839933fc0aSTheodore Ts'o }
1849933fc0aSTheodore Ts'o 
1858fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
1868fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
187bd81d8eeSLaurent Vivier {
1883a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
1898fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1908fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
191bd81d8eeSLaurent Vivier }
192bd81d8eeSLaurent Vivier 
1938fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
1948fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
195bd81d8eeSLaurent Vivier {
1965272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
1978fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1988fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
199bd81d8eeSLaurent Vivier }
200bd81d8eeSLaurent Vivier 
2018fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
2028fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
203bd81d8eeSLaurent Vivier {
2045272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
2058fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2068fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
207bd81d8eeSLaurent Vivier }
208bd81d8eeSLaurent Vivier 
209021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
210560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
211560671a0SAneesh Kumar K.V {
212560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
213560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
214560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
215560671a0SAneesh Kumar K.V }
216560671a0SAneesh Kumar K.V 
217560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
218560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
219560671a0SAneesh Kumar K.V {
220560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
221560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
222560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
223560671a0SAneesh Kumar K.V }
224560671a0SAneesh Kumar K.V 
225560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
226560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
227560671a0SAneesh Kumar K.V {
228560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
229560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
230560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
231560671a0SAneesh Kumar K.V }
232560671a0SAneesh Kumar K.V 
233560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
234560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
235560671a0SAneesh Kumar K.V {
236560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
237560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
238560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
239560671a0SAneesh Kumar K.V }
240560671a0SAneesh Kumar K.V 
2418fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2428fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
243bd81d8eeSLaurent Vivier {
2443a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2458fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2468fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
247bd81d8eeSLaurent Vivier }
248bd81d8eeSLaurent Vivier 
2498fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2508fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
251bd81d8eeSLaurent Vivier {
2525272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2538fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2548fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
255bd81d8eeSLaurent Vivier }
256bd81d8eeSLaurent Vivier 
2578fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2588fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
259bd81d8eeSLaurent Vivier {
2605272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2618fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2628fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
263bd81d8eeSLaurent Vivier }
264bd81d8eeSLaurent Vivier 
265021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
266560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
267560671a0SAneesh Kumar K.V {
268560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
269560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
270560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
271560671a0SAneesh Kumar K.V }
272560671a0SAneesh Kumar K.V 
273560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
274560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
275560671a0SAneesh Kumar K.V {
276560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
277560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
278560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
279560671a0SAneesh Kumar K.V }
280560671a0SAneesh Kumar K.V 
281560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
282560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
283560671a0SAneesh Kumar K.V {
284560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
285560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
286560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
287560671a0SAneesh Kumar K.V }
288560671a0SAneesh Kumar K.V 
289560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
290560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
291560671a0SAneesh Kumar K.V {
292560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
293560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
294560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
295560671a0SAneesh Kumar K.V }
296560671a0SAneesh Kumar K.V 
297d3d1faf6SCurt Wohlgemuth 
298d3d1faf6SCurt Wohlgemuth /* Just increment the non-pointer handle value */
299d3d1faf6SCurt Wohlgemuth static handle_t *ext4_get_nojournal(void)
300d3d1faf6SCurt Wohlgemuth {
301d3d1faf6SCurt Wohlgemuth 	handle_t *handle = current->journal_info;
302d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
303d3d1faf6SCurt Wohlgemuth 
304d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
305d3d1faf6SCurt Wohlgemuth 
306d3d1faf6SCurt Wohlgemuth 	ref_cnt++;
307d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
308d3d1faf6SCurt Wohlgemuth 
309d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
310d3d1faf6SCurt Wohlgemuth 	return handle;
311d3d1faf6SCurt Wohlgemuth }
312d3d1faf6SCurt Wohlgemuth 
313d3d1faf6SCurt Wohlgemuth 
314d3d1faf6SCurt Wohlgemuth /* Decrement the non-pointer handle value */
315d3d1faf6SCurt Wohlgemuth static void ext4_put_nojournal(handle_t *handle)
316d3d1faf6SCurt Wohlgemuth {
317d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
318d3d1faf6SCurt Wohlgemuth 
319d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt == 0);
320d3d1faf6SCurt Wohlgemuth 
321d3d1faf6SCurt Wohlgemuth 	ref_cnt--;
322d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
323d3d1faf6SCurt Wohlgemuth 
324d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
325d3d1faf6SCurt Wohlgemuth }
326d3d1faf6SCurt Wohlgemuth 
327ac27a0ecSDave Kleikamp /*
328dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
329ac27a0ecSDave Kleikamp  *
330ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
331ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
332ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
333ac27a0ecSDave Kleikamp  * appropriate.
334ac27a0ecSDave Kleikamp  */
335617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
336ac27a0ecSDave Kleikamp {
337ac27a0ecSDave Kleikamp 	journal_t *journal;
338ac27a0ecSDave Kleikamp 
33912706394STheodore Ts'o 	trace_ext4_journal_start(sb, nblocks, _RET_IP_);
340ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
341ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
342ac27a0ecSDave Kleikamp 
3438e8ad8a5SJan Kara 	WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
344617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
345be4f27d3SYongqiang Yang 	if (!journal)
346be4f27d3SYongqiang Yang 		return ext4_get_nojournal();
347be4f27d3SYongqiang Yang 	/*
348be4f27d3SYongqiang Yang 	 * Special case here: if the journal has aborted behind our
349be4f27d3SYongqiang Yang 	 * backs (eg. EIO in the commit thread), then we still need to
350be4f27d3SYongqiang Yang 	 * take the FS itself readonly cleanly.
351be4f27d3SYongqiang Yang 	 */
352ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
353c67d859eSTheodore Ts'o 		ext4_abort(sb, "Detected aborted journal");
354ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
355ac27a0ecSDave Kleikamp 	}
356dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
357ac27a0ecSDave Kleikamp }
358ac27a0ecSDave Kleikamp 
359ac27a0ecSDave Kleikamp /*
360ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
361dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
362ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
363ac27a0ecSDave Kleikamp  * appropriate.
364ac27a0ecSDave Kleikamp  */
365c398eda0STheodore Ts'o int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
366ac27a0ecSDave Kleikamp {
367ac27a0ecSDave Kleikamp 	struct super_block *sb;
368ac27a0ecSDave Kleikamp 	int err;
369ac27a0ecSDave Kleikamp 	int rc;
370ac27a0ecSDave Kleikamp 
3710390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
372d3d1faf6SCurt Wohlgemuth 		ext4_put_nojournal(handle);
3730390131bSFrank Mayhar 		return 0;
3740390131bSFrank Mayhar 	}
375ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
376ac27a0ecSDave Kleikamp 	err = handle->h_err;
377dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
378ac27a0ecSDave Kleikamp 
379ac27a0ecSDave Kleikamp 	if (!err)
380ac27a0ecSDave Kleikamp 		err = rc;
381ac27a0ecSDave Kleikamp 	if (err)
382c398eda0STheodore Ts'o 		__ext4_std_error(sb, where, line, err);
383ac27a0ecSDave Kleikamp 	return err;
384ac27a0ecSDave Kleikamp }
385ac27a0ecSDave Kleikamp 
38690c7201bSTheodore Ts'o void ext4_journal_abort_handle(const char *caller, unsigned int line,
38790c7201bSTheodore Ts'o 			       const char *err_fn, struct buffer_head *bh,
38890c7201bSTheodore Ts'o 			       handle_t *handle, int err)
389ac27a0ecSDave Kleikamp {
390ac27a0ecSDave Kleikamp 	char nbuf[16];
391617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
392ac27a0ecSDave Kleikamp 
3930390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
3940390131bSFrank Mayhar 
395ac27a0ecSDave Kleikamp 	if (bh)
396ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
397ac27a0ecSDave Kleikamp 
398ac27a0ecSDave Kleikamp 	if (!handle->h_err)
399ac27a0ecSDave Kleikamp 		handle->h_err = err;
400ac27a0ecSDave Kleikamp 
401ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
402ac27a0ecSDave Kleikamp 		return;
403ac27a0ecSDave Kleikamp 
40492b97816STheodore Ts'o 	printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
40590c7201bSTheodore Ts'o 	       caller, line, errstr, err_fn);
406ac27a0ecSDave Kleikamp 
407dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
408ac27a0ecSDave Kleikamp }
409ac27a0ecSDave Kleikamp 
4101c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
4111c13d5c0STheodore Ts'o 			    unsigned int line)
4121c13d5c0STheodore Ts'o {
4131c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4141c13d5c0STheodore Ts'o 
4151c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4161c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4171c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
4181c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
4191c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
4201c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
4211c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
4221c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
4231c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
4241c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
4251c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
4261c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
4271c13d5c0STheodore Ts'o 	}
42866e61a9eSTheodore Ts'o 	/*
42966e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
43066e61a9eSTheodore Ts'o 	 * started already
43166e61a9eSTheodore Ts'o 	 */
43266e61a9eSTheodore Ts'o 	if (!es->s_error_count)
43366e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
4341c13d5c0STheodore Ts'o 	es->s_error_count = cpu_to_le32(le32_to_cpu(es->s_error_count) + 1);
4351c13d5c0STheodore Ts'o }
4361c13d5c0STheodore Ts'o 
4371c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
4381c13d5c0STheodore Ts'o 			    unsigned int line)
4391c13d5c0STheodore Ts'o {
4401c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
4411c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
4421c13d5c0STheodore Ts'o }
4431c13d5c0STheodore Ts'o 
4447c2e7087STheodore Ts'o /*
4457c2e7087STheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
4467c2e7087STheodore Ts'o  * structures, including the bdi structure, without telling anyone
4477c2e7087STheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
4487c2e7087STheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
4497c2e7087STheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
4507c2e7087STheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
4517c2e7087STheodore Ts'o  */
4527c2e7087STheodore Ts'o static int block_device_ejected(struct super_block *sb)
4537c2e7087STheodore Ts'o {
4547c2e7087STheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
4557c2e7087STheodore Ts'o 	struct backing_dev_info *bdi = bd_inode->i_mapping->backing_dev_info;
4567c2e7087STheodore Ts'o 
4577c2e7087STheodore Ts'o 	return bdi->dev == NULL;
4587c2e7087STheodore Ts'o }
4597c2e7087STheodore Ts'o 
46018aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
46118aadd47SBobi Jam {
46218aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
46318aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
46418aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
46518aadd47SBobi Jam 	struct ext4_journal_cb_entry	*jce, *tmp;
46618aadd47SBobi Jam 
46718aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
46818aadd47SBobi Jam 	list_for_each_entry_safe(jce, tmp, &txn->t_private_list, jce_list) {
46918aadd47SBobi Jam 		list_del_init(&jce->jce_list);
47018aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
47118aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
47218aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
47318aadd47SBobi Jam 	}
47418aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
47518aadd47SBobi Jam }
4761c13d5c0STheodore Ts'o 
477ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
478ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
479ac27a0ecSDave Kleikamp  *
480ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
481617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
482ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
483ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
484ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
485ac27a0ecSDave Kleikamp  * write out the superblock safely.
486ac27a0ecSDave Kleikamp  *
487dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
488d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
489ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
490ac27a0ecSDave Kleikamp  */
491ac27a0ecSDave Kleikamp 
492617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
493ac27a0ecSDave Kleikamp {
494ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
495ac27a0ecSDave Kleikamp 		return;
496ac27a0ecSDave Kleikamp 
497ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
498617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
499ac27a0ecSDave Kleikamp 
5004ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
501ac27a0ecSDave Kleikamp 		if (journal)
502dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
503ac27a0ecSDave Kleikamp 	}
504ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
505b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
506ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
507ac27a0ecSDave Kleikamp 	}
508ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
509617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
510ac27a0ecSDave Kleikamp 			sb->s_id);
511ac27a0ecSDave Kleikamp }
512ac27a0ecSDave Kleikamp 
51312062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
514c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
515ac27a0ecSDave Kleikamp {
5160ff2ea7dSJoe Perches 	struct va_format vaf;
517ac27a0ecSDave Kleikamp 	va_list args;
518ac27a0ecSDave Kleikamp 
519ac27a0ecSDave Kleikamp 	va_start(args, fmt);
5200ff2ea7dSJoe Perches 	vaf.fmt = fmt;
5210ff2ea7dSJoe Perches 	vaf.va = &args;
5220ff2ea7dSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
5230ff2ea7dSJoe Perches 	       sb->s_id, function, line, current->comm, &vaf);
524ac27a0ecSDave Kleikamp 	va_end(args);
525f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
526ac27a0ecSDave Kleikamp 
527617ba13bSMingming Cao 	ext4_handle_error(sb);
528ac27a0ecSDave Kleikamp }
529ac27a0ecSDave Kleikamp 
530c398eda0STheodore Ts'o void ext4_error_inode(struct inode *inode, const char *function,
531c398eda0STheodore Ts'o 		      unsigned int line, ext4_fsblk_t block,
532273df556SFrank Mayhar 		      const char *fmt, ...)
533273df556SFrank Mayhar {
534273df556SFrank Mayhar 	va_list args;
535f7c21177STheodore Ts'o 	struct va_format vaf;
5361c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
537273df556SFrank Mayhar 
5381c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
5391c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
5401c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
541273df556SFrank Mayhar 	va_start(args, fmt);
542f7c21177STheodore Ts'o 	vaf.fmt = fmt;
543f7c21177STheodore Ts'o 	vaf.va = &args;
544c398eda0STheodore Ts'o 	if (block)
545d9ee81daSJoe Perches 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
546d9ee81daSJoe Perches 		       "inode #%lu: block %llu: comm %s: %pV\n",
547d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
548d9ee81daSJoe Perches 		       block, current->comm, &vaf);
549d9ee81daSJoe Perches 	else
550d9ee81daSJoe Perches 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
551d9ee81daSJoe Perches 		       "inode #%lu: comm %s: %pV\n",
552d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
553d9ee81daSJoe Perches 		       current->comm, &vaf);
554273df556SFrank Mayhar 	va_end(args);
555273df556SFrank Mayhar 
556273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
557273df556SFrank Mayhar }
558273df556SFrank Mayhar 
559c398eda0STheodore Ts'o void ext4_error_file(struct file *file, const char *function,
560f7c21177STheodore Ts'o 		     unsigned int line, ext4_fsblk_t block,
561f7c21177STheodore Ts'o 		     const char *fmt, ...)
562273df556SFrank Mayhar {
563273df556SFrank Mayhar 	va_list args;
564f7c21177STheodore Ts'o 	struct va_format vaf;
5651c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
566273df556SFrank Mayhar 	struct inode *inode = file->f_dentry->d_inode;
567273df556SFrank Mayhar 	char pathname[80], *path;
568273df556SFrank Mayhar 
5691c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
5701c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
5711c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
572273df556SFrank Mayhar 	path = d_path(&(file->f_path), pathname, sizeof(pathname));
573f9a62d09SDan Carpenter 	if (IS_ERR(path))
574273df556SFrank Mayhar 		path = "(unknown)";
575f7c21177STheodore Ts'o 	va_start(args, fmt);
576f7c21177STheodore Ts'o 	vaf.fmt = fmt;
577f7c21177STheodore Ts'o 	vaf.va = &args;
578d9ee81daSJoe Perches 	if (block)
579d9ee81daSJoe Perches 		printk(KERN_CRIT
580d9ee81daSJoe Perches 		       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
581d9ee81daSJoe Perches 		       "block %llu: comm %s: path %s: %pV\n",
582d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
583d9ee81daSJoe Perches 		       block, current->comm, path, &vaf);
584d9ee81daSJoe Perches 	else
585d9ee81daSJoe Perches 		printk(KERN_CRIT
586d9ee81daSJoe Perches 		       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
587d9ee81daSJoe Perches 		       "comm %s: path %s: %pV\n",
588d9ee81daSJoe Perches 		       inode->i_sb->s_id, function, line, inode->i_ino,
589d9ee81daSJoe Perches 		       current->comm, path, &vaf);
590273df556SFrank Mayhar 	va_end(args);
591273df556SFrank Mayhar 
592273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
593273df556SFrank Mayhar }
594273df556SFrank Mayhar 
595617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
596ac27a0ecSDave Kleikamp 				     char nbuf[16])
597ac27a0ecSDave Kleikamp {
598ac27a0ecSDave Kleikamp 	char *errstr = NULL;
599ac27a0ecSDave Kleikamp 
600ac27a0ecSDave Kleikamp 	switch (errno) {
601ac27a0ecSDave Kleikamp 	case -EIO:
602ac27a0ecSDave Kleikamp 		errstr = "IO failure";
603ac27a0ecSDave Kleikamp 		break;
604ac27a0ecSDave Kleikamp 	case -ENOMEM:
605ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
606ac27a0ecSDave Kleikamp 		break;
607ac27a0ecSDave Kleikamp 	case -EROFS:
60878f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
60978f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
610ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
611ac27a0ecSDave Kleikamp 		else
612ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
613ac27a0ecSDave Kleikamp 		break;
614ac27a0ecSDave Kleikamp 	default:
615ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
616ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
617ac27a0ecSDave Kleikamp 		 * NULL. */
618ac27a0ecSDave Kleikamp 		if (nbuf) {
619ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
620ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
621ac27a0ecSDave Kleikamp 				errstr = nbuf;
622ac27a0ecSDave Kleikamp 		}
623ac27a0ecSDave Kleikamp 		break;
624ac27a0ecSDave Kleikamp 	}
625ac27a0ecSDave Kleikamp 
626ac27a0ecSDave Kleikamp 	return errstr;
627ac27a0ecSDave Kleikamp }
628ac27a0ecSDave Kleikamp 
629617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
630ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
631ac27a0ecSDave Kleikamp 
632c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
633c398eda0STheodore Ts'o 		      unsigned int line, int errno)
634ac27a0ecSDave Kleikamp {
635ac27a0ecSDave Kleikamp 	char nbuf[16];
636ac27a0ecSDave Kleikamp 	const char *errstr;
637ac27a0ecSDave Kleikamp 
638ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
639ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
640ac27a0ecSDave Kleikamp 	 * an error. */
641ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
642ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
643ac27a0ecSDave Kleikamp 		return;
644ac27a0ecSDave Kleikamp 
645617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
646c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
647c398eda0STheodore Ts'o 	       sb->s_id, function, line, errstr);
6481c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
649ac27a0ecSDave Kleikamp 
650617ba13bSMingming Cao 	ext4_handle_error(sb);
651ac27a0ecSDave Kleikamp }
652ac27a0ecSDave Kleikamp 
653ac27a0ecSDave Kleikamp /*
654617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
655ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
656ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
657ac27a0ecSDave Kleikamp  *
658ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
659ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
660ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
661ac27a0ecSDave Kleikamp  */
662ac27a0ecSDave Kleikamp 
663c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
664c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
665ac27a0ecSDave Kleikamp {
666ac27a0ecSDave Kleikamp 	va_list args;
667ac27a0ecSDave Kleikamp 
6681c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
669ac27a0ecSDave Kleikamp 	va_start(args, fmt);
670c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
671c398eda0STheodore Ts'o 	       function, line);
672ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
673ac27a0ecSDave Kleikamp 	printk("\n");
674ac27a0ecSDave Kleikamp 	va_end(args);
675ac27a0ecSDave Kleikamp 
6761c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
677b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
678ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
6794ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
680ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
681dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
6821c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
6831c13d5c0STheodore Ts'o 	}
6841c13d5c0STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC))
6851c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
686ac27a0ecSDave Kleikamp }
687ac27a0ecSDave Kleikamp 
6880ff2ea7dSJoe Perches void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
689b31e1552SEric Sandeen {
6900ff2ea7dSJoe Perches 	struct va_format vaf;
691b31e1552SEric Sandeen 	va_list args;
692b31e1552SEric Sandeen 
693b31e1552SEric Sandeen 	va_start(args, fmt);
6940ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6950ff2ea7dSJoe Perches 	vaf.va = &args;
6960ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
697b31e1552SEric Sandeen 	va_end(args);
698b31e1552SEric Sandeen }
699b31e1552SEric Sandeen 
70012062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
701c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
702ac27a0ecSDave Kleikamp {
7030ff2ea7dSJoe Perches 	struct va_format vaf;
704ac27a0ecSDave Kleikamp 	va_list args;
705ac27a0ecSDave Kleikamp 
706ac27a0ecSDave Kleikamp 	va_start(args, fmt);
7070ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7080ff2ea7dSJoe Perches 	vaf.va = &args;
7090ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
7100ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
711ac27a0ecSDave Kleikamp 	va_end(args);
712ac27a0ecSDave Kleikamp }
713ac27a0ecSDave Kleikamp 
714e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
715e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
716e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
717e29136f8STheodore Ts'o 			     const char *fmt, ...)
7185d1b1b3fSAneesh Kumar K.V __releases(bitlock)
7195d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
7205d1b1b3fSAneesh Kumar K.V {
7210ff2ea7dSJoe Perches 	struct va_format vaf;
7225d1b1b3fSAneesh Kumar K.V 	va_list args;
7235d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
7245d1b1b3fSAneesh Kumar K.V 
7251c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
7261c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
7271c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
7280ff2ea7dSJoe Perches 
7295d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
7300ff2ea7dSJoe Perches 
7310ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7320ff2ea7dSJoe Perches 	vaf.va = &args;
73321149d61SRobin Dong 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
734e29136f8STheodore Ts'o 	       sb->s_id, function, line, grp);
735e29136f8STheodore Ts'o 	if (ino)
7360ff2ea7dSJoe Perches 		printk(KERN_CONT "inode %lu: ", ino);
737e29136f8STheodore Ts'o 	if (block)
7380ff2ea7dSJoe Perches 		printk(KERN_CONT "block %llu:", (unsigned long long) block);
7390ff2ea7dSJoe Perches 	printk(KERN_CONT "%pV\n", &vaf);
7405d1b1b3fSAneesh Kumar K.V 	va_end(args);
7415d1b1b3fSAneesh Kumar K.V 
7425d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
743e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
7445d1b1b3fSAneesh Kumar K.V 		return;
7455d1b1b3fSAneesh Kumar K.V 	}
7461c13d5c0STheodore Ts'o 
7475d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
7485d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
7495d1b1b3fSAneesh Kumar K.V 	/*
7505d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
7515d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
7525d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
7535d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
7545d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
75525985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
7565d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
7575d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
7585d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
7595d1b1b3fSAneesh Kumar K.V 	 */
7605d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
7615d1b1b3fSAneesh Kumar K.V 	return;
7625d1b1b3fSAneesh Kumar K.V }
7635d1b1b3fSAneesh Kumar K.V 
764617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
765ac27a0ecSDave Kleikamp {
766617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
767ac27a0ecSDave Kleikamp 
768617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
769ac27a0ecSDave Kleikamp 		return;
770ac27a0ecSDave Kleikamp 
77112062dddSEric Sandeen 	ext4_warning(sb,
772ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
773ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
774617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
775ac27a0ecSDave Kleikamp 
776617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
777617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
778617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
779ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
780ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
781ac27a0ecSDave Kleikamp 
782ac27a0ecSDave Kleikamp 	/*
783ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
784ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
785ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
786ac27a0ecSDave Kleikamp 	 */
787ac27a0ecSDave Kleikamp }
788ac27a0ecSDave Kleikamp 
789ac27a0ecSDave Kleikamp /*
790ac27a0ecSDave Kleikamp  * Open the external journal device
791ac27a0ecSDave Kleikamp  */
792b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
793ac27a0ecSDave Kleikamp {
794ac27a0ecSDave Kleikamp 	struct block_device *bdev;
795ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
796ac27a0ecSDave Kleikamp 
797d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
798ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
799ac27a0ecSDave Kleikamp 		goto fail;
800ac27a0ecSDave Kleikamp 	return bdev;
801ac27a0ecSDave Kleikamp 
802ac27a0ecSDave Kleikamp fail:
803b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
804ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
805ac27a0ecSDave Kleikamp 	return NULL;
806ac27a0ecSDave Kleikamp }
807ac27a0ecSDave Kleikamp 
808ac27a0ecSDave Kleikamp /*
809ac27a0ecSDave Kleikamp  * Release the journal device
810ac27a0ecSDave Kleikamp  */
811617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
812ac27a0ecSDave Kleikamp {
813e525fd89STejun Heo 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
814ac27a0ecSDave Kleikamp }
815ac27a0ecSDave Kleikamp 
816617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
817ac27a0ecSDave Kleikamp {
818ac27a0ecSDave Kleikamp 	struct block_device *bdev;
819ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
820ac27a0ecSDave Kleikamp 
821ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
822ac27a0ecSDave Kleikamp 	if (bdev) {
823617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
824ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
825ac27a0ecSDave Kleikamp 	}
826ac27a0ecSDave Kleikamp 	return ret;
827ac27a0ecSDave Kleikamp }
828ac27a0ecSDave Kleikamp 
829ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
830ac27a0ecSDave Kleikamp {
831617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
832ac27a0ecSDave Kleikamp }
833ac27a0ecSDave Kleikamp 
834617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
835ac27a0ecSDave Kleikamp {
836ac27a0ecSDave Kleikamp 	struct list_head *l;
837ac27a0ecSDave Kleikamp 
838b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
839ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
840ac27a0ecSDave Kleikamp 
841ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
842ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
843ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
844ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
845ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
846ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
847ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
848ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
849ac27a0ecSDave Kleikamp 	}
850ac27a0ecSDave Kleikamp }
851ac27a0ecSDave Kleikamp 
852617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
853ac27a0ecSDave Kleikamp {
854617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
855617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
856ef2cabf7SHidehiro Kawai 	int i, err;
857ac27a0ecSDave Kleikamp 
858857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
859e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
860e0ccfd95SChristoph Hellwig 
8614c0425ffSMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
8624c0425ffSMingming Cao 	destroy_workqueue(sbi->dio_unwritten_wq);
8634c0425ffSMingming Cao 
8640390131bSFrank Mayhar 	if (sbi->s_journal) {
865ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
86647b4a50bSJan Kara 		sbi->s_journal = NULL;
867ef2cabf7SHidehiro Kawai 		if (err < 0)
868c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
8690390131bSFrank Mayhar 	}
870d4edac31SJosef Bacik 
871a1c6c569SSergey Senozhatsky 	del_timer(&sbi->s_err_report);
872d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
873d4edac31SJosef Bacik 	ext4_mb_release(sb);
874d4edac31SJosef Bacik 	ext4_ext_release(sb);
875d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
876d4edac31SJosef Bacik 
877ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
878617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
879ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
880ac27a0ecSDave Kleikamp 	}
88158c5873aSArtem Bityutskiy 	if (!(sb->s_flags & MS_RDONLY))
882a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
883a8e25a83SArtem Bityutskiy 
884240799cdSTheodore Ts'o 	if (sbi->s_proc) {
88566acdcf4STheodore Ts'o 		remove_proc_entry("options", sbi->s_proc);
8869f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
887240799cdSTheodore Ts'o 	}
8883197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
889ac27a0ecSDave Kleikamp 
890ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
891ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
892f18a5f21STheodore Ts'o 	ext4_kvfree(sbi->s_group_desc);
8939933fc0aSTheodore Ts'o 	ext4_kvfree(sbi->s_flex_groups);
89457042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
895ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
896ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
89757042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
898ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
899ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
900ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
901ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
902ac27a0ecSDave Kleikamp #endif
903ac27a0ecSDave Kleikamp 
904ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
905ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
906ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
907ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
908ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
909ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
910ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
911ac27a0ecSDave Kleikamp 
912f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
913ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
914ac27a0ecSDave Kleikamp 		/*
915ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
916ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
917ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
918ac27a0ecSDave Kleikamp 		 */
919ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
920f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
921617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
922ac27a0ecSDave Kleikamp 	}
923c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
924c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
925ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
9263197ebdbSTheodore Ts'o 	/*
9273197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
9283197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
9293197ebdbSTheodore Ts'o 	 */
9303197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
9313197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
9320441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
9330441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
934705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
935ac27a0ecSDave Kleikamp 	kfree(sbi);
936ac27a0ecSDave Kleikamp }
937ac27a0ecSDave Kleikamp 
938e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
939ac27a0ecSDave Kleikamp 
940ac27a0ecSDave Kleikamp /*
941ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
942ac27a0ecSDave Kleikamp  */
943617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
944ac27a0ecSDave Kleikamp {
945617ba13bSMingming Cao 	struct ext4_inode_info *ei;
946ac27a0ecSDave Kleikamp 
947e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
948ac27a0ecSDave Kleikamp 	if (!ei)
949ac27a0ecSDave Kleikamp 		return NULL;
9500b8e58a1SAndreas Dilger 
951ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
95291246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
953a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
954c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
955c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
956d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
957d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
958d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
9599d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
9607e731bc9STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = 0;
961d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
962a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
963a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
964a9e7f447SDmitry Monakhov #endif
9658aefcd55STheodore Ts'o 	ei->jinode = NULL;
966c7064ef1SJiaying Zhang 	INIT_LIST_HEAD(&ei->i_completed_io_list);
967744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
9688d5d02e6SMingming Cao 	ei->cur_aio_dio = NULL;
969b436b9beSJan Kara 	ei->i_sync_tid = 0;
970b436b9beSJan Kara 	ei->i_datasync_tid = 0;
971f7ad6d2eSTheodore Ts'o 	atomic_set(&ei->i_ioend_count, 0);
972e9e3bcecSEric Sandeen 	atomic_set(&ei->i_aiodio_unwritten, 0);
9730b8e58a1SAndreas Dilger 
974ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
975ac27a0ecSDave Kleikamp }
976ac27a0ecSDave Kleikamp 
9777ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
9787ff9c073STheodore Ts'o {
9797ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
9807ff9c073STheodore Ts'o 
9817ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
9827ff9c073STheodore Ts'o 	return drop;
9837ff9c073STheodore Ts'o }
9847ff9c073STheodore Ts'o 
985fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
986fa0d7e3dSNick Piggin {
987fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
988fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
989fa0d7e3dSNick Piggin }
990fa0d7e3dSNick Piggin 
991617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
992ac27a0ecSDave Kleikamp {
9939f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
994b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
995b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
996b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
9979f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
9989f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
9999f7dd93dSVasily Averin 				true);
10009f7dd93dSVasily Averin 		dump_stack();
10019f7dd93dSVasily Averin 	}
1002fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
1003ac27a0ecSDave Kleikamp }
1004ac27a0ecSDave Kleikamp 
100551cc5068SAlexey Dobriyan static void init_once(void *foo)
1006ac27a0ecSDave Kleikamp {
1007617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1008ac27a0ecSDave Kleikamp 
1009ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
101003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1011ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
1012ac27a0ecSDave Kleikamp #endif
10130e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1014ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1015ac27a0ecSDave Kleikamp }
1016ac27a0ecSDave Kleikamp 
1017ac27a0ecSDave Kleikamp static int init_inodecache(void)
1018ac27a0ecSDave Kleikamp {
1019617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
1020617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
1021ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
1022ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
102320c2df83SPaul Mundt 					     init_once);
1024617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1025ac27a0ecSDave Kleikamp 		return -ENOMEM;
1026ac27a0ecSDave Kleikamp 	return 0;
1027ac27a0ecSDave Kleikamp }
1028ac27a0ecSDave Kleikamp 
1029ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1030ac27a0ecSDave Kleikamp {
1031617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1032ac27a0ecSDave Kleikamp }
1033ac27a0ecSDave Kleikamp 
10340930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1035ac27a0ecSDave Kleikamp {
10360930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1037dbd5768fSJan Kara 	clear_inode(inode);
10389f754758SChristoph Hellwig 	dquot_drop(inode);
1039c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
10408aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
10418aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
10428aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
10438aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
10448aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
10458aefcd55STheodore Ts'o 	}
1046ac27a0ecSDave Kleikamp }
1047ac27a0ecSDave Kleikamp 
10481b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
10491b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1050ac27a0ecSDave Kleikamp {
1051ac27a0ecSDave Kleikamp 	struct inode *inode;
1052ac27a0ecSDave Kleikamp 
1053617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1054ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1055617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1056ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1057ac27a0ecSDave Kleikamp 
1058ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1059ac27a0ecSDave Kleikamp 	 *
1060617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1061ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1062ac27a0ecSDave Kleikamp 	 *
1063ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1064ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1065ac27a0ecSDave Kleikamp 	 */
10661d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
10671d1fe1eeSDavid Howells 	if (IS_ERR(inode))
10681d1fe1eeSDavid Howells 		return ERR_CAST(inode);
10691d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1070ac27a0ecSDave Kleikamp 		iput(inode);
1071ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1072ac27a0ecSDave Kleikamp 	}
10731b961ac0SChristoph Hellwig 
10741b961ac0SChristoph Hellwig 	return inode;
1075ac27a0ecSDave Kleikamp }
10761b961ac0SChristoph Hellwig 
10771b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
10781b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10791b961ac0SChristoph Hellwig {
10801b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
10811b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
10821b961ac0SChristoph Hellwig }
10831b961ac0SChristoph Hellwig 
10841b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
10851b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10861b961ac0SChristoph Hellwig {
10871b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
10881b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1089ac27a0ecSDave Kleikamp }
1090ac27a0ecSDave Kleikamp 
1091c39a7f84SToshiyuki Okajima /*
1092c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1093c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1094c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1095c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1096c39a7f84SToshiyuki Okajima  */
10970b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
10980b8e58a1SAndreas Dilger 				 gfp_t wait)
1099c39a7f84SToshiyuki Okajima {
1100c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1101c39a7f84SToshiyuki Okajima 
1102c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1103c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1104c39a7f84SToshiyuki Okajima 		return 0;
1105c39a7f84SToshiyuki Okajima 	if (journal)
1106c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1107c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1108c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1109c39a7f84SToshiyuki Okajima }
1110c39a7f84SToshiyuki Okajima 
1111ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1112ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1113ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1114ac27a0ecSDave Kleikamp 
1115617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1116617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1117617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1118617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1119617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
11206f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1121f00c9e44SJan Kara 			 struct path *path);
11227c319d32SAditya Kali static int ext4_quota_on_sysfile(struct super_block *sb, int type,
11237c319d32SAditya Kali 				 int format_id);
1124ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
11257c319d32SAditya Kali static int ext4_quota_off_sysfile(struct super_block *sb, int type);
1126617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1127617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1128ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1129617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1130ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
11317c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
11327c319d32SAditya Kali 			     unsigned int flags);
11337c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1134ac27a0ecSDave Kleikamp 
113561e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
113660e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1137617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1138617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1139617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1140617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1141a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1142a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1143a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1144ac27a0ecSDave Kleikamp };
1145ac27a0ecSDave Kleikamp 
11460d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1147617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1148ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1149287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
1150287a8095SChristoph Hellwig 	.get_info	= dquot_get_dqinfo,
1151287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1152287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1153287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1154ac27a0ecSDave Kleikamp };
11557c319d32SAditya Kali 
11567c319d32SAditya Kali static const struct quotactl_ops ext4_qctl_sysfile_operations = {
11577c319d32SAditya Kali 	.quota_on_meta	= ext4_quota_on_sysfile,
11587c319d32SAditya Kali 	.quota_off	= ext4_quota_off_sysfile,
11597c319d32SAditya Kali 	.quota_sync	= dquot_quota_sync,
11607c319d32SAditya Kali 	.get_info	= dquot_get_dqinfo,
11617c319d32SAditya Kali 	.set_info	= dquot_set_dqinfo,
11627c319d32SAditya Kali 	.get_dqblk	= dquot_get_dqblk,
11637c319d32SAditya Kali 	.set_dqblk	= dquot_set_dqblk
11647c319d32SAditya Kali };
1165ac27a0ecSDave Kleikamp #endif
1166ac27a0ecSDave Kleikamp 
1167ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1168617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1169617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1170617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1171617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
11727ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
11730930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1174617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1175617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1176c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1177c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1178617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1179617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1180617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1181ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1182617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1183617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1184ac27a0ecSDave Kleikamp #endif
1185c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1186ac27a0ecSDave Kleikamp };
1187ac27a0ecSDave Kleikamp 
11889ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
11899ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
11909ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
11919ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
11929ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
11937ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
11940930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
11959ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
11969ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
11979ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
11989ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
11999ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
12009ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
12019ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
12029ca92389STheodore Ts'o #endif
12039ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
12049ca92389STheodore Ts'o };
12059ca92389STheodore Ts'o 
120639655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
12071b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
12081b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1209617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1210ac27a0ecSDave Kleikamp };
1211ac27a0ecSDave Kleikamp 
1212ac27a0ecSDave Kleikamp enum {
1213ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1214ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
121572578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1216ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
121772578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
121830773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
121943e625d8SEric Sandeen 	Opt_journal_dev, Opt_journal_checksum, Opt_journal_async_commit,
1220ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1221296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1222ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
12235a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1224ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1225661aa520SEric Sandeen 	Opt_usrquota, Opt_grpquota, Opt_i_version,
12261449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
12271449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
12285328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1229744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1230fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1231df981d03STheodore Ts'o 	Opt_max_dir_size_kb,
1232ac27a0ecSDave Kleikamp };
1233ac27a0ecSDave Kleikamp 
1234a447c093SSteven Whitehouse static const match_table_t tokens = {
1235ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1236ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1237ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1238ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1239ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1240ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1241ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1242ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1243ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1244ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1245ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1246ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1247ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1248ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
124972578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
125072578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1251ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1252ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1253ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1254ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1255e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
12565a916be1STheodore Ts'o 	{Opt_noload, "noload"},
125772578c33STheodore Ts'o 	{Opt_removed, "nobh"},
125872578c33STheodore Ts'o 	{Opt_removed, "bh"},
1259ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
126030773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
126130773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1262ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1263818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1264818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1265ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1266ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1267ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1268ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
12695bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
12705bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1271ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1272ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1273ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1274ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1275ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1276ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
12775a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1278ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1279ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1280ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1281ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1282ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
128306705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
128406705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
128525ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1286c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
128764769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1288dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
12891449032bSTheodore Ts'o 	{Opt_mblk_io_submit, "mblk_io_submit"},
12901449032bSTheodore Ts'o 	{Opt_nomblk_io_submit, "nomblk_io_submit"},
12916fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
12926fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1293240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1294b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1295afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
129606705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
129706705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1298744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1299744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
13005328e635SEric Sandeen 	{Opt_discard, "discard"},
13015328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1302fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1303fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1304fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1305df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1306c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1307c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1308c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1309c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1310c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1311f3f12faaSJosef Bacik 	{Opt_err, NULL},
1312ac27a0ecSDave Kleikamp };
1313ac27a0ecSDave Kleikamp 
1314617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1315ac27a0ecSDave Kleikamp {
1316617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1317ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1318ac27a0ecSDave Kleikamp 
1319ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1320ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
13210b8e58a1SAndreas Dilger 
1322ac27a0ecSDave Kleikamp 	options += 3;
13230b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1324ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1325ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
13264776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1327ac27a0ecSDave Kleikamp 		       (char *) *data);
1328ac27a0ecSDave Kleikamp 		return 1;
1329ac27a0ecSDave Kleikamp 	}
1330ac27a0ecSDave Kleikamp 	if (*options == ',')
1331ac27a0ecSDave Kleikamp 		options++;
1332ac27a0ecSDave Kleikamp 	*data = (void *) options;
13330b8e58a1SAndreas Dilger 
1334ac27a0ecSDave Kleikamp 	return sb_block;
1335ac27a0ecSDave Kleikamp }
1336ac27a0ecSDave Kleikamp 
1337b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1338437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1339437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1340b3881f74STheodore Ts'o 
134156c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
134256c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
134356c50f11SDmitry Monakhov {
134456c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
134556c50f11SDmitry Monakhov 	char *qname;
134656c50f11SDmitry Monakhov 
134756c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
134856c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
134956c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
135056c50f11SDmitry Monakhov 			"Cannot change journaled "
135156c50f11SDmitry Monakhov 			"quota options when quota turned on");
135257f73c2cSTheodore Ts'o 		return -1;
135356c50f11SDmitry Monakhov 	}
135456c50f11SDmitry Monakhov 	qname = match_strdup(args);
135556c50f11SDmitry Monakhov 	if (!qname) {
135656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
135756c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
135857f73c2cSTheodore Ts'o 		return -1;
135956c50f11SDmitry Monakhov 	}
136056c50f11SDmitry Monakhov 	if (sbi->s_qf_names[qtype] &&
136156c50f11SDmitry Monakhov 		strcmp(sbi->s_qf_names[qtype], qname)) {
136256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
136356c50f11SDmitry Monakhov 			"%s quota file already specified", QTYPE2NAME(qtype));
136456c50f11SDmitry Monakhov 		kfree(qname);
136557f73c2cSTheodore Ts'o 		return -1;
136656c50f11SDmitry Monakhov 	}
136756c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = qname;
136856c50f11SDmitry Monakhov 	if (strchr(sbi->s_qf_names[qtype], '/')) {
136956c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
137056c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
137156c50f11SDmitry Monakhov 		kfree(sbi->s_qf_names[qtype]);
137256c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype] = NULL;
137357f73c2cSTheodore Ts'o 		return -1;
137456c50f11SDmitry Monakhov 	}
1375fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
137656c50f11SDmitry Monakhov 	return 1;
137756c50f11SDmitry Monakhov }
137856c50f11SDmitry Monakhov 
137956c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
138056c50f11SDmitry Monakhov {
138156c50f11SDmitry Monakhov 
138256c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
138356c50f11SDmitry Monakhov 
138456c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
138556c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
138656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
138756c50f11SDmitry Monakhov 			" when quota turned on");
138857f73c2cSTheodore Ts'o 		return -1;
138956c50f11SDmitry Monakhov 	}
139056c50f11SDmitry Monakhov 	/*
139156c50f11SDmitry Monakhov 	 * The space will be released later when all options are confirmed
139256c50f11SDmitry Monakhov 	 * to be correct
139356c50f11SDmitry Monakhov 	 */
139456c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
139556c50f11SDmitry Monakhov 	return 1;
139656c50f11SDmitry Monakhov }
139756c50f11SDmitry Monakhov #endif
139856c50f11SDmitry Monakhov 
139926092bf5STheodore Ts'o #define MOPT_SET	0x0001
140026092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
140126092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
140226092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
140326092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
140426092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
140526092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
140626092bf5STheodore Ts'o #define MOPT_Q		0
140726092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
140826092bf5STheodore Ts'o #else
140926092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
141026092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
141126092bf5STheodore Ts'o #endif
141226092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
141326092bf5STheodore Ts'o 
141426092bf5STheodore Ts'o static const struct mount_opts {
141526092bf5STheodore Ts'o 	int	token;
141626092bf5STheodore Ts'o 	int	mount_opt;
141726092bf5STheodore Ts'o 	int	flags;
141826092bf5STheodore Ts'o } ext4_mount_opts[] = {
141926092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
142026092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
142126092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
142226092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
142326092bf5STheodore Ts'o 	{Opt_mblk_io_submit, EXT4_MOUNT_MBLK_IO_SUBMIT, MOPT_SET},
142426092bf5STheodore Ts'o 	{Opt_nomblk_io_submit, EXT4_MOUNT_MBLK_IO_SUBMIT, MOPT_CLEAR},
142526092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
142626092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
142726092bf5STheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK, MOPT_SET},
142826092bf5STheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK, MOPT_CLEAR},
142926092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
143026092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
143126092bf5STheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC, MOPT_SET | MOPT_EXPLICIT},
143226092bf5STheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC, MOPT_CLEAR | MOPT_EXPLICIT},
143326092bf5STheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, MOPT_SET},
143426092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
143526092bf5STheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM), MOPT_SET},
143626092bf5STheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_SET},
143726092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
143826092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
143926092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
144026092bf5STheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_SET},
144126092bf5STheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_CLEAR},
144226092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
144326092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
144426092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
144526092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
144626092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
144726092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
144826092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
144926092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
145026092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
145126092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
145226092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
145326092bf5STheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_DATAJ},
145426092bf5STheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_DATAJ},
145526092bf5STheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA, MOPT_DATAJ},
145626092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
145726092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
145826092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
145926092bf5STheodore Ts'o #else
146026092bf5STheodore Ts'o 	{Opt_user_xattr, 0, MOPT_NOSUPPORT},
146126092bf5STheodore Ts'o 	{Opt_nouser_xattr, 0, MOPT_NOSUPPORT},
146226092bf5STheodore Ts'o #endif
146326092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
146426092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
146526092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
146626092bf5STheodore Ts'o #else
146726092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
146826092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
146926092bf5STheodore Ts'o #endif
147026092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
147126092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
147226092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
147326092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
147426092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
147526092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
147626092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
147726092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
147826092bf5STheodore Ts'o 		       EXT4_MOUNT_GRPQUOTA), MOPT_CLEAR | MOPT_Q},
147926092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
148026092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
148126092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
148226092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
148326092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
148426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
148526092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1486df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
148726092bf5STheodore Ts'o 	{Opt_err, 0, 0}
148826092bf5STheodore Ts'o };
148926092bf5STheodore Ts'o 
149026092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
149126092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
149226092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
149326092bf5STheodore Ts'o {
149426092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
149526092bf5STheodore Ts'o 	const struct mount_opts *m;
149608cefc7aSEric W. Biederman 	kuid_t uid;
149708cefc7aSEric W. Biederman 	kgid_t gid;
149826092bf5STheodore Ts'o 	int arg = 0;
149926092bf5STheodore Ts'o 
150057f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
150157f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
150257f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
150357f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
150457f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
150557f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
150657f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
150757f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
150857f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
150957f73c2cSTheodore Ts'o #endif
151026092bf5STheodore Ts'o 	if (args->from && match_int(args, &arg))
151126092bf5STheodore Ts'o 		return -1;
151226092bf5STheodore Ts'o 	switch (token) {
1513f7048605STheodore Ts'o 	case Opt_noacl:
1514f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1515f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1516f7048605STheodore Ts'o 		break;
151726092bf5STheodore Ts'o 	case Opt_sb:
151826092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
151926092bf5STheodore Ts'o 	case Opt_removed:
152026092bf5STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
152126092bf5STheodore Ts'o 			 "Ignoring removed %s option", opt);
152226092bf5STheodore Ts'o 		return 1;
152326092bf5STheodore Ts'o 	case Opt_resuid:
152408cefc7aSEric W. Biederman 		uid = make_kuid(current_user_ns(), arg);
152508cefc7aSEric W. Biederman 		if (!uid_valid(uid)) {
152608cefc7aSEric W. Biederman 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
152708cefc7aSEric W. Biederman 			return -1;
152808cefc7aSEric W. Biederman 		}
152908cefc7aSEric W. Biederman 		sbi->s_resuid = uid;
153026092bf5STheodore Ts'o 		return 1;
153126092bf5STheodore Ts'o 	case Opt_resgid:
153208cefc7aSEric W. Biederman 		gid = make_kgid(current_user_ns(), arg);
153308cefc7aSEric W. Biederman 		if (!gid_valid(gid)) {
153408cefc7aSEric W. Biederman 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
153508cefc7aSEric W. Biederman 			return -1;
153608cefc7aSEric W. Biederman 		}
153708cefc7aSEric W. Biederman 		sbi->s_resgid = gid;
153826092bf5STheodore Ts'o 		return 1;
153926092bf5STheodore Ts'o 	case Opt_abort:
154026092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
154126092bf5STheodore Ts'o 		return 1;
154226092bf5STheodore Ts'o 	case Opt_i_version:
154326092bf5STheodore Ts'o 		sb->s_flags |= MS_I_VERSION;
154426092bf5STheodore Ts'o 		return 1;
154526092bf5STheodore Ts'o 	case Opt_journal_dev:
154626092bf5STheodore Ts'o 		if (is_remount) {
154726092bf5STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
154826092bf5STheodore Ts'o 				 "Cannot specify journal on remount");
154926092bf5STheodore Ts'o 			return -1;
155026092bf5STheodore Ts'o 		}
155126092bf5STheodore Ts'o 		*journal_devnum = arg;
155226092bf5STheodore Ts'o 		return 1;
155326092bf5STheodore Ts'o 	case Opt_journal_ioprio:
155426092bf5STheodore Ts'o 		if (arg < 0 || arg > 7)
155526092bf5STheodore Ts'o 			return -1;
155626092bf5STheodore Ts'o 		*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
155726092bf5STheodore Ts'o 		return 1;
155826092bf5STheodore Ts'o 	}
155926092bf5STheodore Ts'o 
156026092bf5STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
156126092bf5STheodore Ts'o 		if (token != m->token)
156226092bf5STheodore Ts'o 			continue;
156326092bf5STheodore Ts'o 		if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
156426092bf5STheodore Ts'o 			return -1;
156526092bf5STheodore Ts'o 		if (m->flags & MOPT_EXPLICIT)
156626092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
156726092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR_ERR)
156826092bf5STheodore Ts'o 			clear_opt(sb, ERRORS_MASK);
156926092bf5STheodore Ts'o 		if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
157026092bf5STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "Cannot change quota "
157126092bf5STheodore Ts'o 				 "options when quota turned on");
157226092bf5STheodore Ts'o 			return -1;
157326092bf5STheodore Ts'o 		}
157426092bf5STheodore Ts'o 
157526092bf5STheodore Ts'o 		if (m->flags & MOPT_NOSUPPORT) {
157626092bf5STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
157726092bf5STheodore Ts'o 		} else if (token == Opt_commit) {
157826092bf5STheodore Ts'o 			if (arg == 0)
157926092bf5STheodore Ts'o 				arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
158026092bf5STheodore Ts'o 			sbi->s_commit_interval = HZ * arg;
158126092bf5STheodore Ts'o 		} else if (token == Opt_max_batch_time) {
158226092bf5STheodore Ts'o 			if (arg == 0)
158326092bf5STheodore Ts'o 				arg = EXT4_DEF_MAX_BATCH_TIME;
158426092bf5STheodore Ts'o 			sbi->s_max_batch_time = arg;
158526092bf5STheodore Ts'o 		} else if (token == Opt_min_batch_time) {
158626092bf5STheodore Ts'o 			sbi->s_min_batch_time = arg;
158726092bf5STheodore Ts'o 		} else if (token == Opt_inode_readahead_blks) {
158826092bf5STheodore Ts'o 			if (arg > (1 << 30))
158926092bf5STheodore Ts'o 				return -1;
159026092bf5STheodore Ts'o 			if (arg && !is_power_of_2(arg)) {
159126092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
159226092bf5STheodore Ts'o 					 "EXT4-fs: inode_readahead_blks"
159326092bf5STheodore Ts'o 					 " must be a power of 2");
159426092bf5STheodore Ts'o 				return -1;
159526092bf5STheodore Ts'o 			}
159626092bf5STheodore Ts'o 			sbi->s_inode_readahead_blks = arg;
159726092bf5STheodore Ts'o 		} else if (token == Opt_init_itable) {
159826092bf5STheodore Ts'o 			set_opt(sb, INIT_INODE_TABLE);
159926092bf5STheodore Ts'o 			if (!args->from)
160026092bf5STheodore Ts'o 				arg = EXT4_DEF_LI_WAIT_MULT;
160126092bf5STheodore Ts'o 			sbi->s_li_wait_mult = arg;
1602df981d03STheodore Ts'o 		} else if (token == Opt_max_dir_size_kb) {
1603df981d03STheodore Ts'o 			sbi->s_max_dir_size_kb = arg;
160426092bf5STheodore Ts'o 		} else if (token == Opt_stripe) {
160526092bf5STheodore Ts'o 			sbi->s_stripe = arg;
160626092bf5STheodore Ts'o 		} else if (m->flags & MOPT_DATAJ) {
160726092bf5STheodore Ts'o 			if (is_remount) {
160826092bf5STheodore Ts'o 				if (!sbi->s_journal)
160926092bf5STheodore Ts'o 					ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
161026092bf5STheodore Ts'o 				else if (test_opt(sb, DATA_FLAGS) !=
161126092bf5STheodore Ts'o 					 m->mount_opt) {
161226092bf5STheodore Ts'o 					ext4_msg(sb, KERN_ERR,
161326092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
161426092bf5STheodore Ts'o 					return -1;
161526092bf5STheodore Ts'o 				}
161626092bf5STheodore Ts'o 			} else {
161726092bf5STheodore Ts'o 				clear_opt(sb, DATA_FLAGS);
161826092bf5STheodore Ts'o 				sbi->s_mount_opt |= m->mount_opt;
161926092bf5STheodore Ts'o 			}
162026092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
162126092bf5STheodore Ts'o 		} else if (m->flags & MOPT_QFMT) {
162226092bf5STheodore Ts'o 			if (sb_any_quota_loaded(sb) &&
162326092bf5STheodore Ts'o 			    sbi->s_jquota_fmt != m->mount_opt) {
162426092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR, "Cannot "
162526092bf5STheodore Ts'o 					 "change journaled quota options "
162626092bf5STheodore Ts'o 					 "when quota turned on");
162726092bf5STheodore Ts'o 				return -1;
162826092bf5STheodore Ts'o 			}
162926092bf5STheodore Ts'o 			sbi->s_jquota_fmt = m->mount_opt;
163026092bf5STheodore Ts'o #endif
163126092bf5STheodore Ts'o 		} else {
163226092bf5STheodore Ts'o 			if (!args->from)
163326092bf5STheodore Ts'o 				arg = 1;
163426092bf5STheodore Ts'o 			if (m->flags & MOPT_CLEAR)
163526092bf5STheodore Ts'o 				arg = !arg;
163626092bf5STheodore Ts'o 			else if (unlikely(!(m->flags & MOPT_SET))) {
163726092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING,
163826092bf5STheodore Ts'o 					 "buggy handling of option %s", opt);
163926092bf5STheodore Ts'o 				WARN_ON(1);
164026092bf5STheodore Ts'o 				return -1;
164126092bf5STheodore Ts'o 			}
164226092bf5STheodore Ts'o 			if (arg != 0)
164326092bf5STheodore Ts'o 				sbi->s_mount_opt |= m->mount_opt;
164426092bf5STheodore Ts'o 			else
164526092bf5STheodore Ts'o 				sbi->s_mount_opt &= ~m->mount_opt;
164626092bf5STheodore Ts'o 		}
164726092bf5STheodore Ts'o 		return 1;
164826092bf5STheodore Ts'o 	}
164926092bf5STheodore Ts'o 	ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
165026092bf5STheodore Ts'o 		 "or missing value", opt);
165126092bf5STheodore Ts'o 	return -1;
165226092bf5STheodore Ts'o }
165326092bf5STheodore Ts'o 
1654ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1655c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1656b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1657661aa520SEric Sandeen 			 int is_remount)
1658ac27a0ecSDave Kleikamp {
1659db7e5c66SEldad Zack #ifdef CONFIG_QUOTA
1660617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1661db7e5c66SEldad Zack #endif
1662ac27a0ecSDave Kleikamp 	char *p;
1663ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
166426092bf5STheodore Ts'o 	int token;
1665ac27a0ecSDave Kleikamp 
1666ac27a0ecSDave Kleikamp 	if (!options)
1667ac27a0ecSDave Kleikamp 		return 1;
1668ac27a0ecSDave Kleikamp 
1669ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1670ac27a0ecSDave Kleikamp 		if (!*p)
1671ac27a0ecSDave Kleikamp 			continue;
167215121c18SEric Sandeen 		/*
167315121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
167415121c18SEric Sandeen 		 * found; some options take optional arguments.
167515121c18SEric Sandeen 		 */
1676caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
1677ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
167826092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
167926092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
1680ac27a0ecSDave Kleikamp 			return 0;
1681ac27a0ecSDave Kleikamp 	}
1682ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1683ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1684482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1685fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1686ac27a0ecSDave Kleikamp 
1687482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1688fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1689ac27a0ecSDave Kleikamp 
169056c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1691b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1692b31e1552SEric Sandeen 					"format mixing");
1693ac27a0ecSDave Kleikamp 			return 0;
1694ac27a0ecSDave Kleikamp 		}
1695ac27a0ecSDave Kleikamp 
1696ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1697b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1698b31e1552SEric Sandeen 					"not specified");
1699ac27a0ecSDave Kleikamp 			return 0;
1700ac27a0ecSDave Kleikamp 		}
1701ac27a0ecSDave Kleikamp 	} else {
1702ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1703b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
17042c8be6b2SJan Kara 					"specified with no journaling "
1705b31e1552SEric Sandeen 					"enabled");
1706ac27a0ecSDave Kleikamp 			return 0;
1707ac27a0ecSDave Kleikamp 		}
1708ac27a0ecSDave Kleikamp 	}
1709ac27a0ecSDave Kleikamp #endif
1710ac27a0ecSDave Kleikamp 	return 1;
1711ac27a0ecSDave Kleikamp }
1712ac27a0ecSDave Kleikamp 
17132adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
17142adf6da8STheodore Ts'o 					   struct super_block *sb)
17152adf6da8STheodore Ts'o {
17162adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
17172adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
17182adf6da8STheodore Ts'o 
17192adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
17202adf6da8STheodore Ts'o 		char *fmtname = "";
17212adf6da8STheodore Ts'o 
17222adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
17232adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
17242adf6da8STheodore Ts'o 			fmtname = "vfsold";
17252adf6da8STheodore Ts'o 			break;
17262adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
17272adf6da8STheodore Ts'o 			fmtname = "vfsv0";
17282adf6da8STheodore Ts'o 			break;
17292adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
17302adf6da8STheodore Ts'o 			fmtname = "vfsv1";
17312adf6da8STheodore Ts'o 			break;
17322adf6da8STheodore Ts'o 		}
17332adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
17342adf6da8STheodore Ts'o 	}
17352adf6da8STheodore Ts'o 
17362adf6da8STheodore Ts'o 	if (sbi->s_qf_names[USRQUOTA])
17372adf6da8STheodore Ts'o 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
17382adf6da8STheodore Ts'o 
17392adf6da8STheodore Ts'o 	if (sbi->s_qf_names[GRPQUOTA])
17402adf6da8STheodore Ts'o 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
17412adf6da8STheodore Ts'o 
17422adf6da8STheodore Ts'o 	if (test_opt(sb, USRQUOTA))
17432adf6da8STheodore Ts'o 		seq_puts(seq, ",usrquota");
17442adf6da8STheodore Ts'o 
17452adf6da8STheodore Ts'o 	if (test_opt(sb, GRPQUOTA))
17462adf6da8STheodore Ts'o 		seq_puts(seq, ",grpquota");
17472adf6da8STheodore Ts'o #endif
17482adf6da8STheodore Ts'o }
17492adf6da8STheodore Ts'o 
17505a916be1STheodore Ts'o static const char *token2str(int token)
17515a916be1STheodore Ts'o {
17525a916be1STheodore Ts'o 	static const struct match_token *t;
17535a916be1STheodore Ts'o 
17545a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
17555a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
17565a916be1STheodore Ts'o 			break;
17575a916be1STheodore Ts'o 	return t->pattern;
17585a916be1STheodore Ts'o }
17595a916be1STheodore Ts'o 
17602adf6da8STheodore Ts'o /*
17612adf6da8STheodore Ts'o  * Show an option if
17622adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
17632adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
17642adf6da8STheodore Ts'o  */
176566acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
176666acdcf4STheodore Ts'o 			      int nodefs)
17672adf6da8STheodore Ts'o {
17682adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
17692adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
177066acdcf4STheodore Ts'o 	int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
17715a916be1STheodore Ts'o 	const struct mount_opts *m;
177266acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
17732adf6da8STheodore Ts'o 
177466acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
177566acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
17762adf6da8STheodore Ts'o 
17772adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
17785a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
17795a916be1STheodore Ts'o 
17805a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
17815a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
17825a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
17835a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
17845a916be1STheodore Ts'o 			continue;
178566acdcf4STheodore Ts'o 		if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
17865a916be1STheodore Ts'o 			continue; /* skip if same as the default */
17875a916be1STheodore Ts'o 		if ((want_set &&
17885a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
17895a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
17905a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
17915a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
17925a916be1STheodore Ts'o 	}
17935a916be1STheodore Ts'o 
179408cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
17955a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
179608cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
179708cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
179808cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
17995a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
180008cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
180108cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
180266acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
18035a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
18045a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
18052adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
18065a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
18072adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
18085a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
180966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
18105a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
181166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
18125a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
181366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
18145a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
18152adf6da8STheodore Ts'o 	if (sb->s_flags & MS_I_VERSION)
18165a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
181766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
18185a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
181966acdcf4STheodore Ts'o 	if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
18202adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
18215a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
18222adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
18235a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
18242adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
18255a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
18265a916be1STheodore Ts'o 	}
182766acdcf4STheodore Ts'o 	if (nodefs ||
182866acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
18295a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
18302adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
18312adf6da8STheodore Ts'o 
183266acdcf4STheodore Ts'o 	if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
183366acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
18345a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
1835df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
1836df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
18372adf6da8STheodore Ts'o 
18382adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
18392adf6da8STheodore Ts'o 	return 0;
18402adf6da8STheodore Ts'o }
18412adf6da8STheodore Ts'o 
184266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
184366acdcf4STheodore Ts'o {
184466acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
184566acdcf4STheodore Ts'o }
184666acdcf4STheodore Ts'o 
184766acdcf4STheodore Ts'o static int options_seq_show(struct seq_file *seq, void *offset)
184866acdcf4STheodore Ts'o {
184966acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
185066acdcf4STheodore Ts'o 	int rc;
185166acdcf4STheodore Ts'o 
185266acdcf4STheodore Ts'o 	seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
185366acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
185466acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
185566acdcf4STheodore Ts'o 	return rc;
185666acdcf4STheodore Ts'o }
185766acdcf4STheodore Ts'o 
185866acdcf4STheodore Ts'o static int options_open_fs(struct inode *inode, struct file *file)
185966acdcf4STheodore Ts'o {
186066acdcf4STheodore Ts'o 	return single_open(file, options_seq_show, PDE(inode)->data);
186166acdcf4STheodore Ts'o }
186266acdcf4STheodore Ts'o 
186366acdcf4STheodore Ts'o static const struct file_operations ext4_seq_options_fops = {
186466acdcf4STheodore Ts'o 	.owner = THIS_MODULE,
186566acdcf4STheodore Ts'o 	.open = options_open_fs,
186666acdcf4STheodore Ts'o 	.read = seq_read,
186766acdcf4STheodore Ts'o 	.llseek = seq_lseek,
186866acdcf4STheodore Ts'o 	.release = single_release,
186966acdcf4STheodore Ts'o };
187066acdcf4STheodore Ts'o 
1871617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1872ac27a0ecSDave Kleikamp 			    int read_only)
1873ac27a0ecSDave Kleikamp {
1874617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1875ac27a0ecSDave Kleikamp 	int res = 0;
1876ac27a0ecSDave Kleikamp 
1877617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1878b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1879b31e1552SEric Sandeen 			 "forcing read-only mode");
1880ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1881ac27a0ecSDave Kleikamp 	}
1882ac27a0ecSDave Kleikamp 	if (read_only)
1883281b5995STheodore Ts'o 		goto done;
1884617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1885b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1886b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1887617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1888b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1889b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1890b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1891ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1892ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1893ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1894b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1895b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1896b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1897ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1898ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1899ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1900b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1901b31e1552SEric Sandeen 			 "warning: checktime reached, "
1902b31e1552SEric Sandeen 			 "running e2fsck is recommended");
19030390131bSFrank Mayhar 	if (!sbi->s_journal)
1904216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1905ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1906617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1907e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1908ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1909617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
19100390131bSFrank Mayhar 	if (sbi->s_journal)
1911617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1912ac27a0ecSDave Kleikamp 
1913e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1914281b5995STheodore Ts'o done:
1915ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1916a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1917a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1918ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1919ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1920617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1921617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1922a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
1923ac27a0ecSDave Kleikamp 
19247abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
1925ac27a0ecSDave Kleikamp 	return res;
1926ac27a0ecSDave Kleikamp }
1927ac27a0ecSDave Kleikamp 
1928117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
1929117fff10STheodore Ts'o {
1930117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1931117fff10STheodore Ts'o 	struct flex_groups *new_groups;
1932117fff10STheodore Ts'o 	int size;
1933117fff10STheodore Ts'o 
1934117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
1935117fff10STheodore Ts'o 		return 0;
1936117fff10STheodore Ts'o 
1937117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
1938117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
1939117fff10STheodore Ts'o 		return 0;
1940117fff10STheodore Ts'o 
1941117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
1942117fff10STheodore Ts'o 	new_groups = ext4_kvzalloc(size, GFP_KERNEL);
1943117fff10STheodore Ts'o 	if (!new_groups) {
1944117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
1945117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
1946117fff10STheodore Ts'o 		return -ENOMEM;
1947117fff10STheodore Ts'o 	}
1948117fff10STheodore Ts'o 
1949117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
1950117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
1951117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
1952117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
1953117fff10STheodore Ts'o 		ext4_kvfree(sbi->s_flex_groups);
1954117fff10STheodore Ts'o 	}
1955117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
1956117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
1957117fff10STheodore Ts'o 	return 0;
1958117fff10STheodore Ts'o }
1959117fff10STheodore Ts'o 
1960772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1961772cb7c8SJose R. Santos {
1962772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1963772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1964772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1965d50f2ab6SXi Wang 	unsigned int groups_per_flex = 0;
1966117fff10STheodore Ts'o 	int i, err;
1967772cb7c8SJose R. Santos 
1968503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1969d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
1970772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1971772cb7c8SJose R. Santos 		return 1;
1972772cb7c8SJose R. Santos 	}
1973d50f2ab6SXi Wang 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1974772cb7c8SJose R. Santos 
1975117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
1976117fff10STheodore Ts'o 	if (err)
1977772cb7c8SJose R. Santos 		goto failed;
1978772cb7c8SJose R. Santos 
1979772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
198088b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1981772cb7c8SJose R. Santos 
1982772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
19837ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
19847ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
1985021b65bbSTheodore Ts'o 		atomic_add(ext4_free_group_clusters(sb, gdp),
198624aaa8efSTheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_clusters);
19877ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
19887ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
1989772cb7c8SJose R. Santos 	}
1990772cb7c8SJose R. Santos 
1991772cb7c8SJose R. Santos 	return 1;
1992772cb7c8SJose R. Santos failed:
1993772cb7c8SJose R. Santos 	return 0;
1994772cb7c8SJose R. Santos }
1995772cb7c8SJose R. Santos 
1996feb0ab32SDarrick J. Wong static __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1997717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
1998717d50e4SAndreas Dilger {
1999feb0ab32SDarrick J. Wong 	int offset;
2000717d50e4SAndreas Dilger 	__u16 crc = 0;
2001717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2002717d50e4SAndreas Dilger 
2003feb0ab32SDarrick J. Wong 	if ((sbi->s_es->s_feature_ro_compat &
2004feb0ab32SDarrick J. Wong 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))) {
2005feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2006feb0ab32SDarrick J. Wong 		__u16 old_csum;
2007feb0ab32SDarrick J. Wong 		__u32 csum32;
2008feb0ab32SDarrick J. Wong 
2009feb0ab32SDarrick J. Wong 		old_csum = gdp->bg_checksum;
2010feb0ab32SDarrick J. Wong 		gdp->bg_checksum = 0;
2011feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2012feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2013feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp,
2014feb0ab32SDarrick J. Wong 				     sbi->s_desc_size);
2015feb0ab32SDarrick J. Wong 		gdp->bg_checksum = old_csum;
2016feb0ab32SDarrick J. Wong 
2017feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2018feb0ab32SDarrick J. Wong 		goto out;
2019feb0ab32SDarrick J. Wong 	}
2020feb0ab32SDarrick J. Wong 
2021feb0ab32SDarrick J. Wong 	/* old crc16 code */
2022feb0ab32SDarrick J. Wong 	offset = offsetof(struct ext4_group_desc, bg_checksum);
2023feb0ab32SDarrick J. Wong 
2024717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2025717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2026717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2027717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2028717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2029717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_incompat &
2030717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
2031717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2032717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2033717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2034717d50e4SAndreas Dilger 				offset);
2035717d50e4SAndreas Dilger 
2036feb0ab32SDarrick J. Wong out:
2037717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2038717d50e4SAndreas Dilger }
2039717d50e4SAndreas Dilger 
2040feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2041717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2042717d50e4SAndreas Dilger {
2043feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2044feb0ab32SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(EXT4_SB(sb),
2045feb0ab32SDarrick J. Wong 						      block_group, gdp)))
2046717d50e4SAndreas Dilger 		return 0;
2047717d50e4SAndreas Dilger 
2048717d50e4SAndreas Dilger 	return 1;
2049717d50e4SAndreas Dilger }
2050717d50e4SAndreas Dilger 
2051feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2052feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2053feb0ab32SDarrick J. Wong {
2054feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2055feb0ab32SDarrick J. Wong 		return;
2056feb0ab32SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(EXT4_SB(sb), block_group, gdp);
2057feb0ab32SDarrick J. Wong }
2058feb0ab32SDarrick J. Wong 
2059ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2060bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2061bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2062ac27a0ecSDave Kleikamp {
2063617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2064617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2065617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2066bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2067bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2068bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2069ce421581SJose R. Santos 	int flexbg_flag = 0;
2070bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2071ac27a0ecSDave Kleikamp 
2072ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2073ce421581SJose R. Santos 		flexbg_flag = 1;
2074ce421581SJose R. Santos 
2075617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2076ac27a0ecSDave Kleikamp 
2077197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2078197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2079197cd65aSAkinobu Mita 
2080ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2081bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2082ac27a0ecSDave Kleikamp 		else
2083ac27a0ecSDave Kleikamp 			last_block = first_block +
2084617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2085ac27a0ecSDave Kleikamp 
2086bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2087bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2088bfff6873SLukas Czerner 			grp = i;
2089bfff6873SLukas Czerner 
20908fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
20912b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2092b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2093a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2094b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2095ac27a0ecSDave Kleikamp 			return 0;
2096ac27a0ecSDave Kleikamp 		}
20978fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
20982b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2099b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2100a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2101b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2102ac27a0ecSDave Kleikamp 			return 0;
2103ac27a0ecSDave Kleikamp 		}
21048fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2105bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
21062b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2107b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2108a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2109b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2110ac27a0ecSDave Kleikamp 			return 0;
2111ac27a0ecSDave Kleikamp 		}
2112955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2113feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2114b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2115b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2116fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2117fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
21187ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2119955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2120717d50e4SAndreas Dilger 				return 0;
2121717d50e4SAndreas Dilger 			}
21227ee1ec4cSLi Zefan 		}
2123955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2124ce421581SJose R. Santos 		if (!flexbg_flag)
2125617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2126ac27a0ecSDave Kleikamp 	}
2127bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2128bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2129ac27a0ecSDave Kleikamp 
21305dee5437STheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
21315dee5437STheodore Ts'o 				   EXT4_C2B(sbi, ext4_count_free_clusters(sb)));
2132617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
2133ac27a0ecSDave Kleikamp 	return 1;
2134ac27a0ecSDave Kleikamp }
2135ac27a0ecSDave Kleikamp 
2136617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2137ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2138ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2139ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2140ac27a0ecSDave Kleikamp  *
2141ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2142ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2143ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2144ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2145ac27a0ecSDave Kleikamp  *
2146ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2147ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2148ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2149617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2150ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2151ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2152ac27a0ecSDave Kleikamp  */
2153617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2154617ba13bSMingming Cao 				struct ext4_super_block *es)
2155ac27a0ecSDave Kleikamp {
2156ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
2157ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
2158ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2159ac27a0ecSDave Kleikamp 	int i;
2160ac27a0ecSDave Kleikamp #endif
2161ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2162ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2163ac27a0ecSDave Kleikamp 		return;
2164ac27a0ecSDave Kleikamp 	}
2165ac27a0ecSDave Kleikamp 
2166a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2167b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2168b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2169a8f48a95SEric Sandeen 		return;
2170a8f48a95SEric Sandeen 	}
2171a8f48a95SEric Sandeen 
2172d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2173d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2174d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2175d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2176d39195c3SAmir Goldstein 		return;
2177d39195c3SAmir Goldstein 	}
2178d39195c3SAmir Goldstein 
2179617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2180ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
2181ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
2182ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2183ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
2184ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2185ac27a0ecSDave Kleikamp 		return;
2186ac27a0ecSDave Kleikamp 	}
2187ac27a0ecSDave Kleikamp 
2188ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2189b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2190ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2191ac27a0ecSDave Kleikamp 	}
2192ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2193ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2194ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2195ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2196ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2197617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2198617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2199ac27a0ecSDave Kleikamp 			if (ret < 0)
2200b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2201b31e1552SEric Sandeen 					"Cannot turn on journaled "
2202b31e1552SEric Sandeen 					"quota: error %d", ret);
2203ac27a0ecSDave Kleikamp 		}
2204ac27a0ecSDave Kleikamp 	}
2205ac27a0ecSDave Kleikamp #endif
2206ac27a0ecSDave Kleikamp 
2207ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2208ac27a0ecSDave Kleikamp 		struct inode *inode;
2209ac27a0ecSDave Kleikamp 
221097bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
221197bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2212ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2213ac27a0ecSDave Kleikamp 			break;
2214ac27a0ecSDave Kleikamp 		}
2215ac27a0ecSDave Kleikamp 
2216617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2217871a2931SChristoph Hellwig 		dquot_initialize(inode);
2218ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2219b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2220b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
222146e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
2222e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2223ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2224617ba13bSMingming Cao 			ext4_truncate(inode);
2225ac27a0ecSDave Kleikamp 			nr_truncates++;
2226ac27a0ecSDave Kleikamp 		} else {
2227b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2228b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
222946e665e9SHarvey Harrison 				__func__, inode->i_ino);
2230ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2231ac27a0ecSDave Kleikamp 				  inode->i_ino);
2232ac27a0ecSDave Kleikamp 			nr_orphans++;
2233ac27a0ecSDave Kleikamp 		}
2234ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2235ac27a0ecSDave Kleikamp 	}
2236ac27a0ecSDave Kleikamp 
2237ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2238ac27a0ecSDave Kleikamp 
2239ac27a0ecSDave Kleikamp 	if (nr_orphans)
2240b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2241b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2242ac27a0ecSDave Kleikamp 	if (nr_truncates)
2243b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2244b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2245ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2246ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2247ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2248ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2249287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2250ac27a0ecSDave Kleikamp 	}
2251ac27a0ecSDave Kleikamp #endif
2252ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2253ac27a0ecSDave Kleikamp }
22540b8e58a1SAndreas Dilger 
2255cd2291a4SEric Sandeen /*
2256cd2291a4SEric Sandeen  * Maximal extent format file size.
2257cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2258cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2259cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2260cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2261cd2291a4SEric Sandeen  *
2262f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2263f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2264f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2265f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2266f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2267f17722f9SLukas Czerner  *
2268cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2269cd2291a4SEric Sandeen  */
2270f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2271cd2291a4SEric Sandeen {
2272cd2291a4SEric Sandeen 	loff_t res;
2273cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2274cd2291a4SEric Sandeen 
2275cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2276f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2277cd2291a4SEric Sandeen 		/*
227890c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2279cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2280cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2281cd2291a4SEric Sandeen 		 */
2282cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2283cd2291a4SEric Sandeen 
2284cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2285cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2286cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2287cd2291a4SEric Sandeen 	}
2288cd2291a4SEric Sandeen 
2289f17722f9SLukas Czerner 	/*
2290f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2291f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2292f17722f9SLukas Czerner 	 * size
2293f17722f9SLukas Czerner 	 */
2294f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2295cd2291a4SEric Sandeen 	res <<= blkbits;
2296cd2291a4SEric Sandeen 
2297cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2298cd2291a4SEric Sandeen 	if (res > upper_limit)
2299cd2291a4SEric Sandeen 		res = upper_limit;
2300cd2291a4SEric Sandeen 
2301cd2291a4SEric Sandeen 	return res;
2302cd2291a4SEric Sandeen }
2303ac27a0ecSDave Kleikamp 
2304ac27a0ecSDave Kleikamp /*
2305cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
23060fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
23070fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2308ac27a0ecSDave Kleikamp  */
2309f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2310ac27a0ecSDave Kleikamp {
2311617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
23120fc1b451SAneesh Kumar K.V 	int meta_blocks;
23130fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
23140b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
23150b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
23160b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
23170b8e58a1SAndreas Dilger 	 *
23180b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
23190b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
23200fc1b451SAneesh Kumar K.V 	 */
23210fc1b451SAneesh Kumar K.V 
2322f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
23230fc1b451SAneesh Kumar K.V 		/*
232490c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
23250b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
23260b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
23270fc1b451SAneesh Kumar K.V 		 */
23280fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
23290fc1b451SAneesh Kumar K.V 
23300fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
23310fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
23320fc1b451SAneesh Kumar K.V 
23330fc1b451SAneesh Kumar K.V 	} else {
23348180a562SAneesh Kumar K.V 		/*
23358180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
23368180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
23378180a562SAneesh Kumar K.V 		 * represent total number of blocks in
23388180a562SAneesh Kumar K.V 		 * file system block size
23398180a562SAneesh Kumar K.V 		 */
23400fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
23410fc1b451SAneesh Kumar K.V 
23420fc1b451SAneesh Kumar K.V 	}
23430fc1b451SAneesh Kumar K.V 
23440fc1b451SAneesh Kumar K.V 	/* indirect blocks */
23450fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
23460fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
23470fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
23480fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
23490fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
23500fc1b451SAneesh Kumar K.V 
23510fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
23520fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2353ac27a0ecSDave Kleikamp 
2354ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2355ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2356ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2357ac27a0ecSDave Kleikamp 	res <<= bits;
2358ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2359ac27a0ecSDave Kleikamp 		res = upper_limit;
23600fc1b451SAneesh Kumar K.V 
23610fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
23620fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
23630fc1b451SAneesh Kumar K.V 
2364ac27a0ecSDave Kleikamp 	return res;
2365ac27a0ecSDave Kleikamp }
2366ac27a0ecSDave Kleikamp 
2367617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
236870bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2369ac27a0ecSDave Kleikamp {
2370617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2371fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2372ac27a0ecSDave Kleikamp 	int has_super = 0;
2373ac27a0ecSDave Kleikamp 
2374ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2375ac27a0ecSDave Kleikamp 
2376617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2377ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
237870bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2379ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2380617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2381ac27a0ecSDave Kleikamp 		has_super = 1;
23820b8e58a1SAndreas Dilger 
2383617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2384ac27a0ecSDave Kleikamp }
2385ac27a0ecSDave Kleikamp 
2386c9de560dSAlex Tomas /**
2387c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2388c9de560dSAlex Tomas  * @sbi: In memory super block info
2389c9de560dSAlex Tomas  *
2390c9de560dSAlex Tomas  * If we have specified it via mount option, then
2391c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2392c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2393c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2394c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2395c9de560dSAlex Tomas  *
2396c9de560dSAlex Tomas  */
2397c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2398c9de560dSAlex Tomas {
2399c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2400c9de560dSAlex Tomas 	unsigned long stripe_width =
2401c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
24023eb08658SDan Ehrenberg 	int ret;
2403c9de560dSAlex Tomas 
2404c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
24053eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
24063eb08658SDan Ehrenberg 	else if (stripe_width <= sbi->s_blocks_per_group)
24073eb08658SDan Ehrenberg 		ret = stripe_width;
24083eb08658SDan Ehrenberg 	else if (stride <= sbi->s_blocks_per_group)
24093eb08658SDan Ehrenberg 		ret = stride;
24103eb08658SDan Ehrenberg 	else
24113eb08658SDan Ehrenberg 		ret = 0;
2412c9de560dSAlex Tomas 
24133eb08658SDan Ehrenberg 	/*
24143eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
24153eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
24163eb08658SDan Ehrenberg 	 */
24173eb08658SDan Ehrenberg 	if (ret <= 1)
24183eb08658SDan Ehrenberg 		ret = 0;
2419c9de560dSAlex Tomas 
24203eb08658SDan Ehrenberg 	return ret;
2421c9de560dSAlex Tomas }
2422ac27a0ecSDave Kleikamp 
24233197ebdbSTheodore Ts'o /* sysfs supprt */
24243197ebdbSTheodore Ts'o 
24253197ebdbSTheodore Ts'o struct ext4_attr {
24263197ebdbSTheodore Ts'o 	struct attribute attr;
24273197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
24283197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
24293197ebdbSTheodore Ts'o 			 const char *, size_t);
24303197ebdbSTheodore Ts'o 	int offset;
24313197ebdbSTheodore Ts'o };
24323197ebdbSTheodore Ts'o 
24333197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
24343197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
24353197ebdbSTheodore Ts'o {
24363197ebdbSTheodore Ts'o 	char *endp;
24373197ebdbSTheodore Ts'o 
2438e7d2860bSAndré Goddard Rosa 	*value = simple_strtoul(skip_spaces(buf), &endp, 0);
2439e7d2860bSAndré Goddard Rosa 	endp = skip_spaces(endp);
24403197ebdbSTheodore Ts'o 	if (*endp || *value > max)
24413197ebdbSTheodore Ts'o 		return -EINVAL;
24423197ebdbSTheodore Ts'o 
24433197ebdbSTheodore Ts'o 	return 0;
24443197ebdbSTheodore Ts'o }
24453197ebdbSTheodore Ts'o 
24463197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
24473197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
24483197ebdbSTheodore Ts'o 					      char *buf)
24493197ebdbSTheodore Ts'o {
24503197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
24517b415bf6SAditya Kali 		(s64) EXT4_C2B(sbi,
24527b415bf6SAditya Kali 			percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
24533197ebdbSTheodore Ts'o }
24543197ebdbSTheodore Ts'o 
24553197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
24563197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
24573197ebdbSTheodore Ts'o {
24583197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24593197ebdbSTheodore Ts'o 
2460f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2461f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24623197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
24633197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
24643197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
24653197ebdbSTheodore Ts'o }
24663197ebdbSTheodore Ts'o 
24673197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
24683197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
24693197ebdbSTheodore Ts'o {
24703197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24713197ebdbSTheodore Ts'o 
2472f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2473f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24743197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2475a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
24763197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2477a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
24783197ebdbSTheodore Ts'o }
24793197ebdbSTheodore Ts'o 
24803197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
24813197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
24823197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
24833197ebdbSTheodore Ts'o {
24843197ebdbSTheodore Ts'o 	unsigned long t;
24853197ebdbSTheodore Ts'o 
24863197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
24873197ebdbSTheodore Ts'o 		return -EINVAL;
24883197ebdbSTheodore Ts'o 
24895dbd571dSAlexander V. Lukyanov 	if (t && !is_power_of_2(t))
24903197ebdbSTheodore Ts'o 		return -EINVAL;
24913197ebdbSTheodore Ts'o 
24923197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
24933197ebdbSTheodore Ts'o 	return count;
24943197ebdbSTheodore Ts'o }
24953197ebdbSTheodore Ts'o 
24963197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
24973197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
24983197ebdbSTheodore Ts'o {
24993197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
25003197ebdbSTheodore Ts'o 
25013197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
25023197ebdbSTheodore Ts'o }
25033197ebdbSTheodore Ts'o 
25043197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
25053197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
25063197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
25073197ebdbSTheodore Ts'o {
25083197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
25093197ebdbSTheodore Ts'o 	unsigned long t;
25103197ebdbSTheodore Ts'o 
25113197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
25123197ebdbSTheodore Ts'o 		return -EINVAL;
25133197ebdbSTheodore Ts'o 	*ui = t;
25143197ebdbSTheodore Ts'o 	return count;
25153197ebdbSTheodore Ts'o }
25163197ebdbSTheodore Ts'o 
25172c0544b2STheodore Ts'o static ssize_t trigger_test_error(struct ext4_attr *a,
25182c0544b2STheodore Ts'o 				  struct ext4_sb_info *sbi,
25192c0544b2STheodore Ts'o 				  const char *buf, size_t count)
25202c0544b2STheodore Ts'o {
25212c0544b2STheodore Ts'o 	int len = count;
25222c0544b2STheodore Ts'o 
25232c0544b2STheodore Ts'o 	if (!capable(CAP_SYS_ADMIN))
25242c0544b2STheodore Ts'o 		return -EPERM;
25252c0544b2STheodore Ts'o 
25262c0544b2STheodore Ts'o 	if (len && buf[len-1] == '\n')
25272c0544b2STheodore Ts'o 		len--;
25282c0544b2STheodore Ts'o 
25292c0544b2STheodore Ts'o 	if (len)
25302c0544b2STheodore Ts'o 		ext4_error(sbi->s_sb, "%.*s", len, buf);
25312c0544b2STheodore Ts'o 	return count;
25322c0544b2STheodore Ts'o }
25332c0544b2STheodore Ts'o 
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,					\
25393197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
25403197ebdbSTheodore Ts'o }
25413197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
25423197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
25433197ebdbSTheodore Ts'o 
2544857ac889SLukas Czerner #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
25453197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
25463197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
25473197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
25483197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
25493197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
25503197ebdbSTheodore Ts'o 
25513197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
25523197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
25533197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
25543197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
25553197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
255611013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
25573197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
25583197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
25593197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
25603197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
25613197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
25623197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
256355138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
256467a5da56SZheng Liu EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
25652c0544b2STheodore Ts'o EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
25663197ebdbSTheodore Ts'o 
25673197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
25683197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
25693197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
25703197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
25713197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
257211013911SAndreas Dilger 	ATTR_LIST(inode_goal),
25733197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
25743197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
25753197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
25763197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
25773197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
25783197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
257955138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
258067a5da56SZheng Liu 	ATTR_LIST(extent_max_zeroout_kb),
25812c0544b2STheodore Ts'o 	ATTR_LIST(trigger_fs_error),
25823197ebdbSTheodore Ts'o 	NULL,
25833197ebdbSTheodore Ts'o };
25843197ebdbSTheodore Ts'o 
2585857ac889SLukas Czerner /* Features this copy of ext4 supports */
2586857ac889SLukas Czerner EXT4_INFO_ATTR(lazy_itable_init);
258727ee40dfSLukas Czerner EXT4_INFO_ATTR(batched_discard);
25885e7bbef1STheodore Ts'o EXT4_INFO_ATTR(meta_bg_resize);
2589857ac889SLukas Czerner 
2590857ac889SLukas Czerner static struct attribute *ext4_feat_attrs[] = {
2591857ac889SLukas Czerner 	ATTR_LIST(lazy_itable_init),
259227ee40dfSLukas Czerner 	ATTR_LIST(batched_discard),
25935e7bbef1STheodore Ts'o 	ATTR_LIST(meta_bg_resize),
2594857ac889SLukas Czerner 	NULL,
2595857ac889SLukas Czerner };
2596857ac889SLukas Czerner 
25973197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
25983197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
25993197ebdbSTheodore Ts'o {
26003197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26013197ebdbSTheodore Ts'o 						s_kobj);
26023197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
26033197ebdbSTheodore Ts'o 
26043197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
26053197ebdbSTheodore Ts'o }
26063197ebdbSTheodore Ts'o 
26073197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
26083197ebdbSTheodore Ts'o 			       struct attribute *attr,
26093197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
26103197ebdbSTheodore Ts'o {
26113197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26123197ebdbSTheodore Ts'o 						s_kobj);
26133197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
26143197ebdbSTheodore Ts'o 
26153197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
26163197ebdbSTheodore Ts'o }
26173197ebdbSTheodore Ts'o 
26183197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
26193197ebdbSTheodore Ts'o {
26203197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26213197ebdbSTheodore Ts'o 						s_kobj);
26223197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
26233197ebdbSTheodore Ts'o }
26243197ebdbSTheodore Ts'o 
262552cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = {
26263197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
26273197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
26283197ebdbSTheodore Ts'o };
26293197ebdbSTheodore Ts'o 
26303197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
26313197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
26323197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
26333197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
26343197ebdbSTheodore Ts'o };
26353197ebdbSTheodore Ts'o 
2636857ac889SLukas Czerner static void ext4_feat_release(struct kobject *kobj)
2637857ac889SLukas Czerner {
2638857ac889SLukas Czerner 	complete(&ext4_feat->f_kobj_unregister);
2639857ac889SLukas Czerner }
2640857ac889SLukas Czerner 
2641857ac889SLukas Czerner static struct kobj_type ext4_feat_ktype = {
2642857ac889SLukas Czerner 	.default_attrs	= ext4_feat_attrs,
2643857ac889SLukas Czerner 	.sysfs_ops	= &ext4_attr_ops,
2644857ac889SLukas Czerner 	.release	= ext4_feat_release,
2645857ac889SLukas Czerner };
2646857ac889SLukas Czerner 
2647a13fb1a4SEric Sandeen /*
2648a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2649a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2650a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2651a13fb1a4SEric Sandeen  * 0 if it cannot be.
2652a13fb1a4SEric Sandeen  */
2653a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2654a13fb1a4SEric Sandeen {
2655a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2656a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2657a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2658a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2659a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2660a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2661a13fb1a4SEric Sandeen 		return 0;
2662a13fb1a4SEric Sandeen 	}
2663a13fb1a4SEric Sandeen 
2664a13fb1a4SEric Sandeen 	if (readonly)
2665a13fb1a4SEric Sandeen 		return 1;
2666a13fb1a4SEric Sandeen 
2667a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2668a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2669a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2670a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2671a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2672a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2673a13fb1a4SEric Sandeen 		return 0;
2674a13fb1a4SEric Sandeen 	}
2675a13fb1a4SEric Sandeen 	/*
2676a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2677a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2678a13fb1a4SEric Sandeen 	 */
2679a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2680a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2681a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2682a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2683a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2684a13fb1a4SEric Sandeen 			return 0;
2685a13fb1a4SEric Sandeen 		}
2686a13fb1a4SEric Sandeen 	}
2687bab08ab9STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2688bab08ab9STheodore Ts'o 	    !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2689bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2690bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2691bab08ab9STheodore Ts'o 			 "extents feature\n");
2692bab08ab9STheodore Ts'o 		return 0;
2693bab08ab9STheodore Ts'o 	}
26947c319d32SAditya Kali 
26957c319d32SAditya Kali #ifndef CONFIG_QUOTA
26967c319d32SAditya Kali 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
26977c319d32SAditya Kali 	    !readonly) {
26987c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
26997c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
27007c319d32SAditya Kali 			 "without CONFIG_QUOTA");
27017c319d32SAditya Kali 		return 0;
27027c319d32SAditya Kali 	}
27037c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2704a13fb1a4SEric Sandeen 	return 1;
2705a13fb1a4SEric Sandeen }
2706a13fb1a4SEric Sandeen 
270766e61a9eSTheodore Ts'o /*
270866e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
270966e61a9eSTheodore Ts'o  * on the file system
271066e61a9eSTheodore Ts'o  */
271166e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
271266e61a9eSTheodore Ts'o {
271366e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
271466e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
271566e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
271666e61a9eSTheodore Ts'o 
271766e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
271866e61a9eSTheodore Ts'o 	es = sbi->s_es;
271966e61a9eSTheodore Ts'o 
272066e61a9eSTheodore Ts'o 	if (es->s_error_count)
272166e61a9eSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count: %u",
272266e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
272366e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
272466e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
272566e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
272666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
272766e61a9eSTheodore Ts'o 		       es->s_first_error_func,
272866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
272966e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
273066e61a9eSTheodore Ts'o 			printk(": inode %u",
273166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
273266e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
273366e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
273466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
273566e61a9eSTheodore Ts'o 		printk("\n");
273666e61a9eSTheodore Ts'o 	}
273766e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
273866e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
273966e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
274066e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
274166e61a9eSTheodore Ts'o 		       es->s_last_error_func,
274266e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
274366e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
274466e61a9eSTheodore Ts'o 			printk(": inode %u",
274566e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
274666e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
274766e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
274866e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
274966e61a9eSTheodore Ts'o 		printk("\n");
275066e61a9eSTheodore Ts'o 	}
275166e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
275266e61a9eSTheodore Ts'o }
275366e61a9eSTheodore Ts'o 
2754bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2755bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2756bfff6873SLukas Czerner {
2757bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2758bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2759bfff6873SLukas Czerner 	struct super_block *sb;
2760bfff6873SLukas Czerner 	unsigned long timeout = 0;
2761bfff6873SLukas Czerner 	int ret = 0;
2762bfff6873SLukas Czerner 
2763bfff6873SLukas Czerner 	sb = elr->lr_super;
2764bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2765bfff6873SLukas Czerner 
27668e8ad8a5SJan Kara 	sb_start_write(sb);
2767bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2768bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2769bfff6873SLukas Czerner 		if (!gdp) {
2770bfff6873SLukas Czerner 			ret = 1;
2771bfff6873SLukas Czerner 			break;
2772bfff6873SLukas Czerner 		}
2773bfff6873SLukas Czerner 
2774bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2775bfff6873SLukas Czerner 			break;
2776bfff6873SLukas Czerner 	}
2777bfff6873SLukas Czerner 
2778bfff6873SLukas Czerner 	if (group == ngroups)
2779bfff6873SLukas Czerner 		ret = 1;
2780bfff6873SLukas Czerner 
2781bfff6873SLukas Czerner 	if (!ret) {
2782bfff6873SLukas Czerner 		timeout = jiffies;
2783bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2784bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2785bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
278651ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
278751ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2788bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2789bfff6873SLukas Czerner 		}
2790bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2791bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2792bfff6873SLukas Czerner 	}
27938e8ad8a5SJan Kara 	sb_end_write(sb);
2794bfff6873SLukas Czerner 
2795bfff6873SLukas Czerner 	return ret;
2796bfff6873SLukas Czerner }
2797bfff6873SLukas Czerner 
2798bfff6873SLukas Czerner /*
2799bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
28004ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2801bfff6873SLukas Czerner  */
2802bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2803bfff6873SLukas Czerner {
2804bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2805bfff6873SLukas Czerner 
2806bfff6873SLukas Czerner 	if (!elr)
2807bfff6873SLukas Czerner 		return;
2808bfff6873SLukas Czerner 
2809bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2810bfff6873SLukas Czerner 
2811bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2812bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2813bfff6873SLukas Czerner 	kfree(elr);
2814bfff6873SLukas Czerner }
2815bfff6873SLukas Czerner 
2816bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2817bfff6873SLukas Czerner {
28181bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
28191bb933fbSLukas Czerner 	if (!ext4_li_info) {
28201bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2821bfff6873SLukas Czerner 		return;
28221bb933fbSLukas Czerner 	}
2823bfff6873SLukas Czerner 
2824bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
28251bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2826bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
28271bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2828bfff6873SLukas Czerner }
2829bfff6873SLukas Czerner 
28308f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
28318f1f7453SEric Sandeen 
2832bfff6873SLukas Czerner /*
2833bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2834bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2835bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2836bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2837bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2838bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2839bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2840bfff6873SLukas Czerner  */
2841bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2842bfff6873SLukas Czerner {
2843bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2844bfff6873SLukas Czerner 	struct list_head *pos, *n;
2845bfff6873SLukas Czerner 	struct ext4_li_request *elr;
28464ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2847bfff6873SLukas Czerner 
2848bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2849bfff6873SLukas Czerner 
2850bfff6873SLukas Czerner cont_thread:
2851bfff6873SLukas Czerner 	while (true) {
2852bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2853bfff6873SLukas Czerner 
2854bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2855bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2856bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2857bfff6873SLukas Czerner 			goto exit_thread;
2858bfff6873SLukas Czerner 		}
2859bfff6873SLukas Czerner 
2860bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2861bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2862bfff6873SLukas Czerner 					 lr_request);
2863bfff6873SLukas Czerner 
2864b2c78cd0STheodore Ts'o 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
2865b2c78cd0STheodore Ts'o 				if (ext4_run_li_request(elr) != 0) {
2866b2c78cd0STheodore Ts'o 					/* error, remove the lazy_init job */
2867bfff6873SLukas Czerner 					ext4_remove_li_request(elr);
2868bfff6873SLukas Czerner 					continue;
2869bfff6873SLukas Czerner 				}
2870b2c78cd0STheodore Ts'o 			}
2871bfff6873SLukas Czerner 
2872bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2873bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2874bfff6873SLukas Czerner 		}
2875bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2876bfff6873SLukas Czerner 
2877a0acae0eSTejun Heo 		try_to_freeze();
2878bfff6873SLukas Czerner 
28794ed5c033SLukas Czerner 		cur = jiffies;
28804ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2881f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2882bfff6873SLukas Czerner 			cond_resched();
2883bfff6873SLukas Czerner 			continue;
2884bfff6873SLukas Czerner 		}
2885bfff6873SLukas Czerner 
28864ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
28874ed5c033SLukas Czerner 
28888f1f7453SEric Sandeen 		if (kthread_should_stop()) {
28898f1f7453SEric Sandeen 			ext4_clear_request_list();
28908f1f7453SEric Sandeen 			goto exit_thread;
28918f1f7453SEric Sandeen 		}
2892bfff6873SLukas Czerner 	}
2893bfff6873SLukas Czerner 
2894bfff6873SLukas Czerner exit_thread:
2895bfff6873SLukas Czerner 	/*
2896bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
2897bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
2898bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
2899bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
2900bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
2901bfff6873SLukas Czerner 	 * new one.
2902bfff6873SLukas Czerner 	 */
2903bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2904bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
2905bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
2906bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2907bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2908bfff6873SLukas Czerner 		goto cont_thread;
2909bfff6873SLukas Czerner 	}
2910bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
2911bfff6873SLukas Czerner 	kfree(ext4_li_info);
2912bfff6873SLukas Czerner 	ext4_li_info = NULL;
2913bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2914bfff6873SLukas Czerner 
2915bfff6873SLukas Czerner 	return 0;
2916bfff6873SLukas Czerner }
2917bfff6873SLukas Czerner 
2918bfff6873SLukas Czerner static void ext4_clear_request_list(void)
2919bfff6873SLukas Czerner {
2920bfff6873SLukas Czerner 	struct list_head *pos, *n;
2921bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2922bfff6873SLukas Czerner 
2923bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2924bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2925bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
2926bfff6873SLukas Czerner 				 lr_request);
2927bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
2928bfff6873SLukas Czerner 	}
2929bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2930bfff6873SLukas Czerner }
2931bfff6873SLukas Czerner 
2932bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
2933bfff6873SLukas Czerner {
29348f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
29358f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
29368f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
29378f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
2938bfff6873SLukas Czerner 		ext4_clear_request_list();
2939bfff6873SLukas Czerner 		kfree(ext4_li_info);
2940bfff6873SLukas Czerner 		ext4_li_info = NULL;
294192b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
2942bfff6873SLukas Czerner 				 "initialization thread\n",
2943bfff6873SLukas Czerner 				 err);
2944bfff6873SLukas Czerner 		return err;
2945bfff6873SLukas Czerner 	}
2946bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
2947bfff6873SLukas Czerner 	return 0;
2948bfff6873SLukas Czerner }
2949bfff6873SLukas Czerner 
2950bfff6873SLukas Czerner /*
2951bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
2952bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
2953bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
2954bfff6873SLukas Czerner  * groups and return total number of groups.
2955bfff6873SLukas Czerner  */
2956bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2957bfff6873SLukas Czerner {
2958bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2959bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2960bfff6873SLukas Czerner 
2961bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
2962bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2963bfff6873SLukas Czerner 		if (!gdp)
2964bfff6873SLukas Czerner 			continue;
2965bfff6873SLukas Czerner 
2966bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2967bfff6873SLukas Czerner 			break;
2968bfff6873SLukas Czerner 	}
2969bfff6873SLukas Czerner 
2970bfff6873SLukas Czerner 	return group;
2971bfff6873SLukas Czerner }
2972bfff6873SLukas Czerner 
2973bfff6873SLukas Czerner static int ext4_li_info_new(void)
2974bfff6873SLukas Czerner {
2975bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
2976bfff6873SLukas Czerner 
2977bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2978bfff6873SLukas Czerner 	if (!eli)
2979bfff6873SLukas Czerner 		return -ENOMEM;
2980bfff6873SLukas Czerner 
2981bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
2982bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
2983bfff6873SLukas Czerner 
2984bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
2985bfff6873SLukas Czerner 
2986bfff6873SLukas Czerner 	ext4_li_info = eli;
2987bfff6873SLukas Czerner 
2988bfff6873SLukas Czerner 	return 0;
2989bfff6873SLukas Czerner }
2990bfff6873SLukas Czerner 
2991bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
2992bfff6873SLukas Czerner 					    ext4_group_t start)
2993bfff6873SLukas Czerner {
2994bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2995bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2996bfff6873SLukas Czerner 	unsigned long rnd;
2997bfff6873SLukas Czerner 
2998bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
2999bfff6873SLukas Czerner 	if (!elr)
3000bfff6873SLukas Czerner 		return NULL;
3001bfff6873SLukas Czerner 
3002bfff6873SLukas Czerner 	elr->lr_super = sb;
3003bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3004bfff6873SLukas Czerner 	elr->lr_next_group = start;
3005bfff6873SLukas Czerner 
3006bfff6873SLukas Czerner 	/*
3007bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3008bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3009bfff6873SLukas Czerner 	 * better.
3010bfff6873SLukas Czerner 	 */
3011bfff6873SLukas Czerner 	get_random_bytes(&rnd, sizeof(rnd));
3012bfff6873SLukas Czerner 	elr->lr_next_sched = jiffies + (unsigned long)rnd %
3013bfff6873SLukas Czerner 			     (EXT4_DEF_LI_MAX_START_DELAY * HZ);
3014bfff6873SLukas Czerner 
3015bfff6873SLukas Czerner 	return elr;
3016bfff6873SLukas Czerner }
3017bfff6873SLukas Czerner 
3018bfff6873SLukas Czerner static int ext4_register_li_request(struct super_block *sb,
3019bfff6873SLukas Czerner 				    ext4_group_t first_not_zeroed)
3020bfff6873SLukas Czerner {
3021bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3022bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3023bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
30246c5a6cb9SAndrew Morton 	int ret = 0;
3025bfff6873SLukas Czerner 
302651ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
302751ce6511SLukas Czerner 		/*
302851ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
302951ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
303051ce6511SLukas Czerner 		 */
303151ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
3032beed5ecbSNicolas Kaiser 		return 0;
303351ce6511SLukas Czerner 	}
3034bfff6873SLukas Czerner 
3035bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
3036bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
303755ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
3038beed5ecbSNicolas Kaiser 		return 0;
3039bfff6873SLukas Czerner 
3040bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
3041beed5ecbSNicolas Kaiser 	if (!elr)
3042beed5ecbSNicolas Kaiser 		return -ENOMEM;
3043bfff6873SLukas Czerner 
3044bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3045bfff6873SLukas Czerner 
3046bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3047bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3048bfff6873SLukas Czerner 		if (ret)
3049bfff6873SLukas Czerner 			goto out;
3050bfff6873SLukas Czerner 	}
3051bfff6873SLukas Czerner 
3052bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3053bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3054bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3055bfff6873SLukas Czerner 
3056bfff6873SLukas Czerner 	sbi->s_li_request = elr;
305746e4690bSTao Ma 	/*
305846e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
305946e4690bSTao Ma 	 * the request_list and the removal and free of it is
306046e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
306146e4690bSTao Ma 	 */
306246e4690bSTao Ma 	elr = NULL;
3063bfff6873SLukas Czerner 
3064bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3065bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3066bfff6873SLukas Czerner 		if (ret)
3067bfff6873SLukas Czerner 			goto out;
3068bfff6873SLukas Czerner 	}
3069bfff6873SLukas Czerner out:
3070bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3071beed5ecbSNicolas Kaiser 	if (ret)
3072bfff6873SLukas Czerner 		kfree(elr);
3073bfff6873SLukas Czerner 	return ret;
3074bfff6873SLukas Czerner }
3075bfff6873SLukas Czerner 
3076bfff6873SLukas Czerner /*
3077bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3078bfff6873SLukas Czerner  * module unload.
3079bfff6873SLukas Czerner  */
3080bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3081bfff6873SLukas Czerner {
3082bfff6873SLukas Czerner 	/*
3083bfff6873SLukas Czerner 	 * If thread exited earlier
3084bfff6873SLukas Czerner 	 * there's nothing to be done.
3085bfff6873SLukas Czerner 	 */
30868f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3087bfff6873SLukas Czerner 		return;
3088bfff6873SLukas Czerner 
30898f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3090bfff6873SLukas Czerner }
3091bfff6873SLukas Czerner 
309225ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
309325ed6e8aSDarrick J. Wong {
309425ed6e8aSDarrick J. Wong 	int ret = 1;
309525ed6e8aSDarrick J. Wong 	int compat, incompat;
309625ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
309725ed6e8aSDarrick J. Wong 
309825ed6e8aSDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
309925ed6e8aSDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
310025ed6e8aSDarrick J. Wong 		/* journal checksum v2 */
310125ed6e8aSDarrick J. Wong 		compat = 0;
310225ed6e8aSDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V2;
310325ed6e8aSDarrick J. Wong 	} else {
310425ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
310525ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
310625ed6e8aSDarrick J. Wong 		incompat = 0;
310725ed6e8aSDarrick J. Wong 	}
310825ed6e8aSDarrick J. Wong 
310925ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
311025ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
311125ed6e8aSDarrick J. Wong 				compat, 0,
311225ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
311325ed6e8aSDarrick J. Wong 				incompat);
311425ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
311525ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
311625ed6e8aSDarrick J. Wong 				compat, 0,
311725ed6e8aSDarrick J. Wong 				incompat);
311825ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
311925ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
312025ed6e8aSDarrick J. Wong 	} else {
312125ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal,
312225ed6e8aSDarrick J. Wong 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
312325ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
312425ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_CSUM_V2);
312525ed6e8aSDarrick J. Wong 	}
312625ed6e8aSDarrick J. Wong 
312725ed6e8aSDarrick J. Wong 	return ret;
312825ed6e8aSDarrick J. Wong }
312925ed6e8aSDarrick J. Wong 
3130952fc18eSTheodore Ts'o /*
3131952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3132952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3133952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3134952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3135952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3136952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3137952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3138952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3139952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3140952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3141952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3142952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3143952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3144952fc18eSTheodore Ts'o  */
3145952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3146952fc18eSTheodore Ts'o 			  char *buf)
3147952fc18eSTheodore Ts'o {
3148952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3149952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3150952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3151952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3152952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3153952fc18eSTheodore Ts'o 
31540548bbb8STheodore Ts'o 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC))
31550548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
31560548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
31570548bbb8STheodore Ts'o 
3158952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3159952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3160952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3161952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3162952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3163952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3164952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3165952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3166952fc18eSTheodore Ts'o 			count++;
3167952fc18eSTheodore Ts'o 		}
3168952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3169952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3170952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3171952fc18eSTheodore Ts'o 			count++;
3172952fc18eSTheodore Ts'o 		}
3173952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3174952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3175952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3176952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3177952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3178952fc18eSTheodore Ts'o 				count++;
3179952fc18eSTheodore Ts'o 			}
3180952fc18eSTheodore Ts'o 		if (i != grp)
3181952fc18eSTheodore Ts'o 			continue;
3182952fc18eSTheodore Ts'o 		s = 0;
3183952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3184952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3185952fc18eSTheodore Ts'o 			count++;
3186952fc18eSTheodore Ts'o 		}
3187952fc18eSTheodore Ts'o 		for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) {
3188952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3189952fc18eSTheodore Ts'o 			count++;
3190952fc18eSTheodore Ts'o 		}
3191952fc18eSTheodore Ts'o 	}
3192952fc18eSTheodore Ts'o 	if (!count)
3193952fc18eSTheodore Ts'o 		return 0;
3194952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3195952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3196952fc18eSTheodore Ts'o }
3197952fc18eSTheodore Ts'o 
3198952fc18eSTheodore Ts'o /*
3199952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3200952fc18eSTheodore Ts'o  */
3201952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3202952fc18eSTheodore Ts'o {
3203952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3204952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
3205952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3206952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
3207952fc18eSTheodore Ts'o 	char *buf = (char *) get_zeroed_page(GFP_KERNEL);
3208952fc18eSTheodore Ts'o 
3209952fc18eSTheodore Ts'o 	memset(buf, 0, PAGE_SIZE);
3210952fc18eSTheodore Ts'o 	if (!buf)
3211952fc18eSTheodore Ts'o 		return -ENOMEM;
3212952fc18eSTheodore Ts'o 
3213952fc18eSTheodore Ts'o 	/*
3214952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3215952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3216952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3217952fc18eSTheodore Ts'o 	 */
3218952fc18eSTheodore Ts'o 
3219952fc18eSTheodore Ts'o 	/*
3220952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3221952fc18eSTheodore Ts'o 	 */
3222952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3223952fc18eSTheodore Ts'o 
3224952fc18eSTheodore Ts'o 	/*
3225952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3226952fc18eSTheodore Ts'o 	 */
3227952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3228952fc18eSTheodore Ts'o 		int blks;
3229952fc18eSTheodore Ts'o 
3230952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3231952fc18eSTheodore Ts'o 		overhead += blks;
3232952fc18eSTheodore Ts'o 		if (blks)
3233952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3234952fc18eSTheodore Ts'o 		cond_resched();
3235952fc18eSTheodore Ts'o 	}
3236952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3237952fc18eSTheodore Ts'o 	smp_wmb();
3238952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3239952fc18eSTheodore Ts'o 	return 0;
3240952fc18eSTheodore Ts'o }
3241952fc18eSTheodore Ts'o 
3242617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3243ac27a0ecSDave Kleikamp {
3244d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3245ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3246617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
3247617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
3248617ba13bSMingming Cao 	ext4_fsblk_t block;
3249617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
325070bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3251ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3252ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3253ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3254ac27a0ecSDave Kleikamp 	struct inode *root;
32559f6200bbSTheodore Ts'o 	char *cp;
32560390131bSFrank Mayhar 	const char *descr;
3257dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3258281b5995STheodore Ts'o 	int blocksize, clustersize;
32594ec11028STheodore Ts'o 	unsigned int db_count;
32604ec11028STheodore Ts'o 	unsigned int i;
3261281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3262bd81d8eeSLaurent Vivier 	__u64 blocks_count;
3263833f4077SPeter Zijlstra 	int err;
3264b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3265bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3266ac27a0ecSDave Kleikamp 
3267ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3268ac27a0ecSDave Kleikamp 	if (!sbi)
3269dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3270705895b6SPekka Enberg 
3271705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3272705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3273705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
3274705895b6SPekka Enberg 		kfree(sbi);
3275dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3276705895b6SPekka Enberg 	}
3277ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
32782c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3279ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
328008cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, EXT4_DEF_RESUID);
328108cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, EXT4_DEF_RESGID);
3282240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3283d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3284f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3285f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3286f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3287ac27a0ecSDave Kleikamp 
32889f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
32899f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
32909f6200bbSTheodore Ts'o 		*cp = '!';
32919f6200bbSTheodore Ts'o 
3292dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3293617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3294ac27a0ecSDave Kleikamp 	if (!blocksize) {
3295b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3296ac27a0ecSDave Kleikamp 		goto out_fail;
3297ac27a0ecSDave Kleikamp 	}
3298ac27a0ecSDave Kleikamp 
3299ac27a0ecSDave Kleikamp 	/*
3300617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3301ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3302ac27a0ecSDave Kleikamp 	 */
3303617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
330470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
330570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3306ac27a0ecSDave Kleikamp 	} else {
330770bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3308ac27a0ecSDave Kleikamp 	}
3309ac27a0ecSDave Kleikamp 
331070bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
3311b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3312ac27a0ecSDave Kleikamp 		goto out_fail;
3313ac27a0ecSDave Kleikamp 	}
3314ac27a0ecSDave Kleikamp 	/*
3315ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3316617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3317ac27a0ecSDave Kleikamp 	 */
33182716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3319ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3320ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3321617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3322617ba13bSMingming Cao 		goto cantfind_ext4;
3323afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3324ac27a0ecSDave Kleikamp 
3325feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3326feb0ab32SDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3327feb0ab32SDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
3328feb0ab32SDarrick J. Wong 	    EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
3329feb0ab32SDarrick J. Wong 		ext4_warning(sb, KERN_INFO "metadata_csum and uninit_bg are "
3330feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3331feb0ab32SDarrick J. Wong 
3332d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3333d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3334d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3335d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3336d25425f8SDarrick J. Wong 		silent = 1;
3337d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3338d25425f8SDarrick J. Wong 	}
3339d25425f8SDarrick J. Wong 
33400441984aSDarrick J. Wong 	/* Load the checksum driver */
33410441984aSDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
33420441984aSDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
33430441984aSDarrick J. Wong 		sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
33440441984aSDarrick J. Wong 		if (IS_ERR(sbi->s_chksum_driver)) {
33450441984aSDarrick J. Wong 			ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
33460441984aSDarrick J. Wong 			ret = PTR_ERR(sbi->s_chksum_driver);
33470441984aSDarrick J. Wong 			sbi->s_chksum_driver = NULL;
33480441984aSDarrick J. Wong 			goto failed_mount;
33490441984aSDarrick J. Wong 		}
33500441984aSDarrick J. Wong 	}
33510441984aSDarrick J. Wong 
3352a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3353a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3354a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3355a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3356a9c47317SDarrick J. Wong 		silent = 1;
3357a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3358a9c47317SDarrick J. Wong 	}
3359a9c47317SDarrick J. Wong 
3360a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3361a9c47317SDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3362a9c47317SDarrick J. Wong 			EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
3363a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3364a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3365a9c47317SDarrick J. Wong 
3366ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3367ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3368fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3369617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3370fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
337187f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3372fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3373617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3374fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3375ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
337603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
3377fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
33782e7842b8SHugh Dickins #endif
337903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3380fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
33812e7842b8SHugh Dickins #endif
33826fd7a467STheodore Ts'o 	set_opt(sb, MBLK_IO_SUBMIT);
3383617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3384fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3385617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3386fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3387617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3388fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3389ac27a0ecSDave Kleikamp 
3390617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3391fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3392bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3393fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3394bb4f397aSAneesh Kumar K.V 	else
3395fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
33968b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
3397fd8c37ecSTheodore Ts'o 		set_opt(sb, BLOCK_VALIDITY);
33988b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3399fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3400ac27a0ecSDave Kleikamp 
340108cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
340208cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
340330773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
340430773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
340530773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3406ac27a0ecSDave Kleikamp 
34078b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3408fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3409ac27a0ecSDave Kleikamp 
34101e2462f9SMingming Cao 	/*
3411dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3412dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3413dd919b98SAneesh Kumar K.V 	 */
3414*bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
34158b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3416fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3417dd919b98SAneesh Kumar K.V 
341851ce6511SLukas Czerner 	/*
341951ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
342051ce6511SLukas Czerner 	 * no mount option specified.
342151ce6511SLukas Czerner 	 */
342251ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
342351ce6511SLukas Czerner 
34248b67f04aSTheodore Ts'o 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
3425661aa520SEric Sandeen 			   &journal_devnum, &journal_ioprio, 0)) {
34268b67f04aSTheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
34278b67f04aSTheodore Ts'o 			 "failed to parse options in superblock: %s",
34288b67f04aSTheodore Ts'o 			 sbi->s_es->s_mount_opts);
34298b67f04aSTheodore Ts'o 	}
34305a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3431b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3432661aa520SEric Sandeen 			   &journal_ioprio, 0))
3433ac27a0ecSDave Kleikamp 		goto failed_mount;
3434ac27a0ecSDave Kleikamp 
343556889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
343656889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
343756889787STheodore Ts'o 			    "with data=journal disables delayed "
343856889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
343956889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
344056889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
344156889787STheodore Ts'o 				 "both data=journal and delalloc");
344256889787STheodore Ts'o 			goto failed_mount;
344356889787STheodore Ts'o 		}
344456889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
344556889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
344656889787STheodore Ts'o 				 "both data=journal and delalloc");
344756889787STheodore Ts'o 			goto failed_mount;
344856889787STheodore Ts'o 		}
344956889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
345056889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
345156889787STheodore Ts'o 	}
345256889787STheodore Ts'o 
345356889787STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
345456889787STheodore Ts'o 	if (test_opt(sb, DIOREAD_NOLOCK)) {
345556889787STheodore Ts'o 		if (blocksize < PAGE_SIZE) {
345656889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
345756889787STheodore Ts'o 				 "dioread_nolock if block size != PAGE_SIZE");
345856889787STheodore Ts'o 			goto failed_mount;
345956889787STheodore Ts'o 		}
346056889787STheodore Ts'o 	}
346156889787STheodore Ts'o 
3462ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3463482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3464ac27a0ecSDave Kleikamp 
3465617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3466617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3467617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3468617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
3469b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3470b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3471b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3472469108ffSTheodore Tso 
34732035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
34742035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
34752035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
34762035e776STheodore Ts'o 				 "using the ext4 subsystem");
34772035e776STheodore Ts'o 		else {
34782035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
34792035e776STheodore Ts'o 				 "to feature incompatibilities");
34802035e776STheodore Ts'o 			goto failed_mount;
34812035e776STheodore Ts'o 		}
34822035e776STheodore Ts'o 	}
34832035e776STheodore Ts'o 
34842035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
34852035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
34862035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
34872035e776STheodore Ts'o 				 "using the ext4 subsystem");
34882035e776STheodore Ts'o 		else {
34892035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
34902035e776STheodore Ts'o 				 "to feature incompatibilities");
34912035e776STheodore Ts'o 			goto failed_mount;
34922035e776STheodore Ts'o 		}
34932035e776STheodore Ts'o 	}
34942035e776STheodore Ts'o 
3495469108ffSTheodore Tso 	/*
3496ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3497ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3498ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3499ac27a0ecSDave Kleikamp 	 */
3500a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3501ac27a0ecSDave Kleikamp 		goto failed_mount;
3502a13fb1a4SEric Sandeen 
3503617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3504617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3505b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3506b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
3507ac27a0ecSDave Kleikamp 		goto failed_mount;
3508ac27a0ecSDave Kleikamp 	}
3509ac27a0ecSDave Kleikamp 
3510ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3511ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3512ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3513b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3514ce40733cSAneesh Kumar K.V 					blocksize);
3515ac27a0ecSDave Kleikamp 			goto failed_mount;
3516ac27a0ecSDave Kleikamp 		}
3517ac27a0ecSDave Kleikamp 
3518ac27a0ecSDave Kleikamp 		brelse(bh);
351970bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
352070bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
352170bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
3522ac27a0ecSDave Kleikamp 		if (!bh) {
3523b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3524b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3525ac27a0ecSDave Kleikamp 			goto failed_mount;
3526ac27a0ecSDave Kleikamp 		}
35272716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
3528ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3529617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3530b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3531b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3532ac27a0ecSDave Kleikamp 			goto failed_mount;
3533ac27a0ecSDave Kleikamp 		}
3534ac27a0ecSDave Kleikamp 	}
3535ac27a0ecSDave Kleikamp 
3536a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3537a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
3538f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3539f287a1a5STheodore Ts'o 						      has_huge_files);
3540f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3541ac27a0ecSDave Kleikamp 
3542617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3543617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3544617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3545ac27a0ecSDave Kleikamp 	} else {
3546ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3547ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3548617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
35491330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3550ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3551b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3552b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3553ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3554ac27a0ecSDave Kleikamp 			goto failed_mount;
3555ac27a0ecSDave Kleikamp 		}
3556ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3557ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3558ac27a0ecSDave Kleikamp 	}
35590b8e58a1SAndreas Dilger 
35600d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
35610d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
35628fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
35630d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3564d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3565b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3566b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
35670d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
35680d1ee42fSAlexandre Ratchov 			goto failed_mount;
35690d1ee42fSAlexandre Ratchov 		}
35700d1ee42fSAlexandre Ratchov 	} else
35710d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
35720b8e58a1SAndreas Dilger 
3573ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3574ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3575b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
3576617ba13bSMingming Cao 		goto cantfind_ext4;
35770b8e58a1SAndreas Dilger 
3578617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3579ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3580617ba13bSMingming Cao 		goto cantfind_ext4;
3581ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3582ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
35830d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3584ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3585ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3586e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3587e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
35880b8e58a1SAndreas Dilger 
3589ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3590ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3591ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3592f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
3593f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
3594f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3595f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3596f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
3597f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3598f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3599f99b2589STheodore Ts'o #else
3600f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3601f99b2589STheodore Ts'o #endif
3602f99b2589STheodore Ts'o 	}
3603ac27a0ecSDave Kleikamp 
3604281b5995STheodore Ts'o 	/* Handle clustersize */
3605281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3606281b5995STheodore Ts'o 	has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3607281b5995STheodore Ts'o 				EXT4_FEATURE_RO_COMPAT_BIGALLOC);
3608281b5995STheodore Ts'o 	if (has_bigalloc) {
3609281b5995STheodore Ts'o 		if (clustersize < blocksize) {
3610281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3611281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
3612281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
3613281b5995STheodore Ts'o 			goto failed_mount;
3614281b5995STheodore Ts'o 		}
3615281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3616281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
3617281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
3618281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
3619281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
3620281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3621281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
3622281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3623281b5995STheodore Ts'o 			goto failed_mount;
3624281b5995STheodore Ts'o 		}
3625281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
3626281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3627281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3628281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
3629281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
3630281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3631281b5995STheodore Ts'o 			goto failed_mount;
3632281b5995STheodore Ts'o 		}
3633281b5995STheodore Ts'o 	} else {
3634281b5995STheodore Ts'o 		if (clustersize != blocksize) {
3635281b5995STheodore Ts'o 			ext4_warning(sb, "fragment/cluster size (%d) != "
3636281b5995STheodore Ts'o 				     "block size (%d)", clustersize,
3637281b5995STheodore Ts'o 				     blocksize);
3638281b5995STheodore Ts'o 			clustersize = blocksize;
3639281b5995STheodore Ts'o 		}
3640ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
3641b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3642b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
3643ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
3644ac27a0ecSDave Kleikamp 			goto failed_mount;
3645ac27a0ecSDave Kleikamp 		}
3646281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3647281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
3648281b5995STheodore Ts'o 	}
3649281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
3650281b5995STheodore Ts'o 
3651ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
3652b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3653b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
3654ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
3655ac27a0ecSDave Kleikamp 		goto failed_mount;
3656ac27a0ecSDave Kleikamp 	}
3657ac27a0ecSDave Kleikamp 
3658bf43d84bSEric Sandeen 	/*
3659bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3660bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3661bf43d84bSEric Sandeen 	 */
36625a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
366330ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
36645a9ae68aSDarrick J. Wong 	if (err) {
3665b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3666bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3667ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
366890c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
36695a9ae68aSDarrick J. Wong 		ret = err;
3670ac27a0ecSDave Kleikamp 		goto failed_mount;
3671ac27a0ecSDave Kleikamp 	}
3672ac27a0ecSDave Kleikamp 
3673617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3674617ba13bSMingming Cao 		goto cantfind_ext4;
3675e7c95593SEric Sandeen 
36760f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
36770f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
36780f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3679b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3680b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
36810f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
36820f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
36830f2ddca6SFrom: Thiemo Nagel 	}
36840f2ddca6SFrom: Thiemo Nagel 
36854ec11028STheodore Ts'o 	/*
36864ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
36874ec11028STheodore Ts'o 	 * of the filesystem.
36884ec11028STheodore Ts'o 	 */
36894ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3690b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
3691b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3692e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
36934ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3694e7c95593SEric Sandeen 		goto failed_mount;
3695e7c95593SEric Sandeen 	}
3696bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3697bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3698bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3699bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
37004ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3701b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
37024ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3703b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
37044ec11028STheodore Ts'o 		       ext4_blocks_count(es),
37054ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
37064ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
37074ec11028STheodore Ts'o 		goto failed_mount;
37084ec11028STheodore Ts'o 	}
3709bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3710fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3711fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3712617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3713617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3714f18a5f21STheodore Ts'o 	sbi->s_group_desc = ext4_kvmalloc(db_count *
3715f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
3716ac27a0ecSDave Kleikamp 					  GFP_KERNEL);
3717ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3718b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
37192cde417dSTheodore Ts'o 		ret = -ENOMEM;
3720ac27a0ecSDave Kleikamp 		goto failed_mount;
3721ac27a0ecSDave Kleikamp 	}
3722ac27a0ecSDave Kleikamp 
37239f6200bbSTheodore Ts'o 	if (ext4_proc_root)
37249f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
3725240799cdSTheodore Ts'o 
372666acdcf4STheodore Ts'o 	if (sbi->s_proc)
372766acdcf4STheodore Ts'o 		proc_create_data("options", S_IRUGO, sbi->s_proc,
372866acdcf4STheodore Ts'o 				 &ext4_seq_options_fops, sb);
372966acdcf4STheodore Ts'o 
3730705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3731ac27a0ecSDave Kleikamp 
3732ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
373370bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3734ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
3735ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3736b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3737b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3738ac27a0ecSDave Kleikamp 			db_count = i;
3739ac27a0ecSDave Kleikamp 			goto failed_mount2;
3740ac27a0ecSDave Kleikamp 		}
3741ac27a0ecSDave Kleikamp 	}
3742bfff6873SLukas Czerner 	if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
3743b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
3744ac27a0ecSDave Kleikamp 		goto failed_mount2;
3745ac27a0ecSDave Kleikamp 	}
3746772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
3747772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
3748b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3749b31e1552SEric Sandeen 			       "unable to initialize "
3750b31e1552SEric Sandeen 			       "flex_bg meta info!");
3751772cb7c8SJose R. Santos 			goto failed_mount2;
3752772cb7c8SJose R. Santos 		}
3753772cb7c8SJose R. Santos 
3754ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
3755ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3756ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3757ac27a0ecSDave Kleikamp 
375804496411STao Ma 	init_timer(&sbi->s_err_report);
375904496411STao Ma 	sbi->s_err_report.function = print_daily_error_info;
376004496411STao Ma 	sbi->s_err_report.data = (unsigned long) sb;
376104496411STao Ma 
376257042651STheodore Ts'o 	err = percpu_counter_init(&sbi->s_freeclusters_counter,
37635dee5437STheodore Ts'o 			ext4_count_free_clusters(sb));
3764ce7e010aSTheodore Ts'o 	if (!err) {
3765ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
3766ce7e010aSTheodore Ts'o 				ext4_count_free_inodes(sb));
3767ce7e010aSTheodore Ts'o 	}
3768ce7e010aSTheodore Ts'o 	if (!err) {
3769ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
3770ce7e010aSTheodore Ts'o 				ext4_count_dirs(sb));
3771ce7e010aSTheodore Ts'o 	}
3772ce7e010aSTheodore Ts'o 	if (!err) {
377357042651STheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0);
3774ce7e010aSTheodore Ts'o 	}
3775ce7e010aSTheodore Ts'o 	if (err) {
3776ce7e010aSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
37772cde417dSTheodore Ts'o 		ret = err;
3778ce7e010aSTheodore Ts'o 		goto failed_mount3;
3779ce7e010aSTheodore Ts'o 	}
3780ce7e010aSTheodore Ts'o 
3781c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
378255138e0bSTheodore Ts'o 	sbi->s_max_writeback_mb_bump = 128;
378367a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
3784c9de560dSAlex Tomas 
3785ac27a0ecSDave Kleikamp 	/*
3786ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
3787ac27a0ecSDave Kleikamp 	 */
37889ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
37899ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
3790617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
37919ca92389STheodore Ts'o 	else
37929ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
3793617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3794617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3795ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3796617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
3797617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
37987c319d32SAditya Kali 
37997c319d32SAditya Kali 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) {
38007c319d32SAditya Kali 		/* Use qctl operations for hidden quota files. */
38017c319d32SAditya Kali 		sb->s_qcop = &ext4_qctl_sysfile_operations;
38027c319d32SAditya Kali 	}
3803ac27a0ecSDave Kleikamp #endif
3804f2fa2ffcSAneesh Kumar K.V 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3805f2fa2ffcSAneesh Kumar K.V 
3806ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
38073b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
38088f82f840SYongqiang Yang 	sbi->s_resize_flags = 0;
3809ac27a0ecSDave Kleikamp 
3810ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3811ac27a0ecSDave Kleikamp 
3812ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3813617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
3814617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
3815ac27a0ecSDave Kleikamp 
3816c5e06d10SJohann Lombardi 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
3817c5e06d10SJohann Lombardi 	    !(sb->s_flags & MS_RDONLY))
3818c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
3819c5e06d10SJohann Lombardi 			goto failed_mount3;
3820c5e06d10SJohann Lombardi 
3821ac27a0ecSDave Kleikamp 	/*
3822ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3823ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3824ac27a0ecSDave Kleikamp 	 */
3825ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
3826617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3827617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
3828ac27a0ecSDave Kleikamp 			goto failed_mount3;
38290390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
38300390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3831b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3832b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3833744692dcSJiaying Zhang 		goto failed_mount_wq;
3834ac27a0ecSDave Kleikamp 	} else {
3835fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
38360390131bSFrank Mayhar 		sbi->s_journal = NULL;
38370390131bSFrank Mayhar 		needs_recovery = 0;
38380390131bSFrank Mayhar 		goto no_journal;
3839ac27a0ecSDave Kleikamp 	}
3840ac27a0ecSDave Kleikamp 
3841f32aaf2dSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT) &&
3842eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3843eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3844b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3845744692dcSJiaying Zhang 		goto failed_mount_wq;
3846eb40a09cSJose R. Santos 	}
3847eb40a09cSJose R. Santos 
384825ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
384925ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
385025ed6e8aSDarrick J. Wong 			 "feature set");
385125ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
3852d4da6c9cSLinus Torvalds 	}
3853818d276cSGirish Shilamkar 
3854ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3855ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3856ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3857ac27a0ecSDave Kleikamp 	case 0:
3858ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
385963f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
386063f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
386163f57933SAndrew Morton 		 */
3862dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3863dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3864fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3865ac27a0ecSDave Kleikamp 		else
3866fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3867ac27a0ecSDave Kleikamp 		break;
3868ac27a0ecSDave Kleikamp 
3869617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
3870617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
3871dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
3872dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
3873b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
3874b31e1552SEric Sandeen 			       "requested data journaling mode");
3875744692dcSJiaying Zhang 			goto failed_mount_wq;
3876ac27a0ecSDave Kleikamp 		}
3877ac27a0ecSDave Kleikamp 	default:
3878ac27a0ecSDave Kleikamp 		break;
3879ac27a0ecSDave Kleikamp 	}
3880b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3881ac27a0ecSDave Kleikamp 
388218aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
388318aadd47SBobi Jam 
3884ce7e010aSTheodore Ts'o 	/*
3885ce7e010aSTheodore Ts'o 	 * The journal may have updated the bg summary counts, so we
3886ce7e010aSTheodore Ts'o 	 * need to update the global counters.
3887ce7e010aSTheodore Ts'o 	 */
388857042651STheodore Ts'o 	percpu_counter_set(&sbi->s_freeclusters_counter,
38895dee5437STheodore Ts'o 			   ext4_count_free_clusters(sb));
3890ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_freeinodes_counter,
389184061e07SDmitry Monakhov 			   ext4_count_free_inodes(sb));
3892ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_dirs_counter,
389384061e07SDmitry Monakhov 			   ext4_count_dirs(sb));
389457042651STheodore Ts'o 	percpu_counter_set(&sbi->s_dirtyclusters_counter, 0);
3895206f7ab4SChristoph Hellwig 
3896ce7e010aSTheodore Ts'o no_journal:
3897fd89d5f2STejun Heo 	/*
3898952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
3899952fc18eSTheodore Ts'o 	 * superblock if present.
3900952fc18eSTheodore Ts'o 	 */
3901952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
3902952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
3903952fc18eSTheodore Ts'o 	else {
3904952fc18eSTheodore Ts'o 		ret = ext4_calculate_overhead(sb);
3905952fc18eSTheodore Ts'o 		if (ret)
3906952fc18eSTheodore Ts'o 			goto failed_mount_wq;
3907952fc18eSTheodore Ts'o 	}
3908952fc18eSTheodore Ts'o 
3909952fc18eSTheodore Ts'o 	/*
3910fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
3911fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
3912fd89d5f2STejun Heo 	 */
3913fd89d5f2STejun Heo 	EXT4_SB(sb)->dio_unwritten_wq =
3914ae005cbeSLinus Torvalds 		alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
39154c0425ffSMingming Cao 	if (!EXT4_SB(sb)->dio_unwritten_wq) {
39164c0425ffSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
39174c0425ffSMingming Cao 		goto failed_mount_wq;
39184c0425ffSMingming Cao 	}
39194c0425ffSMingming Cao 
3920ac27a0ecSDave Kleikamp 	/*
3921dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
3922ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
3923ac27a0ecSDave Kleikamp 	 */
3924ac27a0ecSDave Kleikamp 
39251d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
39261d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
3927b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
39281d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
392932a9bb57SManish Katiyar 		root = NULL;
3930ac27a0ecSDave Kleikamp 		goto failed_mount4;
3931ac27a0ecSDave Kleikamp 	}
3932ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
3933b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
393494bf608aSAl Viro 		iput(root);
3935ac27a0ecSDave Kleikamp 		goto failed_mount4;
3936ac27a0ecSDave Kleikamp 	}
393748fde701SAl Viro 	sb->s_root = d_make_root(root);
39381d1fe1eeSDavid Howells 	if (!sb->s_root) {
3939b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
39401d1fe1eeSDavid Howells 		ret = -ENOMEM;
39411d1fe1eeSDavid Howells 		goto failed_mount4;
39421d1fe1eeSDavid Howells 	}
3943ac27a0ecSDave Kleikamp 
39447e84b621SEric Sandeen 	if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
39457e84b621SEric Sandeen 		sb->s_flags |= MS_RDONLY;
3946ef7f3835SKalpak Shah 
3947ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
3948ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3949ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3950ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
3951ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3952ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
3953ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3954ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
3955ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3956ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
3957ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3958ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
3959ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3960ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
3961ef7f3835SKalpak Shah 		}
3962ef7f3835SKalpak Shah 	}
3963ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
3964ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3965ef7f3835SKalpak Shah 							sbi->s_inode_size) {
3966ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3967ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
3968b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
3969b31e1552SEric Sandeen 			 "available");
3970ef7f3835SKalpak Shah 	}
3971ef7f3835SKalpak Shah 
39726fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
39736fd058f7STheodore Ts'o 	if (err) {
3974b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
3975fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
397694bf608aSAl Viro 		goto failed_mount4a;
39776fd058f7STheodore Ts'o 	}
39786fd058f7STheodore Ts'o 
3979c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
39809d99012fSAkira Fujita 	err = ext4_mb_init(sb);
3981c2774d84SAneesh Kumar K.V 	if (err) {
3982421f91d2SUwe Kleine-König 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
3983c2774d84SAneesh Kumar K.V 			 err);
3984dcf2d804STao Ma 		goto failed_mount5;
3985c2774d84SAneesh Kumar K.V 	}
3986c2774d84SAneesh Kumar K.V 
3987bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
3988bfff6873SLukas Czerner 	if (err)
3989dcf2d804STao Ma 		goto failed_mount6;
3990bfff6873SLukas Czerner 
39913197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
39923197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
39933197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
39943197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
3995dcf2d804STao Ma 	if (err)
3996dcf2d804STao Ma 		goto failed_mount7;
39973197ebdbSTheodore Ts'o 
3998617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3999617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4000617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
40010390131bSFrank Mayhar 	if (needs_recovery) {
4002b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4003617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
40040390131bSFrank Mayhar 	}
40050390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
40060390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
40070390131bSFrank Mayhar 			descr = " journalled data mode";
40080390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
40090390131bSFrank Mayhar 			descr = " ordered data mode";
40100390131bSFrank Mayhar 		else
40110390131bSFrank Mayhar 			descr = " writeback data mode";
40120390131bSFrank Mayhar 	} else
40130390131bSFrank Mayhar 		descr = "out journal";
40140390131bSFrank Mayhar 
40157c319d32SAditya Kali #ifdef CONFIG_QUOTA
40167c319d32SAditya Kali 	/* Enable quota usage during mount. */
40177c319d32SAditya Kali 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
40187c319d32SAditya Kali 	    !(sb->s_flags & MS_RDONLY)) {
40197c319d32SAditya Kali 		ret = ext4_enable_quotas(sb);
40207c319d32SAditya Kali 		if (ret)
40217c319d32SAditya Kali 			goto failed_mount7;
40227c319d32SAditya Kali 	}
40237c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
40247c319d32SAditya Kali 
4025d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
40268b67f04aSTheodore Ts'o 		 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
40278b67f04aSTheodore Ts'o 		 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4028ac27a0ecSDave Kleikamp 
402966e61a9eSTheodore Ts'o 	if (es->s_error_count)
403066e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4031ac27a0ecSDave Kleikamp 
4032d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4033ac27a0ecSDave Kleikamp 	return 0;
4034ac27a0ecSDave Kleikamp 
4035617ba13bSMingming Cao cantfind_ext4:
4036ac27a0ecSDave Kleikamp 	if (!silent)
4037b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4038ac27a0ecSDave Kleikamp 	goto failed_mount;
4039ac27a0ecSDave Kleikamp 
4040dcf2d804STao Ma failed_mount7:
4041dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4042dcf2d804STao Ma failed_mount6:
4043dcf2d804STao Ma 	ext4_mb_release(sb);
404494bf608aSAl Viro failed_mount5:
404594bf608aSAl Viro 	ext4_ext_release(sb);
4046dcf2d804STao Ma 	ext4_release_system_zone(sb);
404794bf608aSAl Viro failed_mount4a:
404894bf608aSAl Viro 	dput(sb->s_root);
404932a9bb57SManish Katiyar 	sb->s_root = NULL;
405094bf608aSAl Viro failed_mount4:
4051b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
40524c0425ffSMingming Cao 	destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
40534c0425ffSMingming Cao failed_mount_wq:
40540390131bSFrank Mayhar 	if (sbi->s_journal) {
4055dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
405647b4a50bSJan Kara 		sbi->s_journal = NULL;
40570390131bSFrank Mayhar 	}
4058ac27a0ecSDave Kleikamp failed_mount3:
405904496411STao Ma 	del_timer(&sbi->s_err_report);
40609933fc0aSTheodore Ts'o 	if (sbi->s_flex_groups)
40619933fc0aSTheodore Ts'o 		ext4_kvfree(sbi->s_flex_groups);
406257042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4063ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4064ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
406557042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4066c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4067c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4068ac27a0ecSDave Kleikamp failed_mount2:
4069ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4070ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4071f18a5f21STheodore Ts'o 	ext4_kvfree(sbi->s_group_desc);
4072ac27a0ecSDave Kleikamp failed_mount:
40730441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
40740441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4075240799cdSTheodore Ts'o 	if (sbi->s_proc) {
407666acdcf4STheodore Ts'o 		remove_proc_entry("options", sbi->s_proc);
40779f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
4078240799cdSTheodore Ts'o 	}
4079ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4080ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4081ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4082ac27a0ecSDave Kleikamp #endif
4083617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4084ac27a0ecSDave Kleikamp 	brelse(bh);
4085ac27a0ecSDave Kleikamp out_fail:
4086ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4087f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
4088ac27a0ecSDave Kleikamp 	kfree(sbi);
4089dcc7dae3SCyrill Gorcunov out_free_orig:
4090d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
40911d1fe1eeSDavid Howells 	return ret;
4092ac27a0ecSDave Kleikamp }
4093ac27a0ecSDave Kleikamp 
4094ac27a0ecSDave Kleikamp /*
4095ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4096ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4097ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4098ac27a0ecSDave Kleikamp  */
4099617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4100ac27a0ecSDave Kleikamp {
4101617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4102ac27a0ecSDave Kleikamp 
4103ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
410430773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
410530773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4106ac27a0ecSDave Kleikamp 
4107a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4108ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4109dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4110ac27a0ecSDave Kleikamp 	else
4111dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
41125bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
41135bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
41145bf5683aSHidehiro Kawai 	else
41155bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4116a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4117ac27a0ecSDave Kleikamp }
4118ac27a0ecSDave Kleikamp 
4119617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
4120ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
4121ac27a0ecSDave Kleikamp {
4122ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4123ac27a0ecSDave Kleikamp 	journal_t *journal;
4124ac27a0ecSDave Kleikamp 
41250390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
41260390131bSFrank Mayhar 
4127ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
4128ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
4129ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
4130ac27a0ecSDave Kleikamp 
41311d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
41321d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4133b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4134ac27a0ecSDave Kleikamp 		return NULL;
4135ac27a0ecSDave Kleikamp 	}
4136ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4137ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4138ac27a0ecSDave Kleikamp 		iput(journal_inode);
4139b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4140ac27a0ecSDave Kleikamp 		return NULL;
4141ac27a0ecSDave Kleikamp 	}
4142ac27a0ecSDave Kleikamp 
4143e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4144ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
41451d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4146b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4147ac27a0ecSDave Kleikamp 		iput(journal_inode);
4148ac27a0ecSDave Kleikamp 		return NULL;
4149ac27a0ecSDave Kleikamp 	}
4150ac27a0ecSDave Kleikamp 
4151dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4152ac27a0ecSDave Kleikamp 	if (!journal) {
4153b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4154ac27a0ecSDave Kleikamp 		iput(journal_inode);
4155ac27a0ecSDave Kleikamp 		return NULL;
4156ac27a0ecSDave Kleikamp 	}
4157ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4158617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4159ac27a0ecSDave Kleikamp 	return journal;
4160ac27a0ecSDave Kleikamp }
4161ac27a0ecSDave Kleikamp 
4162617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4163ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4164ac27a0ecSDave Kleikamp {
4165ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4166ac27a0ecSDave Kleikamp 	journal_t *journal;
4167617ba13bSMingming Cao 	ext4_fsblk_t start;
4168617ba13bSMingming Cao 	ext4_fsblk_t len;
4169ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4170617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4171ac27a0ecSDave Kleikamp 	unsigned long offset;
4172617ba13bSMingming Cao 	struct ext4_super_block *es;
4173ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4174ac27a0ecSDave Kleikamp 
41750390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
41760390131bSFrank Mayhar 
4177b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4178ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4179ac27a0ecSDave Kleikamp 		return NULL;
4180ac27a0ecSDave Kleikamp 
4181ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4182e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4183ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4184b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4185b31e1552SEric Sandeen 			"blocksize too small for journal device");
4186ac27a0ecSDave Kleikamp 		goto out_bdev;
4187ac27a0ecSDave Kleikamp 	}
4188ac27a0ecSDave Kleikamp 
4189617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4190617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4191ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4192ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4193b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4194b31e1552SEric Sandeen 		       "external journal");
4195ac27a0ecSDave Kleikamp 		goto out_bdev;
4196ac27a0ecSDave Kleikamp 	}
4197ac27a0ecSDave Kleikamp 
41982716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4199617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4200ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4201617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4202b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4203b31e1552SEric Sandeen 					"bad superblock");
4204ac27a0ecSDave Kleikamp 		brelse(bh);
4205ac27a0ecSDave Kleikamp 		goto out_bdev;
4206ac27a0ecSDave Kleikamp 	}
4207ac27a0ecSDave Kleikamp 
4208617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4209b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4210ac27a0ecSDave Kleikamp 		brelse(bh);
4211ac27a0ecSDave Kleikamp 		goto out_bdev;
4212ac27a0ecSDave Kleikamp 	}
4213ac27a0ecSDave Kleikamp 
4214bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4215ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4216ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4217ac27a0ecSDave Kleikamp 
4218dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4219ac27a0ecSDave Kleikamp 					start, len, blocksize);
4220ac27a0ecSDave Kleikamp 	if (!journal) {
4221b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4222ac27a0ecSDave Kleikamp 		goto out_bdev;
4223ac27a0ecSDave Kleikamp 	}
4224ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4225ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
4226ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4227ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4228b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4229ac27a0ecSDave Kleikamp 		goto out_journal;
4230ac27a0ecSDave Kleikamp 	}
4231ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4232b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4233b31e1552SEric Sandeen 					"user (unsupported) - %d",
4234ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4235ac27a0ecSDave Kleikamp 		goto out_journal;
4236ac27a0ecSDave Kleikamp 	}
4237617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4238617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4239ac27a0ecSDave Kleikamp 	return journal;
42400b8e58a1SAndreas Dilger 
4241ac27a0ecSDave Kleikamp out_journal:
4242dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4243ac27a0ecSDave Kleikamp out_bdev:
4244617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4245ac27a0ecSDave Kleikamp 	return NULL;
4246ac27a0ecSDave Kleikamp }
4247ac27a0ecSDave Kleikamp 
4248617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4249617ba13bSMingming Cao 			     struct ext4_super_block *es,
4250ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4251ac27a0ecSDave Kleikamp {
4252ac27a0ecSDave Kleikamp 	journal_t *journal;
4253ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4254ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4255ac27a0ecSDave Kleikamp 	int err = 0;
4256ac27a0ecSDave Kleikamp 	int really_read_only;
4257ac27a0ecSDave Kleikamp 
42580390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
42590390131bSFrank Mayhar 
4260ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4261ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4262b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4263b31e1552SEric Sandeen 			"numbers have changed");
4264ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4265ac27a0ecSDave Kleikamp 	} else
4266ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4267ac27a0ecSDave Kleikamp 
4268ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4269ac27a0ecSDave Kleikamp 
4270ac27a0ecSDave Kleikamp 	/*
4271ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4272ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4273ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4274ac27a0ecSDave Kleikamp 	 */
4275617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
4276ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
4277b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4278b31e1552SEric Sandeen 					"required on readonly filesystem");
4279ac27a0ecSDave Kleikamp 			if (really_read_only) {
4280b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4281b31e1552SEric Sandeen 					"unavailable, cannot proceed");
4282ac27a0ecSDave Kleikamp 				return -EROFS;
4283ac27a0ecSDave Kleikamp 			}
4284b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4285b31e1552SEric Sandeen 			       "be enabled during recovery");
4286ac27a0ecSDave Kleikamp 		}
4287ac27a0ecSDave Kleikamp 	}
4288ac27a0ecSDave Kleikamp 
4289ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4290b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4291b31e1552SEric Sandeen 		       "and inode journals!");
4292ac27a0ecSDave Kleikamp 		return -EINVAL;
4293ac27a0ecSDave Kleikamp 	}
4294ac27a0ecSDave Kleikamp 
4295ac27a0ecSDave Kleikamp 	if (journal_inum) {
4296617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4297ac27a0ecSDave Kleikamp 			return -EINVAL;
4298ac27a0ecSDave Kleikamp 	} else {
4299617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4300ac27a0ecSDave Kleikamp 			return -EINVAL;
4301ac27a0ecSDave Kleikamp 	}
4302ac27a0ecSDave Kleikamp 
430390576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4304b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
43054776004fSTheodore Ts'o 
4306617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
4307dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
43081c13d5c0STheodore Ts'o 	if (!err) {
43091c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
43101c13d5c0STheodore Ts'o 		if (save)
43111c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
43121c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4313dab291afSMingming Cao 		err = jbd2_journal_load(journal);
43141c13d5c0STheodore Ts'o 		if (save)
43151c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
43161c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
43171c13d5c0STheodore Ts'o 		kfree(save);
43181c13d5c0STheodore Ts'o 	}
4319ac27a0ecSDave Kleikamp 
4320ac27a0ecSDave Kleikamp 	if (err) {
4321b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4322dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4323ac27a0ecSDave Kleikamp 		return err;
4324ac27a0ecSDave Kleikamp 	}
4325ac27a0ecSDave Kleikamp 
4326617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4327617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4328ac27a0ecSDave Kleikamp 
4329c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4330ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4331ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4332ac27a0ecSDave Kleikamp 
4333ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4334e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4335ac27a0ecSDave Kleikamp 	}
4336ac27a0ecSDave Kleikamp 
4337ac27a0ecSDave Kleikamp 	return 0;
4338ac27a0ecSDave Kleikamp }
4339ac27a0ecSDave Kleikamp 
4340e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4341ac27a0ecSDave Kleikamp {
4342e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4343617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4344c4be0c1dSTakashi Sato 	int error = 0;
4345ac27a0ecSDave Kleikamp 
43467c2e7087STheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4347c4be0c1dSTakashi Sato 		return error;
4348914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
4349914258bfSTheodore Ts'o 		/*
4350914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
4351914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
4352914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
4353914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
4354914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
4355914258bfSTheodore Ts'o 		 * write and hope for the best.
4356914258bfSTheodore Ts'o 		 */
4357b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
4358b31e1552SEric Sandeen 		       "superblock detected");
4359914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
4360914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
4361914258bfSTheodore Ts'o 	}
436271290b36STheodore Ts'o 	/*
436371290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
436471290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
436571290b36STheodore Ts'o 	 * write time when we are mounting the root file system
436671290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
436771290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
436871290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
436971290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
437071290b36STheodore Ts'o 	 * to complain and force a full file system check.
437171290b36STheodore Ts'o 	 */
437271290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4373ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4374f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4375afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4376afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4377afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4378afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4379f613dfcbSTheodore Ts'o 	else
4380f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4381f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
438257042651STheodore Ts'o 	ext4_free_blocks_count_set(es,
438357042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
438457042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
43857f93cff9STheodore Ts'o 	es->s_free_inodes_count =
43867f93cff9STheodore Ts'o 		cpu_to_le32(percpu_counter_sum_positive(
43875d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
4388ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
4389a9c47317SDarrick J. Wong 	ext4_superblock_csum_set(sb, es);
4390ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4391914258bfSTheodore Ts'o 	if (sync) {
4392c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
4393c4be0c1dSTakashi Sato 		if (error)
4394c4be0c1dSTakashi Sato 			return error;
4395c4be0c1dSTakashi Sato 
4396c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4397c4be0c1dSTakashi Sato 		if (error) {
4398b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4399b31e1552SEric Sandeen 			       "superblock");
4400914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4401914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4402914258bfSTheodore Ts'o 		}
4403914258bfSTheodore Ts'o 	}
4404c4be0c1dSTakashi Sato 	return error;
4405ac27a0ecSDave Kleikamp }
4406ac27a0ecSDave Kleikamp 
4407ac27a0ecSDave Kleikamp /*
4408ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4409ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4410ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4411ac27a0ecSDave Kleikamp  */
4412617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4413617ba13bSMingming Cao 					struct ext4_super_block *es)
4414ac27a0ecSDave Kleikamp {
4415617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4416ac27a0ecSDave Kleikamp 
44170390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
44180390131bSFrank Mayhar 		BUG_ON(journal != NULL);
44190390131bSFrank Mayhar 		return;
44200390131bSFrank Mayhar 	}
4421dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
44227ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
44237ffe1ea8SHidehiro Kawai 		goto out;
44247ffe1ea8SHidehiro Kawai 
4425617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
4426ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4427617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4428e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4429ac27a0ecSDave Kleikamp 	}
44307ffe1ea8SHidehiro Kawai 
44317ffe1ea8SHidehiro Kawai out:
4432dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4433ac27a0ecSDave Kleikamp }
4434ac27a0ecSDave Kleikamp 
4435ac27a0ecSDave Kleikamp /*
4436ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4437ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4438ac27a0ecSDave Kleikamp  * main filesystem now.
4439ac27a0ecSDave Kleikamp  */
4440617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4441617ba13bSMingming Cao 				   struct ext4_super_block *es)
4442ac27a0ecSDave Kleikamp {
4443ac27a0ecSDave Kleikamp 	journal_t *journal;
4444ac27a0ecSDave Kleikamp 	int j_errno;
4445ac27a0ecSDave Kleikamp 	const char *errstr;
4446ac27a0ecSDave Kleikamp 
44470390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
44480390131bSFrank Mayhar 
4449617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4450ac27a0ecSDave Kleikamp 
4451ac27a0ecSDave Kleikamp 	/*
4452ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4453617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4454ac27a0ecSDave Kleikamp 	 */
4455ac27a0ecSDave Kleikamp 
4456dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4457ac27a0ecSDave Kleikamp 	if (j_errno) {
4458ac27a0ecSDave Kleikamp 		char nbuf[16];
4459ac27a0ecSDave Kleikamp 
4460617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
446112062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4462ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
446312062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4464ac27a0ecSDave Kleikamp 
4465617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4466617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4467e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4468ac27a0ecSDave Kleikamp 
4469dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4470d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
4471ac27a0ecSDave Kleikamp 	}
4472ac27a0ecSDave Kleikamp }
4473ac27a0ecSDave Kleikamp 
4474ac27a0ecSDave Kleikamp /*
4475ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4476ac27a0ecSDave Kleikamp  * and wait on the commit.
4477ac27a0ecSDave Kleikamp  */
4478617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4479ac27a0ecSDave Kleikamp {
4480ac27a0ecSDave Kleikamp 	journal_t *journal;
44810390131bSFrank Mayhar 	int ret = 0;
4482ac27a0ecSDave Kleikamp 
4483ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4484ac27a0ecSDave Kleikamp 		return 0;
4485ac27a0ecSDave Kleikamp 
4486617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
44878e8ad8a5SJan Kara 	if (journal)
4488617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
44890390131bSFrank Mayhar 
4490ac27a0ecSDave Kleikamp 	return ret;
4491ac27a0ecSDave Kleikamp }
4492ac27a0ecSDave Kleikamp 
4493617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4494ac27a0ecSDave Kleikamp {
449514ce0cb4STheodore Ts'o 	int ret = 0;
44969eddacf9SJan Kara 	tid_t target;
44978d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4498ac27a0ecSDave Kleikamp 
44999bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
45008d5d02e6SMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
4501a1177825SJan Kara 	/*
4502a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
4503a1177825SJan Kara 	 * no dirty dquots
4504a1177825SJan Kara 	 */
4505a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
45068d5d02e6SMingming Cao 	if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4507ac27a0ecSDave Kleikamp 		if (wait)
45088d5d02e6SMingming Cao 			jbd2_log_wait_commit(sbi->s_journal, target);
45090390131bSFrank Mayhar 	}
451014ce0cb4STheodore Ts'o 	return ret;
4511ac27a0ecSDave Kleikamp }
4512ac27a0ecSDave Kleikamp 
4513ac27a0ecSDave Kleikamp /*
4514ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4515ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4516be4f27d3SYongqiang Yang  *
4517be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
45188e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
45198e8ad8a5SJan Kara  * modifications.
4520ac27a0ecSDave Kleikamp  */
4521c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4522ac27a0ecSDave Kleikamp {
4523c4be0c1dSTakashi Sato 	int error = 0;
4524c4be0c1dSTakashi Sato 	journal_t *journal;
4525ac27a0ecSDave Kleikamp 
45269ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
45279ca92389STheodore Ts'o 		return 0;
45289ca92389STheodore Ts'o 
4529c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4530ac27a0ecSDave Kleikamp 
4531ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
4532dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
45337ffe1ea8SHidehiro Kawai 
45347ffe1ea8SHidehiro Kawai 	/*
45359ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
45369ca92389STheodore Ts'o 	 * the journal.
45377ffe1ea8SHidehiro Kawai 	 */
4538c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
45396b0310fbSEric Sandeen 	if (error < 0)
45406b0310fbSEric Sandeen 		goto out;
4541ac27a0ecSDave Kleikamp 
4542ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
4543617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4544e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
45456b0310fbSEric Sandeen out:
45468e8ad8a5SJan Kara 	/* we rely on upper layer to stop further updates */
45476b0310fbSEric Sandeen 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
45486b0310fbSEric Sandeen 	return error;
4549ac27a0ecSDave Kleikamp }
4550ac27a0ecSDave Kleikamp 
4551ac27a0ecSDave Kleikamp /*
4552ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4553ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4554ac27a0ecSDave Kleikamp  */
4555c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4556ac27a0ecSDave Kleikamp {
45579ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
45589ca92389STheodore Ts'o 		return 0;
45599ca92389STheodore Ts'o 
45609ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
4561617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4562e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4563c4be0c1dSTakashi Sato 	return 0;
4564ac27a0ecSDave Kleikamp }
4565ac27a0ecSDave Kleikamp 
4566673c6100STheodore Ts'o /*
4567673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4568673c6100STheodore Ts'o  */
4569673c6100STheodore Ts'o struct ext4_mount_options {
4570673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4571a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
457208cefc7aSEric W. Biederman 	kuid_t s_resuid;
457308cefc7aSEric W. Biederman 	kgid_t s_resgid;
4574673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4575673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4576673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4577673c6100STheodore Ts'o 	int s_jquota_fmt;
4578673c6100STheodore Ts'o 	char *s_qf_names[MAXQUOTAS];
4579673c6100STheodore Ts'o #endif
4580673c6100STheodore Ts'o };
4581673c6100STheodore Ts'o 
4582617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4583ac27a0ecSDave Kleikamp {
4584617ba13bSMingming Cao 	struct ext4_super_block *es;
4585617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4586ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4587617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4588c79d967dSChristoph Hellwig 	int enable_quota = 0;
45898a266467STheodore Ts'o 	ext4_group_t g;
4590b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4591c5e06d10SJohann Lombardi 	int err = 0;
4592ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4593ac27a0ecSDave Kleikamp 	int i;
4594ac27a0ecSDave Kleikamp #endif
4595d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4596ac27a0ecSDave Kleikamp 
4597ac27a0ecSDave Kleikamp 	/* Store the original options */
4598ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4599ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4600a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4601ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4602ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4603ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
460430773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
460530773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4606ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4607ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4608ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4609ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
4610ac27a0ecSDave Kleikamp #endif
4611b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4612b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4613ac27a0ecSDave Kleikamp 
4614ac27a0ecSDave Kleikamp 	/*
4615ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
4616ac27a0ecSDave Kleikamp 	 */
4617661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
4618ac27a0ecSDave Kleikamp 		err = -EINVAL;
4619ac27a0ecSDave Kleikamp 		goto restore_opts;
4620ac27a0ecSDave Kleikamp 	}
4621ac27a0ecSDave Kleikamp 
46224ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4623c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4624ac27a0ecSDave Kleikamp 
4625ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4626482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4627ac27a0ecSDave Kleikamp 
4628ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4629ac27a0ecSDave Kleikamp 
4630b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4631617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4632b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4633b3881f74STheodore Ts'o 	}
4634ac27a0ecSDave Kleikamp 
4635661aa520SEric Sandeen 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
46364ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4637ac27a0ecSDave Kleikamp 			err = -EROFS;
4638ac27a0ecSDave Kleikamp 			goto restore_opts;
4639ac27a0ecSDave Kleikamp 		}
4640ac27a0ecSDave Kleikamp 
4641ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
46420f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
46430f0dd62fSChristoph Hellwig 			if (err < 0)
4644c79d967dSChristoph Hellwig 				goto restore_opts;
4645c79d967dSChristoph Hellwig 
4646ac27a0ecSDave Kleikamp 			/*
4647ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4648ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4649ac27a0ecSDave Kleikamp 			 */
4650ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4651ac27a0ecSDave Kleikamp 
4652ac27a0ecSDave Kleikamp 			/*
4653ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4654ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4655ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4656ac27a0ecSDave Kleikamp 			 */
4657617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4658617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4659ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4660ac27a0ecSDave Kleikamp 
4661a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4662617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4663ac27a0ecSDave Kleikamp 		} else {
4664a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4665a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
4666ac27a0ecSDave Kleikamp 				err = -EROFS;
4667ac27a0ecSDave Kleikamp 				goto restore_opts;
4668ac27a0ecSDave Kleikamp 			}
4669ead6596bSEric Sandeen 			/*
46708a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
46710b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
46728a266467STheodore Ts'o 			 */
46738a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
46748a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
46758a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
46768a266467STheodore Ts'o 
4677feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
4678b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4679b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
46808a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
46818a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
46828a266467STheodore Ts'o 					err = -EINVAL;
46838a266467STheodore Ts'o 					goto restore_opts;
46848a266467STheodore Ts'o 				}
46858a266467STheodore Ts'o 			}
46868a266467STheodore Ts'o 
46878a266467STheodore Ts'o 			/*
4688ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4689ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4690ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4691ead6596bSEric Sandeen 			 */
4692ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4693b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4694ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4695ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4696b31e1552SEric Sandeen 				       "umount/remount instead");
4697ead6596bSEric Sandeen 				err = -EINVAL;
4698ead6596bSEric Sandeen 				goto restore_opts;
4699ead6596bSEric Sandeen 			}
4700ead6596bSEric Sandeen 
4701ac27a0ecSDave Kleikamp 			/*
4702ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4703ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4704ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4705ac27a0ecSDave Kleikamp 			 * the partition.)
4706ac27a0ecSDave Kleikamp 			 */
47070390131bSFrank Mayhar 			if (sbi->s_journal)
4708617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4709ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4710617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4711ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4712c5e06d10SJohann Lombardi 			if (EXT4_HAS_INCOMPAT_FEATURE(sb,
4713c5e06d10SJohann Lombardi 						     EXT4_FEATURE_INCOMPAT_MMP))
4714c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
4715c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
4716c5e06d10SJohann Lombardi 					err = -EROFS;
4717c5e06d10SJohann Lombardi 					goto restore_opts;
4718c5e06d10SJohann Lombardi 				}
4719c79d967dSChristoph Hellwig 			enable_quota = 1;
4720ac27a0ecSDave Kleikamp 		}
4721ac27a0ecSDave Kleikamp 	}
4722bfff6873SLukas Czerner 
4723bfff6873SLukas Czerner 	/*
4724bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4725bfff6873SLukas Czerner 	 * current settings
4726bfff6873SLukas Czerner 	 */
4727bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4728bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4729bfff6873SLukas Czerner 	else {
4730bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4731bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4732bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4733bfff6873SLukas Czerner 	}
4734bfff6873SLukas Czerner 
47356fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
47360390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
4737e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
47380390131bSFrank Mayhar 
4739ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4740ac27a0ecSDave Kleikamp 	/* Release old quota file names */
4741ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4742ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
4743ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4744ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
47457c319d32SAditya Kali 	if (enable_quota) {
47467c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
47470f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
47487c319d32SAditya Kali 		else if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
47497c319d32SAditya Kali 					EXT4_FEATURE_RO_COMPAT_QUOTA)) {
47507c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
475107724f98STheodore Ts'o 			if (err)
47527c319d32SAditya Kali 				goto restore_opts;
47537c319d32SAditya Kali 		}
47547c319d32SAditya Kali 	}
47557c319d32SAditya Kali #endif
4756d4c402d9SCurt Wohlgemuth 
4757d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4758d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4759ac27a0ecSDave Kleikamp 	return 0;
47600b8e58a1SAndreas Dilger 
4761ac27a0ecSDave Kleikamp restore_opts:
4762ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
4763ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
4764a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
4765ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
4766ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
4767ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
476830773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
476930773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
4770ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4771ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
4772ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
4773ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
4774ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4775ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
4776ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
4777ac27a0ecSDave Kleikamp 	}
4778ac27a0ecSDave Kleikamp #endif
4779d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4780ac27a0ecSDave Kleikamp 	return err;
4781ac27a0ecSDave Kleikamp }
4782ac27a0ecSDave Kleikamp 
4783617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
4784ac27a0ecSDave Kleikamp {
4785ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
4786617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4787617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
4788952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
4789960cc398SPekka Enberg 	u64 fsid;
4790d02a9391SKazuya Mio 	s64 bfree;
4791ac27a0ecSDave Kleikamp 
4792952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
4793952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
4794ac27a0ecSDave Kleikamp 
4795617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
4796ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
4797952fc18eSTheodore Ts'o 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, sbi->s_overhead);
479857042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
479957042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
4800d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
480157042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
4802bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
4803bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
4804ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
4805ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
480652d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
4807617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
4808960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
4809960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
4810960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
4811960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
48120b8e58a1SAndreas Dilger 
4813ac27a0ecSDave Kleikamp 	return 0;
4814ac27a0ecSDave Kleikamp }
4815ac27a0ecSDave Kleikamp 
48160b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
48170b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
4818ac27a0ecSDave Kleikamp  * Process 1                         Process 2
4819617ba13bSMingming Cao  * ext4_create()                     quota_sync()
4820dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
4821871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
4822dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
4823ac27a0ecSDave Kleikamp  *
4824ac27a0ecSDave Kleikamp  */
4825ac27a0ecSDave Kleikamp 
4826ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4827ac27a0ecSDave Kleikamp 
4828ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
4829ac27a0ecSDave Kleikamp {
4830ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
4831ac27a0ecSDave Kleikamp }
4832ac27a0ecSDave Kleikamp 
4833617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
4834ac27a0ecSDave Kleikamp {
4835ac27a0ecSDave Kleikamp 	int ret, err;
4836ac27a0ecSDave Kleikamp 	handle_t *handle;
4837ac27a0ecSDave Kleikamp 	struct inode *inode;
4838ac27a0ecSDave Kleikamp 
4839ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
4840617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
4841617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
4842ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4843ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4844ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
4845617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4846ac27a0ecSDave Kleikamp 	if (!ret)
4847ac27a0ecSDave Kleikamp 		ret = err;
4848ac27a0ecSDave Kleikamp 	return ret;
4849ac27a0ecSDave Kleikamp }
4850ac27a0ecSDave Kleikamp 
4851617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
4852ac27a0ecSDave Kleikamp {
4853ac27a0ecSDave Kleikamp 	int ret, err;
4854ac27a0ecSDave Kleikamp 	handle_t *handle;
4855ac27a0ecSDave Kleikamp 
4856617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4857617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
4858ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4859ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4860ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
4861617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4862ac27a0ecSDave Kleikamp 	if (!ret)
4863ac27a0ecSDave Kleikamp 		ret = err;
4864ac27a0ecSDave Kleikamp 	return ret;
4865ac27a0ecSDave Kleikamp }
4866ac27a0ecSDave Kleikamp 
4867617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
4868ac27a0ecSDave Kleikamp {
4869ac27a0ecSDave Kleikamp 	int ret, err;
4870ac27a0ecSDave Kleikamp 	handle_t *handle;
4871ac27a0ecSDave Kleikamp 
4872617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4873617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
48749c3013e9SJan Kara 	if (IS_ERR(handle)) {
48759c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
48769c3013e9SJan Kara 		dquot_release(dquot);
4877ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
48789c3013e9SJan Kara 	}
4879ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
4880617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4881ac27a0ecSDave Kleikamp 	if (!ret)
4882ac27a0ecSDave Kleikamp 		ret = err;
4883ac27a0ecSDave Kleikamp 	return ret;
4884ac27a0ecSDave Kleikamp }
4885ac27a0ecSDave Kleikamp 
4886617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
4887ac27a0ecSDave Kleikamp {
48882c8be6b2SJan Kara 	/* Are we journaling quotas? */
4889617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
4890617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
4891ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
4892617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
4893ac27a0ecSDave Kleikamp 	} else {
4894ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
4895ac27a0ecSDave Kleikamp 	}
4896ac27a0ecSDave Kleikamp }
4897ac27a0ecSDave Kleikamp 
4898617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
4899ac27a0ecSDave Kleikamp {
4900ac27a0ecSDave Kleikamp 	int ret, err;
4901ac27a0ecSDave Kleikamp 	handle_t *handle;
4902ac27a0ecSDave Kleikamp 
4903ac27a0ecSDave Kleikamp 	/* Data block + inode block */
4904617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
4905ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4906ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4907ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
4908617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4909ac27a0ecSDave Kleikamp 	if (!ret)
4910ac27a0ecSDave Kleikamp 		ret = err;
4911ac27a0ecSDave Kleikamp 	return ret;
4912ac27a0ecSDave Kleikamp }
4913ac27a0ecSDave Kleikamp 
4914ac27a0ecSDave Kleikamp /*
4915ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
4916ac27a0ecSDave Kleikamp  * the quota file and such...
4917ac27a0ecSDave Kleikamp  */
4918617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
4919ac27a0ecSDave Kleikamp {
4920287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
4921617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
4922ac27a0ecSDave Kleikamp }
4923ac27a0ecSDave Kleikamp 
4924ac27a0ecSDave Kleikamp /*
4925ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
4926ac27a0ecSDave Kleikamp  */
4927617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
4928f00c9e44SJan Kara 			 struct path *path)
4929ac27a0ecSDave Kleikamp {
4930ac27a0ecSDave Kleikamp 	int err;
4931ac27a0ecSDave Kleikamp 
4932ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
4933ac27a0ecSDave Kleikamp 		return -EINVAL;
49340623543bSJan Kara 
4935ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
4936d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
4937ac27a0ecSDave Kleikamp 		return -EXDEV;
49380623543bSJan Kara 	/* Journaling quota? */
49390623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
49402b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
4941f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
4942b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
4943b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
4944b31e1552SEric Sandeen 				"Journaled quota will not work");
49450623543bSJan Kara 	}
49460623543bSJan Kara 
49470623543bSJan Kara 	/*
49480623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
49490623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
49500623543bSJan Kara 	 */
49510390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
4952f00c9e44SJan Kara 	    ext4_should_journal_data(path->dentry->d_inode)) {
49530623543bSJan Kara 		/*
49540623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
49550623543bSJan Kara 		 * otherwise be livelocked...
49560623543bSJan Kara 		 */
49570623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
49587ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
49590623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
4960f00c9e44SJan Kara 		if (err)
49617ffe1ea8SHidehiro Kawai 			return err;
49627ffe1ea8SHidehiro Kawai 	}
49630623543bSJan Kara 
4964f00c9e44SJan Kara 	return dquot_quota_on(sb, type, format_id, path);
4965ac27a0ecSDave Kleikamp }
4966ac27a0ecSDave Kleikamp 
49677c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
49687c319d32SAditya Kali 			     unsigned int flags)
49697c319d32SAditya Kali {
49707c319d32SAditya Kali 	int err;
49717c319d32SAditya Kali 	struct inode *qf_inode;
49727c319d32SAditya Kali 	unsigned long qf_inums[MAXQUOTAS] = {
49737c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
49747c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
49757c319d32SAditya Kali 	};
49767c319d32SAditya Kali 
49777c319d32SAditya Kali 	BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA));
49787c319d32SAditya Kali 
49797c319d32SAditya Kali 	if (!qf_inums[type])
49807c319d32SAditya Kali 		return -EPERM;
49817c319d32SAditya Kali 
49827c319d32SAditya Kali 	qf_inode = ext4_iget(sb, qf_inums[type]);
49837c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
49847c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
49857c319d32SAditya Kali 		return PTR_ERR(qf_inode);
49867c319d32SAditya Kali 	}
49877c319d32SAditya Kali 
49887c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
49897c319d32SAditya Kali 	iput(qf_inode);
49907c319d32SAditya Kali 
49917c319d32SAditya Kali 	return err;
49927c319d32SAditya Kali }
49937c319d32SAditya Kali 
49947c319d32SAditya Kali /* Enable usage tracking for all quota types. */
49957c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
49967c319d32SAditya Kali {
49977c319d32SAditya Kali 	int type, err = 0;
49987c319d32SAditya Kali 	unsigned long qf_inums[MAXQUOTAS] = {
49997c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
50007c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
50017c319d32SAditya Kali 	};
50027c319d32SAditya Kali 
50037c319d32SAditya Kali 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
50047c319d32SAditya Kali 	for (type = 0; type < MAXQUOTAS; type++) {
50057c319d32SAditya Kali 		if (qf_inums[type]) {
50067c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
50077c319d32SAditya Kali 						DQUOT_USAGE_ENABLED);
50087c319d32SAditya Kali 			if (err) {
50097c319d32SAditya Kali 				ext4_warning(sb,
50107c319d32SAditya Kali 					"Failed to enable quota (type=%d) "
50117c319d32SAditya Kali 					"tracking. Please run e2fsck to fix.",
50127c319d32SAditya Kali 					type);
50137c319d32SAditya Kali 				return err;
50147c319d32SAditya Kali 			}
50157c319d32SAditya Kali 		}
50167c319d32SAditya Kali 	}
50177c319d32SAditya Kali 	return 0;
50187c319d32SAditya Kali }
50197c319d32SAditya Kali 
50207c319d32SAditya Kali /*
50217c319d32SAditya Kali  * quota_on function that is used when QUOTA feature is set.
50227c319d32SAditya Kali  */
50237c319d32SAditya Kali static int ext4_quota_on_sysfile(struct super_block *sb, int type,
50247c319d32SAditya Kali 				 int format_id)
50257c319d32SAditya Kali {
50267c319d32SAditya Kali 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
50277c319d32SAditya Kali 		return -EINVAL;
50287c319d32SAditya Kali 
50297c319d32SAditya Kali 	/*
50307c319d32SAditya Kali 	 * USAGE was enabled at mount time. Only need to enable LIMITS now.
50317c319d32SAditya Kali 	 */
50327c319d32SAditya Kali 	return ext4_quota_enable(sb, type, format_id, DQUOT_LIMITS_ENABLED);
50337c319d32SAditya Kali }
50347c319d32SAditya Kali 
5035ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5036ca0e05e4SDmitry Monakhov {
503721f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
503821f97697SJan Kara 	handle_t *handle;
503921f97697SJan Kara 
504087009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
504187009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
504287009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5043ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5044ca0e05e4SDmitry Monakhov 
50450b268590SAmir Goldstein 	if (!inode)
50460b268590SAmir Goldstein 		goto out;
50470b268590SAmir Goldstein 
504821f97697SJan Kara 	/* Update modification times of quota files when userspace can
504921f97697SJan Kara 	 * start looking at them */
505021f97697SJan Kara 	handle = ext4_journal_start(inode, 1);
505121f97697SJan Kara 	if (IS_ERR(handle))
505221f97697SJan Kara 		goto out;
505321f97697SJan Kara 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
505421f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
505521f97697SJan Kara 	ext4_journal_stop(handle);
505621f97697SJan Kara 
505721f97697SJan Kara out:
5058ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5059ca0e05e4SDmitry Monakhov }
5060ca0e05e4SDmitry Monakhov 
50617c319d32SAditya Kali /*
50627c319d32SAditya Kali  * quota_off function that is used when QUOTA feature is set.
50637c319d32SAditya Kali  */
50647c319d32SAditya Kali static int ext4_quota_off_sysfile(struct super_block *sb, int type)
50657c319d32SAditya Kali {
50667c319d32SAditya Kali 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
50677c319d32SAditya Kali 		return -EINVAL;
50687c319d32SAditya Kali 
50697c319d32SAditya Kali 	/* Disable only the limits. */
50707c319d32SAditya Kali 	return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
50717c319d32SAditya Kali }
50727c319d32SAditya Kali 
5073ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5074ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5075ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5076ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5077617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5078ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5079ac27a0ecSDave Kleikamp {
5080ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5081725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5082ac27a0ecSDave Kleikamp 	int err = 0;
5083ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5084ac27a0ecSDave Kleikamp 	int tocopy;
5085ac27a0ecSDave Kleikamp 	size_t toread;
5086ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5087ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5088ac27a0ecSDave Kleikamp 
5089ac27a0ecSDave Kleikamp 	if (off > i_size)
5090ac27a0ecSDave Kleikamp 		return 0;
5091ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5092ac27a0ecSDave Kleikamp 		len = i_size-off;
5093ac27a0ecSDave Kleikamp 	toread = len;
5094ac27a0ecSDave Kleikamp 	while (toread > 0) {
5095ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5096ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
5097617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
5098ac27a0ecSDave Kleikamp 		if (err)
5099ac27a0ecSDave Kleikamp 			return err;
5100ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5101ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5102ac27a0ecSDave Kleikamp 		else
5103ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5104ac27a0ecSDave Kleikamp 		brelse(bh);
5105ac27a0ecSDave Kleikamp 		offset = 0;
5106ac27a0ecSDave Kleikamp 		toread -= tocopy;
5107ac27a0ecSDave Kleikamp 		data += tocopy;
5108ac27a0ecSDave Kleikamp 		blk++;
5109ac27a0ecSDave Kleikamp 	}
5110ac27a0ecSDave Kleikamp 	return len;
5111ac27a0ecSDave Kleikamp }
5112ac27a0ecSDave Kleikamp 
5113ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5114ac27a0ecSDave Kleikamp  * enough credits) */
5115617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5116ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5117ac27a0ecSDave Kleikamp {
5118ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5119725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5120ac27a0ecSDave Kleikamp 	int err = 0;
5121ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5122ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5123ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5124ac27a0ecSDave Kleikamp 
51250390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5126b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5127b31e1552SEric Sandeen 			" cancelled because transaction is not started",
51289c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
51299c3013e9SJan Kara 		return -EIO;
51309c3013e9SJan Kara 	}
513167eeb568SDmitry Monakhov 	/*
513267eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
513367eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
513467eeb568SDmitry Monakhov 	 */
513567eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
513667eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
513767eeb568SDmitry Monakhov 			" cancelled because not block aligned",
513867eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
513967eeb568SDmitry Monakhov 		return -EIO;
514067eeb568SDmitry Monakhov 	}
514167eeb568SDmitry Monakhov 
5142617ba13bSMingming Cao 	bh = ext4_bread(handle, inode, blk, 1, &err);
5143ac27a0ecSDave Kleikamp 	if (!bh)
5144ac27a0ecSDave Kleikamp 		goto out;
5145617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5146ac27a0ecSDave Kleikamp 	if (err) {
5147ac27a0ecSDave Kleikamp 		brelse(bh);
5148ac27a0ecSDave Kleikamp 		goto out;
5149ac27a0ecSDave Kleikamp 	}
5150ac27a0ecSDave Kleikamp 	lock_buffer(bh);
515167eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5152ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5153ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
51540390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5155ac27a0ecSDave Kleikamp 	brelse(bh);
5156ac27a0ecSDave Kleikamp out:
51570b7f7cefSJan Kara 	if (err)
5158ac27a0ecSDave Kleikamp 		return err;
515967eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
516067eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
5161617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
5162617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
516321f97697SJan Kara 	}
516467eeb568SDmitry Monakhov 	return len;
5165ac27a0ecSDave Kleikamp }
5166ac27a0ecSDave Kleikamp 
5167ac27a0ecSDave Kleikamp #endif
5168ac27a0ecSDave Kleikamp 
5169152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5170152a0836SAl Viro 		       const char *dev_name, void *data)
5171ac27a0ecSDave Kleikamp {
5172152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
5173ac27a0ecSDave Kleikamp }
5174ac27a0ecSDave Kleikamp 
517537f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
517624b58424STheodore Ts'o static inline void register_as_ext2(void)
517724b58424STheodore Ts'o {
517824b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
517924b58424STheodore Ts'o 	if (err)
518024b58424STheodore Ts'o 		printk(KERN_WARNING
518124b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
518224b58424STheodore Ts'o }
518324b58424STheodore Ts'o 
518424b58424STheodore Ts'o static inline void unregister_as_ext2(void)
518524b58424STheodore Ts'o {
518624b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
518724b58424STheodore Ts'o }
51882035e776STheodore Ts'o 
51892035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
51902035e776STheodore Ts'o {
51912035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
51922035e776STheodore Ts'o 		return 0;
51932035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
51942035e776STheodore Ts'o 		return 1;
51952035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
51962035e776STheodore Ts'o 		return 0;
51972035e776STheodore Ts'o 	return 1;
51982035e776STheodore Ts'o }
519951b7e3c9STheodore Ts'o MODULE_ALIAS("ext2");
520024b58424STheodore Ts'o #else
520124b58424STheodore Ts'o static inline void register_as_ext2(void) { }
520224b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
52032035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
520424b58424STheodore Ts'o #endif
520524b58424STheodore Ts'o 
520637f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
520724b58424STheodore Ts'o static inline void register_as_ext3(void)
520824b58424STheodore Ts'o {
520924b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
521024b58424STheodore Ts'o 	if (err)
521124b58424STheodore Ts'o 		printk(KERN_WARNING
521224b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
521324b58424STheodore Ts'o }
521424b58424STheodore Ts'o 
521524b58424STheodore Ts'o static inline void unregister_as_ext3(void)
521624b58424STheodore Ts'o {
521724b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
521824b58424STheodore Ts'o }
52192035e776STheodore Ts'o 
52202035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
52212035e776STheodore Ts'o {
52222035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
52232035e776STheodore Ts'o 		return 0;
52242035e776STheodore Ts'o 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
52252035e776STheodore Ts'o 		return 0;
52262035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
52272035e776STheodore Ts'o 		return 1;
52282035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
52292035e776STheodore Ts'o 		return 0;
52302035e776STheodore Ts'o 	return 1;
52312035e776STheodore Ts'o }
523251b7e3c9STheodore Ts'o MODULE_ALIAS("ext3");
523324b58424STheodore Ts'o #else
523424b58424STheodore Ts'o static inline void register_as_ext3(void) { }
523524b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
52362035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
523724b58424STheodore Ts'o #endif
523824b58424STheodore Ts'o 
523903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
5240ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
524103010a33STheodore Ts'o 	.name		= "ext4",
5242152a0836SAl Viro 	.mount		= ext4_mount,
5243ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
5244ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
5245ac27a0ecSDave Kleikamp };
5246ac27a0ecSDave Kleikamp 
52478f021222SLukas Czerner static int __init ext4_init_feat_adverts(void)
5248857ac889SLukas Czerner {
5249857ac889SLukas Czerner 	struct ext4_features *ef;
5250857ac889SLukas Czerner 	int ret = -ENOMEM;
5251857ac889SLukas Czerner 
5252857ac889SLukas Czerner 	ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
5253857ac889SLukas Czerner 	if (!ef)
5254857ac889SLukas Czerner 		goto out;
5255857ac889SLukas Czerner 
5256857ac889SLukas Czerner 	ef->f_kobj.kset = ext4_kset;
5257857ac889SLukas Czerner 	init_completion(&ef->f_kobj_unregister);
5258857ac889SLukas Czerner 	ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
5259857ac889SLukas Czerner 				   "features");
5260857ac889SLukas Czerner 	if (ret) {
5261857ac889SLukas Czerner 		kfree(ef);
5262857ac889SLukas Czerner 		goto out;
5263857ac889SLukas Czerner 	}
5264857ac889SLukas Czerner 
5265857ac889SLukas Czerner 	ext4_feat = ef;
5266857ac889SLukas Czerner 	ret = 0;
5267857ac889SLukas Czerner out:
5268857ac889SLukas Czerner 	return ret;
5269857ac889SLukas Czerner }
5270857ac889SLukas Czerner 
52718f021222SLukas Czerner static void ext4_exit_feat_adverts(void)
52728f021222SLukas Czerner {
52738f021222SLukas Czerner 	kobject_put(&ext4_feat->f_kobj);
52748f021222SLukas Czerner 	wait_for_completion(&ext4_feat->f_kobj_unregister);
52758f021222SLukas Czerner 	kfree(ext4_feat);
52768f021222SLukas Czerner }
52778f021222SLukas Czerner 
5278e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
5279e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5280e9e3bcecSEric Sandeen struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
5281e9e3bcecSEric Sandeen 
52825dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
5283ac27a0ecSDave Kleikamp {
5284e9e3bcecSEric Sandeen 	int i, err;
5285c9de560dSAlex Tomas 
528607c0c5d8SAl Viro 	ext4_li_info = NULL;
528707c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
528807c0c5d8SAl Viro 
528912e9b892SDmitry Monakhov 	ext4_check_flag_values();
5290e9e3bcecSEric Sandeen 
5291e9e3bcecSEric Sandeen 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
5292e9e3bcecSEric Sandeen 		mutex_init(&ext4__aio_mutex[i]);
5293e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
5294e9e3bcecSEric Sandeen 	}
5295e9e3bcecSEric Sandeen 
52965dabfc78STheodore Ts'o 	err = ext4_init_pageio();
52976fd058f7STheodore Ts'o 	if (err)
52986fd058f7STheodore Ts'o 		return err;
52995dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
5300bd2d0210STheodore Ts'o 	if (err)
5301d44651d0SFabrice Jouhaud 		goto out6;
53023197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
53030e376b1eSTheodore Ts'o 	if (!ext4_kset) {
53040e376b1eSTheodore Ts'o 		err = -ENOMEM;
5305dd68314cSTheodore Ts'o 		goto out5;
53060e376b1eSTheodore Ts'o 	}
5307d44651d0SFabrice Jouhaud 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
5308857ac889SLukas Czerner 
5309857ac889SLukas Czerner 	err = ext4_init_feat_adverts();
5310dd68314cSTheodore Ts'o 	if (err)
5311dd68314cSTheodore Ts'o 		goto out4;
5312857ac889SLukas Czerner 
53135dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
5314ac27a0ecSDave Kleikamp 	if (err)
53156fd058f7STheodore Ts'o 		goto out3;
5316c9de560dSAlex Tomas 
53175dabfc78STheodore Ts'o 	err = ext4_init_xattr();
5318c9de560dSAlex Tomas 	if (err)
5319c9de560dSAlex Tomas 		goto out2;
5320ac27a0ecSDave Kleikamp 	err = init_inodecache();
5321ac27a0ecSDave Kleikamp 	if (err)
5322ac27a0ecSDave Kleikamp 		goto out1;
532324b58424STheodore Ts'o 	register_as_ext3();
53242035e776STheodore Ts'o 	register_as_ext2();
532503010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
5326ac27a0ecSDave Kleikamp 	if (err)
5327ac27a0ecSDave Kleikamp 		goto out;
5328bfff6873SLukas Czerner 
5329ac27a0ecSDave Kleikamp 	return 0;
5330ac27a0ecSDave Kleikamp out:
533124b58424STheodore Ts'o 	unregister_as_ext2();
533224b58424STheodore Ts'o 	unregister_as_ext3();
5333ac27a0ecSDave Kleikamp 	destroy_inodecache();
5334ac27a0ecSDave Kleikamp out1:
53355dabfc78STheodore Ts'o 	ext4_exit_xattr();
5336c9de560dSAlex Tomas out2:
53375dabfc78STheodore Ts'o 	ext4_exit_mballoc();
53386fd058f7STheodore Ts'o out3:
53398f021222SLukas Czerner 	ext4_exit_feat_adverts();
53406fd058f7STheodore Ts'o out4:
5341d44651d0SFabrice Jouhaud 	if (ext4_proc_root)
5342dd68314cSTheodore Ts'o 		remove_proc_entry("fs/ext4", NULL);
5343dd68314cSTheodore Ts'o 	kset_unregister(ext4_kset);
5344d44651d0SFabrice Jouhaud out5:
5345dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5346d44651d0SFabrice Jouhaud out6:
53475dabfc78STheodore Ts'o 	ext4_exit_pageio();
5348ac27a0ecSDave Kleikamp 	return err;
5349ac27a0ecSDave Kleikamp }
5350ac27a0ecSDave Kleikamp 
53515dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5352ac27a0ecSDave Kleikamp {
5353bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
535424b58424STheodore Ts'o 	unregister_as_ext2();
535524b58424STheodore Ts'o 	unregister_as_ext3();
535603010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5357ac27a0ecSDave Kleikamp 	destroy_inodecache();
53585dabfc78STheodore Ts'o 	ext4_exit_xattr();
53595dabfc78STheodore Ts'o 	ext4_exit_mballoc();
53608f021222SLukas Czerner 	ext4_exit_feat_adverts();
53619f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
53623197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
53635dabfc78STheodore Ts'o 	ext4_exit_system_zone();
53645dabfc78STheodore Ts'o 	ext4_exit_pageio();
5365ac27a0ecSDave Kleikamp }
5366ac27a0ecSDave Kleikamp 
5367ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
536883982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5369ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
53705dabfc78STheodore Ts'o module_init(ext4_init_fs)
53715dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5372