xref: /linux/fs/ext4/super.c (revision 363307e6e561cde78572fd22e7fd00cd3e5adb02)
1ac27a0ecSDave Kleikamp /*
2617ba13bSMingming Cao  *  linux/fs/ext4/super.c
3ac27a0ecSDave Kleikamp  *
4ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
5ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
6ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
7ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
8ac27a0ecSDave Kleikamp  *
9ac27a0ecSDave Kleikamp  *  from
10ac27a0ecSDave Kleikamp  *
11ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
12ac27a0ecSDave Kleikamp  *
13ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
14ac27a0ecSDave Kleikamp  *
15ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
16ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
17ac27a0ecSDave Kleikamp  */
18ac27a0ecSDave Kleikamp 
19ac27a0ecSDave Kleikamp #include <linux/module.h>
20ac27a0ecSDave Kleikamp #include <linux/string.h>
21ac27a0ecSDave Kleikamp #include <linux/fs.h>
22ac27a0ecSDave Kleikamp #include <linux/time.h>
23c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
24dab291afSMingming Cao #include <linux/jbd2.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
28ac27a0ecSDave Kleikamp #include <linux/parser.h>
29ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
30a5694255SChristoph Hellwig #include <linux/exportfs.h>
31ac27a0ecSDave Kleikamp #include <linux/vfs.h>
32ac27a0ecSDave Kleikamp #include <linux/random.h>
33ac27a0ecSDave Kleikamp #include <linux/mount.h>
34ac27a0ecSDave Kleikamp #include <linux/namei.h>
35ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
36ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
379f6200bbSTheodore Ts'o #include <linux/proc_fs.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
417abc52c2SDan Magenheimer #include <linux/cleancache.h>
42ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
43ac27a0ecSDave Kleikamp 
44bfff6873SLukas Czerner #include <linux/kthread.h>
45bfff6873SLukas Czerner #include <linux/freezer.h>
46bfff6873SLukas Czerner 
473dcf5451SChristoph Hellwig #include "ext4.h"
484a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
493dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
50ac27a0ecSDave Kleikamp #include "xattr.h"
51ac27a0ecSDave Kleikamp #include "acl.h"
523661d286STheodore Ts'o #include "mballoc.h"
53ac27a0ecSDave Kleikamp 
549bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
559bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
569bffad1eSTheodore Ts'o 
571f109d5aSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root;
583197ebdbSTheodore Ts'o static struct kset *ext4_kset;
590b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
600b75a840SLukas Czerner static struct mutex ext4_li_mtx;
610b75a840SLukas Czerner static struct ext4_features *ext4_feat;
629c191f70ST Makphaibulchoke static int ext4_mballoc_ready;
639f6200bbSTheodore Ts'o 
64617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
65ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
662adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
67e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
68617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
69617ba13bSMingming Cao 					struct ext4_super_block *es);
70617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
71617ba13bSMingming Cao 				   struct ext4_super_block *es);
72617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
73617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
74617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
75c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
76c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
77152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
78152a0836SAl Viro 		       const char *dev_name, void *data);
792035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
802035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
81d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
82bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
83bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
848f1f7453SEric Sandeen static void ext4_clear_request_list(void);
8527dd4385SLukas Czerner static int ext4_reserve_clusters(struct ext4_sb_info *, ext4_fsblk_t);
86ac27a0ecSDave Kleikamp 
872035e776STheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
882035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
892035e776STheodore Ts'o 	.owner		= THIS_MODULE,
902035e776STheodore Ts'o 	.name		= "ext2",
912035e776STheodore Ts'o 	.mount		= ext4_mount,
922035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
932035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
942035e776STheodore Ts'o };
957f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
96fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
972035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
982035e776STheodore Ts'o #else
992035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1002035e776STheodore Ts'o #endif
1012035e776STheodore Ts'o 
1022035e776STheodore Ts'o 
103ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
104ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
105ba69f9abSJan Kara 	.owner		= THIS_MODULE,
106ba69f9abSJan Kara 	.name		= "ext3",
107152a0836SAl Viro 	.mount		= ext4_mount,
108ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
109ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
110ba69f9abSJan Kara };
1117f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
112fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
113ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
114ba69f9abSJan Kara #else
115ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0)
116ba69f9abSJan Kara #endif
117bd81d8eeSLaurent Vivier 
118d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
119d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
120d25425f8SDarrick J. Wong {
121d25425f8SDarrick J. Wong 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
122d25425f8SDarrick J. Wong 					EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
123d25425f8SDarrick J. Wong 		return 1;
124d25425f8SDarrick J. Wong 
125d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
126d25425f8SDarrick J. Wong }
127d25425f8SDarrick J. Wong 
128a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
129a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
130a9c47317SDarrick J. Wong {
131a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
132a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
133a9c47317SDarrick J. Wong 	__u32 csum;
134a9c47317SDarrick J. Wong 
135a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
136a9c47317SDarrick J. Wong 
137a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
138a9c47317SDarrick J. Wong }
139a9c47317SDarrick J. Wong 
140c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb,
141a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
142a9c47317SDarrick J. Wong {
1439aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
144a9c47317SDarrick J. Wong 		return 1;
145a9c47317SDarrick J. Wong 
146a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
147a9c47317SDarrick J. Wong }
148a9c47317SDarrick J. Wong 
14906db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
150a9c47317SDarrick J. Wong {
15106db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
15206db49e6STheodore Ts'o 
1539aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
154a9c47317SDarrick J. Wong 		return;
155a9c47317SDarrick J. Wong 
156a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
157a9c47317SDarrick J. Wong }
158a9c47317SDarrick J. Wong 
1599933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
1609933fc0aSTheodore Ts'o {
1619933fc0aSTheodore Ts'o 	void *ret;
1629933fc0aSTheodore Ts'o 
1638be04b93SJoe Perches 	ret = kmalloc(size, flags | __GFP_NOWARN);
1649933fc0aSTheodore Ts'o 	if (!ret)
1659933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
1669933fc0aSTheodore Ts'o 	return ret;
1679933fc0aSTheodore Ts'o }
1689933fc0aSTheodore Ts'o 
1699933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
1709933fc0aSTheodore Ts'o {
1719933fc0aSTheodore Ts'o 	void *ret;
1729933fc0aSTheodore Ts'o 
1738be04b93SJoe Perches 	ret = kzalloc(size, flags | __GFP_NOWARN);
1749933fc0aSTheodore Ts'o 	if (!ret)
1759933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
1769933fc0aSTheodore Ts'o 	return ret;
1779933fc0aSTheodore Ts'o }
1789933fc0aSTheodore Ts'o 
1798fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
1808fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
181bd81d8eeSLaurent Vivier {
1823a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
1838fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1848fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
185bd81d8eeSLaurent Vivier }
186bd81d8eeSLaurent Vivier 
1878fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
1888fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
189bd81d8eeSLaurent Vivier {
1905272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
1918fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1928fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
193bd81d8eeSLaurent Vivier }
194bd81d8eeSLaurent Vivier 
1958fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
1968fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
197bd81d8eeSLaurent Vivier {
1985272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
1998fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2008fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
201bd81d8eeSLaurent Vivier }
202bd81d8eeSLaurent Vivier 
203021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
204560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
205560671a0SAneesh Kumar K.V {
206560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
207560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
208560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
209560671a0SAneesh Kumar K.V }
210560671a0SAneesh Kumar K.V 
211560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
212560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
213560671a0SAneesh Kumar K.V {
214560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
215560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
216560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
217560671a0SAneesh Kumar K.V }
218560671a0SAneesh Kumar K.V 
219560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
220560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
221560671a0SAneesh Kumar K.V {
222560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
223560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
224560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
225560671a0SAneesh Kumar K.V }
226560671a0SAneesh Kumar K.V 
227560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
228560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
229560671a0SAneesh Kumar K.V {
230560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
231560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
232560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
233560671a0SAneesh Kumar K.V }
234560671a0SAneesh Kumar K.V 
2358fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2368fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
237bd81d8eeSLaurent Vivier {
2383a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2398fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2408fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
241bd81d8eeSLaurent Vivier }
242bd81d8eeSLaurent Vivier 
2438fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2448fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
245bd81d8eeSLaurent Vivier {
2465272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2478fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2488fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
249bd81d8eeSLaurent Vivier }
250bd81d8eeSLaurent Vivier 
2518fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2528fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
253bd81d8eeSLaurent Vivier {
2545272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2558fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2568fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
257bd81d8eeSLaurent Vivier }
258bd81d8eeSLaurent Vivier 
259021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
260560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
261560671a0SAneesh Kumar K.V {
262560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
263560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
264560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
265560671a0SAneesh Kumar K.V }
266560671a0SAneesh Kumar K.V 
267560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
268560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
269560671a0SAneesh Kumar K.V {
270560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
271560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
272560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
273560671a0SAneesh Kumar K.V }
274560671a0SAneesh Kumar K.V 
275560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
276560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
277560671a0SAneesh Kumar K.V {
278560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
279560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
280560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
281560671a0SAneesh Kumar K.V }
282560671a0SAneesh Kumar K.V 
283560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
284560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
285560671a0SAneesh Kumar K.V {
286560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
287560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
288560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
289560671a0SAneesh Kumar K.V }
290560671a0SAneesh Kumar K.V 
291d3d1faf6SCurt Wohlgemuth 
2921c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
2931c13d5c0STheodore Ts'o 			    unsigned int line)
2941c13d5c0STheodore Ts'o {
2951c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
2961c13d5c0STheodore Ts'o 
2971c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2981c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2991c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
3001c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3011c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3021c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3031c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3041c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3051c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3061c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3071c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3081c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3091c13d5c0STheodore Ts'o 	}
31066e61a9eSTheodore Ts'o 	/*
31166e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
31266e61a9eSTheodore Ts'o 	 * started already
31366e61a9eSTheodore Ts'o 	 */
31466e61a9eSTheodore Ts'o 	if (!es->s_error_count)
31566e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
316ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
3171c13d5c0STheodore Ts'o }
3181c13d5c0STheodore Ts'o 
3191c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3201c13d5c0STheodore Ts'o 			    unsigned int line)
3211c13d5c0STheodore Ts'o {
3221c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3231c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3241c13d5c0STheodore Ts'o }
3251c13d5c0STheodore Ts'o 
3267c2e7087STheodore Ts'o /*
3277c2e7087STheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
3287c2e7087STheodore Ts'o  * structures, including the bdi structure, without telling anyone
3297c2e7087STheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
3307c2e7087STheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
3317c2e7087STheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
3327c2e7087STheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
3337c2e7087STheodore Ts'o  */
3347c2e7087STheodore Ts'o static int block_device_ejected(struct super_block *sb)
3357c2e7087STheodore Ts'o {
3367c2e7087STheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
3377c2e7087STheodore Ts'o 	struct backing_dev_info *bdi = bd_inode->i_mapping->backing_dev_info;
3387c2e7087STheodore Ts'o 
3397c2e7087STheodore Ts'o 	return bdi->dev == NULL;
3407c2e7087STheodore Ts'o }
3417c2e7087STheodore Ts'o 
34218aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
34318aadd47SBobi Jam {
34418aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
34518aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
34618aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
3475d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
34818aadd47SBobi Jam 
3495d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
35018aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
3515d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
3525d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
3535d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
35418aadd47SBobi Jam 		list_del_init(&jce->jce_list);
35518aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
35618aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
35718aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
35818aadd47SBobi Jam 	}
35918aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
36018aadd47SBobi Jam }
3611c13d5c0STheodore Ts'o 
362ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
363ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
364ac27a0ecSDave Kleikamp  *
365ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
366617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
367ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
368ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
369ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
370ac27a0ecSDave Kleikamp  * write out the superblock safely.
371ac27a0ecSDave Kleikamp  *
372dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
373d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
374ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
375ac27a0ecSDave Kleikamp  */
376ac27a0ecSDave Kleikamp 
377617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
378ac27a0ecSDave Kleikamp {
379ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
380ac27a0ecSDave Kleikamp 		return;
381ac27a0ecSDave Kleikamp 
382ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
383617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
384ac27a0ecSDave Kleikamp 
3854ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
386ac27a0ecSDave Kleikamp 		if (journal)
387dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
388ac27a0ecSDave Kleikamp 	}
389ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
390b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
3914418e141SDmitry Monakhov 		/*
3924418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
3934418e141SDmitry Monakhov 		 * before ->s_flags update
3944418e141SDmitry Monakhov 		 */
3954418e141SDmitry Monakhov 		smp_wmb();
396ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
397ac27a0ecSDave Kleikamp 	}
398ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
399617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
400ac27a0ecSDave Kleikamp 			sb->s_id);
401ac27a0ecSDave Kleikamp }
402ac27a0ecSDave Kleikamp 
403efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
404efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
405efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
406efbed4dcSTheodore Ts'o 
40712062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
408c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
409ac27a0ecSDave Kleikamp {
4100ff2ea7dSJoe Perches 	struct va_format vaf;
411ac27a0ecSDave Kleikamp 	va_list args;
412ac27a0ecSDave Kleikamp 
413efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
414ac27a0ecSDave Kleikamp 		va_start(args, fmt);
4150ff2ea7dSJoe Perches 		vaf.fmt = fmt;
4160ff2ea7dSJoe Perches 		vaf.va = &args;
417efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
418efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4190ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
420ac27a0ecSDave Kleikamp 		va_end(args);
421efbed4dcSTheodore Ts'o 	}
422f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
423617ba13bSMingming Cao 	ext4_handle_error(sb);
424ac27a0ecSDave Kleikamp }
425ac27a0ecSDave Kleikamp 
426e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
427c398eda0STheodore Ts'o 			unsigned int line, ext4_fsblk_t block,
428273df556SFrank Mayhar 			const char *fmt, ...)
429273df556SFrank Mayhar {
430273df556SFrank Mayhar 	va_list args;
431f7c21177STheodore Ts'o 	struct va_format vaf;
4321c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
433273df556SFrank Mayhar 
4341c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4351c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
436efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
437273df556SFrank Mayhar 		va_start(args, fmt);
438f7c21177STheodore Ts'o 		vaf.fmt = fmt;
439f7c21177STheodore Ts'o 		vaf.va = &args;
440c398eda0STheodore Ts'o 		if (block)
441d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
442d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
443d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
444d9ee81daSJoe Perches 			       block, current->comm, &vaf);
445d9ee81daSJoe Perches 		else
446d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
447d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
448d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
449d9ee81daSJoe Perches 			       current->comm, &vaf);
450273df556SFrank Mayhar 		va_end(args);
451efbed4dcSTheodore Ts'o 	}
452efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
453273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
454273df556SFrank Mayhar }
455273df556SFrank Mayhar 
456e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
457f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
458f7c21177STheodore Ts'o 		       const char *fmt, ...)
459273df556SFrank Mayhar {
460273df556SFrank Mayhar 	va_list args;
461f7c21177STheodore Ts'o 	struct va_format vaf;
4621c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
463496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
464273df556SFrank Mayhar 	char pathname[80], *path;
465273df556SFrank Mayhar 
4661c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
4671c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
468efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
469273df556SFrank Mayhar 		path = d_path(&(file->f_path), pathname, sizeof(pathname));
470f9a62d09SDan Carpenter 		if (IS_ERR(path))
471273df556SFrank Mayhar 			path = "(unknown)";
472f7c21177STheodore Ts'o 		va_start(args, fmt);
473f7c21177STheodore Ts'o 		vaf.fmt = fmt;
474f7c21177STheodore Ts'o 		vaf.va = &args;
475d9ee81daSJoe Perches 		if (block)
476d9ee81daSJoe Perches 			printk(KERN_CRIT
477d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
478d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
479d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
480d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
481d9ee81daSJoe Perches 		else
482d9ee81daSJoe Perches 			printk(KERN_CRIT
483d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
484d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
485d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
486d9ee81daSJoe Perches 			       current->comm, path, &vaf);
487273df556SFrank Mayhar 		va_end(args);
488efbed4dcSTheodore Ts'o 	}
489efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
490273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
491273df556SFrank Mayhar }
492273df556SFrank Mayhar 
493722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
494ac27a0ecSDave Kleikamp 			      char nbuf[16])
495ac27a0ecSDave Kleikamp {
496ac27a0ecSDave Kleikamp 	char *errstr = NULL;
497ac27a0ecSDave Kleikamp 
498ac27a0ecSDave Kleikamp 	switch (errno) {
499ac27a0ecSDave Kleikamp 	case -EIO:
500ac27a0ecSDave Kleikamp 		errstr = "IO failure";
501ac27a0ecSDave Kleikamp 		break;
502ac27a0ecSDave Kleikamp 	case -ENOMEM:
503ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
504ac27a0ecSDave Kleikamp 		break;
505ac27a0ecSDave Kleikamp 	case -EROFS:
50678f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
50778f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
508ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
509ac27a0ecSDave Kleikamp 		else
510ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
511ac27a0ecSDave Kleikamp 		break;
512ac27a0ecSDave Kleikamp 	default:
513ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
514ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
515ac27a0ecSDave Kleikamp 		 * NULL. */
516ac27a0ecSDave Kleikamp 		if (nbuf) {
517ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
518ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
519ac27a0ecSDave Kleikamp 				errstr = nbuf;
520ac27a0ecSDave Kleikamp 		}
521ac27a0ecSDave Kleikamp 		break;
522ac27a0ecSDave Kleikamp 	}
523ac27a0ecSDave Kleikamp 
524ac27a0ecSDave Kleikamp 	return errstr;
525ac27a0ecSDave Kleikamp }
526ac27a0ecSDave Kleikamp 
527617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
528ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
529ac27a0ecSDave Kleikamp 
530c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
531c398eda0STheodore Ts'o 		      unsigned int line, int errno)
532ac27a0ecSDave Kleikamp {
533ac27a0ecSDave Kleikamp 	char nbuf[16];
534ac27a0ecSDave Kleikamp 	const char *errstr;
535ac27a0ecSDave Kleikamp 
536ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
537ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
538ac27a0ecSDave Kleikamp 	 * an error. */
539ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
540ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
541ac27a0ecSDave Kleikamp 		return;
542ac27a0ecSDave Kleikamp 
543efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
544617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
545c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
546c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
547efbed4dcSTheodore Ts'o 	}
548ac27a0ecSDave Kleikamp 
549efbed4dcSTheodore Ts'o 	save_error_info(sb, function, line);
550617ba13bSMingming Cao 	ext4_handle_error(sb);
551ac27a0ecSDave Kleikamp }
552ac27a0ecSDave Kleikamp 
553ac27a0ecSDave Kleikamp /*
554617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
555ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
556ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
557ac27a0ecSDave Kleikamp  *
558ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
559ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
560ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
561ac27a0ecSDave Kleikamp  */
562ac27a0ecSDave Kleikamp 
563c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
564c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
565ac27a0ecSDave Kleikamp {
566ac27a0ecSDave Kleikamp 	va_list args;
567ac27a0ecSDave Kleikamp 
5681c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
569ac27a0ecSDave Kleikamp 	va_start(args, fmt);
570c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
571c398eda0STheodore Ts'o 	       function, line);
572ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
573ac27a0ecSDave Kleikamp 	printk("\n");
574ac27a0ecSDave Kleikamp 	va_end(args);
575ac27a0ecSDave Kleikamp 
5761c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
577b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
5784ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
5794418e141SDmitry Monakhov 		/*
5804418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
5814418e141SDmitry Monakhov 		 * before ->s_flags update
5824418e141SDmitry Monakhov 		 */
5834418e141SDmitry Monakhov 		smp_wmb();
5844418e141SDmitry Monakhov 		sb->s_flags |= MS_RDONLY;
585ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
586dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
5871c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
5881c13d5c0STheodore Ts'o 	}
5891c13d5c0STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC))
5901c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
591ac27a0ecSDave Kleikamp }
592ac27a0ecSDave Kleikamp 
593e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
594e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
595b31e1552SEric Sandeen {
5960ff2ea7dSJoe Perches 	struct va_format vaf;
597b31e1552SEric Sandeen 	va_list args;
598b31e1552SEric Sandeen 
599efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
600efbed4dcSTheodore Ts'o 		return;
601efbed4dcSTheodore Ts'o 
602b31e1552SEric Sandeen 	va_start(args, fmt);
6030ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6040ff2ea7dSJoe Perches 	vaf.va = &args;
6050ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
606b31e1552SEric Sandeen 	va_end(args);
607b31e1552SEric Sandeen }
608b31e1552SEric Sandeen 
60912062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
610c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
611ac27a0ecSDave Kleikamp {
6120ff2ea7dSJoe Perches 	struct va_format vaf;
613ac27a0ecSDave Kleikamp 	va_list args;
614ac27a0ecSDave Kleikamp 
615efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
616efbed4dcSTheodore Ts'o 			  "EXT4-fs warning"))
617efbed4dcSTheodore Ts'o 		return;
618efbed4dcSTheodore Ts'o 
619ac27a0ecSDave Kleikamp 	va_start(args, fmt);
6200ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6210ff2ea7dSJoe Perches 	vaf.va = &args;
6220ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
6230ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
624ac27a0ecSDave Kleikamp 	va_end(args);
625ac27a0ecSDave Kleikamp }
626ac27a0ecSDave Kleikamp 
627e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
628e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
629e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
630e29136f8STheodore Ts'o 			     const char *fmt, ...)
6315d1b1b3fSAneesh Kumar K.V __releases(bitlock)
6325d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
6335d1b1b3fSAneesh Kumar K.V {
6340ff2ea7dSJoe Perches 	struct va_format vaf;
6355d1b1b3fSAneesh Kumar K.V 	va_list args;
6365d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
6375d1b1b3fSAneesh Kumar K.V 
6381c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
6391c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
6401c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
6410ff2ea7dSJoe Perches 
642efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
6435d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
6440ff2ea7dSJoe Perches 		vaf.fmt = fmt;
6450ff2ea7dSJoe Perches 		vaf.va = &args;
64621149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
647e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
648e29136f8STheodore Ts'o 		if (ino)
6490ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
650e29136f8STheodore Ts'o 		if (block)
651efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
652efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
6530ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
6545d1b1b3fSAneesh Kumar K.V 		va_end(args);
655efbed4dcSTheodore Ts'o 	}
6565d1b1b3fSAneesh Kumar K.V 
6575d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
658e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
6595d1b1b3fSAneesh Kumar K.V 		return;
6605d1b1b3fSAneesh Kumar K.V 	}
6611c13d5c0STheodore Ts'o 
6625d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
6635d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
6645d1b1b3fSAneesh Kumar K.V 	/*
6655d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
6665d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
6675d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
6685d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
6695d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
67025985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
6715d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
6725d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
6735d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
6745d1b1b3fSAneesh Kumar K.V 	 */
6755d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
6765d1b1b3fSAneesh Kumar K.V 	return;
6775d1b1b3fSAneesh Kumar K.V }
6785d1b1b3fSAneesh Kumar K.V 
679617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
680ac27a0ecSDave Kleikamp {
681617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
682ac27a0ecSDave Kleikamp 
683617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
684ac27a0ecSDave Kleikamp 		return;
685ac27a0ecSDave Kleikamp 
68612062dddSEric Sandeen 	ext4_warning(sb,
687ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
688ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
689617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
690ac27a0ecSDave Kleikamp 
691617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
692617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
693617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
694ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
695ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
696ac27a0ecSDave Kleikamp 
697ac27a0ecSDave Kleikamp 	/*
698ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
699ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
700ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
701ac27a0ecSDave Kleikamp 	 */
702ac27a0ecSDave Kleikamp }
703ac27a0ecSDave Kleikamp 
704ac27a0ecSDave Kleikamp /*
705ac27a0ecSDave Kleikamp  * Open the external journal device
706ac27a0ecSDave Kleikamp  */
707b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
708ac27a0ecSDave Kleikamp {
709ac27a0ecSDave Kleikamp 	struct block_device *bdev;
710ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
711ac27a0ecSDave Kleikamp 
712d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
713ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
714ac27a0ecSDave Kleikamp 		goto fail;
715ac27a0ecSDave Kleikamp 	return bdev;
716ac27a0ecSDave Kleikamp 
717ac27a0ecSDave Kleikamp fail:
718b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
719ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
720ac27a0ecSDave Kleikamp 	return NULL;
721ac27a0ecSDave Kleikamp }
722ac27a0ecSDave Kleikamp 
723ac27a0ecSDave Kleikamp /*
724ac27a0ecSDave Kleikamp  * Release the journal device
725ac27a0ecSDave Kleikamp  */
7264385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
727ac27a0ecSDave Kleikamp {
7284385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
729ac27a0ecSDave Kleikamp }
730ac27a0ecSDave Kleikamp 
7314385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
732ac27a0ecSDave Kleikamp {
733ac27a0ecSDave Kleikamp 	struct block_device *bdev;
734ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
735ac27a0ecSDave Kleikamp 	if (bdev) {
7364385bab1SAl Viro 		ext4_blkdev_put(bdev);
737ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
738ac27a0ecSDave Kleikamp 	}
739ac27a0ecSDave Kleikamp }
740ac27a0ecSDave Kleikamp 
741ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
742ac27a0ecSDave Kleikamp {
743617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
744ac27a0ecSDave Kleikamp }
745ac27a0ecSDave Kleikamp 
746617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
747ac27a0ecSDave Kleikamp {
748ac27a0ecSDave Kleikamp 	struct list_head *l;
749ac27a0ecSDave Kleikamp 
750b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
751ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
752ac27a0ecSDave Kleikamp 
753ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
754ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
755ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
756ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
757ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
758ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
759ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
760ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
761ac27a0ecSDave Kleikamp 	}
762ac27a0ecSDave Kleikamp }
763ac27a0ecSDave Kleikamp 
764617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
765ac27a0ecSDave Kleikamp {
766617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
767617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
768ef2cabf7SHidehiro Kawai 	int i, err;
769ac27a0ecSDave Kleikamp 
770857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
771e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
772e0ccfd95SChristoph Hellwig 
7732e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
7742e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
7754c0425ffSMingming Cao 
7760390131bSFrank Mayhar 	if (sbi->s_journal) {
777ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
77847b4a50bSJan Kara 		sbi->s_journal = NULL;
779ef2cabf7SHidehiro Kawai 		if (err < 0)
780c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
7810390131bSFrank Mayhar 	}
782d4edac31SJosef Bacik 
783d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
7849105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
785d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
786d4edac31SJosef Bacik 	ext4_mb_release(sb);
787d4edac31SJosef Bacik 	ext4_ext_release(sb);
788d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
789d4edac31SJosef Bacik 
790ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
791617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
792ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
793ac27a0ecSDave Kleikamp 	}
79458c5873aSArtem Bityutskiy 	if (!(sb->s_flags & MS_RDONLY))
795a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
796a8e25a83SArtem Bityutskiy 
797240799cdSTheodore Ts'o 	if (sbi->s_proc) {
79866acdcf4STheodore Ts'o 		remove_proc_entry("options", sbi->s_proc);
7999f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
800240799cdSTheodore Ts'o 	}
8013197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
802ac27a0ecSDave Kleikamp 
803ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
804ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
805b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
806b93b41d4SAl Viro 	kvfree(sbi->s_flex_groups);
80757042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
808ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
809ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
81057042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
811ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
812ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
813a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
814ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
815ac27a0ecSDave Kleikamp #endif
816ac27a0ecSDave Kleikamp 
817ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
818ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
819ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
820ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
821ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
822ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
823ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
824ac27a0ecSDave Kleikamp 
825f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
826ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
827ac27a0ecSDave Kleikamp 		/*
828ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
829ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
830ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
831ac27a0ecSDave Kleikamp 		 */
832ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
833f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
834617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
835ac27a0ecSDave Kleikamp 	}
8369c191f70ST Makphaibulchoke 	if (sbi->s_mb_cache) {
8379c191f70ST Makphaibulchoke 		ext4_xattr_destroy_cache(sbi->s_mb_cache);
8389c191f70ST Makphaibulchoke 		sbi->s_mb_cache = NULL;
8399c191f70ST Makphaibulchoke 	}
840c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
841c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
842ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
8433197ebdbSTheodore Ts'o 	/*
8443197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
8453197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
8463197ebdbSTheodore Ts'o 	 */
8473197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
8483197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
8490441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
8500441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
851705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
852ac27a0ecSDave Kleikamp 	kfree(sbi);
853ac27a0ecSDave Kleikamp }
854ac27a0ecSDave Kleikamp 
855e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
856ac27a0ecSDave Kleikamp 
857ac27a0ecSDave Kleikamp /*
858ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
859ac27a0ecSDave Kleikamp  */
860617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
861ac27a0ecSDave Kleikamp {
862617ba13bSMingming Cao 	struct ext4_inode_info *ei;
863ac27a0ecSDave Kleikamp 
864e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
865ac27a0ecSDave Kleikamp 	if (!ei)
866ac27a0ecSDave Kleikamp 		return NULL;
8670b8e58a1SAndreas Dilger 
868ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
869202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
870c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
871c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
8729a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
8739a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
874edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
875eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
876edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
877dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
878d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
879d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
880d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
8819d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
8827e731bc9STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = 0;
883d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
884a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
885a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
886a9e7f447SDmitry Monakhov #endif
8878aefcd55STheodore Ts'o 	ei->jinode = NULL;
8882e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
889744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
890b436b9beSJan Kara 	ei->i_sync_tid = 0;
891b436b9beSJan Kara 	ei->i_datasync_tid = 0;
892f7ad6d2eSTheodore Ts'o 	atomic_set(&ei->i_ioend_count, 0);
893e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
8942e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
8950b8e58a1SAndreas Dilger 
896ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
897ac27a0ecSDave Kleikamp }
898ac27a0ecSDave Kleikamp 
8997ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
9007ff9c073STheodore Ts'o {
9017ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
9027ff9c073STheodore Ts'o 
9037ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
9047ff9c073STheodore Ts'o 	return drop;
9057ff9c073STheodore Ts'o }
9067ff9c073STheodore Ts'o 
907fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
908fa0d7e3dSNick Piggin {
909fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
910fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
911fa0d7e3dSNick Piggin }
912fa0d7e3dSNick Piggin 
913617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
914ac27a0ecSDave Kleikamp {
9159f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
916b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
917b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
918b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
9199f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
9209f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
9219f7dd93dSVasily Averin 				true);
9229f7dd93dSVasily Averin 		dump_stack();
9239f7dd93dSVasily Averin 	}
924fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
925ac27a0ecSDave Kleikamp }
926ac27a0ecSDave Kleikamp 
92751cc5068SAlexey Dobriyan static void init_once(void *foo)
928ac27a0ecSDave Kleikamp {
929617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
930ac27a0ecSDave Kleikamp 
931ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
932ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
9330e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
934ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
935ac27a0ecSDave Kleikamp }
936ac27a0ecSDave Kleikamp 
937e67bc2b3SFabian Frederick static int __init init_inodecache(void)
938ac27a0ecSDave Kleikamp {
939617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
940617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
941ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
942ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
94320c2df83SPaul Mundt 					     init_once);
944617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
945ac27a0ecSDave Kleikamp 		return -ENOMEM;
946ac27a0ecSDave Kleikamp 	return 0;
947ac27a0ecSDave Kleikamp }
948ac27a0ecSDave Kleikamp 
949ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
950ac27a0ecSDave Kleikamp {
9518c0a8537SKirill A. Shutemov 	/*
9528c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
9538c0a8537SKirill A. Shutemov 	 * destroy cache.
9548c0a8537SKirill A. Shutemov 	 */
9558c0a8537SKirill A. Shutemov 	rcu_barrier();
956617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
957ac27a0ecSDave Kleikamp }
958ac27a0ecSDave Kleikamp 
9590930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
960ac27a0ecSDave Kleikamp {
9610930fcc1SAl Viro 	invalidate_inode_buffers(inode);
962dbd5768fSJan Kara 	clear_inode(inode);
9639f754758SChristoph Hellwig 	dquot_drop(inode);
964c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
96551865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
9668aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
9678aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
9688aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
9698aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
9708aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
9718aefcd55STheodore Ts'o 	}
972ac27a0ecSDave Kleikamp }
973ac27a0ecSDave Kleikamp 
9741b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
9751b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
976ac27a0ecSDave Kleikamp {
977ac27a0ecSDave Kleikamp 	struct inode *inode;
978ac27a0ecSDave Kleikamp 
979617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
980ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
981617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
982ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
983ac27a0ecSDave Kleikamp 
984ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
985ac27a0ecSDave Kleikamp 	 *
986617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
987ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
988ac27a0ecSDave Kleikamp 	 *
989ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
990ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
991ac27a0ecSDave Kleikamp 	 */
992f4bb2981STheodore Ts'o 	inode = ext4_iget_normal(sb, ino);
9931d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9941d1fe1eeSDavid Howells 		return ERR_CAST(inode);
9951d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
996ac27a0ecSDave Kleikamp 		iput(inode);
997ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
998ac27a0ecSDave Kleikamp 	}
9991b961ac0SChristoph Hellwig 
10001b961ac0SChristoph Hellwig 	return inode;
1001ac27a0ecSDave Kleikamp }
10021b961ac0SChristoph Hellwig 
10031b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
10041b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10051b961ac0SChristoph Hellwig {
10061b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
10071b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
10081b961ac0SChristoph Hellwig }
10091b961ac0SChristoph Hellwig 
10101b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
10111b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10121b961ac0SChristoph Hellwig {
10131b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
10141b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1015ac27a0ecSDave Kleikamp }
1016ac27a0ecSDave Kleikamp 
1017c39a7f84SToshiyuki Okajima /*
1018c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1019c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1020c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1021c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1022c39a7f84SToshiyuki Okajima  */
10230b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
10240b8e58a1SAndreas Dilger 				 gfp_t wait)
1025c39a7f84SToshiyuki Okajima {
1026c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1027c39a7f84SToshiyuki Okajima 
1028c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1029c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1030c39a7f84SToshiyuki Okajima 		return 0;
1031c39a7f84SToshiyuki Okajima 	if (journal)
1032c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1033c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1034c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1035c39a7f84SToshiyuki Okajima }
1036c39a7f84SToshiyuki Okajima 
1037ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1038ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1039ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1040ac27a0ecSDave Kleikamp 
1041617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1042617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1043617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1044617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1045617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
10466f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1047f00c9e44SJan Kara 			 struct path *path);
10487c319d32SAditya Kali static int ext4_quota_on_sysfile(struct super_block *sb, int type,
10497c319d32SAditya Kali 				 int format_id);
1050ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
10517c319d32SAditya Kali static int ext4_quota_off_sysfile(struct super_block *sb, int type);
1052617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1053617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1054ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1055617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1056ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
10577c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
10587c319d32SAditya Kali 			     unsigned int flags);
10597c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1060ac27a0ecSDave Kleikamp 
106161e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
106260e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1063617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1064617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1065617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1066617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1067a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1068a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1069a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1070ac27a0ecSDave Kleikamp };
1071ac27a0ecSDave Kleikamp 
10720d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1073617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1074ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1075287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
1076287a8095SChristoph Hellwig 	.get_info	= dquot_get_dqinfo,
1077287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1078287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1079287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1080ac27a0ecSDave Kleikamp };
10817c319d32SAditya Kali 
10827c319d32SAditya Kali static const struct quotactl_ops ext4_qctl_sysfile_operations = {
10837c319d32SAditya Kali 	.quota_on_meta	= ext4_quota_on_sysfile,
10847c319d32SAditya Kali 	.quota_off	= ext4_quota_off_sysfile,
10857c319d32SAditya Kali 	.quota_sync	= dquot_quota_sync,
10867c319d32SAditya Kali 	.get_info	= dquot_get_dqinfo,
10877c319d32SAditya Kali 	.set_info	= dquot_set_dqinfo,
10887c319d32SAditya Kali 	.get_dqblk	= dquot_get_dqblk,
10897c319d32SAditya Kali 	.set_dqblk	= dquot_set_dqblk
10907c319d32SAditya Kali };
1091ac27a0ecSDave Kleikamp #endif
1092ac27a0ecSDave Kleikamp 
1093ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1094617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1095617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1096617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1097617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
10987ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
10990930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1100617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1101617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1102c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1103c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1104617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1105617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1106617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1107ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1108617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1109617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1110ac27a0ecSDave Kleikamp #endif
1111c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1112ac27a0ecSDave Kleikamp };
1113ac27a0ecSDave Kleikamp 
111439655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
11151b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
11161b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1117617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1118ac27a0ecSDave Kleikamp };
1119ac27a0ecSDave Kleikamp 
1120ac27a0ecSDave Kleikamp enum {
1121ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1122ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
112372578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1124ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
112572578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1126ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1127ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1128ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1129296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1130ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
11315a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1132ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1133661aa520SEric Sandeen 	Opt_usrquota, Opt_grpquota, Opt_i_version,
11341449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
11351449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
11365328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1137744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1138fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1139c6d3d56dSDarrick J. Wong 	Opt_max_dir_size_kb, Opt_nojournal_checksum,
1140ac27a0ecSDave Kleikamp };
1141ac27a0ecSDave Kleikamp 
1142a447c093SSteven Whitehouse static const match_table_t tokens = {
1143ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1144ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1145ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1146ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1147ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1148ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1149ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1150ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1151ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1152ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1153ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1154ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1155ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1156ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
115772578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
115872578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1159ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1160ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1161ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1162ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1163e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
11645a916be1STheodore Ts'o 	{Opt_noload, "noload"},
116572578c33STheodore Ts'o 	{Opt_removed, "nobh"},
116672578c33STheodore Ts'o 	{Opt_removed, "bh"},
1167ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
116830773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
116930773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1170ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1171ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1172818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1173c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1174818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1175ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1176ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1177ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1178ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
11795bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
11805bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1181ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1182ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1183ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1184ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1185ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1186ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
11875a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1188ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1189ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1190ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1191ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1192ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
119306705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
119406705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
119525ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1196c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
119764769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1198dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
119936ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
120036ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
12016fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
12026fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1203240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1204b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1205afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
120606705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
120706705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1208744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1209744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
12105328e635SEric Sandeen 	{Opt_discard, "discard"},
12115328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1212fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1213fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1214fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1215df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1216c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1217c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1218c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1219c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1220c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1221f3f12faaSJosef Bacik 	{Opt_err, NULL},
1222ac27a0ecSDave Kleikamp };
1223ac27a0ecSDave Kleikamp 
1224617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1225ac27a0ecSDave Kleikamp {
1226617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1227ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1228ac27a0ecSDave Kleikamp 
1229ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1230ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
12310b8e58a1SAndreas Dilger 
1232ac27a0ecSDave Kleikamp 	options += 3;
12330b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1234ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1235ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
12364776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1237ac27a0ecSDave Kleikamp 		       (char *) *data);
1238ac27a0ecSDave Kleikamp 		return 1;
1239ac27a0ecSDave Kleikamp 	}
1240ac27a0ecSDave Kleikamp 	if (*options == ',')
1241ac27a0ecSDave Kleikamp 		options++;
1242ac27a0ecSDave Kleikamp 	*data = (void *) options;
12430b8e58a1SAndreas Dilger 
1244ac27a0ecSDave Kleikamp 	return sb_block;
1245ac27a0ecSDave Kleikamp }
1246ac27a0ecSDave Kleikamp 
1247b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1248437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1249437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1250b3881f74STheodore Ts'o 
125156c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
125256c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
125356c50f11SDmitry Monakhov {
125456c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
125556c50f11SDmitry Monakhov 	char *qname;
125603dafb5fSChen Gang 	int ret = -1;
125756c50f11SDmitry Monakhov 
125856c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
125956c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
126056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
126156c50f11SDmitry Monakhov 			"Cannot change journaled "
126256c50f11SDmitry Monakhov 			"quota options when quota turned on");
126357f73c2cSTheodore Ts'o 		return -1;
126456c50f11SDmitry Monakhov 	}
1265262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1266262b4662SJan Kara 		ext4_msg(sb, KERN_ERR, "Cannot set journaled quota options "
1267262b4662SJan Kara 			 "when QUOTA feature is enabled");
1268262b4662SJan Kara 		return -1;
1269262b4662SJan Kara 	}
127056c50f11SDmitry Monakhov 	qname = match_strdup(args);
127156c50f11SDmitry Monakhov 	if (!qname) {
127256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
127356c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
127457f73c2cSTheodore Ts'o 		return -1;
127556c50f11SDmitry Monakhov 	}
127603dafb5fSChen Gang 	if (sbi->s_qf_names[qtype]) {
127703dafb5fSChen Gang 		if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
127803dafb5fSChen Gang 			ret = 1;
127903dafb5fSChen Gang 		else
128056c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
128103dafb5fSChen Gang 				 "%s quota file already specified",
128203dafb5fSChen Gang 				 QTYPE2NAME(qtype));
128303dafb5fSChen Gang 		goto errout;
128456c50f11SDmitry Monakhov 	}
128503dafb5fSChen Gang 	if (strchr(qname, '/')) {
128656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
128756c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
128803dafb5fSChen Gang 		goto errout;
128956c50f11SDmitry Monakhov 	}
129003dafb5fSChen Gang 	sbi->s_qf_names[qtype] = qname;
1291fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
129256c50f11SDmitry Monakhov 	return 1;
129303dafb5fSChen Gang errout:
129403dafb5fSChen Gang 	kfree(qname);
129503dafb5fSChen Gang 	return ret;
129656c50f11SDmitry Monakhov }
129756c50f11SDmitry Monakhov 
129856c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
129956c50f11SDmitry Monakhov {
130056c50f11SDmitry Monakhov 
130156c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
130256c50f11SDmitry Monakhov 
130356c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
130456c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
130556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
130656c50f11SDmitry Monakhov 			" when quota turned on");
130757f73c2cSTheodore Ts'o 		return -1;
130856c50f11SDmitry Monakhov 	}
130903dafb5fSChen Gang 	kfree(sbi->s_qf_names[qtype]);
131056c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
131156c50f11SDmitry Monakhov 	return 1;
131256c50f11SDmitry Monakhov }
131356c50f11SDmitry Monakhov #endif
131456c50f11SDmitry Monakhov 
131526092bf5STheodore Ts'o #define MOPT_SET	0x0001
131626092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
131726092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
131826092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
131926092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
132026092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
132126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
132226092bf5STheodore Ts'o #define MOPT_Q		0
132326092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
132426092bf5STheodore Ts'o #else
132526092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
132626092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
132726092bf5STheodore Ts'o #endif
132826092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
13298dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
13308dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
13318dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1332ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
133326092bf5STheodore Ts'o 
133426092bf5STheodore Ts'o static const struct mount_opts {
133526092bf5STheodore Ts'o 	int	token;
133626092bf5STheodore Ts'o 	int	mount_opt;
133726092bf5STheodore Ts'o 	int	flags;
133826092bf5STheodore Ts'o } ext4_mount_opts[] = {
133926092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
134026092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
134126092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
134226092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
134326092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
134426092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
13458dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
13468dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
13478dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
13488dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
134926092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
135026092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
13518dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
13528dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
13538dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
135459d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1355c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1356c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
13578dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
13588dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
135926092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
13608dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
13618dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
13628dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
136326092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
136426092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
136526092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
13668dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
13678dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_SET},
13688dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
13698dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_CLEAR},
137026092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
137126092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
137226092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
137326092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
137426092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
137526092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
137626092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
137726092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
137826092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
137926092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
138026092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
13810efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
13820efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
13830efb3b23SJan Kara 	{Opt_journal_dev, 0, MOPT_GTE0},
1384ad4eec61SEric Sandeen 	{Opt_journal_path, 0, MOPT_STRING},
13850efb3b23SJan Kara 	{Opt_journal_ioprio, 0, MOPT_GTE0},
13868dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
13878dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
13888dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
13898dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
139026092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
139126092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
139226092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
139326092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
139426092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
139526092bf5STheodore Ts'o #else
139626092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
139726092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
139826092bf5STheodore Ts'o #endif
139926092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
140026092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
140126092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
140226092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
140326092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
140426092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
140526092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
140626092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
140726092bf5STheodore Ts'o 		       EXT4_MOUNT_GRPQUOTA), MOPT_CLEAR | MOPT_Q},
140826092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
140926092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
141026092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
141126092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
141226092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
141326092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
141426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1415df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
141626092bf5STheodore Ts'o 	{Opt_err, 0, 0}
141726092bf5STheodore Ts'o };
141826092bf5STheodore Ts'o 
141926092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
142026092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
142126092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
142226092bf5STheodore Ts'o {
142326092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
142426092bf5STheodore Ts'o 	const struct mount_opts *m;
142508cefc7aSEric W. Biederman 	kuid_t uid;
142608cefc7aSEric W. Biederman 	kgid_t gid;
142726092bf5STheodore Ts'o 	int arg = 0;
142826092bf5STheodore Ts'o 
142957f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
143057f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
143157f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
143257f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
143357f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
143457f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
143557f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
143657f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
143757f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
143857f73c2cSTheodore Ts'o #endif
143926092bf5STheodore Ts'o 	switch (token) {
1440f7048605STheodore Ts'o 	case Opt_noacl:
1441f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1442f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1443f7048605STheodore Ts'o 		break;
144426092bf5STheodore Ts'o 	case Opt_sb:
144526092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
144626092bf5STheodore Ts'o 	case Opt_removed:
14475f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
144826092bf5STheodore Ts'o 		return 1;
144926092bf5STheodore Ts'o 	case Opt_abort:
145026092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
145126092bf5STheodore Ts'o 		return 1;
145226092bf5STheodore Ts'o 	case Opt_i_version:
145326092bf5STheodore Ts'o 		sb->s_flags |= MS_I_VERSION;
145426092bf5STheodore Ts'o 		return 1;
145526092bf5STheodore Ts'o 	}
145626092bf5STheodore Ts'o 
14575f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
14585f3633e3SJan Kara 		if (token == m->token)
14595f3633e3SJan Kara 			break;
14605f3633e3SJan Kara 
14615f3633e3SJan Kara 	if (m->token == Opt_err) {
14625f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
14635f3633e3SJan Kara 			 "or missing value", opt);
14645f3633e3SJan Kara 		return -1;
14655f3633e3SJan Kara 	}
14665f3633e3SJan Kara 
14678dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
14688dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
14698dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
14708dc0aa8cSTheodore Ts'o 		return -1;
14718dc0aa8cSTheodore Ts'o 	}
14728dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
14738dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
14748dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
14758dc0aa8cSTheodore Ts'o 		return -1;
14768dc0aa8cSTheodore Ts'o 	}
14778dc0aa8cSTheodore Ts'o 
1478ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
14790efb3b23SJan Kara 		return -1;
148026092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
148126092bf5STheodore Ts'o 		return -1;
148226092bf5STheodore Ts'o 	if (m->flags & MOPT_EXPLICIT)
148326092bf5STheodore Ts'o 		set_opt2(sb, EXPLICIT_DELALLOC);
148426092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
148526092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
148626092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
148726092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
148826092bf5STheodore Ts'o 			 "options when quota turned on");
148926092bf5STheodore Ts'o 		return -1;
149026092bf5STheodore Ts'o 	}
149126092bf5STheodore Ts'o 
149226092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
149326092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
149426092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
149526092bf5STheodore Ts'o 		if (arg == 0)
149626092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
149726092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
149826092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
149926092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
150026092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
150126092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
150226092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1503e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1504e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1505e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1506e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
150726092bf5STheodore Ts'o 			return -1;
150826092bf5STheodore Ts'o 		}
150926092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
151026092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
151126092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
151226092bf5STheodore Ts'o 		if (!args->from)
151326092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
151426092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1515df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1516df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
151726092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
151826092bf5STheodore Ts'o 		sbi->s_stripe = arg;
15190efb3b23SJan Kara 	} else if (token == Opt_resuid) {
15200efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
15210efb3b23SJan Kara 		if (!uid_valid(uid)) {
15225f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
15230efb3b23SJan Kara 			return -1;
15240efb3b23SJan Kara 		}
15250efb3b23SJan Kara 		sbi->s_resuid = uid;
15260efb3b23SJan Kara 	} else if (token == Opt_resgid) {
15270efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
15280efb3b23SJan Kara 		if (!gid_valid(gid)) {
15295f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
15300efb3b23SJan Kara 			return -1;
15310efb3b23SJan Kara 		}
15320efb3b23SJan Kara 		sbi->s_resgid = gid;
15330efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
15340efb3b23SJan Kara 		if (is_remount) {
15350efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
15360efb3b23SJan Kara 				 "Cannot specify journal on remount");
15370efb3b23SJan Kara 			return -1;
15380efb3b23SJan Kara 		}
15390efb3b23SJan Kara 		*journal_devnum = arg;
1540ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1541ad4eec61SEric Sandeen 		char *journal_path;
1542ad4eec61SEric Sandeen 		struct inode *journal_inode;
1543ad4eec61SEric Sandeen 		struct path path;
1544ad4eec61SEric Sandeen 		int error;
1545ad4eec61SEric Sandeen 
1546ad4eec61SEric Sandeen 		if (is_remount) {
1547ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1548ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
1549ad4eec61SEric Sandeen 			return -1;
1550ad4eec61SEric Sandeen 		}
1551ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
1552ad4eec61SEric Sandeen 		if (!journal_path) {
1553ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1554ad4eec61SEric Sandeen 				"journal device string");
1555ad4eec61SEric Sandeen 			return -1;
1556ad4eec61SEric Sandeen 		}
1557ad4eec61SEric Sandeen 
1558ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1559ad4eec61SEric Sandeen 		if (error) {
1560ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
1561ad4eec61SEric Sandeen 				"journal device path: error %d", error);
1562ad4eec61SEric Sandeen 			kfree(journal_path);
1563ad4eec61SEric Sandeen 			return -1;
1564ad4eec61SEric Sandeen 		}
1565ad4eec61SEric Sandeen 
1566ad4eec61SEric Sandeen 		journal_inode = path.dentry->d_inode;
1567ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
1568ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1569ad4eec61SEric Sandeen 				"is not a block device", journal_path);
1570ad4eec61SEric Sandeen 			path_put(&path);
1571ad4eec61SEric Sandeen 			kfree(journal_path);
1572ad4eec61SEric Sandeen 			return -1;
1573ad4eec61SEric Sandeen 		}
1574ad4eec61SEric Sandeen 
1575ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1576ad4eec61SEric Sandeen 		path_put(&path);
1577ad4eec61SEric Sandeen 		kfree(journal_path);
15780efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
15790efb3b23SJan Kara 		if (arg > 7) {
15805f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
15810efb3b23SJan Kara 				 " (must be 0-7)");
15820efb3b23SJan Kara 			return -1;
15830efb3b23SJan Kara 		}
15840efb3b23SJan Kara 		*journal_ioprio =
15850efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
158626092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
158726092bf5STheodore Ts'o 		if (is_remount) {
158826092bf5STheodore Ts'o 			if (!sbi->s_journal)
158926092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
15905f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
159126092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
159226092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
159326092bf5STheodore Ts'o 				return -1;
159426092bf5STheodore Ts'o 			}
159526092bf5STheodore Ts'o 		} else {
159626092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
159726092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
159826092bf5STheodore Ts'o 		}
159926092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
160026092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
160126092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
160226092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
16035f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
16045f3633e3SJan Kara 				 "quota options when quota turned on");
160526092bf5STheodore Ts'o 			return -1;
160626092bf5STheodore Ts'o 		}
1607262b4662SJan Kara 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
1608262b4662SJan Kara 					       EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1609262b4662SJan Kara 			ext4_msg(sb, KERN_ERR,
1610262b4662SJan Kara 				 "Cannot set journaled quota options "
1611262b4662SJan Kara 				 "when QUOTA feature is enabled");
1612262b4662SJan Kara 			return -1;
1613262b4662SJan Kara 		}
161426092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
161526092bf5STheodore Ts'o #endif
161626092bf5STheodore Ts'o 	} else {
161726092bf5STheodore Ts'o 		if (!args->from)
161826092bf5STheodore Ts'o 			arg = 1;
161926092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
162026092bf5STheodore Ts'o 			arg = !arg;
162126092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
162226092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
162326092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
162426092bf5STheodore Ts'o 			WARN_ON(1);
162526092bf5STheodore Ts'o 			return -1;
162626092bf5STheodore Ts'o 		}
162726092bf5STheodore Ts'o 		if (arg != 0)
162826092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
162926092bf5STheodore Ts'o 		else
163026092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
163126092bf5STheodore Ts'o 	}
163226092bf5STheodore Ts'o 	return 1;
163326092bf5STheodore Ts'o }
163426092bf5STheodore Ts'o 
1635ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1636c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1637b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1638661aa520SEric Sandeen 			 int is_remount)
1639ac27a0ecSDave Kleikamp {
1640617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1641ac27a0ecSDave Kleikamp 	char *p;
1642ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
164326092bf5STheodore Ts'o 	int token;
1644ac27a0ecSDave Kleikamp 
1645ac27a0ecSDave Kleikamp 	if (!options)
1646ac27a0ecSDave Kleikamp 		return 1;
1647ac27a0ecSDave Kleikamp 
1648ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1649ac27a0ecSDave Kleikamp 		if (!*p)
1650ac27a0ecSDave Kleikamp 			continue;
165115121c18SEric Sandeen 		/*
165215121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
165315121c18SEric Sandeen 		 * found; some options take optional arguments.
165415121c18SEric Sandeen 		 */
1655caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
1656ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
165726092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
165826092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
1659ac27a0ecSDave Kleikamp 			return 0;
1660ac27a0ecSDave Kleikamp 	}
1661ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1662262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1663262b4662SJan Kara 	    (test_opt(sb, USRQUOTA) || test_opt(sb, GRPQUOTA))) {
1664262b4662SJan Kara 		ext4_msg(sb, KERN_ERR, "Cannot set quota options when QUOTA "
1665262b4662SJan Kara 			 "feature is enabled");
1666262b4662SJan Kara 		return 0;
1667262b4662SJan Kara 	}
1668ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1669482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1670fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1671ac27a0ecSDave Kleikamp 
1672482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1673fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1674ac27a0ecSDave Kleikamp 
167556c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1676b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1677b31e1552SEric Sandeen 					"format mixing");
1678ac27a0ecSDave Kleikamp 			return 0;
1679ac27a0ecSDave Kleikamp 		}
1680ac27a0ecSDave Kleikamp 
1681ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1682b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1683b31e1552SEric Sandeen 					"not specified");
1684ac27a0ecSDave Kleikamp 			return 0;
1685ac27a0ecSDave Kleikamp 		}
1686ac27a0ecSDave Kleikamp 	}
1687ac27a0ecSDave Kleikamp #endif
1688261cb20cSJan Kara 	if (test_opt(sb, DIOREAD_NOLOCK)) {
1689261cb20cSJan Kara 		int blocksize =
1690261cb20cSJan Kara 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
1691261cb20cSJan Kara 
1692261cb20cSJan Kara 		if (blocksize < PAGE_CACHE_SIZE) {
1693261cb20cSJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
1694261cb20cSJan Kara 				 "dioread_nolock if block size != PAGE_SIZE");
1695261cb20cSJan Kara 			return 0;
1696261cb20cSJan Kara 		}
1697261cb20cSJan Kara 	}
1698d4f76107SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
1699d4f76107SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
1700d4f76107SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with journal_async_commit "
1701d4f76107SJan Kara 			 "in data=ordered mode");
1702d4f76107SJan Kara 		return 0;
1703d4f76107SJan Kara 	}
1704ac27a0ecSDave Kleikamp 	return 1;
1705ac27a0ecSDave Kleikamp }
1706ac27a0ecSDave Kleikamp 
17072adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
17082adf6da8STheodore Ts'o 					   struct super_block *sb)
17092adf6da8STheodore Ts'o {
17102adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
17112adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
17122adf6da8STheodore Ts'o 
17132adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
17142adf6da8STheodore Ts'o 		char *fmtname = "";
17152adf6da8STheodore Ts'o 
17162adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
17172adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
17182adf6da8STheodore Ts'o 			fmtname = "vfsold";
17192adf6da8STheodore Ts'o 			break;
17202adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
17212adf6da8STheodore Ts'o 			fmtname = "vfsv0";
17222adf6da8STheodore Ts'o 			break;
17232adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
17242adf6da8STheodore Ts'o 			fmtname = "vfsv1";
17252adf6da8STheodore Ts'o 			break;
17262adf6da8STheodore Ts'o 		}
17272adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
17282adf6da8STheodore Ts'o 	}
17292adf6da8STheodore Ts'o 
17302adf6da8STheodore Ts'o 	if (sbi->s_qf_names[USRQUOTA])
17312adf6da8STheodore Ts'o 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
17322adf6da8STheodore Ts'o 
17332adf6da8STheodore Ts'o 	if (sbi->s_qf_names[GRPQUOTA])
17342adf6da8STheodore Ts'o 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
17352adf6da8STheodore Ts'o #endif
17362adf6da8STheodore Ts'o }
17372adf6da8STheodore Ts'o 
17385a916be1STheodore Ts'o static const char *token2str(int token)
17395a916be1STheodore Ts'o {
174050df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
17415a916be1STheodore Ts'o 
17425a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
17435a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
17445a916be1STheodore Ts'o 			break;
17455a916be1STheodore Ts'o 	return t->pattern;
17465a916be1STheodore Ts'o }
17475a916be1STheodore Ts'o 
17482adf6da8STheodore Ts'o /*
17492adf6da8STheodore Ts'o  * Show an option if
17502adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
17512adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
17522adf6da8STheodore Ts'o  */
175366acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
175466acdcf4STheodore Ts'o 			      int nodefs)
17552adf6da8STheodore Ts'o {
17562adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
17572adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
175866acdcf4STheodore Ts'o 	int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
17595a916be1STheodore Ts'o 	const struct mount_opts *m;
176066acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
17612adf6da8STheodore Ts'o 
176266acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
176366acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
17642adf6da8STheodore Ts'o 
17652adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
17665a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
17675a916be1STheodore Ts'o 
17685a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
17695a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
17705a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
17715a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
17725a916be1STheodore Ts'o 			continue;
177366acdcf4STheodore Ts'o 		if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
17745a916be1STheodore Ts'o 			continue; /* skip if same as the default */
17755a916be1STheodore Ts'o 		if ((want_set &&
17765a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
17775a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
17785a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
17795a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
17805a916be1STheodore Ts'o 	}
17815a916be1STheodore Ts'o 
178208cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
17835a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
178408cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
178508cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
178608cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
17875a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
178808cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
178908cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
179066acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
17915a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
17925a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
17932adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
17945a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
17952adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
17965a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
179766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
17985a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
179966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
18005a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
180166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
18025a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
18032adf6da8STheodore Ts'o 	if (sb->s_flags & MS_I_VERSION)
18045a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
180566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
18065a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
180766acdcf4STheodore Ts'o 	if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
18082adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
18095a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
18102adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
18115a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
18122adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
18135a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
18145a916be1STheodore Ts'o 	}
181566acdcf4STheodore Ts'o 	if (nodefs ||
181666acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
18175a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
18182adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
18192adf6da8STheodore Ts'o 
182066acdcf4STheodore Ts'o 	if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
182166acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
18225a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
1823df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
1824df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
18252adf6da8STheodore Ts'o 
18262adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
18272adf6da8STheodore Ts'o 	return 0;
18282adf6da8STheodore Ts'o }
18292adf6da8STheodore Ts'o 
183066acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
183166acdcf4STheodore Ts'o {
183266acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
183366acdcf4STheodore Ts'o }
183466acdcf4STheodore Ts'o 
183566acdcf4STheodore Ts'o static int options_seq_show(struct seq_file *seq, void *offset)
183666acdcf4STheodore Ts'o {
183766acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
183866acdcf4STheodore Ts'o 	int rc;
183966acdcf4STheodore Ts'o 
184066acdcf4STheodore Ts'o 	seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
184166acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
184266acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
184366acdcf4STheodore Ts'o 	return rc;
184466acdcf4STheodore Ts'o }
184566acdcf4STheodore Ts'o 
184666acdcf4STheodore Ts'o static int options_open_fs(struct inode *inode, struct file *file)
184766acdcf4STheodore Ts'o {
1848d9dda78bSAl Viro 	return single_open(file, options_seq_show, PDE_DATA(inode));
184966acdcf4STheodore Ts'o }
185066acdcf4STheodore Ts'o 
185166acdcf4STheodore Ts'o static const struct file_operations ext4_seq_options_fops = {
185266acdcf4STheodore Ts'o 	.owner = THIS_MODULE,
185366acdcf4STheodore Ts'o 	.open = options_open_fs,
185466acdcf4STheodore Ts'o 	.read = seq_read,
185566acdcf4STheodore Ts'o 	.llseek = seq_lseek,
185666acdcf4STheodore Ts'o 	.release = single_release,
185766acdcf4STheodore Ts'o };
185866acdcf4STheodore Ts'o 
1859617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1860ac27a0ecSDave Kleikamp 			    int read_only)
1861ac27a0ecSDave Kleikamp {
1862617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1863ac27a0ecSDave Kleikamp 	int res = 0;
1864ac27a0ecSDave Kleikamp 
1865617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1866b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1867b31e1552SEric Sandeen 			 "forcing read-only mode");
1868ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1869ac27a0ecSDave Kleikamp 	}
1870ac27a0ecSDave Kleikamp 	if (read_only)
1871281b5995STheodore Ts'o 		goto done;
1872617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1873b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1874b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1875c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
1876b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1877b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1878b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1879ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1880ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1881ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1882b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1883b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1884b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1885ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1886ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1887ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1888b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1889b31e1552SEric Sandeen 			 "warning: checktime reached, "
1890b31e1552SEric Sandeen 			 "running e2fsck is recommended");
18910390131bSFrank Mayhar 	if (!sbi->s_journal)
1892216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1893ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1894617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1895e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1896ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1897617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
18980390131bSFrank Mayhar 	if (sbi->s_journal)
1899617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1900ac27a0ecSDave Kleikamp 
1901e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1902281b5995STheodore Ts'o done:
1903ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1904a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1905a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1906ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1907ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1908617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1909617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1910a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
1911ac27a0ecSDave Kleikamp 
19127abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
1913ac27a0ecSDave Kleikamp 	return res;
1914ac27a0ecSDave Kleikamp }
1915ac27a0ecSDave Kleikamp 
1916117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
1917117fff10STheodore Ts'o {
1918117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1919117fff10STheodore Ts'o 	struct flex_groups *new_groups;
1920117fff10STheodore Ts'o 	int size;
1921117fff10STheodore Ts'o 
1922117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
1923117fff10STheodore Ts'o 		return 0;
1924117fff10STheodore Ts'o 
1925117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
1926117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
1927117fff10STheodore Ts'o 		return 0;
1928117fff10STheodore Ts'o 
1929117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
1930117fff10STheodore Ts'o 	new_groups = ext4_kvzalloc(size, GFP_KERNEL);
1931117fff10STheodore Ts'o 	if (!new_groups) {
1932117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
1933117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
1934117fff10STheodore Ts'o 		return -ENOMEM;
1935117fff10STheodore Ts'o 	}
1936117fff10STheodore Ts'o 
1937117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
1938117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
1939117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
1940117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
1941b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
1942117fff10STheodore Ts'o 	}
1943117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
1944117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
1945117fff10STheodore Ts'o 	return 0;
1946117fff10STheodore Ts'o }
1947117fff10STheodore Ts'o 
1948772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1949772cb7c8SJose R. Santos {
1950772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1951772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1952772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1953117fff10STheodore Ts'o 	int i, err;
1954772cb7c8SJose R. Santos 
1955503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1956d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
1957772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1958772cb7c8SJose R. Santos 		return 1;
1959772cb7c8SJose R. Santos 	}
1960772cb7c8SJose R. Santos 
1961117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
1962117fff10STheodore Ts'o 	if (err)
1963772cb7c8SJose R. Santos 		goto failed;
1964772cb7c8SJose R. Santos 
1965772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
196688b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1967772cb7c8SJose R. Santos 
1968772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
19697ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
19707ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
197190ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
197224aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
19737ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
19747ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
1975772cb7c8SJose R. Santos 	}
1976772cb7c8SJose R. Santos 
1977772cb7c8SJose R. Santos 	return 1;
1978772cb7c8SJose R. Santos failed:
1979772cb7c8SJose R. Santos 	return 0;
1980772cb7c8SJose R. Santos }
1981772cb7c8SJose R. Santos 
1982feb0ab32SDarrick J. Wong static __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1983717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
1984717d50e4SAndreas Dilger {
1985feb0ab32SDarrick J. Wong 	int offset;
1986717d50e4SAndreas Dilger 	__u16 crc = 0;
1987717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
1988717d50e4SAndreas Dilger 
19899aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
1990feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
1991d6a77105STheodore Ts'o 		__le16 save_csum;
1992feb0ab32SDarrick J. Wong 		__u32 csum32;
1993feb0ab32SDarrick J. Wong 
1994d6a77105STheodore Ts'o 		save_csum = gdp->bg_checksum;
1995feb0ab32SDarrick J. Wong 		gdp->bg_checksum = 0;
1996feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
1997feb0ab32SDarrick J. Wong 				     sizeof(le_group));
1998feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp,
1999feb0ab32SDarrick J. Wong 				     sbi->s_desc_size);
2000d6a77105STheodore Ts'o 		gdp->bg_checksum = save_csum;
2001feb0ab32SDarrick J. Wong 
2002feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2003feb0ab32SDarrick J. Wong 		goto out;
2004feb0ab32SDarrick J. Wong 	}
2005feb0ab32SDarrick J. Wong 
2006feb0ab32SDarrick J. Wong 	/* old crc16 code */
2007813d32f9SDarrick J. Wong 	if (!(sbi->s_es->s_feature_ro_compat &
2008813d32f9SDarrick J. Wong 	      cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
2009813d32f9SDarrick J. Wong 		return 0;
2010813d32f9SDarrick J. Wong 
2011feb0ab32SDarrick J. Wong 	offset = offsetof(struct ext4_group_desc, bg_checksum);
2012feb0ab32SDarrick J. Wong 
2013717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2014717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2015717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2016717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2017717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2018717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_incompat &
2019717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
2020717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2021717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2022717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2023717d50e4SAndreas Dilger 				offset);
2024717d50e4SAndreas Dilger 
2025feb0ab32SDarrick J. Wong out:
2026717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2027717d50e4SAndreas Dilger }
2028717d50e4SAndreas Dilger 
2029feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2030717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2031717d50e4SAndreas Dilger {
2032feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2033feb0ab32SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(EXT4_SB(sb),
2034feb0ab32SDarrick J. Wong 						      block_group, gdp)))
2035717d50e4SAndreas Dilger 		return 0;
2036717d50e4SAndreas Dilger 
2037717d50e4SAndreas Dilger 	return 1;
2038717d50e4SAndreas Dilger }
2039717d50e4SAndreas Dilger 
2040feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2041feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2042feb0ab32SDarrick J. Wong {
2043feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2044feb0ab32SDarrick J. Wong 		return;
2045feb0ab32SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(EXT4_SB(sb), block_group, gdp);
2046feb0ab32SDarrick J. Wong }
2047feb0ab32SDarrick J. Wong 
2048ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2049bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2050bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2051ac27a0ecSDave Kleikamp {
2052617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2053617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2054617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2055bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2056bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2057bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2058ce421581SJose R. Santos 	int flexbg_flag = 0;
2059bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2060ac27a0ecSDave Kleikamp 
2061ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2062ce421581SJose R. Santos 		flexbg_flag = 1;
2063ce421581SJose R. Santos 
2064617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2065ac27a0ecSDave Kleikamp 
2066197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2067197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2068197cd65aSAkinobu Mita 
2069ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2070bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2071ac27a0ecSDave Kleikamp 		else
2072ac27a0ecSDave Kleikamp 			last_block = first_block +
2073617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2074ac27a0ecSDave Kleikamp 
2075bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2076bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2077bfff6873SLukas Czerner 			grp = i;
2078bfff6873SLukas Czerner 
20798fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
20802b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2081b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2082a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2083b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2084ac27a0ecSDave Kleikamp 			return 0;
2085ac27a0ecSDave Kleikamp 		}
20868fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
20872b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2088b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2089a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2090b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2091ac27a0ecSDave Kleikamp 			return 0;
2092ac27a0ecSDave Kleikamp 		}
20938fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2094bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
20952b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2096b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2097a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2098b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2099ac27a0ecSDave Kleikamp 			return 0;
2100ac27a0ecSDave Kleikamp 		}
2101955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2102feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2103b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2104b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2105fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2106fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
21077ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2108955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2109717d50e4SAndreas Dilger 				return 0;
2110717d50e4SAndreas Dilger 			}
21117ee1ec4cSLi Zefan 		}
2112955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2113ce421581SJose R. Santos 		if (!flexbg_flag)
2114617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2115ac27a0ecSDave Kleikamp 	}
2116bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2117bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2118ac27a0ecSDave Kleikamp 	return 1;
2119ac27a0ecSDave Kleikamp }
2120ac27a0ecSDave Kleikamp 
2121617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2122ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2123ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2124ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2125ac27a0ecSDave Kleikamp  *
2126ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2127ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2128ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2129ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2130ac27a0ecSDave Kleikamp  *
2131ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2132ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2133ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2134617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2135ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2136ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2137ac27a0ecSDave Kleikamp  */
2138617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2139617ba13bSMingming Cao 				struct ext4_super_block *es)
2140ac27a0ecSDave Kleikamp {
2141ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
2142ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
2143ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2144ac27a0ecSDave Kleikamp 	int i;
2145ac27a0ecSDave Kleikamp #endif
2146ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2147ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2148ac27a0ecSDave Kleikamp 		return;
2149ac27a0ecSDave Kleikamp 	}
2150ac27a0ecSDave Kleikamp 
2151a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2152b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2153b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2154a8f48a95SEric Sandeen 		return;
2155a8f48a95SEric Sandeen 	}
2156a8f48a95SEric Sandeen 
2157d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2158d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2159d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2160d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2161d39195c3SAmir Goldstein 		return;
2162d39195c3SAmir Goldstein 	}
2163d39195c3SAmir Goldstein 
2164617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2165c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
2166c25f9bc6SEric Sandeen 		if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
216784474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2168ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2169ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2170c25f9bc6SEric Sandeen 		}
2171ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2172ac27a0ecSDave Kleikamp 		return;
2173ac27a0ecSDave Kleikamp 	}
2174ac27a0ecSDave Kleikamp 
2175ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2176b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2177ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2178ac27a0ecSDave Kleikamp 	}
2179ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2180ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2181ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2182ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2183a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2184617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2185617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2186ac27a0ecSDave Kleikamp 			if (ret < 0)
2187b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2188b31e1552SEric Sandeen 					"Cannot turn on journaled "
2189b31e1552SEric Sandeen 					"quota: error %d", ret);
2190ac27a0ecSDave Kleikamp 		}
2191ac27a0ecSDave Kleikamp 	}
2192ac27a0ecSDave Kleikamp #endif
2193ac27a0ecSDave Kleikamp 
2194ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2195ac27a0ecSDave Kleikamp 		struct inode *inode;
2196ac27a0ecSDave Kleikamp 
219797bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
219897bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2199ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2200ac27a0ecSDave Kleikamp 			break;
2201ac27a0ecSDave Kleikamp 		}
2202ac27a0ecSDave Kleikamp 
2203617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2204871a2931SChristoph Hellwig 		dquot_initialize(inode);
2205ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2206566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2207b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2208b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
220946e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2210e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2211ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2212721e3ebaSTheodore Ts'o 			mutex_lock(&inode->i_mutex);
221355f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
2214617ba13bSMingming Cao 			ext4_truncate(inode);
2215721e3ebaSTheodore Ts'o 			mutex_unlock(&inode->i_mutex);
2216ac27a0ecSDave Kleikamp 			nr_truncates++;
2217ac27a0ecSDave Kleikamp 		} else {
2218566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2219b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2220b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
222146e665e9SHarvey Harrison 					__func__, inode->i_ino);
2222ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2223ac27a0ecSDave Kleikamp 				  inode->i_ino);
2224ac27a0ecSDave Kleikamp 			nr_orphans++;
2225ac27a0ecSDave Kleikamp 		}
2226ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2227ac27a0ecSDave Kleikamp 	}
2228ac27a0ecSDave Kleikamp 
2229ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2230ac27a0ecSDave Kleikamp 
2231ac27a0ecSDave Kleikamp 	if (nr_orphans)
2232b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2233b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2234ac27a0ecSDave Kleikamp 	if (nr_truncates)
2235b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2236b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2237ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2238ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2239a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2240ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2241287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2242ac27a0ecSDave Kleikamp 	}
2243ac27a0ecSDave Kleikamp #endif
2244ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2245ac27a0ecSDave Kleikamp }
22460b8e58a1SAndreas Dilger 
2247cd2291a4SEric Sandeen /*
2248cd2291a4SEric Sandeen  * Maximal extent format file size.
2249cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2250cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2251cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2252cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2253cd2291a4SEric Sandeen  *
2254f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2255f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2256f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2257f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2258f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2259f17722f9SLukas Czerner  *
2260cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2261cd2291a4SEric Sandeen  */
2262f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2263cd2291a4SEric Sandeen {
2264cd2291a4SEric Sandeen 	loff_t res;
2265cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2266cd2291a4SEric Sandeen 
2267cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2268f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2269cd2291a4SEric Sandeen 		/*
227090c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2271cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2272cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2273cd2291a4SEric Sandeen 		 */
2274cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2275cd2291a4SEric Sandeen 
2276cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2277cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2278cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2279cd2291a4SEric Sandeen 	}
2280cd2291a4SEric Sandeen 
2281f17722f9SLukas Czerner 	/*
2282f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2283f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2284f17722f9SLukas Czerner 	 * size
2285f17722f9SLukas Czerner 	 */
2286f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2287cd2291a4SEric Sandeen 	res <<= blkbits;
2288cd2291a4SEric Sandeen 
2289cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2290cd2291a4SEric Sandeen 	if (res > upper_limit)
2291cd2291a4SEric Sandeen 		res = upper_limit;
2292cd2291a4SEric Sandeen 
2293cd2291a4SEric Sandeen 	return res;
2294cd2291a4SEric Sandeen }
2295ac27a0ecSDave Kleikamp 
2296ac27a0ecSDave Kleikamp /*
2297cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
22980fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
22990fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2300ac27a0ecSDave Kleikamp  */
2301f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2302ac27a0ecSDave Kleikamp {
2303617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
23040fc1b451SAneesh Kumar K.V 	int meta_blocks;
23050fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
23060b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
23070b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
23080b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
23090b8e58a1SAndreas Dilger 	 *
23100b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
23110b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
23120fc1b451SAneesh Kumar K.V 	 */
23130fc1b451SAneesh Kumar K.V 
2314f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
23150fc1b451SAneesh Kumar K.V 		/*
231690c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
23170b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
23180b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
23190fc1b451SAneesh Kumar K.V 		 */
23200fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
23210fc1b451SAneesh Kumar K.V 
23220fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
23230fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
23240fc1b451SAneesh Kumar K.V 
23250fc1b451SAneesh Kumar K.V 	} else {
23268180a562SAneesh Kumar K.V 		/*
23278180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
23288180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
23298180a562SAneesh Kumar K.V 		 * represent total number of blocks in
23308180a562SAneesh Kumar K.V 		 * file system block size
23318180a562SAneesh Kumar K.V 		 */
23320fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
23330fc1b451SAneesh Kumar K.V 
23340fc1b451SAneesh Kumar K.V 	}
23350fc1b451SAneesh Kumar K.V 
23360fc1b451SAneesh Kumar K.V 	/* indirect blocks */
23370fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
23380fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
23390fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
23400fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
23410fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
23420fc1b451SAneesh Kumar K.V 
23430fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
23440fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2345ac27a0ecSDave Kleikamp 
2346ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2347ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2348ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2349ac27a0ecSDave Kleikamp 	res <<= bits;
2350ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2351ac27a0ecSDave Kleikamp 		res = upper_limit;
23520fc1b451SAneesh Kumar K.V 
23530fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
23540fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
23550fc1b451SAneesh Kumar K.V 
2356ac27a0ecSDave Kleikamp 	return res;
2357ac27a0ecSDave Kleikamp }
2358ac27a0ecSDave Kleikamp 
2359617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
236070bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2361ac27a0ecSDave Kleikamp {
2362617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2363fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2364ac27a0ecSDave Kleikamp 	int has_super = 0;
2365ac27a0ecSDave Kleikamp 
2366ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2367ac27a0ecSDave Kleikamp 
2368617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2369ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
237070bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2371ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2372617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2373ac27a0ecSDave Kleikamp 		has_super = 1;
23740b8e58a1SAndreas Dilger 
2375bd63f6b0SDarrick J. Wong 	/*
2376bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2377bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2378bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2379bd63f6b0SDarrick J. Wong 	 * compensate.
2380bd63f6b0SDarrick J. Wong 	 */
2381bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
2382bd63f6b0SDarrick J. Wong 	    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) == 0)
2383bd63f6b0SDarrick J. Wong 		has_super++;
2384bd63f6b0SDarrick J. Wong 
2385617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2386ac27a0ecSDave Kleikamp }
2387ac27a0ecSDave Kleikamp 
2388c9de560dSAlex Tomas /**
2389c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2390c9de560dSAlex Tomas  * @sbi: In memory super block info
2391c9de560dSAlex Tomas  *
2392c9de560dSAlex Tomas  * If we have specified it via mount option, then
2393c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2394c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2395c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2396c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2397c9de560dSAlex Tomas  *
2398c9de560dSAlex Tomas  */
2399c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2400c9de560dSAlex Tomas {
2401c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2402c9de560dSAlex Tomas 	unsigned long stripe_width =
2403c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
24043eb08658SDan Ehrenberg 	int ret;
2405c9de560dSAlex Tomas 
2406c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
24073eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
24083eb08658SDan Ehrenberg 	else if (stripe_width <= sbi->s_blocks_per_group)
24093eb08658SDan Ehrenberg 		ret = stripe_width;
24103eb08658SDan Ehrenberg 	else if (stride <= sbi->s_blocks_per_group)
24113eb08658SDan Ehrenberg 		ret = stride;
24123eb08658SDan Ehrenberg 	else
24133eb08658SDan Ehrenberg 		ret = 0;
2414c9de560dSAlex Tomas 
24153eb08658SDan Ehrenberg 	/*
24163eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
24173eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
24183eb08658SDan Ehrenberg 	 */
24193eb08658SDan Ehrenberg 	if (ret <= 1)
24203eb08658SDan Ehrenberg 		ret = 0;
2421c9de560dSAlex Tomas 
24223eb08658SDan Ehrenberg 	return ret;
2423c9de560dSAlex Tomas }
2424ac27a0ecSDave Kleikamp 
24253197ebdbSTheodore Ts'o /* sysfs supprt */
24263197ebdbSTheodore Ts'o 
24273197ebdbSTheodore Ts'o struct ext4_attr {
24283197ebdbSTheodore Ts'o 	struct attribute attr;
24293197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
24303197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
24313197ebdbSTheodore Ts'o 			 const char *, size_t);
2432f2d50a65SJan Kara 	union {
24333197ebdbSTheodore Ts'o 		int offset;
2434f2d50a65SJan Kara 		int deprecated_val;
2435f2d50a65SJan Kara 	} u;
24363197ebdbSTheodore Ts'o };
24373197ebdbSTheodore Ts'o 
243827dd4385SLukas Czerner static int parse_strtoull(const char *buf,
243927dd4385SLukas Czerner 		unsigned long long max, unsigned long long *value)
244027dd4385SLukas Czerner {
244127dd4385SLukas Czerner 	int ret;
244227dd4385SLukas Czerner 
244327dd4385SLukas Czerner 	ret = kstrtoull(skip_spaces(buf), 0, value);
244427dd4385SLukas Czerner 	if (!ret && *value > max)
244527dd4385SLukas Czerner 		ret = -EINVAL;
244627dd4385SLukas Czerner 	return ret;
244727dd4385SLukas Czerner }
244827dd4385SLukas Czerner 
24493197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
24503197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
24513197ebdbSTheodore Ts'o 					      char *buf)
24523197ebdbSTheodore Ts'o {
24533197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
24547b415bf6SAditya Kali 		(s64) EXT4_C2B(sbi,
24557b415bf6SAditya Kali 			percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
24563197ebdbSTheodore Ts'o }
24573197ebdbSTheodore Ts'o 
24583197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
24593197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
24603197ebdbSTheodore Ts'o {
24613197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24623197ebdbSTheodore Ts'o 
2463f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2464f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24653197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
24663197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
24673197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
24683197ebdbSTheodore Ts'o }
24693197ebdbSTheodore Ts'o 
24703197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
24713197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
24723197ebdbSTheodore Ts'o {
24733197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24743197ebdbSTheodore Ts'o 
2475f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2476f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24773197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2478a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
24793197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2480a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
24813197ebdbSTheodore Ts'o }
24823197ebdbSTheodore Ts'o 
24833197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
24843197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
24853197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
24863197ebdbSTheodore Ts'o {
24873197ebdbSTheodore Ts'o 	unsigned long t;
2488e1091b15SLukas Czerner 	int ret;
24893197ebdbSTheodore Ts'o 
2490e1091b15SLukas Czerner 	ret = kstrtoul(skip_spaces(buf), 0, &t);
2491e1091b15SLukas Czerner 	if (ret)
2492e1091b15SLukas Czerner 		return ret;
24933197ebdbSTheodore Ts'o 
2494e1091b15SLukas Czerner 	if (t && (!is_power_of_2(t) || t > 0x40000000))
24953197ebdbSTheodore Ts'o 		return -EINVAL;
24963197ebdbSTheodore Ts'o 
24973197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
24983197ebdbSTheodore Ts'o 	return count;
24993197ebdbSTheodore Ts'o }
25003197ebdbSTheodore Ts'o 
25013197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
25023197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
25033197ebdbSTheodore Ts'o {
2504f2d50a65SJan Kara 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
25053197ebdbSTheodore Ts'o 
25063197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
25073197ebdbSTheodore Ts'o }
25083197ebdbSTheodore Ts'o 
25093197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
25103197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
25113197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
25123197ebdbSTheodore Ts'o {
2513f2d50a65SJan Kara 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
25143197ebdbSTheodore Ts'o 	unsigned long t;
2515e1091b15SLukas Czerner 	int ret;
25163197ebdbSTheodore Ts'o 
2517e1091b15SLukas Czerner 	ret = kstrtoul(skip_spaces(buf), 0, &t);
2518e1091b15SLukas Czerner 	if (ret)
2519e1091b15SLukas Czerner 		return ret;
25203197ebdbSTheodore Ts'o 	*ui = t;
25213197ebdbSTheodore Ts'o 	return count;
25223197ebdbSTheodore Ts'o }
25233197ebdbSTheodore Ts'o 
252452c198c6SLukas Czerner static ssize_t es_ui_show(struct ext4_attr *a,
252552c198c6SLukas Czerner 			   struct ext4_sb_info *sbi, char *buf)
252652c198c6SLukas Czerner {
252752c198c6SLukas Czerner 
252852c198c6SLukas Czerner 	unsigned int *ui = (unsigned int *) (((char *) sbi->s_es) +
252952c198c6SLukas Czerner 			   a->u.offset);
253052c198c6SLukas Czerner 
253152c198c6SLukas Czerner 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
253252c198c6SLukas Czerner }
253352c198c6SLukas Czerner 
253427dd4385SLukas Czerner static ssize_t reserved_clusters_show(struct ext4_attr *a,
253527dd4385SLukas Czerner 				  struct ext4_sb_info *sbi, char *buf)
253627dd4385SLukas Czerner {
253727dd4385SLukas Czerner 	return snprintf(buf, PAGE_SIZE, "%llu\n",
253827dd4385SLukas Czerner 		(unsigned long long) atomic64_read(&sbi->s_resv_clusters));
253927dd4385SLukas Czerner }
254027dd4385SLukas Czerner 
254127dd4385SLukas Czerner static ssize_t reserved_clusters_store(struct ext4_attr *a,
254227dd4385SLukas Czerner 				   struct ext4_sb_info *sbi,
254327dd4385SLukas Czerner 				   const char *buf, size_t count)
254427dd4385SLukas Czerner {
254527dd4385SLukas Czerner 	unsigned long long val;
254627dd4385SLukas Czerner 	int ret;
254727dd4385SLukas Czerner 
254827dd4385SLukas Czerner 	if (parse_strtoull(buf, -1ULL, &val))
254927dd4385SLukas Czerner 		return -EINVAL;
255027dd4385SLukas Czerner 	ret = ext4_reserve_clusters(sbi, val);
255127dd4385SLukas Czerner 
255227dd4385SLukas Czerner 	return ret ? ret : count;
255327dd4385SLukas Czerner }
255427dd4385SLukas Czerner 
25552c0544b2STheodore Ts'o static ssize_t trigger_test_error(struct ext4_attr *a,
25562c0544b2STheodore Ts'o 				  struct ext4_sb_info *sbi,
25572c0544b2STheodore Ts'o 				  const char *buf, size_t count)
25582c0544b2STheodore Ts'o {
25592c0544b2STheodore Ts'o 	int len = count;
25602c0544b2STheodore Ts'o 
25612c0544b2STheodore Ts'o 	if (!capable(CAP_SYS_ADMIN))
25622c0544b2STheodore Ts'o 		return -EPERM;
25632c0544b2STheodore Ts'o 
25642c0544b2STheodore Ts'o 	if (len && buf[len-1] == '\n')
25652c0544b2STheodore Ts'o 		len--;
25662c0544b2STheodore Ts'o 
25672c0544b2STheodore Ts'o 	if (len)
25682c0544b2STheodore Ts'o 		ext4_error(sbi->s_sb, "%.*s", len, buf);
25692c0544b2STheodore Ts'o 	return count;
25702c0544b2STheodore Ts'o }
25712c0544b2STheodore Ts'o 
2572f2d50a65SJan Kara static ssize_t sbi_deprecated_show(struct ext4_attr *a,
2573f2d50a65SJan Kara 				   struct ext4_sb_info *sbi, char *buf)
2574f2d50a65SJan Kara {
2575f2d50a65SJan Kara 	return snprintf(buf, PAGE_SIZE, "%d\n", a->u.deprecated_val);
2576f2d50a65SJan Kara }
2577f2d50a65SJan Kara 
25783197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
25793197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
25803197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
25813197ebdbSTheodore Ts'o 	.show	= _show,					\
25823197ebdbSTheodore Ts'o 	.store	= _store,					\
2583f2d50a65SJan Kara 	.u = {							\
25843197ebdbSTheodore Ts'o 		.offset = offsetof(struct ext4_sb_info, _elname),\
2585f2d50a65SJan Kara 	},							\
25863197ebdbSTheodore Ts'o }
258752c198c6SLukas Czerner 
258852c198c6SLukas Czerner #define EXT4_ATTR_OFFSET_ES(_name,_mode,_show,_store,_elname)		\
258952c198c6SLukas Czerner static struct ext4_attr ext4_attr_##_name = {				\
259052c198c6SLukas Czerner 	.attr = {.name = __stringify(_name), .mode = _mode },		\
259152c198c6SLukas Czerner 	.show	= _show,						\
259252c198c6SLukas Czerner 	.store	= _store,						\
259352c198c6SLukas Czerner 	.u = {								\
259452c198c6SLukas Czerner 		.offset = offsetof(struct ext4_super_block, _elname),	\
259552c198c6SLukas Czerner 	},								\
259652c198c6SLukas Czerner }
259752c198c6SLukas Czerner 
25983197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
25993197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
26003197ebdbSTheodore Ts'o 
2601857ac889SLukas Czerner #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
26023197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
26033197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
260452c198c6SLukas Czerner 
260552c198c6SLukas Czerner #define EXT4_RO_ATTR_ES_UI(name, elname)	\
260652c198c6SLukas Czerner 	EXT4_ATTR_OFFSET_ES(name, 0444, es_ui_show, NULL, elname)
26073197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
26083197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
260952c198c6SLukas Czerner 
26103197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
2611f2d50a65SJan Kara #define EXT4_DEPRECATED_ATTR(_name, _val)	\
2612f2d50a65SJan Kara static struct ext4_attr ext4_attr_##_name = {			\
2613f2d50a65SJan Kara 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
2614f2d50a65SJan Kara 	.show	= sbi_deprecated_show,				\
2615f2d50a65SJan Kara 	.u = {							\
2616f2d50a65SJan Kara 		.deprecated_val = _val,				\
2617f2d50a65SJan Kara 	},							\
2618f2d50a65SJan Kara }
26193197ebdbSTheodore Ts'o 
26203197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
26213197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
26223197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
262327dd4385SLukas Czerner EXT4_RW_ATTR(reserved_clusters);
26243197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
26253197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
262611013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
26273197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
26283197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
26293197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
26303197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
26313197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
26323197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2633f2d50a65SJan Kara EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
263467a5da56SZheng Liu EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
26352c0544b2STheodore Ts'o EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
2636efbed4dcSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
2637efbed4dcSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
2638efbed4dcSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
2639efbed4dcSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
2640efbed4dcSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
2641efbed4dcSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
264252c198c6SLukas Czerner EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
264352c198c6SLukas Czerner EXT4_RO_ATTR_ES_UI(first_error_time, s_first_error_time);
264452c198c6SLukas Czerner EXT4_RO_ATTR_ES_UI(last_error_time, s_last_error_time);
26453197ebdbSTheodore Ts'o 
26463197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
26473197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
26483197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
26493197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
265027dd4385SLukas Czerner 	ATTR_LIST(reserved_clusters),
26513197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
265211013911SAndreas Dilger 	ATTR_LIST(inode_goal),
26533197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
26543197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
26553197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
26563197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
26573197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
26583197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
265955138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
266067a5da56SZheng Liu 	ATTR_LIST(extent_max_zeroout_kb),
26612c0544b2STheodore Ts'o 	ATTR_LIST(trigger_fs_error),
2662efbed4dcSTheodore Ts'o 	ATTR_LIST(err_ratelimit_interval_ms),
2663efbed4dcSTheodore Ts'o 	ATTR_LIST(err_ratelimit_burst),
2664efbed4dcSTheodore Ts'o 	ATTR_LIST(warning_ratelimit_interval_ms),
2665efbed4dcSTheodore Ts'o 	ATTR_LIST(warning_ratelimit_burst),
2666efbed4dcSTheodore Ts'o 	ATTR_LIST(msg_ratelimit_interval_ms),
2667efbed4dcSTheodore Ts'o 	ATTR_LIST(msg_ratelimit_burst),
266852c198c6SLukas Czerner 	ATTR_LIST(errors_count),
266952c198c6SLukas Czerner 	ATTR_LIST(first_error_time),
267052c198c6SLukas Czerner 	ATTR_LIST(last_error_time),
26713197ebdbSTheodore Ts'o 	NULL,
26723197ebdbSTheodore Ts'o };
26733197ebdbSTheodore Ts'o 
2674857ac889SLukas Czerner /* Features this copy of ext4 supports */
2675857ac889SLukas Czerner EXT4_INFO_ATTR(lazy_itable_init);
267627ee40dfSLukas Czerner EXT4_INFO_ATTR(batched_discard);
26775e7bbef1STheodore Ts'o EXT4_INFO_ATTR(meta_bg_resize);
2678857ac889SLukas Czerner 
2679857ac889SLukas Czerner static struct attribute *ext4_feat_attrs[] = {
2680857ac889SLukas Czerner 	ATTR_LIST(lazy_itable_init),
268127ee40dfSLukas Czerner 	ATTR_LIST(batched_discard),
26825e7bbef1STheodore Ts'o 	ATTR_LIST(meta_bg_resize),
2683857ac889SLukas Czerner 	NULL,
2684857ac889SLukas Czerner };
2685857ac889SLukas Czerner 
26863197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
26873197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
26883197ebdbSTheodore Ts'o {
26893197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26903197ebdbSTheodore Ts'o 						s_kobj);
26913197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
26923197ebdbSTheodore Ts'o 
26933197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
26943197ebdbSTheodore Ts'o }
26953197ebdbSTheodore Ts'o 
26963197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
26973197ebdbSTheodore Ts'o 			       struct attribute *attr,
26983197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
26993197ebdbSTheodore Ts'o {
27003197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
27013197ebdbSTheodore Ts'o 						s_kobj);
27023197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
27033197ebdbSTheodore Ts'o 
27043197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
27053197ebdbSTheodore Ts'o }
27063197ebdbSTheodore Ts'o 
27073197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
27083197ebdbSTheodore Ts'o {
27093197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
27103197ebdbSTheodore Ts'o 						s_kobj);
27113197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
27123197ebdbSTheodore Ts'o }
27133197ebdbSTheodore Ts'o 
271452cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = {
27153197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
27163197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
27173197ebdbSTheodore Ts'o };
27183197ebdbSTheodore Ts'o 
27193197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
27203197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
27213197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
27223197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
27233197ebdbSTheodore Ts'o };
27243197ebdbSTheodore Ts'o 
2725857ac889SLukas Czerner static void ext4_feat_release(struct kobject *kobj)
2726857ac889SLukas Czerner {
2727857ac889SLukas Czerner 	complete(&ext4_feat->f_kobj_unregister);
2728857ac889SLukas Czerner }
2729857ac889SLukas Czerner 
2730c7f72543SLukas Czerner static ssize_t ext4_feat_show(struct kobject *kobj,
2731c7f72543SLukas Czerner 			      struct attribute *attr, char *buf)
2732c7f72543SLukas Czerner {
2733c7f72543SLukas Czerner 	return snprintf(buf, PAGE_SIZE, "supported\n");
2734c7f72543SLukas Czerner }
2735c7f72543SLukas Czerner 
2736c7f72543SLukas Czerner /*
2737c7f72543SLukas Czerner  * We can not use ext4_attr_show/store because it relies on the kobject
2738c7f72543SLukas Czerner  * being embedded in the ext4_sb_info structure which is definitely not
2739c7f72543SLukas Czerner  * true in this case.
2740c7f72543SLukas Czerner  */
2741c7f72543SLukas Czerner static const struct sysfs_ops ext4_feat_ops = {
2742c7f72543SLukas Czerner 	.show	= ext4_feat_show,
2743c7f72543SLukas Czerner 	.store	= NULL,
2744c7f72543SLukas Czerner };
2745c7f72543SLukas Czerner 
2746857ac889SLukas Czerner static struct kobj_type ext4_feat_ktype = {
2747857ac889SLukas Czerner 	.default_attrs	= ext4_feat_attrs,
2748c7f72543SLukas Czerner 	.sysfs_ops	= &ext4_feat_ops,
2749857ac889SLukas Czerner 	.release	= ext4_feat_release,
2750857ac889SLukas Czerner };
2751857ac889SLukas Czerner 
2752a13fb1a4SEric Sandeen /*
2753a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2754a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2755a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2756a13fb1a4SEric Sandeen  * 0 if it cannot be.
2757a13fb1a4SEric Sandeen  */
2758a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2759a13fb1a4SEric Sandeen {
2760a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2761a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2762a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2763a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2764a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2765a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2766a13fb1a4SEric Sandeen 		return 0;
2767a13fb1a4SEric Sandeen 	}
2768a13fb1a4SEric Sandeen 
2769a13fb1a4SEric Sandeen 	if (readonly)
2770a13fb1a4SEric Sandeen 		return 1;
2771a13fb1a4SEric Sandeen 
2772a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2773a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2774a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2775a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2776a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2777a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2778a13fb1a4SEric Sandeen 		return 0;
2779a13fb1a4SEric Sandeen 	}
2780a13fb1a4SEric Sandeen 	/*
2781a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2782a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2783a13fb1a4SEric Sandeen 	 */
2784a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2785a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2786a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2787a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2788a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2789a13fb1a4SEric Sandeen 			return 0;
2790a13fb1a4SEric Sandeen 		}
2791a13fb1a4SEric Sandeen 	}
2792bab08ab9STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2793bab08ab9STheodore Ts'o 	    !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2794bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2795bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2796bab08ab9STheodore Ts'o 			 "extents feature\n");
2797bab08ab9STheodore Ts'o 		return 0;
2798bab08ab9STheodore Ts'o 	}
27997c319d32SAditya Kali 
28007c319d32SAditya Kali #ifndef CONFIG_QUOTA
28017c319d32SAditya Kali 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
28027c319d32SAditya Kali 	    !readonly) {
28037c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
28047c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
28057c319d32SAditya Kali 			 "without CONFIG_QUOTA");
28067c319d32SAditya Kali 		return 0;
28077c319d32SAditya Kali 	}
28087c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2809a13fb1a4SEric Sandeen 	return 1;
2810a13fb1a4SEric Sandeen }
2811a13fb1a4SEric Sandeen 
281266e61a9eSTheodore Ts'o /*
281366e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
281466e61a9eSTheodore Ts'o  * on the file system
281566e61a9eSTheodore Ts'o  */
281666e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
281766e61a9eSTheodore Ts'o {
281866e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
281966e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
282066e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
282166e61a9eSTheodore Ts'o 
282266e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
282366e61a9eSTheodore Ts'o 	es = sbi->s_es;
282466e61a9eSTheodore Ts'o 
282566e61a9eSTheodore Ts'o 	if (es->s_error_count)
2826ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
2827ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
282866e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
282966e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
2830ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %u: %.*s:%d",
283166e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
283266e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
283366e61a9eSTheodore Ts'o 		       es->s_first_error_func,
283466e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
283566e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
283666e61a9eSTheodore Ts'o 			printk(": inode %u",
283766e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
283866e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
283966e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
284066e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
284166e61a9eSTheodore Ts'o 		printk("\n");
284266e61a9eSTheodore Ts'o 	}
284366e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
2844ae0f78deSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %u: %.*s:%d",
284566e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
284666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
284766e61a9eSTheodore Ts'o 		       es->s_last_error_func,
284866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
284966e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
285066e61a9eSTheodore Ts'o 			printk(": inode %u",
285166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
285266e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
285366e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
285466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
285566e61a9eSTheodore Ts'o 		printk("\n");
285666e61a9eSTheodore Ts'o 	}
285766e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
285866e61a9eSTheodore Ts'o }
285966e61a9eSTheodore Ts'o 
2860bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2861bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2862bfff6873SLukas Czerner {
2863bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2864bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2865bfff6873SLukas Czerner 	struct super_block *sb;
2866bfff6873SLukas Czerner 	unsigned long timeout = 0;
2867bfff6873SLukas Czerner 	int ret = 0;
2868bfff6873SLukas Czerner 
2869bfff6873SLukas Czerner 	sb = elr->lr_super;
2870bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2871bfff6873SLukas Czerner 
28728e8ad8a5SJan Kara 	sb_start_write(sb);
2873bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2874bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2875bfff6873SLukas Czerner 		if (!gdp) {
2876bfff6873SLukas Czerner 			ret = 1;
2877bfff6873SLukas Czerner 			break;
2878bfff6873SLukas Czerner 		}
2879bfff6873SLukas Czerner 
2880bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2881bfff6873SLukas Czerner 			break;
2882bfff6873SLukas Czerner 	}
2883bfff6873SLukas Czerner 
28847f511862STheodore Ts'o 	if (group >= ngroups)
2885bfff6873SLukas Czerner 		ret = 1;
2886bfff6873SLukas Czerner 
2887bfff6873SLukas Czerner 	if (!ret) {
2888bfff6873SLukas Czerner 		timeout = jiffies;
2889bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2890bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2891bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
289251ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
289351ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2894bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2895bfff6873SLukas Czerner 		}
2896bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2897bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2898bfff6873SLukas Czerner 	}
28998e8ad8a5SJan Kara 	sb_end_write(sb);
2900bfff6873SLukas Czerner 
2901bfff6873SLukas Czerner 	return ret;
2902bfff6873SLukas Czerner }
2903bfff6873SLukas Czerner 
2904bfff6873SLukas Czerner /*
2905bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
29064ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2907bfff6873SLukas Czerner  */
2908bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2909bfff6873SLukas Czerner {
2910bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2911bfff6873SLukas Czerner 
2912bfff6873SLukas Czerner 	if (!elr)
2913bfff6873SLukas Czerner 		return;
2914bfff6873SLukas Czerner 
2915bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2916bfff6873SLukas Czerner 
2917bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2918bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2919bfff6873SLukas Czerner 	kfree(elr);
2920bfff6873SLukas Czerner }
2921bfff6873SLukas Czerner 
2922bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2923bfff6873SLukas Czerner {
29241bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
29251bb933fbSLukas Czerner 	if (!ext4_li_info) {
29261bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2927bfff6873SLukas Czerner 		return;
29281bb933fbSLukas Czerner 	}
2929bfff6873SLukas Czerner 
2930bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
29311bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2932bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
29331bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2934bfff6873SLukas Czerner }
2935bfff6873SLukas Czerner 
29368f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
29378f1f7453SEric Sandeen 
2938bfff6873SLukas Czerner /*
2939bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2940bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2941bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2942bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2943bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2944bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2945bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2946bfff6873SLukas Czerner  */
2947bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2948bfff6873SLukas Czerner {
2949bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2950bfff6873SLukas Czerner 	struct list_head *pos, *n;
2951bfff6873SLukas Czerner 	struct ext4_li_request *elr;
29524ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2953bfff6873SLukas Czerner 
2954bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2955bfff6873SLukas Czerner 
2956bfff6873SLukas Czerner cont_thread:
2957bfff6873SLukas Czerner 	while (true) {
2958bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2959bfff6873SLukas Czerner 
2960bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2961bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2962bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2963bfff6873SLukas Czerner 			goto exit_thread;
2964bfff6873SLukas Czerner 		}
2965bfff6873SLukas Czerner 
2966bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2967bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2968bfff6873SLukas Czerner 					 lr_request);
2969bfff6873SLukas Czerner 
2970b2c78cd0STheodore Ts'o 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
2971b2c78cd0STheodore Ts'o 				if (ext4_run_li_request(elr) != 0) {
2972b2c78cd0STheodore Ts'o 					/* error, remove the lazy_init job */
2973bfff6873SLukas Czerner 					ext4_remove_li_request(elr);
2974bfff6873SLukas Czerner 					continue;
2975bfff6873SLukas Czerner 				}
2976b2c78cd0STheodore Ts'o 			}
2977bfff6873SLukas Czerner 
2978bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2979bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2980bfff6873SLukas Czerner 		}
2981bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2982bfff6873SLukas Czerner 
2983a0acae0eSTejun Heo 		try_to_freeze();
2984bfff6873SLukas Czerner 
29854ed5c033SLukas Czerner 		cur = jiffies;
29864ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2987f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2988bfff6873SLukas Czerner 			cond_resched();
2989bfff6873SLukas Czerner 			continue;
2990bfff6873SLukas Czerner 		}
2991bfff6873SLukas Czerner 
29924ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
29934ed5c033SLukas Czerner 
29948f1f7453SEric Sandeen 		if (kthread_should_stop()) {
29958f1f7453SEric Sandeen 			ext4_clear_request_list();
29968f1f7453SEric Sandeen 			goto exit_thread;
29978f1f7453SEric Sandeen 		}
2998bfff6873SLukas Czerner 	}
2999bfff6873SLukas Czerner 
3000bfff6873SLukas Czerner exit_thread:
3001bfff6873SLukas Czerner 	/*
3002bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3003bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3004bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3005bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3006bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3007bfff6873SLukas Czerner 	 * new one.
3008bfff6873SLukas Czerner 	 */
3009bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3010bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3011bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3012bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3013bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3014bfff6873SLukas Czerner 		goto cont_thread;
3015bfff6873SLukas Czerner 	}
3016bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3017bfff6873SLukas Czerner 	kfree(ext4_li_info);
3018bfff6873SLukas Czerner 	ext4_li_info = NULL;
3019bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3020bfff6873SLukas Czerner 
3021bfff6873SLukas Czerner 	return 0;
3022bfff6873SLukas Czerner }
3023bfff6873SLukas Czerner 
3024bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3025bfff6873SLukas Czerner {
3026bfff6873SLukas Czerner 	struct list_head *pos, *n;
3027bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3028bfff6873SLukas Czerner 
3029bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3030bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3031bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3032bfff6873SLukas Czerner 				 lr_request);
3033bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3034bfff6873SLukas Czerner 	}
3035bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3036bfff6873SLukas Czerner }
3037bfff6873SLukas Czerner 
3038bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3039bfff6873SLukas Czerner {
30408f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
30418f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
30428f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
30438f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3044bfff6873SLukas Czerner 		ext4_clear_request_list();
3045bfff6873SLukas Czerner 		kfree(ext4_li_info);
3046bfff6873SLukas Czerner 		ext4_li_info = NULL;
304792b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3048bfff6873SLukas Czerner 				 "initialization thread\n",
3049bfff6873SLukas Czerner 				 err);
3050bfff6873SLukas Czerner 		return err;
3051bfff6873SLukas Czerner 	}
3052bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3053bfff6873SLukas Czerner 	return 0;
3054bfff6873SLukas Czerner }
3055bfff6873SLukas Czerner 
3056bfff6873SLukas Czerner /*
3057bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3058bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3059bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3060bfff6873SLukas Czerner  * groups and return total number of groups.
3061bfff6873SLukas Czerner  */
3062bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3063bfff6873SLukas Czerner {
3064bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3065bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3066bfff6873SLukas Czerner 
3067bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3068bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3069bfff6873SLukas Czerner 		if (!gdp)
3070bfff6873SLukas Czerner 			continue;
3071bfff6873SLukas Czerner 
3072bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3073bfff6873SLukas Czerner 			break;
3074bfff6873SLukas Czerner 	}
3075bfff6873SLukas Czerner 
3076bfff6873SLukas Czerner 	return group;
3077bfff6873SLukas Czerner }
3078bfff6873SLukas Czerner 
3079bfff6873SLukas Czerner static int ext4_li_info_new(void)
3080bfff6873SLukas Czerner {
3081bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3082bfff6873SLukas Czerner 
3083bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3084bfff6873SLukas Czerner 	if (!eli)
3085bfff6873SLukas Czerner 		return -ENOMEM;
3086bfff6873SLukas Czerner 
3087bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3088bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3089bfff6873SLukas Czerner 
3090bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3091bfff6873SLukas Czerner 
3092bfff6873SLukas Czerner 	ext4_li_info = eli;
3093bfff6873SLukas Czerner 
3094bfff6873SLukas Czerner 	return 0;
3095bfff6873SLukas Czerner }
3096bfff6873SLukas Czerner 
3097bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3098bfff6873SLukas Czerner 					    ext4_group_t start)
3099bfff6873SLukas Czerner {
3100bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3101bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3102bfff6873SLukas Czerner 
3103bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3104bfff6873SLukas Czerner 	if (!elr)
3105bfff6873SLukas Czerner 		return NULL;
3106bfff6873SLukas Czerner 
3107bfff6873SLukas Czerner 	elr->lr_super = sb;
3108bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3109bfff6873SLukas Czerner 	elr->lr_next_group = start;
3110bfff6873SLukas Czerner 
3111bfff6873SLukas Czerner 	/*
3112bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3113bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3114bfff6873SLukas Czerner 	 * better.
3115bfff6873SLukas Czerner 	 */
3116dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3117dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3118bfff6873SLukas Czerner 	return elr;
3119bfff6873SLukas Czerner }
3120bfff6873SLukas Czerner 
31217f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3122bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3123bfff6873SLukas Czerner {
3124bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
31257f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
3126bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
31276c5a6cb9SAndrew Morton 	int ret = 0;
3128bfff6873SLukas Czerner 
31297f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
313051ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
313151ce6511SLukas Czerner 		/*
313251ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
313351ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
313451ce6511SLukas Czerner 		 */
313551ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
31367f511862STheodore Ts'o 		goto out;
313751ce6511SLukas Czerner 	}
3138bfff6873SLukas Czerner 
3139bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
3140bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
314155ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
31427f511862STheodore Ts'o 		goto out;
3143bfff6873SLukas Czerner 
3144bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
31457f511862STheodore Ts'o 	if (!elr) {
31467f511862STheodore Ts'o 		ret = -ENOMEM;
31477f511862STheodore Ts'o 		goto out;
31487f511862STheodore Ts'o 	}
3149bfff6873SLukas Czerner 
3150bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3151bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3152bfff6873SLukas Czerner 		if (ret)
3153bfff6873SLukas Czerner 			goto out;
3154bfff6873SLukas Czerner 	}
3155bfff6873SLukas Czerner 
3156bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3157bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3158bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3159bfff6873SLukas Czerner 
3160bfff6873SLukas Czerner 	sbi->s_li_request = elr;
316146e4690bSTao Ma 	/*
316246e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
316346e4690bSTao Ma 	 * the request_list and the removal and free of it is
316446e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
316546e4690bSTao Ma 	 */
316646e4690bSTao Ma 	elr = NULL;
3167bfff6873SLukas Czerner 
3168bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3169bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3170bfff6873SLukas Czerner 		if (ret)
3171bfff6873SLukas Czerner 			goto out;
3172bfff6873SLukas Czerner 	}
3173bfff6873SLukas Czerner out:
3174bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3175beed5ecbSNicolas Kaiser 	if (ret)
3176bfff6873SLukas Czerner 		kfree(elr);
3177bfff6873SLukas Czerner 	return ret;
3178bfff6873SLukas Czerner }
3179bfff6873SLukas Czerner 
3180bfff6873SLukas Czerner /*
3181bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3182bfff6873SLukas Czerner  * module unload.
3183bfff6873SLukas Czerner  */
3184bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3185bfff6873SLukas Czerner {
3186bfff6873SLukas Czerner 	/*
3187bfff6873SLukas Czerner 	 * If thread exited earlier
3188bfff6873SLukas Czerner 	 * there's nothing to be done.
3189bfff6873SLukas Czerner 	 */
31908f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3191bfff6873SLukas Czerner 		return;
3192bfff6873SLukas Czerner 
31938f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3194bfff6873SLukas Czerner }
3195bfff6873SLukas Czerner 
319625ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
319725ed6e8aSDarrick J. Wong {
319825ed6e8aSDarrick J. Wong 	int ret = 1;
319925ed6e8aSDarrick J. Wong 	int compat, incompat;
320025ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
320125ed6e8aSDarrick J. Wong 
32029aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3203db9ee220SDarrick J. Wong 		/* journal checksum v3 */
320425ed6e8aSDarrick J. Wong 		compat = 0;
3205db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
320625ed6e8aSDarrick J. Wong 	} else {
320725ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
320825ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
320925ed6e8aSDarrick J. Wong 		incompat = 0;
321025ed6e8aSDarrick J. Wong 	}
321125ed6e8aSDarrick J. Wong 
3212feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3213feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3214feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3215feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
321625ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
321725ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
321825ed6e8aSDarrick J. Wong 				compat, 0,
321925ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
322025ed6e8aSDarrick J. Wong 				incompat);
322125ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
322225ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
322325ed6e8aSDarrick J. Wong 				compat, 0,
322425ed6e8aSDarrick J. Wong 				incompat);
322525ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
322625ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
322725ed6e8aSDarrick J. Wong 	} else {
3228feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3229feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
323025ed6e8aSDarrick J. Wong 	}
323125ed6e8aSDarrick J. Wong 
323225ed6e8aSDarrick J. Wong 	return ret;
323325ed6e8aSDarrick J. Wong }
323425ed6e8aSDarrick J. Wong 
3235952fc18eSTheodore Ts'o /*
3236952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3237952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3238952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3239952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3240952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3241952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3242952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3243952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3244952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3245952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3246952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3247952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3248952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3249952fc18eSTheodore Ts'o  */
3250952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3251952fc18eSTheodore Ts'o 			  char *buf)
3252952fc18eSTheodore Ts'o {
3253952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3254952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3255952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3256952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3257952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3258952fc18eSTheodore Ts'o 
32590548bbb8STheodore Ts'o 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC))
32600548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
32610548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
32620548bbb8STheodore Ts'o 
3263952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3264952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3265952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3266952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3267952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3268952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3269952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3270952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3271952fc18eSTheodore Ts'o 			count++;
3272952fc18eSTheodore Ts'o 		}
3273952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3274952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3275952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3276952fc18eSTheodore Ts'o 			count++;
3277952fc18eSTheodore Ts'o 		}
3278952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3279952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3280952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3281952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3282952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3283952fc18eSTheodore Ts'o 				count++;
3284952fc18eSTheodore Ts'o 			}
3285952fc18eSTheodore Ts'o 		if (i != grp)
3286952fc18eSTheodore Ts'o 			continue;
3287952fc18eSTheodore Ts'o 		s = 0;
3288952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3289952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3290952fc18eSTheodore Ts'o 			count++;
3291952fc18eSTheodore Ts'o 		}
3292952fc18eSTheodore Ts'o 		for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) {
3293952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3294952fc18eSTheodore Ts'o 			count++;
3295952fc18eSTheodore Ts'o 		}
3296952fc18eSTheodore Ts'o 	}
3297952fc18eSTheodore Ts'o 	if (!count)
3298952fc18eSTheodore Ts'o 		return 0;
3299952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3300952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3301952fc18eSTheodore Ts'o }
3302952fc18eSTheodore Ts'o 
3303952fc18eSTheodore Ts'o /*
3304952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3305952fc18eSTheodore Ts'o  */
3306952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3307952fc18eSTheodore Ts'o {
3308952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3309952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
3310952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3311952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
33124fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3313952fc18eSTheodore Ts'o 
3314952fc18eSTheodore Ts'o 	if (!buf)
3315952fc18eSTheodore Ts'o 		return -ENOMEM;
3316952fc18eSTheodore Ts'o 
3317952fc18eSTheodore Ts'o 	/*
3318952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3319952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3320952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3321952fc18eSTheodore Ts'o 	 */
3322952fc18eSTheodore Ts'o 
3323952fc18eSTheodore Ts'o 	/*
3324952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3325952fc18eSTheodore Ts'o 	 */
3326952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3327952fc18eSTheodore Ts'o 
3328952fc18eSTheodore Ts'o 	/*
3329952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3330952fc18eSTheodore Ts'o 	 */
3331952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3332952fc18eSTheodore Ts'o 		int blks;
3333952fc18eSTheodore Ts'o 
3334952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3335952fc18eSTheodore Ts'o 		overhead += blks;
3336952fc18eSTheodore Ts'o 		if (blks)
3337952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3338952fc18eSTheodore Ts'o 		cond_resched();
3339952fc18eSTheodore Ts'o 	}
3340b003b524SEric Sandeen 	/* Add the internal journal blocks as well */
3341b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3342810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
33430875a2b4SEric Sandeen 
3344952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3345952fc18eSTheodore Ts'o 	smp_wmb();
3346952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3347952fc18eSTheodore Ts'o 	return 0;
3348952fc18eSTheodore Ts'o }
3349952fc18eSTheodore Ts'o 
335027dd4385SLukas Czerner 
335130fac0f7SJan Kara static ext4_fsblk_t ext4_calculate_resv_clusters(struct super_block *sb)
335227dd4385SLukas Czerner {
335327dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
335427dd4385SLukas Czerner 
335527dd4385SLukas Czerner 	/*
335630fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
335730fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
335830fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
335930fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
336030fac0f7SJan Kara 	 */
336130fac0f7SJan Kara 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
336230fac0f7SJan Kara 		return 0;
336330fac0f7SJan Kara 	/*
336427dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
336527dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
336627dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3367556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
336827dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
336927dd4385SLukas Czerner 	 * very rare.
337027dd4385SLukas Czerner 	 */
337130fac0f7SJan Kara 	resv_clusters = ext4_blocks_count(EXT4_SB(sb)->s_es) >>
337230fac0f7SJan Kara 			EXT4_SB(sb)->s_cluster_bits;
337327dd4385SLukas Czerner 
337427dd4385SLukas Czerner 	do_div(resv_clusters, 50);
337527dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
337627dd4385SLukas Czerner 
337727dd4385SLukas Czerner 	return resv_clusters;
337827dd4385SLukas Czerner }
337927dd4385SLukas Czerner 
338027dd4385SLukas Czerner 
338127dd4385SLukas Czerner static int ext4_reserve_clusters(struct ext4_sb_info *sbi, ext4_fsblk_t count)
338227dd4385SLukas Czerner {
338327dd4385SLukas Czerner 	ext4_fsblk_t clusters = ext4_blocks_count(sbi->s_es) >>
338427dd4385SLukas Czerner 				sbi->s_cluster_bits;
338527dd4385SLukas Czerner 
338627dd4385SLukas Czerner 	if (count >= clusters)
338727dd4385SLukas Czerner 		return -EINVAL;
338827dd4385SLukas Czerner 
338927dd4385SLukas Czerner 	atomic64_set(&sbi->s_resv_clusters, count);
339027dd4385SLukas Czerner 	return 0;
339127dd4385SLukas Czerner }
339227dd4385SLukas Czerner 
3393617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3394ac27a0ecSDave Kleikamp {
3395d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3396ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3397617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
3398617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
3399617ba13bSMingming Cao 	ext4_fsblk_t block;
3400617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
340170bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3402ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3403ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3404ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3405ac27a0ecSDave Kleikamp 	struct inode *root;
34069f6200bbSTheodore Ts'o 	char *cp;
34070390131bSFrank Mayhar 	const char *descr;
3408dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3409281b5995STheodore Ts'o 	int blocksize, clustersize;
34104ec11028STheodore Ts'o 	unsigned int db_count;
34114ec11028STheodore Ts'o 	unsigned int i;
3412281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3413bd81d8eeSLaurent Vivier 	__u64 blocks_count;
341407aa2ea1SLukas Czerner 	int err = 0;
3415b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3416bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3417ac27a0ecSDave Kleikamp 
3418ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3419ac27a0ecSDave Kleikamp 	if (!sbi)
3420dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3421705895b6SPekka Enberg 
3422705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3423705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3424705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
3425705895b6SPekka Enberg 		kfree(sbi);
3426dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3427705895b6SPekka Enberg 	}
3428ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
34292c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3430240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3431d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3432f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3433f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3434f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3435ac27a0ecSDave Kleikamp 
34369f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
34379f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
34389f6200bbSTheodore Ts'o 		*cp = '!';
34399f6200bbSTheodore Ts'o 
344007aa2ea1SLukas Czerner 	/* -EINVAL is default */
3441dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3442617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3443ac27a0ecSDave Kleikamp 	if (!blocksize) {
3444b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3445ac27a0ecSDave Kleikamp 		goto out_fail;
3446ac27a0ecSDave Kleikamp 	}
3447ac27a0ecSDave Kleikamp 
3448ac27a0ecSDave Kleikamp 	/*
3449617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3450ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3451ac27a0ecSDave Kleikamp 	 */
3452617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
345370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
345470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3455ac27a0ecSDave Kleikamp 	} else {
345670bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3457ac27a0ecSDave Kleikamp 	}
3458ac27a0ecSDave Kleikamp 
3459a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3460b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3461ac27a0ecSDave Kleikamp 		goto out_fail;
3462ac27a0ecSDave Kleikamp 	}
3463ac27a0ecSDave Kleikamp 	/*
3464ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3465617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3466ac27a0ecSDave Kleikamp 	 */
34672716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3468ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3469ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3470617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3471617ba13bSMingming Cao 		goto cantfind_ext4;
3472afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3473ac27a0ecSDave Kleikamp 
3474feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3475feb0ab32SDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3476feb0ab32SDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
3477feb0ab32SDarrick J. Wong 	    EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
3478*363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3479feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3480feb0ab32SDarrick J. Wong 
3481d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3482d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3483d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3484d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3485d25425f8SDarrick J. Wong 		silent = 1;
3486d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3487d25425f8SDarrick J. Wong 	}
3488d25425f8SDarrick J. Wong 
34890441984aSDarrick J. Wong 	/* Load the checksum driver */
34900441984aSDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
34910441984aSDarrick J. Wong 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
34920441984aSDarrick J. Wong 		sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
34930441984aSDarrick J. Wong 		if (IS_ERR(sbi->s_chksum_driver)) {
34940441984aSDarrick J. Wong 			ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
34950441984aSDarrick J. Wong 			ret = PTR_ERR(sbi->s_chksum_driver);
34960441984aSDarrick J. Wong 			sbi->s_chksum_driver = NULL;
34970441984aSDarrick J. Wong 			goto failed_mount;
34980441984aSDarrick J. Wong 		}
34990441984aSDarrick J. Wong 	}
35000441984aSDarrick J. Wong 
3501a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3502a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3503a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3504a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3505a9c47317SDarrick J. Wong 		silent = 1;
3506a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3507a9c47317SDarrick J. Wong 	}
3508a9c47317SDarrick J. Wong 
3509a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
35109aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb))
3511a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3512a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3513a9c47317SDarrick J. Wong 
3514ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3515ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3516fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3517617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3518fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
351987f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3520fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3521617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3522fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3523ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3524fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
352503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3526fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
35272e7842b8SHugh Dickins #endif
352898c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
352998c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
353098c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
353198c1a759SDarrick J. Wong 
3532617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3533fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3534617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3535fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3536617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3537fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3538ac27a0ecSDave Kleikamp 
3539617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3540fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3541bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3542fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3543bb4f397aSAneesh Kumar K.V 	else
3544fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
354545f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3546fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
35478b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3548fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3549ac27a0ecSDave Kleikamp 
355008cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
355108cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
355230773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
355330773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
355430773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3555ac27a0ecSDave Kleikamp 
35568b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3557fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3558ac27a0ecSDave Kleikamp 
35591e2462f9SMingming Cao 	/*
3560dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3561dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3562dd919b98SAneesh Kumar K.V 	 */
3563bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
35648b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3565fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3566dd919b98SAneesh Kumar K.V 
356751ce6511SLukas Czerner 	/*
356851ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
356951ce6511SLukas Czerner 	 * no mount option specified.
357051ce6511SLukas Czerner 	 */
357151ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
357251ce6511SLukas Czerner 
35738b67f04aSTheodore Ts'o 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
3574661aa520SEric Sandeen 			   &journal_devnum, &journal_ioprio, 0)) {
35758b67f04aSTheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
35768b67f04aSTheodore Ts'o 			 "failed to parse options in superblock: %s",
35778b67f04aSTheodore Ts'o 			 sbi->s_es->s_mount_opts);
35788b67f04aSTheodore Ts'o 	}
35795a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3580b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3581661aa520SEric Sandeen 			   &journal_ioprio, 0))
3582ac27a0ecSDave Kleikamp 		goto failed_mount;
3583ac27a0ecSDave Kleikamp 
358456889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
358556889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
358656889787STheodore Ts'o 			    "with data=journal disables delayed "
358756889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
358856889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
358956889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
359056889787STheodore Ts'o 				 "both data=journal and delalloc");
359156889787STheodore Ts'o 			goto failed_mount;
359256889787STheodore Ts'o 		}
359356889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
359456889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
35956ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
359656889787STheodore Ts'o 			goto failed_mount;
359756889787STheodore Ts'o 		}
359856889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
359956889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
360056889787STheodore Ts'o 	}
360156889787STheodore Ts'o 
3602ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3603482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3604ac27a0ecSDave Kleikamp 
3605617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3606617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3607617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3608617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
3609b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3610b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3611b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3612469108ffSTheodore Tso 
3613ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3614ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3615ed3654ebSTheodore Ts'o 		if (EXT4_HAS_INCOMPAT_FEATURE(sb,
3616ed3654ebSTheodore Ts'o 					      EXT4_FEATURE_INCOMPAT_64BIT)) {
3617ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3618ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3619ed3654ebSTheodore Ts'o 			goto failed_mount;
3620ed3654ebSTheodore Ts'o 		}
3621ed3654ebSTheodore Ts'o 	}
3622ed3654ebSTheodore Ts'o 
36232035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
36242035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
36252035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
36262035e776STheodore Ts'o 				 "using the ext4 subsystem");
36272035e776STheodore Ts'o 		else {
36282035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
36292035e776STheodore Ts'o 				 "to feature incompatibilities");
36302035e776STheodore Ts'o 			goto failed_mount;
36312035e776STheodore Ts'o 		}
36322035e776STheodore Ts'o 	}
36332035e776STheodore Ts'o 
36342035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
36352035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
36362035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
36372035e776STheodore Ts'o 				 "using the ext4 subsystem");
36382035e776STheodore Ts'o 		else {
36392035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
36402035e776STheodore Ts'o 				 "to feature incompatibilities");
36412035e776STheodore Ts'o 			goto failed_mount;
36422035e776STheodore Ts'o 		}
36432035e776STheodore Ts'o 	}
36442035e776STheodore Ts'o 
3645469108ffSTheodore Tso 	/*
3646ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3647ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3648ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3649ac27a0ecSDave Kleikamp 	 */
3650a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3651ac27a0ecSDave Kleikamp 		goto failed_mount;
3652a13fb1a4SEric Sandeen 
3653261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3654617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3655617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3656b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3657b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
3658ac27a0ecSDave Kleikamp 		goto failed_mount;
3659ac27a0ecSDave Kleikamp 	}
3660ac27a0ecSDave Kleikamp 
3661ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3662ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3663ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3664b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3665ce40733cSAneesh Kumar K.V 					blocksize);
3666ac27a0ecSDave Kleikamp 			goto failed_mount;
3667ac27a0ecSDave Kleikamp 		}
3668ac27a0ecSDave Kleikamp 
3669ac27a0ecSDave Kleikamp 		brelse(bh);
367070bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
367170bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3672a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
3673ac27a0ecSDave Kleikamp 		if (!bh) {
3674b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3675b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3676ac27a0ecSDave Kleikamp 			goto failed_mount;
3677ac27a0ecSDave Kleikamp 		}
36782716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
3679ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3680617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3681b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3682b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3683ac27a0ecSDave Kleikamp 			goto failed_mount;
3684ac27a0ecSDave Kleikamp 		}
3685ac27a0ecSDave Kleikamp 	}
3686ac27a0ecSDave Kleikamp 
3687a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3688a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
3689f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3690f287a1a5STheodore Ts'o 						      has_huge_files);
3691f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3692ac27a0ecSDave Kleikamp 
3693617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3694617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3695617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3696ac27a0ecSDave Kleikamp 	} else {
3697ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3698ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3699617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
37001330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3701ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3702b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3703b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3704ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3705ac27a0ecSDave Kleikamp 			goto failed_mount;
3706ac27a0ecSDave Kleikamp 		}
3707ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3708ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3709ac27a0ecSDave Kleikamp 	}
37100b8e58a1SAndreas Dilger 
37110d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
37120d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
37138fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
37140d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3715d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3716b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3717b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
37180d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
37190d1ee42fSAlexandre Ratchov 			goto failed_mount;
37200d1ee42fSAlexandre Ratchov 		}
37210d1ee42fSAlexandre Ratchov 	} else
37220d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
37230b8e58a1SAndreas Dilger 
3724ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3725ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3726b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
3727617ba13bSMingming Cao 		goto cantfind_ext4;
37280b8e58a1SAndreas Dilger 
3729617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3730ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3731617ba13bSMingming Cao 		goto cantfind_ext4;
3732ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3733ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
37340d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3735ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3736ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3737e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3738e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
37390b8e58a1SAndreas Dilger 
3740ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3741ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3742ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
374323301410STheodore Ts'o 	if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
3744f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
3745f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
3746f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3747f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3748f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
374923301410STheodore Ts'o 			if (!(sb->s_flags & MS_RDONLY))
375023301410STheodore Ts'o 				es->s_flags |=
375123301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3752f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
3753f99b2589STheodore Ts'o #else
375423301410STheodore Ts'o 			if (!(sb->s_flags & MS_RDONLY))
375523301410STheodore Ts'o 				es->s_flags |=
375623301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3757f99b2589STheodore Ts'o #endif
3758f99b2589STheodore Ts'o 		}
375923301410STheodore Ts'o 	}
3760ac27a0ecSDave Kleikamp 
3761281b5995STheodore Ts'o 	/* Handle clustersize */
3762281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3763281b5995STheodore Ts'o 	has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3764281b5995STheodore Ts'o 				EXT4_FEATURE_RO_COMPAT_BIGALLOC);
3765281b5995STheodore Ts'o 	if (has_bigalloc) {
3766281b5995STheodore Ts'o 		if (clustersize < blocksize) {
3767281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3768281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
3769281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
3770281b5995STheodore Ts'o 			goto failed_mount;
3771281b5995STheodore Ts'o 		}
3772281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3773281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
3774281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
3775281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
3776281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
3777281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3778281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
3779281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3780281b5995STheodore Ts'o 			goto failed_mount;
3781281b5995STheodore Ts'o 		}
3782281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
3783281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3784281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3785281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
3786281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
3787281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3788281b5995STheodore Ts'o 			goto failed_mount;
3789281b5995STheodore Ts'o 		}
3790281b5995STheodore Ts'o 	} else {
3791281b5995STheodore Ts'o 		if (clustersize != blocksize) {
3792281b5995STheodore Ts'o 			ext4_warning(sb, "fragment/cluster size (%d) != "
3793281b5995STheodore Ts'o 				     "block size (%d)", clustersize,
3794281b5995STheodore Ts'o 				     blocksize);
3795281b5995STheodore Ts'o 			clustersize = blocksize;
3796281b5995STheodore Ts'o 		}
3797ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
3798b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3799b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
3800ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
3801ac27a0ecSDave Kleikamp 			goto failed_mount;
3802ac27a0ecSDave Kleikamp 		}
3803281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3804281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
3805281b5995STheodore Ts'o 	}
3806281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
3807281b5995STheodore Ts'o 
3808ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
3809b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3810b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
3811ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
3812ac27a0ecSDave Kleikamp 		goto failed_mount;
3813ac27a0ecSDave Kleikamp 	}
3814ac27a0ecSDave Kleikamp 
3815960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
3816960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
3817960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
3818960fd856STheodore Ts'o 
3819bf43d84bSEric Sandeen 	/*
3820bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3821bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3822bf43d84bSEric Sandeen 	 */
38235a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
382430ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
38255a9ae68aSDarrick J. Wong 	if (err) {
3826b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3827bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3828ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
382990c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
3830ac27a0ecSDave Kleikamp 		goto failed_mount;
3831ac27a0ecSDave Kleikamp 	}
3832ac27a0ecSDave Kleikamp 
3833617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3834617ba13bSMingming Cao 		goto cantfind_ext4;
3835e7c95593SEric Sandeen 
38360f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
38370f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
38380f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3839b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3840b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
38410f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
38420f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
38430f2ddca6SFrom: Thiemo Nagel 	}
38440f2ddca6SFrom: Thiemo Nagel 
38454ec11028STheodore Ts'o 	/*
38464ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
38474ec11028STheodore Ts'o 	 * of the filesystem.
38484ec11028STheodore Ts'o 	 */
38494ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3850b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
3851b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3852e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
38534ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3854e7c95593SEric Sandeen 		goto failed_mount;
3855e7c95593SEric Sandeen 	}
3856bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3857bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3858bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3859bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
38604ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3861b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
38624ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3863b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
38644ec11028STheodore Ts'o 		       ext4_blocks_count(es),
38654ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
38664ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
38674ec11028STheodore Ts'o 		goto failed_mount;
38684ec11028STheodore Ts'o 	}
3869bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3870fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3871fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3872617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3873617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3874f18a5f21STheodore Ts'o 	sbi->s_group_desc = ext4_kvmalloc(db_count *
3875f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
3876ac27a0ecSDave Kleikamp 					  GFP_KERNEL);
3877ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3878b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
38792cde417dSTheodore Ts'o 		ret = -ENOMEM;
3880ac27a0ecSDave Kleikamp 		goto failed_mount;
3881ac27a0ecSDave Kleikamp 	}
3882ac27a0ecSDave Kleikamp 
38839f6200bbSTheodore Ts'o 	if (ext4_proc_root)
38849f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
3885240799cdSTheodore Ts'o 
388666acdcf4STheodore Ts'o 	if (sbi->s_proc)
388766acdcf4STheodore Ts'o 		proc_create_data("options", S_IRUGO, sbi->s_proc,
388866acdcf4STheodore Ts'o 				 &ext4_seq_options_fops, sb);
388966acdcf4STheodore Ts'o 
3890705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3891ac27a0ecSDave Kleikamp 
3892ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
389370bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3894a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
3895ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3896b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3897b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3898ac27a0ecSDave Kleikamp 			db_count = i;
3899ac27a0ecSDave Kleikamp 			goto failed_mount2;
3900ac27a0ecSDave Kleikamp 		}
3901ac27a0ecSDave Kleikamp 	}
3902bfff6873SLukas Czerner 	if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
3903b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
3904f9ae9cf5STheodore Ts'o 		goto failed_mount2;
3905ac27a0ecSDave Kleikamp 	}
3906772cb7c8SJose R. Santos 
3907f9ae9cf5STheodore Ts'o 	sbi->s_gdb_count = db_count;
3908ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3909ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3910ac27a0ecSDave Kleikamp 
391104496411STao Ma 	init_timer(&sbi->s_err_report);
391204496411STao Ma 	sbi->s_err_report.function = print_daily_error_info;
391304496411STao Ma 	sbi->s_err_report.data = (unsigned long) sb;
391404496411STao Ma 
3915a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
3916eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
3917ce7e010aSTheodore Ts'o 		goto failed_mount3;
3918ce7e010aSTheodore Ts'o 
3919c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
392067a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
3921c9de560dSAlex Tomas 
3922f9ae9cf5STheodore Ts'o 	/*
3923f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
3924f9ae9cf5STheodore Ts'o 	 */
3925f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
3926617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3927617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3928ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3929617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
3930262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
39317c319d32SAditya Kali 		sb->s_qcop = &ext4_qctl_sysfile_operations;
3932262b4662SJan Kara 	else
3933262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
3934ac27a0ecSDave Kleikamp #endif
3935f2fa2ffcSAneesh Kumar K.V 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3936f2fa2ffcSAneesh Kumar K.V 
3937ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
39383b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
3939ac27a0ecSDave Kleikamp 
3940ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3941ac27a0ecSDave Kleikamp 
3942ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3943617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
3944617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
3945ac27a0ecSDave Kleikamp 
3946c5e06d10SJohann Lombardi 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
3947c5e06d10SJohann Lombardi 	    !(sb->s_flags & MS_RDONLY))
3948c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
394950460fe8SDarrick J. Wong 			goto failed_mount3a;
3950c5e06d10SJohann Lombardi 
3951ac27a0ecSDave Kleikamp 	/*
3952ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3953ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3954ac27a0ecSDave Kleikamp 	 */
3955ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
3956617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3957617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
395850460fe8SDarrick J. Wong 			goto failed_mount3a;
39590390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
39600390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3961b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3962b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3963744692dcSJiaying Zhang 		goto failed_mount_wq;
3964ac27a0ecSDave Kleikamp 	} else {
3965fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
39660390131bSFrank Mayhar 		sbi->s_journal = NULL;
39670390131bSFrank Mayhar 		needs_recovery = 0;
39680390131bSFrank Mayhar 		goto no_journal;
3969ac27a0ecSDave Kleikamp 	}
3970ac27a0ecSDave Kleikamp 
3971f32aaf2dSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT) &&
3972eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3973eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3974b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3975744692dcSJiaying Zhang 		goto failed_mount_wq;
3976eb40a09cSJose R. Santos 	}
3977eb40a09cSJose R. Santos 
397825ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
397925ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
398025ed6e8aSDarrick J. Wong 			 "feature set");
398125ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
3982d4da6c9cSLinus Torvalds 	}
3983818d276cSGirish Shilamkar 
3984ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3985ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3986ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3987ac27a0ecSDave Kleikamp 	case 0:
3988ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
398963f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
399063f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
399163f57933SAndrew Morton 		 */
3992dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3993dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3994fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3995ac27a0ecSDave Kleikamp 		else
3996fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3997ac27a0ecSDave Kleikamp 		break;
3998ac27a0ecSDave Kleikamp 
3999617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4000617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4001dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4002dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4003b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4004b31e1552SEric Sandeen 			       "requested data journaling mode");
4005744692dcSJiaying Zhang 			goto failed_mount_wq;
4006ac27a0ecSDave Kleikamp 		}
4007ac27a0ecSDave Kleikamp 	default:
4008ac27a0ecSDave Kleikamp 		break;
4009ac27a0ecSDave Kleikamp 	}
4010b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4011ac27a0ecSDave Kleikamp 
401218aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
401318aadd47SBobi Jam 
4014ce7e010aSTheodore Ts'o no_journal:
40159c191f70ST Makphaibulchoke 	if (ext4_mballoc_ready) {
40169c191f70ST Makphaibulchoke 		sbi->s_mb_cache = ext4_xattr_create_cache(sb->s_id);
40179c191f70ST Makphaibulchoke 		if (!sbi->s_mb_cache) {
40189c191f70ST Makphaibulchoke 			ext4_msg(sb, KERN_ERR, "Failed to create an mb_cache");
40199c191f70ST Makphaibulchoke 			goto failed_mount_wq;
40209c191f70ST Makphaibulchoke 		}
40219c191f70ST Makphaibulchoke 	}
40229c191f70ST Makphaibulchoke 
4023fd89d5f2STejun Heo 	/*
4024952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4025952fc18eSTheodore Ts'o 	 * superblock if present.
4026952fc18eSTheodore Ts'o 	 */
4027952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4028952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4029952fc18eSTheodore Ts'o 	else {
403007aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
403107aa2ea1SLukas Czerner 		if (err)
4032952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4033952fc18eSTheodore Ts'o 	}
4034952fc18eSTheodore Ts'o 
4035952fc18eSTheodore Ts'o 	/*
4036fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4037fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4038fd89d5f2STejun Heo 	 */
40392e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
40402e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
40412e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
40422e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
404307aa2ea1SLukas Czerner 		ret = -ENOMEM;
40442e8fa54eSJan Kara 		goto failed_mount4;
40452e8fa54eSJan Kara 	}
40462e8fa54eSJan Kara 
4047ac27a0ecSDave Kleikamp 	/*
4048dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4049ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4050ac27a0ecSDave Kleikamp 	 */
4051ac27a0ecSDave Kleikamp 
40521d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
40531d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4054b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
40551d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
405632a9bb57SManish Katiyar 		root = NULL;
4057ac27a0ecSDave Kleikamp 		goto failed_mount4;
4058ac27a0ecSDave Kleikamp 	}
4059ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4060b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
406194bf608aSAl Viro 		iput(root);
4062ac27a0ecSDave Kleikamp 		goto failed_mount4;
4063ac27a0ecSDave Kleikamp 	}
406448fde701SAl Viro 	sb->s_root = d_make_root(root);
40651d1fe1eeSDavid Howells 	if (!sb->s_root) {
4066b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
40671d1fe1eeSDavid Howells 		ret = -ENOMEM;
40681d1fe1eeSDavid Howells 		goto failed_mount4;
40691d1fe1eeSDavid Howells 	}
4070ac27a0ecSDave Kleikamp 
40717e84b621SEric Sandeen 	if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
40727e84b621SEric Sandeen 		sb->s_flags |= MS_RDONLY;
4073ef7f3835SKalpak Shah 
4074ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
4075ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4076ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4077ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
4078ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4079ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
4080ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4081ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
4082ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4083ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
4084ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
4085ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
4086ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
4087ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
4088ef7f3835SKalpak Shah 		}
4089ef7f3835SKalpak Shah 	}
4090ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
4091ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
4092ef7f3835SKalpak Shah 							sbi->s_inode_size) {
4093ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4094ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
4095b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
4096b31e1552SEric Sandeen 			 "available");
4097ef7f3835SKalpak Shah 	}
4098ef7f3835SKalpak Shah 
409930fac0f7SJan Kara 	err = ext4_reserve_clusters(sbi, ext4_calculate_resv_clusters(sb));
410027dd4385SLukas Czerner 	if (err) {
410127dd4385SLukas Czerner 		ext4_msg(sb, KERN_ERR, "failed to reserve %llu clusters for "
410230fac0f7SJan Kara 			 "reserved pool", ext4_calculate_resv_clusters(sb));
4103f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
410427dd4385SLukas Czerner 	}
410527dd4385SLukas Czerner 
41066fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
41076fd058f7STheodore Ts'o 	if (err) {
4108b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4109fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4110f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4111f9ae9cf5STheodore Ts'o 	}
4112f9ae9cf5STheodore Ts'o 
4113f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4114f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4115f9ae9cf5STheodore Ts'o 	if (err) {
4116f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4117f9ae9cf5STheodore Ts'o 			 err);
4118dcf2d804STao Ma 		goto failed_mount5;
4119c2774d84SAneesh Kumar K.V 	}
4120c2774d84SAneesh Kumar K.V 
4121d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4122d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4123d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
4124908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4125908c7f19STejun Heo 				  GFP_KERNEL);
4126d5e03cbbSTheodore Ts'o 	if (!err) {
4127d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4128d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
4129908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4130908c7f19STejun Heo 					  GFP_KERNEL);
4131d5e03cbbSTheodore Ts'o 	}
4132d5e03cbbSTheodore Ts'o 	if (!err)
4133d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4134908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4135d5e03cbbSTheodore Ts'o 	if (!err)
4136908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4137908c7f19STejun Heo 					  GFP_KERNEL);
4138d5e03cbbSTheodore Ts'o 	if (err) {
4139d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4140d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4141d5e03cbbSTheodore Ts'o 	}
4142d5e03cbbSTheodore Ts'o 
4143d5e03cbbSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
4144d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4145d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4146d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4147d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4148d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4149d5e03cbbSTheodore Ts'o 		}
4150d5e03cbbSTheodore Ts'o 
4151bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4152bfff6873SLukas Czerner 	if (err)
4153dcf2d804STao Ma 		goto failed_mount6;
4154bfff6873SLukas Czerner 
41553197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
41563197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
41573197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
41583197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
4159dcf2d804STao Ma 	if (err)
4160dcf2d804STao Ma 		goto failed_mount7;
41613197ebdbSTheodore Ts'o 
41629b2ff357SJan Kara #ifdef CONFIG_QUOTA
41639b2ff357SJan Kara 	/* Enable quota usage during mount. */
41649b2ff357SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
41659b2ff357SJan Kara 	    !(sb->s_flags & MS_RDONLY)) {
41669b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
41679b2ff357SJan Kara 		if (err)
41689b2ff357SJan Kara 			goto failed_mount8;
41699b2ff357SJan Kara 	}
41709b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
41719b2ff357SJan Kara 
4172617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4173617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4174617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
41750390131bSFrank Mayhar 	if (needs_recovery) {
4176b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4177617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
41780390131bSFrank Mayhar 	}
41790390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
41800390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
41810390131bSFrank Mayhar 			descr = " journalled data mode";
41820390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
41830390131bSFrank Mayhar 			descr = " ordered data mode";
41840390131bSFrank Mayhar 		else
41850390131bSFrank Mayhar 			descr = " writeback data mode";
41860390131bSFrank Mayhar 	} else
41870390131bSFrank Mayhar 		descr = "out journal";
41880390131bSFrank Mayhar 
418979add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
419079add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
419179add3a3SLukas Czerner 		if (!blk_queue_discard(q))
419279add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
419379add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
419479add3a3SLukas Czerner 				 "the device does not support discard");
419579add3a3SLukas Czerner 	}
419679add3a3SLukas Czerner 
4197d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
41988b67f04aSTheodore Ts'o 		 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
41998b67f04aSTheodore Ts'o 		 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4200ac27a0ecSDave Kleikamp 
420166e61a9eSTheodore Ts'o 	if (es->s_error_count)
420266e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4203ac27a0ecSDave Kleikamp 
4204efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4205efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4206efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4207efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4208efbed4dcSTheodore Ts'o 
4209d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4210ac27a0ecSDave Kleikamp 	return 0;
4211ac27a0ecSDave Kleikamp 
4212617ba13bSMingming Cao cantfind_ext4:
4213ac27a0ecSDave Kleikamp 	if (!silent)
4214b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4215ac27a0ecSDave Kleikamp 	goto failed_mount;
4216ac27a0ecSDave Kleikamp 
421772ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
421872ba7450STheodore Ts'o failed_mount8:
421972ba7450STheodore Ts'o 	kobject_del(&sbi->s_kobj);
422072ba7450STheodore Ts'o #endif
4221dcf2d804STao Ma failed_mount7:
4222dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4223dcf2d804STao Ma failed_mount6:
4224f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
4225d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
4226b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
4227d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4228d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4229d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4230d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
423100764937SAzat Khuzhin failed_mount5:
4232f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4233f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4234f9ae9cf5STheodore Ts'o failed_mount4a:
423594bf608aSAl Viro 	dput(sb->s_root);
423632a9bb57SManish Katiyar 	sb->s_root = NULL;
423794bf608aSAl Viro failed_mount4:
4238b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
42392e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
42402e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
42414c0425ffSMingming Cao failed_mount_wq:
42420390131bSFrank Mayhar 	if (sbi->s_journal) {
4243dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
424447b4a50bSJan Kara 		sbi->s_journal = NULL;
42450390131bSFrank Mayhar 	}
424650460fe8SDarrick J. Wong failed_mount3a:
4247d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4248eb68d0e2SZheng Liu failed_mount3:
42499105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4250c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4251c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4252ac27a0ecSDave Kleikamp failed_mount2:
4253ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4254ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4255b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4256ac27a0ecSDave Kleikamp failed_mount:
42570441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
42580441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4259240799cdSTheodore Ts'o 	if (sbi->s_proc) {
426066acdcf4STheodore Ts'o 		remove_proc_entry("options", sbi->s_proc);
42619f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
4262240799cdSTheodore Ts'o 	}
4263ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4264a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
4265ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
4266ac27a0ecSDave Kleikamp #endif
4267617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4268ac27a0ecSDave Kleikamp 	brelse(bh);
4269ac27a0ecSDave Kleikamp out_fail:
4270ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4271f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
4272ac27a0ecSDave Kleikamp 	kfree(sbi);
4273dcc7dae3SCyrill Gorcunov out_free_orig:
4274d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
427507aa2ea1SLukas Czerner 	return err ? err : ret;
4276ac27a0ecSDave Kleikamp }
4277ac27a0ecSDave Kleikamp 
4278ac27a0ecSDave Kleikamp /*
4279ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4280ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4281ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4282ac27a0ecSDave Kleikamp  */
4283617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4284ac27a0ecSDave Kleikamp {
4285617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4286ac27a0ecSDave Kleikamp 
4287ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
428830773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
428930773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4290ac27a0ecSDave Kleikamp 
4291a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4292ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4293dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4294ac27a0ecSDave Kleikamp 	else
4295dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
42965bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
42975bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
42985bf5683aSHidehiro Kawai 	else
42995bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4300a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4301ac27a0ecSDave Kleikamp }
4302ac27a0ecSDave Kleikamp 
4303617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
4304ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
4305ac27a0ecSDave Kleikamp {
4306ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4307ac27a0ecSDave Kleikamp 	journal_t *journal;
4308ac27a0ecSDave Kleikamp 
43090390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
43100390131bSFrank Mayhar 
4311ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
4312ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
4313ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
4314ac27a0ecSDave Kleikamp 
43151d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
43161d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4317b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4318ac27a0ecSDave Kleikamp 		return NULL;
4319ac27a0ecSDave Kleikamp 	}
4320ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4321ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4322ac27a0ecSDave Kleikamp 		iput(journal_inode);
4323b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4324ac27a0ecSDave Kleikamp 		return NULL;
4325ac27a0ecSDave Kleikamp 	}
4326ac27a0ecSDave Kleikamp 
4327e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4328ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
43291d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4330b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4331ac27a0ecSDave Kleikamp 		iput(journal_inode);
4332ac27a0ecSDave Kleikamp 		return NULL;
4333ac27a0ecSDave Kleikamp 	}
4334ac27a0ecSDave Kleikamp 
4335dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4336ac27a0ecSDave Kleikamp 	if (!journal) {
4337b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4338ac27a0ecSDave Kleikamp 		iput(journal_inode);
4339ac27a0ecSDave Kleikamp 		return NULL;
4340ac27a0ecSDave Kleikamp 	}
4341ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4342617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4343ac27a0ecSDave Kleikamp 	return journal;
4344ac27a0ecSDave Kleikamp }
4345ac27a0ecSDave Kleikamp 
4346617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4347ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4348ac27a0ecSDave Kleikamp {
4349ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4350ac27a0ecSDave Kleikamp 	journal_t *journal;
4351617ba13bSMingming Cao 	ext4_fsblk_t start;
4352617ba13bSMingming Cao 	ext4_fsblk_t len;
4353ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4354617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4355ac27a0ecSDave Kleikamp 	unsigned long offset;
4356617ba13bSMingming Cao 	struct ext4_super_block *es;
4357ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4358ac27a0ecSDave Kleikamp 
43590390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
43600390131bSFrank Mayhar 
4361b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4362ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4363ac27a0ecSDave Kleikamp 		return NULL;
4364ac27a0ecSDave Kleikamp 
4365ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4366e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4367ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4368b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4369b31e1552SEric Sandeen 			"blocksize too small for journal device");
4370ac27a0ecSDave Kleikamp 		goto out_bdev;
4371ac27a0ecSDave Kleikamp 	}
4372ac27a0ecSDave Kleikamp 
4373617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4374617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4375ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4376ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4377b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4378b31e1552SEric Sandeen 		       "external journal");
4379ac27a0ecSDave Kleikamp 		goto out_bdev;
4380ac27a0ecSDave Kleikamp 	}
4381ac27a0ecSDave Kleikamp 
43822716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4383617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4384ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4385617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4386b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4387b31e1552SEric Sandeen 					"bad superblock");
4388ac27a0ecSDave Kleikamp 		brelse(bh);
4389ac27a0ecSDave Kleikamp 		goto out_bdev;
4390ac27a0ecSDave Kleikamp 	}
4391ac27a0ecSDave Kleikamp 
4392df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4393df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4394df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4395df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4396df4763beSDarrick J. Wong 				       "corrupt superblock");
4397df4763beSDarrick J. Wong 		brelse(bh);
4398df4763beSDarrick J. Wong 		goto out_bdev;
4399df4763beSDarrick J. Wong 	}
4400df4763beSDarrick J. Wong 
4401617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4402b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4403ac27a0ecSDave Kleikamp 		brelse(bh);
4404ac27a0ecSDave Kleikamp 		goto out_bdev;
4405ac27a0ecSDave Kleikamp 	}
4406ac27a0ecSDave Kleikamp 
4407bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4408ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4409ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4410ac27a0ecSDave Kleikamp 
4411dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4412ac27a0ecSDave Kleikamp 					start, len, blocksize);
4413ac27a0ecSDave Kleikamp 	if (!journal) {
4414b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4415ac27a0ecSDave Kleikamp 		goto out_bdev;
4416ac27a0ecSDave Kleikamp 	}
4417ac27a0ecSDave Kleikamp 	journal->j_private = sb;
44189f203507STheodore Ts'o 	ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4419ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4420ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4421b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4422ac27a0ecSDave Kleikamp 		goto out_journal;
4423ac27a0ecSDave Kleikamp 	}
4424ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4425b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4426b31e1552SEric Sandeen 					"user (unsupported) - %d",
4427ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4428ac27a0ecSDave Kleikamp 		goto out_journal;
4429ac27a0ecSDave Kleikamp 	}
4430617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4431617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4432ac27a0ecSDave Kleikamp 	return journal;
44330b8e58a1SAndreas Dilger 
4434ac27a0ecSDave Kleikamp out_journal:
4435dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4436ac27a0ecSDave Kleikamp out_bdev:
4437617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4438ac27a0ecSDave Kleikamp 	return NULL;
4439ac27a0ecSDave Kleikamp }
4440ac27a0ecSDave Kleikamp 
4441617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4442617ba13bSMingming Cao 			     struct ext4_super_block *es,
4443ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4444ac27a0ecSDave Kleikamp {
4445ac27a0ecSDave Kleikamp 	journal_t *journal;
4446ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4447ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4448ac27a0ecSDave Kleikamp 	int err = 0;
4449ac27a0ecSDave Kleikamp 	int really_read_only;
4450ac27a0ecSDave Kleikamp 
44510390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
44520390131bSFrank Mayhar 
4453ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4454ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4455b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4456b31e1552SEric Sandeen 			"numbers have changed");
4457ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4458ac27a0ecSDave Kleikamp 	} else
4459ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4460ac27a0ecSDave Kleikamp 
4461ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4462ac27a0ecSDave Kleikamp 
4463ac27a0ecSDave Kleikamp 	/*
4464ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4465ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4466ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4467ac27a0ecSDave Kleikamp 	 */
4468617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
4469ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
4470b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4471b31e1552SEric Sandeen 					"required on readonly filesystem");
4472ac27a0ecSDave Kleikamp 			if (really_read_only) {
4473b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4474b31e1552SEric Sandeen 					"unavailable, cannot proceed");
4475ac27a0ecSDave Kleikamp 				return -EROFS;
4476ac27a0ecSDave Kleikamp 			}
4477b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4478b31e1552SEric Sandeen 			       "be enabled during recovery");
4479ac27a0ecSDave Kleikamp 		}
4480ac27a0ecSDave Kleikamp 	}
4481ac27a0ecSDave Kleikamp 
4482ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4483b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4484b31e1552SEric Sandeen 		       "and inode journals!");
4485ac27a0ecSDave Kleikamp 		return -EINVAL;
4486ac27a0ecSDave Kleikamp 	}
4487ac27a0ecSDave Kleikamp 
4488ac27a0ecSDave Kleikamp 	if (journal_inum) {
4489617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4490ac27a0ecSDave Kleikamp 			return -EINVAL;
4491ac27a0ecSDave Kleikamp 	} else {
4492617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4493ac27a0ecSDave Kleikamp 			return -EINVAL;
4494ac27a0ecSDave Kleikamp 	}
4495ac27a0ecSDave Kleikamp 
449690576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4497b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
44984776004fSTheodore Ts'o 
4499617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
4500dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
45011c13d5c0STheodore Ts'o 	if (!err) {
45021c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
45031c13d5c0STheodore Ts'o 		if (save)
45041c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
45051c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4506dab291afSMingming Cao 		err = jbd2_journal_load(journal);
45071c13d5c0STheodore Ts'o 		if (save)
45081c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
45091c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
45101c13d5c0STheodore Ts'o 		kfree(save);
45111c13d5c0STheodore Ts'o 	}
4512ac27a0ecSDave Kleikamp 
4513ac27a0ecSDave Kleikamp 	if (err) {
4514b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4515dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4516ac27a0ecSDave Kleikamp 		return err;
4517ac27a0ecSDave Kleikamp 	}
4518ac27a0ecSDave Kleikamp 
4519617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4520617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4521ac27a0ecSDave Kleikamp 
4522c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4523ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4524ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4525ac27a0ecSDave Kleikamp 
4526ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4527e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4528ac27a0ecSDave Kleikamp 	}
4529ac27a0ecSDave Kleikamp 
4530ac27a0ecSDave Kleikamp 	return 0;
4531ac27a0ecSDave Kleikamp }
4532ac27a0ecSDave Kleikamp 
4533e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4534ac27a0ecSDave Kleikamp {
4535e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4536617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4537c4be0c1dSTakashi Sato 	int error = 0;
4538ac27a0ecSDave Kleikamp 
45397c2e7087STheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4540c4be0c1dSTakashi Sato 		return error;
4541914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
4542914258bfSTheodore Ts'o 		/*
4543914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
4544914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
4545914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
4546914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
4547914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
4548914258bfSTheodore Ts'o 		 * write and hope for the best.
4549914258bfSTheodore Ts'o 		 */
4550b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
4551b31e1552SEric Sandeen 		       "superblock detected");
4552914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
4553914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
4554914258bfSTheodore Ts'o 	}
455571290b36STheodore Ts'o 	/*
455671290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
455771290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
455871290b36STheodore Ts'o 	 * write time when we are mounting the root file system
455971290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
456071290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
456171290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
456271290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
456371290b36STheodore Ts'o 	 * to complain and force a full file system check.
456471290b36STheodore Ts'o 	 */
456571290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4566ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4567f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4568afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4569afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4570afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4571afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4572f613dfcbSTheodore Ts'o 	else
4573f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4574f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
4575d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
457657042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
457757042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
457857042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
4579d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
45807f93cff9STheodore Ts'o 		es->s_free_inodes_count =
45817f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
45825d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
4583ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
458406db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
4585ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4586914258bfSTheodore Ts'o 	if (sync) {
4587c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
4588c4be0c1dSTakashi Sato 		if (error)
4589c4be0c1dSTakashi Sato 			return error;
4590c4be0c1dSTakashi Sato 
4591c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4592c4be0c1dSTakashi Sato 		if (error) {
4593b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4594b31e1552SEric Sandeen 			       "superblock");
4595914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4596914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4597914258bfSTheodore Ts'o 		}
4598914258bfSTheodore Ts'o 	}
4599c4be0c1dSTakashi Sato 	return error;
4600ac27a0ecSDave Kleikamp }
4601ac27a0ecSDave Kleikamp 
4602ac27a0ecSDave Kleikamp /*
4603ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4604ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4605ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4606ac27a0ecSDave Kleikamp  */
4607617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4608617ba13bSMingming Cao 					struct ext4_super_block *es)
4609ac27a0ecSDave Kleikamp {
4610617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4611ac27a0ecSDave Kleikamp 
46120390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
46130390131bSFrank Mayhar 		BUG_ON(journal != NULL);
46140390131bSFrank Mayhar 		return;
46150390131bSFrank Mayhar 	}
4616dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
46177ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
46187ffe1ea8SHidehiro Kawai 		goto out;
46197ffe1ea8SHidehiro Kawai 
4620617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
4621ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4622617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4623e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4624ac27a0ecSDave Kleikamp 	}
46257ffe1ea8SHidehiro Kawai 
46267ffe1ea8SHidehiro Kawai out:
4627dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4628ac27a0ecSDave Kleikamp }
4629ac27a0ecSDave Kleikamp 
4630ac27a0ecSDave Kleikamp /*
4631ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4632ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4633ac27a0ecSDave Kleikamp  * main filesystem now.
4634ac27a0ecSDave Kleikamp  */
4635617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4636617ba13bSMingming Cao 				   struct ext4_super_block *es)
4637ac27a0ecSDave Kleikamp {
4638ac27a0ecSDave Kleikamp 	journal_t *journal;
4639ac27a0ecSDave Kleikamp 	int j_errno;
4640ac27a0ecSDave Kleikamp 	const char *errstr;
4641ac27a0ecSDave Kleikamp 
46420390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
46430390131bSFrank Mayhar 
4644617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4645ac27a0ecSDave Kleikamp 
4646ac27a0ecSDave Kleikamp 	/*
4647ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4648617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4649ac27a0ecSDave Kleikamp 	 */
4650ac27a0ecSDave Kleikamp 
4651dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4652ac27a0ecSDave Kleikamp 	if (j_errno) {
4653ac27a0ecSDave Kleikamp 		char nbuf[16];
4654ac27a0ecSDave Kleikamp 
4655617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
465612062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4657ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
465812062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4659ac27a0ecSDave Kleikamp 
4660617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4661617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4662e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4663ac27a0ecSDave Kleikamp 
4664dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4665d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
4666ac27a0ecSDave Kleikamp 	}
4667ac27a0ecSDave Kleikamp }
4668ac27a0ecSDave Kleikamp 
4669ac27a0ecSDave Kleikamp /*
4670ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4671ac27a0ecSDave Kleikamp  * and wait on the commit.
4672ac27a0ecSDave Kleikamp  */
4673617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4674ac27a0ecSDave Kleikamp {
4675ac27a0ecSDave Kleikamp 	journal_t *journal;
4676ac27a0ecSDave Kleikamp 
4677ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4678ac27a0ecSDave Kleikamp 		return 0;
4679ac27a0ecSDave Kleikamp 
4680617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4681b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
4682ac27a0ecSDave Kleikamp }
4683ac27a0ecSDave Kleikamp 
4684617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4685ac27a0ecSDave Kleikamp {
468614ce0cb4STheodore Ts'o 	int ret = 0;
46879eddacf9SJan Kara 	tid_t target;
468806a407f1SDmitry Monakhov 	bool needs_barrier = false;
46898d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4690ac27a0ecSDave Kleikamp 
46919bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
46922e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
4693a1177825SJan Kara 	/*
4694a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
4695a1177825SJan Kara 	 * no dirty dquots
4696a1177825SJan Kara 	 */
4697a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
469806a407f1SDmitry Monakhov 	/*
469906a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
470006a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
470106a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
470206a407f1SDmitry Monakhov 	 */
4703bda32530STheodore Ts'o 	if (sbi->s_journal) {
470406a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
470506a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
470606a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
470706a407f1SDmitry Monakhov 			needs_barrier = true;
470806a407f1SDmitry Monakhov 
47098d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4710ac27a0ecSDave Kleikamp 			if (wait)
4711bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
4712bda32530STheodore Ts'o 							   target);
47130390131bSFrank Mayhar 		}
4714bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
4715bda32530STheodore Ts'o 		needs_barrier = true;
471606a407f1SDmitry Monakhov 	if (needs_barrier) {
471706a407f1SDmitry Monakhov 		int err;
471806a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
471906a407f1SDmitry Monakhov 		if (!ret)
472006a407f1SDmitry Monakhov 			ret = err;
472106a407f1SDmitry Monakhov 	}
472206a407f1SDmitry Monakhov 
472306a407f1SDmitry Monakhov 	return ret;
472406a407f1SDmitry Monakhov }
472506a407f1SDmitry Monakhov 
4726ac27a0ecSDave Kleikamp /*
4727ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4728ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4729be4f27d3SYongqiang Yang  *
4730be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
47318e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
47328e8ad8a5SJan Kara  * modifications.
4733ac27a0ecSDave Kleikamp  */
4734c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4735ac27a0ecSDave Kleikamp {
4736c4be0c1dSTakashi Sato 	int error = 0;
4737c4be0c1dSTakashi Sato 	journal_t *journal;
4738ac27a0ecSDave Kleikamp 
47399ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
47409ca92389STheodore Ts'o 		return 0;
47419ca92389STheodore Ts'o 
4742c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4743ac27a0ecSDave Kleikamp 
4744bb044576STheodore Ts'o 	if (journal) {
4745ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
4746dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
47477ffe1ea8SHidehiro Kawai 
47487ffe1ea8SHidehiro Kawai 		/*
4749bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
4750bb044576STheodore Ts'o 		 * flush the journal.
47517ffe1ea8SHidehiro Kawai 		 */
4752c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
47536b0310fbSEric Sandeen 		if (error < 0)
47546b0310fbSEric Sandeen 			goto out;
4755bb044576STheodore Ts'o 	}
4756ac27a0ecSDave Kleikamp 
4757ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
4758617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4759e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
47606b0310fbSEric Sandeen out:
4761bb044576STheodore Ts'o 	if (journal)
47628e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
4763bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
47646b0310fbSEric Sandeen 	return error;
4765ac27a0ecSDave Kleikamp }
4766ac27a0ecSDave Kleikamp 
4767ac27a0ecSDave Kleikamp /*
4768ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4769ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4770ac27a0ecSDave Kleikamp  */
4771c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4772ac27a0ecSDave Kleikamp {
47739ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
47749ca92389STheodore Ts'o 		return 0;
47759ca92389STheodore Ts'o 
47769ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
4777617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4778e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4779c4be0c1dSTakashi Sato 	return 0;
4780ac27a0ecSDave Kleikamp }
4781ac27a0ecSDave Kleikamp 
4782673c6100STheodore Ts'o /*
4783673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4784673c6100STheodore Ts'o  */
4785673c6100STheodore Ts'o struct ext4_mount_options {
4786673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4787a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
478808cefc7aSEric W. Biederman 	kuid_t s_resuid;
478908cefc7aSEric W. Biederman 	kgid_t s_resgid;
4790673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4791673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4792673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4793673c6100STheodore Ts'o 	int s_jquota_fmt;
4794a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
4795673c6100STheodore Ts'o #endif
4796673c6100STheodore Ts'o };
4797673c6100STheodore Ts'o 
4798617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4799ac27a0ecSDave Kleikamp {
4800617ba13bSMingming Cao 	struct ext4_super_block *es;
4801617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4802ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4803617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4804c79d967dSChristoph Hellwig 	int enable_quota = 0;
48058a266467STheodore Ts'o 	ext4_group_t g;
4806b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4807c5e06d10SJohann Lombardi 	int err = 0;
4808ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
480903dafb5fSChen Gang 	int i, j;
4810ac27a0ecSDave Kleikamp #endif
4811d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4812ac27a0ecSDave Kleikamp 
4813ac27a0ecSDave Kleikamp 	/* Store the original options */
4814ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4815ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4816a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4817ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4818ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4819ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
482030773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
482130773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4822ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4823ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4824a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
482503dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
482603dafb5fSChen Gang 			old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
482703dafb5fSChen Gang 							 GFP_KERNEL);
482803dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
482903dafb5fSChen Gang 				for (j = 0; j < i; j++)
483003dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
48313e36a163SWei Yongjun 				kfree(orig_data);
483203dafb5fSChen Gang 				return -ENOMEM;
483303dafb5fSChen Gang 			}
483403dafb5fSChen Gang 		} else
483503dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
4836ac27a0ecSDave Kleikamp #endif
4837b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4838b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4839ac27a0ecSDave Kleikamp 
4840ac27a0ecSDave Kleikamp 	/*
4841ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
4842ac27a0ecSDave Kleikamp 	 */
4843661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
4844ac27a0ecSDave Kleikamp 		err = -EINVAL;
4845ac27a0ecSDave Kleikamp 		goto restore_opts;
4846ac27a0ecSDave Kleikamp 	}
4847ac27a0ecSDave Kleikamp 
48486b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
48496b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
48506b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
48516b992ff2SDarrick J. Wong 			 "during remount not supported");
48526b992ff2SDarrick J. Wong 		err = -EINVAL;
48536b992ff2SDarrick J. Wong 		goto restore_opts;
48546b992ff2SDarrick J. Wong 	}
48556b992ff2SDarrick J. Wong 
4856c6d3d56dSDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
4857c6d3d56dSDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
4858c6d3d56dSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
4859c6d3d56dSDarrick J. Wong 			 "during remount not supported");
4860c6d3d56dSDarrick J. Wong 		err = -EINVAL;
4861c6d3d56dSDarrick J. Wong 		goto restore_opts;
4862c6d3d56dSDarrick J. Wong 	}
4863c6d3d56dSDarrick J. Wong 
48646ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
48656ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
48666ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
48676ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
48686ae6514bSPiotr Sarna 			err = -EINVAL;
48696ae6514bSPiotr Sarna 			goto restore_opts;
48706ae6514bSPiotr Sarna 		}
48716ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
48726ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
48736ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
48746ae6514bSPiotr Sarna 			err = -EINVAL;
48756ae6514bSPiotr Sarna 			goto restore_opts;
48766ae6514bSPiotr Sarna 		}
48776ae6514bSPiotr Sarna 	}
48786ae6514bSPiotr Sarna 
48794ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4880c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4881ac27a0ecSDave Kleikamp 
4882ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4883482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4884ac27a0ecSDave Kleikamp 
4885ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4886ac27a0ecSDave Kleikamp 
4887b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4888617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4889b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4890b3881f74STheodore Ts'o 	}
4891ac27a0ecSDave Kleikamp 
4892661aa520SEric Sandeen 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
48934ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4894ac27a0ecSDave Kleikamp 			err = -EROFS;
4895ac27a0ecSDave Kleikamp 			goto restore_opts;
4896ac27a0ecSDave Kleikamp 		}
4897ac27a0ecSDave Kleikamp 
4898ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
489938c03b34STheodore Ts'o 			err = sync_filesystem(sb);
490038c03b34STheodore Ts'o 			if (err < 0)
490138c03b34STheodore Ts'o 				goto restore_opts;
49020f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
49030f0dd62fSChristoph Hellwig 			if (err < 0)
4904c79d967dSChristoph Hellwig 				goto restore_opts;
4905c79d967dSChristoph Hellwig 
4906ac27a0ecSDave Kleikamp 			/*
4907ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4908ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4909ac27a0ecSDave Kleikamp 			 */
4910ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4911ac27a0ecSDave Kleikamp 
4912ac27a0ecSDave Kleikamp 			/*
4913ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4914ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4915ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4916ac27a0ecSDave Kleikamp 			 */
4917617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4918617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4919ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4920ac27a0ecSDave Kleikamp 
4921a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4922617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4923ac27a0ecSDave Kleikamp 		} else {
4924a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4925a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
4926ac27a0ecSDave Kleikamp 				err = -EROFS;
4927ac27a0ecSDave Kleikamp 				goto restore_opts;
4928ac27a0ecSDave Kleikamp 			}
4929ead6596bSEric Sandeen 			/*
49308a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
49310b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
49328a266467STheodore Ts'o 			 */
49338a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
49348a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
49358a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
49368a266467STheodore Ts'o 
4937feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
4938b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4939b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
49408a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
49418a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
49428a266467STheodore Ts'o 					err = -EINVAL;
49438a266467STheodore Ts'o 					goto restore_opts;
49448a266467STheodore Ts'o 				}
49458a266467STheodore Ts'o 			}
49468a266467STheodore Ts'o 
49478a266467STheodore Ts'o 			/*
4948ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4949ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4950ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4951ead6596bSEric Sandeen 			 */
4952ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4953b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4954ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4955ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4956b31e1552SEric Sandeen 				       "umount/remount instead");
4957ead6596bSEric Sandeen 				err = -EINVAL;
4958ead6596bSEric Sandeen 				goto restore_opts;
4959ead6596bSEric Sandeen 			}
4960ead6596bSEric Sandeen 
4961ac27a0ecSDave Kleikamp 			/*
4962ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4963ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4964ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4965ac27a0ecSDave Kleikamp 			 * the partition.)
4966ac27a0ecSDave Kleikamp 			 */
49670390131bSFrank Mayhar 			if (sbi->s_journal)
4968617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4969ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4970617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4971ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4972c5e06d10SJohann Lombardi 			if (EXT4_HAS_INCOMPAT_FEATURE(sb,
4973c5e06d10SJohann Lombardi 						     EXT4_FEATURE_INCOMPAT_MMP))
4974c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
4975c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
4976c5e06d10SJohann Lombardi 					err = -EROFS;
4977c5e06d10SJohann Lombardi 					goto restore_opts;
4978c5e06d10SJohann Lombardi 				}
4979c79d967dSChristoph Hellwig 			enable_quota = 1;
4980ac27a0ecSDave Kleikamp 		}
4981ac27a0ecSDave Kleikamp 	}
4982bfff6873SLukas Czerner 
4983bfff6873SLukas Czerner 	/*
4984bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4985bfff6873SLukas Czerner 	 * current settings
4986bfff6873SLukas Czerner 	 */
4987bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4988bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4989bfff6873SLukas Czerner 	else {
4990bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4991bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4992bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4993bfff6873SLukas Czerner 	}
4994bfff6873SLukas Czerner 
49956fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
4996d096ad0fSMichael Tokarev 	if (sbi->s_journal == NULL && !(old_sb_flags & MS_RDONLY))
4997e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
49980390131bSFrank Mayhar 
4999ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5000ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5001a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5002ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
50037c319d32SAditya Kali 	if (enable_quota) {
50047c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
50050f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
50067c319d32SAditya Kali 		else if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
50077c319d32SAditya Kali 					EXT4_FEATURE_RO_COMPAT_QUOTA)) {
50087c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
500907724f98STheodore Ts'o 			if (err)
50107c319d32SAditya Kali 				goto restore_opts;
50117c319d32SAditya Kali 		}
50127c319d32SAditya Kali 	}
50137c319d32SAditya Kali #endif
5014d4c402d9SCurt Wohlgemuth 
5015d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5016d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5017ac27a0ecSDave Kleikamp 	return 0;
50180b8e58a1SAndreas Dilger 
5019ac27a0ecSDave Kleikamp restore_opts:
5020ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5021ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5022a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5023ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5024ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5025ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
502630773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
502730773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5028ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5029ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5030a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
5031ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
5032ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
5033ac27a0ecSDave Kleikamp 	}
5034ac27a0ecSDave Kleikamp #endif
5035d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5036ac27a0ecSDave Kleikamp 	return err;
5037ac27a0ecSDave Kleikamp }
5038ac27a0ecSDave Kleikamp 
5039617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5040ac27a0ecSDave Kleikamp {
5041ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5042617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5043617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
504427dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5045960cc398SPekka Enberg 	u64 fsid;
5046d02a9391SKazuya Mio 	s64 bfree;
504727dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5048ac27a0ecSDave Kleikamp 
5049952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5050952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5051ac27a0ecSDave Kleikamp 
5052617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5053ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5054b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
505557042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
505657042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5057d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
505857042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
505927dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
506027dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
506127dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5062ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5063ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
506452d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5065617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5066960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5067960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5068960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5069960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
50700b8e58a1SAndreas Dilger 
5071ac27a0ecSDave Kleikamp 	return 0;
5072ac27a0ecSDave Kleikamp }
5073ac27a0ecSDave Kleikamp 
50740b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
50750b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
5076ac27a0ecSDave Kleikamp  * Process 1                         Process 2
5077617ba13bSMingming Cao  * ext4_create()                     quota_sync()
5078dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
5079871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
5080dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
5081ac27a0ecSDave Kleikamp  *
5082ac27a0ecSDave Kleikamp  */
5083ac27a0ecSDave Kleikamp 
5084ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5085ac27a0ecSDave Kleikamp 
5086ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5087ac27a0ecSDave Kleikamp {
50884c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5089ac27a0ecSDave Kleikamp }
5090ac27a0ecSDave Kleikamp 
5091617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5092ac27a0ecSDave Kleikamp {
5093ac27a0ecSDave Kleikamp 	int ret, err;
5094ac27a0ecSDave Kleikamp 	handle_t *handle;
5095ac27a0ecSDave Kleikamp 	struct inode *inode;
5096ac27a0ecSDave Kleikamp 
5097ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
50989924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5099617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5100ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5101ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5102ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5103617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5104ac27a0ecSDave Kleikamp 	if (!ret)
5105ac27a0ecSDave Kleikamp 		ret = err;
5106ac27a0ecSDave Kleikamp 	return ret;
5107ac27a0ecSDave Kleikamp }
5108ac27a0ecSDave Kleikamp 
5109617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5110ac27a0ecSDave Kleikamp {
5111ac27a0ecSDave Kleikamp 	int ret, err;
5112ac27a0ecSDave Kleikamp 	handle_t *handle;
5113ac27a0ecSDave Kleikamp 
51149924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5115617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5116ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5117ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5118ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5119617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5120ac27a0ecSDave Kleikamp 	if (!ret)
5121ac27a0ecSDave Kleikamp 		ret = err;
5122ac27a0ecSDave Kleikamp 	return ret;
5123ac27a0ecSDave Kleikamp }
5124ac27a0ecSDave Kleikamp 
5125617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5126ac27a0ecSDave Kleikamp {
5127ac27a0ecSDave Kleikamp 	int ret, err;
5128ac27a0ecSDave Kleikamp 	handle_t *handle;
5129ac27a0ecSDave Kleikamp 
51309924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5131617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
51329c3013e9SJan Kara 	if (IS_ERR(handle)) {
51339c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
51349c3013e9SJan Kara 		dquot_release(dquot);
5135ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
51369c3013e9SJan Kara 	}
5137ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5138617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5139ac27a0ecSDave Kleikamp 	if (!ret)
5140ac27a0ecSDave Kleikamp 		ret = err;
5141ac27a0ecSDave Kleikamp 	return ret;
5142ac27a0ecSDave Kleikamp }
5143ac27a0ecSDave Kleikamp 
5144617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5145ac27a0ecSDave Kleikamp {
5146262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5147262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5148262b4662SJan Kara 
51492c8be6b2SJan Kara 	/* Are we journaling quotas? */
5150262b4662SJan Kara 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) ||
5151262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5152ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5153617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5154ac27a0ecSDave Kleikamp 	} else {
5155ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5156ac27a0ecSDave Kleikamp 	}
5157ac27a0ecSDave Kleikamp }
5158ac27a0ecSDave Kleikamp 
5159617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5160ac27a0ecSDave Kleikamp {
5161ac27a0ecSDave Kleikamp 	int ret, err;
5162ac27a0ecSDave Kleikamp 	handle_t *handle;
5163ac27a0ecSDave Kleikamp 
5164ac27a0ecSDave Kleikamp 	/* Data block + inode block */
51659924a92aSTheodore Ts'o 	handle = ext4_journal_start(sb->s_root->d_inode, EXT4_HT_QUOTA, 2);
5166ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5167ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5168ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5169617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5170ac27a0ecSDave Kleikamp 	if (!ret)
5171ac27a0ecSDave Kleikamp 		ret = err;
5172ac27a0ecSDave Kleikamp 	return ret;
5173ac27a0ecSDave Kleikamp }
5174ac27a0ecSDave Kleikamp 
5175ac27a0ecSDave Kleikamp /*
5176ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5177ac27a0ecSDave Kleikamp  * the quota file and such...
5178ac27a0ecSDave Kleikamp  */
5179617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5180ac27a0ecSDave Kleikamp {
5181287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
5182617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5183ac27a0ecSDave Kleikamp }
5184ac27a0ecSDave Kleikamp 
5185ac27a0ecSDave Kleikamp /*
5186ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5187ac27a0ecSDave Kleikamp  */
5188617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
5189f00c9e44SJan Kara 			 struct path *path)
5190ac27a0ecSDave Kleikamp {
5191ac27a0ecSDave Kleikamp 	int err;
5192ac27a0ecSDave Kleikamp 
5193ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5194ac27a0ecSDave Kleikamp 		return -EINVAL;
51950623543bSJan Kara 
5196ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5197d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5198ac27a0ecSDave Kleikamp 		return -EXDEV;
51990623543bSJan Kara 	/* Journaling quota? */
52000623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
52012b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5202f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5203b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5204b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5205b31e1552SEric Sandeen 				"Journaled quota will not work");
52060623543bSJan Kara 	}
52070623543bSJan Kara 
52080623543bSJan Kara 	/*
52090623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
52100623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
52110623543bSJan Kara 	 */
52120390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
5213f00c9e44SJan Kara 	    ext4_should_journal_data(path->dentry->d_inode)) {
52140623543bSJan Kara 		/*
52150623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
52160623543bSJan Kara 		 * otherwise be livelocked...
52170623543bSJan Kara 		 */
52180623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
52197ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
52200623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5221f00c9e44SJan Kara 		if (err)
52227ffe1ea8SHidehiro Kawai 			return err;
52237ffe1ea8SHidehiro Kawai 	}
52240623543bSJan Kara 
5225f00c9e44SJan Kara 	return dquot_quota_on(sb, type, format_id, path);
5226ac27a0ecSDave Kleikamp }
5227ac27a0ecSDave Kleikamp 
52287c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
52297c319d32SAditya Kali 			     unsigned int flags)
52307c319d32SAditya Kali {
52317c319d32SAditya Kali 	int err;
52327c319d32SAditya Kali 	struct inode *qf_inode;
5233a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
52347c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
52357c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
52367c319d32SAditya Kali 	};
52377c319d32SAditya Kali 
52387c319d32SAditya Kali 	BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA));
52397c319d32SAditya Kali 
52407c319d32SAditya Kali 	if (!qf_inums[type])
52417c319d32SAditya Kali 		return -EPERM;
52427c319d32SAditya Kali 
52437c319d32SAditya Kali 	qf_inode = ext4_iget(sb, qf_inums[type]);
52447c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
52457c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
52467c319d32SAditya Kali 		return PTR_ERR(qf_inode);
52477c319d32SAditya Kali 	}
52487c319d32SAditya Kali 
5249bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5250bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
52517c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
52527c319d32SAditya Kali 	iput(qf_inode);
52537c319d32SAditya Kali 
52547c319d32SAditya Kali 	return err;
52557c319d32SAditya Kali }
52567c319d32SAditya Kali 
52577c319d32SAditya Kali /* Enable usage tracking for all quota types. */
52587c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
52597c319d32SAditya Kali {
52607c319d32SAditya Kali 	int type, err = 0;
5261a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
52627c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
52637c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
52647c319d32SAditya Kali 	};
52657c319d32SAditya Kali 
52667c319d32SAditya Kali 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
5267a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
52687c319d32SAditya Kali 		if (qf_inums[type]) {
52697c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
52707c319d32SAditya Kali 						DQUOT_USAGE_ENABLED);
52717c319d32SAditya Kali 			if (err) {
52727c319d32SAditya Kali 				ext4_warning(sb,
527372ba7450STheodore Ts'o 					"Failed to enable quota tracking "
527472ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
527572ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
52767c319d32SAditya Kali 				return err;
52777c319d32SAditya Kali 			}
52787c319d32SAditya Kali 		}
52797c319d32SAditya Kali 	}
52807c319d32SAditya Kali 	return 0;
52817c319d32SAditya Kali }
52827c319d32SAditya Kali 
52837c319d32SAditya Kali /*
52847c319d32SAditya Kali  * quota_on function that is used when QUOTA feature is set.
52857c319d32SAditya Kali  */
52867c319d32SAditya Kali static int ext4_quota_on_sysfile(struct super_block *sb, int type,
52877c319d32SAditya Kali 				 int format_id)
52887c319d32SAditya Kali {
52897c319d32SAditya Kali 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
52907c319d32SAditya Kali 		return -EINVAL;
52917c319d32SAditya Kali 
52927c319d32SAditya Kali 	/*
52937c319d32SAditya Kali 	 * USAGE was enabled at mount time. Only need to enable LIMITS now.
52947c319d32SAditya Kali 	 */
52957c319d32SAditya Kali 	return ext4_quota_enable(sb, type, format_id, DQUOT_LIMITS_ENABLED);
52967c319d32SAditya Kali }
52977c319d32SAditya Kali 
5298ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5299ca0e05e4SDmitry Monakhov {
530021f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
530121f97697SJan Kara 	handle_t *handle;
530221f97697SJan Kara 
530387009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
530487009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
530587009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5306ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5307ca0e05e4SDmitry Monakhov 
53080b268590SAmir Goldstein 	if (!inode)
53090b268590SAmir Goldstein 		goto out;
53100b268590SAmir Goldstein 
531121f97697SJan Kara 	/* Update modification times of quota files when userspace can
531221f97697SJan Kara 	 * start looking at them */
53139924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
531421f97697SJan Kara 	if (IS_ERR(handle))
531521f97697SJan Kara 		goto out;
531621f97697SJan Kara 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
531721f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
531821f97697SJan Kara 	ext4_journal_stop(handle);
531921f97697SJan Kara 
532021f97697SJan Kara out:
5321ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5322ca0e05e4SDmitry Monakhov }
5323ca0e05e4SDmitry Monakhov 
53247c319d32SAditya Kali /*
53257c319d32SAditya Kali  * quota_off function that is used when QUOTA feature is set.
53267c319d32SAditya Kali  */
53277c319d32SAditya Kali static int ext4_quota_off_sysfile(struct super_block *sb, int type)
53287c319d32SAditya Kali {
53297c319d32SAditya Kali 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
53307c319d32SAditya Kali 		return -EINVAL;
53317c319d32SAditya Kali 
53327c319d32SAditya Kali 	/* Disable only the limits. */
53337c319d32SAditya Kali 	return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
53347c319d32SAditya Kali }
53357c319d32SAditya Kali 
5336ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5337ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5338ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5339ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5340617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5341ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5342ac27a0ecSDave Kleikamp {
5343ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5344725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5345ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5346ac27a0ecSDave Kleikamp 	int tocopy;
5347ac27a0ecSDave Kleikamp 	size_t toread;
5348ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5349ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5350ac27a0ecSDave Kleikamp 
5351ac27a0ecSDave Kleikamp 	if (off > i_size)
5352ac27a0ecSDave Kleikamp 		return 0;
5353ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5354ac27a0ecSDave Kleikamp 		len = i_size-off;
5355ac27a0ecSDave Kleikamp 	toread = len;
5356ac27a0ecSDave Kleikamp 	while (toread > 0) {
5357ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5358ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
53591c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
53601c215028STheodore Ts'o 		if (IS_ERR(bh))
53611c215028STheodore Ts'o 			return PTR_ERR(bh);
5362ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5363ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5364ac27a0ecSDave Kleikamp 		else
5365ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5366ac27a0ecSDave Kleikamp 		brelse(bh);
5367ac27a0ecSDave Kleikamp 		offset = 0;
5368ac27a0ecSDave Kleikamp 		toread -= tocopy;
5369ac27a0ecSDave Kleikamp 		data += tocopy;
5370ac27a0ecSDave Kleikamp 		blk++;
5371ac27a0ecSDave Kleikamp 	}
5372ac27a0ecSDave Kleikamp 	return len;
5373ac27a0ecSDave Kleikamp }
5374ac27a0ecSDave Kleikamp 
5375ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5376ac27a0ecSDave Kleikamp  * enough credits) */
5377617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5378ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5379ac27a0ecSDave Kleikamp {
5380ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5381725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
53821c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
5383ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5384ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5385ac27a0ecSDave Kleikamp 
53860390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5387b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5388b31e1552SEric Sandeen 			" cancelled because transaction is not started",
53899c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
53909c3013e9SJan Kara 		return -EIO;
53919c3013e9SJan Kara 	}
539267eeb568SDmitry Monakhov 	/*
539367eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
539467eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
539567eeb568SDmitry Monakhov 	 */
539667eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
539767eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
539867eeb568SDmitry Monakhov 			" cancelled because not block aligned",
539967eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
540067eeb568SDmitry Monakhov 		return -EIO;
540167eeb568SDmitry Monakhov 	}
540267eeb568SDmitry Monakhov 
54031c215028STheodore Ts'o 	bh = ext4_bread(handle, inode, blk, 1);
54041c215028STheodore Ts'o 	if (IS_ERR(bh))
54051c215028STheodore Ts'o 		return PTR_ERR(bh);
5406ac27a0ecSDave Kleikamp 	if (!bh)
5407ac27a0ecSDave Kleikamp 		goto out;
54085d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
5409617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5410ac27a0ecSDave Kleikamp 	if (err) {
5411ac27a0ecSDave Kleikamp 		brelse(bh);
54121c215028STheodore Ts'o 		return err;
5413ac27a0ecSDave Kleikamp 	}
5414ac27a0ecSDave Kleikamp 	lock_buffer(bh);
541567eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5416ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5417ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
54180390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5419ac27a0ecSDave Kleikamp 	brelse(bh);
5420ac27a0ecSDave Kleikamp out:
542167eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
542267eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
5423617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
5424617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
542521f97697SJan Kara 	}
542667eeb568SDmitry Monakhov 	return len;
5427ac27a0ecSDave Kleikamp }
5428ac27a0ecSDave Kleikamp 
5429ac27a0ecSDave Kleikamp #endif
5430ac27a0ecSDave Kleikamp 
5431152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5432152a0836SAl Viro 		       const char *dev_name, void *data)
5433ac27a0ecSDave Kleikamp {
5434152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
5435ac27a0ecSDave Kleikamp }
5436ac27a0ecSDave Kleikamp 
543737f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
543824b58424STheodore Ts'o static inline void register_as_ext2(void)
543924b58424STheodore Ts'o {
544024b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
544124b58424STheodore Ts'o 	if (err)
544224b58424STheodore Ts'o 		printk(KERN_WARNING
544324b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
544424b58424STheodore Ts'o }
544524b58424STheodore Ts'o 
544624b58424STheodore Ts'o static inline void unregister_as_ext2(void)
544724b58424STheodore Ts'o {
544824b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
544924b58424STheodore Ts'o }
54502035e776STheodore Ts'o 
54512035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
54522035e776STheodore Ts'o {
54532035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
54542035e776STheodore Ts'o 		return 0;
54552035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
54562035e776STheodore Ts'o 		return 1;
54572035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
54582035e776STheodore Ts'o 		return 0;
54592035e776STheodore Ts'o 	return 1;
54602035e776STheodore Ts'o }
546124b58424STheodore Ts'o #else
546224b58424STheodore Ts'o static inline void register_as_ext2(void) { }
546324b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
54642035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
546524b58424STheodore Ts'o #endif
546624b58424STheodore Ts'o 
546737f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
546824b58424STheodore Ts'o static inline void register_as_ext3(void)
546924b58424STheodore Ts'o {
547024b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
547124b58424STheodore Ts'o 	if (err)
547224b58424STheodore Ts'o 		printk(KERN_WARNING
547324b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
547424b58424STheodore Ts'o }
547524b58424STheodore Ts'o 
547624b58424STheodore Ts'o static inline void unregister_as_ext3(void)
547724b58424STheodore Ts'o {
547824b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
547924b58424STheodore Ts'o }
54802035e776STheodore Ts'o 
54812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
54822035e776STheodore Ts'o {
54832035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
54842035e776STheodore Ts'o 		return 0;
54852035e776STheodore Ts'o 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
54862035e776STheodore Ts'o 		return 0;
54872035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
54882035e776STheodore Ts'o 		return 1;
54892035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
54902035e776STheodore Ts'o 		return 0;
54912035e776STheodore Ts'o 	return 1;
54922035e776STheodore Ts'o }
549324b58424STheodore Ts'o #else
549424b58424STheodore Ts'o static inline void register_as_ext3(void) { }
549524b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
54962035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
549724b58424STheodore Ts'o #endif
549824b58424STheodore Ts'o 
549903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
5500ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
550103010a33STheodore Ts'o 	.name		= "ext4",
5502152a0836SAl Viro 	.mount		= ext4_mount,
5503ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
5504ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
5505ac27a0ecSDave Kleikamp };
55067f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
5507ac27a0ecSDave Kleikamp 
55088f021222SLukas Czerner static int __init ext4_init_feat_adverts(void)
5509857ac889SLukas Czerner {
5510857ac889SLukas Czerner 	struct ext4_features *ef;
5511857ac889SLukas Czerner 	int ret = -ENOMEM;
5512857ac889SLukas Czerner 
5513857ac889SLukas Czerner 	ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
5514857ac889SLukas Czerner 	if (!ef)
5515857ac889SLukas Czerner 		goto out;
5516857ac889SLukas Czerner 
5517857ac889SLukas Czerner 	ef->f_kobj.kset = ext4_kset;
5518857ac889SLukas Czerner 	init_completion(&ef->f_kobj_unregister);
5519857ac889SLukas Czerner 	ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
5520857ac889SLukas Czerner 				   "features");
5521857ac889SLukas Czerner 	if (ret) {
5522857ac889SLukas Czerner 		kfree(ef);
5523857ac889SLukas Czerner 		goto out;
5524857ac889SLukas Czerner 	}
5525857ac889SLukas Czerner 
5526857ac889SLukas Czerner 	ext4_feat = ef;
5527857ac889SLukas Czerner 	ret = 0;
5528857ac889SLukas Czerner out:
5529857ac889SLukas Czerner 	return ret;
5530857ac889SLukas Czerner }
5531857ac889SLukas Czerner 
55328f021222SLukas Czerner static void ext4_exit_feat_adverts(void)
55338f021222SLukas Czerner {
55348f021222SLukas Czerner 	kobject_put(&ext4_feat->f_kobj);
55358f021222SLukas Czerner 	wait_for_completion(&ext4_feat->f_kobj_unregister);
55368f021222SLukas Czerner 	kfree(ext4_feat);
55378f021222SLukas Czerner }
55388f021222SLukas Czerner 
5539e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
5540e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5541e9e3bcecSEric Sandeen struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
5542e9e3bcecSEric Sandeen 
55435dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
5544ac27a0ecSDave Kleikamp {
5545e9e3bcecSEric Sandeen 	int i, err;
5546c9de560dSAlex Tomas 
554707c0c5d8SAl Viro 	ext4_li_info = NULL;
554807c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
554907c0c5d8SAl Viro 
55509a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
555112e9b892SDmitry Monakhov 	ext4_check_flag_values();
5552e9e3bcecSEric Sandeen 
5553e9e3bcecSEric Sandeen 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
5554e9e3bcecSEric Sandeen 		mutex_init(&ext4__aio_mutex[i]);
5555e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
5556e9e3bcecSEric Sandeen 	}
5557e9e3bcecSEric Sandeen 
555851865fdaSZheng Liu 	err = ext4_init_es();
55596fd058f7STheodore Ts'o 	if (err)
55606fd058f7STheodore Ts'o 		return err;
556151865fdaSZheng Liu 
556251865fdaSZheng Liu 	err = ext4_init_pageio();
556351865fdaSZheng Liu 	if (err)
556451865fdaSZheng Liu 		goto out7;
556551865fdaSZheng Liu 
55665dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
5567bd2d0210STheodore Ts'o 	if (err)
5568d44651d0SFabrice Jouhaud 		goto out6;
55693197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
55700e376b1eSTheodore Ts'o 	if (!ext4_kset) {
55710e376b1eSTheodore Ts'o 		err = -ENOMEM;
5572dd68314cSTheodore Ts'o 		goto out5;
55730e376b1eSTheodore Ts'o 	}
5574d44651d0SFabrice Jouhaud 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
5575857ac889SLukas Czerner 
5576857ac889SLukas Czerner 	err = ext4_init_feat_adverts();
5577dd68314cSTheodore Ts'o 	if (err)
5578dd68314cSTheodore Ts'o 		goto out4;
5579857ac889SLukas Czerner 
55805dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
5581ac27a0ecSDave Kleikamp 	if (err)
5582c9de560dSAlex Tomas 		goto out2;
55839c191f70ST Makphaibulchoke 	else
55849c191f70ST Makphaibulchoke 		ext4_mballoc_ready = 1;
5585ac27a0ecSDave Kleikamp 	err = init_inodecache();
5586ac27a0ecSDave Kleikamp 	if (err)
5587ac27a0ecSDave Kleikamp 		goto out1;
558824b58424STheodore Ts'o 	register_as_ext3();
55892035e776STheodore Ts'o 	register_as_ext2();
559003010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
5591ac27a0ecSDave Kleikamp 	if (err)
5592ac27a0ecSDave Kleikamp 		goto out;
5593bfff6873SLukas Czerner 
5594ac27a0ecSDave Kleikamp 	return 0;
5595ac27a0ecSDave Kleikamp out:
559624b58424STheodore Ts'o 	unregister_as_ext2();
559724b58424STheodore Ts'o 	unregister_as_ext3();
5598ac27a0ecSDave Kleikamp 	destroy_inodecache();
5599ac27a0ecSDave Kleikamp out1:
56009c191f70ST Makphaibulchoke 	ext4_mballoc_ready = 0;
56015dabfc78STheodore Ts'o 	ext4_exit_mballoc();
56029c191f70ST Makphaibulchoke out2:
56038f021222SLukas Czerner 	ext4_exit_feat_adverts();
56046fd058f7STheodore Ts'o out4:
5605d44651d0SFabrice Jouhaud 	if (ext4_proc_root)
5606dd68314cSTheodore Ts'o 		remove_proc_entry("fs/ext4", NULL);
5607dd68314cSTheodore Ts'o 	kset_unregister(ext4_kset);
5608d44651d0SFabrice Jouhaud out5:
5609dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5610d44651d0SFabrice Jouhaud out6:
56115dabfc78STheodore Ts'o 	ext4_exit_pageio();
561251865fdaSZheng Liu out7:
561351865fdaSZheng Liu 	ext4_exit_es();
561451865fdaSZheng Liu 
5615ac27a0ecSDave Kleikamp 	return err;
5616ac27a0ecSDave Kleikamp }
5617ac27a0ecSDave Kleikamp 
56185dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5619ac27a0ecSDave Kleikamp {
5620bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
562124b58424STheodore Ts'o 	unregister_as_ext2();
562224b58424STheodore Ts'o 	unregister_as_ext3();
562303010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5624ac27a0ecSDave Kleikamp 	destroy_inodecache();
56255dabfc78STheodore Ts'o 	ext4_exit_mballoc();
56268f021222SLukas Czerner 	ext4_exit_feat_adverts();
56279f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
56283197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
56295dabfc78STheodore Ts'o 	ext4_exit_system_zone();
56305dabfc78STheodore Ts'o 	ext4_exit_pageio();
5631dd12ed14SEric Sandeen 	ext4_exit_es();
5632ac27a0ecSDave Kleikamp }
5633ac27a0ecSDave Kleikamp 
5634ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
563583982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5636ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
56375dabfc78STheodore Ts'o module_init(ext4_init_fs)
56385dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5639