xref: /linux/fs/ext4/super.c (revision 15121c18a22ae483279f76dc9e554334b800d0f7)
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"
483661d286STheodore Ts'o #include "mballoc.h"
49ac27a0ecSDave Kleikamp 
509bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
519bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
529bffad1eSTheodore Ts'o 
539f6200bbSTheodore Ts'o struct proc_dir_entry *ext4_proc_root;
543197ebdbSTheodore Ts'o static struct kset *ext4_kset;
559f6200bbSTheodore Ts'o 
56617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
57ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
58e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
59617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
60617ba13bSMingming Cao 					struct ext4_super_block *es);
61617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
62617ba13bSMingming Cao 				   struct ext4_super_block *es);
63617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
64617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
65ac27a0ecSDave Kleikamp 				     char nbuf[16]);
66617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
67617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
68c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
69617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
70c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
71ac27a0ecSDave Kleikamp 
72bd81d8eeSLaurent Vivier 
738fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
748fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
75bd81d8eeSLaurent Vivier {
763a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
778fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
788fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
79bd81d8eeSLaurent Vivier }
80bd81d8eeSLaurent Vivier 
818fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
828fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
83bd81d8eeSLaurent Vivier {
845272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
858fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
868fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
87bd81d8eeSLaurent Vivier }
88bd81d8eeSLaurent Vivier 
898fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
908fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
91bd81d8eeSLaurent Vivier {
925272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
938fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
948fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
95bd81d8eeSLaurent Vivier }
96bd81d8eeSLaurent Vivier 
97560671a0SAneesh Kumar K.V __u32 ext4_free_blks_count(struct super_block *sb,
98560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
99560671a0SAneesh Kumar K.V {
100560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
101560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
102560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
103560671a0SAneesh Kumar K.V }
104560671a0SAneesh Kumar K.V 
105560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
106560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
107560671a0SAneesh Kumar K.V {
108560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
109560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
110560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
111560671a0SAneesh Kumar K.V }
112560671a0SAneesh Kumar K.V 
113560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
114560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
115560671a0SAneesh Kumar K.V {
116560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
117560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
118560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
119560671a0SAneesh Kumar K.V }
120560671a0SAneesh Kumar K.V 
121560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
122560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
123560671a0SAneesh Kumar K.V {
124560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
125560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
126560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
127560671a0SAneesh Kumar K.V }
128560671a0SAneesh Kumar K.V 
1298fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
1308fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
131bd81d8eeSLaurent Vivier {
1323a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1338fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1348fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
135bd81d8eeSLaurent Vivier }
136bd81d8eeSLaurent Vivier 
1378fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1388fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
139bd81d8eeSLaurent Vivier {
1405272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1418fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1428fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
143bd81d8eeSLaurent Vivier }
144bd81d8eeSLaurent Vivier 
1458fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1468fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
147bd81d8eeSLaurent Vivier {
1485272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1498fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1508fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
151bd81d8eeSLaurent Vivier }
152bd81d8eeSLaurent Vivier 
153560671a0SAneesh Kumar K.V void ext4_free_blks_set(struct super_block *sb,
154560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
155560671a0SAneesh Kumar K.V {
156560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
157560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
158560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
159560671a0SAneesh Kumar K.V }
160560671a0SAneesh Kumar K.V 
161560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
162560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
163560671a0SAneesh Kumar K.V {
164560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
165560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
166560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
167560671a0SAneesh Kumar K.V }
168560671a0SAneesh Kumar K.V 
169560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
170560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
171560671a0SAneesh Kumar K.V {
172560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
173560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
174560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
175560671a0SAneesh Kumar K.V }
176560671a0SAneesh Kumar K.V 
177560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
178560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
179560671a0SAneesh Kumar K.V {
180560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
181560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
182560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
183560671a0SAneesh Kumar K.V }
184560671a0SAneesh Kumar K.V 
185d3d1faf6SCurt Wohlgemuth 
186d3d1faf6SCurt Wohlgemuth /* Just increment the non-pointer handle value */
187d3d1faf6SCurt Wohlgemuth static handle_t *ext4_get_nojournal(void)
188d3d1faf6SCurt Wohlgemuth {
189d3d1faf6SCurt Wohlgemuth 	handle_t *handle = current->journal_info;
190d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
191d3d1faf6SCurt Wohlgemuth 
192d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
193d3d1faf6SCurt Wohlgemuth 
194d3d1faf6SCurt Wohlgemuth 	ref_cnt++;
195d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
196d3d1faf6SCurt Wohlgemuth 
197d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
198d3d1faf6SCurt Wohlgemuth 	return handle;
199d3d1faf6SCurt Wohlgemuth }
200d3d1faf6SCurt Wohlgemuth 
201d3d1faf6SCurt Wohlgemuth 
202d3d1faf6SCurt Wohlgemuth /* Decrement the non-pointer handle value */
203d3d1faf6SCurt Wohlgemuth static void ext4_put_nojournal(handle_t *handle)
204d3d1faf6SCurt Wohlgemuth {
205d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
206d3d1faf6SCurt Wohlgemuth 
207d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt == 0);
208d3d1faf6SCurt Wohlgemuth 
209d3d1faf6SCurt Wohlgemuth 	ref_cnt--;
210d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
211d3d1faf6SCurt Wohlgemuth 
212d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
213d3d1faf6SCurt Wohlgemuth }
214d3d1faf6SCurt Wohlgemuth 
215ac27a0ecSDave Kleikamp /*
216dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
217ac27a0ecSDave Kleikamp  *
218ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
219ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
220ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
221ac27a0ecSDave Kleikamp  * appropriate.
222ac27a0ecSDave Kleikamp  */
223617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
224ac27a0ecSDave Kleikamp {
225ac27a0ecSDave Kleikamp 	journal_t *journal;
226ac27a0ecSDave Kleikamp 
227ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
228ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
229ac27a0ecSDave Kleikamp 
230ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
231ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
232ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
233617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2340390131bSFrank Mayhar 	if (journal) {
235ac27a0ecSDave Kleikamp 		if (is_journal_aborted(journal)) {
2360b8e58a1SAndreas Dilger 			ext4_abort(sb, __func__, "Detected aborted journal");
237ac27a0ecSDave Kleikamp 			return ERR_PTR(-EROFS);
238ac27a0ecSDave Kleikamp 		}
239dab291afSMingming Cao 		return jbd2_journal_start(journal, nblocks);
240ac27a0ecSDave Kleikamp 	}
241d3d1faf6SCurt Wohlgemuth 	return ext4_get_nojournal();
2420390131bSFrank Mayhar }
243ac27a0ecSDave Kleikamp 
244ac27a0ecSDave Kleikamp /*
245ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
246dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
247ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
248ac27a0ecSDave Kleikamp  * appropriate.
249ac27a0ecSDave Kleikamp  */
250617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
251ac27a0ecSDave Kleikamp {
252ac27a0ecSDave Kleikamp 	struct super_block *sb;
253ac27a0ecSDave Kleikamp 	int err;
254ac27a0ecSDave Kleikamp 	int rc;
255ac27a0ecSDave Kleikamp 
2560390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
257d3d1faf6SCurt Wohlgemuth 		ext4_put_nojournal(handle);
2580390131bSFrank Mayhar 		return 0;
2590390131bSFrank Mayhar 	}
260ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
261ac27a0ecSDave Kleikamp 	err = handle->h_err;
262dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
263ac27a0ecSDave Kleikamp 
264ac27a0ecSDave Kleikamp 	if (!err)
265ac27a0ecSDave Kleikamp 		err = rc;
266ac27a0ecSDave Kleikamp 	if (err)
267617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
268ac27a0ecSDave Kleikamp 	return err;
269ac27a0ecSDave Kleikamp }
270ac27a0ecSDave Kleikamp 
271617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
272ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
273ac27a0ecSDave Kleikamp {
274ac27a0ecSDave Kleikamp 	char nbuf[16];
275617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
276ac27a0ecSDave Kleikamp 
2770390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
2780390131bSFrank Mayhar 
279ac27a0ecSDave Kleikamp 	if (bh)
280ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
281ac27a0ecSDave Kleikamp 
282ac27a0ecSDave Kleikamp 	if (!handle->h_err)
283ac27a0ecSDave Kleikamp 		handle->h_err = err;
284ac27a0ecSDave Kleikamp 
285ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
286ac27a0ecSDave Kleikamp 		return;
287ac27a0ecSDave Kleikamp 
288ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
289ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
290ac27a0ecSDave Kleikamp 
291dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
292ac27a0ecSDave Kleikamp }
293ac27a0ecSDave Kleikamp 
294ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
295ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
296ac27a0ecSDave Kleikamp  *
297ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
298617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
299ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
300ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
301ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
302ac27a0ecSDave Kleikamp  * write out the superblock safely.
303ac27a0ecSDave Kleikamp  *
304dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
305ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
306ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
307ac27a0ecSDave Kleikamp  */
308ac27a0ecSDave Kleikamp 
309617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
310ac27a0ecSDave Kleikamp {
311617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
312ac27a0ecSDave Kleikamp 
313617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
314617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
315ac27a0ecSDave Kleikamp 
316ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
317ac27a0ecSDave Kleikamp 		return;
318ac27a0ecSDave Kleikamp 
319ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
320617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
321ac27a0ecSDave Kleikamp 
3224ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
323ac27a0ecSDave Kleikamp 		if (journal)
324dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
325ac27a0ecSDave Kleikamp 	}
326ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
327b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
328ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
329ac27a0ecSDave Kleikamp 	}
330e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
331ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
332617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
333ac27a0ecSDave Kleikamp 			sb->s_id);
334ac27a0ecSDave Kleikamp }
335ac27a0ecSDave Kleikamp 
336617ba13bSMingming Cao void ext4_error(struct super_block *sb, const char *function,
337ac27a0ecSDave Kleikamp 		const char *fmt, ...)
338ac27a0ecSDave Kleikamp {
339ac27a0ecSDave Kleikamp 	va_list args;
340ac27a0ecSDave Kleikamp 
341ac27a0ecSDave Kleikamp 	va_start(args, fmt);
342617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
343ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
344ac27a0ecSDave Kleikamp 	printk("\n");
345ac27a0ecSDave Kleikamp 	va_end(args);
346ac27a0ecSDave Kleikamp 
347617ba13bSMingming Cao 	ext4_handle_error(sb);
348ac27a0ecSDave Kleikamp }
349ac27a0ecSDave Kleikamp 
350617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
351ac27a0ecSDave Kleikamp 				     char nbuf[16])
352ac27a0ecSDave Kleikamp {
353ac27a0ecSDave Kleikamp 	char *errstr = NULL;
354ac27a0ecSDave Kleikamp 
355ac27a0ecSDave Kleikamp 	switch (errno) {
356ac27a0ecSDave Kleikamp 	case -EIO:
357ac27a0ecSDave Kleikamp 		errstr = "IO failure";
358ac27a0ecSDave Kleikamp 		break;
359ac27a0ecSDave Kleikamp 	case -ENOMEM:
360ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
361ac27a0ecSDave Kleikamp 		break;
362ac27a0ecSDave Kleikamp 	case -EROFS:
36378f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
36478f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
365ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
366ac27a0ecSDave Kleikamp 		else
367ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
368ac27a0ecSDave Kleikamp 		break;
369ac27a0ecSDave Kleikamp 	default:
370ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
371ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
372ac27a0ecSDave Kleikamp 		 * NULL. */
373ac27a0ecSDave Kleikamp 		if (nbuf) {
374ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
375ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
376ac27a0ecSDave Kleikamp 				errstr = nbuf;
377ac27a0ecSDave Kleikamp 		}
378ac27a0ecSDave Kleikamp 		break;
379ac27a0ecSDave Kleikamp 	}
380ac27a0ecSDave Kleikamp 
381ac27a0ecSDave Kleikamp 	return errstr;
382ac27a0ecSDave Kleikamp }
383ac27a0ecSDave Kleikamp 
384617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
385ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
386ac27a0ecSDave Kleikamp 
3872b2d6d01STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, int errno)
388ac27a0ecSDave Kleikamp {
389ac27a0ecSDave Kleikamp 	char nbuf[16];
390ac27a0ecSDave Kleikamp 	const char *errstr;
391ac27a0ecSDave Kleikamp 
392ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
393ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
394ac27a0ecSDave Kleikamp 	 * an error. */
395ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
396ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
397ac27a0ecSDave Kleikamp 		return;
398ac27a0ecSDave Kleikamp 
399617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
400617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
401ac27a0ecSDave Kleikamp 	       sb->s_id, function, errstr);
402ac27a0ecSDave Kleikamp 
403617ba13bSMingming Cao 	ext4_handle_error(sb);
404ac27a0ecSDave Kleikamp }
405ac27a0ecSDave Kleikamp 
406ac27a0ecSDave Kleikamp /*
407617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
408ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
409ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
410ac27a0ecSDave Kleikamp  *
411ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
412ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
413ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
414ac27a0ecSDave Kleikamp  */
415ac27a0ecSDave Kleikamp 
416617ba13bSMingming Cao void ext4_abort(struct super_block *sb, const char *function,
417ac27a0ecSDave Kleikamp 		const char *fmt, ...)
418ac27a0ecSDave Kleikamp {
419ac27a0ecSDave Kleikamp 	va_list args;
420ac27a0ecSDave Kleikamp 
421ac27a0ecSDave Kleikamp 	va_start(args, fmt);
422617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
423ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
424ac27a0ecSDave Kleikamp 	printk("\n");
425ac27a0ecSDave Kleikamp 	va_end(args);
426ac27a0ecSDave Kleikamp 
427ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
428617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
429ac27a0ecSDave Kleikamp 
430ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
431ac27a0ecSDave Kleikamp 		return;
432ac27a0ecSDave Kleikamp 
433b31e1552SEric Sandeen 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
434617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
435ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
4364ab2f15bSTheodore Ts'o 	EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
437ef2cabf7SHidehiro Kawai 	if (EXT4_SB(sb)->s_journal)
438dab291afSMingming Cao 		jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
439ac27a0ecSDave Kleikamp }
440ac27a0ecSDave Kleikamp 
441b31e1552SEric Sandeen void ext4_msg (struct super_block * sb, const char *prefix,
442b31e1552SEric Sandeen 		   const char *fmt, ...)
443b31e1552SEric Sandeen {
444b31e1552SEric Sandeen 	va_list args;
445b31e1552SEric Sandeen 
446b31e1552SEric Sandeen 	va_start(args, fmt);
447b31e1552SEric Sandeen 	printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
448b31e1552SEric Sandeen 	vprintk(fmt, args);
449b31e1552SEric Sandeen 	printk("\n");
450b31e1552SEric Sandeen 	va_end(args);
451b31e1552SEric Sandeen }
452b31e1552SEric Sandeen 
453617ba13bSMingming Cao void ext4_warning(struct super_block *sb, const char *function,
454ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
455ac27a0ecSDave Kleikamp {
456ac27a0ecSDave Kleikamp 	va_list args;
457ac27a0ecSDave Kleikamp 
458ac27a0ecSDave Kleikamp 	va_start(args, fmt);
459617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
460ac27a0ecSDave Kleikamp 	       sb->s_id, function);
461ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
462ac27a0ecSDave Kleikamp 	printk("\n");
463ac27a0ecSDave Kleikamp 	va_end(args);
464ac27a0ecSDave Kleikamp }
465ac27a0ecSDave Kleikamp 
4665d1b1b3fSAneesh Kumar K.V void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
4675d1b1b3fSAneesh Kumar K.V 			   const char *function, const char *fmt, ...)
4685d1b1b3fSAneesh Kumar K.V __releases(bitlock)
4695d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
4705d1b1b3fSAneesh Kumar K.V {
4715d1b1b3fSAneesh Kumar K.V 	va_list args;
4725d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4735d1b1b3fSAneesh Kumar K.V 
4745d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
4755d1b1b3fSAneesh Kumar K.V 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
4765d1b1b3fSAneesh Kumar K.V 	vprintk(fmt, args);
4775d1b1b3fSAneesh Kumar K.V 	printk("\n");
4785d1b1b3fSAneesh Kumar K.V 	va_end(args);
4795d1b1b3fSAneesh Kumar K.V 
4805d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
4815d1b1b3fSAneesh Kumar K.V 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4825d1b1b3fSAneesh Kumar K.V 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
483e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
4845d1b1b3fSAneesh Kumar K.V 		return;
4855d1b1b3fSAneesh Kumar K.V 	}
4865d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
4875d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
4885d1b1b3fSAneesh Kumar K.V 	/*
4895d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
4905d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
4915d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
4925d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
4935d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
4945d1b1b3fSAneesh Kumar K.V 	 * ext4_grp_locked_error() to distinguish beween the
4955d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
4965d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
4975d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
4985d1b1b3fSAneesh Kumar K.V 	 */
4995d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
5005d1b1b3fSAneesh Kumar K.V 	return;
5015d1b1b3fSAneesh Kumar K.V }
5025d1b1b3fSAneesh Kumar K.V 
503617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
504ac27a0ecSDave Kleikamp {
505617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
506ac27a0ecSDave Kleikamp 
507617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
508ac27a0ecSDave Kleikamp 		return;
509ac27a0ecSDave Kleikamp 
51046e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
511ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
512ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
513617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
514ac27a0ecSDave Kleikamp 
515617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
516617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
517617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
518ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
519ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
520ac27a0ecSDave Kleikamp 
521ac27a0ecSDave Kleikamp 	/*
522ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
523ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
524ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
525ac27a0ecSDave Kleikamp 	 */
526ac27a0ecSDave Kleikamp }
527ac27a0ecSDave Kleikamp 
528ac27a0ecSDave Kleikamp /*
529ac27a0ecSDave Kleikamp  * Open the external journal device
530ac27a0ecSDave Kleikamp  */
531b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
532ac27a0ecSDave Kleikamp {
533ac27a0ecSDave Kleikamp 	struct block_device *bdev;
534ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
535ac27a0ecSDave Kleikamp 
536ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
537ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
538ac27a0ecSDave Kleikamp 		goto fail;
539ac27a0ecSDave Kleikamp 	return bdev;
540ac27a0ecSDave Kleikamp 
541ac27a0ecSDave Kleikamp fail:
542b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
543ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
544ac27a0ecSDave Kleikamp 	return NULL;
545ac27a0ecSDave Kleikamp }
546ac27a0ecSDave Kleikamp 
547ac27a0ecSDave Kleikamp /*
548ac27a0ecSDave Kleikamp  * Release the journal device
549ac27a0ecSDave Kleikamp  */
550617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
551ac27a0ecSDave Kleikamp {
552ac27a0ecSDave Kleikamp 	bd_release(bdev);
5539a1c3542SAl Viro 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
554ac27a0ecSDave Kleikamp }
555ac27a0ecSDave Kleikamp 
556617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
557ac27a0ecSDave Kleikamp {
558ac27a0ecSDave Kleikamp 	struct block_device *bdev;
559ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
560ac27a0ecSDave Kleikamp 
561ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
562ac27a0ecSDave Kleikamp 	if (bdev) {
563617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
564ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
565ac27a0ecSDave Kleikamp 	}
566ac27a0ecSDave Kleikamp 	return ret;
567ac27a0ecSDave Kleikamp }
568ac27a0ecSDave Kleikamp 
569ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
570ac27a0ecSDave Kleikamp {
571617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
572ac27a0ecSDave Kleikamp }
573ac27a0ecSDave Kleikamp 
574617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
575ac27a0ecSDave Kleikamp {
576ac27a0ecSDave Kleikamp 	struct list_head *l;
577ac27a0ecSDave Kleikamp 
578b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
579ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
580ac27a0ecSDave Kleikamp 
581ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
582ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
583ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
584ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
585ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
586ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
587ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
588ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
589ac27a0ecSDave Kleikamp 	}
590ac27a0ecSDave Kleikamp }
591ac27a0ecSDave Kleikamp 
592617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
593ac27a0ecSDave Kleikamp {
594617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
595617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
596ef2cabf7SHidehiro Kawai 	int i, err;
597ac27a0ecSDave Kleikamp 
5984c0425ffSMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
5994c0425ffSMingming Cao 	destroy_workqueue(sbi->dio_unwritten_wq);
6004c0425ffSMingming Cao 
601a9e220f8SAl Viro 	lock_super(sb);
6026cfd0148SChristoph Hellwig 	lock_kernel();
6038c85e125SChristoph Hellwig 	if (sb->s_dirt)
604ebc1ac16SChristoph Hellwig 		ext4_commit_super(sb, 1);
6058c85e125SChristoph Hellwig 
6060390131bSFrank Mayhar 	if (sbi->s_journal) {
607ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
60847b4a50bSJan Kara 		sbi->s_journal = NULL;
609ef2cabf7SHidehiro Kawai 		if (err < 0)
6100390131bSFrank Mayhar 			ext4_abort(sb, __func__,
6110390131bSFrank Mayhar 				   "Couldn't clean up the journal");
6120390131bSFrank Mayhar 	}
613d4edac31SJosef Bacik 
614d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
615d4edac31SJosef Bacik 	ext4_mb_release(sb);
616d4edac31SJosef Bacik 	ext4_ext_release(sb);
617d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
618d4edac31SJosef Bacik 
619ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
620617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
621ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
622e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
623ac27a0ecSDave Kleikamp 	}
624240799cdSTheodore Ts'o 	if (sbi->s_proc) {
6259f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
626240799cdSTheodore Ts'o 	}
6273197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
628ac27a0ecSDave Kleikamp 
629ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
630ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
631ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
632c5ca7c76STheodore Ts'o 	if (is_vmalloc_addr(sbi->s_flex_groups))
633c5ca7c76STheodore Ts'o 		vfree(sbi->s_flex_groups);
634c5ca7c76STheodore Ts'o 	else
635772cb7c8SJose R. Santos 		kfree(sbi->s_flex_groups);
636ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
637ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
638ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
6396bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
640ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
641ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
642ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
643ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
644ac27a0ecSDave Kleikamp #endif
645ac27a0ecSDave Kleikamp 
646ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
647ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
648ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
649ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
650ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
651ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
652ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
653ac27a0ecSDave Kleikamp 
654f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
655ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
656ac27a0ecSDave Kleikamp 		/*
657ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
658ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
659ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
660ac27a0ecSDave Kleikamp 		 */
661ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
662f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
663617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
664ac27a0ecSDave Kleikamp 	}
665ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
6663197ebdbSTheodore Ts'o 	/*
6673197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
6683197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
6693197ebdbSTheodore Ts'o 	 */
6703197ebdbSTheodore Ts'o 	unlock_kernel();
6713197ebdbSTheodore Ts'o 	unlock_super(sb);
6723197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
6733197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
674705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
675ac27a0ecSDave Kleikamp 	kfree(sbi);
676ac27a0ecSDave Kleikamp }
677ac27a0ecSDave Kleikamp 
678e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
679ac27a0ecSDave Kleikamp 
680ac27a0ecSDave Kleikamp /*
681ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
682ac27a0ecSDave Kleikamp  */
683617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
684ac27a0ecSDave Kleikamp {
685617ba13bSMingming Cao 	struct ext4_inode_info *ei;
686ac27a0ecSDave Kleikamp 
687e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
688ac27a0ecSDave Kleikamp 	if (!ei)
689ac27a0ecSDave Kleikamp 		return NULL;
6900b8e58a1SAndreas Dilger 
691ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
69291246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
693a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
694c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
695c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
6960390131bSFrank Mayhar 	/*
6970390131bSFrank Mayhar 	 * Note:  We can be called before EXT4_SB(sb)->s_journal is set,
6980390131bSFrank Mayhar 	 * therefore it can be null here.  Don't check it, just initialize
6990390131bSFrank Mayhar 	 * jinode.
7000390131bSFrank Mayhar 	 */
701678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
702d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
703d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
704d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
7059d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
706d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
707d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
708a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
709a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
710a9e7f447SDmitry Monakhov #endif
7118d5d02e6SMingming Cao 	INIT_LIST_HEAD(&ei->i_aio_dio_complete_list);
7128d5d02e6SMingming Cao 	ei->cur_aio_dio = NULL;
713b436b9beSJan Kara 	ei->i_sync_tid = 0;
714b436b9beSJan Kara 	ei->i_datasync_tid = 0;
7150b8e58a1SAndreas Dilger 
716ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
717ac27a0ecSDave Kleikamp }
718ac27a0ecSDave Kleikamp 
719617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
720ac27a0ecSDave Kleikamp {
7219f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
722b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
723b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
724b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
7259f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
7269f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
7279f7dd93dSVasily Averin 				true);
7289f7dd93dSVasily Averin 		dump_stack();
7299f7dd93dSVasily Averin 	}
730617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
731ac27a0ecSDave Kleikamp }
732ac27a0ecSDave Kleikamp 
73351cc5068SAlexey Dobriyan static void init_once(void *foo)
734ac27a0ecSDave Kleikamp {
735617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
736ac27a0ecSDave Kleikamp 
737ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
73803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
739ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
740ac27a0ecSDave Kleikamp #endif
7410e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
742ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
743ac27a0ecSDave Kleikamp }
744ac27a0ecSDave Kleikamp 
745ac27a0ecSDave Kleikamp static int init_inodecache(void)
746ac27a0ecSDave Kleikamp {
747617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
748617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
749ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
750ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
75120c2df83SPaul Mundt 					     init_once);
752617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
753ac27a0ecSDave Kleikamp 		return -ENOMEM;
754ac27a0ecSDave Kleikamp 	return 0;
755ac27a0ecSDave Kleikamp }
756ac27a0ecSDave Kleikamp 
757ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
758ac27a0ecSDave Kleikamp {
759617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
760ac27a0ecSDave Kleikamp }
761ac27a0ecSDave Kleikamp 
762617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
763ac27a0ecSDave Kleikamp {
764c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
7650390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode))
766678aaf48SJan Kara 		jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
767678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
768ac27a0ecSDave Kleikamp }
769ac27a0ecSDave Kleikamp 
7702b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
7712b2d6d01STheodore Ts'o 					   struct super_block *sb)
772ac27a0ecSDave Kleikamp {
773ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
774617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
775ac27a0ecSDave Kleikamp 
7765a20bdfcSJan Kara 	if (sbi->s_jquota_fmt) {
7775a20bdfcSJan Kara 		char *fmtname = "";
7785a20bdfcSJan Kara 
7795a20bdfcSJan Kara 		switch (sbi->s_jquota_fmt) {
7805a20bdfcSJan Kara 		case QFMT_VFS_OLD:
7815a20bdfcSJan Kara 			fmtname = "vfsold";
7825a20bdfcSJan Kara 			break;
7835a20bdfcSJan Kara 		case QFMT_VFS_V0:
7845a20bdfcSJan Kara 			fmtname = "vfsv0";
7855a20bdfcSJan Kara 			break;
7865a20bdfcSJan Kara 		case QFMT_VFS_V1:
7875a20bdfcSJan Kara 			fmtname = "vfsv1";
7885a20bdfcSJan Kara 			break;
7895a20bdfcSJan Kara 		}
7905a20bdfcSJan Kara 		seq_printf(seq, ",jqfmt=%s", fmtname);
7915a20bdfcSJan Kara 	}
792ac27a0ecSDave Kleikamp 
793ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
794ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
795ac27a0ecSDave Kleikamp 
796ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
797ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
798ac27a0ecSDave Kleikamp 
799617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
800ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
801ac27a0ecSDave Kleikamp 
802617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
803ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
804ac27a0ecSDave Kleikamp #endif
805ac27a0ecSDave Kleikamp }
806ac27a0ecSDave Kleikamp 
807d9c9bef1SMiklos Szeredi /*
808d9c9bef1SMiklos Szeredi  * Show an option if
809d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
810d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
811d9c9bef1SMiklos Szeredi  */
812617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
813ac27a0ecSDave Kleikamp {
814aa22df2cSAneesh Kumar K.V 	int def_errors;
815aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
816ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
817d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
818d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
819d9c9bef1SMiklos Szeredi 
820d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
821aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
822d9c9bef1SMiklos Szeredi 
823d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
824d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
825d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
826d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
827aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
828d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
829d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
830d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
831d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
832d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
833d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
834d9c9bef1SMiklos Szeredi 	}
835d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
836d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
837d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
838d9c9bef1SMiklos Szeredi 	}
839bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
840d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
841bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
842d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
843bb4f397aSAneesh Kumar K.V 		}
844bb4f397aSAneesh Kumar K.V 	}
845aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
846bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
847aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
848d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
849aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
850d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
851aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
852d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
853d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
854d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
85503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
856aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
857aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
858d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
859d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
860d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
861d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
862d9c9bef1SMiklos Szeredi 	}
863d9c9bef1SMiklos Szeredi #endif
86403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
865aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
866d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
867d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
868d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
869d9c9bef1SMiklos Szeredi #endif
87030773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
871d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
872d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
873d9c9bef1SMiklos Szeredi 	}
87430773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
87530773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
87630773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
87730773840STheodore Ts'o 	}
87830773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
87930773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
88030773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
88130773840STheodore Ts'o 	}
88230773840STheodore Ts'o 
883571640caSEric Sandeen 	/*
884571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
885571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
886571640caSEric Sandeen 	 * status is.
887571640caSEric Sandeen 	 */
888571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
889571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
890cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
891cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
892d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
893d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
89425ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
89525ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
896dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
897dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
898dd919b98SAneesh Kumar K.V 
899ac27a0ecSDave Kleikamp 
900cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
901cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
902aa22df2cSAneesh Kumar K.V 	/*
903aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
904aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
905aa22df2cSAneesh Kumar K.V 	 */
906617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
907ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
908617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
909ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
910617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
911ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
912ac27a0ecSDave Kleikamp 
913240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
914240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
915240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
916240799cdSTheodore Ts'o 
9175bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
9185bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
9195bf5683aSHidehiro Kawai 
920afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
92106705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
922afd4672dSTheodore Ts'o 
9235328e635SEric Sandeen 	if (test_opt(sb, DISCARD))
9245328e635SEric Sandeen 		seq_puts(seq, ",discard");
9255328e635SEric Sandeen 
926e3bb52aeSEric Sandeen 	if (test_opt(sb, NOLOAD))
927e3bb52aeSEric Sandeen 		seq_puts(seq, ",norecovery");
928e3bb52aeSEric Sandeen 
929617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
9300b8e58a1SAndreas Dilger 
931ac27a0ecSDave Kleikamp 	return 0;
932ac27a0ecSDave Kleikamp }
933ac27a0ecSDave Kleikamp 
9341b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
9351b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
936ac27a0ecSDave Kleikamp {
937ac27a0ecSDave Kleikamp 	struct inode *inode;
938ac27a0ecSDave Kleikamp 
939617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
940ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
941617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
942ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
943ac27a0ecSDave Kleikamp 
944ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
945ac27a0ecSDave Kleikamp 	 *
946617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
947ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
948ac27a0ecSDave Kleikamp 	 *
949ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
950ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
951ac27a0ecSDave Kleikamp 	 */
9521d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
9531d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9541d1fe1eeSDavid Howells 		return ERR_CAST(inode);
9551d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
956ac27a0ecSDave Kleikamp 		iput(inode);
957ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
958ac27a0ecSDave Kleikamp 	}
9591b961ac0SChristoph Hellwig 
9601b961ac0SChristoph Hellwig 	return inode;
961ac27a0ecSDave Kleikamp }
9621b961ac0SChristoph Hellwig 
9631b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
9641b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
9651b961ac0SChristoph Hellwig {
9661b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
9671b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
9681b961ac0SChristoph Hellwig }
9691b961ac0SChristoph Hellwig 
9701b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
9711b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
9721b961ac0SChristoph Hellwig {
9731b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
9741b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
975ac27a0ecSDave Kleikamp }
976ac27a0ecSDave Kleikamp 
977c39a7f84SToshiyuki Okajima /*
978c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
979c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
980c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
981c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
982c39a7f84SToshiyuki Okajima  */
9830b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
9840b8e58a1SAndreas Dilger 				 gfp_t wait)
985c39a7f84SToshiyuki Okajima {
986c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
987c39a7f84SToshiyuki Okajima 
988c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
989c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
990c39a7f84SToshiyuki Okajima 		return 0;
991c39a7f84SToshiyuki Okajima 	if (journal)
992c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
993c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
994c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
995c39a7f84SToshiyuki Okajima }
996c39a7f84SToshiyuki Okajima 
997ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
998ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
999ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1000ac27a0ecSDave Kleikamp 
1001617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1002617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1003617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1004617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1005617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
10066f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
10076f28e087SJan Kara 				char *path, int remount);
1008617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1009617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1010ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1011617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1012ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
1013ac27a0ecSDave Kleikamp 
101461e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
1015edf72453SJan Kara 	.initialize	= dquot_initialize,
1016edf72453SJan Kara 	.drop		= dquot_drop,
1017ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
101860e58e0fSMingming Cao 	.reserve_space	= dquot_reserve_space,
101960e58e0fSMingming Cao 	.claim_space	= dquot_claim_space,
102060e58e0fSMingming Cao 	.release_rsv	= dquot_release_reserved_space,
1021a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
102260e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1023a9e7f447SDmitry Monakhov #endif
1024ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
1025ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
1026ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
1027ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
1028617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1029617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1030617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1031617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1032a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1033a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1034a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1035ac27a0ecSDave Kleikamp };
1036ac27a0ecSDave Kleikamp 
10370d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1038617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1039ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
1040ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
1041ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
1042ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
1043ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
1044ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
1045ac27a0ecSDave Kleikamp };
1046ac27a0ecSDave Kleikamp #endif
1047ac27a0ecSDave Kleikamp 
1048ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1049617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1050617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1051617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1052617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
1053617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
1054617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1055617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1056c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1057c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1058617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1059617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1060617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
1061617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1062ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1063617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1064617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1065ac27a0ecSDave Kleikamp #endif
1066c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1067ac27a0ecSDave Kleikamp };
1068ac27a0ecSDave Kleikamp 
10699ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
10709ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
10719ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
10729ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
10739ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
10749ca92389STheodore Ts'o 	.delete_inode	= ext4_delete_inode,
10759ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
10769ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
10779ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
10789ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
10799ca92389STheodore Ts'o 	.clear_inode	= ext4_clear_inode,
10809ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
10819ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
10829ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
10839ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
10849ca92389STheodore Ts'o #endif
10859ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
10869ca92389STheodore Ts'o };
10879ca92389STheodore Ts'o 
108839655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
10891b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
10901b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1091617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1092ac27a0ecSDave Kleikamp };
1093ac27a0ecSDave Kleikamp 
1094ac27a0ecSDave Kleikamp enum {
1095ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1096ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
109701436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1098ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
109906705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
110030773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1101c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1102818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1103ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1104296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1105ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
11065a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
11075a20bdfcSJan Kara 	Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
11085a20bdfcSJan Kara 	Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
110901436ef2STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc,
11106fd058f7STheodore Ts'o 	Opt_block_validity, Opt_noblock_validity,
11115328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
11125328e635SEric Sandeen 	Opt_discard, Opt_nodiscard,
1113ac27a0ecSDave Kleikamp };
1114ac27a0ecSDave Kleikamp 
1115a447c093SSteven Whitehouse static const match_table_t tokens = {
1116ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1117ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1118ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1119ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1120ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1121ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1122ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1123ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1124ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1125ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1126ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1127ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1128ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1129ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1130ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1131ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1132ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1133ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1134ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1135ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1136ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1137e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
1138ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1139ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1140ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
114130773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
114230773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1143ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1144ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1145818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1146818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1147ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1148ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1149ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1150ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
11515bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
11525bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1153ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1154ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1155ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1156ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1157ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1158ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
11595a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1160ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1161ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1162ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1163ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1164ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
116506705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
116606705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
116725ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1168c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1169ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
117064769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1171dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
11726fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
11736fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1174240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1175b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1176afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
117706705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
117806705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
11795328e635SEric Sandeen 	{Opt_discard, "discard"},
11805328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1181f3f12faaSJosef Bacik 	{Opt_err, NULL},
1182ac27a0ecSDave Kleikamp };
1183ac27a0ecSDave Kleikamp 
1184617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1185ac27a0ecSDave Kleikamp {
1186617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1187ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1188ac27a0ecSDave Kleikamp 
1189ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1190ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
11910b8e58a1SAndreas Dilger 
1192ac27a0ecSDave Kleikamp 	options += 3;
11930b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1194ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1195ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
11964776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1197ac27a0ecSDave Kleikamp 		       (char *) *data);
1198ac27a0ecSDave Kleikamp 		return 1;
1199ac27a0ecSDave Kleikamp 	}
1200ac27a0ecSDave Kleikamp 	if (*options == ',')
1201ac27a0ecSDave Kleikamp 		options++;
1202ac27a0ecSDave Kleikamp 	*data = (void *) options;
12030b8e58a1SAndreas Dilger 
1204ac27a0ecSDave Kleikamp 	return sb_block;
1205ac27a0ecSDave Kleikamp }
1206ac27a0ecSDave Kleikamp 
1207b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1208b3881f74STheodore Ts'o 
1209ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1210c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1211b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1212617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1213ac27a0ecSDave Kleikamp {
1214617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1215ac27a0ecSDave Kleikamp 	char *p;
1216ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1217ac27a0ecSDave Kleikamp 	int data_opt = 0;
1218ac27a0ecSDave Kleikamp 	int option;
1219ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1220dfc5d03fSJan Kara 	int qtype, qfmt;
1221ac27a0ecSDave Kleikamp 	char *qname;
1222ac27a0ecSDave Kleikamp #endif
1223ac27a0ecSDave Kleikamp 
1224ac27a0ecSDave Kleikamp 	if (!options)
1225ac27a0ecSDave Kleikamp 		return 1;
1226ac27a0ecSDave Kleikamp 
1227ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1228ac27a0ecSDave Kleikamp 		int token;
1229ac27a0ecSDave Kleikamp 		if (!*p)
1230ac27a0ecSDave Kleikamp 			continue;
1231ac27a0ecSDave Kleikamp 
1232*15121c18SEric Sandeen 		/*
1233*15121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
1234*15121c18SEric Sandeen 		 * found; some options take optional arguments.
1235*15121c18SEric Sandeen 		 */
1236*15121c18SEric Sandeen 		args[0].to = args[0].from = 0;
1237ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1238ac27a0ecSDave Kleikamp 		switch (token) {
1239ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1240ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
1241ac27a0ecSDave Kleikamp 			break;
1242ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1243ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
1244ac27a0ecSDave Kleikamp 			break;
1245ac27a0ecSDave Kleikamp 		case Opt_grpid:
1246ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
1247ac27a0ecSDave Kleikamp 			break;
1248ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1249ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
1250ac27a0ecSDave Kleikamp 			break;
1251ac27a0ecSDave Kleikamp 		case Opt_resuid:
1252ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1253ac27a0ecSDave Kleikamp 				return 0;
1254ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1255ac27a0ecSDave Kleikamp 			break;
1256ac27a0ecSDave Kleikamp 		case Opt_resgid:
1257ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1258ac27a0ecSDave Kleikamp 				return 0;
1259ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1260ac27a0ecSDave Kleikamp 			break;
1261ac27a0ecSDave Kleikamp 		case Opt_sb:
1262ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1263ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1264ac27a0ecSDave Kleikamp 			break;
1265ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1266ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1267ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1268ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1269ac27a0ecSDave Kleikamp 			break;
1270ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1271ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1272ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1273ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1274ac27a0ecSDave Kleikamp 			break;
1275ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1276ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1277ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1278ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1279ac27a0ecSDave Kleikamp 			break;
1280ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1281ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1282ac27a0ecSDave Kleikamp 			break;
1283ac27a0ecSDave Kleikamp 		case Opt_debug:
1284ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1285ac27a0ecSDave Kleikamp 			break;
1286ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1287ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1288ac27a0ecSDave Kleikamp 			break;
1289ac27a0ecSDave Kleikamp 		case Opt_orlov:
1290ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1291ac27a0ecSDave Kleikamp 			break;
129203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1293ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1294ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1295ac27a0ecSDave Kleikamp 			break;
1296ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1297ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1298ac27a0ecSDave Kleikamp 			break;
1299ac27a0ecSDave Kleikamp #else
1300ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1301ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1302b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1303ac27a0ecSDave Kleikamp 			break;
1304ac27a0ecSDave Kleikamp #endif
130503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1306ac27a0ecSDave Kleikamp 		case Opt_acl:
1307ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1308ac27a0ecSDave Kleikamp 			break;
1309ac27a0ecSDave Kleikamp 		case Opt_noacl:
1310ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1311ac27a0ecSDave Kleikamp 			break;
1312ac27a0ecSDave Kleikamp #else
1313ac27a0ecSDave Kleikamp 		case Opt_acl:
1314ac27a0ecSDave Kleikamp 		case Opt_noacl:
1315b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1316ac27a0ecSDave Kleikamp 			break;
1317ac27a0ecSDave Kleikamp #endif
1318ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1319ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1320ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1321ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1322ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1323ac27a0ecSDave Kleikamp 			   journal file. */
1324ac27a0ecSDave Kleikamp 			if (is_remount) {
1325b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1326b31e1552SEric Sandeen 					 "Cannot specify journal on remount");
1327ac27a0ecSDave Kleikamp 				return 0;
1328ac27a0ecSDave Kleikamp 			}
1329ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1330ac27a0ecSDave Kleikamp 			break;
1331ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1332ac27a0ecSDave Kleikamp 			if (is_remount) {
1333b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1334b31e1552SEric Sandeen 					"Cannot specify journal on remount");
1335ac27a0ecSDave Kleikamp 				return 0;
1336ac27a0ecSDave Kleikamp 			}
1337ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1338ac27a0ecSDave Kleikamp 				return 0;
1339ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1340ac27a0ecSDave Kleikamp 			break;
1341818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1342d4da6c9cSLinus Torvalds 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1343d4da6c9cSLinus Torvalds 			break;
1344818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1345818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1346d4da6c9cSLinus Torvalds 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1347818d276cSGirish Shilamkar 			break;
1348ac27a0ecSDave Kleikamp 		case Opt_noload:
1349ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1350ac27a0ecSDave Kleikamp 			break;
1351ac27a0ecSDave Kleikamp 		case Opt_commit:
1352ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1353ac27a0ecSDave Kleikamp 				return 0;
1354ac27a0ecSDave Kleikamp 			if (option < 0)
1355ac27a0ecSDave Kleikamp 				return 0;
1356ac27a0ecSDave Kleikamp 			if (option == 0)
1357cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1358ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1359ac27a0ecSDave Kleikamp 			break;
136030773840STheodore Ts'o 		case Opt_max_batch_time:
136130773840STheodore Ts'o 			if (match_int(&args[0], &option))
136230773840STheodore Ts'o 				return 0;
136330773840STheodore Ts'o 			if (option < 0)
136430773840STheodore Ts'o 				return 0;
136530773840STheodore Ts'o 			if (option == 0)
136630773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
136730773840STheodore Ts'o 			sbi->s_max_batch_time = option;
136830773840STheodore Ts'o 			break;
136930773840STheodore Ts'o 		case Opt_min_batch_time:
137030773840STheodore Ts'o 			if (match_int(&args[0], &option))
137130773840STheodore Ts'o 				return 0;
137230773840STheodore Ts'o 			if (option < 0)
137330773840STheodore Ts'o 				return 0;
137430773840STheodore Ts'o 			sbi->s_min_batch_time = option;
137530773840STheodore Ts'o 			break;
1376ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1377617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1378ac27a0ecSDave Kleikamp 			goto datacheck;
1379ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1380617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1381ac27a0ecSDave Kleikamp 			goto datacheck;
1382ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1383617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1384ac27a0ecSDave Kleikamp 		datacheck:
1385ac27a0ecSDave Kleikamp 			if (is_remount) {
1386617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1387ac27a0ecSDave Kleikamp 						!= data_opt) {
1388b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
1389b31e1552SEric Sandeen 						"Cannot change data mode on remount");
1390ac27a0ecSDave Kleikamp 					return 0;
1391ac27a0ecSDave Kleikamp 				}
1392ac27a0ecSDave Kleikamp 			} else {
1393617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1394ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1395ac27a0ecSDave Kleikamp 			}
1396ac27a0ecSDave Kleikamp 			break;
13975bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
13985bf5683aSHidehiro Kawai 			set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13995bf5683aSHidehiro Kawai 			break;
14005bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
14015bf5683aSHidehiro Kawai 			clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
14025bf5683aSHidehiro Kawai 			break;
1403ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1404ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1405ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1406ac27a0ecSDave Kleikamp 			goto set_qf_name;
1407ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1408ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1409ac27a0ecSDave Kleikamp set_qf_name:
141017bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1411dfc5d03fSJan Kara 			    !sbi->s_qf_names[qtype]) {
1412b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1413b31e1552SEric Sandeen 				       "Cannot change journaled "
1414b31e1552SEric Sandeen 				       "quota options when quota turned on");
1415ac27a0ecSDave Kleikamp 				return 0;
1416ac27a0ecSDave Kleikamp 			}
1417ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1418ac27a0ecSDave Kleikamp 			if (!qname) {
1419b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1420b31e1552SEric Sandeen 					"Not enough memory for "
1421b31e1552SEric Sandeen 					"storing quotafile name");
1422ac27a0ecSDave Kleikamp 				return 0;
1423ac27a0ecSDave Kleikamp 			}
1424ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1425ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1426b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1427b31e1552SEric Sandeen 					"%s quota file already "
1428b31e1552SEric Sandeen 					"specified", QTYPE2NAME(qtype));
1429ac27a0ecSDave Kleikamp 				kfree(qname);
1430ac27a0ecSDave Kleikamp 				return 0;
1431ac27a0ecSDave Kleikamp 			}
1432ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1433ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1434b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1435b31e1552SEric Sandeen 					"quotafile must be on "
1436b31e1552SEric Sandeen 					"filesystem root");
1437ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1438ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1439ac27a0ecSDave Kleikamp 				return 0;
1440ac27a0ecSDave Kleikamp 			}
1441ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1442ac27a0ecSDave Kleikamp 			break;
1443ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1444ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1445ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1446ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1447ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1448ac27a0ecSDave Kleikamp clear_qf_name:
144917bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1450dfc5d03fSJan Kara 			    sbi->s_qf_names[qtype]) {
1451b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
14522c8be6b2SJan Kara 					"journaled quota options when "
1453b31e1552SEric Sandeen 					"quota turned on");
1454ac27a0ecSDave Kleikamp 				return 0;
1455ac27a0ecSDave Kleikamp 			}
1456ac27a0ecSDave Kleikamp 			/*
1457ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1458ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1459ac27a0ecSDave Kleikamp 			 */
1460ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1461ac27a0ecSDave Kleikamp 			break;
1462ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1463dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1464dfc5d03fSJan Kara 			goto set_qf_format;
1465ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1466dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
14675a20bdfcSJan Kara 			goto set_qf_format;
14685a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
14695a20bdfcSJan Kara 			qfmt = QFMT_VFS_V1;
1470dfc5d03fSJan Kara set_qf_format:
147117bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1472dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1473b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
1474dfc5d03fSJan Kara 					"journaled quota options when "
1475b31e1552SEric Sandeen 					"quota turned on");
1476dfc5d03fSJan Kara 				return 0;
1477dfc5d03fSJan Kara 			}
1478dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1479ac27a0ecSDave Kleikamp 			break;
1480ac27a0ecSDave Kleikamp 		case Opt_quota:
1481ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1482ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1483ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1484ac27a0ecSDave Kleikamp 			break;
1485ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1486ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1487ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1488ac27a0ecSDave Kleikamp 			break;
1489ac27a0ecSDave Kleikamp 		case Opt_noquota:
149017bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1491b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change quota "
1492b31e1552SEric Sandeen 					"options when quota turned on");
1493ac27a0ecSDave Kleikamp 				return 0;
1494ac27a0ecSDave Kleikamp 			}
1495ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1496ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1497ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1498ac27a0ecSDave Kleikamp 			break;
1499ac27a0ecSDave Kleikamp #else
1500ac27a0ecSDave Kleikamp 		case Opt_quota:
1501ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1502ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1503b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1504b31e1552SEric Sandeen 				"quota options not supported");
1505cd59e7b9SJan Kara 			break;
1506ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1507ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1508ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1509ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1510ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1511ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
15125a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
1513b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1514b31e1552SEric Sandeen 				"journaled quota options not supported");
1515ac27a0ecSDave Kleikamp 			break;
1516ac27a0ecSDave Kleikamp 		case Opt_noquota:
1517ac27a0ecSDave Kleikamp 			break;
1518ac27a0ecSDave Kleikamp #endif
1519ac27a0ecSDave Kleikamp 		case Opt_abort:
15204ab2f15bSTheodore Ts'o 			sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1521ac27a0ecSDave Kleikamp 			break;
152206705bffSTheodore Ts'o 		case Opt_nobarrier:
152306705bffSTheodore Ts'o 			clear_opt(sbi->s_mount_opt, BARRIER);
152406705bffSTheodore Ts'o 			break;
1525ac27a0ecSDave Kleikamp 		case Opt_barrier:
1526*15121c18SEric Sandeen 			if (args[0].from) {
1527*15121c18SEric Sandeen 				if (match_int(&args[0], &option))
1528*15121c18SEric Sandeen 					return 0;
1529*15121c18SEric Sandeen 			} else
1530*15121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1531ac27a0ecSDave Kleikamp 			if (option)
1532ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1533ac27a0ecSDave Kleikamp 			else
1534ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1535ac27a0ecSDave Kleikamp 			break;
1536ac27a0ecSDave Kleikamp 		case Opt_ignore:
1537ac27a0ecSDave Kleikamp 			break;
1538ac27a0ecSDave Kleikamp 		case Opt_resize:
1539ac27a0ecSDave Kleikamp 			if (!is_remount) {
1540b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1541b31e1552SEric Sandeen 					"resize option only available "
1542b31e1552SEric Sandeen 					"for remount");
1543ac27a0ecSDave Kleikamp 				return 0;
1544ac27a0ecSDave Kleikamp 			}
1545ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1546ac27a0ecSDave Kleikamp 				return 0;
1547ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1548ac27a0ecSDave Kleikamp 			break;
1549ac27a0ecSDave Kleikamp 		case Opt_nobh:
1550ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1551ac27a0ecSDave Kleikamp 			break;
1552ac27a0ecSDave Kleikamp 		case Opt_bh:
1553ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1554ac27a0ecSDave Kleikamp 			break;
155525ec56b5SJean Noel Cordenner 		case Opt_i_version:
155625ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
155725ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
155825ec56b5SJean Noel Cordenner 			break;
1559dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1560dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1561dd919b98SAneesh Kumar K.V 			break;
1562c9de560dSAlex Tomas 		case Opt_stripe:
1563c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1564c9de560dSAlex Tomas 				return 0;
1565c9de560dSAlex Tomas 			if (option < 0)
1566c9de560dSAlex Tomas 				return 0;
1567c9de560dSAlex Tomas 			sbi->s_stripe = option;
1568c9de560dSAlex Tomas 			break;
156964769240SAlex Tomas 		case Opt_delalloc:
157064769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
157164769240SAlex Tomas 			break;
15726fd058f7STheodore Ts'o 		case Opt_block_validity:
15736fd058f7STheodore Ts'o 			set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
15746fd058f7STheodore Ts'o 			break;
15756fd058f7STheodore Ts'o 		case Opt_noblock_validity:
15766fd058f7STheodore Ts'o 			clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
15776fd058f7STheodore Ts'o 			break;
1578240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1579240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1580240799cdSTheodore Ts'o 				return 0;
1581240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1582240799cdSTheodore Ts'o 				return 0;
1583f7c43950STheodore Ts'o 			if (!is_power_of_2(option)) {
1584b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1585b31e1552SEric Sandeen 					 "EXT4-fs: inode_readahead_blks"
1586b31e1552SEric Sandeen 					 " must be a power of 2");
15873197ebdbSTheodore Ts'o 				return 0;
15883197ebdbSTheodore Ts'o 			}
1589240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1590240799cdSTheodore Ts'o 			break;
1591b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1592b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1593b3881f74STheodore Ts'o 				return 0;
1594b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1595b3881f74STheodore Ts'o 				break;
1596b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1597b3881f74STheodore Ts'o 							    option);
1598b3881f74STheodore Ts'o 			break;
159906705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
160006705bffSTheodore Ts'o 			set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
160106705bffSTheodore Ts'o 			break;
1602afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
1603*15121c18SEric Sandeen 			if (args[0].from) {
1604*15121c18SEric Sandeen 				if (match_int(&args[0], &option))
1605*15121c18SEric Sandeen 					return 0;
1606*15121c18SEric Sandeen 			} else
1607*15121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1608afd4672dSTheodore Ts'o 			if (option)
1609afd4672dSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1610afd4672dSTheodore Ts'o 			else
1611afd4672dSTheodore Ts'o 				set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1612afd4672dSTheodore Ts'o 			break;
16135328e635SEric Sandeen 		case Opt_discard:
16145328e635SEric Sandeen 			set_opt(sbi->s_mount_opt, DISCARD);
16155328e635SEric Sandeen 			break;
16165328e635SEric Sandeen 		case Opt_nodiscard:
16175328e635SEric Sandeen 			clear_opt(sbi->s_mount_opt, DISCARD);
16185328e635SEric Sandeen 			break;
1619ac27a0ecSDave Kleikamp 		default:
1620b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1621b31e1552SEric Sandeen 			       "Unrecognized mount option \"%s\" "
1622b31e1552SEric Sandeen 			       "or missing value", p);
1623ac27a0ecSDave Kleikamp 			return 0;
1624ac27a0ecSDave Kleikamp 		}
1625ac27a0ecSDave Kleikamp 	}
1626ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1627ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1628617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1629ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1630ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1631ac27a0ecSDave Kleikamp 
1632617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1633ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1634ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1635ac27a0ecSDave Kleikamp 
1636ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1637617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1638ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1639617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1640b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1641b31e1552SEric Sandeen 					"format mixing");
1642ac27a0ecSDave Kleikamp 			return 0;
1643ac27a0ecSDave Kleikamp 		}
1644ac27a0ecSDave Kleikamp 
1645ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1646b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1647b31e1552SEric Sandeen 					"not specified");
1648ac27a0ecSDave Kleikamp 			return 0;
1649ac27a0ecSDave Kleikamp 		}
1650ac27a0ecSDave Kleikamp 	} else {
1651ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1652b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
16532c8be6b2SJan Kara 					"specified with no journaling "
1654b31e1552SEric Sandeen 					"enabled");
1655ac27a0ecSDave Kleikamp 			return 0;
1656ac27a0ecSDave Kleikamp 		}
1657ac27a0ecSDave Kleikamp 	}
1658ac27a0ecSDave Kleikamp #endif
1659ac27a0ecSDave Kleikamp 	return 1;
1660ac27a0ecSDave Kleikamp }
1661ac27a0ecSDave Kleikamp 
1662617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1663ac27a0ecSDave Kleikamp 			    int read_only)
1664ac27a0ecSDave Kleikamp {
1665617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1666ac27a0ecSDave Kleikamp 	int res = 0;
1667ac27a0ecSDave Kleikamp 
1668617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1669b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1670b31e1552SEric Sandeen 			 "forcing read-only mode");
1671ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1672ac27a0ecSDave Kleikamp 	}
1673ac27a0ecSDave Kleikamp 	if (read_only)
1674ac27a0ecSDave Kleikamp 		return res;
1675617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1676b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1677b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1678617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1679b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1680b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1681b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1682ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1683ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1684ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1685b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1686b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1687b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1688ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1689ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1690ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1691b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1692b31e1552SEric Sandeen 			 "warning: checktime reached, "
1693b31e1552SEric Sandeen 			 "running e2fsck is recommended");
16940390131bSFrank Mayhar 	if (!sbi->s_journal)
1695216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1696ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1697617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1698e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1699ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1700617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
17010390131bSFrank Mayhar 	if (sbi->s_journal)
1702617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1703ac27a0ecSDave Kleikamp 
1704e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1705ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1706a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
17077f4520ccSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x]\n",
1708ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1709ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1710617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1711617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1712ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1713ac27a0ecSDave Kleikamp 
1714ac27a0ecSDave Kleikamp 	return res;
1715ac27a0ecSDave Kleikamp }
1716ac27a0ecSDave Kleikamp 
1717772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1718772cb7c8SJose R. Santos {
1719772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1720772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1721772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1722772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1723772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1724c5ca7c76STheodore Ts'o 	size_t size;
1725772cb7c8SJose R. Santos 	int i;
1726772cb7c8SJose R. Santos 
1727503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1728503358aeSTheodore Ts'o 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1729503358aeSTheodore Ts'o 
1730503358aeSTheodore Ts'o 	if (groups_per_flex < 2) {
1731772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1732772cb7c8SJose R. Santos 		return 1;
1733772cb7c8SJose R. Santos 	}
1734772cb7c8SJose R. Santos 
1735c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1736c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1737d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1738d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1739c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
1740c5ca7c76STheodore Ts'o 	sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1741c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
1742c5ca7c76STheodore Ts'o 		sbi->s_flex_groups = vmalloc(size);
1743c5ca7c76STheodore Ts'o 		if (sbi->s_flex_groups)
1744c5ca7c76STheodore Ts'o 			memset(sbi->s_flex_groups, 0, size);
1745c5ca7c76STheodore Ts'o 	}
1746772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1747b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory for "
1748b31e1552SEric Sandeen 				"%u flex groups", flex_group_count);
1749772cb7c8SJose R. Santos 		goto failed;
1750772cb7c8SJose R. Santos 	}
1751772cb7c8SJose R. Santos 
1752772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
175388b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1754772cb7c8SJose R. Santos 
1755772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
17567ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
17577ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
17587ad9bb65STheodore Ts'o 		atomic_add(ext4_free_blks_count(sb, gdp),
17597ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_blocks);
17607ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
17617ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
1762772cb7c8SJose R. Santos 	}
1763772cb7c8SJose R. Santos 
1764772cb7c8SJose R. Santos 	return 1;
1765772cb7c8SJose R. Santos failed:
1766772cb7c8SJose R. Santos 	return 0;
1767772cb7c8SJose R. Santos }
1768772cb7c8SJose R. Santos 
1769717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1770717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1771717d50e4SAndreas Dilger {
1772717d50e4SAndreas Dilger 	__u16 crc = 0;
1773717d50e4SAndreas Dilger 
1774717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1775717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1776717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1777717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1778717d50e4SAndreas Dilger 
1779717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1780717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1781717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1782717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1783717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1784717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1785717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1786717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1787717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1788717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1789717d50e4SAndreas Dilger 					offset);
1790717d50e4SAndreas Dilger 	}
1791717d50e4SAndreas Dilger 
1792717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1793717d50e4SAndreas Dilger }
1794717d50e4SAndreas Dilger 
1795717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1796717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1797717d50e4SAndreas Dilger {
1798717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1799717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1800717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1801717d50e4SAndreas Dilger 		return 0;
1802717d50e4SAndreas Dilger 
1803717d50e4SAndreas Dilger 	return 1;
1804717d50e4SAndreas Dilger }
1805717d50e4SAndreas Dilger 
1806ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1807617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1808ac27a0ecSDave Kleikamp {
1809617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1810617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1811617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1812bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1813bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1814bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1815ce421581SJose R. Santos 	int flexbg_flag = 0;
1816fd2d4291SAvantika Mathur 	ext4_group_t i;
1817ac27a0ecSDave Kleikamp 
1818ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1819ce421581SJose R. Santos 		flexbg_flag = 1;
1820ce421581SJose R. Santos 
1821617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1822ac27a0ecSDave Kleikamp 
1823197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1824197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1825197cd65aSAkinobu Mita 
1826ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1827bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1828ac27a0ecSDave Kleikamp 		else
1829ac27a0ecSDave Kleikamp 			last_block = first_block +
1830617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1831ac27a0ecSDave Kleikamp 
18328fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
18332b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1834b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1835a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
1836b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
1837ac27a0ecSDave Kleikamp 			return 0;
1838ac27a0ecSDave Kleikamp 		}
18398fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
18402b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1841b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1842a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
1843b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
1844ac27a0ecSDave Kleikamp 			return 0;
1845ac27a0ecSDave Kleikamp 		}
18468fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1847bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
18482b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1849b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1850a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
1851b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
1852ac27a0ecSDave Kleikamp 			return 0;
1853ac27a0ecSDave Kleikamp 		}
1854955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
1855717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1856b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1857b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
1858fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1859fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
18607ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
1861955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
1862717d50e4SAndreas Dilger 				return 0;
1863717d50e4SAndreas Dilger 			}
18647ee1ec4cSLi Zefan 		}
1865955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
1866ce421581SJose R. Santos 		if (!flexbg_flag)
1867617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1868ac27a0ecSDave Kleikamp 	}
1869ac27a0ecSDave Kleikamp 
1870bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1871617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
1872ac27a0ecSDave Kleikamp 	return 1;
1873ac27a0ecSDave Kleikamp }
1874ac27a0ecSDave Kleikamp 
1875617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1876ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1877ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1878ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1879ac27a0ecSDave Kleikamp  *
1880ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1881ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1882ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1883ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1884ac27a0ecSDave Kleikamp  *
1885ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1886ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1887ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1888617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1889ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1890ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1891ac27a0ecSDave Kleikamp  */
1892617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1893617ba13bSMingming Cao 				struct ext4_super_block *es)
1894ac27a0ecSDave Kleikamp {
1895ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1896ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1897ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1898ac27a0ecSDave Kleikamp 	int i;
1899ac27a0ecSDave Kleikamp #endif
1900ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1901ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1902ac27a0ecSDave Kleikamp 		return;
1903ac27a0ecSDave Kleikamp 	}
1904ac27a0ecSDave Kleikamp 
1905a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1906b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
1907b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
1908a8f48a95SEric Sandeen 		return;
1909a8f48a95SEric Sandeen 	}
1910a8f48a95SEric Sandeen 
1911617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1912ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1913ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1914ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1915ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1916ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1917ac27a0ecSDave Kleikamp 		return;
1918ac27a0ecSDave Kleikamp 	}
1919ac27a0ecSDave Kleikamp 
1920ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1921b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
1922ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1923ac27a0ecSDave Kleikamp 	}
1924ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1925ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1926ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1927ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1928ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1929617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1930617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1931ac27a0ecSDave Kleikamp 			if (ret < 0)
1932b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1933b31e1552SEric Sandeen 					"Cannot turn on journaled "
1934b31e1552SEric Sandeen 					"quota: error %d", ret);
1935ac27a0ecSDave Kleikamp 		}
1936ac27a0ecSDave Kleikamp 	}
1937ac27a0ecSDave Kleikamp #endif
1938ac27a0ecSDave Kleikamp 
1939ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1940ac27a0ecSDave Kleikamp 		struct inode *inode;
1941ac27a0ecSDave Kleikamp 
194297bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
194397bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
1944ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1945ac27a0ecSDave Kleikamp 			break;
1946ac27a0ecSDave Kleikamp 		}
1947ac27a0ecSDave Kleikamp 
1948617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1949a269eb18SJan Kara 		vfs_dq_init(inode);
1950ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1951b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
1952b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
195346e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
1954e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1955ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1956617ba13bSMingming Cao 			ext4_truncate(inode);
1957ac27a0ecSDave Kleikamp 			nr_truncates++;
1958ac27a0ecSDave Kleikamp 		} else {
1959b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
1960b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
196146e665e9SHarvey Harrison 				__func__, inode->i_ino);
1962ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1963ac27a0ecSDave Kleikamp 				  inode->i_ino);
1964ac27a0ecSDave Kleikamp 			nr_orphans++;
1965ac27a0ecSDave Kleikamp 		}
1966ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1967ac27a0ecSDave Kleikamp 	}
1968ac27a0ecSDave Kleikamp 
1969ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1970ac27a0ecSDave Kleikamp 
1971ac27a0ecSDave Kleikamp 	if (nr_orphans)
1972b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1973b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
1974ac27a0ecSDave Kleikamp 	if (nr_truncates)
1975b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1976b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
1977ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1978ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1979ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1980ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
19816f28e087SJan Kara 			vfs_quota_off(sb, i, 0);
1982ac27a0ecSDave Kleikamp 	}
1983ac27a0ecSDave Kleikamp #endif
1984ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1985ac27a0ecSDave Kleikamp }
19860b8e58a1SAndreas Dilger 
1987cd2291a4SEric Sandeen /*
1988cd2291a4SEric Sandeen  * Maximal extent format file size.
1989cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1990cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1991cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1992cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1993cd2291a4SEric Sandeen  *
1994cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1995cd2291a4SEric Sandeen  */
1996f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
1997cd2291a4SEric Sandeen {
1998cd2291a4SEric Sandeen 	loff_t res;
1999cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2000cd2291a4SEric Sandeen 
2001cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2002f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2003cd2291a4SEric Sandeen 		/*
200490c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2005cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2006cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2007cd2291a4SEric Sandeen 		 */
2008cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2009cd2291a4SEric Sandeen 
2010cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2011cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2012cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2013cd2291a4SEric Sandeen 	}
2014cd2291a4SEric Sandeen 
2015cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
2016cd2291a4SEric Sandeen 	res = 1LL << 32;
2017cd2291a4SEric Sandeen 	res <<= blkbits;
2018cd2291a4SEric Sandeen 	res -= 1;
2019cd2291a4SEric Sandeen 
2020cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2021cd2291a4SEric Sandeen 	if (res > upper_limit)
2022cd2291a4SEric Sandeen 		res = upper_limit;
2023cd2291a4SEric Sandeen 
2024cd2291a4SEric Sandeen 	return res;
2025cd2291a4SEric Sandeen }
2026ac27a0ecSDave Kleikamp 
2027ac27a0ecSDave Kleikamp /*
2028cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
20290fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
20300fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2031ac27a0ecSDave Kleikamp  */
2032f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2033ac27a0ecSDave Kleikamp {
2034617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
20350fc1b451SAneesh Kumar K.V 	int meta_blocks;
20360fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
20370b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
20380b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
20390b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
20400b8e58a1SAndreas Dilger 	 *
20410b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
20420b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
20430fc1b451SAneesh Kumar K.V 	 */
20440fc1b451SAneesh Kumar K.V 
2045f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
20460fc1b451SAneesh Kumar K.V 		/*
204790c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
20480b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
20490b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
20500fc1b451SAneesh Kumar K.V 		 */
20510fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
20520fc1b451SAneesh Kumar K.V 
20530fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
20540fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
20550fc1b451SAneesh Kumar K.V 
20560fc1b451SAneesh Kumar K.V 	} else {
20578180a562SAneesh Kumar K.V 		/*
20588180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
20598180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
20608180a562SAneesh Kumar K.V 		 * represent total number of blocks in
20618180a562SAneesh Kumar K.V 		 * file system block size
20628180a562SAneesh Kumar K.V 		 */
20630fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
20640fc1b451SAneesh Kumar K.V 
20650fc1b451SAneesh Kumar K.V 	}
20660fc1b451SAneesh Kumar K.V 
20670fc1b451SAneesh Kumar K.V 	/* indirect blocks */
20680fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
20690fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
20700fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
20710fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
20720fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
20730fc1b451SAneesh Kumar K.V 
20740fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
20750fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2076ac27a0ecSDave Kleikamp 
2077ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2078ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2079ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2080ac27a0ecSDave Kleikamp 	res <<= bits;
2081ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2082ac27a0ecSDave Kleikamp 		res = upper_limit;
20830fc1b451SAneesh Kumar K.V 
20840fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
20850fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
20860fc1b451SAneesh Kumar K.V 
2087ac27a0ecSDave Kleikamp 	return res;
2088ac27a0ecSDave Kleikamp }
2089ac27a0ecSDave Kleikamp 
2090617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
209170bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2092ac27a0ecSDave Kleikamp {
2093617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2094fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2095ac27a0ecSDave Kleikamp 	int has_super = 0;
2096ac27a0ecSDave Kleikamp 
2097ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2098ac27a0ecSDave Kleikamp 
2099617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2100ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
210170bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2102ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2103617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2104ac27a0ecSDave Kleikamp 		has_super = 1;
21050b8e58a1SAndreas Dilger 
2106617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2107ac27a0ecSDave Kleikamp }
2108ac27a0ecSDave Kleikamp 
2109c9de560dSAlex Tomas /**
2110c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2111c9de560dSAlex Tomas  * @sbi: In memory super block info
2112c9de560dSAlex Tomas  *
2113c9de560dSAlex Tomas  * If we have specified it via mount option, then
2114c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2115c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2116c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2117c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2118c9de560dSAlex Tomas  *
2119c9de560dSAlex Tomas  */
2120c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2121c9de560dSAlex Tomas {
2122c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2123c9de560dSAlex Tomas 	unsigned long stripe_width =
2124c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2125c9de560dSAlex Tomas 
2126c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2127c9de560dSAlex Tomas 		return sbi->s_stripe;
2128c9de560dSAlex Tomas 
2129c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
2130c9de560dSAlex Tomas 		return stripe_width;
2131c9de560dSAlex Tomas 
2132c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
2133c9de560dSAlex Tomas 		return stride;
2134c9de560dSAlex Tomas 
2135c9de560dSAlex Tomas 	return 0;
2136c9de560dSAlex Tomas }
2137ac27a0ecSDave Kleikamp 
21383197ebdbSTheodore Ts'o /* sysfs supprt */
21393197ebdbSTheodore Ts'o 
21403197ebdbSTheodore Ts'o struct ext4_attr {
21413197ebdbSTheodore Ts'o 	struct attribute attr;
21423197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
21433197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
21443197ebdbSTheodore Ts'o 			 const char *, size_t);
21453197ebdbSTheodore Ts'o 	int offset;
21463197ebdbSTheodore Ts'o };
21473197ebdbSTheodore Ts'o 
21483197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
21493197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
21503197ebdbSTheodore Ts'o {
21513197ebdbSTheodore Ts'o 	char *endp;
21523197ebdbSTheodore Ts'o 
2153e7d2860bSAndré Goddard Rosa 	*value = simple_strtoul(skip_spaces(buf), &endp, 0);
2154e7d2860bSAndré Goddard Rosa 	endp = skip_spaces(endp);
21553197ebdbSTheodore Ts'o 	if (*endp || *value > max)
21563197ebdbSTheodore Ts'o 		return -EINVAL;
21573197ebdbSTheodore Ts'o 
21583197ebdbSTheodore Ts'o 	return 0;
21593197ebdbSTheodore Ts'o }
21603197ebdbSTheodore Ts'o 
21613197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
21623197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
21633197ebdbSTheodore Ts'o 					      char *buf)
21643197ebdbSTheodore Ts'o {
21653197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
21663197ebdbSTheodore Ts'o 			(s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
21673197ebdbSTheodore Ts'o }
21683197ebdbSTheodore Ts'o 
21693197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
21703197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
21713197ebdbSTheodore Ts'o {
21723197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
21733197ebdbSTheodore Ts'o 
21743197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
21753197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
21763197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
21773197ebdbSTheodore Ts'o }
21783197ebdbSTheodore Ts'o 
21793197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
21803197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
21813197ebdbSTheodore Ts'o {
21823197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
21833197ebdbSTheodore Ts'o 
21843197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2185a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
21863197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2187a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
21883197ebdbSTheodore Ts'o }
21893197ebdbSTheodore Ts'o 
21903197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
21913197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
21923197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
21933197ebdbSTheodore Ts'o {
21943197ebdbSTheodore Ts'o 	unsigned long t;
21953197ebdbSTheodore Ts'o 
21963197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
21973197ebdbSTheodore Ts'o 		return -EINVAL;
21983197ebdbSTheodore Ts'o 
2199f7c43950STheodore Ts'o 	if (!is_power_of_2(t))
22003197ebdbSTheodore Ts'o 		return -EINVAL;
22013197ebdbSTheodore Ts'o 
22023197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
22033197ebdbSTheodore Ts'o 	return count;
22043197ebdbSTheodore Ts'o }
22053197ebdbSTheodore Ts'o 
22063197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
22073197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
22083197ebdbSTheodore Ts'o {
22093197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
22103197ebdbSTheodore Ts'o 
22113197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
22123197ebdbSTheodore Ts'o }
22133197ebdbSTheodore Ts'o 
22143197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
22153197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
22163197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
22173197ebdbSTheodore Ts'o {
22183197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
22193197ebdbSTheodore Ts'o 	unsigned long t;
22203197ebdbSTheodore Ts'o 
22213197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
22223197ebdbSTheodore Ts'o 		return -EINVAL;
22233197ebdbSTheodore Ts'o 	*ui = t;
22243197ebdbSTheodore Ts'o 	return count;
22253197ebdbSTheodore Ts'o }
22263197ebdbSTheodore Ts'o 
22273197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
22283197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
22293197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
22303197ebdbSTheodore Ts'o 	.show	= _show,					\
22313197ebdbSTheodore Ts'o 	.store	= _store,					\
22323197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
22333197ebdbSTheodore Ts'o }
22343197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
22353197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
22363197ebdbSTheodore Ts'o 
22373197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
22383197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
22393197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
22403197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
22413197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
22423197ebdbSTheodore Ts'o 
22433197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
22443197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
22453197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
22463197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
22473197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
224811013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
22493197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
22503197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
22513197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
22523197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
22533197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
22543197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
225555138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
22563197ebdbSTheodore Ts'o 
22573197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
22583197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
22593197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
22603197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
22613197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
226211013911SAndreas Dilger 	ATTR_LIST(inode_goal),
22633197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
22643197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
22653197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
22663197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
22673197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
22683197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
226955138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
22703197ebdbSTheodore Ts'o 	NULL,
22713197ebdbSTheodore Ts'o };
22723197ebdbSTheodore Ts'o 
22733197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
22743197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
22753197ebdbSTheodore Ts'o {
22763197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22773197ebdbSTheodore Ts'o 						s_kobj);
22783197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
22793197ebdbSTheodore Ts'o 
22803197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
22813197ebdbSTheodore Ts'o }
22823197ebdbSTheodore Ts'o 
22833197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
22843197ebdbSTheodore Ts'o 			       struct attribute *attr,
22853197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
22863197ebdbSTheodore Ts'o {
22873197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22883197ebdbSTheodore Ts'o 						s_kobj);
22893197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
22903197ebdbSTheodore Ts'o 
22913197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
22923197ebdbSTheodore Ts'o }
22933197ebdbSTheodore Ts'o 
22943197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
22953197ebdbSTheodore Ts'o {
22963197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22973197ebdbSTheodore Ts'o 						s_kobj);
22983197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
22993197ebdbSTheodore Ts'o }
23003197ebdbSTheodore Ts'o 
23013197ebdbSTheodore Ts'o 
23023197ebdbSTheodore Ts'o static struct sysfs_ops ext4_attr_ops = {
23033197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
23043197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
23053197ebdbSTheodore Ts'o };
23063197ebdbSTheodore Ts'o 
23073197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
23083197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
23093197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
23103197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
23113197ebdbSTheodore Ts'o };
23123197ebdbSTheodore Ts'o 
2313a13fb1a4SEric Sandeen /*
2314a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2315a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2316a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2317a13fb1a4SEric Sandeen  * 0 if it cannot be.
2318a13fb1a4SEric Sandeen  */
2319a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2320a13fb1a4SEric Sandeen {
2321a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2322a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2323a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2324a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2325a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2326a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2327a13fb1a4SEric Sandeen 		return 0;
2328a13fb1a4SEric Sandeen 	}
2329a13fb1a4SEric Sandeen 
2330a13fb1a4SEric Sandeen 	if (readonly)
2331a13fb1a4SEric Sandeen 		return 1;
2332a13fb1a4SEric Sandeen 
2333a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2334a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2335a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2336a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2337a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2338a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2339a13fb1a4SEric Sandeen 		return 0;
2340a13fb1a4SEric Sandeen 	}
2341a13fb1a4SEric Sandeen 	/*
2342a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2343a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2344a13fb1a4SEric Sandeen 	 */
2345a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2346a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2347a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2348a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2349a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2350a13fb1a4SEric Sandeen 			return 0;
2351a13fb1a4SEric Sandeen 		}
2352a13fb1a4SEric Sandeen 	}
2353a13fb1a4SEric Sandeen 	return 1;
2354a13fb1a4SEric Sandeen }
2355a13fb1a4SEric Sandeen 
2356617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
23577477827fSAneesh Kumar K.V 				__releases(kernel_lock)
23587477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
2359ac27a0ecSDave Kleikamp {
2360ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2361617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
2362617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2363617ba13bSMingming Cao 	ext4_fsblk_t block;
2364617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
236570bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
2366ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
2367ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
2368ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
2369ac27a0ecSDave Kleikamp 	struct inode *root;
23709f6200bbSTheodore Ts'o 	char *cp;
23710390131bSFrank Mayhar 	const char *descr;
23721d1fe1eeSDavid Howells 	int ret = -EINVAL;
2373ac27a0ecSDave Kleikamp 	int blocksize;
23744ec11028STheodore Ts'o 	unsigned int db_count;
23754ec11028STheodore Ts'o 	unsigned int i;
2376f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
2377bd81d8eeSLaurent Vivier 	__u64 blocks_count;
2378833f4077SPeter Zijlstra 	int err;
2379b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
2380ac27a0ecSDave Kleikamp 
2381ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2382ac27a0ecSDave Kleikamp 	if (!sbi)
2383ac27a0ecSDave Kleikamp 		return -ENOMEM;
2384705895b6SPekka Enberg 
2385705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
2386705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2387705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
2388705895b6SPekka Enberg 		kfree(sbi);
2389705895b6SPekka Enberg 		return -ENOMEM;
2390705895b6SPekka Enberg 	}
2391ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
2392ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
2393617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
2394617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
2395240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2396d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
2397afc32f7eSTheodore Ts'o 	sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2398afc32f7eSTheodore Ts'o 						      sectors[1]);
2399ac27a0ecSDave Kleikamp 
2400ac27a0ecSDave Kleikamp 	unlock_kernel();
2401ac27a0ecSDave Kleikamp 
24029f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
24039f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
24049f6200bbSTheodore Ts'o 		*cp = '!';
24059f6200bbSTheodore Ts'o 
2406617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2407ac27a0ecSDave Kleikamp 	if (!blocksize) {
2408b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
2409ac27a0ecSDave Kleikamp 		goto out_fail;
2410ac27a0ecSDave Kleikamp 	}
2411ac27a0ecSDave Kleikamp 
2412ac27a0ecSDave Kleikamp 	/*
2413617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
2414ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
2415ac27a0ecSDave Kleikamp 	 */
2416617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
241770bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
241870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
2419ac27a0ecSDave Kleikamp 	} else {
242070bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
2421ac27a0ecSDave Kleikamp 	}
2422ac27a0ecSDave Kleikamp 
242370bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
2424b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
2425ac27a0ecSDave Kleikamp 		goto out_fail;
2426ac27a0ecSDave Kleikamp 	}
2427ac27a0ecSDave Kleikamp 	/*
2428ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
2429617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
2430ac27a0ecSDave Kleikamp 	 */
2431617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2432ac27a0ecSDave Kleikamp 	sbi->s_es = es;
2433ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
2434617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
2435617ba13bSMingming Cao 		goto cantfind_ext4;
2436afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
2437ac27a0ecSDave Kleikamp 
2438ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
2439ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2440617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
2441ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
2442617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2443ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
2444617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
2445ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
244603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
2447617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2448ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
24492e7842b8SHugh Dickins #endif
245003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
2451617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
2452ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
24532e7842b8SHugh Dickins #endif
2454617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2455617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2456617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2457617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2458617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2459617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2460ac27a0ecSDave Kleikamp 
2461617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2462ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2463bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2464ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
2465bb4f397aSAneesh Kumar K.V 	else
2466bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
2467ac27a0ecSDave Kleikamp 
2468ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2469ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
247030773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
247130773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
247230773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2473ac27a0ecSDave Kleikamp 
2474571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
2475ac27a0ecSDave Kleikamp 
24761e2462f9SMingming Cao 	/*
2477dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
2478dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
2479dd919b98SAneesh Kumar K.V 	 */
2480dd919b98SAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, DELALLOC);
2481dd919b98SAneesh Kumar K.V 
2482b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
2483b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
2484ac27a0ecSDave Kleikamp 		goto failed_mount;
2485ac27a0ecSDave Kleikamp 
2486ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2487617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2488ac27a0ecSDave Kleikamp 
2489617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2490617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2491617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2492617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2493b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2494b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
2495b31e1552SEric Sandeen 		       "running e2fsck is recommended");
2496469108ffSTheodore Tso 
2497469108ffSTheodore Tso 	/*
2498ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2499ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2500ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2501ac27a0ecSDave Kleikamp 	 */
2502a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
2503ac27a0ecSDave Kleikamp 		goto failed_mount;
2504a13fb1a4SEric Sandeen 
2505ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2506ac27a0ecSDave Kleikamp 
2507617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2508617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2509b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2510b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
2511ac27a0ecSDave Kleikamp 		goto failed_mount;
2512ac27a0ecSDave Kleikamp 	}
2513ac27a0ecSDave Kleikamp 
2514ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2515ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2516ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2517b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
2518ce40733cSAneesh Kumar K.V 					blocksize);
2519ac27a0ecSDave Kleikamp 			goto failed_mount;
2520ac27a0ecSDave Kleikamp 		}
2521ac27a0ecSDave Kleikamp 
2522ac27a0ecSDave Kleikamp 		brelse(bh);
252370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
252470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
252570bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2526ac27a0ecSDave Kleikamp 		if (!bh) {
2527b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2528b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
2529ac27a0ecSDave Kleikamp 			goto failed_mount;
2530ac27a0ecSDave Kleikamp 		}
2531617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2532ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2533617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2534b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2535b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
2536ac27a0ecSDave Kleikamp 			goto failed_mount;
2537ac27a0ecSDave Kleikamp 		}
2538ac27a0ecSDave Kleikamp 	}
2539ac27a0ecSDave Kleikamp 
2540a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2541a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2542f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2543f287a1a5STheodore Ts'o 						      has_huge_files);
2544f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2545ac27a0ecSDave Kleikamp 
2546617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2547617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2548617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2549ac27a0ecSDave Kleikamp 	} else {
2550ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2551ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2552617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
25531330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2554ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2555b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2556b31e1552SEric Sandeen 			       "unsupported inode size: %d",
2557ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2558ac27a0ecSDave Kleikamp 			goto failed_mount;
2559ac27a0ecSDave Kleikamp 		}
2560ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2561ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2562ac27a0ecSDave Kleikamp 	}
25630b8e58a1SAndreas Dilger 
25640d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
25650d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
25668fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
25670d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2568d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
2569b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2570b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
25710d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
25720d1ee42fSAlexandre Ratchov 			goto failed_mount;
25730d1ee42fSAlexandre Ratchov 		}
25740d1ee42fSAlexandre Ratchov 	} else
25750d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
25760b8e58a1SAndreas Dilger 
2577ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2578ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2579b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2580617ba13bSMingming Cao 		goto cantfind_ext4;
25810b8e58a1SAndreas Dilger 
2582617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2583ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2584617ba13bSMingming Cao 		goto cantfind_ext4;
2585ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2586ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
25870d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2588ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2589ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2590e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2591e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
25920b8e58a1SAndreas Dilger 
2593ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2594ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2595ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2596f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
2597f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
2598f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2599f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2600f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
2601f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2602f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2603f99b2589STheodore Ts'o #else
2604f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2605f99b2589STheodore Ts'o #endif
2606f99b2589STheodore Ts'o 		sb->s_dirt = 1;
2607f99b2589STheodore Ts'o 	}
2608ac27a0ecSDave Kleikamp 
2609ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2610b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2611b31e1552SEric Sandeen 		       "#blocks per group too big: %lu",
2612ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2613ac27a0ecSDave Kleikamp 		goto failed_mount;
2614ac27a0ecSDave Kleikamp 	}
2615ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2616b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2617b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
2618ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2619ac27a0ecSDave Kleikamp 		goto failed_mount;
2620ac27a0ecSDave Kleikamp 	}
2621ac27a0ecSDave Kleikamp 
2622bf43d84bSEric Sandeen 	/*
2623bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
2624bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
2625bf43d84bSEric Sandeen 	 */
2626bf43d84bSEric Sandeen 	if ((ext4_blocks_count(es) >
2627bf43d84bSEric Sandeen 	     (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2628bf43d84bSEric Sandeen 	    (ext4_blocks_count(es) >
2629bf43d84bSEric Sandeen 	     (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
2630b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
2631bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
2632ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
263390c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
2634bf43d84bSEric Sandeen 		ret = -EFBIG;
2635ac27a0ecSDave Kleikamp 		goto failed_mount;
2636ac27a0ecSDave Kleikamp 	}
2637ac27a0ecSDave Kleikamp 
2638617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2639617ba13bSMingming Cao 		goto cantfind_ext4;
2640e7c95593SEric Sandeen 
26410f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
26420f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
26430f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2644b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2645b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
26460f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
26470f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
26480f2ddca6SFrom: Thiemo Nagel 	}
26490f2ddca6SFrom: Thiemo Nagel 
26504ec11028STheodore Ts'o 	/*
26514ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
26524ec11028STheodore Ts'o 	 * of the filesystem.
26534ec11028STheodore Ts'o 	 */
26544ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2655b31e1552SEric Sandeen                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2656b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
2657e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
26584ec11028STheodore Ts'o 			 ext4_blocks_count(es));
2659e7c95593SEric Sandeen 		goto failed_mount;
2660e7c95593SEric Sandeen 	}
2661bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2662bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2663bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2664bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
26654ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2666b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
26674ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
2668b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
26694ec11028STheodore Ts'o 		       ext4_blocks_count(es),
26704ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
26714ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
26724ec11028STheodore Ts'o 		goto failed_mount;
26734ec11028STheodore Ts'o 	}
2674bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2675fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2676fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
2677617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2678617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2679ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2680ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2681ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2682b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
2683ac27a0ecSDave Kleikamp 		goto failed_mount;
2684ac27a0ecSDave Kleikamp 	}
2685ac27a0ecSDave Kleikamp 
26863244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
26879f6200bbSTheodore Ts'o 	if (ext4_proc_root)
26889f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
26893244fcb1SAlexander Beregalov #endif
2690240799cdSTheodore Ts'o 
2691705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
2692ac27a0ecSDave Kleikamp 
2693ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
269470bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2695ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2696ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2697b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2698b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
2699ac27a0ecSDave Kleikamp 			db_count = i;
2700ac27a0ecSDave Kleikamp 			goto failed_mount2;
2701ac27a0ecSDave Kleikamp 		}
2702ac27a0ecSDave Kleikamp 	}
2703617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2704b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
2705ac27a0ecSDave Kleikamp 		goto failed_mount2;
2706ac27a0ecSDave Kleikamp 	}
2707772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2708772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2709b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2710b31e1552SEric Sandeen 			       "unable to initialize "
2711b31e1552SEric Sandeen 			       "flex_bg meta info!");
2712772cb7c8SJose R. Santos 			goto failed_mount2;
2713772cb7c8SJose R. Santos 		}
2714772cb7c8SJose R. Santos 
2715ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2716ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2717ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2718ac27a0ecSDave Kleikamp 
2719833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2720617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2721833f4077SPeter Zijlstra 	if (!err) {
2722833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2723617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2724833f4077SPeter Zijlstra 	}
2725833f4077SPeter Zijlstra 	if (!err) {
2726833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2727617ba13bSMingming Cao 				ext4_count_dirs(sb));
2728833f4077SPeter Zijlstra 	}
27296bc6e63fSAneesh Kumar K.V 	if (!err) {
27306bc6e63fSAneesh Kumar K.V 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
27316bc6e63fSAneesh Kumar K.V 	}
2732833f4077SPeter Zijlstra 	if (err) {
2733b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "insufficient memory");
2734833f4077SPeter Zijlstra 		goto failed_mount3;
2735833f4077SPeter Zijlstra 	}
2736ac27a0ecSDave Kleikamp 
2737c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
273855138e0bSTheodore Ts'o 	sbi->s_max_writeback_mb_bump = 128;
2739c9de560dSAlex Tomas 
2740ac27a0ecSDave Kleikamp 	/*
2741ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2742ac27a0ecSDave Kleikamp 	 */
27439ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
27449ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2745617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
27469ca92389STheodore Ts'o 	else
27479ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
2748617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2749617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2750ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2751617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2752617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2753ac27a0ecSDave Kleikamp #endif
2754ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
27553b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
275632ed5058STheodore Ts'o 	mutex_init(&sbi->s_resize_lock);
2757ac27a0ecSDave Kleikamp 
2758ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2759ac27a0ecSDave Kleikamp 
2760ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2761617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2762617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2763ac27a0ecSDave Kleikamp 
2764ac27a0ecSDave Kleikamp 	/*
2765ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2766ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2767ac27a0ecSDave Kleikamp 	 */
2768ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2769617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2770617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2771ac27a0ecSDave Kleikamp 			goto failed_mount3;
27720390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
27730390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2774b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
2775b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
27760390131bSFrank Mayhar 		goto failed_mount4;
2777ac27a0ecSDave Kleikamp 	} else {
27780390131bSFrank Mayhar 		clear_opt(sbi->s_mount_opt, DATA_FLAGS);
27790390131bSFrank Mayhar 		set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
27800390131bSFrank Mayhar 		sbi->s_journal = NULL;
27810390131bSFrank Mayhar 		needs_recovery = 0;
27820390131bSFrank Mayhar 		goto no_journal;
2783ac27a0ecSDave Kleikamp 	}
2784ac27a0ecSDave Kleikamp 
2785eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2786eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2787eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2788b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
2789eb40a09cSJose R. Santos 		goto failed_mount4;
2790eb40a09cSJose R. Santos 	}
2791eb40a09cSJose R. Santos 
2792d4da6c9cSLinus Torvalds 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2793d4da6c9cSLinus Torvalds 		jbd2_journal_set_features(sbi->s_journal,
2794d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2795d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2796d4da6c9cSLinus Torvalds 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2797818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2798818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2799818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2800818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2801d4da6c9cSLinus Torvalds 	} else {
2802d4da6c9cSLinus Torvalds 		jbd2_journal_clear_features(sbi->s_journal,
2803d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2804d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2805d4da6c9cSLinus Torvalds 	}
2806818d276cSGirish Shilamkar 
2807ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2808ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2809ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2810ac27a0ecSDave Kleikamp 	case 0:
2811ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
281263f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
281363f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
281463f57933SAndrew Morton 		 */
2815dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2816dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2817ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2818ac27a0ecSDave Kleikamp 		else
2819ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2820ac27a0ecSDave Kleikamp 		break;
2821ac27a0ecSDave Kleikamp 
2822617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2823617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2824dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2825dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2826b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
2827b31e1552SEric Sandeen 			       "requested data journaling mode");
2828ac27a0ecSDave Kleikamp 			goto failed_mount4;
2829ac27a0ecSDave Kleikamp 		}
2830ac27a0ecSDave Kleikamp 	default:
2831ac27a0ecSDave Kleikamp 		break;
2832ac27a0ecSDave Kleikamp 	}
2833b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
2834ac27a0ecSDave Kleikamp 
28350390131bSFrank Mayhar no_journal:
2836ac27a0ecSDave Kleikamp 
2837ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2838617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2839b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2840b31e1552SEric Sandeen 				"its supported only with writeback mode");
2841ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2842ac27a0ecSDave Kleikamp 		}
2843ac27a0ecSDave Kleikamp 	}
28444c0425ffSMingming Cao 	EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
28454c0425ffSMingming Cao 	if (!EXT4_SB(sb)->dio_unwritten_wq) {
28464c0425ffSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
28474c0425ffSMingming Cao 		goto failed_mount_wq;
28484c0425ffSMingming Cao 	}
28494c0425ffSMingming Cao 
2850ac27a0ecSDave Kleikamp 	/*
2851dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2852ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2853ac27a0ecSDave Kleikamp 	 */
2854ac27a0ecSDave Kleikamp 
28551d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
28561d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2857b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
28581d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2859ac27a0ecSDave Kleikamp 		goto failed_mount4;
2860ac27a0ecSDave Kleikamp 	}
2861ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
28621d1fe1eeSDavid Howells 		iput(root);
2863b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
2864ac27a0ecSDave Kleikamp 		goto failed_mount4;
2865ac27a0ecSDave Kleikamp 	}
28661d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
28671d1fe1eeSDavid Howells 	if (!sb->s_root) {
2868b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
28691d1fe1eeSDavid Howells 		iput(root);
28701d1fe1eeSDavid Howells 		ret = -ENOMEM;
28711d1fe1eeSDavid Howells 		goto failed_mount4;
28721d1fe1eeSDavid Howells 	}
2873ac27a0ecSDave Kleikamp 
2874617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2875ef7f3835SKalpak Shah 
2876ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2877ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2878ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2879ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2880ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2881ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2882ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2883ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2884ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2885ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2886ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2887ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2888ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2889ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2890ef7f3835SKalpak Shah 		}
2891ef7f3835SKalpak Shah 	}
2892ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2893ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2894ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2895ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2896ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2897b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
2898b31e1552SEric Sandeen 			 "available");
2899ef7f3835SKalpak Shah 	}
2900ef7f3835SKalpak Shah 
290190576c0bSTheodore Ts'o 	if (test_opt(sb, DELALLOC) &&
290290576c0bSTheodore Ts'o 	    (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
2903b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2904b31e1552SEric Sandeen 			 "requested data journaling mode");
2905c2774d84SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
290690576c0bSTheodore Ts'o 	}
2907c2774d84SAneesh Kumar K.V 
29086fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
29096fd058f7STheodore Ts'o 	if (err) {
2910b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
29116fd058f7STheodore Ts'o 			 "zone (%d)\n", err);
29126fd058f7STheodore Ts'o 		goto failed_mount4;
29136fd058f7STheodore Ts'o 	}
29146fd058f7STheodore Ts'o 
2915c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
2916c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
2917c2774d84SAneesh Kumar K.V 	if (err) {
2918b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2919c2774d84SAneesh Kumar K.V 			 err);
2920c2774d84SAneesh Kumar K.V 		goto failed_mount4;
2921c2774d84SAneesh Kumar K.V 	}
2922c2774d84SAneesh Kumar K.V 
29233197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
29243197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
29253197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
29263197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
29273197ebdbSTheodore Ts'o 	if (err) {
29283197ebdbSTheodore Ts'o 		ext4_mb_release(sb);
29293197ebdbSTheodore Ts'o 		ext4_ext_release(sb);
29303197ebdbSTheodore Ts'o 		goto failed_mount4;
29313197ebdbSTheodore Ts'o 	};
29323197ebdbSTheodore Ts'o 
2933617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2934617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2935617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
29360390131bSFrank Mayhar 	if (needs_recovery) {
2937b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
2938617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
29390390131bSFrank Mayhar 	}
29400390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
29410390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
29420390131bSFrank Mayhar 			descr = " journalled data mode";
29430390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
29440390131bSFrank Mayhar 			descr = " ordered data mode";
29450390131bSFrank Mayhar 		else
29460390131bSFrank Mayhar 			descr = " writeback data mode";
29470390131bSFrank Mayhar 	} else
29480390131bSFrank Mayhar 		descr = "out journal";
29490390131bSFrank Mayhar 
2950b31e1552SEric Sandeen 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
2951ac27a0ecSDave Kleikamp 
2952ac27a0ecSDave Kleikamp 	lock_kernel();
2953ac27a0ecSDave Kleikamp 	return 0;
2954ac27a0ecSDave Kleikamp 
2955617ba13bSMingming Cao cantfind_ext4:
2956ac27a0ecSDave Kleikamp 	if (!silent)
2957b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
2958ac27a0ecSDave Kleikamp 	goto failed_mount;
2959ac27a0ecSDave Kleikamp 
2960ac27a0ecSDave Kleikamp failed_mount4:
2961b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
29624c0425ffSMingming Cao 	destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
29634c0425ffSMingming Cao failed_mount_wq:
29646fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
29650390131bSFrank Mayhar 	if (sbi->s_journal) {
2966dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
296747b4a50bSJan Kara 		sbi->s_journal = NULL;
29680390131bSFrank Mayhar 	}
2969ac27a0ecSDave Kleikamp failed_mount3:
2970c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups) {
2971c5ca7c76STheodore Ts'o 		if (is_vmalloc_addr(sbi->s_flex_groups))
2972c5ca7c76STheodore Ts'o 			vfree(sbi->s_flex_groups);
2973c5ca7c76STheodore Ts'o 		else
2974c5ca7c76STheodore Ts'o 			kfree(sbi->s_flex_groups);
2975c5ca7c76STheodore Ts'o 	}
2976ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2977ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2978ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
29796bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2980ac27a0ecSDave Kleikamp failed_mount2:
2981ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2982ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2983ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2984ac27a0ecSDave Kleikamp failed_mount:
2985240799cdSTheodore Ts'o 	if (sbi->s_proc) {
29869f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
2987240799cdSTheodore Ts'o 	}
2988ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2989ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2990ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2991ac27a0ecSDave Kleikamp #endif
2992617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2993ac27a0ecSDave Kleikamp 	brelse(bh);
2994ac27a0ecSDave Kleikamp out_fail:
2995ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2996f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
2997ac27a0ecSDave Kleikamp 	kfree(sbi);
2998ac27a0ecSDave Kleikamp 	lock_kernel();
29991d1fe1eeSDavid Howells 	return ret;
3000ac27a0ecSDave Kleikamp }
3001ac27a0ecSDave Kleikamp 
3002ac27a0ecSDave Kleikamp /*
3003ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
3004ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
3005ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
3006ac27a0ecSDave Kleikamp  */
3007617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
3008ac27a0ecSDave Kleikamp {
3009617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3010ac27a0ecSDave Kleikamp 
3011ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
301230773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
301330773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
3014ac27a0ecSDave Kleikamp 
3015ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
3016ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
3017dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
3018ac27a0ecSDave Kleikamp 	else
3019dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
30205bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
30215bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
30225bf5683aSHidehiro Kawai 	else
30235bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
3024ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
3025ac27a0ecSDave Kleikamp }
3026ac27a0ecSDave Kleikamp 
3027617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
3028ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
3029ac27a0ecSDave Kleikamp {
3030ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
3031ac27a0ecSDave Kleikamp 	journal_t *journal;
3032ac27a0ecSDave Kleikamp 
30330390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
30340390131bSFrank Mayhar 
3035ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
3036ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
3037ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
3038ac27a0ecSDave Kleikamp 
30391d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
30401d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
3041b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
3042ac27a0ecSDave Kleikamp 		return NULL;
3043ac27a0ecSDave Kleikamp 	}
3044ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
3045ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
3046ac27a0ecSDave Kleikamp 		iput(journal_inode);
3047b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3048ac27a0ecSDave Kleikamp 		return NULL;
3049ac27a0ecSDave Kleikamp 	}
3050ac27a0ecSDave Kleikamp 
3051e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3052ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
30531d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
3054b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
3055ac27a0ecSDave Kleikamp 		iput(journal_inode);
3056ac27a0ecSDave Kleikamp 		return NULL;
3057ac27a0ecSDave Kleikamp 	}
3058ac27a0ecSDave Kleikamp 
3059dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
3060ac27a0ecSDave Kleikamp 	if (!journal) {
3061b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3062ac27a0ecSDave Kleikamp 		iput(journal_inode);
3063ac27a0ecSDave Kleikamp 		return NULL;
3064ac27a0ecSDave Kleikamp 	}
3065ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3066617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3067ac27a0ecSDave Kleikamp 	return journal;
3068ac27a0ecSDave Kleikamp }
3069ac27a0ecSDave Kleikamp 
3070617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
3071ac27a0ecSDave Kleikamp 				       dev_t j_dev)
3072ac27a0ecSDave Kleikamp {
3073ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3074ac27a0ecSDave Kleikamp 	journal_t *journal;
3075617ba13bSMingming Cao 	ext4_fsblk_t start;
3076617ba13bSMingming Cao 	ext4_fsblk_t len;
3077ac27a0ecSDave Kleikamp 	int hblock, blocksize;
3078617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
3079ac27a0ecSDave Kleikamp 	unsigned long offset;
3080617ba13bSMingming Cao 	struct ext4_super_block *es;
3081ac27a0ecSDave Kleikamp 	struct block_device *bdev;
3082ac27a0ecSDave Kleikamp 
30830390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
30840390131bSFrank Mayhar 
3085b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
3086ac27a0ecSDave Kleikamp 	if (bdev == NULL)
3087ac27a0ecSDave Kleikamp 		return NULL;
3088ac27a0ecSDave Kleikamp 
3089ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
3090b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3091b31e1552SEric Sandeen 			"failed to claim external journal device");
30929a1c3542SAl Viro 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
3093ac27a0ecSDave Kleikamp 		return NULL;
3094ac27a0ecSDave Kleikamp 	}
3095ac27a0ecSDave Kleikamp 
3096ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
3097e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
3098ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
3099b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3100b31e1552SEric Sandeen 			"blocksize too small for journal device");
3101ac27a0ecSDave Kleikamp 		goto out_bdev;
3102ac27a0ecSDave Kleikamp 	}
3103ac27a0ecSDave Kleikamp 
3104617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3105617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3106ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3107ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3108b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3109b31e1552SEric Sandeen 		       "external journal");
3110ac27a0ecSDave Kleikamp 		goto out_bdev;
3111ac27a0ecSDave Kleikamp 	}
3112ac27a0ecSDave Kleikamp 
3113617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3114617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3115ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3116617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3117b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
3118b31e1552SEric Sandeen 					"bad superblock");
3119ac27a0ecSDave Kleikamp 		brelse(bh);
3120ac27a0ecSDave Kleikamp 		goto out_bdev;
3121ac27a0ecSDave Kleikamp 	}
3122ac27a0ecSDave Kleikamp 
3123617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3124b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3125ac27a0ecSDave Kleikamp 		brelse(bh);
3126ac27a0ecSDave Kleikamp 		goto out_bdev;
3127ac27a0ecSDave Kleikamp 	}
3128ac27a0ecSDave Kleikamp 
3129bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
3130ac27a0ecSDave Kleikamp 	start = sb_block + 1;
3131ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
3132ac27a0ecSDave Kleikamp 
3133dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3134ac27a0ecSDave Kleikamp 					start, len, blocksize);
3135ac27a0ecSDave Kleikamp 	if (!journal) {
3136b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
3137ac27a0ecSDave Kleikamp 		goto out_bdev;
3138ac27a0ecSDave Kleikamp 	}
3139ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3140ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
3141ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
3142ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
3143b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3144ac27a0ecSDave Kleikamp 		goto out_journal;
3145ac27a0ecSDave Kleikamp 	}
3146ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3147b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
3148b31e1552SEric Sandeen 					"user (unsupported) - %d",
3149ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
3150ac27a0ecSDave Kleikamp 		goto out_journal;
3151ac27a0ecSDave Kleikamp 	}
3152617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
3153617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3154ac27a0ecSDave Kleikamp 	return journal;
31550b8e58a1SAndreas Dilger 
3156ac27a0ecSDave Kleikamp out_journal:
3157dab291afSMingming Cao 	jbd2_journal_destroy(journal);
3158ac27a0ecSDave Kleikamp out_bdev:
3159617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
3160ac27a0ecSDave Kleikamp 	return NULL;
3161ac27a0ecSDave Kleikamp }
3162ac27a0ecSDave Kleikamp 
3163617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
3164617ba13bSMingming Cao 			     struct ext4_super_block *es,
3165ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
3166ac27a0ecSDave Kleikamp {
3167ac27a0ecSDave Kleikamp 	journal_t *journal;
3168ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3169ac27a0ecSDave Kleikamp 	dev_t journal_dev;
3170ac27a0ecSDave Kleikamp 	int err = 0;
3171ac27a0ecSDave Kleikamp 	int really_read_only;
3172ac27a0ecSDave Kleikamp 
31730390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
31740390131bSFrank Mayhar 
3175ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3176ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3177b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3178b31e1552SEric Sandeen 			"numbers have changed");
3179ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
3180ac27a0ecSDave Kleikamp 	} else
3181ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3182ac27a0ecSDave Kleikamp 
3183ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
3184ac27a0ecSDave Kleikamp 
3185ac27a0ecSDave Kleikamp 	/*
3186ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
3187ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
3188ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
3189ac27a0ecSDave Kleikamp 	 */
3190617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3191ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
3192b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
3193b31e1552SEric Sandeen 					"required on readonly filesystem");
3194ac27a0ecSDave Kleikamp 			if (really_read_only) {
3195b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
3196b31e1552SEric Sandeen 					"unavailable, cannot proceed");
3197ac27a0ecSDave Kleikamp 				return -EROFS;
3198ac27a0ecSDave Kleikamp 			}
3199b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
3200b31e1552SEric Sandeen 			       "be enabled during recovery");
3201ac27a0ecSDave Kleikamp 		}
3202ac27a0ecSDave Kleikamp 	}
3203ac27a0ecSDave Kleikamp 
3204ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
3205b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3206b31e1552SEric Sandeen 		       "and inode journals!");
3207ac27a0ecSDave Kleikamp 		return -EINVAL;
3208ac27a0ecSDave Kleikamp 	}
3209ac27a0ecSDave Kleikamp 
3210ac27a0ecSDave Kleikamp 	if (journal_inum) {
3211617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
3212ac27a0ecSDave Kleikamp 			return -EINVAL;
3213ac27a0ecSDave Kleikamp 	} else {
3214617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3215ac27a0ecSDave Kleikamp 			return -EINVAL;
3216ac27a0ecSDave Kleikamp 	}
3217ac27a0ecSDave Kleikamp 
321890576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
3219b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
32204776004fSTheodore Ts'o 
3221ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3222dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
3223ac27a0ecSDave Kleikamp 		if (err)  {
3224b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error updating journal");
3225dab291afSMingming Cao 			jbd2_journal_destroy(journal);
3226ac27a0ecSDave Kleikamp 			return err;
3227ac27a0ecSDave Kleikamp 		}
3228ac27a0ecSDave Kleikamp 	}
3229ac27a0ecSDave Kleikamp 
3230617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3231dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
3232ac27a0ecSDave Kleikamp 	if (!err)
3233dab291afSMingming Cao 		err = jbd2_journal_load(journal);
3234ac27a0ecSDave Kleikamp 
3235ac27a0ecSDave Kleikamp 	if (err) {
3236b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
3237dab291afSMingming Cao 		jbd2_journal_destroy(journal);
3238ac27a0ecSDave Kleikamp 		return err;
3239ac27a0ecSDave Kleikamp 	}
3240ac27a0ecSDave Kleikamp 
3241617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
3242617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
3243ac27a0ecSDave Kleikamp 
3244ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3245ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3246ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
3247ac27a0ecSDave Kleikamp 
3248ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
3249e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3250ac27a0ecSDave Kleikamp 	}
3251ac27a0ecSDave Kleikamp 
3252ac27a0ecSDave Kleikamp 	return 0;
3253ac27a0ecSDave Kleikamp }
3254ac27a0ecSDave Kleikamp 
3255e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
3256ac27a0ecSDave Kleikamp {
3257e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3258617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3259c4be0c1dSTakashi Sato 	int error = 0;
3260ac27a0ecSDave Kleikamp 
3261ac27a0ecSDave Kleikamp 	if (!sbh)
3262c4be0c1dSTakashi Sato 		return error;
3263914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
3264914258bfSTheodore Ts'o 		/*
3265914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
3266914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
3267914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
3268914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
3269914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
3270914258bfSTheodore Ts'o 		 * write and hope for the best.
3271914258bfSTheodore Ts'o 		 */
3272b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
3273b31e1552SEric Sandeen 		       "superblock detected");
3274914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
3275914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
3276914258bfSTheodore Ts'o 	}
327771290b36STheodore Ts'o 	/*
327871290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
327971290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
328071290b36STheodore Ts'o 	 * write time when we are mounting the root file system
328171290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
328271290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
328371290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
328471290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
328571290b36STheodore Ts'o 	 * to complain and force a full file system check.
328671290b36STheodore Ts'o 	 */
328771290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
3288ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
3289afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
3290afc32f7eSTheodore Ts'o 		cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3291afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3292afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
32935d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
32945d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeblocks_counter));
32955d1b1b3fSAneesh Kumar K.V 	es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
32965d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeinodes_counter));
32977234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
3298ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
3299ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
3300914258bfSTheodore Ts'o 	if (sync) {
3301c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
3302c4be0c1dSTakashi Sato 		if (error)
3303c4be0c1dSTakashi Sato 			return error;
3304c4be0c1dSTakashi Sato 
3305c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
3306c4be0c1dSTakashi Sato 		if (error) {
3307b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
3308b31e1552SEric Sandeen 			       "superblock");
3309914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
3310914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
3311914258bfSTheodore Ts'o 		}
3312914258bfSTheodore Ts'o 	}
3313c4be0c1dSTakashi Sato 	return error;
3314ac27a0ecSDave Kleikamp }
3315ac27a0ecSDave Kleikamp 
3316ac27a0ecSDave Kleikamp /*
3317ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
3318ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
3319ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
3320ac27a0ecSDave Kleikamp  */
3321617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
3322617ba13bSMingming Cao 					struct ext4_super_block *es)
3323ac27a0ecSDave Kleikamp {
3324617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
3325ac27a0ecSDave Kleikamp 
33260390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
33270390131bSFrank Mayhar 		BUG_ON(journal != NULL);
33280390131bSFrank Mayhar 		return;
33290390131bSFrank Mayhar 	}
3330dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
33317ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
33327ffe1ea8SHidehiro Kawai 		goto out;
33337ffe1ea8SHidehiro Kawai 
3334617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3335ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
3336617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3337e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3338ac27a0ecSDave Kleikamp 	}
33397ffe1ea8SHidehiro Kawai 
33407ffe1ea8SHidehiro Kawai out:
3341dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
3342ac27a0ecSDave Kleikamp }
3343ac27a0ecSDave Kleikamp 
3344ac27a0ecSDave Kleikamp /*
3345ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
3346ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
3347ac27a0ecSDave Kleikamp  * main filesystem now.
3348ac27a0ecSDave Kleikamp  */
3349617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
3350617ba13bSMingming Cao 				   struct ext4_super_block *es)
3351ac27a0ecSDave Kleikamp {
3352ac27a0ecSDave Kleikamp 	journal_t *journal;
3353ac27a0ecSDave Kleikamp 	int j_errno;
3354ac27a0ecSDave Kleikamp 	const char *errstr;
3355ac27a0ecSDave Kleikamp 
33560390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
33570390131bSFrank Mayhar 
3358617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
3359ac27a0ecSDave Kleikamp 
3360ac27a0ecSDave Kleikamp 	/*
3361ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
3362617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
3363ac27a0ecSDave Kleikamp 	 */
3364ac27a0ecSDave Kleikamp 
3365dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
3366ac27a0ecSDave Kleikamp 	if (j_errno) {
3367ac27a0ecSDave Kleikamp 		char nbuf[16];
3368ac27a0ecSDave Kleikamp 
3369617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
337046e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Filesystem error recorded "
3371ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
337246e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Marking fs in need of "
3373ac27a0ecSDave Kleikamp 			     "filesystem check.");
3374ac27a0ecSDave Kleikamp 
3375617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3376617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3377e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3378ac27a0ecSDave Kleikamp 
3379dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
3380ac27a0ecSDave Kleikamp 	}
3381ac27a0ecSDave Kleikamp }
3382ac27a0ecSDave Kleikamp 
3383ac27a0ecSDave Kleikamp /*
3384ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
3385ac27a0ecSDave Kleikamp  * and wait on the commit.
3386ac27a0ecSDave Kleikamp  */
3387617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
3388ac27a0ecSDave Kleikamp {
3389ac27a0ecSDave Kleikamp 	journal_t *journal;
33900390131bSFrank Mayhar 	int ret = 0;
3391ac27a0ecSDave Kleikamp 
3392ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
3393ac27a0ecSDave Kleikamp 		return 0;
3394ac27a0ecSDave Kleikamp 
3395617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
33967234ab2aSTheodore Ts'o 	if (journal)
3397617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
33980390131bSFrank Mayhar 
3399ac27a0ecSDave Kleikamp 	return ret;
3400ac27a0ecSDave Kleikamp }
3401ac27a0ecSDave Kleikamp 
3402617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
3403ac27a0ecSDave Kleikamp {
3404ebc1ac16SChristoph Hellwig 	lock_super(sb);
3405e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3406ebc1ac16SChristoph Hellwig 	unlock_super(sb);
34070390131bSFrank Mayhar }
3408ac27a0ecSDave Kleikamp 
3409617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
3410ac27a0ecSDave Kleikamp {
341114ce0cb4STheodore Ts'o 	int ret = 0;
34129eddacf9SJan Kara 	tid_t target;
34138d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3414ac27a0ecSDave Kleikamp 
34159bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
34168d5d02e6SMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
34178d5d02e6SMingming Cao 	if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
3418ac27a0ecSDave Kleikamp 		if (wait)
34198d5d02e6SMingming Cao 			jbd2_log_wait_commit(sbi->s_journal, target);
34200390131bSFrank Mayhar 	}
342114ce0cb4STheodore Ts'o 	return ret;
3422ac27a0ecSDave Kleikamp }
3423ac27a0ecSDave Kleikamp 
3424ac27a0ecSDave Kleikamp /*
3425ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
3426ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
3427ac27a0ecSDave Kleikamp  */
3428c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
3429ac27a0ecSDave Kleikamp {
3430c4be0c1dSTakashi Sato 	int error = 0;
3431c4be0c1dSTakashi Sato 	journal_t *journal;
3432ac27a0ecSDave Kleikamp 
34339ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
34349ca92389STheodore Ts'o 		return 0;
34359ca92389STheodore Ts'o 
3436c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
3437ac27a0ecSDave Kleikamp 
3438ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
3439dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
34407ffe1ea8SHidehiro Kawai 
34417ffe1ea8SHidehiro Kawai 	/*
34429ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
34439ca92389STheodore Ts'o 	 * the journal.
34447ffe1ea8SHidehiro Kawai 	 */
3445c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
34469ca92389STheodore Ts'o 	if (error < 0) {
34479ca92389STheodore Ts'o 	out:
34489ca92389STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
34499ca92389STheodore Ts'o 		return error;
34500390131bSFrank Mayhar 	}
3451ac27a0ecSDave Kleikamp 
3452ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
3453617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3454e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
3455c4be0c1dSTakashi Sato 	if (error)
3456c4be0c1dSTakashi Sato 		goto out;
3457c4be0c1dSTakashi Sato 	return 0;
3458ac27a0ecSDave Kleikamp }
3459ac27a0ecSDave Kleikamp 
3460ac27a0ecSDave Kleikamp /*
3461ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
3462ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
3463ac27a0ecSDave Kleikamp  */
3464c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
3465ac27a0ecSDave Kleikamp {
34669ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
34679ca92389STheodore Ts'o 		return 0;
34689ca92389STheodore Ts'o 
3469ac27a0ecSDave Kleikamp 	lock_super(sb);
34709ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
3471617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3472e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3473ac27a0ecSDave Kleikamp 	unlock_super(sb);
3474dab291afSMingming Cao 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3475c4be0c1dSTakashi Sato 	return 0;
3476ac27a0ecSDave Kleikamp }
3477ac27a0ecSDave Kleikamp 
3478617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
3479ac27a0ecSDave Kleikamp {
3480617ba13bSMingming Cao 	struct ext4_super_block *es;
3481617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3482617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
3483ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
3484617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
34858a266467STheodore Ts'o 	ext4_group_t g;
3486b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3487ac27a0ecSDave Kleikamp 	int err;
3488ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3489ac27a0ecSDave Kleikamp 	int i;
3490ac27a0ecSDave Kleikamp #endif
3491ac27a0ecSDave Kleikamp 
3492337eb00aSAlessio Igor Bogani 	lock_kernel();
3493337eb00aSAlessio Igor Bogani 
3494ac27a0ecSDave Kleikamp 	/* Store the original options */
3495bbd6851aSAl Viro 	lock_super(sb);
3496ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
3497ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
3498ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
3499ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
3500ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
350130773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
350230773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
3503ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3504ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3505ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3506ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3507ac27a0ecSDave Kleikamp #endif
3508b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3509b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
3510ac27a0ecSDave Kleikamp 
3511ac27a0ecSDave Kleikamp 	/*
3512ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
3513ac27a0ecSDave Kleikamp 	 */
3514b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
3515b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
3516ac27a0ecSDave Kleikamp 		err = -EINVAL;
3517ac27a0ecSDave Kleikamp 		goto restore_opts;
3518ac27a0ecSDave Kleikamp 	}
3519ac27a0ecSDave Kleikamp 
35204ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
352146e665e9SHarvey Harrison 		ext4_abort(sb, __func__, "Abort forced by user");
3522ac27a0ecSDave Kleikamp 
3523ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3524617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3525ac27a0ecSDave Kleikamp 
3526ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3527ac27a0ecSDave Kleikamp 
3528b3881f74STheodore Ts'o 	if (sbi->s_journal) {
3529617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
3530b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3531b3881f74STheodore Ts'o 	}
3532ac27a0ecSDave Kleikamp 
3533ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3534bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
35354ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
3536ac27a0ecSDave Kleikamp 			err = -EROFS;
3537ac27a0ecSDave Kleikamp 			goto restore_opts;
3538ac27a0ecSDave Kleikamp 		}
3539ac27a0ecSDave Kleikamp 
3540ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
3541ac27a0ecSDave Kleikamp 			/*
3542ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3543ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3544ac27a0ecSDave Kleikamp 			 */
3545ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3546ac27a0ecSDave Kleikamp 
3547ac27a0ecSDave Kleikamp 			/*
3548ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3549ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3550ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3551ac27a0ecSDave Kleikamp 			 */
3552617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3553617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3554ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3555ac27a0ecSDave Kleikamp 
3556a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
3557617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
3558ac27a0ecSDave Kleikamp 		} else {
3559a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
3560a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
3561ac27a0ecSDave Kleikamp 				err = -EROFS;
3562ac27a0ecSDave Kleikamp 				goto restore_opts;
3563ac27a0ecSDave Kleikamp 			}
3564ead6596bSEric Sandeen 			/*
35658a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
35660b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
35678a266467STheodore Ts'o 			 */
35688a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
35698a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
35708a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
35718a266467STheodore Ts'o 
35728a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3573b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
3574b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
35758a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
35768a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
35778a266467STheodore Ts'o 					err = -EINVAL;
35788a266467STheodore Ts'o 					goto restore_opts;
35798a266467STheodore Ts'o 				}
35808a266467STheodore Ts'o 			}
35818a266467STheodore Ts'o 
35828a266467STheodore Ts'o 			/*
3583ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3584ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3585ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3586ead6596bSEric Sandeen 			 */
3587ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3588b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
3589ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3590ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3591b31e1552SEric Sandeen 				       "umount/remount instead");
3592ead6596bSEric Sandeen 				err = -EINVAL;
3593ead6596bSEric Sandeen 				goto restore_opts;
3594ead6596bSEric Sandeen 			}
3595ead6596bSEric Sandeen 
3596ac27a0ecSDave Kleikamp 			/*
3597ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3598ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3599ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3600ac27a0ecSDave Kleikamp 			 * the partition.)
3601ac27a0ecSDave Kleikamp 			 */
36020390131bSFrank Mayhar 			if (sbi->s_journal)
3603617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
3604ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3605617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3606ac27a0ecSDave Kleikamp 				goto restore_opts;
3607617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3608ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3609ac27a0ecSDave Kleikamp 		}
3610ac27a0ecSDave Kleikamp 	}
36116fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
36120390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
3613e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
36140390131bSFrank Mayhar 
3615ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3616ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3617ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3618ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3619ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3620ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3621ac27a0ecSDave Kleikamp #endif
3622bbd6851aSAl Viro 	unlock_super(sb);
3623337eb00aSAlessio Igor Bogani 	unlock_kernel();
3624ac27a0ecSDave Kleikamp 	return 0;
36250b8e58a1SAndreas Dilger 
3626ac27a0ecSDave Kleikamp restore_opts:
3627ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3628ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3629ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3630ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3631ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
363230773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
363330773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
3634ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3635ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3636ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3637ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3638ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3639ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3640ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3641ac27a0ecSDave Kleikamp 	}
3642ac27a0ecSDave Kleikamp #endif
3643bbd6851aSAl Viro 	unlock_super(sb);
3644337eb00aSAlessio Igor Bogani 	unlock_kernel();
3645ac27a0ecSDave Kleikamp 	return err;
3646ac27a0ecSDave Kleikamp }
3647ac27a0ecSDave Kleikamp 
3648617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3649ac27a0ecSDave Kleikamp {
3650ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3651617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3652617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3653960cc398SPekka Enberg 	u64 fsid;
3654ac27a0ecSDave Kleikamp 
36555e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
36565e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
36576bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
36588df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
36595e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3660ac27a0ecSDave Kleikamp 
3661ac27a0ecSDave Kleikamp 		/*
36625e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
36635e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
36645e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3665ac27a0ecSDave Kleikamp 		 */
3666ac27a0ecSDave Kleikamp 
3667ac27a0ecSDave Kleikamp 		/*
3668ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3669ac27a0ecSDave Kleikamp 		 * overhead
3670ac27a0ecSDave Kleikamp 		 */
3671ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3672ac27a0ecSDave Kleikamp 
3673ac27a0ecSDave Kleikamp 		/*
3674ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3675ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3676ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3677ac27a0ecSDave Kleikamp 		 */
3678ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3679617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3680617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3681ac27a0ecSDave Kleikamp 			cond_resched();
3682ac27a0ecSDave Kleikamp 		}
3683ac27a0ecSDave Kleikamp 
3684ac27a0ecSDave Kleikamp 		/*
3685ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3686ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3687ac27a0ecSDave Kleikamp 		 */
36885e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
36895e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
36905e70030dSBadari Pulavarty 		smp_wmb();
36916bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3692ac27a0ecSDave Kleikamp 	}
3693ac27a0ecSDave Kleikamp 
3694617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3695ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
36965e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
36976bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
36986bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3699bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3700bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3701ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3702ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
370352d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3704617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3705960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3706960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3707960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3708960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
37090b8e58a1SAndreas Dilger 
3710ac27a0ecSDave Kleikamp 	return 0;
3711ac27a0ecSDave Kleikamp }
3712ac27a0ecSDave Kleikamp 
37130b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
37140b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
3715ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3716617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3717dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
3718a269eb18SJan Kara  *   vfs_dq_init()                         down(dqio_mutex)
3719dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3720ac27a0ecSDave Kleikamp  *
3721ac27a0ecSDave Kleikamp  */
3722ac27a0ecSDave Kleikamp 
3723ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3724ac27a0ecSDave Kleikamp 
3725ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3726ac27a0ecSDave Kleikamp {
3727ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3728ac27a0ecSDave Kleikamp }
3729ac27a0ecSDave Kleikamp 
3730617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3731ac27a0ecSDave Kleikamp {
3732ac27a0ecSDave Kleikamp 	int ret, err;
3733ac27a0ecSDave Kleikamp 	handle_t *handle;
3734ac27a0ecSDave Kleikamp 	struct inode *inode;
3735ac27a0ecSDave Kleikamp 
3736ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3737617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3738617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3739ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3740ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3741ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3742617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3743ac27a0ecSDave Kleikamp 	if (!ret)
3744ac27a0ecSDave Kleikamp 		ret = err;
3745ac27a0ecSDave Kleikamp 	return ret;
3746ac27a0ecSDave Kleikamp }
3747ac27a0ecSDave Kleikamp 
3748617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3749ac27a0ecSDave Kleikamp {
3750ac27a0ecSDave Kleikamp 	int ret, err;
3751ac27a0ecSDave Kleikamp 	handle_t *handle;
3752ac27a0ecSDave Kleikamp 
3753617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3754617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3755ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3756ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3757ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3758617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3759ac27a0ecSDave Kleikamp 	if (!ret)
3760ac27a0ecSDave Kleikamp 		ret = err;
3761ac27a0ecSDave Kleikamp 	return ret;
3762ac27a0ecSDave Kleikamp }
3763ac27a0ecSDave Kleikamp 
3764617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3765ac27a0ecSDave Kleikamp {
3766ac27a0ecSDave Kleikamp 	int ret, err;
3767ac27a0ecSDave Kleikamp 	handle_t *handle;
3768ac27a0ecSDave Kleikamp 
3769617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3770617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
37719c3013e9SJan Kara 	if (IS_ERR(handle)) {
37729c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
37739c3013e9SJan Kara 		dquot_release(dquot);
3774ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
37759c3013e9SJan Kara 	}
3776ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3777617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3778ac27a0ecSDave Kleikamp 	if (!ret)
3779ac27a0ecSDave Kleikamp 		ret = err;
3780ac27a0ecSDave Kleikamp 	return ret;
3781ac27a0ecSDave Kleikamp }
3782ac27a0ecSDave Kleikamp 
3783617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3784ac27a0ecSDave Kleikamp {
37852c8be6b2SJan Kara 	/* Are we journaling quotas? */
3786617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3787617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3788ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3789617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3790ac27a0ecSDave Kleikamp 	} else {
3791ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3792ac27a0ecSDave Kleikamp 	}
3793ac27a0ecSDave Kleikamp }
3794ac27a0ecSDave Kleikamp 
3795617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3796ac27a0ecSDave Kleikamp {
3797ac27a0ecSDave Kleikamp 	int ret, err;
3798ac27a0ecSDave Kleikamp 	handle_t *handle;
3799ac27a0ecSDave Kleikamp 
3800ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3801617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3802ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3803ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3804ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3805617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3806ac27a0ecSDave Kleikamp 	if (!ret)
3807ac27a0ecSDave Kleikamp 		ret = err;
3808ac27a0ecSDave Kleikamp 	return ret;
3809ac27a0ecSDave Kleikamp }
3810ac27a0ecSDave Kleikamp 
3811ac27a0ecSDave Kleikamp /*
3812ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3813ac27a0ecSDave Kleikamp  * the quota file and such...
3814ac27a0ecSDave Kleikamp  */
3815617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3816ac27a0ecSDave Kleikamp {
3817617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3818617ba13bSMingming Cao 				  EXT4_SB(sb)->s_jquota_fmt, type);
3819ac27a0ecSDave Kleikamp }
3820ac27a0ecSDave Kleikamp 
3821ac27a0ecSDave Kleikamp /*
3822ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3823ac27a0ecSDave Kleikamp  */
3824617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
38258264613dSAl Viro 			 char *name, int remount)
3826ac27a0ecSDave Kleikamp {
3827ac27a0ecSDave Kleikamp 	int err;
38288264613dSAl Viro 	struct path path;
3829ac27a0ecSDave Kleikamp 
3830ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3831ac27a0ecSDave Kleikamp 		return -EINVAL;
38328264613dSAl Viro 	/* When remounting, no checks are needed and in fact, name is NULL */
38330623543bSJan Kara 	if (remount)
38348264613dSAl Viro 		return vfs_quota_on(sb, type, format_id, name, remount);
38350623543bSJan Kara 
38368264613dSAl Viro 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3837ac27a0ecSDave Kleikamp 	if (err)
3838ac27a0ecSDave Kleikamp 		return err;
38390623543bSJan Kara 
3840ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
38418264613dSAl Viro 	if (path.mnt->mnt_sb != sb) {
38428264613dSAl Viro 		path_put(&path);
3843ac27a0ecSDave Kleikamp 		return -EXDEV;
3844ac27a0ecSDave Kleikamp 	}
38450623543bSJan Kara 	/* Journaling quota? */
38460623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
38472b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
38488264613dSAl Viro 		if (path.dentry->d_parent != sb->s_root)
3849b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
3850b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
3851b31e1552SEric Sandeen 				"Journaled quota will not work");
38520623543bSJan Kara 	}
38530623543bSJan Kara 
38540623543bSJan Kara 	/*
38550623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
38560623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
38570623543bSJan Kara 	 */
38580390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
38590390131bSFrank Mayhar 	    ext4_should_journal_data(path.dentry->d_inode)) {
38600623543bSJan Kara 		/*
38610623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
38620623543bSJan Kara 		 * otherwise be livelocked...
38630623543bSJan Kara 		 */
38640623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
38657ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
38660623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
38677ffe1ea8SHidehiro Kawai 		if (err) {
38688264613dSAl Viro 			path_put(&path);
38697ffe1ea8SHidehiro Kawai 			return err;
38707ffe1ea8SHidehiro Kawai 		}
38710623543bSJan Kara 	}
38720623543bSJan Kara 
38738264613dSAl Viro 	err = vfs_quota_on_path(sb, type, format_id, &path);
38748264613dSAl Viro 	path_put(&path);
387577e69dacSAl Viro 	return err;
3876ac27a0ecSDave Kleikamp }
3877ac27a0ecSDave Kleikamp 
3878ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3879ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3880ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3881ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3882617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3883ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3884ac27a0ecSDave Kleikamp {
3885ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3886725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3887ac27a0ecSDave Kleikamp 	int err = 0;
3888ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3889ac27a0ecSDave Kleikamp 	int tocopy;
3890ac27a0ecSDave Kleikamp 	size_t toread;
3891ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3892ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3893ac27a0ecSDave Kleikamp 
3894ac27a0ecSDave Kleikamp 	if (off > i_size)
3895ac27a0ecSDave Kleikamp 		return 0;
3896ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3897ac27a0ecSDave Kleikamp 		len = i_size-off;
3898ac27a0ecSDave Kleikamp 	toread = len;
3899ac27a0ecSDave Kleikamp 	while (toread > 0) {
3900ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3901ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3902617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3903ac27a0ecSDave Kleikamp 		if (err)
3904ac27a0ecSDave Kleikamp 			return err;
3905ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3906ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3907ac27a0ecSDave Kleikamp 		else
3908ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3909ac27a0ecSDave Kleikamp 		brelse(bh);
3910ac27a0ecSDave Kleikamp 		offset = 0;
3911ac27a0ecSDave Kleikamp 		toread -= tocopy;
3912ac27a0ecSDave Kleikamp 		data += tocopy;
3913ac27a0ecSDave Kleikamp 		blk++;
3914ac27a0ecSDave Kleikamp 	}
3915ac27a0ecSDave Kleikamp 	return len;
3916ac27a0ecSDave Kleikamp }
3917ac27a0ecSDave Kleikamp 
3918ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3919ac27a0ecSDave Kleikamp  * enough credits) */
3920617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3921ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3922ac27a0ecSDave Kleikamp {
3923ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3924725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3925ac27a0ecSDave Kleikamp 	int err = 0;
3926ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3927ac27a0ecSDave Kleikamp 	int tocopy;
3928617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3929ac27a0ecSDave Kleikamp 	size_t towrite = len;
3930ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3931ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3932ac27a0ecSDave Kleikamp 
39330390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
3934b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3935b31e1552SEric Sandeen 			" cancelled because transaction is not started",
39369c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
39379c3013e9SJan Kara 		return -EIO;
39389c3013e9SJan Kara 	}
3939ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3940ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3941ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3942ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3943617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3944ac27a0ecSDave Kleikamp 		if (!bh)
3945ac27a0ecSDave Kleikamp 			goto out;
3946ac27a0ecSDave Kleikamp 		if (journal_quota) {
3947617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3948ac27a0ecSDave Kleikamp 			if (err) {
3949ac27a0ecSDave Kleikamp 				brelse(bh);
3950ac27a0ecSDave Kleikamp 				goto out;
3951ac27a0ecSDave Kleikamp 			}
3952ac27a0ecSDave Kleikamp 		}
3953ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3954ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3955ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3956ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3957ac27a0ecSDave Kleikamp 		if (journal_quota)
39580390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle, NULL, bh);
3959ac27a0ecSDave Kleikamp 		else {
3960ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3961678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3962ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3963ac27a0ecSDave Kleikamp 		}
3964ac27a0ecSDave Kleikamp 		brelse(bh);
3965ac27a0ecSDave Kleikamp 		if (err)
3966ac27a0ecSDave Kleikamp 			goto out;
3967ac27a0ecSDave Kleikamp 		offset = 0;
3968ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3969ac27a0ecSDave Kleikamp 		data += tocopy;
3970ac27a0ecSDave Kleikamp 		blk++;
3971ac27a0ecSDave Kleikamp 	}
3972ac27a0ecSDave Kleikamp out:
39734d04e4fbSJan Kara 	if (len == towrite) {
39744d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
3975ac27a0ecSDave Kleikamp 		return err;
39764d04e4fbSJan Kara 	}
3977ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3978ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3979617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3980ac27a0ecSDave Kleikamp 	}
3981ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3982617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3983ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3984ac27a0ecSDave Kleikamp 	return len - towrite;
3985ac27a0ecSDave Kleikamp }
3986ac27a0ecSDave Kleikamp 
3987ac27a0ecSDave Kleikamp #endif
3988ac27a0ecSDave Kleikamp 
39890b8e58a1SAndreas Dilger static int ext4_get_sb(struct file_system_type *fs_type, int flags,
39900b8e58a1SAndreas Dilger 		       const char *dev_name, void *data, struct vfsmount *mnt)
3991ac27a0ecSDave Kleikamp {
3992617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
3993ac27a0ecSDave Kleikamp }
3994ac27a0ecSDave Kleikamp 
3995a214238dSTheodore Ts'o #if !defined(CONTIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
399624b58424STheodore Ts'o static struct file_system_type ext2_fs_type = {
399724b58424STheodore Ts'o 	.owner		= THIS_MODULE,
399824b58424STheodore Ts'o 	.name		= "ext2",
399924b58424STheodore Ts'o 	.get_sb		= ext4_get_sb,
400024b58424STheodore Ts'o 	.kill_sb	= kill_block_super,
400124b58424STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
400224b58424STheodore Ts'o };
400324b58424STheodore Ts'o 
400424b58424STheodore Ts'o static inline void register_as_ext2(void)
400524b58424STheodore Ts'o {
400624b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
400724b58424STheodore Ts'o 	if (err)
400824b58424STheodore Ts'o 		printk(KERN_WARNING
400924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
401024b58424STheodore Ts'o }
401124b58424STheodore Ts'o 
401224b58424STheodore Ts'o static inline void unregister_as_ext2(void)
401324b58424STheodore Ts'o {
401424b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
401524b58424STheodore Ts'o }
401651b7e3c9STheodore Ts'o MODULE_ALIAS("ext2");
401724b58424STheodore Ts'o #else
401824b58424STheodore Ts'o static inline void register_as_ext2(void) { }
401924b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
402024b58424STheodore Ts'o #endif
402124b58424STheodore Ts'o 
4022a214238dSTheodore Ts'o #if !defined(CONTIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
402324b58424STheodore Ts'o static struct file_system_type ext3_fs_type = {
402424b58424STheodore Ts'o 	.owner		= THIS_MODULE,
402524b58424STheodore Ts'o 	.name		= "ext3",
402624b58424STheodore Ts'o 	.get_sb		= ext4_get_sb,
402724b58424STheodore Ts'o 	.kill_sb	= kill_block_super,
402824b58424STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
402924b58424STheodore Ts'o };
403024b58424STheodore Ts'o 
403124b58424STheodore Ts'o static inline void register_as_ext3(void)
403224b58424STheodore Ts'o {
403324b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
403424b58424STheodore Ts'o 	if (err)
403524b58424STheodore Ts'o 		printk(KERN_WARNING
403624b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
403724b58424STheodore Ts'o }
403824b58424STheodore Ts'o 
403924b58424STheodore Ts'o static inline void unregister_as_ext3(void)
404024b58424STheodore Ts'o {
404124b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
404224b58424STheodore Ts'o }
404351b7e3c9STheodore Ts'o MODULE_ALIAS("ext3");
404424b58424STheodore Ts'o #else
404524b58424STheodore Ts'o static inline void register_as_ext3(void) { }
404624b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
404724b58424STheodore Ts'o #endif
404824b58424STheodore Ts'o 
404903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
4050ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
405103010a33STheodore Ts'o 	.name		= "ext4",
4052617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
4053ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
4054ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
4055ac27a0ecSDave Kleikamp };
4056ac27a0ecSDave Kleikamp 
4057617ba13bSMingming Cao static int __init init_ext4_fs(void)
4058ac27a0ecSDave Kleikamp {
4059c9de560dSAlex Tomas 	int err;
4060c9de560dSAlex Tomas 
40616fd058f7STheodore Ts'o 	err = init_ext4_system_zone();
40626fd058f7STheodore Ts'o 	if (err)
40636fd058f7STheodore Ts'o 		return err;
40643197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
40653197ebdbSTheodore Ts'o 	if (!ext4_kset)
40666fd058f7STheodore Ts'o 		goto out4;
40679f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
4068c9de560dSAlex Tomas 	err = init_ext4_mballoc();
4069ac27a0ecSDave Kleikamp 	if (err)
40706fd058f7STheodore Ts'o 		goto out3;
4071c9de560dSAlex Tomas 
4072c9de560dSAlex Tomas 	err = init_ext4_xattr();
4073c9de560dSAlex Tomas 	if (err)
4074c9de560dSAlex Tomas 		goto out2;
4075ac27a0ecSDave Kleikamp 	err = init_inodecache();
4076ac27a0ecSDave Kleikamp 	if (err)
4077ac27a0ecSDave Kleikamp 		goto out1;
407824b58424STheodore Ts'o 	register_as_ext2();
407924b58424STheodore Ts'o 	register_as_ext3();
408003010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
4081ac27a0ecSDave Kleikamp 	if (err)
4082ac27a0ecSDave Kleikamp 		goto out;
4083ac27a0ecSDave Kleikamp 	return 0;
4084ac27a0ecSDave Kleikamp out:
408524b58424STheodore Ts'o 	unregister_as_ext2();
408624b58424STheodore Ts'o 	unregister_as_ext3();
4087ac27a0ecSDave Kleikamp 	destroy_inodecache();
4088ac27a0ecSDave Kleikamp out1:
4089617ba13bSMingming Cao 	exit_ext4_xattr();
4090c9de560dSAlex Tomas out2:
4091c9de560dSAlex Tomas 	exit_ext4_mballoc();
40926fd058f7STheodore Ts'o out3:
40936fd058f7STheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
40946fd058f7STheodore Ts'o 	kset_unregister(ext4_kset);
40956fd058f7STheodore Ts'o out4:
40966fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4097ac27a0ecSDave Kleikamp 	return err;
4098ac27a0ecSDave Kleikamp }
4099ac27a0ecSDave Kleikamp 
4100617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
4101ac27a0ecSDave Kleikamp {
410224b58424STheodore Ts'o 	unregister_as_ext2();
410324b58424STheodore Ts'o 	unregister_as_ext3();
410403010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
4105ac27a0ecSDave Kleikamp 	destroy_inodecache();
4106617ba13bSMingming Cao 	exit_ext4_xattr();
4107c9de560dSAlex Tomas 	exit_ext4_mballoc();
41089f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
41093197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
41106fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4111ac27a0ecSDave Kleikamp }
4112ac27a0ecSDave Kleikamp 
4113ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
411483982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
4115ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
4116617ba13bSMingming Cao module_init(init_ext4_fs)
4117617ba13bSMingming Cao module_exit(exit_ext4_fs)
4118