xref: /linux/fs/ext4/super.c (revision 4ab2f15b7f709c3626a7eed075a7225b4c775c7e)
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/smp_lock.h>
30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
31a5694255SChristoph Hellwig #include <linux/exportfs.h>
32ac27a0ecSDave Kleikamp #include <linux/vfs.h>
33ac27a0ecSDave Kleikamp #include <linux/random.h>
34ac27a0ecSDave Kleikamp #include <linux/mount.h>
35ac27a0ecSDave Kleikamp #include <linux/namei.h>
36ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
37ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
389f6200bbSTheodore Ts'o #include <linux/proc_fs.h>
393197ebdbSTheodore Ts'o #include <linux/ctype.h>
401330593eSVignesh Babu #include <linux/log2.h>
41717d50e4SAndreas Dilger #include <linux/crc16.h>
42ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
43ac27a0ecSDave Kleikamp 
443dcf5451SChristoph Hellwig #include "ext4.h"
453dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
46ac27a0ecSDave Kleikamp #include "xattr.h"
47ac27a0ecSDave Kleikamp #include "acl.h"
48ac27a0ecSDave Kleikamp 
499bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
509bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
519bffad1eSTheodore Ts'o 
52f4033903SCurt Wohlgemuth static int default_mb_history_length = 1000;
53f4033903SCurt Wohlgemuth 
54f4033903SCurt Wohlgemuth module_param_named(default_mb_history_length, default_mb_history_length,
55f4033903SCurt Wohlgemuth 		   int, 0644);
56f4033903SCurt Wohlgemuth MODULE_PARM_DESC(default_mb_history_length,
57f4033903SCurt Wohlgemuth 		 "Default number of entries saved for mb_history");
58f4033903SCurt Wohlgemuth 
599f6200bbSTheodore Ts'o struct proc_dir_entry *ext4_proc_root;
603197ebdbSTheodore Ts'o static struct kset *ext4_kset;
619f6200bbSTheodore Ts'o 
62617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
63ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
64e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
65617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
66617ba13bSMingming Cao 					struct ext4_super_block *es);
67617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
68617ba13bSMingming Cao 				   struct ext4_super_block *es);
69617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
70617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
71ac27a0ecSDave Kleikamp 				     char nbuf[16]);
72617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
73617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
74c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
75617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
76c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
77ac27a0ecSDave Kleikamp 
78bd81d8eeSLaurent Vivier 
798fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
808fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
81bd81d8eeSLaurent Vivier {
823a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
838fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
848fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
85bd81d8eeSLaurent Vivier }
86bd81d8eeSLaurent Vivier 
878fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
888fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
89bd81d8eeSLaurent Vivier {
905272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
918fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
928fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
93bd81d8eeSLaurent Vivier }
94bd81d8eeSLaurent Vivier 
958fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
968fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
97bd81d8eeSLaurent Vivier {
985272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
998fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1008fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
101bd81d8eeSLaurent Vivier }
102bd81d8eeSLaurent Vivier 
103560671a0SAneesh Kumar K.V __u32 ext4_free_blks_count(struct super_block *sb,
104560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
105560671a0SAneesh Kumar K.V {
106560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
107560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
108560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
109560671a0SAneesh Kumar K.V }
110560671a0SAneesh Kumar K.V 
111560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
112560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
113560671a0SAneesh Kumar K.V {
114560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
115560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
116560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
117560671a0SAneesh Kumar K.V }
118560671a0SAneesh Kumar K.V 
119560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_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_used_dirs_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_used_dirs_count_hi) << 16 : 0);
125560671a0SAneesh Kumar K.V }
126560671a0SAneesh Kumar K.V 
127560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_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_itable_unused_lo) |
131560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
132560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
133560671a0SAneesh Kumar K.V }
134560671a0SAneesh Kumar K.V 
1358fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
1368fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
137bd81d8eeSLaurent Vivier {
1383a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1398fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1408fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
141bd81d8eeSLaurent Vivier }
142bd81d8eeSLaurent Vivier 
1438fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1448fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
145bd81d8eeSLaurent Vivier {
1465272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1478fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1488fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
149bd81d8eeSLaurent Vivier }
150bd81d8eeSLaurent Vivier 
1518fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1528fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
153bd81d8eeSLaurent Vivier {
1545272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1558fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1568fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
157bd81d8eeSLaurent Vivier }
158bd81d8eeSLaurent Vivier 
159560671a0SAneesh Kumar K.V void ext4_free_blks_set(struct super_block *sb,
160560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
161560671a0SAneesh Kumar K.V {
162560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
163560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
164560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
165560671a0SAneesh Kumar K.V }
166560671a0SAneesh Kumar K.V 
167560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
168560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
169560671a0SAneesh Kumar K.V {
170560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
171560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
172560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
173560671a0SAneesh Kumar K.V }
174560671a0SAneesh Kumar K.V 
175560671a0SAneesh Kumar K.V void ext4_used_dirs_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_used_dirs_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_used_dirs_count_hi = cpu_to_le16(count >> 16);
181560671a0SAneesh Kumar K.V }
182560671a0SAneesh Kumar K.V 
183560671a0SAneesh Kumar K.V void ext4_itable_unused_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_itable_unused_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_itable_unused_hi = cpu_to_le16(count >> 16);
189560671a0SAneesh Kumar K.V }
190560671a0SAneesh Kumar K.V 
191ac27a0ecSDave Kleikamp /*
192dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
193ac27a0ecSDave Kleikamp  *
194ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
195ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
196ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
197ac27a0ecSDave Kleikamp  * appropriate.
198ac27a0ecSDave Kleikamp  */
199617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
200ac27a0ecSDave Kleikamp {
201ac27a0ecSDave Kleikamp 	journal_t *journal;
202ac27a0ecSDave Kleikamp 
203ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
204ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
205ac27a0ecSDave Kleikamp 
206ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
207ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
208ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
209617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2100390131bSFrank Mayhar 	if (journal) {
211ac27a0ecSDave Kleikamp 		if (is_journal_aborted(journal)) {
2120b8e58a1SAndreas Dilger 			ext4_abort(sb, __func__, "Detected aborted journal");
213ac27a0ecSDave Kleikamp 			return ERR_PTR(-EROFS);
214ac27a0ecSDave Kleikamp 		}
215dab291afSMingming Cao 		return jbd2_journal_start(journal, nblocks);
216ac27a0ecSDave Kleikamp 	}
2170390131bSFrank Mayhar 	/*
2180390131bSFrank Mayhar 	 * We're not journaling, return the appropriate indication.
2190390131bSFrank Mayhar 	 */
2200390131bSFrank Mayhar 	current->journal_info = EXT4_NOJOURNAL_HANDLE;
2210390131bSFrank Mayhar 	return current->journal_info;
2220390131bSFrank Mayhar }
223ac27a0ecSDave Kleikamp 
224ac27a0ecSDave Kleikamp /*
225ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
226dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
227ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
228ac27a0ecSDave Kleikamp  * appropriate.
229ac27a0ecSDave Kleikamp  */
230617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
231ac27a0ecSDave Kleikamp {
232ac27a0ecSDave Kleikamp 	struct super_block *sb;
233ac27a0ecSDave Kleikamp 	int err;
234ac27a0ecSDave Kleikamp 	int rc;
235ac27a0ecSDave Kleikamp 
2360390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
2370390131bSFrank Mayhar 		/*
2380390131bSFrank Mayhar 		 * Do this here since we don't call jbd2_journal_stop() in
2390390131bSFrank Mayhar 		 * no-journal mode.
2400390131bSFrank Mayhar 		 */
2410390131bSFrank Mayhar 		current->journal_info = NULL;
2420390131bSFrank Mayhar 		return 0;
2430390131bSFrank Mayhar 	}
244ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
245ac27a0ecSDave Kleikamp 	err = handle->h_err;
246dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
247ac27a0ecSDave Kleikamp 
248ac27a0ecSDave Kleikamp 	if (!err)
249ac27a0ecSDave Kleikamp 		err = rc;
250ac27a0ecSDave Kleikamp 	if (err)
251617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
252ac27a0ecSDave Kleikamp 	return err;
253ac27a0ecSDave Kleikamp }
254ac27a0ecSDave Kleikamp 
255617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
256ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
257ac27a0ecSDave Kleikamp {
258ac27a0ecSDave Kleikamp 	char nbuf[16];
259617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
260ac27a0ecSDave Kleikamp 
2610390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
2620390131bSFrank Mayhar 
263ac27a0ecSDave Kleikamp 	if (bh)
264ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
265ac27a0ecSDave Kleikamp 
266ac27a0ecSDave Kleikamp 	if (!handle->h_err)
267ac27a0ecSDave Kleikamp 		handle->h_err = err;
268ac27a0ecSDave Kleikamp 
269ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
270ac27a0ecSDave Kleikamp 		return;
271ac27a0ecSDave Kleikamp 
272ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
273ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
274ac27a0ecSDave Kleikamp 
275dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
276ac27a0ecSDave Kleikamp }
277ac27a0ecSDave Kleikamp 
278ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
279ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
280ac27a0ecSDave Kleikamp  *
281ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
282617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
283ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
284ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
285ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
286ac27a0ecSDave Kleikamp  * write out the superblock safely.
287ac27a0ecSDave Kleikamp  *
288dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
289ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
290ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
291ac27a0ecSDave Kleikamp  */
292ac27a0ecSDave Kleikamp 
293617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
294ac27a0ecSDave Kleikamp {
295617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
296ac27a0ecSDave Kleikamp 
297617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
298617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
299ac27a0ecSDave Kleikamp 
300ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
301ac27a0ecSDave Kleikamp 		return;
302ac27a0ecSDave Kleikamp 
303ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
304617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
305ac27a0ecSDave Kleikamp 
306*4ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
307ac27a0ecSDave Kleikamp 		if (journal)
308dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
309ac27a0ecSDave Kleikamp 	}
310ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
311b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
312ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
313ac27a0ecSDave Kleikamp 	}
314e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
315ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
316617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
317ac27a0ecSDave Kleikamp 			sb->s_id);
318ac27a0ecSDave Kleikamp }
319ac27a0ecSDave Kleikamp 
320617ba13bSMingming Cao void ext4_error(struct super_block *sb, const char *function,
321ac27a0ecSDave Kleikamp 		const char *fmt, ...)
322ac27a0ecSDave Kleikamp {
323ac27a0ecSDave Kleikamp 	va_list args;
324ac27a0ecSDave Kleikamp 
325ac27a0ecSDave Kleikamp 	va_start(args, fmt);
326617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
327ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
328ac27a0ecSDave Kleikamp 	printk("\n");
329ac27a0ecSDave Kleikamp 	va_end(args);
330ac27a0ecSDave Kleikamp 
331617ba13bSMingming Cao 	ext4_handle_error(sb);
332ac27a0ecSDave Kleikamp }
333ac27a0ecSDave Kleikamp 
334617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
335ac27a0ecSDave Kleikamp 				     char nbuf[16])
336ac27a0ecSDave Kleikamp {
337ac27a0ecSDave Kleikamp 	char *errstr = NULL;
338ac27a0ecSDave Kleikamp 
339ac27a0ecSDave Kleikamp 	switch (errno) {
340ac27a0ecSDave Kleikamp 	case -EIO:
341ac27a0ecSDave Kleikamp 		errstr = "IO failure";
342ac27a0ecSDave Kleikamp 		break;
343ac27a0ecSDave Kleikamp 	case -ENOMEM:
344ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
345ac27a0ecSDave Kleikamp 		break;
346ac27a0ecSDave Kleikamp 	case -EROFS:
347dab291afSMingming Cao 		if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
348ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
349ac27a0ecSDave Kleikamp 		else
350ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
351ac27a0ecSDave Kleikamp 		break;
352ac27a0ecSDave Kleikamp 	default:
353ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
354ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
355ac27a0ecSDave Kleikamp 		 * NULL. */
356ac27a0ecSDave Kleikamp 		if (nbuf) {
357ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
358ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
359ac27a0ecSDave Kleikamp 				errstr = nbuf;
360ac27a0ecSDave Kleikamp 		}
361ac27a0ecSDave Kleikamp 		break;
362ac27a0ecSDave Kleikamp 	}
363ac27a0ecSDave Kleikamp 
364ac27a0ecSDave Kleikamp 	return errstr;
365ac27a0ecSDave Kleikamp }
366ac27a0ecSDave Kleikamp 
367617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
368ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
369ac27a0ecSDave Kleikamp 
3702b2d6d01STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, int errno)
371ac27a0ecSDave Kleikamp {
372ac27a0ecSDave Kleikamp 	char nbuf[16];
373ac27a0ecSDave Kleikamp 	const char *errstr;
374ac27a0ecSDave Kleikamp 
375ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
376ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
377ac27a0ecSDave Kleikamp 	 * an error. */
378ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
379ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
380ac27a0ecSDave Kleikamp 		return;
381ac27a0ecSDave Kleikamp 
382617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
383617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
384ac27a0ecSDave Kleikamp 	       sb->s_id, function, errstr);
385ac27a0ecSDave Kleikamp 
386617ba13bSMingming Cao 	ext4_handle_error(sb);
387ac27a0ecSDave Kleikamp }
388ac27a0ecSDave Kleikamp 
389ac27a0ecSDave Kleikamp /*
390617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
391ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
392ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
393ac27a0ecSDave Kleikamp  *
394ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
395ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
396ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
397ac27a0ecSDave Kleikamp  */
398ac27a0ecSDave Kleikamp 
399617ba13bSMingming Cao void ext4_abort(struct super_block *sb, const char *function,
400ac27a0ecSDave Kleikamp 		const char *fmt, ...)
401ac27a0ecSDave Kleikamp {
402ac27a0ecSDave Kleikamp 	va_list args;
403ac27a0ecSDave Kleikamp 
404ac27a0ecSDave Kleikamp 	va_start(args, fmt);
405617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
406ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
407ac27a0ecSDave Kleikamp 	printk("\n");
408ac27a0ecSDave Kleikamp 	va_end(args);
409ac27a0ecSDave Kleikamp 
410ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
411617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
412ac27a0ecSDave Kleikamp 
413ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
414ac27a0ecSDave Kleikamp 		return;
415ac27a0ecSDave Kleikamp 
416b31e1552SEric Sandeen 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
417617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
418ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
419*4ab2f15bSTheodore Ts'o 	EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
420ef2cabf7SHidehiro Kawai 	if (EXT4_SB(sb)->s_journal)
421dab291afSMingming Cao 		jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
422ac27a0ecSDave Kleikamp }
423ac27a0ecSDave Kleikamp 
424b31e1552SEric Sandeen void ext4_msg (struct super_block * sb, const char *prefix,
425b31e1552SEric Sandeen 		   const char *fmt, ...)
426b31e1552SEric Sandeen {
427b31e1552SEric Sandeen 	va_list args;
428b31e1552SEric Sandeen 
429b31e1552SEric Sandeen 	va_start(args, fmt);
430b31e1552SEric Sandeen 	printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
431b31e1552SEric Sandeen 	vprintk(fmt, args);
432b31e1552SEric Sandeen 	printk("\n");
433b31e1552SEric Sandeen 	va_end(args);
434b31e1552SEric Sandeen }
435b31e1552SEric Sandeen 
436617ba13bSMingming Cao void ext4_warning(struct super_block *sb, const char *function,
437ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
438ac27a0ecSDave Kleikamp {
439ac27a0ecSDave Kleikamp 	va_list args;
440ac27a0ecSDave Kleikamp 
441ac27a0ecSDave Kleikamp 	va_start(args, fmt);
442617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
443ac27a0ecSDave Kleikamp 	       sb->s_id, function);
444ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
445ac27a0ecSDave Kleikamp 	printk("\n");
446ac27a0ecSDave Kleikamp 	va_end(args);
447ac27a0ecSDave Kleikamp }
448ac27a0ecSDave Kleikamp 
4495d1b1b3fSAneesh Kumar K.V void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
4505d1b1b3fSAneesh Kumar K.V 			   const char *function, const char *fmt, ...)
4515d1b1b3fSAneesh Kumar K.V __releases(bitlock)
4525d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
4535d1b1b3fSAneesh Kumar K.V {
4545d1b1b3fSAneesh Kumar K.V 	va_list args;
4555d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4565d1b1b3fSAneesh Kumar K.V 
4575d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
4585d1b1b3fSAneesh Kumar K.V 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
4595d1b1b3fSAneesh Kumar K.V 	vprintk(fmt, args);
4605d1b1b3fSAneesh Kumar K.V 	printk("\n");
4615d1b1b3fSAneesh Kumar K.V 	va_end(args);
4625d1b1b3fSAneesh Kumar K.V 
4635d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
4645d1b1b3fSAneesh Kumar K.V 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4655d1b1b3fSAneesh Kumar K.V 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
466e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
4675d1b1b3fSAneesh Kumar K.V 		return;
4685d1b1b3fSAneesh Kumar K.V 	}
4695d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
4705d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
4715d1b1b3fSAneesh Kumar K.V 	/*
4725d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
4735d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
4745d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
4755d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
4765d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
4775d1b1b3fSAneesh Kumar K.V 	 * ext4_grp_locked_error() to distinguish beween the
4785d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
4795d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
4805d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
4815d1b1b3fSAneesh Kumar K.V 	 */
4825d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
4835d1b1b3fSAneesh Kumar K.V 	return;
4845d1b1b3fSAneesh Kumar K.V }
4855d1b1b3fSAneesh Kumar K.V 
486617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
487ac27a0ecSDave Kleikamp {
488617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
489ac27a0ecSDave Kleikamp 
490617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
491ac27a0ecSDave Kleikamp 		return;
492ac27a0ecSDave Kleikamp 
49346e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
494ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
495ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
496617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
497ac27a0ecSDave Kleikamp 
498617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
499617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
500617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
501ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
502ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
503ac27a0ecSDave Kleikamp 
504ac27a0ecSDave Kleikamp 	/*
505ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
506ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
507ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
508ac27a0ecSDave Kleikamp 	 */
509ac27a0ecSDave Kleikamp }
510ac27a0ecSDave Kleikamp 
511ac27a0ecSDave Kleikamp /*
512ac27a0ecSDave Kleikamp  * Open the external journal device
513ac27a0ecSDave Kleikamp  */
514b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
515ac27a0ecSDave Kleikamp {
516ac27a0ecSDave Kleikamp 	struct block_device *bdev;
517ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
518ac27a0ecSDave Kleikamp 
519ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
520ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
521ac27a0ecSDave Kleikamp 		goto fail;
522ac27a0ecSDave Kleikamp 	return bdev;
523ac27a0ecSDave Kleikamp 
524ac27a0ecSDave Kleikamp fail:
525b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
526ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
527ac27a0ecSDave Kleikamp 	return NULL;
528ac27a0ecSDave Kleikamp }
529ac27a0ecSDave Kleikamp 
530ac27a0ecSDave Kleikamp /*
531ac27a0ecSDave Kleikamp  * Release the journal device
532ac27a0ecSDave Kleikamp  */
533617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
534ac27a0ecSDave Kleikamp {
535ac27a0ecSDave Kleikamp 	bd_release(bdev);
5369a1c3542SAl Viro 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
537ac27a0ecSDave Kleikamp }
538ac27a0ecSDave Kleikamp 
539617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
540ac27a0ecSDave Kleikamp {
541ac27a0ecSDave Kleikamp 	struct block_device *bdev;
542ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
543ac27a0ecSDave Kleikamp 
544ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
545ac27a0ecSDave Kleikamp 	if (bdev) {
546617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
547ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
548ac27a0ecSDave Kleikamp 	}
549ac27a0ecSDave Kleikamp 	return ret;
550ac27a0ecSDave Kleikamp }
551ac27a0ecSDave Kleikamp 
552ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
553ac27a0ecSDave Kleikamp {
554617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
555ac27a0ecSDave Kleikamp }
556ac27a0ecSDave Kleikamp 
557617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
558ac27a0ecSDave Kleikamp {
559ac27a0ecSDave Kleikamp 	struct list_head *l;
560ac27a0ecSDave Kleikamp 
561b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
562ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
563ac27a0ecSDave Kleikamp 
564ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
565ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
566ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
567ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
568ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
569ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
570ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
571ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
572ac27a0ecSDave Kleikamp 	}
573ac27a0ecSDave Kleikamp }
574ac27a0ecSDave Kleikamp 
575617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
576ac27a0ecSDave Kleikamp {
577617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
578617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
579ef2cabf7SHidehiro Kawai 	int i, err;
580ac27a0ecSDave Kleikamp 
581a9e220f8SAl Viro 	lock_super(sb);
5826cfd0148SChristoph Hellwig 	lock_kernel();
5838c85e125SChristoph Hellwig 	if (sb->s_dirt)
584ebc1ac16SChristoph Hellwig 		ext4_commit_super(sb, 1);
5858c85e125SChristoph Hellwig 
5866fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
587c9de560dSAlex Tomas 	ext4_mb_release(sb);
588a86c6181SAlex Tomas 	ext4_ext_release(sb);
589617ba13bSMingming Cao 	ext4_xattr_put_super(sb);
5900390131bSFrank Mayhar 	if (sbi->s_journal) {
591ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
59247b4a50bSJan Kara 		sbi->s_journal = NULL;
593ef2cabf7SHidehiro Kawai 		if (err < 0)
5940390131bSFrank Mayhar 			ext4_abort(sb, __func__,
5950390131bSFrank Mayhar 				   "Couldn't clean up the journal");
5960390131bSFrank Mayhar 	}
597ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
598617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
599ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
600e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
601ac27a0ecSDave Kleikamp 	}
602240799cdSTheodore Ts'o 	if (sbi->s_proc) {
6039f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
604240799cdSTheodore Ts'o 	}
6053197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
606ac27a0ecSDave Kleikamp 
607ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
608ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
609ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
610c5ca7c76STheodore Ts'o 	if (is_vmalloc_addr(sbi->s_flex_groups))
611c5ca7c76STheodore Ts'o 		vfree(sbi->s_flex_groups);
612c5ca7c76STheodore Ts'o 	else
613772cb7c8SJose R. Santos 		kfree(sbi->s_flex_groups);
614ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
615ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
616ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
6176bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
618ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
619ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
620ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
621ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
622ac27a0ecSDave Kleikamp #endif
623ac27a0ecSDave Kleikamp 
624ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
625ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
626ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
627ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
628ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
629ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
630ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
631ac27a0ecSDave Kleikamp 
632f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
633ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
634ac27a0ecSDave Kleikamp 		/*
635ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
636ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
637ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
638ac27a0ecSDave Kleikamp 		 */
639ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
640f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
641617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
642ac27a0ecSDave Kleikamp 	}
643ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
6443197ebdbSTheodore Ts'o 	/*
6453197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
6463197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
6473197ebdbSTheodore Ts'o 	 */
6483197ebdbSTheodore Ts'o 	unlock_kernel();
6493197ebdbSTheodore Ts'o 	unlock_super(sb);
6503197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
6513197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
652705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
653ac27a0ecSDave Kleikamp 	kfree(sbi);
654ac27a0ecSDave Kleikamp }
655ac27a0ecSDave Kleikamp 
656e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
657ac27a0ecSDave Kleikamp 
658ac27a0ecSDave Kleikamp /*
659ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
660ac27a0ecSDave Kleikamp  */
661617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
662ac27a0ecSDave Kleikamp {
663617ba13bSMingming Cao 	struct ext4_inode_info *ei;
664ac27a0ecSDave Kleikamp 
665e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
666ac27a0ecSDave Kleikamp 	if (!ei)
667ac27a0ecSDave Kleikamp 		return NULL;
6680b8e58a1SAndreas Dilger 
66903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
670617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
671617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
672ac27a0ecSDave Kleikamp #endif
673ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
67491246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
675a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
676c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
677c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
6780390131bSFrank Mayhar 	/*
6790390131bSFrank Mayhar 	 * Note:  We can be called before EXT4_SB(sb)->s_journal is set,
6800390131bSFrank Mayhar 	 * therefore it can be null here.  Don't check it, just initialize
6810390131bSFrank Mayhar 	 * jinode.
6820390131bSFrank Mayhar 	 */
683678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
684d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
685d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
686d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
687d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
688d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
6890b8e58a1SAndreas Dilger 
690ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
691ac27a0ecSDave Kleikamp }
692ac27a0ecSDave Kleikamp 
693617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
694ac27a0ecSDave Kleikamp {
6959f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
696b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
697b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
698b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
6999f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
7009f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
7019f7dd93dSVasily Averin 				true);
7029f7dd93dSVasily Averin 		dump_stack();
7039f7dd93dSVasily Averin 	}
704617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
705ac27a0ecSDave Kleikamp }
706ac27a0ecSDave Kleikamp 
70751cc5068SAlexey Dobriyan static void init_once(void *foo)
708ac27a0ecSDave Kleikamp {
709617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
710ac27a0ecSDave Kleikamp 
711ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
71203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
713ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
714ac27a0ecSDave Kleikamp #endif
7150e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
716ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
717ac27a0ecSDave Kleikamp }
718ac27a0ecSDave Kleikamp 
719ac27a0ecSDave Kleikamp static int init_inodecache(void)
720ac27a0ecSDave Kleikamp {
721617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
722617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
723ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
724ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
72520c2df83SPaul Mundt 					     init_once);
726617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
727ac27a0ecSDave Kleikamp 		return -ENOMEM;
728ac27a0ecSDave Kleikamp 	return 0;
729ac27a0ecSDave Kleikamp }
730ac27a0ecSDave Kleikamp 
731ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
732ac27a0ecSDave Kleikamp {
733617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
734ac27a0ecSDave Kleikamp }
735ac27a0ecSDave Kleikamp 
736617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
737ac27a0ecSDave Kleikamp {
73803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
739617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
740617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
741617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
742617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
743ac27a0ecSDave Kleikamp 	}
744617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
745617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
746617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
747617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
748ac27a0ecSDave Kleikamp 	}
749ac27a0ecSDave Kleikamp #endif
750c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
7510390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode))
752678aaf48SJan Kara 		jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
753678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
754ac27a0ecSDave Kleikamp }
755ac27a0ecSDave Kleikamp 
7562b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
7572b2d6d01STheodore Ts'o 					   struct super_block *sb)
758ac27a0ecSDave Kleikamp {
759ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
760617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
761ac27a0ecSDave Kleikamp 
762ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
763ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
764ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
765ac27a0ecSDave Kleikamp 
766ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
767ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
768ac27a0ecSDave Kleikamp 
769ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
770ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
771ac27a0ecSDave Kleikamp 
772617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
773ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
774ac27a0ecSDave Kleikamp 
775617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
776ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
777ac27a0ecSDave Kleikamp #endif
778ac27a0ecSDave Kleikamp }
779ac27a0ecSDave Kleikamp 
780d9c9bef1SMiklos Szeredi /*
781d9c9bef1SMiklos Szeredi  * Show an option if
782d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
783d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
784d9c9bef1SMiklos Szeredi  */
785617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
786ac27a0ecSDave Kleikamp {
787aa22df2cSAneesh Kumar K.V 	int def_errors;
788aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
789ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
790d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
791d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
792d9c9bef1SMiklos Szeredi 
793d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
794aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
795d9c9bef1SMiklos Szeredi 
796d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
797d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
798d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
799d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
800aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
801d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
802d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
803d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
804d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
805d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
806d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
807d9c9bef1SMiklos Szeredi 	}
808d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
809d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
810d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
811d9c9bef1SMiklos Szeredi 	}
812bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
813d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
814bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
815d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
816bb4f397aSAneesh Kumar K.V 		}
817bb4f397aSAneesh Kumar K.V 	}
818aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
819bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
820aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
821d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
822aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
823d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
824aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
825d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
826d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
827d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
82803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
829aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
830aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
831d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
832d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
833d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
834d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
835d9c9bef1SMiklos Szeredi 	}
836d9c9bef1SMiklos Szeredi #endif
83703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
838aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
839d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
840d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
841d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
842d9c9bef1SMiklos Szeredi #endif
84330773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
844d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
845d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
846d9c9bef1SMiklos Szeredi 	}
84730773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
84830773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
84930773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
85030773840STheodore Ts'o 	}
85130773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
85230773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
85330773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
85430773840STheodore Ts'o 	}
85530773840STheodore Ts'o 
856571640caSEric Sandeen 	/*
857571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
858571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
859571640caSEric Sandeen 	 * status is.
860571640caSEric Sandeen 	 */
861571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
862571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
863cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
864cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
865d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
866d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
86725ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
86825ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
869dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
870dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
871dd919b98SAneesh Kumar K.V 
872ac27a0ecSDave Kleikamp 
873cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
874cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
875aa22df2cSAneesh Kumar K.V 	/*
876aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
877aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
878aa22df2cSAneesh Kumar K.V 	 */
879617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
880ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
881617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
882ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
883617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
884ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
885ac27a0ecSDave Kleikamp 
886240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
887240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
888240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
889240799cdSTheodore Ts'o 
8905bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
8915bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
8925bf5683aSHidehiro Kawai 
893afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
89406705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
895afd4672dSTheodore Ts'o 
896617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
8970b8e58a1SAndreas Dilger 
898ac27a0ecSDave Kleikamp 	return 0;
899ac27a0ecSDave Kleikamp }
900ac27a0ecSDave Kleikamp 
9011b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
9021b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
903ac27a0ecSDave Kleikamp {
904ac27a0ecSDave Kleikamp 	struct inode *inode;
905ac27a0ecSDave Kleikamp 
906617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
907ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
908617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
909ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
910ac27a0ecSDave Kleikamp 
911ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
912ac27a0ecSDave Kleikamp 	 *
913617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
914ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
915ac27a0ecSDave Kleikamp 	 *
916ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
917ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
918ac27a0ecSDave Kleikamp 	 */
9191d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
9201d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9211d1fe1eeSDavid Howells 		return ERR_CAST(inode);
9221d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
923ac27a0ecSDave Kleikamp 		iput(inode);
924ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
925ac27a0ecSDave Kleikamp 	}
9261b961ac0SChristoph Hellwig 
9271b961ac0SChristoph Hellwig 	return inode;
928ac27a0ecSDave Kleikamp }
9291b961ac0SChristoph Hellwig 
9301b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
9311b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
9321b961ac0SChristoph Hellwig {
9331b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
9341b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
9351b961ac0SChristoph Hellwig }
9361b961ac0SChristoph Hellwig 
9371b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
9381b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
9391b961ac0SChristoph Hellwig {
9401b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
9411b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
942ac27a0ecSDave Kleikamp }
943ac27a0ecSDave Kleikamp 
944c39a7f84SToshiyuki Okajima /*
945c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
946c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
947c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
948c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
949c39a7f84SToshiyuki Okajima  */
9500b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
9510b8e58a1SAndreas Dilger 				 gfp_t wait)
952c39a7f84SToshiyuki Okajima {
953c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
954c39a7f84SToshiyuki Okajima 
955c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
956c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
957c39a7f84SToshiyuki Okajima 		return 0;
958c39a7f84SToshiyuki Okajima 	if (journal)
959c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
960c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
961c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
962c39a7f84SToshiyuki Okajima }
963c39a7f84SToshiyuki Okajima 
964ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
965ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
966ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
967ac27a0ecSDave Kleikamp 
968617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
969617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
970617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
971617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
972617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
9736f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
9746f28e087SJan Kara 				char *path, int remount);
975617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
976617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
977ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
978617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
979ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
980ac27a0ecSDave Kleikamp 
981617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
982edf72453SJan Kara 	.initialize	= dquot_initialize,
983edf72453SJan Kara 	.drop		= dquot_drop,
984ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
98560e58e0fSMingming Cao 	.reserve_space	= dquot_reserve_space,
98660e58e0fSMingming Cao 	.claim_space	= dquot_claim_space,
98760e58e0fSMingming Cao 	.release_rsv	= dquot_release_reserved_space,
98860e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
989ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
990ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
991ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
992ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
993617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
994617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
995617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
996617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
997a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
998a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
999a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1000ac27a0ecSDave Kleikamp };
1001ac27a0ecSDave Kleikamp 
1002617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
1003617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1004ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
1005ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
1006ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
1007ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
1008ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
1009ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
1010ac27a0ecSDave Kleikamp };
1011ac27a0ecSDave Kleikamp #endif
1012ac27a0ecSDave Kleikamp 
1013ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1014617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1015617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1016617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1017617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
1018617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
1019617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1020617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1021c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1022c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1023617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1024617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1025617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
1026617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1027ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1028617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1029617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1030ac27a0ecSDave Kleikamp #endif
1031c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1032ac27a0ecSDave Kleikamp };
1033ac27a0ecSDave Kleikamp 
10349ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
10359ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
10369ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
10379ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
10389ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
10399ca92389STheodore Ts'o 	.delete_inode	= ext4_delete_inode,
10409ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
10419ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
10429ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
10439ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
10449ca92389STheodore Ts'o 	.clear_inode	= ext4_clear_inode,
10459ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
10469ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
10479ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
10489ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
10499ca92389STheodore Ts'o #endif
10509ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
10519ca92389STheodore Ts'o };
10529ca92389STheodore Ts'o 
105339655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
10541b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
10551b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1056617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1057ac27a0ecSDave Kleikamp };
1058ac27a0ecSDave Kleikamp 
1059ac27a0ecSDave Kleikamp enum {
1060ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1061ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
106201436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1063ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
106406705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
106530773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1066c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1067818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1068ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1069f4033903SCurt Wohlgemuth 	Opt_data_err_abort, Opt_data_err_ignore, Opt_mb_history_length,
1070ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1071ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
107206705bffSTheodore Ts'o 	Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize,
107306705bffSTheodore Ts'o 	Opt_usrquota, Opt_grpquota, Opt_i_version,
107401436ef2STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc,
10756fd058f7STheodore Ts'o 	Opt_block_validity, Opt_noblock_validity,
1076b3881f74STheodore Ts'o 	Opt_inode_readahead_blks, Opt_journal_ioprio
1077ac27a0ecSDave Kleikamp };
1078ac27a0ecSDave Kleikamp 
1079a447c093SSteven Whitehouse static const match_table_t tokens = {
1080ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1081ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1082ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1083ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1084ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1085ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1086ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1087ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1088ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1089ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1090ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1091ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1092ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1093ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1094ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1095ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1096ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1097ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1098ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1099ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1100ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1101ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1102ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1103ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
110430773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
110530773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1106ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1107ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1108818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1109818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1110ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1111ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1112ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1113ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
11145bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
11155bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1116f4033903SCurt Wohlgemuth 	{Opt_mb_history_length, "mb_history_length=%u"},
1117ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1118ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1119ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1120ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1121ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1122ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1123ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1124ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1125ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1126ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1127ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
112806705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
112906705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
113025ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1131c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1132ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
113364769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1134dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
11356fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
11366fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1137240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1138b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1139afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
114006705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
114106705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1142f3f12faaSJosef Bacik 	{Opt_err, NULL},
1143ac27a0ecSDave Kleikamp };
1144ac27a0ecSDave Kleikamp 
1145617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1146ac27a0ecSDave Kleikamp {
1147617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1148ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1149ac27a0ecSDave Kleikamp 
1150ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1151ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
11520b8e58a1SAndreas Dilger 
1153ac27a0ecSDave Kleikamp 	options += 3;
11540b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1155ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1156ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
11574776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1158ac27a0ecSDave Kleikamp 		       (char *) *data);
1159ac27a0ecSDave Kleikamp 		return 1;
1160ac27a0ecSDave Kleikamp 	}
1161ac27a0ecSDave Kleikamp 	if (*options == ',')
1162ac27a0ecSDave Kleikamp 		options++;
1163ac27a0ecSDave Kleikamp 	*data = (void *) options;
11640b8e58a1SAndreas Dilger 
1165ac27a0ecSDave Kleikamp 	return sb_block;
1166ac27a0ecSDave Kleikamp }
1167ac27a0ecSDave Kleikamp 
1168b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1169b3881f74STheodore Ts'o 
1170ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1171c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1172b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1173617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1174ac27a0ecSDave Kleikamp {
1175617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1176ac27a0ecSDave Kleikamp 	char *p;
1177ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1178ac27a0ecSDave Kleikamp 	int data_opt = 0;
1179ac27a0ecSDave Kleikamp 	int option;
1180ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1181dfc5d03fSJan Kara 	int qtype, qfmt;
1182ac27a0ecSDave Kleikamp 	char *qname;
1183ac27a0ecSDave Kleikamp #endif
1184ac27a0ecSDave Kleikamp 
1185ac27a0ecSDave Kleikamp 	if (!options)
1186ac27a0ecSDave Kleikamp 		return 1;
1187ac27a0ecSDave Kleikamp 
1188ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1189ac27a0ecSDave Kleikamp 		int token;
1190ac27a0ecSDave Kleikamp 		if (!*p)
1191ac27a0ecSDave Kleikamp 			continue;
1192ac27a0ecSDave Kleikamp 
1193ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1194ac27a0ecSDave Kleikamp 		switch (token) {
1195ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1196ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
1197ac27a0ecSDave Kleikamp 			break;
1198ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1199ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
1200ac27a0ecSDave Kleikamp 			break;
1201ac27a0ecSDave Kleikamp 		case Opt_grpid:
1202ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
1203ac27a0ecSDave Kleikamp 			break;
1204ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1205ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
1206ac27a0ecSDave Kleikamp 			break;
1207ac27a0ecSDave Kleikamp 		case Opt_resuid:
1208ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1209ac27a0ecSDave Kleikamp 				return 0;
1210ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1211ac27a0ecSDave Kleikamp 			break;
1212ac27a0ecSDave Kleikamp 		case Opt_resgid:
1213ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1214ac27a0ecSDave Kleikamp 				return 0;
1215ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1216ac27a0ecSDave Kleikamp 			break;
1217ac27a0ecSDave Kleikamp 		case Opt_sb:
1218ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1219ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1220ac27a0ecSDave Kleikamp 			break;
1221ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1222ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1223ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1224ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1225ac27a0ecSDave Kleikamp 			break;
1226ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1227ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1228ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1229ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1230ac27a0ecSDave Kleikamp 			break;
1231ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1232ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1233ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1234ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1235ac27a0ecSDave Kleikamp 			break;
1236ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1237ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1238ac27a0ecSDave Kleikamp 			break;
1239ac27a0ecSDave Kleikamp 		case Opt_debug:
1240ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1241ac27a0ecSDave Kleikamp 			break;
1242ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1243ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1244ac27a0ecSDave Kleikamp 			break;
1245ac27a0ecSDave Kleikamp 		case Opt_orlov:
1246ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1247ac27a0ecSDave Kleikamp 			break;
124803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1249ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1250ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1251ac27a0ecSDave Kleikamp 			break;
1252ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1253ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1254ac27a0ecSDave Kleikamp 			break;
1255ac27a0ecSDave Kleikamp #else
1256ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1257ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1258b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1259ac27a0ecSDave Kleikamp 			break;
1260ac27a0ecSDave Kleikamp #endif
126103010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1262ac27a0ecSDave Kleikamp 		case Opt_acl:
1263ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1264ac27a0ecSDave Kleikamp 			break;
1265ac27a0ecSDave Kleikamp 		case Opt_noacl:
1266ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1267ac27a0ecSDave Kleikamp 			break;
1268ac27a0ecSDave Kleikamp #else
1269ac27a0ecSDave Kleikamp 		case Opt_acl:
1270ac27a0ecSDave Kleikamp 		case Opt_noacl:
1271b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1272ac27a0ecSDave Kleikamp 			break;
1273ac27a0ecSDave Kleikamp #endif
1274ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1275ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1276ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1277ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1278ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1279ac27a0ecSDave Kleikamp 			   journal file. */
1280ac27a0ecSDave Kleikamp 			if (is_remount) {
1281b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1282b31e1552SEric Sandeen 					 "Cannot specify journal on remount");
1283ac27a0ecSDave Kleikamp 				return 0;
1284ac27a0ecSDave Kleikamp 			}
1285ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1286ac27a0ecSDave Kleikamp 			break;
1287ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1288ac27a0ecSDave Kleikamp 			if (is_remount) {
1289b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1290b31e1552SEric Sandeen 					"Cannot specify journal on remount");
1291ac27a0ecSDave Kleikamp 				return 0;
1292ac27a0ecSDave Kleikamp 			}
1293ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1294ac27a0ecSDave Kleikamp 				return 0;
1295ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1296ac27a0ecSDave Kleikamp 			break;
1297818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1298818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1299818d276cSGirish Shilamkar 			break;
1300818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1301818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1302818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1303818d276cSGirish Shilamkar 			break;
1304ac27a0ecSDave Kleikamp 		case Opt_noload:
1305ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1306ac27a0ecSDave Kleikamp 			break;
1307ac27a0ecSDave Kleikamp 		case Opt_commit:
1308ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1309ac27a0ecSDave Kleikamp 				return 0;
1310ac27a0ecSDave Kleikamp 			if (option < 0)
1311ac27a0ecSDave Kleikamp 				return 0;
1312ac27a0ecSDave Kleikamp 			if (option == 0)
1313cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1314ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1315ac27a0ecSDave Kleikamp 			break;
131630773840STheodore Ts'o 		case Opt_max_batch_time:
131730773840STheodore Ts'o 			if (match_int(&args[0], &option))
131830773840STheodore Ts'o 				return 0;
131930773840STheodore Ts'o 			if (option < 0)
132030773840STheodore Ts'o 				return 0;
132130773840STheodore Ts'o 			if (option == 0)
132230773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
132330773840STheodore Ts'o 			sbi->s_max_batch_time = option;
132430773840STheodore Ts'o 			break;
132530773840STheodore Ts'o 		case Opt_min_batch_time:
132630773840STheodore Ts'o 			if (match_int(&args[0], &option))
132730773840STheodore Ts'o 				return 0;
132830773840STheodore Ts'o 			if (option < 0)
132930773840STheodore Ts'o 				return 0;
133030773840STheodore Ts'o 			sbi->s_min_batch_time = option;
133130773840STheodore Ts'o 			break;
1332ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1333617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1334ac27a0ecSDave Kleikamp 			goto datacheck;
1335ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1336617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1337ac27a0ecSDave Kleikamp 			goto datacheck;
1338ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1339617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1340ac27a0ecSDave Kleikamp 		datacheck:
1341ac27a0ecSDave Kleikamp 			if (is_remount) {
1342617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1343ac27a0ecSDave Kleikamp 						!= data_opt) {
1344b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
1345b31e1552SEric Sandeen 						"Cannot change data mode on remount");
1346ac27a0ecSDave Kleikamp 					return 0;
1347ac27a0ecSDave Kleikamp 				}
1348ac27a0ecSDave Kleikamp 			} else {
1349617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1350ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1351ac27a0ecSDave Kleikamp 			}
1352ac27a0ecSDave Kleikamp 			break;
13535bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
13545bf5683aSHidehiro Kawai 			set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13555bf5683aSHidehiro Kawai 			break;
13565bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
13575bf5683aSHidehiro Kawai 			clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13585bf5683aSHidehiro Kawai 			break;
1359f4033903SCurt Wohlgemuth 		case Opt_mb_history_length:
1360f4033903SCurt Wohlgemuth 			if (match_int(&args[0], &option))
1361f4033903SCurt Wohlgemuth 				return 0;
1362f4033903SCurt Wohlgemuth 			if (option < 0)
1363f4033903SCurt Wohlgemuth 				return 0;
1364f4033903SCurt Wohlgemuth 			sbi->s_mb_history_max = option;
1365f4033903SCurt Wohlgemuth 			break;
1366ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1367ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1368ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1369ac27a0ecSDave Kleikamp 			goto set_qf_name;
1370ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1371ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1372ac27a0ecSDave Kleikamp set_qf_name:
137317bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1374dfc5d03fSJan Kara 			    !sbi->s_qf_names[qtype]) {
1375b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1376b31e1552SEric Sandeen 				       "Cannot change journaled "
1377b31e1552SEric Sandeen 				       "quota options when quota turned on");
1378ac27a0ecSDave Kleikamp 				return 0;
1379ac27a0ecSDave Kleikamp 			}
1380ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1381ac27a0ecSDave Kleikamp 			if (!qname) {
1382b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1383b31e1552SEric Sandeen 					"Not enough memory for "
1384b31e1552SEric Sandeen 					"storing quotafile name");
1385ac27a0ecSDave Kleikamp 				return 0;
1386ac27a0ecSDave Kleikamp 			}
1387ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1388ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1389b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1390b31e1552SEric Sandeen 					"%s quota file already "
1391b31e1552SEric Sandeen 					"specified", QTYPE2NAME(qtype));
1392ac27a0ecSDave Kleikamp 				kfree(qname);
1393ac27a0ecSDave Kleikamp 				return 0;
1394ac27a0ecSDave Kleikamp 			}
1395ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1396ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1397b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1398b31e1552SEric Sandeen 					"quotafile must be on "
1399b31e1552SEric Sandeen 					"filesystem root");
1400ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1401ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1402ac27a0ecSDave Kleikamp 				return 0;
1403ac27a0ecSDave Kleikamp 			}
1404ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1405ac27a0ecSDave Kleikamp 			break;
1406ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1407ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1408ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1409ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1410ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1411ac27a0ecSDave Kleikamp clear_qf_name:
141217bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1413dfc5d03fSJan Kara 			    sbi->s_qf_names[qtype]) {
1414b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
14152c8be6b2SJan Kara 					"journaled quota options when "
1416b31e1552SEric Sandeen 					"quota turned on");
1417ac27a0ecSDave Kleikamp 				return 0;
1418ac27a0ecSDave Kleikamp 			}
1419ac27a0ecSDave Kleikamp 			/*
1420ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1421ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1422ac27a0ecSDave Kleikamp 			 */
1423ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1424ac27a0ecSDave Kleikamp 			break;
1425ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1426dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1427dfc5d03fSJan Kara 			goto set_qf_format;
1428ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1429dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
1430dfc5d03fSJan Kara set_qf_format:
143117bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1432dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1433b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
1434dfc5d03fSJan Kara 					"journaled quota options when "
1435b31e1552SEric Sandeen 					"quota turned on");
1436dfc5d03fSJan Kara 				return 0;
1437dfc5d03fSJan Kara 			}
1438dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1439ac27a0ecSDave Kleikamp 			break;
1440ac27a0ecSDave Kleikamp 		case Opt_quota:
1441ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1442ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1443ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1444ac27a0ecSDave Kleikamp 			break;
1445ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1446ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1447ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1448ac27a0ecSDave Kleikamp 			break;
1449ac27a0ecSDave Kleikamp 		case Opt_noquota:
145017bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1451b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change quota "
1452b31e1552SEric Sandeen 					"options when quota turned on");
1453ac27a0ecSDave Kleikamp 				return 0;
1454ac27a0ecSDave Kleikamp 			}
1455ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1456ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1457ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1458ac27a0ecSDave Kleikamp 			break;
1459ac27a0ecSDave Kleikamp #else
1460ac27a0ecSDave Kleikamp 		case Opt_quota:
1461ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1462ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1463b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1464b31e1552SEric Sandeen 				"quota options not supported");
1465cd59e7b9SJan Kara 			break;
1466ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1467ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1468ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1469ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1470ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1471ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1472b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1473b31e1552SEric Sandeen 				"journaled quota options not supported");
1474ac27a0ecSDave Kleikamp 			break;
1475ac27a0ecSDave Kleikamp 		case Opt_noquota:
1476ac27a0ecSDave Kleikamp 			break;
1477ac27a0ecSDave Kleikamp #endif
1478ac27a0ecSDave Kleikamp 		case Opt_abort:
1479*4ab2f15bSTheodore Ts'o 			sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1480ac27a0ecSDave Kleikamp 			break;
148106705bffSTheodore Ts'o 		case Opt_nobarrier:
148206705bffSTheodore Ts'o 			clear_opt(sbi->s_mount_opt, BARRIER);
148306705bffSTheodore Ts'o 			break;
1484ac27a0ecSDave Kleikamp 		case Opt_barrier:
148506705bffSTheodore Ts'o 			if (match_int(&args[0], &option)) {
148606705bffSTheodore Ts'o 				set_opt(sbi->s_mount_opt, BARRIER);
148706705bffSTheodore Ts'o 				break;
148806705bffSTheodore Ts'o 			}
1489ac27a0ecSDave Kleikamp 			if (option)
1490ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1491ac27a0ecSDave Kleikamp 			else
1492ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1493ac27a0ecSDave Kleikamp 			break;
1494ac27a0ecSDave Kleikamp 		case Opt_ignore:
1495ac27a0ecSDave Kleikamp 			break;
1496ac27a0ecSDave Kleikamp 		case Opt_resize:
1497ac27a0ecSDave Kleikamp 			if (!is_remount) {
1498b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1499b31e1552SEric Sandeen 					"resize option only available "
1500b31e1552SEric Sandeen 					"for remount");
1501ac27a0ecSDave Kleikamp 				return 0;
1502ac27a0ecSDave Kleikamp 			}
1503ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1504ac27a0ecSDave Kleikamp 				return 0;
1505ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1506ac27a0ecSDave Kleikamp 			break;
1507ac27a0ecSDave Kleikamp 		case Opt_nobh:
1508ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1509ac27a0ecSDave Kleikamp 			break;
1510ac27a0ecSDave Kleikamp 		case Opt_bh:
1511ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1512ac27a0ecSDave Kleikamp 			break;
151325ec56b5SJean Noel Cordenner 		case Opt_i_version:
151425ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
151525ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
151625ec56b5SJean Noel Cordenner 			break;
1517dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1518dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1519dd919b98SAneesh Kumar K.V 			break;
1520c9de560dSAlex Tomas 		case Opt_stripe:
1521c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1522c9de560dSAlex Tomas 				return 0;
1523c9de560dSAlex Tomas 			if (option < 0)
1524c9de560dSAlex Tomas 				return 0;
1525c9de560dSAlex Tomas 			sbi->s_stripe = option;
1526c9de560dSAlex Tomas 			break;
152764769240SAlex Tomas 		case Opt_delalloc:
152864769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
152964769240SAlex Tomas 			break;
15306fd058f7STheodore Ts'o 		case Opt_block_validity:
15316fd058f7STheodore Ts'o 			set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
15326fd058f7STheodore Ts'o 			break;
15336fd058f7STheodore Ts'o 		case Opt_noblock_validity:
15346fd058f7STheodore Ts'o 			clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
15356fd058f7STheodore Ts'o 			break;
1536240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1537240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1538240799cdSTheodore Ts'o 				return 0;
1539240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1540240799cdSTheodore Ts'o 				return 0;
1541f7c43950STheodore Ts'o 			if (!is_power_of_2(option)) {
1542b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1543b31e1552SEric Sandeen 					 "EXT4-fs: inode_readahead_blks"
1544b31e1552SEric Sandeen 					 " must be a power of 2");
15453197ebdbSTheodore Ts'o 				return 0;
15463197ebdbSTheodore Ts'o 			}
1547240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1548240799cdSTheodore Ts'o 			break;
1549b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1550b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1551b3881f74STheodore Ts'o 				return 0;
1552b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1553b3881f74STheodore Ts'o 				break;
1554b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1555b3881f74STheodore Ts'o 							    option);
1556b3881f74STheodore Ts'o 			break;
155706705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
155806705bffSTheodore Ts'o 			set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
155906705bffSTheodore Ts'o 			break;
1560afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
156106705bffSTheodore Ts'o 			if (match_int(&args[0], &option)) {
156206705bffSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
156306705bffSTheodore Ts'o 				break;
156406705bffSTheodore Ts'o 			}
1565afd4672dSTheodore Ts'o 			if (option)
1566afd4672dSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1567afd4672dSTheodore Ts'o 			else
1568afd4672dSTheodore Ts'o 				set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1569afd4672dSTheodore Ts'o 			break;
1570ac27a0ecSDave Kleikamp 		default:
1571b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1572b31e1552SEric Sandeen 			       "Unrecognized mount option \"%s\" "
1573b31e1552SEric Sandeen 			       "or missing value", p);
1574ac27a0ecSDave Kleikamp 			return 0;
1575ac27a0ecSDave Kleikamp 		}
1576ac27a0ecSDave Kleikamp 	}
1577ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1578ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1579617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1580ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1581ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1582ac27a0ecSDave Kleikamp 
1583617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1584ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1585ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1586ac27a0ecSDave Kleikamp 
1587ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1588617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1589ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1590617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1591b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1592b31e1552SEric Sandeen 					"format mixing");
1593ac27a0ecSDave Kleikamp 			return 0;
1594ac27a0ecSDave Kleikamp 		}
1595ac27a0ecSDave Kleikamp 
1596ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1597b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1598b31e1552SEric Sandeen 					"not specified");
1599ac27a0ecSDave Kleikamp 			return 0;
1600ac27a0ecSDave Kleikamp 		}
1601ac27a0ecSDave Kleikamp 	} else {
1602ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1603b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
16042c8be6b2SJan Kara 					"specified with no journaling "
1605b31e1552SEric Sandeen 					"enabled");
1606ac27a0ecSDave Kleikamp 			return 0;
1607ac27a0ecSDave Kleikamp 		}
1608ac27a0ecSDave Kleikamp 	}
1609ac27a0ecSDave Kleikamp #endif
1610ac27a0ecSDave Kleikamp 	return 1;
1611ac27a0ecSDave Kleikamp }
1612ac27a0ecSDave Kleikamp 
1613617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1614ac27a0ecSDave Kleikamp 			    int read_only)
1615ac27a0ecSDave Kleikamp {
1616617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1617ac27a0ecSDave Kleikamp 	int res = 0;
1618ac27a0ecSDave Kleikamp 
1619617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1620b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1621b31e1552SEric Sandeen 			 "forcing read-only mode");
1622ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1623ac27a0ecSDave Kleikamp 	}
1624ac27a0ecSDave Kleikamp 	if (read_only)
1625ac27a0ecSDave Kleikamp 		return res;
1626617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1627b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1628b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1629617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1630b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1631b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1632b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1633ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1634ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1635ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1636b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1637b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1638b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1639ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1640ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1641ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1642b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1643b31e1552SEric Sandeen 			 "warning: checktime reached, "
1644b31e1552SEric Sandeen 			 "running e2fsck is recommended");
16450390131bSFrank Mayhar 	if (!sbi->s_journal)
1646216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1647ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1648617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1649e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1650ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1651617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
16520390131bSFrank Mayhar 	if (sbi->s_journal)
1653617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1654ac27a0ecSDave Kleikamp 
1655e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1656ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1657a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
16587f4520ccSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x]\n",
1659ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1660ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1661617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1662617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1663ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1664ac27a0ecSDave Kleikamp 
16650390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
1666b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%s journal on %s",
1667b31e1552SEric Sandeen 		       EXT4_SB(sb)->s_journal->j_inode ? "internal" :
166805496769STheodore Ts'o 		       "external", EXT4_SB(sb)->s_journal->j_devname);
16690390131bSFrank Mayhar 	} else {
1670b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "no journal");
16710390131bSFrank Mayhar 	}
1672ac27a0ecSDave Kleikamp 	return res;
1673ac27a0ecSDave Kleikamp }
1674ac27a0ecSDave Kleikamp 
1675772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1676772cb7c8SJose R. Santos {
1677772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1678772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1679772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1680772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1681772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1682c5ca7c76STheodore Ts'o 	size_t size;
1683772cb7c8SJose R. Santos 	int i;
1684772cb7c8SJose R. Santos 
1685772cb7c8SJose R. Santos 	if (!sbi->s_es->s_log_groups_per_flex) {
1686772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1687772cb7c8SJose R. Santos 		return 1;
1688772cb7c8SJose R. Santos 	}
1689772cb7c8SJose R. Santos 
1690772cb7c8SJose R. Santos 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1691772cb7c8SJose R. Santos 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1692772cb7c8SJose R. Santos 
1693c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1694c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1695d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1696d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1697c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
1698c5ca7c76STheodore Ts'o 	sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1699c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
1700c5ca7c76STheodore Ts'o 		sbi->s_flex_groups = vmalloc(size);
1701c5ca7c76STheodore Ts'o 		if (sbi->s_flex_groups)
1702c5ca7c76STheodore Ts'o 			memset(sbi->s_flex_groups, 0, size);
1703c5ca7c76STheodore Ts'o 	}
1704772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1705b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory for "
1706b31e1552SEric Sandeen 				"%u flex groups", flex_group_count);
1707772cb7c8SJose R. Santos 		goto failed;
1708772cb7c8SJose R. Santos 	}
1709772cb7c8SJose R. Santos 
1710772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
171188b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1712772cb7c8SJose R. Santos 
1713772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
17149f24e420STheodore Ts'o 		atomic_set(&sbi->s_flex_groups[flex_group].free_inodes,
17159f24e420STheodore Ts'o 			   ext4_free_inodes_count(sb, gdp));
17169f24e420STheodore Ts'o 		atomic_set(&sbi->s_flex_groups[flex_group].free_blocks,
17179f24e420STheodore Ts'o 			   ext4_free_blks_count(sb, gdp));
17187d39db14STheodore Ts'o 		atomic_set(&sbi->s_flex_groups[flex_group].used_dirs,
17197d39db14STheodore Ts'o 			   ext4_used_dirs_count(sb, gdp));
1720772cb7c8SJose R. Santos 	}
1721772cb7c8SJose R. Santos 
1722772cb7c8SJose R. Santos 	return 1;
1723772cb7c8SJose R. Santos failed:
1724772cb7c8SJose R. Santos 	return 0;
1725772cb7c8SJose R. Santos }
1726772cb7c8SJose R. Santos 
1727717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1728717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1729717d50e4SAndreas Dilger {
1730717d50e4SAndreas Dilger 	__u16 crc = 0;
1731717d50e4SAndreas Dilger 
1732717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1733717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1734717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1735717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1736717d50e4SAndreas Dilger 
1737717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1738717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1739717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1740717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1741717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1742717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1743717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1744717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1745717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1746717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1747717d50e4SAndreas Dilger 					offset);
1748717d50e4SAndreas Dilger 	}
1749717d50e4SAndreas Dilger 
1750717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1751717d50e4SAndreas Dilger }
1752717d50e4SAndreas Dilger 
1753717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1754717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1755717d50e4SAndreas Dilger {
1756717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1757717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1758717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1759717d50e4SAndreas Dilger 		return 0;
1760717d50e4SAndreas Dilger 
1761717d50e4SAndreas Dilger 	return 1;
1762717d50e4SAndreas Dilger }
1763717d50e4SAndreas Dilger 
1764ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1765617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1766ac27a0ecSDave Kleikamp {
1767617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1768617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1769617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1770bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1771bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1772bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1773ce421581SJose R. Santos 	int flexbg_flag = 0;
1774fd2d4291SAvantika Mathur 	ext4_group_t i;
1775ac27a0ecSDave Kleikamp 
1776ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1777ce421581SJose R. Santos 		flexbg_flag = 1;
1778ce421581SJose R. Santos 
1779617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1780ac27a0ecSDave Kleikamp 
1781197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1782197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1783197cd65aSAkinobu Mita 
1784ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1785bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1786ac27a0ecSDave Kleikamp 		else
1787ac27a0ecSDave Kleikamp 			last_block = first_block +
1788617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1789ac27a0ecSDave Kleikamp 
17908fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
17912b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1792b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1793a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
1794b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
1795ac27a0ecSDave Kleikamp 			return 0;
1796ac27a0ecSDave Kleikamp 		}
17978fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
17982b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1799b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1800a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
1801b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
1802ac27a0ecSDave Kleikamp 			return 0;
1803ac27a0ecSDave Kleikamp 		}
18048fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1805bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
18062b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1807b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1808a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
1809b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
1810ac27a0ecSDave Kleikamp 			return 0;
1811ac27a0ecSDave Kleikamp 		}
1812955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
1813717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1814b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1815b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
1816fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1817fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
18187ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
1819955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
1820717d50e4SAndreas Dilger 				return 0;
1821717d50e4SAndreas Dilger 			}
18227ee1ec4cSLi Zefan 		}
1823955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
1824ce421581SJose R. Santos 		if (!flexbg_flag)
1825617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1826ac27a0ecSDave Kleikamp 	}
1827ac27a0ecSDave Kleikamp 
1828bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1829617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
1830ac27a0ecSDave Kleikamp 	return 1;
1831ac27a0ecSDave Kleikamp }
1832ac27a0ecSDave Kleikamp 
1833617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1834ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1835ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1836ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1837ac27a0ecSDave Kleikamp  *
1838ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1839ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1840ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1841ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1842ac27a0ecSDave Kleikamp  *
1843ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1844ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1845ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1846617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1847ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1848ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1849ac27a0ecSDave Kleikamp  */
1850617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1851617ba13bSMingming Cao 				struct ext4_super_block *es)
1852ac27a0ecSDave Kleikamp {
1853ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1854ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1855ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1856ac27a0ecSDave Kleikamp 	int i;
1857ac27a0ecSDave Kleikamp #endif
1858ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1859ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1860ac27a0ecSDave Kleikamp 		return;
1861ac27a0ecSDave Kleikamp 	}
1862ac27a0ecSDave Kleikamp 
1863a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1864b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
1865b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
1866a8f48a95SEric Sandeen 		return;
1867a8f48a95SEric Sandeen 	}
1868a8f48a95SEric Sandeen 
1869617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1870ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1871ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1872ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1873ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1874ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1875ac27a0ecSDave Kleikamp 		return;
1876ac27a0ecSDave Kleikamp 	}
1877ac27a0ecSDave Kleikamp 
1878ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1879b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
1880ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1881ac27a0ecSDave Kleikamp 	}
1882ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1883ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1884ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1885ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1886ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1887617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1888617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1889ac27a0ecSDave Kleikamp 			if (ret < 0)
1890b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1891b31e1552SEric Sandeen 					"Cannot turn on journaled "
1892b31e1552SEric Sandeen 					"quota: error %d", ret);
1893ac27a0ecSDave Kleikamp 		}
1894ac27a0ecSDave Kleikamp 	}
1895ac27a0ecSDave Kleikamp #endif
1896ac27a0ecSDave Kleikamp 
1897ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1898ac27a0ecSDave Kleikamp 		struct inode *inode;
1899ac27a0ecSDave Kleikamp 
190097bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
190197bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
1902ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1903ac27a0ecSDave Kleikamp 			break;
1904ac27a0ecSDave Kleikamp 		}
1905ac27a0ecSDave Kleikamp 
1906617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1907a269eb18SJan Kara 		vfs_dq_init(inode);
1908ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1909b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
1910b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
191146e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
1912e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1913ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1914617ba13bSMingming Cao 			ext4_truncate(inode);
1915ac27a0ecSDave Kleikamp 			nr_truncates++;
1916ac27a0ecSDave Kleikamp 		} else {
1917b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
1918b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
191946e665e9SHarvey Harrison 				__func__, inode->i_ino);
1920ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1921ac27a0ecSDave Kleikamp 				  inode->i_ino);
1922ac27a0ecSDave Kleikamp 			nr_orphans++;
1923ac27a0ecSDave Kleikamp 		}
1924ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1925ac27a0ecSDave Kleikamp 	}
1926ac27a0ecSDave Kleikamp 
1927ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1928ac27a0ecSDave Kleikamp 
1929ac27a0ecSDave Kleikamp 	if (nr_orphans)
1930b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1931b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
1932ac27a0ecSDave Kleikamp 	if (nr_truncates)
1933b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1934b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
1935ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1936ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1937ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1938ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
19396f28e087SJan Kara 			vfs_quota_off(sb, i, 0);
1940ac27a0ecSDave Kleikamp 	}
1941ac27a0ecSDave Kleikamp #endif
1942ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1943ac27a0ecSDave Kleikamp }
19440b8e58a1SAndreas Dilger 
1945cd2291a4SEric Sandeen /*
1946cd2291a4SEric Sandeen  * Maximal extent format file size.
1947cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1948cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1949cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1950cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1951cd2291a4SEric Sandeen  *
1952cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1953cd2291a4SEric Sandeen  */
1954f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
1955cd2291a4SEric Sandeen {
1956cd2291a4SEric Sandeen 	loff_t res;
1957cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
1958cd2291a4SEric Sandeen 
1959cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
1960f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1961cd2291a4SEric Sandeen 		/*
1962b3a6ffe1SJens Axboe 		 * CONFIG_LBD is not enabled implies the inode
1963cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
1964cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
1965cd2291a4SEric Sandeen 		 */
1966cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
1967cd2291a4SEric Sandeen 
1968cd2291a4SEric Sandeen 		/* total blocks in file system block size */
1969cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
1970cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
1971cd2291a4SEric Sandeen 	}
1972cd2291a4SEric Sandeen 
1973cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
1974cd2291a4SEric Sandeen 	res = 1LL << 32;
1975cd2291a4SEric Sandeen 	res <<= blkbits;
1976cd2291a4SEric Sandeen 	res -= 1;
1977cd2291a4SEric Sandeen 
1978cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
1979cd2291a4SEric Sandeen 	if (res > upper_limit)
1980cd2291a4SEric Sandeen 		res = upper_limit;
1981cd2291a4SEric Sandeen 
1982cd2291a4SEric Sandeen 	return res;
1983cd2291a4SEric Sandeen }
1984ac27a0ecSDave Kleikamp 
1985ac27a0ecSDave Kleikamp /*
1986cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
19870fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
19880fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
1989ac27a0ecSDave Kleikamp  */
1990f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1991ac27a0ecSDave Kleikamp {
1992617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
19930fc1b451SAneesh Kumar K.V 	int meta_blocks;
19940fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
19950b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
19960b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
19970b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
19980b8e58a1SAndreas Dilger 	 *
19990b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
20000b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
20010fc1b451SAneesh Kumar K.V 	 */
20020fc1b451SAneesh Kumar K.V 
2003f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
20040fc1b451SAneesh Kumar K.V 		/*
20050b8e58a1SAndreas Dilger 		 * !has_huge_files or CONFIG_LBD not enabled implies that
20060b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
20070b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
20080fc1b451SAneesh Kumar K.V 		 */
20090fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
20100fc1b451SAneesh Kumar K.V 
20110fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
20120fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
20130fc1b451SAneesh Kumar K.V 
20140fc1b451SAneesh Kumar K.V 	} else {
20158180a562SAneesh Kumar K.V 		/*
20168180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
20178180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
20188180a562SAneesh Kumar K.V 		 * represent total number of blocks in
20198180a562SAneesh Kumar K.V 		 * file system block size
20208180a562SAneesh Kumar K.V 		 */
20210fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
20220fc1b451SAneesh Kumar K.V 
20230fc1b451SAneesh Kumar K.V 	}
20240fc1b451SAneesh Kumar K.V 
20250fc1b451SAneesh Kumar K.V 	/* indirect blocks */
20260fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
20270fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
20280fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
20290fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
20300fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
20310fc1b451SAneesh Kumar K.V 
20320fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
20330fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2034ac27a0ecSDave Kleikamp 
2035ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2036ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2037ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2038ac27a0ecSDave Kleikamp 	res <<= bits;
2039ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2040ac27a0ecSDave Kleikamp 		res = upper_limit;
20410fc1b451SAneesh Kumar K.V 
20420fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
20430fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
20440fc1b451SAneesh Kumar K.V 
2045ac27a0ecSDave Kleikamp 	return res;
2046ac27a0ecSDave Kleikamp }
2047ac27a0ecSDave Kleikamp 
2048617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
204970bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2050ac27a0ecSDave Kleikamp {
2051617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2052fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2053ac27a0ecSDave Kleikamp 	int has_super = 0;
2054ac27a0ecSDave Kleikamp 
2055ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2056ac27a0ecSDave Kleikamp 
2057617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2058ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
205970bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2060ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2061617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2062ac27a0ecSDave Kleikamp 		has_super = 1;
20630b8e58a1SAndreas Dilger 
2064617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2065ac27a0ecSDave Kleikamp }
2066ac27a0ecSDave Kleikamp 
2067c9de560dSAlex Tomas /**
2068c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2069c9de560dSAlex Tomas  * @sbi: In memory super block info
2070c9de560dSAlex Tomas  *
2071c9de560dSAlex Tomas  * If we have specified it via mount option, then
2072c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2073c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2074c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2075c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2076c9de560dSAlex Tomas  *
2077c9de560dSAlex Tomas  */
2078c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2079c9de560dSAlex Tomas {
2080c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2081c9de560dSAlex Tomas 	unsigned long stripe_width =
2082c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2083c9de560dSAlex Tomas 
2084c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2085c9de560dSAlex Tomas 		return sbi->s_stripe;
2086c9de560dSAlex Tomas 
2087c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
2088c9de560dSAlex Tomas 		return stripe_width;
2089c9de560dSAlex Tomas 
2090c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
2091c9de560dSAlex Tomas 		return stride;
2092c9de560dSAlex Tomas 
2093c9de560dSAlex Tomas 	return 0;
2094c9de560dSAlex Tomas }
2095ac27a0ecSDave Kleikamp 
20963197ebdbSTheodore Ts'o /* sysfs supprt */
20973197ebdbSTheodore Ts'o 
20983197ebdbSTheodore Ts'o struct ext4_attr {
20993197ebdbSTheodore Ts'o 	struct attribute attr;
21003197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
21013197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
21023197ebdbSTheodore Ts'o 			 const char *, size_t);
21033197ebdbSTheodore Ts'o 	int offset;
21043197ebdbSTheodore Ts'o };
21053197ebdbSTheodore Ts'o 
21063197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
21073197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
21083197ebdbSTheodore Ts'o {
21093197ebdbSTheodore Ts'o 	char *endp;
21103197ebdbSTheodore Ts'o 
21113197ebdbSTheodore Ts'o 	while (*buf && isspace(*buf))
21123197ebdbSTheodore Ts'o 		buf++;
21133197ebdbSTheodore Ts'o 	*value = simple_strtoul(buf, &endp, 0);
21143197ebdbSTheodore Ts'o 	while (*endp && isspace(*endp))
21153197ebdbSTheodore Ts'o 		endp++;
21163197ebdbSTheodore Ts'o 	if (*endp || *value > max)
21173197ebdbSTheodore Ts'o 		return -EINVAL;
21183197ebdbSTheodore Ts'o 
21193197ebdbSTheodore Ts'o 	return 0;
21203197ebdbSTheodore Ts'o }
21213197ebdbSTheodore Ts'o 
21223197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
21233197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
21243197ebdbSTheodore Ts'o 					      char *buf)
21253197ebdbSTheodore Ts'o {
21263197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
21273197ebdbSTheodore Ts'o 			(s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
21283197ebdbSTheodore Ts'o }
21293197ebdbSTheodore Ts'o 
21303197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
21313197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
21323197ebdbSTheodore Ts'o {
21333197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
21343197ebdbSTheodore Ts'o 
21353197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
21363197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
21373197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
21383197ebdbSTheodore Ts'o }
21393197ebdbSTheodore Ts'o 
21403197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
21413197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
21423197ebdbSTheodore Ts'o {
21433197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
21443197ebdbSTheodore Ts'o 
21453197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
21463197ebdbSTheodore Ts'o 			sbi->s_kbytes_written +
21473197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
21483197ebdbSTheodore Ts'o 			  EXT4_SB(sb)->s_sectors_written_start) >> 1));
21493197ebdbSTheodore Ts'o }
21503197ebdbSTheodore Ts'o 
21513197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
21523197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
21533197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
21543197ebdbSTheodore Ts'o {
21553197ebdbSTheodore Ts'o 	unsigned long t;
21563197ebdbSTheodore Ts'o 
21573197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
21583197ebdbSTheodore Ts'o 		return -EINVAL;
21593197ebdbSTheodore Ts'o 
2160f7c43950STheodore Ts'o 	if (!is_power_of_2(t))
21613197ebdbSTheodore Ts'o 		return -EINVAL;
21623197ebdbSTheodore Ts'o 
21633197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
21643197ebdbSTheodore Ts'o 	return count;
21653197ebdbSTheodore Ts'o }
21663197ebdbSTheodore Ts'o 
21673197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
21683197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
21693197ebdbSTheodore Ts'o {
21703197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
21713197ebdbSTheodore Ts'o 
21723197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
21733197ebdbSTheodore Ts'o }
21743197ebdbSTheodore Ts'o 
21753197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
21763197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
21773197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
21783197ebdbSTheodore Ts'o {
21793197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
21803197ebdbSTheodore Ts'o 	unsigned long t;
21813197ebdbSTheodore Ts'o 
21823197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
21833197ebdbSTheodore Ts'o 		return -EINVAL;
21843197ebdbSTheodore Ts'o 	*ui = t;
21853197ebdbSTheodore Ts'o 	return count;
21863197ebdbSTheodore Ts'o }
21873197ebdbSTheodore Ts'o 
21883197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
21893197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
21903197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
21913197ebdbSTheodore Ts'o 	.show	= _show,					\
21923197ebdbSTheodore Ts'o 	.store	= _store,					\
21933197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
21943197ebdbSTheodore Ts'o }
21953197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
21963197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
21973197ebdbSTheodore Ts'o 
21983197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
21993197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
22003197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
22013197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
22023197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
22033197ebdbSTheodore Ts'o 
22043197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
22053197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
22063197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
22073197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
22083197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
22093197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
22103197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
22113197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
22123197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
22133197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
22143197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
22153197ebdbSTheodore Ts'o 
22163197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
22173197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
22183197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
22193197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
22203197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
22213197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
22223197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
22233197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
22243197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
22253197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
22263197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
22273197ebdbSTheodore Ts'o 	NULL,
22283197ebdbSTheodore Ts'o };
22293197ebdbSTheodore Ts'o 
22303197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
22313197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
22323197ebdbSTheodore Ts'o {
22333197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22343197ebdbSTheodore Ts'o 						s_kobj);
22353197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
22363197ebdbSTheodore Ts'o 
22373197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
22383197ebdbSTheodore Ts'o }
22393197ebdbSTheodore Ts'o 
22403197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
22413197ebdbSTheodore Ts'o 			       struct attribute *attr,
22423197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
22433197ebdbSTheodore Ts'o {
22443197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22453197ebdbSTheodore Ts'o 						s_kobj);
22463197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
22473197ebdbSTheodore Ts'o 
22483197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
22493197ebdbSTheodore Ts'o }
22503197ebdbSTheodore Ts'o 
22513197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
22523197ebdbSTheodore Ts'o {
22533197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22543197ebdbSTheodore Ts'o 						s_kobj);
22553197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
22563197ebdbSTheodore Ts'o }
22573197ebdbSTheodore Ts'o 
22583197ebdbSTheodore Ts'o 
22593197ebdbSTheodore Ts'o static struct sysfs_ops ext4_attr_ops = {
22603197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
22613197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
22623197ebdbSTheodore Ts'o };
22633197ebdbSTheodore Ts'o 
22643197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
22653197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
22663197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
22673197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
22683197ebdbSTheodore Ts'o };
22693197ebdbSTheodore Ts'o 
2270617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
22717477827fSAneesh Kumar K.V 				__releases(kernel_lock)
22727477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
2273ac27a0ecSDave Kleikamp {
2274ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2275617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
2276617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2277617ba13bSMingming Cao 	ext4_fsblk_t block;
2278617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
227970bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
2280ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
2281ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
2282ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
2283ac27a0ecSDave Kleikamp 	struct inode *root;
22849f6200bbSTheodore Ts'o 	char *cp;
22850390131bSFrank Mayhar 	const char *descr;
22861d1fe1eeSDavid Howells 	int ret = -EINVAL;
2287ac27a0ecSDave Kleikamp 	int blocksize;
22884ec11028STheodore Ts'o 	unsigned int db_count;
22894ec11028STheodore Ts'o 	unsigned int i;
2290f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
22913a06d778SAneesh Kumar K.V 	int features;
2292bd81d8eeSLaurent Vivier 	__u64 blocks_count;
2293833f4077SPeter Zijlstra 	int err;
2294b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
2295ac27a0ecSDave Kleikamp 
2296ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2297ac27a0ecSDave Kleikamp 	if (!sbi)
2298ac27a0ecSDave Kleikamp 		return -ENOMEM;
2299705895b6SPekka Enberg 
2300705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
2301705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2302705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
2303705895b6SPekka Enberg 		kfree(sbi);
2304705895b6SPekka Enberg 		return -ENOMEM;
2305705895b6SPekka Enberg 	}
2306ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
2307ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
2308617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
2309617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
2310240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2311d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
2312afc32f7eSTheodore Ts'o 	sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2313afc32f7eSTheodore Ts'o 						      sectors[1]);
2314ac27a0ecSDave Kleikamp 
2315ac27a0ecSDave Kleikamp 	unlock_kernel();
2316ac27a0ecSDave Kleikamp 
23179f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
23189f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
23199f6200bbSTheodore Ts'o 		*cp = '!';
23209f6200bbSTheodore Ts'o 
2321617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2322ac27a0ecSDave Kleikamp 	if (!blocksize) {
2323b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
2324ac27a0ecSDave Kleikamp 		goto out_fail;
2325ac27a0ecSDave Kleikamp 	}
2326ac27a0ecSDave Kleikamp 
2327ac27a0ecSDave Kleikamp 	/*
2328617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
2329ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
2330ac27a0ecSDave Kleikamp 	 */
2331617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
233270bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
233370bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
2334ac27a0ecSDave Kleikamp 	} else {
233570bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
2336ac27a0ecSDave Kleikamp 	}
2337ac27a0ecSDave Kleikamp 
233870bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
2339b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
2340ac27a0ecSDave Kleikamp 		goto out_fail;
2341ac27a0ecSDave Kleikamp 	}
2342ac27a0ecSDave Kleikamp 	/*
2343ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
2344617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
2345ac27a0ecSDave Kleikamp 	 */
2346617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2347ac27a0ecSDave Kleikamp 	sbi->s_es = es;
2348ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
2349617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
2350617ba13bSMingming Cao 		goto cantfind_ext4;
2351afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
2352ac27a0ecSDave Kleikamp 
2353ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
2354ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2355617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
2356ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
2357617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2358ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
2359617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
2360ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
236103010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
2362617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2363ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
23642e7842b8SHugh Dickins #endif
236503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
2366617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
2367ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
23682e7842b8SHugh Dickins #endif
2369617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2370617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2371617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2372617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2373617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2374617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2375ac27a0ecSDave Kleikamp 
2376617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2377ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2378bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2379ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
2380bb4f397aSAneesh Kumar K.V 	else
2381bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
2382ac27a0ecSDave Kleikamp 
2383ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2384ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
238530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
238630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
238730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2388f4033903SCurt Wohlgemuth 	sbi->s_mb_history_max = default_mb_history_length;
2389ac27a0ecSDave Kleikamp 
2390571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
2391ac27a0ecSDave Kleikamp 
23921e2462f9SMingming Cao 	/*
2393dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
2394dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
2395dd919b98SAneesh Kumar K.V 	 */
2396dd919b98SAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, DELALLOC);
2397dd919b98SAneesh Kumar K.V 
2398b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
2399b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
2400ac27a0ecSDave Kleikamp 		goto failed_mount;
2401ac27a0ecSDave Kleikamp 
2402ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2403617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2404ac27a0ecSDave Kleikamp 
2405617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2406617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2407617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2408617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2409b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2410b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
2411b31e1552SEric Sandeen 		       "running e2fsck is recommended");
2412469108ffSTheodore Tso 
2413469108ffSTheodore Tso 	/*
2414ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2415ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2416ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2417ac27a0ecSDave Kleikamp 	 */
2418617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2419ac27a0ecSDave Kleikamp 	if (features) {
2420b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2421b31e1552SEric Sandeen 			"Couldn't mount because of "
2422b31e1552SEric Sandeen 			"unsupported optional features (%x)",
24233a06d778SAneesh Kumar K.V 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
24243a06d778SAneesh Kumar K.V 			~EXT4_FEATURE_INCOMPAT_SUPP));
2425ac27a0ecSDave Kleikamp 		goto failed_mount;
2426ac27a0ecSDave Kleikamp 	}
2427617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2428ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
2429b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2430b31e1552SEric Sandeen 			"Couldn't mount RDWR because of "
2431b31e1552SEric Sandeen 			"unsupported optional features (%x)",
24323a06d778SAneesh Kumar K.V 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
24333a06d778SAneesh Kumar K.V 			~EXT4_FEATURE_RO_COMPAT_SUPP));
2434ac27a0ecSDave Kleikamp 		goto failed_mount;
2435ac27a0ecSDave Kleikamp 	}
2436f287a1a5STheodore Ts'o 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2437f287a1a5STheodore Ts'o 				    EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2438f287a1a5STheodore Ts'o 	if (has_huge_files) {
24390fc1b451SAneesh Kumar K.V 		/*
24400fc1b451SAneesh Kumar K.V 		 * Large file size enabled file system can only be
2441b3a6ffe1SJens Axboe 		 * mount if kernel is build with CONFIG_LBD
24420fc1b451SAneesh Kumar K.V 		 */
24430fc1b451SAneesh Kumar K.V 		if (sizeof(root->i_blocks) < sizeof(u64) &&
24440fc1b451SAneesh Kumar K.V 				!(sb->s_flags & MS_RDONLY)) {
2445b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge "
24460fc1b451SAneesh Kumar K.V 					"files cannot be mounted read-write "
2447b31e1552SEric Sandeen 					"without CONFIG_LBD");
24480fc1b451SAneesh Kumar K.V 			goto failed_mount;
24490fc1b451SAneesh Kumar K.V 		}
24500fc1b451SAneesh Kumar K.V 	}
2451ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2452ac27a0ecSDave Kleikamp 
2453617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2454617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2455b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2456b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
2457ac27a0ecSDave Kleikamp 		goto failed_mount;
2458ac27a0ecSDave Kleikamp 	}
2459ac27a0ecSDave Kleikamp 
2460ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2461ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2462ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2463b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
2464ce40733cSAneesh Kumar K.V 					blocksize);
2465ac27a0ecSDave Kleikamp 			goto failed_mount;
2466ac27a0ecSDave Kleikamp 		}
2467ac27a0ecSDave Kleikamp 
2468ac27a0ecSDave Kleikamp 		brelse(bh);
246970bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
247070bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
247170bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2472ac27a0ecSDave Kleikamp 		if (!bh) {
2473b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2474b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
2475ac27a0ecSDave Kleikamp 			goto failed_mount;
2476ac27a0ecSDave Kleikamp 		}
2477617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2478ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2479617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2480b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2481b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
2482ac27a0ecSDave Kleikamp 			goto failed_mount;
2483ac27a0ecSDave Kleikamp 		}
2484ac27a0ecSDave Kleikamp 	}
2485ac27a0ecSDave Kleikamp 
2486f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2487f287a1a5STheodore Ts'o 						      has_huge_files);
2488f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2489ac27a0ecSDave Kleikamp 
2490617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2491617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2492617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2493ac27a0ecSDave Kleikamp 	} else {
2494ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2495ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2496617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
24971330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2498ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2499b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2500b31e1552SEric Sandeen 			       "unsupported inode size: %d",
2501ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2502ac27a0ecSDave Kleikamp 			goto failed_mount;
2503ac27a0ecSDave Kleikamp 		}
2504ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2505ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2506ac27a0ecSDave Kleikamp 	}
25070b8e58a1SAndreas Dilger 
25080d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
25090d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
25108fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
25110d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2512d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
2513b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2514b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
25150d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
25160d1ee42fSAlexandre Ratchov 			goto failed_mount;
25170d1ee42fSAlexandre Ratchov 		}
25180d1ee42fSAlexandre Ratchov 	} else
25190d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
25200b8e58a1SAndreas Dilger 
2521ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2522ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2523b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2524617ba13bSMingming Cao 		goto cantfind_ext4;
25250b8e58a1SAndreas Dilger 
2526617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2527ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2528617ba13bSMingming Cao 		goto cantfind_ext4;
2529ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2530ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
25310d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2532ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2533ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2534e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2535e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
25360b8e58a1SAndreas Dilger 
2537ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2538ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2539ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2540f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
2541f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
2542f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2543f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2544f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
2545f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2546f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2547f99b2589STheodore Ts'o #else
2548f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2549f99b2589STheodore Ts'o #endif
2550f99b2589STheodore Ts'o 		sb->s_dirt = 1;
2551f99b2589STheodore Ts'o 	}
2552ac27a0ecSDave Kleikamp 
2553ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2554b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2555b31e1552SEric Sandeen 		       "#blocks per group too big: %lu",
2556ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2557ac27a0ecSDave Kleikamp 		goto failed_mount;
2558ac27a0ecSDave Kleikamp 	}
2559ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2560b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2561b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
2562ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2563ac27a0ecSDave Kleikamp 		goto failed_mount;
2564ac27a0ecSDave Kleikamp 	}
2565ac27a0ecSDave Kleikamp 
2566bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
2567ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2568b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
2569b31e1552SEric Sandeen 			" too large to mount safely");
2570ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
2571b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBD not enabled");
2572ac27a0ecSDave Kleikamp 		goto failed_mount;
2573ac27a0ecSDave Kleikamp 	}
2574ac27a0ecSDave Kleikamp 
2575617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2576617ba13bSMingming Cao 		goto cantfind_ext4;
2577e7c95593SEric Sandeen 
25780f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
25790f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
25800f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2581b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2582b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
25830f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
25840f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
25850f2ddca6SFrom: Thiemo Nagel 	}
25860f2ddca6SFrom: Thiemo Nagel 
25874ec11028STheodore Ts'o 	/*
25884ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
25894ec11028STheodore Ts'o 	 * of the filesystem.
25904ec11028STheodore Ts'o 	 */
25914ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2592b31e1552SEric Sandeen                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2593b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
2594e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
25954ec11028STheodore Ts'o 			 ext4_blocks_count(es));
2596e7c95593SEric Sandeen 		goto failed_mount;
2597e7c95593SEric Sandeen 	}
2598bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2599bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2600bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2601bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
26024ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2603b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
26044ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
2605b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
26064ec11028STheodore Ts'o 		       ext4_blocks_count(es),
26074ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
26084ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
26094ec11028STheodore Ts'o 		goto failed_mount;
26104ec11028STheodore Ts'o 	}
2611bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2612617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2613617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2614ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2615ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2616ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2617b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
2618ac27a0ecSDave Kleikamp 		goto failed_mount;
2619ac27a0ecSDave Kleikamp 	}
2620ac27a0ecSDave Kleikamp 
26213244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
26229f6200bbSTheodore Ts'o 	if (ext4_proc_root)
26239f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
26243244fcb1SAlexander Beregalov #endif
2625240799cdSTheodore Ts'o 
2626705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
2627ac27a0ecSDave Kleikamp 
2628ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
262970bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2630ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2631ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2632b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2633b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
2634ac27a0ecSDave Kleikamp 			db_count = i;
2635ac27a0ecSDave Kleikamp 			goto failed_mount2;
2636ac27a0ecSDave Kleikamp 		}
2637ac27a0ecSDave Kleikamp 	}
2638617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2639b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
2640ac27a0ecSDave Kleikamp 		goto failed_mount2;
2641ac27a0ecSDave Kleikamp 	}
2642772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2643772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2644b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2645b31e1552SEric Sandeen 			       "unable to initialize "
2646b31e1552SEric Sandeen 			       "flex_bg meta info!");
2647772cb7c8SJose R. Santos 			goto failed_mount2;
2648772cb7c8SJose R. Santos 		}
2649772cb7c8SJose R. Santos 
2650ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2651ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2652ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2653ac27a0ecSDave Kleikamp 
2654833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2655617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2656833f4077SPeter Zijlstra 	if (!err) {
2657833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2658617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2659833f4077SPeter Zijlstra 	}
2660833f4077SPeter Zijlstra 	if (!err) {
2661833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2662617ba13bSMingming Cao 				ext4_count_dirs(sb));
2663833f4077SPeter Zijlstra 	}
26646bc6e63fSAneesh Kumar K.V 	if (!err) {
26656bc6e63fSAneesh Kumar K.V 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
26666bc6e63fSAneesh Kumar K.V 	}
2667833f4077SPeter Zijlstra 	if (err) {
2668b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "insufficient memory");
2669833f4077SPeter Zijlstra 		goto failed_mount3;
2670833f4077SPeter Zijlstra 	}
2671ac27a0ecSDave Kleikamp 
2672c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
2673c9de560dSAlex Tomas 
2674ac27a0ecSDave Kleikamp 	/*
2675ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2676ac27a0ecSDave Kleikamp 	 */
26779ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
26789ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2679617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
26809ca92389STheodore Ts'o 	else
26819ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
2682617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2683617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2684ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2685617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2686617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2687ac27a0ecSDave Kleikamp #endif
2688ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
26893b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
269032ed5058STheodore Ts'o 	mutex_init(&sbi->s_resize_lock);
2691ac27a0ecSDave Kleikamp 
2692ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2693ac27a0ecSDave Kleikamp 
2694ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2695617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2696617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2697ac27a0ecSDave Kleikamp 
2698ac27a0ecSDave Kleikamp 	/*
2699ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2700ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2701ac27a0ecSDave Kleikamp 	 */
2702ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2703617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2704617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2705ac27a0ecSDave Kleikamp 			goto failed_mount3;
2706624080edSTheodore Ts'o 		if (!(sb->s_flags & MS_RDONLY) &&
2707624080edSTheodore Ts'o 		    EXT4_SB(sb)->s_journal->j_failed_commit) {
2708b31e1552SEric Sandeen 			ext4_msg(sb, KERN_CRIT, "error: "
2709624080edSTheodore Ts'o 			       "ext4_fill_super: Journal transaction "
2710b31e1552SEric Sandeen 			       "%u is corrupt",
2711624080edSTheodore Ts'o 			       EXT4_SB(sb)->s_journal->j_failed_commit);
2712624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_RO)) {
2713b31e1552SEric Sandeen 				ext4_msg(sb, KERN_CRIT,
2714b31e1552SEric Sandeen 				       "Mounting filesystem read-only");
2715624080edSTheodore Ts'o 				sb->s_flags |= MS_RDONLY;
2716624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2717624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2718624080edSTheodore Ts'o 			}
2719624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_PANIC)) {
2720624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2721624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2722e2d67052STheodore Ts'o 				ext4_commit_super(sb, 1);
2723624080edSTheodore Ts'o 				goto failed_mount4;
2724624080edSTheodore Ts'o 			}
2725624080edSTheodore Ts'o 		}
27260390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
27270390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2728b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
2729b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
27300390131bSFrank Mayhar 		goto failed_mount4;
2731ac27a0ecSDave Kleikamp 	} else {
27320390131bSFrank Mayhar 		clear_opt(sbi->s_mount_opt, DATA_FLAGS);
27330390131bSFrank Mayhar 		set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
27340390131bSFrank Mayhar 		sbi->s_journal = NULL;
27350390131bSFrank Mayhar 		needs_recovery = 0;
27360390131bSFrank Mayhar 		goto no_journal;
2737ac27a0ecSDave Kleikamp 	}
2738ac27a0ecSDave Kleikamp 
2739eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2740eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2741eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2742b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
2743eb40a09cSJose R. Santos 		goto failed_mount4;
2744eb40a09cSJose R. Santos 	}
2745eb40a09cSJose R. Santos 
2746818d276cSGirish Shilamkar 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2747818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2748818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2749818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2750818d276cSGirish Shilamkar 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2751818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2752818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2753818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2754818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2755818d276cSGirish Shilamkar 	} else {
2756818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal,
2757818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2758818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2759818d276cSGirish Shilamkar 	}
2760818d276cSGirish Shilamkar 
2761ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2762ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2763ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2764ac27a0ecSDave Kleikamp 	case 0:
2765ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
276663f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
276763f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
276863f57933SAndrew Morton 		 */
2769dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2770dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2771ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2772ac27a0ecSDave Kleikamp 		else
2773ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2774ac27a0ecSDave Kleikamp 		break;
2775ac27a0ecSDave Kleikamp 
2776617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2777617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2778dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2779dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2780b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
2781b31e1552SEric Sandeen 			       "requested data journaling mode");
2782ac27a0ecSDave Kleikamp 			goto failed_mount4;
2783ac27a0ecSDave Kleikamp 		}
2784ac27a0ecSDave Kleikamp 	default:
2785ac27a0ecSDave Kleikamp 		break;
2786ac27a0ecSDave Kleikamp 	}
2787b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
2788ac27a0ecSDave Kleikamp 
27890390131bSFrank Mayhar no_journal:
2790ac27a0ecSDave Kleikamp 
2791ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2792617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2793b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2794b31e1552SEric Sandeen 				"its supported only with writeback mode");
2795ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2796ac27a0ecSDave Kleikamp 		}
2797ac27a0ecSDave Kleikamp 	}
2798ac27a0ecSDave Kleikamp 	/*
2799dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2800ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2801ac27a0ecSDave Kleikamp 	 */
2802ac27a0ecSDave Kleikamp 
28031d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
28041d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2805b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
28061d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2807ac27a0ecSDave Kleikamp 		goto failed_mount4;
2808ac27a0ecSDave Kleikamp 	}
2809ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
28101d1fe1eeSDavid Howells 		iput(root);
2811b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
2812ac27a0ecSDave Kleikamp 		goto failed_mount4;
2813ac27a0ecSDave Kleikamp 	}
28141d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
28151d1fe1eeSDavid Howells 	if (!sb->s_root) {
2816b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
28171d1fe1eeSDavid Howells 		iput(root);
28181d1fe1eeSDavid Howells 		ret = -ENOMEM;
28191d1fe1eeSDavid Howells 		goto failed_mount4;
28201d1fe1eeSDavid Howells 	}
2821ac27a0ecSDave Kleikamp 
2822617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2823ef7f3835SKalpak Shah 
2824ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2825ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2826ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2827ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2828ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2829ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2830ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2831ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2832ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2833ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2834ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2835ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2836ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2837ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2838ef7f3835SKalpak Shah 		}
2839ef7f3835SKalpak Shah 	}
2840ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2841ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2842ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2843ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2844ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2845b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
2846b31e1552SEric Sandeen 			 "available");
2847ef7f3835SKalpak Shah 	}
2848ef7f3835SKalpak Shah 
2849c2774d84SAneesh Kumar K.V 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2850b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2851b31e1552SEric Sandeen 			 "requested data journaling mode");
2852c2774d84SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
2853c2774d84SAneesh Kumar K.V 	} else if (test_opt(sb, DELALLOC))
2854b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "delayed allocation enabled");
2855c2774d84SAneesh Kumar K.V 
28566fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
28576fd058f7STheodore Ts'o 	if (err) {
2858b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
28596fd058f7STheodore Ts'o 			 "zone (%d)\n", err);
28606fd058f7STheodore Ts'o 		goto failed_mount4;
28616fd058f7STheodore Ts'o 	}
28626fd058f7STheodore Ts'o 
2863c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
2864c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
2865c2774d84SAneesh Kumar K.V 	if (err) {
2866b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2867c2774d84SAneesh Kumar K.V 			 err);
2868c2774d84SAneesh Kumar K.V 		goto failed_mount4;
2869c2774d84SAneesh Kumar K.V 	}
2870c2774d84SAneesh Kumar K.V 
28713197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
28723197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
28733197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
28743197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
28753197ebdbSTheodore Ts'o 	if (err) {
28763197ebdbSTheodore Ts'o 		ext4_mb_release(sb);
28773197ebdbSTheodore Ts'o 		ext4_ext_release(sb);
28783197ebdbSTheodore Ts'o 		goto failed_mount4;
28793197ebdbSTheodore Ts'o 	};
28803197ebdbSTheodore Ts'o 
2881617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2882617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2883617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
28840390131bSFrank Mayhar 	if (needs_recovery) {
2885b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
2886617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
28870390131bSFrank Mayhar 	}
28880390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
28890390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
28900390131bSFrank Mayhar 			descr = " journalled data mode";
28910390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
28920390131bSFrank Mayhar 			descr = " ordered data mode";
28930390131bSFrank Mayhar 		else
28940390131bSFrank Mayhar 			descr = " writeback data mode";
28950390131bSFrank Mayhar 	} else
28960390131bSFrank Mayhar 		descr = "out journal";
28970390131bSFrank Mayhar 
2898b31e1552SEric Sandeen 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
2899ac27a0ecSDave Kleikamp 
2900ac27a0ecSDave Kleikamp 	lock_kernel();
2901ac27a0ecSDave Kleikamp 	return 0;
2902ac27a0ecSDave Kleikamp 
2903617ba13bSMingming Cao cantfind_ext4:
2904ac27a0ecSDave Kleikamp 	if (!silent)
2905b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
2906ac27a0ecSDave Kleikamp 	goto failed_mount;
2907ac27a0ecSDave Kleikamp 
2908ac27a0ecSDave Kleikamp failed_mount4:
2909b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
29106fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
29110390131bSFrank Mayhar 	if (sbi->s_journal) {
2912dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
291347b4a50bSJan Kara 		sbi->s_journal = NULL;
29140390131bSFrank Mayhar 	}
2915ac27a0ecSDave Kleikamp failed_mount3:
2916c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups) {
2917c5ca7c76STheodore Ts'o 		if (is_vmalloc_addr(sbi->s_flex_groups))
2918c5ca7c76STheodore Ts'o 			vfree(sbi->s_flex_groups);
2919c5ca7c76STheodore Ts'o 		else
2920c5ca7c76STheodore Ts'o 			kfree(sbi->s_flex_groups);
2921c5ca7c76STheodore Ts'o 	}
2922ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2923ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2924ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
29256bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2926ac27a0ecSDave Kleikamp failed_mount2:
2927ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2928ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2929ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2930ac27a0ecSDave Kleikamp failed_mount:
2931240799cdSTheodore Ts'o 	if (sbi->s_proc) {
29329f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
2933240799cdSTheodore Ts'o 	}
2934ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2935ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2936ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2937ac27a0ecSDave Kleikamp #endif
2938617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2939ac27a0ecSDave Kleikamp 	brelse(bh);
2940ac27a0ecSDave Kleikamp out_fail:
2941ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2942f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
2943ac27a0ecSDave Kleikamp 	kfree(sbi);
2944ac27a0ecSDave Kleikamp 	lock_kernel();
29451d1fe1eeSDavid Howells 	return ret;
2946ac27a0ecSDave Kleikamp }
2947ac27a0ecSDave Kleikamp 
2948ac27a0ecSDave Kleikamp /*
2949ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2950ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2951ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2952ac27a0ecSDave Kleikamp  */
2953617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2954ac27a0ecSDave Kleikamp {
2955617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2956ac27a0ecSDave Kleikamp 
2957ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
295830773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
295930773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
2960ac27a0ecSDave Kleikamp 
2961ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2962ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2963dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2964ac27a0ecSDave Kleikamp 	else
2965dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
29665bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
29675bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
29685bf5683aSHidehiro Kawai 	else
29695bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2970ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2971ac27a0ecSDave Kleikamp }
2972ac27a0ecSDave Kleikamp 
2973617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2974ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2975ac27a0ecSDave Kleikamp {
2976ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2977ac27a0ecSDave Kleikamp 	journal_t *journal;
2978ac27a0ecSDave Kleikamp 
29790390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
29800390131bSFrank Mayhar 
2981ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2982ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2983ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2984ac27a0ecSDave Kleikamp 
29851d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
29861d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
2987b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
2988ac27a0ecSDave Kleikamp 		return NULL;
2989ac27a0ecSDave Kleikamp 	}
2990ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2991ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2992ac27a0ecSDave Kleikamp 		iput(journal_inode);
2993b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
2994ac27a0ecSDave Kleikamp 		return NULL;
2995ac27a0ecSDave Kleikamp 	}
2996ac27a0ecSDave Kleikamp 
2997e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2998ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
29991d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
3000b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
3001ac27a0ecSDave Kleikamp 		iput(journal_inode);
3002ac27a0ecSDave Kleikamp 		return NULL;
3003ac27a0ecSDave Kleikamp 	}
3004ac27a0ecSDave Kleikamp 
3005dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
3006ac27a0ecSDave Kleikamp 	if (!journal) {
3007b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3008ac27a0ecSDave Kleikamp 		iput(journal_inode);
3009ac27a0ecSDave Kleikamp 		return NULL;
3010ac27a0ecSDave Kleikamp 	}
3011ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3012617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3013ac27a0ecSDave Kleikamp 	return journal;
3014ac27a0ecSDave Kleikamp }
3015ac27a0ecSDave Kleikamp 
3016617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
3017ac27a0ecSDave Kleikamp 				       dev_t j_dev)
3018ac27a0ecSDave Kleikamp {
3019ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3020ac27a0ecSDave Kleikamp 	journal_t *journal;
3021617ba13bSMingming Cao 	ext4_fsblk_t start;
3022617ba13bSMingming Cao 	ext4_fsblk_t len;
3023ac27a0ecSDave Kleikamp 	int hblock, blocksize;
3024617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
3025ac27a0ecSDave Kleikamp 	unsigned long offset;
3026617ba13bSMingming Cao 	struct ext4_super_block *es;
3027ac27a0ecSDave Kleikamp 	struct block_device *bdev;
3028ac27a0ecSDave Kleikamp 
30290390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
30300390131bSFrank Mayhar 
3031b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
3032ac27a0ecSDave Kleikamp 	if (bdev == NULL)
3033ac27a0ecSDave Kleikamp 		return NULL;
3034ac27a0ecSDave Kleikamp 
3035ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
3036b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3037b31e1552SEric Sandeen 			"failed to claim external journal device");
30389a1c3542SAl Viro 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
3039ac27a0ecSDave Kleikamp 		return NULL;
3040ac27a0ecSDave Kleikamp 	}
3041ac27a0ecSDave Kleikamp 
3042ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
3043e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
3044ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
3045b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3046b31e1552SEric Sandeen 			"blocksize too small for journal device");
3047ac27a0ecSDave Kleikamp 		goto out_bdev;
3048ac27a0ecSDave Kleikamp 	}
3049ac27a0ecSDave Kleikamp 
3050617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3051617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3052ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3053ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3054b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3055b31e1552SEric Sandeen 		       "external journal");
3056ac27a0ecSDave Kleikamp 		goto out_bdev;
3057ac27a0ecSDave Kleikamp 	}
3058ac27a0ecSDave Kleikamp 
3059617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3060617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3061ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3062617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3063b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
3064b31e1552SEric Sandeen 					"bad superblock");
3065ac27a0ecSDave Kleikamp 		brelse(bh);
3066ac27a0ecSDave Kleikamp 		goto out_bdev;
3067ac27a0ecSDave Kleikamp 	}
3068ac27a0ecSDave Kleikamp 
3069617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3070b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3071ac27a0ecSDave Kleikamp 		brelse(bh);
3072ac27a0ecSDave Kleikamp 		goto out_bdev;
3073ac27a0ecSDave Kleikamp 	}
3074ac27a0ecSDave Kleikamp 
3075bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
3076ac27a0ecSDave Kleikamp 	start = sb_block + 1;
3077ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
3078ac27a0ecSDave Kleikamp 
3079dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3080ac27a0ecSDave Kleikamp 					start, len, blocksize);
3081ac27a0ecSDave Kleikamp 	if (!journal) {
3082b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
3083ac27a0ecSDave Kleikamp 		goto out_bdev;
3084ac27a0ecSDave Kleikamp 	}
3085ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3086ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
3087ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
3088ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
3089b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3090ac27a0ecSDave Kleikamp 		goto out_journal;
3091ac27a0ecSDave Kleikamp 	}
3092ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3093b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
3094b31e1552SEric Sandeen 					"user (unsupported) - %d",
3095ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
3096ac27a0ecSDave Kleikamp 		goto out_journal;
3097ac27a0ecSDave Kleikamp 	}
3098617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
3099617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3100ac27a0ecSDave Kleikamp 	return journal;
31010b8e58a1SAndreas Dilger 
3102ac27a0ecSDave Kleikamp out_journal:
3103dab291afSMingming Cao 	jbd2_journal_destroy(journal);
3104ac27a0ecSDave Kleikamp out_bdev:
3105617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
3106ac27a0ecSDave Kleikamp 	return NULL;
3107ac27a0ecSDave Kleikamp }
3108ac27a0ecSDave Kleikamp 
3109617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
3110617ba13bSMingming Cao 			     struct ext4_super_block *es,
3111ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
3112ac27a0ecSDave Kleikamp {
3113ac27a0ecSDave Kleikamp 	journal_t *journal;
3114ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3115ac27a0ecSDave Kleikamp 	dev_t journal_dev;
3116ac27a0ecSDave Kleikamp 	int err = 0;
3117ac27a0ecSDave Kleikamp 	int really_read_only;
3118ac27a0ecSDave Kleikamp 
31190390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
31200390131bSFrank Mayhar 
3121ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3122ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3123b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3124b31e1552SEric Sandeen 			"numbers have changed");
3125ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
3126ac27a0ecSDave Kleikamp 	} else
3127ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3128ac27a0ecSDave Kleikamp 
3129ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
3130ac27a0ecSDave Kleikamp 
3131ac27a0ecSDave Kleikamp 	/*
3132ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
3133ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
3134ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
3135ac27a0ecSDave Kleikamp 	 */
3136617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3137ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
3138b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
3139b31e1552SEric Sandeen 					"required on readonly filesystem");
3140ac27a0ecSDave Kleikamp 			if (really_read_only) {
3141b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
3142b31e1552SEric Sandeen 					"unavailable, cannot proceed");
3143ac27a0ecSDave Kleikamp 				return -EROFS;
3144ac27a0ecSDave Kleikamp 			}
3145b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
3146b31e1552SEric Sandeen 			       "be enabled during recovery");
3147ac27a0ecSDave Kleikamp 		}
3148ac27a0ecSDave Kleikamp 	}
3149ac27a0ecSDave Kleikamp 
3150ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
3151b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3152b31e1552SEric Sandeen 		       "and inode journals!");
3153ac27a0ecSDave Kleikamp 		return -EINVAL;
3154ac27a0ecSDave Kleikamp 	}
3155ac27a0ecSDave Kleikamp 
3156ac27a0ecSDave Kleikamp 	if (journal_inum) {
3157617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
3158ac27a0ecSDave Kleikamp 			return -EINVAL;
3159ac27a0ecSDave Kleikamp 	} else {
3160617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3161ac27a0ecSDave Kleikamp 			return -EINVAL;
3162ac27a0ecSDave Kleikamp 	}
3163ac27a0ecSDave Kleikamp 
31644776004fSTheodore Ts'o 	if (journal->j_flags & JBD2_BARRIER)
3165b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers enabled");
31664776004fSTheodore Ts'o 	else
3167b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
31684776004fSTheodore Ts'o 
3169ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3170dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
3171ac27a0ecSDave Kleikamp 		if (err)  {
3172b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error updating journal");
3173dab291afSMingming Cao 			jbd2_journal_destroy(journal);
3174ac27a0ecSDave Kleikamp 			return err;
3175ac27a0ecSDave Kleikamp 		}
3176ac27a0ecSDave Kleikamp 	}
3177ac27a0ecSDave Kleikamp 
3178617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3179dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
3180ac27a0ecSDave Kleikamp 	if (!err)
3181dab291afSMingming Cao 		err = jbd2_journal_load(journal);
3182ac27a0ecSDave Kleikamp 
3183ac27a0ecSDave Kleikamp 	if (err) {
3184b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
3185dab291afSMingming Cao 		jbd2_journal_destroy(journal);
3186ac27a0ecSDave Kleikamp 		return err;
3187ac27a0ecSDave Kleikamp 	}
3188ac27a0ecSDave Kleikamp 
3189617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
3190617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
3191ac27a0ecSDave Kleikamp 
3192ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3193ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3194ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
3195ac27a0ecSDave Kleikamp 
3196ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
3197e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3198ac27a0ecSDave Kleikamp 	}
3199ac27a0ecSDave Kleikamp 
3200ac27a0ecSDave Kleikamp 	return 0;
3201ac27a0ecSDave Kleikamp }
3202ac27a0ecSDave Kleikamp 
3203e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
3204ac27a0ecSDave Kleikamp {
3205e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3206617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3207c4be0c1dSTakashi Sato 	int error = 0;
3208ac27a0ecSDave Kleikamp 
3209ac27a0ecSDave Kleikamp 	if (!sbh)
3210c4be0c1dSTakashi Sato 		return error;
3211914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
3212914258bfSTheodore Ts'o 		/*
3213914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
3214914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
3215914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
3216914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
3217914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
3218914258bfSTheodore Ts'o 		 * write and hope for the best.
3219914258bfSTheodore Ts'o 		 */
3220b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
3221b31e1552SEric Sandeen 		       "superblock detected");
3222914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
3223914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
3224914258bfSTheodore Ts'o 	}
3225ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
3226afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
3227afc32f7eSTheodore Ts'o 		cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3228afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3229afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
32305d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
32315d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeblocks_counter));
32325d1b1b3fSAneesh Kumar K.V 	es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
32335d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeinodes_counter));
32347234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
3235ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
3236ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
3237914258bfSTheodore Ts'o 	if (sync) {
3238c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
3239c4be0c1dSTakashi Sato 		if (error)
3240c4be0c1dSTakashi Sato 			return error;
3241c4be0c1dSTakashi Sato 
3242c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
3243c4be0c1dSTakashi Sato 		if (error) {
3244b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
3245b31e1552SEric Sandeen 			       "superblock");
3246914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
3247914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
3248914258bfSTheodore Ts'o 		}
3249914258bfSTheodore Ts'o 	}
3250c4be0c1dSTakashi Sato 	return error;
3251ac27a0ecSDave Kleikamp }
3252ac27a0ecSDave Kleikamp 
3253ac27a0ecSDave Kleikamp /*
3254ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
3255ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
3256ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
3257ac27a0ecSDave Kleikamp  */
3258617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
3259617ba13bSMingming Cao 					struct ext4_super_block *es)
3260ac27a0ecSDave Kleikamp {
3261617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
3262ac27a0ecSDave Kleikamp 
32630390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
32640390131bSFrank Mayhar 		BUG_ON(journal != NULL);
32650390131bSFrank Mayhar 		return;
32660390131bSFrank Mayhar 	}
3267dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
32687ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
32697ffe1ea8SHidehiro Kawai 		goto out;
32707ffe1ea8SHidehiro Kawai 
3271617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3272ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
3273617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3274e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3275ac27a0ecSDave Kleikamp 	}
32767ffe1ea8SHidehiro Kawai 
32777ffe1ea8SHidehiro Kawai out:
3278dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
3279ac27a0ecSDave Kleikamp }
3280ac27a0ecSDave Kleikamp 
3281ac27a0ecSDave Kleikamp /*
3282ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
3283ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
3284ac27a0ecSDave Kleikamp  * main filesystem now.
3285ac27a0ecSDave Kleikamp  */
3286617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
3287617ba13bSMingming Cao 				   struct ext4_super_block *es)
3288ac27a0ecSDave Kleikamp {
3289ac27a0ecSDave Kleikamp 	journal_t *journal;
3290ac27a0ecSDave Kleikamp 	int j_errno;
3291ac27a0ecSDave Kleikamp 	const char *errstr;
3292ac27a0ecSDave Kleikamp 
32930390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
32940390131bSFrank Mayhar 
3295617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
3296ac27a0ecSDave Kleikamp 
3297ac27a0ecSDave Kleikamp 	/*
3298ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
3299617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
3300ac27a0ecSDave Kleikamp 	 */
3301ac27a0ecSDave Kleikamp 
3302dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
3303ac27a0ecSDave Kleikamp 	if (j_errno) {
3304ac27a0ecSDave Kleikamp 		char nbuf[16];
3305ac27a0ecSDave Kleikamp 
3306617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
330746e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Filesystem error recorded "
3308ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
330946e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Marking fs in need of "
3310ac27a0ecSDave Kleikamp 			     "filesystem check.");
3311ac27a0ecSDave Kleikamp 
3312617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3313617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3314e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3315ac27a0ecSDave Kleikamp 
3316dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
3317ac27a0ecSDave Kleikamp 	}
3318ac27a0ecSDave Kleikamp }
3319ac27a0ecSDave Kleikamp 
3320ac27a0ecSDave Kleikamp /*
3321ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
3322ac27a0ecSDave Kleikamp  * and wait on the commit.
3323ac27a0ecSDave Kleikamp  */
3324617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
3325ac27a0ecSDave Kleikamp {
3326ac27a0ecSDave Kleikamp 	journal_t *journal;
33270390131bSFrank Mayhar 	int ret = 0;
3328ac27a0ecSDave Kleikamp 
3329ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
3330ac27a0ecSDave Kleikamp 		return 0;
3331ac27a0ecSDave Kleikamp 
3332617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
33337234ab2aSTheodore Ts'o 	if (journal)
3334617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
33350390131bSFrank Mayhar 
3336ac27a0ecSDave Kleikamp 	return ret;
3337ac27a0ecSDave Kleikamp }
3338ac27a0ecSDave Kleikamp 
3339617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
3340ac27a0ecSDave Kleikamp {
3341ebc1ac16SChristoph Hellwig 	lock_super(sb);
3342e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3343ebc1ac16SChristoph Hellwig 	unlock_super(sb);
33440390131bSFrank Mayhar }
3345ac27a0ecSDave Kleikamp 
3346617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
3347ac27a0ecSDave Kleikamp {
334814ce0cb4STheodore Ts'o 	int ret = 0;
33499eddacf9SJan Kara 	tid_t target;
3350ac27a0ecSDave Kleikamp 
33519bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
33529ca92389STheodore Ts'o 	if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
3353ac27a0ecSDave Kleikamp 		if (wait)
33549ca92389STheodore Ts'o 			jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
33550390131bSFrank Mayhar 	}
335614ce0cb4STheodore Ts'o 	return ret;
3357ac27a0ecSDave Kleikamp }
3358ac27a0ecSDave Kleikamp 
3359ac27a0ecSDave Kleikamp /*
3360ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
3361ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
3362ac27a0ecSDave Kleikamp  */
3363c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
3364ac27a0ecSDave Kleikamp {
3365c4be0c1dSTakashi Sato 	int error = 0;
3366c4be0c1dSTakashi Sato 	journal_t *journal;
3367ac27a0ecSDave Kleikamp 
33689ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
33699ca92389STheodore Ts'o 		return 0;
33709ca92389STheodore Ts'o 
3371c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
3372ac27a0ecSDave Kleikamp 
3373ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
3374dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
33757ffe1ea8SHidehiro Kawai 
33767ffe1ea8SHidehiro Kawai 	/*
33779ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
33789ca92389STheodore Ts'o 	 * the journal.
33797ffe1ea8SHidehiro Kawai 	 */
3380c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
33819ca92389STheodore Ts'o 	if (error < 0) {
33829ca92389STheodore Ts'o 	out:
33839ca92389STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
33849ca92389STheodore Ts'o 		return error;
33850390131bSFrank Mayhar 	}
3386ac27a0ecSDave Kleikamp 
3387ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
3388617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3389e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
3390c4be0c1dSTakashi Sato 	if (error)
3391c4be0c1dSTakashi Sato 		goto out;
3392c4be0c1dSTakashi Sato 	return 0;
3393ac27a0ecSDave Kleikamp }
3394ac27a0ecSDave Kleikamp 
3395ac27a0ecSDave Kleikamp /*
3396ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
3397ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
3398ac27a0ecSDave Kleikamp  */
3399c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
3400ac27a0ecSDave Kleikamp {
34019ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
34029ca92389STheodore Ts'o 		return 0;
34039ca92389STheodore Ts'o 
3404ac27a0ecSDave Kleikamp 	lock_super(sb);
34059ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
3406617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3407e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3408ac27a0ecSDave Kleikamp 	unlock_super(sb);
3409dab291afSMingming Cao 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3410c4be0c1dSTakashi Sato 	return 0;
3411ac27a0ecSDave Kleikamp }
3412ac27a0ecSDave Kleikamp 
3413617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
3414ac27a0ecSDave Kleikamp {
3415617ba13bSMingming Cao 	struct ext4_super_block *es;
3416617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3417617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
3418ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
3419617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
34208a266467STheodore Ts'o 	ext4_group_t g;
3421b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3422ac27a0ecSDave Kleikamp 	int err;
3423ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3424ac27a0ecSDave Kleikamp 	int i;
3425ac27a0ecSDave Kleikamp #endif
3426ac27a0ecSDave Kleikamp 
3427337eb00aSAlessio Igor Bogani 	lock_kernel();
3428337eb00aSAlessio Igor Bogani 
3429ac27a0ecSDave Kleikamp 	/* Store the original options */
3430bbd6851aSAl Viro 	lock_super(sb);
3431ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
3432ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
3433ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
3434ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
3435ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
343630773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
343730773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
3438ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3439ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3440ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3441ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3442ac27a0ecSDave Kleikamp #endif
3443b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3444b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
3445ac27a0ecSDave Kleikamp 
3446ac27a0ecSDave Kleikamp 	/*
3447ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
3448ac27a0ecSDave Kleikamp 	 */
3449b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
3450b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
3451ac27a0ecSDave Kleikamp 		err = -EINVAL;
3452ac27a0ecSDave Kleikamp 		goto restore_opts;
3453ac27a0ecSDave Kleikamp 	}
3454ac27a0ecSDave Kleikamp 
3455*4ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
345646e665e9SHarvey Harrison 		ext4_abort(sb, __func__, "Abort forced by user");
3457ac27a0ecSDave Kleikamp 
3458ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3459617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3460ac27a0ecSDave Kleikamp 
3461ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3462ac27a0ecSDave Kleikamp 
3463b3881f74STheodore Ts'o 	if (sbi->s_journal) {
3464617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
3465b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3466b3881f74STheodore Ts'o 	}
3467ac27a0ecSDave Kleikamp 
3468ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3469bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
3470*4ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
3471ac27a0ecSDave Kleikamp 			err = -EROFS;
3472ac27a0ecSDave Kleikamp 			goto restore_opts;
3473ac27a0ecSDave Kleikamp 		}
3474ac27a0ecSDave Kleikamp 
3475ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
3476ac27a0ecSDave Kleikamp 			/*
3477ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3478ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3479ac27a0ecSDave Kleikamp 			 */
3480ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3481ac27a0ecSDave Kleikamp 
3482ac27a0ecSDave Kleikamp 			/*
3483ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3484ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3485ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3486ac27a0ecSDave Kleikamp 			 */
3487617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3488617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3489ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3490ac27a0ecSDave Kleikamp 
3491a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
3492617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
3493ac27a0ecSDave Kleikamp 		} else {
34943a06d778SAneesh Kumar K.V 			int ret;
3495617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3496617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3497b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "couldn't "
3498ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
3499b31e1552SEric Sandeen 				       "optional features (%x)",
35003a06d778SAneesh Kumar K.V 				(le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
35013a06d778SAneesh Kumar K.V 					~EXT4_FEATURE_RO_COMPAT_SUPP));
3502ac27a0ecSDave Kleikamp 				err = -EROFS;
3503ac27a0ecSDave Kleikamp 				goto restore_opts;
3504ac27a0ecSDave Kleikamp 			}
3505ead6596bSEric Sandeen 
3506ead6596bSEric Sandeen 			/*
35078a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
35080b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
35098a266467STheodore Ts'o 			 */
35108a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
35118a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
35128a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
35138a266467STheodore Ts'o 
35148a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3515b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
3516b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
35178a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
35188a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
35198a266467STheodore Ts'o 					err = -EINVAL;
35208a266467STheodore Ts'o 					goto restore_opts;
35218a266467STheodore Ts'o 				}
35228a266467STheodore Ts'o 			}
35238a266467STheodore Ts'o 
35248a266467STheodore Ts'o 			/*
3525ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3526ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3527ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3528ead6596bSEric Sandeen 			 */
3529ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3530b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
3531ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3532ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3533b31e1552SEric Sandeen 				       "umount/remount instead");
3534ead6596bSEric Sandeen 				err = -EINVAL;
3535ead6596bSEric Sandeen 				goto restore_opts;
3536ead6596bSEric Sandeen 			}
3537ead6596bSEric Sandeen 
3538ac27a0ecSDave Kleikamp 			/*
3539ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3540ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3541ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3542ac27a0ecSDave Kleikamp 			 * the partition.)
3543ac27a0ecSDave Kleikamp 			 */
35440390131bSFrank Mayhar 			if (sbi->s_journal)
3545617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
3546ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3547617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3548ac27a0ecSDave Kleikamp 				goto restore_opts;
3549617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3550ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3551ac27a0ecSDave Kleikamp 		}
3552ac27a0ecSDave Kleikamp 	}
35536fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
35540390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
3555e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
35560390131bSFrank Mayhar 
3557ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3558ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3559ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3560ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3561ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3562ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3563ac27a0ecSDave Kleikamp #endif
3564bbd6851aSAl Viro 	unlock_super(sb);
3565337eb00aSAlessio Igor Bogani 	unlock_kernel();
3566ac27a0ecSDave Kleikamp 	return 0;
35670b8e58a1SAndreas Dilger 
3568ac27a0ecSDave Kleikamp restore_opts:
3569ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3570ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3571ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3572ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3573ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
357430773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
357530773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
3576ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3577ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3578ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3579ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3580ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3581ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3582ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3583ac27a0ecSDave Kleikamp 	}
3584ac27a0ecSDave Kleikamp #endif
3585bbd6851aSAl Viro 	unlock_super(sb);
3586337eb00aSAlessio Igor Bogani 	unlock_kernel();
3587ac27a0ecSDave Kleikamp 	return err;
3588ac27a0ecSDave Kleikamp }
3589ac27a0ecSDave Kleikamp 
3590617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3591ac27a0ecSDave Kleikamp {
3592ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3593617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3594617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3595960cc398SPekka Enberg 	u64 fsid;
3596ac27a0ecSDave Kleikamp 
35975e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
35985e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
35996bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
36008df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
36015e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3602ac27a0ecSDave Kleikamp 
3603ac27a0ecSDave Kleikamp 		/*
36045e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
36055e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
36065e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3607ac27a0ecSDave Kleikamp 		 */
3608ac27a0ecSDave Kleikamp 
3609ac27a0ecSDave Kleikamp 		/*
3610ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3611ac27a0ecSDave Kleikamp 		 * overhead
3612ac27a0ecSDave Kleikamp 		 */
3613ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3614ac27a0ecSDave Kleikamp 
3615ac27a0ecSDave Kleikamp 		/*
3616ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3617ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3618ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3619ac27a0ecSDave Kleikamp 		 */
3620ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3621617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3622617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3623ac27a0ecSDave Kleikamp 			cond_resched();
3624ac27a0ecSDave Kleikamp 		}
3625ac27a0ecSDave Kleikamp 
3626ac27a0ecSDave Kleikamp 		/*
3627ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3628ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3629ac27a0ecSDave Kleikamp 		 */
36305e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
36315e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
36325e70030dSBadari Pulavarty 		smp_wmb();
36336bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3634ac27a0ecSDave Kleikamp 	}
3635ac27a0ecSDave Kleikamp 
3636617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3637ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
36385e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
36396bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
36406bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3641308ba3ecSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, buf->f_bfree);
3642bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3643bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3644ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3645ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
364652d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
36475e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3648617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3649960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3650960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3651960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3652960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
36530b8e58a1SAndreas Dilger 
3654ac27a0ecSDave Kleikamp 	return 0;
3655ac27a0ecSDave Kleikamp }
3656ac27a0ecSDave Kleikamp 
36570b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
36580b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
3659ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3660617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3661dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
3662a269eb18SJan Kara  *   vfs_dq_init()                         down(dqio_mutex)
3663dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3664ac27a0ecSDave Kleikamp  *
3665ac27a0ecSDave Kleikamp  */
3666ac27a0ecSDave Kleikamp 
3667ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3668ac27a0ecSDave Kleikamp 
3669ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3670ac27a0ecSDave Kleikamp {
3671ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3672ac27a0ecSDave Kleikamp }
3673ac27a0ecSDave Kleikamp 
3674617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3675ac27a0ecSDave Kleikamp {
3676ac27a0ecSDave Kleikamp 	int ret, err;
3677ac27a0ecSDave Kleikamp 	handle_t *handle;
3678ac27a0ecSDave Kleikamp 	struct inode *inode;
3679ac27a0ecSDave Kleikamp 
3680ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3681617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3682617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3683ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3684ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3685ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3686617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3687ac27a0ecSDave Kleikamp 	if (!ret)
3688ac27a0ecSDave Kleikamp 		ret = err;
3689ac27a0ecSDave Kleikamp 	return ret;
3690ac27a0ecSDave Kleikamp }
3691ac27a0ecSDave Kleikamp 
3692617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3693ac27a0ecSDave Kleikamp {
3694ac27a0ecSDave Kleikamp 	int ret, err;
3695ac27a0ecSDave Kleikamp 	handle_t *handle;
3696ac27a0ecSDave Kleikamp 
3697617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3698617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3699ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3700ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3701ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3702617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3703ac27a0ecSDave Kleikamp 	if (!ret)
3704ac27a0ecSDave Kleikamp 		ret = err;
3705ac27a0ecSDave Kleikamp 	return ret;
3706ac27a0ecSDave Kleikamp }
3707ac27a0ecSDave Kleikamp 
3708617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3709ac27a0ecSDave Kleikamp {
3710ac27a0ecSDave Kleikamp 	int ret, err;
3711ac27a0ecSDave Kleikamp 	handle_t *handle;
3712ac27a0ecSDave Kleikamp 
3713617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3714617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
37159c3013e9SJan Kara 	if (IS_ERR(handle)) {
37169c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
37179c3013e9SJan Kara 		dquot_release(dquot);
3718ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
37199c3013e9SJan Kara 	}
3720ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3721617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3722ac27a0ecSDave Kleikamp 	if (!ret)
3723ac27a0ecSDave Kleikamp 		ret = err;
3724ac27a0ecSDave Kleikamp 	return ret;
3725ac27a0ecSDave Kleikamp }
3726ac27a0ecSDave Kleikamp 
3727617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3728ac27a0ecSDave Kleikamp {
37292c8be6b2SJan Kara 	/* Are we journaling quotas? */
3730617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3731617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3732ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3733617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3734ac27a0ecSDave Kleikamp 	} else {
3735ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3736ac27a0ecSDave Kleikamp 	}
3737ac27a0ecSDave Kleikamp }
3738ac27a0ecSDave Kleikamp 
3739617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3740ac27a0ecSDave Kleikamp {
3741ac27a0ecSDave Kleikamp 	int ret, err;
3742ac27a0ecSDave Kleikamp 	handle_t *handle;
3743ac27a0ecSDave Kleikamp 
3744ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3745617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3746ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3747ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3748ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3749617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3750ac27a0ecSDave Kleikamp 	if (!ret)
3751ac27a0ecSDave Kleikamp 		ret = err;
3752ac27a0ecSDave Kleikamp 	return ret;
3753ac27a0ecSDave Kleikamp }
3754ac27a0ecSDave Kleikamp 
3755ac27a0ecSDave Kleikamp /*
3756ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3757ac27a0ecSDave Kleikamp  * the quota file and such...
3758ac27a0ecSDave Kleikamp  */
3759617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3760ac27a0ecSDave Kleikamp {
3761617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3762617ba13bSMingming Cao 				  EXT4_SB(sb)->s_jquota_fmt, type);
3763ac27a0ecSDave Kleikamp }
3764ac27a0ecSDave Kleikamp 
3765ac27a0ecSDave Kleikamp /*
3766ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3767ac27a0ecSDave Kleikamp  */
3768617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
37698264613dSAl Viro 			 char *name, int remount)
3770ac27a0ecSDave Kleikamp {
3771ac27a0ecSDave Kleikamp 	int err;
37728264613dSAl Viro 	struct path path;
3773ac27a0ecSDave Kleikamp 
3774ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3775ac27a0ecSDave Kleikamp 		return -EINVAL;
37768264613dSAl Viro 	/* When remounting, no checks are needed and in fact, name is NULL */
37770623543bSJan Kara 	if (remount)
37788264613dSAl Viro 		return vfs_quota_on(sb, type, format_id, name, remount);
37790623543bSJan Kara 
37808264613dSAl Viro 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3781ac27a0ecSDave Kleikamp 	if (err)
3782ac27a0ecSDave Kleikamp 		return err;
37830623543bSJan Kara 
3784ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
37858264613dSAl Viro 	if (path.mnt->mnt_sb != sb) {
37868264613dSAl Viro 		path_put(&path);
3787ac27a0ecSDave Kleikamp 		return -EXDEV;
3788ac27a0ecSDave Kleikamp 	}
37890623543bSJan Kara 	/* Journaling quota? */
37900623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
37912b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
37928264613dSAl Viro 		if (path.dentry->d_parent != sb->s_root)
3793b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
3794b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
3795b31e1552SEric Sandeen 				"Journaled quota will not work");
37960623543bSJan Kara 	}
37970623543bSJan Kara 
37980623543bSJan Kara 	/*
37990623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
38000623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
38010623543bSJan Kara 	 */
38020390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
38030390131bSFrank Mayhar 	    ext4_should_journal_data(path.dentry->d_inode)) {
38040623543bSJan Kara 		/*
38050623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
38060623543bSJan Kara 		 * otherwise be livelocked...
38070623543bSJan Kara 		 */
38080623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
38097ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
38100623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
38117ffe1ea8SHidehiro Kawai 		if (err) {
38128264613dSAl Viro 			path_put(&path);
38137ffe1ea8SHidehiro Kawai 			return err;
38147ffe1ea8SHidehiro Kawai 		}
38150623543bSJan Kara 	}
38160623543bSJan Kara 
38178264613dSAl Viro 	err = vfs_quota_on_path(sb, type, format_id, &path);
38188264613dSAl Viro 	path_put(&path);
381977e69dacSAl Viro 	return err;
3820ac27a0ecSDave Kleikamp }
3821ac27a0ecSDave Kleikamp 
3822ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3823ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3824ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3825ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3826617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3827ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3828ac27a0ecSDave Kleikamp {
3829ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3830725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3831ac27a0ecSDave Kleikamp 	int err = 0;
3832ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3833ac27a0ecSDave Kleikamp 	int tocopy;
3834ac27a0ecSDave Kleikamp 	size_t toread;
3835ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3836ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3837ac27a0ecSDave Kleikamp 
3838ac27a0ecSDave Kleikamp 	if (off > i_size)
3839ac27a0ecSDave Kleikamp 		return 0;
3840ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3841ac27a0ecSDave Kleikamp 		len = i_size-off;
3842ac27a0ecSDave Kleikamp 	toread = len;
3843ac27a0ecSDave Kleikamp 	while (toread > 0) {
3844ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3845ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3846617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3847ac27a0ecSDave Kleikamp 		if (err)
3848ac27a0ecSDave Kleikamp 			return err;
3849ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3850ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3851ac27a0ecSDave Kleikamp 		else
3852ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3853ac27a0ecSDave Kleikamp 		brelse(bh);
3854ac27a0ecSDave Kleikamp 		offset = 0;
3855ac27a0ecSDave Kleikamp 		toread -= tocopy;
3856ac27a0ecSDave Kleikamp 		data += tocopy;
3857ac27a0ecSDave Kleikamp 		blk++;
3858ac27a0ecSDave Kleikamp 	}
3859ac27a0ecSDave Kleikamp 	return len;
3860ac27a0ecSDave Kleikamp }
3861ac27a0ecSDave Kleikamp 
3862ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3863ac27a0ecSDave Kleikamp  * enough credits) */
3864617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3865ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3866ac27a0ecSDave Kleikamp {
3867ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3868725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3869ac27a0ecSDave Kleikamp 	int err = 0;
3870ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3871ac27a0ecSDave Kleikamp 	int tocopy;
3872617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3873ac27a0ecSDave Kleikamp 	size_t towrite = len;
3874ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3875ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3876ac27a0ecSDave Kleikamp 
38770390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
3878b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3879b31e1552SEric Sandeen 			" cancelled because transaction is not started",
38809c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
38819c3013e9SJan Kara 		return -EIO;
38829c3013e9SJan Kara 	}
3883ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3884ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3885ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3886ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3887617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3888ac27a0ecSDave Kleikamp 		if (!bh)
3889ac27a0ecSDave Kleikamp 			goto out;
3890ac27a0ecSDave Kleikamp 		if (journal_quota) {
3891617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3892ac27a0ecSDave Kleikamp 			if (err) {
3893ac27a0ecSDave Kleikamp 				brelse(bh);
3894ac27a0ecSDave Kleikamp 				goto out;
3895ac27a0ecSDave Kleikamp 			}
3896ac27a0ecSDave Kleikamp 		}
3897ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3898ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3899ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3900ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3901ac27a0ecSDave Kleikamp 		if (journal_quota)
39020390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle, NULL, bh);
3903ac27a0ecSDave Kleikamp 		else {
3904ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3905678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3906ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3907ac27a0ecSDave Kleikamp 		}
3908ac27a0ecSDave Kleikamp 		brelse(bh);
3909ac27a0ecSDave Kleikamp 		if (err)
3910ac27a0ecSDave Kleikamp 			goto out;
3911ac27a0ecSDave Kleikamp 		offset = 0;
3912ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3913ac27a0ecSDave Kleikamp 		data += tocopy;
3914ac27a0ecSDave Kleikamp 		blk++;
3915ac27a0ecSDave Kleikamp 	}
3916ac27a0ecSDave Kleikamp out:
39174d04e4fbSJan Kara 	if (len == towrite) {
39184d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
3919ac27a0ecSDave Kleikamp 		return err;
39204d04e4fbSJan Kara 	}
3921ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3922ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3923617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3924ac27a0ecSDave Kleikamp 	}
3925ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3926617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3927ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3928ac27a0ecSDave Kleikamp 	return len - towrite;
3929ac27a0ecSDave Kleikamp }
3930ac27a0ecSDave Kleikamp 
3931ac27a0ecSDave Kleikamp #endif
3932ac27a0ecSDave Kleikamp 
39330b8e58a1SAndreas Dilger static int ext4_get_sb(struct file_system_type *fs_type, int flags,
39340b8e58a1SAndreas Dilger 		       const char *dev_name, void *data, struct vfsmount *mnt)
3935ac27a0ecSDave Kleikamp {
3936617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
3937ac27a0ecSDave Kleikamp }
3938ac27a0ecSDave Kleikamp 
393903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
3940ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
394103010a33STheodore Ts'o 	.name		= "ext4",
3942617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
3943ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
3944ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
3945ac27a0ecSDave Kleikamp };
3946ac27a0ecSDave Kleikamp 
394703010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
39480b8e58a1SAndreas Dilger static int ext4dev_get_sb(struct file_system_type *fs_type, int flags,
39490b8e58a1SAndreas Dilger 			  const char *dev_name, void *data,struct vfsmount *mnt)
395003010a33STheodore Ts'o {
3951b31e1552SEric Sandeen 	printk(KERN_WARNING "EXT4-fs (%s): Update your userspace programs "
3952b31e1552SEric Sandeen 	       "to mount using ext4\n", dev_name);
3953b31e1552SEric Sandeen 	printk(KERN_WARNING "EXT4-fs (%s): ext4dev backwards compatibility "
3954b31e1552SEric Sandeen 	       "will go away by 2.6.31\n", dev_name);
395503010a33STheodore Ts'o 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
395603010a33STheodore Ts'o }
395703010a33STheodore Ts'o 
395803010a33STheodore Ts'o static struct file_system_type ext4dev_fs_type = {
395903010a33STheodore Ts'o 	.owner		= THIS_MODULE,
396003010a33STheodore Ts'o 	.name		= "ext4dev",
396103010a33STheodore Ts'o 	.get_sb		= ext4dev_get_sb,
396203010a33STheodore Ts'o 	.kill_sb	= kill_block_super,
396303010a33STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
396403010a33STheodore Ts'o };
396503010a33STheodore Ts'o MODULE_ALIAS("ext4dev");
396603010a33STheodore Ts'o #endif
396703010a33STheodore Ts'o 
3968617ba13bSMingming Cao static int __init init_ext4_fs(void)
3969ac27a0ecSDave Kleikamp {
3970c9de560dSAlex Tomas 	int err;
3971c9de560dSAlex Tomas 
39726fd058f7STheodore Ts'o 	err = init_ext4_system_zone();
39736fd058f7STheodore Ts'o 	if (err)
39746fd058f7STheodore Ts'o 		return err;
39753197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
39763197ebdbSTheodore Ts'o 	if (!ext4_kset)
39776fd058f7STheodore Ts'o 		goto out4;
39789f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3979c9de560dSAlex Tomas 	err = init_ext4_mballoc();
3980ac27a0ecSDave Kleikamp 	if (err)
39816fd058f7STheodore Ts'o 		goto out3;
3982c9de560dSAlex Tomas 
3983c9de560dSAlex Tomas 	err = init_ext4_xattr();
3984c9de560dSAlex Tomas 	if (err)
3985c9de560dSAlex Tomas 		goto out2;
3986ac27a0ecSDave Kleikamp 	err = init_inodecache();
3987ac27a0ecSDave Kleikamp 	if (err)
3988ac27a0ecSDave Kleikamp 		goto out1;
398903010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
3990ac27a0ecSDave Kleikamp 	if (err)
3991ac27a0ecSDave Kleikamp 		goto out;
399203010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
399303010a33STheodore Ts'o 	err = register_filesystem(&ext4dev_fs_type);
399403010a33STheodore Ts'o 	if (err) {
399503010a33STheodore Ts'o 		unregister_filesystem(&ext4_fs_type);
399603010a33STheodore Ts'o 		goto out;
399703010a33STheodore Ts'o 	}
399803010a33STheodore Ts'o #endif
3999ac27a0ecSDave Kleikamp 	return 0;
4000ac27a0ecSDave Kleikamp out:
4001ac27a0ecSDave Kleikamp 	destroy_inodecache();
4002ac27a0ecSDave Kleikamp out1:
4003617ba13bSMingming Cao 	exit_ext4_xattr();
4004c9de560dSAlex Tomas out2:
4005c9de560dSAlex Tomas 	exit_ext4_mballoc();
40066fd058f7STheodore Ts'o out3:
40076fd058f7STheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
40086fd058f7STheodore Ts'o 	kset_unregister(ext4_kset);
40096fd058f7STheodore Ts'o out4:
40106fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4011ac27a0ecSDave Kleikamp 	return err;
4012ac27a0ecSDave Kleikamp }
4013ac27a0ecSDave Kleikamp 
4014617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
4015ac27a0ecSDave Kleikamp {
401603010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
401703010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
4018617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
401903010a33STheodore Ts'o #endif
4020ac27a0ecSDave Kleikamp 	destroy_inodecache();
4021617ba13bSMingming Cao 	exit_ext4_xattr();
4022c9de560dSAlex Tomas 	exit_ext4_mballoc();
40239f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
40243197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
40256fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4026ac27a0ecSDave Kleikamp }
4027ac27a0ecSDave Kleikamp 
4028ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
402983982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
4030ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
4031617ba13bSMingming Cao module_init(init_ext4_fs)
4032617ba13bSMingming Cao module_exit(exit_ext4_fs)
4033