xref: /linux/fs/ext4/super.c (revision cb45bbe44b09f35bb12d67ffa7ecff862608aeae)
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>
24617ba13bSMingming Cao #include <linux/ext4_fs.h>
25dab291afSMingming Cao #include <linux/ext4_jbd2.h>
26ac27a0ecSDave Kleikamp #include <linux/slab.h>
27ac27a0ecSDave Kleikamp #include <linux/init.h>
28ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
29ac27a0ecSDave Kleikamp #include <linux/parser.h>
30ac27a0ecSDave Kleikamp #include <linux/smp_lock.h>
31ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
32a5694255SChristoph Hellwig #include <linux/exportfs.h>
33ac27a0ecSDave Kleikamp #include <linux/vfs.h>
34ac27a0ecSDave Kleikamp #include <linux/random.h>
35ac27a0ecSDave Kleikamp #include <linux/mount.h>
36ac27a0ecSDave Kleikamp #include <linux/namei.h>
37ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
38ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
41ac27a0ecSDave Kleikamp 
42ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
43ac27a0ecSDave Kleikamp 
44ac27a0ecSDave Kleikamp #include "xattr.h"
45ac27a0ecSDave Kleikamp #include "acl.h"
46ac27a0ecSDave Kleikamp #include "namei.h"
47717d50e4SAndreas Dilger #include "group.h"
48ac27a0ecSDave Kleikamp 
49617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
50ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
51617ba13bSMingming Cao static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
52ac27a0ecSDave Kleikamp 			       unsigned int);
53617ba13bSMingming Cao static void ext4_commit_super (struct super_block * sb,
54617ba13bSMingming Cao 			       struct ext4_super_block * es,
55ac27a0ecSDave Kleikamp 			       int sync);
56617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block * sb,
57617ba13bSMingming Cao 					struct ext4_super_block * es);
58617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block * sb,
59617ba13bSMingming Cao 				   struct ext4_super_block * es);
60617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
61617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block * sb, int errno,
62ac27a0ecSDave Kleikamp 				     char nbuf[16]);
63617ba13bSMingming Cao static int ext4_remount (struct super_block * sb, int * flags, char * data);
64617ba13bSMingming Cao static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
65617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb);
66617ba13bSMingming Cao static void ext4_write_super (struct super_block * sb);
67617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb);
68ac27a0ecSDave Kleikamp 
69bd81d8eeSLaurent Vivier 
708fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
718fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
72bd81d8eeSLaurent Vivier {
733a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
748fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
758fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
76bd81d8eeSLaurent Vivier }
77bd81d8eeSLaurent Vivier 
788fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
798fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
80bd81d8eeSLaurent Vivier {
815272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
828fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
838fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
84bd81d8eeSLaurent Vivier }
85bd81d8eeSLaurent Vivier 
868fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
878fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
88bd81d8eeSLaurent Vivier {
895272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
908fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
918fadc143SAlexandre Ratchov 		(ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
92bd81d8eeSLaurent Vivier }
93bd81d8eeSLaurent Vivier 
948fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
958fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
96bd81d8eeSLaurent Vivier {
973a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
988fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
998fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
100bd81d8eeSLaurent Vivier }
101bd81d8eeSLaurent Vivier 
1028fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1038fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
104bd81d8eeSLaurent Vivier {
1055272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1068fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1078fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
108bd81d8eeSLaurent Vivier }
109bd81d8eeSLaurent Vivier 
1108fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1118fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
112bd81d8eeSLaurent Vivier {
1135272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1148fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1158fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
116bd81d8eeSLaurent Vivier }
117bd81d8eeSLaurent Vivier 
118ac27a0ecSDave Kleikamp /*
119dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
120ac27a0ecSDave Kleikamp  *
121ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
122ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
123ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
124ac27a0ecSDave Kleikamp  * appropriate.
125ac27a0ecSDave Kleikamp  */
126617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
127ac27a0ecSDave Kleikamp {
128ac27a0ecSDave Kleikamp 	journal_t *journal;
129ac27a0ecSDave Kleikamp 
130ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
131ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
132ac27a0ecSDave Kleikamp 
133ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
134ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
135ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
136617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
137ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
138617ba13bSMingming Cao 		ext4_abort(sb, __FUNCTION__,
139ac27a0ecSDave Kleikamp 			   "Detected aborted journal");
140ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
141ac27a0ecSDave Kleikamp 	}
142ac27a0ecSDave Kleikamp 
143dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
144ac27a0ecSDave Kleikamp }
145ac27a0ecSDave Kleikamp 
146ac27a0ecSDave Kleikamp /*
147ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
148dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
149ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
150ac27a0ecSDave Kleikamp  * appropriate.
151ac27a0ecSDave Kleikamp  */
152617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
153ac27a0ecSDave Kleikamp {
154ac27a0ecSDave Kleikamp 	struct super_block *sb;
155ac27a0ecSDave Kleikamp 	int err;
156ac27a0ecSDave Kleikamp 	int rc;
157ac27a0ecSDave Kleikamp 
158ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
159ac27a0ecSDave Kleikamp 	err = handle->h_err;
160dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
161ac27a0ecSDave Kleikamp 
162ac27a0ecSDave Kleikamp 	if (!err)
163ac27a0ecSDave Kleikamp 		err = rc;
164ac27a0ecSDave Kleikamp 	if (err)
165617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
166ac27a0ecSDave Kleikamp 	return err;
167ac27a0ecSDave Kleikamp }
168ac27a0ecSDave Kleikamp 
169617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
170ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
171ac27a0ecSDave Kleikamp {
172ac27a0ecSDave Kleikamp 	char nbuf[16];
173617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
174ac27a0ecSDave Kleikamp 
175ac27a0ecSDave Kleikamp 	if (bh)
176ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
177ac27a0ecSDave Kleikamp 
178ac27a0ecSDave Kleikamp 	if (!handle->h_err)
179ac27a0ecSDave Kleikamp 		handle->h_err = err;
180ac27a0ecSDave Kleikamp 
181ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
182ac27a0ecSDave Kleikamp 		return;
183ac27a0ecSDave Kleikamp 
184ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
185ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
186ac27a0ecSDave Kleikamp 
187dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
188ac27a0ecSDave Kleikamp }
189ac27a0ecSDave Kleikamp 
190ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
191ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
192ac27a0ecSDave Kleikamp  *
193ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
194617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
195ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
196ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
197ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
198ac27a0ecSDave Kleikamp  * write out the superblock safely.
199ac27a0ecSDave Kleikamp  *
200dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
201ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
202ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
203ac27a0ecSDave Kleikamp  */
204ac27a0ecSDave Kleikamp 
205617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
206ac27a0ecSDave Kleikamp {
207617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
208ac27a0ecSDave Kleikamp 
209617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
210617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
211ac27a0ecSDave Kleikamp 
212ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
213ac27a0ecSDave Kleikamp 		return;
214ac27a0ecSDave Kleikamp 
215ac27a0ecSDave Kleikamp 	if (!test_opt (sb, ERRORS_CONT)) {
216617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
217ac27a0ecSDave Kleikamp 
218617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
219ac27a0ecSDave Kleikamp 		if (journal)
220dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
221ac27a0ecSDave Kleikamp 	}
222ac27a0ecSDave Kleikamp 	if (test_opt (sb, ERRORS_RO)) {
223ac27a0ecSDave Kleikamp 		printk (KERN_CRIT "Remounting filesystem read-only\n");
224ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
225ac27a0ecSDave Kleikamp 	}
226617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
227ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
228617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
229ac27a0ecSDave Kleikamp 			sb->s_id);
230ac27a0ecSDave Kleikamp }
231ac27a0ecSDave Kleikamp 
232617ba13bSMingming Cao void ext4_error (struct super_block * sb, const char * function,
233ac27a0ecSDave Kleikamp 		 const char * fmt, ...)
234ac27a0ecSDave Kleikamp {
235ac27a0ecSDave Kleikamp 	va_list args;
236ac27a0ecSDave Kleikamp 
237ac27a0ecSDave Kleikamp 	va_start(args, fmt);
238617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
239ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
240ac27a0ecSDave Kleikamp 	printk("\n");
241ac27a0ecSDave Kleikamp 	va_end(args);
242ac27a0ecSDave Kleikamp 
243617ba13bSMingming Cao 	ext4_handle_error(sb);
244ac27a0ecSDave Kleikamp }
245ac27a0ecSDave Kleikamp 
246617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block * sb, int errno,
247ac27a0ecSDave Kleikamp 				     char nbuf[16])
248ac27a0ecSDave Kleikamp {
249ac27a0ecSDave Kleikamp 	char *errstr = NULL;
250ac27a0ecSDave Kleikamp 
251ac27a0ecSDave Kleikamp 	switch (errno) {
252ac27a0ecSDave Kleikamp 	case -EIO:
253ac27a0ecSDave Kleikamp 		errstr = "IO failure";
254ac27a0ecSDave Kleikamp 		break;
255ac27a0ecSDave Kleikamp 	case -ENOMEM:
256ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
257ac27a0ecSDave Kleikamp 		break;
258ac27a0ecSDave Kleikamp 	case -EROFS:
259dab291afSMingming Cao 		if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
260ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
261ac27a0ecSDave Kleikamp 		else
262ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
263ac27a0ecSDave Kleikamp 		break;
264ac27a0ecSDave Kleikamp 	default:
265ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
266ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
267ac27a0ecSDave Kleikamp 		 * NULL. */
268ac27a0ecSDave Kleikamp 		if (nbuf) {
269ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
270ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
271ac27a0ecSDave Kleikamp 				errstr = nbuf;
272ac27a0ecSDave Kleikamp 		}
273ac27a0ecSDave Kleikamp 		break;
274ac27a0ecSDave Kleikamp 	}
275ac27a0ecSDave Kleikamp 
276ac27a0ecSDave Kleikamp 	return errstr;
277ac27a0ecSDave Kleikamp }
278ac27a0ecSDave Kleikamp 
279617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
280ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
281ac27a0ecSDave Kleikamp 
282617ba13bSMingming Cao void __ext4_std_error (struct super_block * sb, const char * function,
283ac27a0ecSDave Kleikamp 		       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 
358617ba13bSMingming Cao 	ext4_warning(sb, __FUNCTION__,
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);
510ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
511617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
512ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
513ac27a0ecSDave Kleikamp 		BUFFER_TRACE(sbi->s_sbh, "marking dirty");
514ac27a0ecSDave Kleikamp 		mark_buffer_dirty(sbi->s_sbh);
515617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
516ac27a0ecSDave Kleikamp 	}
517ac27a0ecSDave Kleikamp 
518ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
519ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
520ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
521ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
522ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
523ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
524ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
525ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
526ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
527ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
528ac27a0ecSDave Kleikamp #endif
529ac27a0ecSDave Kleikamp 
530ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
531ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
532ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
533ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
534ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
535ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
536ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
537ac27a0ecSDave Kleikamp 
538f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
539ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
540ac27a0ecSDave Kleikamp 		/*
541ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
542ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
543ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
544ac27a0ecSDave Kleikamp 		 */
545ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
546f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
547617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
548ac27a0ecSDave Kleikamp 	}
549ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
550ac27a0ecSDave Kleikamp 	kfree(sbi);
551ac27a0ecSDave Kleikamp 	return;
552ac27a0ecSDave Kleikamp }
553ac27a0ecSDave Kleikamp 
554e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
555ac27a0ecSDave Kleikamp 
556ac27a0ecSDave Kleikamp /*
557ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
558ac27a0ecSDave Kleikamp  */
559617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
560ac27a0ecSDave Kleikamp {
561617ba13bSMingming Cao 	struct ext4_inode_info *ei;
562ac27a0ecSDave Kleikamp 
563e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
564ac27a0ecSDave Kleikamp 	if (!ei)
565ac27a0ecSDave Kleikamp 		return NULL;
566617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
567617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
568617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
569ac27a0ecSDave Kleikamp #endif
570ac27a0ecSDave Kleikamp 	ei->i_block_alloc_info = NULL;
571ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
572a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
573c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
574c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
575ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
576ac27a0ecSDave Kleikamp }
577ac27a0ecSDave Kleikamp 
578617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
579ac27a0ecSDave Kleikamp {
5809f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
5819f7dd93dSVasily Averin 		printk("EXT4 Inode %p: orphan list check failed!\n",
5829f7dd93dSVasily Averin 			EXT4_I(inode));
5839f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
5849f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
5859f7dd93dSVasily Averin 				true);
5869f7dd93dSVasily Averin 		dump_stack();
5879f7dd93dSVasily Averin 	}
588617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
589ac27a0ecSDave Kleikamp }
590ac27a0ecSDave Kleikamp 
5914ba9b9d0SChristoph Lameter static void init_once(struct kmem_cache *cachep, void *foo)
592ac27a0ecSDave Kleikamp {
593617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
594ac27a0ecSDave Kleikamp 
595ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
596617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
597ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
598ac27a0ecSDave Kleikamp #endif
5990e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
600ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
601ac27a0ecSDave Kleikamp }
602ac27a0ecSDave Kleikamp 
603ac27a0ecSDave Kleikamp static int init_inodecache(void)
604ac27a0ecSDave Kleikamp {
605617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
606617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
607ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
608ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
60920c2df83SPaul Mundt 					     init_once);
610617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
611ac27a0ecSDave Kleikamp 		return -ENOMEM;
612ac27a0ecSDave Kleikamp 	return 0;
613ac27a0ecSDave Kleikamp }
614ac27a0ecSDave Kleikamp 
615ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
616ac27a0ecSDave Kleikamp {
617617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
618ac27a0ecSDave Kleikamp }
619ac27a0ecSDave Kleikamp 
620617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
621ac27a0ecSDave Kleikamp {
622617ba13bSMingming Cao 	struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
623617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
624617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
625617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
626617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
627617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
628ac27a0ecSDave Kleikamp 	}
629617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
630617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
631617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
632617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
633ac27a0ecSDave Kleikamp 	}
634ac27a0ecSDave Kleikamp #endif
635617ba13bSMingming Cao 	ext4_discard_reservation(inode);
636617ba13bSMingming Cao 	EXT4_I(inode)->i_block_alloc_info = NULL;
637ac27a0ecSDave Kleikamp 	if (unlikely(rsv))
638ac27a0ecSDave Kleikamp 		kfree(rsv);
639ac27a0ecSDave Kleikamp }
640ac27a0ecSDave Kleikamp 
641617ba13bSMingming Cao static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
642ac27a0ecSDave Kleikamp {
643ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
644617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
645ac27a0ecSDave Kleikamp 
646ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
647ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
648ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
649ac27a0ecSDave Kleikamp 
650ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
651ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
652ac27a0ecSDave Kleikamp 
653ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
654ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
655ac27a0ecSDave Kleikamp 
656617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
657ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
658ac27a0ecSDave Kleikamp 
659617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
660ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
661ac27a0ecSDave Kleikamp #endif
662ac27a0ecSDave Kleikamp }
663ac27a0ecSDave Kleikamp 
664d9c9bef1SMiklos Szeredi /*
665d9c9bef1SMiklos Szeredi  * Show an option if
666d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
667d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
668d9c9bef1SMiklos Szeredi  */
669617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
670ac27a0ecSDave Kleikamp {
671aa22df2cSAneesh Kumar K.V 	int def_errors;
672aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
673ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
674d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
675d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
676d9c9bef1SMiklos Szeredi 
677d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
678aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
679d9c9bef1SMiklos Szeredi 
680d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
681d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
682d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
683d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
684aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
685d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
686d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
687d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
688d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
689d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
690d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
691d9c9bef1SMiklos Szeredi 	}
692d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
693d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
694d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
695d9c9bef1SMiklos Szeredi 	}
696bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
697d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
698bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
699d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
700bb4f397aSAneesh Kumar K.V 		}
701bb4f397aSAneesh Kumar K.V 	}
702aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
703bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
704aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
705d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
706aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
707d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
708aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
709d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
710d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
711d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
71207620f69SAdrian Bunk #ifdef CONFIG_EXT4DEV_FS_XATTR
713aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
714aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
715d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
716d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
717d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
718d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
719d9c9bef1SMiklos Szeredi 	}
720d9c9bef1SMiklos Szeredi #endif
72107620f69SAdrian Bunk #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
722aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
723d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
724d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
725d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
726d9c9bef1SMiklos Szeredi #endif
727d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, RESERVATION))
728d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noreservation");
729d9c9bef1SMiklos Szeredi 	if (sbi->s_commit_interval) {
730d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
731d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
732d9c9bef1SMiklos Szeredi 	}
733d9c9bef1SMiklos Szeredi 	if (test_opt(sb, BARRIER))
734d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",barrier=1");
735d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
736d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
737d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, EXTENTS))
738d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noextents");
7393dbd0edeSAneesh Kumar K.V 	if (!test_opt(sb, MBALLOC))
7403dbd0edeSAneesh Kumar K.V 		seq_puts(seq, ",nomballoc");
74125ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
74225ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
743ac27a0ecSDave Kleikamp 
744*cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
745*cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
746aa22df2cSAneesh Kumar K.V 	/*
747aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
748aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
749aa22df2cSAneesh Kumar K.V 	 */
750617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
751ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
752617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
753ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
754617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
755ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
756ac27a0ecSDave Kleikamp 
757617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
758ac27a0ecSDave Kleikamp 	return 0;
759ac27a0ecSDave Kleikamp }
760ac27a0ecSDave Kleikamp 
761ac27a0ecSDave Kleikamp 
7621b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
7631b961ac0SChristoph Hellwig 		u64 ino, u32 generation)
764ac27a0ecSDave Kleikamp {
765ac27a0ecSDave Kleikamp 	struct inode *inode;
766ac27a0ecSDave Kleikamp 
767617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
768ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
769617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
770ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
771ac27a0ecSDave Kleikamp 
772ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
773ac27a0ecSDave Kleikamp 	 *
774617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
775ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
776ac27a0ecSDave Kleikamp 	 *
777ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
778ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
779ac27a0ecSDave Kleikamp 	 */
780ac27a0ecSDave Kleikamp 	inode = iget(sb, ino);
781ac27a0ecSDave Kleikamp 	if (inode == NULL)
782ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
783ac27a0ecSDave Kleikamp 	if (is_bad_inode(inode) ||
784ac27a0ecSDave Kleikamp 	    (generation && inode->i_generation != generation)) {
785ac27a0ecSDave Kleikamp 		iput(inode);
786ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
787ac27a0ecSDave Kleikamp 	}
7881b961ac0SChristoph Hellwig 
7891b961ac0SChristoph Hellwig 	return inode;
790ac27a0ecSDave Kleikamp }
7911b961ac0SChristoph Hellwig 
7921b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
7931b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
7941b961ac0SChristoph Hellwig {
7951b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
7961b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
7971b961ac0SChristoph Hellwig }
7981b961ac0SChristoph Hellwig 
7991b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
8001b961ac0SChristoph Hellwig 		int fh_len, int fh_type)
8011b961ac0SChristoph Hellwig {
8021b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
8031b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
804ac27a0ecSDave Kleikamp }
805ac27a0ecSDave Kleikamp 
806ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
807ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
808ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
809ac27a0ecSDave Kleikamp 
810617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type);
811617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode);
812617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
813617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
814617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
815617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
816617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
817617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
818617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
819617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
820ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
821617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
822ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
823ac27a0ecSDave Kleikamp 
824617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
825617ba13bSMingming Cao 	.initialize	= ext4_dquot_initialize,
826617ba13bSMingming Cao 	.drop		= ext4_dquot_drop,
827ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
828ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
829ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
830ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
831ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
832617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
833617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
834617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
835617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
836617ba13bSMingming Cao 	.write_info	= ext4_write_info
837ac27a0ecSDave Kleikamp };
838ac27a0ecSDave Kleikamp 
839617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
840617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
841ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
842ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
843ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
844ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
845ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
846ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
847ac27a0ecSDave Kleikamp };
848ac27a0ecSDave Kleikamp #endif
849ac27a0ecSDave Kleikamp 
850ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
851617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
852617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
853617ba13bSMingming Cao 	.read_inode	= ext4_read_inode,
854617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
855617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
856617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
857617ba13bSMingming Cao 	.put_super	= ext4_put_super,
858617ba13bSMingming Cao 	.write_super	= ext4_write_super,
859617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
860617ba13bSMingming Cao 	.write_super_lockfs = ext4_write_super_lockfs,
861617ba13bSMingming Cao 	.unlockfs	= ext4_unlockfs,
862617ba13bSMingming Cao 	.statfs		= ext4_statfs,
863617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
864617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
865617ba13bSMingming Cao 	.show_options	= ext4_show_options,
866ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
867617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
868617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
869ac27a0ecSDave Kleikamp #endif
870ac27a0ecSDave Kleikamp };
871ac27a0ecSDave Kleikamp 
87239655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
8731b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
8741b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
875617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
876ac27a0ecSDave Kleikamp };
877ac27a0ecSDave Kleikamp 
878ac27a0ecSDave Kleikamp enum {
879ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
880ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
881ac27a0ecSDave Kleikamp 	Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
882ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
883ac27a0ecSDave Kleikamp 	Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
884ac27a0ecSDave Kleikamp 	Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
885818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
886ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
887ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
888ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
889ac27a0ecSDave Kleikamp 	Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
89025ec56b5SJean Noel Cordenner 	Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
891c9de560dSAlex Tomas 	Opt_mballoc, Opt_nomballoc, Opt_stripe,
892ac27a0ecSDave Kleikamp };
893ac27a0ecSDave Kleikamp 
894ac27a0ecSDave Kleikamp static match_table_t tokens = {
895ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
896ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
897ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
898ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
899ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
900ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
901ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
902ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
903ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
904ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
905ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
906ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
907ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
908ac27a0ecSDave Kleikamp 	{Opt_nocheck, "nocheck"},
909ac27a0ecSDave Kleikamp 	{Opt_nocheck, "check=none"},
910ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
911ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
912ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
913ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
914ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
915ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
916ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
917ac27a0ecSDave Kleikamp 	{Opt_reservation, "reservation"},
918ac27a0ecSDave Kleikamp 	{Opt_noreservation, "noreservation"},
919ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
920ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
921ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
922ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
923ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
924ac27a0ecSDave Kleikamp 	{Opt_journal_inum, "journal=%u"},
925ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
926818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
927818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
928ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
929ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
930ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
931ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
932ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
933ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
934ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
935ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
936ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
937ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
938ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
939ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
940ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
941ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
942ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
943a86c6181SAlex Tomas 	{Opt_extents, "extents"},
9441e2462f9SMingming Cao 	{Opt_noextents, "noextents"},
94525ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
946c9de560dSAlex Tomas 	{Opt_mballoc, "mballoc"},
947c9de560dSAlex Tomas 	{Opt_nomballoc, "nomballoc"},
948c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
949ac27a0ecSDave Kleikamp 	{Opt_err, NULL},
950ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
951ac27a0ecSDave Kleikamp };
952ac27a0ecSDave Kleikamp 
953617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
954ac27a0ecSDave Kleikamp {
955617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
956ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
957ac27a0ecSDave Kleikamp 
958ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
959ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
960ac27a0ecSDave Kleikamp 	options += 3;
961617ba13bSMingming Cao 	/*todo: use simple_strtoll with >32bit ext4 */
962ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
963ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
964617ba13bSMingming Cao 		printk("EXT4-fs: Invalid sb specification: %s\n",
965ac27a0ecSDave Kleikamp 		       (char *) *data);
966ac27a0ecSDave Kleikamp 		return 1;
967ac27a0ecSDave Kleikamp 	}
968ac27a0ecSDave Kleikamp 	if (*options == ',')
969ac27a0ecSDave Kleikamp 		options++;
970ac27a0ecSDave Kleikamp 	*data = (void *) options;
971ac27a0ecSDave Kleikamp 	return sb_block;
972ac27a0ecSDave Kleikamp }
973ac27a0ecSDave Kleikamp 
974ac27a0ecSDave Kleikamp static int parse_options (char *options, struct super_block *sb,
975ac27a0ecSDave Kleikamp 			  unsigned int *inum, unsigned long *journal_devnum,
976617ba13bSMingming Cao 			  ext4_fsblk_t *n_blocks_count, int is_remount)
977ac27a0ecSDave Kleikamp {
978617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
979ac27a0ecSDave Kleikamp 	char * p;
980ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
981ac27a0ecSDave Kleikamp 	int data_opt = 0;
982ac27a0ecSDave Kleikamp 	int option;
983ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
984ac27a0ecSDave Kleikamp 	int qtype;
985ac27a0ecSDave Kleikamp 	char *qname;
986ac27a0ecSDave Kleikamp #endif
987ac27a0ecSDave Kleikamp 
988ac27a0ecSDave Kleikamp 	if (!options)
989ac27a0ecSDave Kleikamp 		return 1;
990ac27a0ecSDave Kleikamp 
991ac27a0ecSDave Kleikamp 	while ((p = strsep (&options, ",")) != NULL) {
992ac27a0ecSDave Kleikamp 		int token;
993ac27a0ecSDave Kleikamp 		if (!*p)
994ac27a0ecSDave Kleikamp 			continue;
995ac27a0ecSDave Kleikamp 
996ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
997ac27a0ecSDave Kleikamp 		switch (token) {
998ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
999ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, MINIX_DF);
1000ac27a0ecSDave Kleikamp 			break;
1001ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1002ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, MINIX_DF);
1003ac27a0ecSDave Kleikamp 			break;
1004ac27a0ecSDave Kleikamp 		case Opt_grpid:
1005ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, GRPID);
1006ac27a0ecSDave Kleikamp 			break;
1007ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1008ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, GRPID);
1009ac27a0ecSDave Kleikamp 			break;
1010ac27a0ecSDave Kleikamp 		case Opt_resuid:
1011ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1012ac27a0ecSDave Kleikamp 				return 0;
1013ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1014ac27a0ecSDave Kleikamp 			break;
1015ac27a0ecSDave Kleikamp 		case Opt_resgid:
1016ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1017ac27a0ecSDave Kleikamp 				return 0;
1018ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1019ac27a0ecSDave Kleikamp 			break;
1020ac27a0ecSDave Kleikamp 		case Opt_sb:
1021ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1022ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1023ac27a0ecSDave Kleikamp 			break;
1024ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1025ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1026ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_RO);
1027ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_PANIC);
1028ac27a0ecSDave Kleikamp 			break;
1029ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1030ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1031ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1032ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_RO);
1033ac27a0ecSDave Kleikamp 			break;
1034ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1035ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_RO);
1036ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1037ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_CONT);
1038ac27a0ecSDave Kleikamp 			break;
1039ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1040ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, NO_UID32);
1041ac27a0ecSDave Kleikamp 			break;
1042ac27a0ecSDave Kleikamp 		case Opt_nocheck:
1043ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, CHECK);
1044ac27a0ecSDave Kleikamp 			break;
1045ac27a0ecSDave Kleikamp 		case Opt_debug:
1046ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, DEBUG);
1047ac27a0ecSDave Kleikamp 			break;
1048ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1049ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, OLDALLOC);
1050ac27a0ecSDave Kleikamp 			break;
1051ac27a0ecSDave Kleikamp 		case Opt_orlov:
1052ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, OLDALLOC);
1053ac27a0ecSDave Kleikamp 			break;
1054617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
1055ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1056ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, XATTR_USER);
1057ac27a0ecSDave Kleikamp 			break;
1058ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1059ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, XATTR_USER);
1060ac27a0ecSDave Kleikamp 			break;
1061ac27a0ecSDave Kleikamp #else
1062ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1063ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1064617ba13bSMingming Cao 			printk("EXT4 (no)user_xattr options not supported\n");
1065ac27a0ecSDave Kleikamp 			break;
1066ac27a0ecSDave Kleikamp #endif
1067617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1068ac27a0ecSDave Kleikamp 		case Opt_acl:
1069ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1070ac27a0ecSDave Kleikamp 			break;
1071ac27a0ecSDave Kleikamp 		case Opt_noacl:
1072ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1073ac27a0ecSDave Kleikamp 			break;
1074ac27a0ecSDave Kleikamp #else
1075ac27a0ecSDave Kleikamp 		case Opt_acl:
1076ac27a0ecSDave Kleikamp 		case Opt_noacl:
1077617ba13bSMingming Cao 			printk("EXT4 (no)acl options not supported\n");
1078ac27a0ecSDave Kleikamp 			break;
1079ac27a0ecSDave Kleikamp #endif
1080ac27a0ecSDave Kleikamp 		case Opt_reservation:
1081ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, RESERVATION);
1082ac27a0ecSDave Kleikamp 			break;
1083ac27a0ecSDave Kleikamp 		case Opt_noreservation:
1084ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, RESERVATION);
1085ac27a0ecSDave Kleikamp 			break;
1086ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1087ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1088ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1089ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1090ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1091ac27a0ecSDave Kleikamp 			   journal file. */
1092ac27a0ecSDave Kleikamp 			if (is_remount) {
1093617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1094ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1095ac27a0ecSDave Kleikamp 				return 0;
1096ac27a0ecSDave Kleikamp 			}
1097ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
1098ac27a0ecSDave Kleikamp 			break;
1099ac27a0ecSDave Kleikamp 		case Opt_journal_inum:
1100ac27a0ecSDave Kleikamp 			if (is_remount) {
1101617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1102ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1103ac27a0ecSDave Kleikamp 				return 0;
1104ac27a0ecSDave Kleikamp 			}
1105ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1106ac27a0ecSDave Kleikamp 				return 0;
1107ac27a0ecSDave Kleikamp 			*inum = option;
1108ac27a0ecSDave Kleikamp 			break;
1109ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1110ac27a0ecSDave Kleikamp 			if (is_remount) {
1111617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1112ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1113ac27a0ecSDave Kleikamp 				return 0;
1114ac27a0ecSDave Kleikamp 			}
1115ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1116ac27a0ecSDave Kleikamp 				return 0;
1117ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1118ac27a0ecSDave Kleikamp 			break;
1119818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1120818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1121818d276cSGirish Shilamkar 			break;
1122818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1123818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1124818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1125818d276cSGirish Shilamkar 			break;
1126ac27a0ecSDave Kleikamp 		case Opt_noload:
1127ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, NOLOAD);
1128ac27a0ecSDave Kleikamp 			break;
1129ac27a0ecSDave Kleikamp 		case Opt_commit:
1130ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1131ac27a0ecSDave Kleikamp 				return 0;
1132ac27a0ecSDave Kleikamp 			if (option < 0)
1133ac27a0ecSDave Kleikamp 				return 0;
1134ac27a0ecSDave Kleikamp 			if (option == 0)
1135cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1136ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1137ac27a0ecSDave Kleikamp 			break;
1138ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1139617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1140ac27a0ecSDave Kleikamp 			goto datacheck;
1141ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1142617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1143ac27a0ecSDave Kleikamp 			goto datacheck;
1144ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1145617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1146ac27a0ecSDave Kleikamp 		datacheck:
1147ac27a0ecSDave Kleikamp 			if (is_remount) {
1148617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1149ac27a0ecSDave Kleikamp 						!= data_opt) {
1150ac27a0ecSDave Kleikamp 					printk(KERN_ERR
1151617ba13bSMingming Cao 						"EXT4-fs: cannot change data "
1152ac27a0ecSDave Kleikamp 						"mode on remount\n");
1153ac27a0ecSDave Kleikamp 					return 0;
1154ac27a0ecSDave Kleikamp 				}
1155ac27a0ecSDave Kleikamp 			} else {
1156617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1157ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1158ac27a0ecSDave Kleikamp 			}
1159ac27a0ecSDave Kleikamp 			break;
1160ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1161ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1162ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1163ac27a0ecSDave Kleikamp 			goto set_qf_name;
1164ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1165ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1166ac27a0ecSDave Kleikamp set_qf_name:
1167ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1168ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1169617ba13bSMingming Cao 					"EXT4-fs: Cannot change journalled "
1170ac27a0ecSDave Kleikamp 					"quota options when quota turned on.\n");
1171ac27a0ecSDave Kleikamp 				return 0;
1172ac27a0ecSDave Kleikamp 			}
1173ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1174ac27a0ecSDave Kleikamp 			if (!qname) {
1175ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1176617ba13bSMingming Cao 					"EXT4-fs: not enough memory for "
1177ac27a0ecSDave Kleikamp 					"storing quotafile name.\n");
1178ac27a0ecSDave Kleikamp 				return 0;
1179ac27a0ecSDave Kleikamp 			}
1180ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1181ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1182ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1183617ba13bSMingming Cao 					"EXT4-fs: %s quota file already "
1184ac27a0ecSDave Kleikamp 					"specified.\n", QTYPE2NAME(qtype));
1185ac27a0ecSDave Kleikamp 				kfree(qname);
1186ac27a0ecSDave Kleikamp 				return 0;
1187ac27a0ecSDave Kleikamp 			}
1188ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1189ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1190ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1191617ba13bSMingming Cao 					"EXT4-fs: quotafile must be on "
1192ac27a0ecSDave Kleikamp 					"filesystem root.\n");
1193ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1194ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1195ac27a0ecSDave Kleikamp 				return 0;
1196ac27a0ecSDave Kleikamp 			}
1197ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1198ac27a0ecSDave Kleikamp 			break;
1199ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1200ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1201ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1202ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1203ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1204ac27a0ecSDave Kleikamp clear_qf_name:
1205ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1206617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change "
1207ac27a0ecSDave Kleikamp 					"journalled quota options when "
1208ac27a0ecSDave Kleikamp 					"quota turned on.\n");
1209ac27a0ecSDave Kleikamp 				return 0;
1210ac27a0ecSDave Kleikamp 			}
1211ac27a0ecSDave Kleikamp 			/*
1212ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1213ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1214ac27a0ecSDave Kleikamp 			 */
1215ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1216ac27a0ecSDave Kleikamp 			break;
1217ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1218ac27a0ecSDave Kleikamp 			sbi->s_jquota_fmt = QFMT_VFS_OLD;
1219ac27a0ecSDave Kleikamp 			break;
1220ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1221ac27a0ecSDave Kleikamp 			sbi->s_jquota_fmt = QFMT_VFS_V0;
1222ac27a0ecSDave Kleikamp 			break;
1223ac27a0ecSDave Kleikamp 		case Opt_quota:
1224ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1225ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1226ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1227ac27a0ecSDave Kleikamp 			break;
1228ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1229ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1230ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1231ac27a0ecSDave Kleikamp 			break;
1232ac27a0ecSDave Kleikamp 		case Opt_noquota:
1233ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1234617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change quota "
1235ac27a0ecSDave Kleikamp 					"options when quota turned on.\n");
1236ac27a0ecSDave Kleikamp 				return 0;
1237ac27a0ecSDave Kleikamp 			}
1238ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1239ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1240ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1241ac27a0ecSDave Kleikamp 			break;
1242ac27a0ecSDave Kleikamp #else
1243ac27a0ecSDave Kleikamp 		case Opt_quota:
1244ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1245ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1246ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1247ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1248ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1249ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1250ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1251ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1252ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1253617ba13bSMingming Cao 				"EXT4-fs: journalled quota options not "
1254ac27a0ecSDave Kleikamp 				"supported.\n");
1255ac27a0ecSDave Kleikamp 			break;
1256ac27a0ecSDave Kleikamp 		case Opt_noquota:
1257ac27a0ecSDave Kleikamp 			break;
1258ac27a0ecSDave Kleikamp #endif
1259ac27a0ecSDave Kleikamp 		case Opt_abort:
1260ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ABORT);
1261ac27a0ecSDave Kleikamp 			break;
1262ac27a0ecSDave Kleikamp 		case Opt_barrier:
1263ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1264ac27a0ecSDave Kleikamp 				return 0;
1265ac27a0ecSDave Kleikamp 			if (option)
1266ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1267ac27a0ecSDave Kleikamp 			else
1268ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1269ac27a0ecSDave Kleikamp 			break;
1270ac27a0ecSDave Kleikamp 		case Opt_ignore:
1271ac27a0ecSDave Kleikamp 			break;
1272ac27a0ecSDave Kleikamp 		case Opt_resize:
1273ac27a0ecSDave Kleikamp 			if (!is_remount) {
1274617ba13bSMingming Cao 				printk("EXT4-fs: resize option only available "
1275ac27a0ecSDave Kleikamp 					"for remount\n");
1276ac27a0ecSDave Kleikamp 				return 0;
1277ac27a0ecSDave Kleikamp 			}
1278ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1279ac27a0ecSDave Kleikamp 				return 0;
1280ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1281ac27a0ecSDave Kleikamp 			break;
1282ac27a0ecSDave Kleikamp 		case Opt_nobh:
1283ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1284ac27a0ecSDave Kleikamp 			break;
1285ac27a0ecSDave Kleikamp 		case Opt_bh:
1286ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1287ac27a0ecSDave Kleikamp 			break;
1288a86c6181SAlex Tomas 		case Opt_extents:
1289a86c6181SAlex Tomas 			set_opt (sbi->s_mount_opt, EXTENTS);
1290a86c6181SAlex Tomas 			break;
12911e2462f9SMingming Cao 		case Opt_noextents:
12921e2462f9SMingming Cao 			clear_opt (sbi->s_mount_opt, EXTENTS);
12931e2462f9SMingming Cao 			break;
129425ec56b5SJean Noel Cordenner 		case Opt_i_version:
129525ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
129625ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
129725ec56b5SJean Noel Cordenner 			break;
1298c9de560dSAlex Tomas 		case Opt_mballoc:
1299c9de560dSAlex Tomas 			set_opt(sbi->s_mount_opt, MBALLOC);
1300c9de560dSAlex Tomas 			break;
1301c9de560dSAlex Tomas 		case Opt_nomballoc:
1302c9de560dSAlex Tomas 			clear_opt(sbi->s_mount_opt, MBALLOC);
1303c9de560dSAlex Tomas 			break;
1304c9de560dSAlex Tomas 		case Opt_stripe:
1305c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1306c9de560dSAlex Tomas 				return 0;
1307c9de560dSAlex Tomas 			if (option < 0)
1308c9de560dSAlex Tomas 				return 0;
1309c9de560dSAlex Tomas 			sbi->s_stripe = option;
1310c9de560dSAlex Tomas 			break;
1311ac27a0ecSDave Kleikamp 		default:
1312ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1313617ba13bSMingming Cao 				"EXT4-fs: Unrecognized mount option \"%s\" "
1314ac27a0ecSDave Kleikamp 				"or missing value\n", p);
1315ac27a0ecSDave Kleikamp 			return 0;
1316ac27a0ecSDave Kleikamp 		}
1317ac27a0ecSDave Kleikamp 	}
1318ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1319ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1320617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1321ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1322ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1323ac27a0ecSDave Kleikamp 
1324617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1325ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1326ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1327ac27a0ecSDave Kleikamp 
1328ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1329617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1330ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1331617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1332617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: old and new quota "
1333ac27a0ecSDave Kleikamp 					"format mixing.\n");
1334ac27a0ecSDave Kleikamp 			return 0;
1335ac27a0ecSDave Kleikamp 		}
1336ac27a0ecSDave Kleikamp 
1337ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1338617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: journalled quota format "
1339ac27a0ecSDave Kleikamp 					"not specified.\n");
1340ac27a0ecSDave Kleikamp 			return 0;
1341ac27a0ecSDave Kleikamp 		}
1342ac27a0ecSDave Kleikamp 	} else {
1343ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1344617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: journalled quota format "
1345ac27a0ecSDave Kleikamp 					"specified with no journalling "
1346ac27a0ecSDave Kleikamp 					"enabled.\n");
1347ac27a0ecSDave Kleikamp 			return 0;
1348ac27a0ecSDave Kleikamp 		}
1349ac27a0ecSDave Kleikamp 	}
1350ac27a0ecSDave Kleikamp #endif
1351ac27a0ecSDave Kleikamp 	return 1;
1352ac27a0ecSDave Kleikamp }
1353ac27a0ecSDave Kleikamp 
1354617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1355ac27a0ecSDave Kleikamp 			    int read_only)
1356ac27a0ecSDave Kleikamp {
1357617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1358ac27a0ecSDave Kleikamp 	int res = 0;
1359ac27a0ecSDave Kleikamp 
1360617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1361617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs warning: revision level too high, "
1362ac27a0ecSDave Kleikamp 			"forcing read-only mode\n");
1363ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1364ac27a0ecSDave Kleikamp 	}
1365ac27a0ecSDave Kleikamp 	if (read_only)
1366ac27a0ecSDave Kleikamp 		return res;
1367617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1368617ba13bSMingming Cao 		printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1369ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1370617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1371ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1372617ba13bSMingming Cao 			"EXT4-fs warning: mounting fs with errors, "
1373ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1374ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1375ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1376ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1377ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1378617ba13bSMingming Cao 			"EXT4-fs warning: maximal mount count reached, "
1379ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1380ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1381ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1382ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1383ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1384617ba13bSMingming Cao 			"EXT4-fs warning: checktime reached, "
1385ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1386ac27a0ecSDave Kleikamp #if 0
1387ac27a0ecSDave Kleikamp 		/* @@@ We _will_ want to clear the valid bit if we find
138863f57933SAndrew Morton 		 * inconsistencies, to force a fsck at reboot.  But for
138963f57933SAndrew Morton 		 * a plain journaled filesystem we can keep it set as
139063f57933SAndrew Morton 		 * valid forever! :)
139163f57933SAndrew Morton 		 */
1392617ba13bSMingming Cao 	es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
1393ac27a0ecSDave Kleikamp #endif
1394ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1395617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1396ac27a0ecSDave Kleikamp 	es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1397ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1398617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
1399617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1400ac27a0ecSDave Kleikamp 
1401617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
1402ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1403617ba13bSMingming Cao 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1404ac27a0ecSDave Kleikamp 				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
1405ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1406ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1407617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1408617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1409ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1410ac27a0ecSDave Kleikamp 
1411617ba13bSMingming Cao 	printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1412617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
1413ac27a0ecSDave Kleikamp 		char b[BDEVNAME_SIZE];
1414ac27a0ecSDave Kleikamp 
1415ac27a0ecSDave Kleikamp 		printk("external journal on %s\n",
1416617ba13bSMingming Cao 			bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
1417ac27a0ecSDave Kleikamp 	} else {
1418ac27a0ecSDave Kleikamp 		printk("internal journal\n");
1419ac27a0ecSDave Kleikamp 	}
1420ac27a0ecSDave Kleikamp 	return res;
1421ac27a0ecSDave Kleikamp }
1422ac27a0ecSDave Kleikamp 
1423717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1424717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1425717d50e4SAndreas Dilger {
1426717d50e4SAndreas Dilger 	__u16 crc = 0;
1427717d50e4SAndreas Dilger 
1428717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1429717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1430717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1431717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1432717d50e4SAndreas Dilger 
1433717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1434717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1435717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1436717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1437717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1438717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1439717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1440717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1441717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1442717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1443717d50e4SAndreas Dilger 					offset);
1444717d50e4SAndreas Dilger 	}
1445717d50e4SAndreas Dilger 
1446717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1447717d50e4SAndreas Dilger }
1448717d50e4SAndreas Dilger 
1449717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1450717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1451717d50e4SAndreas Dilger {
1452717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1453717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1454717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1455717d50e4SAndreas Dilger 		return 0;
1456717d50e4SAndreas Dilger 
1457717d50e4SAndreas Dilger 	return 1;
1458717d50e4SAndreas Dilger }
1459717d50e4SAndreas Dilger 
1460ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1461617ba13bSMingming Cao static int ext4_check_descriptors (struct super_block * sb)
1462ac27a0ecSDave Kleikamp {
1463617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1464617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1465617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1466bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1467bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1468bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1469617ba13bSMingming Cao 	struct ext4_group_desc * gdp = NULL;
1470ac27a0ecSDave Kleikamp 	int desc_block = 0;
1471ce421581SJose R. Santos 	int flexbg_flag = 0;
1472fd2d4291SAvantika Mathur 	ext4_group_t i;
1473ac27a0ecSDave Kleikamp 
1474ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1475ce421581SJose R. Santos 		flexbg_flag = 1;
1476ce421581SJose R. Santos 
1477617ba13bSMingming Cao 	ext4_debug ("Checking group descriptors");
1478ac27a0ecSDave Kleikamp 
1479ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_groups_count; i++)
1480ac27a0ecSDave Kleikamp 	{
1481ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1482bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1483ac27a0ecSDave Kleikamp 		else
1484ac27a0ecSDave Kleikamp 			last_block = first_block +
1485617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1486ac27a0ecSDave Kleikamp 
1487617ba13bSMingming Cao 		if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1488617ba13bSMingming Cao 			gdp = (struct ext4_group_desc *)
1489ac27a0ecSDave Kleikamp 					sbi->s_group_desc[desc_block++]->b_data;
14908fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
1491bd81d8eeSLaurent Vivier 		if (block_bitmap < first_block || block_bitmap > last_block)
1492ac27a0ecSDave Kleikamp 		{
1493617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1494fd2d4291SAvantika Mathur 				    "Block bitmap for group %lu"
14952ae02107SMingming Cao 				    " not in group (block %llu)!",
1496bd81d8eeSLaurent Vivier 				    i, block_bitmap);
1497ac27a0ecSDave Kleikamp 			return 0;
1498ac27a0ecSDave Kleikamp 		}
14998fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
1500bd81d8eeSLaurent Vivier 		if (inode_bitmap < first_block || inode_bitmap > last_block)
1501ac27a0ecSDave Kleikamp 		{
1502617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1503fd2d4291SAvantika Mathur 				    "Inode bitmap for group %lu"
15042ae02107SMingming Cao 				    " not in group (block %llu)!",
1505bd81d8eeSLaurent Vivier 				    i, inode_bitmap);
1506ac27a0ecSDave Kleikamp 			return 0;
1507ac27a0ecSDave Kleikamp 		}
15088fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1509bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
1510780dcdb2SEric Sandeen 		    inode_table + sbi->s_itb_per_group - 1 > last_block)
1511ac27a0ecSDave Kleikamp 		{
1512617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1513fd2d4291SAvantika Mathur 				    "Inode table for group %lu"
15142ae02107SMingming Cao 				    " not in group (block %llu)!",
1515bd81d8eeSLaurent Vivier 				    i, inode_table);
1516ac27a0ecSDave Kleikamp 			return 0;
1517ac27a0ecSDave Kleikamp 		}
1518717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1519717d50e4SAndreas Dilger 			ext4_error(sb, __FUNCTION__,
1520fd2d4291SAvantika Mathur 				   "Checksum for group %lu failed (%u!=%u)\n",
1521fd2d4291SAvantika Mathur 				    i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1522fd2d4291SAvantika Mathur 				    gdp)), le16_to_cpu(gdp->bg_checksum));
1523717d50e4SAndreas Dilger 			return 0;
1524717d50e4SAndreas Dilger 		}
1525ce421581SJose R. Santos 		if (!flexbg_flag)
1526617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
15270d1ee42fSAlexandre Ratchov 		gdp = (struct ext4_group_desc *)
15280d1ee42fSAlexandre Ratchov 			((__u8 *)gdp + EXT4_DESC_SIZE(sb));
1529ac27a0ecSDave Kleikamp 	}
1530ac27a0ecSDave Kleikamp 
1531bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1532617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
1533ac27a0ecSDave Kleikamp 	return 1;
1534ac27a0ecSDave Kleikamp }
1535ac27a0ecSDave Kleikamp 
1536617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1537ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1538ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1539ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1540ac27a0ecSDave Kleikamp  *
1541ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1542ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1543ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1544ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1545ac27a0ecSDave Kleikamp  *
1546ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1547ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1548ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1549617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1550ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1551ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1552ac27a0ecSDave Kleikamp  */
1553617ba13bSMingming Cao static void ext4_orphan_cleanup (struct super_block * sb,
1554617ba13bSMingming Cao 				 struct ext4_super_block * es)
1555ac27a0ecSDave Kleikamp {
1556ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1557ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1558ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1559ac27a0ecSDave Kleikamp 	int i;
1560ac27a0ecSDave Kleikamp #endif
1561ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1562ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1563ac27a0ecSDave Kleikamp 		return;
1564ac27a0ecSDave Kleikamp 	}
1565ac27a0ecSDave Kleikamp 
1566a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1567a8f48a95SEric Sandeen 		printk(KERN_ERR "EXT4-fs: write access "
1568a8f48a95SEric Sandeen 			"unavailable, skipping orphan cleanup.\n");
1569a8f48a95SEric Sandeen 		return;
1570a8f48a95SEric Sandeen 	}
1571a8f48a95SEric Sandeen 
1572617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1573ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1574ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1575ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1576ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1577ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1578ac27a0ecSDave Kleikamp 		return;
1579ac27a0ecSDave Kleikamp 	}
1580ac27a0ecSDave Kleikamp 
1581ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1582617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1583ac27a0ecSDave Kleikamp 		       sb->s_id);
1584ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1585ac27a0ecSDave Kleikamp 	}
1586ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1587ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1588ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1589ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1590ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1591617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1592617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1593ac27a0ecSDave Kleikamp 			if (ret < 0)
1594ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1595617ba13bSMingming Cao 					"EXT4-fs: Cannot turn on journalled "
1596ac27a0ecSDave Kleikamp 					"quota: error %d\n", ret);
1597ac27a0ecSDave Kleikamp 		}
1598ac27a0ecSDave Kleikamp 	}
1599ac27a0ecSDave Kleikamp #endif
1600ac27a0ecSDave Kleikamp 
1601ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1602ac27a0ecSDave Kleikamp 		struct inode *inode;
1603ac27a0ecSDave Kleikamp 
1604ac27a0ecSDave Kleikamp 		if (!(inode =
1605617ba13bSMingming Cao 		      ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
1606ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1607ac27a0ecSDave Kleikamp 			break;
1608ac27a0ecSDave Kleikamp 		}
1609ac27a0ecSDave Kleikamp 
1610617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1611ac27a0ecSDave Kleikamp 		DQUOT_INIT(inode);
1612ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1613ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1614ac27a0ecSDave Kleikamp 				"%s: truncating inode %lu to %Ld bytes\n",
1615ac27a0ecSDave Kleikamp 				__FUNCTION__, inode->i_ino, inode->i_size);
1616ac27a0ecSDave Kleikamp 			jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1617ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1618617ba13bSMingming Cao 			ext4_truncate(inode);
1619ac27a0ecSDave Kleikamp 			nr_truncates++;
1620ac27a0ecSDave Kleikamp 		} else {
1621ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1622ac27a0ecSDave Kleikamp 				"%s: deleting unreferenced inode %lu\n",
1623ac27a0ecSDave Kleikamp 				__FUNCTION__, inode->i_ino);
1624ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1625ac27a0ecSDave Kleikamp 				  inode->i_ino);
1626ac27a0ecSDave Kleikamp 			nr_orphans++;
1627ac27a0ecSDave Kleikamp 		}
1628ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1629ac27a0ecSDave Kleikamp 	}
1630ac27a0ecSDave Kleikamp 
1631ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x)==1) ? "" : "s"
1632ac27a0ecSDave Kleikamp 
1633ac27a0ecSDave Kleikamp 	if (nr_orphans)
1634617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1635ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_orphans));
1636ac27a0ecSDave Kleikamp 	if (nr_truncates)
1637617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1638ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_truncates));
1639ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1640ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1641ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1642ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
1643ac27a0ecSDave Kleikamp 			vfs_quota_off(sb, i);
1644ac27a0ecSDave Kleikamp 	}
1645ac27a0ecSDave Kleikamp #endif
1646ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1647ac27a0ecSDave Kleikamp }
1648cd2291a4SEric Sandeen /*
1649cd2291a4SEric Sandeen  * Maximal extent format file size.
1650cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1651cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1652cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1653cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1654cd2291a4SEric Sandeen  *
1655cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1656cd2291a4SEric Sandeen  */
1657cd2291a4SEric Sandeen static loff_t ext4_max_size(int blkbits)
1658cd2291a4SEric Sandeen {
1659cd2291a4SEric Sandeen 	loff_t res;
1660cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
1661cd2291a4SEric Sandeen 
1662cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
1663cd2291a4SEric Sandeen 	if (sizeof(blkcnt_t) < sizeof(u64)) {
1664cd2291a4SEric Sandeen 		/*
1665cd2291a4SEric Sandeen 		 * CONFIG_LSF is not enabled implies the inode
1666cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
1667cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
1668cd2291a4SEric Sandeen 		 */
1669cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
1670cd2291a4SEric Sandeen 
1671cd2291a4SEric Sandeen 		/* total blocks in file system block size */
1672cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
1673cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
1674cd2291a4SEric Sandeen 	}
1675cd2291a4SEric Sandeen 
1676cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
1677cd2291a4SEric Sandeen 	res = 1LL << 32;
1678cd2291a4SEric Sandeen 	res <<= blkbits;
1679cd2291a4SEric Sandeen 	res -= 1;
1680cd2291a4SEric Sandeen 
1681cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
1682cd2291a4SEric Sandeen 	if (res > upper_limit)
1683cd2291a4SEric Sandeen 		res = upper_limit;
1684cd2291a4SEric Sandeen 
1685cd2291a4SEric Sandeen 	return res;
1686cd2291a4SEric Sandeen }
1687ac27a0ecSDave Kleikamp 
1688ac27a0ecSDave Kleikamp /*
1689cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
16900fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
16910fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
1692ac27a0ecSDave Kleikamp  */
1693cd2291a4SEric Sandeen static loff_t ext4_max_bitmap_size(int bits)
1694ac27a0ecSDave Kleikamp {
1695617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
16960fc1b451SAneesh Kumar K.V 	int meta_blocks;
16970fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
16980fc1b451SAneesh Kumar K.V 	/* This is calculated to be the largest file size for a
1699cd2291a4SEric Sandeen 	 * dense, bitmapped file such that the total number of
1700ac27a0ecSDave Kleikamp 	 * sectors in the file, including data and all indirect blocks,
17010fc1b451SAneesh Kumar K.V 	 * does not exceed 2^48 -1
17020fc1b451SAneesh Kumar K.V 	 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
17030fc1b451SAneesh Kumar K.V 	 * total number of  512 bytes blocks of the file
17040fc1b451SAneesh Kumar K.V 	 */
17050fc1b451SAneesh Kumar K.V 
17060fc1b451SAneesh Kumar K.V 	if (sizeof(blkcnt_t) < sizeof(u64)) {
17070fc1b451SAneesh Kumar K.V 		/*
17080fc1b451SAneesh Kumar K.V 		 * CONFIG_LSF is not enabled implies the inode
17090fc1b451SAneesh Kumar K.V 		 * i_block represent total blocks in 512 bytes
17100fc1b451SAneesh Kumar K.V 		 * 32 == size of vfs inode i_blocks * 8
17110fc1b451SAneesh Kumar K.V 		 */
17120fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
17130fc1b451SAneesh Kumar K.V 
17140fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
17150fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
17160fc1b451SAneesh Kumar K.V 
17170fc1b451SAneesh Kumar K.V 	} else {
17188180a562SAneesh Kumar K.V 		/*
17198180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
17208180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
17218180a562SAneesh Kumar K.V 		 * represent total number of blocks in
17228180a562SAneesh Kumar K.V 		 * file system block size
17238180a562SAneesh Kumar K.V 		 */
17240fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
17250fc1b451SAneesh Kumar K.V 
17260fc1b451SAneesh Kumar K.V 	}
17270fc1b451SAneesh Kumar K.V 
17280fc1b451SAneesh Kumar K.V 	/* indirect blocks */
17290fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
17300fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
17310fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
17320fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
17330fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
17340fc1b451SAneesh Kumar K.V 
17350fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
17360fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
1737ac27a0ecSDave Kleikamp 
1738ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
1739ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
1740ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
1741ac27a0ecSDave Kleikamp 	res <<= bits;
1742ac27a0ecSDave Kleikamp 	if (res > upper_limit)
1743ac27a0ecSDave Kleikamp 		res = upper_limit;
17440fc1b451SAneesh Kumar K.V 
17450fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
17460fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
17470fc1b451SAneesh Kumar K.V 
1748ac27a0ecSDave Kleikamp 	return res;
1749ac27a0ecSDave Kleikamp }
1750ac27a0ecSDave Kleikamp 
1751617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
175270bbb3e0SAndrew Morton 				ext4_fsblk_t logical_sb_block, int nr)
1753ac27a0ecSDave Kleikamp {
1754617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1755fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
1756ac27a0ecSDave Kleikamp 	int has_super = 0;
1757ac27a0ecSDave Kleikamp 
1758ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1759ac27a0ecSDave Kleikamp 
1760617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1761ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
176270bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
1763ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
1764617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
1765ac27a0ecSDave Kleikamp 		has_super = 1;
1766617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
1767ac27a0ecSDave Kleikamp }
1768ac27a0ecSDave Kleikamp 
1769c9de560dSAlex Tomas /**
1770c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
1771c9de560dSAlex Tomas  * @sbi: In memory super block info
1772c9de560dSAlex Tomas  *
1773c9de560dSAlex Tomas  * If we have specified it via mount option, then
1774c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
1775c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
1776c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
1777c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
1778c9de560dSAlex Tomas  *
1779c9de560dSAlex Tomas  */
1780c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1781c9de560dSAlex Tomas {
1782c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1783c9de560dSAlex Tomas 	unsigned long stripe_width =
1784c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1785c9de560dSAlex Tomas 
1786c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1787c9de560dSAlex Tomas 		return sbi->s_stripe;
1788c9de560dSAlex Tomas 
1789c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
1790c9de560dSAlex Tomas 		return stripe_width;
1791c9de560dSAlex Tomas 
1792c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
1793c9de560dSAlex Tomas 		return stride;
1794c9de560dSAlex Tomas 
1795c9de560dSAlex Tomas 	return 0;
1796c9de560dSAlex Tomas }
1797ac27a0ecSDave Kleikamp 
1798617ba13bSMingming Cao static int ext4_fill_super (struct super_block *sb, void *data, int silent)
17991d03ec98SAneesh Kumar K.V 				__releases(kernel_sem)
18001d03ec98SAneesh Kumar K.V 				__acquires(kernel_sem)
18011d03ec98SAneesh Kumar K.V 
1802ac27a0ecSDave Kleikamp {
1803ac27a0ecSDave Kleikamp 	struct buffer_head * bh;
1804617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
1805617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1806617ba13bSMingming Cao 	ext4_fsblk_t block;
1807617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
180870bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
1809ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
1810ac27a0ecSDave Kleikamp 	unsigned int journal_inum = 0;
1811ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
1812ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
1813ac27a0ecSDave Kleikamp 	struct inode *root;
1814ac27a0ecSDave Kleikamp 	int blocksize;
1815ac27a0ecSDave Kleikamp 	int hblock;
1816ac27a0ecSDave Kleikamp 	int db_count;
1817ac27a0ecSDave Kleikamp 	int i;
1818ac27a0ecSDave Kleikamp 	int needs_recovery;
1819ac27a0ecSDave Kleikamp 	__le32 features;
1820bd81d8eeSLaurent Vivier 	__u64 blocks_count;
1821833f4077SPeter Zijlstra 	int err;
1822ac27a0ecSDave Kleikamp 
1823ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1824ac27a0ecSDave Kleikamp 	if (!sbi)
1825ac27a0ecSDave Kleikamp 		return -ENOMEM;
1826ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
1827ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
1828617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
1829617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
1830d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
1831ac27a0ecSDave Kleikamp 
1832ac27a0ecSDave Kleikamp 	unlock_kernel();
1833ac27a0ecSDave Kleikamp 
1834617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1835ac27a0ecSDave Kleikamp 	if (!blocksize) {
1836617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1837ac27a0ecSDave Kleikamp 		goto out_fail;
1838ac27a0ecSDave Kleikamp 	}
1839ac27a0ecSDave Kleikamp 
1840afc7cbcaSTakashi Sato 	if (!sb_set_blocksize(sb, blocksize)) {
1841afc7cbcaSTakashi Sato 		printk(KERN_ERR "EXT4-fs: bad blocksize %d.\n", blocksize);
1842afc7cbcaSTakashi Sato 		goto out_fail;
1843afc7cbcaSTakashi Sato 	}
1844afc7cbcaSTakashi Sato 
1845ac27a0ecSDave Kleikamp 	/*
1846617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
1847ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
1848ac27a0ecSDave Kleikamp 	 */
1849617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
185070bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
185170bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
1852ac27a0ecSDave Kleikamp 	} else {
185370bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
1854ac27a0ecSDave Kleikamp 	}
1855ac27a0ecSDave Kleikamp 
185670bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
1857617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
1858ac27a0ecSDave Kleikamp 		goto out_fail;
1859ac27a0ecSDave Kleikamp 	}
1860ac27a0ecSDave Kleikamp 	/*
1861ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
1862617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
1863ac27a0ecSDave Kleikamp 	 */
1864617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1865ac27a0ecSDave Kleikamp 	sbi->s_es = es;
1866ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
1867617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
1868617ba13bSMingming Cao 		goto cantfind_ext4;
1869ac27a0ecSDave Kleikamp 
1870ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
1871ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1872617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
1873ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
1874617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1875ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
1876617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
1877ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
18782e7842b8SHugh Dickins #ifdef CONFIG_EXT4DEV_FS_XATTR
1879617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1880ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
18812e7842b8SHugh Dickins #endif
18822e7842b8SHugh Dickins #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1883617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
1884ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
18852e7842b8SHugh Dickins #endif
1886617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1887617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1888617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1889617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1890617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1891617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1892ac27a0ecSDave Kleikamp 
1893617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1894ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1895bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
1896ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
1897bb4f397aSAneesh Kumar K.V 	else
1898bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
1899ac27a0ecSDave Kleikamp 
1900ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1901ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1902ac27a0ecSDave Kleikamp 
1903ac27a0ecSDave Kleikamp 	set_opt(sbi->s_mount_opt, RESERVATION);
1904ac27a0ecSDave Kleikamp 
19051e2462f9SMingming Cao 	/*
19061e2462f9SMingming Cao 	 * turn on extents feature by default in ext4 filesystem
19071e2462f9SMingming Cao 	 * User -o noextents to turn it off
19081e2462f9SMingming Cao 	 */
19091e2462f9SMingming Cao 	set_opt(sbi->s_mount_opt, EXTENTS);
19103dbd0edeSAneesh Kumar K.V 	/*
19113dbd0edeSAneesh Kumar K.V 	 * turn on mballoc feature by default in ext4 filesystem
19123dbd0edeSAneesh Kumar K.V 	 * User -o nomballoc to turn it off
19133dbd0edeSAneesh Kumar K.V 	 */
19143dbd0edeSAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, MBALLOC);
19151e2462f9SMingming Cao 
1916ac27a0ecSDave Kleikamp 	if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1917ac27a0ecSDave Kleikamp 			    NULL, 0))
1918ac27a0ecSDave Kleikamp 		goto failed_mount;
1919ac27a0ecSDave Kleikamp 
1920ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1921617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1922ac27a0ecSDave Kleikamp 
1923617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1924617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1925617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1926617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1927ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1928617ba13bSMingming Cao 		       "EXT4-fs warning: feature flags set on rev 0 fs, "
1929ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1930ac27a0ecSDave Kleikamp 	/*
1931ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
1932ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
1933ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
1934ac27a0ecSDave Kleikamp 	 */
1935617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
1936ac27a0ecSDave Kleikamp 	if (features) {
1937617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
1938ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
1939ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
1940ac27a0ecSDave Kleikamp 		goto failed_mount;
1941ac27a0ecSDave Kleikamp 	}
1942617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
1943ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
1944617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
1945ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
1946ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
1947ac27a0ecSDave Kleikamp 		goto failed_mount;
1948ac27a0ecSDave Kleikamp 	}
19490fc1b451SAneesh Kumar K.V 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
19500fc1b451SAneesh Kumar K.V 		/*
19510fc1b451SAneesh Kumar K.V 		 * Large file size enabled file system can only be
19520fc1b451SAneesh Kumar K.V 		 * mount if kernel is build with CONFIG_LSF
19530fc1b451SAneesh Kumar K.V 		 */
19540fc1b451SAneesh Kumar K.V 		if (sizeof(root->i_blocks) < sizeof(u64) &&
19550fc1b451SAneesh Kumar K.V 				!(sb->s_flags & MS_RDONLY)) {
19560fc1b451SAneesh Kumar K.V 			printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
19570fc1b451SAneesh Kumar K.V 					"files cannot be mounted read-write "
19580fc1b451SAneesh Kumar K.V 					"without CONFIG_LSF.\n", sb->s_id);
19590fc1b451SAneesh Kumar K.V 			goto failed_mount;
19600fc1b451SAneesh Kumar K.V 		}
19610fc1b451SAneesh Kumar K.V 	}
1962ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1963ac27a0ecSDave Kleikamp 
1964617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1965617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
1966ac27a0ecSDave Kleikamp 		printk(KERN_ERR
1967617ba13bSMingming Cao 		       "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
1968ac27a0ecSDave Kleikamp 		       blocksize, sb->s_id);
1969ac27a0ecSDave Kleikamp 		goto failed_mount;
1970ac27a0ecSDave Kleikamp 	}
1971ac27a0ecSDave Kleikamp 
1972ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(sb->s_bdev);
1973ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
1974ac27a0ecSDave Kleikamp 		/*
1975ac27a0ecSDave Kleikamp 		 * Make sure the blocksize for the filesystem is larger
1976ac27a0ecSDave Kleikamp 		 * than the hardware sectorsize for the machine.
1977ac27a0ecSDave Kleikamp 		 */
1978ac27a0ecSDave Kleikamp 		if (blocksize < hblock) {
1979617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
1980ac27a0ecSDave Kleikamp 			       "device blocksize %d.\n", blocksize, hblock);
1981ac27a0ecSDave Kleikamp 			goto failed_mount;
1982ac27a0ecSDave Kleikamp 		}
1983ac27a0ecSDave Kleikamp 
1984ac27a0ecSDave Kleikamp 		brelse (bh);
1985ac27a0ecSDave Kleikamp 		sb_set_blocksize(sb, blocksize);
198670bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
198770bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
198870bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
1989ac27a0ecSDave Kleikamp 		if (!bh) {
1990ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1991617ba13bSMingming Cao 			       "EXT4-fs: Can't read superblock on 2nd try.\n");
1992ac27a0ecSDave Kleikamp 			goto failed_mount;
1993ac27a0ecSDave Kleikamp 		}
1994617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
1995ac27a0ecSDave Kleikamp 		sbi->s_es = es;
1996617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
1997ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1998617ba13bSMingming Cao 				"EXT4-fs: Magic mismatch, very weird !\n");
1999ac27a0ecSDave Kleikamp 			goto failed_mount;
2000ac27a0ecSDave Kleikamp 		}
2001ac27a0ecSDave Kleikamp 	}
2002ac27a0ecSDave Kleikamp 
2003e2b46574SEric Sandeen 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits);
2004617ba13bSMingming Cao 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
2005ac27a0ecSDave Kleikamp 
2006617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2007617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2008617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2009ac27a0ecSDave Kleikamp 	} else {
2010ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2011ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2012617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
20131330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2014ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2015ac27a0ecSDave Kleikamp 			printk (KERN_ERR
2016617ba13bSMingming Cao 				"EXT4-fs: unsupported inode size: %d\n",
2017ac27a0ecSDave Kleikamp 				sbi->s_inode_size);
2018ac27a0ecSDave Kleikamp 			goto failed_mount;
2019ac27a0ecSDave Kleikamp 		}
2020ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2021ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2022ac27a0ecSDave Kleikamp 	}
20230d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
20240d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
20258fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
20260d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2027d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
20280d1ee42fSAlexandre Ratchov 			printk(KERN_ERR
20298fadc143SAlexandre Ratchov 			       "EXT4-fs: unsupported descriptor size %lu\n",
20300d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
20310d1ee42fSAlexandre Ratchov 			goto failed_mount;
20320d1ee42fSAlexandre Ratchov 		}
20330d1ee42fSAlexandre Ratchov 	} else
20340d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2035ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2036ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2037b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2038617ba13bSMingming Cao 		goto cantfind_ext4;
2039617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2040ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2041617ba13bSMingming Cao 		goto cantfind_ext4;
2042ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2043ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
20440d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2045ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2046ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2047e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2048e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2049ac27a0ecSDave Kleikamp 	for (i=0; i < 4; i++)
2050ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2051ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2052ac27a0ecSDave Kleikamp 
2053ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2054ac27a0ecSDave Kleikamp 		printk (KERN_ERR
2055617ba13bSMingming Cao 			"EXT4-fs: #blocks per group too big: %lu\n",
2056ac27a0ecSDave Kleikamp 			sbi->s_blocks_per_group);
2057ac27a0ecSDave Kleikamp 		goto failed_mount;
2058ac27a0ecSDave Kleikamp 	}
2059ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2060ac27a0ecSDave Kleikamp 		printk (KERN_ERR
2061617ba13bSMingming Cao 			"EXT4-fs: #inodes per group too big: %lu\n",
2062ac27a0ecSDave Kleikamp 			sbi->s_inodes_per_group);
2063ac27a0ecSDave Kleikamp 		goto failed_mount;
2064ac27a0ecSDave Kleikamp 	}
2065ac27a0ecSDave Kleikamp 
2066bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
2067ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2068617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2069ac27a0ecSDave Kleikamp 			" too large to mount safely\n", sb->s_id);
2070ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
2071617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2072ac27a0ecSDave Kleikamp 					"enabled\n");
2073ac27a0ecSDave Kleikamp 		goto failed_mount;
2074ac27a0ecSDave Kleikamp 	}
2075ac27a0ecSDave Kleikamp 
2076617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2077617ba13bSMingming Cao 		goto cantfind_ext4;
2078e7c95593SEric Sandeen 
2079e7c95593SEric Sandeen 	/* ensure blocks_count calculation below doesn't sign-extend */
2080e7c95593SEric Sandeen 	if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2081e7c95593SEric Sandeen 	    le32_to_cpu(es->s_first_data_block) + 1) {
2082e7c95593SEric Sandeen 		printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2083e7c95593SEric Sandeen 		       "first data block %u, blocks per group %lu\n",
2084e7c95593SEric Sandeen 			ext4_blocks_count(es),
2085e7c95593SEric Sandeen 			le32_to_cpu(es->s_first_data_block),
2086e7c95593SEric Sandeen 			EXT4_BLOCKS_PER_GROUP(sb));
2087e7c95593SEric Sandeen 		goto failed_mount;
2088e7c95593SEric Sandeen 	}
2089bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2090bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2091bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2092bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2093bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2094617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2095617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2096ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
2097ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2098ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2099617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs: not enough memory\n");
2100ac27a0ecSDave Kleikamp 		goto failed_mount;
2101ac27a0ecSDave Kleikamp 	}
2102ac27a0ecSDave Kleikamp 
2103ac27a0ecSDave Kleikamp 	bgl_lock_init(&sbi->s_blockgroup_lock);
2104ac27a0ecSDave Kleikamp 
2105ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
210670bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2107ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2108ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2109617ba13bSMingming Cao 			printk (KERN_ERR "EXT4-fs: "
2110ac27a0ecSDave Kleikamp 				"can't read group descriptor %d\n", i);
2111ac27a0ecSDave Kleikamp 			db_count = i;
2112ac27a0ecSDave Kleikamp 			goto failed_mount2;
2113ac27a0ecSDave Kleikamp 		}
2114ac27a0ecSDave Kleikamp 	}
2115617ba13bSMingming Cao 	if (!ext4_check_descriptors (sb)) {
2116617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2117ac27a0ecSDave Kleikamp 		goto failed_mount2;
2118ac27a0ecSDave Kleikamp 	}
2119ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2120ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2121ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2122ac27a0ecSDave Kleikamp 
2123833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2124617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2125833f4077SPeter Zijlstra 	if (!err) {
2126833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2127617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2128833f4077SPeter Zijlstra 	}
2129833f4077SPeter Zijlstra 	if (!err) {
2130833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2131617ba13bSMingming Cao 				ext4_count_dirs(sb));
2132833f4077SPeter Zijlstra 	}
2133833f4077SPeter Zijlstra 	if (err) {
2134833f4077SPeter Zijlstra 		printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2135833f4077SPeter Zijlstra 		goto failed_mount3;
2136833f4077SPeter Zijlstra 	}
2137ac27a0ecSDave Kleikamp 
2138ac27a0ecSDave Kleikamp 	/* per fileystem reservation list head & lock */
2139ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_rsv_window_lock);
2140ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_root = RB_ROOT;
2141ac27a0ecSDave Kleikamp 	/* Add a single, static dummy reservation to the start of the
2142ac27a0ecSDave Kleikamp 	 * reservation window list --- it gives us a placeholder for
2143ac27a0ecSDave Kleikamp 	 * append-at-start-of-list which makes the allocation logic
2144ac27a0ecSDave Kleikamp 	 * _much_ simpler. */
2145617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
2146617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
2147ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_alloc_hit = 0;
2148ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_goal_size = 0;
2149617ba13bSMingming Cao 	ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
2150ac27a0ecSDave Kleikamp 
2151c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
2152c9de560dSAlex Tomas 
2153ac27a0ecSDave Kleikamp 	/*
2154ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2155ac27a0ecSDave Kleikamp 	 */
2156617ba13bSMingming Cao 	sb->s_op = &ext4_sops;
2157617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2158617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2159ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2160617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2161617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2162ac27a0ecSDave Kleikamp #endif
2163ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2164ac27a0ecSDave Kleikamp 
2165ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2166ac27a0ecSDave Kleikamp 
2167ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2168617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2169617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2170ac27a0ecSDave Kleikamp 
2171ac27a0ecSDave Kleikamp 	/*
2172ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2173ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2174ac27a0ecSDave Kleikamp 	 */
2175ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2176617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2177617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2178ac27a0ecSDave Kleikamp 			goto failed_mount3;
2179ac27a0ecSDave Kleikamp 	} else if (journal_inum) {
2180617ba13bSMingming Cao 		if (ext4_create_journal(sb, es, journal_inum))
2181ac27a0ecSDave Kleikamp 			goto failed_mount3;
2182ac27a0ecSDave Kleikamp 	} else {
2183ac27a0ecSDave Kleikamp 		if (!silent)
2184ac27a0ecSDave Kleikamp 			printk (KERN_ERR
2185617ba13bSMingming Cao 				"ext4: No journal on filesystem on %s\n",
2186ac27a0ecSDave Kleikamp 				sb->s_id);
2187ac27a0ecSDave Kleikamp 		goto failed_mount3;
2188ac27a0ecSDave Kleikamp 	}
2189ac27a0ecSDave Kleikamp 
2190eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2191eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2192eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2193eb40a09cSJose R. Santos 		printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2194eb40a09cSJose R. Santos 		goto failed_mount4;
2195eb40a09cSJose R. Santos 	}
2196eb40a09cSJose R. Santos 
2197818d276cSGirish Shilamkar 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2198818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2199818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2200818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2201818d276cSGirish Shilamkar 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2202818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2203818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2204818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2205818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2206818d276cSGirish Shilamkar 	} else {
2207818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal,
2208818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2209818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2210818d276cSGirish Shilamkar 	}
2211818d276cSGirish Shilamkar 
2212ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2213ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2214ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2215ac27a0ecSDave Kleikamp 	case 0:
2216ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
221763f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
221863f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
221963f57933SAndrew Morton 		 */
2220dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2221dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2222ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2223ac27a0ecSDave Kleikamp 		else
2224ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2225ac27a0ecSDave Kleikamp 		break;
2226ac27a0ecSDave Kleikamp 
2227617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2228617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2229dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2230dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2231617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: Journal does not support "
2232ac27a0ecSDave Kleikamp 			       "requested data journaling mode\n");
2233ac27a0ecSDave Kleikamp 			goto failed_mount4;
2234ac27a0ecSDave Kleikamp 		}
2235ac27a0ecSDave Kleikamp 	default:
2236ac27a0ecSDave Kleikamp 		break;
2237ac27a0ecSDave Kleikamp 	}
2238ac27a0ecSDave Kleikamp 
2239ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2240617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2241617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2242ac27a0ecSDave Kleikamp 				"its supported only with writeback mode\n");
2243ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2244ac27a0ecSDave Kleikamp 		}
2245ac27a0ecSDave Kleikamp 	}
2246ac27a0ecSDave Kleikamp 	/*
2247dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2248ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2249ac27a0ecSDave Kleikamp 	 */
2250ac27a0ecSDave Kleikamp 
2251617ba13bSMingming Cao 	root = iget(sb, EXT4_ROOT_INO);
2252ac27a0ecSDave Kleikamp 	sb->s_root = d_alloc_root(root);
2253ac27a0ecSDave Kleikamp 	if (!sb->s_root) {
2254617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2255ac27a0ecSDave Kleikamp 		iput(root);
2256ac27a0ecSDave Kleikamp 		goto failed_mount4;
2257ac27a0ecSDave Kleikamp 	}
2258ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2259ac27a0ecSDave Kleikamp 		dput(sb->s_root);
2260ac27a0ecSDave Kleikamp 		sb->s_root = NULL;
2261617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2262ac27a0ecSDave Kleikamp 		goto failed_mount4;
2263ac27a0ecSDave Kleikamp 	}
2264ac27a0ecSDave Kleikamp 
2265617ba13bSMingming Cao 	ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
2266ef7f3835SKalpak Shah 
2267ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2268ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2269ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2270ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2271ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2272ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2273ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2274ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2275ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2276ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2277ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2278ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2279ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2280ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2281ef7f3835SKalpak Shah 		}
2282ef7f3835SKalpak Shah 	}
2283ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2284ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2285ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2286ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2287ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2288ef7f3835SKalpak Shah 		printk(KERN_INFO "EXT4-fs: required extra inode space not"
2289ef7f3835SKalpak Shah 			"available.\n");
2290ef7f3835SKalpak Shah 	}
2291ef7f3835SKalpak Shah 
2292ac27a0ecSDave Kleikamp 	/*
2293ac27a0ecSDave Kleikamp 	 * akpm: core read_super() calls in here with the superblock locked.
2294ac27a0ecSDave Kleikamp 	 * That deadlocks, because orphan cleanup needs to lock the superblock
2295ac27a0ecSDave Kleikamp 	 * in numerous places.  Here we just pop the lock - it's relatively
2296ac27a0ecSDave Kleikamp 	 * harmless, because we are now ready to accept write_super() requests,
2297ac27a0ecSDave Kleikamp 	 * and aviro says that's the only reason for hanging onto the
2298ac27a0ecSDave Kleikamp 	 * superblock lock.
2299ac27a0ecSDave Kleikamp 	 */
2300617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2301617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2302617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2303ac27a0ecSDave Kleikamp 	if (needs_recovery)
2304617ba13bSMingming Cao 		printk (KERN_INFO "EXT4-fs: recovery complete.\n");
2305617ba13bSMingming Cao 	ext4_mark_recovery_complete(sb, es);
2306617ba13bSMingming Cao 	printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2307617ba13bSMingming Cao 		test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2308617ba13bSMingming Cao 		test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2309ac27a0ecSDave Kleikamp 		"writeback");
2310ac27a0ecSDave Kleikamp 
2311a86c6181SAlex Tomas 	ext4_ext_init(sb);
2312c9de560dSAlex Tomas 	ext4_mb_init(sb, needs_recovery);
2313a86c6181SAlex Tomas 
2314ac27a0ecSDave Kleikamp 	lock_kernel();
2315ac27a0ecSDave Kleikamp 	return 0;
2316ac27a0ecSDave Kleikamp 
2317617ba13bSMingming Cao cantfind_ext4:
2318ac27a0ecSDave Kleikamp 	if (!silent)
2319617ba13bSMingming Cao 		printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2320ac27a0ecSDave Kleikamp 		       sb->s_id);
2321ac27a0ecSDave Kleikamp 	goto failed_mount;
2322ac27a0ecSDave Kleikamp 
2323ac27a0ecSDave Kleikamp failed_mount4:
2324dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
2325ac27a0ecSDave Kleikamp failed_mount3:
2326ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2327ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2328ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
2329ac27a0ecSDave Kleikamp failed_mount2:
2330ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2331ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2332ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2333ac27a0ecSDave Kleikamp failed_mount:
2334ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2335ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2336ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2337ac27a0ecSDave Kleikamp #endif
2338617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2339ac27a0ecSDave Kleikamp 	brelse(bh);
2340ac27a0ecSDave Kleikamp out_fail:
2341ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2342ac27a0ecSDave Kleikamp 	kfree(sbi);
2343ac27a0ecSDave Kleikamp 	lock_kernel();
2344ac27a0ecSDave Kleikamp 	return -EINVAL;
2345ac27a0ecSDave Kleikamp }
2346ac27a0ecSDave Kleikamp 
2347ac27a0ecSDave Kleikamp /*
2348ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2349ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2350ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2351ac27a0ecSDave Kleikamp  */
2352617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2353ac27a0ecSDave Kleikamp {
2354617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2355ac27a0ecSDave Kleikamp 
2356ac27a0ecSDave Kleikamp 	if (sbi->s_commit_interval)
2357ac27a0ecSDave Kleikamp 		journal->j_commit_interval = sbi->s_commit_interval;
2358617ba13bSMingming Cao 	/* We could also set up an ext4-specific default for the commit
2359ac27a0ecSDave Kleikamp 	 * interval here, but for now we'll just fall back to the jbd
2360ac27a0ecSDave Kleikamp 	 * default. */
2361ac27a0ecSDave Kleikamp 
2362ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2363ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2364dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2365ac27a0ecSDave Kleikamp 	else
2366dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
2367ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2368ac27a0ecSDave Kleikamp }
2369ac27a0ecSDave Kleikamp 
2370617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2371ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2372ac27a0ecSDave Kleikamp {
2373ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2374ac27a0ecSDave Kleikamp 	journal_t *journal;
2375ac27a0ecSDave Kleikamp 
2376ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2377ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2378ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2379ac27a0ecSDave Kleikamp 
2380ac27a0ecSDave Kleikamp 	journal_inode = iget(sb, journal_inum);
2381ac27a0ecSDave Kleikamp 	if (!journal_inode) {
2382617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: no journal found.\n");
2383ac27a0ecSDave Kleikamp 		return NULL;
2384ac27a0ecSDave Kleikamp 	}
2385ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2386ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2387ac27a0ecSDave Kleikamp 		iput(journal_inode);
2388617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2389ac27a0ecSDave Kleikamp 		return NULL;
2390ac27a0ecSDave Kleikamp 	}
2391ac27a0ecSDave Kleikamp 
2392ac27a0ecSDave Kleikamp 	jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2393ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
2394ac27a0ecSDave Kleikamp 	if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
2395617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2396ac27a0ecSDave Kleikamp 		iput(journal_inode);
2397ac27a0ecSDave Kleikamp 		return NULL;
2398ac27a0ecSDave Kleikamp 	}
2399ac27a0ecSDave Kleikamp 
2400dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
2401ac27a0ecSDave Kleikamp 	if (!journal) {
2402617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2403ac27a0ecSDave Kleikamp 		iput(journal_inode);
2404ac27a0ecSDave Kleikamp 		return NULL;
2405ac27a0ecSDave Kleikamp 	}
2406ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2407617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2408ac27a0ecSDave Kleikamp 	return journal;
2409ac27a0ecSDave Kleikamp }
2410ac27a0ecSDave Kleikamp 
2411617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
2412ac27a0ecSDave Kleikamp 				       dev_t j_dev)
2413ac27a0ecSDave Kleikamp {
2414ac27a0ecSDave Kleikamp 	struct buffer_head * bh;
2415ac27a0ecSDave Kleikamp 	journal_t *journal;
2416617ba13bSMingming Cao 	ext4_fsblk_t start;
2417617ba13bSMingming Cao 	ext4_fsblk_t len;
2418ac27a0ecSDave Kleikamp 	int hblock, blocksize;
2419617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
2420ac27a0ecSDave Kleikamp 	unsigned long offset;
2421617ba13bSMingming Cao 	struct ext4_super_block * es;
2422ac27a0ecSDave Kleikamp 	struct block_device *bdev;
2423ac27a0ecSDave Kleikamp 
2424617ba13bSMingming Cao 	bdev = ext4_blkdev_get(j_dev);
2425ac27a0ecSDave Kleikamp 	if (bdev == NULL)
2426ac27a0ecSDave Kleikamp 		return NULL;
2427ac27a0ecSDave Kleikamp 
2428ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
2429ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2430617ba13bSMingming Cao 			"EXT4: failed to claim external journal device.\n");
2431ac27a0ecSDave Kleikamp 		blkdev_put(bdev);
2432ac27a0ecSDave Kleikamp 		return NULL;
2433ac27a0ecSDave Kleikamp 	}
2434ac27a0ecSDave Kleikamp 
2435ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
2436ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(bdev);
2437ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
2438ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2439617ba13bSMingming Cao 			"EXT4-fs: blocksize too small for journal device.\n");
2440ac27a0ecSDave Kleikamp 		goto out_bdev;
2441ac27a0ecSDave Kleikamp 	}
2442ac27a0ecSDave Kleikamp 
2443617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2444617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2445ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
2446ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
2447617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2448ac27a0ecSDave Kleikamp 		       "external journal\n");
2449ac27a0ecSDave Kleikamp 		goto out_bdev;
2450ac27a0ecSDave Kleikamp 	}
2451ac27a0ecSDave Kleikamp 
2452617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2453617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2454ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
2455617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2456617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: external journal has "
2457ac27a0ecSDave Kleikamp 					"bad superblock\n");
2458ac27a0ecSDave Kleikamp 		brelse(bh);
2459ac27a0ecSDave Kleikamp 		goto out_bdev;
2460ac27a0ecSDave Kleikamp 	}
2461ac27a0ecSDave Kleikamp 
2462617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2463617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2464ac27a0ecSDave Kleikamp 		brelse(bh);
2465ac27a0ecSDave Kleikamp 		goto out_bdev;
2466ac27a0ecSDave Kleikamp 	}
2467ac27a0ecSDave Kleikamp 
2468bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
2469ac27a0ecSDave Kleikamp 	start = sb_block + 1;
2470ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
2471ac27a0ecSDave Kleikamp 
2472dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2473ac27a0ecSDave Kleikamp 					start, len, blocksize);
2474ac27a0ecSDave Kleikamp 	if (!journal) {
2475617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2476ac27a0ecSDave Kleikamp 		goto out_bdev;
2477ac27a0ecSDave Kleikamp 	}
2478ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2479ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
2480ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
2481ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
2482617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2483ac27a0ecSDave Kleikamp 		goto out_journal;
2484ac27a0ecSDave Kleikamp 	}
2485ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2486617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: External journal has more than one "
2487ac27a0ecSDave Kleikamp 					"user (unsupported) - %d\n",
2488ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
2489ac27a0ecSDave Kleikamp 		goto out_journal;
2490ac27a0ecSDave Kleikamp 	}
2491617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
2492617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2493ac27a0ecSDave Kleikamp 	return journal;
2494ac27a0ecSDave Kleikamp out_journal:
2495dab291afSMingming Cao 	jbd2_journal_destroy(journal);
2496ac27a0ecSDave Kleikamp out_bdev:
2497617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
2498ac27a0ecSDave Kleikamp 	return NULL;
2499ac27a0ecSDave Kleikamp }
2500ac27a0ecSDave Kleikamp 
2501617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
2502617ba13bSMingming Cao 			     struct ext4_super_block *es,
2503ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
2504ac27a0ecSDave Kleikamp {
2505ac27a0ecSDave Kleikamp 	journal_t *journal;
2506ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2507ac27a0ecSDave Kleikamp 	dev_t journal_dev;
2508ac27a0ecSDave Kleikamp 	int err = 0;
2509ac27a0ecSDave Kleikamp 	int really_read_only;
2510ac27a0ecSDave Kleikamp 
2511ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2512ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2513617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2514ac27a0ecSDave Kleikamp 			"numbers have changed\n");
2515ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
2516ac27a0ecSDave Kleikamp 	} else
2517ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2518ac27a0ecSDave Kleikamp 
2519ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
2520ac27a0ecSDave Kleikamp 
2521ac27a0ecSDave Kleikamp 	/*
2522ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
2523ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
2524ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
2525ac27a0ecSDave Kleikamp 	 */
2526ac27a0ecSDave Kleikamp 
2527617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2528ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
2529617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: INFO: recovery "
2530ac27a0ecSDave Kleikamp 					"required on readonly filesystem.\n");
2531ac27a0ecSDave Kleikamp 			if (really_read_only) {
2532617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: write access "
2533ac27a0ecSDave Kleikamp 					"unavailable, cannot proceed.\n");
2534ac27a0ecSDave Kleikamp 				return -EROFS;
2535ac27a0ecSDave Kleikamp 			}
2536617ba13bSMingming Cao 			printk (KERN_INFO "EXT4-fs: write access will "
2537ac27a0ecSDave Kleikamp 					"be enabled during recovery.\n");
2538ac27a0ecSDave Kleikamp 		}
2539ac27a0ecSDave Kleikamp 	}
2540ac27a0ecSDave Kleikamp 
2541ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
2542617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2543ac27a0ecSDave Kleikamp 		       "and inode journals!\n");
2544ac27a0ecSDave Kleikamp 		return -EINVAL;
2545ac27a0ecSDave Kleikamp 	}
2546ac27a0ecSDave Kleikamp 
2547ac27a0ecSDave Kleikamp 	if (journal_inum) {
2548617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
2549ac27a0ecSDave Kleikamp 			return -EINVAL;
2550ac27a0ecSDave Kleikamp 	} else {
2551617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2552ac27a0ecSDave Kleikamp 			return -EINVAL;
2553ac27a0ecSDave Kleikamp 	}
2554ac27a0ecSDave Kleikamp 
2555ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2556dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
2557ac27a0ecSDave Kleikamp 		if (err)  {
2558617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2559dab291afSMingming Cao 			jbd2_journal_destroy(journal);
2560ac27a0ecSDave Kleikamp 			return err;
2561ac27a0ecSDave Kleikamp 		}
2562ac27a0ecSDave Kleikamp 	}
2563ac27a0ecSDave Kleikamp 
2564617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2565dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
2566ac27a0ecSDave Kleikamp 	if (!err)
2567dab291afSMingming Cao 		err = jbd2_journal_load(journal);
2568ac27a0ecSDave Kleikamp 
2569ac27a0ecSDave Kleikamp 	if (err) {
2570617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2571dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2572ac27a0ecSDave Kleikamp 		return err;
2573ac27a0ecSDave Kleikamp 	}
2574ac27a0ecSDave Kleikamp 
2575617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2576617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
2577ac27a0ecSDave Kleikamp 
2578ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2579ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2580ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
2581ac27a0ecSDave Kleikamp 		sb->s_dirt = 1;
2582ac27a0ecSDave Kleikamp 
2583ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
2584617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2585ac27a0ecSDave Kleikamp 	}
2586ac27a0ecSDave Kleikamp 
2587ac27a0ecSDave Kleikamp 	return 0;
2588ac27a0ecSDave Kleikamp }
2589ac27a0ecSDave Kleikamp 
2590617ba13bSMingming Cao static int ext4_create_journal(struct super_block * sb,
2591617ba13bSMingming Cao 			       struct ext4_super_block * es,
2592ac27a0ecSDave Kleikamp 			       unsigned int journal_inum)
2593ac27a0ecSDave Kleikamp {
2594ac27a0ecSDave Kleikamp 	journal_t *journal;
25956c675bd4SBorislav Petkov 	int err;
2596ac27a0ecSDave Kleikamp 
2597ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY) {
2598617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2599ac27a0ecSDave Kleikamp 				"create journal.\n");
2600ac27a0ecSDave Kleikamp 		return -EROFS;
2601ac27a0ecSDave Kleikamp 	}
2602ac27a0ecSDave Kleikamp 
26036c675bd4SBorislav Petkov 	journal = ext4_get_journal(sb, journal_inum);
26046c675bd4SBorislav Petkov 	if (!journal)
2605ac27a0ecSDave Kleikamp 		return -EINVAL;
2606ac27a0ecSDave Kleikamp 
2607617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2608ac27a0ecSDave Kleikamp 	       journal_inum);
2609ac27a0ecSDave Kleikamp 
26106c675bd4SBorislav Petkov 	err = jbd2_journal_create(journal);
26116c675bd4SBorislav Petkov 	if (err) {
2612617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2613dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2614ac27a0ecSDave Kleikamp 		return -EIO;
2615ac27a0ecSDave Kleikamp 	}
2616ac27a0ecSDave Kleikamp 
2617617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2618ac27a0ecSDave Kleikamp 
2619617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
2620617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2621617ba13bSMingming Cao 	EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2622ac27a0ecSDave Kleikamp 
2623ac27a0ecSDave Kleikamp 	es->s_journal_inum = cpu_to_le32(journal_inum);
2624ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
2625ac27a0ecSDave Kleikamp 
2626ac27a0ecSDave Kleikamp 	/* Make sure we flush the recovery flag to disk. */
2627617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
2628ac27a0ecSDave Kleikamp 
2629ac27a0ecSDave Kleikamp 	return 0;
2630ac27a0ecSDave Kleikamp }
2631ac27a0ecSDave Kleikamp 
2632617ba13bSMingming Cao static void ext4_commit_super (struct super_block * sb,
2633617ba13bSMingming Cao 			       struct ext4_super_block * es,
2634ac27a0ecSDave Kleikamp 			       int sync)
2635ac27a0ecSDave Kleikamp {
2636617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2637ac27a0ecSDave Kleikamp 
2638ac27a0ecSDave Kleikamp 	if (!sbh)
2639ac27a0ecSDave Kleikamp 		return;
2640ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
2641bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2642617ba13bSMingming Cao 	es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2643ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
2644ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
2645ac27a0ecSDave Kleikamp 	if (sync)
2646ac27a0ecSDave Kleikamp 		sync_dirty_buffer(sbh);
2647ac27a0ecSDave Kleikamp }
2648ac27a0ecSDave Kleikamp 
2649ac27a0ecSDave Kleikamp 
2650ac27a0ecSDave Kleikamp /*
2651ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
2652ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
2653ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
2654ac27a0ecSDave Kleikamp  */
2655617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block * sb,
2656617ba13bSMingming Cao 					struct ext4_super_block * es)
2657ac27a0ecSDave Kleikamp {
2658617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
2659ac27a0ecSDave Kleikamp 
2660dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
2661dab291afSMingming Cao 	jbd2_journal_flush(journal);
266232c37730SJan Kara 	lock_super(sb);
2663617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2664ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
2665617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2666ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
2667617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2668ac27a0ecSDave Kleikamp 	}
266932c37730SJan Kara 	unlock_super(sb);
2670dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
2671ac27a0ecSDave Kleikamp }
2672ac27a0ecSDave Kleikamp 
2673ac27a0ecSDave Kleikamp /*
2674ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
2675ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
2676ac27a0ecSDave Kleikamp  * main filesystem now.
2677ac27a0ecSDave Kleikamp  */
2678617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block * sb,
2679617ba13bSMingming Cao 				   struct ext4_super_block * es)
2680ac27a0ecSDave Kleikamp {
2681ac27a0ecSDave Kleikamp 	journal_t *journal;
2682ac27a0ecSDave Kleikamp 	int j_errno;
2683ac27a0ecSDave Kleikamp 	const char *errstr;
2684ac27a0ecSDave Kleikamp 
2685617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2686ac27a0ecSDave Kleikamp 
2687ac27a0ecSDave Kleikamp 	/*
2688ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
2689617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
2690ac27a0ecSDave Kleikamp 	 */
2691ac27a0ecSDave Kleikamp 
2692dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
2693ac27a0ecSDave Kleikamp 	if (j_errno) {
2694ac27a0ecSDave Kleikamp 		char nbuf[16];
2695ac27a0ecSDave Kleikamp 
2696617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
2697617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
2698ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
2699617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
2700ac27a0ecSDave Kleikamp 			     "filesystem check.");
2701ac27a0ecSDave Kleikamp 
2702617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2703617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2704617ba13bSMingming Cao 		ext4_commit_super (sb, es, 1);
2705ac27a0ecSDave Kleikamp 
2706dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
2707ac27a0ecSDave Kleikamp 	}
2708ac27a0ecSDave Kleikamp }
2709ac27a0ecSDave Kleikamp 
2710ac27a0ecSDave Kleikamp /*
2711ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
2712ac27a0ecSDave Kleikamp  * and wait on the commit.
2713ac27a0ecSDave Kleikamp  */
2714617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
2715ac27a0ecSDave Kleikamp {
2716ac27a0ecSDave Kleikamp 	journal_t *journal;
2717ac27a0ecSDave Kleikamp 	int ret;
2718ac27a0ecSDave Kleikamp 
2719ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
2720ac27a0ecSDave Kleikamp 		return 0;
2721ac27a0ecSDave Kleikamp 
2722617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2723ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2724617ba13bSMingming Cao 	ret = ext4_journal_force_commit(journal);
2725ac27a0ecSDave Kleikamp 	return ret;
2726ac27a0ecSDave Kleikamp }
2727ac27a0ecSDave Kleikamp 
2728ac27a0ecSDave Kleikamp /*
2729617ba13bSMingming Cao  * Ext4 always journals updates to the superblock itself, so we don't
2730ac27a0ecSDave Kleikamp  * have to propagate any other updates to the superblock on disk at this
2731ac27a0ecSDave Kleikamp  * point.  Just start an async writeback to get the buffers on their way
2732ac27a0ecSDave Kleikamp  * to the disk.
2733ac27a0ecSDave Kleikamp  *
2734ac27a0ecSDave Kleikamp  * This implicitly triggers the writebehind on sync().
2735ac27a0ecSDave Kleikamp  */
2736ac27a0ecSDave Kleikamp 
2737617ba13bSMingming Cao static void ext4_write_super (struct super_block * sb)
2738ac27a0ecSDave Kleikamp {
2739ac27a0ecSDave Kleikamp 	if (mutex_trylock(&sb->s_lock) != 0)
2740ac27a0ecSDave Kleikamp 		BUG();
2741ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2742ac27a0ecSDave Kleikamp }
2743ac27a0ecSDave Kleikamp 
2744617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
2745ac27a0ecSDave Kleikamp {
2746ac27a0ecSDave Kleikamp 	tid_t target;
2747ac27a0ecSDave Kleikamp 
2748ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2749dab291afSMingming Cao 	if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
2750ac27a0ecSDave Kleikamp 		if (wait)
2751dab291afSMingming Cao 			jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
2752ac27a0ecSDave Kleikamp 	}
2753ac27a0ecSDave Kleikamp 	return 0;
2754ac27a0ecSDave Kleikamp }
2755ac27a0ecSDave Kleikamp 
2756ac27a0ecSDave Kleikamp /*
2757ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
2758ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
2759ac27a0ecSDave Kleikamp  */
2760617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb)
2761ac27a0ecSDave Kleikamp {
2762ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2763ac27a0ecSDave Kleikamp 
2764ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2765617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
2766ac27a0ecSDave Kleikamp 
2767ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
2768dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
2769dab291afSMingming Cao 		jbd2_journal_flush(journal);
2770ac27a0ecSDave Kleikamp 
2771ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
2772617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2773617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2774ac27a0ecSDave Kleikamp 	}
2775ac27a0ecSDave Kleikamp }
2776ac27a0ecSDave Kleikamp 
2777ac27a0ecSDave Kleikamp /*
2778ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
2779ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
2780ac27a0ecSDave Kleikamp  */
2781617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb)
2782ac27a0ecSDave Kleikamp {
2783ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2784ac27a0ecSDave Kleikamp 		lock_super(sb);
2785ac27a0ecSDave Kleikamp 		/* Reser the needs_recovery flag before the fs is unlocked. */
2786617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2787617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2788ac27a0ecSDave Kleikamp 		unlock_super(sb);
2789dab291afSMingming Cao 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2790ac27a0ecSDave Kleikamp 	}
2791ac27a0ecSDave Kleikamp }
2792ac27a0ecSDave Kleikamp 
2793617ba13bSMingming Cao static int ext4_remount (struct super_block * sb, int * flags, char * data)
2794ac27a0ecSDave Kleikamp {
2795617ba13bSMingming Cao 	struct ext4_super_block * es;
2796617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2797617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
2798ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
2799617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
2800ac27a0ecSDave Kleikamp 	int err;
2801ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2802ac27a0ecSDave Kleikamp 	int i;
2803ac27a0ecSDave Kleikamp #endif
2804ac27a0ecSDave Kleikamp 
2805ac27a0ecSDave Kleikamp 	/* Store the original options */
2806ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
2807ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
2808ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
2809ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
2810ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
2811ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2812ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2813ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2814ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2815ac27a0ecSDave Kleikamp #endif
2816ac27a0ecSDave Kleikamp 
2817ac27a0ecSDave Kleikamp 	/*
2818ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
2819ac27a0ecSDave Kleikamp 	 */
2820ac27a0ecSDave Kleikamp 	if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2821ac27a0ecSDave Kleikamp 		err = -EINVAL;
2822ac27a0ecSDave Kleikamp 		goto restore_opts;
2823ac27a0ecSDave Kleikamp 	}
2824ac27a0ecSDave Kleikamp 
2825617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2826617ba13bSMingming Cao 		ext4_abort(sb, __FUNCTION__, "Abort forced by user");
2827ac27a0ecSDave Kleikamp 
2828ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2829617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2830ac27a0ecSDave Kleikamp 
2831ac27a0ecSDave Kleikamp 	es = sbi->s_es;
2832ac27a0ecSDave Kleikamp 
2833617ba13bSMingming Cao 	ext4_init_journal_params(sb, sbi->s_journal);
2834ac27a0ecSDave Kleikamp 
2835ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2836bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
2837617ba13bSMingming Cao 		if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
2838ac27a0ecSDave Kleikamp 			err = -EROFS;
2839ac27a0ecSDave Kleikamp 			goto restore_opts;
2840ac27a0ecSDave Kleikamp 		}
2841ac27a0ecSDave Kleikamp 
2842ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
2843ac27a0ecSDave Kleikamp 			/*
2844ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
2845ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
2846ac27a0ecSDave Kleikamp 			 */
2847ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
2848ac27a0ecSDave Kleikamp 
2849ac27a0ecSDave Kleikamp 			/*
2850ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
2851ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
2852ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
2853ac27a0ecSDave Kleikamp 			 */
2854617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2855617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
2856ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
2857ac27a0ecSDave Kleikamp 
285832c37730SJan Kara 			/*
285932c37730SJan Kara 			 * We have to unlock super so that we can wait for
286032c37730SJan Kara 			 * transactions.
286132c37730SJan Kara 			 */
286232c37730SJan Kara 			unlock_super(sb);
2863617ba13bSMingming Cao 			ext4_mark_recovery_complete(sb, es);
286432c37730SJan Kara 			lock_super(sb);
2865ac27a0ecSDave Kleikamp 		} else {
2866ac27a0ecSDave Kleikamp 			__le32 ret;
2867617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2868617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2869617ba13bSMingming Cao 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2870ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
2871ac27a0ecSDave Kleikamp 				       "optional features (%x).\n",
2872ac27a0ecSDave Kleikamp 				       sb->s_id, le32_to_cpu(ret));
2873ac27a0ecSDave Kleikamp 				err = -EROFS;
2874ac27a0ecSDave Kleikamp 				goto restore_opts;
2875ac27a0ecSDave Kleikamp 			}
2876ead6596bSEric Sandeen 
2877ead6596bSEric Sandeen 			/*
2878ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
2879ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
2880ead6596bSEric Sandeen 			 * require a full umount/remount for now.
2881ead6596bSEric Sandeen 			 */
2882ead6596bSEric Sandeen 			if (es->s_last_orphan) {
2883ead6596bSEric Sandeen 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2884ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
2885ead6596bSEric Sandeen 				       "orphan inode list.  Please "
2886ead6596bSEric Sandeen 				       "umount/remount instead.\n",
2887ead6596bSEric Sandeen 				       sb->s_id);
2888ead6596bSEric Sandeen 				err = -EINVAL;
2889ead6596bSEric Sandeen 				goto restore_opts;
2890ead6596bSEric Sandeen 			}
2891ead6596bSEric Sandeen 
2892ac27a0ecSDave Kleikamp 			/*
2893ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
2894ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
2895ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
2896ac27a0ecSDave Kleikamp 			 * the partition.)
2897ac27a0ecSDave Kleikamp 			 */
2898617ba13bSMingming Cao 			ext4_clear_journal_err(sb, es);
2899ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
2900617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
2901ac27a0ecSDave Kleikamp 				goto restore_opts;
2902617ba13bSMingming Cao 			if (!ext4_setup_super (sb, es, 0))
2903ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
2904ac27a0ecSDave Kleikamp 		}
2905ac27a0ecSDave Kleikamp 	}
2906ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2907ac27a0ecSDave Kleikamp 	/* Release old quota file names */
2908ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2909ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
2910ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2911ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
2912ac27a0ecSDave Kleikamp #endif
2913ac27a0ecSDave Kleikamp 	return 0;
2914ac27a0ecSDave Kleikamp restore_opts:
2915ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
2916ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
2917ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
2918ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
2919ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
2920ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2921ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2922ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2923ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
2924ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2925ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
2926ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2927ac27a0ecSDave Kleikamp 	}
2928ac27a0ecSDave Kleikamp #endif
2929ac27a0ecSDave Kleikamp 	return err;
2930ac27a0ecSDave Kleikamp }
2931ac27a0ecSDave Kleikamp 
2932617ba13bSMingming Cao static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
2933ac27a0ecSDave Kleikamp {
2934ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
2935617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2936617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
2937960cc398SPekka Enberg 	u64 fsid;
2938ac27a0ecSDave Kleikamp 
29395e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
29405e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
29416bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
2942fd2d4291SAvantika Mathur 		ext4_group_t ngroups = sbi->s_groups_count, i;
29435e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
2944ac27a0ecSDave Kleikamp 		smp_rmb();
2945ac27a0ecSDave Kleikamp 
2946ac27a0ecSDave Kleikamp 		/*
29475e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
29485e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
29495e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
2950ac27a0ecSDave Kleikamp 		 */
2951ac27a0ecSDave Kleikamp 
2952ac27a0ecSDave Kleikamp 		/*
2953ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
2954ac27a0ecSDave Kleikamp 		 * overhead
2955ac27a0ecSDave Kleikamp 		 */
2956ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
2957ac27a0ecSDave Kleikamp 
2958ac27a0ecSDave Kleikamp 		/*
2959ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
2960ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
2961ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
2962ac27a0ecSDave Kleikamp 		 */
2963ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
2964617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
2965617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
2966ac27a0ecSDave Kleikamp 			cond_resched();
2967ac27a0ecSDave Kleikamp 		}
2968ac27a0ecSDave Kleikamp 
2969ac27a0ecSDave Kleikamp 		/*
2970ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
2971ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
2972ac27a0ecSDave Kleikamp 		 */
29735e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
29745e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
29755e70030dSBadari Pulavarty 		smp_wmb();
29766bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
2977ac27a0ecSDave Kleikamp 	}
2978ac27a0ecSDave Kleikamp 
2979617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
2980ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
29815e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
298252d9f3b4SPeter Zijlstra 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
2983308ba3ecSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, buf->f_bfree);
2984bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2985bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
2986ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
2987ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
298852d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
29895e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
2990617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
2991960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
2992960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2993960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2994960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
2995ac27a0ecSDave Kleikamp 	return 0;
2996ac27a0ecSDave Kleikamp }
2997ac27a0ecSDave Kleikamp 
2998ac27a0ecSDave Kleikamp /* Helper function for writing quotas on sync - we need to start transaction before quota file
2999ac27a0ecSDave Kleikamp  * is locked for write. Otherwise the are possible deadlocks:
3000ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3001617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3002dab291afSMingming Cao  *   jbd2_journal_start()                   write_dquot()
3003ac27a0ecSDave Kleikamp  *   DQUOT_INIT()                        down(dqio_mutex)
3004dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3005ac27a0ecSDave Kleikamp  *
3006ac27a0ecSDave Kleikamp  */
3007ac27a0ecSDave Kleikamp 
3008ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3009ac27a0ecSDave Kleikamp 
3010ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3011ac27a0ecSDave Kleikamp {
3012ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3013ac27a0ecSDave Kleikamp }
3014ac27a0ecSDave Kleikamp 
3015617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type)
3016ac27a0ecSDave Kleikamp {
3017ac27a0ecSDave Kleikamp 	handle_t *handle;
3018ac27a0ecSDave Kleikamp 	int ret, err;
3019ac27a0ecSDave Kleikamp 
3020ac27a0ecSDave Kleikamp 	/* We may create quota structure so we need to reserve enough blocks */
3021617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3022ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3023ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3024ac27a0ecSDave Kleikamp 	ret = dquot_initialize(inode, type);
3025617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3026ac27a0ecSDave Kleikamp 	if (!ret)
3027ac27a0ecSDave Kleikamp 		ret = err;
3028ac27a0ecSDave Kleikamp 	return ret;
3029ac27a0ecSDave Kleikamp }
3030ac27a0ecSDave Kleikamp 
3031617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode)
3032ac27a0ecSDave Kleikamp {
3033ac27a0ecSDave Kleikamp 	handle_t *handle;
3034ac27a0ecSDave Kleikamp 	int ret, err;
3035ac27a0ecSDave Kleikamp 
3036ac27a0ecSDave Kleikamp 	/* We may delete quota structure so we need to reserve enough blocks */
3037617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
3038ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3039ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3040ac27a0ecSDave Kleikamp 	ret = dquot_drop(inode);
3041617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3042ac27a0ecSDave Kleikamp 	if (!ret)
3043ac27a0ecSDave Kleikamp 		ret = err;
3044ac27a0ecSDave Kleikamp 	return ret;
3045ac27a0ecSDave Kleikamp }
3046ac27a0ecSDave Kleikamp 
3047617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3048ac27a0ecSDave Kleikamp {
3049ac27a0ecSDave Kleikamp 	int ret, err;
3050ac27a0ecSDave Kleikamp 	handle_t *handle;
3051ac27a0ecSDave Kleikamp 	struct inode *inode;
3052ac27a0ecSDave Kleikamp 
3053ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3054617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3055617ba13bSMingming Cao 					EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3056ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3057ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3058ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3059617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3060ac27a0ecSDave Kleikamp 	if (!ret)
3061ac27a0ecSDave Kleikamp 		ret = err;
3062ac27a0ecSDave Kleikamp 	return ret;
3063ac27a0ecSDave Kleikamp }
3064ac27a0ecSDave Kleikamp 
3065617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3066ac27a0ecSDave Kleikamp {
3067ac27a0ecSDave Kleikamp 	int ret, err;
3068ac27a0ecSDave Kleikamp 	handle_t *handle;
3069ac27a0ecSDave Kleikamp 
3070617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3071617ba13bSMingming Cao 					EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3072ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3073ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3074ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3075617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3076ac27a0ecSDave Kleikamp 	if (!ret)
3077ac27a0ecSDave Kleikamp 		ret = err;
3078ac27a0ecSDave Kleikamp 	return ret;
3079ac27a0ecSDave Kleikamp }
3080ac27a0ecSDave Kleikamp 
3081617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3082ac27a0ecSDave Kleikamp {
3083ac27a0ecSDave Kleikamp 	int ret, err;
3084ac27a0ecSDave Kleikamp 	handle_t *handle;
3085ac27a0ecSDave Kleikamp 
3086617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3087617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
30889c3013e9SJan Kara 	if (IS_ERR(handle)) {
30899c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
30909c3013e9SJan Kara 		dquot_release(dquot);
3091ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
30929c3013e9SJan Kara 	}
3093ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3094617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3095ac27a0ecSDave Kleikamp 	if (!ret)
3096ac27a0ecSDave Kleikamp 		ret = err;
3097ac27a0ecSDave Kleikamp 	return ret;
3098ac27a0ecSDave Kleikamp }
3099ac27a0ecSDave Kleikamp 
3100617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3101ac27a0ecSDave Kleikamp {
3102ac27a0ecSDave Kleikamp 	/* Are we journalling quotas? */
3103617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3104617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3105ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3106617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3107ac27a0ecSDave Kleikamp 	} else {
3108ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3109ac27a0ecSDave Kleikamp 	}
3110ac27a0ecSDave Kleikamp }
3111ac27a0ecSDave Kleikamp 
3112617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3113ac27a0ecSDave Kleikamp {
3114ac27a0ecSDave Kleikamp 	int ret, err;
3115ac27a0ecSDave Kleikamp 	handle_t *handle;
3116ac27a0ecSDave Kleikamp 
3117ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3118617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3119ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3120ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3121ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3122617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3123ac27a0ecSDave Kleikamp 	if (!ret)
3124ac27a0ecSDave Kleikamp 		ret = err;
3125ac27a0ecSDave Kleikamp 	return ret;
3126ac27a0ecSDave Kleikamp }
3127ac27a0ecSDave Kleikamp 
3128ac27a0ecSDave Kleikamp /*
3129ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3130ac27a0ecSDave Kleikamp  * the quota file and such...
3131ac27a0ecSDave Kleikamp  */
3132617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3133ac27a0ecSDave Kleikamp {
3134617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3135617ba13bSMingming Cao 			EXT4_SB(sb)->s_jquota_fmt, type);
3136ac27a0ecSDave Kleikamp }
3137ac27a0ecSDave Kleikamp 
3138ac27a0ecSDave Kleikamp /*
3139ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3140ac27a0ecSDave Kleikamp  */
3141617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3142ac27a0ecSDave Kleikamp 			 char *path)
3143ac27a0ecSDave Kleikamp {
3144ac27a0ecSDave Kleikamp 	int err;
3145ac27a0ecSDave Kleikamp 	struct nameidata nd;
3146ac27a0ecSDave Kleikamp 
3147ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3148ac27a0ecSDave Kleikamp 		return -EINVAL;
3149ac27a0ecSDave Kleikamp 	/* Not journalling quota? */
3150617ba13bSMingming Cao 	if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
3151617ba13bSMingming Cao 	    !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
3152ac27a0ecSDave Kleikamp 		return vfs_quota_on(sb, type, format_id, path);
3153ac27a0ecSDave Kleikamp 	err = path_lookup(path, LOOKUP_FOLLOW, &nd);
3154ac27a0ecSDave Kleikamp 	if (err)
3155ac27a0ecSDave Kleikamp 		return err;
3156ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
3157ac27a0ecSDave Kleikamp 	if (nd.mnt->mnt_sb != sb) {
3158ac27a0ecSDave Kleikamp 		path_release(&nd);
3159ac27a0ecSDave Kleikamp 		return -EXDEV;
3160ac27a0ecSDave Kleikamp 	}
3161ac27a0ecSDave Kleikamp 	/* Quotafile not of fs root? */
3162ac27a0ecSDave Kleikamp 	if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
3163ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
3164617ba13bSMingming Cao 			"EXT4-fs: Quota file not on filesystem root. "
3165ac27a0ecSDave Kleikamp 			"Journalled quota will not work.\n");
3166ac27a0ecSDave Kleikamp 	path_release(&nd);
3167ac27a0ecSDave Kleikamp 	return vfs_quota_on(sb, type, format_id, path);
3168ac27a0ecSDave Kleikamp }
3169ac27a0ecSDave Kleikamp 
3170ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3171ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3172ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3173ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3174617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3175ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3176ac27a0ecSDave Kleikamp {
3177ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3178725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3179ac27a0ecSDave Kleikamp 	int err = 0;
3180ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3181ac27a0ecSDave Kleikamp 	int tocopy;
3182ac27a0ecSDave Kleikamp 	size_t toread;
3183ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3184ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3185ac27a0ecSDave Kleikamp 
3186ac27a0ecSDave Kleikamp 	if (off > i_size)
3187ac27a0ecSDave Kleikamp 		return 0;
3188ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3189ac27a0ecSDave Kleikamp 		len = i_size-off;
3190ac27a0ecSDave Kleikamp 	toread = len;
3191ac27a0ecSDave Kleikamp 	while (toread > 0) {
3192ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3193ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3194617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3195ac27a0ecSDave Kleikamp 		if (err)
3196ac27a0ecSDave Kleikamp 			return err;
3197ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3198ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3199ac27a0ecSDave Kleikamp 		else
3200ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3201ac27a0ecSDave Kleikamp 		brelse(bh);
3202ac27a0ecSDave Kleikamp 		offset = 0;
3203ac27a0ecSDave Kleikamp 		toread -= tocopy;
3204ac27a0ecSDave Kleikamp 		data += tocopy;
3205ac27a0ecSDave Kleikamp 		blk++;
3206ac27a0ecSDave Kleikamp 	}
3207ac27a0ecSDave Kleikamp 	return len;
3208ac27a0ecSDave Kleikamp }
3209ac27a0ecSDave Kleikamp 
3210ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3211ac27a0ecSDave Kleikamp  * enough credits) */
3212617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3213ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3214ac27a0ecSDave Kleikamp {
3215ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3216725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3217ac27a0ecSDave Kleikamp 	int err = 0;
3218ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3219ac27a0ecSDave Kleikamp 	int tocopy;
3220617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3221ac27a0ecSDave Kleikamp 	size_t towrite = len;
3222ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3223ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3224ac27a0ecSDave Kleikamp 
32259c3013e9SJan Kara 	if (!handle) {
32269c3013e9SJan Kara 		printk(KERN_WARNING "EXT4-fs: Quota write (off=%Lu, len=%Lu)"
32279c3013e9SJan Kara 			" cancelled because transaction is not started.\n",
32289c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
32299c3013e9SJan Kara 		return -EIO;
32309c3013e9SJan Kara 	}
3231ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3232ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3233ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3234ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3235617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3236ac27a0ecSDave Kleikamp 		if (!bh)
3237ac27a0ecSDave Kleikamp 			goto out;
3238ac27a0ecSDave Kleikamp 		if (journal_quota) {
3239617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3240ac27a0ecSDave Kleikamp 			if (err) {
3241ac27a0ecSDave Kleikamp 				brelse(bh);
3242ac27a0ecSDave Kleikamp 				goto out;
3243ac27a0ecSDave Kleikamp 			}
3244ac27a0ecSDave Kleikamp 		}
3245ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3246ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3247ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3248ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3249ac27a0ecSDave Kleikamp 		if (journal_quota)
3250617ba13bSMingming Cao 			err = ext4_journal_dirty_metadata(handle, bh);
3251ac27a0ecSDave Kleikamp 		else {
3252ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3253617ba13bSMingming Cao 			err = ext4_journal_dirty_data(handle, bh);
3254ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3255ac27a0ecSDave Kleikamp 		}
3256ac27a0ecSDave Kleikamp 		brelse(bh);
3257ac27a0ecSDave Kleikamp 		if (err)
3258ac27a0ecSDave Kleikamp 			goto out;
3259ac27a0ecSDave Kleikamp 		offset = 0;
3260ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3261ac27a0ecSDave Kleikamp 		data += tocopy;
3262ac27a0ecSDave Kleikamp 		blk++;
3263ac27a0ecSDave Kleikamp 	}
3264ac27a0ecSDave Kleikamp out:
3265ac27a0ecSDave Kleikamp 	if (len == towrite)
3266ac27a0ecSDave Kleikamp 		return err;
3267ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3268ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3269617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3270ac27a0ecSDave Kleikamp 	}
3271ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3272617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3273ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3274ac27a0ecSDave Kleikamp 	return len - towrite;
3275ac27a0ecSDave Kleikamp }
3276ac27a0ecSDave Kleikamp 
3277ac27a0ecSDave Kleikamp #endif
3278ac27a0ecSDave Kleikamp 
3279617ba13bSMingming Cao static int ext4_get_sb(struct file_system_type *fs_type,
3280ac27a0ecSDave Kleikamp 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3281ac27a0ecSDave Kleikamp {
3282617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3283ac27a0ecSDave Kleikamp }
3284ac27a0ecSDave Kleikamp 
3285617ba13bSMingming Cao static struct file_system_type ext4dev_fs_type = {
3286ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
3287617ba13bSMingming Cao 	.name		= "ext4dev",
3288617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
3289ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
3290ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
3291ac27a0ecSDave Kleikamp };
3292ac27a0ecSDave Kleikamp 
3293617ba13bSMingming Cao static int __init init_ext4_fs(void)
3294ac27a0ecSDave Kleikamp {
3295c9de560dSAlex Tomas 	int err;
3296c9de560dSAlex Tomas 
3297c9de560dSAlex Tomas 	err = init_ext4_mballoc();
3298ac27a0ecSDave Kleikamp 	if (err)
3299ac27a0ecSDave Kleikamp 		return err;
3300c9de560dSAlex Tomas 
3301c9de560dSAlex Tomas 	err = init_ext4_xattr();
3302c9de560dSAlex Tomas 	if (err)
3303c9de560dSAlex Tomas 		goto out2;
3304ac27a0ecSDave Kleikamp 	err = init_inodecache();
3305ac27a0ecSDave Kleikamp 	if (err)
3306ac27a0ecSDave Kleikamp 		goto out1;
3307617ba13bSMingming Cao 	err = register_filesystem(&ext4dev_fs_type);
3308ac27a0ecSDave Kleikamp 	if (err)
3309ac27a0ecSDave Kleikamp 		goto out;
3310ac27a0ecSDave Kleikamp 	return 0;
3311ac27a0ecSDave Kleikamp out:
3312ac27a0ecSDave Kleikamp 	destroy_inodecache();
3313ac27a0ecSDave Kleikamp out1:
3314617ba13bSMingming Cao 	exit_ext4_xattr();
3315c9de560dSAlex Tomas out2:
3316c9de560dSAlex Tomas 	exit_ext4_mballoc();
3317ac27a0ecSDave Kleikamp 	return err;
3318ac27a0ecSDave Kleikamp }
3319ac27a0ecSDave Kleikamp 
3320617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
3321ac27a0ecSDave Kleikamp {
3322617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
3323ac27a0ecSDave Kleikamp 	destroy_inodecache();
3324617ba13bSMingming Cao 	exit_ext4_xattr();
3325c9de560dSAlex Tomas 	exit_ext4_mballoc();
3326ac27a0ecSDave Kleikamp }
3327ac27a0ecSDave Kleikamp 
3328ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3329617ba13bSMingming Cao MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3330ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
3331617ba13bSMingming Cao module_init(init_ext4_fs)
3332617ba13bSMingming Cao module_exit(exit_ext4_fs)
3333