xref: /linux/fs/ext4/super.c (revision c39a7f84d7845aa95d1c7c168f38215aedcc13c2)
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>
23dab291afSMingming Cao #include <linux/jbd2.h>
24ac27a0ecSDave Kleikamp #include <linux/slab.h>
25ac27a0ecSDave Kleikamp #include <linux/init.h>
26ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
27ac27a0ecSDave Kleikamp #include <linux/parser.h>
28ac27a0ecSDave Kleikamp #include <linux/smp_lock.h>
29ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
30a5694255SChristoph Hellwig #include <linux/exportfs.h>
31ac27a0ecSDave Kleikamp #include <linux/vfs.h>
32ac27a0ecSDave Kleikamp #include <linux/random.h>
33ac27a0ecSDave Kleikamp #include <linux/mount.h>
34ac27a0ecSDave Kleikamp #include <linux/namei.h>
35ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
36ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
379f6200bbSTheodore Ts'o #include <linux/proc_fs.h>
38ede86cc4STheodore Ts'o #include <linux/marker.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
41ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
42ac27a0ecSDave Kleikamp 
433dcf5451SChristoph Hellwig #include "ext4.h"
443dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
45ac27a0ecSDave Kleikamp #include "xattr.h"
46ac27a0ecSDave Kleikamp #include "acl.h"
47ac27a0ecSDave Kleikamp #include "namei.h"
48717d50e4SAndreas Dilger #include "group.h"
49ac27a0ecSDave Kleikamp 
509f6200bbSTheodore Ts'o struct proc_dir_entry *ext4_proc_root;
519f6200bbSTheodore Ts'o 
52617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
53ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
54617ba13bSMingming Cao static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
55ac27a0ecSDave Kleikamp 			       unsigned int);
56617ba13bSMingming Cao static void ext4_commit_super(struct super_block *sb,
572b2d6d01STheodore Ts'o 			      struct ext4_super_block *es, 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);
67617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb);
68617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
69617ba13bSMingming Cao static void ext4_write_super_lockfs(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 	}
308617ba13bSMingming Cao 	ext4_commit_super(sb, es, 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);
4505d1b1b3fSAneesh Kumar K.V 		ext4_commit_super(sb, es, 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:
510617ba13bSMingming Cao 	printk(KERN_ERR "EXT4: 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);
579617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
580ac27a0ecSDave Kleikamp 	}
581240799cdSTheodore Ts'o 	if (sbi->s_proc) {
582240799cdSTheodore Ts'o 		remove_proc_entry("inode_readahead_blks", sbi->s_proc);
5839f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
584240799cdSTheodore Ts'o 	}
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);
589772cb7c8SJose R. Santos 	kfree(sbi->s_flex_groups);
590ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
591ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
592ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
5936bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
594ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
595ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
596ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
597ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
598ac27a0ecSDave Kleikamp #endif
599ac27a0ecSDave Kleikamp 
600ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
601ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
602ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
603ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
604ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
605ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
606ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
607ac27a0ecSDave Kleikamp 
608f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
609ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
610ac27a0ecSDave Kleikamp 		/*
611ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
612ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
613ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
614ac27a0ecSDave Kleikamp 		 */
615ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
616f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
617617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
618ac27a0ecSDave Kleikamp 	}
619ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
620ac27a0ecSDave Kleikamp 	kfree(sbi);
621ac27a0ecSDave Kleikamp 	return;
622ac27a0ecSDave Kleikamp }
623ac27a0ecSDave Kleikamp 
624e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
625ac27a0ecSDave Kleikamp 
626ac27a0ecSDave Kleikamp /*
627ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
628ac27a0ecSDave Kleikamp  */
629617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
630ac27a0ecSDave Kleikamp {
631617ba13bSMingming Cao 	struct ext4_inode_info *ei;
632ac27a0ecSDave Kleikamp 
633e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
634ac27a0ecSDave Kleikamp 	if (!ei)
635ac27a0ecSDave Kleikamp 		return NULL;
63603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
637617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
638617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
639ac27a0ecSDave Kleikamp #endif
640ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
64191246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
642a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
643c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
644c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
6450390131bSFrank Mayhar 	/*
6460390131bSFrank Mayhar 	 * Note:  We can be called before EXT4_SB(sb)->s_journal is set,
6470390131bSFrank Mayhar 	 * therefore it can be null here.  Don't check it, just initialize
6480390131bSFrank Mayhar 	 * jinode.
6490390131bSFrank Mayhar 	 */
650678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
651d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
652d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
653d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
654d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
655d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
656ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
657ac27a0ecSDave Kleikamp }
658ac27a0ecSDave Kleikamp 
659617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
660ac27a0ecSDave Kleikamp {
6619f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
6629f7dd93dSVasily Averin 		printk("EXT4 Inode %p: orphan list check failed!\n",
6639f7dd93dSVasily Averin 			EXT4_I(inode));
6649f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
6659f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
6669f7dd93dSVasily Averin 				true);
6679f7dd93dSVasily Averin 		dump_stack();
6689f7dd93dSVasily Averin 	}
669617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
670ac27a0ecSDave Kleikamp }
671ac27a0ecSDave Kleikamp 
67251cc5068SAlexey Dobriyan static void init_once(void *foo)
673ac27a0ecSDave Kleikamp {
674617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
675ac27a0ecSDave Kleikamp 
676ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
67703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
678ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
679ac27a0ecSDave Kleikamp #endif
6800e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
681ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
682ac27a0ecSDave Kleikamp }
683ac27a0ecSDave Kleikamp 
684ac27a0ecSDave Kleikamp static int init_inodecache(void)
685ac27a0ecSDave Kleikamp {
686617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
687617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
688ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
689ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
69020c2df83SPaul Mundt 					     init_once);
691617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
692ac27a0ecSDave Kleikamp 		return -ENOMEM;
693ac27a0ecSDave Kleikamp 	return 0;
694ac27a0ecSDave Kleikamp }
695ac27a0ecSDave Kleikamp 
696ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
697ac27a0ecSDave Kleikamp {
698617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
699ac27a0ecSDave Kleikamp }
700ac27a0ecSDave Kleikamp 
701617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
702ac27a0ecSDave Kleikamp {
70303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
704617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
705617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
706617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
707617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
708ac27a0ecSDave Kleikamp 	}
709617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
710617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
711617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
712617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
713ac27a0ecSDave Kleikamp 	}
714ac27a0ecSDave Kleikamp #endif
715c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
7160390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode))
717678aaf48SJan Kara 		jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
718678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
719ac27a0ecSDave Kleikamp }
720ac27a0ecSDave Kleikamp 
7212b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
7222b2d6d01STheodore Ts'o 					   struct super_block *sb)
723ac27a0ecSDave Kleikamp {
724ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
725617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
726ac27a0ecSDave Kleikamp 
727ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
728ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
729ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
730ac27a0ecSDave Kleikamp 
731ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
732ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
733ac27a0ecSDave Kleikamp 
734ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
735ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
736ac27a0ecSDave Kleikamp 
737617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
738ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
739ac27a0ecSDave Kleikamp 
740617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
741ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
742ac27a0ecSDave Kleikamp #endif
743ac27a0ecSDave Kleikamp }
744ac27a0ecSDave Kleikamp 
745d9c9bef1SMiklos Szeredi /*
746d9c9bef1SMiklos Szeredi  * Show an option if
747d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
748d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
749d9c9bef1SMiklos Szeredi  */
750617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
751ac27a0ecSDave Kleikamp {
752aa22df2cSAneesh Kumar K.V 	int def_errors;
753aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
754ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
755d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
756d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
757d9c9bef1SMiklos Szeredi 
758d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
759aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
760d9c9bef1SMiklos Szeredi 
761d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
762d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
763d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
764d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
765aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
766d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
767d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
768d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
769d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
770d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
771d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
772d9c9bef1SMiklos Szeredi 	}
773d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
774d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
775d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
776d9c9bef1SMiklos Szeredi 	}
777bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
778d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
779bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
780d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
781bb4f397aSAneesh Kumar K.V 		}
782bb4f397aSAneesh Kumar K.V 	}
783aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
784bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
785aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
786d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
787aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
788d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
789aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
790d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
791d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
792d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
79303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
794aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
795aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
796d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
797d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
798d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
799d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
800d9c9bef1SMiklos Szeredi 	}
801d9c9bef1SMiklos Szeredi #endif
80203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
803aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
804d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
805d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
806d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
807d9c9bef1SMiklos Szeredi #endif
808d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, RESERVATION))
809d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noreservation");
81030773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
811d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
812d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
813d9c9bef1SMiklos Szeredi 	}
81430773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
81530773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
81630773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
81730773840STheodore Ts'o 	}
81830773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
81930773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
82030773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
82130773840STheodore Ts'o 	}
82230773840STheodore Ts'o 
823571640caSEric Sandeen 	/*
824571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
825571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
826571640caSEric Sandeen 	 * status is.
827571640caSEric Sandeen 	 */
828571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
829571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
830cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
831cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
832d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
833d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
834d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, EXTENTS))
835d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noextents");
83625ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
83725ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
838dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
839dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
840dd919b98SAneesh Kumar K.V 
841ac27a0ecSDave Kleikamp 
842cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
843cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
844aa22df2cSAneesh Kumar K.V 	/*
845aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
846aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
847aa22df2cSAneesh Kumar K.V 	 */
848617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
849ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
850617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
851ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
852617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
853ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
854ac27a0ecSDave Kleikamp 
855240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
856240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
857240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
858240799cdSTheodore Ts'o 
8595bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
8605bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
8615bf5683aSHidehiro Kawai 
862617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
863ac27a0ecSDave Kleikamp 	return 0;
864ac27a0ecSDave Kleikamp }
865ac27a0ecSDave Kleikamp 
866ac27a0ecSDave Kleikamp 
8671b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
8681b961ac0SChristoph Hellwig 		u64 ino, u32 generation)
869ac27a0ecSDave Kleikamp {
870ac27a0ecSDave Kleikamp 	struct inode *inode;
871ac27a0ecSDave Kleikamp 
872617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
873ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
874617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
875ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
876ac27a0ecSDave Kleikamp 
877ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
878ac27a0ecSDave Kleikamp 	 *
879617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
880ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
881ac27a0ecSDave Kleikamp 	 *
882ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
883ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
884ac27a0ecSDave Kleikamp 	 */
8851d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
8861d1fe1eeSDavid Howells 	if (IS_ERR(inode))
8871d1fe1eeSDavid Howells 		return ERR_CAST(inode);
8881d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
889ac27a0ecSDave Kleikamp 		iput(inode);
890ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
891ac27a0ecSDave Kleikamp 	}
8921b961ac0SChristoph Hellwig 
8931b961ac0SChristoph Hellwig 	return inode;
894ac27a0ecSDave Kleikamp }
8951b961ac0SChristoph Hellwig 
8961b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
8971b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
8981b961ac0SChristoph Hellwig {
8991b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
9001b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
9011b961ac0SChristoph Hellwig }
9021b961ac0SChristoph Hellwig 
9031b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
9041b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
9051b961ac0SChristoph Hellwig {
9061b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
9071b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
908ac27a0ecSDave Kleikamp }
909ac27a0ecSDave Kleikamp 
910*c39a7f84SToshiyuki Okajima /*
911*c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
912*c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
913*c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
914*c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
915*c39a7f84SToshiyuki Okajima  */
916*c39a7f84SToshiyuki Okajima static int bdev_try_to_free_page(struct super_block *sb, struct page *page, gfp_t wait)
917*c39a7f84SToshiyuki Okajima {
918*c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
919*c39a7f84SToshiyuki Okajima 
920*c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
921*c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
922*c39a7f84SToshiyuki Okajima 		return 0;
923*c39a7f84SToshiyuki Okajima 	if (journal)
924*c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
925*c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
926*c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
927*c39a7f84SToshiyuki Okajima }
928*c39a7f84SToshiyuki Okajima 
929ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
930ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
931ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
932ac27a0ecSDave Kleikamp 
933617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type);
934617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode);
935617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
936617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
937617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
938617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
939617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
9406f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
9416f28e087SJan Kara 				char *path, int remount);
942617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
943617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
944ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
945617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
946ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
947ac27a0ecSDave Kleikamp 
948617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
949617ba13bSMingming Cao 	.initialize	= ext4_dquot_initialize,
950617ba13bSMingming Cao 	.drop		= ext4_dquot_drop,
951ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
952ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
953ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
954ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
955ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
956617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
957617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
958617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
959617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
960617ba13bSMingming Cao 	.write_info	= ext4_write_info
961ac27a0ecSDave Kleikamp };
962ac27a0ecSDave Kleikamp 
963617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
964617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
965ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
966ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
967ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
968ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
969ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
970ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
971ac27a0ecSDave Kleikamp };
972ac27a0ecSDave Kleikamp #endif
973ac27a0ecSDave Kleikamp 
974ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
975617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
976617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
977617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
978617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
979617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
980617ba13bSMingming Cao 	.put_super	= ext4_put_super,
981617ba13bSMingming Cao 	.write_super	= ext4_write_super,
982617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
983617ba13bSMingming Cao 	.write_super_lockfs = ext4_write_super_lockfs,
984617ba13bSMingming Cao 	.unlockfs	= ext4_unlockfs,
985617ba13bSMingming Cao 	.statfs		= ext4_statfs,
986617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
987617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
988617ba13bSMingming Cao 	.show_options	= ext4_show_options,
989ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
990617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
991617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
992ac27a0ecSDave Kleikamp #endif
993*c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
994ac27a0ecSDave Kleikamp };
995ac27a0ecSDave Kleikamp 
99639655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
9971b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
9981b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
999617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1000ac27a0ecSDave Kleikamp };
1001ac27a0ecSDave Kleikamp 
1002ac27a0ecSDave Kleikamp enum {
1003ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1004ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
100501436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1006ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1007ac27a0ecSDave Kleikamp 	Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
100830773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
100930773840STheodore Ts'o 	Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
1010818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1011ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
10125bf5683aSHidehiro Kawai 	Opt_data_err_abort, Opt_data_err_ignore,
1013ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1014ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
1015ac27a0ecSDave Kleikamp 	Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
101625ec56b5SJean Noel Cordenner 	Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
101701436ef2STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc,
1018240799cdSTheodore Ts'o 	Opt_inode_readahead_blks
1019ac27a0ecSDave Kleikamp };
1020ac27a0ecSDave Kleikamp 
1021a447c093SSteven Whitehouse static const match_table_t tokens = {
1022ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1023ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1024ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1025ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1026ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1027ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1028ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1029ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1030ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1031ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1032ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1033ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1034ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1035ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1036ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1037ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1038ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1039ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1040ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1041ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1042ac27a0ecSDave Kleikamp 	{Opt_reservation, "reservation"},
1043ac27a0ecSDave Kleikamp 	{Opt_noreservation, "noreservation"},
1044ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1045ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1046ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1047ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
104830773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
104930773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1050ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1051ac27a0ecSDave Kleikamp 	{Opt_journal_inum, "journal=%u"},
1052ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1053818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1054818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1055ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1056ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1057ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1058ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
10595bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
10605bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1061ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1062ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1063ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1064ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1065ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1066ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1067ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1068ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1069ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1070ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1071ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
1072a86c6181SAlex Tomas 	{Opt_extents, "extents"},
10731e2462f9SMingming Cao 	{Opt_noextents, "noextents"},
107425ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1075c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1076ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
107764769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1078dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
1079240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1080f3f12faaSJosef Bacik 	{Opt_err, NULL},
1081ac27a0ecSDave Kleikamp };
1082ac27a0ecSDave Kleikamp 
1083617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1084ac27a0ecSDave Kleikamp {
1085617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1086ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1087ac27a0ecSDave Kleikamp 
1088ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1089ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
1090ac27a0ecSDave Kleikamp 	options += 3;
1091617ba13bSMingming Cao 	/*todo: use simple_strtoll with >32bit ext4 */
1092ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1093ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
10944776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1095ac27a0ecSDave Kleikamp 		       (char *) *data);
1096ac27a0ecSDave Kleikamp 		return 1;
1097ac27a0ecSDave Kleikamp 	}
1098ac27a0ecSDave Kleikamp 	if (*options == ',')
1099ac27a0ecSDave Kleikamp 		options++;
1100ac27a0ecSDave Kleikamp 	*data = (void *) options;
1101ac27a0ecSDave Kleikamp 	return sb_block;
1102ac27a0ecSDave Kleikamp }
1103ac27a0ecSDave Kleikamp 
1104ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1105ac27a0ecSDave Kleikamp 			 unsigned int *inum, unsigned long *journal_devnum,
1106617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1107ac27a0ecSDave Kleikamp {
1108617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1109ac27a0ecSDave Kleikamp 	char *p;
1110ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1111ac27a0ecSDave Kleikamp 	int data_opt = 0;
1112ac27a0ecSDave Kleikamp 	int option;
1113ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1114dfc5d03fSJan Kara 	int qtype, qfmt;
1115ac27a0ecSDave Kleikamp 	char *qname;
1116ac27a0ecSDave Kleikamp #endif
1117c07651b5SAneesh Kumar K.V 	ext4_fsblk_t last_block;
1118ac27a0ecSDave Kleikamp 
1119ac27a0ecSDave Kleikamp 	if (!options)
1120ac27a0ecSDave Kleikamp 		return 1;
1121ac27a0ecSDave Kleikamp 
1122ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1123ac27a0ecSDave Kleikamp 		int token;
1124ac27a0ecSDave Kleikamp 		if (!*p)
1125ac27a0ecSDave Kleikamp 			continue;
1126ac27a0ecSDave Kleikamp 
1127ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1128ac27a0ecSDave Kleikamp 		switch (token) {
1129ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1130ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
1131ac27a0ecSDave Kleikamp 			break;
1132ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1133ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
1134ac27a0ecSDave Kleikamp 			break;
1135ac27a0ecSDave Kleikamp 		case Opt_grpid:
1136ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
1137ac27a0ecSDave Kleikamp 			break;
1138ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1139ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
1140ac27a0ecSDave Kleikamp 			break;
1141ac27a0ecSDave Kleikamp 		case Opt_resuid:
1142ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1143ac27a0ecSDave Kleikamp 				return 0;
1144ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1145ac27a0ecSDave Kleikamp 			break;
1146ac27a0ecSDave Kleikamp 		case Opt_resgid:
1147ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1148ac27a0ecSDave Kleikamp 				return 0;
1149ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1150ac27a0ecSDave Kleikamp 			break;
1151ac27a0ecSDave Kleikamp 		case Opt_sb:
1152ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1153ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1154ac27a0ecSDave Kleikamp 			break;
1155ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1156ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1157ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1158ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1159ac27a0ecSDave Kleikamp 			break;
1160ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1161ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1162ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1163ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1164ac27a0ecSDave Kleikamp 			break;
1165ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1166ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1167ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1168ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1169ac27a0ecSDave Kleikamp 			break;
1170ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1171ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1172ac27a0ecSDave Kleikamp 			break;
1173ac27a0ecSDave Kleikamp 		case Opt_debug:
1174ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1175ac27a0ecSDave Kleikamp 			break;
1176ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1177ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1178ac27a0ecSDave Kleikamp 			break;
1179ac27a0ecSDave Kleikamp 		case Opt_orlov:
1180ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1181ac27a0ecSDave Kleikamp 			break;
118203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1183ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1184ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1185ac27a0ecSDave Kleikamp 			break;
1186ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1187ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1188ac27a0ecSDave Kleikamp 			break;
1189ac27a0ecSDave Kleikamp #else
1190ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1191ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
11924776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)user_xattr options "
11934776004fSTheodore Ts'o 			       "not supported\n");
1194ac27a0ecSDave Kleikamp 			break;
1195ac27a0ecSDave Kleikamp #endif
119603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1197ac27a0ecSDave Kleikamp 		case Opt_acl:
1198ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1199ac27a0ecSDave Kleikamp 			break;
1200ac27a0ecSDave Kleikamp 		case Opt_noacl:
1201ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1202ac27a0ecSDave Kleikamp 			break;
1203ac27a0ecSDave Kleikamp #else
1204ac27a0ecSDave Kleikamp 		case Opt_acl:
1205ac27a0ecSDave Kleikamp 		case Opt_noacl:
12064776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)acl options "
12074776004fSTheodore Ts'o 			       "not supported\n");
1208ac27a0ecSDave Kleikamp 			break;
1209ac27a0ecSDave Kleikamp #endif
1210ac27a0ecSDave Kleikamp 		case Opt_reservation:
1211ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, RESERVATION);
1212ac27a0ecSDave Kleikamp 			break;
1213ac27a0ecSDave Kleikamp 		case Opt_noreservation:
1214ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, RESERVATION);
1215ac27a0ecSDave Kleikamp 			break;
1216ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1217ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1218ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1219ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1220ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1221ac27a0ecSDave Kleikamp 			   journal file. */
1222ac27a0ecSDave Kleikamp 			if (is_remount) {
1223617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1224ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1225ac27a0ecSDave Kleikamp 				return 0;
1226ac27a0ecSDave Kleikamp 			}
1227ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1228ac27a0ecSDave Kleikamp 			break;
1229ac27a0ecSDave Kleikamp 		case Opt_journal_inum:
1230ac27a0ecSDave Kleikamp 			if (is_remount) {
1231617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1232ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1233ac27a0ecSDave Kleikamp 				return 0;
1234ac27a0ecSDave Kleikamp 			}
1235ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1236ac27a0ecSDave Kleikamp 				return 0;
1237ac27a0ecSDave Kleikamp 			*inum = option;
1238ac27a0ecSDave Kleikamp 			break;
1239ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1240ac27a0ecSDave Kleikamp 			if (is_remount) {
1241617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1242ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1243ac27a0ecSDave Kleikamp 				return 0;
1244ac27a0ecSDave Kleikamp 			}
1245ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1246ac27a0ecSDave Kleikamp 				return 0;
1247ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1248ac27a0ecSDave Kleikamp 			break;
1249818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1250818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1251818d276cSGirish Shilamkar 			break;
1252818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1253818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1254818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1255818d276cSGirish Shilamkar 			break;
1256ac27a0ecSDave Kleikamp 		case Opt_noload:
1257ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1258ac27a0ecSDave Kleikamp 			break;
1259ac27a0ecSDave Kleikamp 		case Opt_commit:
1260ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1261ac27a0ecSDave Kleikamp 				return 0;
1262ac27a0ecSDave Kleikamp 			if (option < 0)
1263ac27a0ecSDave Kleikamp 				return 0;
1264ac27a0ecSDave Kleikamp 			if (option == 0)
1265cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1266ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1267ac27a0ecSDave Kleikamp 			break;
126830773840STheodore Ts'o 		case Opt_max_batch_time:
126930773840STheodore Ts'o 			if (match_int(&args[0], &option))
127030773840STheodore Ts'o 				return 0;
127130773840STheodore Ts'o 			if (option < 0)
127230773840STheodore Ts'o 				return 0;
127330773840STheodore Ts'o 			if (option == 0)
127430773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
127530773840STheodore Ts'o 			sbi->s_max_batch_time = option;
127630773840STheodore Ts'o 			break;
127730773840STheodore Ts'o 		case Opt_min_batch_time:
127830773840STheodore Ts'o 			if (match_int(&args[0], &option))
127930773840STheodore Ts'o 				return 0;
128030773840STheodore Ts'o 			if (option < 0)
128130773840STheodore Ts'o 				return 0;
128230773840STheodore Ts'o 			sbi->s_min_batch_time = option;
128330773840STheodore Ts'o 			break;
1284ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1285617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1286ac27a0ecSDave Kleikamp 			goto datacheck;
1287ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1288617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1289ac27a0ecSDave Kleikamp 			goto datacheck;
1290ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1291617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1292ac27a0ecSDave Kleikamp 		datacheck:
1293ac27a0ecSDave Kleikamp 			if (is_remount) {
1294617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1295ac27a0ecSDave Kleikamp 						!= data_opt) {
1296ac27a0ecSDave Kleikamp 					printk(KERN_ERR
1297617ba13bSMingming Cao 						"EXT4-fs: cannot change data "
1298ac27a0ecSDave Kleikamp 						"mode on remount\n");
1299ac27a0ecSDave Kleikamp 					return 0;
1300ac27a0ecSDave Kleikamp 				}
1301ac27a0ecSDave Kleikamp 			} else {
1302617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1303ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1304ac27a0ecSDave Kleikamp 			}
1305ac27a0ecSDave Kleikamp 			break;
13065bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
13075bf5683aSHidehiro Kawai 			set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13085bf5683aSHidehiro Kawai 			break;
13095bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
13105bf5683aSHidehiro Kawai 			clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13115bf5683aSHidehiro Kawai 			break;
1312ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1313ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1314ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1315ac27a0ecSDave Kleikamp 			goto set_qf_name;
1316ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1317ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1318ac27a0ecSDave Kleikamp set_qf_name:
1319dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1320dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1321dfc5d03fSJan Kara 			    !sbi->s_qf_names[qtype]) {
1322ac27a0ecSDave Kleikamp 				printk(KERN_ERR
13232c8be6b2SJan Kara 				       "EXT4-fs: Cannot change journaled "
1324ac27a0ecSDave Kleikamp 				       "quota options when quota turned on.\n");
1325ac27a0ecSDave Kleikamp 				return 0;
1326ac27a0ecSDave Kleikamp 			}
1327ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1328ac27a0ecSDave Kleikamp 			if (!qname) {
1329ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1330617ba13bSMingming Cao 					"EXT4-fs: not enough memory for "
1331ac27a0ecSDave Kleikamp 					"storing quotafile name.\n");
1332ac27a0ecSDave Kleikamp 				return 0;
1333ac27a0ecSDave Kleikamp 			}
1334ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1335ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1336ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1337617ba13bSMingming Cao 					"EXT4-fs: %s quota file already "
1338ac27a0ecSDave Kleikamp 					"specified.\n", QTYPE2NAME(qtype));
1339ac27a0ecSDave Kleikamp 				kfree(qname);
1340ac27a0ecSDave Kleikamp 				return 0;
1341ac27a0ecSDave Kleikamp 			}
1342ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1343ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1344ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1345617ba13bSMingming Cao 					"EXT4-fs: quotafile must be on "
1346ac27a0ecSDave Kleikamp 					"filesystem root.\n");
1347ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1348ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1349ac27a0ecSDave Kleikamp 				return 0;
1350ac27a0ecSDave Kleikamp 			}
1351ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1352ac27a0ecSDave Kleikamp 			break;
1353ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1354ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1355ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1356ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1357ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1358ac27a0ecSDave Kleikamp clear_qf_name:
1359dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1360dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1361dfc5d03fSJan Kara 			    sbi->s_qf_names[qtype]) {
1362617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change "
13632c8be6b2SJan Kara 					"journaled quota options when "
1364ac27a0ecSDave Kleikamp 					"quota turned on.\n");
1365ac27a0ecSDave Kleikamp 				return 0;
1366ac27a0ecSDave Kleikamp 			}
1367ac27a0ecSDave Kleikamp 			/*
1368ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1369ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1370ac27a0ecSDave Kleikamp 			 */
1371ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1372ac27a0ecSDave Kleikamp 			break;
1373ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1374dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1375dfc5d03fSJan Kara 			goto set_qf_format;
1376ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1377dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
1378dfc5d03fSJan Kara set_qf_format:
1379dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1380dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1381dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1382dfc5d03fSJan Kara 				printk(KERN_ERR "EXT4-fs: Cannot change "
1383dfc5d03fSJan Kara 					"journaled quota options when "
1384dfc5d03fSJan Kara 					"quota turned on.\n");
1385dfc5d03fSJan Kara 				return 0;
1386dfc5d03fSJan Kara 			}
1387dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1388ac27a0ecSDave Kleikamp 			break;
1389ac27a0ecSDave Kleikamp 		case Opt_quota:
1390ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1391ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1392ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1393ac27a0ecSDave Kleikamp 			break;
1394ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1395ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1396ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1397ac27a0ecSDave Kleikamp 			break;
1398ac27a0ecSDave Kleikamp 		case Opt_noquota:
1399ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1400617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change quota "
1401ac27a0ecSDave Kleikamp 					"options when quota turned on.\n");
1402ac27a0ecSDave Kleikamp 				return 0;
1403ac27a0ecSDave Kleikamp 			}
1404ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1405ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1406ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1407ac27a0ecSDave Kleikamp 			break;
1408ac27a0ecSDave Kleikamp #else
1409ac27a0ecSDave Kleikamp 		case Opt_quota:
1410ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1411ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1412cd59e7b9SJan Kara 			printk(KERN_ERR
1413cd59e7b9SJan Kara 				"EXT4-fs: quota options not supported.\n");
1414cd59e7b9SJan Kara 			break;
1415ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1416ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1417ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1418ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1419ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1420ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1421ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1422cd59e7b9SJan Kara 				"EXT4-fs: journaled quota options not "
1423ac27a0ecSDave Kleikamp 				"supported.\n");
1424ac27a0ecSDave Kleikamp 			break;
1425ac27a0ecSDave Kleikamp 		case Opt_noquota:
1426ac27a0ecSDave Kleikamp 			break;
1427ac27a0ecSDave Kleikamp #endif
1428ac27a0ecSDave Kleikamp 		case Opt_abort:
1429ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ABORT);
1430ac27a0ecSDave Kleikamp 			break;
1431ac27a0ecSDave Kleikamp 		case Opt_barrier:
1432ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1433ac27a0ecSDave Kleikamp 				return 0;
1434ac27a0ecSDave Kleikamp 			if (option)
1435ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1436ac27a0ecSDave Kleikamp 			else
1437ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1438ac27a0ecSDave Kleikamp 			break;
1439ac27a0ecSDave Kleikamp 		case Opt_ignore:
1440ac27a0ecSDave Kleikamp 			break;
1441ac27a0ecSDave Kleikamp 		case Opt_resize:
1442ac27a0ecSDave Kleikamp 			if (!is_remount) {
1443617ba13bSMingming Cao 				printk("EXT4-fs: resize option only available "
1444ac27a0ecSDave Kleikamp 					"for remount\n");
1445ac27a0ecSDave Kleikamp 				return 0;
1446ac27a0ecSDave Kleikamp 			}
1447ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1448ac27a0ecSDave Kleikamp 				return 0;
1449ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1450ac27a0ecSDave Kleikamp 			break;
1451ac27a0ecSDave Kleikamp 		case Opt_nobh:
1452ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1453ac27a0ecSDave Kleikamp 			break;
1454ac27a0ecSDave Kleikamp 		case Opt_bh:
1455ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1456ac27a0ecSDave Kleikamp 			break;
1457a86c6181SAlex Tomas 		case Opt_extents:
1458e4079a11SEric Sandeen 			if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1459e4079a11SEric Sandeen 					EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1460e4079a11SEric Sandeen 				ext4_warning(sb, __func__,
1461e4079a11SEric Sandeen 					"extents feature not enabled "
1462fde4d95aSTheodore Ts'o 					"on this filesystem, use tune2fs");
1463e4079a11SEric Sandeen 				return 0;
1464e4079a11SEric Sandeen 			}
1465a86c6181SAlex Tomas 			set_opt(sbi->s_mount_opt, EXTENTS);
1466a86c6181SAlex Tomas 			break;
14671e2462f9SMingming Cao 		case Opt_noextents:
1468c07651b5SAneesh Kumar K.V 			/*
1469c07651b5SAneesh Kumar K.V 			 * When e2fsprogs support resizing an already existing
1470c07651b5SAneesh Kumar K.V 			 * ext3 file system to greater than 2**32 we need to
1471c07651b5SAneesh Kumar K.V 			 * add support to block allocator to handle growing
1472c07651b5SAneesh Kumar K.V 			 * already existing block  mapped inode so that blocks
1473c07651b5SAneesh Kumar K.V 			 * allocated for them fall within 2**32
1474c07651b5SAneesh Kumar K.V 			 */
1475c07651b5SAneesh Kumar K.V 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1476c07651b5SAneesh Kumar K.V 			if (last_block  > 0xffffffffULL) {
1477c07651b5SAneesh Kumar K.V 				printk(KERN_ERR "EXT4-fs: Filesystem too "
1478c07651b5SAneesh Kumar K.V 						"large to mount with "
1479c07651b5SAneesh Kumar K.V 						"-o noextents options\n");
1480c07651b5SAneesh Kumar K.V 				return 0;
1481c07651b5SAneesh Kumar K.V 			}
14821e2462f9SMingming Cao 			clear_opt(sbi->s_mount_opt, EXTENTS);
14831e2462f9SMingming Cao 			break;
148425ec56b5SJean Noel Cordenner 		case Opt_i_version:
148525ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
148625ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
148725ec56b5SJean Noel Cordenner 			break;
1488dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1489dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1490dd919b98SAneesh Kumar K.V 			break;
1491c9de560dSAlex Tomas 		case Opt_stripe:
1492c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1493c9de560dSAlex Tomas 				return 0;
1494c9de560dSAlex Tomas 			if (option < 0)
1495c9de560dSAlex Tomas 				return 0;
1496c9de560dSAlex Tomas 			sbi->s_stripe = option;
1497c9de560dSAlex Tomas 			break;
149864769240SAlex Tomas 		case Opt_delalloc:
149964769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
150064769240SAlex Tomas 			break;
1501240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1502240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1503240799cdSTheodore Ts'o 				return 0;
1504240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1505240799cdSTheodore Ts'o 				return 0;
1506240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1507240799cdSTheodore Ts'o 			break;
1508ac27a0ecSDave Kleikamp 		default:
1509ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1510617ba13bSMingming Cao 			       "EXT4-fs: Unrecognized mount option \"%s\" "
1511ac27a0ecSDave Kleikamp 			       "or missing value\n", p);
1512ac27a0ecSDave Kleikamp 			return 0;
1513ac27a0ecSDave Kleikamp 		}
1514ac27a0ecSDave Kleikamp 	}
1515ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1516ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1517617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1518ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1519ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1520ac27a0ecSDave Kleikamp 
1521617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1522ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1523ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1524ac27a0ecSDave Kleikamp 
1525ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1526617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1527ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1528617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1529617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: old and new quota "
1530ac27a0ecSDave Kleikamp 					"format mixing.\n");
1531ac27a0ecSDave Kleikamp 			return 0;
1532ac27a0ecSDave Kleikamp 		}
1533ac27a0ecSDave Kleikamp 
1534ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
15352c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
1536ac27a0ecSDave Kleikamp 					"not specified.\n");
1537ac27a0ecSDave Kleikamp 			return 0;
1538ac27a0ecSDave Kleikamp 		}
1539ac27a0ecSDave Kleikamp 	} else {
1540ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
15412c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
15422c8be6b2SJan Kara 					"specified with no journaling "
1543ac27a0ecSDave Kleikamp 					"enabled.\n");
1544ac27a0ecSDave Kleikamp 			return 0;
1545ac27a0ecSDave Kleikamp 		}
1546ac27a0ecSDave Kleikamp 	}
1547ac27a0ecSDave Kleikamp #endif
1548ac27a0ecSDave Kleikamp 	return 1;
1549ac27a0ecSDave Kleikamp }
1550ac27a0ecSDave Kleikamp 
1551617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1552ac27a0ecSDave Kleikamp 			    int read_only)
1553ac27a0ecSDave Kleikamp {
1554617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1555ac27a0ecSDave Kleikamp 	int res = 0;
1556ac27a0ecSDave Kleikamp 
1557617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1558617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1559ac27a0ecSDave Kleikamp 		       "forcing read-only mode\n");
1560ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1561ac27a0ecSDave Kleikamp 	}
1562ac27a0ecSDave Kleikamp 	if (read_only)
1563ac27a0ecSDave Kleikamp 		return res;
1564617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1565617ba13bSMingming Cao 		printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1566ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1567617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1568ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1569617ba13bSMingming Cao 		       "EXT4-fs warning: mounting fs with errors, "
1570ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1571ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1572ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1573ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1574ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1575617ba13bSMingming Cao 		       "EXT4-fs warning: maximal mount count reached, "
1576ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1577ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1578ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1579ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1580ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1581617ba13bSMingming Cao 		       "EXT4-fs warning: checktime reached, "
1582ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
15830390131bSFrank Mayhar 	if (!sbi->s_journal)
1584216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1585ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1586617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1587e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1588ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1589617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
15900390131bSFrank Mayhar 	if (sbi->s_journal)
1591617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1592ac27a0ecSDave Kleikamp 
1593617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
1594ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1595a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1596ac27a0ecSDave Kleikamp 				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
1597ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1598ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1599617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1600617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1601ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1602ac27a0ecSDave Kleikamp 
16030390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
160405496769STheodore Ts'o 		printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
160505496769STheodore Ts'o 		       sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
160605496769STheodore Ts'o 		       "external", EXT4_SB(sb)->s_journal->j_devname);
16070390131bSFrank Mayhar 	} else {
16080390131bSFrank Mayhar 		printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
16090390131bSFrank Mayhar 	}
1610ac27a0ecSDave Kleikamp 	return res;
1611ac27a0ecSDave Kleikamp }
1612ac27a0ecSDave Kleikamp 
1613772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1614772cb7c8SJose R. Santos {
1615772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1616772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1617772cb7c8SJose R. Santos 	struct buffer_head *bh;
1618772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1619772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1620772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1621772cb7c8SJose R. Santos 	int i;
1622772cb7c8SJose R. Santos 
1623772cb7c8SJose R. Santos 	if (!sbi->s_es->s_log_groups_per_flex) {
1624772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1625772cb7c8SJose R. Santos 		return 1;
1626772cb7c8SJose R. Santos 	}
1627772cb7c8SJose R. Santos 
1628772cb7c8SJose R. Santos 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1629772cb7c8SJose R. Santos 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1630772cb7c8SJose R. Santos 
1631c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1632c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1633d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1634d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1635ec05e868SLi Zefan 	sbi->s_flex_groups = kzalloc(flex_group_count *
1636772cb7c8SJose R. Santos 				     sizeof(struct flex_groups), GFP_KERNEL);
1637772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1638ec05e868SLi Zefan 		printk(KERN_ERR "EXT4-fs: not enough memory for "
1639a9df9a49STheodore Ts'o 				"%u flex groups\n", flex_group_count);
1640772cb7c8SJose R. Santos 		goto failed;
1641772cb7c8SJose R. Santos 	}
1642772cb7c8SJose R. Santos 
1643772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
1644772cb7c8SJose R. Santos 		gdp = ext4_get_group_desc(sb, i, &bh);
1645772cb7c8SJose R. Santos 
1646772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
1647772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_inodes +=
1648560671a0SAneesh Kumar K.V 			ext4_free_inodes_count(sb, gdp);
1649772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_blocks +=
1650560671a0SAneesh Kumar K.V 			ext4_free_blks_count(sb, gdp);
1651772cb7c8SJose R. Santos 	}
1652772cb7c8SJose R. Santos 
1653772cb7c8SJose R. Santos 	return 1;
1654772cb7c8SJose R. Santos failed:
1655772cb7c8SJose R. Santos 	return 0;
1656772cb7c8SJose R. Santos }
1657772cb7c8SJose R. Santos 
1658717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1659717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1660717d50e4SAndreas Dilger {
1661717d50e4SAndreas Dilger 	__u16 crc = 0;
1662717d50e4SAndreas Dilger 
1663717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1664717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1665717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1666717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1667717d50e4SAndreas Dilger 
1668717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1669717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1670717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1671717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1672717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1673717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1674717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1675717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1676717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1677717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1678717d50e4SAndreas Dilger 					offset);
1679717d50e4SAndreas Dilger 	}
1680717d50e4SAndreas Dilger 
1681717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1682717d50e4SAndreas Dilger }
1683717d50e4SAndreas Dilger 
1684717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1685717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1686717d50e4SAndreas Dilger {
1687717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1688717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1689717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1690717d50e4SAndreas Dilger 		return 0;
1691717d50e4SAndreas Dilger 
1692717d50e4SAndreas Dilger 	return 1;
1693717d50e4SAndreas Dilger }
1694717d50e4SAndreas Dilger 
1695ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1696617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1697ac27a0ecSDave Kleikamp {
1698617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1699617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1700617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1701bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1702bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1703bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1704ce421581SJose R. Santos 	int flexbg_flag = 0;
1705fd2d4291SAvantika Mathur 	ext4_group_t i;
1706ac27a0ecSDave Kleikamp 
1707ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1708ce421581SJose R. Santos 		flexbg_flag = 1;
1709ce421581SJose R. Santos 
1710617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1711ac27a0ecSDave Kleikamp 
1712197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1713197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1714197cd65aSAkinobu Mita 
1715ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1716bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1717ac27a0ecSDave Kleikamp 		else
1718ac27a0ecSDave Kleikamp 			last_block = first_block +
1719617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1720ac27a0ecSDave Kleikamp 
17218fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
17222b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1723c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1724a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
17255128273aSEric Sesterhenn 			       "(block %llu)!\n", i, block_bitmap);
1726ac27a0ecSDave Kleikamp 			return 0;
1727ac27a0ecSDave Kleikamp 		}
17288fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
17292b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1730c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1731a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
17325128273aSEric Sesterhenn 			       "(block %llu)!\n", i, inode_bitmap);
1733ac27a0ecSDave Kleikamp 			return 0;
1734ac27a0ecSDave Kleikamp 		}
17358fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1736bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
17372b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1738c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1739a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
17405128273aSEric Sesterhenn 			       "(block %llu)!\n", i, inode_table);
1741ac27a0ecSDave Kleikamp 			return 0;
1742ac27a0ecSDave Kleikamp 		}
1743b5f10eedSEric Sandeen 		spin_lock(sb_bgl_lock(sbi, i));
1744717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1745c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1746a9df9a49STheodore Ts'o 			       "Checksum for group %u failed (%u!=%u)\n",
1747fd2d4291SAvantika Mathur 			       i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1748fd2d4291SAvantika Mathur 			       gdp)), le16_to_cpu(gdp->bg_checksum));
17497ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
17507ee1ec4cSLi Zefan 				spin_unlock(sb_bgl_lock(sbi, i));
1751717d50e4SAndreas Dilger 				return 0;
1752717d50e4SAndreas Dilger 			}
17537ee1ec4cSLi Zefan 		}
1754b5f10eedSEric Sandeen 		spin_unlock(sb_bgl_lock(sbi, i));
1755ce421581SJose R. Santos 		if (!flexbg_flag)
1756617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1757ac27a0ecSDave Kleikamp 	}
1758ac27a0ecSDave Kleikamp 
1759bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1760617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1761ac27a0ecSDave Kleikamp 	return 1;
1762ac27a0ecSDave Kleikamp }
1763ac27a0ecSDave Kleikamp 
1764617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1765ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1766ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1767ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1768ac27a0ecSDave Kleikamp  *
1769ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1770ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1771ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1772ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1773ac27a0ecSDave Kleikamp  *
1774ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1775ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1776ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1777617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1778ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1779ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1780ac27a0ecSDave Kleikamp  */
1781617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1782617ba13bSMingming Cao 				struct ext4_super_block *es)
1783ac27a0ecSDave Kleikamp {
1784ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1785ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1786ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1787ac27a0ecSDave Kleikamp 	int i;
1788ac27a0ecSDave Kleikamp #endif
1789ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1790ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1791ac27a0ecSDave Kleikamp 		return;
1792ac27a0ecSDave Kleikamp 	}
1793ac27a0ecSDave Kleikamp 
1794a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1795a8f48a95SEric Sandeen 		printk(KERN_ERR "EXT4-fs: write access "
1796a8f48a95SEric Sandeen 			"unavailable, skipping orphan cleanup.\n");
1797a8f48a95SEric Sandeen 		return;
1798a8f48a95SEric Sandeen 	}
1799a8f48a95SEric Sandeen 
1800617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1801ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1802ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1803ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1804ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1805ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1806ac27a0ecSDave Kleikamp 		return;
1807ac27a0ecSDave Kleikamp 	}
1808ac27a0ecSDave Kleikamp 
1809ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1810617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1811ac27a0ecSDave Kleikamp 		       sb->s_id);
1812ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1813ac27a0ecSDave Kleikamp 	}
1814ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1815ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1816ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1817ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1818ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1819617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1820617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1821ac27a0ecSDave Kleikamp 			if (ret < 0)
1822ac27a0ecSDave Kleikamp 				printk(KERN_ERR
18232c8be6b2SJan Kara 					"EXT4-fs: Cannot turn on journaled "
1824ac27a0ecSDave Kleikamp 					"quota: error %d\n", ret);
1825ac27a0ecSDave Kleikamp 		}
1826ac27a0ecSDave Kleikamp 	}
1827ac27a0ecSDave Kleikamp #endif
1828ac27a0ecSDave Kleikamp 
1829ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1830ac27a0ecSDave Kleikamp 		struct inode *inode;
1831ac27a0ecSDave Kleikamp 
183297bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
183397bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
1834ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1835ac27a0ecSDave Kleikamp 			break;
1836ac27a0ecSDave Kleikamp 		}
1837ac27a0ecSDave Kleikamp 
1838617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1839ac27a0ecSDave Kleikamp 		DQUOT_INIT(inode);
1840ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1841ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1842e5f8eab8STheodore Ts'o 				"%s: truncating inode %lu to %lld bytes\n",
184346e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
1844e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1845ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1846617ba13bSMingming Cao 			ext4_truncate(inode);
1847ac27a0ecSDave Kleikamp 			nr_truncates++;
1848ac27a0ecSDave Kleikamp 		} else {
1849ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1850ac27a0ecSDave Kleikamp 				"%s: deleting unreferenced inode %lu\n",
185146e665e9SHarvey Harrison 				__func__, inode->i_ino);
1852ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1853ac27a0ecSDave Kleikamp 				  inode->i_ino);
1854ac27a0ecSDave Kleikamp 			nr_orphans++;
1855ac27a0ecSDave Kleikamp 		}
1856ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1857ac27a0ecSDave Kleikamp 	}
1858ac27a0ecSDave Kleikamp 
1859ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1860ac27a0ecSDave Kleikamp 
1861ac27a0ecSDave Kleikamp 	if (nr_orphans)
1862617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1863ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_orphans));
1864ac27a0ecSDave Kleikamp 	if (nr_truncates)
1865617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1866ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_truncates));
1867ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1868ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1869ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1870ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
18716f28e087SJan Kara 			vfs_quota_off(sb, i, 0);
1872ac27a0ecSDave Kleikamp 	}
1873ac27a0ecSDave Kleikamp #endif
1874ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1875ac27a0ecSDave Kleikamp }
1876cd2291a4SEric Sandeen /*
1877cd2291a4SEric Sandeen  * Maximal extent format file size.
1878cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1879cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1880cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1881cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1882cd2291a4SEric Sandeen  *
1883cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1884cd2291a4SEric Sandeen  */
1885f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
1886cd2291a4SEric Sandeen {
1887cd2291a4SEric Sandeen 	loff_t res;
1888cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
1889cd2291a4SEric Sandeen 
1890cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
1891f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1892cd2291a4SEric Sandeen 		/*
1893b3a6ffe1SJens Axboe 		 * CONFIG_LBD is not enabled implies the inode
1894cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
1895cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
1896cd2291a4SEric Sandeen 		 */
1897cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
1898cd2291a4SEric Sandeen 
1899cd2291a4SEric Sandeen 		/* total blocks in file system block size */
1900cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
1901cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
1902cd2291a4SEric Sandeen 	}
1903cd2291a4SEric Sandeen 
1904cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
1905cd2291a4SEric Sandeen 	res = 1LL << 32;
1906cd2291a4SEric Sandeen 	res <<= blkbits;
1907cd2291a4SEric Sandeen 	res -= 1;
1908cd2291a4SEric Sandeen 
1909cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
1910cd2291a4SEric Sandeen 	if (res > upper_limit)
1911cd2291a4SEric Sandeen 		res = upper_limit;
1912cd2291a4SEric Sandeen 
1913cd2291a4SEric Sandeen 	return res;
1914cd2291a4SEric Sandeen }
1915ac27a0ecSDave Kleikamp 
1916ac27a0ecSDave Kleikamp /*
1917cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
19180fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
19190fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
1920ac27a0ecSDave Kleikamp  */
1921f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1922ac27a0ecSDave Kleikamp {
1923617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
19240fc1b451SAneesh Kumar K.V 	int meta_blocks;
19250fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
19260fc1b451SAneesh Kumar K.V 	/* This is calculated to be the largest file size for a
1927cd2291a4SEric Sandeen 	 * dense, bitmapped file such that the total number of
1928ac27a0ecSDave Kleikamp 	 * sectors in the file, including data and all indirect blocks,
19290fc1b451SAneesh Kumar K.V 	 * does not exceed 2^48 -1
19300fc1b451SAneesh Kumar K.V 	 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
19310fc1b451SAneesh Kumar K.V 	 * total number of  512 bytes blocks of the file
19320fc1b451SAneesh Kumar K.V 	 */
19330fc1b451SAneesh Kumar K.V 
1934f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
19350fc1b451SAneesh Kumar K.V 		/*
1936b3a6ffe1SJens Axboe 		 * !has_huge_files or CONFIG_LBD is not enabled
1937f287a1a5STheodore Ts'o 		 * implies the inode i_block represent total blocks in
1938f287a1a5STheodore Ts'o 		 * 512 bytes 32 == size of vfs inode i_blocks * 8
19390fc1b451SAneesh Kumar K.V 		 */
19400fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
19410fc1b451SAneesh Kumar K.V 
19420fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
19430fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
19440fc1b451SAneesh Kumar K.V 
19450fc1b451SAneesh Kumar K.V 	} else {
19468180a562SAneesh Kumar K.V 		/*
19478180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
19488180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
19498180a562SAneesh Kumar K.V 		 * represent total number of blocks in
19508180a562SAneesh Kumar K.V 		 * file system block size
19518180a562SAneesh Kumar K.V 		 */
19520fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
19530fc1b451SAneesh Kumar K.V 
19540fc1b451SAneesh Kumar K.V 	}
19550fc1b451SAneesh Kumar K.V 
19560fc1b451SAneesh Kumar K.V 	/* indirect blocks */
19570fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
19580fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
19590fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
19600fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
19610fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
19620fc1b451SAneesh Kumar K.V 
19630fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
19640fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
1965ac27a0ecSDave Kleikamp 
1966ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
1967ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
1968ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
1969ac27a0ecSDave Kleikamp 	res <<= bits;
1970ac27a0ecSDave Kleikamp 	if (res > upper_limit)
1971ac27a0ecSDave Kleikamp 		res = upper_limit;
19720fc1b451SAneesh Kumar K.V 
19730fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
19740fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
19750fc1b451SAneesh Kumar K.V 
1976ac27a0ecSDave Kleikamp 	return res;
1977ac27a0ecSDave Kleikamp }
1978ac27a0ecSDave Kleikamp 
1979617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
198070bbb3e0SAndrew Morton 				ext4_fsblk_t logical_sb_block, int nr)
1981ac27a0ecSDave Kleikamp {
1982617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1983fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
1984ac27a0ecSDave Kleikamp 	int has_super = 0;
1985ac27a0ecSDave Kleikamp 
1986ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1987ac27a0ecSDave Kleikamp 
1988617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1989ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
199070bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
1991ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
1992617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
1993ac27a0ecSDave Kleikamp 		has_super = 1;
1994617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
1995ac27a0ecSDave Kleikamp }
1996ac27a0ecSDave Kleikamp 
1997c9de560dSAlex Tomas /**
1998c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
1999c9de560dSAlex Tomas  * @sbi: In memory super block info
2000c9de560dSAlex Tomas  *
2001c9de560dSAlex Tomas  * If we have specified it via mount option, then
2002c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2003c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2004c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2005c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2006c9de560dSAlex Tomas  *
2007c9de560dSAlex Tomas  */
2008c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2009c9de560dSAlex Tomas {
2010c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2011c9de560dSAlex Tomas 	unsigned long stripe_width =
2012c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2013c9de560dSAlex Tomas 
2014c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2015c9de560dSAlex Tomas 		return sbi->s_stripe;
2016c9de560dSAlex Tomas 
2017c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
2018c9de560dSAlex Tomas 		return stripe_width;
2019c9de560dSAlex Tomas 
2020c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
2021c9de560dSAlex Tomas 		return stride;
2022c9de560dSAlex Tomas 
2023c9de560dSAlex Tomas 	return 0;
2024c9de560dSAlex Tomas }
2025ac27a0ecSDave Kleikamp 
2026617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
20277477827fSAneesh Kumar K.V 				__releases(kernel_lock)
20287477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
20291d03ec98SAneesh Kumar K.V 
2030ac27a0ecSDave Kleikamp {
2031ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2032617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
2033617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2034617ba13bSMingming Cao 	ext4_fsblk_t block;
2035617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
203670bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
2037ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
2038ac27a0ecSDave Kleikamp 	unsigned int journal_inum = 0;
2039ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
2040ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
2041ac27a0ecSDave Kleikamp 	struct inode *root;
20429f6200bbSTheodore Ts'o 	char *cp;
20430390131bSFrank Mayhar 	const char *descr;
20441d1fe1eeSDavid Howells 	int ret = -EINVAL;
2045ac27a0ecSDave Kleikamp 	int blocksize;
2046ac27a0ecSDave Kleikamp 	int db_count;
2047ac27a0ecSDave Kleikamp 	int i;
2048f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
20493a06d778SAneesh Kumar K.V 	int features;
2050bd81d8eeSLaurent Vivier 	__u64 blocks_count;
2051833f4077SPeter Zijlstra 	int err;
2052ac27a0ecSDave Kleikamp 
2053ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2054ac27a0ecSDave Kleikamp 	if (!sbi)
2055ac27a0ecSDave Kleikamp 		return -ENOMEM;
2056ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
2057ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
2058617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
2059617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
2060240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2061d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
2062ac27a0ecSDave Kleikamp 
2063ac27a0ecSDave Kleikamp 	unlock_kernel();
2064ac27a0ecSDave Kleikamp 
20659f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
20669f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
20679f6200bbSTheodore Ts'o 		*cp = '!';
20689f6200bbSTheodore Ts'o 
2069617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2070ac27a0ecSDave Kleikamp 	if (!blocksize) {
2071617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
2072ac27a0ecSDave Kleikamp 		goto out_fail;
2073ac27a0ecSDave Kleikamp 	}
2074ac27a0ecSDave Kleikamp 
2075ac27a0ecSDave Kleikamp 	/*
2076617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
2077ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
2078ac27a0ecSDave Kleikamp 	 */
2079617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
208070bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
208170bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
2082ac27a0ecSDave Kleikamp 	} else {
208370bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
2084ac27a0ecSDave Kleikamp 	}
2085ac27a0ecSDave Kleikamp 
208670bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
2087617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
2088ac27a0ecSDave Kleikamp 		goto out_fail;
2089ac27a0ecSDave Kleikamp 	}
2090ac27a0ecSDave Kleikamp 	/*
2091ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
2092617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
2093ac27a0ecSDave Kleikamp 	 */
2094617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2095ac27a0ecSDave Kleikamp 	sbi->s_es = es;
2096ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
2097617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
2098617ba13bSMingming Cao 		goto cantfind_ext4;
2099ac27a0ecSDave Kleikamp 
2100ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
2101ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2102617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
2103ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
2104617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2105ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
2106617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
2107ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
210803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
2109617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2110ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
21112e7842b8SHugh Dickins #endif
211203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
2113617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
2114ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
21152e7842b8SHugh Dickins #endif
2116617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2117617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2118617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2119617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2120617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2121617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2122ac27a0ecSDave Kleikamp 
2123617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2124ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2125bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2126ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
2127bb4f397aSAneesh Kumar K.V 	else
2128bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
2129ac27a0ecSDave Kleikamp 
2130ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2131ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
213230773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
213330773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
213430773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2135ac27a0ecSDave Kleikamp 
2136ac27a0ecSDave Kleikamp 	set_opt(sbi->s_mount_opt, RESERVATION);
2137571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
2138ac27a0ecSDave Kleikamp 
21391e2462f9SMingming Cao 	/*
21401e2462f9SMingming Cao 	 * turn on extents feature by default in ext4 filesystem
2141e4079a11SEric Sandeen 	 * only if feature flag already set by mkfs or tune2fs.
2142e4079a11SEric Sandeen 	 * Use -o noextents to turn it off
21431e2462f9SMingming Cao 	 */
2144e4079a11SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
21451e2462f9SMingming Cao 		set_opt(sbi->s_mount_opt, EXTENTS);
2146e4079a11SEric Sandeen 	else
2147e4079a11SEric Sandeen 		ext4_warning(sb, __func__,
2148e4079a11SEric Sandeen 			"extents feature not enabled on this filesystem, "
2149fde4d95aSTheodore Ts'o 			"use tune2fs.");
21501e2462f9SMingming Cao 
2151dd919b98SAneesh Kumar K.V 	/*
2152dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
2153dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
2154dd919b98SAneesh Kumar K.V 	 */
2155dd919b98SAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, DELALLOC);
2156dd919b98SAneesh Kumar K.V 
2157dd919b98SAneesh Kumar K.V 
2158ac27a0ecSDave Kleikamp 	if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2159ac27a0ecSDave Kleikamp 			   NULL, 0))
2160ac27a0ecSDave Kleikamp 		goto failed_mount;
2161ac27a0ecSDave Kleikamp 
2162ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2163617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2164ac27a0ecSDave Kleikamp 
2165617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2166617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2167617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2168617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2169ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
2170617ba13bSMingming Cao 		       "EXT4-fs warning: feature flags set on rev 0 fs, "
2171ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
2172469108ffSTheodore Tso 
2173469108ffSTheodore Tso 	/*
2174ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2175ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2176ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2177ac27a0ecSDave Kleikamp 	 */
2178617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2179ac27a0ecSDave Kleikamp 	if (features) {
2180617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
21813a06d778SAneesh Kumar K.V 		       "unsupported optional features (%x).\n", sb->s_id,
21823a06d778SAneesh Kumar K.V 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
21833a06d778SAneesh Kumar K.V 			~EXT4_FEATURE_INCOMPAT_SUPP));
2184ac27a0ecSDave Kleikamp 		goto failed_mount;
2185ac27a0ecSDave Kleikamp 	}
2186617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2187ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
2188617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
21893a06d778SAneesh Kumar K.V 		       "unsupported optional features (%x).\n", sb->s_id,
21903a06d778SAneesh Kumar K.V 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
21913a06d778SAneesh Kumar K.V 			~EXT4_FEATURE_RO_COMPAT_SUPP));
2192ac27a0ecSDave Kleikamp 		goto failed_mount;
2193ac27a0ecSDave Kleikamp 	}
2194f287a1a5STheodore Ts'o 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2195f287a1a5STheodore Ts'o 				    EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2196f287a1a5STheodore Ts'o 	if (has_huge_files) {
21970fc1b451SAneesh Kumar K.V 		/*
21980fc1b451SAneesh Kumar K.V 		 * Large file size enabled file system can only be
2199b3a6ffe1SJens Axboe 		 * mount if kernel is build with CONFIG_LBD
22000fc1b451SAneesh Kumar K.V 		 */
22010fc1b451SAneesh Kumar K.V 		if (sizeof(root->i_blocks) < sizeof(u64) &&
22020fc1b451SAneesh Kumar K.V 				!(sb->s_flags & MS_RDONLY)) {
22030fc1b451SAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
22040fc1b451SAneesh Kumar K.V 					"files cannot be mounted read-write "
2205b3a6ffe1SJens Axboe 					"without CONFIG_LBD.\n", sb->s_id);
22060fc1b451SAneesh Kumar K.V 			goto failed_mount;
22070fc1b451SAneesh Kumar K.V 		}
22080fc1b451SAneesh Kumar K.V 	}
2209ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2210ac27a0ecSDave Kleikamp 
2211617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2212617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2213ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2214617ba13bSMingming Cao 		       "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2215ac27a0ecSDave Kleikamp 		       blocksize, sb->s_id);
2216ac27a0ecSDave Kleikamp 		goto failed_mount;
2217ac27a0ecSDave Kleikamp 	}
2218ac27a0ecSDave Kleikamp 
2219ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2220ce40733cSAneesh Kumar K.V 
2221ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2222ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2223ce40733cSAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2224ce40733cSAneesh Kumar K.V 					blocksize);
2225ac27a0ecSDave Kleikamp 			goto failed_mount;
2226ac27a0ecSDave Kleikamp 		}
2227ac27a0ecSDave Kleikamp 
2228ac27a0ecSDave Kleikamp 		brelse(bh);
222970bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
223070bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
223170bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2232ac27a0ecSDave Kleikamp 		if (!bh) {
2233ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2234617ba13bSMingming Cao 			       "EXT4-fs: Can't read superblock on 2nd try.\n");
2235ac27a0ecSDave Kleikamp 			goto failed_mount;
2236ac27a0ecSDave Kleikamp 		}
2237617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2238ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2239617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2240ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2241617ba13bSMingming Cao 			       "EXT4-fs: Magic mismatch, very weird !\n");
2242ac27a0ecSDave Kleikamp 			goto failed_mount;
2243ac27a0ecSDave Kleikamp 		}
2244ac27a0ecSDave Kleikamp 	}
2245ac27a0ecSDave Kleikamp 
2246f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2247f287a1a5STheodore Ts'o 						      has_huge_files);
2248f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2249ac27a0ecSDave Kleikamp 
2250617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2251617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2252617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2253ac27a0ecSDave Kleikamp 	} else {
2254ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2255ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2256617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
22571330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2258ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2259ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2260617ba13bSMingming Cao 			       "EXT4-fs: unsupported inode size: %d\n",
2261ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2262ac27a0ecSDave Kleikamp 			goto failed_mount;
2263ac27a0ecSDave Kleikamp 		}
2264ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2265ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2266ac27a0ecSDave Kleikamp 	}
22670d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
22680d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
22698fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
22700d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2271d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
22720d1ee42fSAlexandre Ratchov 			printk(KERN_ERR
22738fadc143SAlexandre Ratchov 			       "EXT4-fs: unsupported descriptor size %lu\n",
22740d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
22750d1ee42fSAlexandre Ratchov 			goto failed_mount;
22760d1ee42fSAlexandre Ratchov 		}
22770d1ee42fSAlexandre Ratchov 	} else
22780d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2279ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2280ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2281b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2282617ba13bSMingming Cao 		goto cantfind_ext4;
2283617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2284ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2285617ba13bSMingming Cao 		goto cantfind_ext4;
2286ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2287ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
22880d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2289ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2290ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2291e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2292e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2293ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2294ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2295ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2296f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
2297f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
2298f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2299f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2300f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
2301f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2302f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2303f99b2589STheodore Ts'o #else
2304f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2305f99b2589STheodore Ts'o #endif
2306f99b2589STheodore Ts'o 		sb->s_dirt = 1;
2307f99b2589STheodore Ts'o 	}
2308ac27a0ecSDave Kleikamp 
2309ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2310ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2311617ba13bSMingming Cao 		       "EXT4-fs: #blocks per group too big: %lu\n",
2312ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2313ac27a0ecSDave Kleikamp 		goto failed_mount;
2314ac27a0ecSDave Kleikamp 	}
2315ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2316ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2317617ba13bSMingming Cao 		       "EXT4-fs: #inodes per group too big: %lu\n",
2318ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2319ac27a0ecSDave Kleikamp 		goto failed_mount;
2320ac27a0ecSDave Kleikamp 	}
2321ac27a0ecSDave Kleikamp 
2322bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
2323ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2324617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2325ac27a0ecSDave Kleikamp 			" too large to mount safely\n", sb->s_id);
2326ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
2327617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2328ac27a0ecSDave Kleikamp 					"enabled\n");
2329ac27a0ecSDave Kleikamp 		goto failed_mount;
2330ac27a0ecSDave Kleikamp 	}
2331ac27a0ecSDave Kleikamp 
2332617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2333617ba13bSMingming Cao 		goto cantfind_ext4;
2334e7c95593SEric Sandeen 
2335e7c95593SEric Sandeen 	/* ensure blocks_count calculation below doesn't sign-extend */
2336e7c95593SEric Sandeen 	if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2337e7c95593SEric Sandeen 	    le32_to_cpu(es->s_first_data_block) + 1) {
2338e7c95593SEric Sandeen 		printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2339e7c95593SEric Sandeen 		       "first data block %u, blocks per group %lu\n",
2340e7c95593SEric Sandeen 			ext4_blocks_count(es),
2341e7c95593SEric Sandeen 			le32_to_cpu(es->s_first_data_block),
2342e7c95593SEric Sandeen 			EXT4_BLOCKS_PER_GROUP(sb));
2343e7c95593SEric Sandeen 		goto failed_mount;
2344e7c95593SEric Sandeen 	}
2345bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2346bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2347bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2348bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2349bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2350617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2351617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2352ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2353ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2354ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2355617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: not enough memory\n");
2356ac27a0ecSDave Kleikamp 		goto failed_mount;
2357ac27a0ecSDave Kleikamp 	}
2358ac27a0ecSDave Kleikamp 
23593244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
23609f6200bbSTheodore Ts'o 	if (ext4_proc_root)
23619f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
23629f6200bbSTheodore Ts'o 
2363240799cdSTheodore Ts'o 	if (sbi->s_proc)
2364240799cdSTheodore Ts'o 		proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2365240799cdSTheodore Ts'o 				 &ext4_ui_proc_fops,
2366240799cdSTheodore Ts'o 				 &sbi->s_inode_readahead_blks);
23673244fcb1SAlexander Beregalov #endif
2368240799cdSTheodore Ts'o 
2369ac27a0ecSDave Kleikamp 	bgl_lock_init(&sbi->s_blockgroup_lock);
2370ac27a0ecSDave Kleikamp 
2371ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
237270bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2373ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2374ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2375617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: "
2376ac27a0ecSDave Kleikamp 			       "can't read group descriptor %d\n", i);
2377ac27a0ecSDave Kleikamp 			db_count = i;
2378ac27a0ecSDave Kleikamp 			goto failed_mount2;
2379ac27a0ecSDave Kleikamp 		}
2380ac27a0ecSDave Kleikamp 	}
2381617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2382617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2383ac27a0ecSDave Kleikamp 		goto failed_mount2;
2384ac27a0ecSDave Kleikamp 	}
2385772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2386772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2387772cb7c8SJose R. Santos 			printk(KERN_ERR
2388772cb7c8SJose R. Santos 			       "EXT4-fs: unable to initialize "
2389772cb7c8SJose R. Santos 			       "flex_bg meta info!\n");
2390772cb7c8SJose R. Santos 			goto failed_mount2;
2391772cb7c8SJose R. Santos 		}
2392772cb7c8SJose R. Santos 
2393ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2394ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2395ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2396ac27a0ecSDave Kleikamp 
2397833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2398617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2399833f4077SPeter Zijlstra 	if (!err) {
2400833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2401617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2402833f4077SPeter Zijlstra 	}
2403833f4077SPeter Zijlstra 	if (!err) {
2404833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2405617ba13bSMingming Cao 				ext4_count_dirs(sb));
2406833f4077SPeter Zijlstra 	}
24076bc6e63fSAneesh Kumar K.V 	if (!err) {
24086bc6e63fSAneesh Kumar K.V 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
24096bc6e63fSAneesh Kumar K.V 	}
2410833f4077SPeter Zijlstra 	if (err) {
2411833f4077SPeter Zijlstra 		printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2412833f4077SPeter Zijlstra 		goto failed_mount3;
2413833f4077SPeter Zijlstra 	}
2414ac27a0ecSDave Kleikamp 
2415c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
2416c9de560dSAlex Tomas 
2417ac27a0ecSDave Kleikamp 	/*
2418ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2419ac27a0ecSDave Kleikamp 	 */
2420617ba13bSMingming Cao 	sb->s_op = &ext4_sops;
2421617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2422617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2423ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2424617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2425617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2426ac27a0ecSDave Kleikamp #endif
2427ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2428ac27a0ecSDave Kleikamp 
2429ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2430ac27a0ecSDave Kleikamp 
2431ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2432617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2433617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2434ac27a0ecSDave Kleikamp 
2435ac27a0ecSDave Kleikamp 	/*
2436ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2437ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2438ac27a0ecSDave Kleikamp 	 */
2439ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2440617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2441617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2442ac27a0ecSDave Kleikamp 			goto failed_mount3;
2443624080edSTheodore Ts'o 		if (!(sb->s_flags & MS_RDONLY) &&
2444624080edSTheodore Ts'o 		    EXT4_SB(sb)->s_journal->j_failed_commit) {
2445624080edSTheodore Ts'o 			printk(KERN_CRIT "EXT4-fs error (device %s): "
2446624080edSTheodore Ts'o 			       "ext4_fill_super: Journal transaction "
2447624080edSTheodore Ts'o 			       "%u is corrupt\n", sb->s_id,
2448624080edSTheodore Ts'o 			       EXT4_SB(sb)->s_journal->j_failed_commit);
2449624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_RO)) {
2450624080edSTheodore Ts'o 				printk(KERN_CRIT
2451624080edSTheodore Ts'o 				       "Mounting filesystem read-only\n");
2452624080edSTheodore Ts'o 				sb->s_flags |= MS_RDONLY;
2453624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2454624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2455624080edSTheodore Ts'o 			}
2456624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_PANIC)) {
2457624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2458624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2459624080edSTheodore Ts'o 				ext4_commit_super(sb, es, 1);
2460624080edSTheodore Ts'o 				goto failed_mount4;
2461624080edSTheodore Ts'o 			}
2462624080edSTheodore Ts'o 		}
2463ac27a0ecSDave Kleikamp 	} else if (journal_inum) {
2464617ba13bSMingming Cao 		if (ext4_create_journal(sb, es, journal_inum))
2465ac27a0ecSDave Kleikamp 			goto failed_mount3;
24660390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
24670390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
24680390131bSFrank Mayhar 		printk(KERN_ERR "EXT4-fs: required journal recovery "
24690390131bSFrank Mayhar 		       "suppressed and not mounted read-only\n");
24700390131bSFrank Mayhar 		goto failed_mount4;
2471ac27a0ecSDave Kleikamp 	} else {
24720390131bSFrank Mayhar 		clear_opt(sbi->s_mount_opt, DATA_FLAGS);
24730390131bSFrank Mayhar 		set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
24740390131bSFrank Mayhar 		sbi->s_journal = NULL;
24750390131bSFrank Mayhar 		needs_recovery = 0;
24760390131bSFrank Mayhar 		goto no_journal;
2477ac27a0ecSDave Kleikamp 	}
2478ac27a0ecSDave Kleikamp 
2479eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2480eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2481eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2482eb40a09cSJose R. Santos 		printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2483eb40a09cSJose R. Santos 		goto failed_mount4;
2484eb40a09cSJose R. Santos 	}
2485eb40a09cSJose R. Santos 
2486818d276cSGirish Shilamkar 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2487818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2488818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2489818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2490818d276cSGirish Shilamkar 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2491818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2492818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2493818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2494818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2495818d276cSGirish Shilamkar 	} else {
2496818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal,
2497818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2498818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2499818d276cSGirish Shilamkar 	}
2500818d276cSGirish Shilamkar 
2501ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2502ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2503ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2504ac27a0ecSDave Kleikamp 	case 0:
2505ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
250663f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
250763f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
250863f57933SAndrew Morton 		 */
2509dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2510dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2511ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2512ac27a0ecSDave Kleikamp 		else
2513ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2514ac27a0ecSDave Kleikamp 		break;
2515ac27a0ecSDave Kleikamp 
2516617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2517617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2518dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2519dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2520617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: Journal does not support "
2521ac27a0ecSDave Kleikamp 			       "requested data journaling mode\n");
2522ac27a0ecSDave Kleikamp 			goto failed_mount4;
2523ac27a0ecSDave Kleikamp 		}
2524ac27a0ecSDave Kleikamp 	default:
2525ac27a0ecSDave Kleikamp 		break;
2526ac27a0ecSDave Kleikamp 	}
2527ac27a0ecSDave Kleikamp 
25280390131bSFrank Mayhar no_journal:
25290390131bSFrank Mayhar 
2530ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2531617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2532617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2533ac27a0ecSDave Kleikamp 				"its supported only with writeback mode\n");
2534ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2535ac27a0ecSDave Kleikamp 		}
2536ac27a0ecSDave Kleikamp 	}
2537ac27a0ecSDave Kleikamp 	/*
2538dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2539ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2540ac27a0ecSDave Kleikamp 	 */
2541ac27a0ecSDave Kleikamp 
25421d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
25431d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2544617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: get root inode failed\n");
25451d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2546ac27a0ecSDave Kleikamp 		goto failed_mount4;
2547ac27a0ecSDave Kleikamp 	}
2548ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
25491d1fe1eeSDavid Howells 		iput(root);
2550617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2551ac27a0ecSDave Kleikamp 		goto failed_mount4;
2552ac27a0ecSDave Kleikamp 	}
25531d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
25541d1fe1eeSDavid Howells 	if (!sb->s_root) {
25551d1fe1eeSDavid Howells 		printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
25561d1fe1eeSDavid Howells 		iput(root);
25571d1fe1eeSDavid Howells 		ret = -ENOMEM;
25581d1fe1eeSDavid Howells 		goto failed_mount4;
25591d1fe1eeSDavid Howells 	}
2560ac27a0ecSDave Kleikamp 
2561617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2562ef7f3835SKalpak Shah 
2563ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2564ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2565ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2566ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2567ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2568ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2569ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2570ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2571ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2572ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2573ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2574ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2575ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2576ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2577ef7f3835SKalpak Shah 		}
2578ef7f3835SKalpak Shah 	}
2579ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2580ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2581ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2582ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2583ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2584ef7f3835SKalpak Shah 		printk(KERN_INFO "EXT4-fs: required extra inode space not"
2585ef7f3835SKalpak Shah 			"available.\n");
2586ef7f3835SKalpak Shah 	}
2587ef7f3835SKalpak Shah 
2588c2774d84SAneesh Kumar K.V 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2589c2774d84SAneesh Kumar K.V 		printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2590c2774d84SAneesh Kumar K.V 				"requested data journaling mode\n");
2591c2774d84SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
2592c2774d84SAneesh Kumar K.V 	} else if (test_opt(sb, DELALLOC))
2593c2774d84SAneesh Kumar K.V 		printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2594c2774d84SAneesh Kumar K.V 
2595c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
2596c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
2597c2774d84SAneesh Kumar K.V 	if (err) {
2598c2774d84SAneesh Kumar K.V 		printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2599c2774d84SAneesh Kumar K.V 		       err);
2600c2774d84SAneesh Kumar K.V 		goto failed_mount4;
2601c2774d84SAneesh Kumar K.V 	}
2602c2774d84SAneesh Kumar K.V 
2603ac27a0ecSDave Kleikamp 	/*
2604ac27a0ecSDave Kleikamp 	 * akpm: core read_super() calls in here with the superblock locked.
2605ac27a0ecSDave Kleikamp 	 * That deadlocks, because orphan cleanup needs to lock the superblock
2606ac27a0ecSDave Kleikamp 	 * in numerous places.  Here we just pop the lock - it's relatively
2607ac27a0ecSDave Kleikamp 	 * harmless, because we are now ready to accept write_super() requests,
2608ac27a0ecSDave Kleikamp 	 * and aviro says that's the only reason for hanging onto the
2609ac27a0ecSDave Kleikamp 	 * superblock lock.
2610ac27a0ecSDave Kleikamp 	 */
2611617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2612617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2613617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
26140390131bSFrank Mayhar 	if (needs_recovery) {
2615617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2616617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
26170390131bSFrank Mayhar 	}
26180390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
26190390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
26200390131bSFrank Mayhar 			descr = " journalled data mode";
26210390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
26220390131bSFrank Mayhar 			descr = " ordered data mode";
26230390131bSFrank Mayhar 		else
26240390131bSFrank Mayhar 			descr = " writeback data mode";
26250390131bSFrank Mayhar 	} else
26260390131bSFrank Mayhar 		descr = "out journal";
26270390131bSFrank Mayhar 
26280390131bSFrank Mayhar 	printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
26290390131bSFrank Mayhar 	       sb->s_id, descr);
2630ac27a0ecSDave Kleikamp 
2631ac27a0ecSDave Kleikamp 	lock_kernel();
2632ac27a0ecSDave Kleikamp 	return 0;
2633ac27a0ecSDave Kleikamp 
2634617ba13bSMingming Cao cantfind_ext4:
2635ac27a0ecSDave Kleikamp 	if (!silent)
2636617ba13bSMingming Cao 		printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2637ac27a0ecSDave Kleikamp 		       sb->s_id);
2638ac27a0ecSDave Kleikamp 	goto failed_mount;
2639ac27a0ecSDave Kleikamp 
2640ac27a0ecSDave Kleikamp failed_mount4:
26410390131bSFrank Mayhar 	printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
26420390131bSFrank Mayhar 	if (sbi->s_journal) {
2643dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
264447b4a50bSJan Kara 		sbi->s_journal = NULL;
26450390131bSFrank Mayhar 	}
2646ac27a0ecSDave Kleikamp failed_mount3:
2647ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2648ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2649ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
26506bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2651ac27a0ecSDave Kleikamp failed_mount2:
2652ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2653ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2654ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2655ac27a0ecSDave Kleikamp failed_mount:
2656240799cdSTheodore Ts'o 	if (sbi->s_proc) {
2657240799cdSTheodore Ts'o 		remove_proc_entry("inode_readahead_blks", sbi->s_proc);
26589f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
2659240799cdSTheodore Ts'o 	}
2660ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2661ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2662ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2663ac27a0ecSDave Kleikamp #endif
2664617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2665ac27a0ecSDave Kleikamp 	brelse(bh);
2666ac27a0ecSDave Kleikamp out_fail:
2667ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2668ac27a0ecSDave Kleikamp 	kfree(sbi);
2669ac27a0ecSDave Kleikamp 	lock_kernel();
26701d1fe1eeSDavid Howells 	return ret;
2671ac27a0ecSDave Kleikamp }
2672ac27a0ecSDave Kleikamp 
2673ac27a0ecSDave Kleikamp /*
2674ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2675ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2676ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2677ac27a0ecSDave Kleikamp  */
2678617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2679ac27a0ecSDave Kleikamp {
2680617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2681ac27a0ecSDave Kleikamp 
2682ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
268330773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
268430773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
2685ac27a0ecSDave Kleikamp 
2686ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2687ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2688dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2689ac27a0ecSDave Kleikamp 	else
2690dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
26915bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
26925bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
26935bf5683aSHidehiro Kawai 	else
26945bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2695ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2696ac27a0ecSDave Kleikamp }
2697ac27a0ecSDave Kleikamp 
2698617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2699ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2700ac27a0ecSDave Kleikamp {
2701ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2702ac27a0ecSDave Kleikamp 	journal_t *journal;
2703ac27a0ecSDave Kleikamp 
27040390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
27050390131bSFrank Mayhar 
2706ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2707ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2708ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2709ac27a0ecSDave Kleikamp 
27101d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
27111d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
2712617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: no journal found.\n");
2713ac27a0ecSDave Kleikamp 		return NULL;
2714ac27a0ecSDave Kleikamp 	}
2715ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2716ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2717ac27a0ecSDave Kleikamp 		iput(journal_inode);
2718617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2719ac27a0ecSDave Kleikamp 		return NULL;
2720ac27a0ecSDave Kleikamp 	}
2721ac27a0ecSDave Kleikamp 
2722e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2723ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
27241d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
2725617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2726ac27a0ecSDave Kleikamp 		iput(journal_inode);
2727ac27a0ecSDave Kleikamp 		return NULL;
2728ac27a0ecSDave Kleikamp 	}
2729ac27a0ecSDave Kleikamp 
2730dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
2731ac27a0ecSDave Kleikamp 	if (!journal) {
2732617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2733ac27a0ecSDave Kleikamp 		iput(journal_inode);
2734ac27a0ecSDave Kleikamp 		return NULL;
2735ac27a0ecSDave Kleikamp 	}
2736ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2737617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2738ac27a0ecSDave Kleikamp 	return journal;
2739ac27a0ecSDave Kleikamp }
2740ac27a0ecSDave Kleikamp 
2741617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
2742ac27a0ecSDave Kleikamp 				       dev_t j_dev)
2743ac27a0ecSDave Kleikamp {
2744ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2745ac27a0ecSDave Kleikamp 	journal_t *journal;
2746617ba13bSMingming Cao 	ext4_fsblk_t start;
2747617ba13bSMingming Cao 	ext4_fsblk_t len;
2748ac27a0ecSDave Kleikamp 	int hblock, blocksize;
2749617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
2750ac27a0ecSDave Kleikamp 	unsigned long offset;
2751617ba13bSMingming Cao 	struct ext4_super_block *es;
2752ac27a0ecSDave Kleikamp 	struct block_device *bdev;
2753ac27a0ecSDave Kleikamp 
27540390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
27550390131bSFrank Mayhar 
2756617ba13bSMingming Cao 	bdev = ext4_blkdev_get(j_dev);
2757ac27a0ecSDave Kleikamp 	if (bdev == NULL)
2758ac27a0ecSDave Kleikamp 		return NULL;
2759ac27a0ecSDave Kleikamp 
2760ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
2761ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2762617ba13bSMingming Cao 			"EXT4: failed to claim external journal device.\n");
27639a1c3542SAl Viro 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2764ac27a0ecSDave Kleikamp 		return NULL;
2765ac27a0ecSDave Kleikamp 	}
2766ac27a0ecSDave Kleikamp 
2767ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
2768ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(bdev);
2769ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
2770ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2771617ba13bSMingming Cao 			"EXT4-fs: blocksize too small for journal device.\n");
2772ac27a0ecSDave Kleikamp 		goto out_bdev;
2773ac27a0ecSDave Kleikamp 	}
2774ac27a0ecSDave Kleikamp 
2775617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2776617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2777ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
2778ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
2779617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2780ac27a0ecSDave Kleikamp 		       "external journal\n");
2781ac27a0ecSDave Kleikamp 		goto out_bdev;
2782ac27a0ecSDave Kleikamp 	}
2783ac27a0ecSDave Kleikamp 
2784617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2785617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2786ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
2787617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2788617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: external journal has "
2789ac27a0ecSDave Kleikamp 					"bad superblock\n");
2790ac27a0ecSDave Kleikamp 		brelse(bh);
2791ac27a0ecSDave Kleikamp 		goto out_bdev;
2792ac27a0ecSDave Kleikamp 	}
2793ac27a0ecSDave Kleikamp 
2794617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2795617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2796ac27a0ecSDave Kleikamp 		brelse(bh);
2797ac27a0ecSDave Kleikamp 		goto out_bdev;
2798ac27a0ecSDave Kleikamp 	}
2799ac27a0ecSDave Kleikamp 
2800bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
2801ac27a0ecSDave Kleikamp 	start = sb_block + 1;
2802ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
2803ac27a0ecSDave Kleikamp 
2804dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2805ac27a0ecSDave Kleikamp 					start, len, blocksize);
2806ac27a0ecSDave Kleikamp 	if (!journal) {
2807617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2808ac27a0ecSDave Kleikamp 		goto out_bdev;
2809ac27a0ecSDave Kleikamp 	}
2810ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2811ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
2812ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
2813ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
2814617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2815ac27a0ecSDave Kleikamp 		goto out_journal;
2816ac27a0ecSDave Kleikamp 	}
2817ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2818617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: External journal has more than one "
2819ac27a0ecSDave Kleikamp 					"user (unsupported) - %d\n",
2820ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
2821ac27a0ecSDave Kleikamp 		goto out_journal;
2822ac27a0ecSDave Kleikamp 	}
2823617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
2824617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2825ac27a0ecSDave Kleikamp 	return journal;
2826ac27a0ecSDave Kleikamp out_journal:
2827dab291afSMingming Cao 	jbd2_journal_destroy(journal);
2828ac27a0ecSDave Kleikamp out_bdev:
2829617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
2830ac27a0ecSDave Kleikamp 	return NULL;
2831ac27a0ecSDave Kleikamp }
2832ac27a0ecSDave Kleikamp 
2833617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
2834617ba13bSMingming Cao 			     struct ext4_super_block *es,
2835ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
2836ac27a0ecSDave Kleikamp {
2837ac27a0ecSDave Kleikamp 	journal_t *journal;
2838ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2839ac27a0ecSDave Kleikamp 	dev_t journal_dev;
2840ac27a0ecSDave Kleikamp 	int err = 0;
2841ac27a0ecSDave Kleikamp 	int really_read_only;
2842ac27a0ecSDave Kleikamp 
28430390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
28440390131bSFrank Mayhar 
2845ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2846ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2847617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2848ac27a0ecSDave Kleikamp 			"numbers have changed\n");
2849ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
2850ac27a0ecSDave Kleikamp 	} else
2851ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2852ac27a0ecSDave Kleikamp 
2853ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
2854ac27a0ecSDave Kleikamp 
2855ac27a0ecSDave Kleikamp 	/*
2856ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
2857ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
2858ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
2859ac27a0ecSDave Kleikamp 	 */
2860ac27a0ecSDave Kleikamp 
2861617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2862ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
2863617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: INFO: recovery "
2864ac27a0ecSDave Kleikamp 					"required on readonly filesystem.\n");
2865ac27a0ecSDave Kleikamp 			if (really_read_only) {
2866617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: write access "
2867ac27a0ecSDave Kleikamp 					"unavailable, cannot proceed.\n");
2868ac27a0ecSDave Kleikamp 				return -EROFS;
2869ac27a0ecSDave Kleikamp 			}
2870617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: write access will "
2871ac27a0ecSDave Kleikamp 			       "be enabled during recovery.\n");
2872ac27a0ecSDave Kleikamp 		}
2873ac27a0ecSDave Kleikamp 	}
2874ac27a0ecSDave Kleikamp 
2875ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
2876617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2877ac27a0ecSDave Kleikamp 		       "and inode journals!\n");
2878ac27a0ecSDave Kleikamp 		return -EINVAL;
2879ac27a0ecSDave Kleikamp 	}
2880ac27a0ecSDave Kleikamp 
2881ac27a0ecSDave Kleikamp 	if (journal_inum) {
2882617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
2883ac27a0ecSDave Kleikamp 			return -EINVAL;
2884ac27a0ecSDave Kleikamp 	} else {
2885617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2886ac27a0ecSDave Kleikamp 			return -EINVAL;
2887ac27a0ecSDave Kleikamp 	}
2888ac27a0ecSDave Kleikamp 
28894776004fSTheodore Ts'o 	if (journal->j_flags & JBD2_BARRIER)
28904776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers enabled\n");
28914776004fSTheodore Ts'o 	else
28924776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers disabled\n");
28934776004fSTheodore Ts'o 
2894ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2895dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
2896ac27a0ecSDave Kleikamp 		if (err)  {
2897617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2898dab291afSMingming Cao 			jbd2_journal_destroy(journal);
2899ac27a0ecSDave Kleikamp 			return err;
2900ac27a0ecSDave Kleikamp 		}
2901ac27a0ecSDave Kleikamp 	}
2902ac27a0ecSDave Kleikamp 
2903617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2904dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
2905ac27a0ecSDave Kleikamp 	if (!err)
2906dab291afSMingming Cao 		err = jbd2_journal_load(journal);
2907ac27a0ecSDave Kleikamp 
2908ac27a0ecSDave Kleikamp 	if (err) {
2909617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2910dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2911ac27a0ecSDave Kleikamp 		return err;
2912ac27a0ecSDave Kleikamp 	}
2913ac27a0ecSDave Kleikamp 
2914617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2915617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
2916ac27a0ecSDave Kleikamp 
2917ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2918ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2919ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
2920ac27a0ecSDave Kleikamp 		sb->s_dirt = 1;
2921ac27a0ecSDave Kleikamp 
2922ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
2923617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2924ac27a0ecSDave Kleikamp 	}
2925ac27a0ecSDave Kleikamp 
2926ac27a0ecSDave Kleikamp 	return 0;
2927ac27a0ecSDave Kleikamp }
2928ac27a0ecSDave Kleikamp 
2929617ba13bSMingming Cao static int ext4_create_journal(struct super_block *sb,
2930617ba13bSMingming Cao 			       struct ext4_super_block *es,
2931ac27a0ecSDave Kleikamp 			       unsigned int journal_inum)
2932ac27a0ecSDave Kleikamp {
2933ac27a0ecSDave Kleikamp 	journal_t *journal;
29346c675bd4SBorislav Petkov 	int err;
2935ac27a0ecSDave Kleikamp 
2936ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY) {
2937617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2938ac27a0ecSDave Kleikamp 				"create journal.\n");
2939ac27a0ecSDave Kleikamp 		return -EROFS;
2940ac27a0ecSDave Kleikamp 	}
2941ac27a0ecSDave Kleikamp 
29426c675bd4SBorislav Petkov 	journal = ext4_get_journal(sb, journal_inum);
29436c675bd4SBorislav Petkov 	if (!journal)
2944ac27a0ecSDave Kleikamp 		return -EINVAL;
2945ac27a0ecSDave Kleikamp 
2946617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2947ac27a0ecSDave Kleikamp 	       journal_inum);
2948ac27a0ecSDave Kleikamp 
29496c675bd4SBorislav Petkov 	err = jbd2_journal_create(journal);
29506c675bd4SBorislav Petkov 	if (err) {
2951617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2952dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2953ac27a0ecSDave Kleikamp 		return -EIO;
2954ac27a0ecSDave Kleikamp 	}
2955ac27a0ecSDave Kleikamp 
2956617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2957ac27a0ecSDave Kleikamp 
2958617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
2959617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2960617ba13bSMingming Cao 	EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2961ac27a0ecSDave Kleikamp 
2962ac27a0ecSDave Kleikamp 	es->s_journal_inum = cpu_to_le32(journal_inum);
2963ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
2964ac27a0ecSDave Kleikamp 
2965ac27a0ecSDave Kleikamp 	/* Make sure we flush the recovery flag to disk. */
2966617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
2967ac27a0ecSDave Kleikamp 
2968ac27a0ecSDave Kleikamp 	return 0;
2969ac27a0ecSDave Kleikamp }
2970ac27a0ecSDave Kleikamp 
2971617ba13bSMingming Cao static void ext4_commit_super(struct super_block *sb,
29722b2d6d01STheodore Ts'o 			      struct ext4_super_block *es, int sync)
2973ac27a0ecSDave Kleikamp {
2974617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2975ac27a0ecSDave Kleikamp 
2976ac27a0ecSDave Kleikamp 	if (!sbh)
2977ac27a0ecSDave Kleikamp 		return;
2978914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
2979914258bfSTheodore Ts'o 		/*
2980914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
2981914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
2982914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
2983914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
2984914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
2985914258bfSTheodore Ts'o 		 * write and hope for the best.
2986914258bfSTheodore Ts'o 		 */
2987914258bfSTheodore Ts'o 		printk(KERN_ERR "ext4: previous I/O error to "
2988914258bfSTheodore Ts'o 		       "superblock detected for %s.\n", sb->s_id);
2989914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
2990914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
2991914258bfSTheodore Ts'o 	}
2992ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
29935d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
29945d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeblocks_counter));
29955d1b1b3fSAneesh Kumar K.V 	es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
29965d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeinodes_counter));
29975d1b1b3fSAneesh Kumar K.V 
2998ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
2999ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
3000914258bfSTheodore Ts'o 	if (sync) {
3001ac27a0ecSDave Kleikamp 		sync_dirty_buffer(sbh);
3002914258bfSTheodore Ts'o 		if (buffer_write_io_error(sbh)) {
3003914258bfSTheodore Ts'o 			printk(KERN_ERR "ext4: I/O error while writing "
3004914258bfSTheodore Ts'o 			       "superblock for %s.\n", sb->s_id);
3005914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
3006914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
3007914258bfSTheodore Ts'o 		}
3008914258bfSTheodore Ts'o 	}
3009ac27a0ecSDave Kleikamp }
3010ac27a0ecSDave Kleikamp 
3011ac27a0ecSDave Kleikamp 
3012ac27a0ecSDave Kleikamp /*
3013ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
3014ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
3015ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
3016ac27a0ecSDave Kleikamp  */
3017617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
3018617ba13bSMingming Cao 					struct ext4_super_block *es)
3019ac27a0ecSDave Kleikamp {
3020617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
3021ac27a0ecSDave Kleikamp 
30220390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
30230390131bSFrank Mayhar 		BUG_ON(journal != NULL);
30240390131bSFrank Mayhar 		return;
30250390131bSFrank Mayhar 	}
3026dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
30277ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
30287ffe1ea8SHidehiro Kawai 		goto out;
30297ffe1ea8SHidehiro Kawai 
303032c37730SJan Kara 	lock_super(sb);
3031617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3032ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
3033617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3034ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
3035617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
3036ac27a0ecSDave Kleikamp 	}
303732c37730SJan Kara 	unlock_super(sb);
30387ffe1ea8SHidehiro Kawai 
30397ffe1ea8SHidehiro Kawai out:
3040dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
3041ac27a0ecSDave Kleikamp }
3042ac27a0ecSDave Kleikamp 
3043ac27a0ecSDave Kleikamp /*
3044ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
3045ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
3046ac27a0ecSDave Kleikamp  * main filesystem now.
3047ac27a0ecSDave Kleikamp  */
3048617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
3049617ba13bSMingming Cao 				   struct ext4_super_block *es)
3050ac27a0ecSDave Kleikamp {
3051ac27a0ecSDave Kleikamp 	journal_t *journal;
3052ac27a0ecSDave Kleikamp 	int j_errno;
3053ac27a0ecSDave Kleikamp 	const char *errstr;
3054ac27a0ecSDave Kleikamp 
30550390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
30560390131bSFrank Mayhar 
3057617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
3058ac27a0ecSDave Kleikamp 
3059ac27a0ecSDave Kleikamp 	/*
3060ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
3061617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
3062ac27a0ecSDave Kleikamp 	 */
3063ac27a0ecSDave Kleikamp 
3064dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
3065ac27a0ecSDave Kleikamp 	if (j_errno) {
3066ac27a0ecSDave Kleikamp 		char nbuf[16];
3067ac27a0ecSDave Kleikamp 
3068617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
306946e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Filesystem error recorded "
3070ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
307146e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Marking fs in need of "
3072ac27a0ecSDave Kleikamp 			     "filesystem check.");
3073ac27a0ecSDave Kleikamp 
3074617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3075617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3076617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
3077ac27a0ecSDave Kleikamp 
3078dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
3079ac27a0ecSDave Kleikamp 	}
3080ac27a0ecSDave Kleikamp }
3081ac27a0ecSDave Kleikamp 
3082ac27a0ecSDave Kleikamp /*
3083ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
3084ac27a0ecSDave Kleikamp  * and wait on the commit.
3085ac27a0ecSDave Kleikamp  */
3086617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
3087ac27a0ecSDave Kleikamp {
3088ac27a0ecSDave Kleikamp 	journal_t *journal;
30890390131bSFrank Mayhar 	int ret = 0;
3090ac27a0ecSDave Kleikamp 
3091ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
3092ac27a0ecSDave Kleikamp 		return 0;
3093ac27a0ecSDave Kleikamp 
3094617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
30950390131bSFrank Mayhar 	if (journal) {
3096ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
3097617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
30980390131bSFrank Mayhar 	}
30990390131bSFrank Mayhar 
3100ac27a0ecSDave Kleikamp 	return ret;
3101ac27a0ecSDave Kleikamp }
3102ac27a0ecSDave Kleikamp 
3103ac27a0ecSDave Kleikamp /*
3104617ba13bSMingming Cao  * Ext4 always journals updates to the superblock itself, so we don't
3105ac27a0ecSDave Kleikamp  * have to propagate any other updates to the superblock on disk at this
310614ce0cb4STheodore Ts'o  * point.  (We can probably nuke this function altogether, and remove
310714ce0cb4STheodore Ts'o  * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
3108ac27a0ecSDave Kleikamp  */
3109617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
3110ac27a0ecSDave Kleikamp {
31110390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
3112ac27a0ecSDave Kleikamp 		if (mutex_trylock(&sb->s_lock) != 0)
3113ac27a0ecSDave Kleikamp 			BUG();
3114ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
31150390131bSFrank Mayhar 	} else {
31160390131bSFrank Mayhar 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
31170390131bSFrank Mayhar 	}
3118ac27a0ecSDave Kleikamp }
3119ac27a0ecSDave Kleikamp 
3120617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
3121ac27a0ecSDave Kleikamp {
312214ce0cb4STheodore Ts'o 	int ret = 0;
3123ac27a0ecSDave Kleikamp 
3124ede86cc4STheodore Ts'o 	trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
3125ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
31260390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
3127ac27a0ecSDave Kleikamp 		if (wait)
312814ce0cb4STheodore Ts'o 			ret = ext4_force_commit(sb);
312914ce0cb4STheodore Ts'o 		else
313014ce0cb4STheodore Ts'o  			jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
31310390131bSFrank Mayhar 	} else {
31320390131bSFrank Mayhar 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
31330390131bSFrank Mayhar 	}
313414ce0cb4STheodore Ts'o 	return ret;
3135ac27a0ecSDave Kleikamp }
3136ac27a0ecSDave Kleikamp 
3137ac27a0ecSDave Kleikamp /*
3138ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
3139ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
3140ac27a0ecSDave Kleikamp  */
3141617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb)
3142ac27a0ecSDave Kleikamp {
3143ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
3144ac27a0ecSDave Kleikamp 
3145ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
3146617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
3147ac27a0ecSDave Kleikamp 
31480390131bSFrank Mayhar 		if (journal) {
3149ac27a0ecSDave Kleikamp 			/* Now we set up the journal barrier. */
3150dab291afSMingming Cao 			jbd2_journal_lock_updates(journal);
31517ffe1ea8SHidehiro Kawai 
31527ffe1ea8SHidehiro Kawai 			/*
31530390131bSFrank Mayhar 			 * We don't want to clear needs_recovery flag when we
31540390131bSFrank Mayhar 			 * failed to flush the journal.
31557ffe1ea8SHidehiro Kawai 			 */
31567ffe1ea8SHidehiro Kawai 			if (jbd2_journal_flush(journal) < 0)
31577ffe1ea8SHidehiro Kawai 				return;
31580390131bSFrank Mayhar 		}
3159ac27a0ecSDave Kleikamp 
3160ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
3161617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3162617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3163ac27a0ecSDave Kleikamp 	}
3164ac27a0ecSDave Kleikamp }
3165ac27a0ecSDave Kleikamp 
3166ac27a0ecSDave Kleikamp /*
3167ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
3168ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
3169ac27a0ecSDave Kleikamp  */
3170617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb)
3171ac27a0ecSDave Kleikamp {
31720390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
3173ac27a0ecSDave Kleikamp 		lock_super(sb);
3174ac27a0ecSDave Kleikamp 		/* Reser the needs_recovery flag before the fs is unlocked. */
3175617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3176617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3177ac27a0ecSDave Kleikamp 		unlock_super(sb);
3178dab291afSMingming Cao 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3179ac27a0ecSDave Kleikamp 	}
3180ac27a0ecSDave Kleikamp }
3181ac27a0ecSDave Kleikamp 
3182617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
3183ac27a0ecSDave Kleikamp {
3184617ba13bSMingming Cao 	struct ext4_super_block *es;
3185617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3186617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
3187ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
3188617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
31898a266467STheodore Ts'o 	ext4_group_t g;
3190ac27a0ecSDave Kleikamp 	int err;
3191ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3192ac27a0ecSDave Kleikamp 	int i;
3193ac27a0ecSDave Kleikamp #endif
3194ac27a0ecSDave Kleikamp 
3195ac27a0ecSDave Kleikamp 	/* Store the original options */
3196ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
3197ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
3198ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
3199ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
3200ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
320130773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
320230773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
3203ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3204ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3205ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3206ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3207ac27a0ecSDave Kleikamp #endif
3208ac27a0ecSDave Kleikamp 
3209ac27a0ecSDave Kleikamp 	/*
3210ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
3211ac27a0ecSDave Kleikamp 	 */
3212ac27a0ecSDave Kleikamp 	if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3213ac27a0ecSDave Kleikamp 		err = -EINVAL;
3214ac27a0ecSDave Kleikamp 		goto restore_opts;
3215ac27a0ecSDave Kleikamp 	}
3216ac27a0ecSDave Kleikamp 
3217617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
321846e665e9SHarvey Harrison 		ext4_abort(sb, __func__, "Abort forced by user");
3219ac27a0ecSDave Kleikamp 
3220ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3221617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3222ac27a0ecSDave Kleikamp 
3223ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3224ac27a0ecSDave Kleikamp 
32250390131bSFrank Mayhar 	if (sbi->s_journal)
3226617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
3227ac27a0ecSDave Kleikamp 
3228ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3229bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
3230617ba13bSMingming Cao 		if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3231ac27a0ecSDave Kleikamp 			err = -EROFS;
3232ac27a0ecSDave Kleikamp 			goto restore_opts;
3233ac27a0ecSDave Kleikamp 		}
3234ac27a0ecSDave Kleikamp 
3235ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
3236ac27a0ecSDave Kleikamp 			/*
3237ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3238ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3239ac27a0ecSDave Kleikamp 			 */
3240ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3241ac27a0ecSDave Kleikamp 
3242ac27a0ecSDave Kleikamp 			/*
3243ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3244ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3245ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3246ac27a0ecSDave Kleikamp 			 */
3247617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3248617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3249ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3250ac27a0ecSDave Kleikamp 
325132c37730SJan Kara 			/*
325232c37730SJan Kara 			 * We have to unlock super so that we can wait for
325332c37730SJan Kara 			 * transactions.
325432c37730SJan Kara 			 */
32550390131bSFrank Mayhar 			if (sbi->s_journal) {
325632c37730SJan Kara 				unlock_super(sb);
3257617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
325832c37730SJan Kara 				lock_super(sb);
32590390131bSFrank Mayhar 			}
3260ac27a0ecSDave Kleikamp 		} else {
32613a06d778SAneesh Kumar K.V 			int ret;
3262617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3263617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3264617ba13bSMingming Cao 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3265ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
32663a06d778SAneesh Kumar K.V 				       "optional features (%x).\n", sb->s_id,
32673a06d778SAneesh Kumar K.V 				(le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
32683a06d778SAneesh Kumar K.V 					~EXT4_FEATURE_RO_COMPAT_SUPP));
3269ac27a0ecSDave Kleikamp 				err = -EROFS;
3270ac27a0ecSDave Kleikamp 				goto restore_opts;
3271ac27a0ecSDave Kleikamp 			}
3272ead6596bSEric Sandeen 
3273ead6596bSEric Sandeen 			/*
32748a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
32758a266467STheodore Ts'o 			 * are sane.  If they aren't, refuse to
32768a266467STheodore Ts'o 			 * remount r/w.
32778a266467STheodore Ts'o 			 */
32788a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
32798a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
32808a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
32818a266467STheodore Ts'o 
32828a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
32838a266467STheodore Ts'o 					printk(KERN_ERR
32848a266467STheodore Ts'o 	       "EXT4-fs: ext4_remount: "
3285a9df9a49STheodore Ts'o 		"Checksum for group %u failed (%u!=%u)\n",
32868a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
32878a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
32888a266467STheodore Ts'o 					err = -EINVAL;
32898a266467STheodore Ts'o 					goto restore_opts;
32908a266467STheodore Ts'o 				}
32918a266467STheodore Ts'o 			}
32928a266467STheodore Ts'o 
32938a266467STheodore Ts'o 			/*
3294ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3295ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3296ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3297ead6596bSEric Sandeen 			 */
3298ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3299ead6596bSEric Sandeen 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3300ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3301ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3302ead6596bSEric Sandeen 				       "umount/remount instead.\n",
3303ead6596bSEric Sandeen 				       sb->s_id);
3304ead6596bSEric Sandeen 				err = -EINVAL;
3305ead6596bSEric Sandeen 				goto restore_opts;
3306ead6596bSEric Sandeen 			}
3307ead6596bSEric Sandeen 
3308ac27a0ecSDave Kleikamp 			/*
3309ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3310ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3311ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3312ac27a0ecSDave Kleikamp 			 * the partition.)
3313ac27a0ecSDave Kleikamp 			 */
33140390131bSFrank Mayhar 			if (sbi->s_journal)
3315617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
3316ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3317617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3318ac27a0ecSDave Kleikamp 				goto restore_opts;
3319617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3320ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3321ac27a0ecSDave Kleikamp 		}
3322ac27a0ecSDave Kleikamp 	}
33230390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
33240390131bSFrank Mayhar 		ext4_commit_super(sb, es, 1);
33250390131bSFrank Mayhar 
3326ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3327ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3328ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3329ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3330ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3331ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3332ac27a0ecSDave Kleikamp #endif
3333ac27a0ecSDave Kleikamp 	return 0;
3334ac27a0ecSDave Kleikamp restore_opts:
3335ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3336ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3337ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3338ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3339ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
334030773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
334130773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
3342ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3343ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3344ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3345ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3346ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3347ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3348ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3349ac27a0ecSDave Kleikamp 	}
3350ac27a0ecSDave Kleikamp #endif
3351ac27a0ecSDave Kleikamp 	return err;
3352ac27a0ecSDave Kleikamp }
3353ac27a0ecSDave Kleikamp 
3354617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3355ac27a0ecSDave Kleikamp {
3356ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3357617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3358617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3359960cc398SPekka Enberg 	u64 fsid;
3360ac27a0ecSDave Kleikamp 
33615e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
33625e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
33636bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3364fd2d4291SAvantika Mathur 		ext4_group_t ngroups = sbi->s_groups_count, i;
33655e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3366ac27a0ecSDave Kleikamp 		smp_rmb();
3367ac27a0ecSDave Kleikamp 
3368ac27a0ecSDave Kleikamp 		/*
33695e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
33705e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
33715e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3372ac27a0ecSDave Kleikamp 		 */
3373ac27a0ecSDave Kleikamp 
3374ac27a0ecSDave Kleikamp 		/*
3375ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3376ac27a0ecSDave Kleikamp 		 * overhead
3377ac27a0ecSDave Kleikamp 		 */
3378ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3379ac27a0ecSDave Kleikamp 
3380ac27a0ecSDave Kleikamp 		/*
3381ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3382ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3383ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3384ac27a0ecSDave Kleikamp 		 */
3385ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3386617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3387617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3388ac27a0ecSDave Kleikamp 			cond_resched();
3389ac27a0ecSDave Kleikamp 		}
3390ac27a0ecSDave Kleikamp 
3391ac27a0ecSDave Kleikamp 		/*
3392ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3393ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3394ac27a0ecSDave Kleikamp 		 */
33955e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
33965e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
33975e70030dSBadari Pulavarty 		smp_wmb();
33986bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3399ac27a0ecSDave Kleikamp 	}
3400ac27a0ecSDave Kleikamp 
3401617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3402ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
34035e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
34046bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
34056bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3406308ba3ecSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, buf->f_bfree);
3407bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3408bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3409ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3410ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
341152d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
34125e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3413617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3414960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3415960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3416960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3417960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3418ac27a0ecSDave Kleikamp 	return 0;
3419ac27a0ecSDave Kleikamp }
3420ac27a0ecSDave Kleikamp 
3421ac27a0ecSDave Kleikamp /* Helper function for writing quotas on sync - we need to start transaction before quota file
3422ac27a0ecSDave Kleikamp  * is locked for write. Otherwise the are possible deadlocks:
3423ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3424617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3425dab291afSMingming Cao  *   jbd2_journal_start()                   write_dquot()
3426ac27a0ecSDave Kleikamp  *   DQUOT_INIT()                        down(dqio_mutex)
3427dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3428ac27a0ecSDave Kleikamp  *
3429ac27a0ecSDave Kleikamp  */
3430ac27a0ecSDave Kleikamp 
3431ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3432ac27a0ecSDave Kleikamp 
3433ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3434ac27a0ecSDave Kleikamp {
3435ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3436ac27a0ecSDave Kleikamp }
3437ac27a0ecSDave Kleikamp 
3438617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type)
3439ac27a0ecSDave Kleikamp {
3440ac27a0ecSDave Kleikamp 	handle_t *handle;
3441ac27a0ecSDave Kleikamp 	int ret, err;
3442ac27a0ecSDave Kleikamp 
3443ac27a0ecSDave Kleikamp 	/* We may create quota structure so we need to reserve enough blocks */
3444617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3445ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3446ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3447ac27a0ecSDave Kleikamp 	ret = dquot_initialize(inode, type);
3448617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3449ac27a0ecSDave Kleikamp 	if (!ret)
3450ac27a0ecSDave Kleikamp 		ret = err;
3451ac27a0ecSDave Kleikamp 	return ret;
3452ac27a0ecSDave Kleikamp }
3453ac27a0ecSDave Kleikamp 
3454617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode)
3455ac27a0ecSDave Kleikamp {
3456ac27a0ecSDave Kleikamp 	handle_t *handle;
3457ac27a0ecSDave Kleikamp 	int ret, err;
3458ac27a0ecSDave Kleikamp 
3459ac27a0ecSDave Kleikamp 	/* We may delete quota structure so we need to reserve enough blocks */
3460617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
34612887df13SJan Kara 	if (IS_ERR(handle)) {
34622887df13SJan Kara 		/*
34632887df13SJan Kara 		 * We call dquot_drop() anyway to at least release references
34642887df13SJan Kara 		 * to quota structures so that umount does not hang.
34652887df13SJan Kara 		 */
34662887df13SJan Kara 		dquot_drop(inode);
3467ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
34682887df13SJan Kara 	}
3469ac27a0ecSDave Kleikamp 	ret = dquot_drop(inode);
3470617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3471ac27a0ecSDave Kleikamp 	if (!ret)
3472ac27a0ecSDave Kleikamp 		ret = err;
3473ac27a0ecSDave Kleikamp 	return ret;
3474ac27a0ecSDave Kleikamp }
3475ac27a0ecSDave Kleikamp 
3476617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3477ac27a0ecSDave Kleikamp {
3478ac27a0ecSDave Kleikamp 	int ret, err;
3479ac27a0ecSDave Kleikamp 	handle_t *handle;
3480ac27a0ecSDave Kleikamp 	struct inode *inode;
3481ac27a0ecSDave Kleikamp 
3482ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3483617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3484617ba13bSMingming Cao 					EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3485ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3486ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3487ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3488617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3489ac27a0ecSDave Kleikamp 	if (!ret)
3490ac27a0ecSDave Kleikamp 		ret = err;
3491ac27a0ecSDave Kleikamp 	return ret;
3492ac27a0ecSDave Kleikamp }
3493ac27a0ecSDave Kleikamp 
3494617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3495ac27a0ecSDave Kleikamp {
3496ac27a0ecSDave Kleikamp 	int ret, err;
3497ac27a0ecSDave Kleikamp 	handle_t *handle;
3498ac27a0ecSDave Kleikamp 
3499617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3500617ba13bSMingming Cao 					EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3501ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3502ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3503ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3504617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3505ac27a0ecSDave Kleikamp 	if (!ret)
3506ac27a0ecSDave Kleikamp 		ret = err;
3507ac27a0ecSDave Kleikamp 	return ret;
3508ac27a0ecSDave Kleikamp }
3509ac27a0ecSDave Kleikamp 
3510617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3511ac27a0ecSDave Kleikamp {
3512ac27a0ecSDave Kleikamp 	int ret, err;
3513ac27a0ecSDave Kleikamp 	handle_t *handle;
3514ac27a0ecSDave Kleikamp 
3515617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3516617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
35179c3013e9SJan Kara 	if (IS_ERR(handle)) {
35189c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
35199c3013e9SJan Kara 		dquot_release(dquot);
3520ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
35219c3013e9SJan Kara 	}
3522ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3523617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3524ac27a0ecSDave Kleikamp 	if (!ret)
3525ac27a0ecSDave Kleikamp 		ret = err;
3526ac27a0ecSDave Kleikamp 	return ret;
3527ac27a0ecSDave Kleikamp }
3528ac27a0ecSDave Kleikamp 
3529617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3530ac27a0ecSDave Kleikamp {
35312c8be6b2SJan Kara 	/* Are we journaling quotas? */
3532617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3533617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3534ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3535617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3536ac27a0ecSDave Kleikamp 	} else {
3537ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3538ac27a0ecSDave Kleikamp 	}
3539ac27a0ecSDave Kleikamp }
3540ac27a0ecSDave Kleikamp 
3541617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3542ac27a0ecSDave Kleikamp {
3543ac27a0ecSDave Kleikamp 	int ret, err;
3544ac27a0ecSDave Kleikamp 	handle_t *handle;
3545ac27a0ecSDave Kleikamp 
3546ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3547617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3548ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3549ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3550ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3551617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3552ac27a0ecSDave Kleikamp 	if (!ret)
3553ac27a0ecSDave Kleikamp 		ret = err;
3554ac27a0ecSDave Kleikamp 	return ret;
3555ac27a0ecSDave Kleikamp }
3556ac27a0ecSDave Kleikamp 
3557ac27a0ecSDave Kleikamp /*
3558ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3559ac27a0ecSDave Kleikamp  * the quota file and such...
3560ac27a0ecSDave Kleikamp  */
3561617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3562ac27a0ecSDave Kleikamp {
3563617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3564617ba13bSMingming Cao 			EXT4_SB(sb)->s_jquota_fmt, type);
3565ac27a0ecSDave Kleikamp }
3566ac27a0ecSDave Kleikamp 
3567ac27a0ecSDave Kleikamp /*
3568ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3569ac27a0ecSDave Kleikamp  */
3570617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
35718264613dSAl Viro 			 char *name, int remount)
3572ac27a0ecSDave Kleikamp {
3573ac27a0ecSDave Kleikamp 	int err;
35748264613dSAl Viro 	struct path path;
3575ac27a0ecSDave Kleikamp 
3576ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3577ac27a0ecSDave Kleikamp 		return -EINVAL;
35788264613dSAl Viro 	/* When remounting, no checks are needed and in fact, name is NULL */
35790623543bSJan Kara 	if (remount)
35808264613dSAl Viro 		return vfs_quota_on(sb, type, format_id, name, remount);
35810623543bSJan Kara 
35828264613dSAl Viro 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3583ac27a0ecSDave Kleikamp 	if (err)
3584ac27a0ecSDave Kleikamp 		return err;
35850623543bSJan Kara 
3586ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
35878264613dSAl Viro 	if (path.mnt->mnt_sb != sb) {
35888264613dSAl Viro 		path_put(&path);
3589ac27a0ecSDave Kleikamp 		return -EXDEV;
3590ac27a0ecSDave Kleikamp 	}
35910623543bSJan Kara 	/* Journaling quota? */
35920623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
35932b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
35948264613dSAl Viro 		if (path.dentry->d_parent != sb->s_root)
3595ac27a0ecSDave Kleikamp 			printk(KERN_WARNING
3596617ba13bSMingming Cao 				"EXT4-fs: Quota file not on filesystem root. "
35970623543bSJan Kara 				"Journaled quota will not work.\n");
35980623543bSJan Kara 	}
35990623543bSJan Kara 
36000623543bSJan Kara 	/*
36010623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
36020623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
36030623543bSJan Kara 	 */
36040390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
36050390131bSFrank Mayhar 	    ext4_should_journal_data(path.dentry->d_inode)) {
36060623543bSJan Kara 		/*
36070623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
36080623543bSJan Kara 		 * otherwise be livelocked...
36090623543bSJan Kara 		 */
36100623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
36117ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
36120623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
36137ffe1ea8SHidehiro Kawai 		if (err) {
36148264613dSAl Viro 			path_put(&path);
36157ffe1ea8SHidehiro Kawai 			return err;
36167ffe1ea8SHidehiro Kawai 		}
36170623543bSJan Kara 	}
36180623543bSJan Kara 
36198264613dSAl Viro 	err = vfs_quota_on_path(sb, type, format_id, &path);
36208264613dSAl Viro 	path_put(&path);
362177e69dacSAl Viro 	return err;
3622ac27a0ecSDave Kleikamp }
3623ac27a0ecSDave Kleikamp 
3624ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3625ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3626ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3627ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3628617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3629ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3630ac27a0ecSDave Kleikamp {
3631ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3632725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3633ac27a0ecSDave Kleikamp 	int err = 0;
3634ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3635ac27a0ecSDave Kleikamp 	int tocopy;
3636ac27a0ecSDave Kleikamp 	size_t toread;
3637ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3638ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3639ac27a0ecSDave Kleikamp 
3640ac27a0ecSDave Kleikamp 	if (off > i_size)
3641ac27a0ecSDave Kleikamp 		return 0;
3642ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3643ac27a0ecSDave Kleikamp 		len = i_size-off;
3644ac27a0ecSDave Kleikamp 	toread = len;
3645ac27a0ecSDave Kleikamp 	while (toread > 0) {
3646ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3647ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3648617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3649ac27a0ecSDave Kleikamp 		if (err)
3650ac27a0ecSDave Kleikamp 			return err;
3651ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3652ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3653ac27a0ecSDave Kleikamp 		else
3654ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3655ac27a0ecSDave Kleikamp 		brelse(bh);
3656ac27a0ecSDave Kleikamp 		offset = 0;
3657ac27a0ecSDave Kleikamp 		toread -= tocopy;
3658ac27a0ecSDave Kleikamp 		data += tocopy;
3659ac27a0ecSDave Kleikamp 		blk++;
3660ac27a0ecSDave Kleikamp 	}
3661ac27a0ecSDave Kleikamp 	return len;
3662ac27a0ecSDave Kleikamp }
3663ac27a0ecSDave Kleikamp 
3664ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3665ac27a0ecSDave Kleikamp  * enough credits) */
3666617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3667ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3668ac27a0ecSDave Kleikamp {
3669ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3670725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3671ac27a0ecSDave Kleikamp 	int err = 0;
3672ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3673ac27a0ecSDave Kleikamp 	int tocopy;
3674617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3675ac27a0ecSDave Kleikamp 	size_t towrite = len;
3676ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3677ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3678ac27a0ecSDave Kleikamp 
36790390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
3680e5f8eab8STheodore Ts'o 		printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
36819c3013e9SJan Kara 			" cancelled because transaction is not started.\n",
36829c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
36839c3013e9SJan Kara 		return -EIO;
36849c3013e9SJan Kara 	}
3685ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3686ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3687ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3688ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3689617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3690ac27a0ecSDave Kleikamp 		if (!bh)
3691ac27a0ecSDave Kleikamp 			goto out;
3692ac27a0ecSDave Kleikamp 		if (journal_quota) {
3693617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3694ac27a0ecSDave Kleikamp 			if (err) {
3695ac27a0ecSDave Kleikamp 				brelse(bh);
3696ac27a0ecSDave Kleikamp 				goto out;
3697ac27a0ecSDave Kleikamp 			}
3698ac27a0ecSDave Kleikamp 		}
3699ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3700ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3701ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3702ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3703ac27a0ecSDave Kleikamp 		if (journal_quota)
37040390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle, NULL, bh);
3705ac27a0ecSDave Kleikamp 		else {
3706ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3707678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3708ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3709ac27a0ecSDave Kleikamp 		}
3710ac27a0ecSDave Kleikamp 		brelse(bh);
3711ac27a0ecSDave Kleikamp 		if (err)
3712ac27a0ecSDave Kleikamp 			goto out;
3713ac27a0ecSDave Kleikamp 		offset = 0;
3714ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3715ac27a0ecSDave Kleikamp 		data += tocopy;
3716ac27a0ecSDave Kleikamp 		blk++;
3717ac27a0ecSDave Kleikamp 	}
3718ac27a0ecSDave Kleikamp out:
37194d04e4fbSJan Kara 	if (len == towrite) {
37204d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
3721ac27a0ecSDave Kleikamp 		return err;
37224d04e4fbSJan Kara 	}
3723ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3724ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3725617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3726ac27a0ecSDave Kleikamp 	}
3727ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3728617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3729ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3730ac27a0ecSDave Kleikamp 	return len - towrite;
3731ac27a0ecSDave Kleikamp }
3732ac27a0ecSDave Kleikamp 
3733ac27a0ecSDave Kleikamp #endif
3734ac27a0ecSDave Kleikamp 
3735617ba13bSMingming Cao static int ext4_get_sb(struct file_system_type *fs_type,
3736ac27a0ecSDave Kleikamp 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3737ac27a0ecSDave Kleikamp {
3738617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3739ac27a0ecSDave Kleikamp }
3740ac27a0ecSDave Kleikamp 
37415e8814f2STheodore Ts'o #ifdef CONFIG_PROC_FS
37425e8814f2STheodore Ts'o static int ext4_ui_proc_show(struct seq_file *m, void *v)
37435e8814f2STheodore Ts'o {
37445e8814f2STheodore Ts'o 	unsigned int *p = m->private;
37455e8814f2STheodore Ts'o 
37465e8814f2STheodore Ts'o 	seq_printf(m, "%u\n", *p);
37475e8814f2STheodore Ts'o 	return 0;
37485e8814f2STheodore Ts'o }
37495e8814f2STheodore Ts'o 
37505e8814f2STheodore Ts'o static int ext4_ui_proc_open(struct inode *inode, struct file *file)
37515e8814f2STheodore Ts'o {
37525e8814f2STheodore Ts'o 	return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
37535e8814f2STheodore Ts'o }
37545e8814f2STheodore Ts'o 
37555e8814f2STheodore Ts'o static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
37565e8814f2STheodore Ts'o 			       size_t cnt, loff_t *ppos)
37575e8814f2STheodore Ts'o {
375823475e26SRoel Kluin 	unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
37595e8814f2STheodore Ts'o 	char str[32];
37605e8814f2STheodore Ts'o 
37615e8814f2STheodore Ts'o 	if (cnt >= sizeof(str))
37625e8814f2STheodore Ts'o 		return -EINVAL;
37635e8814f2STheodore Ts'o 	if (copy_from_user(str, buf, cnt))
37645e8814f2STheodore Ts'o 		return -EFAULT;
376523475e26SRoel Kluin 
376623475e26SRoel Kluin 	*p = simple_strtoul(str, NULL, 0);
37675e8814f2STheodore Ts'o 	return cnt;
37685e8814f2STheodore Ts'o }
37695e8814f2STheodore Ts'o 
37705e8814f2STheodore Ts'o const struct file_operations ext4_ui_proc_fops = {
37715e8814f2STheodore Ts'o 	.owner		= THIS_MODULE,
37725e8814f2STheodore Ts'o 	.open		= ext4_ui_proc_open,
37735e8814f2STheodore Ts'o 	.read		= seq_read,
37745e8814f2STheodore Ts'o 	.llseek		= seq_lseek,
37755e8814f2STheodore Ts'o 	.release	= single_release,
37765e8814f2STheodore Ts'o 	.write		= ext4_ui_proc_write,
37775e8814f2STheodore Ts'o };
37785e8814f2STheodore Ts'o #endif
37795e8814f2STheodore Ts'o 
378003010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
3781ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
378203010a33STheodore Ts'o 	.name		= "ext4",
3783617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
3784ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
3785ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
3786ac27a0ecSDave Kleikamp };
3787ac27a0ecSDave Kleikamp 
378803010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
378903010a33STheodore Ts'o static int ext4dev_get_sb(struct file_system_type *fs_type,
379003010a33STheodore Ts'o 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
379103010a33STheodore Ts'o {
379203010a33STheodore Ts'o 	printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
379303010a33STheodore Ts'o 	       "to mount using ext4\n");
379403010a33STheodore Ts'o 	printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
379503010a33STheodore Ts'o 	       "will go away by 2.6.31\n");
379603010a33STheodore Ts'o 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
379703010a33STheodore Ts'o }
379803010a33STheodore Ts'o 
379903010a33STheodore Ts'o static struct file_system_type ext4dev_fs_type = {
380003010a33STheodore Ts'o 	.owner		= THIS_MODULE,
380103010a33STheodore Ts'o 	.name		= "ext4dev",
380203010a33STheodore Ts'o 	.get_sb		= ext4dev_get_sb,
380303010a33STheodore Ts'o 	.kill_sb	= kill_block_super,
380403010a33STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
380503010a33STheodore Ts'o };
380603010a33STheodore Ts'o MODULE_ALIAS("ext4dev");
380703010a33STheodore Ts'o #endif
380803010a33STheodore Ts'o 
3809617ba13bSMingming Cao static int __init init_ext4_fs(void)
3810ac27a0ecSDave Kleikamp {
3811c9de560dSAlex Tomas 	int err;
3812c9de560dSAlex Tomas 
38139f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3814c9de560dSAlex Tomas 	err = init_ext4_mballoc();
3815ac27a0ecSDave Kleikamp 	if (err)
3816ac27a0ecSDave Kleikamp 		return err;
3817c9de560dSAlex Tomas 
3818c9de560dSAlex Tomas 	err = init_ext4_xattr();
3819c9de560dSAlex Tomas 	if (err)
3820c9de560dSAlex Tomas 		goto out2;
3821ac27a0ecSDave Kleikamp 	err = init_inodecache();
3822ac27a0ecSDave Kleikamp 	if (err)
3823ac27a0ecSDave Kleikamp 		goto out1;
382403010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
3825ac27a0ecSDave Kleikamp 	if (err)
3826ac27a0ecSDave Kleikamp 		goto out;
382703010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
382803010a33STheodore Ts'o 	err = register_filesystem(&ext4dev_fs_type);
382903010a33STheodore Ts'o 	if (err) {
383003010a33STheodore Ts'o 		unregister_filesystem(&ext4_fs_type);
383103010a33STheodore Ts'o 		goto out;
383203010a33STheodore Ts'o 	}
383303010a33STheodore Ts'o #endif
3834ac27a0ecSDave Kleikamp 	return 0;
3835ac27a0ecSDave Kleikamp out:
3836ac27a0ecSDave Kleikamp 	destroy_inodecache();
3837ac27a0ecSDave Kleikamp out1:
3838617ba13bSMingming Cao 	exit_ext4_xattr();
3839c9de560dSAlex Tomas out2:
3840c9de560dSAlex Tomas 	exit_ext4_mballoc();
3841ac27a0ecSDave Kleikamp 	return err;
3842ac27a0ecSDave Kleikamp }
3843ac27a0ecSDave Kleikamp 
3844617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
3845ac27a0ecSDave Kleikamp {
384603010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
384703010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
3848617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
384903010a33STheodore Ts'o #endif
3850ac27a0ecSDave Kleikamp 	destroy_inodecache();
3851617ba13bSMingming Cao 	exit_ext4_xattr();
3852c9de560dSAlex Tomas 	exit_ext4_mballoc();
38539f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
3854ac27a0ecSDave Kleikamp }
3855ac27a0ecSDave Kleikamp 
3856ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3857617ba13bSMingming Cao MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3858ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
3859617ba13bSMingming Cao module_init(init_ext4_fs)
3860617ba13bSMingming Cao module_exit(exit_ext4_fs)
3861