xref: /linux/fs/ext4/super.c (revision a63c9eb2ce6f5028da90f282798232c4f398ceb8)
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>
40ede86cc4STheodore Ts'o #include <linux/marker.h>
411330593eSVignesh Babu #include <linux/log2.h>
42717d50e4SAndreas Dilger #include <linux/crc16.h>
43ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
44ac27a0ecSDave Kleikamp 
453dcf5451SChristoph Hellwig #include "ext4.h"
463dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
47ac27a0ecSDave Kleikamp #include "xattr.h"
48ac27a0ecSDave Kleikamp #include "acl.h"
49ac27a0ecSDave Kleikamp #include "namei.h"
50717d50e4SAndreas Dilger #include "group.h"
51ac27a0ecSDave Kleikamp 
529f6200bbSTheodore Ts'o struct proc_dir_entry *ext4_proc_root;
533197ebdbSTheodore Ts'o static struct kset *ext4_kset;
549f6200bbSTheodore Ts'o 
55617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
56ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
57e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
58617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
59617ba13bSMingming Cao 					struct ext4_super_block *es);
60617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
61617ba13bSMingming Cao 				   struct ext4_super_block *es);
62617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
63617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
64ac27a0ecSDave Kleikamp 				     char nbuf[16]);
65617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
66617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
67c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
68617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
69c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
70ac27a0ecSDave Kleikamp 
71bd81d8eeSLaurent Vivier 
728fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
738fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
74bd81d8eeSLaurent Vivier {
753a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
768fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
778fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
78bd81d8eeSLaurent Vivier }
79bd81d8eeSLaurent Vivier 
808fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
818fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
82bd81d8eeSLaurent Vivier {
835272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
848fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
858fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
86bd81d8eeSLaurent Vivier }
87bd81d8eeSLaurent Vivier 
888fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
898fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
90bd81d8eeSLaurent Vivier {
915272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
928fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
938fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
94bd81d8eeSLaurent Vivier }
95bd81d8eeSLaurent Vivier 
96560671a0SAneesh Kumar K.V __u32 ext4_free_blks_count(struct super_block *sb,
97560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
98560671a0SAneesh Kumar K.V {
99560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
100560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
101560671a0SAneesh Kumar K.V 		(__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
102560671a0SAneesh Kumar K.V }
103560671a0SAneesh Kumar K.V 
104560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
105560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
106560671a0SAneesh Kumar K.V {
107560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
108560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
109560671a0SAneesh Kumar K.V 		(__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
110560671a0SAneesh Kumar K.V }
111560671a0SAneesh Kumar K.V 
112560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
113560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
114560671a0SAneesh Kumar K.V {
115560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
116560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
117560671a0SAneesh Kumar K.V 		(__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
118560671a0SAneesh Kumar K.V }
119560671a0SAneesh Kumar K.V 
120560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
121560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
122560671a0SAneesh Kumar K.V {
123560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
124560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
125560671a0SAneesh Kumar K.V 		(__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
126560671a0SAneesh Kumar K.V }
127560671a0SAneesh Kumar K.V 
1288fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
1298fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
130bd81d8eeSLaurent Vivier {
1313a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1328fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1338fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
134bd81d8eeSLaurent Vivier }
135bd81d8eeSLaurent Vivier 
1368fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1378fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
138bd81d8eeSLaurent Vivier {
1395272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1408fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1418fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
142bd81d8eeSLaurent Vivier }
143bd81d8eeSLaurent Vivier 
1448fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1458fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
146bd81d8eeSLaurent Vivier {
1475272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1488fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1498fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
150bd81d8eeSLaurent Vivier }
151bd81d8eeSLaurent Vivier 
152560671a0SAneesh Kumar K.V void ext4_free_blks_set(struct super_block *sb,
153560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
154560671a0SAneesh Kumar K.V {
155560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
156560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
157560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
158560671a0SAneesh Kumar K.V }
159560671a0SAneesh Kumar K.V 
160560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
161560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
162560671a0SAneesh Kumar K.V {
163560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
164560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
165560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
166560671a0SAneesh Kumar K.V }
167560671a0SAneesh Kumar K.V 
168560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
169560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
170560671a0SAneesh Kumar K.V {
171560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
172560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
173560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
174560671a0SAneesh Kumar K.V }
175560671a0SAneesh Kumar K.V 
176560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
177560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
178560671a0SAneesh Kumar K.V {
179560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
180560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
181560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
182560671a0SAneesh Kumar K.V }
183560671a0SAneesh Kumar K.V 
184ac27a0ecSDave Kleikamp /*
185dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
186ac27a0ecSDave Kleikamp  *
187ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
188ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
189ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
190ac27a0ecSDave Kleikamp  * appropriate.
191ac27a0ecSDave Kleikamp  */
192617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
193ac27a0ecSDave Kleikamp {
194ac27a0ecSDave Kleikamp 	journal_t *journal;
195ac27a0ecSDave Kleikamp 
196ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
197ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
198ac27a0ecSDave Kleikamp 
199ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
200ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
201ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
202617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2030390131bSFrank Mayhar 	if (journal) {
204ac27a0ecSDave Kleikamp 		if (is_journal_aborted(journal)) {
20546e665e9SHarvey Harrison 			ext4_abort(sb, __func__,
206ac27a0ecSDave Kleikamp 				   "Detected aborted journal");
207ac27a0ecSDave Kleikamp 			return ERR_PTR(-EROFS);
208ac27a0ecSDave Kleikamp 		}
209dab291afSMingming Cao 		return jbd2_journal_start(journal, nblocks);
210ac27a0ecSDave Kleikamp 	}
2110390131bSFrank Mayhar 	/*
2120390131bSFrank Mayhar 	 * We're not journaling, return the appropriate indication.
2130390131bSFrank Mayhar 	 */
2140390131bSFrank Mayhar 	current->journal_info = EXT4_NOJOURNAL_HANDLE;
2150390131bSFrank Mayhar 	return current->journal_info;
2160390131bSFrank Mayhar }
217ac27a0ecSDave Kleikamp 
218ac27a0ecSDave Kleikamp /*
219ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
220dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
221ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
222ac27a0ecSDave Kleikamp  * appropriate.
223ac27a0ecSDave Kleikamp  */
224617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
225ac27a0ecSDave Kleikamp {
226ac27a0ecSDave Kleikamp 	struct super_block *sb;
227ac27a0ecSDave Kleikamp 	int err;
228ac27a0ecSDave Kleikamp 	int rc;
229ac27a0ecSDave Kleikamp 
2300390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
2310390131bSFrank Mayhar 		/*
2320390131bSFrank Mayhar 		 * Do this here since we don't call jbd2_journal_stop() in
2330390131bSFrank Mayhar 		 * no-journal mode.
2340390131bSFrank Mayhar 		 */
2350390131bSFrank Mayhar 		current->journal_info = NULL;
2360390131bSFrank Mayhar 		return 0;
2370390131bSFrank Mayhar 	}
238ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
239ac27a0ecSDave Kleikamp 	err = handle->h_err;
240dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
241ac27a0ecSDave Kleikamp 
242ac27a0ecSDave Kleikamp 	if (!err)
243ac27a0ecSDave Kleikamp 		err = rc;
244ac27a0ecSDave Kleikamp 	if (err)
245617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
246ac27a0ecSDave Kleikamp 	return err;
247ac27a0ecSDave Kleikamp }
248ac27a0ecSDave Kleikamp 
249617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
250ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
251ac27a0ecSDave Kleikamp {
252ac27a0ecSDave Kleikamp 	char nbuf[16];
253617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
254ac27a0ecSDave Kleikamp 
2550390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
2560390131bSFrank Mayhar 
257ac27a0ecSDave Kleikamp 	if (bh)
258ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
259ac27a0ecSDave Kleikamp 
260ac27a0ecSDave Kleikamp 	if (!handle->h_err)
261ac27a0ecSDave Kleikamp 		handle->h_err = err;
262ac27a0ecSDave Kleikamp 
263ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
264ac27a0ecSDave Kleikamp 		return;
265ac27a0ecSDave Kleikamp 
266ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
267ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
268ac27a0ecSDave Kleikamp 
269dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
270ac27a0ecSDave Kleikamp }
271ac27a0ecSDave Kleikamp 
272ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
273ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
274ac27a0ecSDave Kleikamp  *
275ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
276617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
277ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
278ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
279ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
280ac27a0ecSDave Kleikamp  * write out the superblock safely.
281ac27a0ecSDave Kleikamp  *
282dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
283ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
284ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
285ac27a0ecSDave Kleikamp  */
286ac27a0ecSDave Kleikamp 
287617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
288ac27a0ecSDave Kleikamp {
289617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
290ac27a0ecSDave Kleikamp 
291617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
292617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
293ac27a0ecSDave Kleikamp 
294ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
295ac27a0ecSDave Kleikamp 		return;
296ac27a0ecSDave Kleikamp 
297ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
298617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
299ac27a0ecSDave Kleikamp 
300617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
301ac27a0ecSDave Kleikamp 		if (journal)
302dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
303ac27a0ecSDave Kleikamp 	}
304ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
305ac27a0ecSDave Kleikamp 		printk(KERN_CRIT "Remounting filesystem read-only\n");
306ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
307ac27a0ecSDave Kleikamp 	}
308e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
309ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
310617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
311ac27a0ecSDave Kleikamp 			sb->s_id);
312ac27a0ecSDave Kleikamp }
313ac27a0ecSDave Kleikamp 
314617ba13bSMingming Cao void ext4_error(struct super_block *sb, const char *function,
315ac27a0ecSDave Kleikamp 		const char *fmt, ...)
316ac27a0ecSDave Kleikamp {
317ac27a0ecSDave Kleikamp 	va_list args;
318ac27a0ecSDave Kleikamp 
319ac27a0ecSDave Kleikamp 	va_start(args, fmt);
320617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
321ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
322ac27a0ecSDave Kleikamp 	printk("\n");
323ac27a0ecSDave Kleikamp 	va_end(args);
324ac27a0ecSDave Kleikamp 
325617ba13bSMingming Cao 	ext4_handle_error(sb);
326ac27a0ecSDave Kleikamp }
327ac27a0ecSDave Kleikamp 
328617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
329ac27a0ecSDave Kleikamp 				     char nbuf[16])
330ac27a0ecSDave Kleikamp {
331ac27a0ecSDave Kleikamp 	char *errstr = NULL;
332ac27a0ecSDave Kleikamp 
333ac27a0ecSDave Kleikamp 	switch (errno) {
334ac27a0ecSDave Kleikamp 	case -EIO:
335ac27a0ecSDave Kleikamp 		errstr = "IO failure";
336ac27a0ecSDave Kleikamp 		break;
337ac27a0ecSDave Kleikamp 	case -ENOMEM:
338ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
339ac27a0ecSDave Kleikamp 		break;
340ac27a0ecSDave Kleikamp 	case -EROFS:
341dab291afSMingming Cao 		if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
342ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
343ac27a0ecSDave Kleikamp 		else
344ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
345ac27a0ecSDave Kleikamp 		break;
346ac27a0ecSDave Kleikamp 	default:
347ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
348ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
349ac27a0ecSDave Kleikamp 		 * NULL. */
350ac27a0ecSDave Kleikamp 		if (nbuf) {
351ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
352ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
353ac27a0ecSDave Kleikamp 				errstr = nbuf;
354ac27a0ecSDave Kleikamp 		}
355ac27a0ecSDave Kleikamp 		break;
356ac27a0ecSDave Kleikamp 	}
357ac27a0ecSDave Kleikamp 
358ac27a0ecSDave Kleikamp 	return errstr;
359ac27a0ecSDave Kleikamp }
360ac27a0ecSDave Kleikamp 
361617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
362ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
363ac27a0ecSDave Kleikamp 
3642b2d6d01STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, int errno)
365ac27a0ecSDave Kleikamp {
366ac27a0ecSDave Kleikamp 	char nbuf[16];
367ac27a0ecSDave Kleikamp 	const char *errstr;
368ac27a0ecSDave Kleikamp 
369ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
370ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
371ac27a0ecSDave Kleikamp 	 * an error. */
372ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
373ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
374ac27a0ecSDave Kleikamp 		return;
375ac27a0ecSDave Kleikamp 
376617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
377617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
378ac27a0ecSDave Kleikamp 	       sb->s_id, function, errstr);
379ac27a0ecSDave Kleikamp 
380617ba13bSMingming Cao 	ext4_handle_error(sb);
381ac27a0ecSDave Kleikamp }
382ac27a0ecSDave Kleikamp 
383ac27a0ecSDave Kleikamp /*
384617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
385ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
386ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
387ac27a0ecSDave Kleikamp  *
388ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
389ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
390ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
391ac27a0ecSDave Kleikamp  */
392ac27a0ecSDave Kleikamp 
393617ba13bSMingming Cao void ext4_abort(struct super_block *sb, const char *function,
394ac27a0ecSDave Kleikamp 		const char *fmt, ...)
395ac27a0ecSDave Kleikamp {
396ac27a0ecSDave Kleikamp 	va_list args;
397ac27a0ecSDave Kleikamp 
398617ba13bSMingming Cao 	printk(KERN_CRIT "ext4_abort called.\n");
399ac27a0ecSDave Kleikamp 
400ac27a0ecSDave Kleikamp 	va_start(args, fmt);
401617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
402ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
403ac27a0ecSDave Kleikamp 	printk("\n");
404ac27a0ecSDave Kleikamp 	va_end(args);
405ac27a0ecSDave Kleikamp 
406ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
407617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
408ac27a0ecSDave Kleikamp 
409ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
410ac27a0ecSDave Kleikamp 		return;
411ac27a0ecSDave Kleikamp 
412ac27a0ecSDave Kleikamp 	printk(KERN_CRIT "Remounting filesystem read-only\n");
413617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
414ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
415617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
416ef2cabf7SHidehiro Kawai 	if (EXT4_SB(sb)->s_journal)
417dab291afSMingming Cao 		jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
418ac27a0ecSDave Kleikamp }
419ac27a0ecSDave Kleikamp 
420617ba13bSMingming Cao void ext4_warning(struct super_block *sb, const char *function,
421ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
422ac27a0ecSDave Kleikamp {
423ac27a0ecSDave Kleikamp 	va_list args;
424ac27a0ecSDave Kleikamp 
425ac27a0ecSDave Kleikamp 	va_start(args, fmt);
426617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
427ac27a0ecSDave Kleikamp 	       sb->s_id, function);
428ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
429ac27a0ecSDave Kleikamp 	printk("\n");
430ac27a0ecSDave Kleikamp 	va_end(args);
431ac27a0ecSDave Kleikamp }
432ac27a0ecSDave Kleikamp 
4335d1b1b3fSAneesh Kumar K.V void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
4345d1b1b3fSAneesh Kumar K.V 				const char *function, const char *fmt, ...)
4355d1b1b3fSAneesh Kumar K.V __releases(bitlock)
4365d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
4375d1b1b3fSAneesh Kumar K.V {
4385d1b1b3fSAneesh Kumar K.V 	va_list args;
4395d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4405d1b1b3fSAneesh Kumar K.V 
4415d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
4425d1b1b3fSAneesh Kumar K.V 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
4435d1b1b3fSAneesh Kumar K.V 	vprintk(fmt, args);
4445d1b1b3fSAneesh Kumar K.V 	printk("\n");
4455d1b1b3fSAneesh Kumar K.V 	va_end(args);
4465d1b1b3fSAneesh Kumar K.V 
4475d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
4485d1b1b3fSAneesh Kumar K.V 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4495d1b1b3fSAneesh Kumar K.V 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
450e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
4515d1b1b3fSAneesh Kumar K.V 		return;
4525d1b1b3fSAneesh Kumar K.V 	}
4535d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
4545d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
4555d1b1b3fSAneesh Kumar K.V 	/*
4565d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
4575d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
4585d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
4595d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
4605d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
4615d1b1b3fSAneesh Kumar K.V 	 * ext4_grp_locked_error() to distinguish beween the
4625d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
4635d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
4645d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
4655d1b1b3fSAneesh Kumar K.V 	 */
4665d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
4675d1b1b3fSAneesh Kumar K.V 	return;
4685d1b1b3fSAneesh Kumar K.V }
4695d1b1b3fSAneesh Kumar K.V 
4705d1b1b3fSAneesh Kumar K.V 
471617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
472ac27a0ecSDave Kleikamp {
473617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
474ac27a0ecSDave Kleikamp 
475617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
476ac27a0ecSDave Kleikamp 		return;
477ac27a0ecSDave Kleikamp 
47846e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
479ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
480ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
481617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
482ac27a0ecSDave Kleikamp 
483617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
484617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
485617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
486ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
487ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
488ac27a0ecSDave Kleikamp 
489ac27a0ecSDave Kleikamp 	/*
490ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
491ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
492ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
493ac27a0ecSDave Kleikamp 	 */
494ac27a0ecSDave Kleikamp }
495ac27a0ecSDave Kleikamp 
496ac27a0ecSDave Kleikamp /*
497ac27a0ecSDave Kleikamp  * Open the external journal device
498ac27a0ecSDave Kleikamp  */
499617ba13bSMingming Cao static struct block_device *ext4_blkdev_get(dev_t dev)
500ac27a0ecSDave Kleikamp {
501ac27a0ecSDave Kleikamp 	struct block_device *bdev;
502ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
503ac27a0ecSDave Kleikamp 
504ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
505ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
506ac27a0ecSDave Kleikamp 		goto fail;
507ac27a0ecSDave Kleikamp 	return bdev;
508ac27a0ecSDave Kleikamp 
509ac27a0ecSDave Kleikamp fail:
510abda1418STheodore Ts'o 	printk(KERN_ERR "EXT4-fs: failed to open journal device %s: %ld\n",
511ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
512ac27a0ecSDave Kleikamp 	return NULL;
513ac27a0ecSDave Kleikamp }
514ac27a0ecSDave Kleikamp 
515ac27a0ecSDave Kleikamp /*
516ac27a0ecSDave Kleikamp  * Release the journal device
517ac27a0ecSDave Kleikamp  */
518617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
519ac27a0ecSDave Kleikamp {
520ac27a0ecSDave Kleikamp 	bd_release(bdev);
5219a1c3542SAl Viro 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
522ac27a0ecSDave Kleikamp }
523ac27a0ecSDave Kleikamp 
524617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
525ac27a0ecSDave Kleikamp {
526ac27a0ecSDave Kleikamp 	struct block_device *bdev;
527ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
528ac27a0ecSDave Kleikamp 
529ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
530ac27a0ecSDave Kleikamp 	if (bdev) {
531617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
532ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
533ac27a0ecSDave Kleikamp 	}
534ac27a0ecSDave Kleikamp 	return ret;
535ac27a0ecSDave Kleikamp }
536ac27a0ecSDave Kleikamp 
537ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
538ac27a0ecSDave Kleikamp {
539617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
540ac27a0ecSDave Kleikamp }
541ac27a0ecSDave Kleikamp 
542617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
543ac27a0ecSDave Kleikamp {
544ac27a0ecSDave Kleikamp 	struct list_head *l;
545ac27a0ecSDave Kleikamp 
546ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb orphan head is %d\n",
547ac27a0ecSDave Kleikamp 	       le32_to_cpu(sbi->s_es->s_last_orphan));
548ac27a0ecSDave Kleikamp 
549ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
550ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
551ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
552ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
553ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
554ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
555ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
556ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
557ac27a0ecSDave Kleikamp 	}
558ac27a0ecSDave Kleikamp }
559ac27a0ecSDave Kleikamp 
560617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
561ac27a0ecSDave Kleikamp {
562617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
563617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
564ef2cabf7SHidehiro Kawai 	int i, err;
565ac27a0ecSDave Kleikamp 
566c9de560dSAlex Tomas 	ext4_mb_release(sb);
567a86c6181SAlex Tomas 	ext4_ext_release(sb);
568617ba13bSMingming Cao 	ext4_xattr_put_super(sb);
5690390131bSFrank Mayhar 	if (sbi->s_journal) {
570ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
57147b4a50bSJan Kara 		sbi->s_journal = NULL;
572ef2cabf7SHidehiro Kawai 		if (err < 0)
5730390131bSFrank Mayhar 			ext4_abort(sb, __func__,
5740390131bSFrank Mayhar 				   "Couldn't clean up the journal");
5750390131bSFrank Mayhar 	}
576ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
577617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
578ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
579e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
580ac27a0ecSDave Kleikamp 	}
581240799cdSTheodore Ts'o 	if (sbi->s_proc) {
5829f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
583240799cdSTheodore Ts'o 	}
5843197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
585ac27a0ecSDave Kleikamp 
586ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
587ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
588ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
589c5ca7c76STheodore Ts'o 	if (is_vmalloc_addr(sbi->s_flex_groups))
590c5ca7c76STheodore Ts'o 		vfree(sbi->s_flex_groups);
591c5ca7c76STheodore Ts'o 	else
592772cb7c8SJose R. Santos 		kfree(sbi->s_flex_groups);
593ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
594ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
595ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
5966bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
597ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
598ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
599ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
600ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
601ac27a0ecSDave Kleikamp #endif
602ac27a0ecSDave Kleikamp 
603ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
604ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
605ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
606ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
607ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
608ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
609ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
610ac27a0ecSDave Kleikamp 
611f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
612ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
613ac27a0ecSDave Kleikamp 		/*
614ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
615ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
616ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
617ac27a0ecSDave Kleikamp 		 */
618ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
619f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
620617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
621ac27a0ecSDave Kleikamp 	}
622ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
6233197ebdbSTheodore Ts'o 	/*
6243197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
6253197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
6263197ebdbSTheodore Ts'o 	 */
6273197ebdbSTheodore Ts'o 	unlock_kernel();
6283197ebdbSTheodore Ts'o 	unlock_super(sb);
6293197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
6303197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
6313197ebdbSTheodore Ts'o 	lock_super(sb);
6323197ebdbSTheodore Ts'o 	lock_kernel();
633705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
634ac27a0ecSDave Kleikamp 	kfree(sbi);
635ac27a0ecSDave Kleikamp 	return;
636ac27a0ecSDave Kleikamp }
637ac27a0ecSDave Kleikamp 
638e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
639ac27a0ecSDave Kleikamp 
640ac27a0ecSDave Kleikamp /*
641ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
642ac27a0ecSDave Kleikamp  */
643617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
644ac27a0ecSDave Kleikamp {
645617ba13bSMingming Cao 	struct ext4_inode_info *ei;
646ac27a0ecSDave Kleikamp 
647e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
648ac27a0ecSDave Kleikamp 	if (!ei)
649ac27a0ecSDave Kleikamp 		return NULL;
65003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
651617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
652617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
653ac27a0ecSDave Kleikamp #endif
654ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
65591246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
656a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
657c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
658c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
6590390131bSFrank Mayhar 	/*
6600390131bSFrank Mayhar 	 * Note:  We can be called before EXT4_SB(sb)->s_journal is set,
6610390131bSFrank Mayhar 	 * therefore it can be null here.  Don't check it, just initialize
6620390131bSFrank Mayhar 	 * jinode.
6630390131bSFrank Mayhar 	 */
664678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
665d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
666d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
667d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
668d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
669d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
670ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
671ac27a0ecSDave Kleikamp }
672ac27a0ecSDave Kleikamp 
673617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
674ac27a0ecSDave Kleikamp {
6759f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
6769f7dd93dSVasily Averin 		printk("EXT4 Inode %p: orphan list check failed!\n",
6779f7dd93dSVasily Averin 			EXT4_I(inode));
6789f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
6799f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
6809f7dd93dSVasily Averin 				true);
6819f7dd93dSVasily Averin 		dump_stack();
6829f7dd93dSVasily Averin 	}
683617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
684ac27a0ecSDave Kleikamp }
685ac27a0ecSDave Kleikamp 
68651cc5068SAlexey Dobriyan static void init_once(void *foo)
687ac27a0ecSDave Kleikamp {
688617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
689ac27a0ecSDave Kleikamp 
690ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
69103010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
692ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
693ac27a0ecSDave Kleikamp #endif
6940e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
695ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
696ac27a0ecSDave Kleikamp }
697ac27a0ecSDave Kleikamp 
698ac27a0ecSDave Kleikamp static int init_inodecache(void)
699ac27a0ecSDave Kleikamp {
700617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
701617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
702ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
703ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
70420c2df83SPaul Mundt 					     init_once);
705617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
706ac27a0ecSDave Kleikamp 		return -ENOMEM;
707ac27a0ecSDave Kleikamp 	return 0;
708ac27a0ecSDave Kleikamp }
709ac27a0ecSDave Kleikamp 
710ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
711ac27a0ecSDave Kleikamp {
712617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
713ac27a0ecSDave Kleikamp }
714ac27a0ecSDave Kleikamp 
715617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
716ac27a0ecSDave Kleikamp {
71703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
718617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
719617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
720617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
721617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
722ac27a0ecSDave Kleikamp 	}
723617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
724617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
725617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
726617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
727ac27a0ecSDave Kleikamp 	}
728ac27a0ecSDave Kleikamp #endif
729c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
7300390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode))
731678aaf48SJan Kara 		jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
732678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
733ac27a0ecSDave Kleikamp }
734ac27a0ecSDave Kleikamp 
7352b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
7362b2d6d01STheodore Ts'o 					   struct super_block *sb)
737ac27a0ecSDave Kleikamp {
738ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
739617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
740ac27a0ecSDave Kleikamp 
741ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
742ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
743ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
744ac27a0ecSDave Kleikamp 
745ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
746ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
747ac27a0ecSDave Kleikamp 
748ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
749ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
750ac27a0ecSDave Kleikamp 
751617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
752ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
753ac27a0ecSDave Kleikamp 
754617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
755ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
756ac27a0ecSDave Kleikamp #endif
757ac27a0ecSDave Kleikamp }
758ac27a0ecSDave Kleikamp 
759d9c9bef1SMiklos Szeredi /*
760d9c9bef1SMiklos Szeredi  * Show an option if
761d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
762d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
763d9c9bef1SMiklos Szeredi  */
764617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
765ac27a0ecSDave Kleikamp {
766aa22df2cSAneesh Kumar K.V 	int def_errors;
767aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
768ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
769d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
770d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
771d9c9bef1SMiklos Szeredi 
772d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
773aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
774d9c9bef1SMiklos Szeredi 
775d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
776d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
777d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
778d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
779aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
780d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
781d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
782d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
783d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
784d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
785d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
786d9c9bef1SMiklos Szeredi 	}
787d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
788d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
789d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
790d9c9bef1SMiklos Szeredi 	}
791bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
792d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
793bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
794d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
795bb4f397aSAneesh Kumar K.V 		}
796bb4f397aSAneesh Kumar K.V 	}
797aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
798bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
799aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
800d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
801aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
802d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
803aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
804d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
805d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
806d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
80703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
808aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
809aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
810d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
811d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
812d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
813d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
814d9c9bef1SMiklos Szeredi 	}
815d9c9bef1SMiklos Szeredi #endif
81603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
817aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
818d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
819d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
820d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
821d9c9bef1SMiklos Szeredi #endif
82230773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
823d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
824d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
825d9c9bef1SMiklos Szeredi 	}
82630773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
82730773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
82830773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
82930773840STheodore Ts'o 	}
83030773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
83130773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
83230773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
83330773840STheodore Ts'o 	}
83430773840STheodore Ts'o 
835571640caSEric Sandeen 	/*
836571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
837571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
838571640caSEric Sandeen 	 * status is.
839571640caSEric Sandeen 	 */
840571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
841571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
842cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
843cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
844d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
845d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
84625ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
84725ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
848dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
849dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
850dd919b98SAneesh Kumar K.V 
851ac27a0ecSDave Kleikamp 
852cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
853cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
854aa22df2cSAneesh Kumar K.V 	/*
855aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
856aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
857aa22df2cSAneesh Kumar K.V 	 */
858617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
859ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
860617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
861ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
862617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
863ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
864ac27a0ecSDave Kleikamp 
865240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
866240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
867240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
868240799cdSTheodore Ts'o 
8695bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
8705bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
8715bf5683aSHidehiro Kawai 
872afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
87306705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
874afd4672dSTheodore Ts'o 
875617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
876ac27a0ecSDave Kleikamp 	return 0;
877ac27a0ecSDave Kleikamp }
878ac27a0ecSDave Kleikamp 
879ac27a0ecSDave Kleikamp 
8801b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
8811b961ac0SChristoph Hellwig 		u64 ino, u32 generation)
882ac27a0ecSDave Kleikamp {
883ac27a0ecSDave Kleikamp 	struct inode *inode;
884ac27a0ecSDave Kleikamp 
885617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
886ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
887617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
888ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
889ac27a0ecSDave Kleikamp 
890ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
891ac27a0ecSDave Kleikamp 	 *
892617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
893ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
894ac27a0ecSDave Kleikamp 	 *
895ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
896ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
897ac27a0ecSDave Kleikamp 	 */
8981d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
8991d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9001d1fe1eeSDavid Howells 		return ERR_CAST(inode);
9011d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
902ac27a0ecSDave Kleikamp 		iput(inode);
903ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
904ac27a0ecSDave Kleikamp 	}
9051b961ac0SChristoph Hellwig 
9061b961ac0SChristoph Hellwig 	return inode;
907ac27a0ecSDave Kleikamp }
9081b961ac0SChristoph Hellwig 
9091b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
9101b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
9111b961ac0SChristoph Hellwig {
9121b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
9131b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
9141b961ac0SChristoph Hellwig }
9151b961ac0SChristoph Hellwig 
9161b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
9171b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
9181b961ac0SChristoph Hellwig {
9191b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
9201b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
921ac27a0ecSDave Kleikamp }
922ac27a0ecSDave Kleikamp 
923c39a7f84SToshiyuki Okajima /*
924c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
925c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
926c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
927c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
928c39a7f84SToshiyuki Okajima  */
929c39a7f84SToshiyuki Okajima static int bdev_try_to_free_page(struct super_block *sb, struct page *page, gfp_t wait)
930c39a7f84SToshiyuki Okajima {
931c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
932c39a7f84SToshiyuki Okajima 
933c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
934c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
935c39a7f84SToshiyuki Okajima 		return 0;
936c39a7f84SToshiyuki Okajima 	if (journal)
937c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
938c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
939c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
940c39a7f84SToshiyuki Okajima }
941c39a7f84SToshiyuki Okajima 
942ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
943ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
944ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
945ac27a0ecSDave Kleikamp 
946617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
947617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
948617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
949617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
950617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
9516f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
9526f28e087SJan Kara 				char *path, int remount);
953617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
954617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
955ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
956617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
957ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
958ac27a0ecSDave Kleikamp 
959617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
960edf72453SJan Kara 	.initialize	= dquot_initialize,
961edf72453SJan Kara 	.drop		= dquot_drop,
962ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
96360e58e0fSMingming Cao 	.reserve_space	= dquot_reserve_space,
96460e58e0fSMingming Cao 	.claim_space	= dquot_claim_space,
96560e58e0fSMingming Cao 	.release_rsv	= dquot_release_reserved_space,
96660e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
967ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
968ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
969ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
970ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
971617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
972617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
973617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
974617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
975a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
976a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
977a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
978ac27a0ecSDave Kleikamp };
979ac27a0ecSDave Kleikamp 
980617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
981617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
982ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
983ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
984ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
985ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
986ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
987ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
988ac27a0ecSDave Kleikamp };
989ac27a0ecSDave Kleikamp #endif
990ac27a0ecSDave Kleikamp 
991ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
992617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
993617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
994617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
995617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
996617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
997617ba13bSMingming Cao 	.put_super	= ext4_put_super,
998617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
999c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1000c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1001617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1002617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1003617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
1004617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1005ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1006617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1007617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1008ac27a0ecSDave Kleikamp #endif
1009c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1010ac27a0ecSDave Kleikamp };
1011ac27a0ecSDave Kleikamp 
10129ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
10139ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
10149ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
10159ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
10169ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
10179ca92389STheodore Ts'o 	.delete_inode	= ext4_delete_inode,
10189ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
10199ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
10209ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
10219ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
10229ca92389STheodore Ts'o 	.clear_inode	= ext4_clear_inode,
10239ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
10249ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
10259ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
10269ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
10279ca92389STheodore Ts'o #endif
10289ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
10299ca92389STheodore Ts'o };
10309ca92389STheodore Ts'o 
103139655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
10321b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
10331b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1034617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1035ac27a0ecSDave Kleikamp };
1036ac27a0ecSDave Kleikamp 
1037ac27a0ecSDave Kleikamp enum {
1038ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1039ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
104001436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1041ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
104206705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
104330773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1044c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1045818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1046ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
10475bf5683aSHidehiro Kawai 	Opt_data_err_abort, Opt_data_err_ignore,
1048ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1049ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
105006705bffSTheodore Ts'o 	Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize,
105106705bffSTheodore Ts'o 	Opt_usrquota, Opt_grpquota, Opt_i_version,
105201436ef2STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc,
1053b3881f74STheodore Ts'o 	Opt_inode_readahead_blks, Opt_journal_ioprio
1054ac27a0ecSDave Kleikamp };
1055ac27a0ecSDave Kleikamp 
1056a447c093SSteven Whitehouse static const match_table_t tokens = {
1057ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1058ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1059ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1060ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1061ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1062ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1063ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1064ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1065ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1066ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1067ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1068ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1069ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1070ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1071ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1072ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1073ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1074ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1075ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1076ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1077ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1078ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1079ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1080ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
108130773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
108230773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1083ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1084ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1085818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1086818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1087ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1088ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1089ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1090ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
10915bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
10925bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1093ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1094ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1095ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1096ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1097ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1098ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1099ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1100ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1101ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1102ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1103ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
110406705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
110506705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
110625ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1107c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1108ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
110964769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1110dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
1111240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1112b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1113afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
111406705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
111506705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1116f3f12faaSJosef Bacik 	{Opt_err, NULL},
1117ac27a0ecSDave Kleikamp };
1118ac27a0ecSDave Kleikamp 
1119617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1120ac27a0ecSDave Kleikamp {
1121617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1122ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1123ac27a0ecSDave Kleikamp 
1124ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1125ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
1126ac27a0ecSDave Kleikamp 	options += 3;
1127617ba13bSMingming Cao 	/*todo: use simple_strtoll with >32bit ext4 */
1128ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1129ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
11304776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1131ac27a0ecSDave Kleikamp 		       (char *) *data);
1132ac27a0ecSDave Kleikamp 		return 1;
1133ac27a0ecSDave Kleikamp 	}
1134ac27a0ecSDave Kleikamp 	if (*options == ',')
1135ac27a0ecSDave Kleikamp 		options++;
1136ac27a0ecSDave Kleikamp 	*data = (void *) options;
1137ac27a0ecSDave Kleikamp 	return sb_block;
1138ac27a0ecSDave Kleikamp }
1139ac27a0ecSDave Kleikamp 
1140b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1141b3881f74STheodore Ts'o 
1142ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1143c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1144b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1145617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1146ac27a0ecSDave Kleikamp {
1147617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1148ac27a0ecSDave Kleikamp 	char *p;
1149ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1150ac27a0ecSDave Kleikamp 	int data_opt = 0;
1151ac27a0ecSDave Kleikamp 	int option;
1152ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1153dfc5d03fSJan Kara 	int qtype, qfmt;
1154ac27a0ecSDave Kleikamp 	char *qname;
1155ac27a0ecSDave Kleikamp #endif
1156ac27a0ecSDave Kleikamp 
1157ac27a0ecSDave Kleikamp 	if (!options)
1158ac27a0ecSDave Kleikamp 		return 1;
1159ac27a0ecSDave Kleikamp 
1160ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1161ac27a0ecSDave Kleikamp 		int token;
1162ac27a0ecSDave Kleikamp 		if (!*p)
1163ac27a0ecSDave Kleikamp 			continue;
1164ac27a0ecSDave Kleikamp 
1165ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1166ac27a0ecSDave Kleikamp 		switch (token) {
1167ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1168ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
1169ac27a0ecSDave Kleikamp 			break;
1170ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1171ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
1172ac27a0ecSDave Kleikamp 			break;
1173ac27a0ecSDave Kleikamp 		case Opt_grpid:
1174ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
1175ac27a0ecSDave Kleikamp 			break;
1176ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1177ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
1178ac27a0ecSDave Kleikamp 			break;
1179ac27a0ecSDave Kleikamp 		case Opt_resuid:
1180ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1181ac27a0ecSDave Kleikamp 				return 0;
1182ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1183ac27a0ecSDave Kleikamp 			break;
1184ac27a0ecSDave Kleikamp 		case Opt_resgid:
1185ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1186ac27a0ecSDave Kleikamp 				return 0;
1187ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1188ac27a0ecSDave Kleikamp 			break;
1189ac27a0ecSDave Kleikamp 		case Opt_sb:
1190ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1191ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1192ac27a0ecSDave Kleikamp 			break;
1193ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1194ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1195ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1196ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1197ac27a0ecSDave Kleikamp 			break;
1198ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1199ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1200ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1201ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1202ac27a0ecSDave Kleikamp 			break;
1203ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1204ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1205ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1206ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1207ac27a0ecSDave Kleikamp 			break;
1208ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1209ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1210ac27a0ecSDave Kleikamp 			break;
1211ac27a0ecSDave Kleikamp 		case Opt_debug:
1212ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1213ac27a0ecSDave Kleikamp 			break;
1214ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1215ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1216ac27a0ecSDave Kleikamp 			break;
1217ac27a0ecSDave Kleikamp 		case Opt_orlov:
1218ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1219ac27a0ecSDave Kleikamp 			break;
122003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1221ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1222ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1223ac27a0ecSDave Kleikamp 			break;
1224ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1225ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1226ac27a0ecSDave Kleikamp 			break;
1227ac27a0ecSDave Kleikamp #else
1228ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1229ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
12304776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)user_xattr options "
12314776004fSTheodore Ts'o 			       "not supported\n");
1232ac27a0ecSDave Kleikamp 			break;
1233ac27a0ecSDave Kleikamp #endif
123403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1235ac27a0ecSDave Kleikamp 		case Opt_acl:
1236ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1237ac27a0ecSDave Kleikamp 			break;
1238ac27a0ecSDave Kleikamp 		case Opt_noacl:
1239ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1240ac27a0ecSDave Kleikamp 			break;
1241ac27a0ecSDave Kleikamp #else
1242ac27a0ecSDave Kleikamp 		case Opt_acl:
1243ac27a0ecSDave Kleikamp 		case Opt_noacl:
12444776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)acl options "
12454776004fSTheodore Ts'o 			       "not supported\n");
1246ac27a0ecSDave Kleikamp 			break;
1247ac27a0ecSDave Kleikamp #endif
1248ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1249ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1250ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1251ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1252ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1253ac27a0ecSDave Kleikamp 			   journal file. */
1254ac27a0ecSDave Kleikamp 			if (is_remount) {
1255617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1256ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1257ac27a0ecSDave Kleikamp 				return 0;
1258ac27a0ecSDave Kleikamp 			}
1259ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1260ac27a0ecSDave Kleikamp 			break;
1261ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1262ac27a0ecSDave Kleikamp 			if (is_remount) {
1263617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1264ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1265ac27a0ecSDave Kleikamp 				return 0;
1266ac27a0ecSDave Kleikamp 			}
1267ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1268ac27a0ecSDave Kleikamp 				return 0;
1269ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1270ac27a0ecSDave Kleikamp 			break;
1271818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1272818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1273818d276cSGirish Shilamkar 			break;
1274818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1275818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1276818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1277818d276cSGirish Shilamkar 			break;
1278ac27a0ecSDave Kleikamp 		case Opt_noload:
1279ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1280ac27a0ecSDave Kleikamp 			break;
1281ac27a0ecSDave Kleikamp 		case Opt_commit:
1282ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1283ac27a0ecSDave Kleikamp 				return 0;
1284ac27a0ecSDave Kleikamp 			if (option < 0)
1285ac27a0ecSDave Kleikamp 				return 0;
1286ac27a0ecSDave Kleikamp 			if (option == 0)
1287cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1288ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1289ac27a0ecSDave Kleikamp 			break;
129030773840STheodore Ts'o 		case Opt_max_batch_time:
129130773840STheodore Ts'o 			if (match_int(&args[0], &option))
129230773840STheodore Ts'o 				return 0;
129330773840STheodore Ts'o 			if (option < 0)
129430773840STheodore Ts'o 				return 0;
129530773840STheodore Ts'o 			if (option == 0)
129630773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
129730773840STheodore Ts'o 			sbi->s_max_batch_time = option;
129830773840STheodore Ts'o 			break;
129930773840STheodore Ts'o 		case Opt_min_batch_time:
130030773840STheodore Ts'o 			if (match_int(&args[0], &option))
130130773840STheodore Ts'o 				return 0;
130230773840STheodore Ts'o 			if (option < 0)
130330773840STheodore Ts'o 				return 0;
130430773840STheodore Ts'o 			sbi->s_min_batch_time = option;
130530773840STheodore Ts'o 			break;
1306ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1307617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1308ac27a0ecSDave Kleikamp 			goto datacheck;
1309ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1310617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1311ac27a0ecSDave Kleikamp 			goto datacheck;
1312ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1313617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1314ac27a0ecSDave Kleikamp 		datacheck:
1315ac27a0ecSDave Kleikamp 			if (is_remount) {
1316617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1317ac27a0ecSDave Kleikamp 						!= data_opt) {
1318ac27a0ecSDave Kleikamp 					printk(KERN_ERR
1319617ba13bSMingming Cao 						"EXT4-fs: cannot change data "
1320ac27a0ecSDave Kleikamp 						"mode on remount\n");
1321ac27a0ecSDave Kleikamp 					return 0;
1322ac27a0ecSDave Kleikamp 				}
1323ac27a0ecSDave Kleikamp 			} else {
1324617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1325ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1326ac27a0ecSDave Kleikamp 			}
1327ac27a0ecSDave Kleikamp 			break;
13285bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
13295bf5683aSHidehiro Kawai 			set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13305bf5683aSHidehiro Kawai 			break;
13315bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
13325bf5683aSHidehiro Kawai 			clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13335bf5683aSHidehiro Kawai 			break;
1334ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1335ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1336ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1337ac27a0ecSDave Kleikamp 			goto set_qf_name;
1338ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1339ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1340ac27a0ecSDave Kleikamp set_qf_name:
134117bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1342dfc5d03fSJan Kara 			    !sbi->s_qf_names[qtype]) {
1343ac27a0ecSDave Kleikamp 				printk(KERN_ERR
13442c8be6b2SJan Kara 				       "EXT4-fs: Cannot change journaled "
1345ac27a0ecSDave Kleikamp 				       "quota options when quota turned on.\n");
1346ac27a0ecSDave Kleikamp 				return 0;
1347ac27a0ecSDave Kleikamp 			}
1348ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1349ac27a0ecSDave Kleikamp 			if (!qname) {
1350ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1351617ba13bSMingming Cao 					"EXT4-fs: not enough memory for "
1352ac27a0ecSDave Kleikamp 					"storing quotafile name.\n");
1353ac27a0ecSDave Kleikamp 				return 0;
1354ac27a0ecSDave Kleikamp 			}
1355ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1356ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1357ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1358617ba13bSMingming Cao 					"EXT4-fs: %s quota file already "
1359ac27a0ecSDave Kleikamp 					"specified.\n", QTYPE2NAME(qtype));
1360ac27a0ecSDave Kleikamp 				kfree(qname);
1361ac27a0ecSDave Kleikamp 				return 0;
1362ac27a0ecSDave Kleikamp 			}
1363ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1364ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1365ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1366617ba13bSMingming Cao 					"EXT4-fs: quotafile must be on "
1367ac27a0ecSDave Kleikamp 					"filesystem root.\n");
1368ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1369ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1370ac27a0ecSDave Kleikamp 				return 0;
1371ac27a0ecSDave Kleikamp 			}
1372ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1373ac27a0ecSDave Kleikamp 			break;
1374ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1375ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1376ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1377ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1378ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1379ac27a0ecSDave Kleikamp clear_qf_name:
138017bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1381dfc5d03fSJan Kara 			    sbi->s_qf_names[qtype]) {
1382617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change "
13832c8be6b2SJan Kara 					"journaled quota options when "
1384ac27a0ecSDave Kleikamp 					"quota turned on.\n");
1385ac27a0ecSDave Kleikamp 				return 0;
1386ac27a0ecSDave Kleikamp 			}
1387ac27a0ecSDave Kleikamp 			/*
1388ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1389ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1390ac27a0ecSDave Kleikamp 			 */
1391ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1392ac27a0ecSDave Kleikamp 			break;
1393ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1394dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1395dfc5d03fSJan Kara 			goto set_qf_format;
1396ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1397dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
1398dfc5d03fSJan Kara set_qf_format:
139917bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1400dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1401dfc5d03fSJan Kara 				printk(KERN_ERR "EXT4-fs: Cannot change "
1402dfc5d03fSJan Kara 					"journaled quota options when "
1403dfc5d03fSJan Kara 					"quota turned on.\n");
1404dfc5d03fSJan Kara 				return 0;
1405dfc5d03fSJan Kara 			}
1406dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1407ac27a0ecSDave Kleikamp 			break;
1408ac27a0ecSDave Kleikamp 		case Opt_quota:
1409ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1410ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1411ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1412ac27a0ecSDave Kleikamp 			break;
1413ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1414ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1415ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1416ac27a0ecSDave Kleikamp 			break;
1417ac27a0ecSDave Kleikamp 		case Opt_noquota:
141817bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1419617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change quota "
1420ac27a0ecSDave Kleikamp 					"options when quota turned on.\n");
1421ac27a0ecSDave Kleikamp 				return 0;
1422ac27a0ecSDave Kleikamp 			}
1423ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1424ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1425ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1426ac27a0ecSDave Kleikamp 			break;
1427ac27a0ecSDave Kleikamp #else
1428ac27a0ecSDave Kleikamp 		case Opt_quota:
1429ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1430ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1431cd59e7b9SJan Kara 			printk(KERN_ERR
1432cd59e7b9SJan Kara 				"EXT4-fs: quota options not supported.\n");
1433cd59e7b9SJan Kara 			break;
1434ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1435ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1436ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1437ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1438ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1439ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1440ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1441cd59e7b9SJan Kara 				"EXT4-fs: journaled quota options not "
1442ac27a0ecSDave Kleikamp 				"supported.\n");
1443ac27a0ecSDave Kleikamp 			break;
1444ac27a0ecSDave Kleikamp 		case Opt_noquota:
1445ac27a0ecSDave Kleikamp 			break;
1446ac27a0ecSDave Kleikamp #endif
1447ac27a0ecSDave Kleikamp 		case Opt_abort:
1448ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ABORT);
1449ac27a0ecSDave Kleikamp 			break;
145006705bffSTheodore Ts'o 		case Opt_nobarrier:
145106705bffSTheodore Ts'o 			clear_opt(sbi->s_mount_opt, BARRIER);
145206705bffSTheodore Ts'o 			break;
1453ac27a0ecSDave Kleikamp 		case Opt_barrier:
145406705bffSTheodore Ts'o 			if (match_int(&args[0], &option)) {
145506705bffSTheodore Ts'o 				set_opt(sbi->s_mount_opt, BARRIER);
145606705bffSTheodore Ts'o 				break;
145706705bffSTheodore Ts'o 			}
1458ac27a0ecSDave Kleikamp 			if (option)
1459ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1460ac27a0ecSDave Kleikamp 			else
1461ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1462ac27a0ecSDave Kleikamp 			break;
1463ac27a0ecSDave Kleikamp 		case Opt_ignore:
1464ac27a0ecSDave Kleikamp 			break;
1465ac27a0ecSDave Kleikamp 		case Opt_resize:
1466ac27a0ecSDave Kleikamp 			if (!is_remount) {
1467617ba13bSMingming Cao 				printk("EXT4-fs: resize option only available "
1468ac27a0ecSDave Kleikamp 					"for remount\n");
1469ac27a0ecSDave Kleikamp 				return 0;
1470ac27a0ecSDave Kleikamp 			}
1471ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1472ac27a0ecSDave Kleikamp 				return 0;
1473ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1474ac27a0ecSDave Kleikamp 			break;
1475ac27a0ecSDave Kleikamp 		case Opt_nobh:
1476ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1477ac27a0ecSDave Kleikamp 			break;
1478ac27a0ecSDave Kleikamp 		case Opt_bh:
1479ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1480ac27a0ecSDave Kleikamp 			break;
148125ec56b5SJean Noel Cordenner 		case Opt_i_version:
148225ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
148325ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
148425ec56b5SJean Noel Cordenner 			break;
1485dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1486dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1487dd919b98SAneesh Kumar K.V 			break;
1488c9de560dSAlex Tomas 		case Opt_stripe:
1489c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1490c9de560dSAlex Tomas 				return 0;
1491c9de560dSAlex Tomas 			if (option < 0)
1492c9de560dSAlex Tomas 				return 0;
1493c9de560dSAlex Tomas 			sbi->s_stripe = option;
1494c9de560dSAlex Tomas 			break;
149564769240SAlex Tomas 		case Opt_delalloc:
149664769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
149764769240SAlex Tomas 			break;
1498240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1499240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1500240799cdSTheodore Ts'o 				return 0;
1501240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1502240799cdSTheodore Ts'o 				return 0;
1503f7c43950STheodore Ts'o 			if (!is_power_of_2(option)) {
15043197ebdbSTheodore Ts'o 				printk(KERN_ERR "EXT4-fs: inode_readahead_blks"
15053197ebdbSTheodore Ts'o 				       " must be a power of 2\n");
15063197ebdbSTheodore Ts'o 				return 0;
15073197ebdbSTheodore Ts'o 			}
1508240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1509240799cdSTheodore Ts'o 			break;
1510b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1511b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1512b3881f74STheodore Ts'o 				return 0;
1513b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1514b3881f74STheodore Ts'o 				break;
1515b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1516b3881f74STheodore Ts'o 							    option);
1517b3881f74STheodore Ts'o 			break;
151806705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
151906705bffSTheodore Ts'o 			set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
152006705bffSTheodore Ts'o 			break;
1521afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
152206705bffSTheodore Ts'o 			if (match_int(&args[0], &option)) {
152306705bffSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
152406705bffSTheodore Ts'o 				break;
152506705bffSTheodore Ts'o 			}
1526afd4672dSTheodore Ts'o 			if (option)
1527afd4672dSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1528afd4672dSTheodore Ts'o 			else
1529afd4672dSTheodore Ts'o 				set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1530afd4672dSTheodore Ts'o 			break;
1531ac27a0ecSDave Kleikamp 		default:
1532ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1533617ba13bSMingming Cao 			       "EXT4-fs: Unrecognized mount option \"%s\" "
1534ac27a0ecSDave Kleikamp 			       "or missing value\n", p);
1535ac27a0ecSDave Kleikamp 			return 0;
1536ac27a0ecSDave Kleikamp 		}
1537ac27a0ecSDave Kleikamp 	}
1538ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1539ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1540617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1541ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1542ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1543ac27a0ecSDave Kleikamp 
1544617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1545ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1546ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1547ac27a0ecSDave Kleikamp 
1548ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1549617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1550ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1551617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1552617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: old and new quota "
1553ac27a0ecSDave Kleikamp 					"format mixing.\n");
1554ac27a0ecSDave Kleikamp 			return 0;
1555ac27a0ecSDave Kleikamp 		}
1556ac27a0ecSDave Kleikamp 
1557ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
15582c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
1559ac27a0ecSDave Kleikamp 					"not specified.\n");
1560ac27a0ecSDave Kleikamp 			return 0;
1561ac27a0ecSDave Kleikamp 		}
1562ac27a0ecSDave Kleikamp 	} else {
1563ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
15642c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
15652c8be6b2SJan Kara 					"specified with no journaling "
1566ac27a0ecSDave Kleikamp 					"enabled.\n");
1567ac27a0ecSDave Kleikamp 			return 0;
1568ac27a0ecSDave Kleikamp 		}
1569ac27a0ecSDave Kleikamp 	}
1570ac27a0ecSDave Kleikamp #endif
1571ac27a0ecSDave Kleikamp 	return 1;
1572ac27a0ecSDave Kleikamp }
1573ac27a0ecSDave Kleikamp 
1574617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1575ac27a0ecSDave Kleikamp 			    int read_only)
1576ac27a0ecSDave Kleikamp {
1577617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1578ac27a0ecSDave Kleikamp 	int res = 0;
1579ac27a0ecSDave Kleikamp 
1580617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1581617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1582ac27a0ecSDave Kleikamp 		       "forcing read-only mode\n");
1583ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1584ac27a0ecSDave Kleikamp 	}
1585ac27a0ecSDave Kleikamp 	if (read_only)
1586ac27a0ecSDave Kleikamp 		return res;
1587617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1588617ba13bSMingming Cao 		printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1589ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1590617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1591ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1592617ba13bSMingming Cao 		       "EXT4-fs warning: mounting fs with errors, "
1593ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1594ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1595ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1596ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1597ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1598617ba13bSMingming Cao 		       "EXT4-fs warning: maximal mount count reached, "
1599ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1600ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1601ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1602ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1603ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1604617ba13bSMingming Cao 		       "EXT4-fs warning: checktime reached, "
1605ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
16060390131bSFrank Mayhar 	if (!sbi->s_journal)
1607216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1608ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1609617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1610e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1611ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1612617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
16130390131bSFrank Mayhar 	if (sbi->s_journal)
1614617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1615ac27a0ecSDave Kleikamp 
1616e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1617ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1618a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1619ac27a0ecSDave Kleikamp 				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
1620ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1621ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1622617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1623617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1624ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1625ac27a0ecSDave Kleikamp 
16260390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
162705496769STheodore Ts'o 		printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
162805496769STheodore Ts'o 		       sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
162905496769STheodore Ts'o 		       "external", EXT4_SB(sb)->s_journal->j_devname);
16300390131bSFrank Mayhar 	} else {
16310390131bSFrank Mayhar 		printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
16320390131bSFrank Mayhar 	}
1633ac27a0ecSDave Kleikamp 	return res;
1634ac27a0ecSDave Kleikamp }
1635ac27a0ecSDave Kleikamp 
1636772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1637772cb7c8SJose R. Santos {
1638772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1639772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1640772cb7c8SJose R. Santos 	struct buffer_head *bh;
1641772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1642772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1643772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1644c5ca7c76STheodore Ts'o 	size_t size;
1645772cb7c8SJose R. Santos 	int i;
1646772cb7c8SJose R. Santos 
1647772cb7c8SJose R. Santos 	if (!sbi->s_es->s_log_groups_per_flex) {
1648772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1649772cb7c8SJose R. Santos 		return 1;
1650772cb7c8SJose R. Santos 	}
1651772cb7c8SJose R. Santos 
1652772cb7c8SJose R. Santos 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1653772cb7c8SJose R. Santos 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1654772cb7c8SJose R. Santos 
1655c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1656c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1657d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1658d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1659c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
1660c5ca7c76STheodore Ts'o 	sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1661c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
1662c5ca7c76STheodore Ts'o 		sbi->s_flex_groups = vmalloc(size);
1663c5ca7c76STheodore Ts'o 		if (sbi->s_flex_groups)
1664c5ca7c76STheodore Ts'o 			memset(sbi->s_flex_groups, 0, size);
1665c5ca7c76STheodore Ts'o 	}
1666772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1667ec05e868SLi Zefan 		printk(KERN_ERR "EXT4-fs: not enough memory for "
1668a9df9a49STheodore Ts'o 				"%u flex groups\n", flex_group_count);
1669772cb7c8SJose R. Santos 		goto failed;
1670772cb7c8SJose R. Santos 	}
1671772cb7c8SJose R. Santos 
1672772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
1673772cb7c8SJose R. Santos 		gdp = ext4_get_group_desc(sb, i, &bh);
1674772cb7c8SJose R. Santos 
1675772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
16769f24e420STheodore Ts'o 		atomic_set(&sbi->s_flex_groups[flex_group].free_inodes,
16779f24e420STheodore Ts'o 			   ext4_free_inodes_count(sb, gdp));
16789f24e420STheodore Ts'o 		atomic_set(&sbi->s_flex_groups[flex_group].free_blocks,
16799f24e420STheodore Ts'o 			   ext4_free_blks_count(sb, gdp));
16807d39db14STheodore Ts'o 		atomic_set(&sbi->s_flex_groups[flex_group].used_dirs,
16817d39db14STheodore Ts'o 			   ext4_used_dirs_count(sb, gdp));
1682772cb7c8SJose R. Santos 	}
1683772cb7c8SJose R. Santos 
1684772cb7c8SJose R. Santos 	return 1;
1685772cb7c8SJose R. Santos failed:
1686772cb7c8SJose R. Santos 	return 0;
1687772cb7c8SJose R. Santos }
1688772cb7c8SJose R. Santos 
1689717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1690717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1691717d50e4SAndreas Dilger {
1692717d50e4SAndreas Dilger 	__u16 crc = 0;
1693717d50e4SAndreas Dilger 
1694717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1695717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1696717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1697717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1698717d50e4SAndreas Dilger 
1699717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1700717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1701717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1702717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1703717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1704717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1705717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1706717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1707717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1708717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1709717d50e4SAndreas Dilger 					offset);
1710717d50e4SAndreas Dilger 	}
1711717d50e4SAndreas Dilger 
1712717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1713717d50e4SAndreas Dilger }
1714717d50e4SAndreas Dilger 
1715717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1716717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1717717d50e4SAndreas Dilger {
1718717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1719717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1720717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1721717d50e4SAndreas Dilger 		return 0;
1722717d50e4SAndreas Dilger 
1723717d50e4SAndreas Dilger 	return 1;
1724717d50e4SAndreas Dilger }
1725717d50e4SAndreas Dilger 
1726ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1727617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1728ac27a0ecSDave Kleikamp {
1729617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1730617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1731617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1732bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1733bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1734bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1735ce421581SJose R. Santos 	int flexbg_flag = 0;
1736fd2d4291SAvantika Mathur 	ext4_group_t i;
1737ac27a0ecSDave Kleikamp 
1738ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1739ce421581SJose R. Santos 		flexbg_flag = 1;
1740ce421581SJose R. Santos 
1741617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1742ac27a0ecSDave Kleikamp 
1743197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1744197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1745197cd65aSAkinobu Mita 
1746ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1747bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1748ac27a0ecSDave Kleikamp 		else
1749ac27a0ecSDave Kleikamp 			last_block = first_block +
1750617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1751ac27a0ecSDave Kleikamp 
17528fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
17532b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1754c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1755a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
17565128273aSEric Sesterhenn 			       "(block %llu)!\n", i, block_bitmap);
1757ac27a0ecSDave Kleikamp 			return 0;
1758ac27a0ecSDave Kleikamp 		}
17598fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
17602b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1761c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1762a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
17635128273aSEric Sesterhenn 			       "(block %llu)!\n", i, inode_bitmap);
1764ac27a0ecSDave Kleikamp 			return 0;
1765ac27a0ecSDave Kleikamp 		}
17668fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1767bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
17682b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1769c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1770a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
17715128273aSEric Sesterhenn 			       "(block %llu)!\n", i, inode_table);
1772ac27a0ecSDave Kleikamp 			return 0;
1773ac27a0ecSDave Kleikamp 		}
1774b5f10eedSEric Sandeen 		spin_lock(sb_bgl_lock(sbi, i));
1775717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1776c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1777a9df9a49STheodore Ts'o 			       "Checksum for group %u failed (%u!=%u)\n",
1778fd2d4291SAvantika Mathur 			       i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1779fd2d4291SAvantika Mathur 			       gdp)), le16_to_cpu(gdp->bg_checksum));
17807ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
17817ee1ec4cSLi Zefan 				spin_unlock(sb_bgl_lock(sbi, i));
1782717d50e4SAndreas Dilger 				return 0;
1783717d50e4SAndreas Dilger 			}
17847ee1ec4cSLi Zefan 		}
1785b5f10eedSEric Sandeen 		spin_unlock(sb_bgl_lock(sbi, i));
1786ce421581SJose R. Santos 		if (!flexbg_flag)
1787617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1788ac27a0ecSDave Kleikamp 	}
1789ac27a0ecSDave Kleikamp 
1790bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1791617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1792ac27a0ecSDave Kleikamp 	return 1;
1793ac27a0ecSDave Kleikamp }
1794ac27a0ecSDave Kleikamp 
1795617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1796ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1797ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1798ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1799ac27a0ecSDave Kleikamp  *
1800ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1801ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1802ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1803ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1804ac27a0ecSDave Kleikamp  *
1805ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1806ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1807ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1808617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1809ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1810ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1811ac27a0ecSDave Kleikamp  */
1812617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1813617ba13bSMingming Cao 				struct ext4_super_block *es)
1814ac27a0ecSDave Kleikamp {
1815ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1816ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1817ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1818ac27a0ecSDave Kleikamp 	int i;
1819ac27a0ecSDave Kleikamp #endif
1820ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1821ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1822ac27a0ecSDave Kleikamp 		return;
1823ac27a0ecSDave Kleikamp 	}
1824ac27a0ecSDave Kleikamp 
1825a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1826a8f48a95SEric Sandeen 		printk(KERN_ERR "EXT4-fs: write access "
1827a8f48a95SEric Sandeen 			"unavailable, skipping orphan cleanup.\n");
1828a8f48a95SEric Sandeen 		return;
1829a8f48a95SEric Sandeen 	}
1830a8f48a95SEric Sandeen 
1831617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1832ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1833ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1834ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1835ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1836ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1837ac27a0ecSDave Kleikamp 		return;
1838ac27a0ecSDave Kleikamp 	}
1839ac27a0ecSDave Kleikamp 
1840ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1841617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1842ac27a0ecSDave Kleikamp 		       sb->s_id);
1843ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1844ac27a0ecSDave Kleikamp 	}
1845ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1846ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1847ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1848ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1849ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1850617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1851617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1852ac27a0ecSDave Kleikamp 			if (ret < 0)
1853ac27a0ecSDave Kleikamp 				printk(KERN_ERR
18542c8be6b2SJan Kara 					"EXT4-fs: Cannot turn on journaled "
1855ac27a0ecSDave Kleikamp 					"quota: error %d\n", ret);
1856ac27a0ecSDave Kleikamp 		}
1857ac27a0ecSDave Kleikamp 	}
1858ac27a0ecSDave Kleikamp #endif
1859ac27a0ecSDave Kleikamp 
1860ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1861ac27a0ecSDave Kleikamp 		struct inode *inode;
1862ac27a0ecSDave Kleikamp 
186397bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
186497bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
1865ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1866ac27a0ecSDave Kleikamp 			break;
1867ac27a0ecSDave Kleikamp 		}
1868ac27a0ecSDave Kleikamp 
1869617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1870a269eb18SJan Kara 		vfs_dq_init(inode);
1871ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1872ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1873e5f8eab8STheodore Ts'o 				"%s: truncating inode %lu to %lld bytes\n",
187446e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
1875e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1876ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1877617ba13bSMingming Cao 			ext4_truncate(inode);
1878ac27a0ecSDave Kleikamp 			nr_truncates++;
1879ac27a0ecSDave Kleikamp 		} else {
1880ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1881ac27a0ecSDave Kleikamp 				"%s: deleting unreferenced inode %lu\n",
188246e665e9SHarvey Harrison 				__func__, inode->i_ino);
1883ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1884ac27a0ecSDave Kleikamp 				  inode->i_ino);
1885ac27a0ecSDave Kleikamp 			nr_orphans++;
1886ac27a0ecSDave Kleikamp 		}
1887ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1888ac27a0ecSDave Kleikamp 	}
1889ac27a0ecSDave Kleikamp 
1890ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1891ac27a0ecSDave Kleikamp 
1892ac27a0ecSDave Kleikamp 	if (nr_orphans)
1893617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1894ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_orphans));
1895ac27a0ecSDave Kleikamp 	if (nr_truncates)
1896617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1897ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_truncates));
1898ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1899ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1900ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1901ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
19026f28e087SJan Kara 			vfs_quota_off(sb, i, 0);
1903ac27a0ecSDave Kleikamp 	}
1904ac27a0ecSDave Kleikamp #endif
1905ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1906ac27a0ecSDave Kleikamp }
1907cd2291a4SEric Sandeen /*
1908cd2291a4SEric Sandeen  * Maximal extent format file size.
1909cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1910cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1911cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1912cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1913cd2291a4SEric Sandeen  *
1914cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1915cd2291a4SEric Sandeen  */
1916f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
1917cd2291a4SEric Sandeen {
1918cd2291a4SEric Sandeen 	loff_t res;
1919cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
1920cd2291a4SEric Sandeen 
1921cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
1922f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1923cd2291a4SEric Sandeen 		/*
1924b3a6ffe1SJens Axboe 		 * CONFIG_LBD is not enabled implies the inode
1925cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
1926cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
1927cd2291a4SEric Sandeen 		 */
1928cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
1929cd2291a4SEric Sandeen 
1930cd2291a4SEric Sandeen 		/* total blocks in file system block size */
1931cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
1932cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
1933cd2291a4SEric Sandeen 	}
1934cd2291a4SEric Sandeen 
1935cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
1936cd2291a4SEric Sandeen 	res = 1LL << 32;
1937cd2291a4SEric Sandeen 	res <<= blkbits;
1938cd2291a4SEric Sandeen 	res -= 1;
1939cd2291a4SEric Sandeen 
1940cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
1941cd2291a4SEric Sandeen 	if (res > upper_limit)
1942cd2291a4SEric Sandeen 		res = upper_limit;
1943cd2291a4SEric Sandeen 
1944cd2291a4SEric Sandeen 	return res;
1945cd2291a4SEric Sandeen }
1946ac27a0ecSDave Kleikamp 
1947ac27a0ecSDave Kleikamp /*
1948cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
19490fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
19500fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
1951ac27a0ecSDave Kleikamp  */
1952f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1953ac27a0ecSDave Kleikamp {
1954617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
19550fc1b451SAneesh Kumar K.V 	int meta_blocks;
19560fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
19570fc1b451SAneesh Kumar K.V 	/* This is calculated to be the largest file size for a
1958cd2291a4SEric Sandeen 	 * dense, bitmapped file such that the total number of
1959ac27a0ecSDave Kleikamp 	 * sectors in the file, including data and all indirect blocks,
19600fc1b451SAneesh Kumar K.V 	 * does not exceed 2^48 -1
19610fc1b451SAneesh Kumar K.V 	 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
19620fc1b451SAneesh Kumar K.V 	 * total number of  512 bytes blocks of the file
19630fc1b451SAneesh Kumar K.V 	 */
19640fc1b451SAneesh Kumar K.V 
1965f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
19660fc1b451SAneesh Kumar K.V 		/*
1967b3a6ffe1SJens Axboe 		 * !has_huge_files or CONFIG_LBD is not enabled
1968f287a1a5STheodore Ts'o 		 * implies the inode i_block represent total blocks in
1969f287a1a5STheodore Ts'o 		 * 512 bytes 32 == size of vfs inode i_blocks * 8
19700fc1b451SAneesh Kumar K.V 		 */
19710fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
19720fc1b451SAneesh Kumar K.V 
19730fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
19740fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
19750fc1b451SAneesh Kumar K.V 
19760fc1b451SAneesh Kumar K.V 	} else {
19778180a562SAneesh Kumar K.V 		/*
19788180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
19798180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
19808180a562SAneesh Kumar K.V 		 * represent total number of blocks in
19818180a562SAneesh Kumar K.V 		 * file system block size
19828180a562SAneesh Kumar K.V 		 */
19830fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
19840fc1b451SAneesh Kumar K.V 
19850fc1b451SAneesh Kumar K.V 	}
19860fc1b451SAneesh Kumar K.V 
19870fc1b451SAneesh Kumar K.V 	/* indirect blocks */
19880fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
19890fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
19900fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
19910fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
19920fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
19930fc1b451SAneesh Kumar K.V 
19940fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
19950fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
1996ac27a0ecSDave Kleikamp 
1997ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
1998ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
1999ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2000ac27a0ecSDave Kleikamp 	res <<= bits;
2001ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2002ac27a0ecSDave Kleikamp 		res = upper_limit;
20030fc1b451SAneesh Kumar K.V 
20040fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
20050fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
20060fc1b451SAneesh Kumar K.V 
2007ac27a0ecSDave Kleikamp 	return res;
2008ac27a0ecSDave Kleikamp }
2009ac27a0ecSDave Kleikamp 
2010617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
201170bbb3e0SAndrew Morton 				ext4_fsblk_t logical_sb_block, int nr)
2012ac27a0ecSDave Kleikamp {
2013617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2014fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2015ac27a0ecSDave Kleikamp 	int has_super = 0;
2016ac27a0ecSDave Kleikamp 
2017ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2018ac27a0ecSDave Kleikamp 
2019617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2020ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
202170bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2022ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2023617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2024ac27a0ecSDave Kleikamp 		has_super = 1;
2025617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2026ac27a0ecSDave Kleikamp }
2027ac27a0ecSDave Kleikamp 
2028c9de560dSAlex Tomas /**
2029c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2030c9de560dSAlex Tomas  * @sbi: In memory super block info
2031c9de560dSAlex Tomas  *
2032c9de560dSAlex Tomas  * If we have specified it via mount option, then
2033c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2034c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2035c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2036c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2037c9de560dSAlex Tomas  *
2038c9de560dSAlex Tomas  */
2039c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2040c9de560dSAlex Tomas {
2041c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2042c9de560dSAlex Tomas 	unsigned long stripe_width =
2043c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2044c9de560dSAlex Tomas 
2045c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2046c9de560dSAlex Tomas 		return sbi->s_stripe;
2047c9de560dSAlex Tomas 
2048c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
2049c9de560dSAlex Tomas 		return stripe_width;
2050c9de560dSAlex Tomas 
2051c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
2052c9de560dSAlex Tomas 		return stride;
2053c9de560dSAlex Tomas 
2054c9de560dSAlex Tomas 	return 0;
2055c9de560dSAlex Tomas }
2056ac27a0ecSDave Kleikamp 
20573197ebdbSTheodore Ts'o /* sysfs supprt */
20583197ebdbSTheodore Ts'o 
20593197ebdbSTheodore Ts'o struct ext4_attr {
20603197ebdbSTheodore Ts'o 	struct attribute attr;
20613197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
20623197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
20633197ebdbSTheodore Ts'o 			 const char *, size_t);
20643197ebdbSTheodore Ts'o 	int offset;
20653197ebdbSTheodore Ts'o };
20663197ebdbSTheodore Ts'o 
20673197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
20683197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
20693197ebdbSTheodore Ts'o {
20703197ebdbSTheodore Ts'o 	char *endp;
20713197ebdbSTheodore Ts'o 
20723197ebdbSTheodore Ts'o 	while (*buf && isspace(*buf))
20733197ebdbSTheodore Ts'o 		buf++;
20743197ebdbSTheodore Ts'o 	*value = simple_strtoul(buf, &endp, 0);
20753197ebdbSTheodore Ts'o 	while (*endp && isspace(*endp))
20763197ebdbSTheodore Ts'o 		endp++;
20773197ebdbSTheodore Ts'o 	if (*endp || *value > max)
20783197ebdbSTheodore Ts'o 		return -EINVAL;
20793197ebdbSTheodore Ts'o 
20803197ebdbSTheodore Ts'o 	return 0;
20813197ebdbSTheodore Ts'o }
20823197ebdbSTheodore Ts'o 
20833197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
20843197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
20853197ebdbSTheodore Ts'o 					      char *buf)
20863197ebdbSTheodore Ts'o {
20873197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
20883197ebdbSTheodore Ts'o 			(s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
20893197ebdbSTheodore Ts'o }
20903197ebdbSTheodore Ts'o 
20913197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
20923197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
20933197ebdbSTheodore Ts'o {
20943197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
20953197ebdbSTheodore Ts'o 
20963197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
20973197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
20983197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
20993197ebdbSTheodore Ts'o }
21003197ebdbSTheodore Ts'o 
21013197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
21023197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
21033197ebdbSTheodore Ts'o {
21043197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
21053197ebdbSTheodore Ts'o 
21063197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
21073197ebdbSTheodore Ts'o 			sbi->s_kbytes_written +
21083197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
21093197ebdbSTheodore Ts'o 			  EXT4_SB(sb)->s_sectors_written_start) >> 1));
21103197ebdbSTheodore Ts'o }
21113197ebdbSTheodore Ts'o 
21123197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
21133197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
21143197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
21153197ebdbSTheodore Ts'o {
21163197ebdbSTheodore Ts'o 	unsigned long t;
21173197ebdbSTheodore Ts'o 
21183197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
21193197ebdbSTheodore Ts'o 		return -EINVAL;
21203197ebdbSTheodore Ts'o 
2121f7c43950STheodore Ts'o 	if (!is_power_of_2(t))
21223197ebdbSTheodore Ts'o 		return -EINVAL;
21233197ebdbSTheodore Ts'o 
21243197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
21253197ebdbSTheodore Ts'o 	return count;
21263197ebdbSTheodore Ts'o }
21273197ebdbSTheodore Ts'o 
21283197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
21293197ebdbSTheodore Ts'o 				struct ext4_sb_info *sbi, char *buf)
21303197ebdbSTheodore Ts'o {
21313197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
21323197ebdbSTheodore Ts'o 
21333197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
21343197ebdbSTheodore Ts'o }
21353197ebdbSTheodore Ts'o 
21363197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
21373197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
21383197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
21393197ebdbSTheodore Ts'o {
21403197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
21413197ebdbSTheodore Ts'o 	unsigned long t;
21423197ebdbSTheodore Ts'o 
21433197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
21443197ebdbSTheodore Ts'o 		return -EINVAL;
21453197ebdbSTheodore Ts'o 	*ui = t;
21463197ebdbSTheodore Ts'o 	return count;
21473197ebdbSTheodore Ts'o }
21483197ebdbSTheodore Ts'o 
21493197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
21503197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
21513197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
21523197ebdbSTheodore Ts'o 	.show	= _show,					\
21533197ebdbSTheodore Ts'o 	.store	= _store,					\
21543197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
21553197ebdbSTheodore Ts'o }
21563197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
21573197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
21583197ebdbSTheodore Ts'o 
21593197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
21603197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
21613197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
21623197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
21633197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
21643197ebdbSTheodore Ts'o 
21653197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
21663197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
21673197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
21683197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
21693197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
21703197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
21713197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
21723197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
21733197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
21743197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
21753197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
21763197ebdbSTheodore Ts'o 
21773197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
21783197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
21793197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
21803197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
21813197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
21823197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
21833197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
21843197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
21853197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
21863197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
21873197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
21883197ebdbSTheodore Ts'o 	NULL,
21893197ebdbSTheodore Ts'o };
21903197ebdbSTheodore Ts'o 
21913197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
21923197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
21933197ebdbSTheodore Ts'o {
21943197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
21953197ebdbSTheodore Ts'o 						s_kobj);
21963197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
21973197ebdbSTheodore Ts'o 
21983197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
21993197ebdbSTheodore Ts'o }
22003197ebdbSTheodore Ts'o 
22013197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
22023197ebdbSTheodore Ts'o 			       struct attribute *attr,
22033197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
22043197ebdbSTheodore Ts'o {
22053197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22063197ebdbSTheodore Ts'o 						s_kobj);
22073197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
22083197ebdbSTheodore Ts'o 
22093197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
22103197ebdbSTheodore Ts'o }
22113197ebdbSTheodore Ts'o 
22123197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
22133197ebdbSTheodore Ts'o {
22143197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22153197ebdbSTheodore Ts'o 						s_kobj);
22163197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
22173197ebdbSTheodore Ts'o }
22183197ebdbSTheodore Ts'o 
22193197ebdbSTheodore Ts'o 
22203197ebdbSTheodore Ts'o static struct sysfs_ops ext4_attr_ops = {
22213197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
22223197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
22233197ebdbSTheodore Ts'o };
22243197ebdbSTheodore Ts'o 
22253197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
22263197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
22273197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
22283197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
22293197ebdbSTheodore Ts'o };
22303197ebdbSTheodore Ts'o 
2231617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
22327477827fSAneesh Kumar K.V 				__releases(kernel_lock)
22337477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
22341d03ec98SAneesh Kumar K.V 
2235ac27a0ecSDave Kleikamp {
2236ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2237617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
2238617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2239617ba13bSMingming Cao 	ext4_fsblk_t block;
2240617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
224170bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
2242ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
2243ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
2244ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
2245ac27a0ecSDave Kleikamp 	struct inode *root;
22469f6200bbSTheodore Ts'o 	char *cp;
22470390131bSFrank Mayhar 	const char *descr;
22481d1fe1eeSDavid Howells 	int ret = -EINVAL;
2249ac27a0ecSDave Kleikamp 	int blocksize;
22504ec11028STheodore Ts'o 	unsigned int db_count;
22514ec11028STheodore Ts'o 	unsigned int i;
2252f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
22533a06d778SAneesh Kumar K.V 	int features;
2254bd81d8eeSLaurent Vivier 	__u64 blocks_count;
2255833f4077SPeter Zijlstra 	int err;
2256b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
2257ac27a0ecSDave Kleikamp 
2258ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2259ac27a0ecSDave Kleikamp 	if (!sbi)
2260ac27a0ecSDave Kleikamp 		return -ENOMEM;
2261705895b6SPekka Enberg 
2262705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
2263705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2264705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
2265705895b6SPekka Enberg 		kfree(sbi);
2266705895b6SPekka Enberg 		return -ENOMEM;
2267705895b6SPekka Enberg 	}
2268ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
2269ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
2270617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
2271617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
2272240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2273d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
2274afc32f7eSTheodore Ts'o 	sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2275afc32f7eSTheodore Ts'o 						      sectors[1]);
2276ac27a0ecSDave Kleikamp 
2277ac27a0ecSDave Kleikamp 	unlock_kernel();
2278ac27a0ecSDave Kleikamp 
22799f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
22809f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
22819f6200bbSTheodore Ts'o 		*cp = '!';
22829f6200bbSTheodore Ts'o 
2283617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2284ac27a0ecSDave Kleikamp 	if (!blocksize) {
2285617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
2286ac27a0ecSDave Kleikamp 		goto out_fail;
2287ac27a0ecSDave Kleikamp 	}
2288ac27a0ecSDave Kleikamp 
2289ac27a0ecSDave Kleikamp 	/*
2290617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
2291ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
2292ac27a0ecSDave Kleikamp 	 */
2293617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
229470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
229570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
2296ac27a0ecSDave Kleikamp 	} else {
229770bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
2298ac27a0ecSDave Kleikamp 	}
2299ac27a0ecSDave Kleikamp 
230070bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
2301617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
2302ac27a0ecSDave Kleikamp 		goto out_fail;
2303ac27a0ecSDave Kleikamp 	}
2304ac27a0ecSDave Kleikamp 	/*
2305ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
2306617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
2307ac27a0ecSDave Kleikamp 	 */
2308617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2309ac27a0ecSDave Kleikamp 	sbi->s_es = es;
2310ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
2311617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
2312617ba13bSMingming Cao 		goto cantfind_ext4;
2313afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
2314ac27a0ecSDave Kleikamp 
2315ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
2316ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2317617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
2318ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
2319617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2320ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
2321617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
2322ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
232303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
2324617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2325ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
23262e7842b8SHugh Dickins #endif
232703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
2328617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
2329ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
23302e7842b8SHugh Dickins #endif
2331617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2332617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2333617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2334617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2335617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2336617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2337ac27a0ecSDave Kleikamp 
2338617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2339ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2340bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2341ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
2342bb4f397aSAneesh Kumar K.V 	else
2343bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
2344ac27a0ecSDave Kleikamp 
2345ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2346ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
234730773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
234830773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
234930773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2350ac27a0ecSDave Kleikamp 
2351571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
2352ac27a0ecSDave Kleikamp 
23531e2462f9SMingming Cao 	/*
2354dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
2355dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
2356dd919b98SAneesh Kumar K.V 	 */
2357dd919b98SAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, DELALLOC);
2358dd919b98SAneesh Kumar K.V 
2359dd919b98SAneesh Kumar K.V 
2360b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
2361b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
2362ac27a0ecSDave Kleikamp 		goto failed_mount;
2363ac27a0ecSDave Kleikamp 
2364ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2365617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2366ac27a0ecSDave Kleikamp 
2367617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2368617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2369617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2370617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2371ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
2372617ba13bSMingming Cao 		       "EXT4-fs warning: feature flags set on rev 0 fs, "
2373ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
2374469108ffSTheodore Tso 
2375469108ffSTheodore Tso 	/*
2376ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2377ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2378ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2379ac27a0ecSDave Kleikamp 	 */
2380617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2381ac27a0ecSDave Kleikamp 	if (features) {
2382617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
23833a06d778SAneesh Kumar K.V 		       "unsupported optional features (%x).\n", sb->s_id,
23843a06d778SAneesh Kumar K.V 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
23853a06d778SAneesh Kumar K.V 			~EXT4_FEATURE_INCOMPAT_SUPP));
2386ac27a0ecSDave Kleikamp 		goto failed_mount;
2387ac27a0ecSDave Kleikamp 	}
2388617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2389ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
2390617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
23913a06d778SAneesh Kumar K.V 		       "unsupported optional features (%x).\n", sb->s_id,
23923a06d778SAneesh Kumar K.V 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
23933a06d778SAneesh Kumar K.V 			~EXT4_FEATURE_RO_COMPAT_SUPP));
2394ac27a0ecSDave Kleikamp 		goto failed_mount;
2395ac27a0ecSDave Kleikamp 	}
2396f287a1a5STheodore Ts'o 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2397f287a1a5STheodore Ts'o 				    EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2398f287a1a5STheodore Ts'o 	if (has_huge_files) {
23990fc1b451SAneesh Kumar K.V 		/*
24000fc1b451SAneesh Kumar K.V 		 * Large file size enabled file system can only be
2401b3a6ffe1SJens Axboe 		 * mount if kernel is build with CONFIG_LBD
24020fc1b451SAneesh Kumar K.V 		 */
24030fc1b451SAneesh Kumar K.V 		if (sizeof(root->i_blocks) < sizeof(u64) &&
24040fc1b451SAneesh Kumar K.V 				!(sb->s_flags & MS_RDONLY)) {
24050fc1b451SAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
24060fc1b451SAneesh Kumar K.V 					"files cannot be mounted read-write "
2407b3a6ffe1SJens Axboe 					"without CONFIG_LBD.\n", sb->s_id);
24080fc1b451SAneesh Kumar K.V 			goto failed_mount;
24090fc1b451SAneesh Kumar K.V 		}
24100fc1b451SAneesh Kumar K.V 	}
2411ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2412ac27a0ecSDave Kleikamp 
2413617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2414617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2415ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2416617ba13bSMingming Cao 		       "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2417ac27a0ecSDave Kleikamp 		       blocksize, sb->s_id);
2418ac27a0ecSDave Kleikamp 		goto failed_mount;
2419ac27a0ecSDave Kleikamp 	}
2420ac27a0ecSDave Kleikamp 
2421ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2422ce40733cSAneesh Kumar K.V 
2423ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2424ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2425ce40733cSAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2426ce40733cSAneesh Kumar K.V 					blocksize);
2427ac27a0ecSDave Kleikamp 			goto failed_mount;
2428ac27a0ecSDave Kleikamp 		}
2429ac27a0ecSDave Kleikamp 
2430ac27a0ecSDave Kleikamp 		brelse(bh);
243170bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
243270bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
243370bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2434ac27a0ecSDave Kleikamp 		if (!bh) {
2435ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2436617ba13bSMingming Cao 			       "EXT4-fs: Can't read superblock on 2nd try.\n");
2437ac27a0ecSDave Kleikamp 			goto failed_mount;
2438ac27a0ecSDave Kleikamp 		}
2439617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2440ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2441617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2442ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2443617ba13bSMingming Cao 			       "EXT4-fs: Magic mismatch, very weird !\n");
2444ac27a0ecSDave Kleikamp 			goto failed_mount;
2445ac27a0ecSDave Kleikamp 		}
2446ac27a0ecSDave Kleikamp 	}
2447ac27a0ecSDave Kleikamp 
2448f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2449f287a1a5STheodore Ts'o 						      has_huge_files);
2450f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2451ac27a0ecSDave Kleikamp 
2452617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2453617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2454617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2455ac27a0ecSDave Kleikamp 	} else {
2456ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2457ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2458617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
24591330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2460ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2461ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2462617ba13bSMingming Cao 			       "EXT4-fs: unsupported inode size: %d\n",
2463ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2464ac27a0ecSDave Kleikamp 			goto failed_mount;
2465ac27a0ecSDave Kleikamp 		}
2466ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2467ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2468ac27a0ecSDave Kleikamp 	}
24690d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
24700d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
24718fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
24720d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2473d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
24740d1ee42fSAlexandre Ratchov 			printk(KERN_ERR
24758fadc143SAlexandre Ratchov 			       "EXT4-fs: unsupported descriptor size %lu\n",
24760d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
24770d1ee42fSAlexandre Ratchov 			goto failed_mount;
24780d1ee42fSAlexandre Ratchov 		}
24790d1ee42fSAlexandre Ratchov 	} else
24800d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2481ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2482ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2483b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2484617ba13bSMingming Cao 		goto cantfind_ext4;
2485617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2486ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2487617ba13bSMingming Cao 		goto cantfind_ext4;
2488ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2489ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
24900d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2491ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2492ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2493e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2494e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2495ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2496ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2497ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2498f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
2499f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
2500f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2501f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2502f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
2503f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2504f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2505f99b2589STheodore Ts'o #else
2506f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2507f99b2589STheodore Ts'o #endif
2508f99b2589STheodore Ts'o 		sb->s_dirt = 1;
2509f99b2589STheodore Ts'o 	}
2510ac27a0ecSDave Kleikamp 
2511ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2512ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2513617ba13bSMingming Cao 		       "EXT4-fs: #blocks per group too big: %lu\n",
2514ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2515ac27a0ecSDave Kleikamp 		goto failed_mount;
2516ac27a0ecSDave Kleikamp 	}
2517ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2518ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2519617ba13bSMingming Cao 		       "EXT4-fs: #inodes per group too big: %lu\n",
2520ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2521ac27a0ecSDave Kleikamp 		goto failed_mount;
2522ac27a0ecSDave Kleikamp 	}
2523ac27a0ecSDave Kleikamp 
2524bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
2525ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2526617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2527ac27a0ecSDave Kleikamp 			" too large to mount safely\n", sb->s_id);
2528ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
2529617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2530ac27a0ecSDave Kleikamp 					"enabled\n");
2531ac27a0ecSDave Kleikamp 		goto failed_mount;
2532ac27a0ecSDave Kleikamp 	}
2533ac27a0ecSDave Kleikamp 
2534617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2535617ba13bSMingming Cao 		goto cantfind_ext4;
2536e7c95593SEric Sandeen 
25370f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
25380f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
25390f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
25400f2ddca6SFrom: Thiemo Nagel 		printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu "
25410f2ddca6SFrom: Thiemo Nagel 		       "exceeds size of device (%llu blocks)\n",
25420f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
25430f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
25440f2ddca6SFrom: Thiemo Nagel 	}
25450f2ddca6SFrom: Thiemo Nagel 
25464ec11028STheodore Ts'o         /*
25474ec11028STheodore Ts'o          * It makes no sense for the first data block to be beyond the end
25484ec11028STheodore Ts'o          * of the filesystem.
25494ec11028STheodore Ts'o          */
25504ec11028STheodore Ts'o         if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
25514ec11028STheodore Ts'o                 printk(KERN_WARNING "EXT4-fs: bad geometry: first data"
25524ec11028STheodore Ts'o 		       "block %u is beyond end of filesystem (%llu)\n",
2553e7c95593SEric Sandeen 		       le32_to_cpu(es->s_first_data_block),
25544ec11028STheodore Ts'o 		       ext4_blocks_count(es));
2555e7c95593SEric Sandeen 		goto failed_mount;
2556e7c95593SEric Sandeen 	}
2557bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2558bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2559bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2560bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
25614ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
25624ec11028STheodore Ts'o 		printk(KERN_WARNING "EXT4-fs: groups count too large: %u "
25634ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
25644ec11028STheodore Ts'o 		       "blocks per group %lu)\n", sbi->s_groups_count,
25654ec11028STheodore Ts'o 		       ext4_blocks_count(es),
25664ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
25674ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
25684ec11028STheodore Ts'o 		goto failed_mount;
25694ec11028STheodore Ts'o 	}
2570bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2571617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2572617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2573ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2574ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2575ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2576617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: not enough memory\n");
2577ac27a0ecSDave Kleikamp 		goto failed_mount;
2578ac27a0ecSDave Kleikamp 	}
2579ac27a0ecSDave Kleikamp 
25803244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
25819f6200bbSTheodore Ts'o 	if (ext4_proc_root)
25829f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
25833244fcb1SAlexander Beregalov #endif
2584240799cdSTheodore Ts'o 
2585705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
2586ac27a0ecSDave Kleikamp 
2587ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
258870bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2589ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2590ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2591617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: "
2592ac27a0ecSDave Kleikamp 			       "can't read group descriptor %d\n", i);
2593ac27a0ecSDave Kleikamp 			db_count = i;
2594ac27a0ecSDave Kleikamp 			goto failed_mount2;
2595ac27a0ecSDave Kleikamp 		}
2596ac27a0ecSDave Kleikamp 	}
2597617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2598617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2599ac27a0ecSDave Kleikamp 		goto failed_mount2;
2600ac27a0ecSDave Kleikamp 	}
2601772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2602772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2603772cb7c8SJose R. Santos 			printk(KERN_ERR
2604772cb7c8SJose R. Santos 			       "EXT4-fs: unable to initialize "
2605772cb7c8SJose R. Santos 			       "flex_bg meta info!\n");
2606772cb7c8SJose R. Santos 			goto failed_mount2;
2607772cb7c8SJose R. Santos 		}
2608772cb7c8SJose R. Santos 
2609ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2610ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2611ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2612ac27a0ecSDave Kleikamp 
2613833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2614617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2615833f4077SPeter Zijlstra 	if (!err) {
2616833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2617617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2618833f4077SPeter Zijlstra 	}
2619833f4077SPeter Zijlstra 	if (!err) {
2620833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2621617ba13bSMingming Cao 				ext4_count_dirs(sb));
2622833f4077SPeter Zijlstra 	}
26236bc6e63fSAneesh Kumar K.V 	if (!err) {
26246bc6e63fSAneesh Kumar K.V 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
26256bc6e63fSAneesh Kumar K.V 	}
2626833f4077SPeter Zijlstra 	if (err) {
2627833f4077SPeter Zijlstra 		printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2628833f4077SPeter Zijlstra 		goto failed_mount3;
2629833f4077SPeter Zijlstra 	}
2630ac27a0ecSDave Kleikamp 
2631c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
2632c9de560dSAlex Tomas 
2633ac27a0ecSDave Kleikamp 	/*
2634ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2635ac27a0ecSDave Kleikamp 	 */
26369ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
26379ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2638617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
26399ca92389STheodore Ts'o 	else
26409ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
2641617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2642617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2643ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2644617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2645617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2646ac27a0ecSDave Kleikamp #endif
2647ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2648ac27a0ecSDave Kleikamp 
2649ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2650ac27a0ecSDave Kleikamp 
2651ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2652617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2653617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2654ac27a0ecSDave Kleikamp 
2655ac27a0ecSDave Kleikamp 	/*
2656ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2657ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2658ac27a0ecSDave Kleikamp 	 */
2659ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2660617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2661617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2662ac27a0ecSDave Kleikamp 			goto failed_mount3;
2663624080edSTheodore Ts'o 		if (!(sb->s_flags & MS_RDONLY) &&
2664624080edSTheodore Ts'o 		    EXT4_SB(sb)->s_journal->j_failed_commit) {
2665624080edSTheodore Ts'o 			printk(KERN_CRIT "EXT4-fs error (device %s): "
2666624080edSTheodore Ts'o 			       "ext4_fill_super: Journal transaction "
2667624080edSTheodore Ts'o 			       "%u is corrupt\n", sb->s_id,
2668624080edSTheodore Ts'o 			       EXT4_SB(sb)->s_journal->j_failed_commit);
2669624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_RO)) {
2670624080edSTheodore Ts'o 				printk(KERN_CRIT
2671624080edSTheodore Ts'o 				       "Mounting filesystem read-only\n");
2672624080edSTheodore Ts'o 				sb->s_flags |= MS_RDONLY;
2673624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2674624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2675624080edSTheodore Ts'o 			}
2676624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_PANIC)) {
2677624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2678624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2679e2d67052STheodore Ts'o 				ext4_commit_super(sb, 1);
2680624080edSTheodore Ts'o 				goto failed_mount4;
2681624080edSTheodore Ts'o 			}
2682624080edSTheodore Ts'o 		}
26830390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
26840390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
26850390131bSFrank Mayhar 		printk(KERN_ERR "EXT4-fs: required journal recovery "
26860390131bSFrank Mayhar 		       "suppressed and not mounted read-only\n");
26870390131bSFrank Mayhar 		goto failed_mount4;
2688ac27a0ecSDave Kleikamp 	} else {
26890390131bSFrank Mayhar 		clear_opt(sbi->s_mount_opt, DATA_FLAGS);
26900390131bSFrank Mayhar 		set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
26910390131bSFrank Mayhar 		sbi->s_journal = NULL;
26920390131bSFrank Mayhar 		needs_recovery = 0;
26930390131bSFrank Mayhar 		goto no_journal;
2694ac27a0ecSDave Kleikamp 	}
2695ac27a0ecSDave Kleikamp 
2696eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2697eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2698eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2699abda1418STheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Failed to set 64-bit journal feature\n");
2700eb40a09cSJose R. Santos 		goto failed_mount4;
2701eb40a09cSJose R. Santos 	}
2702eb40a09cSJose R. Santos 
2703818d276cSGirish Shilamkar 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2704818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2705818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2706818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2707818d276cSGirish Shilamkar 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2708818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2709818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2710818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2711818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2712818d276cSGirish Shilamkar 	} else {
2713818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal,
2714818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2715818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2716818d276cSGirish Shilamkar 	}
2717818d276cSGirish Shilamkar 
2718ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2719ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2720ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2721ac27a0ecSDave Kleikamp 	case 0:
2722ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
272363f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
272463f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
272563f57933SAndrew Morton 		 */
2726dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2727dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2728ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2729ac27a0ecSDave Kleikamp 		else
2730ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2731ac27a0ecSDave Kleikamp 		break;
2732ac27a0ecSDave Kleikamp 
2733617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2734617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2735dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2736dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2737617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: Journal does not support "
2738ac27a0ecSDave Kleikamp 			       "requested data journaling mode\n");
2739ac27a0ecSDave Kleikamp 			goto failed_mount4;
2740ac27a0ecSDave Kleikamp 		}
2741ac27a0ecSDave Kleikamp 	default:
2742ac27a0ecSDave Kleikamp 		break;
2743ac27a0ecSDave Kleikamp 	}
2744b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
2745ac27a0ecSDave Kleikamp 
27460390131bSFrank Mayhar no_journal:
2747ac27a0ecSDave Kleikamp 
2748ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2749617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2750617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2751ac27a0ecSDave Kleikamp 				"its supported only with writeback mode\n");
2752ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2753ac27a0ecSDave Kleikamp 		}
2754ac27a0ecSDave Kleikamp 	}
2755ac27a0ecSDave Kleikamp 	/*
2756dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2757ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2758ac27a0ecSDave Kleikamp 	 */
2759ac27a0ecSDave Kleikamp 
27601d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
27611d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2762617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: get root inode failed\n");
27631d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2764ac27a0ecSDave Kleikamp 		goto failed_mount4;
2765ac27a0ecSDave Kleikamp 	}
2766ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
27671d1fe1eeSDavid Howells 		iput(root);
2768617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2769ac27a0ecSDave Kleikamp 		goto failed_mount4;
2770ac27a0ecSDave Kleikamp 	}
27711d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
27721d1fe1eeSDavid Howells 	if (!sb->s_root) {
27731d1fe1eeSDavid Howells 		printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
27741d1fe1eeSDavid Howells 		iput(root);
27751d1fe1eeSDavid Howells 		ret = -ENOMEM;
27761d1fe1eeSDavid Howells 		goto failed_mount4;
27771d1fe1eeSDavid Howells 	}
2778ac27a0ecSDave Kleikamp 
2779617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2780ef7f3835SKalpak Shah 
2781ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2782ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2783ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2784ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2785ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2786ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2787ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2788ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2789ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2790ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2791ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2792ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2793ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2794ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2795ef7f3835SKalpak Shah 		}
2796ef7f3835SKalpak Shah 	}
2797ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2798ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2799ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2800ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2801ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2802ef7f3835SKalpak Shah 		printk(KERN_INFO "EXT4-fs: required extra inode space not"
2803ef7f3835SKalpak Shah 			"available.\n");
2804ef7f3835SKalpak Shah 	}
2805ef7f3835SKalpak Shah 
2806c2774d84SAneesh Kumar K.V 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2807c2774d84SAneesh Kumar K.V 		printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2808c2774d84SAneesh Kumar K.V 				"requested data journaling mode\n");
2809c2774d84SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
2810c2774d84SAneesh Kumar K.V 	} else if (test_opt(sb, DELALLOC))
2811c2774d84SAneesh Kumar K.V 		printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2812c2774d84SAneesh Kumar K.V 
2813c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
2814c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
2815c2774d84SAneesh Kumar K.V 	if (err) {
2816c2774d84SAneesh Kumar K.V 		printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2817c2774d84SAneesh Kumar K.V 		       err);
2818c2774d84SAneesh Kumar K.V 		goto failed_mount4;
2819c2774d84SAneesh Kumar K.V 	}
2820c2774d84SAneesh Kumar K.V 
28213197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
28223197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
28233197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
28243197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
28253197ebdbSTheodore Ts'o 	if (err) {
28263197ebdbSTheodore Ts'o 		ext4_mb_release(sb);
28273197ebdbSTheodore Ts'o 		ext4_ext_release(sb);
28283197ebdbSTheodore Ts'o 		goto failed_mount4;
28293197ebdbSTheodore Ts'o 	};
28303197ebdbSTheodore Ts'o 
2831617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2832617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2833617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
28340390131bSFrank Mayhar 	if (needs_recovery) {
2835617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2836617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
28370390131bSFrank Mayhar 	}
28380390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
28390390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
28400390131bSFrank Mayhar 			descr = " journalled data mode";
28410390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
28420390131bSFrank Mayhar 			descr = " ordered data mode";
28430390131bSFrank Mayhar 		else
28440390131bSFrank Mayhar 			descr = " writeback data mode";
28450390131bSFrank Mayhar 	} else
28460390131bSFrank Mayhar 		descr = "out journal";
28470390131bSFrank Mayhar 
28480390131bSFrank Mayhar 	printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
28490390131bSFrank Mayhar 	       sb->s_id, descr);
2850ac27a0ecSDave Kleikamp 
2851ac27a0ecSDave Kleikamp 	lock_kernel();
2852ac27a0ecSDave Kleikamp 	return 0;
2853ac27a0ecSDave Kleikamp 
2854617ba13bSMingming Cao cantfind_ext4:
2855ac27a0ecSDave Kleikamp 	if (!silent)
2856617ba13bSMingming Cao 		printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2857ac27a0ecSDave Kleikamp 		       sb->s_id);
2858ac27a0ecSDave Kleikamp 	goto failed_mount;
2859ac27a0ecSDave Kleikamp 
2860ac27a0ecSDave Kleikamp failed_mount4:
28610390131bSFrank Mayhar 	printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
28620390131bSFrank Mayhar 	if (sbi->s_journal) {
2863dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
286447b4a50bSJan Kara 		sbi->s_journal = NULL;
28650390131bSFrank Mayhar 	}
2866ac27a0ecSDave Kleikamp failed_mount3:
2867c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups) {
2868c5ca7c76STheodore Ts'o 		if (is_vmalloc_addr(sbi->s_flex_groups))
2869c5ca7c76STheodore Ts'o 			vfree(sbi->s_flex_groups);
2870c5ca7c76STheodore Ts'o 		else
2871c5ca7c76STheodore Ts'o 			kfree(sbi->s_flex_groups);
2872c5ca7c76STheodore Ts'o 	}
2873ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2874ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2875ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
28766bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2877ac27a0ecSDave Kleikamp failed_mount2:
2878ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2879ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2880ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2881ac27a0ecSDave Kleikamp failed_mount:
2882240799cdSTheodore Ts'o 	if (sbi->s_proc) {
28839f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
2884240799cdSTheodore Ts'o 	}
2885ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2886ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2887ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2888ac27a0ecSDave Kleikamp #endif
2889617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2890ac27a0ecSDave Kleikamp 	brelse(bh);
2891ac27a0ecSDave Kleikamp out_fail:
2892ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2893ac27a0ecSDave Kleikamp 	kfree(sbi);
2894ac27a0ecSDave Kleikamp 	lock_kernel();
28951d1fe1eeSDavid Howells 	return ret;
2896ac27a0ecSDave Kleikamp }
2897ac27a0ecSDave Kleikamp 
2898ac27a0ecSDave Kleikamp /*
2899ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2900ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2901ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2902ac27a0ecSDave Kleikamp  */
2903617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2904ac27a0ecSDave Kleikamp {
2905617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2906ac27a0ecSDave Kleikamp 
2907ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
290830773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
290930773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
2910ac27a0ecSDave Kleikamp 
2911ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2912ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2913dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2914ac27a0ecSDave Kleikamp 	else
2915dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
29165bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
29175bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
29185bf5683aSHidehiro Kawai 	else
29195bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2920ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2921ac27a0ecSDave Kleikamp }
2922ac27a0ecSDave Kleikamp 
2923617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2924ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2925ac27a0ecSDave Kleikamp {
2926ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2927ac27a0ecSDave Kleikamp 	journal_t *journal;
2928ac27a0ecSDave Kleikamp 
29290390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
29300390131bSFrank Mayhar 
2931ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2932ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2933ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2934ac27a0ecSDave Kleikamp 
29351d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
29361d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
2937617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: no journal found.\n");
2938ac27a0ecSDave Kleikamp 		return NULL;
2939ac27a0ecSDave Kleikamp 	}
2940ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2941ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2942ac27a0ecSDave Kleikamp 		iput(journal_inode);
2943617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2944ac27a0ecSDave Kleikamp 		return NULL;
2945ac27a0ecSDave Kleikamp 	}
2946ac27a0ecSDave Kleikamp 
2947e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2948ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
29491d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
2950617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2951ac27a0ecSDave Kleikamp 		iput(journal_inode);
2952ac27a0ecSDave Kleikamp 		return NULL;
2953ac27a0ecSDave Kleikamp 	}
2954ac27a0ecSDave Kleikamp 
2955dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
2956ac27a0ecSDave Kleikamp 	if (!journal) {
2957617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2958ac27a0ecSDave Kleikamp 		iput(journal_inode);
2959ac27a0ecSDave Kleikamp 		return NULL;
2960ac27a0ecSDave Kleikamp 	}
2961ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2962617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2963ac27a0ecSDave Kleikamp 	return journal;
2964ac27a0ecSDave Kleikamp }
2965ac27a0ecSDave Kleikamp 
2966617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
2967ac27a0ecSDave Kleikamp 				       dev_t j_dev)
2968ac27a0ecSDave Kleikamp {
2969ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2970ac27a0ecSDave Kleikamp 	journal_t *journal;
2971617ba13bSMingming Cao 	ext4_fsblk_t start;
2972617ba13bSMingming Cao 	ext4_fsblk_t len;
2973ac27a0ecSDave Kleikamp 	int hblock, blocksize;
2974617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
2975ac27a0ecSDave Kleikamp 	unsigned long offset;
2976617ba13bSMingming Cao 	struct ext4_super_block *es;
2977ac27a0ecSDave Kleikamp 	struct block_device *bdev;
2978ac27a0ecSDave Kleikamp 
29790390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
29800390131bSFrank Mayhar 
2981617ba13bSMingming Cao 	bdev = ext4_blkdev_get(j_dev);
2982ac27a0ecSDave Kleikamp 	if (bdev == NULL)
2983ac27a0ecSDave Kleikamp 		return NULL;
2984ac27a0ecSDave Kleikamp 
2985ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
2986ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2987abda1418STheodore Ts'o 			"EXT4-fs: failed to claim external journal device.\n");
29889a1c3542SAl Viro 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2989ac27a0ecSDave Kleikamp 		return NULL;
2990ac27a0ecSDave Kleikamp 	}
2991ac27a0ecSDave Kleikamp 
2992ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
2993ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(bdev);
2994ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
2995ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2996617ba13bSMingming Cao 			"EXT4-fs: blocksize too small for journal device.\n");
2997ac27a0ecSDave Kleikamp 		goto out_bdev;
2998ac27a0ecSDave Kleikamp 	}
2999ac27a0ecSDave Kleikamp 
3000617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3001617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3002ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3003ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3004617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
3005ac27a0ecSDave Kleikamp 		       "external journal\n");
3006ac27a0ecSDave Kleikamp 		goto out_bdev;
3007ac27a0ecSDave Kleikamp 	}
3008ac27a0ecSDave Kleikamp 
3009617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3010617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3011ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3012617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3013617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: external journal has "
3014ac27a0ecSDave Kleikamp 					"bad superblock\n");
3015ac27a0ecSDave Kleikamp 		brelse(bh);
3016ac27a0ecSDave Kleikamp 		goto out_bdev;
3017ac27a0ecSDave Kleikamp 	}
3018ac27a0ecSDave Kleikamp 
3019617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3020617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
3021ac27a0ecSDave Kleikamp 		brelse(bh);
3022ac27a0ecSDave Kleikamp 		goto out_bdev;
3023ac27a0ecSDave Kleikamp 	}
3024ac27a0ecSDave Kleikamp 
3025bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
3026ac27a0ecSDave Kleikamp 	start = sb_block + 1;
3027ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
3028ac27a0ecSDave Kleikamp 
3029dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3030ac27a0ecSDave Kleikamp 					start, len, blocksize);
3031ac27a0ecSDave Kleikamp 	if (!journal) {
3032617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
3033ac27a0ecSDave Kleikamp 		goto out_bdev;
3034ac27a0ecSDave Kleikamp 	}
3035ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3036ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
3037ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
3038ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
3039617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
3040ac27a0ecSDave Kleikamp 		goto out_journal;
3041ac27a0ecSDave Kleikamp 	}
3042ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3043617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: External journal has more than one "
3044ac27a0ecSDave Kleikamp 					"user (unsupported) - %d\n",
3045ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
3046ac27a0ecSDave Kleikamp 		goto out_journal;
3047ac27a0ecSDave Kleikamp 	}
3048617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
3049617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3050ac27a0ecSDave Kleikamp 	return journal;
3051ac27a0ecSDave Kleikamp out_journal:
3052dab291afSMingming Cao 	jbd2_journal_destroy(journal);
3053ac27a0ecSDave Kleikamp out_bdev:
3054617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
3055ac27a0ecSDave Kleikamp 	return NULL;
3056ac27a0ecSDave Kleikamp }
3057ac27a0ecSDave Kleikamp 
3058617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
3059617ba13bSMingming Cao 			     struct ext4_super_block *es,
3060ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
3061ac27a0ecSDave Kleikamp {
3062ac27a0ecSDave Kleikamp 	journal_t *journal;
3063ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3064ac27a0ecSDave Kleikamp 	dev_t journal_dev;
3065ac27a0ecSDave Kleikamp 	int err = 0;
3066ac27a0ecSDave Kleikamp 	int really_read_only;
3067ac27a0ecSDave Kleikamp 
30680390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
30690390131bSFrank Mayhar 
3070ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3071ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3072617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: external journal device major/minor "
3073ac27a0ecSDave Kleikamp 			"numbers have changed\n");
3074ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
3075ac27a0ecSDave Kleikamp 	} else
3076ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3077ac27a0ecSDave Kleikamp 
3078ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
3079ac27a0ecSDave Kleikamp 
3080ac27a0ecSDave Kleikamp 	/*
3081ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
3082ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
3083ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
3084ac27a0ecSDave Kleikamp 	 */
3085ac27a0ecSDave Kleikamp 
3086617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3087ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
3088617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: INFO: recovery "
3089ac27a0ecSDave Kleikamp 					"required on readonly filesystem.\n");
3090ac27a0ecSDave Kleikamp 			if (really_read_only) {
3091617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: write access "
3092ac27a0ecSDave Kleikamp 					"unavailable, cannot proceed.\n");
3093ac27a0ecSDave Kleikamp 				return -EROFS;
3094ac27a0ecSDave Kleikamp 			}
3095617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: write access will "
3096ac27a0ecSDave Kleikamp 			       "be enabled during recovery.\n");
3097ac27a0ecSDave Kleikamp 		}
3098ac27a0ecSDave Kleikamp 	}
3099ac27a0ecSDave Kleikamp 
3100ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
3101617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem has both journal "
3102ac27a0ecSDave Kleikamp 		       "and inode journals!\n");
3103ac27a0ecSDave Kleikamp 		return -EINVAL;
3104ac27a0ecSDave Kleikamp 	}
3105ac27a0ecSDave Kleikamp 
3106ac27a0ecSDave Kleikamp 	if (journal_inum) {
3107617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
3108ac27a0ecSDave Kleikamp 			return -EINVAL;
3109ac27a0ecSDave Kleikamp 	} else {
3110617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3111ac27a0ecSDave Kleikamp 			return -EINVAL;
3112ac27a0ecSDave Kleikamp 	}
3113ac27a0ecSDave Kleikamp 
31144776004fSTheodore Ts'o 	if (journal->j_flags & JBD2_BARRIER)
31154776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers enabled\n");
31164776004fSTheodore Ts'o 	else
31174776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers disabled\n");
31184776004fSTheodore Ts'o 
3119ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3120dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
3121ac27a0ecSDave Kleikamp 		if (err)  {
3122617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: error updating journal.\n");
3123dab291afSMingming Cao 			jbd2_journal_destroy(journal);
3124ac27a0ecSDave Kleikamp 			return err;
3125ac27a0ecSDave Kleikamp 		}
3126ac27a0ecSDave Kleikamp 	}
3127ac27a0ecSDave Kleikamp 
3128617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3129dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
3130ac27a0ecSDave Kleikamp 	if (!err)
3131dab291afSMingming Cao 		err = jbd2_journal_load(journal);
3132ac27a0ecSDave Kleikamp 
3133ac27a0ecSDave Kleikamp 	if (err) {
3134617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error loading journal.\n");
3135dab291afSMingming Cao 		jbd2_journal_destroy(journal);
3136ac27a0ecSDave Kleikamp 		return err;
3137ac27a0ecSDave Kleikamp 	}
3138ac27a0ecSDave Kleikamp 
3139617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
3140617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
3141ac27a0ecSDave Kleikamp 
3142ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3143ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3144ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
3145ac27a0ecSDave Kleikamp 
3146ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
3147e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3148ac27a0ecSDave Kleikamp 	}
3149ac27a0ecSDave Kleikamp 
3150ac27a0ecSDave Kleikamp 	return 0;
3151ac27a0ecSDave Kleikamp }
3152ac27a0ecSDave Kleikamp 
3153e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
3154ac27a0ecSDave Kleikamp {
3155e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3156617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3157c4be0c1dSTakashi Sato 	int error = 0;
3158ac27a0ecSDave Kleikamp 
3159ac27a0ecSDave Kleikamp 	if (!sbh)
3160c4be0c1dSTakashi Sato 		return error;
3161914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
3162914258bfSTheodore Ts'o 		/*
3163914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
3164914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
3165914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
3166914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
3167914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
3168914258bfSTheodore Ts'o 		 * write and hope for the best.
3169914258bfSTheodore Ts'o 		 */
3170abda1418STheodore Ts'o 		printk(KERN_ERR "EXT4-fs: previous I/O error to "
3171914258bfSTheodore Ts'o 		       "superblock detected for %s.\n", sb->s_id);
3172914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
3173914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
3174914258bfSTheodore Ts'o 	}
3175ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
3176afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
3177afc32f7eSTheodore Ts'o 		cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3178afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3179afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
31805d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
31815d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeblocks_counter));
31825d1b1b3fSAneesh Kumar K.V 	es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
31835d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeinodes_counter));
31847234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
3185ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
3186ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
3187914258bfSTheodore Ts'o 	if (sync) {
3188c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
3189c4be0c1dSTakashi Sato 		if (error)
3190c4be0c1dSTakashi Sato 			return error;
3191c4be0c1dSTakashi Sato 
3192c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
3193c4be0c1dSTakashi Sato 		if (error) {
3194abda1418STheodore Ts'o 			printk(KERN_ERR "EXT4-fs: I/O error while writing "
3195914258bfSTheodore Ts'o 			       "superblock for %s.\n", sb->s_id);
3196914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
3197914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
3198914258bfSTheodore Ts'o 		}
3199914258bfSTheodore Ts'o 	}
3200c4be0c1dSTakashi Sato 	return error;
3201ac27a0ecSDave Kleikamp }
3202ac27a0ecSDave Kleikamp 
3203ac27a0ecSDave Kleikamp 
3204ac27a0ecSDave Kleikamp /*
3205ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
3206ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
3207ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
3208ac27a0ecSDave Kleikamp  */
3209617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
3210617ba13bSMingming Cao 					struct ext4_super_block *es)
3211ac27a0ecSDave Kleikamp {
3212617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
3213ac27a0ecSDave Kleikamp 
32140390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
32150390131bSFrank Mayhar 		BUG_ON(journal != NULL);
32160390131bSFrank Mayhar 		return;
32170390131bSFrank Mayhar 	}
3218dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
32197ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
32207ffe1ea8SHidehiro Kawai 		goto out;
32217ffe1ea8SHidehiro Kawai 
3222617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3223ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
3224617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3225e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3226ac27a0ecSDave Kleikamp 	}
32277ffe1ea8SHidehiro Kawai 
32287ffe1ea8SHidehiro Kawai out:
3229dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
3230ac27a0ecSDave Kleikamp }
3231ac27a0ecSDave Kleikamp 
3232ac27a0ecSDave Kleikamp /*
3233ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
3234ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
3235ac27a0ecSDave Kleikamp  * main filesystem now.
3236ac27a0ecSDave Kleikamp  */
3237617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
3238617ba13bSMingming Cao 				   struct ext4_super_block *es)
3239ac27a0ecSDave Kleikamp {
3240ac27a0ecSDave Kleikamp 	journal_t *journal;
3241ac27a0ecSDave Kleikamp 	int j_errno;
3242ac27a0ecSDave Kleikamp 	const char *errstr;
3243ac27a0ecSDave Kleikamp 
32440390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
32450390131bSFrank Mayhar 
3246617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
3247ac27a0ecSDave Kleikamp 
3248ac27a0ecSDave Kleikamp 	/*
3249ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
3250617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
3251ac27a0ecSDave Kleikamp 	 */
3252ac27a0ecSDave Kleikamp 
3253dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
3254ac27a0ecSDave Kleikamp 	if (j_errno) {
3255ac27a0ecSDave Kleikamp 		char nbuf[16];
3256ac27a0ecSDave Kleikamp 
3257617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
325846e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Filesystem error recorded "
3259ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
326046e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Marking fs in need of "
3261ac27a0ecSDave Kleikamp 			     "filesystem check.");
3262ac27a0ecSDave Kleikamp 
3263617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3264617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3265e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3266ac27a0ecSDave Kleikamp 
3267dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
3268ac27a0ecSDave Kleikamp 	}
3269ac27a0ecSDave Kleikamp }
3270ac27a0ecSDave Kleikamp 
3271ac27a0ecSDave Kleikamp /*
3272ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
3273ac27a0ecSDave Kleikamp  * and wait on the commit.
3274ac27a0ecSDave Kleikamp  */
3275617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
3276ac27a0ecSDave Kleikamp {
3277ac27a0ecSDave Kleikamp 	journal_t *journal;
32780390131bSFrank Mayhar 	int ret = 0;
3279ac27a0ecSDave Kleikamp 
3280ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
3281ac27a0ecSDave Kleikamp 		return 0;
3282ac27a0ecSDave Kleikamp 
3283617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
32847234ab2aSTheodore Ts'o 	if (journal)
3285617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
32860390131bSFrank Mayhar 
3287ac27a0ecSDave Kleikamp 	return ret;
3288ac27a0ecSDave Kleikamp }
3289ac27a0ecSDave Kleikamp 
3290617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
3291ac27a0ecSDave Kleikamp {
3292e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
32930390131bSFrank Mayhar }
3294ac27a0ecSDave Kleikamp 
3295617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
3296ac27a0ecSDave Kleikamp {
329714ce0cb4STheodore Ts'o 	int ret = 0;
32989eddacf9SJan Kara 	tid_t target;
3299ac27a0ecSDave Kleikamp 
3300ede86cc4STheodore Ts'o 	trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
33019ca92389STheodore Ts'o 	if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
3302ac27a0ecSDave Kleikamp 		if (wait)
33039ca92389STheodore Ts'o 			jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
33040390131bSFrank Mayhar 	}
330514ce0cb4STheodore Ts'o 	return ret;
3306ac27a0ecSDave Kleikamp }
3307ac27a0ecSDave Kleikamp 
3308ac27a0ecSDave Kleikamp /*
3309ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
3310ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
3311ac27a0ecSDave Kleikamp  */
3312c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
3313ac27a0ecSDave Kleikamp {
3314c4be0c1dSTakashi Sato 	int error = 0;
3315c4be0c1dSTakashi Sato 	journal_t *journal;
3316ac27a0ecSDave Kleikamp 
33179ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
33189ca92389STheodore Ts'o 		return 0;
33199ca92389STheodore Ts'o 
3320c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
3321ac27a0ecSDave Kleikamp 
3322ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
3323dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
33247ffe1ea8SHidehiro Kawai 
33257ffe1ea8SHidehiro Kawai 	/*
33269ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
33279ca92389STheodore Ts'o 	 * the journal.
33287ffe1ea8SHidehiro Kawai 	 */
3329c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
33309ca92389STheodore Ts'o 	if (error < 0) {
33319ca92389STheodore Ts'o 	out:
33329ca92389STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
33339ca92389STheodore Ts'o 		return error;
33340390131bSFrank Mayhar 	}
3335ac27a0ecSDave Kleikamp 
3336ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
3337617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3338e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
3339c4be0c1dSTakashi Sato 	if (error)
3340c4be0c1dSTakashi Sato 		goto out;
3341c4be0c1dSTakashi Sato 	return 0;
3342ac27a0ecSDave Kleikamp }
3343ac27a0ecSDave Kleikamp 
3344ac27a0ecSDave Kleikamp /*
3345ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
3346ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
3347ac27a0ecSDave Kleikamp  */
3348c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
3349ac27a0ecSDave Kleikamp {
33509ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
33519ca92389STheodore Ts'o 		return 0;
33529ca92389STheodore Ts'o 
3353ac27a0ecSDave Kleikamp 	lock_super(sb);
33549ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
3355617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3356e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3357ac27a0ecSDave Kleikamp 	unlock_super(sb);
3358dab291afSMingming Cao 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3359c4be0c1dSTakashi Sato 	return 0;
3360ac27a0ecSDave Kleikamp }
3361ac27a0ecSDave Kleikamp 
3362617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
3363ac27a0ecSDave Kleikamp {
3364617ba13bSMingming Cao 	struct ext4_super_block *es;
3365617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3366617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
3367ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
3368617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
33698a266467STheodore Ts'o 	ext4_group_t g;
3370b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3371ac27a0ecSDave Kleikamp 	int err;
3372ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3373ac27a0ecSDave Kleikamp 	int i;
3374ac27a0ecSDave Kleikamp #endif
3375ac27a0ecSDave Kleikamp 
3376ac27a0ecSDave Kleikamp 	/* Store the original options */
3377ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
3378ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
3379ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
3380ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
3381ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
338230773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
338330773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
3384ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3385ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3386ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3387ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3388ac27a0ecSDave Kleikamp #endif
3389b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3390b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
3391ac27a0ecSDave Kleikamp 
3392ac27a0ecSDave Kleikamp 	/*
3393ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
3394ac27a0ecSDave Kleikamp 	 */
3395b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
3396b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
3397ac27a0ecSDave Kleikamp 		err = -EINVAL;
3398ac27a0ecSDave Kleikamp 		goto restore_opts;
3399ac27a0ecSDave Kleikamp 	}
3400ac27a0ecSDave Kleikamp 
3401617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
340246e665e9SHarvey Harrison 		ext4_abort(sb, __func__, "Abort forced by user");
3403ac27a0ecSDave Kleikamp 
3404ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3405617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3406ac27a0ecSDave Kleikamp 
3407ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3408ac27a0ecSDave Kleikamp 
3409b3881f74STheodore Ts'o 	if (sbi->s_journal) {
3410617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
3411b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3412b3881f74STheodore Ts'o 	}
3413ac27a0ecSDave Kleikamp 
3414ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3415bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
3416617ba13bSMingming Cao 		if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3417ac27a0ecSDave Kleikamp 			err = -EROFS;
3418ac27a0ecSDave Kleikamp 			goto restore_opts;
3419ac27a0ecSDave Kleikamp 		}
3420ac27a0ecSDave Kleikamp 
3421ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
3422ac27a0ecSDave Kleikamp 			/*
3423ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3424ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3425ac27a0ecSDave Kleikamp 			 */
3426ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3427ac27a0ecSDave Kleikamp 
3428ac27a0ecSDave Kleikamp 			/*
3429ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3430ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3431ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3432ac27a0ecSDave Kleikamp 			 */
3433617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3434617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3435ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3436ac27a0ecSDave Kleikamp 
3437*a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
3438617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
3439ac27a0ecSDave Kleikamp 		} else {
34403a06d778SAneesh Kumar K.V 			int ret;
3441617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3442617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3443617ba13bSMingming Cao 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3444ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
34453a06d778SAneesh Kumar K.V 				       "optional features (%x).\n", sb->s_id,
34463a06d778SAneesh Kumar K.V 				(le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
34473a06d778SAneesh Kumar K.V 					~EXT4_FEATURE_RO_COMPAT_SUPP));
3448ac27a0ecSDave Kleikamp 				err = -EROFS;
3449ac27a0ecSDave Kleikamp 				goto restore_opts;
3450ac27a0ecSDave Kleikamp 			}
3451ead6596bSEric Sandeen 
3452ead6596bSEric Sandeen 			/*
34538a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
34548a266467STheodore Ts'o 			 * are sane.  If they aren't, refuse to
34558a266467STheodore Ts'o 			 * remount r/w.
34568a266467STheodore Ts'o 			 */
34578a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
34588a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
34598a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
34608a266467STheodore Ts'o 
34618a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
34628a266467STheodore Ts'o 					printk(KERN_ERR
34638a266467STheodore Ts'o 	       "EXT4-fs: ext4_remount: "
3464a9df9a49STheodore Ts'o 		"Checksum for group %u failed (%u!=%u)\n",
34658a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
34668a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
34678a266467STheodore Ts'o 					err = -EINVAL;
34688a266467STheodore Ts'o 					goto restore_opts;
34698a266467STheodore Ts'o 				}
34708a266467STheodore Ts'o 			}
34718a266467STheodore Ts'o 
34728a266467STheodore Ts'o 			/*
3473ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3474ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3475ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3476ead6596bSEric Sandeen 			 */
3477ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3478ead6596bSEric Sandeen 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3479ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3480ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3481ead6596bSEric Sandeen 				       "umount/remount instead.\n",
3482ead6596bSEric Sandeen 				       sb->s_id);
3483ead6596bSEric Sandeen 				err = -EINVAL;
3484ead6596bSEric Sandeen 				goto restore_opts;
3485ead6596bSEric Sandeen 			}
3486ead6596bSEric Sandeen 
3487ac27a0ecSDave Kleikamp 			/*
3488ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3489ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3490ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3491ac27a0ecSDave Kleikamp 			 * the partition.)
3492ac27a0ecSDave Kleikamp 			 */
34930390131bSFrank Mayhar 			if (sbi->s_journal)
3494617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
3495ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3496617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3497ac27a0ecSDave Kleikamp 				goto restore_opts;
3498617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3499ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3500ac27a0ecSDave Kleikamp 		}
3501ac27a0ecSDave Kleikamp 	}
35020390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
3503e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
35040390131bSFrank Mayhar 
3505ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3506ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3507ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3508ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3509ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3510ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3511ac27a0ecSDave Kleikamp #endif
3512ac27a0ecSDave Kleikamp 	return 0;
3513ac27a0ecSDave Kleikamp restore_opts:
3514ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3515ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3516ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3517ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3518ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
351930773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
352030773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
3521ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3522ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3523ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3524ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3525ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3526ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3527ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3528ac27a0ecSDave Kleikamp 	}
3529ac27a0ecSDave Kleikamp #endif
3530ac27a0ecSDave Kleikamp 	return err;
3531ac27a0ecSDave Kleikamp }
3532ac27a0ecSDave Kleikamp 
3533617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3534ac27a0ecSDave Kleikamp {
3535ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3536617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3537617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3538960cc398SPekka Enberg 	u64 fsid;
3539ac27a0ecSDave Kleikamp 
35405e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
35415e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
35426bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
35438df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
35445e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3545ac27a0ecSDave Kleikamp 
3546ac27a0ecSDave Kleikamp 		/*
35475e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
35485e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
35495e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3550ac27a0ecSDave Kleikamp 		 */
3551ac27a0ecSDave Kleikamp 
3552ac27a0ecSDave Kleikamp 		/*
3553ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3554ac27a0ecSDave Kleikamp 		 * overhead
3555ac27a0ecSDave Kleikamp 		 */
3556ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3557ac27a0ecSDave Kleikamp 
3558ac27a0ecSDave Kleikamp 		/*
3559ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3560ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3561ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3562ac27a0ecSDave Kleikamp 		 */
3563ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3564617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3565617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3566ac27a0ecSDave Kleikamp 			cond_resched();
3567ac27a0ecSDave Kleikamp 		}
3568ac27a0ecSDave Kleikamp 
3569ac27a0ecSDave Kleikamp 		/*
3570ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3571ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3572ac27a0ecSDave Kleikamp 		 */
35735e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
35745e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
35755e70030dSBadari Pulavarty 		smp_wmb();
35766bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3577ac27a0ecSDave Kleikamp 	}
3578ac27a0ecSDave Kleikamp 
3579617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3580ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
35815e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
35826bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
35836bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3584308ba3ecSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, buf->f_bfree);
3585bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3586bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3587ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3588ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
358952d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
35905e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3591617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3592960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3593960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3594960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3595960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3596ac27a0ecSDave Kleikamp 	return 0;
3597ac27a0ecSDave Kleikamp }
3598ac27a0ecSDave Kleikamp 
3599ac27a0ecSDave Kleikamp /* Helper function for writing quotas on sync - we need to start transaction before quota file
3600ac27a0ecSDave Kleikamp  * is locked for write. Otherwise the are possible deadlocks:
3601ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3602617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3603dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
3604a269eb18SJan Kara  *   vfs_dq_init()                         down(dqio_mutex)
3605dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3606ac27a0ecSDave Kleikamp  *
3607ac27a0ecSDave Kleikamp  */
3608ac27a0ecSDave Kleikamp 
3609ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3610ac27a0ecSDave Kleikamp 
3611ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3612ac27a0ecSDave Kleikamp {
3613ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3614ac27a0ecSDave Kleikamp }
3615ac27a0ecSDave Kleikamp 
3616617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3617ac27a0ecSDave Kleikamp {
3618ac27a0ecSDave Kleikamp 	int ret, err;
3619ac27a0ecSDave Kleikamp 	handle_t *handle;
3620ac27a0ecSDave Kleikamp 	struct inode *inode;
3621ac27a0ecSDave Kleikamp 
3622ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3623617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3624617ba13bSMingming Cao 					EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3625ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3626ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3627ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3628617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3629ac27a0ecSDave Kleikamp 	if (!ret)
3630ac27a0ecSDave Kleikamp 		ret = err;
3631ac27a0ecSDave Kleikamp 	return ret;
3632ac27a0ecSDave Kleikamp }
3633ac27a0ecSDave Kleikamp 
3634617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3635ac27a0ecSDave Kleikamp {
3636ac27a0ecSDave Kleikamp 	int ret, err;
3637ac27a0ecSDave Kleikamp 	handle_t *handle;
3638ac27a0ecSDave Kleikamp 
3639617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3640617ba13bSMingming Cao 					EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3641ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3642ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3643ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3644617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3645ac27a0ecSDave Kleikamp 	if (!ret)
3646ac27a0ecSDave Kleikamp 		ret = err;
3647ac27a0ecSDave Kleikamp 	return ret;
3648ac27a0ecSDave Kleikamp }
3649ac27a0ecSDave Kleikamp 
3650617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3651ac27a0ecSDave Kleikamp {
3652ac27a0ecSDave Kleikamp 	int ret, err;
3653ac27a0ecSDave Kleikamp 	handle_t *handle;
3654ac27a0ecSDave Kleikamp 
3655617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3656617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
36579c3013e9SJan Kara 	if (IS_ERR(handle)) {
36589c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
36599c3013e9SJan Kara 		dquot_release(dquot);
3660ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
36619c3013e9SJan Kara 	}
3662ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3663617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3664ac27a0ecSDave Kleikamp 	if (!ret)
3665ac27a0ecSDave Kleikamp 		ret = err;
3666ac27a0ecSDave Kleikamp 	return ret;
3667ac27a0ecSDave Kleikamp }
3668ac27a0ecSDave Kleikamp 
3669617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3670ac27a0ecSDave Kleikamp {
36712c8be6b2SJan Kara 	/* Are we journaling quotas? */
3672617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3673617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3674ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3675617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3676ac27a0ecSDave Kleikamp 	} else {
3677ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3678ac27a0ecSDave Kleikamp 	}
3679ac27a0ecSDave Kleikamp }
3680ac27a0ecSDave Kleikamp 
3681617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3682ac27a0ecSDave Kleikamp {
3683ac27a0ecSDave Kleikamp 	int ret, err;
3684ac27a0ecSDave Kleikamp 	handle_t *handle;
3685ac27a0ecSDave Kleikamp 
3686ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3687617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3688ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3689ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3690ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3691617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3692ac27a0ecSDave Kleikamp 	if (!ret)
3693ac27a0ecSDave Kleikamp 		ret = err;
3694ac27a0ecSDave Kleikamp 	return ret;
3695ac27a0ecSDave Kleikamp }
3696ac27a0ecSDave Kleikamp 
3697ac27a0ecSDave Kleikamp /*
3698ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3699ac27a0ecSDave Kleikamp  * the quota file and such...
3700ac27a0ecSDave Kleikamp  */
3701617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3702ac27a0ecSDave Kleikamp {
3703617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3704617ba13bSMingming Cao 			EXT4_SB(sb)->s_jquota_fmt, type);
3705ac27a0ecSDave Kleikamp }
3706ac27a0ecSDave Kleikamp 
3707ac27a0ecSDave Kleikamp /*
3708ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3709ac27a0ecSDave Kleikamp  */
3710617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
37118264613dSAl Viro 			 char *name, int remount)
3712ac27a0ecSDave Kleikamp {
3713ac27a0ecSDave Kleikamp 	int err;
37148264613dSAl Viro 	struct path path;
3715ac27a0ecSDave Kleikamp 
3716ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3717ac27a0ecSDave Kleikamp 		return -EINVAL;
37188264613dSAl Viro 	/* When remounting, no checks are needed and in fact, name is NULL */
37190623543bSJan Kara 	if (remount)
37208264613dSAl Viro 		return vfs_quota_on(sb, type, format_id, name, remount);
37210623543bSJan Kara 
37228264613dSAl Viro 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3723ac27a0ecSDave Kleikamp 	if (err)
3724ac27a0ecSDave Kleikamp 		return err;
37250623543bSJan Kara 
3726ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
37278264613dSAl Viro 	if (path.mnt->mnt_sb != sb) {
37288264613dSAl Viro 		path_put(&path);
3729ac27a0ecSDave Kleikamp 		return -EXDEV;
3730ac27a0ecSDave Kleikamp 	}
37310623543bSJan Kara 	/* Journaling quota? */
37320623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
37332b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
37348264613dSAl Viro 		if (path.dentry->d_parent != sb->s_root)
3735ac27a0ecSDave Kleikamp 			printk(KERN_WARNING
3736617ba13bSMingming Cao 				"EXT4-fs: Quota file not on filesystem root. "
37370623543bSJan Kara 				"Journaled quota will not work.\n");
37380623543bSJan Kara 	}
37390623543bSJan Kara 
37400623543bSJan Kara 	/*
37410623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
37420623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
37430623543bSJan Kara 	 */
37440390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
37450390131bSFrank Mayhar 	    ext4_should_journal_data(path.dentry->d_inode)) {
37460623543bSJan Kara 		/*
37470623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
37480623543bSJan Kara 		 * otherwise be livelocked...
37490623543bSJan Kara 		 */
37500623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
37517ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
37520623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
37537ffe1ea8SHidehiro Kawai 		if (err) {
37548264613dSAl Viro 			path_put(&path);
37557ffe1ea8SHidehiro Kawai 			return err;
37567ffe1ea8SHidehiro Kawai 		}
37570623543bSJan Kara 	}
37580623543bSJan Kara 
37598264613dSAl Viro 	err = vfs_quota_on_path(sb, type, format_id, &path);
37608264613dSAl Viro 	path_put(&path);
376177e69dacSAl Viro 	return err;
3762ac27a0ecSDave Kleikamp }
3763ac27a0ecSDave Kleikamp 
3764ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3765ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3766ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3767ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3768617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3769ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3770ac27a0ecSDave Kleikamp {
3771ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3772725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3773ac27a0ecSDave Kleikamp 	int err = 0;
3774ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3775ac27a0ecSDave Kleikamp 	int tocopy;
3776ac27a0ecSDave Kleikamp 	size_t toread;
3777ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3778ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3779ac27a0ecSDave Kleikamp 
3780ac27a0ecSDave Kleikamp 	if (off > i_size)
3781ac27a0ecSDave Kleikamp 		return 0;
3782ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3783ac27a0ecSDave Kleikamp 		len = i_size-off;
3784ac27a0ecSDave Kleikamp 	toread = len;
3785ac27a0ecSDave Kleikamp 	while (toread > 0) {
3786ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3787ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3788617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3789ac27a0ecSDave Kleikamp 		if (err)
3790ac27a0ecSDave Kleikamp 			return err;
3791ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3792ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3793ac27a0ecSDave Kleikamp 		else
3794ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3795ac27a0ecSDave Kleikamp 		brelse(bh);
3796ac27a0ecSDave Kleikamp 		offset = 0;
3797ac27a0ecSDave Kleikamp 		toread -= tocopy;
3798ac27a0ecSDave Kleikamp 		data += tocopy;
3799ac27a0ecSDave Kleikamp 		blk++;
3800ac27a0ecSDave Kleikamp 	}
3801ac27a0ecSDave Kleikamp 	return len;
3802ac27a0ecSDave Kleikamp }
3803ac27a0ecSDave Kleikamp 
3804ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3805ac27a0ecSDave Kleikamp  * enough credits) */
3806617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3807ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3808ac27a0ecSDave Kleikamp {
3809ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3810725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3811ac27a0ecSDave Kleikamp 	int err = 0;
3812ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3813ac27a0ecSDave Kleikamp 	int tocopy;
3814617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3815ac27a0ecSDave Kleikamp 	size_t towrite = len;
3816ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3817ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3818ac27a0ecSDave Kleikamp 
38190390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
3820e5f8eab8STheodore Ts'o 		printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
38219c3013e9SJan Kara 			" cancelled because transaction is not started.\n",
38229c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
38239c3013e9SJan Kara 		return -EIO;
38249c3013e9SJan Kara 	}
3825ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3826ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3827ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3828ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3829617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3830ac27a0ecSDave Kleikamp 		if (!bh)
3831ac27a0ecSDave Kleikamp 			goto out;
3832ac27a0ecSDave Kleikamp 		if (journal_quota) {
3833617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3834ac27a0ecSDave Kleikamp 			if (err) {
3835ac27a0ecSDave Kleikamp 				brelse(bh);
3836ac27a0ecSDave Kleikamp 				goto out;
3837ac27a0ecSDave Kleikamp 			}
3838ac27a0ecSDave Kleikamp 		}
3839ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3840ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3841ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3842ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3843ac27a0ecSDave Kleikamp 		if (journal_quota)
38440390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle, NULL, bh);
3845ac27a0ecSDave Kleikamp 		else {
3846ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3847678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3848ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3849ac27a0ecSDave Kleikamp 		}
3850ac27a0ecSDave Kleikamp 		brelse(bh);
3851ac27a0ecSDave Kleikamp 		if (err)
3852ac27a0ecSDave Kleikamp 			goto out;
3853ac27a0ecSDave Kleikamp 		offset = 0;
3854ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3855ac27a0ecSDave Kleikamp 		data += tocopy;
3856ac27a0ecSDave Kleikamp 		blk++;
3857ac27a0ecSDave Kleikamp 	}
3858ac27a0ecSDave Kleikamp out:
38594d04e4fbSJan Kara 	if (len == towrite) {
38604d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
3861ac27a0ecSDave Kleikamp 		return err;
38624d04e4fbSJan Kara 	}
3863ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3864ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3865617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3866ac27a0ecSDave Kleikamp 	}
3867ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3868617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3869ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3870ac27a0ecSDave Kleikamp 	return len - towrite;
3871ac27a0ecSDave Kleikamp }
3872ac27a0ecSDave Kleikamp 
3873ac27a0ecSDave Kleikamp #endif
3874ac27a0ecSDave Kleikamp 
3875617ba13bSMingming Cao static int ext4_get_sb(struct file_system_type *fs_type,
3876ac27a0ecSDave Kleikamp 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3877ac27a0ecSDave Kleikamp {
3878617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3879ac27a0ecSDave Kleikamp }
3880ac27a0ecSDave Kleikamp 
388103010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
3882ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
388303010a33STheodore Ts'o 	.name		= "ext4",
3884617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
3885ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
3886ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
3887ac27a0ecSDave Kleikamp };
3888ac27a0ecSDave Kleikamp 
388903010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
389003010a33STheodore Ts'o static int ext4dev_get_sb(struct file_system_type *fs_type,
389103010a33STheodore Ts'o 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
389203010a33STheodore Ts'o {
389303010a33STheodore Ts'o 	printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
389403010a33STheodore Ts'o 	       "to mount using ext4\n");
389503010a33STheodore Ts'o 	printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
389603010a33STheodore Ts'o 	       "will go away by 2.6.31\n");
389703010a33STheodore Ts'o 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
389803010a33STheodore Ts'o }
389903010a33STheodore Ts'o 
390003010a33STheodore Ts'o static struct file_system_type ext4dev_fs_type = {
390103010a33STheodore Ts'o 	.owner		= THIS_MODULE,
390203010a33STheodore Ts'o 	.name		= "ext4dev",
390303010a33STheodore Ts'o 	.get_sb		= ext4dev_get_sb,
390403010a33STheodore Ts'o 	.kill_sb	= kill_block_super,
390503010a33STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
390603010a33STheodore Ts'o };
390703010a33STheodore Ts'o MODULE_ALIAS("ext4dev");
390803010a33STheodore Ts'o #endif
390903010a33STheodore Ts'o 
3910617ba13bSMingming Cao static int __init init_ext4_fs(void)
3911ac27a0ecSDave Kleikamp {
3912c9de560dSAlex Tomas 	int err;
3913c9de560dSAlex Tomas 
39143197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
39153197ebdbSTheodore Ts'o 	if (!ext4_kset)
39163197ebdbSTheodore Ts'o 		return -ENOMEM;
39179f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3918c9de560dSAlex Tomas 	err = init_ext4_mballoc();
3919ac27a0ecSDave Kleikamp 	if (err)
3920ac27a0ecSDave Kleikamp 		return err;
3921c9de560dSAlex Tomas 
3922c9de560dSAlex Tomas 	err = init_ext4_xattr();
3923c9de560dSAlex Tomas 	if (err)
3924c9de560dSAlex Tomas 		goto out2;
3925ac27a0ecSDave Kleikamp 	err = init_inodecache();
3926ac27a0ecSDave Kleikamp 	if (err)
3927ac27a0ecSDave Kleikamp 		goto out1;
392803010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
3929ac27a0ecSDave Kleikamp 	if (err)
3930ac27a0ecSDave Kleikamp 		goto out;
393103010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
393203010a33STheodore Ts'o 	err = register_filesystem(&ext4dev_fs_type);
393303010a33STheodore Ts'o 	if (err) {
393403010a33STheodore Ts'o 		unregister_filesystem(&ext4_fs_type);
393503010a33STheodore Ts'o 		goto out;
393603010a33STheodore Ts'o 	}
393703010a33STheodore Ts'o #endif
3938ac27a0ecSDave Kleikamp 	return 0;
3939ac27a0ecSDave Kleikamp out:
3940ac27a0ecSDave Kleikamp 	destroy_inodecache();
3941ac27a0ecSDave Kleikamp out1:
3942617ba13bSMingming Cao 	exit_ext4_xattr();
3943c9de560dSAlex Tomas out2:
3944c9de560dSAlex Tomas 	exit_ext4_mballoc();
3945ac27a0ecSDave Kleikamp 	return err;
3946ac27a0ecSDave Kleikamp }
3947ac27a0ecSDave Kleikamp 
3948617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
3949ac27a0ecSDave Kleikamp {
395003010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
395103010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
3952617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
395303010a33STheodore Ts'o #endif
3954ac27a0ecSDave Kleikamp 	destroy_inodecache();
3955617ba13bSMingming Cao 	exit_ext4_xattr();
3956c9de560dSAlex Tomas 	exit_ext4_mballoc();
39579f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
39583197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
3959ac27a0ecSDave Kleikamp }
3960ac27a0ecSDave Kleikamp 
3961ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
396283982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
3963ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
3964617ba13bSMingming Cao module_init(init_ext4_fs)
3965617ba13bSMingming Cao module_exit(exit_ext4_fs)
3966