xref: /linux/fs/ext4/super.c (revision eb513689c97e3e73bb9b4459d490a8e894b4a546)
1ac27a0ecSDave Kleikamp /*
2617ba13bSMingming Cao  *  linux/fs/ext4/super.c
3ac27a0ecSDave Kleikamp  *
4ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
5ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
6ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
7ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
8ac27a0ecSDave Kleikamp  *
9ac27a0ecSDave Kleikamp  *  from
10ac27a0ecSDave Kleikamp  *
11ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
12ac27a0ecSDave Kleikamp  *
13ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
14ac27a0ecSDave Kleikamp  *
15ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
16ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
17ac27a0ecSDave Kleikamp  */
18ac27a0ecSDave Kleikamp 
19ac27a0ecSDave Kleikamp #include <linux/module.h>
20ac27a0ecSDave Kleikamp #include <linux/string.h>
21ac27a0ecSDave Kleikamp #include <linux/fs.h>
22ac27a0ecSDave Kleikamp #include <linux/time.h>
23c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
24dab291afSMingming Cao #include <linux/jbd2.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
28ac27a0ecSDave Kleikamp #include <linux/parser.h>
29ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
30a5694255SChristoph Hellwig #include <linux/exportfs.h>
31ac27a0ecSDave Kleikamp #include <linux/vfs.h>
32ac27a0ecSDave Kleikamp #include <linux/random.h>
33ac27a0ecSDave Kleikamp #include <linux/mount.h>
34ac27a0ecSDave Kleikamp #include <linux/namei.h>
35ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
36ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
379f6200bbSTheodore Ts'o #include <linux/proc_fs.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
417abc52c2SDan Magenheimer #include <linux/cleancache.h>
42ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
43ac27a0ecSDave Kleikamp 
44bfff6873SLukas Czerner #include <linux/kthread.h>
45bfff6873SLukas Czerner #include <linux/freezer.h>
46bfff6873SLukas Czerner 
473dcf5451SChristoph Hellwig #include "ext4.h"
486f91bc5fSEric Gouriou #include "ext4_extents.h"
493dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
50ac27a0ecSDave Kleikamp #include "xattr.h"
51ac27a0ecSDave Kleikamp #include "acl.h"
523661d286STheodore Ts'o #include "mballoc.h"
53ac27a0ecSDave Kleikamp 
549bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
559bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
569bffad1eSTheodore Ts'o 
571f109d5aSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root;
583197ebdbSTheodore Ts'o static struct kset *ext4_kset;
590b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
600b75a840SLukas Czerner static struct mutex ext4_li_mtx;
610b75a840SLukas Czerner static struct ext4_features *ext4_feat;
629f6200bbSTheodore Ts'o 
63617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
64ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
65e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
66617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
67617ba13bSMingming Cao 					struct ext4_super_block *es);
68617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
69617ba13bSMingming Cao 				   struct ext4_super_block *es);
70617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
71617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
72ac27a0ecSDave Kleikamp 				     char nbuf[16]);
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);
76617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
78152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
79152a0836SAl Viro 		       const char *dev_name, void *data);
802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
82d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
83bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
84bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
858f1f7453SEric Sandeen static void ext4_clear_request_list(void);
86ac27a0ecSDave Kleikamp 
872035e776STheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
882035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
892035e776STheodore Ts'o 	.owner		= THIS_MODULE,
902035e776STheodore Ts'o 	.name		= "ext2",
912035e776STheodore Ts'o 	.mount		= ext4_mount,
922035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
932035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
942035e776STheodore Ts'o };
952035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
962035e776STheodore Ts'o #else
972035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
982035e776STheodore Ts'o #endif
992035e776STheodore Ts'o 
1002035e776STheodore Ts'o 
101ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
102ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
103ba69f9abSJan Kara 	.owner		= THIS_MODULE,
104ba69f9abSJan Kara 	.name		= "ext3",
105152a0836SAl Viro 	.mount		= ext4_mount,
106ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
107ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
108ba69f9abSJan Kara };
109ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
110ba69f9abSJan Kara #else
111ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0)
112ba69f9abSJan Kara #endif
113bd81d8eeSLaurent Vivier 
1149933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
1159933fc0aSTheodore Ts'o {
1169933fc0aSTheodore Ts'o 	void *ret;
1179933fc0aSTheodore Ts'o 
1189933fc0aSTheodore Ts'o 	ret = kmalloc(size, flags);
1199933fc0aSTheodore Ts'o 	if (!ret)
1209933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
1219933fc0aSTheodore Ts'o 	return ret;
1229933fc0aSTheodore Ts'o }
1239933fc0aSTheodore Ts'o 
1249933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
1259933fc0aSTheodore Ts'o {
1269933fc0aSTheodore Ts'o 	void *ret;
1279933fc0aSTheodore Ts'o 
128db9481c0SMathias Krause 	ret = kzalloc(size, flags);
1299933fc0aSTheodore Ts'o 	if (!ret)
1309933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
1319933fc0aSTheodore Ts'o 	return ret;
1329933fc0aSTheodore Ts'o }
1339933fc0aSTheodore Ts'o 
1349933fc0aSTheodore Ts'o void ext4_kvfree(void *ptr)
1359933fc0aSTheodore Ts'o {
1369933fc0aSTheodore Ts'o 	if (is_vmalloc_addr(ptr))
1379933fc0aSTheodore Ts'o 		vfree(ptr);
1389933fc0aSTheodore Ts'o 	else
1399933fc0aSTheodore Ts'o 		kfree(ptr);
1409933fc0aSTheodore Ts'o 
1419933fc0aSTheodore Ts'o }
1429933fc0aSTheodore Ts'o 
1438fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
1448fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
145bd81d8eeSLaurent Vivier {
1463a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
1478fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1488fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
149bd81d8eeSLaurent Vivier }
150bd81d8eeSLaurent Vivier 
1518fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
1528fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
153bd81d8eeSLaurent Vivier {
1545272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
1558fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1568fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
157bd81d8eeSLaurent Vivier }
158bd81d8eeSLaurent Vivier 
1598fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
1608fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
161bd81d8eeSLaurent Vivier {
1625272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
1638fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1648fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
165bd81d8eeSLaurent Vivier }
166bd81d8eeSLaurent Vivier 
167021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
168560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
169560671a0SAneesh Kumar K.V {
170560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
171560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
172560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
173560671a0SAneesh Kumar K.V }
174560671a0SAneesh Kumar K.V 
175560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
176560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
177560671a0SAneesh Kumar K.V {
178560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
179560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
180560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
181560671a0SAneesh Kumar K.V }
182560671a0SAneesh Kumar K.V 
183560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
184560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
185560671a0SAneesh Kumar K.V {
186560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
187560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
188560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
189560671a0SAneesh Kumar K.V }
190560671a0SAneesh Kumar K.V 
191560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
192560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
193560671a0SAneesh Kumar K.V {
194560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
195560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
196560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
197560671a0SAneesh Kumar K.V }
198560671a0SAneesh Kumar K.V 
1998fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2008fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
201bd81d8eeSLaurent Vivier {
2023a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2038fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2048fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
205bd81d8eeSLaurent Vivier }
206bd81d8eeSLaurent Vivier 
2078fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2088fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
209bd81d8eeSLaurent Vivier {
2105272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2118fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2128fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
213bd81d8eeSLaurent Vivier }
214bd81d8eeSLaurent Vivier 
2158fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2168fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
217bd81d8eeSLaurent Vivier {
2185272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2198fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2208fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
221bd81d8eeSLaurent Vivier }
222bd81d8eeSLaurent Vivier 
223021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
224560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
225560671a0SAneesh Kumar K.V {
226560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
227560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
228560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
229560671a0SAneesh Kumar K.V }
230560671a0SAneesh Kumar K.V 
231560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
232560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
233560671a0SAneesh Kumar K.V {
234560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
235560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
236560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
237560671a0SAneesh Kumar K.V }
238560671a0SAneesh Kumar K.V 
239560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
240560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
241560671a0SAneesh Kumar K.V {
242560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
243560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
244560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
245560671a0SAneesh Kumar K.V }
246560671a0SAneesh Kumar K.V 
247560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
248560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
249560671a0SAneesh Kumar K.V {
250560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
251560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
252560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
253560671a0SAneesh Kumar K.V }
254560671a0SAneesh Kumar K.V 
255d3d1faf6SCurt Wohlgemuth 
256d3d1faf6SCurt Wohlgemuth /* Just increment the non-pointer handle value */
257d3d1faf6SCurt Wohlgemuth static handle_t *ext4_get_nojournal(void)
258d3d1faf6SCurt Wohlgemuth {
259d3d1faf6SCurt Wohlgemuth 	handle_t *handle = current->journal_info;
260d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
261d3d1faf6SCurt Wohlgemuth 
262d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
263d3d1faf6SCurt Wohlgemuth 
264d3d1faf6SCurt Wohlgemuth 	ref_cnt++;
265d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
266d3d1faf6SCurt Wohlgemuth 
267d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
268d3d1faf6SCurt Wohlgemuth 	return handle;
269d3d1faf6SCurt Wohlgemuth }
270d3d1faf6SCurt Wohlgemuth 
271d3d1faf6SCurt Wohlgemuth 
272d3d1faf6SCurt Wohlgemuth /* Decrement the non-pointer handle value */
273d3d1faf6SCurt Wohlgemuth static void ext4_put_nojournal(handle_t *handle)
274d3d1faf6SCurt Wohlgemuth {
275d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
276d3d1faf6SCurt Wohlgemuth 
277d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt == 0);
278d3d1faf6SCurt Wohlgemuth 
279d3d1faf6SCurt Wohlgemuth 	ref_cnt--;
280d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
281d3d1faf6SCurt Wohlgemuth 
282d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
283d3d1faf6SCurt Wohlgemuth }
284d3d1faf6SCurt Wohlgemuth 
285ac27a0ecSDave Kleikamp /*
286dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
287ac27a0ecSDave Kleikamp  *
288ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
289ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
290ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
291ac27a0ecSDave Kleikamp  * appropriate.
292be4f27d3SYongqiang Yang  *
293be4f27d3SYongqiang Yang  * To avoid j_barrier hold in userspace when a user calls freeze(),
294be4f27d3SYongqiang Yang  * ext4 prevents a new handle from being started by s_frozen, which
295be4f27d3SYongqiang Yang  * is in an upper layer.
296ac27a0ecSDave Kleikamp  */
297617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
298ac27a0ecSDave Kleikamp {
299ac27a0ecSDave Kleikamp 	journal_t *journal;
300be4f27d3SYongqiang Yang 	handle_t  *handle;
301ac27a0ecSDave Kleikamp 
30212706394STheodore Ts'o 	trace_ext4_journal_start(sb, nblocks, _RET_IP_);
303ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
304ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
305ac27a0ecSDave Kleikamp 
306617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
307be4f27d3SYongqiang Yang 	handle = ext4_journal_current_handle();
308be4f27d3SYongqiang Yang 
309be4f27d3SYongqiang Yang 	/*
310be4f27d3SYongqiang Yang 	 * If a handle has been started, it should be allowed to
311be4f27d3SYongqiang Yang 	 * finish, otherwise deadlock could happen between freeze
312be4f27d3SYongqiang Yang 	 * and others(e.g. truncate) due to the restart of the
313be4f27d3SYongqiang Yang 	 * journal handle if the filesystem is forzen and active
314be4f27d3SYongqiang Yang 	 * handles are not stopped.
315be4f27d3SYongqiang Yang 	 */
316be4f27d3SYongqiang Yang 	if (!handle)
317be4f27d3SYongqiang Yang 		vfs_check_frozen(sb, SB_FREEZE_TRANS);
318be4f27d3SYongqiang Yang 
319be4f27d3SYongqiang Yang 	if (!journal)
320be4f27d3SYongqiang Yang 		return ext4_get_nojournal();
321be4f27d3SYongqiang Yang 	/*
322be4f27d3SYongqiang Yang 	 * Special case here: if the journal has aborted behind our
323be4f27d3SYongqiang Yang 	 * backs (eg. EIO in the commit thread), then we still need to
324be4f27d3SYongqiang Yang 	 * take the FS itself readonly cleanly.
325be4f27d3SYongqiang Yang 	 */
326ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
327c67d859eSTheodore Ts'o 		ext4_abort(sb, "Detected aborted journal");
328ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
329ac27a0ecSDave Kleikamp 	}
330dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
331ac27a0ecSDave Kleikamp }
332ac27a0ecSDave Kleikamp 
333ac27a0ecSDave Kleikamp /*
334ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
335dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
336ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
337ac27a0ecSDave Kleikamp  * appropriate.
338ac27a0ecSDave Kleikamp  */
339c398eda0STheodore Ts'o int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
340ac27a0ecSDave Kleikamp {
341ac27a0ecSDave Kleikamp 	struct super_block *sb;
342ac27a0ecSDave Kleikamp 	int err;
343ac27a0ecSDave Kleikamp 	int rc;
344ac27a0ecSDave Kleikamp 
3450390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
346d3d1faf6SCurt Wohlgemuth 		ext4_put_nojournal(handle);
3470390131bSFrank Mayhar 		return 0;
3480390131bSFrank Mayhar 	}
349ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
350ac27a0ecSDave Kleikamp 	err = handle->h_err;
351dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
352ac27a0ecSDave Kleikamp 
353ac27a0ecSDave Kleikamp 	if (!err)
354ac27a0ecSDave Kleikamp 		err = rc;
355ac27a0ecSDave Kleikamp 	if (err)
356c398eda0STheodore Ts'o 		__ext4_std_error(sb, where, line, err);
357ac27a0ecSDave Kleikamp 	return err;
358ac27a0ecSDave Kleikamp }
359ac27a0ecSDave Kleikamp 
36090c7201bSTheodore Ts'o void ext4_journal_abort_handle(const char *caller, unsigned int line,
36190c7201bSTheodore Ts'o 			       const char *err_fn, struct buffer_head *bh,
36290c7201bSTheodore Ts'o 			       handle_t *handle, int err)
363ac27a0ecSDave Kleikamp {
364ac27a0ecSDave Kleikamp 	char nbuf[16];
365617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
366ac27a0ecSDave Kleikamp 
3670390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
3680390131bSFrank Mayhar 
369ac27a0ecSDave Kleikamp 	if (bh)
370ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
371ac27a0ecSDave Kleikamp 
372ac27a0ecSDave Kleikamp 	if (!handle->h_err)
373ac27a0ecSDave Kleikamp 		handle->h_err = err;
374ac27a0ecSDave Kleikamp 
375ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
376ac27a0ecSDave Kleikamp 		return;
377ac27a0ecSDave Kleikamp 
37890c7201bSTheodore Ts'o 	printk(KERN_ERR "%s:%d: aborting transaction: %s in %s\n",
37990c7201bSTheodore Ts'o 	       caller, line, errstr, err_fn);
380ac27a0ecSDave Kleikamp 
381dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
382ac27a0ecSDave Kleikamp }
383ac27a0ecSDave Kleikamp 
3841c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3851c13d5c0STheodore Ts'o 			    unsigned int line)
3861c13d5c0STheodore Ts'o {
3871c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3881c13d5c0STheodore Ts'o 
3891c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3901c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3911c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
3921c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3931c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3941c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3951c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3961c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3971c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3981c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3991c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
4001c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
4011c13d5c0STheodore Ts'o 	}
40266e61a9eSTheodore Ts'o 	/*
40366e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
40466e61a9eSTheodore Ts'o 	 * started already
40566e61a9eSTheodore Ts'o 	 */
40666e61a9eSTheodore Ts'o 	if (!es->s_error_count)
40766e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
4081c13d5c0STheodore Ts'o 	es->s_error_count = cpu_to_le32(le32_to_cpu(es->s_error_count) + 1);
4091c13d5c0STheodore Ts'o }
4101c13d5c0STheodore Ts'o 
4111c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
4121c13d5c0STheodore Ts'o 			    unsigned int line)
4131c13d5c0STheodore Ts'o {
4141c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
4151c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
4161c13d5c0STheodore Ts'o }
4171c13d5c0STheodore Ts'o 
4187c2e7087STheodore Ts'o /*
4197c2e7087STheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
4207c2e7087STheodore Ts'o  * structures, including the bdi structure, without telling anyone
4217c2e7087STheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
4227c2e7087STheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
4237c2e7087STheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
4247c2e7087STheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
4257c2e7087STheodore Ts'o  */
4267c2e7087STheodore Ts'o static int block_device_ejected(struct super_block *sb)
4277c2e7087STheodore Ts'o {
4287c2e7087STheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
4297c2e7087STheodore Ts'o 	struct backing_dev_info *bdi = bd_inode->i_mapping->backing_dev_info;
4307c2e7087STheodore Ts'o 
4317c2e7087STheodore Ts'o 	return bdi->dev == NULL;
4327c2e7087STheodore Ts'o }
4337c2e7087STheodore Ts'o 
4341c13d5c0STheodore Ts'o 
435ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
436ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
437ac27a0ecSDave Kleikamp  *
438ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
439617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
440ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
441ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
442ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
443ac27a0ecSDave Kleikamp  * write out the superblock safely.
444ac27a0ecSDave Kleikamp  *
445dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
446d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
447ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
448ac27a0ecSDave Kleikamp  */
449ac27a0ecSDave Kleikamp 
450617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
451ac27a0ecSDave Kleikamp {
452ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
453ac27a0ecSDave Kleikamp 		return;
454ac27a0ecSDave Kleikamp 
455ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
456617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
457ac27a0ecSDave Kleikamp 
4584ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
459ac27a0ecSDave Kleikamp 		if (journal)
460dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
461ac27a0ecSDave Kleikamp 	}
462ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
463b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
464ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
465ac27a0ecSDave Kleikamp 	}
466ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
467617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
468ac27a0ecSDave Kleikamp 			sb->s_id);
469ac27a0ecSDave Kleikamp }
470ac27a0ecSDave Kleikamp 
47112062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
472c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
473ac27a0ecSDave Kleikamp {
4740ff2ea7dSJoe Perches 	struct va_format vaf;
475ac27a0ecSDave Kleikamp 	va_list args;
476ac27a0ecSDave Kleikamp 
477ac27a0ecSDave Kleikamp 	va_start(args, fmt);
4780ff2ea7dSJoe Perches 	vaf.fmt = fmt;
4790ff2ea7dSJoe Perches 	vaf.va = &args;
4800ff2ea7dSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4810ff2ea7dSJoe Perches 	       sb->s_id, function, line, current->comm, &vaf);
482ac27a0ecSDave Kleikamp 	va_end(args);
483ac27a0ecSDave Kleikamp 
484617ba13bSMingming Cao 	ext4_handle_error(sb);
485ac27a0ecSDave Kleikamp }
486ac27a0ecSDave Kleikamp 
487c398eda0STheodore Ts'o void ext4_error_inode(struct inode *inode, const char *function,
488c398eda0STheodore Ts'o 		      unsigned int line, ext4_fsblk_t block,
489273df556SFrank Mayhar 		      const char *fmt, ...)
490273df556SFrank Mayhar {
491273df556SFrank Mayhar 	va_list args;
492f7c21177STheodore Ts'o 	struct va_format vaf;
4931c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
494273df556SFrank Mayhar 
4951c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4961c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
4971c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
498273df556SFrank Mayhar 	va_start(args, fmt);
499f7c21177STheodore Ts'o 	vaf.fmt = fmt;
500f7c21177STheodore Ts'o 	vaf.va = &args;
501c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
502c398eda0STheodore Ts'o 	       inode->i_sb->s_id, function, line, inode->i_ino);
503c398eda0STheodore Ts'o 	if (block)
504f7c21177STheodore Ts'o 		printk(KERN_CONT "block %llu: ", block);
505f7c21177STheodore Ts'o 	printk(KERN_CONT "comm %s: %pV\n", current->comm, &vaf);
506273df556SFrank Mayhar 	va_end(args);
507273df556SFrank Mayhar 
508273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
509273df556SFrank Mayhar }
510273df556SFrank Mayhar 
511c398eda0STheodore Ts'o void ext4_error_file(struct file *file, const char *function,
512f7c21177STheodore Ts'o 		     unsigned int line, ext4_fsblk_t block,
513f7c21177STheodore Ts'o 		     const char *fmt, ...)
514273df556SFrank Mayhar {
515273df556SFrank Mayhar 	va_list args;
516f7c21177STheodore Ts'o 	struct va_format vaf;
5171c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
518273df556SFrank Mayhar 	struct inode *inode = file->f_dentry->d_inode;
519273df556SFrank Mayhar 	char pathname[80], *path;
520273df556SFrank Mayhar 
5211c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
5221c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
5231c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
524273df556SFrank Mayhar 	path = d_path(&(file->f_path), pathname, sizeof(pathname));
525f9a62d09SDan Carpenter 	if (IS_ERR(path))
526273df556SFrank Mayhar 		path = "(unknown)";
527273df556SFrank Mayhar 	printk(KERN_CRIT
528f7c21177STheodore Ts'o 	       "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
529f7c21177STheodore Ts'o 	       inode->i_sb->s_id, function, line, inode->i_ino);
530f7c21177STheodore Ts'o 	if (block)
531f7c21177STheodore Ts'o 		printk(KERN_CONT "block %llu: ", block);
532f7c21177STheodore Ts'o 	va_start(args, fmt);
533f7c21177STheodore Ts'o 	vaf.fmt = fmt;
534f7c21177STheodore Ts'o 	vaf.va = &args;
535f7c21177STheodore Ts'o 	printk(KERN_CONT "comm %s: path %s: %pV\n", current->comm, path, &vaf);
536273df556SFrank Mayhar 	va_end(args);
537273df556SFrank Mayhar 
538273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
539273df556SFrank Mayhar }
540273df556SFrank Mayhar 
541617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
542ac27a0ecSDave Kleikamp 				     char nbuf[16])
543ac27a0ecSDave Kleikamp {
544ac27a0ecSDave Kleikamp 	char *errstr = NULL;
545ac27a0ecSDave Kleikamp 
546ac27a0ecSDave Kleikamp 	switch (errno) {
547ac27a0ecSDave Kleikamp 	case -EIO:
548ac27a0ecSDave Kleikamp 		errstr = "IO failure";
549ac27a0ecSDave Kleikamp 		break;
550ac27a0ecSDave Kleikamp 	case -ENOMEM:
551ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
552ac27a0ecSDave Kleikamp 		break;
553ac27a0ecSDave Kleikamp 	case -EROFS:
55478f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
55578f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
556ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
557ac27a0ecSDave Kleikamp 		else
558ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
559ac27a0ecSDave Kleikamp 		break;
560ac27a0ecSDave Kleikamp 	default:
561ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
562ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
563ac27a0ecSDave Kleikamp 		 * NULL. */
564ac27a0ecSDave Kleikamp 		if (nbuf) {
565ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
566ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
567ac27a0ecSDave Kleikamp 				errstr = nbuf;
568ac27a0ecSDave Kleikamp 		}
569ac27a0ecSDave Kleikamp 		break;
570ac27a0ecSDave Kleikamp 	}
571ac27a0ecSDave Kleikamp 
572ac27a0ecSDave Kleikamp 	return errstr;
573ac27a0ecSDave Kleikamp }
574ac27a0ecSDave Kleikamp 
575617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
576ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
577ac27a0ecSDave Kleikamp 
578c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
579c398eda0STheodore Ts'o 		      unsigned int line, int errno)
580ac27a0ecSDave Kleikamp {
581ac27a0ecSDave Kleikamp 	char nbuf[16];
582ac27a0ecSDave Kleikamp 	const char *errstr;
583ac27a0ecSDave Kleikamp 
584ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
585ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
586ac27a0ecSDave Kleikamp 	 * an error. */
587ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
588ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
589ac27a0ecSDave Kleikamp 		return;
590ac27a0ecSDave Kleikamp 
591617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
592c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
593c398eda0STheodore Ts'o 	       sb->s_id, function, line, errstr);
5941c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
595ac27a0ecSDave Kleikamp 
596617ba13bSMingming Cao 	ext4_handle_error(sb);
597ac27a0ecSDave Kleikamp }
598ac27a0ecSDave Kleikamp 
599ac27a0ecSDave Kleikamp /*
600617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
601ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
602ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
603ac27a0ecSDave Kleikamp  *
604ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
605ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
606ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
607ac27a0ecSDave Kleikamp  */
608ac27a0ecSDave Kleikamp 
609c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
610c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
611ac27a0ecSDave Kleikamp {
612ac27a0ecSDave Kleikamp 	va_list args;
613ac27a0ecSDave Kleikamp 
6141c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
615ac27a0ecSDave Kleikamp 	va_start(args, fmt);
616c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
617c398eda0STheodore Ts'o 	       function, line);
618ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
619ac27a0ecSDave Kleikamp 	printk("\n");
620ac27a0ecSDave Kleikamp 	va_end(args);
621ac27a0ecSDave Kleikamp 
6221c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
623b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
624ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
6254ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
626ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
627dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
6281c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
6291c13d5c0STheodore Ts'o 	}
6301c13d5c0STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC))
6311c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
632ac27a0ecSDave Kleikamp }
633ac27a0ecSDave Kleikamp 
6340ff2ea7dSJoe Perches void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
635b31e1552SEric Sandeen {
6360ff2ea7dSJoe Perches 	struct va_format vaf;
637b31e1552SEric Sandeen 	va_list args;
638b31e1552SEric Sandeen 
639b31e1552SEric Sandeen 	va_start(args, fmt);
6400ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6410ff2ea7dSJoe Perches 	vaf.va = &args;
6420ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
643b31e1552SEric Sandeen 	va_end(args);
644b31e1552SEric Sandeen }
645b31e1552SEric Sandeen 
64612062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
647c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
648ac27a0ecSDave Kleikamp {
6490ff2ea7dSJoe Perches 	struct va_format vaf;
650ac27a0ecSDave Kleikamp 	va_list args;
651ac27a0ecSDave Kleikamp 
652ac27a0ecSDave Kleikamp 	va_start(args, fmt);
6530ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6540ff2ea7dSJoe Perches 	vaf.va = &args;
6550ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
6560ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
657ac27a0ecSDave Kleikamp 	va_end(args);
658ac27a0ecSDave Kleikamp }
659ac27a0ecSDave Kleikamp 
660e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
661e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
662e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
663e29136f8STheodore Ts'o 			     const char *fmt, ...)
6645d1b1b3fSAneesh Kumar K.V __releases(bitlock)
6655d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
6665d1b1b3fSAneesh Kumar K.V {
6670ff2ea7dSJoe Perches 	struct va_format vaf;
6685d1b1b3fSAneesh Kumar K.V 	va_list args;
6695d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
6705d1b1b3fSAneesh Kumar K.V 
6711c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
6721c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
6731c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
6740ff2ea7dSJoe Perches 
6755d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
6760ff2ea7dSJoe Perches 
6770ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6780ff2ea7dSJoe Perches 	vaf.va = &args;
67921149d61SRobin Dong 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
680e29136f8STheodore Ts'o 	       sb->s_id, function, line, grp);
681e29136f8STheodore Ts'o 	if (ino)
6820ff2ea7dSJoe Perches 		printk(KERN_CONT "inode %lu: ", ino);
683e29136f8STheodore Ts'o 	if (block)
6840ff2ea7dSJoe Perches 		printk(KERN_CONT "block %llu:", (unsigned long long) block);
6850ff2ea7dSJoe Perches 	printk(KERN_CONT "%pV\n", &vaf);
6865d1b1b3fSAneesh Kumar K.V 	va_end(args);
6875d1b1b3fSAneesh Kumar K.V 
6885d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
689e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
6905d1b1b3fSAneesh Kumar K.V 		return;
6915d1b1b3fSAneesh Kumar K.V 	}
6921c13d5c0STheodore Ts'o 
6935d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
6945d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
6955d1b1b3fSAneesh Kumar K.V 	/*
6965d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
6975d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
6985d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
6995d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
7005d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
70125985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
7025d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
7035d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
7045d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
7055d1b1b3fSAneesh Kumar K.V 	 */
7065d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
7075d1b1b3fSAneesh Kumar K.V 	return;
7085d1b1b3fSAneesh Kumar K.V }
7095d1b1b3fSAneesh Kumar K.V 
710617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
711ac27a0ecSDave Kleikamp {
712617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
713ac27a0ecSDave Kleikamp 
714617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
715ac27a0ecSDave Kleikamp 		return;
716ac27a0ecSDave Kleikamp 
71712062dddSEric Sandeen 	ext4_warning(sb,
718ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
719ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
720617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
721ac27a0ecSDave Kleikamp 
722617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
723617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
724617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
725ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
726ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
727ac27a0ecSDave Kleikamp 
728ac27a0ecSDave Kleikamp 	/*
729ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
730ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
731ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
732ac27a0ecSDave Kleikamp 	 */
733ac27a0ecSDave Kleikamp }
734ac27a0ecSDave Kleikamp 
735ac27a0ecSDave Kleikamp /*
736ac27a0ecSDave Kleikamp  * Open the external journal device
737ac27a0ecSDave Kleikamp  */
738b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
739ac27a0ecSDave Kleikamp {
740ac27a0ecSDave Kleikamp 	struct block_device *bdev;
741ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
742ac27a0ecSDave Kleikamp 
743d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
744ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
745ac27a0ecSDave Kleikamp 		goto fail;
746ac27a0ecSDave Kleikamp 	return bdev;
747ac27a0ecSDave Kleikamp 
748ac27a0ecSDave Kleikamp fail:
749b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
750ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
751ac27a0ecSDave Kleikamp 	return NULL;
752ac27a0ecSDave Kleikamp }
753ac27a0ecSDave Kleikamp 
754ac27a0ecSDave Kleikamp /*
755ac27a0ecSDave Kleikamp  * Release the journal device
756ac27a0ecSDave Kleikamp  */
757617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
758ac27a0ecSDave Kleikamp {
759e525fd89STejun Heo 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
760ac27a0ecSDave Kleikamp }
761ac27a0ecSDave Kleikamp 
762617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
763ac27a0ecSDave Kleikamp {
764ac27a0ecSDave Kleikamp 	struct block_device *bdev;
765ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
766ac27a0ecSDave Kleikamp 
767ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
768ac27a0ecSDave Kleikamp 	if (bdev) {
769617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
770ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
771ac27a0ecSDave Kleikamp 	}
772ac27a0ecSDave Kleikamp 	return ret;
773ac27a0ecSDave Kleikamp }
774ac27a0ecSDave Kleikamp 
775ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
776ac27a0ecSDave Kleikamp {
777617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
778ac27a0ecSDave Kleikamp }
779ac27a0ecSDave Kleikamp 
780617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
781ac27a0ecSDave Kleikamp {
782ac27a0ecSDave Kleikamp 	struct list_head *l;
783ac27a0ecSDave Kleikamp 
784b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
785ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
786ac27a0ecSDave Kleikamp 
787ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
788ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
789ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
790ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
791ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
792ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
793ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
794ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
795ac27a0ecSDave Kleikamp 	}
796ac27a0ecSDave Kleikamp }
797ac27a0ecSDave Kleikamp 
798617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
799ac27a0ecSDave Kleikamp {
800617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
801617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
802ef2cabf7SHidehiro Kawai 	int i, err;
803ac27a0ecSDave Kleikamp 
804857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
805e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
806e0ccfd95SChristoph Hellwig 
8074c0425ffSMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
8084c0425ffSMingming Cao 	destroy_workqueue(sbi->dio_unwritten_wq);
8094c0425ffSMingming Cao 
810a9e220f8SAl Viro 	lock_super(sb);
8118c85e125SChristoph Hellwig 	if (sb->s_dirt)
812ebc1ac16SChristoph Hellwig 		ext4_commit_super(sb, 1);
8138c85e125SChristoph Hellwig 
8140390131bSFrank Mayhar 	if (sbi->s_journal) {
815ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
81647b4a50bSJan Kara 		sbi->s_journal = NULL;
817ef2cabf7SHidehiro Kawai 		if (err < 0)
818c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
8190390131bSFrank Mayhar 	}
820d4edac31SJosef Bacik 
821a1c6c569SSergey Senozhatsky 	del_timer(&sbi->s_err_report);
822d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
823d4edac31SJosef Bacik 	ext4_mb_release(sb);
824d4edac31SJosef Bacik 	ext4_ext_release(sb);
825d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
826d4edac31SJosef Bacik 
827ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
828617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
829ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
830e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
831ac27a0ecSDave Kleikamp 	}
832240799cdSTheodore Ts'o 	if (sbi->s_proc) {
8339f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
834240799cdSTheodore Ts'o 	}
8353197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
836ac27a0ecSDave Kleikamp 
837ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
838ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
839f18a5f21STheodore Ts'o 	ext4_kvfree(sbi->s_group_desc);
8409933fc0aSTheodore Ts'o 	ext4_kvfree(sbi->s_flex_groups);
84157042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
842ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
843ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
84457042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
845ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
846ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
847ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
848ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
849ac27a0ecSDave Kleikamp #endif
850ac27a0ecSDave Kleikamp 
851ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
852ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
853ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
854ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
855ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
856ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
857ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
858ac27a0ecSDave Kleikamp 
859f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
860ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
861ac27a0ecSDave Kleikamp 		/*
862ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
863ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
864ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
865ac27a0ecSDave Kleikamp 		 */
866ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
867f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
868617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
869ac27a0ecSDave Kleikamp 	}
870c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
871c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
872ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
8733197ebdbSTheodore Ts'o 	/*
8743197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
8753197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
8763197ebdbSTheodore Ts'o 	 */
8773197ebdbSTheodore Ts'o 	unlock_super(sb);
8783197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
8793197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
880705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
881ac27a0ecSDave Kleikamp 	kfree(sbi);
882ac27a0ecSDave Kleikamp }
883ac27a0ecSDave Kleikamp 
884e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
885ac27a0ecSDave Kleikamp 
886ac27a0ecSDave Kleikamp /*
887ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
888ac27a0ecSDave Kleikamp  */
889617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
890ac27a0ecSDave Kleikamp {
891617ba13bSMingming Cao 	struct ext4_inode_info *ei;
892ac27a0ecSDave Kleikamp 
893e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
894ac27a0ecSDave Kleikamp 	if (!ei)
895ac27a0ecSDave Kleikamp 		return NULL;
8960b8e58a1SAndreas Dilger 
897ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
89891246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
899a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
900c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
901c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
902d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
903d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
904d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
9059d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
906d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
907a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
908a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
909a9e7f447SDmitry Monakhov #endif
9108aefcd55STheodore Ts'o 	ei->jinode = NULL;
911c7064ef1SJiaying Zhang 	INIT_LIST_HEAD(&ei->i_completed_io_list);
912744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
9138d5d02e6SMingming Cao 	ei->cur_aio_dio = NULL;
914b436b9beSJan Kara 	ei->i_sync_tid = 0;
915b436b9beSJan Kara 	ei->i_datasync_tid = 0;
916f7ad6d2eSTheodore Ts'o 	atomic_set(&ei->i_ioend_count, 0);
917e9e3bcecSEric Sandeen 	atomic_set(&ei->i_aiodio_unwritten, 0);
9180b8e58a1SAndreas Dilger 
919ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
920ac27a0ecSDave Kleikamp }
921ac27a0ecSDave Kleikamp 
9227ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
9237ff9c073STheodore Ts'o {
9247ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
9257ff9c073STheodore Ts'o 
9267ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
9277ff9c073STheodore Ts'o 	return drop;
9287ff9c073STheodore Ts'o }
9297ff9c073STheodore Ts'o 
930fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
931fa0d7e3dSNick Piggin {
932fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
933fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
934fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
935fa0d7e3dSNick Piggin }
936fa0d7e3dSNick Piggin 
937617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
938ac27a0ecSDave Kleikamp {
9399f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
940b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
941b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
942b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
9439f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
9449f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
9459f7dd93dSVasily Averin 				true);
9469f7dd93dSVasily Averin 		dump_stack();
9479f7dd93dSVasily Averin 	}
948fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
949ac27a0ecSDave Kleikamp }
950ac27a0ecSDave Kleikamp 
95151cc5068SAlexey Dobriyan static void init_once(void *foo)
952ac27a0ecSDave Kleikamp {
953617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
954ac27a0ecSDave Kleikamp 
955ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
95603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
957ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
958ac27a0ecSDave Kleikamp #endif
9590e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
960ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
961ac27a0ecSDave Kleikamp }
962ac27a0ecSDave Kleikamp 
963ac27a0ecSDave Kleikamp static int init_inodecache(void)
964ac27a0ecSDave Kleikamp {
965617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
966617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
967ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
968ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
96920c2df83SPaul Mundt 					     init_once);
970617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
971ac27a0ecSDave Kleikamp 		return -ENOMEM;
972ac27a0ecSDave Kleikamp 	return 0;
973ac27a0ecSDave Kleikamp }
974ac27a0ecSDave Kleikamp 
975ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
976ac27a0ecSDave Kleikamp {
977617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
978ac27a0ecSDave Kleikamp }
979ac27a0ecSDave Kleikamp 
9800930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
981ac27a0ecSDave Kleikamp {
9820930fcc1SAl Viro 	invalidate_inode_buffers(inode);
9830930fcc1SAl Viro 	end_writeback(inode);
9849f754758SChristoph Hellwig 	dquot_drop(inode);
985c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
9868aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
9878aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
9888aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
9898aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
9908aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
9918aefcd55STheodore Ts'o 	}
992ac27a0ecSDave Kleikamp }
993ac27a0ecSDave Kleikamp 
9942b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
9952b2d6d01STheodore Ts'o 					   struct super_block *sb)
996ac27a0ecSDave Kleikamp {
997ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
998617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
999ac27a0ecSDave Kleikamp 
10005a20bdfcSJan Kara 	if (sbi->s_jquota_fmt) {
10015a20bdfcSJan Kara 		char *fmtname = "";
10025a20bdfcSJan Kara 
10035a20bdfcSJan Kara 		switch (sbi->s_jquota_fmt) {
10045a20bdfcSJan Kara 		case QFMT_VFS_OLD:
10055a20bdfcSJan Kara 			fmtname = "vfsold";
10065a20bdfcSJan Kara 			break;
10075a20bdfcSJan Kara 		case QFMT_VFS_V0:
10085a20bdfcSJan Kara 			fmtname = "vfsv0";
10095a20bdfcSJan Kara 			break;
10105a20bdfcSJan Kara 		case QFMT_VFS_V1:
10115a20bdfcSJan Kara 			fmtname = "vfsv1";
10125a20bdfcSJan Kara 			break;
10135a20bdfcSJan Kara 		}
10145a20bdfcSJan Kara 		seq_printf(seq, ",jqfmt=%s", fmtname);
10155a20bdfcSJan Kara 	}
1016ac27a0ecSDave Kleikamp 
1017ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
1018ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
1019ac27a0ecSDave Kleikamp 
1020ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
1021ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
1022ac27a0ecSDave Kleikamp 
1023482a7425SDmitry Monakhov 	if (test_opt(sb, USRQUOTA))
1024ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
1025ac27a0ecSDave Kleikamp 
1026482a7425SDmitry Monakhov 	if (test_opt(sb, GRPQUOTA))
1027ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
1028ac27a0ecSDave Kleikamp #endif
1029ac27a0ecSDave Kleikamp }
1030ac27a0ecSDave Kleikamp 
1031d9c9bef1SMiklos Szeredi /*
1032d9c9bef1SMiklos Szeredi  * Show an option if
1033d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
1034d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
1035d9c9bef1SMiklos Szeredi  */
1036617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
1037ac27a0ecSDave Kleikamp {
1038aa22df2cSAneesh Kumar K.V 	int def_errors;
1039aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
1040ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
1041d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1042d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
1043d9c9bef1SMiklos Szeredi 
1044d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1045aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
1046d9c9bef1SMiklos Szeredi 
1047d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
1048d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
1049d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
1050d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
1051aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
1052d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
1053d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
1054d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
1055d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
1056d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
1057d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
1058d9c9bef1SMiklos Szeredi 	}
1059d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
1060d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
1061d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
1062d9c9bef1SMiklos Szeredi 	}
1063bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
1064d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
1065bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
1066d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
1067bb4f397aSAneesh Kumar K.V 		}
1068bb4f397aSAneesh Kumar K.V 	}
1069aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
1070bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
1071aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
1072d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
1073aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
1074d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
1075aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
1076d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
107703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1078ea663336SEric Sandeen 	if (test_opt(sb, XATTR_USER))
1079d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
1080ea663336SEric Sandeen 	if (!test_opt(sb, XATTR_USER))
1081d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
1082d9c9bef1SMiklos Szeredi #endif
108303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1084aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
1085d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
1086d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
1087d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
1088d9c9bef1SMiklos Szeredi #endif
108930773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
1090d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
1091d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
1092d9c9bef1SMiklos Szeredi 	}
109330773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
109430773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
109530773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
109630773840STheodore Ts'o 	}
109730773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
109830773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
109930773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
110030773840STheodore Ts'o 	}
110130773840STheodore Ts'o 
1102571640caSEric Sandeen 	/*
1103571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
1104571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
1105571640caSEric Sandeen 	 * status is.
1106571640caSEric Sandeen 	 */
1107571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
1108571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
1109cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
1110cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
111139a4badeSJan Kara 	else if (test_opt(sb, JOURNAL_CHECKSUM))
111239a4badeSJan Kara 		seq_puts(seq, ",journal_checksum");
111325ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
111425ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
11158b67f04aSTheodore Ts'o 	if (!test_opt(sb, DELALLOC) &&
11168b67f04aSTheodore Ts'o 	    !(def_mount_opts & EXT4_DEFM_NODELALLOC))
1117dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
1118dd919b98SAneesh Kumar K.V 
11196fd7a467STheodore Ts'o 	if (!test_opt(sb, MBLK_IO_SUBMIT))
11206fd7a467STheodore Ts'o 		seq_puts(seq, ",nomblk_io_submit");
1121cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
1122cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
1123aa22df2cSAneesh Kumar K.V 	/*
1124aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
1125aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
1126aa22df2cSAneesh Kumar K.V 	 */
1127617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
1128ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
1129617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
1130ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
1131617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
1132ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
1133ac27a0ecSDave Kleikamp 
1134240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
1135240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
1136240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
1137240799cdSTheodore Ts'o 
11385bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
11395bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
11405bf5683aSHidehiro Kawai 
1141afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
114206705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
1143afd4672dSTheodore Ts'o 
11448b67f04aSTheodore Ts'o 	if (test_opt(sb, DISCARD) && !(def_mount_opts & EXT4_DEFM_DISCARD))
11455328e635SEric Sandeen 		seq_puts(seq, ",discard");
11465328e635SEric Sandeen 
1147e3bb52aeSEric Sandeen 	if (test_opt(sb, NOLOAD))
1148e3bb52aeSEric Sandeen 		seq_puts(seq, ",norecovery");
1149e3bb52aeSEric Sandeen 
1150744692dcSJiaying Zhang 	if (test_opt(sb, DIOREAD_NOLOCK))
1151744692dcSJiaying Zhang 		seq_puts(seq, ",dioread_nolock");
1152744692dcSJiaying Zhang 
11538b67f04aSTheodore Ts'o 	if (test_opt(sb, BLOCK_VALIDITY) &&
11548b67f04aSTheodore Ts'o 	    !(def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY))
11558b67f04aSTheodore Ts'o 		seq_puts(seq, ",block_validity");
11568b67f04aSTheodore Ts'o 
1157bfff6873SLukas Czerner 	if (!test_opt(sb, INIT_INODE_TABLE))
1158bfff6873SLukas Czerner 		seq_puts(seq, ",noinit_inode_table");
115951ce6511SLukas Czerner 	else if (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)
1160bfff6873SLukas Czerner 		seq_printf(seq, ",init_inode_table=%u",
1161bfff6873SLukas Czerner 			   (unsigned) sbi->s_li_wait_mult);
1162bfff6873SLukas Czerner 
1163617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
11640b8e58a1SAndreas Dilger 
1165ac27a0ecSDave Kleikamp 	return 0;
1166ac27a0ecSDave Kleikamp }
1167ac27a0ecSDave Kleikamp 
11681b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
11691b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1170ac27a0ecSDave Kleikamp {
1171ac27a0ecSDave Kleikamp 	struct inode *inode;
1172ac27a0ecSDave Kleikamp 
1173617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1174ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1175617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1176ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1177ac27a0ecSDave Kleikamp 
1178ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1179ac27a0ecSDave Kleikamp 	 *
1180617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1181ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1182ac27a0ecSDave Kleikamp 	 *
1183ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1184ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1185ac27a0ecSDave Kleikamp 	 */
11861d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
11871d1fe1eeSDavid Howells 	if (IS_ERR(inode))
11881d1fe1eeSDavid Howells 		return ERR_CAST(inode);
11891d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1190ac27a0ecSDave Kleikamp 		iput(inode);
1191ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1192ac27a0ecSDave Kleikamp 	}
11931b961ac0SChristoph Hellwig 
11941b961ac0SChristoph Hellwig 	return inode;
1195ac27a0ecSDave Kleikamp }
11961b961ac0SChristoph Hellwig 
11971b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
11981b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
11991b961ac0SChristoph Hellwig {
12001b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
12011b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
12021b961ac0SChristoph Hellwig }
12031b961ac0SChristoph Hellwig 
12041b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
12051b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12061b961ac0SChristoph Hellwig {
12071b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
12081b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1209ac27a0ecSDave Kleikamp }
1210ac27a0ecSDave Kleikamp 
1211c39a7f84SToshiyuki Okajima /*
1212c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1213c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1214c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1215c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1216c39a7f84SToshiyuki Okajima  */
12170b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
12180b8e58a1SAndreas Dilger 				 gfp_t wait)
1219c39a7f84SToshiyuki Okajima {
1220c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1221c39a7f84SToshiyuki Okajima 
1222c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1223c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1224c39a7f84SToshiyuki Okajima 		return 0;
1225c39a7f84SToshiyuki Okajima 	if (journal)
1226c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1227c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1228c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1229c39a7f84SToshiyuki Okajima }
1230c39a7f84SToshiyuki Okajima 
1231ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1232ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1233ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1234ac27a0ecSDave Kleikamp 
1235617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1236617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1237617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1238617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1239617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
12406f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1241f00c9e44SJan Kara 			 struct path *path);
1242ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
1243617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1244617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1245ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1246617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1247ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
1248ac27a0ecSDave Kleikamp 
124961e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
125060e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1251617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1252617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1253617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1254617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1255a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1256a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1257a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1258ac27a0ecSDave Kleikamp };
1259ac27a0ecSDave Kleikamp 
12600d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1261617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1262ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1263287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
1264287a8095SChristoph Hellwig 	.get_info	= dquot_get_dqinfo,
1265287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1266287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1267287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1268ac27a0ecSDave Kleikamp };
1269ac27a0ecSDave Kleikamp #endif
1270ac27a0ecSDave Kleikamp 
1271ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1272617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1273617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1274617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1275617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
12767ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
12770930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1278617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1279617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1280c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1281c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1282617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1283617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1284617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1285ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1286617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1287617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1288ac27a0ecSDave Kleikamp #endif
1289c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1290ac27a0ecSDave Kleikamp };
1291ac27a0ecSDave Kleikamp 
12929ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
12939ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
12949ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
12959ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
12969ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
12977ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
12980930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
12999ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
13009ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
13019ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
13029ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
13039ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
13049ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
13059ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
13069ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
13079ca92389STheodore Ts'o #endif
13089ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
13099ca92389STheodore Ts'o };
13109ca92389STheodore Ts'o 
131139655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
13121b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
13131b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1314617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1315ac27a0ecSDave Kleikamp };
1316ac27a0ecSDave Kleikamp 
1317ac27a0ecSDave Kleikamp enum {
1318ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1319ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
132001436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1321ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
132206705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
132330773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1324c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1325818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1326ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1327296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1328ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
13295a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
13305a20bdfcSJan Kara 	Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
13315a20bdfcSJan Kara 	Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
13321449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
13331449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
13345328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1335744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
13365328e635SEric Sandeen 	Opt_discard, Opt_nodiscard,
1337bfff6873SLukas Czerner 	Opt_init_inode_table, Opt_noinit_inode_table,
1338ac27a0ecSDave Kleikamp };
1339ac27a0ecSDave Kleikamp 
1340a447c093SSteven Whitehouse static const match_table_t tokens = {
1341ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1342ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1343ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1344ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1345ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1346ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1347ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1348ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1349ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1350ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1351ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1352ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1353ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1354ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1355ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1356ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1357ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1358ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1359ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1360ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1361ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1362e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
1363ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1364ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1365ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
136630773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
136730773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1368ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1369ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1370818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1371818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1372ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1373ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1374ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1375ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
13765bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
13775bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1378ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1379ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1380ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1381ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1382ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1383ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
13845a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1385ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1386ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1387ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1388ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1389ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
139006705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
139106705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
139225ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1393c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1394ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
139564769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1396dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
13971449032bSTheodore Ts'o 	{Opt_mblk_io_submit, "mblk_io_submit"},
13981449032bSTheodore Ts'o 	{Opt_nomblk_io_submit, "nomblk_io_submit"},
13996fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
14006fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1401240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1402b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1403afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
140406705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
140506705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1406744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1407744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
14085328e635SEric Sandeen 	{Opt_discard, "discard"},
14095328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1410bfff6873SLukas Czerner 	{Opt_init_inode_table, "init_itable=%u"},
1411bfff6873SLukas Czerner 	{Opt_init_inode_table, "init_itable"},
1412bfff6873SLukas Czerner 	{Opt_noinit_inode_table, "noinit_itable"},
1413f3f12faaSJosef Bacik 	{Opt_err, NULL},
1414ac27a0ecSDave Kleikamp };
1415ac27a0ecSDave Kleikamp 
1416617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1417ac27a0ecSDave Kleikamp {
1418617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1419ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1420ac27a0ecSDave Kleikamp 
1421ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1422ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
14230b8e58a1SAndreas Dilger 
1424ac27a0ecSDave Kleikamp 	options += 3;
14250b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1426ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1427ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
14284776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1429ac27a0ecSDave Kleikamp 		       (char *) *data);
1430ac27a0ecSDave Kleikamp 		return 1;
1431ac27a0ecSDave Kleikamp 	}
1432ac27a0ecSDave Kleikamp 	if (*options == ',')
1433ac27a0ecSDave Kleikamp 		options++;
1434ac27a0ecSDave Kleikamp 	*data = (void *) options;
14350b8e58a1SAndreas Dilger 
1436ac27a0ecSDave Kleikamp 	return sb_block;
1437ac27a0ecSDave Kleikamp }
1438ac27a0ecSDave Kleikamp 
1439b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1440437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1441437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1442b3881f74STheodore Ts'o 
144356c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
144456c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
144556c50f11SDmitry Monakhov {
144656c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
144756c50f11SDmitry Monakhov 	char *qname;
144856c50f11SDmitry Monakhov 
144956c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
145056c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
145156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
145256c50f11SDmitry Monakhov 			"Cannot change journaled "
145356c50f11SDmitry Monakhov 			"quota options when quota turned on");
145456c50f11SDmitry Monakhov 		return 0;
145556c50f11SDmitry Monakhov 	}
145656c50f11SDmitry Monakhov 	qname = match_strdup(args);
145756c50f11SDmitry Monakhov 	if (!qname) {
145856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
145956c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
146056c50f11SDmitry Monakhov 		return 0;
146156c50f11SDmitry Monakhov 	}
146256c50f11SDmitry Monakhov 	if (sbi->s_qf_names[qtype] &&
146356c50f11SDmitry Monakhov 		strcmp(sbi->s_qf_names[qtype], qname)) {
146456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
146556c50f11SDmitry Monakhov 			"%s quota file already specified", QTYPE2NAME(qtype));
146656c50f11SDmitry Monakhov 		kfree(qname);
146756c50f11SDmitry Monakhov 		return 0;
146856c50f11SDmitry Monakhov 	}
146956c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = qname;
147056c50f11SDmitry Monakhov 	if (strchr(sbi->s_qf_names[qtype], '/')) {
147156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
147256c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
147356c50f11SDmitry Monakhov 		kfree(sbi->s_qf_names[qtype]);
147456c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype] = NULL;
147556c50f11SDmitry Monakhov 		return 0;
147656c50f11SDmitry Monakhov 	}
1477fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
147856c50f11SDmitry Monakhov 	return 1;
147956c50f11SDmitry Monakhov }
148056c50f11SDmitry Monakhov 
148156c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
148256c50f11SDmitry Monakhov {
148356c50f11SDmitry Monakhov 
148456c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
148556c50f11SDmitry Monakhov 
148656c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
148756c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
148856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
148956c50f11SDmitry Monakhov 			" when quota turned on");
149056c50f11SDmitry Monakhov 		return 0;
149156c50f11SDmitry Monakhov 	}
149256c50f11SDmitry Monakhov 	/*
149356c50f11SDmitry Monakhov 	 * The space will be released later when all options are confirmed
149456c50f11SDmitry Monakhov 	 * to be correct
149556c50f11SDmitry Monakhov 	 */
149656c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
149756c50f11SDmitry Monakhov 	return 1;
149856c50f11SDmitry Monakhov }
149956c50f11SDmitry Monakhov #endif
150056c50f11SDmitry Monakhov 
1501ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1502c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1503b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1504617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1505ac27a0ecSDave Kleikamp {
1506617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1507ac27a0ecSDave Kleikamp 	char *p;
1508ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1509ac27a0ecSDave Kleikamp 	int data_opt = 0;
1510ac27a0ecSDave Kleikamp 	int option;
1511ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
151256c50f11SDmitry Monakhov 	int qfmt;
1513ac27a0ecSDave Kleikamp #endif
1514ac27a0ecSDave Kleikamp 
1515ac27a0ecSDave Kleikamp 	if (!options)
1516ac27a0ecSDave Kleikamp 		return 1;
1517ac27a0ecSDave Kleikamp 
1518ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1519ac27a0ecSDave Kleikamp 		int token;
1520ac27a0ecSDave Kleikamp 		if (!*p)
1521ac27a0ecSDave Kleikamp 			continue;
1522ac27a0ecSDave Kleikamp 
152315121c18SEric Sandeen 		/*
152415121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
152515121c18SEric Sandeen 		 * found; some options take optional arguments.
152615121c18SEric Sandeen 		 */
15277dc57615SPeter Huewe 		args[0].to = args[0].from = NULL;
1528ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1529ac27a0ecSDave Kleikamp 		switch (token) {
1530ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1531437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1532fd8c37ecSTheodore Ts'o 			clear_opt(sb, MINIX_DF);
1533ac27a0ecSDave Kleikamp 			break;
1534ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1535437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1536fd8c37ecSTheodore Ts'o 			set_opt(sb, MINIX_DF);
1537437ca0fdSDmitry Monakhov 
1538ac27a0ecSDave Kleikamp 			break;
1539ac27a0ecSDave Kleikamp 		case Opt_grpid:
1540437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1541fd8c37ecSTheodore Ts'o 			set_opt(sb, GRPID);
1542437ca0fdSDmitry Monakhov 
1543ac27a0ecSDave Kleikamp 			break;
1544ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1545437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1546fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPID);
1547437ca0fdSDmitry Monakhov 
1548ac27a0ecSDave Kleikamp 			break;
1549ac27a0ecSDave Kleikamp 		case Opt_resuid:
1550ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1551ac27a0ecSDave Kleikamp 				return 0;
1552ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1553ac27a0ecSDave Kleikamp 			break;
1554ac27a0ecSDave Kleikamp 		case Opt_resgid:
1555ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1556ac27a0ecSDave Kleikamp 				return 0;
1557ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1558ac27a0ecSDave Kleikamp 			break;
1559ac27a0ecSDave Kleikamp 		case Opt_sb:
1560ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1561ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1562ac27a0ecSDave Kleikamp 			break;
1563ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1564fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_CONT);
1565fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_RO);
1566fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_PANIC);
1567ac27a0ecSDave Kleikamp 			break;
1568ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1569fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_CONT);
1570fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_PANIC);
1571fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_RO);
1572ac27a0ecSDave Kleikamp 			break;
1573ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1574fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_RO);
1575fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_PANIC);
1576fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_CONT);
1577ac27a0ecSDave Kleikamp 			break;
1578ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1579fd8c37ecSTheodore Ts'o 			set_opt(sb, NO_UID32);
1580ac27a0ecSDave Kleikamp 			break;
1581ac27a0ecSDave Kleikamp 		case Opt_debug:
1582fd8c37ecSTheodore Ts'o 			set_opt(sb, DEBUG);
1583ac27a0ecSDave Kleikamp 			break;
1584ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
15854113c4caSLukas Czerner 			ext4_msg(sb, KERN_WARNING,
15864113c4caSLukas Czerner 				 "Ignoring deprecated oldalloc option");
1587ac27a0ecSDave Kleikamp 			break;
1588ac27a0ecSDave Kleikamp 		case Opt_orlov:
15894113c4caSLukas Czerner 			ext4_msg(sb, KERN_WARNING,
15904113c4caSLukas Czerner 				 "Ignoring deprecated orlov option");
1591ac27a0ecSDave Kleikamp 			break;
159203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1593ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1594fd8c37ecSTheodore Ts'o 			set_opt(sb, XATTR_USER);
1595ac27a0ecSDave Kleikamp 			break;
1596ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1597fd8c37ecSTheodore Ts'o 			clear_opt(sb, XATTR_USER);
1598ac27a0ecSDave Kleikamp 			break;
1599ac27a0ecSDave Kleikamp #else
1600ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1601ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1602b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1603ac27a0ecSDave Kleikamp 			break;
1604ac27a0ecSDave Kleikamp #endif
160503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1606ac27a0ecSDave Kleikamp 		case Opt_acl:
1607fd8c37ecSTheodore Ts'o 			set_opt(sb, POSIX_ACL);
1608ac27a0ecSDave Kleikamp 			break;
1609ac27a0ecSDave Kleikamp 		case Opt_noacl:
1610fd8c37ecSTheodore Ts'o 			clear_opt(sb, POSIX_ACL);
1611ac27a0ecSDave Kleikamp 			break;
1612ac27a0ecSDave Kleikamp #else
1613ac27a0ecSDave Kleikamp 		case Opt_acl:
1614ac27a0ecSDave Kleikamp 		case Opt_noacl:
1615b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1616ac27a0ecSDave Kleikamp 			break;
1617ac27a0ecSDave Kleikamp #endif
1618ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1619ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1620ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1621ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1622ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1623ac27a0ecSDave Kleikamp 			   journal file. */
1624ac27a0ecSDave Kleikamp 			if (is_remount) {
1625b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1626b31e1552SEric Sandeen 					 "Cannot specify journal on remount");
1627ac27a0ecSDave Kleikamp 				return 0;
1628ac27a0ecSDave Kleikamp 			}
1629fd8c37ecSTheodore Ts'o 			set_opt(sb, UPDATE_JOURNAL);
1630ac27a0ecSDave Kleikamp 			break;
1631ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1632ac27a0ecSDave Kleikamp 			if (is_remount) {
1633b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1634b31e1552SEric Sandeen 					"Cannot specify journal on remount");
1635ac27a0ecSDave Kleikamp 				return 0;
1636ac27a0ecSDave Kleikamp 			}
1637ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1638ac27a0ecSDave Kleikamp 				return 0;
1639ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1640ac27a0ecSDave Kleikamp 			break;
1641818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1642fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_CHECKSUM);
1643d4da6c9cSLinus Torvalds 			break;
1644818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1645fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_ASYNC_COMMIT);
1646fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_CHECKSUM);
1647818d276cSGirish Shilamkar 			break;
1648ac27a0ecSDave Kleikamp 		case Opt_noload:
1649fd8c37ecSTheodore Ts'o 			set_opt(sb, NOLOAD);
1650ac27a0ecSDave Kleikamp 			break;
1651ac27a0ecSDave Kleikamp 		case Opt_commit:
1652ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1653ac27a0ecSDave Kleikamp 				return 0;
1654ac27a0ecSDave Kleikamp 			if (option < 0)
1655ac27a0ecSDave Kleikamp 				return 0;
1656ac27a0ecSDave Kleikamp 			if (option == 0)
1657cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1658ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1659ac27a0ecSDave Kleikamp 			break;
166030773840STheodore Ts'o 		case Opt_max_batch_time:
166130773840STheodore Ts'o 			if (match_int(&args[0], &option))
166230773840STheodore Ts'o 				return 0;
166330773840STheodore Ts'o 			if (option < 0)
166430773840STheodore Ts'o 				return 0;
166530773840STheodore Ts'o 			if (option == 0)
166630773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
166730773840STheodore Ts'o 			sbi->s_max_batch_time = option;
166830773840STheodore Ts'o 			break;
166930773840STheodore Ts'o 		case Opt_min_batch_time:
167030773840STheodore Ts'o 			if (match_int(&args[0], &option))
167130773840STheodore Ts'o 				return 0;
167230773840STheodore Ts'o 			if (option < 0)
167330773840STheodore Ts'o 				return 0;
167430773840STheodore Ts'o 			sbi->s_min_batch_time = option;
167530773840STheodore Ts'o 			break;
1676ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1677617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1678ac27a0ecSDave Kleikamp 			goto datacheck;
1679ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1680617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1681ac27a0ecSDave Kleikamp 			goto datacheck;
1682ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1683617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1684ac27a0ecSDave Kleikamp 		datacheck:
1685ac27a0ecSDave Kleikamp 			if (is_remount) {
1686*eb513689STheodore Ts'o 				if (!sbi->s_journal)
1687*eb513689STheodore Ts'o 					ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
1688*eb513689STheodore Ts'o 				else if (test_opt(sb, DATA_FLAGS) != data_opt) {
1689b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
1690b31e1552SEric Sandeen 						"Cannot change data mode on remount");
1691ac27a0ecSDave Kleikamp 					return 0;
1692ac27a0ecSDave Kleikamp 				}
1693ac27a0ecSDave Kleikamp 			} else {
1694fd8c37ecSTheodore Ts'o 				clear_opt(sb, DATA_FLAGS);
1695ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1696ac27a0ecSDave Kleikamp 			}
1697ac27a0ecSDave Kleikamp 			break;
16985bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
1699fd8c37ecSTheodore Ts'o 			set_opt(sb, DATA_ERR_ABORT);
17005bf5683aSHidehiro Kawai 			break;
17015bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
1702fd8c37ecSTheodore Ts'o 			clear_opt(sb, DATA_ERR_ABORT);
17035bf5683aSHidehiro Kawai 			break;
1704ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1705ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
170656c50f11SDmitry Monakhov 			if (!set_qf_name(sb, USRQUOTA, &args[0]))
170756c50f11SDmitry Monakhov 				return 0;
170856c50f11SDmitry Monakhov 			break;
1709ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
171056c50f11SDmitry Monakhov 			if (!set_qf_name(sb, GRPQUOTA, &args[0]))
1711ac27a0ecSDave Kleikamp 				return 0;
1712ac27a0ecSDave Kleikamp 			break;
1713ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
171456c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, USRQUOTA))
1715ac27a0ecSDave Kleikamp 				return 0;
1716ac27a0ecSDave Kleikamp 			break;
171756c50f11SDmitry Monakhov 		case Opt_offgrpjquota:
171856c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, GRPQUOTA))
171956c50f11SDmitry Monakhov 				return 0;
172056c50f11SDmitry Monakhov 			break;
172156c50f11SDmitry Monakhov 
1722ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1723dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1724dfc5d03fSJan Kara 			goto set_qf_format;
1725ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1726dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
17275a20bdfcSJan Kara 			goto set_qf_format;
17285a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
17295a20bdfcSJan Kara 			qfmt = QFMT_VFS_V1;
1730dfc5d03fSJan Kara set_qf_format:
173117bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1732dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1733b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
1734dfc5d03fSJan Kara 					"journaled quota options when "
1735b31e1552SEric Sandeen 					"quota turned on");
1736dfc5d03fSJan Kara 				return 0;
1737dfc5d03fSJan Kara 			}
1738dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1739ac27a0ecSDave Kleikamp 			break;
1740ac27a0ecSDave Kleikamp 		case Opt_quota:
1741ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1742fd8c37ecSTheodore Ts'o 			set_opt(sb, QUOTA);
1743fd8c37ecSTheodore Ts'o 			set_opt(sb, USRQUOTA);
1744ac27a0ecSDave Kleikamp 			break;
1745ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1746fd8c37ecSTheodore Ts'o 			set_opt(sb, QUOTA);
1747fd8c37ecSTheodore Ts'o 			set_opt(sb, GRPQUOTA);
1748ac27a0ecSDave Kleikamp 			break;
1749ac27a0ecSDave Kleikamp 		case Opt_noquota:
175017bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1751b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change quota "
1752b31e1552SEric Sandeen 					"options when quota turned on");
1753ac27a0ecSDave Kleikamp 				return 0;
1754ac27a0ecSDave Kleikamp 			}
1755fd8c37ecSTheodore Ts'o 			clear_opt(sb, QUOTA);
1756fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1757fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1758ac27a0ecSDave Kleikamp 			break;
1759ac27a0ecSDave Kleikamp #else
1760ac27a0ecSDave Kleikamp 		case Opt_quota:
1761ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1762ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1763b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1764b31e1552SEric Sandeen 				"quota options not supported");
1765cd59e7b9SJan Kara 			break;
1766ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1767ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1768ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1769ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1770ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1771ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
17725a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
1773b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1774b31e1552SEric Sandeen 				"journaled quota options not supported");
1775ac27a0ecSDave Kleikamp 			break;
1776ac27a0ecSDave Kleikamp 		case Opt_noquota:
1777ac27a0ecSDave Kleikamp 			break;
1778ac27a0ecSDave Kleikamp #endif
1779ac27a0ecSDave Kleikamp 		case Opt_abort:
17804ab2f15bSTheodore Ts'o 			sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1781ac27a0ecSDave Kleikamp 			break;
178206705bffSTheodore Ts'o 		case Opt_nobarrier:
1783fd8c37ecSTheodore Ts'o 			clear_opt(sb, BARRIER);
178406705bffSTheodore Ts'o 			break;
1785ac27a0ecSDave Kleikamp 		case Opt_barrier:
178615121c18SEric Sandeen 			if (args[0].from) {
178715121c18SEric Sandeen 				if (match_int(&args[0], &option))
178815121c18SEric Sandeen 					return 0;
178915121c18SEric Sandeen 			} else
179015121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1791ac27a0ecSDave Kleikamp 			if (option)
1792fd8c37ecSTheodore Ts'o 				set_opt(sb, BARRIER);
1793ac27a0ecSDave Kleikamp 			else
1794fd8c37ecSTheodore Ts'o 				clear_opt(sb, BARRIER);
1795ac27a0ecSDave Kleikamp 			break;
1796ac27a0ecSDave Kleikamp 		case Opt_ignore:
1797ac27a0ecSDave Kleikamp 			break;
1798ac27a0ecSDave Kleikamp 		case Opt_resize:
1799ac27a0ecSDave Kleikamp 			if (!is_remount) {
1800b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1801b31e1552SEric Sandeen 					"resize option only available "
1802b31e1552SEric Sandeen 					"for remount");
1803ac27a0ecSDave Kleikamp 				return 0;
1804ac27a0ecSDave Kleikamp 			}
1805ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1806ac27a0ecSDave Kleikamp 				return 0;
1807ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1808ac27a0ecSDave Kleikamp 			break;
1809ac27a0ecSDave Kleikamp 		case Opt_nobh:
1810206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1811206f7ab4SChristoph Hellwig 				 "Ignoring deprecated nobh option");
1812ac27a0ecSDave Kleikamp 			break;
1813ac27a0ecSDave Kleikamp 		case Opt_bh:
1814206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1815206f7ab4SChristoph Hellwig 				 "Ignoring deprecated bh option");
1816ac27a0ecSDave Kleikamp 			break;
181725ec56b5SJean Noel Cordenner 		case Opt_i_version:
1818fd8c37ecSTheodore Ts'o 			set_opt(sb, I_VERSION);
181925ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
182025ec56b5SJean Noel Cordenner 			break;
1821dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1822fd8c37ecSTheodore Ts'o 			clear_opt(sb, DELALLOC);
182356889787STheodore Ts'o 			clear_opt2(sb, EXPLICIT_DELALLOC);
1824dd919b98SAneesh Kumar K.V 			break;
18251449032bSTheodore Ts'o 		case Opt_mblk_io_submit:
1826fd8c37ecSTheodore Ts'o 			set_opt(sb, MBLK_IO_SUBMIT);
18271449032bSTheodore Ts'o 			break;
18281449032bSTheodore Ts'o 		case Opt_nomblk_io_submit:
1829fd8c37ecSTheodore Ts'o 			clear_opt(sb, MBLK_IO_SUBMIT);
18301449032bSTheodore Ts'o 			break;
1831c9de560dSAlex Tomas 		case Opt_stripe:
1832c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1833c9de560dSAlex Tomas 				return 0;
1834c9de560dSAlex Tomas 			if (option < 0)
1835c9de560dSAlex Tomas 				return 0;
1836c9de560dSAlex Tomas 			sbi->s_stripe = option;
1837c9de560dSAlex Tomas 			break;
183864769240SAlex Tomas 		case Opt_delalloc:
1839fd8c37ecSTheodore Ts'o 			set_opt(sb, DELALLOC);
184056889787STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
184164769240SAlex Tomas 			break;
18426fd058f7STheodore Ts'o 		case Opt_block_validity:
1843fd8c37ecSTheodore Ts'o 			set_opt(sb, BLOCK_VALIDITY);
18446fd058f7STheodore Ts'o 			break;
18456fd058f7STheodore Ts'o 		case Opt_noblock_validity:
1846fd8c37ecSTheodore Ts'o 			clear_opt(sb, BLOCK_VALIDITY);
18476fd058f7STheodore Ts'o 			break;
1848240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1849240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1850240799cdSTheodore Ts'o 				return 0;
1851240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1852240799cdSTheodore Ts'o 				return 0;
18535dbd571dSAlexander V. Lukyanov 			if (option && !is_power_of_2(option)) {
1854b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1855b31e1552SEric Sandeen 					 "EXT4-fs: inode_readahead_blks"
1856b31e1552SEric Sandeen 					 " must be a power of 2");
18573197ebdbSTheodore Ts'o 				return 0;
18583197ebdbSTheodore Ts'o 			}
1859240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1860240799cdSTheodore Ts'o 			break;
1861b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1862b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1863b3881f74STheodore Ts'o 				return 0;
1864b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1865b3881f74STheodore Ts'o 				break;
1866b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1867b3881f74STheodore Ts'o 							    option);
1868b3881f74STheodore Ts'o 			break;
186906705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
1870fd8c37ecSTheodore Ts'o 			set_opt(sb, NO_AUTO_DA_ALLOC);
187106705bffSTheodore Ts'o 			break;
1872afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
187315121c18SEric Sandeen 			if (args[0].from) {
187415121c18SEric Sandeen 				if (match_int(&args[0], &option))
187515121c18SEric Sandeen 					return 0;
187615121c18SEric Sandeen 			} else
187715121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1878afd4672dSTheodore Ts'o 			if (option)
1879fd8c37ecSTheodore Ts'o 				clear_opt(sb, NO_AUTO_DA_ALLOC);
1880afd4672dSTheodore Ts'o 			else
1881fd8c37ecSTheodore Ts'o 				set_opt(sb,NO_AUTO_DA_ALLOC);
1882afd4672dSTheodore Ts'o 			break;
18835328e635SEric Sandeen 		case Opt_discard:
1884fd8c37ecSTheodore Ts'o 			set_opt(sb, DISCARD);
18855328e635SEric Sandeen 			break;
18865328e635SEric Sandeen 		case Opt_nodiscard:
1887fd8c37ecSTheodore Ts'o 			clear_opt(sb, DISCARD);
18885328e635SEric Sandeen 			break;
1889744692dcSJiaying Zhang 		case Opt_dioread_nolock:
1890fd8c37ecSTheodore Ts'o 			set_opt(sb, DIOREAD_NOLOCK);
1891744692dcSJiaying Zhang 			break;
1892744692dcSJiaying Zhang 		case Opt_dioread_lock:
1893fd8c37ecSTheodore Ts'o 			clear_opt(sb, DIOREAD_NOLOCK);
1894744692dcSJiaying Zhang 			break;
1895bfff6873SLukas Czerner 		case Opt_init_inode_table:
1896fd8c37ecSTheodore Ts'o 			set_opt(sb, INIT_INODE_TABLE);
1897bfff6873SLukas Czerner 			if (args[0].from) {
1898bfff6873SLukas Czerner 				if (match_int(&args[0], &option))
1899bfff6873SLukas Czerner 					return 0;
1900bfff6873SLukas Czerner 			} else
1901bfff6873SLukas Czerner 				option = EXT4_DEF_LI_WAIT_MULT;
1902bfff6873SLukas Czerner 			if (option < 0)
1903bfff6873SLukas Czerner 				return 0;
1904bfff6873SLukas Czerner 			sbi->s_li_wait_mult = option;
1905bfff6873SLukas Czerner 			break;
1906bfff6873SLukas Czerner 		case Opt_noinit_inode_table:
1907fd8c37ecSTheodore Ts'o 			clear_opt(sb, INIT_INODE_TABLE);
1908bfff6873SLukas Czerner 			break;
1909ac27a0ecSDave Kleikamp 		default:
1910b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1911b31e1552SEric Sandeen 			       "Unrecognized mount option \"%s\" "
1912b31e1552SEric Sandeen 			       "or missing value", p);
1913ac27a0ecSDave Kleikamp 			return 0;
1914ac27a0ecSDave Kleikamp 		}
1915ac27a0ecSDave Kleikamp 	}
1916ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1917ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1918482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1919fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1920ac27a0ecSDave Kleikamp 
1921482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1922fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1923ac27a0ecSDave Kleikamp 
192456c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1925b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1926b31e1552SEric Sandeen 					"format mixing");
1927ac27a0ecSDave Kleikamp 			return 0;
1928ac27a0ecSDave Kleikamp 		}
1929ac27a0ecSDave Kleikamp 
1930ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1931b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1932b31e1552SEric Sandeen 					"not specified");
1933ac27a0ecSDave Kleikamp 			return 0;
1934ac27a0ecSDave Kleikamp 		}
1935ac27a0ecSDave Kleikamp 	} else {
1936ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1937b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
19382c8be6b2SJan Kara 					"specified with no journaling "
1939b31e1552SEric Sandeen 					"enabled");
1940ac27a0ecSDave Kleikamp 			return 0;
1941ac27a0ecSDave Kleikamp 		}
1942ac27a0ecSDave Kleikamp 	}
1943ac27a0ecSDave Kleikamp #endif
1944ac27a0ecSDave Kleikamp 	return 1;
1945ac27a0ecSDave Kleikamp }
1946ac27a0ecSDave Kleikamp 
1947617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1948ac27a0ecSDave Kleikamp 			    int read_only)
1949ac27a0ecSDave Kleikamp {
1950617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1951ac27a0ecSDave Kleikamp 	int res = 0;
1952ac27a0ecSDave Kleikamp 
1953617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1954b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1955b31e1552SEric Sandeen 			 "forcing read-only mode");
1956ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1957ac27a0ecSDave Kleikamp 	}
1958ac27a0ecSDave Kleikamp 	if (read_only)
1959281b5995STheodore Ts'o 		goto done;
1960617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1961b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1962b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1963617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1964b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1965b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1966b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1967ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1968ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1969ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1970b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1971b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1972b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1973ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1974ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1975ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1976b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1977b31e1552SEric Sandeen 			 "warning: checktime reached, "
1978b31e1552SEric Sandeen 			 "running e2fsck is recommended");
19790390131bSFrank Mayhar 	if (!sbi->s_journal)
1980216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1981ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1982617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1983e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1984ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1985617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
19860390131bSFrank Mayhar 	if (sbi->s_journal)
1987617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1988ac27a0ecSDave Kleikamp 
1989e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1990281b5995STheodore Ts'o done:
1991ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1992a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1993a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1994ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1995ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1996617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1997617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1998a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
1999ac27a0ecSDave Kleikamp 
20007abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2001ac27a0ecSDave Kleikamp 	return res;
2002ac27a0ecSDave Kleikamp }
2003ac27a0ecSDave Kleikamp 
2004772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2005772cb7c8SJose R. Santos {
2006772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2007772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
2008772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
2009772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2010772cb7c8SJose R. Santos 	int groups_per_flex = 0;
2011c5ca7c76STheodore Ts'o 	size_t size;
2012772cb7c8SJose R. Santos 	int i;
2013772cb7c8SJose R. Santos 
2014503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2015503358aeSTheodore Ts'o 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
2016503358aeSTheodore Ts'o 
2017503358aeSTheodore Ts'o 	if (groups_per_flex < 2) {
2018772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2019772cb7c8SJose R. Santos 		return 1;
2020772cb7c8SJose R. Santos 	}
2021772cb7c8SJose R. Santos 
2022c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
2023c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
2024d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
2025d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
2026c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
20279933fc0aSTheodore Ts'o 	sbi->s_flex_groups = ext4_kvzalloc(size, GFP_KERNEL);
2028c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
20299933fc0aSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %u flex groups",
203094de56abSJoe Perches 			 flex_group_count);
2031772cb7c8SJose R. Santos 		goto failed;
2032772cb7c8SJose R. Santos 	}
2033772cb7c8SJose R. Santos 
2034772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
203588b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2036772cb7c8SJose R. Santos 
2037772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
20387ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
20397ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
2040021b65bbSTheodore Ts'o 		atomic_add(ext4_free_group_clusters(sb, gdp),
204124aaa8efSTheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_clusters);
20427ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
20437ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2044772cb7c8SJose R. Santos 	}
2045772cb7c8SJose R. Santos 
2046772cb7c8SJose R. Santos 	return 1;
2047772cb7c8SJose R. Santos failed:
2048772cb7c8SJose R. Santos 	return 0;
2049772cb7c8SJose R. Santos }
2050772cb7c8SJose R. Santos 
2051717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
2052717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
2053717d50e4SAndreas Dilger {
2054717d50e4SAndreas Dilger 	__u16 crc = 0;
2055717d50e4SAndreas Dilger 
2056717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
2057717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
2058717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
2059717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
2060717d50e4SAndreas Dilger 
2061717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2062717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2063717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
2064717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
2065717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
2066717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
2067717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
2068717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2069717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
2070717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
2071717d50e4SAndreas Dilger 					offset);
2072717d50e4SAndreas Dilger 	}
2073717d50e4SAndreas Dilger 
2074717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2075717d50e4SAndreas Dilger }
2076717d50e4SAndreas Dilger 
2077717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
2078717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2079717d50e4SAndreas Dilger {
2080717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
2081717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
2082717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
2083717d50e4SAndreas Dilger 		return 0;
2084717d50e4SAndreas Dilger 
2085717d50e4SAndreas Dilger 	return 1;
2086717d50e4SAndreas Dilger }
2087717d50e4SAndreas Dilger 
2088ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2089bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2090bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2091ac27a0ecSDave Kleikamp {
2092617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2093617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2094617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2095bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2096bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2097bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2098ce421581SJose R. Santos 	int flexbg_flag = 0;
2099bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2100ac27a0ecSDave Kleikamp 
2101ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2102ce421581SJose R. Santos 		flexbg_flag = 1;
2103ce421581SJose R. Santos 
2104617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2105ac27a0ecSDave Kleikamp 
2106197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2107197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2108197cd65aSAkinobu Mita 
2109ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2110bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2111ac27a0ecSDave Kleikamp 		else
2112ac27a0ecSDave Kleikamp 			last_block = first_block +
2113617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2114ac27a0ecSDave Kleikamp 
2115bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2116bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2117bfff6873SLukas Czerner 			grp = i;
2118bfff6873SLukas Czerner 
21198fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
21202b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2121b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2122a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2123b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2124ac27a0ecSDave Kleikamp 			return 0;
2125ac27a0ecSDave Kleikamp 		}
21268fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
21272b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2128b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2129a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2130b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2131ac27a0ecSDave Kleikamp 			return 0;
2132ac27a0ecSDave Kleikamp 		}
21338fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2134bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
21352b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2136b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2137a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2138b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2139ac27a0ecSDave Kleikamp 			return 0;
2140ac27a0ecSDave Kleikamp 		}
2141955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2142717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
2143b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2144b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2145fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2146fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
21477ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2148955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2149717d50e4SAndreas Dilger 				return 0;
2150717d50e4SAndreas Dilger 			}
21517ee1ec4cSLi Zefan 		}
2152955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2153ce421581SJose R. Santos 		if (!flexbg_flag)
2154617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2155ac27a0ecSDave Kleikamp 	}
2156bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2157bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2158ac27a0ecSDave Kleikamp 
21595dee5437STheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
21605dee5437STheodore Ts'o 				   EXT4_C2B(sbi, ext4_count_free_clusters(sb)));
2161617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
2162ac27a0ecSDave Kleikamp 	return 1;
2163ac27a0ecSDave Kleikamp }
2164ac27a0ecSDave Kleikamp 
2165617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2166ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2167ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2168ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2169ac27a0ecSDave Kleikamp  *
2170ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2171ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2172ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2173ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2174ac27a0ecSDave Kleikamp  *
2175ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2176ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2177ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2178617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2179ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2180ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2181ac27a0ecSDave Kleikamp  */
2182617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2183617ba13bSMingming Cao 				struct ext4_super_block *es)
2184ac27a0ecSDave Kleikamp {
2185ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
2186ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
2187ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2188ac27a0ecSDave Kleikamp 	int i;
2189ac27a0ecSDave Kleikamp #endif
2190ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2191ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2192ac27a0ecSDave Kleikamp 		return;
2193ac27a0ecSDave Kleikamp 	}
2194ac27a0ecSDave Kleikamp 
2195a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2196b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2197b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2198a8f48a95SEric Sandeen 		return;
2199a8f48a95SEric Sandeen 	}
2200a8f48a95SEric Sandeen 
2201d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2202d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2203d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2204d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2205d39195c3SAmir Goldstein 		return;
2206d39195c3SAmir Goldstein 	}
2207d39195c3SAmir Goldstein 
2208617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2209ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
2210ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
2211ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2212ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
2213ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2214ac27a0ecSDave Kleikamp 		return;
2215ac27a0ecSDave Kleikamp 	}
2216ac27a0ecSDave Kleikamp 
2217ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2218b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2219ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2220ac27a0ecSDave Kleikamp 	}
2221ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2222ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2223ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2224ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2225ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2226617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2227617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2228ac27a0ecSDave Kleikamp 			if (ret < 0)
2229b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2230b31e1552SEric Sandeen 					"Cannot turn on journaled "
2231b31e1552SEric Sandeen 					"quota: error %d", ret);
2232ac27a0ecSDave Kleikamp 		}
2233ac27a0ecSDave Kleikamp 	}
2234ac27a0ecSDave Kleikamp #endif
2235ac27a0ecSDave Kleikamp 
2236ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2237ac27a0ecSDave Kleikamp 		struct inode *inode;
2238ac27a0ecSDave Kleikamp 
223997bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
224097bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2241ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2242ac27a0ecSDave Kleikamp 			break;
2243ac27a0ecSDave Kleikamp 		}
2244ac27a0ecSDave Kleikamp 
2245617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2246871a2931SChristoph Hellwig 		dquot_initialize(inode);
2247ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2248b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2249b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
225046e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
2251e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2252ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2253617ba13bSMingming Cao 			ext4_truncate(inode);
2254ac27a0ecSDave Kleikamp 			nr_truncates++;
2255ac27a0ecSDave Kleikamp 		} else {
2256b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2257b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
225846e665e9SHarvey Harrison 				__func__, inode->i_ino);
2259ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2260ac27a0ecSDave Kleikamp 				  inode->i_ino);
2261ac27a0ecSDave Kleikamp 			nr_orphans++;
2262ac27a0ecSDave Kleikamp 		}
2263ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2264ac27a0ecSDave Kleikamp 	}
2265ac27a0ecSDave Kleikamp 
2266ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2267ac27a0ecSDave Kleikamp 
2268ac27a0ecSDave Kleikamp 	if (nr_orphans)
2269b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2270b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2271ac27a0ecSDave Kleikamp 	if (nr_truncates)
2272b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2273b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2274ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2275ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2276ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2277ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2278287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2279ac27a0ecSDave Kleikamp 	}
2280ac27a0ecSDave Kleikamp #endif
2281ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2282ac27a0ecSDave Kleikamp }
22830b8e58a1SAndreas Dilger 
2284cd2291a4SEric Sandeen /*
2285cd2291a4SEric Sandeen  * Maximal extent format file size.
2286cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2287cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2288cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2289cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2290cd2291a4SEric Sandeen  *
2291f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2292f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2293f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2294f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2295f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2296f17722f9SLukas Czerner  *
2297cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2298cd2291a4SEric Sandeen  */
2299f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2300cd2291a4SEric Sandeen {
2301cd2291a4SEric Sandeen 	loff_t res;
2302cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2303cd2291a4SEric Sandeen 
2304cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2305f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2306cd2291a4SEric Sandeen 		/*
230790c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2308cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2309cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2310cd2291a4SEric Sandeen 		 */
2311cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2312cd2291a4SEric Sandeen 
2313cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2314cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2315cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2316cd2291a4SEric Sandeen 	}
2317cd2291a4SEric Sandeen 
2318f17722f9SLukas Czerner 	/*
2319f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2320f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2321f17722f9SLukas Czerner 	 * size
2322f17722f9SLukas Czerner 	 */
2323f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2324cd2291a4SEric Sandeen 	res <<= blkbits;
2325cd2291a4SEric Sandeen 
2326cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2327cd2291a4SEric Sandeen 	if (res > upper_limit)
2328cd2291a4SEric Sandeen 		res = upper_limit;
2329cd2291a4SEric Sandeen 
2330cd2291a4SEric Sandeen 	return res;
2331cd2291a4SEric Sandeen }
2332ac27a0ecSDave Kleikamp 
2333ac27a0ecSDave Kleikamp /*
2334cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
23350fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
23360fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2337ac27a0ecSDave Kleikamp  */
2338f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2339ac27a0ecSDave Kleikamp {
2340617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
23410fc1b451SAneesh Kumar K.V 	int meta_blocks;
23420fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
23430b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
23440b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
23450b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
23460b8e58a1SAndreas Dilger 	 *
23470b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
23480b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
23490fc1b451SAneesh Kumar K.V 	 */
23500fc1b451SAneesh Kumar K.V 
2351f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
23520fc1b451SAneesh Kumar K.V 		/*
235390c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
23540b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
23550b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
23560fc1b451SAneesh Kumar K.V 		 */
23570fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
23580fc1b451SAneesh Kumar K.V 
23590fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
23600fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
23610fc1b451SAneesh Kumar K.V 
23620fc1b451SAneesh Kumar K.V 	} else {
23638180a562SAneesh Kumar K.V 		/*
23648180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
23658180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
23668180a562SAneesh Kumar K.V 		 * represent total number of blocks in
23678180a562SAneesh Kumar K.V 		 * file system block size
23688180a562SAneesh Kumar K.V 		 */
23690fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
23700fc1b451SAneesh Kumar K.V 
23710fc1b451SAneesh Kumar K.V 	}
23720fc1b451SAneesh Kumar K.V 
23730fc1b451SAneesh Kumar K.V 	/* indirect blocks */
23740fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
23750fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
23760fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
23770fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
23780fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
23790fc1b451SAneesh Kumar K.V 
23800fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
23810fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2382ac27a0ecSDave Kleikamp 
2383ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2384ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2385ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2386ac27a0ecSDave Kleikamp 	res <<= bits;
2387ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2388ac27a0ecSDave Kleikamp 		res = upper_limit;
23890fc1b451SAneesh Kumar K.V 
23900fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
23910fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
23920fc1b451SAneesh Kumar K.V 
2393ac27a0ecSDave Kleikamp 	return res;
2394ac27a0ecSDave Kleikamp }
2395ac27a0ecSDave Kleikamp 
2396617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
239770bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2398ac27a0ecSDave Kleikamp {
2399617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2400fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2401ac27a0ecSDave Kleikamp 	int has_super = 0;
2402ac27a0ecSDave Kleikamp 
2403ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2404ac27a0ecSDave Kleikamp 
2405617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2406ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
240770bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2408ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2409617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2410ac27a0ecSDave Kleikamp 		has_super = 1;
24110b8e58a1SAndreas Dilger 
2412617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2413ac27a0ecSDave Kleikamp }
2414ac27a0ecSDave Kleikamp 
2415c9de560dSAlex Tomas /**
2416c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2417c9de560dSAlex Tomas  * @sbi: In memory super block info
2418c9de560dSAlex Tomas  *
2419c9de560dSAlex Tomas  * If we have specified it via mount option, then
2420c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2421c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2422c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2423c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2424c9de560dSAlex Tomas  *
2425c9de560dSAlex Tomas  */
2426c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2427c9de560dSAlex Tomas {
2428c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2429c9de560dSAlex Tomas 	unsigned long stripe_width =
2430c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
24313eb08658SDan Ehrenberg 	int ret;
2432c9de560dSAlex Tomas 
2433c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
24343eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
24353eb08658SDan Ehrenberg 	else if (stripe_width <= sbi->s_blocks_per_group)
24363eb08658SDan Ehrenberg 		ret = stripe_width;
24373eb08658SDan Ehrenberg 	else if (stride <= sbi->s_blocks_per_group)
24383eb08658SDan Ehrenberg 		ret = stride;
24393eb08658SDan Ehrenberg 	else
24403eb08658SDan Ehrenberg 		ret = 0;
2441c9de560dSAlex Tomas 
24423eb08658SDan Ehrenberg 	/*
24433eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
24443eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
24453eb08658SDan Ehrenberg 	 */
24463eb08658SDan Ehrenberg 	if (ret <= 1)
24473eb08658SDan Ehrenberg 		ret = 0;
2448c9de560dSAlex Tomas 
24493eb08658SDan Ehrenberg 	return ret;
2450c9de560dSAlex Tomas }
2451ac27a0ecSDave Kleikamp 
24523197ebdbSTheodore Ts'o /* sysfs supprt */
24533197ebdbSTheodore Ts'o 
24543197ebdbSTheodore Ts'o struct ext4_attr {
24553197ebdbSTheodore Ts'o 	struct attribute attr;
24563197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
24573197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
24583197ebdbSTheodore Ts'o 			 const char *, size_t);
24593197ebdbSTheodore Ts'o 	int offset;
24603197ebdbSTheodore Ts'o };
24613197ebdbSTheodore Ts'o 
24623197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
24633197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
24643197ebdbSTheodore Ts'o {
24653197ebdbSTheodore Ts'o 	char *endp;
24663197ebdbSTheodore Ts'o 
2467e7d2860bSAndré Goddard Rosa 	*value = simple_strtoul(skip_spaces(buf), &endp, 0);
2468e7d2860bSAndré Goddard Rosa 	endp = skip_spaces(endp);
24693197ebdbSTheodore Ts'o 	if (*endp || *value > max)
24703197ebdbSTheodore Ts'o 		return -EINVAL;
24713197ebdbSTheodore Ts'o 
24723197ebdbSTheodore Ts'o 	return 0;
24733197ebdbSTheodore Ts'o }
24743197ebdbSTheodore Ts'o 
24753197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
24763197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
24773197ebdbSTheodore Ts'o 					      char *buf)
24783197ebdbSTheodore Ts'o {
24793197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
24807b415bf6SAditya Kali 		(s64) EXT4_C2B(sbi,
24817b415bf6SAditya Kali 			percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
24823197ebdbSTheodore Ts'o }
24833197ebdbSTheodore Ts'o 
24843197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
24853197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
24863197ebdbSTheodore Ts'o {
24873197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24883197ebdbSTheodore Ts'o 
2489f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2490f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24913197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
24923197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
24933197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
24943197ebdbSTheodore Ts'o }
24953197ebdbSTheodore Ts'o 
24963197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
24973197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
24983197ebdbSTheodore Ts'o {
24993197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
25003197ebdbSTheodore Ts'o 
2501f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2502f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
25033197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2504a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
25053197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2506a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
25073197ebdbSTheodore Ts'o }
25083197ebdbSTheodore Ts'o 
250977f4135fSVivek Haldar static ssize_t extent_cache_hits_show(struct ext4_attr *a,
251077f4135fSVivek Haldar 				      struct ext4_sb_info *sbi, char *buf)
251177f4135fSVivek Haldar {
251277f4135fSVivek Haldar 	return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_hits);
251377f4135fSVivek Haldar }
251477f4135fSVivek Haldar 
251577f4135fSVivek Haldar static ssize_t extent_cache_misses_show(struct ext4_attr *a,
251677f4135fSVivek Haldar 					struct ext4_sb_info *sbi, char *buf)
251777f4135fSVivek Haldar {
251877f4135fSVivek Haldar 	return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_misses);
251977f4135fSVivek Haldar }
252077f4135fSVivek Haldar 
25213197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
25223197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
25233197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
25243197ebdbSTheodore Ts'o {
25253197ebdbSTheodore Ts'o 	unsigned long t;
25263197ebdbSTheodore Ts'o 
25273197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
25283197ebdbSTheodore Ts'o 		return -EINVAL;
25293197ebdbSTheodore Ts'o 
25305dbd571dSAlexander V. Lukyanov 	if (t && !is_power_of_2(t))
25313197ebdbSTheodore Ts'o 		return -EINVAL;
25323197ebdbSTheodore Ts'o 
25333197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
25343197ebdbSTheodore Ts'o 	return count;
25353197ebdbSTheodore Ts'o }
25363197ebdbSTheodore Ts'o 
25373197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
25383197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
25393197ebdbSTheodore Ts'o {
25403197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
25413197ebdbSTheodore Ts'o 
25423197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
25433197ebdbSTheodore Ts'o }
25443197ebdbSTheodore Ts'o 
25453197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
25463197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
25473197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
25483197ebdbSTheodore Ts'o {
25493197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
25503197ebdbSTheodore Ts'o 	unsigned long t;
25513197ebdbSTheodore Ts'o 
25523197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
25533197ebdbSTheodore Ts'o 		return -EINVAL;
25543197ebdbSTheodore Ts'o 	*ui = t;
25553197ebdbSTheodore Ts'o 	return count;
25563197ebdbSTheodore Ts'o }
25573197ebdbSTheodore Ts'o 
25583197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
25593197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
25603197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
25613197ebdbSTheodore Ts'o 	.show	= _show,					\
25623197ebdbSTheodore Ts'o 	.store	= _store,					\
25633197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
25643197ebdbSTheodore Ts'o }
25653197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
25663197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
25673197ebdbSTheodore Ts'o 
2568857ac889SLukas Czerner #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
25693197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
25703197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
25713197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
25723197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
25733197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
25743197ebdbSTheodore Ts'o 
25753197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
25763197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
25773197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
257877f4135fSVivek Haldar EXT4_RO_ATTR(extent_cache_hits);
257977f4135fSVivek Haldar EXT4_RO_ATTR(extent_cache_misses);
25803197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
25813197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
258211013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
25833197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
25843197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
25853197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
25863197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
25873197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
25883197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
258955138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
25903197ebdbSTheodore Ts'o 
25913197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
25923197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
25933197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
25943197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
259577f4135fSVivek Haldar 	ATTR_LIST(extent_cache_hits),
259677f4135fSVivek Haldar 	ATTR_LIST(extent_cache_misses),
25973197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
259811013911SAndreas Dilger 	ATTR_LIST(inode_goal),
25993197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
26003197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
26013197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
26023197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
26033197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
26043197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
260555138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
26063197ebdbSTheodore Ts'o 	NULL,
26073197ebdbSTheodore Ts'o };
26083197ebdbSTheodore Ts'o 
2609857ac889SLukas Czerner /* Features this copy of ext4 supports */
2610857ac889SLukas Czerner EXT4_INFO_ATTR(lazy_itable_init);
261127ee40dfSLukas Czerner EXT4_INFO_ATTR(batched_discard);
2612857ac889SLukas Czerner 
2613857ac889SLukas Czerner static struct attribute *ext4_feat_attrs[] = {
2614857ac889SLukas Czerner 	ATTR_LIST(lazy_itable_init),
261527ee40dfSLukas Czerner 	ATTR_LIST(batched_discard),
2616857ac889SLukas Czerner 	NULL,
2617857ac889SLukas Czerner };
2618857ac889SLukas Czerner 
26193197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
26203197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
26213197ebdbSTheodore Ts'o {
26223197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26233197ebdbSTheodore Ts'o 						s_kobj);
26243197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
26253197ebdbSTheodore Ts'o 
26263197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
26273197ebdbSTheodore Ts'o }
26283197ebdbSTheodore Ts'o 
26293197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
26303197ebdbSTheodore Ts'o 			       struct attribute *attr,
26313197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
26323197ebdbSTheodore Ts'o {
26333197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26343197ebdbSTheodore Ts'o 						s_kobj);
26353197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
26363197ebdbSTheodore Ts'o 
26373197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
26383197ebdbSTheodore Ts'o }
26393197ebdbSTheodore Ts'o 
26403197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
26413197ebdbSTheodore Ts'o {
26423197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
26433197ebdbSTheodore Ts'o 						s_kobj);
26443197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
26453197ebdbSTheodore Ts'o }
26463197ebdbSTheodore Ts'o 
264752cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = {
26483197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
26493197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
26503197ebdbSTheodore Ts'o };
26513197ebdbSTheodore Ts'o 
26523197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
26533197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
26543197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
26553197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
26563197ebdbSTheodore Ts'o };
26573197ebdbSTheodore Ts'o 
2658857ac889SLukas Czerner static void ext4_feat_release(struct kobject *kobj)
2659857ac889SLukas Czerner {
2660857ac889SLukas Czerner 	complete(&ext4_feat->f_kobj_unregister);
2661857ac889SLukas Czerner }
2662857ac889SLukas Czerner 
2663857ac889SLukas Czerner static struct kobj_type ext4_feat_ktype = {
2664857ac889SLukas Czerner 	.default_attrs	= ext4_feat_attrs,
2665857ac889SLukas Czerner 	.sysfs_ops	= &ext4_attr_ops,
2666857ac889SLukas Czerner 	.release	= ext4_feat_release,
2667857ac889SLukas Czerner };
2668857ac889SLukas Czerner 
2669a13fb1a4SEric Sandeen /*
2670a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2671a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2672a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2673a13fb1a4SEric Sandeen  * 0 if it cannot be.
2674a13fb1a4SEric Sandeen  */
2675a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2676a13fb1a4SEric Sandeen {
2677a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2678a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2679a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2680a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2681a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2682a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2683a13fb1a4SEric Sandeen 		return 0;
2684a13fb1a4SEric Sandeen 	}
2685a13fb1a4SEric Sandeen 
2686a13fb1a4SEric Sandeen 	if (readonly)
2687a13fb1a4SEric Sandeen 		return 1;
2688a13fb1a4SEric Sandeen 
2689a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2690a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2691a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2692a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2693a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2694a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2695a13fb1a4SEric Sandeen 		return 0;
2696a13fb1a4SEric Sandeen 	}
2697a13fb1a4SEric Sandeen 	/*
2698a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2699a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2700a13fb1a4SEric Sandeen 	 */
2701a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2702a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2703a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2704a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2705a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2706a13fb1a4SEric Sandeen 			return 0;
2707a13fb1a4SEric Sandeen 		}
2708a13fb1a4SEric Sandeen 	}
2709bab08ab9STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2710bab08ab9STheodore Ts'o 	    !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2711bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2712bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2713bab08ab9STheodore Ts'o 			 "extents feature\n");
2714bab08ab9STheodore Ts'o 		return 0;
2715bab08ab9STheodore Ts'o 	}
2716a13fb1a4SEric Sandeen 	return 1;
2717a13fb1a4SEric Sandeen }
2718a13fb1a4SEric Sandeen 
271966e61a9eSTheodore Ts'o /*
272066e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
272166e61a9eSTheodore Ts'o  * on the file system
272266e61a9eSTheodore Ts'o  */
272366e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
272466e61a9eSTheodore Ts'o {
272566e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
272666e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
272766e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
272866e61a9eSTheodore Ts'o 
272966e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
273066e61a9eSTheodore Ts'o 	es = sbi->s_es;
273166e61a9eSTheodore Ts'o 
273266e61a9eSTheodore Ts'o 	if (es->s_error_count)
273366e61a9eSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count: %u",
273466e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
273566e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
273666e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
273766e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
273866e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
273966e61a9eSTheodore Ts'o 		       es->s_first_error_func,
274066e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
274166e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
274266e61a9eSTheodore Ts'o 			printk(": inode %u",
274366e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
274466e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
274566e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
274666e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
274766e61a9eSTheodore Ts'o 		printk("\n");
274866e61a9eSTheodore Ts'o 	}
274966e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
275066e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
275166e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
275266e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
275366e61a9eSTheodore Ts'o 		       es->s_last_error_func,
275466e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
275566e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
275666e61a9eSTheodore Ts'o 			printk(": inode %u",
275766e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
275866e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
275966e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
276066e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
276166e61a9eSTheodore Ts'o 		printk("\n");
276266e61a9eSTheodore Ts'o 	}
276366e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
276466e61a9eSTheodore Ts'o }
276566e61a9eSTheodore Ts'o 
2766bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2767bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2768bfff6873SLukas Czerner {
2769bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2770bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2771bfff6873SLukas Czerner 	struct super_block *sb;
2772bfff6873SLukas Czerner 	unsigned long timeout = 0;
2773bfff6873SLukas Czerner 	int ret = 0;
2774bfff6873SLukas Czerner 
2775bfff6873SLukas Czerner 	sb = elr->lr_super;
2776bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2777bfff6873SLukas Czerner 
2778bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2779bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2780bfff6873SLukas Czerner 		if (!gdp) {
2781bfff6873SLukas Czerner 			ret = 1;
2782bfff6873SLukas Czerner 			break;
2783bfff6873SLukas Czerner 		}
2784bfff6873SLukas Czerner 
2785bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2786bfff6873SLukas Czerner 			break;
2787bfff6873SLukas Czerner 	}
2788bfff6873SLukas Czerner 
2789bfff6873SLukas Czerner 	if (group == ngroups)
2790bfff6873SLukas Czerner 		ret = 1;
2791bfff6873SLukas Czerner 
2792bfff6873SLukas Czerner 	if (!ret) {
2793bfff6873SLukas Czerner 		timeout = jiffies;
2794bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2795bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2796bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
279751ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
279851ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2799bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2800bfff6873SLukas Czerner 		}
2801bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2802bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2803bfff6873SLukas Czerner 	}
2804bfff6873SLukas Czerner 
2805bfff6873SLukas Czerner 	return ret;
2806bfff6873SLukas Czerner }
2807bfff6873SLukas Czerner 
2808bfff6873SLukas Czerner /*
2809bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
28104ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2811bfff6873SLukas Czerner  */
2812bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2813bfff6873SLukas Czerner {
2814bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2815bfff6873SLukas Czerner 
2816bfff6873SLukas Czerner 	if (!elr)
2817bfff6873SLukas Czerner 		return;
2818bfff6873SLukas Czerner 
2819bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2820bfff6873SLukas Czerner 
2821bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2822bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2823bfff6873SLukas Czerner 	kfree(elr);
2824bfff6873SLukas Czerner }
2825bfff6873SLukas Czerner 
2826bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2827bfff6873SLukas Czerner {
28281bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
28291bb933fbSLukas Czerner 	if (!ext4_li_info) {
28301bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2831bfff6873SLukas Czerner 		return;
28321bb933fbSLukas Czerner 	}
2833bfff6873SLukas Czerner 
2834bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
28351bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2836bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
28371bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2838bfff6873SLukas Czerner }
2839bfff6873SLukas Czerner 
28408f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
28418f1f7453SEric Sandeen 
2842bfff6873SLukas Czerner /*
2843bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2844bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2845bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2846bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2847bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2848bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2849bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2850bfff6873SLukas Czerner  */
2851bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2852bfff6873SLukas Czerner {
2853bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2854bfff6873SLukas Czerner 	struct list_head *pos, *n;
2855bfff6873SLukas Czerner 	struct ext4_li_request *elr;
28564ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2857bfff6873SLukas Czerner 
2858bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2859bfff6873SLukas Czerner 
2860bfff6873SLukas Czerner cont_thread:
2861bfff6873SLukas Czerner 	while (true) {
2862bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2863bfff6873SLukas Czerner 
2864bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2865bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2866bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2867bfff6873SLukas Czerner 			goto exit_thread;
2868bfff6873SLukas Czerner 		}
2869bfff6873SLukas Czerner 
2870bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2871bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2872bfff6873SLukas Czerner 					 lr_request);
2873bfff6873SLukas Czerner 
2874b2c78cd0STheodore Ts'o 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
2875b2c78cd0STheodore Ts'o 				if (ext4_run_li_request(elr) != 0) {
2876b2c78cd0STheodore Ts'o 					/* error, remove the lazy_init job */
2877bfff6873SLukas Czerner 					ext4_remove_li_request(elr);
2878bfff6873SLukas Czerner 					continue;
2879bfff6873SLukas Czerner 				}
2880b2c78cd0STheodore Ts'o 			}
2881bfff6873SLukas Czerner 
2882bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2883bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2884bfff6873SLukas Czerner 		}
2885bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2886bfff6873SLukas Czerner 
2887bfff6873SLukas Czerner 		if (freezing(current))
2888bfff6873SLukas Czerner 			refrigerator();
2889bfff6873SLukas Czerner 
28904ed5c033SLukas Czerner 		cur = jiffies;
28914ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2892f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2893bfff6873SLukas Czerner 			cond_resched();
2894bfff6873SLukas Czerner 			continue;
2895bfff6873SLukas Czerner 		}
2896bfff6873SLukas Czerner 
28974ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
28984ed5c033SLukas Czerner 
28998f1f7453SEric Sandeen 		if (kthread_should_stop()) {
29008f1f7453SEric Sandeen 			ext4_clear_request_list();
29018f1f7453SEric Sandeen 			goto exit_thread;
29028f1f7453SEric Sandeen 		}
2903bfff6873SLukas Czerner 	}
2904bfff6873SLukas Czerner 
2905bfff6873SLukas Czerner exit_thread:
2906bfff6873SLukas Czerner 	/*
2907bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
2908bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
2909bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
2910bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
2911bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
2912bfff6873SLukas Czerner 	 * new one.
2913bfff6873SLukas Czerner 	 */
2914bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2915bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
2916bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
2917bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2918bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2919bfff6873SLukas Czerner 		goto cont_thread;
2920bfff6873SLukas Czerner 	}
2921bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
2922bfff6873SLukas Czerner 	kfree(ext4_li_info);
2923bfff6873SLukas Czerner 	ext4_li_info = NULL;
2924bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2925bfff6873SLukas Czerner 
2926bfff6873SLukas Czerner 	return 0;
2927bfff6873SLukas Czerner }
2928bfff6873SLukas Czerner 
2929bfff6873SLukas Czerner static void ext4_clear_request_list(void)
2930bfff6873SLukas Czerner {
2931bfff6873SLukas Czerner 	struct list_head *pos, *n;
2932bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2933bfff6873SLukas Czerner 
2934bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2935bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2936bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
2937bfff6873SLukas Czerner 				 lr_request);
2938bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
2939bfff6873SLukas Czerner 	}
2940bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2941bfff6873SLukas Czerner }
2942bfff6873SLukas Czerner 
2943bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
2944bfff6873SLukas Czerner {
29458f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
29468f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
29478f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
29488f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
2949bfff6873SLukas Czerner 		ext4_clear_request_list();
2950bfff6873SLukas Czerner 		kfree(ext4_li_info);
2951bfff6873SLukas Czerner 		ext4_li_info = NULL;
2952bfff6873SLukas Czerner 		printk(KERN_CRIT "EXT4: error %d creating inode table "
2953bfff6873SLukas Czerner 				 "initialization thread\n",
2954bfff6873SLukas Czerner 				 err);
2955bfff6873SLukas Czerner 		return err;
2956bfff6873SLukas Czerner 	}
2957bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
2958bfff6873SLukas Czerner 	return 0;
2959bfff6873SLukas Czerner }
2960bfff6873SLukas Czerner 
2961bfff6873SLukas Czerner /*
2962bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
2963bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
2964bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
2965bfff6873SLukas Czerner  * groups and return total number of groups.
2966bfff6873SLukas Czerner  */
2967bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2968bfff6873SLukas Czerner {
2969bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2970bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2971bfff6873SLukas Czerner 
2972bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
2973bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2974bfff6873SLukas Czerner 		if (!gdp)
2975bfff6873SLukas Czerner 			continue;
2976bfff6873SLukas Czerner 
2977bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2978bfff6873SLukas Czerner 			break;
2979bfff6873SLukas Czerner 	}
2980bfff6873SLukas Czerner 
2981bfff6873SLukas Czerner 	return group;
2982bfff6873SLukas Czerner }
2983bfff6873SLukas Czerner 
2984bfff6873SLukas Czerner static int ext4_li_info_new(void)
2985bfff6873SLukas Czerner {
2986bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
2987bfff6873SLukas Czerner 
2988bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2989bfff6873SLukas Czerner 	if (!eli)
2990bfff6873SLukas Czerner 		return -ENOMEM;
2991bfff6873SLukas Czerner 
2992bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
2993bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
2994bfff6873SLukas Czerner 
2995bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
2996bfff6873SLukas Czerner 
2997bfff6873SLukas Czerner 	ext4_li_info = eli;
2998bfff6873SLukas Czerner 
2999bfff6873SLukas Czerner 	return 0;
3000bfff6873SLukas Czerner }
3001bfff6873SLukas Czerner 
3002bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3003bfff6873SLukas Czerner 					    ext4_group_t start)
3004bfff6873SLukas Czerner {
3005bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3006bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3007bfff6873SLukas Czerner 	unsigned long rnd;
3008bfff6873SLukas Czerner 
3009bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3010bfff6873SLukas Czerner 	if (!elr)
3011bfff6873SLukas Czerner 		return NULL;
3012bfff6873SLukas Czerner 
3013bfff6873SLukas Czerner 	elr->lr_super = sb;
3014bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3015bfff6873SLukas Czerner 	elr->lr_next_group = start;
3016bfff6873SLukas Czerner 
3017bfff6873SLukas Czerner 	/*
3018bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3019bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3020bfff6873SLukas Czerner 	 * better.
3021bfff6873SLukas Czerner 	 */
3022bfff6873SLukas Czerner 	get_random_bytes(&rnd, sizeof(rnd));
3023bfff6873SLukas Czerner 	elr->lr_next_sched = jiffies + (unsigned long)rnd %
3024bfff6873SLukas Czerner 			     (EXT4_DEF_LI_MAX_START_DELAY * HZ);
3025bfff6873SLukas Czerner 
3026bfff6873SLukas Czerner 	return elr;
3027bfff6873SLukas Czerner }
3028bfff6873SLukas Czerner 
3029bfff6873SLukas Czerner static int ext4_register_li_request(struct super_block *sb,
3030bfff6873SLukas Czerner 				    ext4_group_t first_not_zeroed)
3031bfff6873SLukas Czerner {
3032bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3033bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3034bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
30356c5a6cb9SAndrew Morton 	int ret = 0;
3036bfff6873SLukas Czerner 
303751ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
303851ce6511SLukas Czerner 		/*
303951ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
304051ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
304151ce6511SLukas Czerner 		 */
304251ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
3043beed5ecbSNicolas Kaiser 		return 0;
304451ce6511SLukas Czerner 	}
3045bfff6873SLukas Czerner 
3046bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
3047bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
304855ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
3049beed5ecbSNicolas Kaiser 		return 0;
3050bfff6873SLukas Czerner 
3051bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
3052beed5ecbSNicolas Kaiser 	if (!elr)
3053beed5ecbSNicolas Kaiser 		return -ENOMEM;
3054bfff6873SLukas Czerner 
3055bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3056bfff6873SLukas Czerner 
3057bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3058bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3059bfff6873SLukas Czerner 		if (ret)
3060bfff6873SLukas Czerner 			goto out;
3061bfff6873SLukas Czerner 	}
3062bfff6873SLukas Czerner 
3063bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3064bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3065bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3066bfff6873SLukas Czerner 
3067bfff6873SLukas Czerner 	sbi->s_li_request = elr;
306846e4690bSTao Ma 	/*
306946e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
307046e4690bSTao Ma 	 * the request_list and the removal and free of it is
307146e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
307246e4690bSTao Ma 	 */
307346e4690bSTao Ma 	elr = NULL;
3074bfff6873SLukas Czerner 
3075bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3076bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3077bfff6873SLukas Czerner 		if (ret)
3078bfff6873SLukas Czerner 			goto out;
3079bfff6873SLukas Czerner 	}
3080bfff6873SLukas Czerner out:
3081bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3082beed5ecbSNicolas Kaiser 	if (ret)
3083bfff6873SLukas Czerner 		kfree(elr);
3084bfff6873SLukas Czerner 	return ret;
3085bfff6873SLukas Czerner }
3086bfff6873SLukas Czerner 
3087bfff6873SLukas Czerner /*
3088bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3089bfff6873SLukas Czerner  * module unload.
3090bfff6873SLukas Czerner  */
3091bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3092bfff6873SLukas Czerner {
3093bfff6873SLukas Czerner 	/*
3094bfff6873SLukas Czerner 	 * If thread exited earlier
3095bfff6873SLukas Czerner 	 * there's nothing to be done.
3096bfff6873SLukas Czerner 	 */
30978f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3098bfff6873SLukas Czerner 		return;
3099bfff6873SLukas Czerner 
31008f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3101bfff6873SLukas Czerner }
3102bfff6873SLukas Czerner 
3103617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
31047477827fSAneesh Kumar K.V 				__releases(kernel_lock)
31057477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
3106ac27a0ecSDave Kleikamp {
3107d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3108ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3109617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
3110617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
3111617ba13bSMingming Cao 	ext4_fsblk_t block;
3112617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
311370bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3114ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3115ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3116ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3117ac27a0ecSDave Kleikamp 	struct inode *root;
31189f6200bbSTheodore Ts'o 	char *cp;
31190390131bSFrank Mayhar 	const char *descr;
3120dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3121281b5995STheodore Ts'o 	int blocksize, clustersize;
31224ec11028STheodore Ts'o 	unsigned int db_count;
31234ec11028STheodore Ts'o 	unsigned int i;
3124281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3125bd81d8eeSLaurent Vivier 	__u64 blocks_count;
3126833f4077SPeter Zijlstra 	int err;
3127b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3128bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3129ac27a0ecSDave Kleikamp 
3130ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3131ac27a0ecSDave Kleikamp 	if (!sbi)
3132dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3133705895b6SPekka Enberg 
3134705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3135705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3136705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
3137705895b6SPekka Enberg 		kfree(sbi);
3138dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3139705895b6SPekka Enberg 	}
3140ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
3141ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
3142617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
3143617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
3144240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3145d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3146f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3147f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3148f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3149ac27a0ecSDave Kleikamp 
31509f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
31519f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
31529f6200bbSTheodore Ts'o 		*cp = '!';
31539f6200bbSTheodore Ts'o 
3154dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3155617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3156ac27a0ecSDave Kleikamp 	if (!blocksize) {
3157b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3158ac27a0ecSDave Kleikamp 		goto out_fail;
3159ac27a0ecSDave Kleikamp 	}
3160ac27a0ecSDave Kleikamp 
3161ac27a0ecSDave Kleikamp 	/*
3162617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3163ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3164ac27a0ecSDave Kleikamp 	 */
3165617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
316670bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
316770bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3168ac27a0ecSDave Kleikamp 	} else {
316970bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3170ac27a0ecSDave Kleikamp 	}
3171ac27a0ecSDave Kleikamp 
317270bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
3173b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3174ac27a0ecSDave Kleikamp 		goto out_fail;
3175ac27a0ecSDave Kleikamp 	}
3176ac27a0ecSDave Kleikamp 	/*
3177ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3178617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3179ac27a0ecSDave Kleikamp 	 */
3180617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3181ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3182ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3183617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3184617ba13bSMingming Cao 		goto cantfind_ext4;
3185afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3186ac27a0ecSDave Kleikamp 
3187ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3188ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3189fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3190617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3191fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
3192437ca0fdSDmitry Monakhov 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
3193437ca0fdSDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
3194437ca0fdSDmitry Monakhov 			"2.6.38");
3195fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3196437ca0fdSDmitry Monakhov 	}
3197617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3198fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3199ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
320003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
3201fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
32022e7842b8SHugh Dickins #endif
320303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3204fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
32052e7842b8SHugh Dickins #endif
32066fd7a467STheodore Ts'o 	set_opt(sb, MBLK_IO_SUBMIT);
3207617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3208fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3209617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3210fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3211617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3212fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3213ac27a0ecSDave Kleikamp 
3214617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3215fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3216bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3217fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3218bb4f397aSAneesh Kumar K.V 	else
3219fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
32208b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
3221fd8c37ecSTheodore Ts'o 		set_opt(sb, BLOCK_VALIDITY);
32228b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3223fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3224ac27a0ecSDave Kleikamp 
3225ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
3226ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
322730773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
322830773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
322930773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3230ac27a0ecSDave Kleikamp 
32318b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3232fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3233ac27a0ecSDave Kleikamp 
32341e2462f9SMingming Cao 	/*
3235dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3236dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3237dd919b98SAneesh Kumar K.V 	 */
32388b67f04aSTheodore Ts'o 	if (!IS_EXT3_SB(sb) &&
32398b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3240fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3241dd919b98SAneesh Kumar K.V 
324251ce6511SLukas Czerner 	/*
324351ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
324451ce6511SLukas Czerner 	 * no mount option specified.
324551ce6511SLukas Czerner 	 */
324651ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
324751ce6511SLukas Czerner 
32488b67f04aSTheodore Ts'o 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
32498b67f04aSTheodore Ts'o 			   &journal_devnum, &journal_ioprio, NULL, 0)) {
32508b67f04aSTheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
32518b67f04aSTheodore Ts'o 			 "failed to parse options in superblock: %s",
32528b67f04aSTheodore Ts'o 			 sbi->s_es->s_mount_opts);
32538b67f04aSTheodore Ts'o 	}
3254b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3255b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
3256ac27a0ecSDave Kleikamp 		goto failed_mount;
3257ac27a0ecSDave Kleikamp 
325856889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
325956889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
326056889787STheodore Ts'o 			    "with data=journal disables delayed "
326156889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
326256889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
326356889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
326456889787STheodore Ts'o 				 "both data=journal and delalloc");
326556889787STheodore Ts'o 			goto failed_mount;
326656889787STheodore Ts'o 		}
326756889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
326856889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
326956889787STheodore Ts'o 				 "both data=journal and delalloc");
327056889787STheodore Ts'o 			goto failed_mount;
327156889787STheodore Ts'o 		}
327256889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
327356889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
327456889787STheodore Ts'o 	}
327556889787STheodore Ts'o 
327656889787STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
327756889787STheodore Ts'o 	if (test_opt(sb, DIOREAD_NOLOCK)) {
327856889787STheodore Ts'o 		if (blocksize < PAGE_SIZE) {
327956889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
328056889787STheodore Ts'o 				 "dioread_nolock if block size != PAGE_SIZE");
328156889787STheodore Ts'o 			goto failed_mount;
328256889787STheodore Ts'o 		}
328356889787STheodore Ts'o 	}
328456889787STheodore Ts'o 
3285ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3286482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3287ac27a0ecSDave Kleikamp 
3288617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3289617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3290617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3291617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
3292b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3293b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3294b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3295469108ffSTheodore Tso 
32962035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
32972035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
32982035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
32992035e776STheodore Ts'o 				 "using the ext4 subsystem");
33002035e776STheodore Ts'o 		else {
33012035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
33022035e776STheodore Ts'o 				 "to feature incompatibilities");
33032035e776STheodore Ts'o 			goto failed_mount;
33042035e776STheodore Ts'o 		}
33052035e776STheodore Ts'o 	}
33062035e776STheodore Ts'o 
33072035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
33082035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
33092035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
33102035e776STheodore Ts'o 				 "using the ext4 subsystem");
33112035e776STheodore Ts'o 		else {
33122035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
33132035e776STheodore Ts'o 				 "to feature incompatibilities");
33142035e776STheodore Ts'o 			goto failed_mount;
33152035e776STheodore Ts'o 		}
33162035e776STheodore Ts'o 	}
33172035e776STheodore Ts'o 
3318469108ffSTheodore Tso 	/*
3319ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3320ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3321ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3322ac27a0ecSDave Kleikamp 	 */
3323a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3324ac27a0ecSDave Kleikamp 		goto failed_mount;
3325a13fb1a4SEric Sandeen 
3326617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3327617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3328b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3329b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
3330ac27a0ecSDave Kleikamp 		goto failed_mount;
3331ac27a0ecSDave Kleikamp 	}
3332ac27a0ecSDave Kleikamp 
3333ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3334ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3335ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3336b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3337ce40733cSAneesh Kumar K.V 					blocksize);
3338ac27a0ecSDave Kleikamp 			goto failed_mount;
3339ac27a0ecSDave Kleikamp 		}
3340ac27a0ecSDave Kleikamp 
3341ac27a0ecSDave Kleikamp 		brelse(bh);
334270bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
334370bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
334470bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
3345ac27a0ecSDave Kleikamp 		if (!bh) {
3346b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3347b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3348ac27a0ecSDave Kleikamp 			goto failed_mount;
3349ac27a0ecSDave Kleikamp 		}
3350617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
3351ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3352617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3353b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3354b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3355ac27a0ecSDave Kleikamp 			goto failed_mount;
3356ac27a0ecSDave Kleikamp 		}
3357ac27a0ecSDave Kleikamp 	}
3358ac27a0ecSDave Kleikamp 
3359a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3360a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
3361f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3362f287a1a5STheodore Ts'o 						      has_huge_files);
3363f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3364ac27a0ecSDave Kleikamp 
3365617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3366617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3367617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3368ac27a0ecSDave Kleikamp 	} else {
3369ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3370ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3371617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
33721330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3373ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3374b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3375b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3376ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3377ac27a0ecSDave Kleikamp 			goto failed_mount;
3378ac27a0ecSDave Kleikamp 		}
3379ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3380ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3381ac27a0ecSDave Kleikamp 	}
33820b8e58a1SAndreas Dilger 
33830d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
33840d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
33858fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
33860d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3387d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3388b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3389b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
33900d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
33910d1ee42fSAlexandre Ratchov 			goto failed_mount;
33920d1ee42fSAlexandre Ratchov 		}
33930d1ee42fSAlexandre Ratchov 	} else
33940d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
33950b8e58a1SAndreas Dilger 
3396ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3397ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3398b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
3399617ba13bSMingming Cao 		goto cantfind_ext4;
34000b8e58a1SAndreas Dilger 
3401617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3402ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3403617ba13bSMingming Cao 		goto cantfind_ext4;
3404ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3405ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
34060d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3407ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3408ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3409e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3410e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
34110b8e58a1SAndreas Dilger 
3412ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3413ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3414ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3415f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
3416f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
3417f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3418f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3419f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
3420f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3421f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3422f99b2589STheodore Ts'o #else
3423f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3424f99b2589STheodore Ts'o #endif
3425f99b2589STheodore Ts'o 		sb->s_dirt = 1;
3426f99b2589STheodore Ts'o 	}
3427ac27a0ecSDave Kleikamp 
3428281b5995STheodore Ts'o 	/* Handle clustersize */
3429281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3430281b5995STheodore Ts'o 	has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3431281b5995STheodore Ts'o 				EXT4_FEATURE_RO_COMPAT_BIGALLOC);
3432281b5995STheodore Ts'o 	if (has_bigalloc) {
3433281b5995STheodore Ts'o 		if (clustersize < blocksize) {
3434281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3435281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
3436281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
3437281b5995STheodore Ts'o 			goto failed_mount;
3438281b5995STheodore Ts'o 		}
3439281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3440281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
3441281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
3442281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
3443281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
3444281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3445281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
3446281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3447281b5995STheodore Ts'o 			goto failed_mount;
3448281b5995STheodore Ts'o 		}
3449281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
3450281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3451281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3452281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
3453281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
3454281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
3455281b5995STheodore Ts'o 			goto failed_mount;
3456281b5995STheodore Ts'o 		}
3457281b5995STheodore Ts'o 	} else {
3458281b5995STheodore Ts'o 		if (clustersize != blocksize) {
3459281b5995STheodore Ts'o 			ext4_warning(sb, "fragment/cluster size (%d) != "
3460281b5995STheodore Ts'o 				     "block size (%d)", clustersize,
3461281b5995STheodore Ts'o 				     blocksize);
3462281b5995STheodore Ts'o 			clustersize = blocksize;
3463281b5995STheodore Ts'o 		}
3464ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
3465b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3466b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
3467ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
3468ac27a0ecSDave Kleikamp 			goto failed_mount;
3469ac27a0ecSDave Kleikamp 		}
3470281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3471281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
3472281b5995STheodore Ts'o 	}
3473281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
3474281b5995STheodore Ts'o 
3475ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
3476b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3477b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
3478ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
3479ac27a0ecSDave Kleikamp 		goto failed_mount;
3480ac27a0ecSDave Kleikamp 	}
3481ac27a0ecSDave Kleikamp 
3482bf43d84bSEric Sandeen 	/*
3483bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3484bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3485bf43d84bSEric Sandeen 	 */
34865a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
348730ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
34885a9ae68aSDarrick J. Wong 	if (err) {
3489b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3490bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3491ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
349290c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
34935a9ae68aSDarrick J. Wong 		ret = err;
3494ac27a0ecSDave Kleikamp 		goto failed_mount;
3495ac27a0ecSDave Kleikamp 	}
3496ac27a0ecSDave Kleikamp 
3497617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3498617ba13bSMingming Cao 		goto cantfind_ext4;
3499e7c95593SEric Sandeen 
35000f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
35010f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
35020f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3503b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3504b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
35050f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
35060f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
35070f2ddca6SFrom: Thiemo Nagel 	}
35080f2ddca6SFrom: Thiemo Nagel 
35094ec11028STheodore Ts'o 	/*
35104ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
35114ec11028STheodore Ts'o 	 * of the filesystem.
35124ec11028STheodore Ts'o 	 */
35134ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3514b31e1552SEric Sandeen                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
3515b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3516e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
35174ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3518e7c95593SEric Sandeen 		goto failed_mount;
3519e7c95593SEric Sandeen 	}
3520bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3521bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3522bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3523bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
35244ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3525b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
35264ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3527b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
35284ec11028STheodore Ts'o 		       ext4_blocks_count(es),
35294ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
35304ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
35314ec11028STheodore Ts'o 		goto failed_mount;
35324ec11028STheodore Ts'o 	}
3533bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3534fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3535fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3536617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3537617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3538f18a5f21STheodore Ts'o 	sbi->s_group_desc = ext4_kvmalloc(db_count *
3539f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
3540ac27a0ecSDave Kleikamp 					  GFP_KERNEL);
3541ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3542b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
3543ac27a0ecSDave Kleikamp 		goto failed_mount;
3544ac27a0ecSDave Kleikamp 	}
3545ac27a0ecSDave Kleikamp 
35469f6200bbSTheodore Ts'o 	if (ext4_proc_root)
35479f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
3548240799cdSTheodore Ts'o 
3549705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3550ac27a0ecSDave Kleikamp 
3551ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
355270bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3553ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
3554ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3555b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3556b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3557ac27a0ecSDave Kleikamp 			db_count = i;
3558ac27a0ecSDave Kleikamp 			goto failed_mount2;
3559ac27a0ecSDave Kleikamp 		}
3560ac27a0ecSDave Kleikamp 	}
3561bfff6873SLukas Czerner 	if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
3562b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
3563ac27a0ecSDave Kleikamp 		goto failed_mount2;
3564ac27a0ecSDave Kleikamp 	}
3565772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
3566772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
3567b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3568b31e1552SEric Sandeen 			       "unable to initialize "
3569b31e1552SEric Sandeen 			       "flex_bg meta info!");
3570772cb7c8SJose R. Santos 			goto failed_mount2;
3571772cb7c8SJose R. Santos 		}
3572772cb7c8SJose R. Santos 
3573ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
3574ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3575ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3576ac27a0ecSDave Kleikamp 
357704496411STao Ma 	init_timer(&sbi->s_err_report);
357804496411STao Ma 	sbi->s_err_report.function = print_daily_error_info;
357904496411STao Ma 	sbi->s_err_report.data = (unsigned long) sb;
358004496411STao Ma 
358157042651STheodore Ts'o 	err = percpu_counter_init(&sbi->s_freeclusters_counter,
35825dee5437STheodore Ts'o 			ext4_count_free_clusters(sb));
3583ce7e010aSTheodore Ts'o 	if (!err) {
3584ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
3585ce7e010aSTheodore Ts'o 				ext4_count_free_inodes(sb));
3586ce7e010aSTheodore Ts'o 	}
3587ce7e010aSTheodore Ts'o 	if (!err) {
3588ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
3589ce7e010aSTheodore Ts'o 				ext4_count_dirs(sb));
3590ce7e010aSTheodore Ts'o 	}
3591ce7e010aSTheodore Ts'o 	if (!err) {
359257042651STheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0);
3593ce7e010aSTheodore Ts'o 	}
3594ce7e010aSTheodore Ts'o 	if (err) {
3595ce7e010aSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
3596ce7e010aSTheodore Ts'o 		goto failed_mount3;
3597ce7e010aSTheodore Ts'o 	}
3598ce7e010aSTheodore Ts'o 
3599c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
360055138e0bSTheodore Ts'o 	sbi->s_max_writeback_mb_bump = 128;
3601c9de560dSAlex Tomas 
3602ac27a0ecSDave Kleikamp 	/*
3603ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
3604ac27a0ecSDave Kleikamp 	 */
36059ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
36069ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
3607617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
36089ca92389STheodore Ts'o 	else
36099ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
3610617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3611617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3612ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3613617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
3614617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
3615ac27a0ecSDave Kleikamp #endif
3616f2fa2ffcSAneesh Kumar K.V 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3617f2fa2ffcSAneesh Kumar K.V 
3618ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
36193b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
36208f82f840SYongqiang Yang 	sbi->s_resize_flags = 0;
3621ac27a0ecSDave Kleikamp 
3622ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3623ac27a0ecSDave Kleikamp 
3624ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3625617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
3626617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
3627ac27a0ecSDave Kleikamp 
3628c5e06d10SJohann Lombardi 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
3629c5e06d10SJohann Lombardi 	    !(sb->s_flags & MS_RDONLY))
3630c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
3631c5e06d10SJohann Lombardi 			goto failed_mount3;
3632c5e06d10SJohann Lombardi 
3633ac27a0ecSDave Kleikamp 	/*
3634ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3635ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3636ac27a0ecSDave Kleikamp 	 */
3637ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
3638617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3639617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
3640ac27a0ecSDave Kleikamp 			goto failed_mount3;
36410390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
36420390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3643b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3644b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3645744692dcSJiaying Zhang 		goto failed_mount_wq;
3646ac27a0ecSDave Kleikamp 	} else {
3647fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
36480390131bSFrank Mayhar 		sbi->s_journal = NULL;
36490390131bSFrank Mayhar 		needs_recovery = 0;
36500390131bSFrank Mayhar 		goto no_journal;
3651ac27a0ecSDave Kleikamp 	}
3652ac27a0ecSDave Kleikamp 
3653eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
3654eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3655eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3656b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3657744692dcSJiaying Zhang 		goto failed_mount_wq;
3658eb40a09cSJose R. Santos 	}
3659eb40a09cSJose R. Santos 
3660d4da6c9cSLinus Torvalds 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3661d4da6c9cSLinus Torvalds 		jbd2_journal_set_features(sbi->s_journal,
3662d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3663d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3664d4da6c9cSLinus Torvalds 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3665818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
3666818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
3667818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3668818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3669d4da6c9cSLinus Torvalds 	} else {
3670d4da6c9cSLinus Torvalds 		jbd2_journal_clear_features(sbi->s_journal,
3671d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3672d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3673d4da6c9cSLinus Torvalds 	}
3674818d276cSGirish Shilamkar 
3675ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3676ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3677ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3678ac27a0ecSDave Kleikamp 	case 0:
3679ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
368063f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
368163f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
368263f57933SAndrew Morton 		 */
3683dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3684dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3685fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3686ac27a0ecSDave Kleikamp 		else
3687fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3688ac27a0ecSDave Kleikamp 		break;
3689ac27a0ecSDave Kleikamp 
3690617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
3691617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
3692dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
3693dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
3694b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
3695b31e1552SEric Sandeen 			       "requested data journaling mode");
3696744692dcSJiaying Zhang 			goto failed_mount_wq;
3697ac27a0ecSDave Kleikamp 		}
3698ac27a0ecSDave Kleikamp 	default:
3699ac27a0ecSDave Kleikamp 		break;
3700ac27a0ecSDave Kleikamp 	}
3701b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3702ac27a0ecSDave Kleikamp 
3703ce7e010aSTheodore Ts'o 	/*
3704ce7e010aSTheodore Ts'o 	 * The journal may have updated the bg summary counts, so we
3705ce7e010aSTheodore Ts'o 	 * need to update the global counters.
3706ce7e010aSTheodore Ts'o 	 */
370757042651STheodore Ts'o 	percpu_counter_set(&sbi->s_freeclusters_counter,
37085dee5437STheodore Ts'o 			   ext4_count_free_clusters(sb));
3709ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_freeinodes_counter,
371084061e07SDmitry Monakhov 			   ext4_count_free_inodes(sb));
3711ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_dirs_counter,
371284061e07SDmitry Monakhov 			   ext4_count_dirs(sb));
371357042651STheodore Ts'o 	percpu_counter_set(&sbi->s_dirtyclusters_counter, 0);
3714206f7ab4SChristoph Hellwig 
3715ce7e010aSTheodore Ts'o no_journal:
3716fd89d5f2STejun Heo 	/*
3717fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
3718fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
3719fd89d5f2STejun Heo 	 */
3720fd89d5f2STejun Heo 	EXT4_SB(sb)->dio_unwritten_wq =
3721ae005cbeSLinus Torvalds 		alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
37224c0425ffSMingming Cao 	if (!EXT4_SB(sb)->dio_unwritten_wq) {
37234c0425ffSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
37244c0425ffSMingming Cao 		goto failed_mount_wq;
37254c0425ffSMingming Cao 	}
37264c0425ffSMingming Cao 
3727ac27a0ecSDave Kleikamp 	/*
3728dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
3729ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
3730ac27a0ecSDave Kleikamp 	 */
3731ac27a0ecSDave Kleikamp 
37321d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
37331d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
3734b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
37351d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
373632a9bb57SManish Katiyar 		root = NULL;
3737ac27a0ecSDave Kleikamp 		goto failed_mount4;
3738ac27a0ecSDave Kleikamp 	}
3739ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
3740b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
3741ac27a0ecSDave Kleikamp 		goto failed_mount4;
3742ac27a0ecSDave Kleikamp 	}
37431d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
37441d1fe1eeSDavid Howells 	if (!sb->s_root) {
3745b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
37461d1fe1eeSDavid Howells 		ret = -ENOMEM;
37471d1fe1eeSDavid Howells 		goto failed_mount4;
37481d1fe1eeSDavid Howells 	}
3749ac27a0ecSDave Kleikamp 
3750617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
3751ef7f3835SKalpak Shah 
3752ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
3753ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3754ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3755ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
3756ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3757ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
3758ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3759ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
3760ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3761ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
3762ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3763ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
3764ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3765ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
3766ef7f3835SKalpak Shah 		}
3767ef7f3835SKalpak Shah 	}
3768ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
3769ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3770ef7f3835SKalpak Shah 							sbi->s_inode_size) {
3771ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3772ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
3773b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
3774b31e1552SEric Sandeen 			 "available");
3775ef7f3835SKalpak Shah 	}
3776ef7f3835SKalpak Shah 
37776fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
37786fd058f7STheodore Ts'o 	if (err) {
3779b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
3780fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
37816fd058f7STheodore Ts'o 		goto failed_mount4;
37826fd058f7STheodore Ts'o 	}
37836fd058f7STheodore Ts'o 
3784c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
3785c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
3786c2774d84SAneesh Kumar K.V 	if (err) {
3787421f91d2SUwe Kleine-König 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
3788c2774d84SAneesh Kumar K.V 			 err);
3789dcf2d804STao Ma 		goto failed_mount5;
3790c2774d84SAneesh Kumar K.V 	}
3791c2774d84SAneesh Kumar K.V 
3792bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
3793bfff6873SLukas Czerner 	if (err)
3794dcf2d804STao Ma 		goto failed_mount6;
3795bfff6873SLukas Czerner 
37963197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
37973197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
37983197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
37993197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
3800dcf2d804STao Ma 	if (err)
3801dcf2d804STao Ma 		goto failed_mount7;
38023197ebdbSTheodore Ts'o 
3803617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3804617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
3805617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
38060390131bSFrank Mayhar 	if (needs_recovery) {
3807b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
3808617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
38090390131bSFrank Mayhar 	}
38100390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
38110390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
38120390131bSFrank Mayhar 			descr = " journalled data mode";
38130390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
38140390131bSFrank Mayhar 			descr = " ordered data mode";
38150390131bSFrank Mayhar 		else
38160390131bSFrank Mayhar 			descr = " writeback data mode";
38170390131bSFrank Mayhar 	} else
38180390131bSFrank Mayhar 		descr = "out journal";
38190390131bSFrank Mayhar 
3820d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
38218b67f04aSTheodore Ts'o 		 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
38228b67f04aSTheodore Ts'o 		 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
3823ac27a0ecSDave Kleikamp 
382466e61a9eSTheodore Ts'o 	if (es->s_error_count)
382566e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
3826ac27a0ecSDave Kleikamp 
3827d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
3828ac27a0ecSDave Kleikamp 	return 0;
3829ac27a0ecSDave Kleikamp 
3830617ba13bSMingming Cao cantfind_ext4:
3831ac27a0ecSDave Kleikamp 	if (!silent)
3832b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
3833ac27a0ecSDave Kleikamp 	goto failed_mount;
3834ac27a0ecSDave Kleikamp 
3835dcf2d804STao Ma failed_mount7:
3836dcf2d804STao Ma 	ext4_unregister_li_request(sb);
3837dcf2d804STao Ma failed_mount6:
3838dcf2d804STao Ma 	ext4_ext_release(sb);
3839dcf2d804STao Ma failed_mount5:
3840dcf2d804STao Ma 	ext4_mb_release(sb);
3841dcf2d804STao Ma 	ext4_release_system_zone(sb);
3842ac27a0ecSDave Kleikamp failed_mount4:
384332a9bb57SManish Katiyar 	iput(root);
384432a9bb57SManish Katiyar 	sb->s_root = NULL;
3845b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
38464c0425ffSMingming Cao 	destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
38474c0425ffSMingming Cao failed_mount_wq:
38480390131bSFrank Mayhar 	if (sbi->s_journal) {
3849dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
385047b4a50bSJan Kara 		sbi->s_journal = NULL;
38510390131bSFrank Mayhar 	}
3852ac27a0ecSDave Kleikamp failed_mount3:
385304496411STao Ma 	del_timer(&sbi->s_err_report);
38549933fc0aSTheodore Ts'o 	if (sbi->s_flex_groups)
38559933fc0aSTheodore Ts'o 		ext4_kvfree(sbi->s_flex_groups);
385657042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
3857ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
3858ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
385957042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
3860c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
3861c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
3862ac27a0ecSDave Kleikamp failed_mount2:
3863ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
3864ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
3865f18a5f21STheodore Ts'o 	ext4_kvfree(sbi->s_group_desc);
3866ac27a0ecSDave Kleikamp failed_mount:
3867240799cdSTheodore Ts'o 	if (sbi->s_proc) {
38689f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
3869240799cdSTheodore Ts'o 	}
3870ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3871ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3872ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
3873ac27a0ecSDave Kleikamp #endif
3874617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
3875ac27a0ecSDave Kleikamp 	brelse(bh);
3876ac27a0ecSDave Kleikamp out_fail:
3877ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
3878f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
3879ac27a0ecSDave Kleikamp 	kfree(sbi);
3880dcc7dae3SCyrill Gorcunov out_free_orig:
3881d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
38821d1fe1eeSDavid Howells 	return ret;
3883ac27a0ecSDave Kleikamp }
3884ac27a0ecSDave Kleikamp 
3885ac27a0ecSDave Kleikamp /*
3886ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
3887ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
3888ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
3889ac27a0ecSDave Kleikamp  */
3890617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
3891ac27a0ecSDave Kleikamp {
3892617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3893ac27a0ecSDave Kleikamp 
3894ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
389530773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
389630773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
3897ac27a0ecSDave Kleikamp 
3898a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
3899ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
3900dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
3901ac27a0ecSDave Kleikamp 	else
3902dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
39035bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
39045bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
39055bf5683aSHidehiro Kawai 	else
39065bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
3907a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
3908ac27a0ecSDave Kleikamp }
3909ac27a0ecSDave Kleikamp 
3910617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
3911ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
3912ac27a0ecSDave Kleikamp {
3913ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
3914ac27a0ecSDave Kleikamp 	journal_t *journal;
3915ac27a0ecSDave Kleikamp 
39160390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
39170390131bSFrank Mayhar 
3918ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
3919ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
3920ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
3921ac27a0ecSDave Kleikamp 
39221d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
39231d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
3924b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
3925ac27a0ecSDave Kleikamp 		return NULL;
3926ac27a0ecSDave Kleikamp 	}
3927ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
3928ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
3929ac27a0ecSDave Kleikamp 		iput(journal_inode);
3930b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3931ac27a0ecSDave Kleikamp 		return NULL;
3932ac27a0ecSDave Kleikamp 	}
3933ac27a0ecSDave Kleikamp 
3934e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3935ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
39361d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
3937b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
3938ac27a0ecSDave Kleikamp 		iput(journal_inode);
3939ac27a0ecSDave Kleikamp 		return NULL;
3940ac27a0ecSDave Kleikamp 	}
3941ac27a0ecSDave Kleikamp 
3942dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
3943ac27a0ecSDave Kleikamp 	if (!journal) {
3944b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3945ac27a0ecSDave Kleikamp 		iput(journal_inode);
3946ac27a0ecSDave Kleikamp 		return NULL;
3947ac27a0ecSDave Kleikamp 	}
3948ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3949617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3950ac27a0ecSDave Kleikamp 	return journal;
3951ac27a0ecSDave Kleikamp }
3952ac27a0ecSDave Kleikamp 
3953617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
3954ac27a0ecSDave Kleikamp 				       dev_t j_dev)
3955ac27a0ecSDave Kleikamp {
3956ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3957ac27a0ecSDave Kleikamp 	journal_t *journal;
3958617ba13bSMingming Cao 	ext4_fsblk_t start;
3959617ba13bSMingming Cao 	ext4_fsblk_t len;
3960ac27a0ecSDave Kleikamp 	int hblock, blocksize;
3961617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
3962ac27a0ecSDave Kleikamp 	unsigned long offset;
3963617ba13bSMingming Cao 	struct ext4_super_block *es;
3964ac27a0ecSDave Kleikamp 	struct block_device *bdev;
3965ac27a0ecSDave Kleikamp 
39660390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
39670390131bSFrank Mayhar 
3968b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
3969ac27a0ecSDave Kleikamp 	if (bdev == NULL)
3970ac27a0ecSDave Kleikamp 		return NULL;
3971ac27a0ecSDave Kleikamp 
3972ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
3973e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
3974ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
3975b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3976b31e1552SEric Sandeen 			"blocksize too small for journal device");
3977ac27a0ecSDave Kleikamp 		goto out_bdev;
3978ac27a0ecSDave Kleikamp 	}
3979ac27a0ecSDave Kleikamp 
3980617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3981617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3982ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3983ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3984b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3985b31e1552SEric Sandeen 		       "external journal");
3986ac27a0ecSDave Kleikamp 		goto out_bdev;
3987ac27a0ecSDave Kleikamp 	}
3988ac27a0ecSDave Kleikamp 
3989617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3990617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3991ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3992617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3993b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
3994b31e1552SEric Sandeen 					"bad superblock");
3995ac27a0ecSDave Kleikamp 		brelse(bh);
3996ac27a0ecSDave Kleikamp 		goto out_bdev;
3997ac27a0ecSDave Kleikamp 	}
3998ac27a0ecSDave Kleikamp 
3999617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4000b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4001ac27a0ecSDave Kleikamp 		brelse(bh);
4002ac27a0ecSDave Kleikamp 		goto out_bdev;
4003ac27a0ecSDave Kleikamp 	}
4004ac27a0ecSDave Kleikamp 
4005bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4006ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4007ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4008ac27a0ecSDave Kleikamp 
4009dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4010ac27a0ecSDave Kleikamp 					start, len, blocksize);
4011ac27a0ecSDave Kleikamp 	if (!journal) {
4012b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4013ac27a0ecSDave Kleikamp 		goto out_bdev;
4014ac27a0ecSDave Kleikamp 	}
4015ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4016ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
4017ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4018ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4019b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4020ac27a0ecSDave Kleikamp 		goto out_journal;
4021ac27a0ecSDave Kleikamp 	}
4022ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4023b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4024b31e1552SEric Sandeen 					"user (unsupported) - %d",
4025ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4026ac27a0ecSDave Kleikamp 		goto out_journal;
4027ac27a0ecSDave Kleikamp 	}
4028617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4029617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4030ac27a0ecSDave Kleikamp 	return journal;
40310b8e58a1SAndreas Dilger 
4032ac27a0ecSDave Kleikamp out_journal:
4033dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4034ac27a0ecSDave Kleikamp out_bdev:
4035617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4036ac27a0ecSDave Kleikamp 	return NULL;
4037ac27a0ecSDave Kleikamp }
4038ac27a0ecSDave Kleikamp 
4039617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4040617ba13bSMingming Cao 			     struct ext4_super_block *es,
4041ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4042ac27a0ecSDave Kleikamp {
4043ac27a0ecSDave Kleikamp 	journal_t *journal;
4044ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4045ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4046ac27a0ecSDave Kleikamp 	int err = 0;
4047ac27a0ecSDave Kleikamp 	int really_read_only;
4048ac27a0ecSDave Kleikamp 
40490390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
40500390131bSFrank Mayhar 
4051ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4052ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4053b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4054b31e1552SEric Sandeen 			"numbers have changed");
4055ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4056ac27a0ecSDave Kleikamp 	} else
4057ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4058ac27a0ecSDave Kleikamp 
4059ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4060ac27a0ecSDave Kleikamp 
4061ac27a0ecSDave Kleikamp 	/*
4062ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4063ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4064ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4065ac27a0ecSDave Kleikamp 	 */
4066617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
4067ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
4068b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4069b31e1552SEric Sandeen 					"required on readonly filesystem");
4070ac27a0ecSDave Kleikamp 			if (really_read_only) {
4071b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4072b31e1552SEric Sandeen 					"unavailable, cannot proceed");
4073ac27a0ecSDave Kleikamp 				return -EROFS;
4074ac27a0ecSDave Kleikamp 			}
4075b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4076b31e1552SEric Sandeen 			       "be enabled during recovery");
4077ac27a0ecSDave Kleikamp 		}
4078ac27a0ecSDave Kleikamp 	}
4079ac27a0ecSDave Kleikamp 
4080ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4081b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4082b31e1552SEric Sandeen 		       "and inode journals!");
4083ac27a0ecSDave Kleikamp 		return -EINVAL;
4084ac27a0ecSDave Kleikamp 	}
4085ac27a0ecSDave Kleikamp 
4086ac27a0ecSDave Kleikamp 	if (journal_inum) {
4087617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4088ac27a0ecSDave Kleikamp 			return -EINVAL;
4089ac27a0ecSDave Kleikamp 	} else {
4090617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4091ac27a0ecSDave Kleikamp 			return -EINVAL;
4092ac27a0ecSDave Kleikamp 	}
4093ac27a0ecSDave Kleikamp 
409490576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4095b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
40964776004fSTheodore Ts'o 
4097ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
4098dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
4099ac27a0ecSDave Kleikamp 		if (err)  {
4100b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error updating journal");
4101dab291afSMingming Cao 			jbd2_journal_destroy(journal);
4102ac27a0ecSDave Kleikamp 			return err;
4103ac27a0ecSDave Kleikamp 		}
4104ac27a0ecSDave Kleikamp 	}
4105ac27a0ecSDave Kleikamp 
4106617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
4107dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
41081c13d5c0STheodore Ts'o 	if (!err) {
41091c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
41101c13d5c0STheodore Ts'o 		if (save)
41111c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
41121c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4113dab291afSMingming Cao 		err = jbd2_journal_load(journal);
41141c13d5c0STheodore Ts'o 		if (save)
41151c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
41161c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
41171c13d5c0STheodore Ts'o 		kfree(save);
41181c13d5c0STheodore Ts'o 	}
4119ac27a0ecSDave Kleikamp 
4120ac27a0ecSDave Kleikamp 	if (err) {
4121b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4122dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4123ac27a0ecSDave Kleikamp 		return err;
4124ac27a0ecSDave Kleikamp 	}
4125ac27a0ecSDave Kleikamp 
4126617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4127617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4128ac27a0ecSDave Kleikamp 
4129c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4130ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4131ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4132ac27a0ecSDave Kleikamp 
4133ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4134e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4135ac27a0ecSDave Kleikamp 	}
4136ac27a0ecSDave Kleikamp 
4137ac27a0ecSDave Kleikamp 	return 0;
4138ac27a0ecSDave Kleikamp }
4139ac27a0ecSDave Kleikamp 
4140e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4141ac27a0ecSDave Kleikamp {
4142e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4143617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4144c4be0c1dSTakashi Sato 	int error = 0;
4145ac27a0ecSDave Kleikamp 
41467c2e7087STheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4147c4be0c1dSTakashi Sato 		return error;
4148914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
4149914258bfSTheodore Ts'o 		/*
4150914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
4151914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
4152914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
4153914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
4154914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
4155914258bfSTheodore Ts'o 		 * write and hope for the best.
4156914258bfSTheodore Ts'o 		 */
4157b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
4158b31e1552SEric Sandeen 		       "superblock detected");
4159914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
4160914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
4161914258bfSTheodore Ts'o 	}
416271290b36STheodore Ts'o 	/*
416371290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
416471290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
416571290b36STheodore Ts'o 	 * write time when we are mounting the root file system
416671290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
416771290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
416871290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
416971290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
417071290b36STheodore Ts'o 	 * to complain and force a full file system check.
417171290b36STheodore Ts'o 	 */
417271290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4173ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4174f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4175afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4176afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4177afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4178afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4179f613dfcbSTheodore Ts'o 	else
4180f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4181f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
418257042651STheodore Ts'o 	ext4_free_blocks_count_set(es,
418357042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
418457042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
41857f93cff9STheodore Ts'o 	es->s_free_inodes_count =
41867f93cff9STheodore Ts'o 		cpu_to_le32(percpu_counter_sum_positive(
41875d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
41887234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
4189ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
4190ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4191914258bfSTheodore Ts'o 	if (sync) {
4192c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
4193c4be0c1dSTakashi Sato 		if (error)
4194c4be0c1dSTakashi Sato 			return error;
4195c4be0c1dSTakashi Sato 
4196c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4197c4be0c1dSTakashi Sato 		if (error) {
4198b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4199b31e1552SEric Sandeen 			       "superblock");
4200914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4201914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4202914258bfSTheodore Ts'o 		}
4203914258bfSTheodore Ts'o 	}
4204c4be0c1dSTakashi Sato 	return error;
4205ac27a0ecSDave Kleikamp }
4206ac27a0ecSDave Kleikamp 
4207ac27a0ecSDave Kleikamp /*
4208ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4209ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4210ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4211ac27a0ecSDave Kleikamp  */
4212617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4213617ba13bSMingming Cao 					struct ext4_super_block *es)
4214ac27a0ecSDave Kleikamp {
4215617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4216ac27a0ecSDave Kleikamp 
42170390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
42180390131bSFrank Mayhar 		BUG_ON(journal != NULL);
42190390131bSFrank Mayhar 		return;
42200390131bSFrank Mayhar 	}
4221dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
42227ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
42237ffe1ea8SHidehiro Kawai 		goto out;
42247ffe1ea8SHidehiro Kawai 
4225617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
4226ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4227617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4228e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4229ac27a0ecSDave Kleikamp 	}
42307ffe1ea8SHidehiro Kawai 
42317ffe1ea8SHidehiro Kawai out:
4232dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4233ac27a0ecSDave Kleikamp }
4234ac27a0ecSDave Kleikamp 
4235ac27a0ecSDave Kleikamp /*
4236ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4237ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4238ac27a0ecSDave Kleikamp  * main filesystem now.
4239ac27a0ecSDave Kleikamp  */
4240617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4241617ba13bSMingming Cao 				   struct ext4_super_block *es)
4242ac27a0ecSDave Kleikamp {
4243ac27a0ecSDave Kleikamp 	journal_t *journal;
4244ac27a0ecSDave Kleikamp 	int j_errno;
4245ac27a0ecSDave Kleikamp 	const char *errstr;
4246ac27a0ecSDave Kleikamp 
42470390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
42480390131bSFrank Mayhar 
4249617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4250ac27a0ecSDave Kleikamp 
4251ac27a0ecSDave Kleikamp 	/*
4252ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4253617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4254ac27a0ecSDave Kleikamp 	 */
4255ac27a0ecSDave Kleikamp 
4256dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4257ac27a0ecSDave Kleikamp 	if (j_errno) {
4258ac27a0ecSDave Kleikamp 		char nbuf[16];
4259ac27a0ecSDave Kleikamp 
4260617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
426112062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4262ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
426312062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4264ac27a0ecSDave Kleikamp 
4265617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4266617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4267e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4268ac27a0ecSDave Kleikamp 
4269dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4270ac27a0ecSDave Kleikamp 	}
4271ac27a0ecSDave Kleikamp }
4272ac27a0ecSDave Kleikamp 
4273ac27a0ecSDave Kleikamp /*
4274ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4275ac27a0ecSDave Kleikamp  * and wait on the commit.
4276ac27a0ecSDave Kleikamp  */
4277617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4278ac27a0ecSDave Kleikamp {
4279ac27a0ecSDave Kleikamp 	journal_t *journal;
42800390131bSFrank Mayhar 	int ret = 0;
4281ac27a0ecSDave Kleikamp 
4282ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4283ac27a0ecSDave Kleikamp 		return 0;
4284ac27a0ecSDave Kleikamp 
4285617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
42866b0310fbSEric Sandeen 	if (journal) {
4287437f88ccSEric Sandeen 		vfs_check_frozen(sb, SB_FREEZE_TRANS);
4288617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
42896b0310fbSEric Sandeen 	}
42900390131bSFrank Mayhar 
4291ac27a0ecSDave Kleikamp 	return ret;
4292ac27a0ecSDave Kleikamp }
4293ac27a0ecSDave Kleikamp 
4294617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
4295ac27a0ecSDave Kleikamp {
4296ebc1ac16SChristoph Hellwig 	lock_super(sb);
4297e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4298ebc1ac16SChristoph Hellwig 	unlock_super(sb);
42990390131bSFrank Mayhar }
4300ac27a0ecSDave Kleikamp 
4301617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4302ac27a0ecSDave Kleikamp {
430314ce0cb4STheodore Ts'o 	int ret = 0;
43049eddacf9SJan Kara 	tid_t target;
43058d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4306ac27a0ecSDave Kleikamp 
43079bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
43088d5d02e6SMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
43098d5d02e6SMingming Cao 	if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4310ac27a0ecSDave Kleikamp 		if (wait)
43118d5d02e6SMingming Cao 			jbd2_log_wait_commit(sbi->s_journal, target);
43120390131bSFrank Mayhar 	}
431314ce0cb4STheodore Ts'o 	return ret;
4314ac27a0ecSDave Kleikamp }
4315ac27a0ecSDave Kleikamp 
4316ac27a0ecSDave Kleikamp /*
4317ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4318ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4319be4f27d3SYongqiang Yang  *
4320be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
4321be4f27d3SYongqiang Yang  * state independently, because ext4 prevents a new handle from being started
4322be4f27d3SYongqiang Yang  * by @sb->s_frozen, which stays in an upper layer.  It thus needs help from
4323be4f27d3SYongqiang Yang  * the upper layer.
4324ac27a0ecSDave Kleikamp  */
4325c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4326ac27a0ecSDave Kleikamp {
4327c4be0c1dSTakashi Sato 	int error = 0;
4328c4be0c1dSTakashi Sato 	journal_t *journal;
4329ac27a0ecSDave Kleikamp 
43309ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
43319ca92389STheodore Ts'o 		return 0;
43329ca92389STheodore Ts'o 
4333c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4334ac27a0ecSDave Kleikamp 
4335ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
4336dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
43377ffe1ea8SHidehiro Kawai 
43387ffe1ea8SHidehiro Kawai 	/*
43399ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
43409ca92389STheodore Ts'o 	 * the journal.
43417ffe1ea8SHidehiro Kawai 	 */
4342c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
43436b0310fbSEric Sandeen 	if (error < 0)
43446b0310fbSEric Sandeen 		goto out;
4345ac27a0ecSDave Kleikamp 
4346ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
4347617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4348e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
43496b0310fbSEric Sandeen out:
43506b0310fbSEric Sandeen 	/* we rely on s_frozen to stop further updates */
43516b0310fbSEric Sandeen 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
43526b0310fbSEric Sandeen 	return error;
4353ac27a0ecSDave Kleikamp }
4354ac27a0ecSDave Kleikamp 
4355ac27a0ecSDave Kleikamp /*
4356ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4357ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4358ac27a0ecSDave Kleikamp  */
4359c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4360ac27a0ecSDave Kleikamp {
43619ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
43629ca92389STheodore Ts'o 		return 0;
43639ca92389STheodore Ts'o 
4364ac27a0ecSDave Kleikamp 	lock_super(sb);
43659ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
4366617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4367e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4368ac27a0ecSDave Kleikamp 	unlock_super(sb);
4369c4be0c1dSTakashi Sato 	return 0;
4370ac27a0ecSDave Kleikamp }
4371ac27a0ecSDave Kleikamp 
4372673c6100STheodore Ts'o /*
4373673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4374673c6100STheodore Ts'o  */
4375673c6100STheodore Ts'o struct ext4_mount_options {
4376673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4377a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
4378673c6100STheodore Ts'o 	uid_t s_resuid;
4379673c6100STheodore Ts'o 	gid_t s_resgid;
4380673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4381673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4382673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4383673c6100STheodore Ts'o 	int s_jquota_fmt;
4384673c6100STheodore Ts'o 	char *s_qf_names[MAXQUOTAS];
4385673c6100STheodore Ts'o #endif
4386673c6100STheodore Ts'o };
4387673c6100STheodore Ts'o 
4388617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4389ac27a0ecSDave Kleikamp {
4390617ba13bSMingming Cao 	struct ext4_super_block *es;
4391617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4392617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
4393ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4394617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4395c79d967dSChristoph Hellwig 	int enable_quota = 0;
43968a266467STheodore Ts'o 	ext4_group_t g;
4397b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4398c5e06d10SJohann Lombardi 	int err = 0;
4399ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4400ac27a0ecSDave Kleikamp 	int i;
4401ac27a0ecSDave Kleikamp #endif
4402d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4403ac27a0ecSDave Kleikamp 
4404ac27a0ecSDave Kleikamp 	/* Store the original options */
4405bbd6851aSAl Viro 	lock_super(sb);
4406ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4407ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4408a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4409ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4410ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4411ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
441230773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
441330773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4414ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4415ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4416ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4417ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
4418ac27a0ecSDave Kleikamp #endif
4419b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4420b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4421ac27a0ecSDave Kleikamp 
4422ac27a0ecSDave Kleikamp 	/*
4423ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
4424ac27a0ecSDave Kleikamp 	 */
4425b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
4426b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
4427ac27a0ecSDave Kleikamp 		err = -EINVAL;
4428ac27a0ecSDave Kleikamp 		goto restore_opts;
4429ac27a0ecSDave Kleikamp 	}
4430ac27a0ecSDave Kleikamp 
44314ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4432c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4433ac27a0ecSDave Kleikamp 
4434ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4435482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4436ac27a0ecSDave Kleikamp 
4437ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4438ac27a0ecSDave Kleikamp 
4439b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4440617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4441b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4442b3881f74STheodore Ts'o 	}
4443ac27a0ecSDave Kleikamp 
4444ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
4445bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
44464ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4447ac27a0ecSDave Kleikamp 			err = -EROFS;
4448ac27a0ecSDave Kleikamp 			goto restore_opts;
4449ac27a0ecSDave Kleikamp 		}
4450ac27a0ecSDave Kleikamp 
4451ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
44520f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
44530f0dd62fSChristoph Hellwig 			if (err < 0)
4454c79d967dSChristoph Hellwig 				goto restore_opts;
4455c79d967dSChristoph Hellwig 
4456ac27a0ecSDave Kleikamp 			/*
4457ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4458ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4459ac27a0ecSDave Kleikamp 			 */
4460ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4461ac27a0ecSDave Kleikamp 
4462ac27a0ecSDave Kleikamp 			/*
4463ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4464ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4465ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4466ac27a0ecSDave Kleikamp 			 */
4467617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4468617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4469ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4470ac27a0ecSDave Kleikamp 
4471a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4472617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4473ac27a0ecSDave Kleikamp 		} else {
4474a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4475a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
4476ac27a0ecSDave Kleikamp 				err = -EROFS;
4477ac27a0ecSDave Kleikamp 				goto restore_opts;
4478ac27a0ecSDave Kleikamp 			}
4479ead6596bSEric Sandeen 			/*
44808a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
44810b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
44828a266467STheodore Ts'o 			 */
44838a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
44848a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
44858a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
44868a266467STheodore Ts'o 
44878a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
4488b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4489b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
44908a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
44918a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
44928a266467STheodore Ts'o 					err = -EINVAL;
44938a266467STheodore Ts'o 					goto restore_opts;
44948a266467STheodore Ts'o 				}
44958a266467STheodore Ts'o 			}
44968a266467STheodore Ts'o 
44978a266467STheodore Ts'o 			/*
4498ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4499ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4500ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4501ead6596bSEric Sandeen 			 */
4502ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4503b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4504ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4505ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4506b31e1552SEric Sandeen 				       "umount/remount instead");
4507ead6596bSEric Sandeen 				err = -EINVAL;
4508ead6596bSEric Sandeen 				goto restore_opts;
4509ead6596bSEric Sandeen 			}
4510ead6596bSEric Sandeen 
4511ac27a0ecSDave Kleikamp 			/*
4512ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4513ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4514ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4515ac27a0ecSDave Kleikamp 			 * the partition.)
4516ac27a0ecSDave Kleikamp 			 */
45170390131bSFrank Mayhar 			if (sbi->s_journal)
4518617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4519ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4520617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
4521ac27a0ecSDave Kleikamp 				goto restore_opts;
4522617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4523ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4524c5e06d10SJohann Lombardi 			if (EXT4_HAS_INCOMPAT_FEATURE(sb,
4525c5e06d10SJohann Lombardi 						     EXT4_FEATURE_INCOMPAT_MMP))
4526c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
4527c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
4528c5e06d10SJohann Lombardi 					err = -EROFS;
4529c5e06d10SJohann Lombardi 					goto restore_opts;
4530c5e06d10SJohann Lombardi 				}
4531c79d967dSChristoph Hellwig 			enable_quota = 1;
4532ac27a0ecSDave Kleikamp 		}
4533ac27a0ecSDave Kleikamp 	}
4534bfff6873SLukas Czerner 
4535bfff6873SLukas Czerner 	/*
4536bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4537bfff6873SLukas Czerner 	 * current settings
4538bfff6873SLukas Czerner 	 */
4539bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4540bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4541bfff6873SLukas Czerner 	else {
4542bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4543bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4544bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4545bfff6873SLukas Czerner 	}
4546bfff6873SLukas Czerner 
45476fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
45480390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
4549e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
45500390131bSFrank Mayhar 
4551ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4552ac27a0ecSDave Kleikamp 	/* Release old quota file names */
4553ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4554ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
4555ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4556ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
4557ac27a0ecSDave Kleikamp #endif
4558bbd6851aSAl Viro 	unlock_super(sb);
4559c79d967dSChristoph Hellwig 	if (enable_quota)
45600f0dd62fSChristoph Hellwig 		dquot_resume(sb, -1);
4561d4c402d9SCurt Wohlgemuth 
4562d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4563d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4564ac27a0ecSDave Kleikamp 	return 0;
45650b8e58a1SAndreas Dilger 
4566ac27a0ecSDave Kleikamp restore_opts:
4567ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
4568ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
4569a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
4570ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
4571ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
4572ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
457330773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
457430773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
4575ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4576ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
4577ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
4578ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
4579ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4580ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
4581ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
4582ac27a0ecSDave Kleikamp 	}
4583ac27a0ecSDave Kleikamp #endif
4584bbd6851aSAl Viro 	unlock_super(sb);
4585d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4586ac27a0ecSDave Kleikamp 	return err;
4587ac27a0ecSDave Kleikamp }
4588ac27a0ecSDave Kleikamp 
4589f975d6bcSTheodore Ts'o /*
4590f975d6bcSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
4591f975d6bcSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
4592f975d6bcSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
4593f975d6bcSTheodore Ts'o  * different block group can end up in the same allocation cluster.
4594f975d6bcSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
4595f975d6bcSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
4596f975d6bcSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
4597f975d6bcSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
4598f975d6bcSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
4599f975d6bcSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
4600f975d6bcSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
4601f975d6bcSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
4602f975d6bcSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
4603f975d6bcSTheodore Ts'o  */
4604617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
4605ac27a0ecSDave Kleikamp {
4606ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
4607617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4608617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
4609f975d6bcSTheodore Ts'o 	struct ext4_group_desc *gdp;
4610960cc398SPekka Enberg 	u64 fsid;
4611d02a9391SKazuya Mio 	s64 bfree;
4612ac27a0ecSDave Kleikamp 
46135e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
46145e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
4615f975d6bcSTheodore Ts'o 	} else if (es->s_overhead_clusters) {
4616f975d6bcSTheodore Ts'o 		sbi->s_overhead_last = le32_to_cpu(es->s_overhead_clusters);
46176bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
46188df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
46195e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
4620ac27a0ecSDave Kleikamp 
4621ac27a0ecSDave Kleikamp 		/*
46225e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
46235e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
46245e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
4625ac27a0ecSDave Kleikamp 		 */
4626ac27a0ecSDave Kleikamp 
4627ac27a0ecSDave Kleikamp 		/*
4628ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
4629ac27a0ecSDave Kleikamp 		 * overhead
4630ac27a0ecSDave Kleikamp 		 */
4631f975d6bcSTheodore Ts'o 		overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
4632ac27a0ecSDave Kleikamp 
4633ac27a0ecSDave Kleikamp 		/*
4634f975d6bcSTheodore Ts'o 		 * Add the overhead found in each block group
4635ac27a0ecSDave Kleikamp 		 */
4636ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
4637f975d6bcSTheodore Ts'o 			gdp = ext4_get_group_desc(sb, i, NULL);
4638f975d6bcSTheodore Ts'o 			overhead += ext4_num_overhead_clusters(sb, i, gdp);
4639ac27a0ecSDave Kleikamp 			cond_resched();
4640ac27a0ecSDave Kleikamp 		}
46415e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
46425e70030dSBadari Pulavarty 		smp_wmb();
46436bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
4644ac27a0ecSDave Kleikamp 	}
4645ac27a0ecSDave Kleikamp 
4646617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
4647ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
4648f975d6bcSTheodore Ts'o 	buf->f_blocks = (ext4_blocks_count(es) -
4649f975d6bcSTheodore Ts'o 			 EXT4_C2B(sbi, sbi->s_overhead_last));
465057042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
465157042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
4652d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
465357042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
4654bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
4655bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
4656ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
4657ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
465852d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
4659617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
4660960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
4661960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
4662960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
4663960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
46640b8e58a1SAndreas Dilger 
4665ac27a0ecSDave Kleikamp 	return 0;
4666ac27a0ecSDave Kleikamp }
4667ac27a0ecSDave Kleikamp 
46680b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
46690b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
4670ac27a0ecSDave Kleikamp  * Process 1                         Process 2
4671617ba13bSMingming Cao  * ext4_create()                     quota_sync()
4672dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
4673871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
4674dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
4675ac27a0ecSDave Kleikamp  *
4676ac27a0ecSDave Kleikamp  */
4677ac27a0ecSDave Kleikamp 
4678ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4679ac27a0ecSDave Kleikamp 
4680ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
4681ac27a0ecSDave Kleikamp {
4682ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
4683ac27a0ecSDave Kleikamp }
4684ac27a0ecSDave Kleikamp 
4685617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
4686ac27a0ecSDave Kleikamp {
4687ac27a0ecSDave Kleikamp 	int ret, err;
4688ac27a0ecSDave Kleikamp 	handle_t *handle;
4689ac27a0ecSDave Kleikamp 	struct inode *inode;
4690ac27a0ecSDave Kleikamp 
4691ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
4692617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
4693617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
4694ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4695ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4696ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
4697617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4698ac27a0ecSDave Kleikamp 	if (!ret)
4699ac27a0ecSDave Kleikamp 		ret = err;
4700ac27a0ecSDave Kleikamp 	return ret;
4701ac27a0ecSDave Kleikamp }
4702ac27a0ecSDave Kleikamp 
4703617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
4704ac27a0ecSDave Kleikamp {
4705ac27a0ecSDave Kleikamp 	int ret, err;
4706ac27a0ecSDave Kleikamp 	handle_t *handle;
4707ac27a0ecSDave Kleikamp 
4708617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4709617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
4710ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4711ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4712ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
4713617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4714ac27a0ecSDave Kleikamp 	if (!ret)
4715ac27a0ecSDave Kleikamp 		ret = err;
4716ac27a0ecSDave Kleikamp 	return ret;
4717ac27a0ecSDave Kleikamp }
4718ac27a0ecSDave Kleikamp 
4719617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
4720ac27a0ecSDave Kleikamp {
4721ac27a0ecSDave Kleikamp 	int ret, err;
4722ac27a0ecSDave Kleikamp 	handle_t *handle;
4723ac27a0ecSDave Kleikamp 
4724617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4725617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
47269c3013e9SJan Kara 	if (IS_ERR(handle)) {
47279c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
47289c3013e9SJan Kara 		dquot_release(dquot);
4729ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
47309c3013e9SJan Kara 	}
4731ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
4732617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4733ac27a0ecSDave Kleikamp 	if (!ret)
4734ac27a0ecSDave Kleikamp 		ret = err;
4735ac27a0ecSDave Kleikamp 	return ret;
4736ac27a0ecSDave Kleikamp }
4737ac27a0ecSDave Kleikamp 
4738617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
4739ac27a0ecSDave Kleikamp {
47402c8be6b2SJan Kara 	/* Are we journaling quotas? */
4741617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
4742617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
4743ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
4744617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
4745ac27a0ecSDave Kleikamp 	} else {
4746ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
4747ac27a0ecSDave Kleikamp 	}
4748ac27a0ecSDave Kleikamp }
4749ac27a0ecSDave Kleikamp 
4750617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
4751ac27a0ecSDave Kleikamp {
4752ac27a0ecSDave Kleikamp 	int ret, err;
4753ac27a0ecSDave Kleikamp 	handle_t *handle;
4754ac27a0ecSDave Kleikamp 
4755ac27a0ecSDave Kleikamp 	/* Data block + inode block */
4756617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
4757ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4758ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4759ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
4760617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4761ac27a0ecSDave Kleikamp 	if (!ret)
4762ac27a0ecSDave Kleikamp 		ret = err;
4763ac27a0ecSDave Kleikamp 	return ret;
4764ac27a0ecSDave Kleikamp }
4765ac27a0ecSDave Kleikamp 
4766ac27a0ecSDave Kleikamp /*
4767ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
4768ac27a0ecSDave Kleikamp  * the quota file and such...
4769ac27a0ecSDave Kleikamp  */
4770617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
4771ac27a0ecSDave Kleikamp {
4772287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
4773617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
4774ac27a0ecSDave Kleikamp }
4775ac27a0ecSDave Kleikamp 
4776ac27a0ecSDave Kleikamp /*
4777ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
4778ac27a0ecSDave Kleikamp  */
4779617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
4780f00c9e44SJan Kara 			 struct path *path)
4781ac27a0ecSDave Kleikamp {
4782ac27a0ecSDave Kleikamp 	int err;
4783ac27a0ecSDave Kleikamp 
4784ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
4785ac27a0ecSDave Kleikamp 		return -EINVAL;
47860623543bSJan Kara 
4787ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
4788f00c9e44SJan Kara 	if (path->mnt->mnt_sb != sb)
4789ac27a0ecSDave Kleikamp 		return -EXDEV;
47900623543bSJan Kara 	/* Journaling quota? */
47910623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
47922b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
4793f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
4794b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
4795b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
4796b31e1552SEric Sandeen 				"Journaled quota will not work");
47970623543bSJan Kara 	}
47980623543bSJan Kara 
47990623543bSJan Kara 	/*
48000623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
48010623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
48020623543bSJan Kara 	 */
48030390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
4804f00c9e44SJan Kara 	    ext4_should_journal_data(path->dentry->d_inode)) {
48050623543bSJan Kara 		/*
48060623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
48070623543bSJan Kara 		 * otherwise be livelocked...
48080623543bSJan Kara 		 */
48090623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
48107ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
48110623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
4812f00c9e44SJan Kara 		if (err)
48137ffe1ea8SHidehiro Kawai 			return err;
48147ffe1ea8SHidehiro Kawai 	}
48150623543bSJan Kara 
4816f00c9e44SJan Kara 	return dquot_quota_on(sb, type, format_id, path);
4817ac27a0ecSDave Kleikamp }
4818ac27a0ecSDave Kleikamp 
4819ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
4820ca0e05e4SDmitry Monakhov {
482121f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
482221f97697SJan Kara 	handle_t *handle;
482321f97697SJan Kara 
482487009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
482587009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
482687009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
4827ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
4828ca0e05e4SDmitry Monakhov 
48290b268590SAmir Goldstein 	if (!inode)
48300b268590SAmir Goldstein 		goto out;
48310b268590SAmir Goldstein 
483221f97697SJan Kara 	/* Update modification times of quota files when userspace can
483321f97697SJan Kara 	 * start looking at them */
483421f97697SJan Kara 	handle = ext4_journal_start(inode, 1);
483521f97697SJan Kara 	if (IS_ERR(handle))
483621f97697SJan Kara 		goto out;
483721f97697SJan Kara 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
483821f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
483921f97697SJan Kara 	ext4_journal_stop(handle);
484021f97697SJan Kara 
484121f97697SJan Kara out:
4842ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
4843ca0e05e4SDmitry Monakhov }
4844ca0e05e4SDmitry Monakhov 
4845ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
4846ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
4847ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
4848ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
4849617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
4850ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
4851ac27a0ecSDave Kleikamp {
4852ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
4853725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4854ac27a0ecSDave Kleikamp 	int err = 0;
4855ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
4856ac27a0ecSDave Kleikamp 	int tocopy;
4857ac27a0ecSDave Kleikamp 	size_t toread;
4858ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4859ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
4860ac27a0ecSDave Kleikamp 
4861ac27a0ecSDave Kleikamp 	if (off > i_size)
4862ac27a0ecSDave Kleikamp 		return 0;
4863ac27a0ecSDave Kleikamp 	if (off+len > i_size)
4864ac27a0ecSDave Kleikamp 		len = i_size-off;
4865ac27a0ecSDave Kleikamp 	toread = len;
4866ac27a0ecSDave Kleikamp 	while (toread > 0) {
4867ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
4868ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
4869617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
4870ac27a0ecSDave Kleikamp 		if (err)
4871ac27a0ecSDave Kleikamp 			return err;
4872ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
4873ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
4874ac27a0ecSDave Kleikamp 		else
4875ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
4876ac27a0ecSDave Kleikamp 		brelse(bh);
4877ac27a0ecSDave Kleikamp 		offset = 0;
4878ac27a0ecSDave Kleikamp 		toread -= tocopy;
4879ac27a0ecSDave Kleikamp 		data += tocopy;
4880ac27a0ecSDave Kleikamp 		blk++;
4881ac27a0ecSDave Kleikamp 	}
4882ac27a0ecSDave Kleikamp 	return len;
4883ac27a0ecSDave Kleikamp }
4884ac27a0ecSDave Kleikamp 
4885ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
4886ac27a0ecSDave Kleikamp  * enough credits) */
4887617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
4888ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
4889ac27a0ecSDave Kleikamp {
4890ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
4891725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4892ac27a0ecSDave Kleikamp 	int err = 0;
4893ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
4894ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4895ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
4896ac27a0ecSDave Kleikamp 
48970390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
4898b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4899b31e1552SEric Sandeen 			" cancelled because transaction is not started",
49009c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
49019c3013e9SJan Kara 		return -EIO;
49029c3013e9SJan Kara 	}
490367eeb568SDmitry Monakhov 	/*
490467eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
490567eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
490667eeb568SDmitry Monakhov 	 */
490767eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
490867eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
490967eeb568SDmitry Monakhov 			" cancelled because not block aligned",
491067eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
491167eeb568SDmitry Monakhov 		return -EIO;
491267eeb568SDmitry Monakhov 	}
491367eeb568SDmitry Monakhov 
4914ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
4915617ba13bSMingming Cao 	bh = ext4_bread(handle, inode, blk, 1, &err);
4916ac27a0ecSDave Kleikamp 	if (!bh)
4917ac27a0ecSDave Kleikamp 		goto out;
4918617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
4919ac27a0ecSDave Kleikamp 	if (err) {
4920ac27a0ecSDave Kleikamp 		brelse(bh);
4921ac27a0ecSDave Kleikamp 		goto out;
4922ac27a0ecSDave Kleikamp 	}
4923ac27a0ecSDave Kleikamp 	lock_buffer(bh);
492467eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
4925ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
4926ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
49270390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
4928ac27a0ecSDave Kleikamp 	brelse(bh);
4929ac27a0ecSDave Kleikamp out:
493067eeb568SDmitry Monakhov 	if (err) {
49314d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
4932ac27a0ecSDave Kleikamp 		return err;
49334d04e4fbSJan Kara 	}
493467eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
493567eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
4936617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
4937617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
493821f97697SJan Kara 	}
4939ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
494067eeb568SDmitry Monakhov 	return len;
4941ac27a0ecSDave Kleikamp }
4942ac27a0ecSDave Kleikamp 
4943ac27a0ecSDave Kleikamp #endif
4944ac27a0ecSDave Kleikamp 
4945152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
4946152a0836SAl Viro 		       const char *dev_name, void *data)
4947ac27a0ecSDave Kleikamp {
4948152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
4949ac27a0ecSDave Kleikamp }
4950ac27a0ecSDave Kleikamp 
495137f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
495224b58424STheodore Ts'o static inline void register_as_ext2(void)
495324b58424STheodore Ts'o {
495424b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
495524b58424STheodore Ts'o 	if (err)
495624b58424STheodore Ts'o 		printk(KERN_WARNING
495724b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
495824b58424STheodore Ts'o }
495924b58424STheodore Ts'o 
496024b58424STheodore Ts'o static inline void unregister_as_ext2(void)
496124b58424STheodore Ts'o {
496224b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
496324b58424STheodore Ts'o }
49642035e776STheodore Ts'o 
49652035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
49662035e776STheodore Ts'o {
49672035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
49682035e776STheodore Ts'o 		return 0;
49692035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
49702035e776STheodore Ts'o 		return 1;
49712035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
49722035e776STheodore Ts'o 		return 0;
49732035e776STheodore Ts'o 	return 1;
49742035e776STheodore Ts'o }
497551b7e3c9STheodore Ts'o MODULE_ALIAS("ext2");
497624b58424STheodore Ts'o #else
497724b58424STheodore Ts'o static inline void register_as_ext2(void) { }
497824b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
49792035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
498024b58424STheodore Ts'o #endif
498124b58424STheodore Ts'o 
498237f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
498324b58424STheodore Ts'o static inline void register_as_ext3(void)
498424b58424STheodore Ts'o {
498524b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
498624b58424STheodore Ts'o 	if (err)
498724b58424STheodore Ts'o 		printk(KERN_WARNING
498824b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
498924b58424STheodore Ts'o }
499024b58424STheodore Ts'o 
499124b58424STheodore Ts'o static inline void unregister_as_ext3(void)
499224b58424STheodore Ts'o {
499324b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
499424b58424STheodore Ts'o }
49952035e776STheodore Ts'o 
49962035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
49972035e776STheodore Ts'o {
49982035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
49992035e776STheodore Ts'o 		return 0;
50002035e776STheodore Ts'o 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
50012035e776STheodore Ts'o 		return 0;
50022035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
50032035e776STheodore Ts'o 		return 1;
50042035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
50052035e776STheodore Ts'o 		return 0;
50062035e776STheodore Ts'o 	return 1;
50072035e776STheodore Ts'o }
500851b7e3c9STheodore Ts'o MODULE_ALIAS("ext3");
500924b58424STheodore Ts'o #else
501024b58424STheodore Ts'o static inline void register_as_ext3(void) { }
501124b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
50122035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
501324b58424STheodore Ts'o #endif
501424b58424STheodore Ts'o 
501503010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
5016ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
501703010a33STheodore Ts'o 	.name		= "ext4",
5018152a0836SAl Viro 	.mount		= ext4_mount,
5019ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
5020ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
5021ac27a0ecSDave Kleikamp };
5022ac27a0ecSDave Kleikamp 
50238f021222SLukas Czerner static int __init ext4_init_feat_adverts(void)
5024857ac889SLukas Czerner {
5025857ac889SLukas Czerner 	struct ext4_features *ef;
5026857ac889SLukas Czerner 	int ret = -ENOMEM;
5027857ac889SLukas Czerner 
5028857ac889SLukas Czerner 	ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
5029857ac889SLukas Czerner 	if (!ef)
5030857ac889SLukas Czerner 		goto out;
5031857ac889SLukas Czerner 
5032857ac889SLukas Czerner 	ef->f_kobj.kset = ext4_kset;
5033857ac889SLukas Czerner 	init_completion(&ef->f_kobj_unregister);
5034857ac889SLukas Czerner 	ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
5035857ac889SLukas Czerner 				   "features");
5036857ac889SLukas Czerner 	if (ret) {
5037857ac889SLukas Czerner 		kfree(ef);
5038857ac889SLukas Czerner 		goto out;
5039857ac889SLukas Czerner 	}
5040857ac889SLukas Czerner 
5041857ac889SLukas Czerner 	ext4_feat = ef;
5042857ac889SLukas Czerner 	ret = 0;
5043857ac889SLukas Czerner out:
5044857ac889SLukas Czerner 	return ret;
5045857ac889SLukas Czerner }
5046857ac889SLukas Czerner 
50478f021222SLukas Czerner static void ext4_exit_feat_adverts(void)
50488f021222SLukas Czerner {
50498f021222SLukas Czerner 	kobject_put(&ext4_feat->f_kobj);
50508f021222SLukas Czerner 	wait_for_completion(&ext4_feat->f_kobj_unregister);
50518f021222SLukas Czerner 	kfree(ext4_feat);
50528f021222SLukas Czerner }
50538f021222SLukas Czerner 
5054e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
5055e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5056e9e3bcecSEric Sandeen struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
5057e9e3bcecSEric Sandeen 
50585dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
5059ac27a0ecSDave Kleikamp {
5060e9e3bcecSEric Sandeen 	int i, err;
5061c9de560dSAlex Tomas 
506212e9b892SDmitry Monakhov 	ext4_check_flag_values();
5063e9e3bcecSEric Sandeen 
5064e9e3bcecSEric Sandeen 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
5065e9e3bcecSEric Sandeen 		mutex_init(&ext4__aio_mutex[i]);
5066e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
5067e9e3bcecSEric Sandeen 	}
5068e9e3bcecSEric Sandeen 
50695dabfc78STheodore Ts'o 	err = ext4_init_pageio();
50706fd058f7STheodore Ts'o 	if (err)
50716fd058f7STheodore Ts'o 		return err;
50725dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
5073bd2d0210STheodore Ts'o 	if (err)
5074d44651d0SFabrice Jouhaud 		goto out6;
50753197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
50763197ebdbSTheodore Ts'o 	if (!ext4_kset)
5077dd68314cSTheodore Ts'o 		goto out5;
5078d44651d0SFabrice Jouhaud 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
5079857ac889SLukas Czerner 
5080857ac889SLukas Czerner 	err = ext4_init_feat_adverts();
5081dd68314cSTheodore Ts'o 	if (err)
5082dd68314cSTheodore Ts'o 		goto out4;
5083857ac889SLukas Czerner 
50845dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
5085ac27a0ecSDave Kleikamp 	if (err)
50866fd058f7STheodore Ts'o 		goto out3;
5087c9de560dSAlex Tomas 
50885dabfc78STheodore Ts'o 	err = ext4_init_xattr();
5089c9de560dSAlex Tomas 	if (err)
5090c9de560dSAlex Tomas 		goto out2;
5091ac27a0ecSDave Kleikamp 	err = init_inodecache();
5092ac27a0ecSDave Kleikamp 	if (err)
5093ac27a0ecSDave Kleikamp 		goto out1;
509424b58424STheodore Ts'o 	register_as_ext3();
50952035e776STheodore Ts'o 	register_as_ext2();
509603010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
5097ac27a0ecSDave Kleikamp 	if (err)
5098ac27a0ecSDave Kleikamp 		goto out;
5099bfff6873SLukas Czerner 
5100bfff6873SLukas Czerner 	ext4_li_info = NULL;
5101bfff6873SLukas Czerner 	mutex_init(&ext4_li_mtx);
5102ac27a0ecSDave Kleikamp 	return 0;
5103ac27a0ecSDave Kleikamp out:
510424b58424STheodore Ts'o 	unregister_as_ext2();
510524b58424STheodore Ts'o 	unregister_as_ext3();
5106ac27a0ecSDave Kleikamp 	destroy_inodecache();
5107ac27a0ecSDave Kleikamp out1:
51085dabfc78STheodore Ts'o 	ext4_exit_xattr();
5109c9de560dSAlex Tomas out2:
51105dabfc78STheodore Ts'o 	ext4_exit_mballoc();
51116fd058f7STheodore Ts'o out3:
51128f021222SLukas Czerner 	ext4_exit_feat_adverts();
51136fd058f7STheodore Ts'o out4:
5114d44651d0SFabrice Jouhaud 	if (ext4_proc_root)
5115dd68314cSTheodore Ts'o 		remove_proc_entry("fs/ext4", NULL);
5116dd68314cSTheodore Ts'o 	kset_unregister(ext4_kset);
5117d44651d0SFabrice Jouhaud out5:
5118dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5119d44651d0SFabrice Jouhaud out6:
51205dabfc78STheodore Ts'o 	ext4_exit_pageio();
5121ac27a0ecSDave Kleikamp 	return err;
5122ac27a0ecSDave Kleikamp }
5123ac27a0ecSDave Kleikamp 
51245dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5125ac27a0ecSDave Kleikamp {
5126bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
512724b58424STheodore Ts'o 	unregister_as_ext2();
512824b58424STheodore Ts'o 	unregister_as_ext3();
512903010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5130ac27a0ecSDave Kleikamp 	destroy_inodecache();
51315dabfc78STheodore Ts'o 	ext4_exit_xattr();
51325dabfc78STheodore Ts'o 	ext4_exit_mballoc();
51338f021222SLukas Czerner 	ext4_exit_feat_adverts();
51349f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
51353197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
51365dabfc78STheodore Ts'o 	ext4_exit_system_zone();
51375dabfc78STheodore Ts'o 	ext4_exit_pageio();
5138ac27a0ecSDave Kleikamp }
5139ac27a0ecSDave Kleikamp 
5140ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
514183982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5142ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
51435dabfc78STheodore Ts'o module_init(ext4_init_fs)
51445dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5145