xref: /linux/fs/ext4/super.c (revision 5e8814f2f74f53d58aa5679bf32b38a7940033fe)
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>
381330593eSVignesh Babu #include <linux/log2.h>
39717d50e4SAndreas Dilger #include <linux/crc16.h>
40ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
41ac27a0ecSDave Kleikamp 
423dcf5451SChristoph Hellwig #include "ext4.h"
433dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
44ac27a0ecSDave Kleikamp #include "xattr.h"
45ac27a0ecSDave Kleikamp #include "acl.h"
46ac27a0ecSDave Kleikamp #include "namei.h"
47717d50e4SAndreas Dilger #include "group.h"
48ac27a0ecSDave Kleikamp 
499f6200bbSTheodore Ts'o struct proc_dir_entry *ext4_proc_root;
509f6200bbSTheodore Ts'o 
51617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
52ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
53617ba13bSMingming Cao static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
54ac27a0ecSDave Kleikamp 			       unsigned int);
55617ba13bSMingming Cao static void ext4_commit_super(struct super_block *sb,
562b2d6d01STheodore Ts'o 			      struct ext4_super_block *es, int sync);
57617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
58617ba13bSMingming Cao 					struct ext4_super_block *es);
59617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
60617ba13bSMingming Cao 				   struct ext4_super_block *es);
61617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
62617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
63ac27a0ecSDave Kleikamp 				     char nbuf[16]);
64617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
65617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
66617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb);
67617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
68617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb);
69ac27a0ecSDave Kleikamp 
70bd81d8eeSLaurent Vivier 
718fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
728fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
73bd81d8eeSLaurent Vivier {
743a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
758fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
768fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
77bd81d8eeSLaurent Vivier }
78bd81d8eeSLaurent Vivier 
798fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
808fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
81bd81d8eeSLaurent Vivier {
825272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
838fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
848fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
85bd81d8eeSLaurent Vivier }
86bd81d8eeSLaurent Vivier 
878fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
888fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
89bd81d8eeSLaurent Vivier {
905272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
918fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
928fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
93bd81d8eeSLaurent Vivier }
94bd81d8eeSLaurent Vivier 
958fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
968fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
97bd81d8eeSLaurent Vivier {
983a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
998fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1008fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
101bd81d8eeSLaurent Vivier }
102bd81d8eeSLaurent Vivier 
1038fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1048fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
105bd81d8eeSLaurent Vivier {
1065272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1078fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1088fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
109bd81d8eeSLaurent Vivier }
110bd81d8eeSLaurent Vivier 
1118fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1128fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
113bd81d8eeSLaurent Vivier {
1145272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1158fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1168fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
117bd81d8eeSLaurent Vivier }
118bd81d8eeSLaurent Vivier 
119ac27a0ecSDave Kleikamp /*
120dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
121ac27a0ecSDave Kleikamp  *
122ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
123ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
124ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
125ac27a0ecSDave Kleikamp  * appropriate.
126ac27a0ecSDave Kleikamp  */
127617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
128ac27a0ecSDave Kleikamp {
129ac27a0ecSDave Kleikamp 	journal_t *journal;
130ac27a0ecSDave Kleikamp 
131ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
132ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
133ac27a0ecSDave Kleikamp 
134ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
135ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
136ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
137617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
138ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
13946e665e9SHarvey Harrison 		ext4_abort(sb, __func__,
140ac27a0ecSDave Kleikamp 			   "Detected aborted journal");
141ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
142ac27a0ecSDave Kleikamp 	}
143ac27a0ecSDave Kleikamp 
144dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
145ac27a0ecSDave Kleikamp }
146ac27a0ecSDave Kleikamp 
147ac27a0ecSDave Kleikamp /*
148ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
149dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
150ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
151ac27a0ecSDave Kleikamp  * appropriate.
152ac27a0ecSDave Kleikamp  */
153617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
154ac27a0ecSDave Kleikamp {
155ac27a0ecSDave Kleikamp 	struct super_block *sb;
156ac27a0ecSDave Kleikamp 	int err;
157ac27a0ecSDave Kleikamp 	int rc;
158ac27a0ecSDave Kleikamp 
159ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
160ac27a0ecSDave Kleikamp 	err = handle->h_err;
161dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
162ac27a0ecSDave Kleikamp 
163ac27a0ecSDave Kleikamp 	if (!err)
164ac27a0ecSDave Kleikamp 		err = rc;
165ac27a0ecSDave Kleikamp 	if (err)
166617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
167ac27a0ecSDave Kleikamp 	return err;
168ac27a0ecSDave Kleikamp }
169ac27a0ecSDave Kleikamp 
170617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
171ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
172ac27a0ecSDave Kleikamp {
173ac27a0ecSDave Kleikamp 	char nbuf[16];
174617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
175ac27a0ecSDave Kleikamp 
176ac27a0ecSDave Kleikamp 	if (bh)
177ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
178ac27a0ecSDave Kleikamp 
179ac27a0ecSDave Kleikamp 	if (!handle->h_err)
180ac27a0ecSDave Kleikamp 		handle->h_err = err;
181ac27a0ecSDave Kleikamp 
182ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
183ac27a0ecSDave Kleikamp 		return;
184ac27a0ecSDave Kleikamp 
185ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
186ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
187ac27a0ecSDave Kleikamp 
188dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
189ac27a0ecSDave Kleikamp }
190ac27a0ecSDave Kleikamp 
191ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
192ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
193ac27a0ecSDave Kleikamp  *
194ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
195617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
196ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
197ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
198ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
199ac27a0ecSDave Kleikamp  * write out the superblock safely.
200ac27a0ecSDave Kleikamp  *
201dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
202ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
203ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
204ac27a0ecSDave Kleikamp  */
205ac27a0ecSDave Kleikamp 
206617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
207ac27a0ecSDave Kleikamp {
208617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
209ac27a0ecSDave Kleikamp 
210617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
211617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
212ac27a0ecSDave Kleikamp 
213ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
214ac27a0ecSDave Kleikamp 		return;
215ac27a0ecSDave Kleikamp 
216ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
217617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
218ac27a0ecSDave Kleikamp 
219617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
220ac27a0ecSDave Kleikamp 		if (journal)
221dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
222ac27a0ecSDave Kleikamp 	}
223ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
224ac27a0ecSDave Kleikamp 		printk(KERN_CRIT "Remounting filesystem read-only\n");
225ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
226ac27a0ecSDave Kleikamp 	}
227617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
228ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
229617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
230ac27a0ecSDave Kleikamp 			sb->s_id);
231ac27a0ecSDave Kleikamp }
232ac27a0ecSDave Kleikamp 
233617ba13bSMingming Cao void ext4_error(struct super_block *sb, const char *function,
234ac27a0ecSDave Kleikamp 		const char *fmt, ...)
235ac27a0ecSDave Kleikamp {
236ac27a0ecSDave Kleikamp 	va_list args;
237ac27a0ecSDave Kleikamp 
238ac27a0ecSDave Kleikamp 	va_start(args, fmt);
239617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
240ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
241ac27a0ecSDave Kleikamp 	printk("\n");
242ac27a0ecSDave Kleikamp 	va_end(args);
243ac27a0ecSDave Kleikamp 
244617ba13bSMingming Cao 	ext4_handle_error(sb);
245ac27a0ecSDave Kleikamp }
246ac27a0ecSDave Kleikamp 
247617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
248ac27a0ecSDave Kleikamp 				     char nbuf[16])
249ac27a0ecSDave Kleikamp {
250ac27a0ecSDave Kleikamp 	char *errstr = NULL;
251ac27a0ecSDave Kleikamp 
252ac27a0ecSDave Kleikamp 	switch (errno) {
253ac27a0ecSDave Kleikamp 	case -EIO:
254ac27a0ecSDave Kleikamp 		errstr = "IO failure";
255ac27a0ecSDave Kleikamp 		break;
256ac27a0ecSDave Kleikamp 	case -ENOMEM:
257ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
258ac27a0ecSDave Kleikamp 		break;
259ac27a0ecSDave Kleikamp 	case -EROFS:
260dab291afSMingming Cao 		if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
261ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
262ac27a0ecSDave Kleikamp 		else
263ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
264ac27a0ecSDave Kleikamp 		break;
265ac27a0ecSDave Kleikamp 	default:
266ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
267ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
268ac27a0ecSDave Kleikamp 		 * NULL. */
269ac27a0ecSDave Kleikamp 		if (nbuf) {
270ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
271ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
272ac27a0ecSDave Kleikamp 				errstr = nbuf;
273ac27a0ecSDave Kleikamp 		}
274ac27a0ecSDave Kleikamp 		break;
275ac27a0ecSDave Kleikamp 	}
276ac27a0ecSDave Kleikamp 
277ac27a0ecSDave Kleikamp 	return errstr;
278ac27a0ecSDave Kleikamp }
279ac27a0ecSDave Kleikamp 
280617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
281ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
282ac27a0ecSDave Kleikamp 
2832b2d6d01STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, int errno)
284ac27a0ecSDave Kleikamp {
285ac27a0ecSDave Kleikamp 	char nbuf[16];
286ac27a0ecSDave Kleikamp 	const char *errstr;
287ac27a0ecSDave Kleikamp 
288ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
289ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
290ac27a0ecSDave Kleikamp 	 * an error. */
291ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
292ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
293ac27a0ecSDave Kleikamp 		return;
294ac27a0ecSDave Kleikamp 
295617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
296617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
297ac27a0ecSDave Kleikamp 	       sb->s_id, function, errstr);
298ac27a0ecSDave Kleikamp 
299617ba13bSMingming Cao 	ext4_handle_error(sb);
300ac27a0ecSDave Kleikamp }
301ac27a0ecSDave Kleikamp 
302ac27a0ecSDave Kleikamp /*
303617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
304ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
305ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
306ac27a0ecSDave Kleikamp  *
307ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
308ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
309ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
310ac27a0ecSDave Kleikamp  */
311ac27a0ecSDave Kleikamp 
312617ba13bSMingming Cao void ext4_abort(struct super_block *sb, const char *function,
313ac27a0ecSDave Kleikamp 		const char *fmt, ...)
314ac27a0ecSDave Kleikamp {
315ac27a0ecSDave Kleikamp 	va_list args;
316ac27a0ecSDave Kleikamp 
317617ba13bSMingming Cao 	printk(KERN_CRIT "ext4_abort called.\n");
318ac27a0ecSDave Kleikamp 
319ac27a0ecSDave Kleikamp 	va_start(args, fmt);
320617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
321ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
322ac27a0ecSDave Kleikamp 	printk("\n");
323ac27a0ecSDave Kleikamp 	va_end(args);
324ac27a0ecSDave Kleikamp 
325ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
326617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
327ac27a0ecSDave Kleikamp 
328ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
329ac27a0ecSDave Kleikamp 		return;
330ac27a0ecSDave Kleikamp 
331ac27a0ecSDave Kleikamp 	printk(KERN_CRIT "Remounting filesystem read-only\n");
332617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
333ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
334617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
335dab291afSMingming Cao 	jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
336ac27a0ecSDave Kleikamp }
337ac27a0ecSDave Kleikamp 
338617ba13bSMingming Cao void ext4_warning(struct super_block *sb, const char *function,
339ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
340ac27a0ecSDave Kleikamp {
341ac27a0ecSDave Kleikamp 	va_list args;
342ac27a0ecSDave Kleikamp 
343ac27a0ecSDave Kleikamp 	va_start(args, fmt);
344617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
345ac27a0ecSDave Kleikamp 	       sb->s_id, function);
346ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
347ac27a0ecSDave Kleikamp 	printk("\n");
348ac27a0ecSDave Kleikamp 	va_end(args);
349ac27a0ecSDave Kleikamp }
350ac27a0ecSDave Kleikamp 
351617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
352ac27a0ecSDave Kleikamp {
353617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
354ac27a0ecSDave Kleikamp 
355617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
356ac27a0ecSDave Kleikamp 		return;
357ac27a0ecSDave Kleikamp 
35846e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
359ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
360ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
361617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
362ac27a0ecSDave Kleikamp 
363617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
364617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
365617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
366ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
367ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
368ac27a0ecSDave Kleikamp 
369ac27a0ecSDave Kleikamp 	/*
370ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
371ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
372ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
373ac27a0ecSDave Kleikamp 	 */
374ac27a0ecSDave Kleikamp }
375ac27a0ecSDave Kleikamp 
37699e6f829SAneesh Kumar K.V int ext4_update_compat_feature(handle_t *handle,
37799e6f829SAneesh Kumar K.V 					struct super_block *sb, __u32 compat)
37899e6f829SAneesh Kumar K.V {
37999e6f829SAneesh Kumar K.V 	int err = 0;
38099e6f829SAneesh Kumar K.V 	if (!EXT4_HAS_COMPAT_FEATURE(sb, compat)) {
38199e6f829SAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle,
38299e6f829SAneesh Kumar K.V 				EXT4_SB(sb)->s_sbh);
38399e6f829SAneesh Kumar K.V 		if (err)
38499e6f829SAneesh Kumar K.V 			return err;
38599e6f829SAneesh Kumar K.V 		EXT4_SET_COMPAT_FEATURE(sb, compat);
38699e6f829SAneesh Kumar K.V 		sb->s_dirt = 1;
38799e6f829SAneesh Kumar K.V 		handle->h_sync = 1;
38899e6f829SAneesh Kumar K.V 		BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
38999e6f829SAneesh Kumar K.V 					"call ext4_journal_dirty_met adata");
39099e6f829SAneesh Kumar K.V 		err = ext4_journal_dirty_metadata(handle,
39199e6f829SAneesh Kumar K.V 				EXT4_SB(sb)->s_sbh);
39299e6f829SAneesh Kumar K.V 	}
39399e6f829SAneesh Kumar K.V 	return err;
39499e6f829SAneesh Kumar K.V }
39599e6f829SAneesh Kumar K.V 
39699e6f829SAneesh Kumar K.V int ext4_update_rocompat_feature(handle_t *handle,
39799e6f829SAneesh Kumar K.V 					struct super_block *sb, __u32 rocompat)
39899e6f829SAneesh Kumar K.V {
39999e6f829SAneesh Kumar K.V 	int err = 0;
40099e6f829SAneesh Kumar K.V 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, rocompat)) {
40199e6f829SAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle,
40299e6f829SAneesh Kumar K.V 				EXT4_SB(sb)->s_sbh);
40399e6f829SAneesh Kumar K.V 		if (err)
40499e6f829SAneesh Kumar K.V 			return err;
40599e6f829SAneesh Kumar K.V 		EXT4_SET_RO_COMPAT_FEATURE(sb, rocompat);
40699e6f829SAneesh Kumar K.V 		sb->s_dirt = 1;
40799e6f829SAneesh Kumar K.V 		handle->h_sync = 1;
40899e6f829SAneesh Kumar K.V 		BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
40999e6f829SAneesh Kumar K.V 					"call ext4_journal_dirty_met adata");
41099e6f829SAneesh Kumar K.V 		err = ext4_journal_dirty_metadata(handle,
41199e6f829SAneesh Kumar K.V 				EXT4_SB(sb)->s_sbh);
41299e6f829SAneesh Kumar K.V 	}
41399e6f829SAneesh Kumar K.V 	return err;
41499e6f829SAneesh Kumar K.V }
41599e6f829SAneesh Kumar K.V 
41699e6f829SAneesh Kumar K.V int ext4_update_incompat_feature(handle_t *handle,
41799e6f829SAneesh Kumar K.V 					struct super_block *sb, __u32 incompat)
41899e6f829SAneesh Kumar K.V {
41999e6f829SAneesh Kumar K.V 	int err = 0;
42099e6f829SAneesh Kumar K.V 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, incompat)) {
42199e6f829SAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle,
42299e6f829SAneesh Kumar K.V 				EXT4_SB(sb)->s_sbh);
42399e6f829SAneesh Kumar K.V 		if (err)
42499e6f829SAneesh Kumar K.V 			return err;
42599e6f829SAneesh Kumar K.V 		EXT4_SET_INCOMPAT_FEATURE(sb, incompat);
42699e6f829SAneesh Kumar K.V 		sb->s_dirt = 1;
42799e6f829SAneesh Kumar K.V 		handle->h_sync = 1;
42899e6f829SAneesh Kumar K.V 		BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
42999e6f829SAneesh Kumar K.V 					"call ext4_journal_dirty_met adata");
43099e6f829SAneesh Kumar K.V 		err = ext4_journal_dirty_metadata(handle,
43199e6f829SAneesh Kumar K.V 				EXT4_SB(sb)->s_sbh);
43299e6f829SAneesh Kumar K.V 	}
43399e6f829SAneesh Kumar K.V 	return err;
43499e6f829SAneesh Kumar K.V }
43599e6f829SAneesh Kumar K.V 
436ac27a0ecSDave Kleikamp /*
437ac27a0ecSDave Kleikamp  * Open the external journal device
438ac27a0ecSDave Kleikamp  */
439617ba13bSMingming Cao static struct block_device *ext4_blkdev_get(dev_t dev)
440ac27a0ecSDave Kleikamp {
441ac27a0ecSDave Kleikamp 	struct block_device *bdev;
442ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
443ac27a0ecSDave Kleikamp 
444ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
445ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
446ac27a0ecSDave Kleikamp 		goto fail;
447ac27a0ecSDave Kleikamp 	return bdev;
448ac27a0ecSDave Kleikamp 
449ac27a0ecSDave Kleikamp fail:
450617ba13bSMingming Cao 	printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
451ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
452ac27a0ecSDave Kleikamp 	return NULL;
453ac27a0ecSDave Kleikamp }
454ac27a0ecSDave Kleikamp 
455ac27a0ecSDave Kleikamp /*
456ac27a0ecSDave Kleikamp  * Release the journal device
457ac27a0ecSDave Kleikamp  */
458617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
459ac27a0ecSDave Kleikamp {
460ac27a0ecSDave Kleikamp 	bd_release(bdev);
461ac27a0ecSDave Kleikamp 	return blkdev_put(bdev);
462ac27a0ecSDave Kleikamp }
463ac27a0ecSDave Kleikamp 
464617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
465ac27a0ecSDave Kleikamp {
466ac27a0ecSDave Kleikamp 	struct block_device *bdev;
467ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
468ac27a0ecSDave Kleikamp 
469ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
470ac27a0ecSDave Kleikamp 	if (bdev) {
471617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
472ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
473ac27a0ecSDave Kleikamp 	}
474ac27a0ecSDave Kleikamp 	return ret;
475ac27a0ecSDave Kleikamp }
476ac27a0ecSDave Kleikamp 
477ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
478ac27a0ecSDave Kleikamp {
479617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
480ac27a0ecSDave Kleikamp }
481ac27a0ecSDave Kleikamp 
482617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
483ac27a0ecSDave Kleikamp {
484ac27a0ecSDave Kleikamp 	struct list_head *l;
485ac27a0ecSDave Kleikamp 
486ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb orphan head is %d\n",
487ac27a0ecSDave Kleikamp 	       le32_to_cpu(sbi->s_es->s_last_orphan));
488ac27a0ecSDave Kleikamp 
489ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
490ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
491ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
492ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
493ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
494ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
495ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
496ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
497ac27a0ecSDave Kleikamp 	}
498ac27a0ecSDave Kleikamp }
499ac27a0ecSDave Kleikamp 
500617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
501ac27a0ecSDave Kleikamp {
502617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
503617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
504ac27a0ecSDave Kleikamp 	int i;
505ac27a0ecSDave Kleikamp 
506c9de560dSAlex Tomas 	ext4_mb_release(sb);
507a86c6181SAlex Tomas 	ext4_ext_release(sb);
508617ba13bSMingming Cao 	ext4_xattr_put_super(sb);
509dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
51047b4a50bSJan Kara 	sbi->s_journal = NULL;
511ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
512617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
513ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
514ac27a0ecSDave Kleikamp 		BUFFER_TRACE(sbi->s_sbh, "marking dirty");
515ac27a0ecSDave Kleikamp 		mark_buffer_dirty(sbi->s_sbh);
516617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
517ac27a0ecSDave Kleikamp 	}
5189f6200bbSTheodore Ts'o 	if (sbi->s_proc)
5199f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
520ac27a0ecSDave Kleikamp 
521ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
522ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
523ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
524772cb7c8SJose R. Santos 	kfree(sbi->s_flex_groups);
525ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
526ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
527ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
5286bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
529ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
530ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
531ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
532ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
533ac27a0ecSDave Kleikamp #endif
534ac27a0ecSDave Kleikamp 
535ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
536ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
537ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
538ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
539ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
540ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
541ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
542ac27a0ecSDave Kleikamp 
543f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
544ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
545ac27a0ecSDave Kleikamp 		/*
546ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
547ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
548ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
549ac27a0ecSDave Kleikamp 		 */
550ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
551f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
552617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
553ac27a0ecSDave Kleikamp 	}
554ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
555ac27a0ecSDave Kleikamp 	kfree(sbi);
556ac27a0ecSDave Kleikamp 	return;
557ac27a0ecSDave Kleikamp }
558ac27a0ecSDave Kleikamp 
559e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
560ac27a0ecSDave Kleikamp 
561ac27a0ecSDave Kleikamp /*
562ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
563ac27a0ecSDave Kleikamp  */
564617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
565ac27a0ecSDave Kleikamp {
566617ba13bSMingming Cao 	struct ext4_inode_info *ei;
567ac27a0ecSDave Kleikamp 
568e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
569ac27a0ecSDave Kleikamp 	if (!ei)
570ac27a0ecSDave Kleikamp 		return NULL;
571617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
572617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
573617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
574ac27a0ecSDave Kleikamp #endif
575ac27a0ecSDave Kleikamp 	ei->i_block_alloc_info = NULL;
576ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
57791246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
578a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
579c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
580c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
581678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
582d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
583d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
584d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
585d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
586d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
587ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
588ac27a0ecSDave Kleikamp }
589ac27a0ecSDave Kleikamp 
590617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
591ac27a0ecSDave Kleikamp {
5929f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
5939f7dd93dSVasily Averin 		printk("EXT4 Inode %p: orphan list check failed!\n",
5949f7dd93dSVasily Averin 			EXT4_I(inode));
5959f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
5969f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
5979f7dd93dSVasily Averin 				true);
5989f7dd93dSVasily Averin 		dump_stack();
5999f7dd93dSVasily Averin 	}
600617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
601ac27a0ecSDave Kleikamp }
602ac27a0ecSDave Kleikamp 
60351cc5068SAlexey Dobriyan static void init_once(void *foo)
604ac27a0ecSDave Kleikamp {
605617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
606ac27a0ecSDave Kleikamp 
607ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
608617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
609ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
610ac27a0ecSDave Kleikamp #endif
6110e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
612ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
613ac27a0ecSDave Kleikamp }
614ac27a0ecSDave Kleikamp 
615ac27a0ecSDave Kleikamp static int init_inodecache(void)
616ac27a0ecSDave Kleikamp {
617617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
618617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
619ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
620ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
62120c2df83SPaul Mundt 					     init_once);
622617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
623ac27a0ecSDave Kleikamp 		return -ENOMEM;
624ac27a0ecSDave Kleikamp 	return 0;
625ac27a0ecSDave Kleikamp }
626ac27a0ecSDave Kleikamp 
627ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
628ac27a0ecSDave Kleikamp {
629617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
630ac27a0ecSDave Kleikamp }
631ac27a0ecSDave Kleikamp 
632617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
633ac27a0ecSDave Kleikamp {
634617ba13bSMingming Cao 	struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
635617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
636617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
637617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
638617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
639617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
640ac27a0ecSDave Kleikamp 	}
641617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
642617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
643617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
644617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
645ac27a0ecSDave Kleikamp 	}
646ac27a0ecSDave Kleikamp #endif
647617ba13bSMingming Cao 	ext4_discard_reservation(inode);
648617ba13bSMingming Cao 	EXT4_I(inode)->i_block_alloc_info = NULL;
649ac27a0ecSDave Kleikamp 	if (unlikely(rsv))
650ac27a0ecSDave Kleikamp 		kfree(rsv);
651678aaf48SJan Kara 	jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
652678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
653ac27a0ecSDave Kleikamp }
654ac27a0ecSDave Kleikamp 
6552b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
6562b2d6d01STheodore Ts'o 					   struct super_block *sb)
657ac27a0ecSDave Kleikamp {
658ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
659617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
660ac27a0ecSDave Kleikamp 
661ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
662ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
663ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
664ac27a0ecSDave Kleikamp 
665ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
666ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
667ac27a0ecSDave Kleikamp 
668ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
669ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
670ac27a0ecSDave Kleikamp 
671617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
672ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
673ac27a0ecSDave Kleikamp 
674617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
675ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
676ac27a0ecSDave Kleikamp #endif
677ac27a0ecSDave Kleikamp }
678ac27a0ecSDave Kleikamp 
679d9c9bef1SMiklos Szeredi /*
680d9c9bef1SMiklos Szeredi  * Show an option if
681d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
682d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
683d9c9bef1SMiklos Szeredi  */
684617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
685ac27a0ecSDave Kleikamp {
686aa22df2cSAneesh Kumar K.V 	int def_errors;
687aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
688ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
689d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
690d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
691d9c9bef1SMiklos Szeredi 
692d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
693aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
694d9c9bef1SMiklos Szeredi 
695d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
696d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
697d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
698d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
699aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
700d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
701d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
702d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
703d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
704d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
705d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
706d9c9bef1SMiklos Szeredi 	}
707d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
708d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
709d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
710d9c9bef1SMiklos Szeredi 	}
711bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
712d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
713bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
714d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
715bb4f397aSAneesh Kumar K.V 		}
716bb4f397aSAneesh Kumar K.V 	}
717aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
718bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
719aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
720d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
721aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
722d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
723aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
724d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
725d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
726d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
72707620f69SAdrian Bunk #ifdef CONFIG_EXT4DEV_FS_XATTR
728aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
729aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
730d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
731d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
732d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
733d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
734d9c9bef1SMiklos Szeredi 	}
735d9c9bef1SMiklos Szeredi #endif
73607620f69SAdrian Bunk #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
737aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
738d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
739d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
740d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
741d9c9bef1SMiklos Szeredi #endif
742d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, RESERVATION))
743d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noreservation");
744d9c9bef1SMiklos Szeredi 	if (sbi->s_commit_interval) {
745d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
746d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
747d9c9bef1SMiklos Szeredi 	}
748571640caSEric Sandeen 	/*
749571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
750571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
751571640caSEric Sandeen 	 * status is.
752571640caSEric Sandeen 	 */
753571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
754571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
755cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
756cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
757d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
758d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
759d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, EXTENTS))
760d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noextents");
7613dbd0edeSAneesh Kumar K.V 	if (!test_opt(sb, MBALLOC))
7623dbd0edeSAneesh Kumar K.V 		seq_puts(seq, ",nomballoc");
76325ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
76425ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
765dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
766dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
767dd919b98SAneesh Kumar K.V 
768ac27a0ecSDave Kleikamp 
769cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
770cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
771aa22df2cSAneesh Kumar K.V 	/*
772aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
773aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
774aa22df2cSAneesh Kumar K.V 	 */
775617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
776ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
777617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
778ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
779617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
780ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
781ac27a0ecSDave Kleikamp 
782617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
783ac27a0ecSDave Kleikamp 	return 0;
784ac27a0ecSDave Kleikamp }
785ac27a0ecSDave Kleikamp 
786ac27a0ecSDave Kleikamp 
7871b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
7881b961ac0SChristoph Hellwig 		u64 ino, u32 generation)
789ac27a0ecSDave Kleikamp {
790ac27a0ecSDave Kleikamp 	struct inode *inode;
791ac27a0ecSDave Kleikamp 
792617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
793ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
794617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
795ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
796ac27a0ecSDave Kleikamp 
797ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
798ac27a0ecSDave Kleikamp 	 *
799617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
800ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
801ac27a0ecSDave Kleikamp 	 *
802ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
803ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
804ac27a0ecSDave Kleikamp 	 */
8051d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
8061d1fe1eeSDavid Howells 	if (IS_ERR(inode))
8071d1fe1eeSDavid Howells 		return ERR_CAST(inode);
8081d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
809ac27a0ecSDave Kleikamp 		iput(inode);
810ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
811ac27a0ecSDave Kleikamp 	}
8121b961ac0SChristoph Hellwig 
8131b961ac0SChristoph Hellwig 	return inode;
814ac27a0ecSDave Kleikamp }
8151b961ac0SChristoph Hellwig 
8161b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
8171b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
8181b961ac0SChristoph Hellwig {
8191b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
8201b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
8211b961ac0SChristoph Hellwig }
8221b961ac0SChristoph Hellwig 
8231b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
8241b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
8251b961ac0SChristoph Hellwig {
8261b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
8271b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
828ac27a0ecSDave Kleikamp }
829ac27a0ecSDave Kleikamp 
830ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
831ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
832ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
833ac27a0ecSDave Kleikamp 
834617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type);
835617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode);
836617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
837617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
838617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
839617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
840617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
8416f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
8426f28e087SJan Kara 				char *path, int remount);
843617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
844617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
845ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
846617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
847ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
848ac27a0ecSDave Kleikamp 
849617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
850617ba13bSMingming Cao 	.initialize	= ext4_dquot_initialize,
851617ba13bSMingming Cao 	.drop		= ext4_dquot_drop,
852ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
853ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
854ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
855ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
856ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
857617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
858617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
859617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
860617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
861617ba13bSMingming Cao 	.write_info	= ext4_write_info
862ac27a0ecSDave Kleikamp };
863ac27a0ecSDave Kleikamp 
864617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
865617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
866ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
867ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
868ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
869ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
870ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
871ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
872ac27a0ecSDave Kleikamp };
873ac27a0ecSDave Kleikamp #endif
874ac27a0ecSDave Kleikamp 
875ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
876617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
877617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
878617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
879617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
880617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
881617ba13bSMingming Cao 	.put_super	= ext4_put_super,
882617ba13bSMingming Cao 	.write_super	= ext4_write_super,
883617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
884617ba13bSMingming Cao 	.write_super_lockfs = ext4_write_super_lockfs,
885617ba13bSMingming Cao 	.unlockfs	= ext4_unlockfs,
886617ba13bSMingming Cao 	.statfs		= ext4_statfs,
887617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
888617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
889617ba13bSMingming Cao 	.show_options	= ext4_show_options,
890ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
891617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
892617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
893ac27a0ecSDave Kleikamp #endif
894ac27a0ecSDave Kleikamp };
895ac27a0ecSDave Kleikamp 
89639655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
8971b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
8981b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
899617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
900ac27a0ecSDave Kleikamp };
901ac27a0ecSDave Kleikamp 
902ac27a0ecSDave Kleikamp enum {
903ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
904ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
905ac27a0ecSDave Kleikamp 	Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
906ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
907ac27a0ecSDave Kleikamp 	Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
908ac27a0ecSDave Kleikamp 	Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
909818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
910ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
911ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
912ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
913ac27a0ecSDave Kleikamp 	Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
91425ec56b5SJean Noel Cordenner 	Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
915dd919b98SAneesh Kumar K.V 	Opt_mballoc, Opt_nomballoc, Opt_stripe, Opt_delalloc, Opt_nodelalloc,
916ac27a0ecSDave Kleikamp };
917ac27a0ecSDave Kleikamp 
918ac27a0ecSDave Kleikamp static match_table_t tokens = {
919ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
920ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
921ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
922ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
923ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
924ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
925ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
926ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
927ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
928ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
929ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
930ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
931ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
932ac27a0ecSDave Kleikamp 	{Opt_nocheck, "nocheck"},
933ac27a0ecSDave Kleikamp 	{Opt_nocheck, "check=none"},
934ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
935ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
936ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
937ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
938ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
939ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
940ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
941ac27a0ecSDave Kleikamp 	{Opt_reservation, "reservation"},
942ac27a0ecSDave Kleikamp 	{Opt_noreservation, "noreservation"},
943ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
944ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
945ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
946ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
947ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
948ac27a0ecSDave Kleikamp 	{Opt_journal_inum, "journal=%u"},
949ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
950818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
951818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
952ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
953ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
954ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
955ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
956ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
957ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
958ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
959ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
960ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
961ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
962ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
963ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
964ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
965ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
966ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
967a86c6181SAlex Tomas 	{Opt_extents, "extents"},
9681e2462f9SMingming Cao 	{Opt_noextents, "noextents"},
96925ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
970c9de560dSAlex Tomas 	{Opt_mballoc, "mballoc"},
971c9de560dSAlex Tomas 	{Opt_nomballoc, "nomballoc"},
972c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
973ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
97464769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
975dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
976f3f12faaSJosef Bacik 	{Opt_err, NULL},
977ac27a0ecSDave Kleikamp };
978ac27a0ecSDave Kleikamp 
979617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
980ac27a0ecSDave Kleikamp {
981617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
982ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
983ac27a0ecSDave Kleikamp 
984ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
985ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
986ac27a0ecSDave Kleikamp 	options += 3;
987617ba13bSMingming Cao 	/*todo: use simple_strtoll with >32bit ext4 */
988ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
989ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
9904776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
991ac27a0ecSDave Kleikamp 		       (char *) *data);
992ac27a0ecSDave Kleikamp 		return 1;
993ac27a0ecSDave Kleikamp 	}
994ac27a0ecSDave Kleikamp 	if (*options == ',')
995ac27a0ecSDave Kleikamp 		options++;
996ac27a0ecSDave Kleikamp 	*data = (void *) options;
997ac27a0ecSDave Kleikamp 	return sb_block;
998ac27a0ecSDave Kleikamp }
999ac27a0ecSDave Kleikamp 
1000ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1001ac27a0ecSDave Kleikamp 			 unsigned int *inum, unsigned long *journal_devnum,
1002617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1003ac27a0ecSDave Kleikamp {
1004617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1005ac27a0ecSDave Kleikamp 	char *p;
1006ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1007ac27a0ecSDave Kleikamp 	int data_opt = 0;
1008ac27a0ecSDave Kleikamp 	int option;
1009ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1010dfc5d03fSJan Kara 	int qtype, qfmt;
1011ac27a0ecSDave Kleikamp 	char *qname;
1012ac27a0ecSDave Kleikamp #endif
1013c07651b5SAneesh Kumar K.V 	ext4_fsblk_t last_block;
1014ac27a0ecSDave Kleikamp 
1015ac27a0ecSDave Kleikamp 	if (!options)
1016ac27a0ecSDave Kleikamp 		return 1;
1017ac27a0ecSDave Kleikamp 
1018ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1019ac27a0ecSDave Kleikamp 		int token;
1020ac27a0ecSDave Kleikamp 		if (!*p)
1021ac27a0ecSDave Kleikamp 			continue;
1022ac27a0ecSDave Kleikamp 
1023ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1024ac27a0ecSDave Kleikamp 		switch (token) {
1025ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1026ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
1027ac27a0ecSDave Kleikamp 			break;
1028ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1029ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
1030ac27a0ecSDave Kleikamp 			break;
1031ac27a0ecSDave Kleikamp 		case Opt_grpid:
1032ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
1033ac27a0ecSDave Kleikamp 			break;
1034ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1035ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
1036ac27a0ecSDave Kleikamp 			break;
1037ac27a0ecSDave Kleikamp 		case Opt_resuid:
1038ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1039ac27a0ecSDave Kleikamp 				return 0;
1040ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1041ac27a0ecSDave Kleikamp 			break;
1042ac27a0ecSDave Kleikamp 		case Opt_resgid:
1043ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1044ac27a0ecSDave Kleikamp 				return 0;
1045ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1046ac27a0ecSDave Kleikamp 			break;
1047ac27a0ecSDave Kleikamp 		case Opt_sb:
1048ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1049ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1050ac27a0ecSDave Kleikamp 			break;
1051ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1052ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1053ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1054ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1055ac27a0ecSDave Kleikamp 			break;
1056ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1057ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1058ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1059ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1060ac27a0ecSDave Kleikamp 			break;
1061ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1062ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1063ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1064ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1065ac27a0ecSDave Kleikamp 			break;
1066ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1067ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1068ac27a0ecSDave Kleikamp 			break;
1069ac27a0ecSDave Kleikamp 		case Opt_nocheck:
1070ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, CHECK);
1071ac27a0ecSDave Kleikamp 			break;
1072ac27a0ecSDave Kleikamp 		case Opt_debug:
1073ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1074ac27a0ecSDave Kleikamp 			break;
1075ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1076ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1077ac27a0ecSDave Kleikamp 			break;
1078ac27a0ecSDave Kleikamp 		case Opt_orlov:
1079ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1080ac27a0ecSDave Kleikamp 			break;
1081617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
1082ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1083ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1084ac27a0ecSDave Kleikamp 			break;
1085ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1086ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1087ac27a0ecSDave Kleikamp 			break;
1088ac27a0ecSDave Kleikamp #else
1089ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1090ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
10914776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)user_xattr options "
10924776004fSTheodore Ts'o 			       "not supported\n");
1093ac27a0ecSDave Kleikamp 			break;
1094ac27a0ecSDave Kleikamp #endif
1095617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1096ac27a0ecSDave Kleikamp 		case Opt_acl:
1097ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1098ac27a0ecSDave Kleikamp 			break;
1099ac27a0ecSDave Kleikamp 		case Opt_noacl:
1100ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1101ac27a0ecSDave Kleikamp 			break;
1102ac27a0ecSDave Kleikamp #else
1103ac27a0ecSDave Kleikamp 		case Opt_acl:
1104ac27a0ecSDave Kleikamp 		case Opt_noacl:
11054776004fSTheodore Ts'o 			printk(KERN_ERR "EXT4 (no)acl options "
11064776004fSTheodore Ts'o 			       "not supported\n");
1107ac27a0ecSDave Kleikamp 			break;
1108ac27a0ecSDave Kleikamp #endif
1109ac27a0ecSDave Kleikamp 		case Opt_reservation:
1110ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, RESERVATION);
1111ac27a0ecSDave Kleikamp 			break;
1112ac27a0ecSDave Kleikamp 		case Opt_noreservation:
1113ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, RESERVATION);
1114ac27a0ecSDave Kleikamp 			break;
1115ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1116ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1117ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1118ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1119ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1120ac27a0ecSDave Kleikamp 			   journal file. */
1121ac27a0ecSDave Kleikamp 			if (is_remount) {
1122617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1123ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1124ac27a0ecSDave Kleikamp 				return 0;
1125ac27a0ecSDave Kleikamp 			}
1126ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1127ac27a0ecSDave Kleikamp 			break;
1128ac27a0ecSDave Kleikamp 		case Opt_journal_inum:
1129ac27a0ecSDave Kleikamp 			if (is_remount) {
1130617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1131ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1132ac27a0ecSDave Kleikamp 				return 0;
1133ac27a0ecSDave Kleikamp 			}
1134ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1135ac27a0ecSDave Kleikamp 				return 0;
1136ac27a0ecSDave Kleikamp 			*inum = option;
1137ac27a0ecSDave Kleikamp 			break;
1138ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1139ac27a0ecSDave Kleikamp 			if (is_remount) {
1140617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1141ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1142ac27a0ecSDave Kleikamp 				return 0;
1143ac27a0ecSDave Kleikamp 			}
1144ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1145ac27a0ecSDave Kleikamp 				return 0;
1146ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1147ac27a0ecSDave Kleikamp 			break;
1148818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1149818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1150818d276cSGirish Shilamkar 			break;
1151818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1152818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1153818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1154818d276cSGirish Shilamkar 			break;
1155ac27a0ecSDave Kleikamp 		case Opt_noload:
1156ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1157ac27a0ecSDave Kleikamp 			break;
1158ac27a0ecSDave Kleikamp 		case Opt_commit:
1159ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1160ac27a0ecSDave Kleikamp 				return 0;
1161ac27a0ecSDave Kleikamp 			if (option < 0)
1162ac27a0ecSDave Kleikamp 				return 0;
1163ac27a0ecSDave Kleikamp 			if (option == 0)
1164cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1165ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1166ac27a0ecSDave Kleikamp 			break;
1167ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1168617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1169ac27a0ecSDave Kleikamp 			goto datacheck;
1170ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1171617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1172ac27a0ecSDave Kleikamp 			goto datacheck;
1173ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1174617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1175ac27a0ecSDave Kleikamp 		datacheck:
1176ac27a0ecSDave Kleikamp 			if (is_remount) {
1177617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1178ac27a0ecSDave Kleikamp 						!= data_opt) {
1179ac27a0ecSDave Kleikamp 					printk(KERN_ERR
1180617ba13bSMingming Cao 						"EXT4-fs: cannot change data "
1181ac27a0ecSDave Kleikamp 						"mode on remount\n");
1182ac27a0ecSDave Kleikamp 					return 0;
1183ac27a0ecSDave Kleikamp 				}
1184ac27a0ecSDave Kleikamp 			} else {
1185617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1186ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1187ac27a0ecSDave Kleikamp 			}
1188ac27a0ecSDave Kleikamp 			break;
1189ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1190ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1191ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1192ac27a0ecSDave Kleikamp 			goto set_qf_name;
1193ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1194ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1195ac27a0ecSDave Kleikamp set_qf_name:
1196dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1197dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1198dfc5d03fSJan Kara 			    !sbi->s_qf_names[qtype]) {
1199ac27a0ecSDave Kleikamp 				printk(KERN_ERR
12002c8be6b2SJan Kara 				       "EXT4-fs: Cannot change journaled "
1201ac27a0ecSDave Kleikamp 				       "quota options when quota turned on.\n");
1202ac27a0ecSDave Kleikamp 				return 0;
1203ac27a0ecSDave Kleikamp 			}
1204ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1205ac27a0ecSDave Kleikamp 			if (!qname) {
1206ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1207617ba13bSMingming Cao 					"EXT4-fs: not enough memory for "
1208ac27a0ecSDave Kleikamp 					"storing quotafile name.\n");
1209ac27a0ecSDave Kleikamp 				return 0;
1210ac27a0ecSDave Kleikamp 			}
1211ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1212ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1213ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1214617ba13bSMingming Cao 					"EXT4-fs: %s quota file already "
1215ac27a0ecSDave Kleikamp 					"specified.\n", QTYPE2NAME(qtype));
1216ac27a0ecSDave Kleikamp 				kfree(qname);
1217ac27a0ecSDave Kleikamp 				return 0;
1218ac27a0ecSDave Kleikamp 			}
1219ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1220ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1221ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1222617ba13bSMingming Cao 					"EXT4-fs: quotafile must be on "
1223ac27a0ecSDave Kleikamp 					"filesystem root.\n");
1224ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1225ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1226ac27a0ecSDave Kleikamp 				return 0;
1227ac27a0ecSDave Kleikamp 			}
1228ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1229ac27a0ecSDave Kleikamp 			break;
1230ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1231ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1232ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1233ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1234ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1235ac27a0ecSDave Kleikamp clear_qf_name:
1236dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1237dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1238dfc5d03fSJan Kara 			    sbi->s_qf_names[qtype]) {
1239617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change "
12402c8be6b2SJan Kara 					"journaled quota options when "
1241ac27a0ecSDave Kleikamp 					"quota turned on.\n");
1242ac27a0ecSDave Kleikamp 				return 0;
1243ac27a0ecSDave Kleikamp 			}
1244ac27a0ecSDave Kleikamp 			/*
1245ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1246ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1247ac27a0ecSDave Kleikamp 			 */
1248ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1249ac27a0ecSDave Kleikamp 			break;
1250ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1251dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1252dfc5d03fSJan Kara 			goto set_qf_format;
1253ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1254dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
1255dfc5d03fSJan Kara set_qf_format:
1256dfc5d03fSJan Kara 			if ((sb_any_quota_enabled(sb) ||
1257dfc5d03fSJan Kara 			     sb_any_quota_suspended(sb)) &&
1258dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1259dfc5d03fSJan Kara 				printk(KERN_ERR "EXT4-fs: Cannot change "
1260dfc5d03fSJan Kara 					"journaled quota options when "
1261dfc5d03fSJan Kara 					"quota turned on.\n");
1262dfc5d03fSJan Kara 				return 0;
1263dfc5d03fSJan Kara 			}
1264dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1265ac27a0ecSDave Kleikamp 			break;
1266ac27a0ecSDave Kleikamp 		case Opt_quota:
1267ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1268ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1269ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1270ac27a0ecSDave Kleikamp 			break;
1271ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1272ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1273ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1274ac27a0ecSDave Kleikamp 			break;
1275ac27a0ecSDave Kleikamp 		case Opt_noquota:
1276ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1277617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change quota "
1278ac27a0ecSDave Kleikamp 					"options when quota turned on.\n");
1279ac27a0ecSDave Kleikamp 				return 0;
1280ac27a0ecSDave Kleikamp 			}
1281ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1282ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1283ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1284ac27a0ecSDave Kleikamp 			break;
1285ac27a0ecSDave Kleikamp #else
1286ac27a0ecSDave Kleikamp 		case Opt_quota:
1287ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1288ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1289cd59e7b9SJan Kara 			printk(KERN_ERR
1290cd59e7b9SJan Kara 				"EXT4-fs: quota options not supported.\n");
1291cd59e7b9SJan Kara 			break;
1292ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1293ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1294ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1295ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1296ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1297ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1298ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1299cd59e7b9SJan Kara 				"EXT4-fs: journaled quota options not "
1300ac27a0ecSDave Kleikamp 				"supported.\n");
1301ac27a0ecSDave Kleikamp 			break;
1302ac27a0ecSDave Kleikamp 		case Opt_noquota:
1303ac27a0ecSDave Kleikamp 			break;
1304ac27a0ecSDave Kleikamp #endif
1305ac27a0ecSDave Kleikamp 		case Opt_abort:
1306ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ABORT);
1307ac27a0ecSDave Kleikamp 			break;
1308ac27a0ecSDave Kleikamp 		case Opt_barrier:
1309ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1310ac27a0ecSDave Kleikamp 				return 0;
1311ac27a0ecSDave Kleikamp 			if (option)
1312ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1313ac27a0ecSDave Kleikamp 			else
1314ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1315ac27a0ecSDave Kleikamp 			break;
1316ac27a0ecSDave Kleikamp 		case Opt_ignore:
1317ac27a0ecSDave Kleikamp 			break;
1318ac27a0ecSDave Kleikamp 		case Opt_resize:
1319ac27a0ecSDave Kleikamp 			if (!is_remount) {
1320617ba13bSMingming Cao 				printk("EXT4-fs: resize option only available "
1321ac27a0ecSDave Kleikamp 					"for remount\n");
1322ac27a0ecSDave Kleikamp 				return 0;
1323ac27a0ecSDave Kleikamp 			}
1324ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1325ac27a0ecSDave Kleikamp 				return 0;
1326ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1327ac27a0ecSDave Kleikamp 			break;
1328ac27a0ecSDave Kleikamp 		case Opt_nobh:
1329ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1330ac27a0ecSDave Kleikamp 			break;
1331ac27a0ecSDave Kleikamp 		case Opt_bh:
1332ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1333ac27a0ecSDave Kleikamp 			break;
1334a86c6181SAlex Tomas 		case Opt_extents:
1335e4079a11SEric Sandeen 			if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1336e4079a11SEric Sandeen 					EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1337e4079a11SEric Sandeen 				ext4_warning(sb, __func__,
1338e4079a11SEric Sandeen 					"extents feature not enabled "
1339e4079a11SEric Sandeen 					"on this filesystem, use tune2fs\n");
1340e4079a11SEric Sandeen 				return 0;
1341e4079a11SEric Sandeen 			}
1342a86c6181SAlex Tomas 			set_opt(sbi->s_mount_opt, EXTENTS);
1343a86c6181SAlex Tomas 			break;
13441e2462f9SMingming Cao 		case Opt_noextents:
1345c07651b5SAneesh Kumar K.V 			/*
1346c07651b5SAneesh Kumar K.V 			 * When e2fsprogs support resizing an already existing
1347c07651b5SAneesh Kumar K.V 			 * ext3 file system to greater than 2**32 we need to
1348c07651b5SAneesh Kumar K.V 			 * add support to block allocator to handle growing
1349c07651b5SAneesh Kumar K.V 			 * already existing block  mapped inode so that blocks
1350c07651b5SAneesh Kumar K.V 			 * allocated for them fall within 2**32
1351c07651b5SAneesh Kumar K.V 			 */
1352c07651b5SAneesh Kumar K.V 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1353c07651b5SAneesh Kumar K.V 			if (last_block  > 0xffffffffULL) {
1354c07651b5SAneesh Kumar K.V 				printk(KERN_ERR "EXT4-fs: Filesystem too "
1355c07651b5SAneesh Kumar K.V 						"large to mount with "
1356c07651b5SAneesh Kumar K.V 						"-o noextents options\n");
1357c07651b5SAneesh Kumar K.V 				return 0;
1358c07651b5SAneesh Kumar K.V 			}
13591e2462f9SMingming Cao 			clear_opt(sbi->s_mount_opt, EXTENTS);
13601e2462f9SMingming Cao 			break;
136125ec56b5SJean Noel Cordenner 		case Opt_i_version:
136225ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
136325ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
136425ec56b5SJean Noel Cordenner 			break;
1365dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1366dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1367dd919b98SAneesh Kumar K.V 			break;
1368c9de560dSAlex Tomas 		case Opt_mballoc:
1369c9de560dSAlex Tomas 			set_opt(sbi->s_mount_opt, MBALLOC);
1370c9de560dSAlex Tomas 			break;
1371c9de560dSAlex Tomas 		case Opt_nomballoc:
1372c9de560dSAlex Tomas 			clear_opt(sbi->s_mount_opt, MBALLOC);
1373c9de560dSAlex Tomas 			break;
1374c9de560dSAlex Tomas 		case Opt_stripe:
1375c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1376c9de560dSAlex Tomas 				return 0;
1377c9de560dSAlex Tomas 			if (option < 0)
1378c9de560dSAlex Tomas 				return 0;
1379c9de560dSAlex Tomas 			sbi->s_stripe = option;
1380c9de560dSAlex Tomas 			break;
138164769240SAlex Tomas 		case Opt_delalloc:
138264769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
138364769240SAlex Tomas 			break;
1384ac27a0ecSDave Kleikamp 		default:
1385ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1386617ba13bSMingming Cao 			       "EXT4-fs: Unrecognized mount option \"%s\" "
1387ac27a0ecSDave Kleikamp 			       "or missing value\n", p);
1388ac27a0ecSDave Kleikamp 			return 0;
1389ac27a0ecSDave Kleikamp 		}
1390ac27a0ecSDave Kleikamp 	}
1391ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1392ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1393617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1394ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1395ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1396ac27a0ecSDave Kleikamp 
1397617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1398ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1399ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1400ac27a0ecSDave Kleikamp 
1401ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1402617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1403ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1404617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1405617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: old and new quota "
1406ac27a0ecSDave Kleikamp 					"format mixing.\n");
1407ac27a0ecSDave Kleikamp 			return 0;
1408ac27a0ecSDave Kleikamp 		}
1409ac27a0ecSDave Kleikamp 
1410ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
14112c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
1412ac27a0ecSDave Kleikamp 					"not specified.\n");
1413ac27a0ecSDave Kleikamp 			return 0;
1414ac27a0ecSDave Kleikamp 		}
1415ac27a0ecSDave Kleikamp 	} else {
1416ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
14172c8be6b2SJan Kara 			printk(KERN_ERR "EXT4-fs: journaled quota format "
14182c8be6b2SJan Kara 					"specified with no journaling "
1419ac27a0ecSDave Kleikamp 					"enabled.\n");
1420ac27a0ecSDave Kleikamp 			return 0;
1421ac27a0ecSDave Kleikamp 		}
1422ac27a0ecSDave Kleikamp 	}
1423ac27a0ecSDave Kleikamp #endif
1424ac27a0ecSDave Kleikamp 	return 1;
1425ac27a0ecSDave Kleikamp }
1426ac27a0ecSDave Kleikamp 
1427617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1428ac27a0ecSDave Kleikamp 			    int read_only)
1429ac27a0ecSDave Kleikamp {
1430617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1431ac27a0ecSDave Kleikamp 	int res = 0;
1432ac27a0ecSDave Kleikamp 
1433617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1434617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1435ac27a0ecSDave Kleikamp 		       "forcing read-only mode\n");
1436ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1437ac27a0ecSDave Kleikamp 	}
1438ac27a0ecSDave Kleikamp 	if (read_only)
1439ac27a0ecSDave Kleikamp 		return res;
1440617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1441617ba13bSMingming Cao 		printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1442ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1443617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1444ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1445617ba13bSMingming Cao 		       "EXT4-fs warning: mounting fs with errors, "
1446ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1447ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1448ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1449ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1450ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1451617ba13bSMingming Cao 		       "EXT4-fs warning: maximal mount count reached, "
1452ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1453ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1454ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1455ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1456ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1457617ba13bSMingming Cao 		       "EXT4-fs warning: checktime reached, "
1458ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1459ac27a0ecSDave Kleikamp #if 0
1460ac27a0ecSDave Kleikamp 		/* @@@ We _will_ want to clear the valid bit if we find
146163f57933SAndrew Morton 		 * inconsistencies, to force a fsck at reboot.  But for
146263f57933SAndrew Morton 		 * a plain journaled filesystem we can keep it set as
146363f57933SAndrew Morton 		 * valid forever! :)
146463f57933SAndrew Morton 		 */
1465216c34b2SMarcin Slusarz 	es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1466ac27a0ecSDave Kleikamp #endif
1467ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1468617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1469e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1470ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1471617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
1472617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1473ac27a0ecSDave Kleikamp 
1474617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
1475ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1476617ba13bSMingming Cao 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1477ac27a0ecSDave Kleikamp 				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
1478ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1479ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1480617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1481617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1482ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1483ac27a0ecSDave Kleikamp 
148405496769STheodore Ts'o 	printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
148505496769STheodore Ts'o 	       sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
148605496769STheodore Ts'o 	       "external", EXT4_SB(sb)->s_journal->j_devname);
1487ac27a0ecSDave Kleikamp 	return res;
1488ac27a0ecSDave Kleikamp }
1489ac27a0ecSDave Kleikamp 
1490772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1491772cb7c8SJose R. Santos {
1492772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1493772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1494772cb7c8SJose R. Santos 	struct buffer_head *bh;
1495772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1496772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1497772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1498772cb7c8SJose R. Santos 	__u64 block_bitmap = 0;
1499772cb7c8SJose R. Santos 	int i;
1500772cb7c8SJose R. Santos 
1501772cb7c8SJose R. Santos 	if (!sbi->s_es->s_log_groups_per_flex) {
1502772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1503772cb7c8SJose R. Santos 		return 1;
1504772cb7c8SJose R. Santos 	}
1505772cb7c8SJose R. Santos 
1506772cb7c8SJose R. Santos 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1507772cb7c8SJose R. Santos 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1508772cb7c8SJose R. Santos 
1509c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1510c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1511c62a11fdSFrederic Bohe 			    ((sbi->s_es->s_reserved_gdt_blocks +1 ) <<
1512c62a11fdSFrederic Bohe 			      EXT4_DESC_PER_BLOCK_BITS(sb))) /
1513772cb7c8SJose R. Santos 			   groups_per_flex;
1514ec05e868SLi Zefan 	sbi->s_flex_groups = kzalloc(flex_group_count *
1515772cb7c8SJose R. Santos 				     sizeof(struct flex_groups), GFP_KERNEL);
1516772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1517ec05e868SLi Zefan 		printk(KERN_ERR "EXT4-fs: not enough memory for "
1518ec05e868SLi Zefan 				"%lu flex groups\n", flex_group_count);
1519772cb7c8SJose R. Santos 		goto failed;
1520772cb7c8SJose R. Santos 	}
1521772cb7c8SJose R. Santos 
1522772cb7c8SJose R. Santos 	gdp = ext4_get_group_desc(sb, 1, &bh);
1523772cb7c8SJose R. Santos 	block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
1524772cb7c8SJose R. Santos 
1525772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
1526772cb7c8SJose R. Santos 		gdp = ext4_get_group_desc(sb, i, &bh);
1527772cb7c8SJose R. Santos 
1528772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
1529772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_inodes +=
1530772cb7c8SJose R. Santos 			le16_to_cpu(gdp->bg_free_inodes_count);
1531772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_blocks +=
1532772cb7c8SJose R. Santos 			le16_to_cpu(gdp->bg_free_blocks_count);
1533772cb7c8SJose R. Santos 	}
1534772cb7c8SJose R. Santos 
1535772cb7c8SJose R. Santos 	return 1;
1536772cb7c8SJose R. Santos failed:
1537772cb7c8SJose R. Santos 	return 0;
1538772cb7c8SJose R. Santos }
1539772cb7c8SJose R. Santos 
1540717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1541717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1542717d50e4SAndreas Dilger {
1543717d50e4SAndreas Dilger 	__u16 crc = 0;
1544717d50e4SAndreas Dilger 
1545717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1546717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1547717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1548717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1549717d50e4SAndreas Dilger 
1550717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1551717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1552717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1553717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1554717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1555717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1556717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1557717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1558717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1559717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1560717d50e4SAndreas Dilger 					offset);
1561717d50e4SAndreas Dilger 	}
1562717d50e4SAndreas Dilger 
1563717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1564717d50e4SAndreas Dilger }
1565717d50e4SAndreas Dilger 
1566717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1567717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1568717d50e4SAndreas Dilger {
1569717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1570717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1571717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1572717d50e4SAndreas Dilger 		return 0;
1573717d50e4SAndreas Dilger 
1574717d50e4SAndreas Dilger 	return 1;
1575717d50e4SAndreas Dilger }
1576717d50e4SAndreas Dilger 
1577ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1578617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1579ac27a0ecSDave Kleikamp {
1580617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1581617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1582617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1583bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1584bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1585bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1586ce421581SJose R. Santos 	int flexbg_flag = 0;
1587fd2d4291SAvantika Mathur 	ext4_group_t i;
1588ac27a0ecSDave Kleikamp 
1589ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1590ce421581SJose R. Santos 		flexbg_flag = 1;
1591ce421581SJose R. Santos 
1592617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1593ac27a0ecSDave Kleikamp 
1594197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1595197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1596197cd65aSAkinobu Mita 
1597ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1598bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1599ac27a0ecSDave Kleikamp 		else
1600ac27a0ecSDave Kleikamp 			last_block = first_block +
1601617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1602ac27a0ecSDave Kleikamp 
16038fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
16042b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1605c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1606c19204b0SJosef Bacik 			       "Block bitmap for group %lu not in group "
1607c19204b0SJosef Bacik 			       "(block %llu)!", i, block_bitmap);
1608ac27a0ecSDave Kleikamp 			return 0;
1609ac27a0ecSDave Kleikamp 		}
16108fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
16112b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1612c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1613c19204b0SJosef Bacik 			       "Inode bitmap for group %lu not in group "
1614c19204b0SJosef Bacik 			       "(block %llu)!", i, inode_bitmap);
1615ac27a0ecSDave Kleikamp 			return 0;
1616ac27a0ecSDave Kleikamp 		}
16178fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1618bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
16192b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1620c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1621c19204b0SJosef Bacik 			       "Inode table for group %lu not in group "
1622c19204b0SJosef Bacik 			       "(block %llu)!", i, inode_table);
1623ac27a0ecSDave Kleikamp 			return 0;
1624ac27a0ecSDave Kleikamp 		}
1625b5f10eedSEric Sandeen 		spin_lock(sb_bgl_lock(sbi, i));
1626717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1627c19204b0SJosef Bacik 			printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1628fd2d4291SAvantika Mathur 			       "Checksum for group %lu failed (%u!=%u)\n",
1629fd2d4291SAvantika Mathur 			       i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1630fd2d4291SAvantika Mathur 			       gdp)), le16_to_cpu(gdp->bg_checksum));
16317ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
16327ee1ec4cSLi Zefan 				spin_unlock(sb_bgl_lock(sbi, i));
1633717d50e4SAndreas Dilger 				return 0;
1634717d50e4SAndreas Dilger 			}
16357ee1ec4cSLi Zefan 		}
1636b5f10eedSEric Sandeen 		spin_unlock(sb_bgl_lock(sbi, i));
1637ce421581SJose R. Santos 		if (!flexbg_flag)
1638617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1639ac27a0ecSDave Kleikamp 	}
1640ac27a0ecSDave Kleikamp 
1641bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1642617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1643ac27a0ecSDave Kleikamp 	return 1;
1644ac27a0ecSDave Kleikamp }
1645ac27a0ecSDave Kleikamp 
1646617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1647ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1648ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1649ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1650ac27a0ecSDave Kleikamp  *
1651ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1652ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1653ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1654ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1655ac27a0ecSDave Kleikamp  *
1656ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1657ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1658ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1659617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1660ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1661ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1662ac27a0ecSDave Kleikamp  */
1663617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1664617ba13bSMingming Cao 				struct ext4_super_block *es)
1665ac27a0ecSDave Kleikamp {
1666ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1667ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1668ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1669ac27a0ecSDave Kleikamp 	int i;
1670ac27a0ecSDave Kleikamp #endif
1671ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1672ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1673ac27a0ecSDave Kleikamp 		return;
1674ac27a0ecSDave Kleikamp 	}
1675ac27a0ecSDave Kleikamp 
1676a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1677a8f48a95SEric Sandeen 		printk(KERN_ERR "EXT4-fs: write access "
1678a8f48a95SEric Sandeen 			"unavailable, skipping orphan cleanup.\n");
1679a8f48a95SEric Sandeen 		return;
1680a8f48a95SEric Sandeen 	}
1681a8f48a95SEric Sandeen 
1682617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1683ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1684ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1685ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1686ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1687ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1688ac27a0ecSDave Kleikamp 		return;
1689ac27a0ecSDave Kleikamp 	}
1690ac27a0ecSDave Kleikamp 
1691ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1692617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1693ac27a0ecSDave Kleikamp 		       sb->s_id);
1694ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1695ac27a0ecSDave Kleikamp 	}
1696ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1697ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1698ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1699ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1700ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1701617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1702617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1703ac27a0ecSDave Kleikamp 			if (ret < 0)
1704ac27a0ecSDave Kleikamp 				printk(KERN_ERR
17052c8be6b2SJan Kara 					"EXT4-fs: Cannot turn on journaled "
1706ac27a0ecSDave Kleikamp 					"quota: error %d\n", ret);
1707ac27a0ecSDave Kleikamp 		}
1708ac27a0ecSDave Kleikamp 	}
1709ac27a0ecSDave Kleikamp #endif
1710ac27a0ecSDave Kleikamp 
1711ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1712ac27a0ecSDave Kleikamp 		struct inode *inode;
1713ac27a0ecSDave Kleikamp 
171497bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
171597bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
1716ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1717ac27a0ecSDave Kleikamp 			break;
1718ac27a0ecSDave Kleikamp 		}
1719ac27a0ecSDave Kleikamp 
1720617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1721ac27a0ecSDave Kleikamp 		DQUOT_INIT(inode);
1722ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1723ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1724e5f8eab8STheodore Ts'o 				"%s: truncating inode %lu to %lld bytes\n",
172546e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
1726e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1727ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1728617ba13bSMingming Cao 			ext4_truncate(inode);
1729ac27a0ecSDave Kleikamp 			nr_truncates++;
1730ac27a0ecSDave Kleikamp 		} else {
1731ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1732ac27a0ecSDave Kleikamp 				"%s: deleting unreferenced inode %lu\n",
173346e665e9SHarvey Harrison 				__func__, inode->i_ino);
1734ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1735ac27a0ecSDave Kleikamp 				  inode->i_ino);
1736ac27a0ecSDave Kleikamp 			nr_orphans++;
1737ac27a0ecSDave Kleikamp 		}
1738ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1739ac27a0ecSDave Kleikamp 	}
1740ac27a0ecSDave Kleikamp 
1741ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1742ac27a0ecSDave Kleikamp 
1743ac27a0ecSDave Kleikamp 	if (nr_orphans)
1744617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1745ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_orphans));
1746ac27a0ecSDave Kleikamp 	if (nr_truncates)
1747617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1748ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_truncates));
1749ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1750ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1751ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1752ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
17536f28e087SJan Kara 			vfs_quota_off(sb, i, 0);
1754ac27a0ecSDave Kleikamp 	}
1755ac27a0ecSDave Kleikamp #endif
1756ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1757ac27a0ecSDave Kleikamp }
1758cd2291a4SEric Sandeen /*
1759cd2291a4SEric Sandeen  * Maximal extent format file size.
1760cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1761cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1762cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1763cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1764cd2291a4SEric Sandeen  *
1765cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1766cd2291a4SEric Sandeen  */
1767cd2291a4SEric Sandeen static loff_t ext4_max_size(int blkbits)
1768cd2291a4SEric Sandeen {
1769cd2291a4SEric Sandeen 	loff_t res;
1770cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
1771cd2291a4SEric Sandeen 
1772cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
1773cd2291a4SEric Sandeen 	if (sizeof(blkcnt_t) < sizeof(u64)) {
1774cd2291a4SEric Sandeen 		/*
1775cd2291a4SEric Sandeen 		 * CONFIG_LSF is not enabled implies the inode
1776cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
1777cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
1778cd2291a4SEric Sandeen 		 */
1779cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
1780cd2291a4SEric Sandeen 
1781cd2291a4SEric Sandeen 		/* total blocks in file system block size */
1782cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
1783cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
1784cd2291a4SEric Sandeen 	}
1785cd2291a4SEric Sandeen 
1786cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
1787cd2291a4SEric Sandeen 	res = 1LL << 32;
1788cd2291a4SEric Sandeen 	res <<= blkbits;
1789cd2291a4SEric Sandeen 	res -= 1;
1790cd2291a4SEric Sandeen 
1791cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
1792cd2291a4SEric Sandeen 	if (res > upper_limit)
1793cd2291a4SEric Sandeen 		res = upper_limit;
1794cd2291a4SEric Sandeen 
1795cd2291a4SEric Sandeen 	return res;
1796cd2291a4SEric Sandeen }
1797ac27a0ecSDave Kleikamp 
1798ac27a0ecSDave Kleikamp /*
1799cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
18000fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
18010fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
1802ac27a0ecSDave Kleikamp  */
1803cd2291a4SEric Sandeen static loff_t ext4_max_bitmap_size(int bits)
1804ac27a0ecSDave Kleikamp {
1805617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
18060fc1b451SAneesh Kumar K.V 	int meta_blocks;
18070fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
18080fc1b451SAneesh Kumar K.V 	/* This is calculated to be the largest file size for a
1809cd2291a4SEric Sandeen 	 * dense, bitmapped file such that the total number of
1810ac27a0ecSDave Kleikamp 	 * sectors in the file, including data and all indirect blocks,
18110fc1b451SAneesh Kumar K.V 	 * does not exceed 2^48 -1
18120fc1b451SAneesh Kumar K.V 	 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
18130fc1b451SAneesh Kumar K.V 	 * total number of  512 bytes blocks of the file
18140fc1b451SAneesh Kumar K.V 	 */
18150fc1b451SAneesh Kumar K.V 
18160fc1b451SAneesh Kumar K.V 	if (sizeof(blkcnt_t) < sizeof(u64)) {
18170fc1b451SAneesh Kumar K.V 		/*
18180fc1b451SAneesh Kumar K.V 		 * CONFIG_LSF is not enabled implies the inode
18190fc1b451SAneesh Kumar K.V 		 * i_block represent total blocks in 512 bytes
18200fc1b451SAneesh Kumar K.V 		 * 32 == size of vfs inode i_blocks * 8
18210fc1b451SAneesh Kumar K.V 		 */
18220fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
18230fc1b451SAneesh Kumar K.V 
18240fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
18250fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
18260fc1b451SAneesh Kumar K.V 
18270fc1b451SAneesh Kumar K.V 	} else {
18288180a562SAneesh Kumar K.V 		/*
18298180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
18308180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
18318180a562SAneesh Kumar K.V 		 * represent total number of blocks in
18328180a562SAneesh Kumar K.V 		 * file system block size
18338180a562SAneesh Kumar K.V 		 */
18340fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
18350fc1b451SAneesh Kumar K.V 
18360fc1b451SAneesh Kumar K.V 	}
18370fc1b451SAneesh Kumar K.V 
18380fc1b451SAneesh Kumar K.V 	/* indirect blocks */
18390fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
18400fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
18410fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
18420fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
18430fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
18440fc1b451SAneesh Kumar K.V 
18450fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
18460fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
1847ac27a0ecSDave Kleikamp 
1848ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
1849ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
1850ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
1851ac27a0ecSDave Kleikamp 	res <<= bits;
1852ac27a0ecSDave Kleikamp 	if (res > upper_limit)
1853ac27a0ecSDave Kleikamp 		res = upper_limit;
18540fc1b451SAneesh Kumar K.V 
18550fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
18560fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
18570fc1b451SAneesh Kumar K.V 
1858ac27a0ecSDave Kleikamp 	return res;
1859ac27a0ecSDave Kleikamp }
1860ac27a0ecSDave Kleikamp 
1861617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
186270bbb3e0SAndrew Morton 				ext4_fsblk_t logical_sb_block, int nr)
1863ac27a0ecSDave Kleikamp {
1864617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1865fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
1866ac27a0ecSDave Kleikamp 	int has_super = 0;
1867ac27a0ecSDave Kleikamp 
1868ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1869ac27a0ecSDave Kleikamp 
1870617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1871ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
187270bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
1873ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
1874617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
1875ac27a0ecSDave Kleikamp 		has_super = 1;
1876617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
1877ac27a0ecSDave Kleikamp }
1878ac27a0ecSDave Kleikamp 
1879c9de560dSAlex Tomas /**
1880c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
1881c9de560dSAlex Tomas  * @sbi: In memory super block info
1882c9de560dSAlex Tomas  *
1883c9de560dSAlex Tomas  * If we have specified it via mount option, then
1884c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
1885c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
1886c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
1887c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
1888c9de560dSAlex Tomas  *
1889c9de560dSAlex Tomas  */
1890c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1891c9de560dSAlex Tomas {
1892c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1893c9de560dSAlex Tomas 	unsigned long stripe_width =
1894c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1895c9de560dSAlex Tomas 
1896c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1897c9de560dSAlex Tomas 		return sbi->s_stripe;
1898c9de560dSAlex Tomas 
1899c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
1900c9de560dSAlex Tomas 		return stripe_width;
1901c9de560dSAlex Tomas 
1902c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
1903c9de560dSAlex Tomas 		return stride;
1904c9de560dSAlex Tomas 
1905c9de560dSAlex Tomas 	return 0;
1906c9de560dSAlex Tomas }
1907ac27a0ecSDave Kleikamp 
1908617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
19097477827fSAneesh Kumar K.V 				__releases(kernel_lock)
19107477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
19111d03ec98SAneesh Kumar K.V 
1912ac27a0ecSDave Kleikamp {
1913ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
1914617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
1915617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1916617ba13bSMingming Cao 	ext4_fsblk_t block;
1917617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
191870bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
1919ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
1920ac27a0ecSDave Kleikamp 	unsigned int journal_inum = 0;
1921ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
1922ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
1923ac27a0ecSDave Kleikamp 	struct inode *root;
19249f6200bbSTheodore Ts'o 	char *cp;
19251d1fe1eeSDavid Howells 	int ret = -EINVAL;
1926ac27a0ecSDave Kleikamp 	int blocksize;
1927ac27a0ecSDave Kleikamp 	int db_count;
1928ac27a0ecSDave Kleikamp 	int i;
1929ac27a0ecSDave Kleikamp 	int needs_recovery;
1930ac27a0ecSDave Kleikamp 	__le32 features;
1931bd81d8eeSLaurent Vivier 	__u64 blocks_count;
1932833f4077SPeter Zijlstra 	int err;
1933ac27a0ecSDave Kleikamp 
1934ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1935ac27a0ecSDave Kleikamp 	if (!sbi)
1936ac27a0ecSDave Kleikamp 		return -ENOMEM;
1937ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
1938ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
1939617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
1940617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
1941d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
1942ac27a0ecSDave Kleikamp 
1943ac27a0ecSDave Kleikamp 	unlock_kernel();
1944ac27a0ecSDave Kleikamp 
19459f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
19469f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
19479f6200bbSTheodore Ts'o 		*cp = '!';
19489f6200bbSTheodore Ts'o 
1949617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1950ac27a0ecSDave Kleikamp 	if (!blocksize) {
1951617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1952ac27a0ecSDave Kleikamp 		goto out_fail;
1953ac27a0ecSDave Kleikamp 	}
1954ac27a0ecSDave Kleikamp 
1955ac27a0ecSDave Kleikamp 	/*
1956617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
1957ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
1958ac27a0ecSDave Kleikamp 	 */
1959617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
196070bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
196170bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
1962ac27a0ecSDave Kleikamp 	} else {
196370bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
1964ac27a0ecSDave Kleikamp 	}
1965ac27a0ecSDave Kleikamp 
196670bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
1967617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
1968ac27a0ecSDave Kleikamp 		goto out_fail;
1969ac27a0ecSDave Kleikamp 	}
1970ac27a0ecSDave Kleikamp 	/*
1971ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
1972617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
1973ac27a0ecSDave Kleikamp 	 */
1974617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1975ac27a0ecSDave Kleikamp 	sbi->s_es = es;
1976ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
1977617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
1978617ba13bSMingming Cao 		goto cantfind_ext4;
1979ac27a0ecSDave Kleikamp 
1980ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
1981ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1982617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
1983ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
1984617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1985ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
1986617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
1987ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
19882e7842b8SHugh Dickins #ifdef CONFIG_EXT4DEV_FS_XATTR
1989617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1990ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
19912e7842b8SHugh Dickins #endif
19922e7842b8SHugh Dickins #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1993617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
1994ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
19952e7842b8SHugh Dickins #endif
1996617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1997617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1998617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1999617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2000617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2001617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2002ac27a0ecSDave Kleikamp 
2003617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2004ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2005bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2006ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
2007bb4f397aSAneesh Kumar K.V 	else
2008bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
2009ac27a0ecSDave Kleikamp 
2010ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2011ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2012ac27a0ecSDave Kleikamp 
2013ac27a0ecSDave Kleikamp 	set_opt(sbi->s_mount_opt, RESERVATION);
2014571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
2015ac27a0ecSDave Kleikamp 
20161e2462f9SMingming Cao 	/*
20171e2462f9SMingming Cao 	 * turn on extents feature by default in ext4 filesystem
2018e4079a11SEric Sandeen 	 * only if feature flag already set by mkfs or tune2fs.
2019e4079a11SEric Sandeen 	 * Use -o noextents to turn it off
20201e2462f9SMingming Cao 	 */
2021e4079a11SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
20221e2462f9SMingming Cao 		set_opt(sbi->s_mount_opt, EXTENTS);
2023e4079a11SEric Sandeen 	else
2024e4079a11SEric Sandeen 		ext4_warning(sb, __func__,
2025e4079a11SEric Sandeen 			"extents feature not enabled on this filesystem, "
2026e4079a11SEric Sandeen 			"use tune2fs.\n");
20273dbd0edeSAneesh Kumar K.V 	/*
2028e4079a11SEric Sandeen 	 * turn on mballoc code by default in ext4 filesystem
2029e4079a11SEric Sandeen 	 * Use -o nomballoc to turn it off
20303dbd0edeSAneesh Kumar K.V 	 */
20313dbd0edeSAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, MBALLOC);
20321e2462f9SMingming Cao 
2033dd919b98SAneesh Kumar K.V 	/*
2034dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
2035dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
2036dd919b98SAneesh Kumar K.V 	 */
2037dd919b98SAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, DELALLOC);
2038dd919b98SAneesh Kumar K.V 
2039dd919b98SAneesh Kumar K.V 
2040ac27a0ecSDave Kleikamp 	if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2041ac27a0ecSDave Kleikamp 			   NULL, 0))
2042ac27a0ecSDave Kleikamp 		goto failed_mount;
2043ac27a0ecSDave Kleikamp 
2044ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2045617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2046ac27a0ecSDave Kleikamp 
2047617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2048617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2049617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2050617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2051ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
2052617ba13bSMingming Cao 		       "EXT4-fs warning: feature flags set on rev 0 fs, "
2053ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
2054469108ffSTheodore Tso 
2055469108ffSTheodore Tso 	/*
2056469108ffSTheodore Tso 	 * Since ext4 is still considered development code, we require
2057469108ffSTheodore Tso 	 * that the TEST_FILESYS flag in s->flags be set.
2058469108ffSTheodore Tso 	 */
2059469108ffSTheodore Tso 	if (!(le32_to_cpu(es->s_flags) & EXT2_FLAGS_TEST_FILESYS)) {
2060469108ffSTheodore Tso 		printk(KERN_WARNING "EXT4-fs: %s: not marked "
2061469108ffSTheodore Tso 		       "OK to use with test code.\n", sb->s_id);
2062469108ffSTheodore Tso 		goto failed_mount;
2063469108ffSTheodore Tso 	}
2064469108ffSTheodore Tso 
2065ac27a0ecSDave Kleikamp 	/*
2066ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2067ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2068ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2069ac27a0ecSDave Kleikamp 	 */
2070617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2071ac27a0ecSDave Kleikamp 	if (features) {
2072617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2073ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
2074ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
2075ac27a0ecSDave Kleikamp 		goto failed_mount;
2076ac27a0ecSDave Kleikamp 	}
2077617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2078ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
2079617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2080ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
2081ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
2082ac27a0ecSDave Kleikamp 		goto failed_mount;
2083ac27a0ecSDave Kleikamp 	}
20840fc1b451SAneesh Kumar K.V 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
20850fc1b451SAneesh Kumar K.V 		/*
20860fc1b451SAneesh Kumar K.V 		 * Large file size enabled file system can only be
20870fc1b451SAneesh Kumar K.V 		 * mount if kernel is build with CONFIG_LSF
20880fc1b451SAneesh Kumar K.V 		 */
20890fc1b451SAneesh Kumar K.V 		if (sizeof(root->i_blocks) < sizeof(u64) &&
20900fc1b451SAneesh Kumar K.V 				!(sb->s_flags & MS_RDONLY)) {
20910fc1b451SAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
20920fc1b451SAneesh Kumar K.V 					"files cannot be mounted read-write "
20930fc1b451SAneesh Kumar K.V 					"without CONFIG_LSF.\n", sb->s_id);
20940fc1b451SAneesh Kumar K.V 			goto failed_mount;
20950fc1b451SAneesh Kumar K.V 		}
20960fc1b451SAneesh Kumar K.V 	}
2097ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2098ac27a0ecSDave Kleikamp 
2099617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2100617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2101ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2102617ba13bSMingming Cao 		       "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2103ac27a0ecSDave Kleikamp 		       blocksize, sb->s_id);
2104ac27a0ecSDave Kleikamp 		goto failed_mount;
2105ac27a0ecSDave Kleikamp 	}
2106ac27a0ecSDave Kleikamp 
2107ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2108ce40733cSAneesh Kumar K.V 
2109ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2110ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2111ce40733cSAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2112ce40733cSAneesh Kumar K.V 					blocksize);
2113ac27a0ecSDave Kleikamp 			goto failed_mount;
2114ac27a0ecSDave Kleikamp 		}
2115ac27a0ecSDave Kleikamp 
2116ac27a0ecSDave Kleikamp 		brelse(bh);
211770bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
211870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
211970bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2120ac27a0ecSDave Kleikamp 		if (!bh) {
2121ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2122617ba13bSMingming Cao 			       "EXT4-fs: Can't read superblock on 2nd try.\n");
2123ac27a0ecSDave Kleikamp 			goto failed_mount;
2124ac27a0ecSDave Kleikamp 		}
2125617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2126ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2127617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2128ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2129617ba13bSMingming Cao 			       "EXT4-fs: Magic mismatch, very weird !\n");
2130ac27a0ecSDave Kleikamp 			goto failed_mount;
2131ac27a0ecSDave Kleikamp 		}
2132ac27a0ecSDave Kleikamp 	}
2133ac27a0ecSDave Kleikamp 
2134e2b46574SEric Sandeen 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits);
2135617ba13bSMingming Cao 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
2136ac27a0ecSDave Kleikamp 
2137617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2138617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2139617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2140ac27a0ecSDave Kleikamp 	} else {
2141ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2142ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2143617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
21441330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2145ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2146ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2147617ba13bSMingming Cao 			       "EXT4-fs: unsupported inode size: %d\n",
2148ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2149ac27a0ecSDave Kleikamp 			goto failed_mount;
2150ac27a0ecSDave Kleikamp 		}
2151ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2152ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2153ac27a0ecSDave Kleikamp 	}
21540d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
21550d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
21568fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
21570d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2158d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
21590d1ee42fSAlexandre Ratchov 			printk(KERN_ERR
21608fadc143SAlexandre Ratchov 			       "EXT4-fs: unsupported descriptor size %lu\n",
21610d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
21620d1ee42fSAlexandre Ratchov 			goto failed_mount;
21630d1ee42fSAlexandre Ratchov 		}
21640d1ee42fSAlexandre Ratchov 	} else
21650d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2166ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2167ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2168b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2169617ba13bSMingming Cao 		goto cantfind_ext4;
2170617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2171ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2172617ba13bSMingming Cao 		goto cantfind_ext4;
2173ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2174ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
21750d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2176ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2177ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2178e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2179e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2180ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2181ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2182ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2183ac27a0ecSDave Kleikamp 
2184ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2185ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2186617ba13bSMingming Cao 		       "EXT4-fs: #blocks per group too big: %lu\n",
2187ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2188ac27a0ecSDave Kleikamp 		goto failed_mount;
2189ac27a0ecSDave Kleikamp 	}
2190ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2191ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2192617ba13bSMingming Cao 		       "EXT4-fs: #inodes per group too big: %lu\n",
2193ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2194ac27a0ecSDave Kleikamp 		goto failed_mount;
2195ac27a0ecSDave Kleikamp 	}
2196ac27a0ecSDave Kleikamp 
2197bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
2198ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2199617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2200ac27a0ecSDave Kleikamp 			" too large to mount safely\n", sb->s_id);
2201ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
2202617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2203ac27a0ecSDave Kleikamp 					"enabled\n");
2204ac27a0ecSDave Kleikamp 		goto failed_mount;
2205ac27a0ecSDave Kleikamp 	}
2206ac27a0ecSDave Kleikamp 
2207617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2208617ba13bSMingming Cao 		goto cantfind_ext4;
2209e7c95593SEric Sandeen 
2210e7c95593SEric Sandeen 	/* ensure blocks_count calculation below doesn't sign-extend */
2211e7c95593SEric Sandeen 	if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2212e7c95593SEric Sandeen 	    le32_to_cpu(es->s_first_data_block) + 1) {
2213e7c95593SEric Sandeen 		printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2214e7c95593SEric Sandeen 		       "first data block %u, blocks per group %lu\n",
2215e7c95593SEric Sandeen 			ext4_blocks_count(es),
2216e7c95593SEric Sandeen 			le32_to_cpu(es->s_first_data_block),
2217e7c95593SEric Sandeen 			EXT4_BLOCKS_PER_GROUP(sb));
2218e7c95593SEric Sandeen 		goto failed_mount;
2219e7c95593SEric Sandeen 	}
2220bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2221bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2222bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2223bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2224bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2225617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2226617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2227ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2228ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2229ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2230617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: not enough memory\n");
2231ac27a0ecSDave Kleikamp 		goto failed_mount;
2232ac27a0ecSDave Kleikamp 	}
2233ac27a0ecSDave Kleikamp 
22349f6200bbSTheodore Ts'o 	if (ext4_proc_root)
22359f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
22369f6200bbSTheodore Ts'o 
2237ac27a0ecSDave Kleikamp 	bgl_lock_init(&sbi->s_blockgroup_lock);
2238ac27a0ecSDave Kleikamp 
2239ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
224070bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2241ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2242ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2243617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: "
2244ac27a0ecSDave Kleikamp 			       "can't read group descriptor %d\n", i);
2245ac27a0ecSDave Kleikamp 			db_count = i;
2246ac27a0ecSDave Kleikamp 			goto failed_mount2;
2247ac27a0ecSDave Kleikamp 		}
2248ac27a0ecSDave Kleikamp 	}
2249617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2250617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2251ac27a0ecSDave Kleikamp 		goto failed_mount2;
2252ac27a0ecSDave Kleikamp 	}
2253772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2254772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2255772cb7c8SJose R. Santos 			printk(KERN_ERR
2256772cb7c8SJose R. Santos 			       "EXT4-fs: unable to initialize "
2257772cb7c8SJose R. Santos 			       "flex_bg meta info!\n");
2258772cb7c8SJose R. Santos 			goto failed_mount2;
2259772cb7c8SJose R. Santos 		}
2260772cb7c8SJose R. Santos 
2261ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2262ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2263ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2264ac27a0ecSDave Kleikamp 
2265833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2266617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2267833f4077SPeter Zijlstra 	if (!err) {
2268833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2269617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2270833f4077SPeter Zijlstra 	}
2271833f4077SPeter Zijlstra 	if (!err) {
2272833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2273617ba13bSMingming Cao 				ext4_count_dirs(sb));
2274833f4077SPeter Zijlstra 	}
22756bc6e63fSAneesh Kumar K.V 	if (!err) {
22766bc6e63fSAneesh Kumar K.V 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
22776bc6e63fSAneesh Kumar K.V 	}
2278833f4077SPeter Zijlstra 	if (err) {
2279833f4077SPeter Zijlstra 		printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2280833f4077SPeter Zijlstra 		goto failed_mount3;
2281833f4077SPeter Zijlstra 	}
2282ac27a0ecSDave Kleikamp 
2283ac27a0ecSDave Kleikamp 	/* per fileystem reservation list head & lock */
2284ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_rsv_window_lock);
2285ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_root = RB_ROOT;
2286ac27a0ecSDave Kleikamp 	/* Add a single, static dummy reservation to the start of the
2287ac27a0ecSDave Kleikamp 	 * reservation window list --- it gives us a placeholder for
2288ac27a0ecSDave Kleikamp 	 * append-at-start-of-list which makes the allocation logic
2289ac27a0ecSDave Kleikamp 	 * _much_ simpler. */
2290617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
2291617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
2292ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_alloc_hit = 0;
2293ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_goal_size = 0;
2294617ba13bSMingming Cao 	ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
2295ac27a0ecSDave Kleikamp 
2296c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
2297c9de560dSAlex Tomas 
2298ac27a0ecSDave Kleikamp 	/*
2299ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2300ac27a0ecSDave Kleikamp 	 */
2301617ba13bSMingming Cao 	sb->s_op = &ext4_sops;
2302617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2303617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2304ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2305617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2306617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2307ac27a0ecSDave Kleikamp #endif
2308ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2309ac27a0ecSDave Kleikamp 
2310ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2311ac27a0ecSDave Kleikamp 
2312ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2313617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2314617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2315ac27a0ecSDave Kleikamp 
2316ac27a0ecSDave Kleikamp 	/*
2317ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2318ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2319ac27a0ecSDave Kleikamp 	 */
2320ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2321617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2322617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2323ac27a0ecSDave Kleikamp 			goto failed_mount3;
2324624080edSTheodore Ts'o 		if (!(sb->s_flags & MS_RDONLY) &&
2325624080edSTheodore Ts'o 		    EXT4_SB(sb)->s_journal->j_failed_commit) {
2326624080edSTheodore Ts'o 			printk(KERN_CRIT "EXT4-fs error (device %s): "
2327624080edSTheodore Ts'o 			       "ext4_fill_super: Journal transaction "
2328624080edSTheodore Ts'o 			       "%u is corrupt\n", sb->s_id,
2329624080edSTheodore Ts'o 			       EXT4_SB(sb)->s_journal->j_failed_commit);
2330624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_RO)) {
2331624080edSTheodore Ts'o 				printk(KERN_CRIT
2332624080edSTheodore Ts'o 				       "Mounting filesystem read-only\n");
2333624080edSTheodore Ts'o 				sb->s_flags |= MS_RDONLY;
2334624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2335624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2336624080edSTheodore Ts'o 			}
2337624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_PANIC)) {
2338624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2339624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2340624080edSTheodore Ts'o 				ext4_commit_super(sb, es, 1);
2341624080edSTheodore Ts'o 				printk(KERN_CRIT
2342624080edSTheodore Ts'o 				       "EXT4-fs (device %s): mount failed\n",
2343624080edSTheodore Ts'o 				      sb->s_id);
2344624080edSTheodore Ts'o 				goto failed_mount4;
2345624080edSTheodore Ts'o 			}
2346624080edSTheodore Ts'o 		}
2347ac27a0ecSDave Kleikamp 	} else if (journal_inum) {
2348617ba13bSMingming Cao 		if (ext4_create_journal(sb, es, journal_inum))
2349ac27a0ecSDave Kleikamp 			goto failed_mount3;
2350ac27a0ecSDave Kleikamp 	} else {
2351ac27a0ecSDave Kleikamp 		if (!silent)
2352ac27a0ecSDave Kleikamp 			printk(KERN_ERR
2353617ba13bSMingming Cao 			       "ext4: No journal on filesystem on %s\n",
2354ac27a0ecSDave Kleikamp 			       sb->s_id);
2355ac27a0ecSDave Kleikamp 		goto failed_mount3;
2356ac27a0ecSDave Kleikamp 	}
2357ac27a0ecSDave Kleikamp 
2358eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2359eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2360eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2361eb40a09cSJose R. Santos 		printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2362eb40a09cSJose R. Santos 		goto failed_mount4;
2363eb40a09cSJose R. Santos 	}
2364eb40a09cSJose R. Santos 
2365818d276cSGirish Shilamkar 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2366818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2367818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2368818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2369818d276cSGirish Shilamkar 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2370818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2371818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2372818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2373818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2374818d276cSGirish Shilamkar 	} else {
2375818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal,
2376818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2377818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2378818d276cSGirish Shilamkar 	}
2379818d276cSGirish Shilamkar 
2380ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2381ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2382ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2383ac27a0ecSDave Kleikamp 	case 0:
2384ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
238563f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
238663f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
238763f57933SAndrew Morton 		 */
2388dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2389dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2390ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2391ac27a0ecSDave Kleikamp 		else
2392ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2393ac27a0ecSDave Kleikamp 		break;
2394ac27a0ecSDave Kleikamp 
2395617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2396617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2397dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2398dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2399617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: Journal does not support "
2400ac27a0ecSDave Kleikamp 			       "requested data journaling mode\n");
2401ac27a0ecSDave Kleikamp 			goto failed_mount4;
2402ac27a0ecSDave Kleikamp 		}
2403ac27a0ecSDave Kleikamp 	default:
2404ac27a0ecSDave Kleikamp 		break;
2405ac27a0ecSDave Kleikamp 	}
2406ac27a0ecSDave Kleikamp 
2407ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2408617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2409617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2410ac27a0ecSDave Kleikamp 				"its supported only with writeback mode\n");
2411ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2412ac27a0ecSDave Kleikamp 		}
2413ac27a0ecSDave Kleikamp 	}
2414ac27a0ecSDave Kleikamp 	/*
2415dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2416ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2417ac27a0ecSDave Kleikamp 	 */
2418ac27a0ecSDave Kleikamp 
24191d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
24201d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2421617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: get root inode failed\n");
24221d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2423ac27a0ecSDave Kleikamp 		goto failed_mount4;
2424ac27a0ecSDave Kleikamp 	}
2425ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
24261d1fe1eeSDavid Howells 		iput(root);
2427617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2428ac27a0ecSDave Kleikamp 		goto failed_mount4;
2429ac27a0ecSDave Kleikamp 	}
24301d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
24311d1fe1eeSDavid Howells 	if (!sb->s_root) {
24321d1fe1eeSDavid Howells 		printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
24331d1fe1eeSDavid Howells 		iput(root);
24341d1fe1eeSDavid Howells 		ret = -ENOMEM;
24351d1fe1eeSDavid Howells 		goto failed_mount4;
24361d1fe1eeSDavid Howells 	}
2437ac27a0ecSDave Kleikamp 
2438617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2439ef7f3835SKalpak Shah 
2440ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2441ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2442ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2443ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2444ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2445ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2446ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2447ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2448ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2449ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2450ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2451ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2452ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2453ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2454ef7f3835SKalpak Shah 		}
2455ef7f3835SKalpak Shah 	}
2456ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2457ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2458ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2459ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2460ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2461ef7f3835SKalpak Shah 		printk(KERN_INFO "EXT4-fs: required extra inode space not"
2462ef7f3835SKalpak Shah 			"available.\n");
2463ef7f3835SKalpak Shah 	}
2464ef7f3835SKalpak Shah 
2465ac27a0ecSDave Kleikamp 	/*
2466ac27a0ecSDave Kleikamp 	 * akpm: core read_super() calls in here with the superblock locked.
2467ac27a0ecSDave Kleikamp 	 * That deadlocks, because orphan cleanup needs to lock the superblock
2468ac27a0ecSDave Kleikamp 	 * in numerous places.  Here we just pop the lock - it's relatively
2469ac27a0ecSDave Kleikamp 	 * harmless, because we are now ready to accept write_super() requests,
2470ac27a0ecSDave Kleikamp 	 * and aviro says that's the only reason for hanging onto the
2471ac27a0ecSDave Kleikamp 	 * superblock lock.
2472ac27a0ecSDave Kleikamp 	 */
2473617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2474617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2475617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2476ac27a0ecSDave Kleikamp 	if (needs_recovery)
2477617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2478617ba13bSMingming Cao 	ext4_mark_recovery_complete(sb, es);
2479617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2480617ba13bSMingming Cao 	       test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2481617ba13bSMingming Cao 	       test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2482ac27a0ecSDave Kleikamp 	       "writeback");
2483ac27a0ecSDave Kleikamp 
2484dd919b98SAneesh Kumar K.V 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2485dd919b98SAneesh Kumar K.V 		printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2486dd919b98SAneesh Kumar K.V 				"requested data journaling mode\n");
2487dd919b98SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
2488dd919b98SAneesh Kumar K.V 	} else if (test_opt(sb, DELALLOC))
2489dd919b98SAneesh Kumar K.V 		printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2490dd919b98SAneesh Kumar K.V 
2491a86c6181SAlex Tomas 	ext4_ext_init(sb);
2492c9de560dSAlex Tomas 	ext4_mb_init(sb, needs_recovery);
2493a86c6181SAlex Tomas 
2494ac27a0ecSDave Kleikamp 	lock_kernel();
2495ac27a0ecSDave Kleikamp 	return 0;
2496ac27a0ecSDave Kleikamp 
2497617ba13bSMingming Cao cantfind_ext4:
2498ac27a0ecSDave Kleikamp 	if (!silent)
2499617ba13bSMingming Cao 		printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2500ac27a0ecSDave Kleikamp 		       sb->s_id);
2501ac27a0ecSDave Kleikamp 	goto failed_mount;
2502ac27a0ecSDave Kleikamp 
2503ac27a0ecSDave Kleikamp failed_mount4:
2504dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
250547b4a50bSJan Kara 	sbi->s_journal = NULL;
2506ac27a0ecSDave Kleikamp failed_mount3:
2507ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2508ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2509ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
25106bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2511ac27a0ecSDave Kleikamp failed_mount2:
2512ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2513ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2514ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2515ac27a0ecSDave Kleikamp failed_mount:
25169f6200bbSTheodore Ts'o 	if (sbi->s_proc)
25179f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
2518ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2519ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2520ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2521ac27a0ecSDave Kleikamp #endif
2522617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2523ac27a0ecSDave Kleikamp 	brelse(bh);
2524ac27a0ecSDave Kleikamp out_fail:
2525ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2526ac27a0ecSDave Kleikamp 	kfree(sbi);
2527ac27a0ecSDave Kleikamp 	lock_kernel();
25281d1fe1eeSDavid Howells 	return ret;
2529ac27a0ecSDave Kleikamp }
2530ac27a0ecSDave Kleikamp 
2531ac27a0ecSDave Kleikamp /*
2532ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2533ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2534ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2535ac27a0ecSDave Kleikamp  */
2536617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2537ac27a0ecSDave Kleikamp {
2538617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2539ac27a0ecSDave Kleikamp 
2540ac27a0ecSDave Kleikamp 	if (sbi->s_commit_interval)
2541ac27a0ecSDave Kleikamp 		journal->j_commit_interval = sbi->s_commit_interval;
2542617ba13bSMingming Cao 	/* We could also set up an ext4-specific default for the commit
2543ac27a0ecSDave Kleikamp 	 * interval here, but for now we'll just fall back to the jbd
2544ac27a0ecSDave Kleikamp 	 * default. */
2545ac27a0ecSDave Kleikamp 
2546ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2547ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2548dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2549ac27a0ecSDave Kleikamp 	else
2550dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
2551ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2552ac27a0ecSDave Kleikamp }
2553ac27a0ecSDave Kleikamp 
2554617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2555ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2556ac27a0ecSDave Kleikamp {
2557ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2558ac27a0ecSDave Kleikamp 	journal_t *journal;
2559ac27a0ecSDave Kleikamp 
2560ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2561ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2562ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2563ac27a0ecSDave Kleikamp 
25641d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
25651d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
2566617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: no journal found.\n");
2567ac27a0ecSDave Kleikamp 		return NULL;
2568ac27a0ecSDave Kleikamp 	}
2569ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2570ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2571ac27a0ecSDave Kleikamp 		iput(journal_inode);
2572617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2573ac27a0ecSDave Kleikamp 		return NULL;
2574ac27a0ecSDave Kleikamp 	}
2575ac27a0ecSDave Kleikamp 
2576e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2577ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
25781d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
2579617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2580ac27a0ecSDave Kleikamp 		iput(journal_inode);
2581ac27a0ecSDave Kleikamp 		return NULL;
2582ac27a0ecSDave Kleikamp 	}
2583ac27a0ecSDave Kleikamp 
2584dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
2585ac27a0ecSDave Kleikamp 	if (!journal) {
2586617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2587ac27a0ecSDave Kleikamp 		iput(journal_inode);
2588ac27a0ecSDave Kleikamp 		return NULL;
2589ac27a0ecSDave Kleikamp 	}
2590ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2591617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2592ac27a0ecSDave Kleikamp 	return journal;
2593ac27a0ecSDave Kleikamp }
2594ac27a0ecSDave Kleikamp 
2595617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
2596ac27a0ecSDave Kleikamp 				       dev_t j_dev)
2597ac27a0ecSDave Kleikamp {
2598ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2599ac27a0ecSDave Kleikamp 	journal_t *journal;
2600617ba13bSMingming Cao 	ext4_fsblk_t start;
2601617ba13bSMingming Cao 	ext4_fsblk_t len;
2602ac27a0ecSDave Kleikamp 	int hblock, blocksize;
2603617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
2604ac27a0ecSDave Kleikamp 	unsigned long offset;
2605617ba13bSMingming Cao 	struct ext4_super_block *es;
2606ac27a0ecSDave Kleikamp 	struct block_device *bdev;
2607ac27a0ecSDave Kleikamp 
2608617ba13bSMingming Cao 	bdev = ext4_blkdev_get(j_dev);
2609ac27a0ecSDave Kleikamp 	if (bdev == NULL)
2610ac27a0ecSDave Kleikamp 		return NULL;
2611ac27a0ecSDave Kleikamp 
2612ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
2613ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2614617ba13bSMingming Cao 			"EXT4: failed to claim external journal device.\n");
2615ac27a0ecSDave Kleikamp 		blkdev_put(bdev);
2616ac27a0ecSDave Kleikamp 		return NULL;
2617ac27a0ecSDave Kleikamp 	}
2618ac27a0ecSDave Kleikamp 
2619ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
2620ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(bdev);
2621ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
2622ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2623617ba13bSMingming Cao 			"EXT4-fs: blocksize too small for journal device.\n");
2624ac27a0ecSDave Kleikamp 		goto out_bdev;
2625ac27a0ecSDave Kleikamp 	}
2626ac27a0ecSDave Kleikamp 
2627617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2628617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2629ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
2630ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
2631617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2632ac27a0ecSDave Kleikamp 		       "external journal\n");
2633ac27a0ecSDave Kleikamp 		goto out_bdev;
2634ac27a0ecSDave Kleikamp 	}
2635ac27a0ecSDave Kleikamp 
2636617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2637617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2638ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
2639617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2640617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: external journal has "
2641ac27a0ecSDave Kleikamp 					"bad superblock\n");
2642ac27a0ecSDave Kleikamp 		brelse(bh);
2643ac27a0ecSDave Kleikamp 		goto out_bdev;
2644ac27a0ecSDave Kleikamp 	}
2645ac27a0ecSDave Kleikamp 
2646617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2647617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2648ac27a0ecSDave Kleikamp 		brelse(bh);
2649ac27a0ecSDave Kleikamp 		goto out_bdev;
2650ac27a0ecSDave Kleikamp 	}
2651ac27a0ecSDave Kleikamp 
2652bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
2653ac27a0ecSDave Kleikamp 	start = sb_block + 1;
2654ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
2655ac27a0ecSDave Kleikamp 
2656dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2657ac27a0ecSDave Kleikamp 					start, len, blocksize);
2658ac27a0ecSDave Kleikamp 	if (!journal) {
2659617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2660ac27a0ecSDave Kleikamp 		goto out_bdev;
2661ac27a0ecSDave Kleikamp 	}
2662ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2663ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
2664ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
2665ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
2666617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2667ac27a0ecSDave Kleikamp 		goto out_journal;
2668ac27a0ecSDave Kleikamp 	}
2669ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2670617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: External journal has more than one "
2671ac27a0ecSDave Kleikamp 					"user (unsupported) - %d\n",
2672ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
2673ac27a0ecSDave Kleikamp 		goto out_journal;
2674ac27a0ecSDave Kleikamp 	}
2675617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
2676617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2677ac27a0ecSDave Kleikamp 	return journal;
2678ac27a0ecSDave Kleikamp out_journal:
2679dab291afSMingming Cao 	jbd2_journal_destroy(journal);
2680ac27a0ecSDave Kleikamp out_bdev:
2681617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
2682ac27a0ecSDave Kleikamp 	return NULL;
2683ac27a0ecSDave Kleikamp }
2684ac27a0ecSDave Kleikamp 
2685617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
2686617ba13bSMingming Cao 			     struct ext4_super_block *es,
2687ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
2688ac27a0ecSDave Kleikamp {
2689ac27a0ecSDave Kleikamp 	journal_t *journal;
2690ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2691ac27a0ecSDave Kleikamp 	dev_t journal_dev;
2692ac27a0ecSDave Kleikamp 	int err = 0;
2693ac27a0ecSDave Kleikamp 	int really_read_only;
2694ac27a0ecSDave Kleikamp 
2695ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2696ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2697617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2698ac27a0ecSDave Kleikamp 			"numbers have changed\n");
2699ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
2700ac27a0ecSDave Kleikamp 	} else
2701ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2702ac27a0ecSDave Kleikamp 
2703ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
2704ac27a0ecSDave Kleikamp 
2705ac27a0ecSDave Kleikamp 	/*
2706ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
2707ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
2708ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
2709ac27a0ecSDave Kleikamp 	 */
2710ac27a0ecSDave Kleikamp 
2711617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2712ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
2713617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: INFO: recovery "
2714ac27a0ecSDave Kleikamp 					"required on readonly filesystem.\n");
2715ac27a0ecSDave Kleikamp 			if (really_read_only) {
2716617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: write access "
2717ac27a0ecSDave Kleikamp 					"unavailable, cannot proceed.\n");
2718ac27a0ecSDave Kleikamp 				return -EROFS;
2719ac27a0ecSDave Kleikamp 			}
2720617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: write access will "
2721ac27a0ecSDave Kleikamp 			       "be enabled during recovery.\n");
2722ac27a0ecSDave Kleikamp 		}
2723ac27a0ecSDave Kleikamp 	}
2724ac27a0ecSDave Kleikamp 
2725ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
2726617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2727ac27a0ecSDave Kleikamp 		       "and inode journals!\n");
2728ac27a0ecSDave Kleikamp 		return -EINVAL;
2729ac27a0ecSDave Kleikamp 	}
2730ac27a0ecSDave Kleikamp 
2731ac27a0ecSDave Kleikamp 	if (journal_inum) {
2732617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
2733ac27a0ecSDave Kleikamp 			return -EINVAL;
2734ac27a0ecSDave Kleikamp 	} else {
2735617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2736ac27a0ecSDave Kleikamp 			return -EINVAL;
2737ac27a0ecSDave Kleikamp 	}
2738ac27a0ecSDave Kleikamp 
27394776004fSTheodore Ts'o 	if (journal->j_flags & JBD2_BARRIER)
27404776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers enabled\n");
27414776004fSTheodore Ts'o 	else
27424776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: barriers disabled\n");
27434776004fSTheodore Ts'o 
2744ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2745dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
2746ac27a0ecSDave Kleikamp 		if (err)  {
2747617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2748dab291afSMingming Cao 			jbd2_journal_destroy(journal);
2749ac27a0ecSDave Kleikamp 			return err;
2750ac27a0ecSDave Kleikamp 		}
2751ac27a0ecSDave Kleikamp 	}
2752ac27a0ecSDave Kleikamp 
2753617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2754dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
2755ac27a0ecSDave Kleikamp 	if (!err)
2756dab291afSMingming Cao 		err = jbd2_journal_load(journal);
2757ac27a0ecSDave Kleikamp 
2758ac27a0ecSDave Kleikamp 	if (err) {
2759617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2760dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2761ac27a0ecSDave Kleikamp 		return err;
2762ac27a0ecSDave Kleikamp 	}
2763ac27a0ecSDave Kleikamp 
2764617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2765617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
2766ac27a0ecSDave Kleikamp 
2767ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2768ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2769ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
2770ac27a0ecSDave Kleikamp 		sb->s_dirt = 1;
2771ac27a0ecSDave Kleikamp 
2772ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
2773617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2774ac27a0ecSDave Kleikamp 	}
2775ac27a0ecSDave Kleikamp 
2776ac27a0ecSDave Kleikamp 	return 0;
2777ac27a0ecSDave Kleikamp }
2778ac27a0ecSDave Kleikamp 
2779617ba13bSMingming Cao static int ext4_create_journal(struct super_block *sb,
2780617ba13bSMingming Cao 			       struct ext4_super_block *es,
2781ac27a0ecSDave Kleikamp 			       unsigned int journal_inum)
2782ac27a0ecSDave Kleikamp {
2783ac27a0ecSDave Kleikamp 	journal_t *journal;
27846c675bd4SBorislav Petkov 	int err;
2785ac27a0ecSDave Kleikamp 
2786ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY) {
2787617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2788ac27a0ecSDave Kleikamp 				"create journal.\n");
2789ac27a0ecSDave Kleikamp 		return -EROFS;
2790ac27a0ecSDave Kleikamp 	}
2791ac27a0ecSDave Kleikamp 
27926c675bd4SBorislav Petkov 	journal = ext4_get_journal(sb, journal_inum);
27936c675bd4SBorislav Petkov 	if (!journal)
2794ac27a0ecSDave Kleikamp 		return -EINVAL;
2795ac27a0ecSDave Kleikamp 
2796617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2797ac27a0ecSDave Kleikamp 	       journal_inum);
2798ac27a0ecSDave Kleikamp 
27996c675bd4SBorislav Petkov 	err = jbd2_journal_create(journal);
28006c675bd4SBorislav Petkov 	if (err) {
2801617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2802dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2803ac27a0ecSDave Kleikamp 		return -EIO;
2804ac27a0ecSDave Kleikamp 	}
2805ac27a0ecSDave Kleikamp 
2806617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2807ac27a0ecSDave Kleikamp 
2808617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
2809617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2810617ba13bSMingming Cao 	EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2811ac27a0ecSDave Kleikamp 
2812ac27a0ecSDave Kleikamp 	es->s_journal_inum = cpu_to_le32(journal_inum);
2813ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
2814ac27a0ecSDave Kleikamp 
2815ac27a0ecSDave Kleikamp 	/* Make sure we flush the recovery flag to disk. */
2816617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
2817ac27a0ecSDave Kleikamp 
2818ac27a0ecSDave Kleikamp 	return 0;
2819ac27a0ecSDave Kleikamp }
2820ac27a0ecSDave Kleikamp 
2821617ba13bSMingming Cao static void ext4_commit_super(struct super_block *sb,
28222b2d6d01STheodore Ts'o 			      struct ext4_super_block *es, int sync)
2823ac27a0ecSDave Kleikamp {
2824617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2825ac27a0ecSDave Kleikamp 
2826ac27a0ecSDave Kleikamp 	if (!sbh)
2827ac27a0ecSDave Kleikamp 		return;
2828914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
2829914258bfSTheodore Ts'o 		/*
2830914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
2831914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
2832914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
2833914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
2834914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
2835914258bfSTheodore Ts'o 		 * write and hope for the best.
2836914258bfSTheodore Ts'o 		 */
2837914258bfSTheodore Ts'o 		printk(KERN_ERR "ext4: previous I/O error to "
2838914258bfSTheodore Ts'o 		       "superblock detected for %s.\n", sb->s_id);
2839914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
2840914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
2841914258bfSTheodore Ts'o 	}
2842ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
2843bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2844617ba13bSMingming Cao 	es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2845ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
2846ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
2847914258bfSTheodore Ts'o 	if (sync) {
2848ac27a0ecSDave Kleikamp 		sync_dirty_buffer(sbh);
2849914258bfSTheodore Ts'o 		if (buffer_write_io_error(sbh)) {
2850914258bfSTheodore Ts'o 			printk(KERN_ERR "ext4: I/O error while writing "
2851914258bfSTheodore Ts'o 			       "superblock for %s.\n", sb->s_id);
2852914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
2853914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
2854914258bfSTheodore Ts'o 		}
2855914258bfSTheodore Ts'o 	}
2856ac27a0ecSDave Kleikamp }
2857ac27a0ecSDave Kleikamp 
2858ac27a0ecSDave Kleikamp 
2859ac27a0ecSDave Kleikamp /*
2860ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
2861ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
2862ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
2863ac27a0ecSDave Kleikamp  */
2864617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
2865617ba13bSMingming Cao 					struct ext4_super_block *es)
2866ac27a0ecSDave Kleikamp {
2867617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
2868ac27a0ecSDave Kleikamp 
2869dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
2870dab291afSMingming Cao 	jbd2_journal_flush(journal);
287132c37730SJan Kara 	lock_super(sb);
2872617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2873ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
2874617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2875ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
2876617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2877ac27a0ecSDave Kleikamp 	}
287832c37730SJan Kara 	unlock_super(sb);
2879dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
2880ac27a0ecSDave Kleikamp }
2881ac27a0ecSDave Kleikamp 
2882ac27a0ecSDave Kleikamp /*
2883ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
2884ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
2885ac27a0ecSDave Kleikamp  * main filesystem now.
2886ac27a0ecSDave Kleikamp  */
2887617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
2888617ba13bSMingming Cao 				   struct ext4_super_block *es)
2889ac27a0ecSDave Kleikamp {
2890ac27a0ecSDave Kleikamp 	journal_t *journal;
2891ac27a0ecSDave Kleikamp 	int j_errno;
2892ac27a0ecSDave Kleikamp 	const char *errstr;
2893ac27a0ecSDave Kleikamp 
2894617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2895ac27a0ecSDave Kleikamp 
2896ac27a0ecSDave Kleikamp 	/*
2897ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
2898617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
2899ac27a0ecSDave Kleikamp 	 */
2900ac27a0ecSDave Kleikamp 
2901dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
2902ac27a0ecSDave Kleikamp 	if (j_errno) {
2903ac27a0ecSDave Kleikamp 		char nbuf[16];
2904ac27a0ecSDave Kleikamp 
2905617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
290646e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Filesystem error recorded "
2907ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
290846e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Marking fs in need of "
2909ac27a0ecSDave Kleikamp 			     "filesystem check.");
2910ac27a0ecSDave Kleikamp 
2911617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2912617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2913617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2914ac27a0ecSDave Kleikamp 
2915dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
2916ac27a0ecSDave Kleikamp 	}
2917ac27a0ecSDave Kleikamp }
2918ac27a0ecSDave Kleikamp 
2919ac27a0ecSDave Kleikamp /*
2920ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
2921ac27a0ecSDave Kleikamp  * and wait on the commit.
2922ac27a0ecSDave Kleikamp  */
2923617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
2924ac27a0ecSDave Kleikamp {
2925ac27a0ecSDave Kleikamp 	journal_t *journal;
2926ac27a0ecSDave Kleikamp 	int ret;
2927ac27a0ecSDave Kleikamp 
2928ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
2929ac27a0ecSDave Kleikamp 		return 0;
2930ac27a0ecSDave Kleikamp 
2931617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2932ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2933617ba13bSMingming Cao 	ret = ext4_journal_force_commit(journal);
2934ac27a0ecSDave Kleikamp 	return ret;
2935ac27a0ecSDave Kleikamp }
2936ac27a0ecSDave Kleikamp 
2937ac27a0ecSDave Kleikamp /*
2938617ba13bSMingming Cao  * Ext4 always journals updates to the superblock itself, so we don't
2939ac27a0ecSDave Kleikamp  * have to propagate any other updates to the superblock on disk at this
2940ac27a0ecSDave Kleikamp  * point.  Just start an async writeback to get the buffers on their way
2941ac27a0ecSDave Kleikamp  * to the disk.
2942ac27a0ecSDave Kleikamp  *
2943ac27a0ecSDave Kleikamp  * This implicitly triggers the writebehind on sync().
2944ac27a0ecSDave Kleikamp  */
2945ac27a0ecSDave Kleikamp 
2946617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
2947ac27a0ecSDave Kleikamp {
2948ac27a0ecSDave Kleikamp 	if (mutex_trylock(&sb->s_lock) != 0)
2949ac27a0ecSDave Kleikamp 		BUG();
2950ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2951ac27a0ecSDave Kleikamp }
2952ac27a0ecSDave Kleikamp 
2953617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
2954ac27a0ecSDave Kleikamp {
2955ac27a0ecSDave Kleikamp 	tid_t target;
2956ac27a0ecSDave Kleikamp 
2957ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2958dab291afSMingming Cao 	if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
2959ac27a0ecSDave Kleikamp 		if (wait)
2960dab291afSMingming Cao 			jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
2961ac27a0ecSDave Kleikamp 	}
2962ac27a0ecSDave Kleikamp 	return 0;
2963ac27a0ecSDave Kleikamp }
2964ac27a0ecSDave Kleikamp 
2965ac27a0ecSDave Kleikamp /*
2966ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
2967ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
2968ac27a0ecSDave Kleikamp  */
2969617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb)
2970ac27a0ecSDave Kleikamp {
2971ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2972ac27a0ecSDave Kleikamp 
2973ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2974617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
2975ac27a0ecSDave Kleikamp 
2976ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
2977dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
2978dab291afSMingming Cao 		jbd2_journal_flush(journal);
2979ac27a0ecSDave Kleikamp 
2980ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
2981617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2982617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2983ac27a0ecSDave Kleikamp 	}
2984ac27a0ecSDave Kleikamp }
2985ac27a0ecSDave Kleikamp 
2986ac27a0ecSDave Kleikamp /*
2987ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
2988ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
2989ac27a0ecSDave Kleikamp  */
2990617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb)
2991ac27a0ecSDave Kleikamp {
2992ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2993ac27a0ecSDave Kleikamp 		lock_super(sb);
2994ac27a0ecSDave Kleikamp 		/* Reser the needs_recovery flag before the fs is unlocked. */
2995617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2996617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2997ac27a0ecSDave Kleikamp 		unlock_super(sb);
2998dab291afSMingming Cao 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2999ac27a0ecSDave Kleikamp 	}
3000ac27a0ecSDave Kleikamp }
3001ac27a0ecSDave Kleikamp 
3002617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
3003ac27a0ecSDave Kleikamp {
3004617ba13bSMingming Cao 	struct ext4_super_block *es;
3005617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3006617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
3007ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
3008617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
30098a266467STheodore Ts'o 	ext4_group_t g;
3010ac27a0ecSDave Kleikamp 	int err;
3011ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3012ac27a0ecSDave Kleikamp 	int i;
3013ac27a0ecSDave Kleikamp #endif
3014ac27a0ecSDave Kleikamp 
3015ac27a0ecSDave Kleikamp 	/* Store the original options */
3016ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
3017ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
3018ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
3019ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
3020ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
3021ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3022ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3023ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3024ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3025ac27a0ecSDave Kleikamp #endif
3026ac27a0ecSDave Kleikamp 
3027ac27a0ecSDave Kleikamp 	/*
3028ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
3029ac27a0ecSDave Kleikamp 	 */
3030ac27a0ecSDave Kleikamp 	if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3031ac27a0ecSDave Kleikamp 		err = -EINVAL;
3032ac27a0ecSDave Kleikamp 		goto restore_opts;
3033ac27a0ecSDave Kleikamp 	}
3034ac27a0ecSDave Kleikamp 
3035617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
303646e665e9SHarvey Harrison 		ext4_abort(sb, __func__, "Abort forced by user");
3037ac27a0ecSDave Kleikamp 
3038ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3039617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3040ac27a0ecSDave Kleikamp 
3041ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3042ac27a0ecSDave Kleikamp 
3043617ba13bSMingming Cao 	ext4_init_journal_params(sb, sbi->s_journal);
3044ac27a0ecSDave Kleikamp 
3045ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3046bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
3047617ba13bSMingming Cao 		if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3048ac27a0ecSDave Kleikamp 			err = -EROFS;
3049ac27a0ecSDave Kleikamp 			goto restore_opts;
3050ac27a0ecSDave Kleikamp 		}
3051ac27a0ecSDave Kleikamp 
3052ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
3053ac27a0ecSDave Kleikamp 			/*
3054ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3055ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3056ac27a0ecSDave Kleikamp 			 */
3057ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3058ac27a0ecSDave Kleikamp 
3059ac27a0ecSDave Kleikamp 			/*
3060ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3061ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3062ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3063ac27a0ecSDave Kleikamp 			 */
3064617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3065617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3066ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3067ac27a0ecSDave Kleikamp 
306832c37730SJan Kara 			/*
306932c37730SJan Kara 			 * We have to unlock super so that we can wait for
307032c37730SJan Kara 			 * transactions.
307132c37730SJan Kara 			 */
307232c37730SJan Kara 			unlock_super(sb);
3073617ba13bSMingming Cao 			ext4_mark_recovery_complete(sb, es);
307432c37730SJan Kara 			lock_super(sb);
3075ac27a0ecSDave Kleikamp 		} else {
3076ac27a0ecSDave Kleikamp 			__le32 ret;
3077617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3078617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3079617ba13bSMingming Cao 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3080ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
3081ac27a0ecSDave Kleikamp 				       "optional features (%x).\n",
3082ac27a0ecSDave Kleikamp 				       sb->s_id, le32_to_cpu(ret));
3083ac27a0ecSDave Kleikamp 				err = -EROFS;
3084ac27a0ecSDave Kleikamp 				goto restore_opts;
3085ac27a0ecSDave Kleikamp 			}
3086ead6596bSEric Sandeen 
3087ead6596bSEric Sandeen 			/*
30888a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
30898a266467STheodore Ts'o 			 * are sane.  If they aren't, refuse to
30908a266467STheodore Ts'o 			 * remount r/w.
30918a266467STheodore Ts'o 			 */
30928a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
30938a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
30948a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
30958a266467STheodore Ts'o 
30968a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
30978a266467STheodore Ts'o 					printk(KERN_ERR
30988a266467STheodore Ts'o 	       "EXT4-fs: ext4_remount: "
30998a266467STheodore Ts'o 		"Checksum for group %lu failed (%u!=%u)\n",
31008a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
31018a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
31028a266467STheodore Ts'o 					err = -EINVAL;
31038a266467STheodore Ts'o 					goto restore_opts;
31048a266467STheodore Ts'o 				}
31058a266467STheodore Ts'o 			}
31068a266467STheodore Ts'o 
31078a266467STheodore Ts'o 			/*
3108ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3109ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3110ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3111ead6596bSEric Sandeen 			 */
3112ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3113ead6596bSEric Sandeen 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3114ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3115ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3116ead6596bSEric Sandeen 				       "umount/remount instead.\n",
3117ead6596bSEric Sandeen 				       sb->s_id);
3118ead6596bSEric Sandeen 				err = -EINVAL;
3119ead6596bSEric Sandeen 				goto restore_opts;
3120ead6596bSEric Sandeen 			}
3121ead6596bSEric Sandeen 
3122ac27a0ecSDave Kleikamp 			/*
3123ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3124ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3125ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3126ac27a0ecSDave Kleikamp 			 * the partition.)
3127ac27a0ecSDave Kleikamp 			 */
3128617ba13bSMingming Cao 			ext4_clear_journal_err(sb, es);
3129ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3130617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3131ac27a0ecSDave Kleikamp 				goto restore_opts;
3132617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3133ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3134ac27a0ecSDave Kleikamp 		}
3135ac27a0ecSDave Kleikamp 	}
3136ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3137ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3138ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3139ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3140ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3141ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3142ac27a0ecSDave Kleikamp #endif
3143ac27a0ecSDave Kleikamp 	return 0;
3144ac27a0ecSDave Kleikamp restore_opts:
3145ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3146ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3147ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3148ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3149ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
3150ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3151ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3152ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3153ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3154ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3155ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3156ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3157ac27a0ecSDave Kleikamp 	}
3158ac27a0ecSDave Kleikamp #endif
3159ac27a0ecSDave Kleikamp 	return err;
3160ac27a0ecSDave Kleikamp }
3161ac27a0ecSDave Kleikamp 
3162617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3163ac27a0ecSDave Kleikamp {
3164ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3165617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3166617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3167960cc398SPekka Enberg 	u64 fsid;
3168ac27a0ecSDave Kleikamp 
31695e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
31705e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
31716bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3172fd2d4291SAvantika Mathur 		ext4_group_t ngroups = sbi->s_groups_count, i;
31735e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3174ac27a0ecSDave Kleikamp 		smp_rmb();
3175ac27a0ecSDave Kleikamp 
3176ac27a0ecSDave Kleikamp 		/*
31775e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
31785e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
31795e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3180ac27a0ecSDave Kleikamp 		 */
3181ac27a0ecSDave Kleikamp 
3182ac27a0ecSDave Kleikamp 		/*
3183ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3184ac27a0ecSDave Kleikamp 		 * overhead
3185ac27a0ecSDave Kleikamp 		 */
3186ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3187ac27a0ecSDave Kleikamp 
3188ac27a0ecSDave Kleikamp 		/*
3189ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3190ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3191ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3192ac27a0ecSDave Kleikamp 		 */
3193ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3194617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3195617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3196ac27a0ecSDave Kleikamp 			cond_resched();
3197ac27a0ecSDave Kleikamp 		}
3198ac27a0ecSDave Kleikamp 
3199ac27a0ecSDave Kleikamp 		/*
3200ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3201ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3202ac27a0ecSDave Kleikamp 		 */
32035e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
32045e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
32055e70030dSBadari Pulavarty 		smp_wmb();
32066bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3207ac27a0ecSDave Kleikamp 	}
3208ac27a0ecSDave Kleikamp 
3209617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3210ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
32115e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
32126bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
32136bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3214308ba3ecSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, buf->f_bfree);
3215bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3216bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3217ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3218ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
321952d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
32205e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3221617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3222960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3223960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3224960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3225960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3226ac27a0ecSDave Kleikamp 	return 0;
3227ac27a0ecSDave Kleikamp }
3228ac27a0ecSDave Kleikamp 
3229ac27a0ecSDave Kleikamp /* Helper function for writing quotas on sync - we need to start transaction before quota file
3230ac27a0ecSDave Kleikamp  * is locked for write. Otherwise the are possible deadlocks:
3231ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3232617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3233dab291afSMingming Cao  *   jbd2_journal_start()                   write_dquot()
3234ac27a0ecSDave Kleikamp  *   DQUOT_INIT()                        down(dqio_mutex)
3235dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3236ac27a0ecSDave Kleikamp  *
3237ac27a0ecSDave Kleikamp  */
3238ac27a0ecSDave Kleikamp 
3239ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3240ac27a0ecSDave Kleikamp 
3241ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3242ac27a0ecSDave Kleikamp {
3243ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3244ac27a0ecSDave Kleikamp }
3245ac27a0ecSDave Kleikamp 
3246617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type)
3247ac27a0ecSDave Kleikamp {
3248ac27a0ecSDave Kleikamp 	handle_t *handle;
3249ac27a0ecSDave Kleikamp 	int ret, err;
3250ac27a0ecSDave Kleikamp 
3251ac27a0ecSDave Kleikamp 	/* We may create quota structure so we need to reserve enough blocks */
3252617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3253ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3254ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3255ac27a0ecSDave Kleikamp 	ret = dquot_initialize(inode, type);
3256617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3257ac27a0ecSDave Kleikamp 	if (!ret)
3258ac27a0ecSDave Kleikamp 		ret = err;
3259ac27a0ecSDave Kleikamp 	return ret;
3260ac27a0ecSDave Kleikamp }
3261ac27a0ecSDave Kleikamp 
3262617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode)
3263ac27a0ecSDave Kleikamp {
3264ac27a0ecSDave Kleikamp 	handle_t *handle;
3265ac27a0ecSDave Kleikamp 	int ret, err;
3266ac27a0ecSDave Kleikamp 
3267ac27a0ecSDave Kleikamp 	/* We may delete quota structure so we need to reserve enough blocks */
3268617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
32692887df13SJan Kara 	if (IS_ERR(handle)) {
32702887df13SJan Kara 		/*
32712887df13SJan Kara 		 * We call dquot_drop() anyway to at least release references
32722887df13SJan Kara 		 * to quota structures so that umount does not hang.
32732887df13SJan Kara 		 */
32742887df13SJan Kara 		dquot_drop(inode);
3275ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
32762887df13SJan Kara 	}
3277ac27a0ecSDave Kleikamp 	ret = dquot_drop(inode);
3278617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3279ac27a0ecSDave Kleikamp 	if (!ret)
3280ac27a0ecSDave Kleikamp 		ret = err;
3281ac27a0ecSDave Kleikamp 	return ret;
3282ac27a0ecSDave Kleikamp }
3283ac27a0ecSDave Kleikamp 
3284617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3285ac27a0ecSDave Kleikamp {
3286ac27a0ecSDave Kleikamp 	int ret, err;
3287ac27a0ecSDave Kleikamp 	handle_t *handle;
3288ac27a0ecSDave Kleikamp 	struct inode *inode;
3289ac27a0ecSDave Kleikamp 
3290ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3291617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3292617ba13bSMingming Cao 					EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3293ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3294ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3295ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3296617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3297ac27a0ecSDave Kleikamp 	if (!ret)
3298ac27a0ecSDave Kleikamp 		ret = err;
3299ac27a0ecSDave Kleikamp 	return ret;
3300ac27a0ecSDave Kleikamp }
3301ac27a0ecSDave Kleikamp 
3302617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3303ac27a0ecSDave Kleikamp {
3304ac27a0ecSDave Kleikamp 	int ret, err;
3305ac27a0ecSDave Kleikamp 	handle_t *handle;
3306ac27a0ecSDave Kleikamp 
3307617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3308617ba13bSMingming Cao 					EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3309ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3310ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3311ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3312617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3313ac27a0ecSDave Kleikamp 	if (!ret)
3314ac27a0ecSDave Kleikamp 		ret = err;
3315ac27a0ecSDave Kleikamp 	return ret;
3316ac27a0ecSDave Kleikamp }
3317ac27a0ecSDave Kleikamp 
3318617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3319ac27a0ecSDave Kleikamp {
3320ac27a0ecSDave Kleikamp 	int ret, err;
3321ac27a0ecSDave Kleikamp 	handle_t *handle;
3322ac27a0ecSDave Kleikamp 
3323617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3324617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
33259c3013e9SJan Kara 	if (IS_ERR(handle)) {
33269c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
33279c3013e9SJan Kara 		dquot_release(dquot);
3328ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
33299c3013e9SJan Kara 	}
3330ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3331617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3332ac27a0ecSDave Kleikamp 	if (!ret)
3333ac27a0ecSDave Kleikamp 		ret = err;
3334ac27a0ecSDave Kleikamp 	return ret;
3335ac27a0ecSDave Kleikamp }
3336ac27a0ecSDave Kleikamp 
3337617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3338ac27a0ecSDave Kleikamp {
33392c8be6b2SJan Kara 	/* Are we journaling quotas? */
3340617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3341617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3342ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3343617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3344ac27a0ecSDave Kleikamp 	} else {
3345ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3346ac27a0ecSDave Kleikamp 	}
3347ac27a0ecSDave Kleikamp }
3348ac27a0ecSDave Kleikamp 
3349617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3350ac27a0ecSDave Kleikamp {
3351ac27a0ecSDave Kleikamp 	int ret, err;
3352ac27a0ecSDave Kleikamp 	handle_t *handle;
3353ac27a0ecSDave Kleikamp 
3354ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3355617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3356ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3357ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3358ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3359617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3360ac27a0ecSDave Kleikamp 	if (!ret)
3361ac27a0ecSDave Kleikamp 		ret = err;
3362ac27a0ecSDave Kleikamp 	return ret;
3363ac27a0ecSDave Kleikamp }
3364ac27a0ecSDave Kleikamp 
3365ac27a0ecSDave Kleikamp /*
3366ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3367ac27a0ecSDave Kleikamp  * the quota file and such...
3368ac27a0ecSDave Kleikamp  */
3369617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3370ac27a0ecSDave Kleikamp {
3371617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3372617ba13bSMingming Cao 			EXT4_SB(sb)->s_jquota_fmt, type);
3373ac27a0ecSDave Kleikamp }
3374ac27a0ecSDave Kleikamp 
3375ac27a0ecSDave Kleikamp /*
3376ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3377ac27a0ecSDave Kleikamp  */
3378617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
33796f28e087SJan Kara 			 char *path, int remount)
3380ac27a0ecSDave Kleikamp {
3381ac27a0ecSDave Kleikamp 	int err;
3382ac27a0ecSDave Kleikamp 	struct nameidata nd;
3383ac27a0ecSDave Kleikamp 
3384ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3385ac27a0ecSDave Kleikamp 		return -EINVAL;
33860623543bSJan Kara 	/* When remounting, no checks are needed and in fact, path is NULL */
33870623543bSJan Kara 	if (remount)
33886f28e087SJan Kara 		return vfs_quota_on(sb, type, format_id, path, remount);
33890623543bSJan Kara 
3390ac27a0ecSDave Kleikamp 	err = path_lookup(path, LOOKUP_FOLLOW, &nd);
3391ac27a0ecSDave Kleikamp 	if (err)
3392ac27a0ecSDave Kleikamp 		return err;
33930623543bSJan Kara 
3394ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
33954ac91378SJan Blunck 	if (nd.path.mnt->mnt_sb != sb) {
33961d957f9bSJan Blunck 		path_put(&nd.path);
3397ac27a0ecSDave Kleikamp 		return -EXDEV;
3398ac27a0ecSDave Kleikamp 	}
33990623543bSJan Kara 	/* Journaling quota? */
34000623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
34012b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
34024ac91378SJan Blunck 		if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
3403ac27a0ecSDave Kleikamp 			printk(KERN_WARNING
3404617ba13bSMingming Cao 				"EXT4-fs: Quota file not on filesystem root. "
34050623543bSJan Kara 				"Journaled quota will not work.\n");
34060623543bSJan Kara 	}
34070623543bSJan Kara 
34080623543bSJan Kara 	/*
34090623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
34100623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
34110623543bSJan Kara 	 */
34120623543bSJan Kara 	if (ext4_should_journal_data(nd.path.dentry->d_inode)) {
34130623543bSJan Kara 		/*
34140623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
34150623543bSJan Kara 		 * otherwise be livelocked...
34160623543bSJan Kara 		 */
34170623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
34180623543bSJan Kara 		jbd2_journal_flush(EXT4_SB(sb)->s_journal);
34190623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
34200623543bSJan Kara 	}
34210623543bSJan Kara 
342277e69dacSAl Viro 	err = vfs_quota_on_path(sb, type, format_id, &nd.path);
34231d957f9bSJan Blunck 	path_put(&nd.path);
342477e69dacSAl Viro 	return err;
3425ac27a0ecSDave Kleikamp }
3426ac27a0ecSDave Kleikamp 
3427ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3428ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3429ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3430ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3431617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3432ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3433ac27a0ecSDave Kleikamp {
3434ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3435725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3436ac27a0ecSDave Kleikamp 	int err = 0;
3437ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3438ac27a0ecSDave Kleikamp 	int tocopy;
3439ac27a0ecSDave Kleikamp 	size_t toread;
3440ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3441ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3442ac27a0ecSDave Kleikamp 
3443ac27a0ecSDave Kleikamp 	if (off > i_size)
3444ac27a0ecSDave Kleikamp 		return 0;
3445ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3446ac27a0ecSDave Kleikamp 		len = i_size-off;
3447ac27a0ecSDave Kleikamp 	toread = len;
3448ac27a0ecSDave Kleikamp 	while (toread > 0) {
3449ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3450ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3451617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3452ac27a0ecSDave Kleikamp 		if (err)
3453ac27a0ecSDave Kleikamp 			return err;
3454ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3455ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3456ac27a0ecSDave Kleikamp 		else
3457ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3458ac27a0ecSDave Kleikamp 		brelse(bh);
3459ac27a0ecSDave Kleikamp 		offset = 0;
3460ac27a0ecSDave Kleikamp 		toread -= tocopy;
3461ac27a0ecSDave Kleikamp 		data += tocopy;
3462ac27a0ecSDave Kleikamp 		blk++;
3463ac27a0ecSDave Kleikamp 	}
3464ac27a0ecSDave Kleikamp 	return len;
3465ac27a0ecSDave Kleikamp }
3466ac27a0ecSDave Kleikamp 
3467ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3468ac27a0ecSDave Kleikamp  * enough credits) */
3469617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3470ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3471ac27a0ecSDave Kleikamp {
3472ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3473725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3474ac27a0ecSDave Kleikamp 	int err = 0;
3475ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3476ac27a0ecSDave Kleikamp 	int tocopy;
3477617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3478ac27a0ecSDave Kleikamp 	size_t towrite = len;
3479ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3480ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3481ac27a0ecSDave Kleikamp 
34829c3013e9SJan Kara 	if (!handle) {
3483e5f8eab8STheodore Ts'o 		printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
34849c3013e9SJan Kara 			" cancelled because transaction is not started.\n",
34859c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
34869c3013e9SJan Kara 		return -EIO;
34879c3013e9SJan Kara 	}
3488ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3489ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3490ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3491ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3492617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3493ac27a0ecSDave Kleikamp 		if (!bh)
3494ac27a0ecSDave Kleikamp 			goto out;
3495ac27a0ecSDave Kleikamp 		if (journal_quota) {
3496617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3497ac27a0ecSDave Kleikamp 			if (err) {
3498ac27a0ecSDave Kleikamp 				brelse(bh);
3499ac27a0ecSDave Kleikamp 				goto out;
3500ac27a0ecSDave Kleikamp 			}
3501ac27a0ecSDave Kleikamp 		}
3502ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3503ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3504ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3505ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3506ac27a0ecSDave Kleikamp 		if (journal_quota)
3507617ba13bSMingming Cao 			err = ext4_journal_dirty_metadata(handle, bh);
3508ac27a0ecSDave Kleikamp 		else {
3509ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3510678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3511ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3512ac27a0ecSDave Kleikamp 		}
3513ac27a0ecSDave Kleikamp 		brelse(bh);
3514ac27a0ecSDave Kleikamp 		if (err)
3515ac27a0ecSDave Kleikamp 			goto out;
3516ac27a0ecSDave Kleikamp 		offset = 0;
3517ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3518ac27a0ecSDave Kleikamp 		data += tocopy;
3519ac27a0ecSDave Kleikamp 		blk++;
3520ac27a0ecSDave Kleikamp 	}
3521ac27a0ecSDave Kleikamp out:
35224d04e4fbSJan Kara 	if (len == towrite) {
35234d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
3524ac27a0ecSDave Kleikamp 		return err;
35254d04e4fbSJan Kara 	}
3526ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3527ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3528617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3529ac27a0ecSDave Kleikamp 	}
3530ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3531617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3532ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3533ac27a0ecSDave Kleikamp 	return len - towrite;
3534ac27a0ecSDave Kleikamp }
3535ac27a0ecSDave Kleikamp 
3536ac27a0ecSDave Kleikamp #endif
3537ac27a0ecSDave Kleikamp 
3538617ba13bSMingming Cao static int ext4_get_sb(struct file_system_type *fs_type,
3539ac27a0ecSDave Kleikamp 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3540ac27a0ecSDave Kleikamp {
3541617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3542ac27a0ecSDave Kleikamp }
3543ac27a0ecSDave Kleikamp 
3544*5e8814f2STheodore Ts'o #ifdef CONFIG_PROC_FS
3545*5e8814f2STheodore Ts'o static int ext4_ui_proc_show(struct seq_file *m, void *v)
3546*5e8814f2STheodore Ts'o {
3547*5e8814f2STheodore Ts'o 	unsigned int *p = m->private;
3548*5e8814f2STheodore Ts'o 
3549*5e8814f2STheodore Ts'o 	seq_printf(m, "%u\n", *p);
3550*5e8814f2STheodore Ts'o 	return 0;
3551*5e8814f2STheodore Ts'o }
3552*5e8814f2STheodore Ts'o 
3553*5e8814f2STheodore Ts'o static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3554*5e8814f2STheodore Ts'o {
3555*5e8814f2STheodore Ts'o 	return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3556*5e8814f2STheodore Ts'o }
3557*5e8814f2STheodore Ts'o 
3558*5e8814f2STheodore Ts'o static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3559*5e8814f2STheodore Ts'o 			       size_t cnt, loff_t *ppos)
3560*5e8814f2STheodore Ts'o {
3561*5e8814f2STheodore Ts'o 	unsigned int *p = PDE(file->f_path.dentry->d_inode)->data;
3562*5e8814f2STheodore Ts'o 	char str[32];
3563*5e8814f2STheodore Ts'o 	unsigned long value;
3564*5e8814f2STheodore Ts'o 
3565*5e8814f2STheodore Ts'o 	if (cnt >= sizeof(str))
3566*5e8814f2STheodore Ts'o 		return -EINVAL;
3567*5e8814f2STheodore Ts'o 	if (copy_from_user(str, buf, cnt))
3568*5e8814f2STheodore Ts'o 		return -EFAULT;
3569*5e8814f2STheodore Ts'o 	value = simple_strtol(str, NULL, 0);
3570*5e8814f2STheodore Ts'o 	if (value < 0)
3571*5e8814f2STheodore Ts'o 		return -ERANGE;
3572*5e8814f2STheodore Ts'o 	*p = value;
3573*5e8814f2STheodore Ts'o 	return cnt;
3574*5e8814f2STheodore Ts'o }
3575*5e8814f2STheodore Ts'o 
3576*5e8814f2STheodore Ts'o const struct file_operations ext4_ui_proc_fops = {
3577*5e8814f2STheodore Ts'o 	.owner		= THIS_MODULE,
3578*5e8814f2STheodore Ts'o 	.open		= ext4_ui_proc_open,
3579*5e8814f2STheodore Ts'o 	.read		= seq_read,
3580*5e8814f2STheodore Ts'o 	.llseek		= seq_lseek,
3581*5e8814f2STheodore Ts'o 	.release	= single_release,
3582*5e8814f2STheodore Ts'o 	.write		= ext4_ui_proc_write,
3583*5e8814f2STheodore Ts'o };
3584*5e8814f2STheodore Ts'o #endif
3585*5e8814f2STheodore Ts'o 
3586617ba13bSMingming Cao static struct file_system_type ext4dev_fs_type = {
3587ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
3588617ba13bSMingming Cao 	.name		= "ext4dev",
3589617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
3590ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
3591ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
3592ac27a0ecSDave Kleikamp };
3593ac27a0ecSDave Kleikamp 
3594617ba13bSMingming Cao static int __init init_ext4_fs(void)
3595ac27a0ecSDave Kleikamp {
3596c9de560dSAlex Tomas 	int err;
3597c9de560dSAlex Tomas 
35989f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3599c9de560dSAlex Tomas 	err = init_ext4_mballoc();
3600ac27a0ecSDave Kleikamp 	if (err)
3601ac27a0ecSDave Kleikamp 		return err;
3602c9de560dSAlex Tomas 
3603c9de560dSAlex Tomas 	err = init_ext4_xattr();
3604c9de560dSAlex Tomas 	if (err)
3605c9de560dSAlex Tomas 		goto out2;
3606ac27a0ecSDave Kleikamp 	err = init_inodecache();
3607ac27a0ecSDave Kleikamp 	if (err)
3608ac27a0ecSDave Kleikamp 		goto out1;
3609617ba13bSMingming Cao 	err = register_filesystem(&ext4dev_fs_type);
3610ac27a0ecSDave Kleikamp 	if (err)
3611ac27a0ecSDave Kleikamp 		goto out;
3612ac27a0ecSDave Kleikamp 	return 0;
3613ac27a0ecSDave Kleikamp out:
3614ac27a0ecSDave Kleikamp 	destroy_inodecache();
3615ac27a0ecSDave Kleikamp out1:
3616617ba13bSMingming Cao 	exit_ext4_xattr();
3617c9de560dSAlex Tomas out2:
3618c9de560dSAlex Tomas 	exit_ext4_mballoc();
3619ac27a0ecSDave Kleikamp 	return err;
3620ac27a0ecSDave Kleikamp }
3621ac27a0ecSDave Kleikamp 
3622617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
3623ac27a0ecSDave Kleikamp {
3624617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
3625ac27a0ecSDave Kleikamp 	destroy_inodecache();
3626617ba13bSMingming Cao 	exit_ext4_xattr();
3627c9de560dSAlex Tomas 	exit_ext4_mballoc();
36289f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
3629ac27a0ecSDave Kleikamp }
3630ac27a0ecSDave Kleikamp 
3631ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3632617ba13bSMingming Cao MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3633ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
3634617ba13bSMingming Cao module_init(init_ext4_fs)
3635617ba13bSMingming Cao module_exit(exit_ext4_fs)
3636