xref: /linux/fs/ext4/super.c (revision 8f82f840ec6ab873f520364d443ff6fa1b3f8e22)
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"
483dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
49ac27a0ecSDave Kleikamp #include "xattr.h"
50ac27a0ecSDave Kleikamp #include "acl.h"
513661d286STheodore Ts'o #include "mballoc.h"
52ac27a0ecSDave Kleikamp 
539bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
549bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
559bffad1eSTheodore Ts'o 
561f109d5aSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root;
573197ebdbSTheodore Ts'o static struct kset *ext4_kset;
580b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
590b75a840SLukas Czerner static struct mutex ext4_li_mtx;
600b75a840SLukas Czerner static struct ext4_features *ext4_feat;
619f6200bbSTheodore Ts'o 
62617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
63ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
64e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
65617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
66617ba13bSMingming Cao 					struct ext4_super_block *es);
67617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
68617ba13bSMingming Cao 				   struct ext4_super_block *es);
69617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
70617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
71ac27a0ecSDave Kleikamp 				     char nbuf[16]);
72617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
73617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
74c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
75617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
76c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
77152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
78152a0836SAl Viro 		       const char *dev_name, void *data);
792035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
802035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
81d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
82bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
83bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
848f1f7453SEric Sandeen static void ext4_clear_request_list(void);
85ac27a0ecSDave Kleikamp 
862035e776STheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
872035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
882035e776STheodore Ts'o 	.owner		= THIS_MODULE,
892035e776STheodore Ts'o 	.name		= "ext2",
902035e776STheodore Ts'o 	.mount		= ext4_mount,
912035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
922035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
932035e776STheodore Ts'o };
942035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
952035e776STheodore Ts'o #else
962035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
972035e776STheodore Ts'o #endif
982035e776STheodore Ts'o 
992035e776STheodore Ts'o 
100ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
101ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
102ba69f9abSJan Kara 	.owner		= THIS_MODULE,
103ba69f9abSJan Kara 	.name		= "ext3",
104152a0836SAl Viro 	.mount		= ext4_mount,
105ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
106ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
107ba69f9abSJan Kara };
108ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
109ba69f9abSJan Kara #else
110ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0)
111ba69f9abSJan Kara #endif
112bd81d8eeSLaurent Vivier 
1138fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
1148fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
115bd81d8eeSLaurent Vivier {
1163a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
1178fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1188fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
119bd81d8eeSLaurent Vivier }
120bd81d8eeSLaurent Vivier 
1218fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
1228fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
123bd81d8eeSLaurent Vivier {
1245272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
1258fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1268fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
127bd81d8eeSLaurent Vivier }
128bd81d8eeSLaurent Vivier 
1298fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
1308fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
131bd81d8eeSLaurent Vivier {
1325272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
1338fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1348fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
135bd81d8eeSLaurent Vivier }
136bd81d8eeSLaurent Vivier 
137560671a0SAneesh Kumar K.V __u32 ext4_free_blks_count(struct super_block *sb,
138560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
139560671a0SAneesh Kumar K.V {
140560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
141560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
142560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
143560671a0SAneesh Kumar K.V }
144560671a0SAneesh Kumar K.V 
145560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
146560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
147560671a0SAneesh Kumar K.V {
148560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
149560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
150560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
151560671a0SAneesh Kumar K.V }
152560671a0SAneesh Kumar K.V 
153560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
154560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
155560671a0SAneesh Kumar K.V {
156560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
157560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
158560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
159560671a0SAneesh Kumar K.V }
160560671a0SAneesh Kumar K.V 
161560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
162560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
163560671a0SAneesh Kumar K.V {
164560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
165560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
166560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
167560671a0SAneesh Kumar K.V }
168560671a0SAneesh Kumar K.V 
1698fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
1708fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
171bd81d8eeSLaurent Vivier {
1723a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1738fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1748fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
175bd81d8eeSLaurent Vivier }
176bd81d8eeSLaurent Vivier 
1778fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1788fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
179bd81d8eeSLaurent Vivier {
1805272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1818fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1828fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
183bd81d8eeSLaurent Vivier }
184bd81d8eeSLaurent Vivier 
1858fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1868fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
187bd81d8eeSLaurent Vivier {
1885272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1898fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1908fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
191bd81d8eeSLaurent Vivier }
192bd81d8eeSLaurent Vivier 
193560671a0SAneesh Kumar K.V void ext4_free_blks_set(struct super_block *sb,
194560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
195560671a0SAneesh Kumar K.V {
196560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
197560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
198560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
199560671a0SAneesh Kumar K.V }
200560671a0SAneesh Kumar K.V 
201560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
202560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
203560671a0SAneesh Kumar K.V {
204560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
205560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
206560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
207560671a0SAneesh Kumar K.V }
208560671a0SAneesh Kumar K.V 
209560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
210560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
211560671a0SAneesh Kumar K.V {
212560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
213560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
214560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
215560671a0SAneesh Kumar K.V }
216560671a0SAneesh Kumar K.V 
217560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
218560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
219560671a0SAneesh Kumar K.V {
220560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
221560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
222560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
223560671a0SAneesh Kumar K.V }
224560671a0SAneesh Kumar K.V 
225d3d1faf6SCurt Wohlgemuth 
226d3d1faf6SCurt Wohlgemuth /* Just increment the non-pointer handle value */
227d3d1faf6SCurt Wohlgemuth static handle_t *ext4_get_nojournal(void)
228d3d1faf6SCurt Wohlgemuth {
229d3d1faf6SCurt Wohlgemuth 	handle_t *handle = current->journal_info;
230d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
231d3d1faf6SCurt Wohlgemuth 
232d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
233d3d1faf6SCurt Wohlgemuth 
234d3d1faf6SCurt Wohlgemuth 	ref_cnt++;
235d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
236d3d1faf6SCurt Wohlgemuth 
237d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
238d3d1faf6SCurt Wohlgemuth 	return handle;
239d3d1faf6SCurt Wohlgemuth }
240d3d1faf6SCurt Wohlgemuth 
241d3d1faf6SCurt Wohlgemuth 
242d3d1faf6SCurt Wohlgemuth /* Decrement the non-pointer handle value */
243d3d1faf6SCurt Wohlgemuth static void ext4_put_nojournal(handle_t *handle)
244d3d1faf6SCurt Wohlgemuth {
245d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
246d3d1faf6SCurt Wohlgemuth 
247d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt == 0);
248d3d1faf6SCurt Wohlgemuth 
249d3d1faf6SCurt Wohlgemuth 	ref_cnt--;
250d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
251d3d1faf6SCurt Wohlgemuth 
252d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
253d3d1faf6SCurt Wohlgemuth }
254d3d1faf6SCurt Wohlgemuth 
255ac27a0ecSDave Kleikamp /*
256dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
257ac27a0ecSDave Kleikamp  *
258ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
259ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
260ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
261ac27a0ecSDave Kleikamp  * appropriate.
262be4f27d3SYongqiang Yang  *
263be4f27d3SYongqiang Yang  * To avoid j_barrier hold in userspace when a user calls freeze(),
264be4f27d3SYongqiang Yang  * ext4 prevents a new handle from being started by s_frozen, which
265be4f27d3SYongqiang Yang  * is in an upper layer.
266ac27a0ecSDave Kleikamp  */
267617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
268ac27a0ecSDave Kleikamp {
269ac27a0ecSDave Kleikamp 	journal_t *journal;
270be4f27d3SYongqiang Yang 	handle_t  *handle;
271ac27a0ecSDave Kleikamp 
27212706394STheodore Ts'o 	trace_ext4_journal_start(sb, nblocks, _RET_IP_);
273ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
274ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
275ac27a0ecSDave Kleikamp 
276617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
277be4f27d3SYongqiang Yang 	handle = ext4_journal_current_handle();
278be4f27d3SYongqiang Yang 
279be4f27d3SYongqiang Yang 	/*
280be4f27d3SYongqiang Yang 	 * If a handle has been started, it should be allowed to
281be4f27d3SYongqiang Yang 	 * finish, otherwise deadlock could happen between freeze
282be4f27d3SYongqiang Yang 	 * and others(e.g. truncate) due to the restart of the
283be4f27d3SYongqiang Yang 	 * journal handle if the filesystem is forzen and active
284be4f27d3SYongqiang Yang 	 * handles are not stopped.
285be4f27d3SYongqiang Yang 	 */
286be4f27d3SYongqiang Yang 	if (!handle)
287be4f27d3SYongqiang Yang 		vfs_check_frozen(sb, SB_FREEZE_TRANS);
288be4f27d3SYongqiang Yang 
289be4f27d3SYongqiang Yang 	if (!journal)
290be4f27d3SYongqiang Yang 		return ext4_get_nojournal();
291be4f27d3SYongqiang Yang 	/*
292be4f27d3SYongqiang Yang 	 * Special case here: if the journal has aborted behind our
293be4f27d3SYongqiang Yang 	 * backs (eg. EIO in the commit thread), then we still need to
294be4f27d3SYongqiang Yang 	 * take the FS itself readonly cleanly.
295be4f27d3SYongqiang Yang 	 */
296ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
297c67d859eSTheodore Ts'o 		ext4_abort(sb, "Detected aborted journal");
298ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
299ac27a0ecSDave Kleikamp 	}
300dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
301ac27a0ecSDave Kleikamp }
302ac27a0ecSDave Kleikamp 
303ac27a0ecSDave Kleikamp /*
304ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
305dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
306ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
307ac27a0ecSDave Kleikamp  * appropriate.
308ac27a0ecSDave Kleikamp  */
309c398eda0STheodore Ts'o int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
310ac27a0ecSDave Kleikamp {
311ac27a0ecSDave Kleikamp 	struct super_block *sb;
312ac27a0ecSDave Kleikamp 	int err;
313ac27a0ecSDave Kleikamp 	int rc;
314ac27a0ecSDave Kleikamp 
3150390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
316d3d1faf6SCurt Wohlgemuth 		ext4_put_nojournal(handle);
3170390131bSFrank Mayhar 		return 0;
3180390131bSFrank Mayhar 	}
319ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
320ac27a0ecSDave Kleikamp 	err = handle->h_err;
321dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
322ac27a0ecSDave Kleikamp 
323ac27a0ecSDave Kleikamp 	if (!err)
324ac27a0ecSDave Kleikamp 		err = rc;
325ac27a0ecSDave Kleikamp 	if (err)
326c398eda0STheodore Ts'o 		__ext4_std_error(sb, where, line, err);
327ac27a0ecSDave Kleikamp 	return err;
328ac27a0ecSDave Kleikamp }
329ac27a0ecSDave Kleikamp 
33090c7201bSTheodore Ts'o void ext4_journal_abort_handle(const char *caller, unsigned int line,
33190c7201bSTheodore Ts'o 			       const char *err_fn, struct buffer_head *bh,
33290c7201bSTheodore Ts'o 			       handle_t *handle, int err)
333ac27a0ecSDave Kleikamp {
334ac27a0ecSDave Kleikamp 	char nbuf[16];
335617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
336ac27a0ecSDave Kleikamp 
3370390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
3380390131bSFrank Mayhar 
339ac27a0ecSDave Kleikamp 	if (bh)
340ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
341ac27a0ecSDave Kleikamp 
342ac27a0ecSDave Kleikamp 	if (!handle->h_err)
343ac27a0ecSDave Kleikamp 		handle->h_err = err;
344ac27a0ecSDave Kleikamp 
345ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
346ac27a0ecSDave Kleikamp 		return;
347ac27a0ecSDave Kleikamp 
34890c7201bSTheodore Ts'o 	printk(KERN_ERR "%s:%d: aborting transaction: %s in %s\n",
34990c7201bSTheodore Ts'o 	       caller, line, errstr, err_fn);
350ac27a0ecSDave Kleikamp 
351dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
352ac27a0ecSDave Kleikamp }
353ac27a0ecSDave Kleikamp 
3541c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3551c13d5c0STheodore Ts'o 			    unsigned int line)
3561c13d5c0STheodore Ts'o {
3571c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3581c13d5c0STheodore Ts'o 
3591c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3601c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3611c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
3621c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3631c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3641c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3651c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3661c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3671c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3681c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3691c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3701c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3711c13d5c0STheodore Ts'o 	}
37266e61a9eSTheodore Ts'o 	/*
37366e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
37466e61a9eSTheodore Ts'o 	 * started already
37566e61a9eSTheodore Ts'o 	 */
37666e61a9eSTheodore Ts'o 	if (!es->s_error_count)
37766e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
3781c13d5c0STheodore Ts'o 	es->s_error_count = cpu_to_le32(le32_to_cpu(es->s_error_count) + 1);
3791c13d5c0STheodore Ts'o }
3801c13d5c0STheodore Ts'o 
3811c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3821c13d5c0STheodore Ts'o 			    unsigned int line)
3831c13d5c0STheodore Ts'o {
3841c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3851c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3861c13d5c0STheodore Ts'o }
3871c13d5c0STheodore Ts'o 
3881c13d5c0STheodore Ts'o 
389ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
390ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
391ac27a0ecSDave Kleikamp  *
392ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
393617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
394ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
395ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
396ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
397ac27a0ecSDave Kleikamp  * write out the superblock safely.
398ac27a0ecSDave Kleikamp  *
399dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
400d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
401ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
402ac27a0ecSDave Kleikamp  */
403ac27a0ecSDave Kleikamp 
404617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
405ac27a0ecSDave Kleikamp {
406ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
407ac27a0ecSDave Kleikamp 		return;
408ac27a0ecSDave Kleikamp 
409ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
410617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
411ac27a0ecSDave Kleikamp 
4124ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
413ac27a0ecSDave Kleikamp 		if (journal)
414dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
415ac27a0ecSDave Kleikamp 	}
416ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
417b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
418ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
419ac27a0ecSDave Kleikamp 	}
420ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
421617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
422ac27a0ecSDave Kleikamp 			sb->s_id);
423ac27a0ecSDave Kleikamp }
424ac27a0ecSDave Kleikamp 
42512062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
426c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
427ac27a0ecSDave Kleikamp {
4280ff2ea7dSJoe Perches 	struct va_format vaf;
429ac27a0ecSDave Kleikamp 	va_list args;
430ac27a0ecSDave Kleikamp 
431ac27a0ecSDave Kleikamp 	va_start(args, fmt);
4320ff2ea7dSJoe Perches 	vaf.fmt = fmt;
4330ff2ea7dSJoe Perches 	vaf.va = &args;
4340ff2ea7dSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
4350ff2ea7dSJoe Perches 	       sb->s_id, function, line, current->comm, &vaf);
436ac27a0ecSDave Kleikamp 	va_end(args);
437ac27a0ecSDave Kleikamp 
438617ba13bSMingming Cao 	ext4_handle_error(sb);
439ac27a0ecSDave Kleikamp }
440ac27a0ecSDave Kleikamp 
441c398eda0STheodore Ts'o void ext4_error_inode(struct inode *inode, const char *function,
442c398eda0STheodore Ts'o 		      unsigned int line, ext4_fsblk_t block,
443273df556SFrank Mayhar 		      const char *fmt, ...)
444273df556SFrank Mayhar {
445273df556SFrank Mayhar 	va_list args;
446f7c21177STheodore Ts'o 	struct va_format vaf;
4471c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
448273df556SFrank Mayhar 
4491c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4501c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
4511c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
452273df556SFrank Mayhar 	va_start(args, fmt);
453f7c21177STheodore Ts'o 	vaf.fmt = fmt;
454f7c21177STheodore Ts'o 	vaf.va = &args;
455c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
456c398eda0STheodore Ts'o 	       inode->i_sb->s_id, function, line, inode->i_ino);
457c398eda0STheodore Ts'o 	if (block)
458f7c21177STheodore Ts'o 		printk(KERN_CONT "block %llu: ", block);
459f7c21177STheodore Ts'o 	printk(KERN_CONT "comm %s: %pV\n", current->comm, &vaf);
460273df556SFrank Mayhar 	va_end(args);
461273df556SFrank Mayhar 
462273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
463273df556SFrank Mayhar }
464273df556SFrank Mayhar 
465c398eda0STheodore Ts'o void ext4_error_file(struct file *file, const char *function,
466f7c21177STheodore Ts'o 		     unsigned int line, ext4_fsblk_t block,
467f7c21177STheodore Ts'o 		     const char *fmt, ...)
468273df556SFrank Mayhar {
469273df556SFrank Mayhar 	va_list args;
470f7c21177STheodore Ts'o 	struct va_format vaf;
4711c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
472273df556SFrank Mayhar 	struct inode *inode = file->f_dentry->d_inode;
473273df556SFrank Mayhar 	char pathname[80], *path;
474273df556SFrank Mayhar 
4751c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
4761c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4771c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
478273df556SFrank Mayhar 	path = d_path(&(file->f_path), pathname, sizeof(pathname));
479f9a62d09SDan Carpenter 	if (IS_ERR(path))
480273df556SFrank Mayhar 		path = "(unknown)";
481273df556SFrank Mayhar 	printk(KERN_CRIT
482f7c21177STheodore Ts'o 	       "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
483f7c21177STheodore Ts'o 	       inode->i_sb->s_id, function, line, inode->i_ino);
484f7c21177STheodore Ts'o 	if (block)
485f7c21177STheodore Ts'o 		printk(KERN_CONT "block %llu: ", block);
486f7c21177STheodore Ts'o 	va_start(args, fmt);
487f7c21177STheodore Ts'o 	vaf.fmt = fmt;
488f7c21177STheodore Ts'o 	vaf.va = &args;
489f7c21177STheodore Ts'o 	printk(KERN_CONT "comm %s: path %s: %pV\n", current->comm, path, &vaf);
490273df556SFrank Mayhar 	va_end(args);
491273df556SFrank Mayhar 
492273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
493273df556SFrank Mayhar }
494273df556SFrank Mayhar 
495617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
496ac27a0ecSDave Kleikamp 				     char nbuf[16])
497ac27a0ecSDave Kleikamp {
498ac27a0ecSDave Kleikamp 	char *errstr = NULL;
499ac27a0ecSDave Kleikamp 
500ac27a0ecSDave Kleikamp 	switch (errno) {
501ac27a0ecSDave Kleikamp 	case -EIO:
502ac27a0ecSDave Kleikamp 		errstr = "IO failure";
503ac27a0ecSDave Kleikamp 		break;
504ac27a0ecSDave Kleikamp 	case -ENOMEM:
505ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
506ac27a0ecSDave Kleikamp 		break;
507ac27a0ecSDave Kleikamp 	case -EROFS:
50878f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
50978f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
510ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
511ac27a0ecSDave Kleikamp 		else
512ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
513ac27a0ecSDave Kleikamp 		break;
514ac27a0ecSDave Kleikamp 	default:
515ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
516ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
517ac27a0ecSDave Kleikamp 		 * NULL. */
518ac27a0ecSDave Kleikamp 		if (nbuf) {
519ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
520ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
521ac27a0ecSDave Kleikamp 				errstr = nbuf;
522ac27a0ecSDave Kleikamp 		}
523ac27a0ecSDave Kleikamp 		break;
524ac27a0ecSDave Kleikamp 	}
525ac27a0ecSDave Kleikamp 
526ac27a0ecSDave Kleikamp 	return errstr;
527ac27a0ecSDave Kleikamp }
528ac27a0ecSDave Kleikamp 
529617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
530ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
531ac27a0ecSDave Kleikamp 
532c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
533c398eda0STheodore Ts'o 		      unsigned int line, int errno)
534ac27a0ecSDave Kleikamp {
535ac27a0ecSDave Kleikamp 	char nbuf[16];
536ac27a0ecSDave Kleikamp 	const char *errstr;
537ac27a0ecSDave Kleikamp 
538ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
539ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
540ac27a0ecSDave Kleikamp 	 * an error. */
541ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
542ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
543ac27a0ecSDave Kleikamp 		return;
544ac27a0ecSDave Kleikamp 
545617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
546c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
547c398eda0STheodore Ts'o 	       sb->s_id, function, line, errstr);
5481c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
549ac27a0ecSDave Kleikamp 
550617ba13bSMingming Cao 	ext4_handle_error(sb);
551ac27a0ecSDave Kleikamp }
552ac27a0ecSDave Kleikamp 
553ac27a0ecSDave Kleikamp /*
554617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
555ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
556ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
557ac27a0ecSDave Kleikamp  *
558ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
559ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
560ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
561ac27a0ecSDave Kleikamp  */
562ac27a0ecSDave Kleikamp 
563c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
564c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
565ac27a0ecSDave Kleikamp {
566ac27a0ecSDave Kleikamp 	va_list args;
567ac27a0ecSDave Kleikamp 
5681c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
569ac27a0ecSDave Kleikamp 	va_start(args, fmt);
570c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
571c398eda0STheodore Ts'o 	       function, line);
572ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
573ac27a0ecSDave Kleikamp 	printk("\n");
574ac27a0ecSDave Kleikamp 	va_end(args);
575ac27a0ecSDave Kleikamp 
5761c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
577b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
578ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
5794ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
580ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
581dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
5821c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
5831c13d5c0STheodore Ts'o 	}
5841c13d5c0STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC))
5851c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
586ac27a0ecSDave Kleikamp }
587ac27a0ecSDave Kleikamp 
5880ff2ea7dSJoe Perches void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
589b31e1552SEric Sandeen {
5900ff2ea7dSJoe Perches 	struct va_format vaf;
591b31e1552SEric Sandeen 	va_list args;
592b31e1552SEric Sandeen 
593b31e1552SEric Sandeen 	va_start(args, fmt);
5940ff2ea7dSJoe Perches 	vaf.fmt = fmt;
5950ff2ea7dSJoe Perches 	vaf.va = &args;
5960ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
597b31e1552SEric Sandeen 	va_end(args);
598b31e1552SEric Sandeen }
599b31e1552SEric Sandeen 
60012062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
601c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
602ac27a0ecSDave Kleikamp {
6030ff2ea7dSJoe Perches 	struct va_format vaf;
604ac27a0ecSDave Kleikamp 	va_list args;
605ac27a0ecSDave Kleikamp 
606ac27a0ecSDave Kleikamp 	va_start(args, fmt);
6070ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6080ff2ea7dSJoe Perches 	vaf.va = &args;
6090ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
6100ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
611ac27a0ecSDave Kleikamp 	va_end(args);
612ac27a0ecSDave Kleikamp }
613ac27a0ecSDave Kleikamp 
614e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
615e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
616e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
617e29136f8STheodore Ts'o 			     const char *fmt, ...)
6185d1b1b3fSAneesh Kumar K.V __releases(bitlock)
6195d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
6205d1b1b3fSAneesh Kumar K.V {
6210ff2ea7dSJoe Perches 	struct va_format vaf;
6225d1b1b3fSAneesh Kumar K.V 	va_list args;
6235d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
6245d1b1b3fSAneesh Kumar K.V 
6251c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
6261c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
6271c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
6280ff2ea7dSJoe Perches 
6295d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
6300ff2ea7dSJoe Perches 
6310ff2ea7dSJoe Perches 	vaf.fmt = fmt;
6320ff2ea7dSJoe Perches 	vaf.va = &args;
63321149d61SRobin Dong 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
634e29136f8STheodore Ts'o 	       sb->s_id, function, line, grp);
635e29136f8STheodore Ts'o 	if (ino)
6360ff2ea7dSJoe Perches 		printk(KERN_CONT "inode %lu: ", ino);
637e29136f8STheodore Ts'o 	if (block)
6380ff2ea7dSJoe Perches 		printk(KERN_CONT "block %llu:", (unsigned long long) block);
6390ff2ea7dSJoe Perches 	printk(KERN_CONT "%pV\n", &vaf);
6405d1b1b3fSAneesh Kumar K.V 	va_end(args);
6415d1b1b3fSAneesh Kumar K.V 
6425d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
643e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
6445d1b1b3fSAneesh Kumar K.V 		return;
6455d1b1b3fSAneesh Kumar K.V 	}
6461c13d5c0STheodore Ts'o 
6475d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
6485d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
6495d1b1b3fSAneesh Kumar K.V 	/*
6505d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
6515d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
6525d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
6535d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
6545d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
65525985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
6565d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
6575d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
6585d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
6595d1b1b3fSAneesh Kumar K.V 	 */
6605d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
6615d1b1b3fSAneesh Kumar K.V 	return;
6625d1b1b3fSAneesh Kumar K.V }
6635d1b1b3fSAneesh Kumar K.V 
664617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
665ac27a0ecSDave Kleikamp {
666617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
667ac27a0ecSDave Kleikamp 
668617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
669ac27a0ecSDave Kleikamp 		return;
670ac27a0ecSDave Kleikamp 
67112062dddSEric Sandeen 	ext4_warning(sb,
672ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
673ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
674617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
675ac27a0ecSDave Kleikamp 
676617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
677617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
678617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
679ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
680ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
681ac27a0ecSDave Kleikamp 
682ac27a0ecSDave Kleikamp 	/*
683ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
684ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
685ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
686ac27a0ecSDave Kleikamp 	 */
687ac27a0ecSDave Kleikamp }
688ac27a0ecSDave Kleikamp 
689ac27a0ecSDave Kleikamp /*
690ac27a0ecSDave Kleikamp  * Open the external journal device
691ac27a0ecSDave Kleikamp  */
692b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
693ac27a0ecSDave Kleikamp {
694ac27a0ecSDave Kleikamp 	struct block_device *bdev;
695ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
696ac27a0ecSDave Kleikamp 
697d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
698ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
699ac27a0ecSDave Kleikamp 		goto fail;
700ac27a0ecSDave Kleikamp 	return bdev;
701ac27a0ecSDave Kleikamp 
702ac27a0ecSDave Kleikamp fail:
703b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
704ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
705ac27a0ecSDave Kleikamp 	return NULL;
706ac27a0ecSDave Kleikamp }
707ac27a0ecSDave Kleikamp 
708ac27a0ecSDave Kleikamp /*
709ac27a0ecSDave Kleikamp  * Release the journal device
710ac27a0ecSDave Kleikamp  */
711617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
712ac27a0ecSDave Kleikamp {
713e525fd89STejun Heo 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
714ac27a0ecSDave Kleikamp }
715ac27a0ecSDave Kleikamp 
716617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
717ac27a0ecSDave Kleikamp {
718ac27a0ecSDave Kleikamp 	struct block_device *bdev;
719ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
720ac27a0ecSDave Kleikamp 
721ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
722ac27a0ecSDave Kleikamp 	if (bdev) {
723617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
724ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
725ac27a0ecSDave Kleikamp 	}
726ac27a0ecSDave Kleikamp 	return ret;
727ac27a0ecSDave Kleikamp }
728ac27a0ecSDave Kleikamp 
729ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
730ac27a0ecSDave Kleikamp {
731617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
732ac27a0ecSDave Kleikamp }
733ac27a0ecSDave Kleikamp 
734617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
735ac27a0ecSDave Kleikamp {
736ac27a0ecSDave Kleikamp 	struct list_head *l;
737ac27a0ecSDave Kleikamp 
738b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
739ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
740ac27a0ecSDave Kleikamp 
741ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
742ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
743ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
744ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
745ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
746ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
747ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
748ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
749ac27a0ecSDave Kleikamp 	}
750ac27a0ecSDave Kleikamp }
751ac27a0ecSDave Kleikamp 
752617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
753ac27a0ecSDave Kleikamp {
754617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
755617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
756ef2cabf7SHidehiro Kawai 	int i, err;
757ac27a0ecSDave Kleikamp 
758857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
759e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
760e0ccfd95SChristoph Hellwig 
7614c0425ffSMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
7624c0425ffSMingming Cao 	destroy_workqueue(sbi->dio_unwritten_wq);
7634c0425ffSMingming Cao 
764a9e220f8SAl Viro 	lock_super(sb);
7658c85e125SChristoph Hellwig 	if (sb->s_dirt)
766ebc1ac16SChristoph Hellwig 		ext4_commit_super(sb, 1);
7678c85e125SChristoph Hellwig 
7680390131bSFrank Mayhar 	if (sbi->s_journal) {
769ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
77047b4a50bSJan Kara 		sbi->s_journal = NULL;
771ef2cabf7SHidehiro Kawai 		if (err < 0)
772c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
7730390131bSFrank Mayhar 	}
774d4edac31SJosef Bacik 
775a1c6c569SSergey Senozhatsky 	del_timer(&sbi->s_err_report);
776d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
777d4edac31SJosef Bacik 	ext4_mb_release(sb);
778d4edac31SJosef Bacik 	ext4_ext_release(sb);
779d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
780d4edac31SJosef Bacik 
781ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
782617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
783ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
784e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
785ac27a0ecSDave Kleikamp 	}
786240799cdSTheodore Ts'o 	if (sbi->s_proc) {
7879f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
788240799cdSTheodore Ts'o 	}
7893197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
790ac27a0ecSDave Kleikamp 
791ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
792ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
793ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
794c5ca7c76STheodore Ts'o 	if (is_vmalloc_addr(sbi->s_flex_groups))
795c5ca7c76STheodore Ts'o 		vfree(sbi->s_flex_groups);
796c5ca7c76STheodore Ts'o 	else
797772cb7c8SJose R. Santos 		kfree(sbi->s_flex_groups);
798ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
799ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
800ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
8016bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
802ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
803ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
804ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
805ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
806ac27a0ecSDave Kleikamp #endif
807ac27a0ecSDave Kleikamp 
808ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
809ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
810ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
811ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
812ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
813ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
814ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
815ac27a0ecSDave Kleikamp 
816f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
817ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
818ac27a0ecSDave Kleikamp 		/*
819ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
820ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
821ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
822ac27a0ecSDave Kleikamp 		 */
823ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
824f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
825617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
826ac27a0ecSDave Kleikamp 	}
827c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
828c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
829ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
8303197ebdbSTheodore Ts'o 	/*
8313197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
8323197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
8333197ebdbSTheodore Ts'o 	 */
8343197ebdbSTheodore Ts'o 	unlock_super(sb);
8353197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
8363197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
837705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
838ac27a0ecSDave Kleikamp 	kfree(sbi);
839ac27a0ecSDave Kleikamp }
840ac27a0ecSDave Kleikamp 
841e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
842ac27a0ecSDave Kleikamp 
843ac27a0ecSDave Kleikamp /*
844ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
845ac27a0ecSDave Kleikamp  */
846617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
847ac27a0ecSDave Kleikamp {
848617ba13bSMingming Cao 	struct ext4_inode_info *ei;
849ac27a0ecSDave Kleikamp 
850e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
851ac27a0ecSDave Kleikamp 	if (!ei)
852ac27a0ecSDave Kleikamp 		return NULL;
8530b8e58a1SAndreas Dilger 
854ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
85591246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
856a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
857c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
858c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
859d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
860d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
861d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
8629d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
863d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
864a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
865a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
866a9e7f447SDmitry Monakhov #endif
8678aefcd55STheodore Ts'o 	ei->jinode = NULL;
868c7064ef1SJiaying Zhang 	INIT_LIST_HEAD(&ei->i_completed_io_list);
869744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
8708d5d02e6SMingming Cao 	ei->cur_aio_dio = NULL;
871b436b9beSJan Kara 	ei->i_sync_tid = 0;
872b436b9beSJan Kara 	ei->i_datasync_tid = 0;
873f7ad6d2eSTheodore Ts'o 	atomic_set(&ei->i_ioend_count, 0);
874e9e3bcecSEric Sandeen 	atomic_set(&ei->i_aiodio_unwritten, 0);
8750b8e58a1SAndreas Dilger 
876ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
877ac27a0ecSDave Kleikamp }
878ac27a0ecSDave Kleikamp 
8797ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
8807ff9c073STheodore Ts'o {
8817ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
8827ff9c073STheodore Ts'o 
8837ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
8847ff9c073STheodore Ts'o 	return drop;
8857ff9c073STheodore Ts'o }
8867ff9c073STheodore Ts'o 
887fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
888fa0d7e3dSNick Piggin {
889fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
890fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
891fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
892fa0d7e3dSNick Piggin }
893fa0d7e3dSNick Piggin 
894617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
895ac27a0ecSDave Kleikamp {
896f7ad6d2eSTheodore Ts'o 	ext4_ioend_wait(inode);
8979f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
898b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
899b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
900b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
9019f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
9029f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
9039f7dd93dSVasily Averin 				true);
9049f7dd93dSVasily Averin 		dump_stack();
9059f7dd93dSVasily Averin 	}
906fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
907ac27a0ecSDave Kleikamp }
908ac27a0ecSDave Kleikamp 
90951cc5068SAlexey Dobriyan static void init_once(void *foo)
910ac27a0ecSDave Kleikamp {
911617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
912ac27a0ecSDave Kleikamp 
913ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
91403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
915ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
916ac27a0ecSDave Kleikamp #endif
9170e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
918ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
919ac27a0ecSDave Kleikamp }
920ac27a0ecSDave Kleikamp 
921ac27a0ecSDave Kleikamp static int init_inodecache(void)
922ac27a0ecSDave Kleikamp {
923617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
924617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
925ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
926ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
92720c2df83SPaul Mundt 					     init_once);
928617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
929ac27a0ecSDave Kleikamp 		return -ENOMEM;
930ac27a0ecSDave Kleikamp 	return 0;
931ac27a0ecSDave Kleikamp }
932ac27a0ecSDave Kleikamp 
933ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
934ac27a0ecSDave Kleikamp {
935617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
936ac27a0ecSDave Kleikamp }
937ac27a0ecSDave Kleikamp 
9380930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
939ac27a0ecSDave Kleikamp {
9400930fcc1SAl Viro 	invalidate_inode_buffers(inode);
9410930fcc1SAl Viro 	end_writeback(inode);
9429f754758SChristoph Hellwig 	dquot_drop(inode);
943c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
9448aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
9458aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
9468aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
9478aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
9488aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
9498aefcd55STheodore Ts'o 	}
950ac27a0ecSDave Kleikamp }
951ac27a0ecSDave Kleikamp 
9522b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
9532b2d6d01STheodore Ts'o 					   struct super_block *sb)
954ac27a0ecSDave Kleikamp {
955ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
956617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
957ac27a0ecSDave Kleikamp 
9585a20bdfcSJan Kara 	if (sbi->s_jquota_fmt) {
9595a20bdfcSJan Kara 		char *fmtname = "";
9605a20bdfcSJan Kara 
9615a20bdfcSJan Kara 		switch (sbi->s_jquota_fmt) {
9625a20bdfcSJan Kara 		case QFMT_VFS_OLD:
9635a20bdfcSJan Kara 			fmtname = "vfsold";
9645a20bdfcSJan Kara 			break;
9655a20bdfcSJan Kara 		case QFMT_VFS_V0:
9665a20bdfcSJan Kara 			fmtname = "vfsv0";
9675a20bdfcSJan Kara 			break;
9685a20bdfcSJan Kara 		case QFMT_VFS_V1:
9695a20bdfcSJan Kara 			fmtname = "vfsv1";
9705a20bdfcSJan Kara 			break;
9715a20bdfcSJan Kara 		}
9725a20bdfcSJan Kara 		seq_printf(seq, ",jqfmt=%s", fmtname);
9735a20bdfcSJan Kara 	}
974ac27a0ecSDave Kleikamp 
975ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
976ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
977ac27a0ecSDave Kleikamp 
978ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
979ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
980ac27a0ecSDave Kleikamp 
981482a7425SDmitry Monakhov 	if (test_opt(sb, USRQUOTA))
982ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
983ac27a0ecSDave Kleikamp 
984482a7425SDmitry Monakhov 	if (test_opt(sb, GRPQUOTA))
985ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
986ac27a0ecSDave Kleikamp #endif
987ac27a0ecSDave Kleikamp }
988ac27a0ecSDave Kleikamp 
989d9c9bef1SMiklos Szeredi /*
990d9c9bef1SMiklos Szeredi  * Show an option if
991d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
992d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
993d9c9bef1SMiklos Szeredi  */
994617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
995ac27a0ecSDave Kleikamp {
996aa22df2cSAneesh Kumar K.V 	int def_errors;
997aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
998ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
999d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1000d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
1001d9c9bef1SMiklos Szeredi 
1002d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1003aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
1004d9c9bef1SMiklos Szeredi 
1005d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
1006d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
1007d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
1008d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
1009aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
1010d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
1011d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
1012d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
1013d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
1014d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
1015d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
1016d9c9bef1SMiklos Szeredi 	}
1017d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
1018d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
1019d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
1020d9c9bef1SMiklos Szeredi 	}
1021bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
1022d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
1023bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
1024d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
1025bb4f397aSAneesh Kumar K.V 		}
1026bb4f397aSAneesh Kumar K.V 	}
1027aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
1028bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
1029aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
1030d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
1031aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
1032d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
1033aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
1034d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
1035d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
1036d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
103703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1038ea663336SEric Sandeen 	if (test_opt(sb, XATTR_USER))
1039d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
1040ea663336SEric Sandeen 	if (!test_opt(sb, XATTR_USER))
1041d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
1042d9c9bef1SMiklos Szeredi #endif
104303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1044aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
1045d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
1046d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
1047d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
1048d9c9bef1SMiklos Szeredi #endif
104930773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
1050d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
1051d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
1052d9c9bef1SMiklos Szeredi 	}
105330773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
105430773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
105530773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
105630773840STheodore Ts'o 	}
105730773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
105830773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
105930773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
106030773840STheodore Ts'o 	}
106130773840STheodore Ts'o 
1062571640caSEric Sandeen 	/*
1063571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
1064571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
1065571640caSEric Sandeen 	 * status is.
1066571640caSEric Sandeen 	 */
1067571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
1068571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
1069cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
1070cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
107139a4badeSJan Kara 	else if (test_opt(sb, JOURNAL_CHECKSUM))
107239a4badeSJan Kara 		seq_puts(seq, ",journal_checksum");
107325ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
107425ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
10758b67f04aSTheodore Ts'o 	if (!test_opt(sb, DELALLOC) &&
10768b67f04aSTheodore Ts'o 	    !(def_mount_opts & EXT4_DEFM_NODELALLOC))
1077dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
1078dd919b98SAneesh Kumar K.V 
10796fd7a467STheodore Ts'o 	if (!test_opt(sb, MBLK_IO_SUBMIT))
10806fd7a467STheodore Ts'o 		seq_puts(seq, ",nomblk_io_submit");
1081cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
1082cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
1083aa22df2cSAneesh Kumar K.V 	/*
1084aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
1085aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
1086aa22df2cSAneesh Kumar K.V 	 */
1087617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
1088ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
1089617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
1090ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
1091617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
1092ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
1093ac27a0ecSDave Kleikamp 
1094240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
1095240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
1096240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
1097240799cdSTheodore Ts'o 
10985bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
10995bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
11005bf5683aSHidehiro Kawai 
1101afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
110206705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
1103afd4672dSTheodore Ts'o 
11048b67f04aSTheodore Ts'o 	if (test_opt(sb, DISCARD) && !(def_mount_opts & EXT4_DEFM_DISCARD))
11055328e635SEric Sandeen 		seq_puts(seq, ",discard");
11065328e635SEric Sandeen 
1107e3bb52aeSEric Sandeen 	if (test_opt(sb, NOLOAD))
1108e3bb52aeSEric Sandeen 		seq_puts(seq, ",norecovery");
1109e3bb52aeSEric Sandeen 
1110744692dcSJiaying Zhang 	if (test_opt(sb, DIOREAD_NOLOCK))
1111744692dcSJiaying Zhang 		seq_puts(seq, ",dioread_nolock");
1112744692dcSJiaying Zhang 
11138b67f04aSTheodore Ts'o 	if (test_opt(sb, BLOCK_VALIDITY) &&
11148b67f04aSTheodore Ts'o 	    !(def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY))
11158b67f04aSTheodore Ts'o 		seq_puts(seq, ",block_validity");
11168b67f04aSTheodore Ts'o 
1117bfff6873SLukas Czerner 	if (!test_opt(sb, INIT_INODE_TABLE))
1118bfff6873SLukas Czerner 		seq_puts(seq, ",noinit_inode_table");
111951ce6511SLukas Czerner 	else if (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)
1120bfff6873SLukas Czerner 		seq_printf(seq, ",init_inode_table=%u",
1121bfff6873SLukas Czerner 			   (unsigned) sbi->s_li_wait_mult);
1122bfff6873SLukas Czerner 
1123617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
11240b8e58a1SAndreas Dilger 
1125ac27a0ecSDave Kleikamp 	return 0;
1126ac27a0ecSDave Kleikamp }
1127ac27a0ecSDave Kleikamp 
11281b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
11291b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1130ac27a0ecSDave Kleikamp {
1131ac27a0ecSDave Kleikamp 	struct inode *inode;
1132ac27a0ecSDave Kleikamp 
1133617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1134ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1135617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1136ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1137ac27a0ecSDave Kleikamp 
1138ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1139ac27a0ecSDave Kleikamp 	 *
1140617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1141ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1142ac27a0ecSDave Kleikamp 	 *
1143ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1144ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1145ac27a0ecSDave Kleikamp 	 */
11461d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
11471d1fe1eeSDavid Howells 	if (IS_ERR(inode))
11481d1fe1eeSDavid Howells 		return ERR_CAST(inode);
11491d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1150ac27a0ecSDave Kleikamp 		iput(inode);
1151ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1152ac27a0ecSDave Kleikamp 	}
11531b961ac0SChristoph Hellwig 
11541b961ac0SChristoph Hellwig 	return inode;
1155ac27a0ecSDave Kleikamp }
11561b961ac0SChristoph Hellwig 
11571b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
11581b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
11591b961ac0SChristoph Hellwig {
11601b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
11611b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
11621b961ac0SChristoph Hellwig }
11631b961ac0SChristoph Hellwig 
11641b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
11651b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
11661b961ac0SChristoph Hellwig {
11671b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
11681b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1169ac27a0ecSDave Kleikamp }
1170ac27a0ecSDave Kleikamp 
1171c39a7f84SToshiyuki Okajima /*
1172c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1173c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1174c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1175c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1176c39a7f84SToshiyuki Okajima  */
11770b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
11780b8e58a1SAndreas Dilger 				 gfp_t wait)
1179c39a7f84SToshiyuki Okajima {
1180c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1181c39a7f84SToshiyuki Okajima 
1182c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1183c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1184c39a7f84SToshiyuki Okajima 		return 0;
1185c39a7f84SToshiyuki Okajima 	if (journal)
1186c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1187c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1188c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1189c39a7f84SToshiyuki Okajima }
1190c39a7f84SToshiyuki Okajima 
1191ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1192ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1193ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1194ac27a0ecSDave Kleikamp 
1195617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1196617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1197617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1198617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1199617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
12006f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1201f00c9e44SJan Kara 			 struct path *path);
1202ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
1203617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1204617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1205ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1206617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1207ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
1208ac27a0ecSDave Kleikamp 
120961e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
121060e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1211617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1212617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1213617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1214617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1215a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1216a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1217a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1218ac27a0ecSDave Kleikamp };
1219ac27a0ecSDave Kleikamp 
12200d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1221617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1222ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1223287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
1224287a8095SChristoph Hellwig 	.get_info	= dquot_get_dqinfo,
1225287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1226287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1227287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1228ac27a0ecSDave Kleikamp };
1229ac27a0ecSDave Kleikamp #endif
1230ac27a0ecSDave Kleikamp 
1231ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1232617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1233617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1234617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1235617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
12367ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
12370930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1238617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1239617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1240c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1241c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1242617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1243617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1244617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1245ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1246617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1247617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1248ac27a0ecSDave Kleikamp #endif
1249c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1250ac27a0ecSDave Kleikamp };
1251ac27a0ecSDave Kleikamp 
12529ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
12539ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
12549ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
12559ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
12569ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
12577ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
12580930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
12599ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
12609ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
12619ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
12629ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
12639ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
12649ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
12659ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
12669ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
12679ca92389STheodore Ts'o #endif
12689ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
12699ca92389STheodore Ts'o };
12709ca92389STheodore Ts'o 
127139655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
12721b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
12731b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1274617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1275ac27a0ecSDave Kleikamp };
1276ac27a0ecSDave Kleikamp 
1277ac27a0ecSDave Kleikamp enum {
1278ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1279ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
128001436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1281ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
128206705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
128330773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1284c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1285818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1286ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1287296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1288ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
12895a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
12905a20bdfcSJan Kara 	Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
12915a20bdfcSJan Kara 	Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
12921449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
12931449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
12945328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1295744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
12965328e635SEric Sandeen 	Opt_discard, Opt_nodiscard,
1297bfff6873SLukas Czerner 	Opt_init_inode_table, Opt_noinit_inode_table,
1298ac27a0ecSDave Kleikamp };
1299ac27a0ecSDave Kleikamp 
1300a447c093SSteven Whitehouse static const match_table_t tokens = {
1301ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1302ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1303ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1304ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1305ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1306ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1307ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1308ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1309ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1310ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1311ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1312ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1313ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1314ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1315ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1316ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1317ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1318ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1319ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1320ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1321ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1322e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
1323ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1324ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1325ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
132630773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
132730773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1328ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1329ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1330818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1331818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1332ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1333ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1334ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1335ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
13365bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
13375bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1338ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1339ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1340ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1341ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1342ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1343ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
13445a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1345ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1346ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1347ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1348ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1349ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
135006705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
135106705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
135225ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1353c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1354ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
135564769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1356dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
13571449032bSTheodore Ts'o 	{Opt_mblk_io_submit, "mblk_io_submit"},
13581449032bSTheodore Ts'o 	{Opt_nomblk_io_submit, "nomblk_io_submit"},
13596fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
13606fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1361240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1362b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1363afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
136406705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
136506705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1366744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1367744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
13685328e635SEric Sandeen 	{Opt_discard, "discard"},
13695328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1370bfff6873SLukas Czerner 	{Opt_init_inode_table, "init_itable=%u"},
1371bfff6873SLukas Czerner 	{Opt_init_inode_table, "init_itable"},
1372bfff6873SLukas Czerner 	{Opt_noinit_inode_table, "noinit_itable"},
1373f3f12faaSJosef Bacik 	{Opt_err, NULL},
1374ac27a0ecSDave Kleikamp };
1375ac27a0ecSDave Kleikamp 
1376617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1377ac27a0ecSDave Kleikamp {
1378617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1379ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1380ac27a0ecSDave Kleikamp 
1381ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1382ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
13830b8e58a1SAndreas Dilger 
1384ac27a0ecSDave Kleikamp 	options += 3;
13850b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1386ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1387ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
13884776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1389ac27a0ecSDave Kleikamp 		       (char *) *data);
1390ac27a0ecSDave Kleikamp 		return 1;
1391ac27a0ecSDave Kleikamp 	}
1392ac27a0ecSDave Kleikamp 	if (*options == ',')
1393ac27a0ecSDave Kleikamp 		options++;
1394ac27a0ecSDave Kleikamp 	*data = (void *) options;
13950b8e58a1SAndreas Dilger 
1396ac27a0ecSDave Kleikamp 	return sb_block;
1397ac27a0ecSDave Kleikamp }
1398ac27a0ecSDave Kleikamp 
1399b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1400437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1401437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1402b3881f74STheodore Ts'o 
140356c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
140456c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
140556c50f11SDmitry Monakhov {
140656c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
140756c50f11SDmitry Monakhov 	char *qname;
140856c50f11SDmitry Monakhov 
140956c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
141056c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
141156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
141256c50f11SDmitry Monakhov 			"Cannot change journaled "
141356c50f11SDmitry Monakhov 			"quota options when quota turned on");
141456c50f11SDmitry Monakhov 		return 0;
141556c50f11SDmitry Monakhov 	}
141656c50f11SDmitry Monakhov 	qname = match_strdup(args);
141756c50f11SDmitry Monakhov 	if (!qname) {
141856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
141956c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
142056c50f11SDmitry Monakhov 		return 0;
142156c50f11SDmitry Monakhov 	}
142256c50f11SDmitry Monakhov 	if (sbi->s_qf_names[qtype] &&
142356c50f11SDmitry Monakhov 		strcmp(sbi->s_qf_names[qtype], qname)) {
142456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
142556c50f11SDmitry Monakhov 			"%s quota file already specified", QTYPE2NAME(qtype));
142656c50f11SDmitry Monakhov 		kfree(qname);
142756c50f11SDmitry Monakhov 		return 0;
142856c50f11SDmitry Monakhov 	}
142956c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = qname;
143056c50f11SDmitry Monakhov 	if (strchr(sbi->s_qf_names[qtype], '/')) {
143156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
143256c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
143356c50f11SDmitry Monakhov 		kfree(sbi->s_qf_names[qtype]);
143456c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype] = NULL;
143556c50f11SDmitry Monakhov 		return 0;
143656c50f11SDmitry Monakhov 	}
1437fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
143856c50f11SDmitry Monakhov 	return 1;
143956c50f11SDmitry Monakhov }
144056c50f11SDmitry Monakhov 
144156c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
144256c50f11SDmitry Monakhov {
144356c50f11SDmitry Monakhov 
144456c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
144556c50f11SDmitry Monakhov 
144656c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
144756c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
144856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
144956c50f11SDmitry Monakhov 			" when quota turned on");
145056c50f11SDmitry Monakhov 		return 0;
145156c50f11SDmitry Monakhov 	}
145256c50f11SDmitry Monakhov 	/*
145356c50f11SDmitry Monakhov 	 * The space will be released later when all options are confirmed
145456c50f11SDmitry Monakhov 	 * to be correct
145556c50f11SDmitry Monakhov 	 */
145656c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
145756c50f11SDmitry Monakhov 	return 1;
145856c50f11SDmitry Monakhov }
145956c50f11SDmitry Monakhov #endif
146056c50f11SDmitry Monakhov 
1461ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1462c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1463b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1464617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1465ac27a0ecSDave Kleikamp {
1466617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1467ac27a0ecSDave Kleikamp 	char *p;
1468ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1469ac27a0ecSDave Kleikamp 	int data_opt = 0;
1470ac27a0ecSDave Kleikamp 	int option;
1471ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
147256c50f11SDmitry Monakhov 	int qfmt;
1473ac27a0ecSDave Kleikamp #endif
1474ac27a0ecSDave Kleikamp 
1475ac27a0ecSDave Kleikamp 	if (!options)
1476ac27a0ecSDave Kleikamp 		return 1;
1477ac27a0ecSDave Kleikamp 
1478ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1479ac27a0ecSDave Kleikamp 		int token;
1480ac27a0ecSDave Kleikamp 		if (!*p)
1481ac27a0ecSDave Kleikamp 			continue;
1482ac27a0ecSDave Kleikamp 
148315121c18SEric Sandeen 		/*
148415121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
148515121c18SEric Sandeen 		 * found; some options take optional arguments.
148615121c18SEric Sandeen 		 */
14877dc57615SPeter Huewe 		args[0].to = args[0].from = NULL;
1488ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1489ac27a0ecSDave Kleikamp 		switch (token) {
1490ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1491437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1492fd8c37ecSTheodore Ts'o 			clear_opt(sb, MINIX_DF);
1493ac27a0ecSDave Kleikamp 			break;
1494ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1495437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1496fd8c37ecSTheodore Ts'o 			set_opt(sb, MINIX_DF);
1497437ca0fdSDmitry Monakhov 
1498ac27a0ecSDave Kleikamp 			break;
1499ac27a0ecSDave Kleikamp 		case Opt_grpid:
1500437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1501fd8c37ecSTheodore Ts'o 			set_opt(sb, GRPID);
1502437ca0fdSDmitry Monakhov 
1503ac27a0ecSDave Kleikamp 			break;
1504ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1505437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1506fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPID);
1507437ca0fdSDmitry Monakhov 
1508ac27a0ecSDave Kleikamp 			break;
1509ac27a0ecSDave Kleikamp 		case Opt_resuid:
1510ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1511ac27a0ecSDave Kleikamp 				return 0;
1512ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1513ac27a0ecSDave Kleikamp 			break;
1514ac27a0ecSDave Kleikamp 		case Opt_resgid:
1515ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1516ac27a0ecSDave Kleikamp 				return 0;
1517ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1518ac27a0ecSDave Kleikamp 			break;
1519ac27a0ecSDave Kleikamp 		case Opt_sb:
1520ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1521ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1522ac27a0ecSDave Kleikamp 			break;
1523ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1524fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_CONT);
1525fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_RO);
1526fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_PANIC);
1527ac27a0ecSDave Kleikamp 			break;
1528ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1529fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_CONT);
1530fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_PANIC);
1531fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_RO);
1532ac27a0ecSDave Kleikamp 			break;
1533ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1534fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_RO);
1535fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_PANIC);
1536fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_CONT);
1537ac27a0ecSDave Kleikamp 			break;
1538ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1539fd8c37ecSTheodore Ts'o 			set_opt(sb, NO_UID32);
1540ac27a0ecSDave Kleikamp 			break;
1541ac27a0ecSDave Kleikamp 		case Opt_debug:
1542fd8c37ecSTheodore Ts'o 			set_opt(sb, DEBUG);
1543ac27a0ecSDave Kleikamp 			break;
1544ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1545fd8c37ecSTheodore Ts'o 			set_opt(sb, OLDALLOC);
1546ac27a0ecSDave Kleikamp 			break;
1547ac27a0ecSDave Kleikamp 		case Opt_orlov:
1548fd8c37ecSTheodore Ts'o 			clear_opt(sb, OLDALLOC);
1549ac27a0ecSDave Kleikamp 			break;
155003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1551ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1552fd8c37ecSTheodore Ts'o 			set_opt(sb, XATTR_USER);
1553ac27a0ecSDave Kleikamp 			break;
1554ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1555fd8c37ecSTheodore Ts'o 			clear_opt(sb, XATTR_USER);
1556ac27a0ecSDave Kleikamp 			break;
1557ac27a0ecSDave Kleikamp #else
1558ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1559ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1560b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1561ac27a0ecSDave Kleikamp 			break;
1562ac27a0ecSDave Kleikamp #endif
156303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1564ac27a0ecSDave Kleikamp 		case Opt_acl:
1565fd8c37ecSTheodore Ts'o 			set_opt(sb, POSIX_ACL);
1566ac27a0ecSDave Kleikamp 			break;
1567ac27a0ecSDave Kleikamp 		case Opt_noacl:
1568fd8c37ecSTheodore Ts'o 			clear_opt(sb, POSIX_ACL);
1569ac27a0ecSDave Kleikamp 			break;
1570ac27a0ecSDave Kleikamp #else
1571ac27a0ecSDave Kleikamp 		case Opt_acl:
1572ac27a0ecSDave Kleikamp 		case Opt_noacl:
1573b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1574ac27a0ecSDave Kleikamp 			break;
1575ac27a0ecSDave Kleikamp #endif
1576ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1577ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1578ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1579ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1580ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1581ac27a0ecSDave Kleikamp 			   journal file. */
1582ac27a0ecSDave Kleikamp 			if (is_remount) {
1583b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1584b31e1552SEric Sandeen 					 "Cannot specify journal on remount");
1585ac27a0ecSDave Kleikamp 				return 0;
1586ac27a0ecSDave Kleikamp 			}
1587fd8c37ecSTheodore Ts'o 			set_opt(sb, UPDATE_JOURNAL);
1588ac27a0ecSDave Kleikamp 			break;
1589ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1590ac27a0ecSDave Kleikamp 			if (is_remount) {
1591b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1592b31e1552SEric Sandeen 					"Cannot specify journal on remount");
1593ac27a0ecSDave Kleikamp 				return 0;
1594ac27a0ecSDave Kleikamp 			}
1595ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1596ac27a0ecSDave Kleikamp 				return 0;
1597ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1598ac27a0ecSDave Kleikamp 			break;
1599818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1600fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_CHECKSUM);
1601d4da6c9cSLinus Torvalds 			break;
1602818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1603fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_ASYNC_COMMIT);
1604fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_CHECKSUM);
1605818d276cSGirish Shilamkar 			break;
1606ac27a0ecSDave Kleikamp 		case Opt_noload:
1607fd8c37ecSTheodore Ts'o 			set_opt(sb, NOLOAD);
1608ac27a0ecSDave Kleikamp 			break;
1609ac27a0ecSDave Kleikamp 		case Opt_commit:
1610ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1611ac27a0ecSDave Kleikamp 				return 0;
1612ac27a0ecSDave Kleikamp 			if (option < 0)
1613ac27a0ecSDave Kleikamp 				return 0;
1614ac27a0ecSDave Kleikamp 			if (option == 0)
1615cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1616ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1617ac27a0ecSDave Kleikamp 			break;
161830773840STheodore Ts'o 		case Opt_max_batch_time:
161930773840STheodore Ts'o 			if (match_int(&args[0], &option))
162030773840STheodore Ts'o 				return 0;
162130773840STheodore Ts'o 			if (option < 0)
162230773840STheodore Ts'o 				return 0;
162330773840STheodore Ts'o 			if (option == 0)
162430773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
162530773840STheodore Ts'o 			sbi->s_max_batch_time = option;
162630773840STheodore Ts'o 			break;
162730773840STheodore Ts'o 		case Opt_min_batch_time:
162830773840STheodore Ts'o 			if (match_int(&args[0], &option))
162930773840STheodore Ts'o 				return 0;
163030773840STheodore Ts'o 			if (option < 0)
163130773840STheodore Ts'o 				return 0;
163230773840STheodore Ts'o 			sbi->s_min_batch_time = option;
163330773840STheodore Ts'o 			break;
1634ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1635617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1636ac27a0ecSDave Kleikamp 			goto datacheck;
1637ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1638617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1639ac27a0ecSDave Kleikamp 			goto datacheck;
1640ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1641617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1642ac27a0ecSDave Kleikamp 		datacheck:
1643ac27a0ecSDave Kleikamp 			if (is_remount) {
1644482a7425SDmitry Monakhov 				if (test_opt(sb, DATA_FLAGS) != data_opt) {
1645b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
1646b31e1552SEric Sandeen 						"Cannot change data mode on remount");
1647ac27a0ecSDave Kleikamp 					return 0;
1648ac27a0ecSDave Kleikamp 				}
1649ac27a0ecSDave Kleikamp 			} else {
1650fd8c37ecSTheodore Ts'o 				clear_opt(sb, DATA_FLAGS);
1651ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1652ac27a0ecSDave Kleikamp 			}
1653ac27a0ecSDave Kleikamp 			break;
16545bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
1655fd8c37ecSTheodore Ts'o 			set_opt(sb, DATA_ERR_ABORT);
16565bf5683aSHidehiro Kawai 			break;
16575bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
1658fd8c37ecSTheodore Ts'o 			clear_opt(sb, DATA_ERR_ABORT);
16595bf5683aSHidehiro Kawai 			break;
1660ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1661ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
166256c50f11SDmitry Monakhov 			if (!set_qf_name(sb, USRQUOTA, &args[0]))
166356c50f11SDmitry Monakhov 				return 0;
166456c50f11SDmitry Monakhov 			break;
1665ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
166656c50f11SDmitry Monakhov 			if (!set_qf_name(sb, GRPQUOTA, &args[0]))
1667ac27a0ecSDave Kleikamp 				return 0;
1668ac27a0ecSDave Kleikamp 			break;
1669ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
167056c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, USRQUOTA))
1671ac27a0ecSDave Kleikamp 				return 0;
1672ac27a0ecSDave Kleikamp 			break;
167356c50f11SDmitry Monakhov 		case Opt_offgrpjquota:
167456c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, GRPQUOTA))
167556c50f11SDmitry Monakhov 				return 0;
167656c50f11SDmitry Monakhov 			break;
167756c50f11SDmitry Monakhov 
1678ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1679dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1680dfc5d03fSJan Kara 			goto set_qf_format;
1681ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1682dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
16835a20bdfcSJan Kara 			goto set_qf_format;
16845a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
16855a20bdfcSJan Kara 			qfmt = QFMT_VFS_V1;
1686dfc5d03fSJan Kara set_qf_format:
168717bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1688dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1689b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
1690dfc5d03fSJan Kara 					"journaled quota options when "
1691b31e1552SEric Sandeen 					"quota turned on");
1692dfc5d03fSJan Kara 				return 0;
1693dfc5d03fSJan Kara 			}
1694dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1695ac27a0ecSDave Kleikamp 			break;
1696ac27a0ecSDave Kleikamp 		case Opt_quota:
1697ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1698fd8c37ecSTheodore Ts'o 			set_opt(sb, QUOTA);
1699fd8c37ecSTheodore Ts'o 			set_opt(sb, USRQUOTA);
1700ac27a0ecSDave Kleikamp 			break;
1701ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1702fd8c37ecSTheodore Ts'o 			set_opt(sb, QUOTA);
1703fd8c37ecSTheodore Ts'o 			set_opt(sb, GRPQUOTA);
1704ac27a0ecSDave Kleikamp 			break;
1705ac27a0ecSDave Kleikamp 		case Opt_noquota:
170617bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1707b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change quota "
1708b31e1552SEric Sandeen 					"options when quota turned on");
1709ac27a0ecSDave Kleikamp 				return 0;
1710ac27a0ecSDave Kleikamp 			}
1711fd8c37ecSTheodore Ts'o 			clear_opt(sb, QUOTA);
1712fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1713fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1714ac27a0ecSDave Kleikamp 			break;
1715ac27a0ecSDave Kleikamp #else
1716ac27a0ecSDave Kleikamp 		case Opt_quota:
1717ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1718ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1719b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1720b31e1552SEric Sandeen 				"quota options not supported");
1721cd59e7b9SJan Kara 			break;
1722ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1723ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1724ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1725ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1726ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1727ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
17285a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
1729b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1730b31e1552SEric Sandeen 				"journaled quota options not supported");
1731ac27a0ecSDave Kleikamp 			break;
1732ac27a0ecSDave Kleikamp 		case Opt_noquota:
1733ac27a0ecSDave Kleikamp 			break;
1734ac27a0ecSDave Kleikamp #endif
1735ac27a0ecSDave Kleikamp 		case Opt_abort:
17364ab2f15bSTheodore Ts'o 			sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1737ac27a0ecSDave Kleikamp 			break;
173806705bffSTheodore Ts'o 		case Opt_nobarrier:
1739fd8c37ecSTheodore Ts'o 			clear_opt(sb, BARRIER);
174006705bffSTheodore Ts'o 			break;
1741ac27a0ecSDave Kleikamp 		case Opt_barrier:
174215121c18SEric Sandeen 			if (args[0].from) {
174315121c18SEric Sandeen 				if (match_int(&args[0], &option))
174415121c18SEric Sandeen 					return 0;
174515121c18SEric Sandeen 			} else
174615121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1747ac27a0ecSDave Kleikamp 			if (option)
1748fd8c37ecSTheodore Ts'o 				set_opt(sb, BARRIER);
1749ac27a0ecSDave Kleikamp 			else
1750fd8c37ecSTheodore Ts'o 				clear_opt(sb, BARRIER);
1751ac27a0ecSDave Kleikamp 			break;
1752ac27a0ecSDave Kleikamp 		case Opt_ignore:
1753ac27a0ecSDave Kleikamp 			break;
1754ac27a0ecSDave Kleikamp 		case Opt_resize:
1755ac27a0ecSDave Kleikamp 			if (!is_remount) {
1756b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1757b31e1552SEric Sandeen 					"resize option only available "
1758b31e1552SEric Sandeen 					"for remount");
1759ac27a0ecSDave Kleikamp 				return 0;
1760ac27a0ecSDave Kleikamp 			}
1761ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1762ac27a0ecSDave Kleikamp 				return 0;
1763ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1764ac27a0ecSDave Kleikamp 			break;
1765ac27a0ecSDave Kleikamp 		case Opt_nobh:
1766206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1767206f7ab4SChristoph Hellwig 				 "Ignoring deprecated nobh option");
1768ac27a0ecSDave Kleikamp 			break;
1769ac27a0ecSDave Kleikamp 		case Opt_bh:
1770206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1771206f7ab4SChristoph Hellwig 				 "Ignoring deprecated bh option");
1772ac27a0ecSDave Kleikamp 			break;
177325ec56b5SJean Noel Cordenner 		case Opt_i_version:
1774fd8c37ecSTheodore Ts'o 			set_opt(sb, I_VERSION);
177525ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
177625ec56b5SJean Noel Cordenner 			break;
1777dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1778fd8c37ecSTheodore Ts'o 			clear_opt(sb, DELALLOC);
1779dd919b98SAneesh Kumar K.V 			break;
17801449032bSTheodore Ts'o 		case Opt_mblk_io_submit:
1781fd8c37ecSTheodore Ts'o 			set_opt(sb, MBLK_IO_SUBMIT);
17821449032bSTheodore Ts'o 			break;
17831449032bSTheodore Ts'o 		case Opt_nomblk_io_submit:
1784fd8c37ecSTheodore Ts'o 			clear_opt(sb, MBLK_IO_SUBMIT);
17851449032bSTheodore Ts'o 			break;
1786c9de560dSAlex Tomas 		case Opt_stripe:
1787c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1788c9de560dSAlex Tomas 				return 0;
1789c9de560dSAlex Tomas 			if (option < 0)
1790c9de560dSAlex Tomas 				return 0;
1791c9de560dSAlex Tomas 			sbi->s_stripe = option;
1792c9de560dSAlex Tomas 			break;
179364769240SAlex Tomas 		case Opt_delalloc:
1794fd8c37ecSTheodore Ts'o 			set_opt(sb, DELALLOC);
179564769240SAlex Tomas 			break;
17966fd058f7STheodore Ts'o 		case Opt_block_validity:
1797fd8c37ecSTheodore Ts'o 			set_opt(sb, BLOCK_VALIDITY);
17986fd058f7STheodore Ts'o 			break;
17996fd058f7STheodore Ts'o 		case Opt_noblock_validity:
1800fd8c37ecSTheodore Ts'o 			clear_opt(sb, BLOCK_VALIDITY);
18016fd058f7STheodore Ts'o 			break;
1802240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1803240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1804240799cdSTheodore Ts'o 				return 0;
1805240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1806240799cdSTheodore Ts'o 				return 0;
18075dbd571dSAlexander V. Lukyanov 			if (option && !is_power_of_2(option)) {
1808b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1809b31e1552SEric Sandeen 					 "EXT4-fs: inode_readahead_blks"
1810b31e1552SEric Sandeen 					 " must be a power of 2");
18113197ebdbSTheodore Ts'o 				return 0;
18123197ebdbSTheodore Ts'o 			}
1813240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1814240799cdSTheodore Ts'o 			break;
1815b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1816b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1817b3881f74STheodore Ts'o 				return 0;
1818b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1819b3881f74STheodore Ts'o 				break;
1820b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1821b3881f74STheodore Ts'o 							    option);
1822b3881f74STheodore Ts'o 			break;
182306705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
1824fd8c37ecSTheodore Ts'o 			set_opt(sb, NO_AUTO_DA_ALLOC);
182506705bffSTheodore Ts'o 			break;
1826afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
182715121c18SEric Sandeen 			if (args[0].from) {
182815121c18SEric Sandeen 				if (match_int(&args[0], &option))
182915121c18SEric Sandeen 					return 0;
183015121c18SEric Sandeen 			} else
183115121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1832afd4672dSTheodore Ts'o 			if (option)
1833fd8c37ecSTheodore Ts'o 				clear_opt(sb, NO_AUTO_DA_ALLOC);
1834afd4672dSTheodore Ts'o 			else
1835fd8c37ecSTheodore Ts'o 				set_opt(sb,NO_AUTO_DA_ALLOC);
1836afd4672dSTheodore Ts'o 			break;
18375328e635SEric Sandeen 		case Opt_discard:
1838fd8c37ecSTheodore Ts'o 			set_opt(sb, DISCARD);
18395328e635SEric Sandeen 			break;
18405328e635SEric Sandeen 		case Opt_nodiscard:
1841fd8c37ecSTheodore Ts'o 			clear_opt(sb, DISCARD);
18425328e635SEric Sandeen 			break;
1843744692dcSJiaying Zhang 		case Opt_dioread_nolock:
1844fd8c37ecSTheodore Ts'o 			set_opt(sb, DIOREAD_NOLOCK);
1845744692dcSJiaying Zhang 			break;
1846744692dcSJiaying Zhang 		case Opt_dioread_lock:
1847fd8c37ecSTheodore Ts'o 			clear_opt(sb, DIOREAD_NOLOCK);
1848744692dcSJiaying Zhang 			break;
1849bfff6873SLukas Czerner 		case Opt_init_inode_table:
1850fd8c37ecSTheodore Ts'o 			set_opt(sb, INIT_INODE_TABLE);
1851bfff6873SLukas Czerner 			if (args[0].from) {
1852bfff6873SLukas Czerner 				if (match_int(&args[0], &option))
1853bfff6873SLukas Czerner 					return 0;
1854bfff6873SLukas Czerner 			} else
1855bfff6873SLukas Czerner 				option = EXT4_DEF_LI_WAIT_MULT;
1856bfff6873SLukas Czerner 			if (option < 0)
1857bfff6873SLukas Czerner 				return 0;
1858bfff6873SLukas Czerner 			sbi->s_li_wait_mult = option;
1859bfff6873SLukas Czerner 			break;
1860bfff6873SLukas Czerner 		case Opt_noinit_inode_table:
1861fd8c37ecSTheodore Ts'o 			clear_opt(sb, INIT_INODE_TABLE);
1862bfff6873SLukas Czerner 			break;
1863ac27a0ecSDave Kleikamp 		default:
1864b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1865b31e1552SEric Sandeen 			       "Unrecognized mount option \"%s\" "
1866b31e1552SEric Sandeen 			       "or missing value", p);
1867ac27a0ecSDave Kleikamp 			return 0;
1868ac27a0ecSDave Kleikamp 		}
1869ac27a0ecSDave Kleikamp 	}
1870ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1871ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1872482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1873fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1874ac27a0ecSDave Kleikamp 
1875482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1876fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1877ac27a0ecSDave Kleikamp 
187856c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1879b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1880b31e1552SEric Sandeen 					"format mixing");
1881ac27a0ecSDave Kleikamp 			return 0;
1882ac27a0ecSDave Kleikamp 		}
1883ac27a0ecSDave Kleikamp 
1884ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1885b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1886b31e1552SEric Sandeen 					"not specified");
1887ac27a0ecSDave Kleikamp 			return 0;
1888ac27a0ecSDave Kleikamp 		}
1889ac27a0ecSDave Kleikamp 	} else {
1890ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1891b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
18922c8be6b2SJan Kara 					"specified with no journaling "
1893b31e1552SEric Sandeen 					"enabled");
1894ac27a0ecSDave Kleikamp 			return 0;
1895ac27a0ecSDave Kleikamp 		}
1896ac27a0ecSDave Kleikamp 	}
1897ac27a0ecSDave Kleikamp #endif
1898ac27a0ecSDave Kleikamp 	return 1;
1899ac27a0ecSDave Kleikamp }
1900ac27a0ecSDave Kleikamp 
1901617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1902ac27a0ecSDave Kleikamp 			    int read_only)
1903ac27a0ecSDave Kleikamp {
1904617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1905ac27a0ecSDave Kleikamp 	int res = 0;
1906ac27a0ecSDave Kleikamp 
1907617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1908b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1909b31e1552SEric Sandeen 			 "forcing read-only mode");
1910ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1911ac27a0ecSDave Kleikamp 	}
1912ac27a0ecSDave Kleikamp 	if (read_only)
1913ac27a0ecSDave Kleikamp 		return res;
1914617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1915b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1916b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1917617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1918b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1919b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1920b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1921ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1922ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1923ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1924b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1925b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1926b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1927ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1928ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1929ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1930b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1931b31e1552SEric Sandeen 			 "warning: checktime reached, "
1932b31e1552SEric Sandeen 			 "running e2fsck is recommended");
19330390131bSFrank Mayhar 	if (!sbi->s_journal)
1934216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1935ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1936617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1937e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1938ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1939617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
19400390131bSFrank Mayhar 	if (sbi->s_journal)
1941617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1942ac27a0ecSDave Kleikamp 
1943e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1944ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1945a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1946a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1947ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1948ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1949617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1950617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1951a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
1952ac27a0ecSDave Kleikamp 
19537abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
1954ac27a0ecSDave Kleikamp 	return res;
1955ac27a0ecSDave Kleikamp }
1956ac27a0ecSDave Kleikamp 
1957772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1958772cb7c8SJose R. Santos {
1959772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1960772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1961772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1962772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1963772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1964c5ca7c76STheodore Ts'o 	size_t size;
1965772cb7c8SJose R. Santos 	int i;
1966772cb7c8SJose R. Santos 
1967503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1968503358aeSTheodore Ts'o 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1969503358aeSTheodore Ts'o 
1970503358aeSTheodore Ts'o 	if (groups_per_flex < 2) {
1971772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1972772cb7c8SJose R. Santos 		return 1;
1973772cb7c8SJose R. Santos 	}
1974772cb7c8SJose R. Santos 
1975c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1976c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1977d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1978d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1979c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
1980c5ca7c76STheodore Ts'o 	sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1981c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
198294de56abSJoe Perches 		sbi->s_flex_groups = vzalloc(size);
1983772cb7c8SJose R. Santos 		if (sbi->s_flex_groups == NULL) {
198494de56abSJoe Perches 			ext4_msg(sb, KERN_ERR,
198594de56abSJoe Perches 				 "not enough memory for %u flex groups",
198694de56abSJoe Perches 				 flex_group_count);
1987772cb7c8SJose R. Santos 			goto failed;
1988772cb7c8SJose R. Santos 		}
198994de56abSJoe Perches 	}
1990772cb7c8SJose R. Santos 
1991772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
199288b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1993772cb7c8SJose R. Santos 
1994772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
19957ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
19967ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
19977ad9bb65STheodore Ts'o 		atomic_add(ext4_free_blks_count(sb, gdp),
19987ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_blocks);
19997ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
20007ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2001772cb7c8SJose R. Santos 	}
2002772cb7c8SJose R. Santos 
2003772cb7c8SJose R. Santos 	return 1;
2004772cb7c8SJose R. Santos failed:
2005772cb7c8SJose R. Santos 	return 0;
2006772cb7c8SJose R. Santos }
2007772cb7c8SJose R. Santos 
2008717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
2009717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
2010717d50e4SAndreas Dilger {
2011717d50e4SAndreas Dilger 	__u16 crc = 0;
2012717d50e4SAndreas Dilger 
2013717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
2014717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
2015717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
2016717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
2017717d50e4SAndreas Dilger 
2018717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2019717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2020717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
2021717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
2022717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
2023717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
2024717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
2025717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2026717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
2027717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
2028717d50e4SAndreas Dilger 					offset);
2029717d50e4SAndreas Dilger 	}
2030717d50e4SAndreas Dilger 
2031717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2032717d50e4SAndreas Dilger }
2033717d50e4SAndreas Dilger 
2034717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
2035717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2036717d50e4SAndreas Dilger {
2037717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
2038717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
2039717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
2040717d50e4SAndreas Dilger 		return 0;
2041717d50e4SAndreas Dilger 
2042717d50e4SAndreas Dilger 	return 1;
2043717d50e4SAndreas Dilger }
2044717d50e4SAndreas Dilger 
2045ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2046bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2047bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2048ac27a0ecSDave Kleikamp {
2049617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2050617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2051617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2052bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2053bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2054bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2055ce421581SJose R. Santos 	int flexbg_flag = 0;
2056bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2057ac27a0ecSDave Kleikamp 
2058ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2059ce421581SJose R. Santos 		flexbg_flag = 1;
2060ce421581SJose R. Santos 
2061617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2062ac27a0ecSDave Kleikamp 
2063197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2064197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2065197cd65aSAkinobu Mita 
2066ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2067bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2068ac27a0ecSDave Kleikamp 		else
2069ac27a0ecSDave Kleikamp 			last_block = first_block +
2070617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2071ac27a0ecSDave Kleikamp 
2072bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2073bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2074bfff6873SLukas Czerner 			grp = i;
2075bfff6873SLukas Czerner 
20768fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
20772b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2078b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2079a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2080b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2081ac27a0ecSDave Kleikamp 			return 0;
2082ac27a0ecSDave Kleikamp 		}
20838fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
20842b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2085b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2086a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2087b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2088ac27a0ecSDave Kleikamp 			return 0;
2089ac27a0ecSDave Kleikamp 		}
20908fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2091bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
20922b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2093b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2094a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2095b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2096ac27a0ecSDave Kleikamp 			return 0;
2097ac27a0ecSDave Kleikamp 		}
2098955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2099717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
2100b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2101b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2102fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2103fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
21047ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2105955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2106717d50e4SAndreas Dilger 				return 0;
2107717d50e4SAndreas Dilger 			}
21087ee1ec4cSLi Zefan 		}
2109955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2110ce421581SJose R. Santos 		if (!flexbg_flag)
2111617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2112ac27a0ecSDave Kleikamp 	}
2113bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2114bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2115ac27a0ecSDave Kleikamp 
2116bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
2117617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
2118ac27a0ecSDave Kleikamp 	return 1;
2119ac27a0ecSDave Kleikamp }
2120ac27a0ecSDave Kleikamp 
2121617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2122ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2123ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2124ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2125ac27a0ecSDave Kleikamp  *
2126ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2127ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2128ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2129ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2130ac27a0ecSDave Kleikamp  *
2131ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2132ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2133ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2134617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2135ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2136ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2137ac27a0ecSDave Kleikamp  */
2138617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2139617ba13bSMingming Cao 				struct ext4_super_block *es)
2140ac27a0ecSDave Kleikamp {
2141ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
2142ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
2143ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2144ac27a0ecSDave Kleikamp 	int i;
2145ac27a0ecSDave Kleikamp #endif
2146ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2147ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2148ac27a0ecSDave Kleikamp 		return;
2149ac27a0ecSDave Kleikamp 	}
2150ac27a0ecSDave Kleikamp 
2151a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2152b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2153b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2154a8f48a95SEric Sandeen 		return;
2155a8f48a95SEric Sandeen 	}
2156a8f48a95SEric Sandeen 
2157d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2158d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2159d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2160d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2161d39195c3SAmir Goldstein 		return;
2162d39195c3SAmir Goldstein 	}
2163d39195c3SAmir Goldstein 
2164617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2165ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
2166ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
2167ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2168ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
2169ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2170ac27a0ecSDave Kleikamp 		return;
2171ac27a0ecSDave Kleikamp 	}
2172ac27a0ecSDave Kleikamp 
2173ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2174b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2175ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2176ac27a0ecSDave Kleikamp 	}
2177ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2178ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2179ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2180ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2181ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2182617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2183617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2184ac27a0ecSDave Kleikamp 			if (ret < 0)
2185b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2186b31e1552SEric Sandeen 					"Cannot turn on journaled "
2187b31e1552SEric Sandeen 					"quota: error %d", ret);
2188ac27a0ecSDave Kleikamp 		}
2189ac27a0ecSDave Kleikamp 	}
2190ac27a0ecSDave Kleikamp #endif
2191ac27a0ecSDave Kleikamp 
2192ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2193ac27a0ecSDave Kleikamp 		struct inode *inode;
2194ac27a0ecSDave Kleikamp 
219597bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
219697bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2197ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2198ac27a0ecSDave Kleikamp 			break;
2199ac27a0ecSDave Kleikamp 		}
2200ac27a0ecSDave Kleikamp 
2201617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2202871a2931SChristoph Hellwig 		dquot_initialize(inode);
2203ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2204b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2205b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
220646e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
2207e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2208ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2209617ba13bSMingming Cao 			ext4_truncate(inode);
2210ac27a0ecSDave Kleikamp 			nr_truncates++;
2211ac27a0ecSDave Kleikamp 		} else {
2212b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2213b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
221446e665e9SHarvey Harrison 				__func__, inode->i_ino);
2215ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2216ac27a0ecSDave Kleikamp 				  inode->i_ino);
2217ac27a0ecSDave Kleikamp 			nr_orphans++;
2218ac27a0ecSDave Kleikamp 		}
2219ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2220ac27a0ecSDave Kleikamp 	}
2221ac27a0ecSDave Kleikamp 
2222ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2223ac27a0ecSDave Kleikamp 
2224ac27a0ecSDave Kleikamp 	if (nr_orphans)
2225b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2226b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2227ac27a0ecSDave Kleikamp 	if (nr_truncates)
2228b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2229b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2230ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2231ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2232ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2233ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2234287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2235ac27a0ecSDave Kleikamp 	}
2236ac27a0ecSDave Kleikamp #endif
2237ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2238ac27a0ecSDave Kleikamp }
22390b8e58a1SAndreas Dilger 
2240cd2291a4SEric Sandeen /*
2241cd2291a4SEric Sandeen  * Maximal extent format file size.
2242cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2243cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2244cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2245cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2246cd2291a4SEric Sandeen  *
2247f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2248f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2249f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2250f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2251f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2252f17722f9SLukas Czerner  *
2253cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2254cd2291a4SEric Sandeen  */
2255f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2256cd2291a4SEric Sandeen {
2257cd2291a4SEric Sandeen 	loff_t res;
2258cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2259cd2291a4SEric Sandeen 
2260cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2261f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2262cd2291a4SEric Sandeen 		/*
226390c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2264cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2265cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2266cd2291a4SEric Sandeen 		 */
2267cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2268cd2291a4SEric Sandeen 
2269cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2270cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2271cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2272cd2291a4SEric Sandeen 	}
2273cd2291a4SEric Sandeen 
2274f17722f9SLukas Czerner 	/*
2275f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2276f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2277f17722f9SLukas Czerner 	 * size
2278f17722f9SLukas Czerner 	 */
2279f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2280cd2291a4SEric Sandeen 	res <<= blkbits;
2281cd2291a4SEric Sandeen 
2282cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2283cd2291a4SEric Sandeen 	if (res > upper_limit)
2284cd2291a4SEric Sandeen 		res = upper_limit;
2285cd2291a4SEric Sandeen 
2286cd2291a4SEric Sandeen 	return res;
2287cd2291a4SEric Sandeen }
2288ac27a0ecSDave Kleikamp 
2289ac27a0ecSDave Kleikamp /*
2290cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
22910fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
22920fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2293ac27a0ecSDave Kleikamp  */
2294f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2295ac27a0ecSDave Kleikamp {
2296617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
22970fc1b451SAneesh Kumar K.V 	int meta_blocks;
22980fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
22990b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
23000b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
23010b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
23020b8e58a1SAndreas Dilger 	 *
23030b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
23040b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
23050fc1b451SAneesh Kumar K.V 	 */
23060fc1b451SAneesh Kumar K.V 
2307f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
23080fc1b451SAneesh Kumar K.V 		/*
230990c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
23100b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
23110b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
23120fc1b451SAneesh Kumar K.V 		 */
23130fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
23140fc1b451SAneesh Kumar K.V 
23150fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
23160fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
23170fc1b451SAneesh Kumar K.V 
23180fc1b451SAneesh Kumar K.V 	} else {
23198180a562SAneesh Kumar K.V 		/*
23208180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
23218180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
23228180a562SAneesh Kumar K.V 		 * represent total number of blocks in
23238180a562SAneesh Kumar K.V 		 * file system block size
23248180a562SAneesh Kumar K.V 		 */
23250fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
23260fc1b451SAneesh Kumar K.V 
23270fc1b451SAneesh Kumar K.V 	}
23280fc1b451SAneesh Kumar K.V 
23290fc1b451SAneesh Kumar K.V 	/* indirect blocks */
23300fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
23310fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
23320fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
23330fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
23340fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
23350fc1b451SAneesh Kumar K.V 
23360fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
23370fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2338ac27a0ecSDave Kleikamp 
2339ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2340ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2341ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2342ac27a0ecSDave Kleikamp 	res <<= bits;
2343ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2344ac27a0ecSDave Kleikamp 		res = upper_limit;
23450fc1b451SAneesh Kumar K.V 
23460fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
23470fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
23480fc1b451SAneesh Kumar K.V 
2349ac27a0ecSDave Kleikamp 	return res;
2350ac27a0ecSDave Kleikamp }
2351ac27a0ecSDave Kleikamp 
2352617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
235370bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2354ac27a0ecSDave Kleikamp {
2355617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2356fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2357ac27a0ecSDave Kleikamp 	int has_super = 0;
2358ac27a0ecSDave Kleikamp 
2359ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2360ac27a0ecSDave Kleikamp 
2361617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2362ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
236370bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2364ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2365617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2366ac27a0ecSDave Kleikamp 		has_super = 1;
23670b8e58a1SAndreas Dilger 
2368617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2369ac27a0ecSDave Kleikamp }
2370ac27a0ecSDave Kleikamp 
2371c9de560dSAlex Tomas /**
2372c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2373c9de560dSAlex Tomas  * @sbi: In memory super block info
2374c9de560dSAlex Tomas  *
2375c9de560dSAlex Tomas  * If we have specified it via mount option, then
2376c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2377c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2378c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2379c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2380c9de560dSAlex Tomas  *
2381c9de560dSAlex Tomas  */
2382c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2383c9de560dSAlex Tomas {
2384c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2385c9de560dSAlex Tomas 	unsigned long stripe_width =
2386c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
23873eb08658SDan Ehrenberg 	int ret;
2388c9de560dSAlex Tomas 
2389c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
23903eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
23913eb08658SDan Ehrenberg 	else if (stripe_width <= sbi->s_blocks_per_group)
23923eb08658SDan Ehrenberg 		ret = stripe_width;
23933eb08658SDan Ehrenberg 	else if (stride <= sbi->s_blocks_per_group)
23943eb08658SDan Ehrenberg 		ret = stride;
23953eb08658SDan Ehrenberg 	else
23963eb08658SDan Ehrenberg 		ret = 0;
2397c9de560dSAlex Tomas 
23983eb08658SDan Ehrenberg 	/*
23993eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
24003eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
24013eb08658SDan Ehrenberg 	 */
24023eb08658SDan Ehrenberg 	if (ret <= 1)
24033eb08658SDan Ehrenberg 		ret = 0;
2404c9de560dSAlex Tomas 
24053eb08658SDan Ehrenberg 	return ret;
2406c9de560dSAlex Tomas }
2407ac27a0ecSDave Kleikamp 
24083197ebdbSTheodore Ts'o /* sysfs supprt */
24093197ebdbSTheodore Ts'o 
24103197ebdbSTheodore Ts'o struct ext4_attr {
24113197ebdbSTheodore Ts'o 	struct attribute attr;
24123197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
24133197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
24143197ebdbSTheodore Ts'o 			 const char *, size_t);
24153197ebdbSTheodore Ts'o 	int offset;
24163197ebdbSTheodore Ts'o };
24173197ebdbSTheodore Ts'o 
24183197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
24193197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
24203197ebdbSTheodore Ts'o {
24213197ebdbSTheodore Ts'o 	char *endp;
24223197ebdbSTheodore Ts'o 
2423e7d2860bSAndré Goddard Rosa 	*value = simple_strtoul(skip_spaces(buf), &endp, 0);
2424e7d2860bSAndré Goddard Rosa 	endp = skip_spaces(endp);
24253197ebdbSTheodore Ts'o 	if (*endp || *value > max)
24263197ebdbSTheodore Ts'o 		return -EINVAL;
24273197ebdbSTheodore Ts'o 
24283197ebdbSTheodore Ts'o 	return 0;
24293197ebdbSTheodore Ts'o }
24303197ebdbSTheodore Ts'o 
24313197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
24323197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
24333197ebdbSTheodore Ts'o 					      char *buf)
24343197ebdbSTheodore Ts'o {
24353197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
24363197ebdbSTheodore Ts'o 			(s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
24373197ebdbSTheodore Ts'o }
24383197ebdbSTheodore Ts'o 
24393197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
24403197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
24413197ebdbSTheodore Ts'o {
24423197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24433197ebdbSTheodore Ts'o 
2444f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2445f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24463197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
24473197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
24483197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
24493197ebdbSTheodore Ts'o }
24503197ebdbSTheodore Ts'o 
24513197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
24523197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
24533197ebdbSTheodore Ts'o {
24543197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
24553197ebdbSTheodore Ts'o 
2456f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2457f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
24583197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2459a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
24603197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2461a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
24623197ebdbSTheodore Ts'o }
24633197ebdbSTheodore Ts'o 
246477f4135fSVivek Haldar static ssize_t extent_cache_hits_show(struct ext4_attr *a,
246577f4135fSVivek Haldar 				      struct ext4_sb_info *sbi, char *buf)
246677f4135fSVivek Haldar {
246777f4135fSVivek Haldar 	return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_hits);
246877f4135fSVivek Haldar }
246977f4135fSVivek Haldar 
247077f4135fSVivek Haldar static ssize_t extent_cache_misses_show(struct ext4_attr *a,
247177f4135fSVivek Haldar 					struct ext4_sb_info *sbi, char *buf)
247277f4135fSVivek Haldar {
247377f4135fSVivek Haldar 	return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_misses);
247477f4135fSVivek Haldar }
247577f4135fSVivek Haldar 
24763197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
24773197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
24783197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
24793197ebdbSTheodore Ts'o {
24803197ebdbSTheodore Ts'o 	unsigned long t;
24813197ebdbSTheodore Ts'o 
24823197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
24833197ebdbSTheodore Ts'o 		return -EINVAL;
24843197ebdbSTheodore Ts'o 
24855dbd571dSAlexander V. Lukyanov 	if (t && !is_power_of_2(t))
24863197ebdbSTheodore Ts'o 		return -EINVAL;
24873197ebdbSTheodore Ts'o 
24883197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
24893197ebdbSTheodore Ts'o 	return count;
24903197ebdbSTheodore Ts'o }
24913197ebdbSTheodore Ts'o 
24923197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
24933197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
24943197ebdbSTheodore Ts'o {
24953197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
24963197ebdbSTheodore Ts'o 
24973197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
24983197ebdbSTheodore Ts'o }
24993197ebdbSTheodore Ts'o 
25003197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
25013197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
25023197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
25033197ebdbSTheodore Ts'o {
25043197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
25053197ebdbSTheodore Ts'o 	unsigned long t;
25063197ebdbSTheodore Ts'o 
25073197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
25083197ebdbSTheodore Ts'o 		return -EINVAL;
25093197ebdbSTheodore Ts'o 	*ui = t;
25103197ebdbSTheodore Ts'o 	return count;
25113197ebdbSTheodore Ts'o }
25123197ebdbSTheodore Ts'o 
25133197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
25143197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
25153197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
25163197ebdbSTheodore Ts'o 	.show	= _show,					\
25173197ebdbSTheodore Ts'o 	.store	= _store,					\
25183197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
25193197ebdbSTheodore Ts'o }
25203197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
25213197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
25223197ebdbSTheodore Ts'o 
2523857ac889SLukas Czerner #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
25243197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
25253197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
25263197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
25273197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
25283197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
25293197ebdbSTheodore Ts'o 
25303197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
25313197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
25323197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
253377f4135fSVivek Haldar EXT4_RO_ATTR(extent_cache_hits);
253477f4135fSVivek Haldar EXT4_RO_ATTR(extent_cache_misses);
25353197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
25363197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
253711013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
25383197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
25393197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
25403197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
25413197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
25423197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
25433197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
254455138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
25453197ebdbSTheodore Ts'o 
25463197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
25473197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
25483197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
25493197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
255077f4135fSVivek Haldar 	ATTR_LIST(extent_cache_hits),
255177f4135fSVivek Haldar 	ATTR_LIST(extent_cache_misses),
25523197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
255311013911SAndreas Dilger 	ATTR_LIST(inode_goal),
25543197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
25553197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
25563197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
25573197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
25583197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
25593197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
256055138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
25613197ebdbSTheodore Ts'o 	NULL,
25623197ebdbSTheodore Ts'o };
25633197ebdbSTheodore Ts'o 
2564857ac889SLukas Czerner /* Features this copy of ext4 supports */
2565857ac889SLukas Czerner EXT4_INFO_ATTR(lazy_itable_init);
256627ee40dfSLukas Czerner EXT4_INFO_ATTR(batched_discard);
2567857ac889SLukas Czerner 
2568857ac889SLukas Czerner static struct attribute *ext4_feat_attrs[] = {
2569857ac889SLukas Czerner 	ATTR_LIST(lazy_itable_init),
257027ee40dfSLukas Czerner 	ATTR_LIST(batched_discard),
2571857ac889SLukas Czerner 	NULL,
2572857ac889SLukas Czerner };
2573857ac889SLukas Czerner 
25743197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
25753197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
25763197ebdbSTheodore Ts'o {
25773197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
25783197ebdbSTheodore Ts'o 						s_kobj);
25793197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
25803197ebdbSTheodore Ts'o 
25813197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
25823197ebdbSTheodore Ts'o }
25833197ebdbSTheodore Ts'o 
25843197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
25853197ebdbSTheodore Ts'o 			       struct attribute *attr,
25863197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
25873197ebdbSTheodore Ts'o {
25883197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
25893197ebdbSTheodore Ts'o 						s_kobj);
25903197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
25913197ebdbSTheodore Ts'o 
25923197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
25933197ebdbSTheodore Ts'o }
25943197ebdbSTheodore Ts'o 
25953197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
25963197ebdbSTheodore Ts'o {
25973197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
25983197ebdbSTheodore Ts'o 						s_kobj);
25993197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
26003197ebdbSTheodore Ts'o }
26013197ebdbSTheodore Ts'o 
260252cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = {
26033197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
26043197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
26053197ebdbSTheodore Ts'o };
26063197ebdbSTheodore Ts'o 
26073197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
26083197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
26093197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
26103197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
26113197ebdbSTheodore Ts'o };
26123197ebdbSTheodore Ts'o 
2613857ac889SLukas Czerner static void ext4_feat_release(struct kobject *kobj)
2614857ac889SLukas Czerner {
2615857ac889SLukas Czerner 	complete(&ext4_feat->f_kobj_unregister);
2616857ac889SLukas Czerner }
2617857ac889SLukas Czerner 
2618857ac889SLukas Czerner static struct kobj_type ext4_feat_ktype = {
2619857ac889SLukas Czerner 	.default_attrs	= ext4_feat_attrs,
2620857ac889SLukas Czerner 	.sysfs_ops	= &ext4_attr_ops,
2621857ac889SLukas Czerner 	.release	= ext4_feat_release,
2622857ac889SLukas Czerner };
2623857ac889SLukas Czerner 
2624a13fb1a4SEric Sandeen /*
2625a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2626a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2627a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2628a13fb1a4SEric Sandeen  * 0 if it cannot be.
2629a13fb1a4SEric Sandeen  */
2630a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2631a13fb1a4SEric Sandeen {
2632a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2633a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2634a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2635a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2636a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2637a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2638a13fb1a4SEric Sandeen 		return 0;
2639a13fb1a4SEric Sandeen 	}
2640a13fb1a4SEric Sandeen 
2641a13fb1a4SEric Sandeen 	if (readonly)
2642a13fb1a4SEric Sandeen 		return 1;
2643a13fb1a4SEric Sandeen 
2644a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2645a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2646a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2647a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2648a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2649a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2650a13fb1a4SEric Sandeen 		return 0;
2651a13fb1a4SEric Sandeen 	}
2652a13fb1a4SEric Sandeen 	/*
2653a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2654a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2655a13fb1a4SEric Sandeen 	 */
2656a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2657a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2658a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2659a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2660a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2661a13fb1a4SEric Sandeen 			return 0;
2662a13fb1a4SEric Sandeen 		}
2663a13fb1a4SEric Sandeen 	}
2664a13fb1a4SEric Sandeen 	return 1;
2665a13fb1a4SEric Sandeen }
2666a13fb1a4SEric Sandeen 
266766e61a9eSTheodore Ts'o /*
266866e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
266966e61a9eSTheodore Ts'o  * on the file system
267066e61a9eSTheodore Ts'o  */
267166e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
267266e61a9eSTheodore Ts'o {
267366e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
267466e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
267566e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
267666e61a9eSTheodore Ts'o 
267766e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
267866e61a9eSTheodore Ts'o 	es = sbi->s_es;
267966e61a9eSTheodore Ts'o 
268066e61a9eSTheodore Ts'o 	if (es->s_error_count)
268166e61a9eSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count: %u",
268266e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
268366e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
268466e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
268566e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
268666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
268766e61a9eSTheodore Ts'o 		       es->s_first_error_func,
268866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
268966e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
269066e61a9eSTheodore Ts'o 			printk(": inode %u",
269166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
269266e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
269366e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
269466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
269566e61a9eSTheodore Ts'o 		printk("\n");
269666e61a9eSTheodore Ts'o 	}
269766e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
269866e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
269966e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
270066e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
270166e61a9eSTheodore Ts'o 		       es->s_last_error_func,
270266e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
270366e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
270466e61a9eSTheodore Ts'o 			printk(": inode %u",
270566e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
270666e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
270766e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
270866e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
270966e61a9eSTheodore Ts'o 		printk("\n");
271066e61a9eSTheodore Ts'o 	}
271166e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
271266e61a9eSTheodore Ts'o }
271366e61a9eSTheodore Ts'o 
2714bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2715bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2716bfff6873SLukas Czerner {
2717bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2718bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2719bfff6873SLukas Czerner 	struct super_block *sb;
2720bfff6873SLukas Czerner 	unsigned long timeout = 0;
2721bfff6873SLukas Czerner 	int ret = 0;
2722bfff6873SLukas Czerner 
2723bfff6873SLukas Czerner 	sb = elr->lr_super;
2724bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2725bfff6873SLukas Czerner 
2726bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2727bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2728bfff6873SLukas Czerner 		if (!gdp) {
2729bfff6873SLukas Czerner 			ret = 1;
2730bfff6873SLukas Czerner 			break;
2731bfff6873SLukas Czerner 		}
2732bfff6873SLukas Czerner 
2733bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2734bfff6873SLukas Czerner 			break;
2735bfff6873SLukas Czerner 	}
2736bfff6873SLukas Czerner 
2737bfff6873SLukas Czerner 	if (group == ngroups)
2738bfff6873SLukas Czerner 		ret = 1;
2739bfff6873SLukas Czerner 
2740bfff6873SLukas Czerner 	if (!ret) {
2741bfff6873SLukas Czerner 		timeout = jiffies;
2742bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2743bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2744bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
274551ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
274651ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
2747bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2748bfff6873SLukas Czerner 		}
2749bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2750bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2751bfff6873SLukas Czerner 	}
2752bfff6873SLukas Czerner 
2753bfff6873SLukas Czerner 	return ret;
2754bfff6873SLukas Czerner }
2755bfff6873SLukas Czerner 
2756bfff6873SLukas Czerner /*
2757bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
27584ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
2759bfff6873SLukas Czerner  */
2760bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2761bfff6873SLukas Czerner {
2762bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2763bfff6873SLukas Czerner 
2764bfff6873SLukas Czerner 	if (!elr)
2765bfff6873SLukas Czerner 		return;
2766bfff6873SLukas Czerner 
2767bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2768bfff6873SLukas Czerner 
2769bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2770bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2771bfff6873SLukas Czerner 	kfree(elr);
2772bfff6873SLukas Czerner }
2773bfff6873SLukas Czerner 
2774bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2775bfff6873SLukas Czerner {
27761bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
27771bb933fbSLukas Czerner 	if (!ext4_li_info) {
27781bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2779bfff6873SLukas Czerner 		return;
27801bb933fbSLukas Czerner 	}
2781bfff6873SLukas Czerner 
2782bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
27831bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2784bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
27851bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2786bfff6873SLukas Czerner }
2787bfff6873SLukas Czerner 
27888f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
27898f1f7453SEric Sandeen 
2790bfff6873SLukas Czerner /*
2791bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2792bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2793bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2794bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2795bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2796bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2797bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2798bfff6873SLukas Czerner  */
2799bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2800bfff6873SLukas Czerner {
2801bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2802bfff6873SLukas Czerner 	struct list_head *pos, *n;
2803bfff6873SLukas Czerner 	struct ext4_li_request *elr;
28044ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
2805bfff6873SLukas Czerner 
2806bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2807bfff6873SLukas Czerner 
2808bfff6873SLukas Czerner cont_thread:
2809bfff6873SLukas Czerner 	while (true) {
2810bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2811bfff6873SLukas Czerner 
2812bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2813bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2814bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2815bfff6873SLukas Czerner 			goto exit_thread;
2816bfff6873SLukas Czerner 		}
2817bfff6873SLukas Czerner 
2818bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2819bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2820bfff6873SLukas Czerner 					 lr_request);
2821bfff6873SLukas Czerner 
2822b2c78cd0STheodore Ts'o 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
2823b2c78cd0STheodore Ts'o 				if (ext4_run_li_request(elr) != 0) {
2824b2c78cd0STheodore Ts'o 					/* error, remove the lazy_init job */
2825bfff6873SLukas Czerner 					ext4_remove_li_request(elr);
2826bfff6873SLukas Czerner 					continue;
2827bfff6873SLukas Czerner 				}
2828b2c78cd0STheodore Ts'o 			}
2829bfff6873SLukas Czerner 
2830bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2831bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2832bfff6873SLukas Czerner 		}
2833bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2834bfff6873SLukas Czerner 
2835bfff6873SLukas Czerner 		if (freezing(current))
2836bfff6873SLukas Czerner 			refrigerator();
2837bfff6873SLukas Czerner 
28384ed5c033SLukas Czerner 		cur = jiffies;
28394ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
2840f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2841bfff6873SLukas Czerner 			cond_resched();
2842bfff6873SLukas Czerner 			continue;
2843bfff6873SLukas Czerner 		}
2844bfff6873SLukas Czerner 
28454ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
28464ed5c033SLukas Czerner 
28478f1f7453SEric Sandeen 		if (kthread_should_stop()) {
28488f1f7453SEric Sandeen 			ext4_clear_request_list();
28498f1f7453SEric Sandeen 			goto exit_thread;
28508f1f7453SEric Sandeen 		}
2851bfff6873SLukas Czerner 	}
2852bfff6873SLukas Czerner 
2853bfff6873SLukas Czerner exit_thread:
2854bfff6873SLukas Czerner 	/*
2855bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
2856bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
2857bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
2858bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
2859bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
2860bfff6873SLukas Czerner 	 * new one.
2861bfff6873SLukas Czerner 	 */
2862bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2863bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
2864bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
2865bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2866bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2867bfff6873SLukas Czerner 		goto cont_thread;
2868bfff6873SLukas Czerner 	}
2869bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
2870bfff6873SLukas Czerner 	kfree(ext4_li_info);
2871bfff6873SLukas Czerner 	ext4_li_info = NULL;
2872bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2873bfff6873SLukas Czerner 
2874bfff6873SLukas Czerner 	return 0;
2875bfff6873SLukas Czerner }
2876bfff6873SLukas Czerner 
2877bfff6873SLukas Czerner static void ext4_clear_request_list(void)
2878bfff6873SLukas Czerner {
2879bfff6873SLukas Czerner 	struct list_head *pos, *n;
2880bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2881bfff6873SLukas Czerner 
2882bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2883bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2884bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
2885bfff6873SLukas Czerner 				 lr_request);
2886bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
2887bfff6873SLukas Czerner 	}
2888bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2889bfff6873SLukas Czerner }
2890bfff6873SLukas Czerner 
2891bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
2892bfff6873SLukas Czerner {
28938f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
28948f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
28958f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
28968f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
2897bfff6873SLukas Czerner 		ext4_clear_request_list();
2898bfff6873SLukas Czerner 		kfree(ext4_li_info);
2899bfff6873SLukas Czerner 		ext4_li_info = NULL;
2900bfff6873SLukas Czerner 		printk(KERN_CRIT "EXT4: error %d creating inode table "
2901bfff6873SLukas Czerner 				 "initialization thread\n",
2902bfff6873SLukas Czerner 				 err);
2903bfff6873SLukas Czerner 		return err;
2904bfff6873SLukas Czerner 	}
2905bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
2906bfff6873SLukas Czerner 	return 0;
2907bfff6873SLukas Czerner }
2908bfff6873SLukas Czerner 
2909bfff6873SLukas Czerner /*
2910bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
2911bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
2912bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
2913bfff6873SLukas Czerner  * groups and return total number of groups.
2914bfff6873SLukas Czerner  */
2915bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2916bfff6873SLukas Czerner {
2917bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2918bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2919bfff6873SLukas Czerner 
2920bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
2921bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2922bfff6873SLukas Czerner 		if (!gdp)
2923bfff6873SLukas Czerner 			continue;
2924bfff6873SLukas Czerner 
2925bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2926bfff6873SLukas Czerner 			break;
2927bfff6873SLukas Czerner 	}
2928bfff6873SLukas Czerner 
2929bfff6873SLukas Czerner 	return group;
2930bfff6873SLukas Czerner }
2931bfff6873SLukas Czerner 
2932bfff6873SLukas Czerner static int ext4_li_info_new(void)
2933bfff6873SLukas Czerner {
2934bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
2935bfff6873SLukas Czerner 
2936bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2937bfff6873SLukas Czerner 	if (!eli)
2938bfff6873SLukas Czerner 		return -ENOMEM;
2939bfff6873SLukas Czerner 
2940bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
2941bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
2942bfff6873SLukas Czerner 
2943bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
2944bfff6873SLukas Czerner 
2945bfff6873SLukas Czerner 	ext4_li_info = eli;
2946bfff6873SLukas Czerner 
2947bfff6873SLukas Czerner 	return 0;
2948bfff6873SLukas Czerner }
2949bfff6873SLukas Czerner 
2950bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
2951bfff6873SLukas Czerner 					    ext4_group_t start)
2952bfff6873SLukas Czerner {
2953bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2954bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2955bfff6873SLukas Czerner 	unsigned long rnd;
2956bfff6873SLukas Czerner 
2957bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
2958bfff6873SLukas Czerner 	if (!elr)
2959bfff6873SLukas Czerner 		return NULL;
2960bfff6873SLukas Czerner 
2961bfff6873SLukas Czerner 	elr->lr_super = sb;
2962bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
2963bfff6873SLukas Czerner 	elr->lr_next_group = start;
2964bfff6873SLukas Czerner 
2965bfff6873SLukas Czerner 	/*
2966bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
2967bfff6873SLukas Czerner 	 * spread the inode table initialization requests
2968bfff6873SLukas Czerner 	 * better.
2969bfff6873SLukas Czerner 	 */
2970bfff6873SLukas Czerner 	get_random_bytes(&rnd, sizeof(rnd));
2971bfff6873SLukas Czerner 	elr->lr_next_sched = jiffies + (unsigned long)rnd %
2972bfff6873SLukas Czerner 			     (EXT4_DEF_LI_MAX_START_DELAY * HZ);
2973bfff6873SLukas Czerner 
2974bfff6873SLukas Czerner 	return elr;
2975bfff6873SLukas Czerner }
2976bfff6873SLukas Czerner 
2977bfff6873SLukas Czerner static int ext4_register_li_request(struct super_block *sb,
2978bfff6873SLukas Czerner 				    ext4_group_t first_not_zeroed)
2979bfff6873SLukas Czerner {
2980bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2981bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2982bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
29836c5a6cb9SAndrew Morton 	int ret = 0;
2984bfff6873SLukas Czerner 
298551ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
298651ce6511SLukas Czerner 		/*
298751ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
298851ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
298951ce6511SLukas Czerner 		 */
299051ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
2991beed5ecbSNicolas Kaiser 		return 0;
299251ce6511SLukas Czerner 	}
2993bfff6873SLukas Czerner 
2994bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
2995bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
299655ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
2997beed5ecbSNicolas Kaiser 		return 0;
2998bfff6873SLukas Czerner 
2999bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
3000beed5ecbSNicolas Kaiser 	if (!elr)
3001beed5ecbSNicolas Kaiser 		return -ENOMEM;
3002bfff6873SLukas Czerner 
3003bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3004bfff6873SLukas Czerner 
3005bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3006bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3007bfff6873SLukas Czerner 		if (ret)
3008bfff6873SLukas Czerner 			goto out;
3009bfff6873SLukas Czerner 	}
3010bfff6873SLukas Czerner 
3011bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3012bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3013bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3014bfff6873SLukas Czerner 
3015bfff6873SLukas Czerner 	sbi->s_li_request = elr;
301646e4690bSTao Ma 	/*
301746e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
301846e4690bSTao Ma 	 * the request_list and the removal and free of it is
301946e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
302046e4690bSTao Ma 	 */
302146e4690bSTao Ma 	elr = NULL;
3022bfff6873SLukas Czerner 
3023bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3024bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3025bfff6873SLukas Czerner 		if (ret)
3026bfff6873SLukas Czerner 			goto out;
3027bfff6873SLukas Czerner 	}
3028bfff6873SLukas Czerner out:
3029bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3030beed5ecbSNicolas Kaiser 	if (ret)
3031bfff6873SLukas Czerner 		kfree(elr);
3032bfff6873SLukas Czerner 	return ret;
3033bfff6873SLukas Czerner }
3034bfff6873SLukas Czerner 
3035bfff6873SLukas Czerner /*
3036bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3037bfff6873SLukas Czerner  * module unload.
3038bfff6873SLukas Czerner  */
3039bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3040bfff6873SLukas Czerner {
3041bfff6873SLukas Czerner 	/*
3042bfff6873SLukas Czerner 	 * If thread exited earlier
3043bfff6873SLukas Czerner 	 * there's nothing to be done.
3044bfff6873SLukas Czerner 	 */
30458f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3046bfff6873SLukas Czerner 		return;
3047bfff6873SLukas Czerner 
30488f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3049bfff6873SLukas Czerner }
3050bfff6873SLukas Czerner 
3051617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
30527477827fSAneesh Kumar K.V 				__releases(kernel_lock)
30537477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
3054ac27a0ecSDave Kleikamp {
3055d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3056ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3057617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
3058617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
3059617ba13bSMingming Cao 	ext4_fsblk_t block;
3060617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
306170bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3062ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3063ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3064ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3065ac27a0ecSDave Kleikamp 	struct inode *root;
30669f6200bbSTheodore Ts'o 	char *cp;
30670390131bSFrank Mayhar 	const char *descr;
3068dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3069ac27a0ecSDave Kleikamp 	int blocksize;
30704ec11028STheodore Ts'o 	unsigned int db_count;
30714ec11028STheodore Ts'o 	unsigned int i;
3072f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
3073bd81d8eeSLaurent Vivier 	__u64 blocks_count;
3074833f4077SPeter Zijlstra 	int err;
3075b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3076bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3077ac27a0ecSDave Kleikamp 
3078ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3079ac27a0ecSDave Kleikamp 	if (!sbi)
3080dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3081705895b6SPekka Enberg 
3082705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3083705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3084705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
3085705895b6SPekka Enberg 		kfree(sbi);
3086dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3087705895b6SPekka Enberg 	}
3088ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
3089ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
3090617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
3091617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
3092240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3093d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3094f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3095f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3096f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3097ac27a0ecSDave Kleikamp 
30989f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
30999f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
31009f6200bbSTheodore Ts'o 		*cp = '!';
31019f6200bbSTheodore Ts'o 
3102dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3103617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3104ac27a0ecSDave Kleikamp 	if (!blocksize) {
3105b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3106ac27a0ecSDave Kleikamp 		goto out_fail;
3107ac27a0ecSDave Kleikamp 	}
3108ac27a0ecSDave Kleikamp 
3109ac27a0ecSDave Kleikamp 	/*
3110617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3111ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3112ac27a0ecSDave Kleikamp 	 */
3113617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
311470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
311570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3116ac27a0ecSDave Kleikamp 	} else {
311770bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3118ac27a0ecSDave Kleikamp 	}
3119ac27a0ecSDave Kleikamp 
312070bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
3121b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3122ac27a0ecSDave Kleikamp 		goto out_fail;
3123ac27a0ecSDave Kleikamp 	}
3124ac27a0ecSDave Kleikamp 	/*
3125ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3126617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3127ac27a0ecSDave Kleikamp 	 */
3128617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3129ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3130ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3131617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3132617ba13bSMingming Cao 		goto cantfind_ext4;
3133afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3134ac27a0ecSDave Kleikamp 
3135ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3136ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3137fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3138617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3139fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
3140437ca0fdSDmitry Monakhov 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
3141437ca0fdSDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
3142437ca0fdSDmitry Monakhov 			"2.6.38");
3143fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3144437ca0fdSDmitry Monakhov 	}
3145617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3146fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3147ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
314803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
3149fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
31502e7842b8SHugh Dickins #endif
315103010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3152fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
31532e7842b8SHugh Dickins #endif
31546fd7a467STheodore Ts'o 	set_opt(sb, MBLK_IO_SUBMIT);
3155617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3156fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3157617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3158fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3159617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3160fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3161ac27a0ecSDave Kleikamp 
3162617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3163fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3164bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3165fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3166bb4f397aSAneesh Kumar K.V 	else
3167fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
31688b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
3169fd8c37ecSTheodore Ts'o 		set_opt(sb, BLOCK_VALIDITY);
31708b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3171fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3172ac27a0ecSDave Kleikamp 
3173ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
3174ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
317530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
317630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
317730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3178ac27a0ecSDave Kleikamp 
31798b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3180fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3181ac27a0ecSDave Kleikamp 
31821e2462f9SMingming Cao 	/*
3183dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3184dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3185dd919b98SAneesh Kumar K.V 	 */
31868b67f04aSTheodore Ts'o 	if (!IS_EXT3_SB(sb) &&
31878b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3188fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3189dd919b98SAneesh Kumar K.V 
319051ce6511SLukas Czerner 	/*
319151ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
319251ce6511SLukas Czerner 	 * no mount option specified.
319351ce6511SLukas Czerner 	 */
319451ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
319551ce6511SLukas Czerner 
31968b67f04aSTheodore Ts'o 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
31978b67f04aSTheodore Ts'o 			   &journal_devnum, &journal_ioprio, NULL, 0)) {
31988b67f04aSTheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
31998b67f04aSTheodore Ts'o 			 "failed to parse options in superblock: %s",
32008b67f04aSTheodore Ts'o 			 sbi->s_es->s_mount_opts);
32018b67f04aSTheodore Ts'o 	}
3202b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3203b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
3204ac27a0ecSDave Kleikamp 		goto failed_mount;
3205ac27a0ecSDave Kleikamp 
3206ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3207482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3208ac27a0ecSDave Kleikamp 
3209617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3210617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3211617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3212617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
3213b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3214b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3215b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3216469108ffSTheodore Tso 
32172035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
32182035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
32192035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
32202035e776STheodore Ts'o 				 "using the ext4 subsystem");
32212035e776STheodore Ts'o 		else {
32222035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
32232035e776STheodore Ts'o 				 "to feature incompatibilities");
32242035e776STheodore Ts'o 			goto failed_mount;
32252035e776STheodore Ts'o 		}
32262035e776STheodore Ts'o 	}
32272035e776STheodore Ts'o 
32282035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
32292035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
32302035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
32312035e776STheodore Ts'o 				 "using the ext4 subsystem");
32322035e776STheodore Ts'o 		else {
32332035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
32342035e776STheodore Ts'o 				 "to feature incompatibilities");
32352035e776STheodore Ts'o 			goto failed_mount;
32362035e776STheodore Ts'o 		}
32372035e776STheodore Ts'o 	}
32382035e776STheodore Ts'o 
3239469108ffSTheodore Tso 	/*
3240ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3241ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3242ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3243ac27a0ecSDave Kleikamp 	 */
3244a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3245ac27a0ecSDave Kleikamp 		goto failed_mount;
3246a13fb1a4SEric Sandeen 
3247ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3248ac27a0ecSDave Kleikamp 
3249617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3250617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3251b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3252b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
3253ac27a0ecSDave Kleikamp 		goto failed_mount;
3254ac27a0ecSDave Kleikamp 	}
3255ac27a0ecSDave Kleikamp 
3256ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3257ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3258ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3259b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3260ce40733cSAneesh Kumar K.V 					blocksize);
3261ac27a0ecSDave Kleikamp 			goto failed_mount;
3262ac27a0ecSDave Kleikamp 		}
3263ac27a0ecSDave Kleikamp 
3264ac27a0ecSDave Kleikamp 		brelse(bh);
326570bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
326670bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
326770bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
3268ac27a0ecSDave Kleikamp 		if (!bh) {
3269b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3270b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3271ac27a0ecSDave Kleikamp 			goto failed_mount;
3272ac27a0ecSDave Kleikamp 		}
3273617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
3274ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3275617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3276b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3277b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3278ac27a0ecSDave Kleikamp 			goto failed_mount;
3279ac27a0ecSDave Kleikamp 		}
3280ac27a0ecSDave Kleikamp 	}
3281ac27a0ecSDave Kleikamp 
3282a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3283a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
3284f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3285f287a1a5STheodore Ts'o 						      has_huge_files);
3286f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3287ac27a0ecSDave Kleikamp 
3288617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3289617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3290617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3291ac27a0ecSDave Kleikamp 	} else {
3292ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3293ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3294617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
32951330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3296ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3297b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3298b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3299ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3300ac27a0ecSDave Kleikamp 			goto failed_mount;
3301ac27a0ecSDave Kleikamp 		}
3302ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3303ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3304ac27a0ecSDave Kleikamp 	}
33050b8e58a1SAndreas Dilger 
33060d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
33070d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
33088fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
33090d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3310d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3311b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3312b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
33130d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
33140d1ee42fSAlexandre Ratchov 			goto failed_mount;
33150d1ee42fSAlexandre Ratchov 		}
33160d1ee42fSAlexandre Ratchov 	} else
33170d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
33180b8e58a1SAndreas Dilger 
3319ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3320ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3321b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
3322617ba13bSMingming Cao 		goto cantfind_ext4;
33230b8e58a1SAndreas Dilger 
3324617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3325ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3326617ba13bSMingming Cao 		goto cantfind_ext4;
3327ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3328ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
33290d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3330ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3331ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3332e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3333e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
33340b8e58a1SAndreas Dilger 
3335ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3336ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3337ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3338f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
3339f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
3340f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3341f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3342f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
3343f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3344f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3345f99b2589STheodore Ts'o #else
3346f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3347f99b2589STheodore Ts'o #endif
3348f99b2589STheodore Ts'o 		sb->s_dirt = 1;
3349f99b2589STheodore Ts'o 	}
3350ac27a0ecSDave Kleikamp 
3351ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
3352b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3353b31e1552SEric Sandeen 		       "#blocks per group too big: %lu",
3354ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
3355ac27a0ecSDave Kleikamp 		goto failed_mount;
3356ac27a0ecSDave Kleikamp 	}
3357ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
3358b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3359b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
3360ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
3361ac27a0ecSDave Kleikamp 		goto failed_mount;
3362ac27a0ecSDave Kleikamp 	}
3363ac27a0ecSDave Kleikamp 
3364bf43d84bSEric Sandeen 	/*
3365bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3366bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3367bf43d84bSEric Sandeen 	 */
33685a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
336930ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
33705a9ae68aSDarrick J. Wong 	if (err) {
3371b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3372bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3373ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
337490c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
33755a9ae68aSDarrick J. Wong 		ret = err;
3376ac27a0ecSDave Kleikamp 		goto failed_mount;
3377ac27a0ecSDave Kleikamp 	}
3378ac27a0ecSDave Kleikamp 
3379617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3380617ba13bSMingming Cao 		goto cantfind_ext4;
3381e7c95593SEric Sandeen 
33820f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
33830f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
33840f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3385b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3386b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
33870f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
33880f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
33890f2ddca6SFrom: Thiemo Nagel 	}
33900f2ddca6SFrom: Thiemo Nagel 
33914ec11028STheodore Ts'o 	/*
33924ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
33934ec11028STheodore Ts'o 	 * of the filesystem.
33944ec11028STheodore Ts'o 	 */
33954ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3396b31e1552SEric Sandeen                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
3397b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3398e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
33994ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3400e7c95593SEric Sandeen 		goto failed_mount;
3401e7c95593SEric Sandeen 	}
3402bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3403bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3404bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3405bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
34064ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3407b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
34084ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3409b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
34104ec11028STheodore Ts'o 		       ext4_blocks_count(es),
34114ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
34124ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
34134ec11028STheodore Ts'o 		goto failed_mount;
34144ec11028STheodore Ts'o 	}
3415bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3416fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3417fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3418617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3419617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3420ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
3421ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
3422ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3423b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
3424ac27a0ecSDave Kleikamp 		goto failed_mount;
3425ac27a0ecSDave Kleikamp 	}
3426ac27a0ecSDave Kleikamp 
34273244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
34289f6200bbSTheodore Ts'o 	if (ext4_proc_root)
34299f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
34303244fcb1SAlexander Beregalov #endif
3431240799cdSTheodore Ts'o 
3432705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3433ac27a0ecSDave Kleikamp 
3434ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
343570bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3436ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
3437ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3438b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3439b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3440ac27a0ecSDave Kleikamp 			db_count = i;
3441ac27a0ecSDave Kleikamp 			goto failed_mount2;
3442ac27a0ecSDave Kleikamp 		}
3443ac27a0ecSDave Kleikamp 	}
3444bfff6873SLukas Czerner 	if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
3445b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
3446ac27a0ecSDave Kleikamp 		goto failed_mount2;
3447ac27a0ecSDave Kleikamp 	}
3448772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
3449772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
3450b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3451b31e1552SEric Sandeen 			       "unable to initialize "
3452b31e1552SEric Sandeen 			       "flex_bg meta info!");
3453772cb7c8SJose R. Santos 			goto failed_mount2;
3454772cb7c8SJose R. Santos 		}
3455772cb7c8SJose R. Santos 
3456ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
3457ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3458ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3459ac27a0ecSDave Kleikamp 
346004496411STao Ma 	init_timer(&sbi->s_err_report);
346104496411STao Ma 	sbi->s_err_report.function = print_daily_error_info;
346204496411STao Ma 	sbi->s_err_report.data = (unsigned long) sb;
346304496411STao Ma 
3464ce7e010aSTheodore Ts'o 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
3465ce7e010aSTheodore Ts'o 			ext4_count_free_blocks(sb));
3466ce7e010aSTheodore Ts'o 	if (!err) {
3467ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
3468ce7e010aSTheodore Ts'o 				ext4_count_free_inodes(sb));
3469ce7e010aSTheodore Ts'o 	}
3470ce7e010aSTheodore Ts'o 	if (!err) {
3471ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
3472ce7e010aSTheodore Ts'o 				ext4_count_dirs(sb));
3473ce7e010aSTheodore Ts'o 	}
3474ce7e010aSTheodore Ts'o 	if (!err) {
3475ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
3476ce7e010aSTheodore Ts'o 	}
3477ce7e010aSTheodore Ts'o 	if (err) {
3478ce7e010aSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
3479ce7e010aSTheodore Ts'o 		goto failed_mount3;
3480ce7e010aSTheodore Ts'o 	}
3481ce7e010aSTheodore Ts'o 
3482c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
348355138e0bSTheodore Ts'o 	sbi->s_max_writeback_mb_bump = 128;
3484c9de560dSAlex Tomas 
3485ac27a0ecSDave Kleikamp 	/*
3486ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
3487ac27a0ecSDave Kleikamp 	 */
34889ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
34899ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
3490617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
34919ca92389STheodore Ts'o 	else
34929ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
3493617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3494617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3495ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3496617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
3497617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
3498ac27a0ecSDave Kleikamp #endif
3499f2fa2ffcSAneesh Kumar K.V 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3500f2fa2ffcSAneesh Kumar K.V 
3501ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
35023b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
3503*8f82f840SYongqiang Yang 	sbi->s_resize_flags = 0;
3504ac27a0ecSDave Kleikamp 
3505ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3506ac27a0ecSDave Kleikamp 
3507ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3508617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
3509617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
3510ac27a0ecSDave Kleikamp 
3511c5e06d10SJohann Lombardi 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
3512c5e06d10SJohann Lombardi 	    !(sb->s_flags & MS_RDONLY))
3513c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
3514c5e06d10SJohann Lombardi 			goto failed_mount3;
3515c5e06d10SJohann Lombardi 
3516ac27a0ecSDave Kleikamp 	/*
3517ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3518ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3519ac27a0ecSDave Kleikamp 	 */
3520ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
3521617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3522617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
3523ac27a0ecSDave Kleikamp 			goto failed_mount3;
35240390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
35250390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3526b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3527b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3528744692dcSJiaying Zhang 		goto failed_mount_wq;
3529ac27a0ecSDave Kleikamp 	} else {
3530fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
35310390131bSFrank Mayhar 		sbi->s_journal = NULL;
35320390131bSFrank Mayhar 		needs_recovery = 0;
35330390131bSFrank Mayhar 		goto no_journal;
3534ac27a0ecSDave Kleikamp 	}
3535ac27a0ecSDave Kleikamp 
3536eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
3537eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3538eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3539b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3540744692dcSJiaying Zhang 		goto failed_mount_wq;
3541eb40a09cSJose R. Santos 	}
3542eb40a09cSJose R. Santos 
3543d4da6c9cSLinus Torvalds 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3544d4da6c9cSLinus Torvalds 		jbd2_journal_set_features(sbi->s_journal,
3545d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3546d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3547d4da6c9cSLinus Torvalds 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3548818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
3549818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
3550818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3551818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3552d4da6c9cSLinus Torvalds 	} else {
3553d4da6c9cSLinus Torvalds 		jbd2_journal_clear_features(sbi->s_journal,
3554d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3555d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3556d4da6c9cSLinus Torvalds 	}
3557818d276cSGirish Shilamkar 
3558ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3559ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3560ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3561ac27a0ecSDave Kleikamp 	case 0:
3562ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
356363f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
356463f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
356563f57933SAndrew Morton 		 */
3566dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3567dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3568fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3569ac27a0ecSDave Kleikamp 		else
3570fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3571ac27a0ecSDave Kleikamp 		break;
3572ac27a0ecSDave Kleikamp 
3573617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
3574617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
3575dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
3576dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
3577b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
3578b31e1552SEric Sandeen 			       "requested data journaling mode");
3579744692dcSJiaying Zhang 			goto failed_mount_wq;
3580ac27a0ecSDave Kleikamp 		}
3581ac27a0ecSDave Kleikamp 	default:
3582ac27a0ecSDave Kleikamp 		break;
3583ac27a0ecSDave Kleikamp 	}
3584b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3585ac27a0ecSDave Kleikamp 
3586ce7e010aSTheodore Ts'o 	/*
3587ce7e010aSTheodore Ts'o 	 * The journal may have updated the bg summary counts, so we
3588ce7e010aSTheodore Ts'o 	 * need to update the global counters.
3589ce7e010aSTheodore Ts'o 	 */
3590ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_freeblocks_counter,
359184061e07SDmitry Monakhov 			   ext4_count_free_blocks(sb));
3592ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_freeinodes_counter,
359384061e07SDmitry Monakhov 			   ext4_count_free_inodes(sb));
3594ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_dirs_counter,
359584061e07SDmitry Monakhov 			   ext4_count_dirs(sb));
3596ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_dirtyblocks_counter, 0);
3597206f7ab4SChristoph Hellwig 
3598ce7e010aSTheodore Ts'o no_journal:
3599fd89d5f2STejun Heo 	/*
3600fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
3601fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
3602fd89d5f2STejun Heo 	 */
3603fd89d5f2STejun Heo 	EXT4_SB(sb)->dio_unwritten_wq =
3604ae005cbeSLinus Torvalds 		alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
36054c0425ffSMingming Cao 	if (!EXT4_SB(sb)->dio_unwritten_wq) {
36064c0425ffSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
36074c0425ffSMingming Cao 		goto failed_mount_wq;
36084c0425ffSMingming Cao 	}
36094c0425ffSMingming Cao 
3610ac27a0ecSDave Kleikamp 	/*
3611dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
3612ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
3613ac27a0ecSDave Kleikamp 	 */
3614ac27a0ecSDave Kleikamp 
36151d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
36161d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
3617b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
36181d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
361932a9bb57SManish Katiyar 		root = NULL;
3620ac27a0ecSDave Kleikamp 		goto failed_mount4;
3621ac27a0ecSDave Kleikamp 	}
3622ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
3623b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
3624ac27a0ecSDave Kleikamp 		goto failed_mount4;
3625ac27a0ecSDave Kleikamp 	}
36261d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
36271d1fe1eeSDavid Howells 	if (!sb->s_root) {
3628b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
36291d1fe1eeSDavid Howells 		ret = -ENOMEM;
36301d1fe1eeSDavid Howells 		goto failed_mount4;
36311d1fe1eeSDavid Howells 	}
3632ac27a0ecSDave Kleikamp 
3633617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
3634ef7f3835SKalpak Shah 
3635ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
3636ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3637ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3638ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
3639ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3640ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
3641ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3642ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
3643ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3644ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
3645ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3646ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
3647ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3648ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
3649ef7f3835SKalpak Shah 		}
3650ef7f3835SKalpak Shah 	}
3651ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
3652ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3653ef7f3835SKalpak Shah 							sbi->s_inode_size) {
3654ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3655ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
3656b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
3657b31e1552SEric Sandeen 			 "available");
3658ef7f3835SKalpak Shah 	}
3659ef7f3835SKalpak Shah 
366090576c0bSTheodore Ts'o 	if (test_opt(sb, DELALLOC) &&
366190576c0bSTheodore Ts'o 	    (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
3662b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
3663b31e1552SEric Sandeen 			 "requested data journaling mode");
3664fd8c37ecSTheodore Ts'o 		clear_opt(sb, DELALLOC);
366590576c0bSTheodore Ts'o 	}
3666744692dcSJiaying Zhang 	if (test_opt(sb, DIOREAD_NOLOCK)) {
3667744692dcSJiaying Zhang 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
3668744692dcSJiaying Zhang 			ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
3669744692dcSJiaying Zhang 				"option - requested data journaling mode");
3670fd8c37ecSTheodore Ts'o 			clear_opt(sb, DIOREAD_NOLOCK);
3671744692dcSJiaying Zhang 		}
3672744692dcSJiaying Zhang 		if (sb->s_blocksize < PAGE_SIZE) {
3673744692dcSJiaying Zhang 			ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
3674744692dcSJiaying Zhang 				"option - block size is too small");
3675fd8c37ecSTheodore Ts'o 			clear_opt(sb, DIOREAD_NOLOCK);
3676744692dcSJiaying Zhang 		}
3677744692dcSJiaying Zhang 	}
3678c2774d84SAneesh Kumar K.V 
36796fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
36806fd058f7STheodore Ts'o 	if (err) {
3681b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
3682fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
36836fd058f7STheodore Ts'o 		goto failed_mount4;
36846fd058f7STheodore Ts'o 	}
36856fd058f7STheodore Ts'o 
3686c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
3687c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
3688c2774d84SAneesh Kumar K.V 	if (err) {
3689421f91d2SUwe Kleine-König 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
3690c2774d84SAneesh Kumar K.V 			 err);
3691c2774d84SAneesh Kumar K.V 		goto failed_mount4;
3692c2774d84SAneesh Kumar K.V 	}
3693c2774d84SAneesh Kumar K.V 
3694bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
3695bfff6873SLukas Czerner 	if (err)
3696bfff6873SLukas Czerner 		goto failed_mount4;
3697bfff6873SLukas Czerner 
36983197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
36993197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
37003197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
37013197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
37023197ebdbSTheodore Ts'o 	if (err) {
37033197ebdbSTheodore Ts'o 		ext4_mb_release(sb);
37043197ebdbSTheodore Ts'o 		ext4_ext_release(sb);
37053197ebdbSTheodore Ts'o 		goto failed_mount4;
37063197ebdbSTheodore Ts'o 	};
37073197ebdbSTheodore Ts'o 
3708617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3709617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
3710617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
37110390131bSFrank Mayhar 	if (needs_recovery) {
3712b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
3713617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
37140390131bSFrank Mayhar 	}
37150390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
37160390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
37170390131bSFrank Mayhar 			descr = " journalled data mode";
37180390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
37190390131bSFrank Mayhar 			descr = " ordered data mode";
37200390131bSFrank Mayhar 		else
37210390131bSFrank Mayhar 			descr = " writeback data mode";
37220390131bSFrank Mayhar 	} else
37230390131bSFrank Mayhar 		descr = "out journal";
37240390131bSFrank Mayhar 
3725d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
37268b67f04aSTheodore Ts'o 		 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
37278b67f04aSTheodore Ts'o 		 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
3728ac27a0ecSDave Kleikamp 
372966e61a9eSTheodore Ts'o 	if (es->s_error_count)
373066e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
3731ac27a0ecSDave Kleikamp 
3732d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
3733ac27a0ecSDave Kleikamp 	return 0;
3734ac27a0ecSDave Kleikamp 
3735617ba13bSMingming Cao cantfind_ext4:
3736ac27a0ecSDave Kleikamp 	if (!silent)
3737b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
3738ac27a0ecSDave Kleikamp 	goto failed_mount;
3739ac27a0ecSDave Kleikamp 
3740ac27a0ecSDave Kleikamp failed_mount4:
374132a9bb57SManish Katiyar 	iput(root);
374232a9bb57SManish Katiyar 	sb->s_root = NULL;
3743b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
37444c0425ffSMingming Cao 	destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
37454c0425ffSMingming Cao failed_mount_wq:
37466fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
37470390131bSFrank Mayhar 	if (sbi->s_journal) {
3748dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
374947b4a50bSJan Kara 		sbi->s_journal = NULL;
37500390131bSFrank Mayhar 	}
3751ac27a0ecSDave Kleikamp failed_mount3:
375204496411STao Ma 	del_timer(&sbi->s_err_report);
3753c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups) {
3754c5ca7c76STheodore Ts'o 		if (is_vmalloc_addr(sbi->s_flex_groups))
3755c5ca7c76STheodore Ts'o 			vfree(sbi->s_flex_groups);
3756c5ca7c76STheodore Ts'o 		else
3757c5ca7c76STheodore Ts'o 			kfree(sbi->s_flex_groups);
3758c5ca7c76STheodore Ts'o 	}
3759ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
3760ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
3761ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
3762ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
3763c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
3764c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
3765ac27a0ecSDave Kleikamp failed_mount2:
3766ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
3767ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
3768ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
3769ac27a0ecSDave Kleikamp failed_mount:
3770240799cdSTheodore Ts'o 	if (sbi->s_proc) {
37719f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
3772240799cdSTheodore Ts'o 	}
3773ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3774ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3775ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
3776ac27a0ecSDave Kleikamp #endif
3777617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
3778ac27a0ecSDave Kleikamp 	brelse(bh);
3779ac27a0ecSDave Kleikamp out_fail:
3780ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
3781f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
3782ac27a0ecSDave Kleikamp 	kfree(sbi);
3783dcc7dae3SCyrill Gorcunov out_free_orig:
3784d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
37851d1fe1eeSDavid Howells 	return ret;
3786ac27a0ecSDave Kleikamp }
3787ac27a0ecSDave Kleikamp 
3788ac27a0ecSDave Kleikamp /*
3789ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
3790ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
3791ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
3792ac27a0ecSDave Kleikamp  */
3793617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
3794ac27a0ecSDave Kleikamp {
3795617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3796ac27a0ecSDave Kleikamp 
3797ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
379830773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
379930773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
3800ac27a0ecSDave Kleikamp 
3801a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
3802ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
3803dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
3804ac27a0ecSDave Kleikamp 	else
3805dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
38065bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
38075bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
38085bf5683aSHidehiro Kawai 	else
38095bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
3810a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
3811ac27a0ecSDave Kleikamp }
3812ac27a0ecSDave Kleikamp 
3813617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
3814ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
3815ac27a0ecSDave Kleikamp {
3816ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
3817ac27a0ecSDave Kleikamp 	journal_t *journal;
3818ac27a0ecSDave Kleikamp 
38190390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
38200390131bSFrank Mayhar 
3821ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
3822ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
3823ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
3824ac27a0ecSDave Kleikamp 
38251d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
38261d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
3827b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
3828ac27a0ecSDave Kleikamp 		return NULL;
3829ac27a0ecSDave Kleikamp 	}
3830ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
3831ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
3832ac27a0ecSDave Kleikamp 		iput(journal_inode);
3833b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3834ac27a0ecSDave Kleikamp 		return NULL;
3835ac27a0ecSDave Kleikamp 	}
3836ac27a0ecSDave Kleikamp 
3837e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3838ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
38391d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
3840b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
3841ac27a0ecSDave Kleikamp 		iput(journal_inode);
3842ac27a0ecSDave Kleikamp 		return NULL;
3843ac27a0ecSDave Kleikamp 	}
3844ac27a0ecSDave Kleikamp 
3845dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
3846ac27a0ecSDave Kleikamp 	if (!journal) {
3847b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3848ac27a0ecSDave Kleikamp 		iput(journal_inode);
3849ac27a0ecSDave Kleikamp 		return NULL;
3850ac27a0ecSDave Kleikamp 	}
3851ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3852617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3853ac27a0ecSDave Kleikamp 	return journal;
3854ac27a0ecSDave Kleikamp }
3855ac27a0ecSDave Kleikamp 
3856617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
3857ac27a0ecSDave Kleikamp 				       dev_t j_dev)
3858ac27a0ecSDave Kleikamp {
3859ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3860ac27a0ecSDave Kleikamp 	journal_t *journal;
3861617ba13bSMingming Cao 	ext4_fsblk_t start;
3862617ba13bSMingming Cao 	ext4_fsblk_t len;
3863ac27a0ecSDave Kleikamp 	int hblock, blocksize;
3864617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
3865ac27a0ecSDave Kleikamp 	unsigned long offset;
3866617ba13bSMingming Cao 	struct ext4_super_block *es;
3867ac27a0ecSDave Kleikamp 	struct block_device *bdev;
3868ac27a0ecSDave Kleikamp 
38690390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
38700390131bSFrank Mayhar 
3871b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
3872ac27a0ecSDave Kleikamp 	if (bdev == NULL)
3873ac27a0ecSDave Kleikamp 		return NULL;
3874ac27a0ecSDave Kleikamp 
3875ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
3876e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
3877ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
3878b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3879b31e1552SEric Sandeen 			"blocksize too small for journal device");
3880ac27a0ecSDave Kleikamp 		goto out_bdev;
3881ac27a0ecSDave Kleikamp 	}
3882ac27a0ecSDave Kleikamp 
3883617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3884617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3885ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3886ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3887b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3888b31e1552SEric Sandeen 		       "external journal");
3889ac27a0ecSDave Kleikamp 		goto out_bdev;
3890ac27a0ecSDave Kleikamp 	}
3891ac27a0ecSDave Kleikamp 
3892617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3893617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3894ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3895617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3896b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
3897b31e1552SEric Sandeen 					"bad superblock");
3898ac27a0ecSDave Kleikamp 		brelse(bh);
3899ac27a0ecSDave Kleikamp 		goto out_bdev;
3900ac27a0ecSDave Kleikamp 	}
3901ac27a0ecSDave Kleikamp 
3902617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3903b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3904ac27a0ecSDave Kleikamp 		brelse(bh);
3905ac27a0ecSDave Kleikamp 		goto out_bdev;
3906ac27a0ecSDave Kleikamp 	}
3907ac27a0ecSDave Kleikamp 
3908bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
3909ac27a0ecSDave Kleikamp 	start = sb_block + 1;
3910ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
3911ac27a0ecSDave Kleikamp 
3912dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3913ac27a0ecSDave Kleikamp 					start, len, blocksize);
3914ac27a0ecSDave Kleikamp 	if (!journal) {
3915b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
3916ac27a0ecSDave Kleikamp 		goto out_bdev;
3917ac27a0ecSDave Kleikamp 	}
3918ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3919ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
3920ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
3921ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
3922b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3923ac27a0ecSDave Kleikamp 		goto out_journal;
3924ac27a0ecSDave Kleikamp 	}
3925ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3926b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
3927b31e1552SEric Sandeen 					"user (unsupported) - %d",
3928ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
3929ac27a0ecSDave Kleikamp 		goto out_journal;
3930ac27a0ecSDave Kleikamp 	}
3931617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
3932617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3933ac27a0ecSDave Kleikamp 	return journal;
39340b8e58a1SAndreas Dilger 
3935ac27a0ecSDave Kleikamp out_journal:
3936dab291afSMingming Cao 	jbd2_journal_destroy(journal);
3937ac27a0ecSDave Kleikamp out_bdev:
3938617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
3939ac27a0ecSDave Kleikamp 	return NULL;
3940ac27a0ecSDave Kleikamp }
3941ac27a0ecSDave Kleikamp 
3942617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
3943617ba13bSMingming Cao 			     struct ext4_super_block *es,
3944ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
3945ac27a0ecSDave Kleikamp {
3946ac27a0ecSDave Kleikamp 	journal_t *journal;
3947ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3948ac27a0ecSDave Kleikamp 	dev_t journal_dev;
3949ac27a0ecSDave Kleikamp 	int err = 0;
3950ac27a0ecSDave Kleikamp 	int really_read_only;
3951ac27a0ecSDave Kleikamp 
39520390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
39530390131bSFrank Mayhar 
3954ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3955ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3956b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3957b31e1552SEric Sandeen 			"numbers have changed");
3958ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
3959ac27a0ecSDave Kleikamp 	} else
3960ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3961ac27a0ecSDave Kleikamp 
3962ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
3963ac27a0ecSDave Kleikamp 
3964ac27a0ecSDave Kleikamp 	/*
3965ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
3966ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
3967ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
3968ac27a0ecSDave Kleikamp 	 */
3969617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3970ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
3971b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
3972b31e1552SEric Sandeen 					"required on readonly filesystem");
3973ac27a0ecSDave Kleikamp 			if (really_read_only) {
3974b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
3975b31e1552SEric Sandeen 					"unavailable, cannot proceed");
3976ac27a0ecSDave Kleikamp 				return -EROFS;
3977ac27a0ecSDave Kleikamp 			}
3978b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
3979b31e1552SEric Sandeen 			       "be enabled during recovery");
3980ac27a0ecSDave Kleikamp 		}
3981ac27a0ecSDave Kleikamp 	}
3982ac27a0ecSDave Kleikamp 
3983ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
3984b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3985b31e1552SEric Sandeen 		       "and inode journals!");
3986ac27a0ecSDave Kleikamp 		return -EINVAL;
3987ac27a0ecSDave Kleikamp 	}
3988ac27a0ecSDave Kleikamp 
3989ac27a0ecSDave Kleikamp 	if (journal_inum) {
3990617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
3991ac27a0ecSDave Kleikamp 			return -EINVAL;
3992ac27a0ecSDave Kleikamp 	} else {
3993617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3994ac27a0ecSDave Kleikamp 			return -EINVAL;
3995ac27a0ecSDave Kleikamp 	}
3996ac27a0ecSDave Kleikamp 
399790576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
3998b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
39994776004fSTheodore Ts'o 
4000ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
4001dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
4002ac27a0ecSDave Kleikamp 		if (err)  {
4003b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error updating journal");
4004dab291afSMingming Cao 			jbd2_journal_destroy(journal);
4005ac27a0ecSDave Kleikamp 			return err;
4006ac27a0ecSDave Kleikamp 		}
4007ac27a0ecSDave Kleikamp 	}
4008ac27a0ecSDave Kleikamp 
4009617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
4010dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
40111c13d5c0STheodore Ts'o 	if (!err) {
40121c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
40131c13d5c0STheodore Ts'o 		if (save)
40141c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
40151c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4016dab291afSMingming Cao 		err = jbd2_journal_load(journal);
40171c13d5c0STheodore Ts'o 		if (save)
40181c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
40191c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
40201c13d5c0STheodore Ts'o 		kfree(save);
40211c13d5c0STheodore Ts'o 	}
4022ac27a0ecSDave Kleikamp 
4023ac27a0ecSDave Kleikamp 	if (err) {
4024b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4025dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4026ac27a0ecSDave Kleikamp 		return err;
4027ac27a0ecSDave Kleikamp 	}
4028ac27a0ecSDave Kleikamp 
4029617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4030617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4031ac27a0ecSDave Kleikamp 
4032c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4033ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4034ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4035ac27a0ecSDave Kleikamp 
4036ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4037e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4038ac27a0ecSDave Kleikamp 	}
4039ac27a0ecSDave Kleikamp 
4040ac27a0ecSDave Kleikamp 	return 0;
4041ac27a0ecSDave Kleikamp }
4042ac27a0ecSDave Kleikamp 
4043e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4044ac27a0ecSDave Kleikamp {
4045e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4046617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4047c4be0c1dSTakashi Sato 	int error = 0;
4048ac27a0ecSDave Kleikamp 
4049ac27a0ecSDave Kleikamp 	if (!sbh)
4050c4be0c1dSTakashi Sato 		return error;
4051914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
4052914258bfSTheodore Ts'o 		/*
4053914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
4054914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
4055914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
4056914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
4057914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
4058914258bfSTheodore Ts'o 		 * write and hope for the best.
4059914258bfSTheodore Ts'o 		 */
4060b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
4061b31e1552SEric Sandeen 		       "superblock detected");
4062914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
4063914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
4064914258bfSTheodore Ts'o 	}
406571290b36STheodore Ts'o 	/*
406671290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
406771290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
406871290b36STheodore Ts'o 	 * write time when we are mounting the root file system
406971290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
407071290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
407171290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
407271290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
407371290b36STheodore Ts'o 	 * to complain and force a full file system check.
407471290b36STheodore Ts'o 	 */
407571290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
4076ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
4077f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4078afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4079afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4080afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4081afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4082f613dfcbSTheodore Ts'o 	else
4083f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4084f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
40855d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
40865d1b1b3fSAneesh Kumar K.V 					   &EXT4_SB(sb)->s_freeblocks_counter));
40877f93cff9STheodore Ts'o 	es->s_free_inodes_count =
40887f93cff9STheodore Ts'o 		cpu_to_le32(percpu_counter_sum_positive(
40895d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
40907234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
4091ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
4092ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
4093914258bfSTheodore Ts'o 	if (sync) {
4094c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
4095c4be0c1dSTakashi Sato 		if (error)
4096c4be0c1dSTakashi Sato 			return error;
4097c4be0c1dSTakashi Sato 
4098c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4099c4be0c1dSTakashi Sato 		if (error) {
4100b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4101b31e1552SEric Sandeen 			       "superblock");
4102914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4103914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4104914258bfSTheodore Ts'o 		}
4105914258bfSTheodore Ts'o 	}
4106c4be0c1dSTakashi Sato 	return error;
4107ac27a0ecSDave Kleikamp }
4108ac27a0ecSDave Kleikamp 
4109ac27a0ecSDave Kleikamp /*
4110ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4111ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4112ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4113ac27a0ecSDave Kleikamp  */
4114617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4115617ba13bSMingming Cao 					struct ext4_super_block *es)
4116ac27a0ecSDave Kleikamp {
4117617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4118ac27a0ecSDave Kleikamp 
41190390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
41200390131bSFrank Mayhar 		BUG_ON(journal != NULL);
41210390131bSFrank Mayhar 		return;
41220390131bSFrank Mayhar 	}
4123dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
41247ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
41257ffe1ea8SHidehiro Kawai 		goto out;
41267ffe1ea8SHidehiro Kawai 
4127617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
4128ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4129617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4130e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4131ac27a0ecSDave Kleikamp 	}
41327ffe1ea8SHidehiro Kawai 
41337ffe1ea8SHidehiro Kawai out:
4134dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4135ac27a0ecSDave Kleikamp }
4136ac27a0ecSDave Kleikamp 
4137ac27a0ecSDave Kleikamp /*
4138ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4139ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4140ac27a0ecSDave Kleikamp  * main filesystem now.
4141ac27a0ecSDave Kleikamp  */
4142617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4143617ba13bSMingming Cao 				   struct ext4_super_block *es)
4144ac27a0ecSDave Kleikamp {
4145ac27a0ecSDave Kleikamp 	journal_t *journal;
4146ac27a0ecSDave Kleikamp 	int j_errno;
4147ac27a0ecSDave Kleikamp 	const char *errstr;
4148ac27a0ecSDave Kleikamp 
41490390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
41500390131bSFrank Mayhar 
4151617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4152ac27a0ecSDave Kleikamp 
4153ac27a0ecSDave Kleikamp 	/*
4154ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4155617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4156ac27a0ecSDave Kleikamp 	 */
4157ac27a0ecSDave Kleikamp 
4158dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4159ac27a0ecSDave Kleikamp 	if (j_errno) {
4160ac27a0ecSDave Kleikamp 		char nbuf[16];
4161ac27a0ecSDave Kleikamp 
4162617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
416312062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4164ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
416512062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4166ac27a0ecSDave Kleikamp 
4167617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4168617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4169e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4170ac27a0ecSDave Kleikamp 
4171dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4172ac27a0ecSDave Kleikamp 	}
4173ac27a0ecSDave Kleikamp }
4174ac27a0ecSDave Kleikamp 
4175ac27a0ecSDave Kleikamp /*
4176ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4177ac27a0ecSDave Kleikamp  * and wait on the commit.
4178ac27a0ecSDave Kleikamp  */
4179617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4180ac27a0ecSDave Kleikamp {
4181ac27a0ecSDave Kleikamp 	journal_t *journal;
41820390131bSFrank Mayhar 	int ret = 0;
4183ac27a0ecSDave Kleikamp 
4184ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4185ac27a0ecSDave Kleikamp 		return 0;
4186ac27a0ecSDave Kleikamp 
4187617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
41886b0310fbSEric Sandeen 	if (journal) {
4189437f88ccSEric Sandeen 		vfs_check_frozen(sb, SB_FREEZE_TRANS);
4190617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
41916b0310fbSEric Sandeen 	}
41920390131bSFrank Mayhar 
4193ac27a0ecSDave Kleikamp 	return ret;
4194ac27a0ecSDave Kleikamp }
4195ac27a0ecSDave Kleikamp 
4196617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
4197ac27a0ecSDave Kleikamp {
4198ebc1ac16SChristoph Hellwig 	lock_super(sb);
4199e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4200ebc1ac16SChristoph Hellwig 	unlock_super(sb);
42010390131bSFrank Mayhar }
4202ac27a0ecSDave Kleikamp 
4203617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4204ac27a0ecSDave Kleikamp {
420514ce0cb4STheodore Ts'o 	int ret = 0;
42069eddacf9SJan Kara 	tid_t target;
42078d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4208ac27a0ecSDave Kleikamp 
42099bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
42108d5d02e6SMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
42118d5d02e6SMingming Cao 	if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4212ac27a0ecSDave Kleikamp 		if (wait)
42138d5d02e6SMingming Cao 			jbd2_log_wait_commit(sbi->s_journal, target);
42140390131bSFrank Mayhar 	}
421514ce0cb4STheodore Ts'o 	return ret;
4216ac27a0ecSDave Kleikamp }
4217ac27a0ecSDave Kleikamp 
4218ac27a0ecSDave Kleikamp /*
4219ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4220ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4221be4f27d3SYongqiang Yang  *
4222be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
4223be4f27d3SYongqiang Yang  * state independently, because ext4 prevents a new handle from being started
4224be4f27d3SYongqiang Yang  * by @sb->s_frozen, which stays in an upper layer.  It thus needs help from
4225be4f27d3SYongqiang Yang  * the upper layer.
4226ac27a0ecSDave Kleikamp  */
4227c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4228ac27a0ecSDave Kleikamp {
4229c4be0c1dSTakashi Sato 	int error = 0;
4230c4be0c1dSTakashi Sato 	journal_t *journal;
4231ac27a0ecSDave Kleikamp 
42329ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
42339ca92389STheodore Ts'o 		return 0;
42349ca92389STheodore Ts'o 
4235c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4236ac27a0ecSDave Kleikamp 
4237ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
4238dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
42397ffe1ea8SHidehiro Kawai 
42407ffe1ea8SHidehiro Kawai 	/*
42419ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
42429ca92389STheodore Ts'o 	 * the journal.
42437ffe1ea8SHidehiro Kawai 	 */
4244c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
42456b0310fbSEric Sandeen 	if (error < 0)
42466b0310fbSEric Sandeen 		goto out;
4247ac27a0ecSDave Kleikamp 
4248ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
4249617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4250e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
42516b0310fbSEric Sandeen out:
42526b0310fbSEric Sandeen 	/* we rely on s_frozen to stop further updates */
42536b0310fbSEric Sandeen 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
42546b0310fbSEric Sandeen 	return error;
4255ac27a0ecSDave Kleikamp }
4256ac27a0ecSDave Kleikamp 
4257ac27a0ecSDave Kleikamp /*
4258ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4259ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4260ac27a0ecSDave Kleikamp  */
4261c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4262ac27a0ecSDave Kleikamp {
42639ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
42649ca92389STheodore Ts'o 		return 0;
42659ca92389STheodore Ts'o 
4266ac27a0ecSDave Kleikamp 	lock_super(sb);
42679ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
4268617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4269e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4270ac27a0ecSDave Kleikamp 	unlock_super(sb);
4271c4be0c1dSTakashi Sato 	return 0;
4272ac27a0ecSDave Kleikamp }
4273ac27a0ecSDave Kleikamp 
4274673c6100STheodore Ts'o /*
4275673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4276673c6100STheodore Ts'o  */
4277673c6100STheodore Ts'o struct ext4_mount_options {
4278673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4279a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
4280673c6100STheodore Ts'o 	uid_t s_resuid;
4281673c6100STheodore Ts'o 	gid_t s_resgid;
4282673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4283673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4284673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4285673c6100STheodore Ts'o 	int s_jquota_fmt;
4286673c6100STheodore Ts'o 	char *s_qf_names[MAXQUOTAS];
4287673c6100STheodore Ts'o #endif
4288673c6100STheodore Ts'o };
4289673c6100STheodore Ts'o 
4290617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4291ac27a0ecSDave Kleikamp {
4292617ba13bSMingming Cao 	struct ext4_super_block *es;
4293617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4294617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
4295ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4296617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4297c79d967dSChristoph Hellwig 	int enable_quota = 0;
42988a266467STheodore Ts'o 	ext4_group_t g;
4299b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4300c5e06d10SJohann Lombardi 	int err = 0;
4301ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4302ac27a0ecSDave Kleikamp 	int i;
4303ac27a0ecSDave Kleikamp #endif
4304d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4305ac27a0ecSDave Kleikamp 
4306ac27a0ecSDave Kleikamp 	/* Store the original options */
4307bbd6851aSAl Viro 	lock_super(sb);
4308ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4309ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4310a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4311ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4312ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4313ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
431430773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
431530773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4316ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4317ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4318ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4319ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
4320ac27a0ecSDave Kleikamp #endif
4321b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4322b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4323ac27a0ecSDave Kleikamp 
4324ac27a0ecSDave Kleikamp 	/*
4325ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
4326ac27a0ecSDave Kleikamp 	 */
4327b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
4328b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
4329ac27a0ecSDave Kleikamp 		err = -EINVAL;
4330ac27a0ecSDave Kleikamp 		goto restore_opts;
4331ac27a0ecSDave Kleikamp 	}
4332ac27a0ecSDave Kleikamp 
43334ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4334c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4335ac27a0ecSDave Kleikamp 
4336ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4337482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4338ac27a0ecSDave Kleikamp 
4339ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4340ac27a0ecSDave Kleikamp 
4341b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4342617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4343b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4344b3881f74STheodore Ts'o 	}
4345ac27a0ecSDave Kleikamp 
4346ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
4347bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
43484ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4349ac27a0ecSDave Kleikamp 			err = -EROFS;
4350ac27a0ecSDave Kleikamp 			goto restore_opts;
4351ac27a0ecSDave Kleikamp 		}
4352ac27a0ecSDave Kleikamp 
4353ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
43540f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
43550f0dd62fSChristoph Hellwig 			if (err < 0)
4356c79d967dSChristoph Hellwig 				goto restore_opts;
4357c79d967dSChristoph Hellwig 
4358ac27a0ecSDave Kleikamp 			/*
4359ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4360ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4361ac27a0ecSDave Kleikamp 			 */
4362ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4363ac27a0ecSDave Kleikamp 
4364ac27a0ecSDave Kleikamp 			/*
4365ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4366ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4367ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4368ac27a0ecSDave Kleikamp 			 */
4369617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4370617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4371ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4372ac27a0ecSDave Kleikamp 
4373a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4374617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4375ac27a0ecSDave Kleikamp 		} else {
4376a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4377a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
4378ac27a0ecSDave Kleikamp 				err = -EROFS;
4379ac27a0ecSDave Kleikamp 				goto restore_opts;
4380ac27a0ecSDave Kleikamp 			}
4381ead6596bSEric Sandeen 			/*
43828a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
43830b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
43848a266467STheodore Ts'o 			 */
43858a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
43868a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
43878a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
43888a266467STheodore Ts'o 
43898a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
4390b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4391b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
43928a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
43938a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
43948a266467STheodore Ts'o 					err = -EINVAL;
43958a266467STheodore Ts'o 					goto restore_opts;
43968a266467STheodore Ts'o 				}
43978a266467STheodore Ts'o 			}
43988a266467STheodore Ts'o 
43998a266467STheodore Ts'o 			/*
4400ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4401ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4402ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4403ead6596bSEric Sandeen 			 */
4404ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4405b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4406ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4407ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4408b31e1552SEric Sandeen 				       "umount/remount instead");
4409ead6596bSEric Sandeen 				err = -EINVAL;
4410ead6596bSEric Sandeen 				goto restore_opts;
4411ead6596bSEric Sandeen 			}
4412ead6596bSEric Sandeen 
4413ac27a0ecSDave Kleikamp 			/*
4414ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4415ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4416ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4417ac27a0ecSDave Kleikamp 			 * the partition.)
4418ac27a0ecSDave Kleikamp 			 */
44190390131bSFrank Mayhar 			if (sbi->s_journal)
4420617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4421ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4422617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
4423ac27a0ecSDave Kleikamp 				goto restore_opts;
4424617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4425ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4426c5e06d10SJohann Lombardi 			if (EXT4_HAS_INCOMPAT_FEATURE(sb,
4427c5e06d10SJohann Lombardi 						     EXT4_FEATURE_INCOMPAT_MMP))
4428c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
4429c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
4430c5e06d10SJohann Lombardi 					err = -EROFS;
4431c5e06d10SJohann Lombardi 					goto restore_opts;
4432c5e06d10SJohann Lombardi 				}
4433c79d967dSChristoph Hellwig 			enable_quota = 1;
4434ac27a0ecSDave Kleikamp 		}
4435ac27a0ecSDave Kleikamp 	}
4436bfff6873SLukas Czerner 
4437bfff6873SLukas Czerner 	/*
4438bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4439bfff6873SLukas Czerner 	 * current settings
4440bfff6873SLukas Czerner 	 */
4441bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4442bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4443bfff6873SLukas Czerner 	else {
4444bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4445bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4446bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4447bfff6873SLukas Czerner 	}
4448bfff6873SLukas Czerner 
44496fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
44500390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
4451e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
44520390131bSFrank Mayhar 
4453ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4454ac27a0ecSDave Kleikamp 	/* Release old quota file names */
4455ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4456ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
4457ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4458ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
4459ac27a0ecSDave Kleikamp #endif
4460bbd6851aSAl Viro 	unlock_super(sb);
4461c79d967dSChristoph Hellwig 	if (enable_quota)
44620f0dd62fSChristoph Hellwig 		dquot_resume(sb, -1);
4463d4c402d9SCurt Wohlgemuth 
4464d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4465d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4466ac27a0ecSDave Kleikamp 	return 0;
44670b8e58a1SAndreas Dilger 
4468ac27a0ecSDave Kleikamp restore_opts:
4469ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
4470ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
4471a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
4472ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
4473ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
4474ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
447530773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
447630773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
4477ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4478ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
4479ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
4480ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
4481ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4482ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
4483ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
4484ac27a0ecSDave Kleikamp 	}
4485ac27a0ecSDave Kleikamp #endif
4486bbd6851aSAl Viro 	unlock_super(sb);
4487d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4488ac27a0ecSDave Kleikamp 	return err;
4489ac27a0ecSDave Kleikamp }
4490ac27a0ecSDave Kleikamp 
4491617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
4492ac27a0ecSDave Kleikamp {
4493ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
4494617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4495617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
4496960cc398SPekka Enberg 	u64 fsid;
4497d02a9391SKazuya Mio 	s64 bfree;
4498ac27a0ecSDave Kleikamp 
44995e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
45005e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
45016bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
45028df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
45035e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
4504ac27a0ecSDave Kleikamp 
4505ac27a0ecSDave Kleikamp 		/*
45065e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
45075e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
45085e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
4509ac27a0ecSDave Kleikamp 		 */
4510ac27a0ecSDave Kleikamp 
4511ac27a0ecSDave Kleikamp 		/*
4512ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
4513ac27a0ecSDave Kleikamp 		 * overhead
4514ac27a0ecSDave Kleikamp 		 */
4515ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
4516ac27a0ecSDave Kleikamp 
4517ac27a0ecSDave Kleikamp 		/*
4518ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
4519ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
4520ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
4521ac27a0ecSDave Kleikamp 		 */
4522ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
4523617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
4524617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
4525ac27a0ecSDave Kleikamp 			cond_resched();
4526ac27a0ecSDave Kleikamp 		}
4527ac27a0ecSDave Kleikamp 
4528ac27a0ecSDave Kleikamp 		/*
4529ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
4530ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
4531ac27a0ecSDave Kleikamp 		 */
45325e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
45335e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
45345e70030dSBadari Pulavarty 		smp_wmb();
45356bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
4536ac27a0ecSDave Kleikamp 	}
4537ac27a0ecSDave Kleikamp 
4538617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
4539ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
45405e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
4541d02a9391SKazuya Mio 	bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
45426bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
4543d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
4544d02a9391SKazuya Mio 	buf->f_bfree = max_t(s64, bfree, 0);
4545bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
4546bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
4547ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
4548ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
454952d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
4550617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
4551960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
4552960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
4553960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
4554960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
45550b8e58a1SAndreas Dilger 
4556ac27a0ecSDave Kleikamp 	return 0;
4557ac27a0ecSDave Kleikamp }
4558ac27a0ecSDave Kleikamp 
45590b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
45600b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
4561ac27a0ecSDave Kleikamp  * Process 1                         Process 2
4562617ba13bSMingming Cao  * ext4_create()                     quota_sync()
4563dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
4564871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
4565dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
4566ac27a0ecSDave Kleikamp  *
4567ac27a0ecSDave Kleikamp  */
4568ac27a0ecSDave Kleikamp 
4569ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4570ac27a0ecSDave Kleikamp 
4571ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
4572ac27a0ecSDave Kleikamp {
4573ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
4574ac27a0ecSDave Kleikamp }
4575ac27a0ecSDave Kleikamp 
4576617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
4577ac27a0ecSDave Kleikamp {
4578ac27a0ecSDave Kleikamp 	int ret, err;
4579ac27a0ecSDave Kleikamp 	handle_t *handle;
4580ac27a0ecSDave Kleikamp 	struct inode *inode;
4581ac27a0ecSDave Kleikamp 
4582ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
4583617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
4584617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
4585ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4586ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4587ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
4588617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4589ac27a0ecSDave Kleikamp 	if (!ret)
4590ac27a0ecSDave Kleikamp 		ret = err;
4591ac27a0ecSDave Kleikamp 	return ret;
4592ac27a0ecSDave Kleikamp }
4593ac27a0ecSDave Kleikamp 
4594617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
4595ac27a0ecSDave Kleikamp {
4596ac27a0ecSDave Kleikamp 	int ret, err;
4597ac27a0ecSDave Kleikamp 	handle_t *handle;
4598ac27a0ecSDave Kleikamp 
4599617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4600617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
4601ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4602ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4603ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
4604617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4605ac27a0ecSDave Kleikamp 	if (!ret)
4606ac27a0ecSDave Kleikamp 		ret = err;
4607ac27a0ecSDave Kleikamp 	return ret;
4608ac27a0ecSDave Kleikamp }
4609ac27a0ecSDave Kleikamp 
4610617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
4611ac27a0ecSDave Kleikamp {
4612ac27a0ecSDave Kleikamp 	int ret, err;
4613ac27a0ecSDave Kleikamp 	handle_t *handle;
4614ac27a0ecSDave Kleikamp 
4615617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4616617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
46179c3013e9SJan Kara 	if (IS_ERR(handle)) {
46189c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
46199c3013e9SJan Kara 		dquot_release(dquot);
4620ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
46219c3013e9SJan Kara 	}
4622ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
4623617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4624ac27a0ecSDave Kleikamp 	if (!ret)
4625ac27a0ecSDave Kleikamp 		ret = err;
4626ac27a0ecSDave Kleikamp 	return ret;
4627ac27a0ecSDave Kleikamp }
4628ac27a0ecSDave Kleikamp 
4629617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
4630ac27a0ecSDave Kleikamp {
46312c8be6b2SJan Kara 	/* Are we journaling quotas? */
4632617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
4633617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
4634ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
4635617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
4636ac27a0ecSDave Kleikamp 	} else {
4637ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
4638ac27a0ecSDave Kleikamp 	}
4639ac27a0ecSDave Kleikamp }
4640ac27a0ecSDave Kleikamp 
4641617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
4642ac27a0ecSDave Kleikamp {
4643ac27a0ecSDave Kleikamp 	int ret, err;
4644ac27a0ecSDave Kleikamp 	handle_t *handle;
4645ac27a0ecSDave Kleikamp 
4646ac27a0ecSDave Kleikamp 	/* Data block + inode block */
4647617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
4648ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4649ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4650ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
4651617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4652ac27a0ecSDave Kleikamp 	if (!ret)
4653ac27a0ecSDave Kleikamp 		ret = err;
4654ac27a0ecSDave Kleikamp 	return ret;
4655ac27a0ecSDave Kleikamp }
4656ac27a0ecSDave Kleikamp 
4657ac27a0ecSDave Kleikamp /*
4658ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
4659ac27a0ecSDave Kleikamp  * the quota file and such...
4660ac27a0ecSDave Kleikamp  */
4661617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
4662ac27a0ecSDave Kleikamp {
4663287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
4664617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
4665ac27a0ecSDave Kleikamp }
4666ac27a0ecSDave Kleikamp 
4667ac27a0ecSDave Kleikamp /*
4668ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
4669ac27a0ecSDave Kleikamp  */
4670617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
4671f00c9e44SJan Kara 			 struct path *path)
4672ac27a0ecSDave Kleikamp {
4673ac27a0ecSDave Kleikamp 	int err;
4674ac27a0ecSDave Kleikamp 
4675ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
4676ac27a0ecSDave Kleikamp 		return -EINVAL;
46770623543bSJan Kara 
4678ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
4679f00c9e44SJan Kara 	if (path->mnt->mnt_sb != sb)
4680ac27a0ecSDave Kleikamp 		return -EXDEV;
46810623543bSJan Kara 	/* Journaling quota? */
46820623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
46832b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
4684f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
4685b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
4686b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
4687b31e1552SEric Sandeen 				"Journaled quota will not work");
46880623543bSJan Kara 	}
46890623543bSJan Kara 
46900623543bSJan Kara 	/*
46910623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
46920623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
46930623543bSJan Kara 	 */
46940390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
4695f00c9e44SJan Kara 	    ext4_should_journal_data(path->dentry->d_inode)) {
46960623543bSJan Kara 		/*
46970623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
46980623543bSJan Kara 		 * otherwise be livelocked...
46990623543bSJan Kara 		 */
47000623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
47017ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
47020623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
4703f00c9e44SJan Kara 		if (err)
47047ffe1ea8SHidehiro Kawai 			return err;
47057ffe1ea8SHidehiro Kawai 	}
47060623543bSJan Kara 
4707f00c9e44SJan Kara 	return dquot_quota_on(sb, type, format_id, path);
4708ac27a0ecSDave Kleikamp }
4709ac27a0ecSDave Kleikamp 
4710ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
4711ca0e05e4SDmitry Monakhov {
471221f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
471321f97697SJan Kara 	handle_t *handle;
471421f97697SJan Kara 
471587009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
471687009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
471787009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
4718ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
4719ca0e05e4SDmitry Monakhov 
47200b268590SAmir Goldstein 	if (!inode)
47210b268590SAmir Goldstein 		goto out;
47220b268590SAmir Goldstein 
472321f97697SJan Kara 	/* Update modification times of quota files when userspace can
472421f97697SJan Kara 	 * start looking at them */
472521f97697SJan Kara 	handle = ext4_journal_start(inode, 1);
472621f97697SJan Kara 	if (IS_ERR(handle))
472721f97697SJan Kara 		goto out;
472821f97697SJan Kara 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
472921f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
473021f97697SJan Kara 	ext4_journal_stop(handle);
473121f97697SJan Kara 
473221f97697SJan Kara out:
4733ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
4734ca0e05e4SDmitry Monakhov }
4735ca0e05e4SDmitry Monakhov 
4736ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
4737ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
4738ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
4739ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
4740617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
4741ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
4742ac27a0ecSDave Kleikamp {
4743ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
4744725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4745ac27a0ecSDave Kleikamp 	int err = 0;
4746ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
4747ac27a0ecSDave Kleikamp 	int tocopy;
4748ac27a0ecSDave Kleikamp 	size_t toread;
4749ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4750ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
4751ac27a0ecSDave Kleikamp 
4752ac27a0ecSDave Kleikamp 	if (off > i_size)
4753ac27a0ecSDave Kleikamp 		return 0;
4754ac27a0ecSDave Kleikamp 	if (off+len > i_size)
4755ac27a0ecSDave Kleikamp 		len = i_size-off;
4756ac27a0ecSDave Kleikamp 	toread = len;
4757ac27a0ecSDave Kleikamp 	while (toread > 0) {
4758ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
4759ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
4760617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
4761ac27a0ecSDave Kleikamp 		if (err)
4762ac27a0ecSDave Kleikamp 			return err;
4763ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
4764ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
4765ac27a0ecSDave Kleikamp 		else
4766ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
4767ac27a0ecSDave Kleikamp 		brelse(bh);
4768ac27a0ecSDave Kleikamp 		offset = 0;
4769ac27a0ecSDave Kleikamp 		toread -= tocopy;
4770ac27a0ecSDave Kleikamp 		data += tocopy;
4771ac27a0ecSDave Kleikamp 		blk++;
4772ac27a0ecSDave Kleikamp 	}
4773ac27a0ecSDave Kleikamp 	return len;
4774ac27a0ecSDave Kleikamp }
4775ac27a0ecSDave Kleikamp 
4776ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
4777ac27a0ecSDave Kleikamp  * enough credits) */
4778617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
4779ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
4780ac27a0ecSDave Kleikamp {
4781ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
4782725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4783ac27a0ecSDave Kleikamp 	int err = 0;
4784ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
4785ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4786ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
4787ac27a0ecSDave Kleikamp 
47880390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
4789b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4790b31e1552SEric Sandeen 			" cancelled because transaction is not started",
47919c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
47929c3013e9SJan Kara 		return -EIO;
47939c3013e9SJan Kara 	}
479467eeb568SDmitry Monakhov 	/*
479567eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
479667eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
479767eeb568SDmitry Monakhov 	 */
479867eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
479967eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
480067eeb568SDmitry Monakhov 			" cancelled because not block aligned",
480167eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
480267eeb568SDmitry Monakhov 		return -EIO;
480367eeb568SDmitry Monakhov 	}
480467eeb568SDmitry Monakhov 
4805ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
4806617ba13bSMingming Cao 	bh = ext4_bread(handle, inode, blk, 1, &err);
4807ac27a0ecSDave Kleikamp 	if (!bh)
4808ac27a0ecSDave Kleikamp 		goto out;
4809617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
4810ac27a0ecSDave Kleikamp 	if (err) {
4811ac27a0ecSDave Kleikamp 		brelse(bh);
4812ac27a0ecSDave Kleikamp 		goto out;
4813ac27a0ecSDave Kleikamp 	}
4814ac27a0ecSDave Kleikamp 	lock_buffer(bh);
481567eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
4816ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
4817ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
48180390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
4819ac27a0ecSDave Kleikamp 	brelse(bh);
4820ac27a0ecSDave Kleikamp out:
482167eeb568SDmitry Monakhov 	if (err) {
48224d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
4823ac27a0ecSDave Kleikamp 		return err;
48244d04e4fbSJan Kara 	}
482567eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
482667eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
4827617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
4828617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
482921f97697SJan Kara 	}
4830ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
483167eeb568SDmitry Monakhov 	return len;
4832ac27a0ecSDave Kleikamp }
4833ac27a0ecSDave Kleikamp 
4834ac27a0ecSDave Kleikamp #endif
4835ac27a0ecSDave Kleikamp 
4836152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
4837152a0836SAl Viro 		       const char *dev_name, void *data)
4838ac27a0ecSDave Kleikamp {
4839152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
4840ac27a0ecSDave Kleikamp }
4841ac27a0ecSDave Kleikamp 
484237f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
484324b58424STheodore Ts'o static inline void register_as_ext2(void)
484424b58424STheodore Ts'o {
484524b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
484624b58424STheodore Ts'o 	if (err)
484724b58424STheodore Ts'o 		printk(KERN_WARNING
484824b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
484924b58424STheodore Ts'o }
485024b58424STheodore Ts'o 
485124b58424STheodore Ts'o static inline void unregister_as_ext2(void)
485224b58424STheodore Ts'o {
485324b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
485424b58424STheodore Ts'o }
48552035e776STheodore Ts'o 
48562035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
48572035e776STheodore Ts'o {
48582035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
48592035e776STheodore Ts'o 		return 0;
48602035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
48612035e776STheodore Ts'o 		return 1;
48622035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
48632035e776STheodore Ts'o 		return 0;
48642035e776STheodore Ts'o 	return 1;
48652035e776STheodore Ts'o }
486651b7e3c9STheodore Ts'o MODULE_ALIAS("ext2");
486724b58424STheodore Ts'o #else
486824b58424STheodore Ts'o static inline void register_as_ext2(void) { }
486924b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
48702035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
487124b58424STheodore Ts'o #endif
487224b58424STheodore Ts'o 
487337f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
487424b58424STheodore Ts'o static inline void register_as_ext3(void)
487524b58424STheodore Ts'o {
487624b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
487724b58424STheodore Ts'o 	if (err)
487824b58424STheodore Ts'o 		printk(KERN_WARNING
487924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
488024b58424STheodore Ts'o }
488124b58424STheodore Ts'o 
488224b58424STheodore Ts'o static inline void unregister_as_ext3(void)
488324b58424STheodore Ts'o {
488424b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
488524b58424STheodore Ts'o }
48862035e776STheodore Ts'o 
48872035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
48882035e776STheodore Ts'o {
48892035e776STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
48902035e776STheodore Ts'o 		return 0;
48912035e776STheodore Ts'o 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
48922035e776STheodore Ts'o 		return 0;
48932035e776STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
48942035e776STheodore Ts'o 		return 1;
48952035e776STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
48962035e776STheodore Ts'o 		return 0;
48972035e776STheodore Ts'o 	return 1;
48982035e776STheodore Ts'o }
489951b7e3c9STheodore Ts'o MODULE_ALIAS("ext3");
490024b58424STheodore Ts'o #else
490124b58424STheodore Ts'o static inline void register_as_ext3(void) { }
490224b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
49032035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
490424b58424STheodore Ts'o #endif
490524b58424STheodore Ts'o 
490603010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
4907ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
490803010a33STheodore Ts'o 	.name		= "ext4",
4909152a0836SAl Viro 	.mount		= ext4_mount,
4910ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
4911ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
4912ac27a0ecSDave Kleikamp };
4913ac27a0ecSDave Kleikamp 
49148f021222SLukas Czerner static int __init ext4_init_feat_adverts(void)
4915857ac889SLukas Czerner {
4916857ac889SLukas Czerner 	struct ext4_features *ef;
4917857ac889SLukas Czerner 	int ret = -ENOMEM;
4918857ac889SLukas Czerner 
4919857ac889SLukas Czerner 	ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
4920857ac889SLukas Czerner 	if (!ef)
4921857ac889SLukas Czerner 		goto out;
4922857ac889SLukas Czerner 
4923857ac889SLukas Czerner 	ef->f_kobj.kset = ext4_kset;
4924857ac889SLukas Czerner 	init_completion(&ef->f_kobj_unregister);
4925857ac889SLukas Czerner 	ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
4926857ac889SLukas Czerner 				   "features");
4927857ac889SLukas Czerner 	if (ret) {
4928857ac889SLukas Czerner 		kfree(ef);
4929857ac889SLukas Czerner 		goto out;
4930857ac889SLukas Czerner 	}
4931857ac889SLukas Czerner 
4932857ac889SLukas Czerner 	ext4_feat = ef;
4933857ac889SLukas Czerner 	ret = 0;
4934857ac889SLukas Czerner out:
4935857ac889SLukas Czerner 	return ret;
4936857ac889SLukas Czerner }
4937857ac889SLukas Czerner 
49388f021222SLukas Czerner static void ext4_exit_feat_adverts(void)
49398f021222SLukas Czerner {
49408f021222SLukas Czerner 	kobject_put(&ext4_feat->f_kobj);
49418f021222SLukas Czerner 	wait_for_completion(&ext4_feat->f_kobj_unregister);
49428f021222SLukas Czerner 	kfree(ext4_feat);
49438f021222SLukas Czerner }
49448f021222SLukas Czerner 
4945e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
4946e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
4947e9e3bcecSEric Sandeen struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
4948e9e3bcecSEric Sandeen 
49495dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
4950ac27a0ecSDave Kleikamp {
4951e9e3bcecSEric Sandeen 	int i, err;
4952c9de560dSAlex Tomas 
495312e9b892SDmitry Monakhov 	ext4_check_flag_values();
4954e9e3bcecSEric Sandeen 
4955e9e3bcecSEric Sandeen 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
4956e9e3bcecSEric Sandeen 		mutex_init(&ext4__aio_mutex[i]);
4957e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
4958e9e3bcecSEric Sandeen 	}
4959e9e3bcecSEric Sandeen 
49605dabfc78STheodore Ts'o 	err = ext4_init_pageio();
49616fd058f7STheodore Ts'o 	if (err)
49626fd058f7STheodore Ts'o 		return err;
49635dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
4964bd2d0210STheodore Ts'o 	if (err)
4965dd68314cSTheodore Ts'o 		goto out7;
49663197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
49673197ebdbSTheodore Ts'o 	if (!ext4_kset)
4968dd68314cSTheodore Ts'o 		goto out6;
49699f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
4970dd68314cSTheodore Ts'o 	if (!ext4_proc_root)
4971dd68314cSTheodore Ts'o 		goto out5;
4972857ac889SLukas Czerner 
4973857ac889SLukas Czerner 	err = ext4_init_feat_adverts();
4974dd68314cSTheodore Ts'o 	if (err)
4975dd68314cSTheodore Ts'o 		goto out4;
4976857ac889SLukas Czerner 
49775dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
4978ac27a0ecSDave Kleikamp 	if (err)
49796fd058f7STheodore Ts'o 		goto out3;
4980c9de560dSAlex Tomas 
49815dabfc78STheodore Ts'o 	err = ext4_init_xattr();
4982c9de560dSAlex Tomas 	if (err)
4983c9de560dSAlex Tomas 		goto out2;
4984ac27a0ecSDave Kleikamp 	err = init_inodecache();
4985ac27a0ecSDave Kleikamp 	if (err)
4986ac27a0ecSDave Kleikamp 		goto out1;
498724b58424STheodore Ts'o 	register_as_ext3();
49882035e776STheodore Ts'o 	register_as_ext2();
498903010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
4990ac27a0ecSDave Kleikamp 	if (err)
4991ac27a0ecSDave Kleikamp 		goto out;
4992bfff6873SLukas Czerner 
4993bfff6873SLukas Czerner 	ext4_li_info = NULL;
4994bfff6873SLukas Czerner 	mutex_init(&ext4_li_mtx);
4995ac27a0ecSDave Kleikamp 	return 0;
4996ac27a0ecSDave Kleikamp out:
499724b58424STheodore Ts'o 	unregister_as_ext2();
499824b58424STheodore Ts'o 	unregister_as_ext3();
4999ac27a0ecSDave Kleikamp 	destroy_inodecache();
5000ac27a0ecSDave Kleikamp out1:
50015dabfc78STheodore Ts'o 	ext4_exit_xattr();
5002c9de560dSAlex Tomas out2:
50035dabfc78STheodore Ts'o 	ext4_exit_mballoc();
50046fd058f7STheodore Ts'o out3:
50058f021222SLukas Czerner 	ext4_exit_feat_adverts();
50066fd058f7STheodore Ts'o out4:
5007dd68314cSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
5008bd2d0210STheodore Ts'o out5:
5009dd68314cSTheodore Ts'o 	kset_unregister(ext4_kset);
5010dd68314cSTheodore Ts'o out6:
5011dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
5012dd68314cSTheodore Ts'o out7:
50135dabfc78STheodore Ts'o 	ext4_exit_pageio();
5014ac27a0ecSDave Kleikamp 	return err;
5015ac27a0ecSDave Kleikamp }
5016ac27a0ecSDave Kleikamp 
50175dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
5018ac27a0ecSDave Kleikamp {
5019bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
502024b58424STheodore Ts'o 	unregister_as_ext2();
502124b58424STheodore Ts'o 	unregister_as_ext3();
502203010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
5023ac27a0ecSDave Kleikamp 	destroy_inodecache();
50245dabfc78STheodore Ts'o 	ext4_exit_xattr();
50255dabfc78STheodore Ts'o 	ext4_exit_mballoc();
50268f021222SLukas Czerner 	ext4_exit_feat_adverts();
50279f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
50283197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
50295dabfc78STheodore Ts'o 	ext4_exit_system_zone();
50305dabfc78STheodore Ts'o 	ext4_exit_pageio();
5031ac27a0ecSDave Kleikamp }
5032ac27a0ecSDave Kleikamp 
5033ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
503483982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
5035ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
50365dabfc78STheodore Ts'o module_init(ext4_init_fs)
50375dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
5038