xref: /linux/fs/ext4/super.c (revision f99b25897a86fcfff9140396a97261ae65fed872)
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 
968fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
978fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
98bd81d8eeSLaurent Vivier {
993a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1008fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1018fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
102bd81d8eeSLaurent Vivier }
103bd81d8eeSLaurent Vivier 
1048fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1058fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
106bd81d8eeSLaurent Vivier {
1075272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1088fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1098fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
110bd81d8eeSLaurent Vivier }
111bd81d8eeSLaurent Vivier 
1128fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1138fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
114bd81d8eeSLaurent Vivier {
1155272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1168fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1178fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
118bd81d8eeSLaurent Vivier }
119bd81d8eeSLaurent Vivier 
120ac27a0ecSDave Kleikamp /*
121dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
122ac27a0ecSDave Kleikamp  *
123ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
124ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
125ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
126ac27a0ecSDave Kleikamp  * appropriate.
127ac27a0ecSDave Kleikamp  */
128617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
129ac27a0ecSDave Kleikamp {
130ac27a0ecSDave Kleikamp 	journal_t *journal;
131ac27a0ecSDave Kleikamp 
132ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
133ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
134ac27a0ecSDave Kleikamp 
135ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
136ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
137ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
138617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
139ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
14046e665e9SHarvey Harrison 		ext4_abort(sb, __func__,
141ac27a0ecSDave Kleikamp 			   "Detected aborted journal");
142ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
143ac27a0ecSDave Kleikamp 	}
144ac27a0ecSDave Kleikamp 
145dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
146ac27a0ecSDave Kleikamp }
147ac27a0ecSDave Kleikamp 
148ac27a0ecSDave Kleikamp /*
149ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
150dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
151ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
152ac27a0ecSDave Kleikamp  * appropriate.
153ac27a0ecSDave Kleikamp  */
154617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
155ac27a0ecSDave Kleikamp {
156ac27a0ecSDave Kleikamp 	struct super_block *sb;
157ac27a0ecSDave Kleikamp 	int err;
158ac27a0ecSDave Kleikamp 	int rc;
159ac27a0ecSDave Kleikamp 
160ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
161ac27a0ecSDave Kleikamp 	err = handle->h_err;
162dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
163ac27a0ecSDave Kleikamp 
164ac27a0ecSDave Kleikamp 	if (!err)
165ac27a0ecSDave Kleikamp 		err = rc;
166ac27a0ecSDave Kleikamp 	if (err)
167617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
168ac27a0ecSDave Kleikamp 	return err;
169ac27a0ecSDave Kleikamp }
170ac27a0ecSDave Kleikamp 
171617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
172ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
173ac27a0ecSDave Kleikamp {
174ac27a0ecSDave Kleikamp 	char nbuf[16];
175617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
176ac27a0ecSDave Kleikamp 
177ac27a0ecSDave Kleikamp 	if (bh)
178ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
179ac27a0ecSDave Kleikamp 
180ac27a0ecSDave Kleikamp 	if (!handle->h_err)
181ac27a0ecSDave Kleikamp 		handle->h_err = err;
182ac27a0ecSDave Kleikamp 
183ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
184ac27a0ecSDave Kleikamp 		return;
185ac27a0ecSDave Kleikamp 
186ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
187ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
188ac27a0ecSDave Kleikamp 
189dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
190ac27a0ecSDave Kleikamp }
191ac27a0ecSDave Kleikamp 
192ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
193ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
194ac27a0ecSDave Kleikamp  *
195ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
196617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
197ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
198ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
199ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
200ac27a0ecSDave Kleikamp  * write out the superblock safely.
201ac27a0ecSDave Kleikamp  *
202dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
203ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
204ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
205ac27a0ecSDave Kleikamp  */
206ac27a0ecSDave Kleikamp 
207617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
208ac27a0ecSDave Kleikamp {
209617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
210ac27a0ecSDave Kleikamp 
211617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
212617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
213ac27a0ecSDave Kleikamp 
214ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
215ac27a0ecSDave Kleikamp 		return;
216ac27a0ecSDave Kleikamp 
217ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
218617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
219ac27a0ecSDave Kleikamp 
220617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
221ac27a0ecSDave Kleikamp 		if (journal)
222dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
223ac27a0ecSDave Kleikamp 	}
224ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
225ac27a0ecSDave Kleikamp 		printk(KERN_CRIT "Remounting filesystem read-only\n");
226ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
227ac27a0ecSDave Kleikamp 	}
228617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
229ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
230617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
231ac27a0ecSDave Kleikamp 			sb->s_id);
232ac27a0ecSDave Kleikamp }
233ac27a0ecSDave Kleikamp 
234617ba13bSMingming Cao void ext4_error(struct super_block *sb, const char *function,
235ac27a0ecSDave Kleikamp 		const char *fmt, ...)
236ac27a0ecSDave Kleikamp {
237ac27a0ecSDave Kleikamp 	va_list args;
238ac27a0ecSDave Kleikamp 
239ac27a0ecSDave Kleikamp 	va_start(args, fmt);
240617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
241ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
242ac27a0ecSDave Kleikamp 	printk("\n");
243ac27a0ecSDave Kleikamp 	va_end(args);
244ac27a0ecSDave Kleikamp 
245617ba13bSMingming Cao 	ext4_handle_error(sb);
246ac27a0ecSDave Kleikamp }
247ac27a0ecSDave Kleikamp 
248617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
249ac27a0ecSDave Kleikamp 				     char nbuf[16])
250ac27a0ecSDave Kleikamp {
251ac27a0ecSDave Kleikamp 	char *errstr = NULL;
252ac27a0ecSDave Kleikamp 
253ac27a0ecSDave Kleikamp 	switch (errno) {
254ac27a0ecSDave Kleikamp 	case -EIO:
255ac27a0ecSDave Kleikamp 		errstr = "IO failure";
256ac27a0ecSDave Kleikamp 		break;
257ac27a0ecSDave Kleikamp 	case -ENOMEM:
258ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
259ac27a0ecSDave Kleikamp 		break;
260ac27a0ecSDave Kleikamp 	case -EROFS:
261dab291afSMingming Cao 		if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
262ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
263ac27a0ecSDave Kleikamp 		else
264ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
265ac27a0ecSDave Kleikamp 		break;
266ac27a0ecSDave Kleikamp 	default:
267ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
268ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
269ac27a0ecSDave Kleikamp 		 * NULL. */
270ac27a0ecSDave Kleikamp 		if (nbuf) {
271ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
272ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
273ac27a0ecSDave Kleikamp 				errstr = nbuf;
274ac27a0ecSDave Kleikamp 		}
275ac27a0ecSDave Kleikamp 		break;
276ac27a0ecSDave Kleikamp 	}
277ac27a0ecSDave Kleikamp 
278ac27a0ecSDave Kleikamp 	return errstr;
279ac27a0ecSDave Kleikamp }
280ac27a0ecSDave Kleikamp 
281617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
282ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
283ac27a0ecSDave Kleikamp 
2842b2d6d01STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, int errno)
285ac27a0ecSDave Kleikamp {
286ac27a0ecSDave Kleikamp 	char nbuf[16];
287ac27a0ecSDave Kleikamp 	const char *errstr;
288ac27a0ecSDave Kleikamp 
289ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
290ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
291ac27a0ecSDave Kleikamp 	 * an error. */
292ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
293ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
294ac27a0ecSDave Kleikamp 		return;
295ac27a0ecSDave Kleikamp 
296617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
297617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
298ac27a0ecSDave Kleikamp 	       sb->s_id, function, errstr);
299ac27a0ecSDave Kleikamp 
300617ba13bSMingming Cao 	ext4_handle_error(sb);
301ac27a0ecSDave Kleikamp }
302ac27a0ecSDave Kleikamp 
303ac27a0ecSDave Kleikamp /*
304617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
305ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
306ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
307ac27a0ecSDave Kleikamp  *
308ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
309ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
310ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
311ac27a0ecSDave Kleikamp  */
312ac27a0ecSDave Kleikamp 
313617ba13bSMingming Cao void ext4_abort(struct super_block *sb, const char *function,
314ac27a0ecSDave Kleikamp 		const char *fmt, ...)
315ac27a0ecSDave Kleikamp {
316ac27a0ecSDave Kleikamp 	va_list args;
317ac27a0ecSDave Kleikamp 
318617ba13bSMingming Cao 	printk(KERN_CRIT "ext4_abort called.\n");
319ac27a0ecSDave Kleikamp 
320ac27a0ecSDave Kleikamp 	va_start(args, fmt);
321617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
322ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
323ac27a0ecSDave Kleikamp 	printk("\n");
324ac27a0ecSDave Kleikamp 	va_end(args);
325ac27a0ecSDave Kleikamp 
326ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
327617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
328ac27a0ecSDave Kleikamp 
329ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
330ac27a0ecSDave Kleikamp 		return;
331ac27a0ecSDave Kleikamp 
332ac27a0ecSDave Kleikamp 	printk(KERN_CRIT "Remounting filesystem read-only\n");
333617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
334ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
335617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
336ef2cabf7SHidehiro Kawai 	if (EXT4_SB(sb)->s_journal)
337dab291afSMingming Cao 		jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
338ac27a0ecSDave Kleikamp }
339ac27a0ecSDave Kleikamp 
340617ba13bSMingming Cao void ext4_warning(struct super_block *sb, const char *function,
341ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
342ac27a0ecSDave Kleikamp {
343ac27a0ecSDave Kleikamp 	va_list args;
344ac27a0ecSDave Kleikamp 
345ac27a0ecSDave Kleikamp 	va_start(args, fmt);
346617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
347ac27a0ecSDave Kleikamp 	       sb->s_id, function);
348ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
349ac27a0ecSDave Kleikamp 	printk("\n");
350ac27a0ecSDave Kleikamp 	va_end(args);
351ac27a0ecSDave Kleikamp }
352ac27a0ecSDave Kleikamp 
353617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
354ac27a0ecSDave Kleikamp {
355617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
356ac27a0ecSDave Kleikamp 
357617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
358ac27a0ecSDave Kleikamp 		return;
359ac27a0ecSDave Kleikamp 
36046e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
361ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
362ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
363617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
364ac27a0ecSDave Kleikamp 
365617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
366617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
367617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
368ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
369ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
370ac27a0ecSDave Kleikamp 
371ac27a0ecSDave Kleikamp 	/*
372ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
373ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
374ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
375ac27a0ecSDave Kleikamp 	 */
376ac27a0ecSDave Kleikamp }
377ac27a0ecSDave Kleikamp 
378ac27a0ecSDave Kleikamp /*
379ac27a0ecSDave Kleikamp  * Open the external journal device
380ac27a0ecSDave Kleikamp  */
381617ba13bSMingming Cao static struct block_device *ext4_blkdev_get(dev_t dev)
382ac27a0ecSDave Kleikamp {
383ac27a0ecSDave Kleikamp 	struct block_device *bdev;
384ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
385ac27a0ecSDave Kleikamp 
386ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
387ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
388ac27a0ecSDave Kleikamp 		goto fail;
389ac27a0ecSDave Kleikamp 	return bdev;
390ac27a0ecSDave Kleikamp 
391ac27a0ecSDave Kleikamp fail:
392617ba13bSMingming Cao 	printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
393ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
394ac27a0ecSDave Kleikamp 	return NULL;
395ac27a0ecSDave Kleikamp }
396ac27a0ecSDave Kleikamp 
397ac27a0ecSDave Kleikamp /*
398ac27a0ecSDave Kleikamp  * Release the journal device
399ac27a0ecSDave Kleikamp  */
400617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
401ac27a0ecSDave Kleikamp {
402ac27a0ecSDave Kleikamp 	bd_release(bdev);
4039a1c3542SAl Viro 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
404ac27a0ecSDave Kleikamp }
405ac27a0ecSDave Kleikamp 
406617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
407ac27a0ecSDave Kleikamp {
408ac27a0ecSDave Kleikamp 	struct block_device *bdev;
409ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
410ac27a0ecSDave Kleikamp 
411ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
412ac27a0ecSDave Kleikamp 	if (bdev) {
413617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
414ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
415ac27a0ecSDave Kleikamp 	}
416ac27a0ecSDave Kleikamp 	return ret;
417ac27a0ecSDave Kleikamp }
418ac27a0ecSDave Kleikamp 
419ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
420ac27a0ecSDave Kleikamp {
421617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
422ac27a0ecSDave Kleikamp }
423ac27a0ecSDave Kleikamp 
424617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
425ac27a0ecSDave Kleikamp {
426ac27a0ecSDave Kleikamp 	struct list_head *l;
427ac27a0ecSDave Kleikamp 
428ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb orphan head is %d\n",
429ac27a0ecSDave Kleikamp 	       le32_to_cpu(sbi->s_es->s_last_orphan));
430ac27a0ecSDave Kleikamp 
431ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
432ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
433ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
434ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
435ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
436ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
437ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
438ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
439ac27a0ecSDave Kleikamp 	}
440ac27a0ecSDave Kleikamp }
441ac27a0ecSDave Kleikamp 
442617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
443ac27a0ecSDave Kleikamp {
444617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
445617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
446ef2cabf7SHidehiro Kawai 	int i, err;
447ac27a0ecSDave Kleikamp 
448c9de560dSAlex Tomas 	ext4_mb_release(sb);
449a86c6181SAlex Tomas 	ext4_ext_release(sb);
450617ba13bSMingming Cao 	ext4_xattr_put_super(sb);
451ef2cabf7SHidehiro Kawai 	err = jbd2_journal_destroy(sbi->s_journal);
45247b4a50bSJan Kara 	sbi->s_journal = NULL;
453ef2cabf7SHidehiro Kawai 	if (err < 0)
454ef2cabf7SHidehiro Kawai 		ext4_abort(sb, __func__, "Couldn't clean up the journal");
455ef2cabf7SHidehiro Kawai 
456ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
457617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
458ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
459617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
460ac27a0ecSDave Kleikamp 	}
461240799cdSTheodore Ts'o 	if (sbi->s_proc) {
462240799cdSTheodore Ts'o 		remove_proc_entry("inode_readahead_blks", sbi->s_proc);
4639f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
464240799cdSTheodore Ts'o 	}
465ac27a0ecSDave Kleikamp 
466ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
467ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
468ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
469772cb7c8SJose R. Santos 	kfree(sbi->s_flex_groups);
470ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
471ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
472ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
4736bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
474ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
475ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
476ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
477ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
478ac27a0ecSDave Kleikamp #endif
479ac27a0ecSDave Kleikamp 
480ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
481ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
482ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
483ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
484ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
485ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
486ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
487ac27a0ecSDave Kleikamp 
488f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
489ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
490ac27a0ecSDave Kleikamp 		/*
491ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
492ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
493ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
494ac27a0ecSDave Kleikamp 		 */
495ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
496f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
497617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
498ac27a0ecSDave Kleikamp 	}
499ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
500ac27a0ecSDave Kleikamp 	kfree(sbi);
501ac27a0ecSDave Kleikamp 	return;
502ac27a0ecSDave Kleikamp }
503ac27a0ecSDave Kleikamp 
504e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
505ac27a0ecSDave Kleikamp 
506ac27a0ecSDave Kleikamp /*
507ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
508ac27a0ecSDave Kleikamp  */
509617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
510ac27a0ecSDave Kleikamp {
511617ba13bSMingming Cao 	struct ext4_inode_info *ei;
512ac27a0ecSDave Kleikamp 
513e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
514ac27a0ecSDave Kleikamp 	if (!ei)
515ac27a0ecSDave Kleikamp 		return NULL;
51603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
517617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
518617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
519ac27a0ecSDave Kleikamp #endif
520ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
52191246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
522a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
523c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
524c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
525678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
526d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
527d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
528d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
529d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
530d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
531ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
532ac27a0ecSDave Kleikamp }
533ac27a0ecSDave Kleikamp 
534617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
535ac27a0ecSDave Kleikamp {
5369f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
5379f7dd93dSVasily Averin 		printk("EXT4 Inode %p: orphan list check failed!\n",
5389f7dd93dSVasily Averin 			EXT4_I(inode));
5399f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
5409f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
5419f7dd93dSVasily Averin 				true);
5429f7dd93dSVasily Averin 		dump_stack();
5439f7dd93dSVasily Averin 	}
544617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
545ac27a0ecSDave Kleikamp }
546ac27a0ecSDave Kleikamp 
54751cc5068SAlexey Dobriyan static void init_once(void *foo)
548ac27a0ecSDave Kleikamp {
549617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
550ac27a0ecSDave Kleikamp 
551ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
55203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
553ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
554ac27a0ecSDave Kleikamp #endif
5550e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
556ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
557ac27a0ecSDave Kleikamp }
558ac27a0ecSDave Kleikamp 
559ac27a0ecSDave Kleikamp static int init_inodecache(void)
560ac27a0ecSDave Kleikamp {
561617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
562617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
563ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
564ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
56520c2df83SPaul Mundt 					     init_once);
566617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
567ac27a0ecSDave Kleikamp 		return -ENOMEM;
568ac27a0ecSDave Kleikamp 	return 0;
569ac27a0ecSDave Kleikamp }
570ac27a0ecSDave Kleikamp 
571ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
572ac27a0ecSDave Kleikamp {
573617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
574ac27a0ecSDave Kleikamp }
575ac27a0ecSDave Kleikamp 
576617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
577ac27a0ecSDave Kleikamp {
57803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
579617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
580617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
581617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
582617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
583ac27a0ecSDave Kleikamp 	}
584617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
585617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
586617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
587617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
588ac27a0ecSDave Kleikamp 	}
589ac27a0ecSDave Kleikamp #endif
590c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
591678aaf48SJan Kara 	jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
592678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
593ac27a0ecSDave Kleikamp }
594ac27a0ecSDave Kleikamp 
5952b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
5962b2d6d01STheodore Ts'o 					   struct super_block *sb)
597ac27a0ecSDave Kleikamp {
598ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
599617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
600ac27a0ecSDave Kleikamp 
601ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
602ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
603ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
604ac27a0ecSDave Kleikamp 
605ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
606ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
607ac27a0ecSDave Kleikamp 
608ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
609ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
610ac27a0ecSDave Kleikamp 
611617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
612ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
613ac27a0ecSDave Kleikamp 
614617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
615ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
616ac27a0ecSDave Kleikamp #endif
617ac27a0ecSDave Kleikamp }
618ac27a0ecSDave Kleikamp 
619d9c9bef1SMiklos Szeredi /*
620d9c9bef1SMiklos Szeredi  * Show an option if
621d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
622d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
623d9c9bef1SMiklos Szeredi  */
624617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
625ac27a0ecSDave Kleikamp {
626aa22df2cSAneesh Kumar K.V 	int def_errors;
627aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
628ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
629d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
630d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
631d9c9bef1SMiklos Szeredi 
632d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
633aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
634d9c9bef1SMiklos Szeredi 
635d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
636d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
637d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
638d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
639aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
640d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
641d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
642d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
643d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
644d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
645d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
646d9c9bef1SMiklos Szeredi 	}
647d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
648d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
649d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
650d9c9bef1SMiklos Szeredi 	}
651bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
652d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
653bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
654d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
655bb4f397aSAneesh Kumar K.V 		}
656bb4f397aSAneesh Kumar K.V 	}
657aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
658bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
659aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
660d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
661aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
662d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
663aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
664d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
665d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
666d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
66703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
668aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
669aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
670d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
671d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
672d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
673d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
674d9c9bef1SMiklos Szeredi 	}
675d9c9bef1SMiklos Szeredi #endif
67603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
677aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
678d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
679d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
680d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
681d9c9bef1SMiklos Szeredi #endif
682d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, RESERVATION))
683d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noreservation");
684d9c9bef1SMiklos Szeredi 	if (sbi->s_commit_interval) {
685d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
686d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
687d9c9bef1SMiklos Szeredi 	}
688571640caSEric Sandeen 	/*
689571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
690571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
691571640caSEric Sandeen 	 * status is.
692571640caSEric Sandeen 	 */
693571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
694571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
695cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
696cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
697d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
698d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
699d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, EXTENTS))
700d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noextents");
70125ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
70225ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
703dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
704dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
705dd919b98SAneesh Kumar K.V 
706ac27a0ecSDave Kleikamp 
707cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
708cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
709aa22df2cSAneesh Kumar K.V 	/*
710aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
711aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
712aa22df2cSAneesh Kumar K.V 	 */
713617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
714ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
715617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
716ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
717617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
718ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
719ac27a0ecSDave Kleikamp 
720240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
721240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
722240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
723240799cdSTheodore Ts'o 
7245bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
7255bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
7265bf5683aSHidehiro Kawai 
727617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
728ac27a0ecSDave Kleikamp 	return 0;
729ac27a0ecSDave Kleikamp }
730ac27a0ecSDave Kleikamp 
731ac27a0ecSDave Kleikamp 
7321b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
7331b961ac0SChristoph Hellwig 		u64 ino, u32 generation)
734ac27a0ecSDave Kleikamp {
735ac27a0ecSDave Kleikamp 	struct inode *inode;
736ac27a0ecSDave Kleikamp 
737617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
738ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
739617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
740ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
741ac27a0ecSDave Kleikamp 
742ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
743ac27a0ecSDave Kleikamp 	 *
744617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
745ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
746ac27a0ecSDave Kleikamp 	 *
747ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
748ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
749ac27a0ecSDave Kleikamp 	 */
7501d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
7511d1fe1eeSDavid Howells 	if (IS_ERR(inode))
7521d1fe1eeSDavid Howells 		return ERR_CAST(inode);
7531d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
754ac27a0ecSDave Kleikamp 		iput(inode);
755ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
756ac27a0ecSDave Kleikamp 	}
7571b961ac0SChristoph Hellwig 
7581b961ac0SChristoph Hellwig 	return inode;
759ac27a0ecSDave Kleikamp }
7601b961ac0SChristoph Hellwig 
7611b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
7621b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
7631b961ac0SChristoph Hellwig {
7641b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
7651b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
7661b961ac0SChristoph Hellwig }
7671b961ac0SChristoph Hellwig 
7681b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
7691b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
7701b961ac0SChristoph Hellwig {
7711b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
7721b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
773ac27a0ecSDave Kleikamp }
774ac27a0ecSDave Kleikamp 
775ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
776ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
777ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
778ac27a0ecSDave Kleikamp 
779617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type);
780617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode);
781617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
782617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
783617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
784617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
785617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
7866f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
7876f28e087SJan Kara 				char *path, int remount);
788617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
789617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
790ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
791617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
792ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
793ac27a0ecSDave Kleikamp 
794617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
795617ba13bSMingming Cao 	.initialize	= ext4_dquot_initialize,
796617ba13bSMingming Cao 	.drop		= ext4_dquot_drop,
797ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
798ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
799ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
800ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
801ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
802617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
803617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
804617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
805617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
806617ba13bSMingming Cao 	.write_info	= ext4_write_info
807ac27a0ecSDave Kleikamp };
808ac27a0ecSDave Kleikamp 
809617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
810617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
811ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
812ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
813ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
814ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
815ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
816ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
817ac27a0ecSDave Kleikamp };
818ac27a0ecSDave Kleikamp #endif
819ac27a0ecSDave Kleikamp 
820ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
821617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
822617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
823617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
824617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
825617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
826617ba13bSMingming Cao 	.put_super	= ext4_put_super,
827617ba13bSMingming Cao 	.write_super	= ext4_write_super,
828617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
829617ba13bSMingming Cao 	.write_super_lockfs = ext4_write_super_lockfs,
830617ba13bSMingming Cao 	.unlockfs	= ext4_unlockfs,
831617ba13bSMingming Cao 	.statfs		= ext4_statfs,
832617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
833617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
834617ba13bSMingming Cao 	.show_options	= ext4_show_options,
835ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
836617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
837617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
838ac27a0ecSDave Kleikamp #endif
839ac27a0ecSDave Kleikamp };
840ac27a0ecSDave Kleikamp 
84139655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
8421b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
8431b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
844617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
845ac27a0ecSDave Kleikamp };
846ac27a0ecSDave Kleikamp 
847ac27a0ecSDave Kleikamp enum {
848ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
849ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
85001436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
851ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
852ac27a0ecSDave Kleikamp 	Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
853ac27a0ecSDave Kleikamp 	Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
854818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
855ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
8565bf5683aSHidehiro Kawai 	Opt_data_err_abort, Opt_data_err_ignore,
857ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
858ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
859ac27a0ecSDave Kleikamp 	Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
86025ec56b5SJean Noel Cordenner 	Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
86101436ef2STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc,
862240799cdSTheodore Ts'o 	Opt_inode_readahead_blks
863ac27a0ecSDave Kleikamp };
864ac27a0ecSDave Kleikamp 
865a447c093SSteven Whitehouse static const match_table_t tokens = {
866ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
867ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
868ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
869ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
870ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
871ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
872ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
873ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
874ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
875ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
876ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
877ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
878ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
879ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
880ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
881ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
882ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
883ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
884ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
885ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
886ac27a0ecSDave Kleikamp 	{Opt_reservation, "reservation"},
887ac27a0ecSDave Kleikamp 	{Opt_noreservation, "noreservation"},
888ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
889ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
890ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
891ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
892ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
893ac27a0ecSDave Kleikamp 	{Opt_journal_inum, "journal=%u"},
894ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
895818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
896818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
897ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
898ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
899ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
900ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
9015bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
9025bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
903ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
904ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
905ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
906ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
907ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
908ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
909ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
910ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
911ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
912ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
913ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
914a86c6181SAlex Tomas 	{Opt_extents, "extents"},
9151e2462f9SMingming Cao 	{Opt_noextents, "noextents"},
91625ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
917c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
918ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
91964769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
920dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
921240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
922f3f12faaSJosef Bacik 	{Opt_err, NULL},
923ac27a0ecSDave Kleikamp };
924ac27a0ecSDave Kleikamp 
925617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
926ac27a0ecSDave Kleikamp {
927617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
928ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
929ac27a0ecSDave Kleikamp 
930ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
931ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
932ac27a0ecSDave Kleikamp 	options += 3;
933617ba13bSMingming Cao 	/*todo: use simple_strtoll with >32bit ext4 */
934ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
935ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
9364776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
937ac27a0ecSDave Kleikamp 		       (char *) *data);
938ac27a0ecSDave Kleikamp 		return 1;
939ac27a0ecSDave Kleikamp 	}
940ac27a0ecSDave Kleikamp 	if (*options == ',')
941ac27a0ecSDave Kleikamp 		options++;
942ac27a0ecSDave Kleikamp 	*data = (void *) options;
943ac27a0ecSDave Kleikamp 	return sb_block;
944ac27a0ecSDave Kleikamp }
945ac27a0ecSDave Kleikamp 
946ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
947ac27a0ecSDave Kleikamp 			 unsigned int *inum, unsigned long *journal_devnum,
948617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
949ac27a0ecSDave Kleikamp {
950617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
951ac27a0ecSDave Kleikamp 	char *p;
952ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
953ac27a0ecSDave Kleikamp 	int data_opt = 0;
954ac27a0ecSDave Kleikamp 	int option;
955ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
956dfc5d03fSJan Kara 	int qtype, qfmt;
957ac27a0ecSDave Kleikamp 	char *qname;
958ac27a0ecSDave Kleikamp #endif
959c07651b5SAneesh Kumar K.V 	ext4_fsblk_t last_block;
960ac27a0ecSDave Kleikamp 
961ac27a0ecSDave Kleikamp 	if (!options)
962ac27a0ecSDave Kleikamp 		return 1;
963ac27a0ecSDave Kleikamp 
964ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
965ac27a0ecSDave Kleikamp 		int token;
966ac27a0ecSDave Kleikamp 		if (!*p)
967ac27a0ecSDave Kleikamp 			continue;
968ac27a0ecSDave Kleikamp 
969ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
970ac27a0ecSDave Kleikamp 		switch (token) {
971ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
972ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
973ac27a0ecSDave Kleikamp 			break;
974ac27a0ecSDave Kleikamp 		case Opt_minix_df:
975ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
976ac27a0ecSDave Kleikamp 			break;
977ac27a0ecSDave Kleikamp 		case Opt_grpid:
978ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
979ac27a0ecSDave Kleikamp 			break;
980ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
981ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
982ac27a0ecSDave Kleikamp 			break;
983ac27a0ecSDave Kleikamp 		case Opt_resuid:
984ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
985ac27a0ecSDave Kleikamp 				return 0;
986ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
987ac27a0ecSDave Kleikamp 			break;
988ac27a0ecSDave Kleikamp 		case Opt_resgid:
989ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
990ac27a0ecSDave Kleikamp 				return 0;
991ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
992ac27a0ecSDave Kleikamp 			break;
993ac27a0ecSDave Kleikamp 		case Opt_sb:
994ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
995ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
996ac27a0ecSDave Kleikamp 			break;
997ac27a0ecSDave Kleikamp 		case Opt_err_panic:
998ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
999ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1000ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1001ac27a0ecSDave Kleikamp 			break;
1002ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1003ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1004ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1005ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1006ac27a0ecSDave Kleikamp 			break;
1007ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1008ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1009ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1010ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1011ac27a0ecSDave Kleikamp 			break;
1012ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1013ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1014ac27a0ecSDave Kleikamp 			break;
1015ac27a0ecSDave Kleikamp 		case Opt_debug:
1016ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1017ac27a0ecSDave Kleikamp 			break;
1018ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1019ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1020ac27a0ecSDave Kleikamp 			break;
1021ac27a0ecSDave Kleikamp 		case Opt_orlov:
1022ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1023ac27a0ecSDave Kleikamp 			break;
102403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1025ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1026ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1027ac27a0ecSDave Kleikamp 			break;
1028ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1029ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1030ac27a0ecSDave Kleikamp 			break;
1031ac27a0ecSDave Kleikamp #else
1032ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1033ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
10344776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)user_xattr options "
10354776004fSTheodore Ts'o 			       "not supported\n");
1036ac27a0ecSDave Kleikamp 			break;
1037ac27a0ecSDave Kleikamp #endif
103803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1039ac27a0ecSDave Kleikamp 		case Opt_acl:
1040ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1041ac27a0ecSDave Kleikamp 			break;
1042ac27a0ecSDave Kleikamp 		case Opt_noacl:
1043ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1044ac27a0ecSDave Kleikamp 			break;
1045ac27a0ecSDave Kleikamp #else
1046ac27a0ecSDave Kleikamp 		case Opt_acl:
1047ac27a0ecSDave Kleikamp 		case Opt_noacl:
10484776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)acl options "
10494776004fSTheodore Ts'o 			       "not supported\n");
1050ac27a0ecSDave Kleikamp 			break;
1051ac27a0ecSDave Kleikamp #endif
1052ac27a0ecSDave Kleikamp 		case Opt_reservation:
1053ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, RESERVATION);
1054ac27a0ecSDave Kleikamp 			break;
1055ac27a0ecSDave Kleikamp 		case Opt_noreservation:
1056ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, RESERVATION);
1057ac27a0ecSDave Kleikamp 			break;
1058ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1059ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1060ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1061ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1062ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1063ac27a0ecSDave Kleikamp 			   journal file. */
1064ac27a0ecSDave Kleikamp 			if (is_remount) {
1065617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1066ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1067ac27a0ecSDave Kleikamp 				return 0;
1068ac27a0ecSDave Kleikamp 			}
1069ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1070ac27a0ecSDave Kleikamp 			break;
1071ac27a0ecSDave Kleikamp 		case Opt_journal_inum:
1072ac27a0ecSDave Kleikamp 			if (is_remount) {
1073617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1074ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1075ac27a0ecSDave Kleikamp 				return 0;
1076ac27a0ecSDave Kleikamp 			}
1077ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1078ac27a0ecSDave Kleikamp 				return 0;
1079ac27a0ecSDave Kleikamp 			*inum = option;
1080ac27a0ecSDave Kleikamp 			break;
1081ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1082ac27a0ecSDave Kleikamp 			if (is_remount) {
1083617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1084ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1085ac27a0ecSDave Kleikamp 				return 0;
1086ac27a0ecSDave Kleikamp 			}
1087ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1088ac27a0ecSDave Kleikamp 				return 0;
1089ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1090ac27a0ecSDave Kleikamp 			break;
1091818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1092818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1093818d276cSGirish Shilamkar 			break;
1094818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1095818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1096818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1097818d276cSGirish Shilamkar 			break;
1098ac27a0ecSDave Kleikamp 		case Opt_noload:
1099ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1100ac27a0ecSDave Kleikamp 			break;
1101ac27a0ecSDave Kleikamp 		case Opt_commit:
1102ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1103ac27a0ecSDave Kleikamp 				return 0;
1104ac27a0ecSDave Kleikamp 			if (option < 0)
1105ac27a0ecSDave Kleikamp 				return 0;
1106ac27a0ecSDave Kleikamp 			if (option == 0)
1107cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1108ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1109ac27a0ecSDave Kleikamp 			break;
1110ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1111617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1112ac27a0ecSDave Kleikamp 			goto datacheck;
1113ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1114617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1115ac27a0ecSDave Kleikamp 			goto datacheck;
1116ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1117617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1118ac27a0ecSDave Kleikamp 		datacheck:
1119ac27a0ecSDave Kleikamp 			if (is_remount) {
1120617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1121ac27a0ecSDave Kleikamp 						!= data_opt) {
1122ac27a0ecSDave Kleikamp 					printk(KERN_ERR
1123617ba13bSMingming Cao 						"EXT4-fs: cannot change data "
1124ac27a0ecSDave Kleikamp 						"mode on remount\n");
1125ac27a0ecSDave Kleikamp 					return 0;
1126ac27a0ecSDave Kleikamp 				}
1127ac27a0ecSDave Kleikamp 			} else {
1128617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1129ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1130ac27a0ecSDave Kleikamp 			}
1131ac27a0ecSDave Kleikamp 			break;
11325bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
11335bf5683aSHidehiro Kawai 			set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
11345bf5683aSHidehiro Kawai 			break;
11355bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
11365bf5683aSHidehiro Kawai 			clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
11375bf5683aSHidehiro Kawai 			break;
1138ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1139ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1140ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1141ac27a0ecSDave Kleikamp 			goto set_qf_name;
1142ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1143ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1144ac27a0ecSDave Kleikamp set_qf_name:
1145dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1146dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1147dfc5d03fSJan Kara 			    !sbi->s_qf_names[qtype]) {
1148ac27a0ecSDave Kleikamp 				printk(KERN_ERR
11492c8be6b2SJan Kara 				       "EXT4-fs: Cannot change journaled "
1150ac27a0ecSDave Kleikamp 				       "quota options when quota turned on.\n");
1151ac27a0ecSDave Kleikamp 				return 0;
1152ac27a0ecSDave Kleikamp 			}
1153ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1154ac27a0ecSDave Kleikamp 			if (!qname) {
1155ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1156617ba13bSMingming Cao 					"EXT4-fs: not enough memory for "
1157ac27a0ecSDave Kleikamp 					"storing quotafile name.\n");
1158ac27a0ecSDave Kleikamp 				return 0;
1159ac27a0ecSDave Kleikamp 			}
1160ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1161ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1162ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1163617ba13bSMingming Cao 					"EXT4-fs: %s quota file already "
1164ac27a0ecSDave Kleikamp 					"specified.\n", QTYPE2NAME(qtype));
1165ac27a0ecSDave Kleikamp 				kfree(qname);
1166ac27a0ecSDave Kleikamp 				return 0;
1167ac27a0ecSDave Kleikamp 			}
1168ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1169ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1170ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1171617ba13bSMingming Cao 					"EXT4-fs: quotafile must be on "
1172ac27a0ecSDave Kleikamp 					"filesystem root.\n");
1173ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1174ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1175ac27a0ecSDave Kleikamp 				return 0;
1176ac27a0ecSDave Kleikamp 			}
1177ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1178ac27a0ecSDave Kleikamp 			break;
1179ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1180ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1181ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1182ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1183ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1184ac27a0ecSDave Kleikamp clear_qf_name:
1185dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1186dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1187dfc5d03fSJan Kara 			    sbi->s_qf_names[qtype]) {
1188617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change "
11892c8be6b2SJan Kara 					"journaled quota options when "
1190ac27a0ecSDave Kleikamp 					"quota turned on.\n");
1191ac27a0ecSDave Kleikamp 				return 0;
1192ac27a0ecSDave Kleikamp 			}
1193ac27a0ecSDave Kleikamp 			/*
1194ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1195ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1196ac27a0ecSDave Kleikamp 			 */
1197ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1198ac27a0ecSDave Kleikamp 			break;
1199ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1200dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1201dfc5d03fSJan Kara 			goto set_qf_format;
1202ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1203dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
1204dfc5d03fSJan Kara set_qf_format:
1205dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1206dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1207dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1208dfc5d03fSJan Kara 				printk(KERN_ERR "EXT4-fs: Cannot change "
1209dfc5d03fSJan Kara 					"journaled quota options when "
1210dfc5d03fSJan Kara 					"quota turned on.\n");
1211dfc5d03fSJan Kara 				return 0;
1212dfc5d03fSJan Kara 			}
1213dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1214ac27a0ecSDave Kleikamp 			break;
1215ac27a0ecSDave Kleikamp 		case Opt_quota:
1216ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1217ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1218ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1219ac27a0ecSDave Kleikamp 			break;
1220ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1221ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1222ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1223ac27a0ecSDave Kleikamp 			break;
1224ac27a0ecSDave Kleikamp 		case Opt_noquota:
1225ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1226617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change quota "
1227ac27a0ecSDave Kleikamp 					"options when quota turned on.\n");
1228ac27a0ecSDave Kleikamp 				return 0;
1229ac27a0ecSDave Kleikamp 			}
1230ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1231ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1232ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1233ac27a0ecSDave Kleikamp 			break;
1234ac27a0ecSDave Kleikamp #else
1235ac27a0ecSDave Kleikamp 		case Opt_quota:
1236ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1237ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1238cd59e7b9SJan Kara 			printk(KERN_ERR
1239cd59e7b9SJan Kara 				"EXT4-fs: quota options not supported.\n");
1240cd59e7b9SJan Kara 			break;
1241ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1242ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1243ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1244ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1245ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1246ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1247ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1248cd59e7b9SJan Kara 				"EXT4-fs: journaled quota options not "
1249ac27a0ecSDave Kleikamp 				"supported.\n");
1250ac27a0ecSDave Kleikamp 			break;
1251ac27a0ecSDave Kleikamp 		case Opt_noquota:
1252ac27a0ecSDave Kleikamp 			break;
1253ac27a0ecSDave Kleikamp #endif
1254ac27a0ecSDave Kleikamp 		case Opt_abort:
1255ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ABORT);
1256ac27a0ecSDave Kleikamp 			break;
1257ac27a0ecSDave Kleikamp 		case Opt_barrier:
1258ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1259ac27a0ecSDave Kleikamp 				return 0;
1260ac27a0ecSDave Kleikamp 			if (option)
1261ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1262ac27a0ecSDave Kleikamp 			else
1263ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1264ac27a0ecSDave Kleikamp 			break;
1265ac27a0ecSDave Kleikamp 		case Opt_ignore:
1266ac27a0ecSDave Kleikamp 			break;
1267ac27a0ecSDave Kleikamp 		case Opt_resize:
1268ac27a0ecSDave Kleikamp 			if (!is_remount) {
1269617ba13bSMingming Cao 				printk("EXT4-fs: resize option only available "
1270ac27a0ecSDave Kleikamp 					"for remount\n");
1271ac27a0ecSDave Kleikamp 				return 0;
1272ac27a0ecSDave Kleikamp 			}
1273ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1274ac27a0ecSDave Kleikamp 				return 0;
1275ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1276ac27a0ecSDave Kleikamp 			break;
1277ac27a0ecSDave Kleikamp 		case Opt_nobh:
1278ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1279ac27a0ecSDave Kleikamp 			break;
1280ac27a0ecSDave Kleikamp 		case Opt_bh:
1281ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1282ac27a0ecSDave Kleikamp 			break;
1283a86c6181SAlex Tomas 		case Opt_extents:
1284e4079a11SEric Sandeen 			if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1285e4079a11SEric Sandeen 					EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1286e4079a11SEric Sandeen 				ext4_warning(sb, __func__,
1287e4079a11SEric Sandeen 					"extents feature not enabled "
1288e4079a11SEric Sandeen 					"on this filesystem, use tune2fs\n");
1289e4079a11SEric Sandeen 				return 0;
1290e4079a11SEric Sandeen 			}
1291a86c6181SAlex Tomas 			set_opt(sbi->s_mount_opt, EXTENTS);
1292a86c6181SAlex Tomas 			break;
12931e2462f9SMingming Cao 		case Opt_noextents:
1294c07651b5SAneesh Kumar K.V 			/*
1295c07651b5SAneesh Kumar K.V 			 * When e2fsprogs support resizing an already existing
1296c07651b5SAneesh Kumar K.V 			 * ext3 file system to greater than 2**32 we need to
1297c07651b5SAneesh Kumar K.V 			 * add support to block allocator to handle growing
1298c07651b5SAneesh Kumar K.V 			 * already existing block  mapped inode so that blocks
1299c07651b5SAneesh Kumar K.V 			 * allocated for them fall within 2**32
1300c07651b5SAneesh Kumar K.V 			 */
1301c07651b5SAneesh Kumar K.V 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1302c07651b5SAneesh Kumar K.V 			if (last_block  > 0xffffffffULL) {
1303c07651b5SAneesh Kumar K.V 				printk(KERN_ERR "EXT4-fs: Filesystem too "
1304c07651b5SAneesh Kumar K.V 						"large to mount with "
1305c07651b5SAneesh Kumar K.V 						"-o noextents options\n");
1306c07651b5SAneesh Kumar K.V 				return 0;
1307c07651b5SAneesh Kumar K.V 			}
13081e2462f9SMingming Cao 			clear_opt(sbi->s_mount_opt, EXTENTS);
13091e2462f9SMingming Cao 			break;
131025ec56b5SJean Noel Cordenner 		case Opt_i_version:
131125ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
131225ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
131325ec56b5SJean Noel Cordenner 			break;
1314dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1315dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1316dd919b98SAneesh Kumar K.V 			break;
1317c9de560dSAlex Tomas 		case Opt_stripe:
1318c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1319c9de560dSAlex Tomas 				return 0;
1320c9de560dSAlex Tomas 			if (option < 0)
1321c9de560dSAlex Tomas 				return 0;
1322c9de560dSAlex Tomas 			sbi->s_stripe = option;
1323c9de560dSAlex Tomas 			break;
132464769240SAlex Tomas 		case Opt_delalloc:
132564769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
132664769240SAlex Tomas 			break;
1327240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1328240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1329240799cdSTheodore Ts'o 				return 0;
1330240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1331240799cdSTheodore Ts'o 				return 0;
1332240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1333240799cdSTheodore Ts'o 			break;
1334ac27a0ecSDave Kleikamp 		default:
1335ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1336617ba13bSMingming Cao 			       "EXT4-fs: Unrecognized mount option \"%s\" "
1337ac27a0ecSDave Kleikamp 			       "or missing value\n", p);
1338ac27a0ecSDave Kleikamp 			return 0;
1339ac27a0ecSDave Kleikamp 		}
1340ac27a0ecSDave Kleikamp 	}
1341ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1342ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1343617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1344ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1345ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1346ac27a0ecSDave Kleikamp 
1347617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1348ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1349ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1350ac27a0ecSDave Kleikamp 
1351ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1352617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1353ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1354617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1355617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: old and new quota "
1356ac27a0ecSDave Kleikamp 					"format mixing.\n");
1357ac27a0ecSDave Kleikamp 			return 0;
1358ac27a0ecSDave Kleikamp 		}
1359ac27a0ecSDave Kleikamp 
1360ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
13612c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
1362ac27a0ecSDave Kleikamp 					"not specified.\n");
1363ac27a0ecSDave Kleikamp 			return 0;
1364ac27a0ecSDave Kleikamp 		}
1365ac27a0ecSDave Kleikamp 	} else {
1366ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
13672c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
13682c8be6b2SJan Kara 					"specified with no journaling "
1369ac27a0ecSDave Kleikamp 					"enabled.\n");
1370ac27a0ecSDave Kleikamp 			return 0;
1371ac27a0ecSDave Kleikamp 		}
1372ac27a0ecSDave Kleikamp 	}
1373ac27a0ecSDave Kleikamp #endif
1374ac27a0ecSDave Kleikamp 	return 1;
1375ac27a0ecSDave Kleikamp }
1376ac27a0ecSDave Kleikamp 
1377617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1378ac27a0ecSDave Kleikamp 			    int read_only)
1379ac27a0ecSDave Kleikamp {
1380617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1381ac27a0ecSDave Kleikamp 	int res = 0;
1382ac27a0ecSDave Kleikamp 
1383617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1384617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1385ac27a0ecSDave Kleikamp 		       "forcing read-only mode\n");
1386ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1387ac27a0ecSDave Kleikamp 	}
1388ac27a0ecSDave Kleikamp 	if (read_only)
1389ac27a0ecSDave Kleikamp 		return res;
1390617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1391617ba13bSMingming Cao 		printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1392ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1393617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1394ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1395617ba13bSMingming Cao 		       "EXT4-fs warning: mounting fs with errors, "
1396ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1397ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1398ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1399ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1400ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1401617ba13bSMingming Cao 		       "EXT4-fs warning: maximal mount count reached, "
1402ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1403ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1404ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1405ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1406ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1407617ba13bSMingming Cao 		       "EXT4-fs warning: checktime reached, "
1408ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1409ac27a0ecSDave Kleikamp #if 0
1410ac27a0ecSDave Kleikamp 		/* @@@ We _will_ want to clear the valid bit if we find
141163f57933SAndrew Morton 		 * inconsistencies, to force a fsck at reboot.  But for
141263f57933SAndrew Morton 		 * a plain journaled filesystem we can keep it set as
141363f57933SAndrew Morton 		 * valid forever! :)
141463f57933SAndrew Morton 		 */
1415216c34b2SMarcin Slusarz 	es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1416ac27a0ecSDave Kleikamp #endif
1417ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1418617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1419e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1420ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1421617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
1422617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1423ac27a0ecSDave Kleikamp 
1424617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
1425ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1426617ba13bSMingming Cao 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1427ac27a0ecSDave Kleikamp 				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
1428ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1429ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1430617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1431617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1432ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1433ac27a0ecSDave Kleikamp 
143405496769STheodore Ts'o 	printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
143505496769STheodore Ts'o 	       sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
143605496769STheodore Ts'o 	       "external", EXT4_SB(sb)->s_journal->j_devname);
1437ac27a0ecSDave Kleikamp 	return res;
1438ac27a0ecSDave Kleikamp }
1439ac27a0ecSDave Kleikamp 
1440772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1441772cb7c8SJose R. Santos {
1442772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1443772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1444772cb7c8SJose R. Santos 	struct buffer_head *bh;
1445772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1446772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1447772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1448772cb7c8SJose R. Santos 	__u64 block_bitmap = 0;
1449772cb7c8SJose R. Santos 	int i;
1450772cb7c8SJose R. Santos 
1451772cb7c8SJose R. Santos 	if (!sbi->s_es->s_log_groups_per_flex) {
1452772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1453772cb7c8SJose R. Santos 		return 1;
1454772cb7c8SJose R. Santos 	}
1455772cb7c8SJose R. Santos 
1456772cb7c8SJose R. Santos 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1457772cb7c8SJose R. Santos 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1458772cb7c8SJose R. Santos 
1459c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1460c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1461d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1462d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1463ec05e868SLi Zefan 	sbi->s_flex_groups = kzalloc(flex_group_count *
1464772cb7c8SJose R. Santos 				     sizeof(struct flex_groups), GFP_KERNEL);
1465772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1466ec05e868SLi Zefan 		printk(KERN_ERR "EXT4-fs: not enough memory for "
1467ec05e868SLi Zefan 				"%lu flex groups\n", flex_group_count);
1468772cb7c8SJose R. Santos 		goto failed;
1469772cb7c8SJose R. Santos 	}
1470772cb7c8SJose R. Santos 
1471772cb7c8SJose R. Santos 	gdp = ext4_get_group_desc(sb, 1, &bh);
1472772cb7c8SJose R. Santos 	block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
1473772cb7c8SJose R. Santos 
1474772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
1475772cb7c8SJose R. Santos 		gdp = ext4_get_group_desc(sb, i, &bh);
1476772cb7c8SJose R. Santos 
1477772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
1478772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_inodes +=
1479772cb7c8SJose R. Santos 			le16_to_cpu(gdp->bg_free_inodes_count);
1480772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_blocks +=
1481772cb7c8SJose R. Santos 			le16_to_cpu(gdp->bg_free_blocks_count);
1482772cb7c8SJose R. Santos 	}
1483772cb7c8SJose R. Santos 
1484772cb7c8SJose R. Santos 	return 1;
1485772cb7c8SJose R. Santos failed:
1486772cb7c8SJose R. Santos 	return 0;
1487772cb7c8SJose R. Santos }
1488772cb7c8SJose R. Santos 
1489717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1490717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1491717d50e4SAndreas Dilger {
1492717d50e4SAndreas Dilger 	__u16 crc = 0;
1493717d50e4SAndreas Dilger 
1494717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1495717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1496717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1497717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1498717d50e4SAndreas Dilger 
1499717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1500717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1501717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1502717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1503717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1504717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1505717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1506717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1507717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1508717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1509717d50e4SAndreas Dilger 					offset);
1510717d50e4SAndreas Dilger 	}
1511717d50e4SAndreas Dilger 
1512717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1513717d50e4SAndreas Dilger }
1514717d50e4SAndreas Dilger 
1515717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1516717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1517717d50e4SAndreas Dilger {
1518717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1519717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1520717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1521717d50e4SAndreas Dilger 		return 0;
1522717d50e4SAndreas Dilger 
1523717d50e4SAndreas Dilger 	return 1;
1524717d50e4SAndreas Dilger }
1525717d50e4SAndreas Dilger 
1526ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1527617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1528ac27a0ecSDave Kleikamp {
1529617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1530617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1531617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1532bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1533bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1534bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1535ce421581SJose R. Santos 	int flexbg_flag = 0;
1536fd2d4291SAvantika Mathur 	ext4_group_t i;
1537ac27a0ecSDave Kleikamp 
1538ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1539ce421581SJose R. Santos 		flexbg_flag = 1;
1540ce421581SJose R. Santos 
1541617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1542ac27a0ecSDave Kleikamp 
1543197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1544197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1545197cd65aSAkinobu Mita 
1546ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1547bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1548ac27a0ecSDave Kleikamp 		else
1549ac27a0ecSDave Kleikamp 			last_block = first_block +
1550617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1551ac27a0ecSDave Kleikamp 
15528fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
15532b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1554c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1555c19204b0SJosef Bacik 			       "Block bitmap for group %lu not in group "
15565128273aSEric Sesterhenn 			       "(block %llu)!\n", i, block_bitmap);
1557ac27a0ecSDave Kleikamp 			return 0;
1558ac27a0ecSDave Kleikamp 		}
15598fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
15602b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1561c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1562c19204b0SJosef Bacik 			       "Inode bitmap for group %lu not in group "
15635128273aSEric Sesterhenn 			       "(block %llu)!\n", i, inode_bitmap);
1564ac27a0ecSDave Kleikamp 			return 0;
1565ac27a0ecSDave Kleikamp 		}
15668fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1567bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
15682b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1569c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1570c19204b0SJosef Bacik 			       "Inode table for group %lu not in group "
15715128273aSEric Sesterhenn 			       "(block %llu)!\n", i, inode_table);
1572ac27a0ecSDave Kleikamp 			return 0;
1573ac27a0ecSDave Kleikamp 		}
1574b5f10eedSEric Sandeen 		spin_lock(sb_bgl_lock(sbi, i));
1575717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1576c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1577fd2d4291SAvantika Mathur 			       "Checksum for group %lu failed (%u!=%u)\n",
1578fd2d4291SAvantika Mathur 			       i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1579fd2d4291SAvantika Mathur 			       gdp)), le16_to_cpu(gdp->bg_checksum));
15807ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
15817ee1ec4cSLi Zefan 				spin_unlock(sb_bgl_lock(sbi, i));
1582717d50e4SAndreas Dilger 				return 0;
1583717d50e4SAndreas Dilger 			}
15847ee1ec4cSLi Zefan 		}
1585b5f10eedSEric Sandeen 		spin_unlock(sb_bgl_lock(sbi, i));
1586ce421581SJose R. Santos 		if (!flexbg_flag)
1587617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1588ac27a0ecSDave Kleikamp 	}
1589ac27a0ecSDave Kleikamp 
1590bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1591617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1592ac27a0ecSDave Kleikamp 	return 1;
1593ac27a0ecSDave Kleikamp }
1594ac27a0ecSDave Kleikamp 
1595617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1596ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1597ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1598ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1599ac27a0ecSDave Kleikamp  *
1600ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1601ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1602ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1603ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1604ac27a0ecSDave Kleikamp  *
1605ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1606ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1607ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1608617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1609ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1610ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1611ac27a0ecSDave Kleikamp  */
1612617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1613617ba13bSMingming Cao 				struct ext4_super_block *es)
1614ac27a0ecSDave Kleikamp {
1615ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1616ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1617ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1618ac27a0ecSDave Kleikamp 	int i;
1619ac27a0ecSDave Kleikamp #endif
1620ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1621ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1622ac27a0ecSDave Kleikamp 		return;
1623ac27a0ecSDave Kleikamp 	}
1624ac27a0ecSDave Kleikamp 
1625a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1626a8f48a95SEric Sandeen 		printk(KERN_ERR "EXT4-fs: write access "
1627a8f48a95SEric Sandeen 			"unavailable, skipping orphan cleanup.\n");
1628a8f48a95SEric Sandeen 		return;
1629a8f48a95SEric Sandeen 	}
1630a8f48a95SEric Sandeen 
1631617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1632ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1633ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1634ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1635ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1636ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1637ac27a0ecSDave Kleikamp 		return;
1638ac27a0ecSDave Kleikamp 	}
1639ac27a0ecSDave Kleikamp 
1640ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1641617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1642ac27a0ecSDave Kleikamp 		       sb->s_id);
1643ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1644ac27a0ecSDave Kleikamp 	}
1645ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1646ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1647ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1648ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1649ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1650617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1651617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1652ac27a0ecSDave Kleikamp 			if (ret < 0)
1653ac27a0ecSDave Kleikamp 				printk(KERN_ERR
16542c8be6b2SJan Kara 					"EXT4-fs: Cannot turn on journaled "
1655ac27a0ecSDave Kleikamp 					"quota: error %d\n", ret);
1656ac27a0ecSDave Kleikamp 		}
1657ac27a0ecSDave Kleikamp 	}
1658ac27a0ecSDave Kleikamp #endif
1659ac27a0ecSDave Kleikamp 
1660ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1661ac27a0ecSDave Kleikamp 		struct inode *inode;
1662ac27a0ecSDave Kleikamp 
166397bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
166497bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
1665ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1666ac27a0ecSDave Kleikamp 			break;
1667ac27a0ecSDave Kleikamp 		}
1668ac27a0ecSDave Kleikamp 
1669617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1670ac27a0ecSDave Kleikamp 		DQUOT_INIT(inode);
1671ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1672ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1673e5f8eab8STheodore Ts'o 				"%s: truncating inode %lu to %lld bytes\n",
167446e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
1675e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1676ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1677617ba13bSMingming Cao 			ext4_truncate(inode);
1678ac27a0ecSDave Kleikamp 			nr_truncates++;
1679ac27a0ecSDave Kleikamp 		} else {
1680ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1681ac27a0ecSDave Kleikamp 				"%s: deleting unreferenced inode %lu\n",
168246e665e9SHarvey Harrison 				__func__, inode->i_ino);
1683ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1684ac27a0ecSDave Kleikamp 				  inode->i_ino);
1685ac27a0ecSDave Kleikamp 			nr_orphans++;
1686ac27a0ecSDave Kleikamp 		}
1687ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1688ac27a0ecSDave Kleikamp 	}
1689ac27a0ecSDave Kleikamp 
1690ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1691ac27a0ecSDave Kleikamp 
1692ac27a0ecSDave Kleikamp 	if (nr_orphans)
1693617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1694ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_orphans));
1695ac27a0ecSDave Kleikamp 	if (nr_truncates)
1696617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1697ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_truncates));
1698ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1699ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1700ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1701ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
17026f28e087SJan Kara 			vfs_quota_off(sb, i, 0);
1703ac27a0ecSDave Kleikamp 	}
1704ac27a0ecSDave Kleikamp #endif
1705ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1706ac27a0ecSDave Kleikamp }
1707cd2291a4SEric Sandeen /*
1708cd2291a4SEric Sandeen  * Maximal extent format file size.
1709cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1710cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1711cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1712cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1713cd2291a4SEric Sandeen  *
1714cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1715cd2291a4SEric Sandeen  */
1716f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
1717cd2291a4SEric Sandeen {
1718cd2291a4SEric Sandeen 	loff_t res;
1719cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
1720cd2291a4SEric Sandeen 
1721cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
1722f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1723cd2291a4SEric Sandeen 		/*
1724*b3a6ffe1SJens Axboe 		 * CONFIG_LBD is not enabled implies the inode
1725cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
1726cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
1727cd2291a4SEric Sandeen 		 */
1728cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
1729cd2291a4SEric Sandeen 
1730cd2291a4SEric Sandeen 		/* total blocks in file system block size */
1731cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
1732cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
1733cd2291a4SEric Sandeen 	}
1734cd2291a4SEric Sandeen 
1735cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
1736cd2291a4SEric Sandeen 	res = 1LL << 32;
1737cd2291a4SEric Sandeen 	res <<= blkbits;
1738cd2291a4SEric Sandeen 	res -= 1;
1739cd2291a4SEric Sandeen 
1740cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
1741cd2291a4SEric Sandeen 	if (res > upper_limit)
1742cd2291a4SEric Sandeen 		res = upper_limit;
1743cd2291a4SEric Sandeen 
1744cd2291a4SEric Sandeen 	return res;
1745cd2291a4SEric Sandeen }
1746ac27a0ecSDave Kleikamp 
1747ac27a0ecSDave Kleikamp /*
1748cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
17490fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
17500fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
1751ac27a0ecSDave Kleikamp  */
1752f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1753ac27a0ecSDave Kleikamp {
1754617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
17550fc1b451SAneesh Kumar K.V 	int meta_blocks;
17560fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
17570fc1b451SAneesh Kumar K.V 	/* This is calculated to be the largest file size for a
1758cd2291a4SEric Sandeen 	 * dense, bitmapped file such that the total number of
1759ac27a0ecSDave Kleikamp 	 * sectors in the file, including data and all indirect blocks,
17600fc1b451SAneesh Kumar K.V 	 * does not exceed 2^48 -1
17610fc1b451SAneesh Kumar K.V 	 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
17620fc1b451SAneesh Kumar K.V 	 * total number of  512 bytes blocks of the file
17630fc1b451SAneesh Kumar K.V 	 */
17640fc1b451SAneesh Kumar K.V 
1765f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
17660fc1b451SAneesh Kumar K.V 		/*
1767*b3a6ffe1SJens Axboe 		 * !has_huge_files or CONFIG_LBD is not enabled
1768f287a1a5STheodore Ts'o 		 * implies the inode i_block represent total blocks in
1769f287a1a5STheodore Ts'o 		 * 512 bytes 32 == size of vfs inode i_blocks * 8
17700fc1b451SAneesh Kumar K.V 		 */
17710fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
17720fc1b451SAneesh Kumar K.V 
17730fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
17740fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
17750fc1b451SAneesh Kumar K.V 
17760fc1b451SAneesh Kumar K.V 	} else {
17778180a562SAneesh Kumar K.V 		/*
17788180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
17798180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
17808180a562SAneesh Kumar K.V 		 * represent total number of blocks in
17818180a562SAneesh Kumar K.V 		 * file system block size
17828180a562SAneesh Kumar K.V 		 */
17830fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
17840fc1b451SAneesh Kumar K.V 
17850fc1b451SAneesh Kumar K.V 	}
17860fc1b451SAneesh Kumar K.V 
17870fc1b451SAneesh Kumar K.V 	/* indirect blocks */
17880fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
17890fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
17900fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
17910fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
17920fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
17930fc1b451SAneesh Kumar K.V 
17940fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
17950fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
1796ac27a0ecSDave Kleikamp 
1797ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
1798ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
1799ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
1800ac27a0ecSDave Kleikamp 	res <<= bits;
1801ac27a0ecSDave Kleikamp 	if (res > upper_limit)
1802ac27a0ecSDave Kleikamp 		res = upper_limit;
18030fc1b451SAneesh Kumar K.V 
18040fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
18050fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
18060fc1b451SAneesh Kumar K.V 
1807ac27a0ecSDave Kleikamp 	return res;
1808ac27a0ecSDave Kleikamp }
1809ac27a0ecSDave Kleikamp 
1810617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
181170bbb3e0SAndrew Morton 				ext4_fsblk_t logical_sb_block, int nr)
1812ac27a0ecSDave Kleikamp {
1813617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1814fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
1815ac27a0ecSDave Kleikamp 	int has_super = 0;
1816ac27a0ecSDave Kleikamp 
1817ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1818ac27a0ecSDave Kleikamp 
1819617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1820ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
182170bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
1822ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
1823617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
1824ac27a0ecSDave Kleikamp 		has_super = 1;
1825617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
1826ac27a0ecSDave Kleikamp }
1827ac27a0ecSDave Kleikamp 
1828c9de560dSAlex Tomas /**
1829c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
1830c9de560dSAlex Tomas  * @sbi: In memory super block info
1831c9de560dSAlex Tomas  *
1832c9de560dSAlex Tomas  * If we have specified it via mount option, then
1833c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
1834c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
1835c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
1836c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
1837c9de560dSAlex Tomas  *
1838c9de560dSAlex Tomas  */
1839c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1840c9de560dSAlex Tomas {
1841c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1842c9de560dSAlex Tomas 	unsigned long stripe_width =
1843c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1844c9de560dSAlex Tomas 
1845c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1846c9de560dSAlex Tomas 		return sbi->s_stripe;
1847c9de560dSAlex Tomas 
1848c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
1849c9de560dSAlex Tomas 		return stripe_width;
1850c9de560dSAlex Tomas 
1851c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
1852c9de560dSAlex Tomas 		return stride;
1853c9de560dSAlex Tomas 
1854c9de560dSAlex Tomas 	return 0;
1855c9de560dSAlex Tomas }
1856ac27a0ecSDave Kleikamp 
1857617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
18587477827fSAneesh Kumar K.V 				__releases(kernel_lock)
18597477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
18601d03ec98SAneesh Kumar K.V 
1861ac27a0ecSDave Kleikamp {
1862ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
1863617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
1864617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1865617ba13bSMingming Cao 	ext4_fsblk_t block;
1866617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
186770bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
1868ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
1869ac27a0ecSDave Kleikamp 	unsigned int journal_inum = 0;
1870ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
1871ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
1872ac27a0ecSDave Kleikamp 	struct inode *root;
18739f6200bbSTheodore Ts'o 	char *cp;
18741d1fe1eeSDavid Howells 	int ret = -EINVAL;
1875ac27a0ecSDave Kleikamp 	int blocksize;
1876ac27a0ecSDave Kleikamp 	int db_count;
1877ac27a0ecSDave Kleikamp 	int i;
1878f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
1879ac27a0ecSDave Kleikamp 	__le32 features;
1880bd81d8eeSLaurent Vivier 	__u64 blocks_count;
1881833f4077SPeter Zijlstra 	int err;
1882ac27a0ecSDave Kleikamp 
1883ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1884ac27a0ecSDave Kleikamp 	if (!sbi)
1885ac27a0ecSDave Kleikamp 		return -ENOMEM;
1886ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
1887ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
1888617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
1889617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
1890240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
1891d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
1892ac27a0ecSDave Kleikamp 
1893ac27a0ecSDave Kleikamp 	unlock_kernel();
1894ac27a0ecSDave Kleikamp 
18959f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
18969f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
18979f6200bbSTheodore Ts'o 		*cp = '!';
18989f6200bbSTheodore Ts'o 
1899617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1900ac27a0ecSDave Kleikamp 	if (!blocksize) {
1901617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1902ac27a0ecSDave Kleikamp 		goto out_fail;
1903ac27a0ecSDave Kleikamp 	}
1904ac27a0ecSDave Kleikamp 
1905ac27a0ecSDave Kleikamp 	/*
1906617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
1907ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
1908ac27a0ecSDave Kleikamp 	 */
1909617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
191070bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
191170bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
1912ac27a0ecSDave Kleikamp 	} else {
191370bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
1914ac27a0ecSDave Kleikamp 	}
1915ac27a0ecSDave Kleikamp 
191670bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
1917617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
1918ac27a0ecSDave Kleikamp 		goto out_fail;
1919ac27a0ecSDave Kleikamp 	}
1920ac27a0ecSDave Kleikamp 	/*
1921ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
1922617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
1923ac27a0ecSDave Kleikamp 	 */
1924617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1925ac27a0ecSDave Kleikamp 	sbi->s_es = es;
1926ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
1927617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
1928617ba13bSMingming Cao 		goto cantfind_ext4;
1929ac27a0ecSDave Kleikamp 
1930ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
1931ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1932617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
1933ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
1934617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1935ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
1936617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
1937ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
193803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1939617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1940ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
19412e7842b8SHugh Dickins #endif
194203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1943617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
1944ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
19452e7842b8SHugh Dickins #endif
1946617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1947617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1948617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1949617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1950617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1951617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1952ac27a0ecSDave Kleikamp 
1953617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1954ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1955bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
1956ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
1957bb4f397aSAneesh Kumar K.V 	else
1958bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
1959ac27a0ecSDave Kleikamp 
1960ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1961ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1962ac27a0ecSDave Kleikamp 
1963ac27a0ecSDave Kleikamp 	set_opt(sbi->s_mount_opt, RESERVATION);
1964571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
1965ac27a0ecSDave Kleikamp 
19661e2462f9SMingming Cao 	/*
19671e2462f9SMingming Cao 	 * turn on extents feature by default in ext4 filesystem
1968e4079a11SEric Sandeen 	 * only if feature flag already set by mkfs or tune2fs.
1969e4079a11SEric Sandeen 	 * Use -o noextents to turn it off
19701e2462f9SMingming Cao 	 */
1971e4079a11SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
19721e2462f9SMingming Cao 		set_opt(sbi->s_mount_opt, EXTENTS);
1973e4079a11SEric Sandeen 	else
1974e4079a11SEric Sandeen 		ext4_warning(sb, __func__,
1975e4079a11SEric Sandeen 			"extents feature not enabled on this filesystem, "
1976e4079a11SEric Sandeen 			"use tune2fs.\n");
19771e2462f9SMingming Cao 
1978dd919b98SAneesh Kumar K.V 	/*
1979dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
1980dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
1981dd919b98SAneesh Kumar K.V 	 */
1982dd919b98SAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, DELALLOC);
1983dd919b98SAneesh Kumar K.V 
1984dd919b98SAneesh Kumar K.V 
1985ac27a0ecSDave Kleikamp 	if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
1986ac27a0ecSDave Kleikamp 			   NULL, 0))
1987ac27a0ecSDave Kleikamp 		goto failed_mount;
1988ac27a0ecSDave Kleikamp 
1989ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1990617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1991ac27a0ecSDave Kleikamp 
1992617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1993617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1994617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1995617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1996ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1997617ba13bSMingming Cao 		       "EXT4-fs warning: feature flags set on rev 0 fs, "
1998ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1999469108ffSTheodore Tso 
2000469108ffSTheodore Tso 	/*
2001ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2002ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2003ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2004ac27a0ecSDave Kleikamp 	 */
2005617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2006ac27a0ecSDave Kleikamp 	if (features) {
2007617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2008ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
2009ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
2010ac27a0ecSDave Kleikamp 		goto failed_mount;
2011ac27a0ecSDave Kleikamp 	}
2012617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2013ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
2014617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2015ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
2016ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
2017ac27a0ecSDave Kleikamp 		goto failed_mount;
2018ac27a0ecSDave Kleikamp 	}
2019f287a1a5STheodore Ts'o 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2020f287a1a5STheodore Ts'o 				    EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2021f287a1a5STheodore Ts'o 	if (has_huge_files) {
20220fc1b451SAneesh Kumar K.V 		/*
20230fc1b451SAneesh Kumar K.V 		 * Large file size enabled file system can only be
2024*b3a6ffe1SJens Axboe 		 * mount if kernel is build with CONFIG_LBD
20250fc1b451SAneesh Kumar K.V 		 */
20260fc1b451SAneesh Kumar K.V 		if (sizeof(root->i_blocks) < sizeof(u64) &&
20270fc1b451SAneesh Kumar K.V 				!(sb->s_flags & MS_RDONLY)) {
20280fc1b451SAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
20290fc1b451SAneesh Kumar K.V 					"files cannot be mounted read-write "
2030*b3a6ffe1SJens Axboe 					"without CONFIG_LBD.\n", sb->s_id);
20310fc1b451SAneesh Kumar K.V 			goto failed_mount;
20320fc1b451SAneesh Kumar K.V 		}
20330fc1b451SAneesh Kumar K.V 	}
2034ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2035ac27a0ecSDave Kleikamp 
2036617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2037617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2038ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2039617ba13bSMingming Cao 		       "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2040ac27a0ecSDave Kleikamp 		       blocksize, sb->s_id);
2041ac27a0ecSDave Kleikamp 		goto failed_mount;
2042ac27a0ecSDave Kleikamp 	}
2043ac27a0ecSDave Kleikamp 
2044ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2045ce40733cSAneesh Kumar K.V 
2046ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2047ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2048ce40733cSAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2049ce40733cSAneesh Kumar K.V 					blocksize);
2050ac27a0ecSDave Kleikamp 			goto failed_mount;
2051ac27a0ecSDave Kleikamp 		}
2052ac27a0ecSDave Kleikamp 
2053ac27a0ecSDave Kleikamp 		brelse(bh);
205470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
205570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
205670bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2057ac27a0ecSDave Kleikamp 		if (!bh) {
2058ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2059617ba13bSMingming Cao 			       "EXT4-fs: Can't read superblock on 2nd try.\n");
2060ac27a0ecSDave Kleikamp 			goto failed_mount;
2061ac27a0ecSDave Kleikamp 		}
2062617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2063ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2064617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2065ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2066617ba13bSMingming Cao 			       "EXT4-fs: Magic mismatch, very weird !\n");
2067ac27a0ecSDave Kleikamp 			goto failed_mount;
2068ac27a0ecSDave Kleikamp 		}
2069ac27a0ecSDave Kleikamp 	}
2070ac27a0ecSDave Kleikamp 
2071f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2072f287a1a5STheodore Ts'o 						      has_huge_files);
2073f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2074ac27a0ecSDave Kleikamp 
2075617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2076617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2077617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2078ac27a0ecSDave Kleikamp 	} else {
2079ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2080ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2081617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
20821330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2083ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2084ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2085617ba13bSMingming Cao 			       "EXT4-fs: unsupported inode size: %d\n",
2086ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2087ac27a0ecSDave Kleikamp 			goto failed_mount;
2088ac27a0ecSDave Kleikamp 		}
2089ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2090ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2091ac27a0ecSDave Kleikamp 	}
20920d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
20930d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
20948fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
20950d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2096d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
20970d1ee42fSAlexandre Ratchov 			printk(KERN_ERR
20988fadc143SAlexandre Ratchov 			       "EXT4-fs: unsupported descriptor size %lu\n",
20990d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
21000d1ee42fSAlexandre Ratchov 			goto failed_mount;
21010d1ee42fSAlexandre Ratchov 		}
21020d1ee42fSAlexandre Ratchov 	} else
21030d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2104ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2105ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2106b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2107617ba13bSMingming Cao 		goto cantfind_ext4;
2108617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2109ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2110617ba13bSMingming Cao 		goto cantfind_ext4;
2111ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2112ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
21130d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2114ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2115ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2116e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2117e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2118ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2119ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2120ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2121f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
2122f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
2123f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2124f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2125f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
2126f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2127f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2128f99b2589STheodore Ts'o #else
2129f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2130f99b2589STheodore Ts'o #endif
2131f99b2589STheodore Ts'o 		sb->s_dirt = 1;
2132f99b2589STheodore Ts'o 	}
2133ac27a0ecSDave Kleikamp 
2134ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2135ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2136617ba13bSMingming Cao 		       "EXT4-fs: #blocks per group too big: %lu\n",
2137ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2138ac27a0ecSDave Kleikamp 		goto failed_mount;
2139ac27a0ecSDave Kleikamp 	}
2140ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2141ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2142617ba13bSMingming Cao 		       "EXT4-fs: #inodes per group too big: %lu\n",
2143ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2144ac27a0ecSDave Kleikamp 		goto failed_mount;
2145ac27a0ecSDave Kleikamp 	}
2146ac27a0ecSDave Kleikamp 
2147bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
2148ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2149617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2150ac27a0ecSDave Kleikamp 			" too large to mount safely\n", sb->s_id);
2151ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
2152617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2153ac27a0ecSDave Kleikamp 					"enabled\n");
2154ac27a0ecSDave Kleikamp 		goto failed_mount;
2155ac27a0ecSDave Kleikamp 	}
2156ac27a0ecSDave Kleikamp 
2157617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2158617ba13bSMingming Cao 		goto cantfind_ext4;
2159e7c95593SEric Sandeen 
2160e7c95593SEric Sandeen 	/* ensure blocks_count calculation below doesn't sign-extend */
2161e7c95593SEric Sandeen 	if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2162e7c95593SEric Sandeen 	    le32_to_cpu(es->s_first_data_block) + 1) {
2163e7c95593SEric Sandeen 		printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2164e7c95593SEric Sandeen 		       "first data block %u, blocks per group %lu\n",
2165e7c95593SEric Sandeen 			ext4_blocks_count(es),
2166e7c95593SEric Sandeen 			le32_to_cpu(es->s_first_data_block),
2167e7c95593SEric Sandeen 			EXT4_BLOCKS_PER_GROUP(sb));
2168e7c95593SEric Sandeen 		goto failed_mount;
2169e7c95593SEric Sandeen 	}
2170bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2171bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2172bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2173bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2174bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2175617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2176617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2177ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2178ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2179ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2180617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: not enough memory\n");
2181ac27a0ecSDave Kleikamp 		goto failed_mount;
2182ac27a0ecSDave Kleikamp 	}
2183ac27a0ecSDave Kleikamp 
21843244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
21859f6200bbSTheodore Ts'o 	if (ext4_proc_root)
21869f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
21879f6200bbSTheodore Ts'o 
2188240799cdSTheodore Ts'o 	if (sbi->s_proc)
2189240799cdSTheodore Ts'o 		proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2190240799cdSTheodore Ts'o 				 &ext4_ui_proc_fops,
2191240799cdSTheodore Ts'o 				 &sbi->s_inode_readahead_blks);
21923244fcb1SAlexander Beregalov #endif
2193240799cdSTheodore Ts'o 
2194ac27a0ecSDave Kleikamp 	bgl_lock_init(&sbi->s_blockgroup_lock);
2195ac27a0ecSDave Kleikamp 
2196ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
219770bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2198ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2199ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2200617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: "
2201ac27a0ecSDave Kleikamp 			       "can't read group descriptor %d\n", i);
2202ac27a0ecSDave Kleikamp 			db_count = i;
2203ac27a0ecSDave Kleikamp 			goto failed_mount2;
2204ac27a0ecSDave Kleikamp 		}
2205ac27a0ecSDave Kleikamp 	}
2206617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2207617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2208ac27a0ecSDave Kleikamp 		goto failed_mount2;
2209ac27a0ecSDave Kleikamp 	}
2210772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2211772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2212772cb7c8SJose R. Santos 			printk(KERN_ERR
2213772cb7c8SJose R. Santos 			       "EXT4-fs: unable to initialize "
2214772cb7c8SJose R. Santos 			       "flex_bg meta info!\n");
2215772cb7c8SJose R. Santos 			goto failed_mount2;
2216772cb7c8SJose R. Santos 		}
2217772cb7c8SJose R. Santos 
2218ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2219ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2220ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2221ac27a0ecSDave Kleikamp 
2222833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2223617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2224833f4077SPeter Zijlstra 	if (!err) {
2225833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2226617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2227833f4077SPeter Zijlstra 	}
2228833f4077SPeter Zijlstra 	if (!err) {
2229833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2230617ba13bSMingming Cao 				ext4_count_dirs(sb));
2231833f4077SPeter Zijlstra 	}
22326bc6e63fSAneesh Kumar K.V 	if (!err) {
22336bc6e63fSAneesh Kumar K.V 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
22346bc6e63fSAneesh Kumar K.V 	}
2235833f4077SPeter Zijlstra 	if (err) {
2236833f4077SPeter Zijlstra 		printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2237833f4077SPeter Zijlstra 		goto failed_mount3;
2238833f4077SPeter Zijlstra 	}
2239ac27a0ecSDave Kleikamp 
2240c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
2241c9de560dSAlex Tomas 
2242ac27a0ecSDave Kleikamp 	/*
2243ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2244ac27a0ecSDave Kleikamp 	 */
2245617ba13bSMingming Cao 	sb->s_op = &ext4_sops;
2246617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2247617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2248ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2249617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2250617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2251ac27a0ecSDave Kleikamp #endif
2252ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2253ac27a0ecSDave Kleikamp 
2254ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2255ac27a0ecSDave Kleikamp 
2256ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2257617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2258617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2259ac27a0ecSDave Kleikamp 
2260ac27a0ecSDave Kleikamp 	/*
2261ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2262ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2263ac27a0ecSDave Kleikamp 	 */
2264ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2265617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2266617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2267ac27a0ecSDave Kleikamp 			goto failed_mount3;
2268624080edSTheodore Ts'o 		if (!(sb->s_flags & MS_RDONLY) &&
2269624080edSTheodore Ts'o 		    EXT4_SB(sb)->s_journal->j_failed_commit) {
2270624080edSTheodore Ts'o 			printk(KERN_CRIT "EXT4-fs error (device %s): "
2271624080edSTheodore Ts'o 			       "ext4_fill_super: Journal transaction "
2272624080edSTheodore Ts'o 			       "%u is corrupt\n", sb->s_id,
2273624080edSTheodore Ts'o 			       EXT4_SB(sb)->s_journal->j_failed_commit);
2274624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_RO)) {
2275624080edSTheodore Ts'o 				printk(KERN_CRIT
2276624080edSTheodore Ts'o 				       "Mounting filesystem read-only\n");
2277624080edSTheodore Ts'o 				sb->s_flags |= MS_RDONLY;
2278624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2279624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2280624080edSTheodore Ts'o 			}
2281624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_PANIC)) {
2282624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2283624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2284624080edSTheodore Ts'o 				ext4_commit_super(sb, es, 1);
2285624080edSTheodore Ts'o 				printk(KERN_CRIT
2286624080edSTheodore Ts'o 				       "EXT4-fs (device %s): mount failed\n",
2287624080edSTheodore Ts'o 				      sb->s_id);
2288624080edSTheodore Ts'o 				goto failed_mount4;
2289624080edSTheodore Ts'o 			}
2290624080edSTheodore Ts'o 		}
2291ac27a0ecSDave Kleikamp 	} else if (journal_inum) {
2292617ba13bSMingming Cao 		if (ext4_create_journal(sb, es, journal_inum))
2293ac27a0ecSDave Kleikamp 			goto failed_mount3;
2294ac27a0ecSDave Kleikamp 	} else {
2295ac27a0ecSDave Kleikamp 		if (!silent)
2296ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2297617ba13bSMingming Cao 			       "ext4: No journal on filesystem on %s\n",
2298ac27a0ecSDave Kleikamp 			       sb->s_id);
2299ac27a0ecSDave Kleikamp 		goto failed_mount3;
2300ac27a0ecSDave Kleikamp 	}
2301ac27a0ecSDave Kleikamp 
2302eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2303eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2304eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2305eb40a09cSJose R. Santos 		printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2306eb40a09cSJose R. Santos 		goto failed_mount4;
2307eb40a09cSJose R. Santos 	}
2308eb40a09cSJose R. Santos 
2309818d276cSGirish Shilamkar 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2310818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2311818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2312818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2313818d276cSGirish Shilamkar 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2314818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2315818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2316818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2317818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2318818d276cSGirish Shilamkar 	} else {
2319818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal,
2320818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2321818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2322818d276cSGirish Shilamkar 	}
2323818d276cSGirish Shilamkar 
2324ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2325ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2326ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2327ac27a0ecSDave Kleikamp 	case 0:
2328ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
232963f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
233063f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
233163f57933SAndrew Morton 		 */
2332dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2333dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2334ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2335ac27a0ecSDave Kleikamp 		else
2336ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2337ac27a0ecSDave Kleikamp 		break;
2338ac27a0ecSDave Kleikamp 
2339617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2340617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2341dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2342dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2343617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: Journal does not support "
2344ac27a0ecSDave Kleikamp 			       "requested data journaling mode\n");
2345ac27a0ecSDave Kleikamp 			goto failed_mount4;
2346ac27a0ecSDave Kleikamp 		}
2347ac27a0ecSDave Kleikamp 	default:
2348ac27a0ecSDave Kleikamp 		break;
2349ac27a0ecSDave Kleikamp 	}
2350ac27a0ecSDave Kleikamp 
2351ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2352617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2353617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2354ac27a0ecSDave Kleikamp 				"its supported only with writeback mode\n");
2355ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2356ac27a0ecSDave Kleikamp 		}
2357ac27a0ecSDave Kleikamp 	}
2358ac27a0ecSDave Kleikamp 	/*
2359dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2360ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2361ac27a0ecSDave Kleikamp 	 */
2362ac27a0ecSDave Kleikamp 
23631d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
23641d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2365617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: get root inode failed\n");
23661d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2367ac27a0ecSDave Kleikamp 		goto failed_mount4;
2368ac27a0ecSDave Kleikamp 	}
2369ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
23701d1fe1eeSDavid Howells 		iput(root);
2371617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2372ac27a0ecSDave Kleikamp 		goto failed_mount4;
2373ac27a0ecSDave Kleikamp 	}
23741d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
23751d1fe1eeSDavid Howells 	if (!sb->s_root) {
23761d1fe1eeSDavid Howells 		printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
23771d1fe1eeSDavid Howells 		iput(root);
23781d1fe1eeSDavid Howells 		ret = -ENOMEM;
23791d1fe1eeSDavid Howells 		goto failed_mount4;
23801d1fe1eeSDavid Howells 	}
2381ac27a0ecSDave Kleikamp 
2382617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2383ef7f3835SKalpak Shah 
2384ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2385ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2386ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2387ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2388ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2389ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2390ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2391ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2392ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2393ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2394ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2395ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2396ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2397ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2398ef7f3835SKalpak Shah 		}
2399ef7f3835SKalpak Shah 	}
2400ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2401ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2402ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2403ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2404ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2405ef7f3835SKalpak Shah 		printk(KERN_INFO "EXT4-fs: required extra inode space not"
2406ef7f3835SKalpak Shah 			"available.\n");
2407ef7f3835SKalpak Shah 	}
2408ef7f3835SKalpak Shah 
2409c2774d84SAneesh Kumar K.V 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2410c2774d84SAneesh Kumar K.V 		printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2411c2774d84SAneesh Kumar K.V 				"requested data journaling mode\n");
2412c2774d84SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
2413c2774d84SAneesh Kumar K.V 	} else if (test_opt(sb, DELALLOC))
2414c2774d84SAneesh Kumar K.V 		printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2415c2774d84SAneesh Kumar K.V 
2416c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
2417c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
2418c2774d84SAneesh Kumar K.V 	if (err) {
2419c2774d84SAneesh Kumar K.V 		printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2420c2774d84SAneesh Kumar K.V 		       err);
2421c2774d84SAneesh Kumar K.V 		goto failed_mount4;
2422c2774d84SAneesh Kumar K.V 	}
2423c2774d84SAneesh Kumar K.V 
2424ac27a0ecSDave Kleikamp 	/*
2425ac27a0ecSDave Kleikamp 	 * akpm: core read_super() calls in here with the superblock locked.
2426ac27a0ecSDave Kleikamp 	 * That deadlocks, because orphan cleanup needs to lock the superblock
2427ac27a0ecSDave Kleikamp 	 * in numerous places.  Here we just pop the lock - it's relatively
2428ac27a0ecSDave Kleikamp 	 * harmless, because we are now ready to accept write_super() requests,
2429ac27a0ecSDave Kleikamp 	 * and aviro says that's the only reason for hanging onto the
2430ac27a0ecSDave Kleikamp 	 * superblock lock.
2431ac27a0ecSDave Kleikamp 	 */
2432617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2433617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2434617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2435ac27a0ecSDave Kleikamp 	if (needs_recovery)
2436617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2437617ba13bSMingming Cao 	ext4_mark_recovery_complete(sb, es);
2438617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2439617ba13bSMingming Cao 	       test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2440617ba13bSMingming Cao 	       test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2441ac27a0ecSDave Kleikamp 	       "writeback");
2442ac27a0ecSDave Kleikamp 
2443ac27a0ecSDave Kleikamp 	lock_kernel();
2444ac27a0ecSDave Kleikamp 	return 0;
2445ac27a0ecSDave Kleikamp 
2446617ba13bSMingming Cao cantfind_ext4:
2447ac27a0ecSDave Kleikamp 	if (!silent)
2448617ba13bSMingming Cao 		printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2449ac27a0ecSDave Kleikamp 		       sb->s_id);
2450ac27a0ecSDave Kleikamp 	goto failed_mount;
2451ac27a0ecSDave Kleikamp 
2452ac27a0ecSDave Kleikamp failed_mount4:
2453dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
245447b4a50bSJan Kara 	sbi->s_journal = NULL;
2455ac27a0ecSDave Kleikamp failed_mount3:
2456ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2457ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2458ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
24596bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2460ac27a0ecSDave Kleikamp failed_mount2:
2461ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2462ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2463ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2464ac27a0ecSDave Kleikamp failed_mount:
2465240799cdSTheodore Ts'o 	if (sbi->s_proc) {
2466240799cdSTheodore Ts'o 		remove_proc_entry("inode_readahead_blks", sbi->s_proc);
24679f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
2468240799cdSTheodore Ts'o 	}
2469ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2470ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2471ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2472ac27a0ecSDave Kleikamp #endif
2473617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2474ac27a0ecSDave Kleikamp 	brelse(bh);
2475ac27a0ecSDave Kleikamp out_fail:
2476ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2477ac27a0ecSDave Kleikamp 	kfree(sbi);
2478ac27a0ecSDave Kleikamp 	lock_kernel();
24791d1fe1eeSDavid Howells 	return ret;
2480ac27a0ecSDave Kleikamp }
2481ac27a0ecSDave Kleikamp 
2482ac27a0ecSDave Kleikamp /*
2483ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2484ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2485ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2486ac27a0ecSDave Kleikamp  */
2487617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2488ac27a0ecSDave Kleikamp {
2489617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2490ac27a0ecSDave Kleikamp 
2491ac27a0ecSDave Kleikamp 	if (sbi->s_commit_interval)
2492ac27a0ecSDave Kleikamp 		journal->j_commit_interval = sbi->s_commit_interval;
2493617ba13bSMingming Cao 	/* We could also set up an ext4-specific default for the commit
2494ac27a0ecSDave Kleikamp 	 * interval here, but for now we'll just fall back to the jbd
2495ac27a0ecSDave Kleikamp 	 * default. */
2496ac27a0ecSDave Kleikamp 
2497ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2498ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2499dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2500ac27a0ecSDave Kleikamp 	else
2501dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
25025bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
25035bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
25045bf5683aSHidehiro Kawai 	else
25055bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2506ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2507ac27a0ecSDave Kleikamp }
2508ac27a0ecSDave Kleikamp 
2509617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2510ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2511ac27a0ecSDave Kleikamp {
2512ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2513ac27a0ecSDave Kleikamp 	journal_t *journal;
2514ac27a0ecSDave Kleikamp 
2515ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2516ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2517ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2518ac27a0ecSDave Kleikamp 
25191d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
25201d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
2521617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: no journal found.\n");
2522ac27a0ecSDave Kleikamp 		return NULL;
2523ac27a0ecSDave Kleikamp 	}
2524ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2525ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2526ac27a0ecSDave Kleikamp 		iput(journal_inode);
2527617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2528ac27a0ecSDave Kleikamp 		return NULL;
2529ac27a0ecSDave Kleikamp 	}
2530ac27a0ecSDave Kleikamp 
2531e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2532ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
25331d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
2534617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2535ac27a0ecSDave Kleikamp 		iput(journal_inode);
2536ac27a0ecSDave Kleikamp 		return NULL;
2537ac27a0ecSDave Kleikamp 	}
2538ac27a0ecSDave Kleikamp 
2539dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
2540ac27a0ecSDave Kleikamp 	if (!journal) {
2541617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2542ac27a0ecSDave Kleikamp 		iput(journal_inode);
2543ac27a0ecSDave Kleikamp 		return NULL;
2544ac27a0ecSDave Kleikamp 	}
2545ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2546617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2547ac27a0ecSDave Kleikamp 	return journal;
2548ac27a0ecSDave Kleikamp }
2549ac27a0ecSDave Kleikamp 
2550617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
2551ac27a0ecSDave Kleikamp 				       dev_t j_dev)
2552ac27a0ecSDave Kleikamp {
2553ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2554ac27a0ecSDave Kleikamp 	journal_t *journal;
2555617ba13bSMingming Cao 	ext4_fsblk_t start;
2556617ba13bSMingming Cao 	ext4_fsblk_t len;
2557ac27a0ecSDave Kleikamp 	int hblock, blocksize;
2558617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
2559ac27a0ecSDave Kleikamp 	unsigned long offset;
2560617ba13bSMingming Cao 	struct ext4_super_block *es;
2561ac27a0ecSDave Kleikamp 	struct block_device *bdev;
2562ac27a0ecSDave Kleikamp 
2563617ba13bSMingming Cao 	bdev = ext4_blkdev_get(j_dev);
2564ac27a0ecSDave Kleikamp 	if (bdev == NULL)
2565ac27a0ecSDave Kleikamp 		return NULL;
2566ac27a0ecSDave Kleikamp 
2567ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
2568ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2569617ba13bSMingming Cao 			"EXT4: failed to claim external journal device.\n");
25709a1c3542SAl Viro 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2571ac27a0ecSDave Kleikamp 		return NULL;
2572ac27a0ecSDave Kleikamp 	}
2573ac27a0ecSDave Kleikamp 
2574ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
2575ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(bdev);
2576ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
2577ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2578617ba13bSMingming Cao 			"EXT4-fs: blocksize too small for journal device.\n");
2579ac27a0ecSDave Kleikamp 		goto out_bdev;
2580ac27a0ecSDave Kleikamp 	}
2581ac27a0ecSDave Kleikamp 
2582617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2583617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2584ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
2585ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
2586617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2587ac27a0ecSDave Kleikamp 		       "external journal\n");
2588ac27a0ecSDave Kleikamp 		goto out_bdev;
2589ac27a0ecSDave Kleikamp 	}
2590ac27a0ecSDave Kleikamp 
2591617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2592617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2593ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
2594617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2595617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: external journal has "
2596ac27a0ecSDave Kleikamp 					"bad superblock\n");
2597ac27a0ecSDave Kleikamp 		brelse(bh);
2598ac27a0ecSDave Kleikamp 		goto out_bdev;
2599ac27a0ecSDave Kleikamp 	}
2600ac27a0ecSDave Kleikamp 
2601617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2602617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2603ac27a0ecSDave Kleikamp 		brelse(bh);
2604ac27a0ecSDave Kleikamp 		goto out_bdev;
2605ac27a0ecSDave Kleikamp 	}
2606ac27a0ecSDave Kleikamp 
2607bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
2608ac27a0ecSDave Kleikamp 	start = sb_block + 1;
2609ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
2610ac27a0ecSDave Kleikamp 
2611dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2612ac27a0ecSDave Kleikamp 					start, len, blocksize);
2613ac27a0ecSDave Kleikamp 	if (!journal) {
2614617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2615ac27a0ecSDave Kleikamp 		goto out_bdev;
2616ac27a0ecSDave Kleikamp 	}
2617ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2618ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
2619ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
2620ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
2621617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2622ac27a0ecSDave Kleikamp 		goto out_journal;
2623ac27a0ecSDave Kleikamp 	}
2624ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2625617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: External journal has more than one "
2626ac27a0ecSDave Kleikamp 					"user (unsupported) - %d\n",
2627ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
2628ac27a0ecSDave Kleikamp 		goto out_journal;
2629ac27a0ecSDave Kleikamp 	}
2630617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
2631617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2632ac27a0ecSDave Kleikamp 	return journal;
2633ac27a0ecSDave Kleikamp out_journal:
2634dab291afSMingming Cao 	jbd2_journal_destroy(journal);
2635ac27a0ecSDave Kleikamp out_bdev:
2636617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
2637ac27a0ecSDave Kleikamp 	return NULL;
2638ac27a0ecSDave Kleikamp }
2639ac27a0ecSDave Kleikamp 
2640617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
2641617ba13bSMingming Cao 			     struct ext4_super_block *es,
2642ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
2643ac27a0ecSDave Kleikamp {
2644ac27a0ecSDave Kleikamp 	journal_t *journal;
2645ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2646ac27a0ecSDave Kleikamp 	dev_t journal_dev;
2647ac27a0ecSDave Kleikamp 	int err = 0;
2648ac27a0ecSDave Kleikamp 	int really_read_only;
2649ac27a0ecSDave Kleikamp 
2650ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2651ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2652617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2653ac27a0ecSDave Kleikamp 			"numbers have changed\n");
2654ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
2655ac27a0ecSDave Kleikamp 	} else
2656ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2657ac27a0ecSDave Kleikamp 
2658ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
2659ac27a0ecSDave Kleikamp 
2660ac27a0ecSDave Kleikamp 	/*
2661ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
2662ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
2663ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
2664ac27a0ecSDave Kleikamp 	 */
2665ac27a0ecSDave Kleikamp 
2666617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2667ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
2668617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: INFO: recovery "
2669ac27a0ecSDave Kleikamp 					"required on readonly filesystem.\n");
2670ac27a0ecSDave Kleikamp 			if (really_read_only) {
2671617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: write access "
2672ac27a0ecSDave Kleikamp 					"unavailable, cannot proceed.\n");
2673ac27a0ecSDave Kleikamp 				return -EROFS;
2674ac27a0ecSDave Kleikamp 			}
2675617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: write access will "
2676ac27a0ecSDave Kleikamp 			       "be enabled during recovery.\n");
2677ac27a0ecSDave Kleikamp 		}
2678ac27a0ecSDave Kleikamp 	}
2679ac27a0ecSDave Kleikamp 
2680ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
2681617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2682ac27a0ecSDave Kleikamp 		       "and inode journals!\n");
2683ac27a0ecSDave Kleikamp 		return -EINVAL;
2684ac27a0ecSDave Kleikamp 	}
2685ac27a0ecSDave Kleikamp 
2686ac27a0ecSDave Kleikamp 	if (journal_inum) {
2687617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
2688ac27a0ecSDave Kleikamp 			return -EINVAL;
2689ac27a0ecSDave Kleikamp 	} else {
2690617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2691ac27a0ecSDave Kleikamp 			return -EINVAL;
2692ac27a0ecSDave Kleikamp 	}
2693ac27a0ecSDave Kleikamp 
26944776004fSTheodore Ts'o 	if (journal->j_flags & JBD2_BARRIER)
26954776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers enabled\n");
26964776004fSTheodore Ts'o 	else
26974776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers disabled\n");
26984776004fSTheodore Ts'o 
2699ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2700dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
2701ac27a0ecSDave Kleikamp 		if (err)  {
2702617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2703dab291afSMingming Cao 			jbd2_journal_destroy(journal);
2704ac27a0ecSDave Kleikamp 			return err;
2705ac27a0ecSDave Kleikamp 		}
2706ac27a0ecSDave Kleikamp 	}
2707ac27a0ecSDave Kleikamp 
2708617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2709dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
2710ac27a0ecSDave Kleikamp 	if (!err)
2711dab291afSMingming Cao 		err = jbd2_journal_load(journal);
2712ac27a0ecSDave Kleikamp 
2713ac27a0ecSDave Kleikamp 	if (err) {
2714617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2715dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2716ac27a0ecSDave Kleikamp 		return err;
2717ac27a0ecSDave Kleikamp 	}
2718ac27a0ecSDave Kleikamp 
2719617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2720617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
2721ac27a0ecSDave Kleikamp 
2722ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2723ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2724ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
2725ac27a0ecSDave Kleikamp 		sb->s_dirt = 1;
2726ac27a0ecSDave Kleikamp 
2727ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
2728617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2729ac27a0ecSDave Kleikamp 	}
2730ac27a0ecSDave Kleikamp 
2731ac27a0ecSDave Kleikamp 	return 0;
2732ac27a0ecSDave Kleikamp }
2733ac27a0ecSDave Kleikamp 
2734617ba13bSMingming Cao static int ext4_create_journal(struct super_block *sb,
2735617ba13bSMingming Cao 			       struct ext4_super_block *es,
2736ac27a0ecSDave Kleikamp 			       unsigned int journal_inum)
2737ac27a0ecSDave Kleikamp {
2738ac27a0ecSDave Kleikamp 	journal_t *journal;
27396c675bd4SBorislav Petkov 	int err;
2740ac27a0ecSDave Kleikamp 
2741ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY) {
2742617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2743ac27a0ecSDave Kleikamp 				"create journal.\n");
2744ac27a0ecSDave Kleikamp 		return -EROFS;
2745ac27a0ecSDave Kleikamp 	}
2746ac27a0ecSDave Kleikamp 
27476c675bd4SBorislav Petkov 	journal = ext4_get_journal(sb, journal_inum);
27486c675bd4SBorislav Petkov 	if (!journal)
2749ac27a0ecSDave Kleikamp 		return -EINVAL;
2750ac27a0ecSDave Kleikamp 
2751617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2752ac27a0ecSDave Kleikamp 	       journal_inum);
2753ac27a0ecSDave Kleikamp 
27546c675bd4SBorislav Petkov 	err = jbd2_journal_create(journal);
27556c675bd4SBorislav Petkov 	if (err) {
2756617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2757dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2758ac27a0ecSDave Kleikamp 		return -EIO;
2759ac27a0ecSDave Kleikamp 	}
2760ac27a0ecSDave Kleikamp 
2761617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2762ac27a0ecSDave Kleikamp 
2763617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
2764617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2765617ba13bSMingming Cao 	EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2766ac27a0ecSDave Kleikamp 
2767ac27a0ecSDave Kleikamp 	es->s_journal_inum = cpu_to_le32(journal_inum);
2768ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
2769ac27a0ecSDave Kleikamp 
2770ac27a0ecSDave Kleikamp 	/* Make sure we flush the recovery flag to disk. */
2771617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
2772ac27a0ecSDave Kleikamp 
2773ac27a0ecSDave Kleikamp 	return 0;
2774ac27a0ecSDave Kleikamp }
2775ac27a0ecSDave Kleikamp 
2776617ba13bSMingming Cao static void ext4_commit_super(struct super_block *sb,
27772b2d6d01STheodore Ts'o 			      struct ext4_super_block *es, int sync)
2778ac27a0ecSDave Kleikamp {
2779617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2780ac27a0ecSDave Kleikamp 
2781ac27a0ecSDave Kleikamp 	if (!sbh)
2782ac27a0ecSDave Kleikamp 		return;
2783914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
2784914258bfSTheodore Ts'o 		/*
2785914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
2786914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
2787914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
2788914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
2789914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
2790914258bfSTheodore Ts'o 		 * write and hope for the best.
2791914258bfSTheodore Ts'o 		 */
2792914258bfSTheodore Ts'o 		printk(KERN_ERR "ext4: previous I/O error to "
2793914258bfSTheodore Ts'o 		       "superblock detected for %s.\n", sb->s_id);
2794914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
2795914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
2796914258bfSTheodore Ts'o 	}
2797ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
2798bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2799617ba13bSMingming Cao 	es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2800ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
2801ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
2802914258bfSTheodore Ts'o 	if (sync) {
2803ac27a0ecSDave Kleikamp 		sync_dirty_buffer(sbh);
2804914258bfSTheodore Ts'o 		if (buffer_write_io_error(sbh)) {
2805914258bfSTheodore Ts'o 			printk(KERN_ERR "ext4: I/O error while writing "
2806914258bfSTheodore Ts'o 			       "superblock for %s.\n", sb->s_id);
2807914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
2808914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
2809914258bfSTheodore Ts'o 		}
2810914258bfSTheodore Ts'o 	}
2811ac27a0ecSDave Kleikamp }
2812ac27a0ecSDave Kleikamp 
2813ac27a0ecSDave Kleikamp 
2814ac27a0ecSDave Kleikamp /*
2815ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
2816ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
2817ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
2818ac27a0ecSDave Kleikamp  */
2819617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
2820617ba13bSMingming Cao 					struct ext4_super_block *es)
2821ac27a0ecSDave Kleikamp {
2822617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
2823ac27a0ecSDave Kleikamp 
2824dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
28257ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
28267ffe1ea8SHidehiro Kawai 		goto out;
28277ffe1ea8SHidehiro Kawai 
282832c37730SJan Kara 	lock_super(sb);
2829617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2830ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
2831617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2832ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
2833617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2834ac27a0ecSDave Kleikamp 	}
283532c37730SJan Kara 	unlock_super(sb);
28367ffe1ea8SHidehiro Kawai 
28377ffe1ea8SHidehiro Kawai out:
2838dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
2839ac27a0ecSDave Kleikamp }
2840ac27a0ecSDave Kleikamp 
2841ac27a0ecSDave Kleikamp /*
2842ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
2843ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
2844ac27a0ecSDave Kleikamp  * main filesystem now.
2845ac27a0ecSDave Kleikamp  */
2846617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
2847617ba13bSMingming Cao 				   struct ext4_super_block *es)
2848ac27a0ecSDave Kleikamp {
2849ac27a0ecSDave Kleikamp 	journal_t *journal;
2850ac27a0ecSDave Kleikamp 	int j_errno;
2851ac27a0ecSDave Kleikamp 	const char *errstr;
2852ac27a0ecSDave Kleikamp 
2853617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2854ac27a0ecSDave Kleikamp 
2855ac27a0ecSDave Kleikamp 	/*
2856ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
2857617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
2858ac27a0ecSDave Kleikamp 	 */
2859ac27a0ecSDave Kleikamp 
2860dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
2861ac27a0ecSDave Kleikamp 	if (j_errno) {
2862ac27a0ecSDave Kleikamp 		char nbuf[16];
2863ac27a0ecSDave Kleikamp 
2864617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
286546e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Filesystem error recorded "
2866ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
286746e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Marking fs in need of "
2868ac27a0ecSDave Kleikamp 			     "filesystem check.");
2869ac27a0ecSDave Kleikamp 
2870617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2871617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2872617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2873ac27a0ecSDave Kleikamp 
2874dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
2875ac27a0ecSDave Kleikamp 	}
2876ac27a0ecSDave Kleikamp }
2877ac27a0ecSDave Kleikamp 
2878ac27a0ecSDave Kleikamp /*
2879ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
2880ac27a0ecSDave Kleikamp  * and wait on the commit.
2881ac27a0ecSDave Kleikamp  */
2882617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
2883ac27a0ecSDave Kleikamp {
2884ac27a0ecSDave Kleikamp 	journal_t *journal;
2885ac27a0ecSDave Kleikamp 	int ret;
2886ac27a0ecSDave Kleikamp 
2887ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
2888ac27a0ecSDave Kleikamp 		return 0;
2889ac27a0ecSDave Kleikamp 
2890617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2891ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2892617ba13bSMingming Cao 	ret = ext4_journal_force_commit(journal);
2893ac27a0ecSDave Kleikamp 	return ret;
2894ac27a0ecSDave Kleikamp }
2895ac27a0ecSDave Kleikamp 
2896ac27a0ecSDave Kleikamp /*
2897617ba13bSMingming Cao  * Ext4 always journals updates to the superblock itself, so we don't
2898ac27a0ecSDave Kleikamp  * have to propagate any other updates to the superblock on disk at this
289914ce0cb4STheodore Ts'o  * point.  (We can probably nuke this function altogether, and remove
290014ce0cb4STheodore Ts'o  * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
2901ac27a0ecSDave Kleikamp  */
2902617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
2903ac27a0ecSDave Kleikamp {
2904ac27a0ecSDave Kleikamp 	if (mutex_trylock(&sb->s_lock) != 0)
2905ac27a0ecSDave Kleikamp 		BUG();
2906ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2907ac27a0ecSDave Kleikamp }
2908ac27a0ecSDave Kleikamp 
2909617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
2910ac27a0ecSDave Kleikamp {
291114ce0cb4STheodore Ts'o 	int ret = 0;
2912ac27a0ecSDave Kleikamp 
2913ede86cc4STheodore Ts'o 	trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
2914ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2915ac27a0ecSDave Kleikamp 	if (wait)
291614ce0cb4STheodore Ts'o 		ret = ext4_force_commit(sb);
291714ce0cb4STheodore Ts'o 	else
291814ce0cb4STheodore Ts'o 		jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
291914ce0cb4STheodore Ts'o 	return ret;
2920ac27a0ecSDave Kleikamp }
2921ac27a0ecSDave Kleikamp 
2922ac27a0ecSDave Kleikamp /*
2923ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
2924ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
2925ac27a0ecSDave Kleikamp  */
2926617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb)
2927ac27a0ecSDave Kleikamp {
2928ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2929ac27a0ecSDave Kleikamp 
2930ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2931617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
2932ac27a0ecSDave Kleikamp 
2933ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
2934dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
29357ffe1ea8SHidehiro Kawai 
29367ffe1ea8SHidehiro Kawai 		/*
29377ffe1ea8SHidehiro Kawai 		 * We don't want to clear needs_recovery flag when we failed
29387ffe1ea8SHidehiro Kawai 		 * to flush the journal.
29397ffe1ea8SHidehiro Kawai 		 */
29407ffe1ea8SHidehiro Kawai 		if (jbd2_journal_flush(journal) < 0)
29417ffe1ea8SHidehiro Kawai 			return;
2942ac27a0ecSDave Kleikamp 
2943ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
2944617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2945617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2946ac27a0ecSDave Kleikamp 	}
2947ac27a0ecSDave Kleikamp }
2948ac27a0ecSDave Kleikamp 
2949ac27a0ecSDave Kleikamp /*
2950ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
2951ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
2952ac27a0ecSDave Kleikamp  */
2953617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb)
2954ac27a0ecSDave Kleikamp {
2955ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2956ac27a0ecSDave Kleikamp 		lock_super(sb);
2957ac27a0ecSDave Kleikamp 		/* Reser the needs_recovery flag before the fs is unlocked. */
2958617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2959617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2960ac27a0ecSDave Kleikamp 		unlock_super(sb);
2961dab291afSMingming Cao 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2962ac27a0ecSDave Kleikamp 	}
2963ac27a0ecSDave Kleikamp }
2964ac27a0ecSDave Kleikamp 
2965617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
2966ac27a0ecSDave Kleikamp {
2967617ba13bSMingming Cao 	struct ext4_super_block *es;
2968617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2969617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
2970ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
2971617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
29728a266467STheodore Ts'o 	ext4_group_t g;
2973ac27a0ecSDave Kleikamp 	int err;
2974ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2975ac27a0ecSDave Kleikamp 	int i;
2976ac27a0ecSDave Kleikamp #endif
2977ac27a0ecSDave Kleikamp 
2978ac27a0ecSDave Kleikamp 	/* Store the original options */
2979ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
2980ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
2981ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
2982ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
2983ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
2984ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2985ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2986ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2987ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2988ac27a0ecSDave Kleikamp #endif
2989ac27a0ecSDave Kleikamp 
2990ac27a0ecSDave Kleikamp 	/*
2991ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
2992ac27a0ecSDave Kleikamp 	 */
2993ac27a0ecSDave Kleikamp 	if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2994ac27a0ecSDave Kleikamp 		err = -EINVAL;
2995ac27a0ecSDave Kleikamp 		goto restore_opts;
2996ac27a0ecSDave Kleikamp 	}
2997ac27a0ecSDave Kleikamp 
2998617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
299946e665e9SHarvey Harrison 		ext4_abort(sb, __func__, "Abort forced by user");
3000ac27a0ecSDave Kleikamp 
3001ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3002617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3003ac27a0ecSDave Kleikamp 
3004ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3005ac27a0ecSDave Kleikamp 
3006617ba13bSMingming Cao 	ext4_init_journal_params(sb, sbi->s_journal);
3007ac27a0ecSDave Kleikamp 
3008ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3009bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
3010617ba13bSMingming Cao 		if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3011ac27a0ecSDave Kleikamp 			err = -EROFS;
3012ac27a0ecSDave Kleikamp 			goto restore_opts;
3013ac27a0ecSDave Kleikamp 		}
3014ac27a0ecSDave Kleikamp 
3015ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
3016ac27a0ecSDave Kleikamp 			/*
3017ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3018ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3019ac27a0ecSDave Kleikamp 			 */
3020ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3021ac27a0ecSDave Kleikamp 
3022ac27a0ecSDave Kleikamp 			/*
3023ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3024ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3025ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3026ac27a0ecSDave Kleikamp 			 */
3027617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3028617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3029ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3030ac27a0ecSDave Kleikamp 
303132c37730SJan Kara 			/*
303232c37730SJan Kara 			 * We have to unlock super so that we can wait for
303332c37730SJan Kara 			 * transactions.
303432c37730SJan Kara 			 */
303532c37730SJan Kara 			unlock_super(sb);
3036617ba13bSMingming Cao 			ext4_mark_recovery_complete(sb, es);
303732c37730SJan Kara 			lock_super(sb);
3038ac27a0ecSDave Kleikamp 		} else {
3039ac27a0ecSDave Kleikamp 			__le32 ret;
3040617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3041617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3042617ba13bSMingming Cao 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3043ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
3044ac27a0ecSDave Kleikamp 				       "optional features (%x).\n",
3045ac27a0ecSDave Kleikamp 				       sb->s_id, le32_to_cpu(ret));
3046ac27a0ecSDave Kleikamp 				err = -EROFS;
3047ac27a0ecSDave Kleikamp 				goto restore_opts;
3048ac27a0ecSDave Kleikamp 			}
3049ead6596bSEric Sandeen 
3050ead6596bSEric Sandeen 			/*
30518a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
30528a266467STheodore Ts'o 			 * are sane.  If they aren't, refuse to
30538a266467STheodore Ts'o 			 * remount r/w.
30548a266467STheodore Ts'o 			 */
30558a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
30568a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
30578a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
30588a266467STheodore Ts'o 
30598a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
30608a266467STheodore Ts'o 					printk(KERN_ERR
30618a266467STheodore Ts'o 	       "EXT4-fs: ext4_remount: "
30628a266467STheodore Ts'o 		"Checksum for group %lu failed (%u!=%u)\n",
30638a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
30648a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
30658a266467STheodore Ts'o 					err = -EINVAL;
30668a266467STheodore Ts'o 					goto restore_opts;
30678a266467STheodore Ts'o 				}
30688a266467STheodore Ts'o 			}
30698a266467STheodore Ts'o 
30708a266467STheodore Ts'o 			/*
3071ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3072ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3073ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3074ead6596bSEric Sandeen 			 */
3075ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3076ead6596bSEric Sandeen 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3077ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3078ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3079ead6596bSEric Sandeen 				       "umount/remount instead.\n",
3080ead6596bSEric Sandeen 				       sb->s_id);
3081ead6596bSEric Sandeen 				err = -EINVAL;
3082ead6596bSEric Sandeen 				goto restore_opts;
3083ead6596bSEric Sandeen 			}
3084ead6596bSEric Sandeen 
3085ac27a0ecSDave Kleikamp 			/*
3086ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3087ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3088ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3089ac27a0ecSDave Kleikamp 			 * the partition.)
3090ac27a0ecSDave Kleikamp 			 */
3091617ba13bSMingming Cao 			ext4_clear_journal_err(sb, es);
3092ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3093617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3094ac27a0ecSDave Kleikamp 				goto restore_opts;
3095617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3096ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3097ac27a0ecSDave Kleikamp 		}
3098ac27a0ecSDave Kleikamp 	}
3099ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3100ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3101ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3102ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3103ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3104ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3105ac27a0ecSDave Kleikamp #endif
3106ac27a0ecSDave Kleikamp 	return 0;
3107ac27a0ecSDave Kleikamp restore_opts:
3108ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3109ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3110ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3111ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3112ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
3113ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3114ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3115ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3116ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3117ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3118ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3119ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3120ac27a0ecSDave Kleikamp 	}
3121ac27a0ecSDave Kleikamp #endif
3122ac27a0ecSDave Kleikamp 	return err;
3123ac27a0ecSDave Kleikamp }
3124ac27a0ecSDave Kleikamp 
3125617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3126ac27a0ecSDave Kleikamp {
3127ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3128617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3129617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3130960cc398SPekka Enberg 	u64 fsid;
3131ac27a0ecSDave Kleikamp 
31325e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
31335e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
31346bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3135fd2d4291SAvantika Mathur 		ext4_group_t ngroups = sbi->s_groups_count, i;
31365e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3137ac27a0ecSDave Kleikamp 		smp_rmb();
3138ac27a0ecSDave Kleikamp 
3139ac27a0ecSDave Kleikamp 		/*
31405e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
31415e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
31425e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3143ac27a0ecSDave Kleikamp 		 */
3144ac27a0ecSDave Kleikamp 
3145ac27a0ecSDave Kleikamp 		/*
3146ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3147ac27a0ecSDave Kleikamp 		 * overhead
3148ac27a0ecSDave Kleikamp 		 */
3149ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3150ac27a0ecSDave Kleikamp 
3151ac27a0ecSDave Kleikamp 		/*
3152ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3153ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3154ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3155ac27a0ecSDave Kleikamp 		 */
3156ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3157617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3158617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3159ac27a0ecSDave Kleikamp 			cond_resched();
3160ac27a0ecSDave Kleikamp 		}
3161ac27a0ecSDave Kleikamp 
3162ac27a0ecSDave Kleikamp 		/*
3163ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3164ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3165ac27a0ecSDave Kleikamp 		 */
31665e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
31675e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
31685e70030dSBadari Pulavarty 		smp_wmb();
31696bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3170ac27a0ecSDave Kleikamp 	}
3171ac27a0ecSDave Kleikamp 
3172617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3173ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
31745e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
31756bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
31766bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3177308ba3ecSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, buf->f_bfree);
3178bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3179bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3180ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3181ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
318252d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
31835e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3184617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3185960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3186960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3187960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3188960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3189ac27a0ecSDave Kleikamp 	return 0;
3190ac27a0ecSDave Kleikamp }
3191ac27a0ecSDave Kleikamp 
3192ac27a0ecSDave Kleikamp /* Helper function for writing quotas on sync - we need to start transaction before quota file
3193ac27a0ecSDave Kleikamp  * is locked for write. Otherwise the are possible deadlocks:
3194ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3195617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3196dab291afSMingming Cao  *   jbd2_journal_start()                   write_dquot()
3197ac27a0ecSDave Kleikamp  *   DQUOT_INIT()                        down(dqio_mutex)
3198dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3199ac27a0ecSDave Kleikamp  *
3200ac27a0ecSDave Kleikamp  */
3201ac27a0ecSDave Kleikamp 
3202ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3203ac27a0ecSDave Kleikamp 
3204ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3205ac27a0ecSDave Kleikamp {
3206ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3207ac27a0ecSDave Kleikamp }
3208ac27a0ecSDave Kleikamp 
3209617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type)
3210ac27a0ecSDave Kleikamp {
3211ac27a0ecSDave Kleikamp 	handle_t *handle;
3212ac27a0ecSDave Kleikamp 	int ret, err;
3213ac27a0ecSDave Kleikamp 
3214ac27a0ecSDave Kleikamp 	/* We may create quota structure so we need to reserve enough blocks */
3215617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3216ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3217ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3218ac27a0ecSDave Kleikamp 	ret = dquot_initialize(inode, type);
3219617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3220ac27a0ecSDave Kleikamp 	if (!ret)
3221ac27a0ecSDave Kleikamp 		ret = err;
3222ac27a0ecSDave Kleikamp 	return ret;
3223ac27a0ecSDave Kleikamp }
3224ac27a0ecSDave Kleikamp 
3225617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode)
3226ac27a0ecSDave Kleikamp {
3227ac27a0ecSDave Kleikamp 	handle_t *handle;
3228ac27a0ecSDave Kleikamp 	int ret, err;
3229ac27a0ecSDave Kleikamp 
3230ac27a0ecSDave Kleikamp 	/* We may delete quota structure so we need to reserve enough blocks */
3231617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
32322887df13SJan Kara 	if (IS_ERR(handle)) {
32332887df13SJan Kara 		/*
32342887df13SJan Kara 		 * We call dquot_drop() anyway to at least release references
32352887df13SJan Kara 		 * to quota structures so that umount does not hang.
32362887df13SJan Kara 		 */
32372887df13SJan Kara 		dquot_drop(inode);
3238ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
32392887df13SJan Kara 	}
3240ac27a0ecSDave Kleikamp 	ret = dquot_drop(inode);
3241617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3242ac27a0ecSDave Kleikamp 	if (!ret)
3243ac27a0ecSDave Kleikamp 		ret = err;
3244ac27a0ecSDave Kleikamp 	return ret;
3245ac27a0ecSDave Kleikamp }
3246ac27a0ecSDave Kleikamp 
3247617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3248ac27a0ecSDave Kleikamp {
3249ac27a0ecSDave Kleikamp 	int ret, err;
3250ac27a0ecSDave Kleikamp 	handle_t *handle;
3251ac27a0ecSDave Kleikamp 	struct inode *inode;
3252ac27a0ecSDave Kleikamp 
3253ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3254617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3255617ba13bSMingming Cao 					EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3256ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3257ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3258ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3259617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3260ac27a0ecSDave Kleikamp 	if (!ret)
3261ac27a0ecSDave Kleikamp 		ret = err;
3262ac27a0ecSDave Kleikamp 	return ret;
3263ac27a0ecSDave Kleikamp }
3264ac27a0ecSDave Kleikamp 
3265617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3266ac27a0ecSDave Kleikamp {
3267ac27a0ecSDave Kleikamp 	int ret, err;
3268ac27a0ecSDave Kleikamp 	handle_t *handle;
3269ac27a0ecSDave Kleikamp 
3270617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3271617ba13bSMingming Cao 					EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3272ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3273ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3274ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3275617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3276ac27a0ecSDave Kleikamp 	if (!ret)
3277ac27a0ecSDave Kleikamp 		ret = err;
3278ac27a0ecSDave Kleikamp 	return ret;
3279ac27a0ecSDave Kleikamp }
3280ac27a0ecSDave Kleikamp 
3281617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3282ac27a0ecSDave Kleikamp {
3283ac27a0ecSDave Kleikamp 	int ret, err;
3284ac27a0ecSDave Kleikamp 	handle_t *handle;
3285ac27a0ecSDave Kleikamp 
3286617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3287617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
32889c3013e9SJan Kara 	if (IS_ERR(handle)) {
32899c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
32909c3013e9SJan Kara 		dquot_release(dquot);
3291ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
32929c3013e9SJan Kara 	}
3293ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3294617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3295ac27a0ecSDave Kleikamp 	if (!ret)
3296ac27a0ecSDave Kleikamp 		ret = err;
3297ac27a0ecSDave Kleikamp 	return ret;
3298ac27a0ecSDave Kleikamp }
3299ac27a0ecSDave Kleikamp 
3300617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3301ac27a0ecSDave Kleikamp {
33022c8be6b2SJan Kara 	/* Are we journaling quotas? */
3303617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3304617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3305ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3306617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3307ac27a0ecSDave Kleikamp 	} else {
3308ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3309ac27a0ecSDave Kleikamp 	}
3310ac27a0ecSDave Kleikamp }
3311ac27a0ecSDave Kleikamp 
3312617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3313ac27a0ecSDave Kleikamp {
3314ac27a0ecSDave Kleikamp 	int ret, err;
3315ac27a0ecSDave Kleikamp 	handle_t *handle;
3316ac27a0ecSDave Kleikamp 
3317ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3318617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3319ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3320ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3321ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3322617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3323ac27a0ecSDave Kleikamp 	if (!ret)
3324ac27a0ecSDave Kleikamp 		ret = err;
3325ac27a0ecSDave Kleikamp 	return ret;
3326ac27a0ecSDave Kleikamp }
3327ac27a0ecSDave Kleikamp 
3328ac27a0ecSDave Kleikamp /*
3329ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3330ac27a0ecSDave Kleikamp  * the quota file and such...
3331ac27a0ecSDave Kleikamp  */
3332617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3333ac27a0ecSDave Kleikamp {
3334617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3335617ba13bSMingming Cao 			EXT4_SB(sb)->s_jquota_fmt, type);
3336ac27a0ecSDave Kleikamp }
3337ac27a0ecSDave Kleikamp 
3338ac27a0ecSDave Kleikamp /*
3339ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3340ac27a0ecSDave Kleikamp  */
3341617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
33428264613dSAl Viro 			 char *name, int remount)
3343ac27a0ecSDave Kleikamp {
3344ac27a0ecSDave Kleikamp 	int err;
33458264613dSAl Viro 	struct path path;
3346ac27a0ecSDave Kleikamp 
3347ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3348ac27a0ecSDave Kleikamp 		return -EINVAL;
33498264613dSAl Viro 	/* When remounting, no checks are needed and in fact, name is NULL */
33500623543bSJan Kara 	if (remount)
33518264613dSAl Viro 		return vfs_quota_on(sb, type, format_id, name, remount);
33520623543bSJan Kara 
33538264613dSAl Viro 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3354ac27a0ecSDave Kleikamp 	if (err)
3355ac27a0ecSDave Kleikamp 		return err;
33560623543bSJan Kara 
3357ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
33588264613dSAl Viro 	if (path.mnt->mnt_sb != sb) {
33598264613dSAl Viro 		path_put(&path);
3360ac27a0ecSDave Kleikamp 		return -EXDEV;
3361ac27a0ecSDave Kleikamp 	}
33620623543bSJan Kara 	/* Journaling quota? */
33630623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
33642b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
33658264613dSAl Viro 		if (path.dentry->d_parent != sb->s_root)
3366ac27a0ecSDave Kleikamp 			printk(KERN_WARNING
3367617ba13bSMingming Cao 				"EXT4-fs: Quota file not on filesystem root. "
33680623543bSJan Kara 				"Journaled quota will not work.\n");
33690623543bSJan Kara 	}
33700623543bSJan Kara 
33710623543bSJan Kara 	/*
33720623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
33730623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
33740623543bSJan Kara 	 */
33758264613dSAl Viro 	if (ext4_should_journal_data(path.dentry->d_inode)) {
33760623543bSJan Kara 		/*
33770623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
33780623543bSJan Kara 		 * otherwise be livelocked...
33790623543bSJan Kara 		 */
33800623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
33817ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
33820623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
33837ffe1ea8SHidehiro Kawai 		if (err) {
33848264613dSAl Viro 			path_put(&path);
33857ffe1ea8SHidehiro Kawai 			return err;
33867ffe1ea8SHidehiro Kawai 		}
33870623543bSJan Kara 	}
33880623543bSJan Kara 
33898264613dSAl Viro 	err = vfs_quota_on_path(sb, type, format_id, &path);
33908264613dSAl Viro 	path_put(&path);
339177e69dacSAl Viro 	return err;
3392ac27a0ecSDave Kleikamp }
3393ac27a0ecSDave Kleikamp 
3394ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3395ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3396ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3397ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3398617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3399ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3400ac27a0ecSDave Kleikamp {
3401ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3402725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3403ac27a0ecSDave Kleikamp 	int err = 0;
3404ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3405ac27a0ecSDave Kleikamp 	int tocopy;
3406ac27a0ecSDave Kleikamp 	size_t toread;
3407ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3408ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3409ac27a0ecSDave Kleikamp 
3410ac27a0ecSDave Kleikamp 	if (off > i_size)
3411ac27a0ecSDave Kleikamp 		return 0;
3412ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3413ac27a0ecSDave Kleikamp 		len = i_size-off;
3414ac27a0ecSDave Kleikamp 	toread = len;
3415ac27a0ecSDave Kleikamp 	while (toread > 0) {
3416ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3417ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3418617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3419ac27a0ecSDave Kleikamp 		if (err)
3420ac27a0ecSDave Kleikamp 			return err;
3421ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3422ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3423ac27a0ecSDave Kleikamp 		else
3424ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3425ac27a0ecSDave Kleikamp 		brelse(bh);
3426ac27a0ecSDave Kleikamp 		offset = 0;
3427ac27a0ecSDave Kleikamp 		toread -= tocopy;
3428ac27a0ecSDave Kleikamp 		data += tocopy;
3429ac27a0ecSDave Kleikamp 		blk++;
3430ac27a0ecSDave Kleikamp 	}
3431ac27a0ecSDave Kleikamp 	return len;
3432ac27a0ecSDave Kleikamp }
3433ac27a0ecSDave Kleikamp 
3434ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3435ac27a0ecSDave Kleikamp  * enough credits) */
3436617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3437ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3438ac27a0ecSDave Kleikamp {
3439ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3440725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3441ac27a0ecSDave Kleikamp 	int err = 0;
3442ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3443ac27a0ecSDave Kleikamp 	int tocopy;
3444617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3445ac27a0ecSDave Kleikamp 	size_t towrite = len;
3446ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3447ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3448ac27a0ecSDave Kleikamp 
34499c3013e9SJan Kara 	if (!handle) {
3450e5f8eab8STheodore Ts'o 		printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
34519c3013e9SJan Kara 			" cancelled because transaction is not started.\n",
34529c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
34539c3013e9SJan Kara 		return -EIO;
34549c3013e9SJan Kara 	}
3455ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3456ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3457ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3458ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3459617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3460ac27a0ecSDave Kleikamp 		if (!bh)
3461ac27a0ecSDave Kleikamp 			goto out;
3462ac27a0ecSDave Kleikamp 		if (journal_quota) {
3463617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3464ac27a0ecSDave Kleikamp 			if (err) {
3465ac27a0ecSDave Kleikamp 				brelse(bh);
3466ac27a0ecSDave Kleikamp 				goto out;
3467ac27a0ecSDave Kleikamp 			}
3468ac27a0ecSDave Kleikamp 		}
3469ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3470ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3471ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3472ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3473ac27a0ecSDave Kleikamp 		if (journal_quota)
3474617ba13bSMingming Cao 			err = ext4_journal_dirty_metadata(handle, bh);
3475ac27a0ecSDave Kleikamp 		else {
3476ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3477678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3478ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3479ac27a0ecSDave Kleikamp 		}
3480ac27a0ecSDave Kleikamp 		brelse(bh);
3481ac27a0ecSDave Kleikamp 		if (err)
3482ac27a0ecSDave Kleikamp 			goto out;
3483ac27a0ecSDave Kleikamp 		offset = 0;
3484ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3485ac27a0ecSDave Kleikamp 		data += tocopy;
3486ac27a0ecSDave Kleikamp 		blk++;
3487ac27a0ecSDave Kleikamp 	}
3488ac27a0ecSDave Kleikamp out:
34894d04e4fbSJan Kara 	if (len == towrite) {
34904d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
3491ac27a0ecSDave Kleikamp 		return err;
34924d04e4fbSJan Kara 	}
3493ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3494ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3495617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3496ac27a0ecSDave Kleikamp 	}
3497ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3498617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3499ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3500ac27a0ecSDave Kleikamp 	return len - towrite;
3501ac27a0ecSDave Kleikamp }
3502ac27a0ecSDave Kleikamp 
3503ac27a0ecSDave Kleikamp #endif
3504ac27a0ecSDave Kleikamp 
3505617ba13bSMingming Cao static int ext4_get_sb(struct file_system_type *fs_type,
3506ac27a0ecSDave Kleikamp 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3507ac27a0ecSDave Kleikamp {
3508617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3509ac27a0ecSDave Kleikamp }
3510ac27a0ecSDave Kleikamp 
35115e8814f2STheodore Ts'o #ifdef CONFIG_PROC_FS
35125e8814f2STheodore Ts'o static int ext4_ui_proc_show(struct seq_file *m, void *v)
35135e8814f2STheodore Ts'o {
35145e8814f2STheodore Ts'o 	unsigned int *p = m->private;
35155e8814f2STheodore Ts'o 
35165e8814f2STheodore Ts'o 	seq_printf(m, "%u\n", *p);
35175e8814f2STheodore Ts'o 	return 0;
35185e8814f2STheodore Ts'o }
35195e8814f2STheodore Ts'o 
35205e8814f2STheodore Ts'o static int ext4_ui_proc_open(struct inode *inode, struct file *file)
35215e8814f2STheodore Ts'o {
35225e8814f2STheodore Ts'o 	return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
35235e8814f2STheodore Ts'o }
35245e8814f2STheodore Ts'o 
35255e8814f2STheodore Ts'o static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
35265e8814f2STheodore Ts'o 			       size_t cnt, loff_t *ppos)
35275e8814f2STheodore Ts'o {
35285e8814f2STheodore Ts'o 	unsigned int *p = PDE(file->f_path.dentry->d_inode)->data;
35295e8814f2STheodore Ts'o 	char str[32];
35305e8814f2STheodore Ts'o 	unsigned long value;
35315e8814f2STheodore Ts'o 
35325e8814f2STheodore Ts'o 	if (cnt >= sizeof(str))
35335e8814f2STheodore Ts'o 		return -EINVAL;
35345e8814f2STheodore Ts'o 	if (copy_from_user(str, buf, cnt))
35355e8814f2STheodore Ts'o 		return -EFAULT;
35365e8814f2STheodore Ts'o 	value = simple_strtol(str, NULL, 0);
35375e8814f2STheodore Ts'o 	if (value < 0)
35385e8814f2STheodore Ts'o 		return -ERANGE;
35395e8814f2STheodore Ts'o 	*p = value;
35405e8814f2STheodore Ts'o 	return cnt;
35415e8814f2STheodore Ts'o }
35425e8814f2STheodore Ts'o 
35435e8814f2STheodore Ts'o const struct file_operations ext4_ui_proc_fops = {
35445e8814f2STheodore Ts'o 	.owner		= THIS_MODULE,
35455e8814f2STheodore Ts'o 	.open		= ext4_ui_proc_open,
35465e8814f2STheodore Ts'o 	.read		= seq_read,
35475e8814f2STheodore Ts'o 	.llseek		= seq_lseek,
35485e8814f2STheodore Ts'o 	.release	= single_release,
35495e8814f2STheodore Ts'o 	.write		= ext4_ui_proc_write,
35505e8814f2STheodore Ts'o };
35515e8814f2STheodore Ts'o #endif
35525e8814f2STheodore Ts'o 
355303010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
3554ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
355503010a33STheodore Ts'o 	.name		= "ext4",
3556617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
3557ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
3558ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
3559ac27a0ecSDave Kleikamp };
3560ac27a0ecSDave Kleikamp 
356103010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
356203010a33STheodore Ts'o static int ext4dev_get_sb(struct file_system_type *fs_type,
356303010a33STheodore Ts'o 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
356403010a33STheodore Ts'o {
356503010a33STheodore Ts'o 	printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
356603010a33STheodore Ts'o 	       "to mount using ext4\n");
356703010a33STheodore Ts'o 	printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
356803010a33STheodore Ts'o 	       "will go away by 2.6.31\n");
356903010a33STheodore Ts'o 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
357003010a33STheodore Ts'o }
357103010a33STheodore Ts'o 
357203010a33STheodore Ts'o static struct file_system_type ext4dev_fs_type = {
357303010a33STheodore Ts'o 	.owner		= THIS_MODULE,
357403010a33STheodore Ts'o 	.name		= "ext4dev",
357503010a33STheodore Ts'o 	.get_sb		= ext4dev_get_sb,
357603010a33STheodore Ts'o 	.kill_sb	= kill_block_super,
357703010a33STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
357803010a33STheodore Ts'o };
357903010a33STheodore Ts'o MODULE_ALIAS("ext4dev");
358003010a33STheodore Ts'o #endif
358103010a33STheodore Ts'o 
3582617ba13bSMingming Cao static int __init init_ext4_fs(void)
3583ac27a0ecSDave Kleikamp {
3584c9de560dSAlex Tomas 	int err;
3585c9de560dSAlex Tomas 
35869f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3587c9de560dSAlex Tomas 	err = init_ext4_mballoc();
3588ac27a0ecSDave Kleikamp 	if (err)
3589ac27a0ecSDave Kleikamp 		return err;
3590c9de560dSAlex Tomas 
3591c9de560dSAlex Tomas 	err = init_ext4_xattr();
3592c9de560dSAlex Tomas 	if (err)
3593c9de560dSAlex Tomas 		goto out2;
3594ac27a0ecSDave Kleikamp 	err = init_inodecache();
3595ac27a0ecSDave Kleikamp 	if (err)
3596ac27a0ecSDave Kleikamp 		goto out1;
359703010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
3598ac27a0ecSDave Kleikamp 	if (err)
3599ac27a0ecSDave Kleikamp 		goto out;
360003010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
360103010a33STheodore Ts'o 	err = register_filesystem(&ext4dev_fs_type);
360203010a33STheodore Ts'o 	if (err) {
360303010a33STheodore Ts'o 		unregister_filesystem(&ext4_fs_type);
360403010a33STheodore Ts'o 		goto out;
360503010a33STheodore Ts'o 	}
360603010a33STheodore Ts'o #endif
3607ac27a0ecSDave Kleikamp 	return 0;
3608ac27a0ecSDave Kleikamp out:
3609ac27a0ecSDave Kleikamp 	destroy_inodecache();
3610ac27a0ecSDave Kleikamp out1:
3611617ba13bSMingming Cao 	exit_ext4_xattr();
3612c9de560dSAlex Tomas out2:
3613c9de560dSAlex Tomas 	exit_ext4_mballoc();
3614ac27a0ecSDave Kleikamp 	return err;
3615ac27a0ecSDave Kleikamp }
3616ac27a0ecSDave Kleikamp 
3617617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
3618ac27a0ecSDave Kleikamp {
361903010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
362003010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
3621617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
362203010a33STheodore Ts'o #endif
3623ac27a0ecSDave Kleikamp 	destroy_inodecache();
3624617ba13bSMingming Cao 	exit_ext4_xattr();
3625c9de560dSAlex Tomas 	exit_ext4_mballoc();
36269f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
3627ac27a0ecSDave Kleikamp }
3628ac27a0ecSDave Kleikamp 
3629ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3630617ba13bSMingming Cao MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3631ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
3632617ba13bSMingming Cao module_init(init_ext4_fs)
3633617ba13bSMingming Cao module_exit(exit_ext4_fs)
3634