xref: /linux/fs/ext4/super.c (revision 8f021222c1e2756ea4c9dde93b23e1d2a0a4ec37)
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>
41ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
42ac27a0ecSDave Kleikamp 
43bfff6873SLukas Czerner #include <linux/kthread.h>
44bfff6873SLukas Czerner #include <linux/freezer.h>
45bfff6873SLukas Czerner 
463dcf5451SChristoph Hellwig #include "ext4.h"
473dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
48ac27a0ecSDave Kleikamp #include "xattr.h"
49ac27a0ecSDave Kleikamp #include "acl.h"
503661d286STheodore Ts'o #include "mballoc.h"
51ac27a0ecSDave Kleikamp 
529bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
539bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
549bffad1eSTheodore Ts'o 
551f109d5aSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root;
563197ebdbSTheodore Ts'o static struct kset *ext4_kset;
57bfff6873SLukas Czerner struct ext4_lazy_init *ext4_li_info;
58bfff6873SLukas Czerner struct mutex ext4_li_mtx;
59857ac889SLukas Czerner struct ext4_features *ext4_feat;
609f6200bbSTheodore Ts'o 
61617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
62ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
63e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
64617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
65617ba13bSMingming Cao 					struct ext4_super_block *es);
66617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
67617ba13bSMingming Cao 				   struct ext4_super_block *es);
68617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
69617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
70ac27a0ecSDave Kleikamp 				     char nbuf[16]);
71617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
72617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
73c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
74617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
75c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
76152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
77152a0836SAl Viro 		       const char *dev_name, void *data);
78bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
79bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
808f1f7453SEric Sandeen static void ext4_clear_request_list(void);
81ac27a0ecSDave Kleikamp 
82ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
83ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
84ba69f9abSJan Kara 	.owner		= THIS_MODULE,
85ba69f9abSJan Kara 	.name		= "ext3",
86152a0836SAl Viro 	.mount		= ext4_mount,
87ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
88ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
89ba69f9abSJan Kara };
90ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
91ba69f9abSJan Kara #else
92ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0)
93ba69f9abSJan Kara #endif
94bd81d8eeSLaurent Vivier 
958fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
968fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
97bd81d8eeSLaurent Vivier {
983a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
998fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1008fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
101bd81d8eeSLaurent Vivier }
102bd81d8eeSLaurent Vivier 
1038fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
1048fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
105bd81d8eeSLaurent Vivier {
1065272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
1078fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1088fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
109bd81d8eeSLaurent Vivier }
110bd81d8eeSLaurent Vivier 
1118fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
1128fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
113bd81d8eeSLaurent Vivier {
1145272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
1158fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1168fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
117bd81d8eeSLaurent Vivier }
118bd81d8eeSLaurent Vivier 
119560671a0SAneesh Kumar K.V __u32 ext4_free_blks_count(struct super_block *sb,
120560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
121560671a0SAneesh Kumar K.V {
122560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
123560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
124560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
125560671a0SAneesh Kumar K.V }
126560671a0SAneesh Kumar K.V 
127560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
128560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
129560671a0SAneesh Kumar K.V {
130560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
131560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
132560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
133560671a0SAneesh Kumar K.V }
134560671a0SAneesh Kumar K.V 
135560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
136560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
137560671a0SAneesh Kumar K.V {
138560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
139560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
140560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
141560671a0SAneesh Kumar K.V }
142560671a0SAneesh Kumar K.V 
143560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
144560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
145560671a0SAneesh Kumar K.V {
146560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
147560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
148560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
149560671a0SAneesh Kumar K.V }
150560671a0SAneesh Kumar K.V 
1518fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
1528fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
153bd81d8eeSLaurent Vivier {
1543a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1558fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1568fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
157bd81d8eeSLaurent Vivier }
158bd81d8eeSLaurent Vivier 
1598fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1608fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
161bd81d8eeSLaurent Vivier {
1625272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1638fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1648fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
165bd81d8eeSLaurent Vivier }
166bd81d8eeSLaurent Vivier 
1678fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1688fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
169bd81d8eeSLaurent Vivier {
1705272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1718fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1728fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
173bd81d8eeSLaurent Vivier }
174bd81d8eeSLaurent Vivier 
175560671a0SAneesh Kumar K.V void ext4_free_blks_set(struct super_block *sb,
176560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
177560671a0SAneesh Kumar K.V {
178560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
179560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
180560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
181560671a0SAneesh Kumar K.V }
182560671a0SAneesh Kumar K.V 
183560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
184560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
185560671a0SAneesh Kumar K.V {
186560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
187560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
188560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
189560671a0SAneesh Kumar K.V }
190560671a0SAneesh Kumar K.V 
191560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
192560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
193560671a0SAneesh Kumar K.V {
194560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
195560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
196560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
197560671a0SAneesh Kumar K.V }
198560671a0SAneesh Kumar K.V 
199560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
200560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
201560671a0SAneesh Kumar K.V {
202560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
203560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
204560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
205560671a0SAneesh Kumar K.V }
206560671a0SAneesh Kumar K.V 
207d3d1faf6SCurt Wohlgemuth 
208d3d1faf6SCurt Wohlgemuth /* Just increment the non-pointer handle value */
209d3d1faf6SCurt Wohlgemuth static handle_t *ext4_get_nojournal(void)
210d3d1faf6SCurt Wohlgemuth {
211d3d1faf6SCurt Wohlgemuth 	handle_t *handle = current->journal_info;
212d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
213d3d1faf6SCurt Wohlgemuth 
214d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
215d3d1faf6SCurt Wohlgemuth 
216d3d1faf6SCurt Wohlgemuth 	ref_cnt++;
217d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
218d3d1faf6SCurt Wohlgemuth 
219d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
220d3d1faf6SCurt Wohlgemuth 	return handle;
221d3d1faf6SCurt Wohlgemuth }
222d3d1faf6SCurt Wohlgemuth 
223d3d1faf6SCurt Wohlgemuth 
224d3d1faf6SCurt Wohlgemuth /* Decrement the non-pointer handle value */
225d3d1faf6SCurt Wohlgemuth static void ext4_put_nojournal(handle_t *handle)
226d3d1faf6SCurt Wohlgemuth {
227d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
228d3d1faf6SCurt Wohlgemuth 
229d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt == 0);
230d3d1faf6SCurt Wohlgemuth 
231d3d1faf6SCurt Wohlgemuth 	ref_cnt--;
232d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
233d3d1faf6SCurt Wohlgemuth 
234d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
235d3d1faf6SCurt Wohlgemuth }
236d3d1faf6SCurt Wohlgemuth 
237ac27a0ecSDave Kleikamp /*
238dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
239ac27a0ecSDave Kleikamp  *
240ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
241ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
242ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
243ac27a0ecSDave Kleikamp  * appropriate.
244ac27a0ecSDave Kleikamp  */
245617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
246ac27a0ecSDave Kleikamp {
247ac27a0ecSDave Kleikamp 	journal_t *journal;
248ac27a0ecSDave Kleikamp 
249ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
250ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
251ac27a0ecSDave Kleikamp 
252437f88ccSEric Sandeen 	vfs_check_frozen(sb, SB_FREEZE_TRANS);
253ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
254ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
255ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
256617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2570390131bSFrank Mayhar 	if (journal) {
258ac27a0ecSDave Kleikamp 		if (is_journal_aborted(journal)) {
259c67d859eSTheodore Ts'o 			ext4_abort(sb, "Detected aborted journal");
260ac27a0ecSDave Kleikamp 			return ERR_PTR(-EROFS);
261ac27a0ecSDave Kleikamp 		}
262dab291afSMingming Cao 		return jbd2_journal_start(journal, nblocks);
263ac27a0ecSDave Kleikamp 	}
264d3d1faf6SCurt Wohlgemuth 	return ext4_get_nojournal();
2650390131bSFrank Mayhar }
266ac27a0ecSDave Kleikamp 
267ac27a0ecSDave Kleikamp /*
268ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
269dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
270ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
271ac27a0ecSDave Kleikamp  * appropriate.
272ac27a0ecSDave Kleikamp  */
273c398eda0STheodore Ts'o int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
274ac27a0ecSDave Kleikamp {
275ac27a0ecSDave Kleikamp 	struct super_block *sb;
276ac27a0ecSDave Kleikamp 	int err;
277ac27a0ecSDave Kleikamp 	int rc;
278ac27a0ecSDave Kleikamp 
2790390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
280d3d1faf6SCurt Wohlgemuth 		ext4_put_nojournal(handle);
2810390131bSFrank Mayhar 		return 0;
2820390131bSFrank Mayhar 	}
283ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
284ac27a0ecSDave Kleikamp 	err = handle->h_err;
285dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
286ac27a0ecSDave Kleikamp 
287ac27a0ecSDave Kleikamp 	if (!err)
288ac27a0ecSDave Kleikamp 		err = rc;
289ac27a0ecSDave Kleikamp 	if (err)
290c398eda0STheodore Ts'o 		__ext4_std_error(sb, where, line, err);
291ac27a0ecSDave Kleikamp 	return err;
292ac27a0ecSDave Kleikamp }
293ac27a0ecSDave Kleikamp 
29490c7201bSTheodore Ts'o void ext4_journal_abort_handle(const char *caller, unsigned int line,
29590c7201bSTheodore Ts'o 			       const char *err_fn, struct buffer_head *bh,
29690c7201bSTheodore Ts'o 			       handle_t *handle, int err)
297ac27a0ecSDave Kleikamp {
298ac27a0ecSDave Kleikamp 	char nbuf[16];
299617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
300ac27a0ecSDave Kleikamp 
3010390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
3020390131bSFrank Mayhar 
303ac27a0ecSDave Kleikamp 	if (bh)
304ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
305ac27a0ecSDave Kleikamp 
306ac27a0ecSDave Kleikamp 	if (!handle->h_err)
307ac27a0ecSDave Kleikamp 		handle->h_err = err;
308ac27a0ecSDave Kleikamp 
309ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
310ac27a0ecSDave Kleikamp 		return;
311ac27a0ecSDave Kleikamp 
31290c7201bSTheodore Ts'o 	printk(KERN_ERR "%s:%d: aborting transaction: %s in %s\n",
31390c7201bSTheodore Ts'o 	       caller, line, errstr, err_fn);
314ac27a0ecSDave Kleikamp 
315dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
316ac27a0ecSDave Kleikamp }
317ac27a0ecSDave Kleikamp 
3181c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3191c13d5c0STheodore Ts'o 			    unsigned int line)
3201c13d5c0STheodore Ts'o {
3211c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3221c13d5c0STheodore Ts'o 
3231c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3241c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3251c13d5c0STheodore Ts'o 	es->s_last_error_time = cpu_to_le32(get_seconds());
3261c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3271c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3281c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3291c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3301c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3311c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3321c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3331c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3341c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3351c13d5c0STheodore Ts'o 	}
33666e61a9eSTheodore Ts'o 	/*
33766e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
33866e61a9eSTheodore Ts'o 	 * started already
33966e61a9eSTheodore Ts'o 	 */
34066e61a9eSTheodore Ts'o 	if (!es->s_error_count)
34166e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
3421c13d5c0STheodore Ts'o 	es->s_error_count = cpu_to_le32(le32_to_cpu(es->s_error_count) + 1);
3431c13d5c0STheodore Ts'o }
3441c13d5c0STheodore Ts'o 
3451c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3461c13d5c0STheodore Ts'o 			    unsigned int line)
3471c13d5c0STheodore Ts'o {
3481c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3491c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3501c13d5c0STheodore Ts'o }
3511c13d5c0STheodore Ts'o 
3521c13d5c0STheodore Ts'o 
353ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
354ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
355ac27a0ecSDave Kleikamp  *
356ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
357617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
358ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
359ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
360ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
361ac27a0ecSDave Kleikamp  * write out the superblock safely.
362ac27a0ecSDave Kleikamp  *
363dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
364d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
365ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
366ac27a0ecSDave Kleikamp  */
367ac27a0ecSDave Kleikamp 
368617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
369ac27a0ecSDave Kleikamp {
370ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
371ac27a0ecSDave Kleikamp 		return;
372ac27a0ecSDave Kleikamp 
373ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
374617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
375ac27a0ecSDave Kleikamp 
3764ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
377ac27a0ecSDave Kleikamp 		if (journal)
378dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
379ac27a0ecSDave Kleikamp 	}
380ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
381b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
382ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
383ac27a0ecSDave Kleikamp 	}
384ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
385617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
386ac27a0ecSDave Kleikamp 			sb->s_id);
387ac27a0ecSDave Kleikamp }
388ac27a0ecSDave Kleikamp 
38912062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
390c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
391ac27a0ecSDave Kleikamp {
3920ff2ea7dSJoe Perches 	struct va_format vaf;
393ac27a0ecSDave Kleikamp 	va_list args;
394ac27a0ecSDave Kleikamp 
395ac27a0ecSDave Kleikamp 	va_start(args, fmt);
3960ff2ea7dSJoe Perches 	vaf.fmt = fmt;
3970ff2ea7dSJoe Perches 	vaf.va = &args;
3980ff2ea7dSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
3990ff2ea7dSJoe Perches 	       sb->s_id, function, line, current->comm, &vaf);
400ac27a0ecSDave Kleikamp 	va_end(args);
401ac27a0ecSDave Kleikamp 
402617ba13bSMingming Cao 	ext4_handle_error(sb);
403ac27a0ecSDave Kleikamp }
404ac27a0ecSDave Kleikamp 
405c398eda0STheodore Ts'o void ext4_error_inode(struct inode *inode, const char *function,
406c398eda0STheodore Ts'o 		      unsigned int line, ext4_fsblk_t block,
407273df556SFrank Mayhar 		      const char *fmt, ...)
408273df556SFrank Mayhar {
409273df556SFrank Mayhar 	va_list args;
410f7c21177STheodore Ts'o 	struct va_format vaf;
4111c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
412273df556SFrank Mayhar 
4131c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4141c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
4151c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
416273df556SFrank Mayhar 	va_start(args, fmt);
417f7c21177STheodore Ts'o 	vaf.fmt = fmt;
418f7c21177STheodore Ts'o 	vaf.va = &args;
419c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
420c398eda0STheodore Ts'o 	       inode->i_sb->s_id, function, line, inode->i_ino);
421c398eda0STheodore Ts'o 	if (block)
422f7c21177STheodore Ts'o 		printk(KERN_CONT "block %llu: ", block);
423f7c21177STheodore Ts'o 	printk(KERN_CONT "comm %s: %pV\n", current->comm, &vaf);
424273df556SFrank Mayhar 	va_end(args);
425273df556SFrank Mayhar 
426273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
427273df556SFrank Mayhar }
428273df556SFrank Mayhar 
429c398eda0STheodore Ts'o void ext4_error_file(struct file *file, const char *function,
430f7c21177STheodore Ts'o 		     unsigned int line, ext4_fsblk_t block,
431f7c21177STheodore Ts'o 		     const char *fmt, ...)
432273df556SFrank Mayhar {
433273df556SFrank Mayhar 	va_list args;
434f7c21177STheodore Ts'o 	struct va_format vaf;
4351c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
436273df556SFrank Mayhar 	struct inode *inode = file->f_dentry->d_inode;
437273df556SFrank Mayhar 	char pathname[80], *path;
438273df556SFrank Mayhar 
4391c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
4401c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
4411c13d5c0STheodore Ts'o 	save_error_info(inode->i_sb, function, line);
442273df556SFrank Mayhar 	path = d_path(&(file->f_path), pathname, sizeof(pathname));
443f9a62d09SDan Carpenter 	if (IS_ERR(path))
444273df556SFrank Mayhar 		path = "(unknown)";
445273df556SFrank Mayhar 	printk(KERN_CRIT
446f7c21177STheodore Ts'o 	       "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
447f7c21177STheodore Ts'o 	       inode->i_sb->s_id, function, line, inode->i_ino);
448f7c21177STheodore Ts'o 	if (block)
449f7c21177STheodore Ts'o 		printk(KERN_CONT "block %llu: ", block);
450f7c21177STheodore Ts'o 	va_start(args, fmt);
451f7c21177STheodore Ts'o 	vaf.fmt = fmt;
452f7c21177STheodore Ts'o 	vaf.va = &args;
453f7c21177STheodore Ts'o 	printk(KERN_CONT "comm %s: path %s: %pV\n", current->comm, path, &vaf);
454273df556SFrank Mayhar 	va_end(args);
455273df556SFrank Mayhar 
456273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
457273df556SFrank Mayhar }
458273df556SFrank Mayhar 
459617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
460ac27a0ecSDave Kleikamp 				     char nbuf[16])
461ac27a0ecSDave Kleikamp {
462ac27a0ecSDave Kleikamp 	char *errstr = NULL;
463ac27a0ecSDave Kleikamp 
464ac27a0ecSDave Kleikamp 	switch (errno) {
465ac27a0ecSDave Kleikamp 	case -EIO:
466ac27a0ecSDave Kleikamp 		errstr = "IO failure";
467ac27a0ecSDave Kleikamp 		break;
468ac27a0ecSDave Kleikamp 	case -ENOMEM:
469ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
470ac27a0ecSDave Kleikamp 		break;
471ac27a0ecSDave Kleikamp 	case -EROFS:
47278f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
47378f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
474ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
475ac27a0ecSDave Kleikamp 		else
476ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
477ac27a0ecSDave Kleikamp 		break;
478ac27a0ecSDave Kleikamp 	default:
479ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
480ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
481ac27a0ecSDave Kleikamp 		 * NULL. */
482ac27a0ecSDave Kleikamp 		if (nbuf) {
483ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
484ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
485ac27a0ecSDave Kleikamp 				errstr = nbuf;
486ac27a0ecSDave Kleikamp 		}
487ac27a0ecSDave Kleikamp 		break;
488ac27a0ecSDave Kleikamp 	}
489ac27a0ecSDave Kleikamp 
490ac27a0ecSDave Kleikamp 	return errstr;
491ac27a0ecSDave Kleikamp }
492ac27a0ecSDave Kleikamp 
493617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
494ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
495ac27a0ecSDave Kleikamp 
496c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
497c398eda0STheodore Ts'o 		      unsigned int line, int errno)
498ac27a0ecSDave Kleikamp {
499ac27a0ecSDave Kleikamp 	char nbuf[16];
500ac27a0ecSDave Kleikamp 	const char *errstr;
501ac27a0ecSDave Kleikamp 
502ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
503ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
504ac27a0ecSDave Kleikamp 	 * an error. */
505ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
506ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
507ac27a0ecSDave Kleikamp 		return;
508ac27a0ecSDave Kleikamp 
509617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
510c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
511c398eda0STheodore Ts'o 	       sb->s_id, function, line, errstr);
5121c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
513ac27a0ecSDave Kleikamp 
514617ba13bSMingming Cao 	ext4_handle_error(sb);
515ac27a0ecSDave Kleikamp }
516ac27a0ecSDave Kleikamp 
517ac27a0ecSDave Kleikamp /*
518617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
519ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
520ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
521ac27a0ecSDave Kleikamp  *
522ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
523ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
524ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
525ac27a0ecSDave Kleikamp  */
526ac27a0ecSDave Kleikamp 
527c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
528c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
529ac27a0ecSDave Kleikamp {
530ac27a0ecSDave Kleikamp 	va_list args;
531ac27a0ecSDave Kleikamp 
5321c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
533ac27a0ecSDave Kleikamp 	va_start(args, fmt);
534c398eda0STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
535c398eda0STheodore Ts'o 	       function, line);
536ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
537ac27a0ecSDave Kleikamp 	printk("\n");
538ac27a0ecSDave Kleikamp 	va_end(args);
539ac27a0ecSDave Kleikamp 
5401c13d5c0STheodore Ts'o 	if ((sb->s_flags & MS_RDONLY) == 0) {
541b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
542ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
5434ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
544ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
545dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
5461c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
5471c13d5c0STheodore Ts'o 	}
5481c13d5c0STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC))
5491c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
550ac27a0ecSDave Kleikamp }
551ac27a0ecSDave Kleikamp 
5520ff2ea7dSJoe Perches void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
553b31e1552SEric Sandeen {
5540ff2ea7dSJoe Perches 	struct va_format vaf;
555b31e1552SEric Sandeen 	va_list args;
556b31e1552SEric Sandeen 
557b31e1552SEric Sandeen 	va_start(args, fmt);
5580ff2ea7dSJoe Perches 	vaf.fmt = fmt;
5590ff2ea7dSJoe Perches 	vaf.va = &args;
5600ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
561b31e1552SEric Sandeen 	va_end(args);
562b31e1552SEric Sandeen }
563b31e1552SEric Sandeen 
56412062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
565c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
566ac27a0ecSDave Kleikamp {
5670ff2ea7dSJoe Perches 	struct va_format vaf;
568ac27a0ecSDave Kleikamp 	va_list args;
569ac27a0ecSDave Kleikamp 
570ac27a0ecSDave Kleikamp 	va_start(args, fmt);
5710ff2ea7dSJoe Perches 	vaf.fmt = fmt;
5720ff2ea7dSJoe Perches 	vaf.va = &args;
5730ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
5740ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
575ac27a0ecSDave Kleikamp 	va_end(args);
576ac27a0ecSDave Kleikamp }
577ac27a0ecSDave Kleikamp 
578e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
579e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
580e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
581e29136f8STheodore Ts'o 			     const char *fmt, ...)
5825d1b1b3fSAneesh Kumar K.V __releases(bitlock)
5835d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
5845d1b1b3fSAneesh Kumar K.V {
5850ff2ea7dSJoe Perches 	struct va_format vaf;
5865d1b1b3fSAneesh Kumar K.V 	va_list args;
5875d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5885d1b1b3fSAneesh Kumar K.V 
5891c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
5901c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
5911c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
5920ff2ea7dSJoe Perches 
5935d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
5940ff2ea7dSJoe Perches 
5950ff2ea7dSJoe Perches 	vaf.fmt = fmt;
5960ff2ea7dSJoe Perches 	vaf.va = &args;
597e29136f8STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u",
598e29136f8STheodore Ts'o 	       sb->s_id, function, line, grp);
599e29136f8STheodore Ts'o 	if (ino)
6000ff2ea7dSJoe Perches 		printk(KERN_CONT "inode %lu: ", ino);
601e29136f8STheodore Ts'o 	if (block)
6020ff2ea7dSJoe Perches 		printk(KERN_CONT "block %llu:", (unsigned long long) block);
6030ff2ea7dSJoe Perches 	printk(KERN_CONT "%pV\n", &vaf);
6045d1b1b3fSAneesh Kumar K.V 	va_end(args);
6055d1b1b3fSAneesh Kumar K.V 
6065d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
607e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
6085d1b1b3fSAneesh Kumar K.V 		return;
6095d1b1b3fSAneesh Kumar K.V 	}
6101c13d5c0STheodore Ts'o 
6115d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
6125d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
6135d1b1b3fSAneesh Kumar K.V 	/*
6145d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
6155d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
6165d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
6175d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
6185d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
6195d1b1b3fSAneesh Kumar K.V 	 * ext4_grp_locked_error() to distinguish beween the
6205d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
6215d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
6225d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
6235d1b1b3fSAneesh Kumar K.V 	 */
6245d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
6255d1b1b3fSAneesh Kumar K.V 	return;
6265d1b1b3fSAneesh Kumar K.V }
6275d1b1b3fSAneesh Kumar K.V 
628617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
629ac27a0ecSDave Kleikamp {
630617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
631ac27a0ecSDave Kleikamp 
632617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
633ac27a0ecSDave Kleikamp 		return;
634ac27a0ecSDave Kleikamp 
63512062dddSEric Sandeen 	ext4_warning(sb,
636ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
637ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
638617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
639ac27a0ecSDave Kleikamp 
640617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
641617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
642617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
643ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
644ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
645ac27a0ecSDave Kleikamp 
646ac27a0ecSDave Kleikamp 	/*
647ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
648ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
649ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
650ac27a0ecSDave Kleikamp 	 */
651ac27a0ecSDave Kleikamp }
652ac27a0ecSDave Kleikamp 
653ac27a0ecSDave Kleikamp /*
654ac27a0ecSDave Kleikamp  * Open the external journal device
655ac27a0ecSDave Kleikamp  */
656b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
657ac27a0ecSDave Kleikamp {
658ac27a0ecSDave Kleikamp 	struct block_device *bdev;
659ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
660ac27a0ecSDave Kleikamp 
661d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
662ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
663ac27a0ecSDave Kleikamp 		goto fail;
664ac27a0ecSDave Kleikamp 	return bdev;
665ac27a0ecSDave Kleikamp 
666ac27a0ecSDave Kleikamp fail:
667b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
668ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
669ac27a0ecSDave Kleikamp 	return NULL;
670ac27a0ecSDave Kleikamp }
671ac27a0ecSDave Kleikamp 
672ac27a0ecSDave Kleikamp /*
673ac27a0ecSDave Kleikamp  * Release the journal device
674ac27a0ecSDave Kleikamp  */
675617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
676ac27a0ecSDave Kleikamp {
677e525fd89STejun Heo 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
678ac27a0ecSDave Kleikamp }
679ac27a0ecSDave Kleikamp 
680617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
681ac27a0ecSDave Kleikamp {
682ac27a0ecSDave Kleikamp 	struct block_device *bdev;
683ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
684ac27a0ecSDave Kleikamp 
685ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
686ac27a0ecSDave Kleikamp 	if (bdev) {
687617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
688ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
689ac27a0ecSDave Kleikamp 	}
690ac27a0ecSDave Kleikamp 	return ret;
691ac27a0ecSDave Kleikamp }
692ac27a0ecSDave Kleikamp 
693ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
694ac27a0ecSDave Kleikamp {
695617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
696ac27a0ecSDave Kleikamp }
697ac27a0ecSDave Kleikamp 
698617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
699ac27a0ecSDave Kleikamp {
700ac27a0ecSDave Kleikamp 	struct list_head *l;
701ac27a0ecSDave Kleikamp 
702b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
703ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
704ac27a0ecSDave Kleikamp 
705ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
706ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
707ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
708ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
709ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
710ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
711ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
712ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
713ac27a0ecSDave Kleikamp 	}
714ac27a0ecSDave Kleikamp }
715ac27a0ecSDave Kleikamp 
716617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
717ac27a0ecSDave Kleikamp {
718617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
719617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
720ef2cabf7SHidehiro Kawai 	int i, err;
721ac27a0ecSDave Kleikamp 
722857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
723e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
724e0ccfd95SChristoph Hellwig 
7254c0425ffSMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
7264c0425ffSMingming Cao 	destroy_workqueue(sbi->dio_unwritten_wq);
7274c0425ffSMingming Cao 
728a9e220f8SAl Viro 	lock_super(sb);
7298c85e125SChristoph Hellwig 	if (sb->s_dirt)
730ebc1ac16SChristoph Hellwig 		ext4_commit_super(sb, 1);
7318c85e125SChristoph Hellwig 
7320390131bSFrank Mayhar 	if (sbi->s_journal) {
733ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
73447b4a50bSJan Kara 		sbi->s_journal = NULL;
735ef2cabf7SHidehiro Kawai 		if (err < 0)
736c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
7370390131bSFrank Mayhar 	}
738d4edac31SJosef Bacik 
739a1c6c569SSergey Senozhatsky 	del_timer(&sbi->s_err_report);
740d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
741d4edac31SJosef Bacik 	ext4_mb_release(sb);
742d4edac31SJosef Bacik 	ext4_ext_release(sb);
743d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
744d4edac31SJosef Bacik 
745ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
746617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
747ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
748e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
749ac27a0ecSDave Kleikamp 	}
750240799cdSTheodore Ts'o 	if (sbi->s_proc) {
7519f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
752240799cdSTheodore Ts'o 	}
7533197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
754ac27a0ecSDave Kleikamp 
755ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
756ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
757ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
758c5ca7c76STheodore Ts'o 	if (is_vmalloc_addr(sbi->s_flex_groups))
759c5ca7c76STheodore Ts'o 		vfree(sbi->s_flex_groups);
760c5ca7c76STheodore Ts'o 	else
761772cb7c8SJose R. Santos 		kfree(sbi->s_flex_groups);
762ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
763ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
764ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
7656bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
766ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
767ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
768ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
769ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
770ac27a0ecSDave Kleikamp #endif
771ac27a0ecSDave Kleikamp 
772ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
773ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
774ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
775ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
776ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
777ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
778ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
779ac27a0ecSDave Kleikamp 
780f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
781ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
782ac27a0ecSDave Kleikamp 		/*
783ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
784ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
785ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
786ac27a0ecSDave Kleikamp 		 */
787ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
788f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
789617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
790ac27a0ecSDave Kleikamp 	}
791ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
7923197ebdbSTheodore Ts'o 	/*
7933197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
7943197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
7953197ebdbSTheodore Ts'o 	 */
7963197ebdbSTheodore Ts'o 	unlock_super(sb);
7973197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
7983197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
799705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
800ac27a0ecSDave Kleikamp 	kfree(sbi);
801ac27a0ecSDave Kleikamp }
802ac27a0ecSDave Kleikamp 
803e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
804ac27a0ecSDave Kleikamp 
805ac27a0ecSDave Kleikamp /*
806ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
807ac27a0ecSDave Kleikamp  */
808617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
809ac27a0ecSDave Kleikamp {
810617ba13bSMingming Cao 	struct ext4_inode_info *ei;
811ac27a0ecSDave Kleikamp 
812e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
813ac27a0ecSDave Kleikamp 	if (!ei)
814ac27a0ecSDave Kleikamp 		return NULL;
8150b8e58a1SAndreas Dilger 
816ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
81791246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
818a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
819c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
820c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
821d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
822d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
823d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
8249d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
825d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
826a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
827a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
828a9e7f447SDmitry Monakhov #endif
8298aefcd55STheodore Ts'o 	ei->jinode = NULL;
830c7064ef1SJiaying Zhang 	INIT_LIST_HEAD(&ei->i_completed_io_list);
831744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
8328d5d02e6SMingming Cao 	ei->cur_aio_dio = NULL;
833b436b9beSJan Kara 	ei->i_sync_tid = 0;
834b436b9beSJan Kara 	ei->i_datasync_tid = 0;
835f7ad6d2eSTheodore Ts'o 	atomic_set(&ei->i_ioend_count, 0);
8360b8e58a1SAndreas Dilger 
837ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
838ac27a0ecSDave Kleikamp }
839ac27a0ecSDave Kleikamp 
8407ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
8417ff9c073STheodore Ts'o {
8427ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
8437ff9c073STheodore Ts'o 
8447ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
8457ff9c073STheodore Ts'o 	return drop;
8467ff9c073STheodore Ts'o }
8477ff9c073STheodore Ts'o 
848fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
849fa0d7e3dSNick Piggin {
850fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
851fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
852fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
853fa0d7e3dSNick Piggin }
854fa0d7e3dSNick Piggin 
855617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
856ac27a0ecSDave Kleikamp {
857f7ad6d2eSTheodore Ts'o 	ext4_ioend_wait(inode);
8589f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
859b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
860b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
861b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
8629f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
8639f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
8649f7dd93dSVasily Averin 				true);
8659f7dd93dSVasily Averin 		dump_stack();
8669f7dd93dSVasily Averin 	}
867fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
868ac27a0ecSDave Kleikamp }
869ac27a0ecSDave Kleikamp 
87051cc5068SAlexey Dobriyan static void init_once(void *foo)
871ac27a0ecSDave Kleikamp {
872617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
873ac27a0ecSDave Kleikamp 
874ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
87503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
876ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
877ac27a0ecSDave Kleikamp #endif
8780e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
879ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
880ac27a0ecSDave Kleikamp }
881ac27a0ecSDave Kleikamp 
882ac27a0ecSDave Kleikamp static int init_inodecache(void)
883ac27a0ecSDave Kleikamp {
884617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
885617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
886ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
887ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
88820c2df83SPaul Mundt 					     init_once);
889617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
890ac27a0ecSDave Kleikamp 		return -ENOMEM;
891ac27a0ecSDave Kleikamp 	return 0;
892ac27a0ecSDave Kleikamp }
893ac27a0ecSDave Kleikamp 
894ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
895ac27a0ecSDave Kleikamp {
896617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
897ac27a0ecSDave Kleikamp }
898ac27a0ecSDave Kleikamp 
8990930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
900ac27a0ecSDave Kleikamp {
9010930fcc1SAl Viro 	invalidate_inode_buffers(inode);
9020930fcc1SAl Viro 	end_writeback(inode);
9039f754758SChristoph Hellwig 	dquot_drop(inode);
904c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
9058aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
9068aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
9078aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
9088aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
9098aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
9108aefcd55STheodore Ts'o 	}
911ac27a0ecSDave Kleikamp }
912ac27a0ecSDave Kleikamp 
9132b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
9142b2d6d01STheodore Ts'o 					   struct super_block *sb)
915ac27a0ecSDave Kleikamp {
916ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
917617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
918ac27a0ecSDave Kleikamp 
9195a20bdfcSJan Kara 	if (sbi->s_jquota_fmt) {
9205a20bdfcSJan Kara 		char *fmtname = "";
9215a20bdfcSJan Kara 
9225a20bdfcSJan Kara 		switch (sbi->s_jquota_fmt) {
9235a20bdfcSJan Kara 		case QFMT_VFS_OLD:
9245a20bdfcSJan Kara 			fmtname = "vfsold";
9255a20bdfcSJan Kara 			break;
9265a20bdfcSJan Kara 		case QFMT_VFS_V0:
9275a20bdfcSJan Kara 			fmtname = "vfsv0";
9285a20bdfcSJan Kara 			break;
9295a20bdfcSJan Kara 		case QFMT_VFS_V1:
9305a20bdfcSJan Kara 			fmtname = "vfsv1";
9315a20bdfcSJan Kara 			break;
9325a20bdfcSJan Kara 		}
9335a20bdfcSJan Kara 		seq_printf(seq, ",jqfmt=%s", fmtname);
9345a20bdfcSJan Kara 	}
935ac27a0ecSDave Kleikamp 
936ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
937ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
938ac27a0ecSDave Kleikamp 
939ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
940ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
941ac27a0ecSDave Kleikamp 
942482a7425SDmitry Monakhov 	if (test_opt(sb, USRQUOTA))
943ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
944ac27a0ecSDave Kleikamp 
945482a7425SDmitry Monakhov 	if (test_opt(sb, GRPQUOTA))
946ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
947ac27a0ecSDave Kleikamp #endif
948ac27a0ecSDave Kleikamp }
949ac27a0ecSDave Kleikamp 
950d9c9bef1SMiklos Szeredi /*
951d9c9bef1SMiklos Szeredi  * Show an option if
952d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
953d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
954d9c9bef1SMiklos Szeredi  */
955617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
956ac27a0ecSDave Kleikamp {
957aa22df2cSAneesh Kumar K.V 	int def_errors;
958aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
959ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
960d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
961d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
962d9c9bef1SMiklos Szeredi 
963d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
964aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
965d9c9bef1SMiklos Szeredi 
966d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
967d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
968d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
969d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
970aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
971d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
972d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
973d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
974d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
975d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
976d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
977d9c9bef1SMiklos Szeredi 	}
978d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
979d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
980d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
981d9c9bef1SMiklos Szeredi 	}
982bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
983d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
984bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
985d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
986bb4f397aSAneesh Kumar K.V 		}
987bb4f397aSAneesh Kumar K.V 	}
988aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
989bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
990aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
991d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
992aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
993d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
994aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
995d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
996d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
997d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
99803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
999aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
1000aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
1001d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
1002d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
1003d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
1004d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
1005d9c9bef1SMiklos Szeredi 	}
1006d9c9bef1SMiklos Szeredi #endif
100703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1008aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
1009d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
1010d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
1011d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
1012d9c9bef1SMiklos Szeredi #endif
101330773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
1014d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
1015d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
1016d9c9bef1SMiklos Szeredi 	}
101730773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
101830773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
101930773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
102030773840STheodore Ts'o 	}
102130773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
102230773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
102330773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
102430773840STheodore Ts'o 	}
102530773840STheodore Ts'o 
1026571640caSEric Sandeen 	/*
1027571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
1028571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
1029571640caSEric Sandeen 	 * status is.
1030571640caSEric Sandeen 	 */
1031571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
1032571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
1033cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
1034cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
103539a4badeSJan Kara 	else if (test_opt(sb, JOURNAL_CHECKSUM))
103639a4badeSJan Kara 		seq_puts(seq, ",journal_checksum");
103725ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
103825ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
10398b67f04aSTheodore Ts'o 	if (!test_opt(sb, DELALLOC) &&
10408b67f04aSTheodore Ts'o 	    !(def_mount_opts & EXT4_DEFM_NODELALLOC))
1041dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
1042dd919b98SAneesh Kumar K.V 
10431449032bSTheodore Ts'o 	if (test_opt(sb, MBLK_IO_SUBMIT))
10441449032bSTheodore Ts'o 		seq_puts(seq, ",mblk_io_submit");
1045cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
1046cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
1047aa22df2cSAneesh Kumar K.V 	/*
1048aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
1049aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
1050aa22df2cSAneesh Kumar K.V 	 */
1051617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
1052ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
1053617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
1054ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
1055617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
1056ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
1057ac27a0ecSDave Kleikamp 
1058240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
1059240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
1060240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
1061240799cdSTheodore Ts'o 
10625bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
10635bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
10645bf5683aSHidehiro Kawai 
1065afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
106606705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
1067afd4672dSTheodore Ts'o 
10688b67f04aSTheodore Ts'o 	if (test_opt(sb, DISCARD) && !(def_mount_opts & EXT4_DEFM_DISCARD))
10695328e635SEric Sandeen 		seq_puts(seq, ",discard");
10705328e635SEric Sandeen 
1071e3bb52aeSEric Sandeen 	if (test_opt(sb, NOLOAD))
1072e3bb52aeSEric Sandeen 		seq_puts(seq, ",norecovery");
1073e3bb52aeSEric Sandeen 
1074744692dcSJiaying Zhang 	if (test_opt(sb, DIOREAD_NOLOCK))
1075744692dcSJiaying Zhang 		seq_puts(seq, ",dioread_nolock");
1076744692dcSJiaying Zhang 
10778b67f04aSTheodore Ts'o 	if (test_opt(sb, BLOCK_VALIDITY) &&
10788b67f04aSTheodore Ts'o 	    !(def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY))
10798b67f04aSTheodore Ts'o 		seq_puts(seq, ",block_validity");
10808b67f04aSTheodore Ts'o 
1081bfff6873SLukas Czerner 	if (!test_opt(sb, INIT_INODE_TABLE))
1082bfff6873SLukas Czerner 		seq_puts(seq, ",noinit_inode_table");
1083bfff6873SLukas Czerner 	else if (sbi->s_li_wait_mult)
1084bfff6873SLukas Czerner 		seq_printf(seq, ",init_inode_table=%u",
1085bfff6873SLukas Czerner 			   (unsigned) sbi->s_li_wait_mult);
1086bfff6873SLukas Czerner 
1087617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
10880b8e58a1SAndreas Dilger 
1089ac27a0ecSDave Kleikamp 	return 0;
1090ac27a0ecSDave Kleikamp }
1091ac27a0ecSDave Kleikamp 
10921b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
10931b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1094ac27a0ecSDave Kleikamp {
1095ac27a0ecSDave Kleikamp 	struct inode *inode;
1096ac27a0ecSDave Kleikamp 
1097617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1098ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1099617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1100ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1101ac27a0ecSDave Kleikamp 
1102ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1103ac27a0ecSDave Kleikamp 	 *
1104617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1105ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1106ac27a0ecSDave Kleikamp 	 *
1107ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1108ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1109ac27a0ecSDave Kleikamp 	 */
11101d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
11111d1fe1eeSDavid Howells 	if (IS_ERR(inode))
11121d1fe1eeSDavid Howells 		return ERR_CAST(inode);
11131d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1114ac27a0ecSDave Kleikamp 		iput(inode);
1115ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1116ac27a0ecSDave Kleikamp 	}
11171b961ac0SChristoph Hellwig 
11181b961ac0SChristoph Hellwig 	return inode;
1119ac27a0ecSDave Kleikamp }
11201b961ac0SChristoph Hellwig 
11211b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
11221b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
11231b961ac0SChristoph Hellwig {
11241b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
11251b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
11261b961ac0SChristoph Hellwig }
11271b961ac0SChristoph Hellwig 
11281b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
11291b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
11301b961ac0SChristoph Hellwig {
11311b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
11321b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1133ac27a0ecSDave Kleikamp }
1134ac27a0ecSDave Kleikamp 
1135c39a7f84SToshiyuki Okajima /*
1136c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1137c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1138c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1139c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1140c39a7f84SToshiyuki Okajima  */
11410b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
11420b8e58a1SAndreas Dilger 				 gfp_t wait)
1143c39a7f84SToshiyuki Okajima {
1144c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1145c39a7f84SToshiyuki Okajima 
1146c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1147c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1148c39a7f84SToshiyuki Okajima 		return 0;
1149c39a7f84SToshiyuki Okajima 	if (journal)
1150c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1151c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1152c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1153c39a7f84SToshiyuki Okajima }
1154c39a7f84SToshiyuki Okajima 
1155ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1156ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1157ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1158ac27a0ecSDave Kleikamp 
1159617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1160617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1161617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1162617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1163617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
11646f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1165f00c9e44SJan Kara 			 struct path *path);
1166ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type);
1167617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1168617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1169ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1170617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1171ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
1172ac27a0ecSDave Kleikamp 
117361e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
1174a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
117560e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1176a9e7f447SDmitry Monakhov #endif
1177617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1178617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1179617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1180617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1181a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1182a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1183a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1184ac27a0ecSDave Kleikamp };
1185ac27a0ecSDave Kleikamp 
11860d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1187617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1188ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1189287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
1190287a8095SChristoph Hellwig 	.get_info	= dquot_get_dqinfo,
1191287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1192287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1193287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1194ac27a0ecSDave Kleikamp };
1195ac27a0ecSDave Kleikamp #endif
1196ac27a0ecSDave Kleikamp 
1197ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1198617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1199617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1200617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1201617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
12027ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
12030930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1204617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1205617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1206c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1207c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1208617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1209617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1210617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1211ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1212617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1213617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1214ac27a0ecSDave Kleikamp #endif
1215c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1216ac27a0ecSDave Kleikamp };
1217ac27a0ecSDave Kleikamp 
12189ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
12199ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
12209ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
12219ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
12229ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
12237ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
12240930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
12259ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
12269ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
12279ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
12289ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
12299ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
12309ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
12319ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
12329ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
12339ca92389STheodore Ts'o #endif
12349ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
12359ca92389STheodore Ts'o };
12369ca92389STheodore Ts'o 
123739655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
12381b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
12391b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1240617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1241ac27a0ecSDave Kleikamp };
1242ac27a0ecSDave Kleikamp 
1243ac27a0ecSDave Kleikamp enum {
1244ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1245ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
124601436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1247ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
124806705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
124930773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1250c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1251818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1252ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1253296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1254ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
12555a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
12565a20bdfcSJan Kara 	Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
12575a20bdfcSJan Kara 	Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
12581449032bSTheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
12591449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
12605328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1261744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
12625328e635SEric Sandeen 	Opt_discard, Opt_nodiscard,
1263bfff6873SLukas Czerner 	Opt_init_inode_table, Opt_noinit_inode_table,
1264ac27a0ecSDave Kleikamp };
1265ac27a0ecSDave Kleikamp 
1266a447c093SSteven Whitehouse static const match_table_t tokens = {
1267ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1268ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1269ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1270ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1271ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1272ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1273ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1274ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1275ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1276ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1277ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1278ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1279ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1280ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1281ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1282ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1283ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1284ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1285ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1286ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1287ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1288e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
1289ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1290ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1291ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
129230773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
129330773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1294ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1295ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1296818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1297818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1298ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1299ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1300ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1301ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
13025bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
13035bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1304ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1305ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1306ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1307ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1308ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1309ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
13105a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1311ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1312ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1313ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1314ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1315ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
131606705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
131706705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
131825ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1319c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1320ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
132164769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1322dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
13231449032bSTheodore Ts'o 	{Opt_mblk_io_submit, "mblk_io_submit"},
13241449032bSTheodore Ts'o 	{Opt_nomblk_io_submit, "nomblk_io_submit"},
13256fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
13266fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1327240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1328b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1329afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
133006705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
133106705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1332744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1333744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
13345328e635SEric Sandeen 	{Opt_discard, "discard"},
13355328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1336bfff6873SLukas Czerner 	{Opt_init_inode_table, "init_itable=%u"},
1337bfff6873SLukas Czerner 	{Opt_init_inode_table, "init_itable"},
1338bfff6873SLukas Czerner 	{Opt_noinit_inode_table, "noinit_itable"},
1339f3f12faaSJosef Bacik 	{Opt_err, NULL},
1340ac27a0ecSDave Kleikamp };
1341ac27a0ecSDave Kleikamp 
1342617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1343ac27a0ecSDave Kleikamp {
1344617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1345ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1346ac27a0ecSDave Kleikamp 
1347ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1348ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
13490b8e58a1SAndreas Dilger 
1350ac27a0ecSDave Kleikamp 	options += 3;
13510b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1352ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1353ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
13544776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1355ac27a0ecSDave Kleikamp 		       (char *) *data);
1356ac27a0ecSDave Kleikamp 		return 1;
1357ac27a0ecSDave Kleikamp 	}
1358ac27a0ecSDave Kleikamp 	if (*options == ',')
1359ac27a0ecSDave Kleikamp 		options++;
1360ac27a0ecSDave Kleikamp 	*data = (void *) options;
13610b8e58a1SAndreas Dilger 
1362ac27a0ecSDave Kleikamp 	return sb_block;
1363ac27a0ecSDave Kleikamp }
1364ac27a0ecSDave Kleikamp 
1365b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1366437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1367437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1368b3881f74STheodore Ts'o 
136956c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
137056c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
137156c50f11SDmitry Monakhov {
137256c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
137356c50f11SDmitry Monakhov 	char *qname;
137456c50f11SDmitry Monakhov 
137556c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
137656c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
137756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
137856c50f11SDmitry Monakhov 			"Cannot change journaled "
137956c50f11SDmitry Monakhov 			"quota options when quota turned on");
138056c50f11SDmitry Monakhov 		return 0;
138156c50f11SDmitry Monakhov 	}
138256c50f11SDmitry Monakhov 	qname = match_strdup(args);
138356c50f11SDmitry Monakhov 	if (!qname) {
138456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
138556c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
138656c50f11SDmitry Monakhov 		return 0;
138756c50f11SDmitry Monakhov 	}
138856c50f11SDmitry Monakhov 	if (sbi->s_qf_names[qtype] &&
138956c50f11SDmitry Monakhov 		strcmp(sbi->s_qf_names[qtype], qname)) {
139056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
139156c50f11SDmitry Monakhov 			"%s quota file already specified", QTYPE2NAME(qtype));
139256c50f11SDmitry Monakhov 		kfree(qname);
139356c50f11SDmitry Monakhov 		return 0;
139456c50f11SDmitry Monakhov 	}
139556c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = qname;
139656c50f11SDmitry Monakhov 	if (strchr(sbi->s_qf_names[qtype], '/')) {
139756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
139856c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
139956c50f11SDmitry Monakhov 		kfree(sbi->s_qf_names[qtype]);
140056c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype] = NULL;
140156c50f11SDmitry Monakhov 		return 0;
140256c50f11SDmitry Monakhov 	}
1403fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
140456c50f11SDmitry Monakhov 	return 1;
140556c50f11SDmitry Monakhov }
140656c50f11SDmitry Monakhov 
140756c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
140856c50f11SDmitry Monakhov {
140956c50f11SDmitry Monakhov 
141056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
141156c50f11SDmitry Monakhov 
141256c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
141356c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
141456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
141556c50f11SDmitry Monakhov 			" when quota turned on");
141656c50f11SDmitry Monakhov 		return 0;
141756c50f11SDmitry Monakhov 	}
141856c50f11SDmitry Monakhov 	/*
141956c50f11SDmitry Monakhov 	 * The space will be released later when all options are confirmed
142056c50f11SDmitry Monakhov 	 * to be correct
142156c50f11SDmitry Monakhov 	 */
142256c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
142356c50f11SDmitry Monakhov 	return 1;
142456c50f11SDmitry Monakhov }
142556c50f11SDmitry Monakhov #endif
142656c50f11SDmitry Monakhov 
1427ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1428c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1429b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1430617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1431ac27a0ecSDave Kleikamp {
1432617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1433ac27a0ecSDave Kleikamp 	char *p;
1434ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1435ac27a0ecSDave Kleikamp 	int data_opt = 0;
1436ac27a0ecSDave Kleikamp 	int option;
1437ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
143856c50f11SDmitry Monakhov 	int qfmt;
1439ac27a0ecSDave Kleikamp #endif
1440ac27a0ecSDave Kleikamp 
1441ac27a0ecSDave Kleikamp 	if (!options)
1442ac27a0ecSDave Kleikamp 		return 1;
1443ac27a0ecSDave Kleikamp 
1444ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1445ac27a0ecSDave Kleikamp 		int token;
1446ac27a0ecSDave Kleikamp 		if (!*p)
1447ac27a0ecSDave Kleikamp 			continue;
1448ac27a0ecSDave Kleikamp 
144915121c18SEric Sandeen 		/*
145015121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
145115121c18SEric Sandeen 		 * found; some options take optional arguments.
145215121c18SEric Sandeen 		 */
145315121c18SEric Sandeen 		args[0].to = args[0].from = 0;
1454ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1455ac27a0ecSDave Kleikamp 		switch (token) {
1456ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1457437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1458fd8c37ecSTheodore Ts'o 			clear_opt(sb, MINIX_DF);
1459ac27a0ecSDave Kleikamp 			break;
1460ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1461437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1462fd8c37ecSTheodore Ts'o 			set_opt(sb, MINIX_DF);
1463437ca0fdSDmitry Monakhov 
1464ac27a0ecSDave Kleikamp 			break;
1465ac27a0ecSDave Kleikamp 		case Opt_grpid:
1466437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1467fd8c37ecSTheodore Ts'o 			set_opt(sb, GRPID);
1468437ca0fdSDmitry Monakhov 
1469ac27a0ecSDave Kleikamp 			break;
1470ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1471437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1472fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPID);
1473437ca0fdSDmitry Monakhov 
1474ac27a0ecSDave Kleikamp 			break;
1475ac27a0ecSDave Kleikamp 		case Opt_resuid:
1476ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1477ac27a0ecSDave Kleikamp 				return 0;
1478ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1479ac27a0ecSDave Kleikamp 			break;
1480ac27a0ecSDave Kleikamp 		case Opt_resgid:
1481ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1482ac27a0ecSDave Kleikamp 				return 0;
1483ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1484ac27a0ecSDave Kleikamp 			break;
1485ac27a0ecSDave Kleikamp 		case Opt_sb:
1486ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1487ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1488ac27a0ecSDave Kleikamp 			break;
1489ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1490fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_CONT);
1491fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_RO);
1492fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_PANIC);
1493ac27a0ecSDave Kleikamp 			break;
1494ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1495fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_CONT);
1496fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_PANIC);
1497fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_RO);
1498ac27a0ecSDave Kleikamp 			break;
1499ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1500fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_RO);
1501fd8c37ecSTheodore Ts'o 			clear_opt(sb, ERRORS_PANIC);
1502fd8c37ecSTheodore Ts'o 			set_opt(sb, ERRORS_CONT);
1503ac27a0ecSDave Kleikamp 			break;
1504ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1505fd8c37ecSTheodore Ts'o 			set_opt(sb, NO_UID32);
1506ac27a0ecSDave Kleikamp 			break;
1507ac27a0ecSDave Kleikamp 		case Opt_debug:
1508fd8c37ecSTheodore Ts'o 			set_opt(sb, DEBUG);
1509ac27a0ecSDave Kleikamp 			break;
1510ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1511fd8c37ecSTheodore Ts'o 			set_opt(sb, OLDALLOC);
1512ac27a0ecSDave Kleikamp 			break;
1513ac27a0ecSDave Kleikamp 		case Opt_orlov:
1514fd8c37ecSTheodore Ts'o 			clear_opt(sb, OLDALLOC);
1515ac27a0ecSDave Kleikamp 			break;
151603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1517ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1518fd8c37ecSTheodore Ts'o 			set_opt(sb, XATTR_USER);
1519ac27a0ecSDave Kleikamp 			break;
1520ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1521fd8c37ecSTheodore Ts'o 			clear_opt(sb, XATTR_USER);
1522ac27a0ecSDave Kleikamp 			break;
1523ac27a0ecSDave Kleikamp #else
1524ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1525ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1526b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1527ac27a0ecSDave Kleikamp 			break;
1528ac27a0ecSDave Kleikamp #endif
152903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1530ac27a0ecSDave Kleikamp 		case Opt_acl:
1531fd8c37ecSTheodore Ts'o 			set_opt(sb, POSIX_ACL);
1532ac27a0ecSDave Kleikamp 			break;
1533ac27a0ecSDave Kleikamp 		case Opt_noacl:
1534fd8c37ecSTheodore Ts'o 			clear_opt(sb, POSIX_ACL);
1535ac27a0ecSDave Kleikamp 			break;
1536ac27a0ecSDave Kleikamp #else
1537ac27a0ecSDave Kleikamp 		case Opt_acl:
1538ac27a0ecSDave Kleikamp 		case Opt_noacl:
1539b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1540ac27a0ecSDave Kleikamp 			break;
1541ac27a0ecSDave Kleikamp #endif
1542ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1543ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1544ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1545ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1546ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1547ac27a0ecSDave Kleikamp 			   journal file. */
1548ac27a0ecSDave Kleikamp 			if (is_remount) {
1549b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1550b31e1552SEric Sandeen 					 "Cannot specify journal on remount");
1551ac27a0ecSDave Kleikamp 				return 0;
1552ac27a0ecSDave Kleikamp 			}
1553fd8c37ecSTheodore Ts'o 			set_opt(sb, UPDATE_JOURNAL);
1554ac27a0ecSDave Kleikamp 			break;
1555ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1556ac27a0ecSDave Kleikamp 			if (is_remount) {
1557b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1558b31e1552SEric Sandeen 					"Cannot specify journal on remount");
1559ac27a0ecSDave Kleikamp 				return 0;
1560ac27a0ecSDave Kleikamp 			}
1561ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1562ac27a0ecSDave Kleikamp 				return 0;
1563ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1564ac27a0ecSDave Kleikamp 			break;
1565818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1566fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_CHECKSUM);
1567d4da6c9cSLinus Torvalds 			break;
1568818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1569fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_ASYNC_COMMIT);
1570fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_CHECKSUM);
1571818d276cSGirish Shilamkar 			break;
1572ac27a0ecSDave Kleikamp 		case Opt_noload:
1573fd8c37ecSTheodore Ts'o 			set_opt(sb, NOLOAD);
1574ac27a0ecSDave Kleikamp 			break;
1575ac27a0ecSDave Kleikamp 		case Opt_commit:
1576ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1577ac27a0ecSDave Kleikamp 				return 0;
1578ac27a0ecSDave Kleikamp 			if (option < 0)
1579ac27a0ecSDave Kleikamp 				return 0;
1580ac27a0ecSDave Kleikamp 			if (option == 0)
1581cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1582ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1583ac27a0ecSDave Kleikamp 			break;
158430773840STheodore Ts'o 		case Opt_max_batch_time:
158530773840STheodore Ts'o 			if (match_int(&args[0], &option))
158630773840STheodore Ts'o 				return 0;
158730773840STheodore Ts'o 			if (option < 0)
158830773840STheodore Ts'o 				return 0;
158930773840STheodore Ts'o 			if (option == 0)
159030773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
159130773840STheodore Ts'o 			sbi->s_max_batch_time = option;
159230773840STheodore Ts'o 			break;
159330773840STheodore Ts'o 		case Opt_min_batch_time:
159430773840STheodore Ts'o 			if (match_int(&args[0], &option))
159530773840STheodore Ts'o 				return 0;
159630773840STheodore Ts'o 			if (option < 0)
159730773840STheodore Ts'o 				return 0;
159830773840STheodore Ts'o 			sbi->s_min_batch_time = option;
159930773840STheodore Ts'o 			break;
1600ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1601617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1602ac27a0ecSDave Kleikamp 			goto datacheck;
1603ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1604617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1605ac27a0ecSDave Kleikamp 			goto datacheck;
1606ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1607617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1608ac27a0ecSDave Kleikamp 		datacheck:
1609ac27a0ecSDave Kleikamp 			if (is_remount) {
1610482a7425SDmitry Monakhov 				if (test_opt(sb, DATA_FLAGS) != data_opt) {
1611b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
1612b31e1552SEric Sandeen 						"Cannot change data mode on remount");
1613ac27a0ecSDave Kleikamp 					return 0;
1614ac27a0ecSDave Kleikamp 				}
1615ac27a0ecSDave Kleikamp 			} else {
1616fd8c37ecSTheodore Ts'o 				clear_opt(sb, DATA_FLAGS);
1617ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1618ac27a0ecSDave Kleikamp 			}
1619ac27a0ecSDave Kleikamp 			break;
16205bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
1621fd8c37ecSTheodore Ts'o 			set_opt(sb, DATA_ERR_ABORT);
16225bf5683aSHidehiro Kawai 			break;
16235bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
1624fd8c37ecSTheodore Ts'o 			clear_opt(sb, DATA_ERR_ABORT);
16255bf5683aSHidehiro Kawai 			break;
1626ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1627ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
162856c50f11SDmitry Monakhov 			if (!set_qf_name(sb, USRQUOTA, &args[0]))
162956c50f11SDmitry Monakhov 				return 0;
163056c50f11SDmitry Monakhov 			break;
1631ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
163256c50f11SDmitry Monakhov 			if (!set_qf_name(sb, GRPQUOTA, &args[0]))
1633ac27a0ecSDave Kleikamp 				return 0;
1634ac27a0ecSDave Kleikamp 			break;
1635ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
163656c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, USRQUOTA))
1637ac27a0ecSDave Kleikamp 				return 0;
1638ac27a0ecSDave Kleikamp 			break;
163956c50f11SDmitry Monakhov 		case Opt_offgrpjquota:
164056c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, GRPQUOTA))
164156c50f11SDmitry Monakhov 				return 0;
164256c50f11SDmitry Monakhov 			break;
164356c50f11SDmitry Monakhov 
1644ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1645dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1646dfc5d03fSJan Kara 			goto set_qf_format;
1647ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1648dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
16495a20bdfcSJan Kara 			goto set_qf_format;
16505a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
16515a20bdfcSJan Kara 			qfmt = QFMT_VFS_V1;
1652dfc5d03fSJan Kara set_qf_format:
165317bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1654dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1655b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
1656dfc5d03fSJan Kara 					"journaled quota options when "
1657b31e1552SEric Sandeen 					"quota turned on");
1658dfc5d03fSJan Kara 				return 0;
1659dfc5d03fSJan Kara 			}
1660dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1661ac27a0ecSDave Kleikamp 			break;
1662ac27a0ecSDave Kleikamp 		case Opt_quota:
1663ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1664fd8c37ecSTheodore Ts'o 			set_opt(sb, QUOTA);
1665fd8c37ecSTheodore Ts'o 			set_opt(sb, USRQUOTA);
1666ac27a0ecSDave Kleikamp 			break;
1667ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1668fd8c37ecSTheodore Ts'o 			set_opt(sb, QUOTA);
1669fd8c37ecSTheodore Ts'o 			set_opt(sb, GRPQUOTA);
1670ac27a0ecSDave Kleikamp 			break;
1671ac27a0ecSDave Kleikamp 		case Opt_noquota:
167217bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1673b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change quota "
1674b31e1552SEric Sandeen 					"options when quota turned on");
1675ac27a0ecSDave Kleikamp 				return 0;
1676ac27a0ecSDave Kleikamp 			}
1677fd8c37ecSTheodore Ts'o 			clear_opt(sb, QUOTA);
1678fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1679fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1680ac27a0ecSDave Kleikamp 			break;
1681ac27a0ecSDave Kleikamp #else
1682ac27a0ecSDave Kleikamp 		case Opt_quota:
1683ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1684ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1685b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1686b31e1552SEric Sandeen 				"quota options not supported");
1687cd59e7b9SJan Kara 			break;
1688ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1689ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1690ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1691ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1692ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1693ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
16945a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
1695b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1696b31e1552SEric Sandeen 				"journaled quota options not supported");
1697ac27a0ecSDave Kleikamp 			break;
1698ac27a0ecSDave Kleikamp 		case Opt_noquota:
1699ac27a0ecSDave Kleikamp 			break;
1700ac27a0ecSDave Kleikamp #endif
1701ac27a0ecSDave Kleikamp 		case Opt_abort:
17024ab2f15bSTheodore Ts'o 			sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1703ac27a0ecSDave Kleikamp 			break;
170406705bffSTheodore Ts'o 		case Opt_nobarrier:
1705fd8c37ecSTheodore Ts'o 			clear_opt(sb, BARRIER);
170606705bffSTheodore Ts'o 			break;
1707ac27a0ecSDave Kleikamp 		case Opt_barrier:
170815121c18SEric Sandeen 			if (args[0].from) {
170915121c18SEric Sandeen 				if (match_int(&args[0], &option))
171015121c18SEric Sandeen 					return 0;
171115121c18SEric Sandeen 			} else
171215121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1713ac27a0ecSDave Kleikamp 			if (option)
1714fd8c37ecSTheodore Ts'o 				set_opt(sb, BARRIER);
1715ac27a0ecSDave Kleikamp 			else
1716fd8c37ecSTheodore Ts'o 				clear_opt(sb, BARRIER);
1717ac27a0ecSDave Kleikamp 			break;
1718ac27a0ecSDave Kleikamp 		case Opt_ignore:
1719ac27a0ecSDave Kleikamp 			break;
1720ac27a0ecSDave Kleikamp 		case Opt_resize:
1721ac27a0ecSDave Kleikamp 			if (!is_remount) {
1722b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1723b31e1552SEric Sandeen 					"resize option only available "
1724b31e1552SEric Sandeen 					"for remount");
1725ac27a0ecSDave Kleikamp 				return 0;
1726ac27a0ecSDave Kleikamp 			}
1727ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1728ac27a0ecSDave Kleikamp 				return 0;
1729ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1730ac27a0ecSDave Kleikamp 			break;
1731ac27a0ecSDave Kleikamp 		case Opt_nobh:
1732206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1733206f7ab4SChristoph Hellwig 				 "Ignoring deprecated nobh option");
1734ac27a0ecSDave Kleikamp 			break;
1735ac27a0ecSDave Kleikamp 		case Opt_bh:
1736206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1737206f7ab4SChristoph Hellwig 				 "Ignoring deprecated bh option");
1738ac27a0ecSDave Kleikamp 			break;
173925ec56b5SJean Noel Cordenner 		case Opt_i_version:
1740fd8c37ecSTheodore Ts'o 			set_opt(sb, I_VERSION);
174125ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
174225ec56b5SJean Noel Cordenner 			break;
1743dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1744fd8c37ecSTheodore Ts'o 			clear_opt(sb, DELALLOC);
1745dd919b98SAneesh Kumar K.V 			break;
17461449032bSTheodore Ts'o 		case Opt_mblk_io_submit:
1747fd8c37ecSTheodore Ts'o 			set_opt(sb, MBLK_IO_SUBMIT);
17481449032bSTheodore Ts'o 			break;
17491449032bSTheodore Ts'o 		case Opt_nomblk_io_submit:
1750fd8c37ecSTheodore Ts'o 			clear_opt(sb, MBLK_IO_SUBMIT);
17511449032bSTheodore Ts'o 			break;
1752c9de560dSAlex Tomas 		case Opt_stripe:
1753c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1754c9de560dSAlex Tomas 				return 0;
1755c9de560dSAlex Tomas 			if (option < 0)
1756c9de560dSAlex Tomas 				return 0;
1757c9de560dSAlex Tomas 			sbi->s_stripe = option;
1758c9de560dSAlex Tomas 			break;
175964769240SAlex Tomas 		case Opt_delalloc:
1760fd8c37ecSTheodore Ts'o 			set_opt(sb, DELALLOC);
176164769240SAlex Tomas 			break;
17626fd058f7STheodore Ts'o 		case Opt_block_validity:
1763fd8c37ecSTheodore Ts'o 			set_opt(sb, BLOCK_VALIDITY);
17646fd058f7STheodore Ts'o 			break;
17656fd058f7STheodore Ts'o 		case Opt_noblock_validity:
1766fd8c37ecSTheodore Ts'o 			clear_opt(sb, BLOCK_VALIDITY);
17676fd058f7STheodore Ts'o 			break;
1768240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1769240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1770240799cdSTheodore Ts'o 				return 0;
1771240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1772240799cdSTheodore Ts'o 				return 0;
1773f7c43950STheodore Ts'o 			if (!is_power_of_2(option)) {
1774b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1775b31e1552SEric Sandeen 					 "EXT4-fs: inode_readahead_blks"
1776b31e1552SEric Sandeen 					 " must be a power of 2");
17773197ebdbSTheodore Ts'o 				return 0;
17783197ebdbSTheodore Ts'o 			}
1779240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1780240799cdSTheodore Ts'o 			break;
1781b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1782b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1783b3881f74STheodore Ts'o 				return 0;
1784b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1785b3881f74STheodore Ts'o 				break;
1786b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1787b3881f74STheodore Ts'o 							    option);
1788b3881f74STheodore Ts'o 			break;
178906705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
1790fd8c37ecSTheodore Ts'o 			set_opt(sb, NO_AUTO_DA_ALLOC);
179106705bffSTheodore Ts'o 			break;
1792afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
179315121c18SEric Sandeen 			if (args[0].from) {
179415121c18SEric Sandeen 				if (match_int(&args[0], &option))
179515121c18SEric Sandeen 					return 0;
179615121c18SEric Sandeen 			} else
179715121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1798afd4672dSTheodore Ts'o 			if (option)
1799fd8c37ecSTheodore Ts'o 				clear_opt(sb, NO_AUTO_DA_ALLOC);
1800afd4672dSTheodore Ts'o 			else
1801fd8c37ecSTheodore Ts'o 				set_opt(sb,NO_AUTO_DA_ALLOC);
1802afd4672dSTheodore Ts'o 			break;
18035328e635SEric Sandeen 		case Opt_discard:
1804fd8c37ecSTheodore Ts'o 			set_opt(sb, DISCARD);
18055328e635SEric Sandeen 			break;
18065328e635SEric Sandeen 		case Opt_nodiscard:
1807fd8c37ecSTheodore Ts'o 			clear_opt(sb, DISCARD);
18085328e635SEric Sandeen 			break;
1809744692dcSJiaying Zhang 		case Opt_dioread_nolock:
1810fd8c37ecSTheodore Ts'o 			set_opt(sb, DIOREAD_NOLOCK);
1811744692dcSJiaying Zhang 			break;
1812744692dcSJiaying Zhang 		case Opt_dioread_lock:
1813fd8c37ecSTheodore Ts'o 			clear_opt(sb, DIOREAD_NOLOCK);
1814744692dcSJiaying Zhang 			break;
1815bfff6873SLukas Czerner 		case Opt_init_inode_table:
1816fd8c37ecSTheodore Ts'o 			set_opt(sb, INIT_INODE_TABLE);
1817bfff6873SLukas Czerner 			if (args[0].from) {
1818bfff6873SLukas Czerner 				if (match_int(&args[0], &option))
1819bfff6873SLukas Czerner 					return 0;
1820bfff6873SLukas Czerner 			} else
1821bfff6873SLukas Czerner 				option = EXT4_DEF_LI_WAIT_MULT;
1822bfff6873SLukas Czerner 			if (option < 0)
1823bfff6873SLukas Czerner 				return 0;
1824bfff6873SLukas Czerner 			sbi->s_li_wait_mult = option;
1825bfff6873SLukas Czerner 			break;
1826bfff6873SLukas Czerner 		case Opt_noinit_inode_table:
1827fd8c37ecSTheodore Ts'o 			clear_opt(sb, INIT_INODE_TABLE);
1828bfff6873SLukas Czerner 			break;
1829ac27a0ecSDave Kleikamp 		default:
1830b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1831b31e1552SEric Sandeen 			       "Unrecognized mount option \"%s\" "
1832b31e1552SEric Sandeen 			       "or missing value", p);
1833ac27a0ecSDave Kleikamp 			return 0;
1834ac27a0ecSDave Kleikamp 		}
1835ac27a0ecSDave Kleikamp 	}
1836ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1837ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1838482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1839fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
1840ac27a0ecSDave Kleikamp 
1841482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1842fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
1843ac27a0ecSDave Kleikamp 
184456c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1845b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1846b31e1552SEric Sandeen 					"format mixing");
1847ac27a0ecSDave Kleikamp 			return 0;
1848ac27a0ecSDave Kleikamp 		}
1849ac27a0ecSDave Kleikamp 
1850ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1851b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1852b31e1552SEric Sandeen 					"not specified");
1853ac27a0ecSDave Kleikamp 			return 0;
1854ac27a0ecSDave Kleikamp 		}
1855ac27a0ecSDave Kleikamp 	} else {
1856ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1857b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
18582c8be6b2SJan Kara 					"specified with no journaling "
1859b31e1552SEric Sandeen 					"enabled");
1860ac27a0ecSDave Kleikamp 			return 0;
1861ac27a0ecSDave Kleikamp 		}
1862ac27a0ecSDave Kleikamp 	}
1863ac27a0ecSDave Kleikamp #endif
1864ac27a0ecSDave Kleikamp 	return 1;
1865ac27a0ecSDave Kleikamp }
1866ac27a0ecSDave Kleikamp 
1867617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1868ac27a0ecSDave Kleikamp 			    int read_only)
1869ac27a0ecSDave Kleikamp {
1870617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1871ac27a0ecSDave Kleikamp 	int res = 0;
1872ac27a0ecSDave Kleikamp 
1873617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1874b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1875b31e1552SEric Sandeen 			 "forcing read-only mode");
1876ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1877ac27a0ecSDave Kleikamp 	}
1878ac27a0ecSDave Kleikamp 	if (read_only)
1879ac27a0ecSDave Kleikamp 		return res;
1880617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1881b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1882b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1883617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1884b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1885b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1886b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1887ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1888ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1889ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1890b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1891b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1892b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1893ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1894ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1895ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1896b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1897b31e1552SEric Sandeen 			 "warning: checktime reached, "
1898b31e1552SEric Sandeen 			 "running e2fsck is recommended");
18990390131bSFrank Mayhar 	if (!sbi->s_journal)
1900216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1901ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1902617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1903e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1904ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1905617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
19060390131bSFrank Mayhar 	if (sbi->s_journal)
1907617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1908ac27a0ecSDave Kleikamp 
1909e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1910ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1911a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1912a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1913ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1914ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1915617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1916617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1917a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
1918ac27a0ecSDave Kleikamp 
1919ac27a0ecSDave Kleikamp 	return res;
1920ac27a0ecSDave Kleikamp }
1921ac27a0ecSDave Kleikamp 
1922772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1923772cb7c8SJose R. Santos {
1924772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1925772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1926772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1927772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1928772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1929c5ca7c76STheodore Ts'o 	size_t size;
1930772cb7c8SJose R. Santos 	int i;
1931772cb7c8SJose R. Santos 
1932503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1933503358aeSTheodore Ts'o 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1934503358aeSTheodore Ts'o 
1935503358aeSTheodore Ts'o 	if (groups_per_flex < 2) {
1936772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1937772cb7c8SJose R. Santos 		return 1;
1938772cb7c8SJose R. Santos 	}
1939772cb7c8SJose R. Santos 
1940c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1941c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1942d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1943d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1944c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
1945c5ca7c76STheodore Ts'o 	sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1946c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
194794de56abSJoe Perches 		sbi->s_flex_groups = vzalloc(size);
1948772cb7c8SJose R. Santos 		if (sbi->s_flex_groups == NULL) {
194994de56abSJoe Perches 			ext4_msg(sb, KERN_ERR,
195094de56abSJoe Perches 				 "not enough memory for %u flex groups",
195194de56abSJoe Perches 				 flex_group_count);
1952772cb7c8SJose R. Santos 			goto failed;
1953772cb7c8SJose R. Santos 		}
195494de56abSJoe Perches 	}
1955772cb7c8SJose R. Santos 
1956772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
195788b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1958772cb7c8SJose R. Santos 
1959772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
19607ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
19617ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
19627ad9bb65STheodore Ts'o 		atomic_add(ext4_free_blks_count(sb, gdp),
19637ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_blocks);
19647ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
19657ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
1966772cb7c8SJose R. Santos 	}
1967772cb7c8SJose R. Santos 
1968772cb7c8SJose R. Santos 	return 1;
1969772cb7c8SJose R. Santos failed:
1970772cb7c8SJose R. Santos 	return 0;
1971772cb7c8SJose R. Santos }
1972772cb7c8SJose R. Santos 
1973717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1974717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1975717d50e4SAndreas Dilger {
1976717d50e4SAndreas Dilger 	__u16 crc = 0;
1977717d50e4SAndreas Dilger 
1978717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1979717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1980717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1981717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1982717d50e4SAndreas Dilger 
1983717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1984717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1985717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1986717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1987717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1988717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1989717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1990717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1991717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1992717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1993717d50e4SAndreas Dilger 					offset);
1994717d50e4SAndreas Dilger 	}
1995717d50e4SAndreas Dilger 
1996717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1997717d50e4SAndreas Dilger }
1998717d50e4SAndreas Dilger 
1999717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
2000717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2001717d50e4SAndreas Dilger {
2002717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
2003717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
2004717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
2005717d50e4SAndreas Dilger 		return 0;
2006717d50e4SAndreas Dilger 
2007717d50e4SAndreas Dilger 	return 1;
2008717d50e4SAndreas Dilger }
2009717d50e4SAndreas Dilger 
2010ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2011bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2012bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2013ac27a0ecSDave Kleikamp {
2014617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2015617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2016617ba13bSMingming Cao 	ext4_fsblk_t last_block;
2017bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2018bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2019bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2020ce421581SJose R. Santos 	int flexbg_flag = 0;
2021bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2022ac27a0ecSDave Kleikamp 
2023ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2024ce421581SJose R. Santos 		flexbg_flag = 1;
2025ce421581SJose R. Santos 
2026617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2027ac27a0ecSDave Kleikamp 
2028197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2029197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2030197cd65aSAkinobu Mita 
2031ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2032bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2033ac27a0ecSDave Kleikamp 		else
2034ac27a0ecSDave Kleikamp 			last_block = first_block +
2035617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2036ac27a0ecSDave Kleikamp 
2037bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2038bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2039bfff6873SLukas Czerner 			grp = i;
2040bfff6873SLukas Czerner 
20418fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
20422b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2043b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2044a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2045b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2046ac27a0ecSDave Kleikamp 			return 0;
2047ac27a0ecSDave Kleikamp 		}
20488fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
20492b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2050b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2051a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2052b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2053ac27a0ecSDave Kleikamp 			return 0;
2054ac27a0ecSDave Kleikamp 		}
20558fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2056bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
20572b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2058b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2059a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2060b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2061ac27a0ecSDave Kleikamp 			return 0;
2062ac27a0ecSDave Kleikamp 		}
2063955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2064717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
2065b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2066b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2067fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2068fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
20697ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
2070955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2071717d50e4SAndreas Dilger 				return 0;
2072717d50e4SAndreas Dilger 			}
20737ee1ec4cSLi Zefan 		}
2074955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2075ce421581SJose R. Santos 		if (!flexbg_flag)
2076617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2077ac27a0ecSDave Kleikamp 	}
2078bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2079bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2080ac27a0ecSDave Kleikamp 
2081bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
2082617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
2083ac27a0ecSDave Kleikamp 	return 1;
2084ac27a0ecSDave Kleikamp }
2085ac27a0ecSDave Kleikamp 
2086617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2087ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2088ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2089ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2090ac27a0ecSDave Kleikamp  *
2091ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2092ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2093ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2094ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2095ac27a0ecSDave Kleikamp  *
2096ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2097ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2098ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2099617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2100ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2101ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2102ac27a0ecSDave Kleikamp  */
2103617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2104617ba13bSMingming Cao 				struct ext4_super_block *es)
2105ac27a0ecSDave Kleikamp {
2106ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
2107ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
2108ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2109ac27a0ecSDave Kleikamp 	int i;
2110ac27a0ecSDave Kleikamp #endif
2111ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2112ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2113ac27a0ecSDave Kleikamp 		return;
2114ac27a0ecSDave Kleikamp 	}
2115ac27a0ecSDave Kleikamp 
2116a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2117b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2118b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2119a8f48a95SEric Sandeen 		return;
2120a8f48a95SEric Sandeen 	}
2121a8f48a95SEric Sandeen 
2122617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2123ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
2124ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
2125ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2126ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
2127ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2128ac27a0ecSDave Kleikamp 		return;
2129ac27a0ecSDave Kleikamp 	}
2130ac27a0ecSDave Kleikamp 
2131ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2132b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2133ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2134ac27a0ecSDave Kleikamp 	}
2135ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2136ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2137ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2138ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2139ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2140617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2141617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2142ac27a0ecSDave Kleikamp 			if (ret < 0)
2143b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2144b31e1552SEric Sandeen 					"Cannot turn on journaled "
2145b31e1552SEric Sandeen 					"quota: error %d", ret);
2146ac27a0ecSDave Kleikamp 		}
2147ac27a0ecSDave Kleikamp 	}
2148ac27a0ecSDave Kleikamp #endif
2149ac27a0ecSDave Kleikamp 
2150ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2151ac27a0ecSDave Kleikamp 		struct inode *inode;
2152ac27a0ecSDave Kleikamp 
215397bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
215497bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2155ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2156ac27a0ecSDave Kleikamp 			break;
2157ac27a0ecSDave Kleikamp 		}
2158ac27a0ecSDave Kleikamp 
2159617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2160871a2931SChristoph Hellwig 		dquot_initialize(inode);
2161ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2162b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2163b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
216446e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
2165e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2166ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2167617ba13bSMingming Cao 			ext4_truncate(inode);
2168ac27a0ecSDave Kleikamp 			nr_truncates++;
2169ac27a0ecSDave Kleikamp 		} else {
2170b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2171b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
217246e665e9SHarvey Harrison 				__func__, inode->i_ino);
2173ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2174ac27a0ecSDave Kleikamp 				  inode->i_ino);
2175ac27a0ecSDave Kleikamp 			nr_orphans++;
2176ac27a0ecSDave Kleikamp 		}
2177ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2178ac27a0ecSDave Kleikamp 	}
2179ac27a0ecSDave Kleikamp 
2180ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2181ac27a0ecSDave Kleikamp 
2182ac27a0ecSDave Kleikamp 	if (nr_orphans)
2183b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2184b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2185ac27a0ecSDave Kleikamp 	if (nr_truncates)
2186b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2187b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2188ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2189ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2190ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2191ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2192287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2193ac27a0ecSDave Kleikamp 	}
2194ac27a0ecSDave Kleikamp #endif
2195ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2196ac27a0ecSDave Kleikamp }
21970b8e58a1SAndreas Dilger 
2198cd2291a4SEric Sandeen /*
2199cd2291a4SEric Sandeen  * Maximal extent format file size.
2200cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2201cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2202cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2203cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2204cd2291a4SEric Sandeen  *
2205cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2206cd2291a4SEric Sandeen  */
2207f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2208cd2291a4SEric Sandeen {
2209cd2291a4SEric Sandeen 	loff_t res;
2210cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2211cd2291a4SEric Sandeen 
2212cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2213f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2214cd2291a4SEric Sandeen 		/*
221590c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2216cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2217cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2218cd2291a4SEric Sandeen 		 */
2219cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2220cd2291a4SEric Sandeen 
2221cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2222cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2223cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2224cd2291a4SEric Sandeen 	}
2225cd2291a4SEric Sandeen 
2226cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
2227cd2291a4SEric Sandeen 	res = 1LL << 32;
2228cd2291a4SEric Sandeen 	res <<= blkbits;
2229cd2291a4SEric Sandeen 	res -= 1;
2230cd2291a4SEric Sandeen 
2231cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2232cd2291a4SEric Sandeen 	if (res > upper_limit)
2233cd2291a4SEric Sandeen 		res = upper_limit;
2234cd2291a4SEric Sandeen 
2235cd2291a4SEric Sandeen 	return res;
2236cd2291a4SEric Sandeen }
2237ac27a0ecSDave Kleikamp 
2238ac27a0ecSDave Kleikamp /*
2239cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
22400fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
22410fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2242ac27a0ecSDave Kleikamp  */
2243f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2244ac27a0ecSDave Kleikamp {
2245617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
22460fc1b451SAneesh Kumar K.V 	int meta_blocks;
22470fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
22480b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
22490b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
22500b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
22510b8e58a1SAndreas Dilger 	 *
22520b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
22530b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
22540fc1b451SAneesh Kumar K.V 	 */
22550fc1b451SAneesh Kumar K.V 
2256f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
22570fc1b451SAneesh Kumar K.V 		/*
225890c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
22590b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
22600b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
22610fc1b451SAneesh Kumar K.V 		 */
22620fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
22630fc1b451SAneesh Kumar K.V 
22640fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
22650fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
22660fc1b451SAneesh Kumar K.V 
22670fc1b451SAneesh Kumar K.V 	} else {
22688180a562SAneesh Kumar K.V 		/*
22698180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
22708180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
22718180a562SAneesh Kumar K.V 		 * represent total number of blocks in
22728180a562SAneesh Kumar K.V 		 * file system block size
22738180a562SAneesh Kumar K.V 		 */
22740fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
22750fc1b451SAneesh Kumar K.V 
22760fc1b451SAneesh Kumar K.V 	}
22770fc1b451SAneesh Kumar K.V 
22780fc1b451SAneesh Kumar K.V 	/* indirect blocks */
22790fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
22800fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
22810fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
22820fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
22830fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
22840fc1b451SAneesh Kumar K.V 
22850fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
22860fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2287ac27a0ecSDave Kleikamp 
2288ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2289ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2290ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2291ac27a0ecSDave Kleikamp 	res <<= bits;
2292ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2293ac27a0ecSDave Kleikamp 		res = upper_limit;
22940fc1b451SAneesh Kumar K.V 
22950fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
22960fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
22970fc1b451SAneesh Kumar K.V 
2298ac27a0ecSDave Kleikamp 	return res;
2299ac27a0ecSDave Kleikamp }
2300ac27a0ecSDave Kleikamp 
2301617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
230270bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2303ac27a0ecSDave Kleikamp {
2304617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2305fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2306ac27a0ecSDave Kleikamp 	int has_super = 0;
2307ac27a0ecSDave Kleikamp 
2308ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2309ac27a0ecSDave Kleikamp 
2310617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2311ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
231270bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2313ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2314617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2315ac27a0ecSDave Kleikamp 		has_super = 1;
23160b8e58a1SAndreas Dilger 
2317617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2318ac27a0ecSDave Kleikamp }
2319ac27a0ecSDave Kleikamp 
2320c9de560dSAlex Tomas /**
2321c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2322c9de560dSAlex Tomas  * @sbi: In memory super block info
2323c9de560dSAlex Tomas  *
2324c9de560dSAlex Tomas  * If we have specified it via mount option, then
2325c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2326c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2327c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2328c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2329c9de560dSAlex Tomas  *
2330c9de560dSAlex Tomas  */
2331c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2332c9de560dSAlex Tomas {
2333c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2334c9de560dSAlex Tomas 	unsigned long stripe_width =
2335c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2336c9de560dSAlex Tomas 
2337c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2338c9de560dSAlex Tomas 		return sbi->s_stripe;
2339c9de560dSAlex Tomas 
2340c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
2341c9de560dSAlex Tomas 		return stripe_width;
2342c9de560dSAlex Tomas 
2343c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
2344c9de560dSAlex Tomas 		return stride;
2345c9de560dSAlex Tomas 
2346c9de560dSAlex Tomas 	return 0;
2347c9de560dSAlex Tomas }
2348ac27a0ecSDave Kleikamp 
23493197ebdbSTheodore Ts'o /* sysfs supprt */
23503197ebdbSTheodore Ts'o 
23513197ebdbSTheodore Ts'o struct ext4_attr {
23523197ebdbSTheodore Ts'o 	struct attribute attr;
23533197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
23543197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
23553197ebdbSTheodore Ts'o 			 const char *, size_t);
23563197ebdbSTheodore Ts'o 	int offset;
23573197ebdbSTheodore Ts'o };
23583197ebdbSTheodore Ts'o 
23593197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
23603197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
23613197ebdbSTheodore Ts'o {
23623197ebdbSTheodore Ts'o 	char *endp;
23633197ebdbSTheodore Ts'o 
2364e7d2860bSAndré Goddard Rosa 	*value = simple_strtoul(skip_spaces(buf), &endp, 0);
2365e7d2860bSAndré Goddard Rosa 	endp = skip_spaces(endp);
23663197ebdbSTheodore Ts'o 	if (*endp || *value > max)
23673197ebdbSTheodore Ts'o 		return -EINVAL;
23683197ebdbSTheodore Ts'o 
23693197ebdbSTheodore Ts'o 	return 0;
23703197ebdbSTheodore Ts'o }
23713197ebdbSTheodore Ts'o 
23723197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
23733197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
23743197ebdbSTheodore Ts'o 					      char *buf)
23753197ebdbSTheodore Ts'o {
23763197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
23773197ebdbSTheodore Ts'o 			(s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
23783197ebdbSTheodore Ts'o }
23793197ebdbSTheodore Ts'o 
23803197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
23813197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
23823197ebdbSTheodore Ts'o {
23833197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
23843197ebdbSTheodore Ts'o 
2385f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2386f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
23873197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
23883197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
23893197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
23903197ebdbSTheodore Ts'o }
23913197ebdbSTheodore Ts'o 
23923197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
23933197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
23943197ebdbSTheodore Ts'o {
23953197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
23963197ebdbSTheodore Ts'o 
2397f613dfcbSTheodore Ts'o 	if (!sb->s_bdev->bd_part)
2398f613dfcbSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
23993197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2400a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
24013197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2402a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
24033197ebdbSTheodore Ts'o }
24043197ebdbSTheodore Ts'o 
24053197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
24063197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
24073197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
24083197ebdbSTheodore Ts'o {
24093197ebdbSTheodore Ts'o 	unsigned long t;
24103197ebdbSTheodore Ts'o 
24113197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
24123197ebdbSTheodore Ts'o 		return -EINVAL;
24133197ebdbSTheodore Ts'o 
2414f7c43950STheodore Ts'o 	if (!is_power_of_2(t))
24153197ebdbSTheodore Ts'o 		return -EINVAL;
24163197ebdbSTheodore Ts'o 
24173197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
24183197ebdbSTheodore Ts'o 	return count;
24193197ebdbSTheodore Ts'o }
24203197ebdbSTheodore Ts'o 
24213197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
24223197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
24233197ebdbSTheodore Ts'o {
24243197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
24253197ebdbSTheodore Ts'o 
24263197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
24273197ebdbSTheodore Ts'o }
24283197ebdbSTheodore Ts'o 
24293197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
24303197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
24313197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
24323197ebdbSTheodore Ts'o {
24333197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
24343197ebdbSTheodore Ts'o 	unsigned long t;
24353197ebdbSTheodore Ts'o 
24363197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
24373197ebdbSTheodore Ts'o 		return -EINVAL;
24383197ebdbSTheodore Ts'o 	*ui = t;
24393197ebdbSTheodore Ts'o 	return count;
24403197ebdbSTheodore Ts'o }
24413197ebdbSTheodore Ts'o 
24423197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
24433197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
24443197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
24453197ebdbSTheodore Ts'o 	.show	= _show,					\
24463197ebdbSTheodore Ts'o 	.store	= _store,					\
24473197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
24483197ebdbSTheodore Ts'o }
24493197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
24503197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
24513197ebdbSTheodore Ts'o 
2452857ac889SLukas Czerner #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
24533197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
24543197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
24553197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
24563197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
24573197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
24583197ebdbSTheodore Ts'o 
24593197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
24603197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
24613197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
24623197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
24633197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
246411013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
24653197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
24663197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
24673197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
24683197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
24693197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
24703197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
247155138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
24723197ebdbSTheodore Ts'o 
24733197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
24743197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
24753197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
24763197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
24773197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
247811013911SAndreas Dilger 	ATTR_LIST(inode_goal),
24793197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
24803197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
24813197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
24823197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
24833197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
24843197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
248555138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
24863197ebdbSTheodore Ts'o 	NULL,
24873197ebdbSTheodore Ts'o };
24883197ebdbSTheodore Ts'o 
2489857ac889SLukas Czerner /* Features this copy of ext4 supports */
2490857ac889SLukas Czerner EXT4_INFO_ATTR(lazy_itable_init);
249127ee40dfSLukas Czerner EXT4_INFO_ATTR(batched_discard);
2492857ac889SLukas Czerner 
2493857ac889SLukas Czerner static struct attribute *ext4_feat_attrs[] = {
2494857ac889SLukas Czerner 	ATTR_LIST(lazy_itable_init),
249527ee40dfSLukas Czerner 	ATTR_LIST(batched_discard),
2496857ac889SLukas Czerner 	NULL,
2497857ac889SLukas Czerner };
2498857ac889SLukas Czerner 
24993197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
25003197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
25013197ebdbSTheodore Ts'o {
25023197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
25033197ebdbSTheodore Ts'o 						s_kobj);
25043197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
25053197ebdbSTheodore Ts'o 
25063197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
25073197ebdbSTheodore Ts'o }
25083197ebdbSTheodore Ts'o 
25093197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
25103197ebdbSTheodore Ts'o 			       struct attribute *attr,
25113197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
25123197ebdbSTheodore Ts'o {
25133197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
25143197ebdbSTheodore Ts'o 						s_kobj);
25153197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
25163197ebdbSTheodore Ts'o 
25173197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
25183197ebdbSTheodore Ts'o }
25193197ebdbSTheodore Ts'o 
25203197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
25213197ebdbSTheodore Ts'o {
25223197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
25233197ebdbSTheodore Ts'o 						s_kobj);
25243197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
25253197ebdbSTheodore Ts'o }
25263197ebdbSTheodore Ts'o 
252752cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = {
25283197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
25293197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
25303197ebdbSTheodore Ts'o };
25313197ebdbSTheodore Ts'o 
25323197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
25333197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
25343197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
25353197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
25363197ebdbSTheodore Ts'o };
25373197ebdbSTheodore Ts'o 
2538857ac889SLukas Czerner static void ext4_feat_release(struct kobject *kobj)
2539857ac889SLukas Czerner {
2540857ac889SLukas Czerner 	complete(&ext4_feat->f_kobj_unregister);
2541857ac889SLukas Czerner }
2542857ac889SLukas Czerner 
2543857ac889SLukas Czerner static struct kobj_type ext4_feat_ktype = {
2544857ac889SLukas Czerner 	.default_attrs	= ext4_feat_attrs,
2545857ac889SLukas Czerner 	.sysfs_ops	= &ext4_attr_ops,
2546857ac889SLukas Czerner 	.release	= ext4_feat_release,
2547857ac889SLukas Czerner };
2548857ac889SLukas Czerner 
2549a13fb1a4SEric Sandeen /*
2550a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2551a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2552a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2553a13fb1a4SEric Sandeen  * 0 if it cannot be.
2554a13fb1a4SEric Sandeen  */
2555a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2556a13fb1a4SEric Sandeen {
2557a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2558a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2559a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2560a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2561a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2562a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2563a13fb1a4SEric Sandeen 		return 0;
2564a13fb1a4SEric Sandeen 	}
2565a13fb1a4SEric Sandeen 
2566a13fb1a4SEric Sandeen 	if (readonly)
2567a13fb1a4SEric Sandeen 		return 1;
2568a13fb1a4SEric Sandeen 
2569a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2570a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2571a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2572a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2573a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2574a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2575a13fb1a4SEric Sandeen 		return 0;
2576a13fb1a4SEric Sandeen 	}
2577a13fb1a4SEric Sandeen 	/*
2578a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2579a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2580a13fb1a4SEric Sandeen 	 */
2581a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2582a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2583a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2584a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2585a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2586a13fb1a4SEric Sandeen 			return 0;
2587a13fb1a4SEric Sandeen 		}
2588a13fb1a4SEric Sandeen 	}
2589a13fb1a4SEric Sandeen 	return 1;
2590a13fb1a4SEric Sandeen }
2591a13fb1a4SEric Sandeen 
259266e61a9eSTheodore Ts'o /*
259366e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
259466e61a9eSTheodore Ts'o  * on the file system
259566e61a9eSTheodore Ts'o  */
259666e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg)
259766e61a9eSTheodore Ts'o {
259866e61a9eSTheodore Ts'o 	struct super_block *sb = (struct super_block *) arg;
259966e61a9eSTheodore Ts'o 	struct ext4_sb_info *sbi;
260066e61a9eSTheodore Ts'o 	struct ext4_super_block *es;
260166e61a9eSTheodore Ts'o 
260266e61a9eSTheodore Ts'o 	sbi = EXT4_SB(sb);
260366e61a9eSTheodore Ts'o 	es = sbi->s_es;
260466e61a9eSTheodore Ts'o 
260566e61a9eSTheodore Ts'o 	if (es->s_error_count)
260666e61a9eSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count: %u",
260766e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
260866e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
260966e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
261066e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
261166e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
261266e61a9eSTheodore Ts'o 		       es->s_first_error_func,
261366e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
261466e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
261566e61a9eSTheodore Ts'o 			printk(": inode %u",
261666e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
261766e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
261866e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
261966e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
262066e61a9eSTheodore Ts'o 		printk("\n");
262166e61a9eSTheodore Ts'o 	}
262266e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
262366e61a9eSTheodore Ts'o 		printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
262466e61a9eSTheodore Ts'o 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
262566e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
262666e61a9eSTheodore Ts'o 		       es->s_last_error_func,
262766e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
262866e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
262966e61a9eSTheodore Ts'o 			printk(": inode %u",
263066e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
263166e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
263266e61a9eSTheodore Ts'o 			printk(": block %llu", (unsigned long long)
263366e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
263466e61a9eSTheodore Ts'o 		printk("\n");
263566e61a9eSTheodore Ts'o 	}
263666e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
263766e61a9eSTheodore Ts'o }
263866e61a9eSTheodore Ts'o 
2639bfff6873SLukas Czerner static void ext4_lazyinode_timeout(unsigned long data)
2640bfff6873SLukas Czerner {
2641bfff6873SLukas Czerner 	struct task_struct *p = (struct task_struct *)data;
2642bfff6873SLukas Czerner 	wake_up_process(p);
2643bfff6873SLukas Czerner }
2644bfff6873SLukas Czerner 
2645bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
2646bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
2647bfff6873SLukas Czerner {
2648bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2649bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
2650bfff6873SLukas Czerner 	struct super_block *sb;
2651bfff6873SLukas Czerner 	unsigned long timeout = 0;
2652bfff6873SLukas Czerner 	int ret = 0;
2653bfff6873SLukas Czerner 
2654bfff6873SLukas Czerner 	sb = elr->lr_super;
2655bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
2656bfff6873SLukas Czerner 
2657bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
2658bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2659bfff6873SLukas Czerner 		if (!gdp) {
2660bfff6873SLukas Czerner 			ret = 1;
2661bfff6873SLukas Czerner 			break;
2662bfff6873SLukas Czerner 		}
2663bfff6873SLukas Czerner 
2664bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2665bfff6873SLukas Czerner 			break;
2666bfff6873SLukas Czerner 	}
2667bfff6873SLukas Czerner 
2668bfff6873SLukas Czerner 	if (group == ngroups)
2669bfff6873SLukas Czerner 		ret = 1;
2670bfff6873SLukas Czerner 
2671bfff6873SLukas Czerner 	if (!ret) {
2672bfff6873SLukas Czerner 		timeout = jiffies;
2673bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
2674bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
2675bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
2676bfff6873SLukas Czerner 			timeout = jiffies - timeout;
2677bfff6873SLukas Czerner 			if (elr->lr_sbi->s_li_wait_mult)
2678bfff6873SLukas Czerner 				timeout *= elr->lr_sbi->s_li_wait_mult;
2679bfff6873SLukas Czerner 			else
2680bfff6873SLukas Czerner 				timeout *= 20;
2681bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
2682bfff6873SLukas Czerner 		}
2683bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2684bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
2685bfff6873SLukas Czerner 	}
2686bfff6873SLukas Czerner 
2687bfff6873SLukas Czerner 	return ret;
2688bfff6873SLukas Czerner }
2689bfff6873SLukas Czerner 
2690bfff6873SLukas Czerner /*
2691bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
2692bfff6873SLukas Czerner  * request tructure. Should be called with li_list_mtx held
2693bfff6873SLukas Czerner  */
2694bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
2695bfff6873SLukas Czerner {
2696bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
2697bfff6873SLukas Czerner 
2698bfff6873SLukas Czerner 	if (!elr)
2699bfff6873SLukas Czerner 		return;
2700bfff6873SLukas Czerner 
2701bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
2702bfff6873SLukas Czerner 
2703bfff6873SLukas Czerner 	list_del(&elr->lr_request);
2704bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
2705bfff6873SLukas Czerner 	kfree(elr);
2706bfff6873SLukas Czerner }
2707bfff6873SLukas Czerner 
2708bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
2709bfff6873SLukas Czerner {
2710bfff6873SLukas Czerner 	struct ext4_li_request *elr = EXT4_SB(sb)->s_li_request;
2711bfff6873SLukas Czerner 
2712bfff6873SLukas Czerner 	if (!ext4_li_info)
2713bfff6873SLukas Czerner 		return;
2714bfff6873SLukas Czerner 
2715bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2716bfff6873SLukas Czerner 	ext4_remove_li_request(elr);
2717bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2718bfff6873SLukas Czerner }
2719bfff6873SLukas Czerner 
27208f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
27218f1f7453SEric Sandeen 
2722bfff6873SLukas Czerner /*
2723bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
2724bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
2725bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
2726bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
2727bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
2728bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
2729bfff6873SLukas Czerner  * next waking time and put itself into sleep.
2730bfff6873SLukas Czerner  */
2731bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
2732bfff6873SLukas Czerner {
2733bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2734bfff6873SLukas Czerner 	struct list_head *pos, *n;
2735bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2736bfff6873SLukas Czerner 	unsigned long next_wakeup;
2737bfff6873SLukas Czerner 	DEFINE_WAIT(wait);
2738bfff6873SLukas Czerner 
2739bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
2740bfff6873SLukas Czerner 
2741bfff6873SLukas Czerner 	eli->li_timer.data = (unsigned long)current;
2742bfff6873SLukas Czerner 	eli->li_timer.function = ext4_lazyinode_timeout;
2743bfff6873SLukas Czerner 
2744bfff6873SLukas Czerner 	eli->li_task = current;
2745bfff6873SLukas Czerner 	wake_up(&eli->li_wait_task);
2746bfff6873SLukas Czerner 
2747bfff6873SLukas Czerner cont_thread:
2748bfff6873SLukas Czerner 	while (true) {
2749bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
2750bfff6873SLukas Czerner 
2751bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
2752bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
2753bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
2754bfff6873SLukas Czerner 			goto exit_thread;
2755bfff6873SLukas Czerner 		}
2756bfff6873SLukas Czerner 
2757bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
2758bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
2759bfff6873SLukas Czerner 					 lr_request);
2760bfff6873SLukas Czerner 
2761b2c78cd0STheodore Ts'o 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
2762b2c78cd0STheodore Ts'o 				if (ext4_run_li_request(elr) != 0) {
2763b2c78cd0STheodore Ts'o 					/* error, remove the lazy_init job */
2764bfff6873SLukas Czerner 					ext4_remove_li_request(elr);
2765bfff6873SLukas Czerner 					continue;
2766bfff6873SLukas Czerner 				}
2767b2c78cd0STheodore Ts'o 			}
2768bfff6873SLukas Czerner 
2769bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
2770bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
2771bfff6873SLukas Czerner 		}
2772bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2773bfff6873SLukas Czerner 
2774bfff6873SLukas Czerner 		if (freezing(current))
2775bfff6873SLukas Czerner 			refrigerator();
2776bfff6873SLukas Czerner 
2777f4245bd4SLukas Czerner 		if ((time_after_eq(jiffies, next_wakeup)) ||
2778f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
2779bfff6873SLukas Czerner 			cond_resched();
2780bfff6873SLukas Czerner 			continue;
2781bfff6873SLukas Czerner 		}
2782bfff6873SLukas Czerner 
2783bfff6873SLukas Czerner 		eli->li_timer.expires = next_wakeup;
2784bfff6873SLukas Czerner 		add_timer(&eli->li_timer);
2785bfff6873SLukas Czerner 		prepare_to_wait(&eli->li_wait_daemon, &wait,
2786bfff6873SLukas Czerner 				TASK_INTERRUPTIBLE);
2787bfff6873SLukas Czerner 		if (time_before(jiffies, next_wakeup))
2788bfff6873SLukas Czerner 			schedule();
2789bfff6873SLukas Czerner 		finish_wait(&eli->li_wait_daemon, &wait);
27908f1f7453SEric Sandeen 		if (kthread_should_stop()) {
27918f1f7453SEric Sandeen 			ext4_clear_request_list();
27928f1f7453SEric Sandeen 			goto exit_thread;
27938f1f7453SEric Sandeen 		}
2794bfff6873SLukas Czerner 	}
2795bfff6873SLukas Czerner 
2796bfff6873SLukas Czerner exit_thread:
2797bfff6873SLukas Czerner 	/*
2798bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
2799bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
2800bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
2801bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
2802bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
2803bfff6873SLukas Czerner 	 * new one.
2804bfff6873SLukas Czerner 	 */
2805bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2806bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
2807bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
2808bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
2809bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
2810bfff6873SLukas Czerner 		goto cont_thread;
2811bfff6873SLukas Czerner 	}
2812bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
2813bfff6873SLukas Czerner 	del_timer_sync(&ext4_li_info->li_timer);
2814bfff6873SLukas Czerner 	eli->li_task = NULL;
2815bfff6873SLukas Czerner 	wake_up(&eli->li_wait_task);
2816bfff6873SLukas Czerner 
2817bfff6873SLukas Czerner 	kfree(ext4_li_info);
28188f1f7453SEric Sandeen 	ext4_lazyinit_task = NULL;
2819bfff6873SLukas Czerner 	ext4_li_info = NULL;
2820bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2821bfff6873SLukas Czerner 
2822bfff6873SLukas Czerner 	return 0;
2823bfff6873SLukas Czerner }
2824bfff6873SLukas Czerner 
2825bfff6873SLukas Czerner static void ext4_clear_request_list(void)
2826bfff6873SLukas Czerner {
2827bfff6873SLukas Czerner 	struct list_head *pos, *n;
2828bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2829bfff6873SLukas Czerner 
2830bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2831bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2832bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
2833bfff6873SLukas Czerner 				 lr_request);
2834bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
2835bfff6873SLukas Czerner 	}
2836bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2837bfff6873SLukas Czerner }
2838bfff6873SLukas Czerner 
2839bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
2840bfff6873SLukas Czerner {
28418f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
28428f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
28438f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
28448f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
2845bfff6873SLukas Czerner 		ext4_clear_request_list();
2846bfff6873SLukas Czerner 		del_timer_sync(&ext4_li_info->li_timer);
2847bfff6873SLukas Czerner 		kfree(ext4_li_info);
2848bfff6873SLukas Czerner 		ext4_li_info = NULL;
2849bfff6873SLukas Czerner 		printk(KERN_CRIT "EXT4: error %d creating inode table "
2850bfff6873SLukas Czerner 				 "initialization thread\n",
2851bfff6873SLukas Czerner 				 err);
2852bfff6873SLukas Czerner 		return err;
2853bfff6873SLukas Czerner 	}
2854bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
2855bfff6873SLukas Czerner 
2856bfff6873SLukas Czerner 	wait_event(ext4_li_info->li_wait_task, ext4_li_info->li_task != NULL);
2857bfff6873SLukas Czerner 	return 0;
2858bfff6873SLukas Czerner }
2859bfff6873SLukas Czerner 
2860bfff6873SLukas Czerner /*
2861bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
2862bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
2863bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
2864bfff6873SLukas Czerner  * groups and return total number of groups.
2865bfff6873SLukas Czerner  */
2866bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2867bfff6873SLukas Czerner {
2868bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2869bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
2870bfff6873SLukas Czerner 
2871bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
2872bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
2873bfff6873SLukas Czerner 		if (!gdp)
2874bfff6873SLukas Czerner 			continue;
2875bfff6873SLukas Czerner 
2876bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2877bfff6873SLukas Czerner 			break;
2878bfff6873SLukas Czerner 	}
2879bfff6873SLukas Czerner 
2880bfff6873SLukas Czerner 	return group;
2881bfff6873SLukas Czerner }
2882bfff6873SLukas Czerner 
2883bfff6873SLukas Czerner static int ext4_li_info_new(void)
2884bfff6873SLukas Czerner {
2885bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
2886bfff6873SLukas Czerner 
2887bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2888bfff6873SLukas Czerner 	if (!eli)
2889bfff6873SLukas Czerner 		return -ENOMEM;
2890bfff6873SLukas Czerner 
2891bfff6873SLukas Czerner 	eli->li_task = NULL;
2892bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
2893bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
2894bfff6873SLukas Czerner 
2895bfff6873SLukas Czerner 	init_waitqueue_head(&eli->li_wait_daemon);
2896bfff6873SLukas Czerner 	init_waitqueue_head(&eli->li_wait_task);
2897bfff6873SLukas Czerner 	init_timer(&eli->li_timer);
2898bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
2899bfff6873SLukas Czerner 
2900bfff6873SLukas Czerner 	ext4_li_info = eli;
2901bfff6873SLukas Czerner 
2902bfff6873SLukas Czerner 	return 0;
2903bfff6873SLukas Czerner }
2904bfff6873SLukas Czerner 
2905bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
2906bfff6873SLukas Czerner 					    ext4_group_t start)
2907bfff6873SLukas Czerner {
2908bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2909bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2910bfff6873SLukas Czerner 	unsigned long rnd;
2911bfff6873SLukas Czerner 
2912bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
2913bfff6873SLukas Czerner 	if (!elr)
2914bfff6873SLukas Czerner 		return NULL;
2915bfff6873SLukas Czerner 
2916bfff6873SLukas Czerner 	elr->lr_super = sb;
2917bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
2918bfff6873SLukas Czerner 	elr->lr_next_group = start;
2919bfff6873SLukas Czerner 
2920bfff6873SLukas Czerner 	/*
2921bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
2922bfff6873SLukas Czerner 	 * spread the inode table initialization requests
2923bfff6873SLukas Czerner 	 * better.
2924bfff6873SLukas Czerner 	 */
2925bfff6873SLukas Czerner 	get_random_bytes(&rnd, sizeof(rnd));
2926bfff6873SLukas Czerner 	elr->lr_next_sched = jiffies + (unsigned long)rnd %
2927bfff6873SLukas Czerner 			     (EXT4_DEF_LI_MAX_START_DELAY * HZ);
2928bfff6873SLukas Czerner 
2929bfff6873SLukas Czerner 	return elr;
2930bfff6873SLukas Czerner }
2931bfff6873SLukas Czerner 
2932bfff6873SLukas Czerner static int ext4_register_li_request(struct super_block *sb,
2933bfff6873SLukas Czerner 				    ext4_group_t first_not_zeroed)
2934bfff6873SLukas Czerner {
2935bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2936bfff6873SLukas Czerner 	struct ext4_li_request *elr;
2937bfff6873SLukas Czerner 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
29386c5a6cb9SAndrew Morton 	int ret = 0;
2939bfff6873SLukas Czerner 
2940bfff6873SLukas Czerner 	if (sbi->s_li_request != NULL)
2941beed5ecbSNicolas Kaiser 		return 0;
2942bfff6873SLukas Czerner 
2943bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups ||
2944bfff6873SLukas Czerner 	    (sb->s_flags & MS_RDONLY) ||
2945bfff6873SLukas Czerner 	    !test_opt(sb, INIT_INODE_TABLE)) {
2946bfff6873SLukas Czerner 		sbi->s_li_request = NULL;
2947beed5ecbSNicolas Kaiser 		return 0;
2948bfff6873SLukas Czerner 	}
2949bfff6873SLukas Czerner 
2950bfff6873SLukas Czerner 	if (first_not_zeroed == ngroups) {
2951bfff6873SLukas Czerner 		sbi->s_li_request = NULL;
2952beed5ecbSNicolas Kaiser 		return 0;
2953bfff6873SLukas Czerner 	}
2954bfff6873SLukas Czerner 
2955bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
2956beed5ecbSNicolas Kaiser 	if (!elr)
2957beed5ecbSNicolas Kaiser 		return -ENOMEM;
2958bfff6873SLukas Czerner 
2959bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
2960bfff6873SLukas Czerner 
2961bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
2962bfff6873SLukas Czerner 		ret = ext4_li_info_new();
2963bfff6873SLukas Czerner 		if (ret)
2964bfff6873SLukas Czerner 			goto out;
2965bfff6873SLukas Czerner 	}
2966bfff6873SLukas Czerner 
2967bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
2968bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
2969bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
2970bfff6873SLukas Czerner 
2971bfff6873SLukas Czerner 	sbi->s_li_request = elr;
2972bfff6873SLukas Czerner 
2973bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
2974bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
2975bfff6873SLukas Czerner 		if (ret)
2976bfff6873SLukas Czerner 			goto out;
2977bfff6873SLukas Czerner 	}
2978bfff6873SLukas Czerner out:
2979bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
2980beed5ecbSNicolas Kaiser 	if (ret)
2981bfff6873SLukas Czerner 		kfree(elr);
2982bfff6873SLukas Czerner 	return ret;
2983bfff6873SLukas Czerner }
2984bfff6873SLukas Czerner 
2985bfff6873SLukas Czerner /*
2986bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
2987bfff6873SLukas Czerner  * module unload.
2988bfff6873SLukas Czerner  */
2989bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
2990bfff6873SLukas Czerner {
2991bfff6873SLukas Czerner 	/*
2992bfff6873SLukas Czerner 	 * If thread exited earlier
2993bfff6873SLukas Czerner 	 * there's nothing to be done.
2994bfff6873SLukas Czerner 	 */
29958f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
2996bfff6873SLukas Czerner 		return;
2997bfff6873SLukas Czerner 
29988f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
2999bfff6873SLukas Czerner }
3000bfff6873SLukas Czerner 
3001617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
30027477827fSAneesh Kumar K.V 				__releases(kernel_lock)
30037477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
3004ac27a0ecSDave Kleikamp {
3005d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3006ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3007617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
3008617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
3009617ba13bSMingming Cao 	ext4_fsblk_t block;
3010617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
301170bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3012ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3013ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3014ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3015ac27a0ecSDave Kleikamp 	struct inode *root;
30169f6200bbSTheodore Ts'o 	char *cp;
30170390131bSFrank Mayhar 	const char *descr;
3018dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3019ac27a0ecSDave Kleikamp 	int blocksize;
30204ec11028STheodore Ts'o 	unsigned int db_count;
30214ec11028STheodore Ts'o 	unsigned int i;
3022f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
3023bd81d8eeSLaurent Vivier 	__u64 blocks_count;
3024833f4077SPeter Zijlstra 	int err;
3025b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3026bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3027ac27a0ecSDave Kleikamp 
3028ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3029ac27a0ecSDave Kleikamp 	if (!sbi)
3030dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3031705895b6SPekka Enberg 
3032705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3033705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3034705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
3035705895b6SPekka Enberg 		kfree(sbi);
3036dcc7dae3SCyrill Gorcunov 		goto out_free_orig;
3037705895b6SPekka Enberg 	}
3038ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
3039ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
3040617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
3041617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
3042240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3043d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3044f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3045f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3046f613dfcbSTheodore Ts'o 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3047ac27a0ecSDave Kleikamp 
30489f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
30499f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
30509f6200bbSTheodore Ts'o 		*cp = '!';
30519f6200bbSTheodore Ts'o 
3052dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3053617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3054ac27a0ecSDave Kleikamp 	if (!blocksize) {
3055b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3056ac27a0ecSDave Kleikamp 		goto out_fail;
3057ac27a0ecSDave Kleikamp 	}
3058ac27a0ecSDave Kleikamp 
3059ac27a0ecSDave Kleikamp 	/*
3060617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3061ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3062ac27a0ecSDave Kleikamp 	 */
3063617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
306470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
306570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3066ac27a0ecSDave Kleikamp 	} else {
306770bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3068ac27a0ecSDave Kleikamp 	}
3069ac27a0ecSDave Kleikamp 
307070bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
3071b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3072ac27a0ecSDave Kleikamp 		goto out_fail;
3073ac27a0ecSDave Kleikamp 	}
3074ac27a0ecSDave Kleikamp 	/*
3075ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3076617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3077ac27a0ecSDave Kleikamp 	 */
3078617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3079ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3080ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3081617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3082617ba13bSMingming Cao 		goto cantfind_ext4;
3083afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3084ac27a0ecSDave Kleikamp 
3085ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3086ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3087fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3088617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3089fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
3090437ca0fdSDmitry Monakhov 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
3091437ca0fdSDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
3092437ca0fdSDmitry Monakhov 			"2.6.38");
3093fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3094437ca0fdSDmitry Monakhov 	}
3095617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3096fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
309703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
3098617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
3099fd8c37ecSTheodore Ts'o 		set_opt(sb, XATTR_USER);
31002e7842b8SHugh Dickins #endif
310103010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3102617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
3103fd8c37ecSTheodore Ts'o 		set_opt(sb, POSIX_ACL);
31042e7842b8SHugh Dickins #endif
3105617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3106fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3107617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3108fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3109617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3110fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3111ac27a0ecSDave Kleikamp 
3112617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3113fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3114bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3115fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3116bb4f397aSAneesh Kumar K.V 	else
3117fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
31188b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
3119fd8c37ecSTheodore Ts'o 		set_opt(sb, BLOCK_VALIDITY);
31208b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3121fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3122ac27a0ecSDave Kleikamp 
3123ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
3124ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
312530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
312630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
312730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3128ac27a0ecSDave Kleikamp 
31298b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3130fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3131ac27a0ecSDave Kleikamp 
31321e2462f9SMingming Cao 	/*
3133dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3134dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3135dd919b98SAneesh Kumar K.V 	 */
31368b67f04aSTheodore Ts'o 	if (!IS_EXT3_SB(sb) &&
31378b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3138fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3139dd919b98SAneesh Kumar K.V 
31408b67f04aSTheodore Ts'o 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
31418b67f04aSTheodore Ts'o 			   &journal_devnum, &journal_ioprio, NULL, 0)) {
31428b67f04aSTheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
31438b67f04aSTheodore Ts'o 			 "failed to parse options in superblock: %s",
31448b67f04aSTheodore Ts'o 			 sbi->s_es->s_mount_opts);
31458b67f04aSTheodore Ts'o 	}
3146b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3147b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
3148ac27a0ecSDave Kleikamp 		goto failed_mount;
3149ac27a0ecSDave Kleikamp 
3150ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3151482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3152ac27a0ecSDave Kleikamp 
3153617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3154617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3155617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3156617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
3157b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3158b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3159b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3160469108ffSTheodore Tso 
3161469108ffSTheodore Tso 	/*
3162ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3163ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3164ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3165ac27a0ecSDave Kleikamp 	 */
3166a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3167ac27a0ecSDave Kleikamp 		goto failed_mount;
3168a13fb1a4SEric Sandeen 
3169ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3170ac27a0ecSDave Kleikamp 
3171617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3172617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3173b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3174b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
3175ac27a0ecSDave Kleikamp 		goto failed_mount;
3176ac27a0ecSDave Kleikamp 	}
3177ac27a0ecSDave Kleikamp 
3178ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3179ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3180ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3181b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3182ce40733cSAneesh Kumar K.V 					blocksize);
3183ac27a0ecSDave Kleikamp 			goto failed_mount;
3184ac27a0ecSDave Kleikamp 		}
3185ac27a0ecSDave Kleikamp 
3186ac27a0ecSDave Kleikamp 		brelse(bh);
318770bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
318870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
318970bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
3190ac27a0ecSDave Kleikamp 		if (!bh) {
3191b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3192b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
3193ac27a0ecSDave Kleikamp 			goto failed_mount;
3194ac27a0ecSDave Kleikamp 		}
3195617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
3196ac27a0ecSDave Kleikamp 		sbi->s_es = es;
3197617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3198b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3199b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
3200ac27a0ecSDave Kleikamp 			goto failed_mount;
3201ac27a0ecSDave Kleikamp 		}
3202ac27a0ecSDave Kleikamp 	}
3203ac27a0ecSDave Kleikamp 
3204a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3205a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
3206f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3207f287a1a5STheodore Ts'o 						      has_huge_files);
3208f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3209ac27a0ecSDave Kleikamp 
3210617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3211617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3212617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3213ac27a0ecSDave Kleikamp 	} else {
3214ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3215ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3216617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
32171330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
3218ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
3219b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3220b31e1552SEric Sandeen 			       "unsupported inode size: %d",
3221ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
3222ac27a0ecSDave Kleikamp 			goto failed_mount;
3223ac27a0ecSDave Kleikamp 		}
3224ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3225ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3226ac27a0ecSDave Kleikamp 	}
32270b8e58a1SAndreas Dilger 
32280d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
32290d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
32308fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
32310d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3232d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
3233b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3234b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
32350d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
32360d1ee42fSAlexandre Ratchov 			goto failed_mount;
32370d1ee42fSAlexandre Ratchov 		}
32380d1ee42fSAlexandre Ratchov 	} else
32390d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
32400b8e58a1SAndreas Dilger 
3241ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3242ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3243b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
3244617ba13bSMingming Cao 		goto cantfind_ext4;
32450b8e58a1SAndreas Dilger 
3246617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3247ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
3248617ba13bSMingming Cao 		goto cantfind_ext4;
3249ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3250ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
32510d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3252ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
3253ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3254e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3255e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
32560b8e58a1SAndreas Dilger 
3257ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
3258ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3259ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
3260f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
3261f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
3262f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3263f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3264f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
3265f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3266f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
3267f99b2589STheodore Ts'o #else
3268f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3269f99b2589STheodore Ts'o #endif
3270f99b2589STheodore Ts'o 		sb->s_dirt = 1;
3271f99b2589STheodore Ts'o 	}
3272ac27a0ecSDave Kleikamp 
3273ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
3274b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3275b31e1552SEric Sandeen 		       "#blocks per group too big: %lu",
3276ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
3277ac27a0ecSDave Kleikamp 		goto failed_mount;
3278ac27a0ecSDave Kleikamp 	}
3279ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
3280b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3281b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
3282ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
3283ac27a0ecSDave Kleikamp 		goto failed_mount;
3284ac27a0ecSDave Kleikamp 	}
3285ac27a0ecSDave Kleikamp 
3286bf43d84bSEric Sandeen 	/*
3287bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
3288bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
3289bf43d84bSEric Sandeen 	 */
32905a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
329130ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
32925a9ae68aSDarrick J. Wong 	if (err) {
3293b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
3294bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
3295ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
329690c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
32975a9ae68aSDarrick J. Wong 		ret = err;
3298ac27a0ecSDave Kleikamp 		goto failed_mount;
3299ac27a0ecSDave Kleikamp 	}
3300ac27a0ecSDave Kleikamp 
3301617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3302617ba13bSMingming Cao 		goto cantfind_ext4;
3303e7c95593SEric Sandeen 
33040f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
33050f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
33060f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3307b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3308b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
33090f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
33100f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
33110f2ddca6SFrom: Thiemo Nagel 	}
33120f2ddca6SFrom: Thiemo Nagel 
33134ec11028STheodore Ts'o 	/*
33144ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
33154ec11028STheodore Ts'o 	 * of the filesystem.
33164ec11028STheodore Ts'o 	 */
33174ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3318b31e1552SEric Sandeen                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
3319b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
3320e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
33214ec11028STheodore Ts'o 			 ext4_blocks_count(es));
3322e7c95593SEric Sandeen 		goto failed_mount;
3323e7c95593SEric Sandeen 	}
3324bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
3325bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
3326bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3327bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
33284ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3329b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
33304ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
3331b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
33324ec11028STheodore Ts'o 		       ext4_blocks_count(es),
33334ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
33344ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
33354ec11028STheodore Ts'o 		goto failed_mount;
33364ec11028STheodore Ts'o 	}
3337bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
3338fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3339fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3340617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3341617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
3342ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
3343ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
3344ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
3345b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
3346ac27a0ecSDave Kleikamp 		goto failed_mount;
3347ac27a0ecSDave Kleikamp 	}
3348ac27a0ecSDave Kleikamp 
33493244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
33509f6200bbSTheodore Ts'o 	if (ext4_proc_root)
33519f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
33523244fcb1SAlexander Beregalov #endif
3353240799cdSTheodore Ts'o 
3354705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
3355ac27a0ecSDave Kleikamp 
3356ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
335770bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
3358ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
3359ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
3360b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3361b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
3362ac27a0ecSDave Kleikamp 			db_count = i;
3363ac27a0ecSDave Kleikamp 			goto failed_mount2;
3364ac27a0ecSDave Kleikamp 		}
3365ac27a0ecSDave Kleikamp 	}
3366bfff6873SLukas Czerner 	if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
3367b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
3368ac27a0ecSDave Kleikamp 		goto failed_mount2;
3369ac27a0ecSDave Kleikamp 	}
3370772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
3371772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
3372b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
3373b31e1552SEric Sandeen 			       "unable to initialize "
3374b31e1552SEric Sandeen 			       "flex_bg meta info!");
3375772cb7c8SJose R. Santos 			goto failed_mount2;
3376772cb7c8SJose R. Santos 		}
3377772cb7c8SJose R. Santos 
3378ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
3379ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3380ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
3381ac27a0ecSDave Kleikamp 
3382ce7e010aSTheodore Ts'o 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
3383ce7e010aSTheodore Ts'o 			ext4_count_free_blocks(sb));
3384ce7e010aSTheodore Ts'o 	if (!err) {
3385ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
3386ce7e010aSTheodore Ts'o 				ext4_count_free_inodes(sb));
3387ce7e010aSTheodore Ts'o 	}
3388ce7e010aSTheodore Ts'o 	if (!err) {
3389ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
3390ce7e010aSTheodore Ts'o 				ext4_count_dirs(sb));
3391ce7e010aSTheodore Ts'o 	}
3392ce7e010aSTheodore Ts'o 	if (!err) {
3393ce7e010aSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
3394ce7e010aSTheodore Ts'o 	}
3395ce7e010aSTheodore Ts'o 	if (err) {
3396ce7e010aSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
3397ce7e010aSTheodore Ts'o 		goto failed_mount3;
3398ce7e010aSTheodore Ts'o 	}
3399ce7e010aSTheodore Ts'o 
3400c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
340155138e0bSTheodore Ts'o 	sbi->s_max_writeback_mb_bump = 128;
3402c9de560dSAlex Tomas 
3403ac27a0ecSDave Kleikamp 	/*
3404ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
3405ac27a0ecSDave Kleikamp 	 */
34069ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
34079ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
3408617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
34099ca92389STheodore Ts'o 	else
34109ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
3411617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
3412617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
3413ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3414617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
3415617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
3416ac27a0ecSDave Kleikamp #endif
3417ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
34183b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
341932ed5058STheodore Ts'o 	mutex_init(&sbi->s_resize_lock);
3420ac27a0ecSDave Kleikamp 
3421ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
3422ac27a0ecSDave Kleikamp 
3423ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
3424617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
3425617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
3426ac27a0ecSDave Kleikamp 
3427ac27a0ecSDave Kleikamp 	/*
3428ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
3429ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
3430ac27a0ecSDave Kleikamp 	 */
3431ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
3432617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3433617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
3434ac27a0ecSDave Kleikamp 			goto failed_mount3;
34350390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
34360390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3437b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
3438b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
3439744692dcSJiaying Zhang 		goto failed_mount_wq;
3440ac27a0ecSDave Kleikamp 	} else {
3441fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
3442fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
34430390131bSFrank Mayhar 		sbi->s_journal = NULL;
34440390131bSFrank Mayhar 		needs_recovery = 0;
34450390131bSFrank Mayhar 		goto no_journal;
3446ac27a0ecSDave Kleikamp 	}
3447ac27a0ecSDave Kleikamp 
3448eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
3449eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3450eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
3451b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
3452744692dcSJiaying Zhang 		goto failed_mount_wq;
3453eb40a09cSJose R. Santos 	}
3454eb40a09cSJose R. Santos 
3455d4da6c9cSLinus Torvalds 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3456d4da6c9cSLinus Torvalds 		jbd2_journal_set_features(sbi->s_journal,
3457d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3458d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3459d4da6c9cSLinus Torvalds 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3460818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
3461818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
3462818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3463818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3464d4da6c9cSLinus Torvalds 	} else {
3465d4da6c9cSLinus Torvalds 		jbd2_journal_clear_features(sbi->s_journal,
3466d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3467d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3468d4da6c9cSLinus Torvalds 	}
3469818d276cSGirish Shilamkar 
3470ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
3471ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
3472ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
3473ac27a0ecSDave Kleikamp 	case 0:
3474ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
347563f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
347663f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
347763f57933SAndrew Morton 		 */
3478dab291afSMingming Cao 		if (jbd2_journal_check_available_features
3479dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
3480fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
3481ac27a0ecSDave Kleikamp 		else
3482fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
3483ac27a0ecSDave Kleikamp 		break;
3484ac27a0ecSDave Kleikamp 
3485617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
3486617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
3487dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
3488dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
3489b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
3490b31e1552SEric Sandeen 			       "requested data journaling mode");
3491744692dcSJiaying Zhang 			goto failed_mount_wq;
3492ac27a0ecSDave Kleikamp 		}
3493ac27a0ecSDave Kleikamp 	default:
3494ac27a0ecSDave Kleikamp 		break;
3495ac27a0ecSDave Kleikamp 	}
3496b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3497ac27a0ecSDave Kleikamp 
3498ce7e010aSTheodore Ts'o 	/*
3499ce7e010aSTheodore Ts'o 	 * The journal may have updated the bg summary counts, so we
3500ce7e010aSTheodore Ts'o 	 * need to update the global counters.
3501ce7e010aSTheodore Ts'o 	 */
3502ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_freeblocks_counter,
350384061e07SDmitry Monakhov 			   ext4_count_free_blocks(sb));
3504ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_freeinodes_counter,
350584061e07SDmitry Monakhov 			   ext4_count_free_inodes(sb));
3506ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_dirs_counter,
350784061e07SDmitry Monakhov 			   ext4_count_dirs(sb));
3508ce7e010aSTheodore Ts'o 	percpu_counter_set(&sbi->s_dirtyblocks_counter, 0);
3509206f7ab4SChristoph Hellwig 
3510ce7e010aSTheodore Ts'o no_journal:
35114c0425ffSMingming Cao 	EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
35124c0425ffSMingming Cao 	if (!EXT4_SB(sb)->dio_unwritten_wq) {
35134c0425ffSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
35144c0425ffSMingming Cao 		goto failed_mount_wq;
35154c0425ffSMingming Cao 	}
35164c0425ffSMingming Cao 
3517ac27a0ecSDave Kleikamp 	/*
3518dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
3519ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
3520ac27a0ecSDave Kleikamp 	 */
3521ac27a0ecSDave Kleikamp 
35221d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
35231d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
3524b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
35251d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
3526ac27a0ecSDave Kleikamp 		goto failed_mount4;
3527ac27a0ecSDave Kleikamp 	}
3528ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
35291d1fe1eeSDavid Howells 		iput(root);
3530b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
3531ac27a0ecSDave Kleikamp 		goto failed_mount4;
3532ac27a0ecSDave Kleikamp 	}
35331d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
35341d1fe1eeSDavid Howells 	if (!sb->s_root) {
3535b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
35361d1fe1eeSDavid Howells 		iput(root);
35371d1fe1eeSDavid Howells 		ret = -ENOMEM;
35381d1fe1eeSDavid Howells 		goto failed_mount4;
35391d1fe1eeSDavid Howells 	}
3540ac27a0ecSDave Kleikamp 
3541617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
3542ef7f3835SKalpak Shah 
3543ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
3544ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3545ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3546ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
3547ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3548ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
3549ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3550ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
3551ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3552ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
3553ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
3554ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
3555ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
3556ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
3557ef7f3835SKalpak Shah 		}
3558ef7f3835SKalpak Shah 	}
3559ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
3560ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3561ef7f3835SKalpak Shah 							sbi->s_inode_size) {
3562ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3563ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
3564b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
3565b31e1552SEric Sandeen 			 "available");
3566ef7f3835SKalpak Shah 	}
3567ef7f3835SKalpak Shah 
356890576c0bSTheodore Ts'o 	if (test_opt(sb, DELALLOC) &&
356990576c0bSTheodore Ts'o 	    (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
3570b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
3571b31e1552SEric Sandeen 			 "requested data journaling mode");
3572fd8c37ecSTheodore Ts'o 		clear_opt(sb, DELALLOC);
357390576c0bSTheodore Ts'o 	}
3574744692dcSJiaying Zhang 	if (test_opt(sb, DIOREAD_NOLOCK)) {
3575744692dcSJiaying Zhang 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
3576744692dcSJiaying Zhang 			ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
3577744692dcSJiaying Zhang 				"option - requested data journaling mode");
3578fd8c37ecSTheodore Ts'o 			clear_opt(sb, DIOREAD_NOLOCK);
3579744692dcSJiaying Zhang 		}
3580744692dcSJiaying Zhang 		if (sb->s_blocksize < PAGE_SIZE) {
3581744692dcSJiaying Zhang 			ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
3582744692dcSJiaying Zhang 				"option - block size is too small");
3583fd8c37ecSTheodore Ts'o 			clear_opt(sb, DIOREAD_NOLOCK);
3584744692dcSJiaying Zhang 		}
3585744692dcSJiaying Zhang 	}
3586c2774d84SAneesh Kumar K.V 
35876fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
35886fd058f7STheodore Ts'o 	if (err) {
3589b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
3590fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
35916fd058f7STheodore Ts'o 		goto failed_mount4;
35926fd058f7STheodore Ts'o 	}
35936fd058f7STheodore Ts'o 
3594c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
3595c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
3596c2774d84SAneesh Kumar K.V 	if (err) {
3597421f91d2SUwe Kleine-König 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
3598c2774d84SAneesh Kumar K.V 			 err);
3599c2774d84SAneesh Kumar K.V 		goto failed_mount4;
3600c2774d84SAneesh Kumar K.V 	}
3601c2774d84SAneesh Kumar K.V 
3602bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
3603bfff6873SLukas Czerner 	if (err)
3604bfff6873SLukas Czerner 		goto failed_mount4;
3605bfff6873SLukas Czerner 
36063197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
36073197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
36083197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
36093197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
36103197ebdbSTheodore Ts'o 	if (err) {
36113197ebdbSTheodore Ts'o 		ext4_mb_release(sb);
36123197ebdbSTheodore Ts'o 		ext4_ext_release(sb);
36133197ebdbSTheodore Ts'o 		goto failed_mount4;
36143197ebdbSTheodore Ts'o 	};
36153197ebdbSTheodore Ts'o 
3616617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3617617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
3618617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
36190390131bSFrank Mayhar 	if (needs_recovery) {
3620b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
3621617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
36220390131bSFrank Mayhar 	}
36230390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
36240390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
36250390131bSFrank Mayhar 			descr = " journalled data mode";
36260390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
36270390131bSFrank Mayhar 			descr = " ordered data mode";
36280390131bSFrank Mayhar 		else
36290390131bSFrank Mayhar 			descr = " writeback data mode";
36300390131bSFrank Mayhar 	} else
36310390131bSFrank Mayhar 		descr = "out journal";
36320390131bSFrank Mayhar 
3633d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
36348b67f04aSTheodore Ts'o 		 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
36358b67f04aSTheodore Ts'o 		 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
3636ac27a0ecSDave Kleikamp 
363766e61a9eSTheodore Ts'o 	init_timer(&sbi->s_err_report);
363866e61a9eSTheodore Ts'o 	sbi->s_err_report.function = print_daily_error_info;
363966e61a9eSTheodore Ts'o 	sbi->s_err_report.data = (unsigned long) sb;
364066e61a9eSTheodore Ts'o 	if (es->s_error_count)
364166e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
3642ac27a0ecSDave Kleikamp 
3643d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
3644ac27a0ecSDave Kleikamp 	return 0;
3645ac27a0ecSDave Kleikamp 
3646617ba13bSMingming Cao cantfind_ext4:
3647ac27a0ecSDave Kleikamp 	if (!silent)
3648b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
3649ac27a0ecSDave Kleikamp 	goto failed_mount;
3650ac27a0ecSDave Kleikamp 
3651ac27a0ecSDave Kleikamp failed_mount4:
3652b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
36534c0425ffSMingming Cao 	destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
36544c0425ffSMingming Cao failed_mount_wq:
36556fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
36560390131bSFrank Mayhar 	if (sbi->s_journal) {
3657dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
365847b4a50bSJan Kara 		sbi->s_journal = NULL;
36590390131bSFrank Mayhar 	}
3660ac27a0ecSDave Kleikamp failed_mount3:
3661c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups) {
3662c5ca7c76STheodore Ts'o 		if (is_vmalloc_addr(sbi->s_flex_groups))
3663c5ca7c76STheodore Ts'o 			vfree(sbi->s_flex_groups);
3664c5ca7c76STheodore Ts'o 		else
3665c5ca7c76STheodore Ts'o 			kfree(sbi->s_flex_groups);
3666c5ca7c76STheodore Ts'o 	}
3667ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
3668ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
3669ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
3670ce7e010aSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
3671ac27a0ecSDave Kleikamp failed_mount2:
3672ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
3673ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
3674ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
3675ac27a0ecSDave Kleikamp failed_mount:
3676240799cdSTheodore Ts'o 	if (sbi->s_proc) {
36779f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
3678240799cdSTheodore Ts'o 	}
3679ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3680ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3681ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
3682ac27a0ecSDave Kleikamp #endif
3683617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
3684ac27a0ecSDave Kleikamp 	brelse(bh);
3685ac27a0ecSDave Kleikamp out_fail:
3686ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
3687f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
3688ac27a0ecSDave Kleikamp 	kfree(sbi);
3689dcc7dae3SCyrill Gorcunov out_free_orig:
3690d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
36911d1fe1eeSDavid Howells 	return ret;
3692ac27a0ecSDave Kleikamp }
3693ac27a0ecSDave Kleikamp 
3694ac27a0ecSDave Kleikamp /*
3695ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
3696ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
3697ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
3698ac27a0ecSDave Kleikamp  */
3699617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
3700ac27a0ecSDave Kleikamp {
3701617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3702ac27a0ecSDave Kleikamp 
3703ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
370430773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
370530773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
3706ac27a0ecSDave Kleikamp 
3707a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
3708ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
3709dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
3710ac27a0ecSDave Kleikamp 	else
3711dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
37125bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
37135bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
37145bf5683aSHidehiro Kawai 	else
37155bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
3716a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
3717ac27a0ecSDave Kleikamp }
3718ac27a0ecSDave Kleikamp 
3719617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
3720ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
3721ac27a0ecSDave Kleikamp {
3722ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
3723ac27a0ecSDave Kleikamp 	journal_t *journal;
3724ac27a0ecSDave Kleikamp 
37250390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
37260390131bSFrank Mayhar 
3727ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
3728ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
3729ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
3730ac27a0ecSDave Kleikamp 
37311d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
37321d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
3733b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
3734ac27a0ecSDave Kleikamp 		return NULL;
3735ac27a0ecSDave Kleikamp 	}
3736ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
3737ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
3738ac27a0ecSDave Kleikamp 		iput(journal_inode);
3739b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3740ac27a0ecSDave Kleikamp 		return NULL;
3741ac27a0ecSDave Kleikamp 	}
3742ac27a0ecSDave Kleikamp 
3743e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3744ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
37451d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
3746b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
3747ac27a0ecSDave Kleikamp 		iput(journal_inode);
3748ac27a0ecSDave Kleikamp 		return NULL;
3749ac27a0ecSDave Kleikamp 	}
3750ac27a0ecSDave Kleikamp 
3751dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
3752ac27a0ecSDave Kleikamp 	if (!journal) {
3753b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3754ac27a0ecSDave Kleikamp 		iput(journal_inode);
3755ac27a0ecSDave Kleikamp 		return NULL;
3756ac27a0ecSDave Kleikamp 	}
3757ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3758617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3759ac27a0ecSDave Kleikamp 	return journal;
3760ac27a0ecSDave Kleikamp }
3761ac27a0ecSDave Kleikamp 
3762617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
3763ac27a0ecSDave Kleikamp 				       dev_t j_dev)
3764ac27a0ecSDave Kleikamp {
3765ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3766ac27a0ecSDave Kleikamp 	journal_t *journal;
3767617ba13bSMingming Cao 	ext4_fsblk_t start;
3768617ba13bSMingming Cao 	ext4_fsblk_t len;
3769ac27a0ecSDave Kleikamp 	int hblock, blocksize;
3770617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
3771ac27a0ecSDave Kleikamp 	unsigned long offset;
3772617ba13bSMingming Cao 	struct ext4_super_block *es;
3773ac27a0ecSDave Kleikamp 	struct block_device *bdev;
3774ac27a0ecSDave Kleikamp 
37750390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
37760390131bSFrank Mayhar 
3777b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
3778ac27a0ecSDave Kleikamp 	if (bdev == NULL)
3779ac27a0ecSDave Kleikamp 		return NULL;
3780ac27a0ecSDave Kleikamp 
3781ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
3782e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
3783ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
3784b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3785b31e1552SEric Sandeen 			"blocksize too small for journal device");
3786ac27a0ecSDave Kleikamp 		goto out_bdev;
3787ac27a0ecSDave Kleikamp 	}
3788ac27a0ecSDave Kleikamp 
3789617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3790617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3791ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3792ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3793b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3794b31e1552SEric Sandeen 		       "external journal");
3795ac27a0ecSDave Kleikamp 		goto out_bdev;
3796ac27a0ecSDave Kleikamp 	}
3797ac27a0ecSDave Kleikamp 
3798617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3799617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3800ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3801617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3802b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
3803b31e1552SEric Sandeen 					"bad superblock");
3804ac27a0ecSDave Kleikamp 		brelse(bh);
3805ac27a0ecSDave Kleikamp 		goto out_bdev;
3806ac27a0ecSDave Kleikamp 	}
3807ac27a0ecSDave Kleikamp 
3808617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3809b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3810ac27a0ecSDave Kleikamp 		brelse(bh);
3811ac27a0ecSDave Kleikamp 		goto out_bdev;
3812ac27a0ecSDave Kleikamp 	}
3813ac27a0ecSDave Kleikamp 
3814bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
3815ac27a0ecSDave Kleikamp 	start = sb_block + 1;
3816ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
3817ac27a0ecSDave Kleikamp 
3818dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3819ac27a0ecSDave Kleikamp 					start, len, blocksize);
3820ac27a0ecSDave Kleikamp 	if (!journal) {
3821b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
3822ac27a0ecSDave Kleikamp 		goto out_bdev;
3823ac27a0ecSDave Kleikamp 	}
3824ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3825ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
3826ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
3827ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
3828b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3829ac27a0ecSDave Kleikamp 		goto out_journal;
3830ac27a0ecSDave Kleikamp 	}
3831ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3832b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
3833b31e1552SEric Sandeen 					"user (unsupported) - %d",
3834ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
3835ac27a0ecSDave Kleikamp 		goto out_journal;
3836ac27a0ecSDave Kleikamp 	}
3837617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
3838617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3839ac27a0ecSDave Kleikamp 	return journal;
38400b8e58a1SAndreas Dilger 
3841ac27a0ecSDave Kleikamp out_journal:
3842dab291afSMingming Cao 	jbd2_journal_destroy(journal);
3843ac27a0ecSDave Kleikamp out_bdev:
3844617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
3845ac27a0ecSDave Kleikamp 	return NULL;
3846ac27a0ecSDave Kleikamp }
3847ac27a0ecSDave Kleikamp 
3848617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
3849617ba13bSMingming Cao 			     struct ext4_super_block *es,
3850ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
3851ac27a0ecSDave Kleikamp {
3852ac27a0ecSDave Kleikamp 	journal_t *journal;
3853ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3854ac27a0ecSDave Kleikamp 	dev_t journal_dev;
3855ac27a0ecSDave Kleikamp 	int err = 0;
3856ac27a0ecSDave Kleikamp 	int really_read_only;
3857ac27a0ecSDave Kleikamp 
38580390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
38590390131bSFrank Mayhar 
3860ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3861ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3862b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3863b31e1552SEric Sandeen 			"numbers have changed");
3864ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
3865ac27a0ecSDave Kleikamp 	} else
3866ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3867ac27a0ecSDave Kleikamp 
3868ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
3869ac27a0ecSDave Kleikamp 
3870ac27a0ecSDave Kleikamp 	/*
3871ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
3872ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
3873ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
3874ac27a0ecSDave Kleikamp 	 */
3875617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3876ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
3877b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
3878b31e1552SEric Sandeen 					"required on readonly filesystem");
3879ac27a0ecSDave Kleikamp 			if (really_read_only) {
3880b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
3881b31e1552SEric Sandeen 					"unavailable, cannot proceed");
3882ac27a0ecSDave Kleikamp 				return -EROFS;
3883ac27a0ecSDave Kleikamp 			}
3884b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
3885b31e1552SEric Sandeen 			       "be enabled during recovery");
3886ac27a0ecSDave Kleikamp 		}
3887ac27a0ecSDave Kleikamp 	}
3888ac27a0ecSDave Kleikamp 
3889ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
3890b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3891b31e1552SEric Sandeen 		       "and inode journals!");
3892ac27a0ecSDave Kleikamp 		return -EINVAL;
3893ac27a0ecSDave Kleikamp 	}
3894ac27a0ecSDave Kleikamp 
3895ac27a0ecSDave Kleikamp 	if (journal_inum) {
3896617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
3897ac27a0ecSDave Kleikamp 			return -EINVAL;
3898ac27a0ecSDave Kleikamp 	} else {
3899617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3900ac27a0ecSDave Kleikamp 			return -EINVAL;
3901ac27a0ecSDave Kleikamp 	}
3902ac27a0ecSDave Kleikamp 
390390576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
3904b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
39054776004fSTheodore Ts'o 
3906ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3907dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
3908ac27a0ecSDave Kleikamp 		if (err)  {
3909b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error updating journal");
3910dab291afSMingming Cao 			jbd2_journal_destroy(journal);
3911ac27a0ecSDave Kleikamp 			return err;
3912ac27a0ecSDave Kleikamp 		}
3913ac27a0ecSDave Kleikamp 	}
3914ac27a0ecSDave Kleikamp 
3915617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3916dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
39171c13d5c0STheodore Ts'o 	if (!err) {
39181c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
39191c13d5c0STheodore Ts'o 		if (save)
39201c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
39211c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
3922dab291afSMingming Cao 		err = jbd2_journal_load(journal);
39231c13d5c0STheodore Ts'o 		if (save)
39241c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
39251c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
39261c13d5c0STheodore Ts'o 		kfree(save);
39271c13d5c0STheodore Ts'o 	}
3928ac27a0ecSDave Kleikamp 
3929ac27a0ecSDave Kleikamp 	if (err) {
3930b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
3931dab291afSMingming Cao 		jbd2_journal_destroy(journal);
3932ac27a0ecSDave Kleikamp 		return err;
3933ac27a0ecSDave Kleikamp 	}
3934ac27a0ecSDave Kleikamp 
3935617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
3936617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
3937ac27a0ecSDave Kleikamp 
3938c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
3939ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3940ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
3941ac27a0ecSDave Kleikamp 
3942ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
3943e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3944ac27a0ecSDave Kleikamp 	}
3945ac27a0ecSDave Kleikamp 
3946ac27a0ecSDave Kleikamp 	return 0;
3947ac27a0ecSDave Kleikamp }
3948ac27a0ecSDave Kleikamp 
3949e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
3950ac27a0ecSDave Kleikamp {
3951e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3952617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3953c4be0c1dSTakashi Sato 	int error = 0;
3954ac27a0ecSDave Kleikamp 
3955ac27a0ecSDave Kleikamp 	if (!sbh)
3956c4be0c1dSTakashi Sato 		return error;
3957914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
3958914258bfSTheodore Ts'o 		/*
3959914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
3960914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
3961914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
3962914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
3963914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
3964914258bfSTheodore Ts'o 		 * write and hope for the best.
3965914258bfSTheodore Ts'o 		 */
3966b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
3967b31e1552SEric Sandeen 		       "superblock detected");
3968914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
3969914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
3970914258bfSTheodore Ts'o 	}
397171290b36STheodore Ts'o 	/*
397271290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
397371290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
397471290b36STheodore Ts'o 	 * write time when we are mounting the root file system
397571290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
397671290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
397771290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
397871290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
397971290b36STheodore Ts'o 	 * to complain and force a full file system check.
398071290b36STheodore Ts'o 	 */
398171290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
3982ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
3983f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3984afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
3985afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3986afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3987afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
3988f613dfcbSTheodore Ts'o 	else
3989f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
3990f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
39915d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
39925d1b1b3fSAneesh Kumar K.V 					   &EXT4_SB(sb)->s_freeblocks_counter));
39937f93cff9STheodore Ts'o 	es->s_free_inodes_count =
39947f93cff9STheodore Ts'o 		cpu_to_le32(percpu_counter_sum_positive(
39955d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
39967234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
3997ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
3998ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
3999914258bfSTheodore Ts'o 	if (sync) {
4000c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
4001c4be0c1dSTakashi Sato 		if (error)
4002c4be0c1dSTakashi Sato 			return error;
4003c4be0c1dSTakashi Sato 
4004c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
4005c4be0c1dSTakashi Sato 		if (error) {
4006b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4007b31e1552SEric Sandeen 			       "superblock");
4008914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
4009914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
4010914258bfSTheodore Ts'o 		}
4011914258bfSTheodore Ts'o 	}
4012c4be0c1dSTakashi Sato 	return error;
4013ac27a0ecSDave Kleikamp }
4014ac27a0ecSDave Kleikamp 
4015ac27a0ecSDave Kleikamp /*
4016ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
4017ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
4018ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
4019ac27a0ecSDave Kleikamp  */
4020617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
4021617ba13bSMingming Cao 					struct ext4_super_block *es)
4022ac27a0ecSDave Kleikamp {
4023617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
4024ac27a0ecSDave Kleikamp 
40250390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
40260390131bSFrank Mayhar 		BUG_ON(journal != NULL);
40270390131bSFrank Mayhar 		return;
40280390131bSFrank Mayhar 	}
4029dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
40307ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
40317ffe1ea8SHidehiro Kawai 		goto out;
40327ffe1ea8SHidehiro Kawai 
4033617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
4034ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
4035617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4036e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4037ac27a0ecSDave Kleikamp 	}
40387ffe1ea8SHidehiro Kawai 
40397ffe1ea8SHidehiro Kawai out:
4040dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4041ac27a0ecSDave Kleikamp }
4042ac27a0ecSDave Kleikamp 
4043ac27a0ecSDave Kleikamp /*
4044ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
4045ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
4046ac27a0ecSDave Kleikamp  * main filesystem now.
4047ac27a0ecSDave Kleikamp  */
4048617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
4049617ba13bSMingming Cao 				   struct ext4_super_block *es)
4050ac27a0ecSDave Kleikamp {
4051ac27a0ecSDave Kleikamp 	journal_t *journal;
4052ac27a0ecSDave Kleikamp 	int j_errno;
4053ac27a0ecSDave Kleikamp 	const char *errstr;
4054ac27a0ecSDave Kleikamp 
40550390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
40560390131bSFrank Mayhar 
4057617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
4058ac27a0ecSDave Kleikamp 
4059ac27a0ecSDave Kleikamp 	/*
4060ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
4061617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
4062ac27a0ecSDave Kleikamp 	 */
4063ac27a0ecSDave Kleikamp 
4064dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
4065ac27a0ecSDave Kleikamp 	if (j_errno) {
4066ac27a0ecSDave Kleikamp 		char nbuf[16];
4067ac27a0ecSDave Kleikamp 
4068617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
406912062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
4070ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
407112062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4072ac27a0ecSDave Kleikamp 
4073617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4074617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4075e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4076ac27a0ecSDave Kleikamp 
4077dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
4078ac27a0ecSDave Kleikamp 	}
4079ac27a0ecSDave Kleikamp }
4080ac27a0ecSDave Kleikamp 
4081ac27a0ecSDave Kleikamp /*
4082ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
4083ac27a0ecSDave Kleikamp  * and wait on the commit.
4084ac27a0ecSDave Kleikamp  */
4085617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
4086ac27a0ecSDave Kleikamp {
4087ac27a0ecSDave Kleikamp 	journal_t *journal;
40880390131bSFrank Mayhar 	int ret = 0;
4089ac27a0ecSDave Kleikamp 
4090ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
4091ac27a0ecSDave Kleikamp 		return 0;
4092ac27a0ecSDave Kleikamp 
4093617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
40946b0310fbSEric Sandeen 	if (journal) {
4095437f88ccSEric Sandeen 		vfs_check_frozen(sb, SB_FREEZE_TRANS);
4096617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
40976b0310fbSEric Sandeen 	}
40980390131bSFrank Mayhar 
4099ac27a0ecSDave Kleikamp 	return ret;
4100ac27a0ecSDave Kleikamp }
4101ac27a0ecSDave Kleikamp 
4102617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
4103ac27a0ecSDave Kleikamp {
4104ebc1ac16SChristoph Hellwig 	lock_super(sb);
4105e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4106ebc1ac16SChristoph Hellwig 	unlock_super(sb);
41070390131bSFrank Mayhar }
4108ac27a0ecSDave Kleikamp 
4109617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
4110ac27a0ecSDave Kleikamp {
411114ce0cb4STheodore Ts'o 	int ret = 0;
41129eddacf9SJan Kara 	tid_t target;
41138d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4114ac27a0ecSDave Kleikamp 
41159bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
41168d5d02e6SMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
41178d5d02e6SMingming Cao 	if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4118ac27a0ecSDave Kleikamp 		if (wait)
41198d5d02e6SMingming Cao 			jbd2_log_wait_commit(sbi->s_journal, target);
41200390131bSFrank Mayhar 	}
412114ce0cb4STheodore Ts'o 	return ret;
4122ac27a0ecSDave Kleikamp }
4123ac27a0ecSDave Kleikamp 
4124ac27a0ecSDave Kleikamp /*
4125ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
4126ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
4127ac27a0ecSDave Kleikamp  */
4128c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
4129ac27a0ecSDave Kleikamp {
4130c4be0c1dSTakashi Sato 	int error = 0;
4131c4be0c1dSTakashi Sato 	journal_t *journal;
4132ac27a0ecSDave Kleikamp 
41339ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
41349ca92389STheodore Ts'o 		return 0;
41359ca92389STheodore Ts'o 
4136c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
4137ac27a0ecSDave Kleikamp 
4138ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
4139dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
41407ffe1ea8SHidehiro Kawai 
41417ffe1ea8SHidehiro Kawai 	/*
41429ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
41439ca92389STheodore Ts'o 	 * the journal.
41447ffe1ea8SHidehiro Kawai 	 */
4145c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
41466b0310fbSEric Sandeen 	if (error < 0)
41476b0310fbSEric Sandeen 		goto out;
4148ac27a0ecSDave Kleikamp 
4149ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
4150617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4151e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
41526b0310fbSEric Sandeen out:
41536b0310fbSEric Sandeen 	/* we rely on s_frozen to stop further updates */
41546b0310fbSEric Sandeen 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
41556b0310fbSEric Sandeen 	return error;
4156ac27a0ecSDave Kleikamp }
4157ac27a0ecSDave Kleikamp 
4158ac27a0ecSDave Kleikamp /*
4159ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4160ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
4161ac27a0ecSDave Kleikamp  */
4162c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
4163ac27a0ecSDave Kleikamp {
41649ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
41659ca92389STheodore Ts'o 		return 0;
41669ca92389STheodore Ts'o 
4167ac27a0ecSDave Kleikamp 	lock_super(sb);
41689ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
4169617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4170e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
4171ac27a0ecSDave Kleikamp 	unlock_super(sb);
4172c4be0c1dSTakashi Sato 	return 0;
4173ac27a0ecSDave Kleikamp }
4174ac27a0ecSDave Kleikamp 
4175673c6100STheodore Ts'o /*
4176673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
4177673c6100STheodore Ts'o  */
4178673c6100STheodore Ts'o struct ext4_mount_options {
4179673c6100STheodore Ts'o 	unsigned long s_mount_opt;
4180a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
4181673c6100STheodore Ts'o 	uid_t s_resuid;
4182673c6100STheodore Ts'o 	gid_t s_resgid;
4183673c6100STheodore Ts'o 	unsigned long s_commit_interval;
4184673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
4185673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
4186673c6100STheodore Ts'o 	int s_jquota_fmt;
4187673c6100STheodore Ts'o 	char *s_qf_names[MAXQUOTAS];
4188673c6100STheodore Ts'o #endif
4189673c6100STheodore Ts'o };
4190673c6100STheodore Ts'o 
4191617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
4192ac27a0ecSDave Kleikamp {
4193617ba13bSMingming Cao 	struct ext4_super_block *es;
4194617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4195617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
4196ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
4197617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
4198c79d967dSChristoph Hellwig 	int enable_quota = 0;
41998a266467STheodore Ts'o 	ext4_group_t g;
4200b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4201ac27a0ecSDave Kleikamp 	int err;
4202ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4203ac27a0ecSDave Kleikamp 	int i;
4204ac27a0ecSDave Kleikamp #endif
4205d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
4206ac27a0ecSDave Kleikamp 
4207ac27a0ecSDave Kleikamp 	/* Store the original options */
4208bbd6851aSAl Viro 	lock_super(sb);
4209ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
4210ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
4211a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4212ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
4213ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
4214ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
421530773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
421630773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4217ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4218ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4219ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4220ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
4221ac27a0ecSDave Kleikamp #endif
4222b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4223b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4224ac27a0ecSDave Kleikamp 
4225ac27a0ecSDave Kleikamp 	/*
4226ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
4227ac27a0ecSDave Kleikamp 	 */
4228b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
4229b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
4230ac27a0ecSDave Kleikamp 		err = -EINVAL;
4231ac27a0ecSDave Kleikamp 		goto restore_opts;
4232ac27a0ecSDave Kleikamp 	}
4233ac27a0ecSDave Kleikamp 
42344ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4235c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
4236ac27a0ecSDave Kleikamp 
4237ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4238482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4239ac27a0ecSDave Kleikamp 
4240ac27a0ecSDave Kleikamp 	es = sbi->s_es;
4241ac27a0ecSDave Kleikamp 
4242b3881f74STheodore Ts'o 	if (sbi->s_journal) {
4243617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
4244b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4245b3881f74STheodore Ts'o 	}
4246ac27a0ecSDave Kleikamp 
4247ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
4248bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
42494ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
4250ac27a0ecSDave Kleikamp 			err = -EROFS;
4251ac27a0ecSDave Kleikamp 			goto restore_opts;
4252ac27a0ecSDave Kleikamp 		}
4253ac27a0ecSDave Kleikamp 
4254ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
42550f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
42560f0dd62fSChristoph Hellwig 			if (err < 0)
4257c79d967dSChristoph Hellwig 				goto restore_opts;
4258c79d967dSChristoph Hellwig 
4259ac27a0ecSDave Kleikamp 			/*
4260ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
4261ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
4262ac27a0ecSDave Kleikamp 			 */
4263ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
4264ac27a0ecSDave Kleikamp 
4265ac27a0ecSDave Kleikamp 			/*
4266ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
4267ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
4268ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
4269ac27a0ecSDave Kleikamp 			 */
4270617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4271617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
4272ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
4273ac27a0ecSDave Kleikamp 
4274a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
4275617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
4276ac27a0ecSDave Kleikamp 		} else {
4277a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
4278a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
4279ac27a0ecSDave Kleikamp 				err = -EROFS;
4280ac27a0ecSDave Kleikamp 				goto restore_opts;
4281ac27a0ecSDave Kleikamp 			}
4282ead6596bSEric Sandeen 			/*
42838a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
42840b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
42858a266467STheodore Ts'o 			 */
42868a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
42878a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
42888a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
42898a266467STheodore Ts'o 
42908a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
4291b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
4292b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
42938a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
42948a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
42958a266467STheodore Ts'o 					err = -EINVAL;
42968a266467STheodore Ts'o 					goto restore_opts;
42978a266467STheodore Ts'o 				}
42988a266467STheodore Ts'o 			}
42998a266467STheodore Ts'o 
43008a266467STheodore Ts'o 			/*
4301ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
4302ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
4303ead6596bSEric Sandeen 			 * require a full umount/remount for now.
4304ead6596bSEric Sandeen 			 */
4305ead6596bSEric Sandeen 			if (es->s_last_orphan) {
4306b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
4307ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
4308ead6596bSEric Sandeen 				       "orphan inode list.  Please "
4309b31e1552SEric Sandeen 				       "umount/remount instead");
4310ead6596bSEric Sandeen 				err = -EINVAL;
4311ead6596bSEric Sandeen 				goto restore_opts;
4312ead6596bSEric Sandeen 			}
4313ead6596bSEric Sandeen 
4314ac27a0ecSDave Kleikamp 			/*
4315ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
4316ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
4317ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
4318ac27a0ecSDave Kleikamp 			 * the partition.)
4319ac27a0ecSDave Kleikamp 			 */
43200390131bSFrank Mayhar 			if (sbi->s_journal)
4321617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
4322ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
4323617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
4324ac27a0ecSDave Kleikamp 				goto restore_opts;
4325617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
4326ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
4327c79d967dSChristoph Hellwig 			enable_quota = 1;
4328ac27a0ecSDave Kleikamp 		}
4329ac27a0ecSDave Kleikamp 	}
4330bfff6873SLukas Czerner 
4331bfff6873SLukas Czerner 	/*
4332bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
4333bfff6873SLukas Czerner 	 * current settings
4334bfff6873SLukas Czerner 	 */
4335bfff6873SLukas Czerner 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4336bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
4337bfff6873SLukas Czerner 	else {
4338bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
4339bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
4340bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
4341bfff6873SLukas Czerner 	}
4342bfff6873SLukas Czerner 
43436fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
43440390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
4345e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
43460390131bSFrank Mayhar 
4347ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4348ac27a0ecSDave Kleikamp 	/* Release old quota file names */
4349ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
4350ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
4351ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4352ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
4353ac27a0ecSDave Kleikamp #endif
4354bbd6851aSAl Viro 	unlock_super(sb);
4355c79d967dSChristoph Hellwig 	if (enable_quota)
43560f0dd62fSChristoph Hellwig 		dquot_resume(sb, -1);
4357d4c402d9SCurt Wohlgemuth 
4358d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4359d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4360ac27a0ecSDave Kleikamp 	return 0;
43610b8e58a1SAndreas Dilger 
4362ac27a0ecSDave Kleikamp restore_opts:
4363ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
4364ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
4365a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
4366ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
4367ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
4368ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
436930773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
437030773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
4371ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4372ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
4373ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
4374ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
4375ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4376ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
4377ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
4378ac27a0ecSDave Kleikamp 	}
4379ac27a0ecSDave Kleikamp #endif
4380bbd6851aSAl Viro 	unlock_super(sb);
4381d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4382ac27a0ecSDave Kleikamp 	return err;
4383ac27a0ecSDave Kleikamp }
4384ac27a0ecSDave Kleikamp 
4385617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
4386ac27a0ecSDave Kleikamp {
4387ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
4388617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4389617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
4390960cc398SPekka Enberg 	u64 fsid;
4391ac27a0ecSDave Kleikamp 
43925e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
43935e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
43946bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
43958df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
43965e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
4397ac27a0ecSDave Kleikamp 
4398ac27a0ecSDave Kleikamp 		/*
43995e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
44005e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
44015e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
4402ac27a0ecSDave Kleikamp 		 */
4403ac27a0ecSDave Kleikamp 
4404ac27a0ecSDave Kleikamp 		/*
4405ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
4406ac27a0ecSDave Kleikamp 		 * overhead
4407ac27a0ecSDave Kleikamp 		 */
4408ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
4409ac27a0ecSDave Kleikamp 
4410ac27a0ecSDave Kleikamp 		/*
4411ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
4412ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
4413ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
4414ac27a0ecSDave Kleikamp 		 */
4415ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
4416617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
4417617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
4418ac27a0ecSDave Kleikamp 			cond_resched();
4419ac27a0ecSDave Kleikamp 		}
4420ac27a0ecSDave Kleikamp 
4421ac27a0ecSDave Kleikamp 		/*
4422ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
4423ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
4424ac27a0ecSDave Kleikamp 		 */
44255e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
44265e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
44275e70030dSBadari Pulavarty 		smp_wmb();
44286bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
4429ac27a0ecSDave Kleikamp 	}
4430ac27a0ecSDave Kleikamp 
4431617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
4432ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
44335e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
44346bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
44356bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
4436bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
4437bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
4438ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
4439ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
444052d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
4441617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
4442960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
4443960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
4444960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
4445960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
44460b8e58a1SAndreas Dilger 
4447ac27a0ecSDave Kleikamp 	return 0;
4448ac27a0ecSDave Kleikamp }
4449ac27a0ecSDave Kleikamp 
44500b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
44510b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
4452ac27a0ecSDave Kleikamp  * Process 1                         Process 2
4453617ba13bSMingming Cao  * ext4_create()                     quota_sync()
4454dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
4455871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
4456dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
4457ac27a0ecSDave Kleikamp  *
4458ac27a0ecSDave Kleikamp  */
4459ac27a0ecSDave Kleikamp 
4460ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4461ac27a0ecSDave Kleikamp 
4462ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
4463ac27a0ecSDave Kleikamp {
4464ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
4465ac27a0ecSDave Kleikamp }
4466ac27a0ecSDave Kleikamp 
4467617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
4468ac27a0ecSDave Kleikamp {
4469ac27a0ecSDave Kleikamp 	int ret, err;
4470ac27a0ecSDave Kleikamp 	handle_t *handle;
4471ac27a0ecSDave Kleikamp 	struct inode *inode;
4472ac27a0ecSDave Kleikamp 
4473ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
4474617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
4475617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
4476ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4477ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4478ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
4479617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4480ac27a0ecSDave Kleikamp 	if (!ret)
4481ac27a0ecSDave Kleikamp 		ret = err;
4482ac27a0ecSDave Kleikamp 	return ret;
4483ac27a0ecSDave Kleikamp }
4484ac27a0ecSDave Kleikamp 
4485617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
4486ac27a0ecSDave Kleikamp {
4487ac27a0ecSDave Kleikamp 	int ret, err;
4488ac27a0ecSDave Kleikamp 	handle_t *handle;
4489ac27a0ecSDave Kleikamp 
4490617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4491617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
4492ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4493ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4494ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
4495617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4496ac27a0ecSDave Kleikamp 	if (!ret)
4497ac27a0ecSDave Kleikamp 		ret = err;
4498ac27a0ecSDave Kleikamp 	return ret;
4499ac27a0ecSDave Kleikamp }
4500ac27a0ecSDave Kleikamp 
4501617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
4502ac27a0ecSDave Kleikamp {
4503ac27a0ecSDave Kleikamp 	int ret, err;
4504ac27a0ecSDave Kleikamp 	handle_t *handle;
4505ac27a0ecSDave Kleikamp 
4506617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
4507617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
45089c3013e9SJan Kara 	if (IS_ERR(handle)) {
45099c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
45109c3013e9SJan Kara 		dquot_release(dquot);
4511ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
45129c3013e9SJan Kara 	}
4513ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
4514617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4515ac27a0ecSDave Kleikamp 	if (!ret)
4516ac27a0ecSDave Kleikamp 		ret = err;
4517ac27a0ecSDave Kleikamp 	return ret;
4518ac27a0ecSDave Kleikamp }
4519ac27a0ecSDave Kleikamp 
4520617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
4521ac27a0ecSDave Kleikamp {
45222c8be6b2SJan Kara 	/* Are we journaling quotas? */
4523617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
4524617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
4525ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
4526617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
4527ac27a0ecSDave Kleikamp 	} else {
4528ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
4529ac27a0ecSDave Kleikamp 	}
4530ac27a0ecSDave Kleikamp }
4531ac27a0ecSDave Kleikamp 
4532617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
4533ac27a0ecSDave Kleikamp {
4534ac27a0ecSDave Kleikamp 	int ret, err;
4535ac27a0ecSDave Kleikamp 	handle_t *handle;
4536ac27a0ecSDave Kleikamp 
4537ac27a0ecSDave Kleikamp 	/* Data block + inode block */
4538617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
4539ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4540ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4541ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
4542617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
4543ac27a0ecSDave Kleikamp 	if (!ret)
4544ac27a0ecSDave Kleikamp 		ret = err;
4545ac27a0ecSDave Kleikamp 	return ret;
4546ac27a0ecSDave Kleikamp }
4547ac27a0ecSDave Kleikamp 
4548ac27a0ecSDave Kleikamp /*
4549ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
4550ac27a0ecSDave Kleikamp  * the quota file and such...
4551ac27a0ecSDave Kleikamp  */
4552617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
4553ac27a0ecSDave Kleikamp {
4554287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
4555617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
4556ac27a0ecSDave Kleikamp }
4557ac27a0ecSDave Kleikamp 
4558ac27a0ecSDave Kleikamp /*
4559ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
4560ac27a0ecSDave Kleikamp  */
4561617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
4562f00c9e44SJan Kara 			 struct path *path)
4563ac27a0ecSDave Kleikamp {
4564ac27a0ecSDave Kleikamp 	int err;
4565ac27a0ecSDave Kleikamp 
4566ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
4567ac27a0ecSDave Kleikamp 		return -EINVAL;
45680623543bSJan Kara 
4569ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
4570f00c9e44SJan Kara 	if (path->mnt->mnt_sb != sb)
4571ac27a0ecSDave Kleikamp 		return -EXDEV;
45720623543bSJan Kara 	/* Journaling quota? */
45730623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
45742b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
4575f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
4576b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
4577b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
4578b31e1552SEric Sandeen 				"Journaled quota will not work");
45790623543bSJan Kara 	}
45800623543bSJan Kara 
45810623543bSJan Kara 	/*
45820623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
45830623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
45840623543bSJan Kara 	 */
45850390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
4586f00c9e44SJan Kara 	    ext4_should_journal_data(path->dentry->d_inode)) {
45870623543bSJan Kara 		/*
45880623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
45890623543bSJan Kara 		 * otherwise be livelocked...
45900623543bSJan Kara 		 */
45910623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
45927ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
45930623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
4594f00c9e44SJan Kara 		if (err)
45957ffe1ea8SHidehiro Kawai 			return err;
45967ffe1ea8SHidehiro Kawai 	}
45970623543bSJan Kara 
4598f00c9e44SJan Kara 	return dquot_quota_on(sb, type, format_id, path);
4599ac27a0ecSDave Kleikamp }
4600ac27a0ecSDave Kleikamp 
4601ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
4602ca0e05e4SDmitry Monakhov {
460387009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
460487009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
460587009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
4606ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
4607ca0e05e4SDmitry Monakhov 
4608ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
4609ca0e05e4SDmitry Monakhov }
4610ca0e05e4SDmitry Monakhov 
4611ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
4612ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
4613ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
4614ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
4615617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
4616ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
4617ac27a0ecSDave Kleikamp {
4618ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
4619725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4620ac27a0ecSDave Kleikamp 	int err = 0;
4621ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
4622ac27a0ecSDave Kleikamp 	int tocopy;
4623ac27a0ecSDave Kleikamp 	size_t toread;
4624ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4625ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
4626ac27a0ecSDave Kleikamp 
4627ac27a0ecSDave Kleikamp 	if (off > i_size)
4628ac27a0ecSDave Kleikamp 		return 0;
4629ac27a0ecSDave Kleikamp 	if (off+len > i_size)
4630ac27a0ecSDave Kleikamp 		len = i_size-off;
4631ac27a0ecSDave Kleikamp 	toread = len;
4632ac27a0ecSDave Kleikamp 	while (toread > 0) {
4633ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
4634ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
4635617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
4636ac27a0ecSDave Kleikamp 		if (err)
4637ac27a0ecSDave Kleikamp 			return err;
4638ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
4639ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
4640ac27a0ecSDave Kleikamp 		else
4641ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
4642ac27a0ecSDave Kleikamp 		brelse(bh);
4643ac27a0ecSDave Kleikamp 		offset = 0;
4644ac27a0ecSDave Kleikamp 		toread -= tocopy;
4645ac27a0ecSDave Kleikamp 		data += tocopy;
4646ac27a0ecSDave Kleikamp 		blk++;
4647ac27a0ecSDave Kleikamp 	}
4648ac27a0ecSDave Kleikamp 	return len;
4649ac27a0ecSDave Kleikamp }
4650ac27a0ecSDave Kleikamp 
4651ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
4652ac27a0ecSDave Kleikamp  * enough credits) */
4653617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
4654ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
4655ac27a0ecSDave Kleikamp {
4656ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
4657725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4658ac27a0ecSDave Kleikamp 	int err = 0;
4659ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
4660ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4661ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
4662ac27a0ecSDave Kleikamp 
46630390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
4664b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4665b31e1552SEric Sandeen 			" cancelled because transaction is not started",
46669c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
46679c3013e9SJan Kara 		return -EIO;
46689c3013e9SJan Kara 	}
466967eeb568SDmitry Monakhov 	/*
467067eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
467167eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
467267eeb568SDmitry Monakhov 	 */
467367eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
467467eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
467567eeb568SDmitry Monakhov 			" cancelled because not block aligned",
467667eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
467767eeb568SDmitry Monakhov 		return -EIO;
467867eeb568SDmitry Monakhov 	}
467967eeb568SDmitry Monakhov 
4680ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
4681617ba13bSMingming Cao 	bh = ext4_bread(handle, inode, blk, 1, &err);
4682ac27a0ecSDave Kleikamp 	if (!bh)
4683ac27a0ecSDave Kleikamp 		goto out;
4684617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
4685ac27a0ecSDave Kleikamp 	if (err) {
4686ac27a0ecSDave Kleikamp 		brelse(bh);
4687ac27a0ecSDave Kleikamp 		goto out;
4688ac27a0ecSDave Kleikamp 	}
4689ac27a0ecSDave Kleikamp 	lock_buffer(bh);
469067eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
4691ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
4692ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
46930390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
4694ac27a0ecSDave Kleikamp 	brelse(bh);
4695ac27a0ecSDave Kleikamp out:
469667eeb568SDmitry Monakhov 	if (err) {
46974d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
4698ac27a0ecSDave Kleikamp 		return err;
46994d04e4fbSJan Kara 	}
470067eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
470167eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
4702617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
4703ac27a0ecSDave Kleikamp 	}
4704ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
4705617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
4706ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
470767eeb568SDmitry Monakhov 	return len;
4708ac27a0ecSDave Kleikamp }
4709ac27a0ecSDave Kleikamp 
4710ac27a0ecSDave Kleikamp #endif
4711ac27a0ecSDave Kleikamp 
4712152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
4713152a0836SAl Viro 		       const char *dev_name, void *data)
4714ac27a0ecSDave Kleikamp {
4715152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
4716ac27a0ecSDave Kleikamp }
4717ac27a0ecSDave Kleikamp 
471837f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
471924b58424STheodore Ts'o static struct file_system_type ext2_fs_type = {
472024b58424STheodore Ts'o 	.owner		= THIS_MODULE,
472124b58424STheodore Ts'o 	.name		= "ext2",
4722152a0836SAl Viro 	.mount		= ext4_mount,
472324b58424STheodore Ts'o 	.kill_sb	= kill_block_super,
472424b58424STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
472524b58424STheodore Ts'o };
472624b58424STheodore Ts'o 
472724b58424STheodore Ts'o static inline void register_as_ext2(void)
472824b58424STheodore Ts'o {
472924b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
473024b58424STheodore Ts'o 	if (err)
473124b58424STheodore Ts'o 		printk(KERN_WARNING
473224b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
473324b58424STheodore Ts'o }
473424b58424STheodore Ts'o 
473524b58424STheodore Ts'o static inline void unregister_as_ext2(void)
473624b58424STheodore Ts'o {
473724b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
473824b58424STheodore Ts'o }
473951b7e3c9STheodore Ts'o MODULE_ALIAS("ext2");
474024b58424STheodore Ts'o #else
474124b58424STheodore Ts'o static inline void register_as_ext2(void) { }
474224b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
474324b58424STheodore Ts'o #endif
474424b58424STheodore Ts'o 
474537f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
474624b58424STheodore Ts'o static inline void register_as_ext3(void)
474724b58424STheodore Ts'o {
474824b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
474924b58424STheodore Ts'o 	if (err)
475024b58424STheodore Ts'o 		printk(KERN_WARNING
475124b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
475224b58424STheodore Ts'o }
475324b58424STheodore Ts'o 
475424b58424STheodore Ts'o static inline void unregister_as_ext3(void)
475524b58424STheodore Ts'o {
475624b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
475724b58424STheodore Ts'o }
475851b7e3c9STheodore Ts'o MODULE_ALIAS("ext3");
475924b58424STheodore Ts'o #else
476024b58424STheodore Ts'o static inline void register_as_ext3(void) { }
476124b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
476224b58424STheodore Ts'o #endif
476324b58424STheodore Ts'o 
476403010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
4765ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
476603010a33STheodore Ts'o 	.name		= "ext4",
4767152a0836SAl Viro 	.mount		= ext4_mount,
4768ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
4769ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
4770ac27a0ecSDave Kleikamp };
4771ac27a0ecSDave Kleikamp 
4772*8f021222SLukas Czerner static int __init ext4_init_feat_adverts(void)
4773857ac889SLukas Czerner {
4774857ac889SLukas Czerner 	struct ext4_features *ef;
4775857ac889SLukas Czerner 	int ret = -ENOMEM;
4776857ac889SLukas Czerner 
4777857ac889SLukas Czerner 	ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
4778857ac889SLukas Czerner 	if (!ef)
4779857ac889SLukas Czerner 		goto out;
4780857ac889SLukas Czerner 
4781857ac889SLukas Czerner 	ef->f_kobj.kset = ext4_kset;
4782857ac889SLukas Czerner 	init_completion(&ef->f_kobj_unregister);
4783857ac889SLukas Czerner 	ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
4784857ac889SLukas Czerner 				   "features");
4785857ac889SLukas Czerner 	if (ret) {
4786857ac889SLukas Czerner 		kfree(ef);
4787857ac889SLukas Czerner 		goto out;
4788857ac889SLukas Czerner 	}
4789857ac889SLukas Czerner 
4790857ac889SLukas Czerner 	ext4_feat = ef;
4791857ac889SLukas Czerner 	ret = 0;
4792857ac889SLukas Czerner out:
4793857ac889SLukas Czerner 	return ret;
4794857ac889SLukas Czerner }
4795857ac889SLukas Czerner 
4796*8f021222SLukas Czerner static void ext4_exit_feat_adverts(void)
4797*8f021222SLukas Czerner {
4798*8f021222SLukas Czerner 	kobject_put(&ext4_feat->f_kobj);
4799*8f021222SLukas Czerner 	wait_for_completion(&ext4_feat->f_kobj_unregister);
4800*8f021222SLukas Czerner 	kfree(ext4_feat);
4801*8f021222SLukas Czerner }
4802*8f021222SLukas Czerner 
48035dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
4804ac27a0ecSDave Kleikamp {
4805c9de560dSAlex Tomas 	int err;
4806c9de560dSAlex Tomas 
480712e9b892SDmitry Monakhov 	ext4_check_flag_values();
48085dabfc78STheodore Ts'o 	err = ext4_init_pageio();
48096fd058f7STheodore Ts'o 	if (err)
48106fd058f7STheodore Ts'o 		return err;
48115dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
4812bd2d0210STheodore Ts'o 	if (err)
4813bd2d0210STheodore Ts'o 		goto out5;
48143197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
48153197ebdbSTheodore Ts'o 	if (!ext4_kset)
48166fd058f7STheodore Ts'o 		goto out4;
48179f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
4818857ac889SLukas Czerner 
4819857ac889SLukas Czerner 	err = ext4_init_feat_adverts();
4820857ac889SLukas Czerner 
48215dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
4822ac27a0ecSDave Kleikamp 	if (err)
48236fd058f7STheodore Ts'o 		goto out3;
4824c9de560dSAlex Tomas 
48255dabfc78STheodore Ts'o 	err = ext4_init_xattr();
4826c9de560dSAlex Tomas 	if (err)
4827c9de560dSAlex Tomas 		goto out2;
4828ac27a0ecSDave Kleikamp 	err = init_inodecache();
4829ac27a0ecSDave Kleikamp 	if (err)
4830ac27a0ecSDave Kleikamp 		goto out1;
483124b58424STheodore Ts'o 	register_as_ext2();
483224b58424STheodore Ts'o 	register_as_ext3();
483303010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
4834ac27a0ecSDave Kleikamp 	if (err)
4835ac27a0ecSDave Kleikamp 		goto out;
4836bfff6873SLukas Czerner 
4837bfff6873SLukas Czerner 	ext4_li_info = NULL;
4838bfff6873SLukas Czerner 	mutex_init(&ext4_li_mtx);
4839ac27a0ecSDave Kleikamp 	return 0;
4840ac27a0ecSDave Kleikamp out:
484124b58424STheodore Ts'o 	unregister_as_ext2();
484224b58424STheodore Ts'o 	unregister_as_ext3();
4843ac27a0ecSDave Kleikamp 	destroy_inodecache();
4844ac27a0ecSDave Kleikamp out1:
48455dabfc78STheodore Ts'o 	ext4_exit_xattr();
4846c9de560dSAlex Tomas out2:
48475dabfc78STheodore Ts'o 	ext4_exit_mballoc();
48486fd058f7STheodore Ts'o out3:
4849*8f021222SLukas Czerner 	ext4_exit_feat_adverts();
48506fd058f7STheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
48516fd058f7STheodore Ts'o 	kset_unregister(ext4_kset);
48526fd058f7STheodore Ts'o out4:
48535dabfc78STheodore Ts'o 	ext4_exit_system_zone();
4854bd2d0210STheodore Ts'o out5:
48555dabfc78STheodore Ts'o 	ext4_exit_pageio();
4856ac27a0ecSDave Kleikamp 	return err;
4857ac27a0ecSDave Kleikamp }
4858ac27a0ecSDave Kleikamp 
48595dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
4860ac27a0ecSDave Kleikamp {
4861bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
486224b58424STheodore Ts'o 	unregister_as_ext2();
486324b58424STheodore Ts'o 	unregister_as_ext3();
486403010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
4865ac27a0ecSDave Kleikamp 	destroy_inodecache();
48665dabfc78STheodore Ts'o 	ext4_exit_xattr();
48675dabfc78STheodore Ts'o 	ext4_exit_mballoc();
4868*8f021222SLukas Czerner 	ext4_exit_feat_adverts();
48699f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
48703197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
48715dabfc78STheodore Ts'o 	ext4_exit_system_zone();
48725dabfc78STheodore Ts'o 	ext4_exit_pageio();
4873ac27a0ecSDave Kleikamp }
4874ac27a0ecSDave Kleikamp 
4875ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
487683982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
4877ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
48785dabfc78STheodore Ts'o module_init(ext4_init_fs)
48795dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
4880