xref: /linux/fs/ext4/super.c (revision cd02ff0b14b7e27cbdfd7ff2814b08199a2b7168)
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>
40ac27a0ecSDave Kleikamp 
41ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
42ac27a0ecSDave Kleikamp 
43ac27a0ecSDave Kleikamp #include "xattr.h"
44ac27a0ecSDave Kleikamp #include "acl.h"
45ac27a0ecSDave Kleikamp #include "namei.h"
46ac27a0ecSDave Kleikamp 
47617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
48ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
49617ba13bSMingming Cao static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
50ac27a0ecSDave Kleikamp 			       unsigned int);
51617ba13bSMingming Cao static void ext4_commit_super (struct super_block * sb,
52617ba13bSMingming Cao 			       struct ext4_super_block * es,
53ac27a0ecSDave Kleikamp 			       int sync);
54617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block * sb,
55617ba13bSMingming Cao 					struct ext4_super_block * es);
56617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block * sb,
57617ba13bSMingming Cao 				   struct ext4_super_block * es);
58617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
59617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block * sb, int errno,
60ac27a0ecSDave Kleikamp 				     char nbuf[16]);
61617ba13bSMingming Cao static int ext4_remount (struct super_block * sb, int * flags, char * data);
62617ba13bSMingming Cao static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
63617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb);
64617ba13bSMingming Cao static void ext4_write_super (struct super_block * sb);
65617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb);
66ac27a0ecSDave Kleikamp 
67bd81d8eeSLaurent Vivier 
688fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
698fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
70bd81d8eeSLaurent Vivier {
71bd81d8eeSLaurent Vivier 	return le32_to_cpu(bg->bg_block_bitmap) |
728fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
738fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
74bd81d8eeSLaurent Vivier }
75bd81d8eeSLaurent Vivier 
768fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
778fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
78bd81d8eeSLaurent Vivier {
79bd81d8eeSLaurent Vivier 	return le32_to_cpu(bg->bg_inode_bitmap) |
808fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
818fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
82bd81d8eeSLaurent Vivier }
83bd81d8eeSLaurent Vivier 
848fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
858fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
86bd81d8eeSLaurent Vivier {
87bd81d8eeSLaurent Vivier 	return le32_to_cpu(bg->bg_inode_table) |
888fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
898fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
90bd81d8eeSLaurent Vivier }
91bd81d8eeSLaurent Vivier 
928fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
938fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
94bd81d8eeSLaurent Vivier {
95bd81d8eeSLaurent Vivier 	bg->bg_block_bitmap = cpu_to_le32((u32)blk);
968fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
978fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
98bd81d8eeSLaurent Vivier }
99bd81d8eeSLaurent Vivier 
1008fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1018fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
102bd81d8eeSLaurent Vivier {
103bd81d8eeSLaurent Vivier 	bg->bg_inode_bitmap  = cpu_to_le32((u32)blk);
1048fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1058fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
106bd81d8eeSLaurent Vivier }
107bd81d8eeSLaurent Vivier 
1088fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1098fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
110bd81d8eeSLaurent Vivier {
111bd81d8eeSLaurent Vivier 	bg->bg_inode_table = cpu_to_le32((u32)blk);
1128fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1138fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
114bd81d8eeSLaurent Vivier }
115bd81d8eeSLaurent Vivier 
116ac27a0ecSDave Kleikamp /*
117dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
118ac27a0ecSDave Kleikamp  *
119ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
120ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
121ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
122ac27a0ecSDave Kleikamp  * appropriate.
123ac27a0ecSDave Kleikamp  */
124617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
125ac27a0ecSDave Kleikamp {
126ac27a0ecSDave Kleikamp 	journal_t *journal;
127ac27a0ecSDave Kleikamp 
128ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
129ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
130ac27a0ecSDave Kleikamp 
131ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
132ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
133ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
134617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
135ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
136617ba13bSMingming Cao 		ext4_abort(sb, __FUNCTION__,
137ac27a0ecSDave Kleikamp 			   "Detected aborted journal");
138ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
139ac27a0ecSDave Kleikamp 	}
140ac27a0ecSDave Kleikamp 
141dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
142ac27a0ecSDave Kleikamp }
143ac27a0ecSDave Kleikamp 
144ac27a0ecSDave Kleikamp /*
145ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
146dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
147ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
148ac27a0ecSDave Kleikamp  * appropriate.
149ac27a0ecSDave Kleikamp  */
150617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
151ac27a0ecSDave Kleikamp {
152ac27a0ecSDave Kleikamp 	struct super_block *sb;
153ac27a0ecSDave Kleikamp 	int err;
154ac27a0ecSDave Kleikamp 	int rc;
155ac27a0ecSDave Kleikamp 
156ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
157ac27a0ecSDave Kleikamp 	err = handle->h_err;
158dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
159ac27a0ecSDave Kleikamp 
160ac27a0ecSDave Kleikamp 	if (!err)
161ac27a0ecSDave Kleikamp 		err = rc;
162ac27a0ecSDave Kleikamp 	if (err)
163617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
164ac27a0ecSDave Kleikamp 	return err;
165ac27a0ecSDave Kleikamp }
166ac27a0ecSDave Kleikamp 
167617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
168ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
169ac27a0ecSDave Kleikamp {
170ac27a0ecSDave Kleikamp 	char nbuf[16];
171617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
172ac27a0ecSDave Kleikamp 
173ac27a0ecSDave Kleikamp 	if (bh)
174ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
175ac27a0ecSDave Kleikamp 
176ac27a0ecSDave Kleikamp 	if (!handle->h_err)
177ac27a0ecSDave Kleikamp 		handle->h_err = err;
178ac27a0ecSDave Kleikamp 
179ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
180ac27a0ecSDave Kleikamp 		return;
181ac27a0ecSDave Kleikamp 
182ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
183ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
184ac27a0ecSDave Kleikamp 
185dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
186ac27a0ecSDave Kleikamp }
187ac27a0ecSDave Kleikamp 
188ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
189ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
190ac27a0ecSDave Kleikamp  *
191ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
192617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
193ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
194ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
195ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
196ac27a0ecSDave Kleikamp  * write out the superblock safely.
197ac27a0ecSDave Kleikamp  *
198dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
199ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
200ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
201ac27a0ecSDave Kleikamp  */
202ac27a0ecSDave Kleikamp 
203617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
204ac27a0ecSDave Kleikamp {
205617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
206ac27a0ecSDave Kleikamp 
207617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
208617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
209ac27a0ecSDave Kleikamp 
210ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
211ac27a0ecSDave Kleikamp 		return;
212ac27a0ecSDave Kleikamp 
213ac27a0ecSDave Kleikamp 	if (!test_opt (sb, ERRORS_CONT)) {
214617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
215ac27a0ecSDave Kleikamp 
216617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
217ac27a0ecSDave Kleikamp 		if (journal)
218dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
219ac27a0ecSDave Kleikamp 	}
220ac27a0ecSDave Kleikamp 	if (test_opt (sb, ERRORS_RO)) {
221ac27a0ecSDave Kleikamp 		printk (KERN_CRIT "Remounting filesystem read-only\n");
222ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
223ac27a0ecSDave Kleikamp 	}
224617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
225ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
226617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
227ac27a0ecSDave Kleikamp 			sb->s_id);
228ac27a0ecSDave Kleikamp }
229ac27a0ecSDave Kleikamp 
230617ba13bSMingming Cao void ext4_error (struct super_block * sb, const char * function,
231ac27a0ecSDave Kleikamp 		 const char * fmt, ...)
232ac27a0ecSDave Kleikamp {
233ac27a0ecSDave Kleikamp 	va_list args;
234ac27a0ecSDave Kleikamp 
235ac27a0ecSDave Kleikamp 	va_start(args, fmt);
236617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
237ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
238ac27a0ecSDave Kleikamp 	printk("\n");
239ac27a0ecSDave Kleikamp 	va_end(args);
240ac27a0ecSDave Kleikamp 
241617ba13bSMingming Cao 	ext4_handle_error(sb);
242ac27a0ecSDave Kleikamp }
243ac27a0ecSDave Kleikamp 
244617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block * sb, int errno,
245ac27a0ecSDave Kleikamp 				     char nbuf[16])
246ac27a0ecSDave Kleikamp {
247ac27a0ecSDave Kleikamp 	char *errstr = NULL;
248ac27a0ecSDave Kleikamp 
249ac27a0ecSDave Kleikamp 	switch (errno) {
250ac27a0ecSDave Kleikamp 	case -EIO:
251ac27a0ecSDave Kleikamp 		errstr = "IO failure";
252ac27a0ecSDave Kleikamp 		break;
253ac27a0ecSDave Kleikamp 	case -ENOMEM:
254ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
255ac27a0ecSDave Kleikamp 		break;
256ac27a0ecSDave Kleikamp 	case -EROFS:
257dab291afSMingming Cao 		if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
258ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
259ac27a0ecSDave Kleikamp 		else
260ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
261ac27a0ecSDave Kleikamp 		break;
262ac27a0ecSDave Kleikamp 	default:
263ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
264ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
265ac27a0ecSDave Kleikamp 		 * NULL. */
266ac27a0ecSDave Kleikamp 		if (nbuf) {
267ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
268ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
269ac27a0ecSDave Kleikamp 				errstr = nbuf;
270ac27a0ecSDave Kleikamp 		}
271ac27a0ecSDave Kleikamp 		break;
272ac27a0ecSDave Kleikamp 	}
273ac27a0ecSDave Kleikamp 
274ac27a0ecSDave Kleikamp 	return errstr;
275ac27a0ecSDave Kleikamp }
276ac27a0ecSDave Kleikamp 
277617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
278ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
279ac27a0ecSDave Kleikamp 
280617ba13bSMingming Cao void __ext4_std_error (struct super_block * sb, const char * function,
281ac27a0ecSDave Kleikamp 		       int errno)
282ac27a0ecSDave Kleikamp {
283ac27a0ecSDave Kleikamp 	char nbuf[16];
284ac27a0ecSDave Kleikamp 	const char *errstr;
285ac27a0ecSDave Kleikamp 
286ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
287ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
288ac27a0ecSDave Kleikamp 	 * an error. */
289ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
290ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
291ac27a0ecSDave Kleikamp 		return;
292ac27a0ecSDave Kleikamp 
293617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
294617ba13bSMingming Cao 	printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
295ac27a0ecSDave Kleikamp 		sb->s_id, function, errstr);
296ac27a0ecSDave Kleikamp 
297617ba13bSMingming Cao 	ext4_handle_error(sb);
298ac27a0ecSDave Kleikamp }
299ac27a0ecSDave Kleikamp 
300ac27a0ecSDave Kleikamp /*
301617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
302ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
303ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
304ac27a0ecSDave Kleikamp  *
305ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
306ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
307ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
308ac27a0ecSDave Kleikamp  */
309ac27a0ecSDave Kleikamp 
310617ba13bSMingming Cao void ext4_abort (struct super_block * sb, const char * function,
311ac27a0ecSDave Kleikamp 		 const char * fmt, ...)
312ac27a0ecSDave Kleikamp {
313ac27a0ecSDave Kleikamp 	va_list args;
314ac27a0ecSDave Kleikamp 
315617ba13bSMingming Cao 	printk (KERN_CRIT "ext4_abort called.\n");
316ac27a0ecSDave Kleikamp 
317ac27a0ecSDave Kleikamp 	va_start(args, fmt);
318617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
319ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
320ac27a0ecSDave Kleikamp 	printk("\n");
321ac27a0ecSDave Kleikamp 	va_end(args);
322ac27a0ecSDave Kleikamp 
323ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
324617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
325ac27a0ecSDave Kleikamp 
326ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
327ac27a0ecSDave Kleikamp 		return;
328ac27a0ecSDave Kleikamp 
329ac27a0ecSDave Kleikamp 	printk(KERN_CRIT "Remounting filesystem read-only\n");
330617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
331ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
332617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
333dab291afSMingming Cao 	jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
334ac27a0ecSDave Kleikamp }
335ac27a0ecSDave Kleikamp 
336617ba13bSMingming Cao void ext4_warning (struct super_block * sb, const char * function,
337ac27a0ecSDave Kleikamp 		   const char * fmt, ...)
338ac27a0ecSDave Kleikamp {
339ac27a0ecSDave Kleikamp 	va_list args;
340ac27a0ecSDave Kleikamp 
341ac27a0ecSDave Kleikamp 	va_start(args, fmt);
342617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
343ac27a0ecSDave Kleikamp 	       sb->s_id, function);
344ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
345ac27a0ecSDave Kleikamp 	printk("\n");
346ac27a0ecSDave Kleikamp 	va_end(args);
347ac27a0ecSDave Kleikamp }
348ac27a0ecSDave Kleikamp 
349617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
350ac27a0ecSDave Kleikamp {
351617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
352ac27a0ecSDave Kleikamp 
353617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
354ac27a0ecSDave Kleikamp 		return;
355ac27a0ecSDave Kleikamp 
356617ba13bSMingming Cao 	ext4_warning(sb, __FUNCTION__,
357ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
358ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
359617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
360ac27a0ecSDave Kleikamp 
361617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
362617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
363617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
364ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
365ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
366ac27a0ecSDave Kleikamp 
367ac27a0ecSDave Kleikamp 	/*
368ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
369ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
370ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
371ac27a0ecSDave Kleikamp 	 */
372ac27a0ecSDave Kleikamp }
373ac27a0ecSDave Kleikamp 
374ac27a0ecSDave Kleikamp /*
375ac27a0ecSDave Kleikamp  * Open the external journal device
376ac27a0ecSDave Kleikamp  */
377617ba13bSMingming Cao static struct block_device *ext4_blkdev_get(dev_t dev)
378ac27a0ecSDave Kleikamp {
379ac27a0ecSDave Kleikamp 	struct block_device *bdev;
380ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
381ac27a0ecSDave Kleikamp 
382ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
383ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
384ac27a0ecSDave Kleikamp 		goto fail;
385ac27a0ecSDave Kleikamp 	return bdev;
386ac27a0ecSDave Kleikamp 
387ac27a0ecSDave Kleikamp fail:
388617ba13bSMingming Cao 	printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
389ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
390ac27a0ecSDave Kleikamp 	return NULL;
391ac27a0ecSDave Kleikamp }
392ac27a0ecSDave Kleikamp 
393ac27a0ecSDave Kleikamp /*
394ac27a0ecSDave Kleikamp  * Release the journal device
395ac27a0ecSDave Kleikamp  */
396617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
397ac27a0ecSDave Kleikamp {
398ac27a0ecSDave Kleikamp 	bd_release(bdev);
399ac27a0ecSDave Kleikamp 	return blkdev_put(bdev);
400ac27a0ecSDave Kleikamp }
401ac27a0ecSDave Kleikamp 
402617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
403ac27a0ecSDave Kleikamp {
404ac27a0ecSDave Kleikamp 	struct block_device *bdev;
405ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
406ac27a0ecSDave Kleikamp 
407ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
408ac27a0ecSDave Kleikamp 	if (bdev) {
409617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
410ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
411ac27a0ecSDave Kleikamp 	}
412ac27a0ecSDave Kleikamp 	return ret;
413ac27a0ecSDave Kleikamp }
414ac27a0ecSDave Kleikamp 
415ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
416ac27a0ecSDave Kleikamp {
417617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
418ac27a0ecSDave Kleikamp }
419ac27a0ecSDave Kleikamp 
420617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
421ac27a0ecSDave Kleikamp {
422ac27a0ecSDave Kleikamp 	struct list_head *l;
423ac27a0ecSDave Kleikamp 
424ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb orphan head is %d\n",
425ac27a0ecSDave Kleikamp 	       le32_to_cpu(sbi->s_es->s_last_orphan));
426ac27a0ecSDave Kleikamp 
427ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
428ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
429ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
430ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
431ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
432ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
433ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
434ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
435ac27a0ecSDave Kleikamp 	}
436ac27a0ecSDave Kleikamp }
437ac27a0ecSDave Kleikamp 
438617ba13bSMingming Cao static void ext4_put_super (struct super_block * sb)
439ac27a0ecSDave Kleikamp {
440617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
441617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
442ac27a0ecSDave Kleikamp 	int i;
443ac27a0ecSDave Kleikamp 
444a86c6181SAlex Tomas 	ext4_ext_release(sb);
445617ba13bSMingming Cao 	ext4_xattr_put_super(sb);
446dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
447ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
448617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
449ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
450ac27a0ecSDave Kleikamp 		BUFFER_TRACE(sbi->s_sbh, "marking dirty");
451ac27a0ecSDave Kleikamp 		mark_buffer_dirty(sbi->s_sbh);
452617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
453ac27a0ecSDave Kleikamp 	}
454ac27a0ecSDave Kleikamp 
455ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
456ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
457ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
458ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
459ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
460ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
461ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
462ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
463ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
464ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
465ac27a0ecSDave Kleikamp #endif
466ac27a0ecSDave Kleikamp 
467ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
468ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
469ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
470ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
471ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
472ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
473ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
474ac27a0ecSDave Kleikamp 
475f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
476ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
477ac27a0ecSDave Kleikamp 		/*
478ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
479ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
480ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
481ac27a0ecSDave Kleikamp 		 */
482ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
483f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
484617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
485ac27a0ecSDave Kleikamp 	}
486ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
487ac27a0ecSDave Kleikamp 	kfree(sbi);
488ac27a0ecSDave Kleikamp 	return;
489ac27a0ecSDave Kleikamp }
490ac27a0ecSDave Kleikamp 
491e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
492ac27a0ecSDave Kleikamp 
493ac27a0ecSDave Kleikamp /*
494ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
495ac27a0ecSDave Kleikamp  */
496617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
497ac27a0ecSDave Kleikamp {
498617ba13bSMingming Cao 	struct ext4_inode_info *ei;
499ac27a0ecSDave Kleikamp 
500e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
501ac27a0ecSDave Kleikamp 	if (!ei)
502ac27a0ecSDave Kleikamp 		return NULL;
503617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
504617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
505617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
506ac27a0ecSDave Kleikamp #endif
507ac27a0ecSDave Kleikamp 	ei->i_block_alloc_info = NULL;
508ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
509a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
510ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
511ac27a0ecSDave Kleikamp }
512ac27a0ecSDave Kleikamp 
513617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
514ac27a0ecSDave Kleikamp {
5159f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
5169f7dd93dSVasily Averin 		printk("EXT4 Inode %p: orphan list check failed!\n",
5179f7dd93dSVasily Averin 			EXT4_I(inode));
5189f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
5199f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
5209f7dd93dSVasily Averin 				true);
5219f7dd93dSVasily Averin 		dump_stack();
5229f7dd93dSVasily Averin 	}
523617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
524ac27a0ecSDave Kleikamp }
525ac27a0ecSDave Kleikamp 
5264ba9b9d0SChristoph Lameter static void init_once(struct kmem_cache *cachep, void *foo)
527ac27a0ecSDave Kleikamp {
528617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
529ac27a0ecSDave Kleikamp 
530ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
531617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
532ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
533ac27a0ecSDave Kleikamp #endif
534ac27a0ecSDave Kleikamp 	mutex_init(&ei->truncate_mutex);
535ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
536ac27a0ecSDave Kleikamp }
537ac27a0ecSDave Kleikamp 
538ac27a0ecSDave Kleikamp static int init_inodecache(void)
539ac27a0ecSDave Kleikamp {
540617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
541617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
542ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
543ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
54420c2df83SPaul Mundt 					     init_once);
545617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
546ac27a0ecSDave Kleikamp 		return -ENOMEM;
547ac27a0ecSDave Kleikamp 	return 0;
548ac27a0ecSDave Kleikamp }
549ac27a0ecSDave Kleikamp 
550ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
551ac27a0ecSDave Kleikamp {
552617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
553ac27a0ecSDave Kleikamp }
554ac27a0ecSDave Kleikamp 
555617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
556ac27a0ecSDave Kleikamp {
557617ba13bSMingming Cao 	struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
558617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
559617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
560617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
561617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
562617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
563ac27a0ecSDave Kleikamp 	}
564617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
565617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
566617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
567617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
568ac27a0ecSDave Kleikamp 	}
569ac27a0ecSDave Kleikamp #endif
570617ba13bSMingming Cao 	ext4_discard_reservation(inode);
571617ba13bSMingming Cao 	EXT4_I(inode)->i_block_alloc_info = NULL;
572ac27a0ecSDave Kleikamp 	if (unlikely(rsv))
573ac27a0ecSDave Kleikamp 		kfree(rsv);
574ac27a0ecSDave Kleikamp }
575ac27a0ecSDave Kleikamp 
576617ba13bSMingming Cao static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
577ac27a0ecSDave Kleikamp {
578ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
579617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
580ac27a0ecSDave Kleikamp 
581ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
582ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
583ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
584ac27a0ecSDave Kleikamp 
585ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
586ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
587ac27a0ecSDave Kleikamp 
588ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
589ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
590ac27a0ecSDave Kleikamp 
591617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
592ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
593ac27a0ecSDave Kleikamp 
594617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
595ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
596ac27a0ecSDave Kleikamp #endif
597ac27a0ecSDave Kleikamp }
598ac27a0ecSDave Kleikamp 
599d9c9bef1SMiklos Szeredi /*
600d9c9bef1SMiklos Szeredi  * Show an option if
601d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
602d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
603d9c9bef1SMiklos Szeredi  */
604617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
605ac27a0ecSDave Kleikamp {
606ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
607d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
608d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
609d9c9bef1SMiklos Szeredi 	unsigned long def_mount_opts;
610d9c9bef1SMiklos Szeredi 
611d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
612d9c9bef1SMiklos Szeredi 
613d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
614d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
615d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
616d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
617d9c9bef1SMiklos Szeredi 	if (test_opt(sb, GRPID))
618d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
619d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
620d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
621d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
622d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
623d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
624d9c9bef1SMiklos Szeredi 	}
625d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
626d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
627d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
628d9c9bef1SMiklos Szeredi 	}
629d9c9bef1SMiklos Szeredi 	if (test_opt(sb, ERRORS_CONT)) {
630d9c9bef1SMiklos Szeredi 		int def_errors = le16_to_cpu(es->s_errors);
631d9c9bef1SMiklos Szeredi 
632d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
633d9c9bef1SMiklos Szeredi 		    def_errors == EXT4_ERRORS_RO) {
634d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=continue");
635d9c9bef1SMiklos Szeredi 		}
636d9c9bef1SMiklos Szeredi 	}
637d9c9bef1SMiklos Szeredi 	if (test_opt(sb, ERRORS_RO))
638d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=remount-ro");
639d9c9bef1SMiklos Szeredi 	if (test_opt(sb, ERRORS_PANIC))
640d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
641d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NO_UID32))
642d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
643d9c9bef1SMiklos Szeredi 	if (test_opt(sb, DEBUG))
644d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
645d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
646d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
647d9c9bef1SMiklos Szeredi #ifdef CONFIG_EXT4_FS_XATTR
648d9c9bef1SMiklos Szeredi 	if (test_opt(sb, XATTR_USER))
649d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
650d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
651d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
652d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
653d9c9bef1SMiklos Szeredi 	}
654d9c9bef1SMiklos Szeredi #endif
655d9c9bef1SMiklos Szeredi #ifdef CONFIG_EXT4_FS_POSIX_ACL
656d9c9bef1SMiklos Szeredi 	if (test_opt(sb, POSIX_ACL))
657d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
658d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
659d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
660d9c9bef1SMiklos Szeredi #endif
661d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, RESERVATION))
662d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noreservation");
663d9c9bef1SMiklos Szeredi 	if (sbi->s_commit_interval) {
664d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
665d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
666d9c9bef1SMiklos Szeredi 	}
667d9c9bef1SMiklos Szeredi 	if (test_opt(sb, BARRIER))
668d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",barrier=1");
669d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
670d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
671d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, EXTENTS))
672d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noextents");
673ac27a0ecSDave Kleikamp 
674617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
675ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
676617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
677ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
678617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
679ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
680ac27a0ecSDave Kleikamp 
681617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
682ac27a0ecSDave Kleikamp 
683ac27a0ecSDave Kleikamp 	return 0;
684ac27a0ecSDave Kleikamp }
685ac27a0ecSDave Kleikamp 
686ac27a0ecSDave Kleikamp 
687617ba13bSMingming Cao static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
688ac27a0ecSDave Kleikamp {
689ac27a0ecSDave Kleikamp 	__u32 *objp = vobjp;
690ac27a0ecSDave Kleikamp 	unsigned long ino = objp[0];
691ac27a0ecSDave Kleikamp 	__u32 generation = objp[1];
692ac27a0ecSDave Kleikamp 	struct inode *inode;
693ac27a0ecSDave Kleikamp 	struct dentry *result;
694ac27a0ecSDave Kleikamp 
695617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
696ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
697617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
698ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
699ac27a0ecSDave Kleikamp 
700ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
701ac27a0ecSDave Kleikamp 	 *
702617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
703ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
704ac27a0ecSDave Kleikamp 	 *
705ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
706ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
707ac27a0ecSDave Kleikamp 	 */
708ac27a0ecSDave Kleikamp 	inode = iget(sb, ino);
709ac27a0ecSDave Kleikamp 	if (inode == NULL)
710ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
711ac27a0ecSDave Kleikamp 	if (is_bad_inode(inode) ||
712ac27a0ecSDave Kleikamp 	    (generation && inode->i_generation != generation)) {
713ac27a0ecSDave Kleikamp 		iput(inode);
714ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
715ac27a0ecSDave Kleikamp 	}
716ac27a0ecSDave Kleikamp 	/* now to find a dentry.
717ac27a0ecSDave Kleikamp 	 * If possible, get a well-connected one
718ac27a0ecSDave Kleikamp 	 */
719ac27a0ecSDave Kleikamp 	result = d_alloc_anon(inode);
720ac27a0ecSDave Kleikamp 	if (!result) {
721ac27a0ecSDave Kleikamp 		iput(inode);
722ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
723ac27a0ecSDave Kleikamp 	}
724ac27a0ecSDave Kleikamp 	return result;
725ac27a0ecSDave Kleikamp }
726ac27a0ecSDave Kleikamp 
727ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
728ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
729ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
730ac27a0ecSDave Kleikamp 
731617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type);
732617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode);
733617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
734617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
735617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
736617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
737617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
738617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
739617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
740617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
741ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
742617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
743ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
744ac27a0ecSDave Kleikamp 
745617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
746617ba13bSMingming Cao 	.initialize	= ext4_dquot_initialize,
747617ba13bSMingming Cao 	.drop		= ext4_dquot_drop,
748ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
749ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
750ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
751ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
752ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
753617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
754617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
755617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
756617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
757617ba13bSMingming Cao 	.write_info	= ext4_write_info
758ac27a0ecSDave Kleikamp };
759ac27a0ecSDave Kleikamp 
760617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
761617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
762ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
763ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
764ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
765ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
766ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
767ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
768ac27a0ecSDave Kleikamp };
769ac27a0ecSDave Kleikamp #endif
770ac27a0ecSDave Kleikamp 
771ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
772617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
773617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
774617ba13bSMingming Cao 	.read_inode	= ext4_read_inode,
775617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
776617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
777617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
778617ba13bSMingming Cao 	.put_super	= ext4_put_super,
779617ba13bSMingming Cao 	.write_super	= ext4_write_super,
780617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
781617ba13bSMingming Cao 	.write_super_lockfs = ext4_write_super_lockfs,
782617ba13bSMingming Cao 	.unlockfs	= ext4_unlockfs,
783617ba13bSMingming Cao 	.statfs		= ext4_statfs,
784617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
785617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
786617ba13bSMingming Cao 	.show_options	= ext4_show_options,
787ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
788617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
789617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
790ac27a0ecSDave Kleikamp #endif
791ac27a0ecSDave Kleikamp };
792ac27a0ecSDave Kleikamp 
793617ba13bSMingming Cao static struct export_operations ext4_export_ops = {
794617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
795617ba13bSMingming Cao 	.get_dentry = ext4_get_dentry,
796ac27a0ecSDave Kleikamp };
797ac27a0ecSDave Kleikamp 
798ac27a0ecSDave Kleikamp enum {
799ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
800ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
801ac27a0ecSDave Kleikamp 	Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
802ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
803ac27a0ecSDave Kleikamp 	Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
804ac27a0ecSDave Kleikamp 	Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
805ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
806ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
807ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
808ac27a0ecSDave Kleikamp 	Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
8091e2462f9SMingming Cao 	Opt_grpquota, Opt_extents, Opt_noextents,
810ac27a0ecSDave Kleikamp };
811ac27a0ecSDave Kleikamp 
812ac27a0ecSDave Kleikamp static match_table_t tokens = {
813ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
814ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
815ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
816ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
817ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
818ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
819ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
820ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
821ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
822ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
823ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
824ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
825ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
826ac27a0ecSDave Kleikamp 	{Opt_nocheck, "nocheck"},
827ac27a0ecSDave Kleikamp 	{Opt_nocheck, "check=none"},
828ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
829ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
830ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
831ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
832ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
833ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
834ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
835ac27a0ecSDave Kleikamp 	{Opt_reservation, "reservation"},
836ac27a0ecSDave Kleikamp 	{Opt_noreservation, "noreservation"},
837ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
838ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
839ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
840ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
841ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
842ac27a0ecSDave Kleikamp 	{Opt_journal_inum, "journal=%u"},
843ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
844ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
845ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
846ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
847ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
848ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
849ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
850ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
851ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
852ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
853ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
854ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
855ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
856ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
857ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
858ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
859a86c6181SAlex Tomas 	{Opt_extents, "extents"},
8601e2462f9SMingming Cao 	{Opt_noextents, "noextents"},
861ac27a0ecSDave Kleikamp 	{Opt_err, NULL},
862ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
863ac27a0ecSDave Kleikamp };
864ac27a0ecSDave Kleikamp 
865617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
866ac27a0ecSDave Kleikamp {
867617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
868ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
869ac27a0ecSDave Kleikamp 
870ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
871ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
872ac27a0ecSDave Kleikamp 	options += 3;
873617ba13bSMingming Cao 	/*todo: use simple_strtoll with >32bit ext4 */
874ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
875ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
876617ba13bSMingming Cao 		printk("EXT4-fs: Invalid sb specification: %s\n",
877ac27a0ecSDave Kleikamp 		       (char *) *data);
878ac27a0ecSDave Kleikamp 		return 1;
879ac27a0ecSDave Kleikamp 	}
880ac27a0ecSDave Kleikamp 	if (*options == ',')
881ac27a0ecSDave Kleikamp 		options++;
882ac27a0ecSDave Kleikamp 	*data = (void *) options;
883ac27a0ecSDave Kleikamp 	return sb_block;
884ac27a0ecSDave Kleikamp }
885ac27a0ecSDave Kleikamp 
886ac27a0ecSDave Kleikamp static int parse_options (char *options, struct super_block *sb,
887ac27a0ecSDave Kleikamp 			  unsigned int *inum, unsigned long *journal_devnum,
888617ba13bSMingming Cao 			  ext4_fsblk_t *n_blocks_count, int is_remount)
889ac27a0ecSDave Kleikamp {
890617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
891ac27a0ecSDave Kleikamp 	char * p;
892ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
893ac27a0ecSDave Kleikamp 	int data_opt = 0;
894ac27a0ecSDave Kleikamp 	int option;
895ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
896ac27a0ecSDave Kleikamp 	int qtype;
897ac27a0ecSDave Kleikamp 	char *qname;
898ac27a0ecSDave Kleikamp #endif
899ac27a0ecSDave Kleikamp 
900ac27a0ecSDave Kleikamp 	if (!options)
901ac27a0ecSDave Kleikamp 		return 1;
902ac27a0ecSDave Kleikamp 
903ac27a0ecSDave Kleikamp 	while ((p = strsep (&options, ",")) != NULL) {
904ac27a0ecSDave Kleikamp 		int token;
905ac27a0ecSDave Kleikamp 		if (!*p)
906ac27a0ecSDave Kleikamp 			continue;
907ac27a0ecSDave Kleikamp 
908ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
909ac27a0ecSDave Kleikamp 		switch (token) {
910ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
911ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, MINIX_DF);
912ac27a0ecSDave Kleikamp 			break;
913ac27a0ecSDave Kleikamp 		case Opt_minix_df:
914ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, MINIX_DF);
915ac27a0ecSDave Kleikamp 			break;
916ac27a0ecSDave Kleikamp 		case Opt_grpid:
917ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, GRPID);
918ac27a0ecSDave Kleikamp 			break;
919ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
920ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, GRPID);
921ac27a0ecSDave Kleikamp 			break;
922ac27a0ecSDave Kleikamp 		case Opt_resuid:
923ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
924ac27a0ecSDave Kleikamp 				return 0;
925ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
926ac27a0ecSDave Kleikamp 			break;
927ac27a0ecSDave Kleikamp 		case Opt_resgid:
928ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
929ac27a0ecSDave Kleikamp 				return 0;
930ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
931ac27a0ecSDave Kleikamp 			break;
932ac27a0ecSDave Kleikamp 		case Opt_sb:
933ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
934ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
935ac27a0ecSDave Kleikamp 			break;
936ac27a0ecSDave Kleikamp 		case Opt_err_panic:
937ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
938ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_RO);
939ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_PANIC);
940ac27a0ecSDave Kleikamp 			break;
941ac27a0ecSDave Kleikamp 		case Opt_err_ro:
942ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
943ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
944ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_RO);
945ac27a0ecSDave Kleikamp 			break;
946ac27a0ecSDave Kleikamp 		case Opt_err_cont:
947ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_RO);
948ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
949ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_CONT);
950ac27a0ecSDave Kleikamp 			break;
951ac27a0ecSDave Kleikamp 		case Opt_nouid32:
952ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, NO_UID32);
953ac27a0ecSDave Kleikamp 			break;
954ac27a0ecSDave Kleikamp 		case Opt_nocheck:
955ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, CHECK);
956ac27a0ecSDave Kleikamp 			break;
957ac27a0ecSDave Kleikamp 		case Opt_debug:
958ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, DEBUG);
959ac27a0ecSDave Kleikamp 			break;
960ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
961ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, OLDALLOC);
962ac27a0ecSDave Kleikamp 			break;
963ac27a0ecSDave Kleikamp 		case Opt_orlov:
964ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, OLDALLOC);
965ac27a0ecSDave Kleikamp 			break;
966617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
967ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
968ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, XATTR_USER);
969ac27a0ecSDave Kleikamp 			break;
970ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
971ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, XATTR_USER);
972ac27a0ecSDave Kleikamp 			break;
973ac27a0ecSDave Kleikamp #else
974ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
975ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
976617ba13bSMingming Cao 			printk("EXT4 (no)user_xattr options not supported\n");
977ac27a0ecSDave Kleikamp 			break;
978ac27a0ecSDave Kleikamp #endif
979617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
980ac27a0ecSDave Kleikamp 		case Opt_acl:
981ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
982ac27a0ecSDave Kleikamp 			break;
983ac27a0ecSDave Kleikamp 		case Opt_noacl:
984ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
985ac27a0ecSDave Kleikamp 			break;
986ac27a0ecSDave Kleikamp #else
987ac27a0ecSDave Kleikamp 		case Opt_acl:
988ac27a0ecSDave Kleikamp 		case Opt_noacl:
989617ba13bSMingming Cao 			printk("EXT4 (no)acl options not supported\n");
990ac27a0ecSDave Kleikamp 			break;
991ac27a0ecSDave Kleikamp #endif
992ac27a0ecSDave Kleikamp 		case Opt_reservation:
993ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, RESERVATION);
994ac27a0ecSDave Kleikamp 			break;
995ac27a0ecSDave Kleikamp 		case Opt_noreservation:
996ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, RESERVATION);
997ac27a0ecSDave Kleikamp 			break;
998ac27a0ecSDave Kleikamp 		case Opt_journal_update:
999ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1000ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1001ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1002ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1003ac27a0ecSDave Kleikamp 			   journal file. */
1004ac27a0ecSDave Kleikamp 			if (is_remount) {
1005617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1006ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1007ac27a0ecSDave Kleikamp 				return 0;
1008ac27a0ecSDave Kleikamp 			}
1009ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
1010ac27a0ecSDave Kleikamp 			break;
1011ac27a0ecSDave Kleikamp 		case Opt_journal_inum:
1012ac27a0ecSDave Kleikamp 			if (is_remount) {
1013617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1014ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1015ac27a0ecSDave Kleikamp 				return 0;
1016ac27a0ecSDave Kleikamp 			}
1017ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1018ac27a0ecSDave Kleikamp 				return 0;
1019ac27a0ecSDave Kleikamp 			*inum = option;
1020ac27a0ecSDave Kleikamp 			break;
1021ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1022ac27a0ecSDave Kleikamp 			if (is_remount) {
1023617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
1024ac27a0ecSDave Kleikamp 				       "journal on remount\n");
1025ac27a0ecSDave Kleikamp 				return 0;
1026ac27a0ecSDave Kleikamp 			}
1027ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1028ac27a0ecSDave Kleikamp 				return 0;
1029ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1030ac27a0ecSDave Kleikamp 			break;
1031ac27a0ecSDave Kleikamp 		case Opt_noload:
1032ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, NOLOAD);
1033ac27a0ecSDave Kleikamp 			break;
1034ac27a0ecSDave Kleikamp 		case Opt_commit:
1035ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1036ac27a0ecSDave Kleikamp 				return 0;
1037ac27a0ecSDave Kleikamp 			if (option < 0)
1038ac27a0ecSDave Kleikamp 				return 0;
1039ac27a0ecSDave Kleikamp 			if (option == 0)
1040*cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1041ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1042ac27a0ecSDave Kleikamp 			break;
1043ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1044617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1045ac27a0ecSDave Kleikamp 			goto datacheck;
1046ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1047617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1048ac27a0ecSDave Kleikamp 			goto datacheck;
1049ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1050617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1051ac27a0ecSDave Kleikamp 		datacheck:
1052ac27a0ecSDave Kleikamp 			if (is_remount) {
1053617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1054ac27a0ecSDave Kleikamp 						!= data_opt) {
1055ac27a0ecSDave Kleikamp 					printk(KERN_ERR
1056617ba13bSMingming Cao 						"EXT4-fs: cannot change data "
1057ac27a0ecSDave Kleikamp 						"mode on remount\n");
1058ac27a0ecSDave Kleikamp 					return 0;
1059ac27a0ecSDave Kleikamp 				}
1060ac27a0ecSDave Kleikamp 			} else {
1061617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1062ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1063ac27a0ecSDave Kleikamp 			}
1064ac27a0ecSDave Kleikamp 			break;
1065ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1066ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1067ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1068ac27a0ecSDave Kleikamp 			goto set_qf_name;
1069ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1070ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1071ac27a0ecSDave Kleikamp set_qf_name:
1072ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1073ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1074617ba13bSMingming Cao 					"EXT4-fs: Cannot change journalled "
1075ac27a0ecSDave Kleikamp 					"quota options when quota turned on.\n");
1076ac27a0ecSDave Kleikamp 				return 0;
1077ac27a0ecSDave Kleikamp 			}
1078ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1079ac27a0ecSDave Kleikamp 			if (!qname) {
1080ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1081617ba13bSMingming Cao 					"EXT4-fs: not enough memory for "
1082ac27a0ecSDave Kleikamp 					"storing quotafile name.\n");
1083ac27a0ecSDave Kleikamp 				return 0;
1084ac27a0ecSDave Kleikamp 			}
1085ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1086ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1087ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1088617ba13bSMingming Cao 					"EXT4-fs: %s quota file already "
1089ac27a0ecSDave Kleikamp 					"specified.\n", QTYPE2NAME(qtype));
1090ac27a0ecSDave Kleikamp 				kfree(qname);
1091ac27a0ecSDave Kleikamp 				return 0;
1092ac27a0ecSDave Kleikamp 			}
1093ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1094ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1095ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1096617ba13bSMingming Cao 					"EXT4-fs: quotafile must be on "
1097ac27a0ecSDave Kleikamp 					"filesystem root.\n");
1098ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1099ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1100ac27a0ecSDave Kleikamp 				return 0;
1101ac27a0ecSDave Kleikamp 			}
1102ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1103ac27a0ecSDave Kleikamp 			break;
1104ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1105ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1106ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1107ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1108ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1109ac27a0ecSDave Kleikamp clear_qf_name:
1110ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1111617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change "
1112ac27a0ecSDave Kleikamp 					"journalled quota options when "
1113ac27a0ecSDave Kleikamp 					"quota turned on.\n");
1114ac27a0ecSDave Kleikamp 				return 0;
1115ac27a0ecSDave Kleikamp 			}
1116ac27a0ecSDave Kleikamp 			/*
1117ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1118ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1119ac27a0ecSDave Kleikamp 			 */
1120ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1121ac27a0ecSDave Kleikamp 			break;
1122ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1123ac27a0ecSDave Kleikamp 			sbi->s_jquota_fmt = QFMT_VFS_OLD;
1124ac27a0ecSDave Kleikamp 			break;
1125ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1126ac27a0ecSDave Kleikamp 			sbi->s_jquota_fmt = QFMT_VFS_V0;
1127ac27a0ecSDave Kleikamp 			break;
1128ac27a0ecSDave Kleikamp 		case Opt_quota:
1129ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1130ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1131ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1132ac27a0ecSDave Kleikamp 			break;
1133ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1134ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1135ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1136ac27a0ecSDave Kleikamp 			break;
1137ac27a0ecSDave Kleikamp 		case Opt_noquota:
1138ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1139617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change quota "
1140ac27a0ecSDave Kleikamp 					"options when quota turned on.\n");
1141ac27a0ecSDave Kleikamp 				return 0;
1142ac27a0ecSDave Kleikamp 			}
1143ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1144ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1145ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1146ac27a0ecSDave Kleikamp 			break;
1147ac27a0ecSDave Kleikamp #else
1148ac27a0ecSDave Kleikamp 		case Opt_quota:
1149ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1150ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1151ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1152ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1153ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1154ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1155ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1156ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1157ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1158617ba13bSMingming Cao 				"EXT4-fs: journalled quota options not "
1159ac27a0ecSDave Kleikamp 				"supported.\n");
1160ac27a0ecSDave Kleikamp 			break;
1161ac27a0ecSDave Kleikamp 		case Opt_noquota:
1162ac27a0ecSDave Kleikamp 			break;
1163ac27a0ecSDave Kleikamp #endif
1164ac27a0ecSDave Kleikamp 		case Opt_abort:
1165ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ABORT);
1166ac27a0ecSDave Kleikamp 			break;
1167ac27a0ecSDave Kleikamp 		case Opt_barrier:
1168ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1169ac27a0ecSDave Kleikamp 				return 0;
1170ac27a0ecSDave Kleikamp 			if (option)
1171ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1172ac27a0ecSDave Kleikamp 			else
1173ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1174ac27a0ecSDave Kleikamp 			break;
1175ac27a0ecSDave Kleikamp 		case Opt_ignore:
1176ac27a0ecSDave Kleikamp 			break;
1177ac27a0ecSDave Kleikamp 		case Opt_resize:
1178ac27a0ecSDave Kleikamp 			if (!is_remount) {
1179617ba13bSMingming Cao 				printk("EXT4-fs: resize option only available "
1180ac27a0ecSDave Kleikamp 					"for remount\n");
1181ac27a0ecSDave Kleikamp 				return 0;
1182ac27a0ecSDave Kleikamp 			}
1183ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1184ac27a0ecSDave Kleikamp 				return 0;
1185ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1186ac27a0ecSDave Kleikamp 			break;
1187ac27a0ecSDave Kleikamp 		case Opt_nobh:
1188ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1189ac27a0ecSDave Kleikamp 			break;
1190ac27a0ecSDave Kleikamp 		case Opt_bh:
1191ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1192ac27a0ecSDave Kleikamp 			break;
1193a86c6181SAlex Tomas 		case Opt_extents:
1194a86c6181SAlex Tomas 			set_opt (sbi->s_mount_opt, EXTENTS);
1195a86c6181SAlex Tomas 			break;
11961e2462f9SMingming Cao 		case Opt_noextents:
11971e2462f9SMingming Cao 			clear_opt (sbi->s_mount_opt, EXTENTS);
11981e2462f9SMingming Cao 			break;
1199ac27a0ecSDave Kleikamp 		default:
1200ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1201617ba13bSMingming Cao 				"EXT4-fs: Unrecognized mount option \"%s\" "
1202ac27a0ecSDave Kleikamp 				"or missing value\n", p);
1203ac27a0ecSDave Kleikamp 			return 0;
1204ac27a0ecSDave Kleikamp 		}
1205ac27a0ecSDave Kleikamp 	}
1206ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1207ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1208617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1209ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1210ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1211ac27a0ecSDave Kleikamp 
1212617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1213ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1214ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1215ac27a0ecSDave Kleikamp 
1216ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1217617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1218ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1219617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1220617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: old and new quota "
1221ac27a0ecSDave Kleikamp 					"format mixing.\n");
1222ac27a0ecSDave Kleikamp 			return 0;
1223ac27a0ecSDave Kleikamp 		}
1224ac27a0ecSDave Kleikamp 
1225ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1226617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: journalled quota format "
1227ac27a0ecSDave Kleikamp 					"not specified.\n");
1228ac27a0ecSDave Kleikamp 			return 0;
1229ac27a0ecSDave Kleikamp 		}
1230ac27a0ecSDave Kleikamp 	} else {
1231ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1232617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: journalled quota format "
1233ac27a0ecSDave Kleikamp 					"specified with no journalling "
1234ac27a0ecSDave Kleikamp 					"enabled.\n");
1235ac27a0ecSDave Kleikamp 			return 0;
1236ac27a0ecSDave Kleikamp 		}
1237ac27a0ecSDave Kleikamp 	}
1238ac27a0ecSDave Kleikamp #endif
1239ac27a0ecSDave Kleikamp 	return 1;
1240ac27a0ecSDave Kleikamp }
1241ac27a0ecSDave Kleikamp 
1242617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1243ac27a0ecSDave Kleikamp 			    int read_only)
1244ac27a0ecSDave Kleikamp {
1245617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1246ac27a0ecSDave Kleikamp 	int res = 0;
1247ac27a0ecSDave Kleikamp 
1248617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1249617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs warning: revision level too high, "
1250ac27a0ecSDave Kleikamp 			"forcing read-only mode\n");
1251ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1252ac27a0ecSDave Kleikamp 	}
1253ac27a0ecSDave Kleikamp 	if (read_only)
1254ac27a0ecSDave Kleikamp 		return res;
1255617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1256617ba13bSMingming Cao 		printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1257ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1258617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1259ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1260617ba13bSMingming Cao 			"EXT4-fs warning: mounting fs with errors, "
1261ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1262ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1263ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1264ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1265ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1266617ba13bSMingming Cao 			"EXT4-fs warning: maximal mount count reached, "
1267ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1268ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1269ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1270ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1271ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1272617ba13bSMingming Cao 			"EXT4-fs warning: checktime reached, "
1273ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1274ac27a0ecSDave Kleikamp #if 0
1275ac27a0ecSDave Kleikamp 		/* @@@ We _will_ want to clear the valid bit if we find
127663f57933SAndrew Morton 		 * inconsistencies, to force a fsck at reboot.  But for
127763f57933SAndrew Morton 		 * a plain journaled filesystem we can keep it set as
127863f57933SAndrew Morton 		 * valid forever! :)
127963f57933SAndrew Morton 		 */
1280617ba13bSMingming Cao 	es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
1281ac27a0ecSDave Kleikamp #endif
1282ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1283617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1284ac27a0ecSDave Kleikamp 	es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1285ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1286617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
1287617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1288ac27a0ecSDave Kleikamp 
1289617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
1290ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1291617ba13bSMingming Cao 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1292ac27a0ecSDave Kleikamp 				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
1293ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1294ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1295617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1296617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1297ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1298ac27a0ecSDave Kleikamp 
1299617ba13bSMingming Cao 	printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1300617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
1301ac27a0ecSDave Kleikamp 		char b[BDEVNAME_SIZE];
1302ac27a0ecSDave Kleikamp 
1303ac27a0ecSDave Kleikamp 		printk("external journal on %s\n",
1304617ba13bSMingming Cao 			bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
1305ac27a0ecSDave Kleikamp 	} else {
1306ac27a0ecSDave Kleikamp 		printk("internal journal\n");
1307ac27a0ecSDave Kleikamp 	}
1308ac27a0ecSDave Kleikamp 	return res;
1309ac27a0ecSDave Kleikamp }
1310ac27a0ecSDave Kleikamp 
1311ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1312617ba13bSMingming Cao static int ext4_check_descriptors (struct super_block * sb)
1313ac27a0ecSDave Kleikamp {
1314617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1315617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1316617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1317bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1318bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1319bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1320617ba13bSMingming Cao 	struct ext4_group_desc * gdp = NULL;
1321ac27a0ecSDave Kleikamp 	int desc_block = 0;
1322ac27a0ecSDave Kleikamp 	int i;
1323ac27a0ecSDave Kleikamp 
1324617ba13bSMingming Cao 	ext4_debug ("Checking group descriptors");
1325ac27a0ecSDave Kleikamp 
1326ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_groups_count; i++)
1327ac27a0ecSDave Kleikamp 	{
1328ac27a0ecSDave Kleikamp 		if (i == sbi->s_groups_count - 1)
1329bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1330ac27a0ecSDave Kleikamp 		else
1331ac27a0ecSDave Kleikamp 			last_block = first_block +
1332617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1333ac27a0ecSDave Kleikamp 
1334617ba13bSMingming Cao 		if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1335617ba13bSMingming Cao 			gdp = (struct ext4_group_desc *)
1336ac27a0ecSDave Kleikamp 					sbi->s_group_desc[desc_block++]->b_data;
13378fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
1338bd81d8eeSLaurent Vivier 		if (block_bitmap < first_block || block_bitmap > last_block)
1339ac27a0ecSDave Kleikamp 		{
1340617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1341ac27a0ecSDave Kleikamp 				    "Block bitmap for group %d"
13422ae02107SMingming Cao 				    " not in group (block %llu)!",
1343bd81d8eeSLaurent Vivier 				    i, block_bitmap);
1344ac27a0ecSDave Kleikamp 			return 0;
1345ac27a0ecSDave Kleikamp 		}
13468fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
1347bd81d8eeSLaurent Vivier 		if (inode_bitmap < first_block || inode_bitmap > last_block)
1348ac27a0ecSDave Kleikamp 		{
1349617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1350ac27a0ecSDave Kleikamp 				    "Inode bitmap for group %d"
13512ae02107SMingming Cao 				    " not in group (block %llu)!",
1352bd81d8eeSLaurent Vivier 				    i, inode_bitmap);
1353ac27a0ecSDave Kleikamp 			return 0;
1354ac27a0ecSDave Kleikamp 		}
13558fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1356bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
1357780dcdb2SEric Sandeen 		    inode_table + sbi->s_itb_per_group - 1 > last_block)
1358ac27a0ecSDave Kleikamp 		{
1359617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1360ac27a0ecSDave Kleikamp 				    "Inode table for group %d"
13612ae02107SMingming Cao 				    " not in group (block %llu)!",
1362bd81d8eeSLaurent Vivier 				    i, inode_table);
1363ac27a0ecSDave Kleikamp 			return 0;
1364ac27a0ecSDave Kleikamp 		}
1365617ba13bSMingming Cao 		first_block += EXT4_BLOCKS_PER_GROUP(sb);
13660d1ee42fSAlexandre Ratchov 		gdp = (struct ext4_group_desc *)
13670d1ee42fSAlexandre Ratchov 			((__u8 *)gdp + EXT4_DESC_SIZE(sb));
1368ac27a0ecSDave Kleikamp 	}
1369ac27a0ecSDave Kleikamp 
1370bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1371617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
1372ac27a0ecSDave Kleikamp 	return 1;
1373ac27a0ecSDave Kleikamp }
1374ac27a0ecSDave Kleikamp 
1375ac27a0ecSDave Kleikamp 
1376617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1377ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1378ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1379ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1380ac27a0ecSDave Kleikamp  *
1381ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1382ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1383ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1384ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1385ac27a0ecSDave Kleikamp  *
1386ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1387ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1388ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1389617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1390ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1391ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1392ac27a0ecSDave Kleikamp  */
1393617ba13bSMingming Cao static void ext4_orphan_cleanup (struct super_block * sb,
1394617ba13bSMingming Cao 				 struct ext4_super_block * es)
1395ac27a0ecSDave Kleikamp {
1396ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1397ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1398ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1399ac27a0ecSDave Kleikamp 	int i;
1400ac27a0ecSDave Kleikamp #endif
1401ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1402ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1403ac27a0ecSDave Kleikamp 		return;
1404ac27a0ecSDave Kleikamp 	}
1405ac27a0ecSDave Kleikamp 
1406a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1407a8f48a95SEric Sandeen 		printk(KERN_ERR "EXT4-fs: write access "
1408a8f48a95SEric Sandeen 			"unavailable, skipping orphan cleanup.\n");
1409a8f48a95SEric Sandeen 		return;
1410a8f48a95SEric Sandeen 	}
1411a8f48a95SEric Sandeen 
1412617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1413ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1414ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1415ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1416ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1417ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1418ac27a0ecSDave Kleikamp 		return;
1419ac27a0ecSDave Kleikamp 	}
1420ac27a0ecSDave Kleikamp 
1421ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1422617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1423ac27a0ecSDave Kleikamp 		       sb->s_id);
1424ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1425ac27a0ecSDave Kleikamp 	}
1426ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1427ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1428ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1429ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1430ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1431617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1432617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1433ac27a0ecSDave Kleikamp 			if (ret < 0)
1434ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1435617ba13bSMingming Cao 					"EXT4-fs: Cannot turn on journalled "
1436ac27a0ecSDave Kleikamp 					"quota: error %d\n", ret);
1437ac27a0ecSDave Kleikamp 		}
1438ac27a0ecSDave Kleikamp 	}
1439ac27a0ecSDave Kleikamp #endif
1440ac27a0ecSDave Kleikamp 
1441ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1442ac27a0ecSDave Kleikamp 		struct inode *inode;
1443ac27a0ecSDave Kleikamp 
1444ac27a0ecSDave Kleikamp 		if (!(inode =
1445617ba13bSMingming Cao 		      ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
1446ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1447ac27a0ecSDave Kleikamp 			break;
1448ac27a0ecSDave Kleikamp 		}
1449ac27a0ecSDave Kleikamp 
1450617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1451ac27a0ecSDave Kleikamp 		DQUOT_INIT(inode);
1452ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1453ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1454ac27a0ecSDave Kleikamp 				"%s: truncating inode %lu to %Ld bytes\n",
1455ac27a0ecSDave Kleikamp 				__FUNCTION__, inode->i_ino, inode->i_size);
1456ac27a0ecSDave Kleikamp 			jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1457ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1458617ba13bSMingming Cao 			ext4_truncate(inode);
1459ac27a0ecSDave Kleikamp 			nr_truncates++;
1460ac27a0ecSDave Kleikamp 		} else {
1461ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1462ac27a0ecSDave Kleikamp 				"%s: deleting unreferenced inode %lu\n",
1463ac27a0ecSDave Kleikamp 				__FUNCTION__, inode->i_ino);
1464ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1465ac27a0ecSDave Kleikamp 				  inode->i_ino);
1466ac27a0ecSDave Kleikamp 			nr_orphans++;
1467ac27a0ecSDave Kleikamp 		}
1468ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1469ac27a0ecSDave Kleikamp 	}
1470ac27a0ecSDave Kleikamp 
1471ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x)==1) ? "" : "s"
1472ac27a0ecSDave Kleikamp 
1473ac27a0ecSDave Kleikamp 	if (nr_orphans)
1474617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1475ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_orphans));
1476ac27a0ecSDave Kleikamp 	if (nr_truncates)
1477617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1478ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_truncates));
1479ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1480ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1481ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1482ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
1483ac27a0ecSDave Kleikamp 			vfs_quota_off(sb, i);
1484ac27a0ecSDave Kleikamp 	}
1485ac27a0ecSDave Kleikamp #endif
1486ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1487ac27a0ecSDave Kleikamp }
1488ac27a0ecSDave Kleikamp 
1489ac27a0ecSDave Kleikamp /*
1490ac27a0ecSDave Kleikamp  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
1491ac27a0ecSDave Kleikamp  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1492ac27a0ecSDave Kleikamp  * We need to be 1 filesystem block less than the 2^32 sector limit.
1493ac27a0ecSDave Kleikamp  */
1494617ba13bSMingming Cao static loff_t ext4_max_size(int bits)
1495ac27a0ecSDave Kleikamp {
1496617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
1497ac27a0ecSDave Kleikamp 	/* This constant is calculated to be the largest file size for a
1498ac27a0ecSDave Kleikamp 	 * dense, 4k-blocksize file such that the total number of
1499ac27a0ecSDave Kleikamp 	 * sectors in the file, including data and all indirect blocks,
1500ac27a0ecSDave Kleikamp 	 * does not exceed 2^32. */
1501ac27a0ecSDave Kleikamp 	const loff_t upper_limit = 0x1ff7fffd000LL;
1502ac27a0ecSDave Kleikamp 
1503ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
1504ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
1505ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
1506ac27a0ecSDave Kleikamp 	res <<= bits;
1507ac27a0ecSDave Kleikamp 	if (res > upper_limit)
1508ac27a0ecSDave Kleikamp 		res = upper_limit;
1509ac27a0ecSDave Kleikamp 	return res;
1510ac27a0ecSDave Kleikamp }
1511ac27a0ecSDave Kleikamp 
1512617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
151370bbb3e0SAndrew Morton 				ext4_fsblk_t logical_sb_block, int nr)
1514ac27a0ecSDave Kleikamp {
1515617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1516ac27a0ecSDave Kleikamp 	unsigned long bg, first_meta_bg;
1517ac27a0ecSDave Kleikamp 	int has_super = 0;
1518ac27a0ecSDave Kleikamp 
1519ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1520ac27a0ecSDave Kleikamp 
1521617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1522ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
152370bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
1524ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
1525617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
1526ac27a0ecSDave Kleikamp 		has_super = 1;
1527617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
1528ac27a0ecSDave Kleikamp }
1529ac27a0ecSDave Kleikamp 
1530ac27a0ecSDave Kleikamp 
1531617ba13bSMingming Cao static int ext4_fill_super (struct super_block *sb, void *data, int silent)
1532ac27a0ecSDave Kleikamp {
1533ac27a0ecSDave Kleikamp 	struct buffer_head * bh;
1534617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
1535617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1536617ba13bSMingming Cao 	ext4_fsblk_t block;
1537617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
153870bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
1539ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
1540ac27a0ecSDave Kleikamp 	unsigned int journal_inum = 0;
1541ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
1542ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
1543ac27a0ecSDave Kleikamp 	struct inode *root;
1544ac27a0ecSDave Kleikamp 	int blocksize;
1545ac27a0ecSDave Kleikamp 	int hblock;
1546ac27a0ecSDave Kleikamp 	int db_count;
1547ac27a0ecSDave Kleikamp 	int i;
1548ac27a0ecSDave Kleikamp 	int needs_recovery;
1549ac27a0ecSDave Kleikamp 	__le32 features;
1550bd81d8eeSLaurent Vivier 	__u64 blocks_count;
1551833f4077SPeter Zijlstra 	int err;
1552ac27a0ecSDave Kleikamp 
1553ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1554ac27a0ecSDave Kleikamp 	if (!sbi)
1555ac27a0ecSDave Kleikamp 		return -ENOMEM;
1556ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
1557ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
1558617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
1559617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
1560d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
1561ac27a0ecSDave Kleikamp 
1562ac27a0ecSDave Kleikamp 	unlock_kernel();
1563ac27a0ecSDave Kleikamp 
1564617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1565ac27a0ecSDave Kleikamp 	if (!blocksize) {
1566617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1567ac27a0ecSDave Kleikamp 		goto out_fail;
1568ac27a0ecSDave Kleikamp 	}
1569ac27a0ecSDave Kleikamp 
1570ac27a0ecSDave Kleikamp 	/*
1571617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
1572ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
1573ac27a0ecSDave Kleikamp 	 */
1574617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
157570bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
157670bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
1577ac27a0ecSDave Kleikamp 	} else {
157870bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
1579ac27a0ecSDave Kleikamp 	}
1580ac27a0ecSDave Kleikamp 
158170bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
1582617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
1583ac27a0ecSDave Kleikamp 		goto out_fail;
1584ac27a0ecSDave Kleikamp 	}
1585ac27a0ecSDave Kleikamp 	/*
1586ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
1587617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
1588ac27a0ecSDave Kleikamp 	 */
1589617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1590ac27a0ecSDave Kleikamp 	sbi->s_es = es;
1591ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
1592617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
1593617ba13bSMingming Cao 		goto cantfind_ext4;
1594ac27a0ecSDave Kleikamp 
1595ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
1596ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1597617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
1598ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
1599617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1600ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
1601617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
1602ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
16032e7842b8SHugh Dickins #ifdef CONFIG_EXT4DEV_FS_XATTR
1604617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1605ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
16062e7842b8SHugh Dickins #endif
16072e7842b8SHugh Dickins #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1608617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
1609ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
16102e7842b8SHugh Dickins #endif
1611617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1612617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1613617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1614617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1615617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1616617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1617ac27a0ecSDave Kleikamp 
1618617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1619ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1620617ba13bSMingming Cao 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
1621ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_RO);
1622ceea16bfSDmitry Mishin 	else
1623ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
1624ac27a0ecSDave Kleikamp 
1625ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1626ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1627ac27a0ecSDave Kleikamp 
1628ac27a0ecSDave Kleikamp 	set_opt(sbi->s_mount_opt, RESERVATION);
1629ac27a0ecSDave Kleikamp 
16301e2462f9SMingming Cao 	/*
16311e2462f9SMingming Cao 	 * turn on extents feature by default in ext4 filesystem
16321e2462f9SMingming Cao 	 * User -o noextents to turn it off
16331e2462f9SMingming Cao 	 */
16341e2462f9SMingming Cao 	set_opt(sbi->s_mount_opt, EXTENTS);
16351e2462f9SMingming Cao 
1636ac27a0ecSDave Kleikamp 	if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1637ac27a0ecSDave Kleikamp 			    NULL, 0))
1638ac27a0ecSDave Kleikamp 		goto failed_mount;
1639ac27a0ecSDave Kleikamp 
1640ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1641617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1642ac27a0ecSDave Kleikamp 
1643617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1644617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1645617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1646617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1647ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1648617ba13bSMingming Cao 		       "EXT4-fs warning: feature flags set on rev 0 fs, "
1649ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1650ac27a0ecSDave Kleikamp 	/*
1651ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
1652ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
1653ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
1654ac27a0ecSDave Kleikamp 	 */
1655617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
1656ac27a0ecSDave Kleikamp 	if (features) {
1657617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
1658ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
1659ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
1660ac27a0ecSDave Kleikamp 		goto failed_mount;
1661ac27a0ecSDave Kleikamp 	}
1662617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
1663ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
1664617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
1665ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
1666ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
1667ac27a0ecSDave Kleikamp 		goto failed_mount;
1668ac27a0ecSDave Kleikamp 	}
1669ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1670ac27a0ecSDave Kleikamp 
1671617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1672617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
1673ac27a0ecSDave Kleikamp 		printk(KERN_ERR
1674617ba13bSMingming Cao 		       "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
1675ac27a0ecSDave Kleikamp 		       blocksize, sb->s_id);
1676ac27a0ecSDave Kleikamp 		goto failed_mount;
1677ac27a0ecSDave Kleikamp 	}
1678ac27a0ecSDave Kleikamp 
1679ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(sb->s_bdev);
1680ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
1681ac27a0ecSDave Kleikamp 		/*
1682ac27a0ecSDave Kleikamp 		 * Make sure the blocksize for the filesystem is larger
1683ac27a0ecSDave Kleikamp 		 * than the hardware sectorsize for the machine.
1684ac27a0ecSDave Kleikamp 		 */
1685ac27a0ecSDave Kleikamp 		if (blocksize < hblock) {
1686617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
1687ac27a0ecSDave Kleikamp 			       "device blocksize %d.\n", blocksize, hblock);
1688ac27a0ecSDave Kleikamp 			goto failed_mount;
1689ac27a0ecSDave Kleikamp 		}
1690ac27a0ecSDave Kleikamp 
1691ac27a0ecSDave Kleikamp 		brelse (bh);
1692ac27a0ecSDave Kleikamp 		sb_set_blocksize(sb, blocksize);
169370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
169470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
169570bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
1696ac27a0ecSDave Kleikamp 		if (!bh) {
1697ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1698617ba13bSMingming Cao 			       "EXT4-fs: Can't read superblock on 2nd try.\n");
1699ac27a0ecSDave Kleikamp 			goto failed_mount;
1700ac27a0ecSDave Kleikamp 		}
1701617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
1702ac27a0ecSDave Kleikamp 		sbi->s_es = es;
1703617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
1704ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1705617ba13bSMingming Cao 				"EXT4-fs: Magic mismatch, very weird !\n");
1706ac27a0ecSDave Kleikamp 			goto failed_mount;
1707ac27a0ecSDave Kleikamp 		}
1708ac27a0ecSDave Kleikamp 	}
1709ac27a0ecSDave Kleikamp 
1710617ba13bSMingming Cao 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
1711ac27a0ecSDave Kleikamp 
1712617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1713617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1714617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
1715ac27a0ecSDave Kleikamp 	} else {
1716ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1717ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1718617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
17191330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
1720ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
1721ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1722617ba13bSMingming Cao 				"EXT4-fs: unsupported inode size: %d\n",
1723ac27a0ecSDave Kleikamp 				sbi->s_inode_size);
1724ac27a0ecSDave Kleikamp 			goto failed_mount;
1725ac27a0ecSDave Kleikamp 		}
1726ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
1727ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
1728ac27a0ecSDave Kleikamp 	}
1729617ba13bSMingming Cao 	sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
1730ac27a0ecSDave Kleikamp 				   le32_to_cpu(es->s_log_frag_size);
1731ac27a0ecSDave Kleikamp 	if (blocksize != sbi->s_frag_size) {
1732ac27a0ecSDave Kleikamp 		printk(KERN_ERR
1733617ba13bSMingming Cao 		       "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
1734ac27a0ecSDave Kleikamp 		       sbi->s_frag_size, blocksize);
1735ac27a0ecSDave Kleikamp 		goto failed_mount;
1736ac27a0ecSDave Kleikamp 	}
17370d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
17380d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
17398fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
17400d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
1741d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
17420d1ee42fSAlexandre Ratchov 			printk(KERN_ERR
17438fadc143SAlexandre Ratchov 			       "EXT4-fs: unsupported descriptor size %lu\n",
17440d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
17450d1ee42fSAlexandre Ratchov 			goto failed_mount;
17460d1ee42fSAlexandre Ratchov 		}
17470d1ee42fSAlexandre Ratchov 	} else
17480d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
1749ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1750ac27a0ecSDave Kleikamp 	sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1751ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1752617ba13bSMingming Cao 	if (EXT4_INODE_SIZE(sb) == 0)
1753617ba13bSMingming Cao 		goto cantfind_ext4;
1754617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
1755ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
1756617ba13bSMingming Cao 		goto cantfind_ext4;
1757ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
1758ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
17590d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
1760ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
1761ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
1762e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
1763e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
1764ac27a0ecSDave Kleikamp 	for (i=0; i < 4; i++)
1765ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1766ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
1767ac27a0ecSDave Kleikamp 
1768ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
1769ac27a0ecSDave Kleikamp 		printk (KERN_ERR
1770617ba13bSMingming Cao 			"EXT4-fs: #blocks per group too big: %lu\n",
1771ac27a0ecSDave Kleikamp 			sbi->s_blocks_per_group);
1772ac27a0ecSDave Kleikamp 		goto failed_mount;
1773ac27a0ecSDave Kleikamp 	}
1774ac27a0ecSDave Kleikamp 	if (sbi->s_frags_per_group > blocksize * 8) {
1775ac27a0ecSDave Kleikamp 		printk (KERN_ERR
1776617ba13bSMingming Cao 			"EXT4-fs: #fragments per group too big: %lu\n",
1777ac27a0ecSDave Kleikamp 			sbi->s_frags_per_group);
1778ac27a0ecSDave Kleikamp 		goto failed_mount;
1779ac27a0ecSDave Kleikamp 	}
1780ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
1781ac27a0ecSDave Kleikamp 		printk (KERN_ERR
1782617ba13bSMingming Cao 			"EXT4-fs: #inodes per group too big: %lu\n",
1783ac27a0ecSDave Kleikamp 			sbi->s_inodes_per_group);
1784ac27a0ecSDave Kleikamp 		goto failed_mount;
1785ac27a0ecSDave Kleikamp 	}
1786ac27a0ecSDave Kleikamp 
1787bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
1788ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1789617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
1790ac27a0ecSDave Kleikamp 			" too large to mount safely\n", sb->s_id);
1791ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
1792617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
1793ac27a0ecSDave Kleikamp 					"enabled\n");
1794ac27a0ecSDave Kleikamp 		goto failed_mount;
1795ac27a0ecSDave Kleikamp 	}
1796ac27a0ecSDave Kleikamp 
1797617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1798617ba13bSMingming Cao 		goto cantfind_ext4;
1799bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
1800bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
1801bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
1802bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1803bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
1804617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1805617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
1806ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1807ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
1808ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
1809617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs: not enough memory\n");
1810ac27a0ecSDave Kleikamp 		goto failed_mount;
1811ac27a0ecSDave Kleikamp 	}
1812ac27a0ecSDave Kleikamp 
1813ac27a0ecSDave Kleikamp 	bgl_lock_init(&sbi->s_blockgroup_lock);
1814ac27a0ecSDave Kleikamp 
1815ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
181670bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
1817ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
1818ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
1819617ba13bSMingming Cao 			printk (KERN_ERR "EXT4-fs: "
1820ac27a0ecSDave Kleikamp 				"can't read group descriptor %d\n", i);
1821ac27a0ecSDave Kleikamp 			db_count = i;
1822ac27a0ecSDave Kleikamp 			goto failed_mount2;
1823ac27a0ecSDave Kleikamp 		}
1824ac27a0ecSDave Kleikamp 	}
1825617ba13bSMingming Cao 	if (!ext4_check_descriptors (sb)) {
1826617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
1827ac27a0ecSDave Kleikamp 		goto failed_mount2;
1828ac27a0ecSDave Kleikamp 	}
1829ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
1830ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1831ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
1832ac27a0ecSDave Kleikamp 
1833833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
1834617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
1835833f4077SPeter Zijlstra 	if (!err) {
1836833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
1837617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
1838833f4077SPeter Zijlstra 	}
1839833f4077SPeter Zijlstra 	if (!err) {
1840833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
1841617ba13bSMingming Cao 				ext4_count_dirs(sb));
1842833f4077SPeter Zijlstra 	}
1843833f4077SPeter Zijlstra 	if (err) {
1844833f4077SPeter Zijlstra 		printk(KERN_ERR "EXT4-fs: insufficient memory\n");
1845833f4077SPeter Zijlstra 		goto failed_mount3;
1846833f4077SPeter Zijlstra 	}
1847ac27a0ecSDave Kleikamp 
1848ac27a0ecSDave Kleikamp 	/* per fileystem reservation list head & lock */
1849ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_rsv_window_lock);
1850ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_root = RB_ROOT;
1851ac27a0ecSDave Kleikamp 	/* Add a single, static dummy reservation to the start of the
1852ac27a0ecSDave Kleikamp 	 * reservation window list --- it gives us a placeholder for
1853ac27a0ecSDave Kleikamp 	 * append-at-start-of-list which makes the allocation logic
1854ac27a0ecSDave Kleikamp 	 * _much_ simpler. */
1855617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1856617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1857ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1858ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_goal_size = 0;
1859617ba13bSMingming Cao 	ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
1860ac27a0ecSDave Kleikamp 
1861ac27a0ecSDave Kleikamp 	/*
1862ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
1863ac27a0ecSDave Kleikamp 	 */
1864617ba13bSMingming Cao 	sb->s_op = &ext4_sops;
1865617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
1866617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
1867ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1868617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
1869617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
1870ac27a0ecSDave Kleikamp #endif
1871ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1872ac27a0ecSDave Kleikamp 
1873ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
1874ac27a0ecSDave Kleikamp 
1875ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
1876617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
1877617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
1878ac27a0ecSDave Kleikamp 
1879ac27a0ecSDave Kleikamp 	/*
1880ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
1881ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
1882ac27a0ecSDave Kleikamp 	 */
1883ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
1884617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1885617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
1886ac27a0ecSDave Kleikamp 			goto failed_mount3;
1887ac27a0ecSDave Kleikamp 	} else if (journal_inum) {
1888617ba13bSMingming Cao 		if (ext4_create_journal(sb, es, journal_inum))
1889ac27a0ecSDave Kleikamp 			goto failed_mount3;
1890ac27a0ecSDave Kleikamp 	} else {
1891ac27a0ecSDave Kleikamp 		if (!silent)
1892ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1893617ba13bSMingming Cao 				"ext4: No journal on filesystem on %s\n",
1894ac27a0ecSDave Kleikamp 				sb->s_id);
1895ac27a0ecSDave Kleikamp 		goto failed_mount3;
1896ac27a0ecSDave Kleikamp 	}
1897ac27a0ecSDave Kleikamp 
1898eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
1899eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
1900eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
1901eb40a09cSJose R. Santos 		printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
1902eb40a09cSJose R. Santos 		goto failed_mount4;
1903eb40a09cSJose R. Santos 	}
1904eb40a09cSJose R. Santos 
1905ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
1906ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
1907ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
1908ac27a0ecSDave Kleikamp 	case 0:
1909ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
191063f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
191163f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
191263f57933SAndrew Morton 		 */
1913dab291afSMingming Cao 		if (jbd2_journal_check_available_features
1914dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
1915ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
1916ac27a0ecSDave Kleikamp 		else
1917ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1918ac27a0ecSDave Kleikamp 		break;
1919ac27a0ecSDave Kleikamp 
1920617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
1921617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
1922dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
1923dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
1924617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: Journal does not support "
1925ac27a0ecSDave Kleikamp 			       "requested data journaling mode\n");
1926ac27a0ecSDave Kleikamp 			goto failed_mount4;
1927ac27a0ecSDave Kleikamp 		}
1928ac27a0ecSDave Kleikamp 	default:
1929ac27a0ecSDave Kleikamp 		break;
1930ac27a0ecSDave Kleikamp 	}
1931ac27a0ecSDave Kleikamp 
1932ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
1933617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1934617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
1935ac27a0ecSDave Kleikamp 				"its supported only with writeback mode\n");
1936ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1937ac27a0ecSDave Kleikamp 		}
1938ac27a0ecSDave Kleikamp 	}
1939ac27a0ecSDave Kleikamp 	/*
1940dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
1941ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
1942ac27a0ecSDave Kleikamp 	 */
1943ac27a0ecSDave Kleikamp 
1944617ba13bSMingming Cao 	root = iget(sb, EXT4_ROOT_INO);
1945ac27a0ecSDave Kleikamp 	sb->s_root = d_alloc_root(root);
1946ac27a0ecSDave Kleikamp 	if (!sb->s_root) {
1947617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: get root inode failed\n");
1948ac27a0ecSDave Kleikamp 		iput(root);
1949ac27a0ecSDave Kleikamp 		goto failed_mount4;
1950ac27a0ecSDave Kleikamp 	}
1951ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1952ac27a0ecSDave Kleikamp 		dput(sb->s_root);
1953ac27a0ecSDave Kleikamp 		sb->s_root = NULL;
1954617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
1955ac27a0ecSDave Kleikamp 		goto failed_mount4;
1956ac27a0ecSDave Kleikamp 	}
1957ac27a0ecSDave Kleikamp 
1958617ba13bSMingming Cao 	ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1959ef7f3835SKalpak Shah 
1960ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
1961ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
1962ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
1963ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
1964ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
1965ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
1966ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
1967ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
1968ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
1969ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
1970ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
1971ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
1972ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
1973ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
1974ef7f3835SKalpak Shah 		}
1975ef7f3835SKalpak Shah 	}
1976ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
1977ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
1978ef7f3835SKalpak Shah 							sbi->s_inode_size) {
1979ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
1980ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
1981ef7f3835SKalpak Shah 		printk(KERN_INFO "EXT4-fs: required extra inode space not"
1982ef7f3835SKalpak Shah 			"available.\n");
1983ef7f3835SKalpak Shah 	}
1984ef7f3835SKalpak Shah 
1985ac27a0ecSDave Kleikamp 	/*
1986ac27a0ecSDave Kleikamp 	 * akpm: core read_super() calls in here with the superblock locked.
1987ac27a0ecSDave Kleikamp 	 * That deadlocks, because orphan cleanup needs to lock the superblock
1988ac27a0ecSDave Kleikamp 	 * in numerous places.  Here we just pop the lock - it's relatively
1989ac27a0ecSDave Kleikamp 	 * harmless, because we are now ready to accept write_super() requests,
1990ac27a0ecSDave Kleikamp 	 * and aviro says that's the only reason for hanging onto the
1991ac27a0ecSDave Kleikamp 	 * superblock lock.
1992ac27a0ecSDave Kleikamp 	 */
1993617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1994617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
1995617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
1996ac27a0ecSDave Kleikamp 	if (needs_recovery)
1997617ba13bSMingming Cao 		printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1998617ba13bSMingming Cao 	ext4_mark_recovery_complete(sb, es);
1999617ba13bSMingming Cao 	printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2000617ba13bSMingming Cao 		test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2001617ba13bSMingming Cao 		test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2002ac27a0ecSDave Kleikamp 		"writeback");
2003ac27a0ecSDave Kleikamp 
2004a86c6181SAlex Tomas 	ext4_ext_init(sb);
2005a86c6181SAlex Tomas 
2006ac27a0ecSDave Kleikamp 	lock_kernel();
2007ac27a0ecSDave Kleikamp 	return 0;
2008ac27a0ecSDave Kleikamp 
2009617ba13bSMingming Cao cantfind_ext4:
2010ac27a0ecSDave Kleikamp 	if (!silent)
2011617ba13bSMingming Cao 		printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2012ac27a0ecSDave Kleikamp 		       sb->s_id);
2013ac27a0ecSDave Kleikamp 	goto failed_mount;
2014ac27a0ecSDave Kleikamp 
2015ac27a0ecSDave Kleikamp failed_mount4:
2016dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
2017ac27a0ecSDave Kleikamp failed_mount3:
2018ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2019ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2020ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
2021ac27a0ecSDave Kleikamp failed_mount2:
2022ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2023ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2024ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2025ac27a0ecSDave Kleikamp failed_mount:
2026ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2027ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2028ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2029ac27a0ecSDave Kleikamp #endif
2030617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2031ac27a0ecSDave Kleikamp 	brelse(bh);
2032ac27a0ecSDave Kleikamp out_fail:
2033ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2034ac27a0ecSDave Kleikamp 	kfree(sbi);
2035ac27a0ecSDave Kleikamp 	lock_kernel();
2036ac27a0ecSDave Kleikamp 	return -EINVAL;
2037ac27a0ecSDave Kleikamp }
2038ac27a0ecSDave Kleikamp 
2039ac27a0ecSDave Kleikamp /*
2040ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2041ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2042ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2043ac27a0ecSDave Kleikamp  */
2044617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2045ac27a0ecSDave Kleikamp {
2046617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2047ac27a0ecSDave Kleikamp 
2048ac27a0ecSDave Kleikamp 	if (sbi->s_commit_interval)
2049ac27a0ecSDave Kleikamp 		journal->j_commit_interval = sbi->s_commit_interval;
2050617ba13bSMingming Cao 	/* We could also set up an ext4-specific default for the commit
2051ac27a0ecSDave Kleikamp 	 * interval here, but for now we'll just fall back to the jbd
2052ac27a0ecSDave Kleikamp 	 * default. */
2053ac27a0ecSDave Kleikamp 
2054ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2055ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2056dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2057ac27a0ecSDave Kleikamp 	else
2058dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
2059ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2060ac27a0ecSDave Kleikamp }
2061ac27a0ecSDave Kleikamp 
2062617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2063ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2064ac27a0ecSDave Kleikamp {
2065ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2066ac27a0ecSDave Kleikamp 	journal_t *journal;
2067ac27a0ecSDave Kleikamp 
2068ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2069ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2070ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2071ac27a0ecSDave Kleikamp 
2072ac27a0ecSDave Kleikamp 	journal_inode = iget(sb, journal_inum);
2073ac27a0ecSDave Kleikamp 	if (!journal_inode) {
2074617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: no journal found.\n");
2075ac27a0ecSDave Kleikamp 		return NULL;
2076ac27a0ecSDave Kleikamp 	}
2077ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2078ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2079ac27a0ecSDave Kleikamp 		iput(journal_inode);
2080617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2081ac27a0ecSDave Kleikamp 		return NULL;
2082ac27a0ecSDave Kleikamp 	}
2083ac27a0ecSDave Kleikamp 
2084ac27a0ecSDave Kleikamp 	jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2085ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
2086ac27a0ecSDave Kleikamp 	if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
2087617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2088ac27a0ecSDave Kleikamp 		iput(journal_inode);
2089ac27a0ecSDave Kleikamp 		return NULL;
2090ac27a0ecSDave Kleikamp 	}
2091ac27a0ecSDave Kleikamp 
2092dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
2093ac27a0ecSDave Kleikamp 	if (!journal) {
2094617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2095ac27a0ecSDave Kleikamp 		iput(journal_inode);
2096ac27a0ecSDave Kleikamp 		return NULL;
2097ac27a0ecSDave Kleikamp 	}
2098ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2099617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2100ac27a0ecSDave Kleikamp 	return journal;
2101ac27a0ecSDave Kleikamp }
2102ac27a0ecSDave Kleikamp 
2103617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
2104ac27a0ecSDave Kleikamp 				       dev_t j_dev)
2105ac27a0ecSDave Kleikamp {
2106ac27a0ecSDave Kleikamp 	struct buffer_head * bh;
2107ac27a0ecSDave Kleikamp 	journal_t *journal;
2108617ba13bSMingming Cao 	ext4_fsblk_t start;
2109617ba13bSMingming Cao 	ext4_fsblk_t len;
2110ac27a0ecSDave Kleikamp 	int hblock, blocksize;
2111617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
2112ac27a0ecSDave Kleikamp 	unsigned long offset;
2113617ba13bSMingming Cao 	struct ext4_super_block * es;
2114ac27a0ecSDave Kleikamp 	struct block_device *bdev;
2115ac27a0ecSDave Kleikamp 
2116617ba13bSMingming Cao 	bdev = ext4_blkdev_get(j_dev);
2117ac27a0ecSDave Kleikamp 	if (bdev == NULL)
2118ac27a0ecSDave Kleikamp 		return NULL;
2119ac27a0ecSDave Kleikamp 
2120ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
2121ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2122617ba13bSMingming Cao 			"EXT4: failed to claim external journal device.\n");
2123ac27a0ecSDave Kleikamp 		blkdev_put(bdev);
2124ac27a0ecSDave Kleikamp 		return NULL;
2125ac27a0ecSDave Kleikamp 	}
2126ac27a0ecSDave Kleikamp 
2127ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
2128ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(bdev);
2129ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
2130ac27a0ecSDave Kleikamp 		printk(KERN_ERR
2131617ba13bSMingming Cao 			"EXT4-fs: blocksize too small for journal device.\n");
2132ac27a0ecSDave Kleikamp 		goto out_bdev;
2133ac27a0ecSDave Kleikamp 	}
2134ac27a0ecSDave Kleikamp 
2135617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2136617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2137ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
2138ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
2139617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2140ac27a0ecSDave Kleikamp 		       "external journal\n");
2141ac27a0ecSDave Kleikamp 		goto out_bdev;
2142ac27a0ecSDave Kleikamp 	}
2143ac27a0ecSDave Kleikamp 
2144617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2145617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2146ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
2147617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2148617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: external journal has "
2149ac27a0ecSDave Kleikamp 					"bad superblock\n");
2150ac27a0ecSDave Kleikamp 		brelse(bh);
2151ac27a0ecSDave Kleikamp 		goto out_bdev;
2152ac27a0ecSDave Kleikamp 	}
2153ac27a0ecSDave Kleikamp 
2154617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2155617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2156ac27a0ecSDave Kleikamp 		brelse(bh);
2157ac27a0ecSDave Kleikamp 		goto out_bdev;
2158ac27a0ecSDave Kleikamp 	}
2159ac27a0ecSDave Kleikamp 
2160bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
2161ac27a0ecSDave Kleikamp 	start = sb_block + 1;
2162ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
2163ac27a0ecSDave Kleikamp 
2164dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2165ac27a0ecSDave Kleikamp 					start, len, blocksize);
2166ac27a0ecSDave Kleikamp 	if (!journal) {
2167617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2168ac27a0ecSDave Kleikamp 		goto out_bdev;
2169ac27a0ecSDave Kleikamp 	}
2170ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2171ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
2172ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
2173ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
2174617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2175ac27a0ecSDave Kleikamp 		goto out_journal;
2176ac27a0ecSDave Kleikamp 	}
2177ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2178617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: External journal has more than one "
2179ac27a0ecSDave Kleikamp 					"user (unsupported) - %d\n",
2180ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
2181ac27a0ecSDave Kleikamp 		goto out_journal;
2182ac27a0ecSDave Kleikamp 	}
2183617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
2184617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2185ac27a0ecSDave Kleikamp 	return journal;
2186ac27a0ecSDave Kleikamp out_journal:
2187dab291afSMingming Cao 	jbd2_journal_destroy(journal);
2188ac27a0ecSDave Kleikamp out_bdev:
2189617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
2190ac27a0ecSDave Kleikamp 	return NULL;
2191ac27a0ecSDave Kleikamp }
2192ac27a0ecSDave Kleikamp 
2193617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
2194617ba13bSMingming Cao 			     struct ext4_super_block *es,
2195ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
2196ac27a0ecSDave Kleikamp {
2197ac27a0ecSDave Kleikamp 	journal_t *journal;
2198ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2199ac27a0ecSDave Kleikamp 	dev_t journal_dev;
2200ac27a0ecSDave Kleikamp 	int err = 0;
2201ac27a0ecSDave Kleikamp 	int really_read_only;
2202ac27a0ecSDave Kleikamp 
2203ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2204ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2205617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2206ac27a0ecSDave Kleikamp 			"numbers have changed\n");
2207ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
2208ac27a0ecSDave Kleikamp 	} else
2209ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2210ac27a0ecSDave Kleikamp 
2211ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
2212ac27a0ecSDave Kleikamp 
2213ac27a0ecSDave Kleikamp 	/*
2214ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
2215ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
2216ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
2217ac27a0ecSDave Kleikamp 	 */
2218ac27a0ecSDave Kleikamp 
2219617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2220ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
2221617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: INFO: recovery "
2222ac27a0ecSDave Kleikamp 					"required on readonly filesystem.\n");
2223ac27a0ecSDave Kleikamp 			if (really_read_only) {
2224617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: write access "
2225ac27a0ecSDave Kleikamp 					"unavailable, cannot proceed.\n");
2226ac27a0ecSDave Kleikamp 				return -EROFS;
2227ac27a0ecSDave Kleikamp 			}
2228617ba13bSMingming Cao 			printk (KERN_INFO "EXT4-fs: write access will "
2229ac27a0ecSDave Kleikamp 					"be enabled during recovery.\n");
2230ac27a0ecSDave Kleikamp 		}
2231ac27a0ecSDave Kleikamp 	}
2232ac27a0ecSDave Kleikamp 
2233ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
2234617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2235ac27a0ecSDave Kleikamp 		       "and inode journals!\n");
2236ac27a0ecSDave Kleikamp 		return -EINVAL;
2237ac27a0ecSDave Kleikamp 	}
2238ac27a0ecSDave Kleikamp 
2239ac27a0ecSDave Kleikamp 	if (journal_inum) {
2240617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
2241ac27a0ecSDave Kleikamp 			return -EINVAL;
2242ac27a0ecSDave Kleikamp 	} else {
2243617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2244ac27a0ecSDave Kleikamp 			return -EINVAL;
2245ac27a0ecSDave Kleikamp 	}
2246ac27a0ecSDave Kleikamp 
2247ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2248dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
2249ac27a0ecSDave Kleikamp 		if (err)  {
2250617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2251dab291afSMingming Cao 			jbd2_journal_destroy(journal);
2252ac27a0ecSDave Kleikamp 			return err;
2253ac27a0ecSDave Kleikamp 		}
2254ac27a0ecSDave Kleikamp 	}
2255ac27a0ecSDave Kleikamp 
2256617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2257dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
2258ac27a0ecSDave Kleikamp 	if (!err)
2259dab291afSMingming Cao 		err = jbd2_journal_load(journal);
2260ac27a0ecSDave Kleikamp 
2261ac27a0ecSDave Kleikamp 	if (err) {
2262617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2263dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2264ac27a0ecSDave Kleikamp 		return err;
2265ac27a0ecSDave Kleikamp 	}
2266ac27a0ecSDave Kleikamp 
2267617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2268617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
2269ac27a0ecSDave Kleikamp 
2270ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2271ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2272ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
2273ac27a0ecSDave Kleikamp 		sb->s_dirt = 1;
2274ac27a0ecSDave Kleikamp 
2275ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
2276617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2277ac27a0ecSDave Kleikamp 	}
2278ac27a0ecSDave Kleikamp 
2279ac27a0ecSDave Kleikamp 	return 0;
2280ac27a0ecSDave Kleikamp }
2281ac27a0ecSDave Kleikamp 
2282617ba13bSMingming Cao static int ext4_create_journal(struct super_block * sb,
2283617ba13bSMingming Cao 			       struct ext4_super_block * es,
2284ac27a0ecSDave Kleikamp 			       unsigned int journal_inum)
2285ac27a0ecSDave Kleikamp {
2286ac27a0ecSDave Kleikamp 	journal_t *journal;
22876c675bd4SBorislav Petkov 	int err;
2288ac27a0ecSDave Kleikamp 
2289ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY) {
2290617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2291ac27a0ecSDave Kleikamp 				"create journal.\n");
2292ac27a0ecSDave Kleikamp 		return -EROFS;
2293ac27a0ecSDave Kleikamp 	}
2294ac27a0ecSDave Kleikamp 
22956c675bd4SBorislav Petkov 	journal = ext4_get_journal(sb, journal_inum);
22966c675bd4SBorislav Petkov 	if (!journal)
2297ac27a0ecSDave Kleikamp 		return -EINVAL;
2298ac27a0ecSDave Kleikamp 
2299617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2300ac27a0ecSDave Kleikamp 	       journal_inum);
2301ac27a0ecSDave Kleikamp 
23026c675bd4SBorislav Petkov 	err = jbd2_journal_create(journal);
23036c675bd4SBorislav Petkov 	if (err) {
2304617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2305dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2306ac27a0ecSDave Kleikamp 		return -EIO;
2307ac27a0ecSDave Kleikamp 	}
2308ac27a0ecSDave Kleikamp 
2309617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2310ac27a0ecSDave Kleikamp 
2311617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
2312617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2313617ba13bSMingming Cao 	EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2314ac27a0ecSDave Kleikamp 
2315ac27a0ecSDave Kleikamp 	es->s_journal_inum = cpu_to_le32(journal_inum);
2316ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
2317ac27a0ecSDave Kleikamp 
2318ac27a0ecSDave Kleikamp 	/* Make sure we flush the recovery flag to disk. */
2319617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
2320ac27a0ecSDave Kleikamp 
2321ac27a0ecSDave Kleikamp 	return 0;
2322ac27a0ecSDave Kleikamp }
2323ac27a0ecSDave Kleikamp 
2324617ba13bSMingming Cao static void ext4_commit_super (struct super_block * sb,
2325617ba13bSMingming Cao 			       struct ext4_super_block * es,
2326ac27a0ecSDave Kleikamp 			       int sync)
2327ac27a0ecSDave Kleikamp {
2328617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2329ac27a0ecSDave Kleikamp 
2330ac27a0ecSDave Kleikamp 	if (!sbh)
2331ac27a0ecSDave Kleikamp 		return;
2332ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
2333bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2334617ba13bSMingming Cao 	es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2335ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
2336ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
2337ac27a0ecSDave Kleikamp 	if (sync)
2338ac27a0ecSDave Kleikamp 		sync_dirty_buffer(sbh);
2339ac27a0ecSDave Kleikamp }
2340ac27a0ecSDave Kleikamp 
2341ac27a0ecSDave Kleikamp 
2342ac27a0ecSDave Kleikamp /*
2343ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
2344ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
2345ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
2346ac27a0ecSDave Kleikamp  */
2347617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block * sb,
2348617ba13bSMingming Cao 					struct ext4_super_block * es)
2349ac27a0ecSDave Kleikamp {
2350617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
2351ac27a0ecSDave Kleikamp 
2352dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
2353dab291afSMingming Cao 	jbd2_journal_flush(journal);
235432c37730SJan Kara 	lock_super(sb);
2355617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2356ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
2357617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2358ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
2359617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2360ac27a0ecSDave Kleikamp 	}
236132c37730SJan Kara 	unlock_super(sb);
2362dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
2363ac27a0ecSDave Kleikamp }
2364ac27a0ecSDave Kleikamp 
2365ac27a0ecSDave Kleikamp /*
2366ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
2367ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
2368ac27a0ecSDave Kleikamp  * main filesystem now.
2369ac27a0ecSDave Kleikamp  */
2370617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block * sb,
2371617ba13bSMingming Cao 				   struct ext4_super_block * es)
2372ac27a0ecSDave Kleikamp {
2373ac27a0ecSDave Kleikamp 	journal_t *journal;
2374ac27a0ecSDave Kleikamp 	int j_errno;
2375ac27a0ecSDave Kleikamp 	const char *errstr;
2376ac27a0ecSDave Kleikamp 
2377617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2378ac27a0ecSDave Kleikamp 
2379ac27a0ecSDave Kleikamp 	/*
2380ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
2381617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
2382ac27a0ecSDave Kleikamp 	 */
2383ac27a0ecSDave Kleikamp 
2384dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
2385ac27a0ecSDave Kleikamp 	if (j_errno) {
2386ac27a0ecSDave Kleikamp 		char nbuf[16];
2387ac27a0ecSDave Kleikamp 
2388617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
2389617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
2390ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
2391617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
2392ac27a0ecSDave Kleikamp 			     "filesystem check.");
2393ac27a0ecSDave Kleikamp 
2394617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2395617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2396617ba13bSMingming Cao 		ext4_commit_super (sb, es, 1);
2397ac27a0ecSDave Kleikamp 
2398dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
2399ac27a0ecSDave Kleikamp 	}
2400ac27a0ecSDave Kleikamp }
2401ac27a0ecSDave Kleikamp 
2402ac27a0ecSDave Kleikamp /*
2403ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
2404ac27a0ecSDave Kleikamp  * and wait on the commit.
2405ac27a0ecSDave Kleikamp  */
2406617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
2407ac27a0ecSDave Kleikamp {
2408ac27a0ecSDave Kleikamp 	journal_t *journal;
2409ac27a0ecSDave Kleikamp 	int ret;
2410ac27a0ecSDave Kleikamp 
2411ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
2412ac27a0ecSDave Kleikamp 		return 0;
2413ac27a0ecSDave Kleikamp 
2414617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2415ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2416617ba13bSMingming Cao 	ret = ext4_journal_force_commit(journal);
2417ac27a0ecSDave Kleikamp 	return ret;
2418ac27a0ecSDave Kleikamp }
2419ac27a0ecSDave Kleikamp 
2420ac27a0ecSDave Kleikamp /*
2421617ba13bSMingming Cao  * Ext4 always journals updates to the superblock itself, so we don't
2422ac27a0ecSDave Kleikamp  * have to propagate any other updates to the superblock on disk at this
2423ac27a0ecSDave Kleikamp  * point.  Just start an async writeback to get the buffers on their way
2424ac27a0ecSDave Kleikamp  * to the disk.
2425ac27a0ecSDave Kleikamp  *
2426ac27a0ecSDave Kleikamp  * This implicitly triggers the writebehind on sync().
2427ac27a0ecSDave Kleikamp  */
2428ac27a0ecSDave Kleikamp 
2429617ba13bSMingming Cao static void ext4_write_super (struct super_block * sb)
2430ac27a0ecSDave Kleikamp {
2431ac27a0ecSDave Kleikamp 	if (mutex_trylock(&sb->s_lock) != 0)
2432ac27a0ecSDave Kleikamp 		BUG();
2433ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2434ac27a0ecSDave Kleikamp }
2435ac27a0ecSDave Kleikamp 
2436617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
2437ac27a0ecSDave Kleikamp {
2438ac27a0ecSDave Kleikamp 	tid_t target;
2439ac27a0ecSDave Kleikamp 
2440ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2441dab291afSMingming Cao 	if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
2442ac27a0ecSDave Kleikamp 		if (wait)
2443dab291afSMingming Cao 			jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
2444ac27a0ecSDave Kleikamp 	}
2445ac27a0ecSDave Kleikamp 	return 0;
2446ac27a0ecSDave Kleikamp }
2447ac27a0ecSDave Kleikamp 
2448ac27a0ecSDave Kleikamp /*
2449ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
2450ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
2451ac27a0ecSDave Kleikamp  */
2452617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb)
2453ac27a0ecSDave Kleikamp {
2454ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2455ac27a0ecSDave Kleikamp 
2456ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2457617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
2458ac27a0ecSDave Kleikamp 
2459ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
2460dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
2461dab291afSMingming Cao 		jbd2_journal_flush(journal);
2462ac27a0ecSDave Kleikamp 
2463ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
2464617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2465617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2466ac27a0ecSDave Kleikamp 	}
2467ac27a0ecSDave Kleikamp }
2468ac27a0ecSDave Kleikamp 
2469ac27a0ecSDave Kleikamp /*
2470ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
2471ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
2472ac27a0ecSDave Kleikamp  */
2473617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb)
2474ac27a0ecSDave Kleikamp {
2475ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2476ac27a0ecSDave Kleikamp 		lock_super(sb);
2477ac27a0ecSDave Kleikamp 		/* Reser the needs_recovery flag before the fs is unlocked. */
2478617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2479617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2480ac27a0ecSDave Kleikamp 		unlock_super(sb);
2481dab291afSMingming Cao 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2482ac27a0ecSDave Kleikamp 	}
2483ac27a0ecSDave Kleikamp }
2484ac27a0ecSDave Kleikamp 
2485617ba13bSMingming Cao static int ext4_remount (struct super_block * sb, int * flags, char * data)
2486ac27a0ecSDave Kleikamp {
2487617ba13bSMingming Cao 	struct ext4_super_block * es;
2488617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2489617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
2490ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
2491617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
2492ac27a0ecSDave Kleikamp 	int err;
2493ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2494ac27a0ecSDave Kleikamp 	int i;
2495ac27a0ecSDave Kleikamp #endif
2496ac27a0ecSDave Kleikamp 
2497ac27a0ecSDave Kleikamp 	/* Store the original options */
2498ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
2499ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
2500ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
2501ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
2502ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
2503ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2504ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2505ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2506ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2507ac27a0ecSDave Kleikamp #endif
2508ac27a0ecSDave Kleikamp 
2509ac27a0ecSDave Kleikamp 	/*
2510ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
2511ac27a0ecSDave Kleikamp 	 */
2512ac27a0ecSDave Kleikamp 	if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2513ac27a0ecSDave Kleikamp 		err = -EINVAL;
2514ac27a0ecSDave Kleikamp 		goto restore_opts;
2515ac27a0ecSDave Kleikamp 	}
2516ac27a0ecSDave Kleikamp 
2517617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2518617ba13bSMingming Cao 		ext4_abort(sb, __FUNCTION__, "Abort forced by user");
2519ac27a0ecSDave Kleikamp 
2520ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2521617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2522ac27a0ecSDave Kleikamp 
2523ac27a0ecSDave Kleikamp 	es = sbi->s_es;
2524ac27a0ecSDave Kleikamp 
2525617ba13bSMingming Cao 	ext4_init_journal_params(sb, sbi->s_journal);
2526ac27a0ecSDave Kleikamp 
2527ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2528bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
2529617ba13bSMingming Cao 		if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
2530ac27a0ecSDave Kleikamp 			err = -EROFS;
2531ac27a0ecSDave Kleikamp 			goto restore_opts;
2532ac27a0ecSDave Kleikamp 		}
2533ac27a0ecSDave Kleikamp 
2534ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
2535ac27a0ecSDave Kleikamp 			/*
2536ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
2537ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
2538ac27a0ecSDave Kleikamp 			 */
2539ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
2540ac27a0ecSDave Kleikamp 
2541ac27a0ecSDave Kleikamp 			/*
2542ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
2543ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
2544ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
2545ac27a0ecSDave Kleikamp 			 */
2546617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2547617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
2548ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
2549ac27a0ecSDave Kleikamp 
255032c37730SJan Kara 			/*
255132c37730SJan Kara 			 * We have to unlock super so that we can wait for
255232c37730SJan Kara 			 * transactions.
255332c37730SJan Kara 			 */
255432c37730SJan Kara 			unlock_super(sb);
2555617ba13bSMingming Cao 			ext4_mark_recovery_complete(sb, es);
255632c37730SJan Kara 			lock_super(sb);
2557ac27a0ecSDave Kleikamp 		} else {
2558ac27a0ecSDave Kleikamp 			__le32 ret;
2559617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2560617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2561617ba13bSMingming Cao 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2562ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
2563ac27a0ecSDave Kleikamp 				       "optional features (%x).\n",
2564ac27a0ecSDave Kleikamp 				       sb->s_id, le32_to_cpu(ret));
2565ac27a0ecSDave Kleikamp 				err = -EROFS;
2566ac27a0ecSDave Kleikamp 				goto restore_opts;
2567ac27a0ecSDave Kleikamp 			}
2568ead6596bSEric Sandeen 
2569ead6596bSEric Sandeen 			/*
2570ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
2571ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
2572ead6596bSEric Sandeen 			 * require a full umount/remount for now.
2573ead6596bSEric Sandeen 			 */
2574ead6596bSEric Sandeen 			if (es->s_last_orphan) {
2575ead6596bSEric Sandeen 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2576ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
2577ead6596bSEric Sandeen 				       "orphan inode list.  Please "
2578ead6596bSEric Sandeen 				       "umount/remount instead.\n",
2579ead6596bSEric Sandeen 				       sb->s_id);
2580ead6596bSEric Sandeen 				err = -EINVAL;
2581ead6596bSEric Sandeen 				goto restore_opts;
2582ead6596bSEric Sandeen 			}
2583ead6596bSEric Sandeen 
2584ac27a0ecSDave Kleikamp 			/*
2585ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
2586ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
2587ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
2588ac27a0ecSDave Kleikamp 			 * the partition.)
2589ac27a0ecSDave Kleikamp 			 */
2590617ba13bSMingming Cao 			ext4_clear_journal_err(sb, es);
2591ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
2592617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
2593ac27a0ecSDave Kleikamp 				goto restore_opts;
2594617ba13bSMingming Cao 			if (!ext4_setup_super (sb, es, 0))
2595ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
2596ac27a0ecSDave Kleikamp 		}
2597ac27a0ecSDave Kleikamp 	}
2598ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2599ac27a0ecSDave Kleikamp 	/* Release old quota file names */
2600ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2601ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
2602ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2603ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
2604ac27a0ecSDave Kleikamp #endif
2605ac27a0ecSDave Kleikamp 	return 0;
2606ac27a0ecSDave Kleikamp restore_opts:
2607ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
2608ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
2609ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
2610ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
2611ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
2612ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2613ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2614ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2615ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
2616ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2617ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
2618ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2619ac27a0ecSDave Kleikamp 	}
2620ac27a0ecSDave Kleikamp #endif
2621ac27a0ecSDave Kleikamp 	return err;
2622ac27a0ecSDave Kleikamp }
2623ac27a0ecSDave Kleikamp 
2624617ba13bSMingming Cao static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
2625ac27a0ecSDave Kleikamp {
2626ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
2627617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2628617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
2629960cc398SPekka Enberg 	u64 fsid;
2630ac27a0ecSDave Kleikamp 
26315e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
26325e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
26335e70030dSBadari Pulavarty 	} else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
26345e70030dSBadari Pulavarty 		unsigned long ngroups = sbi->s_groups_count, i;
26355e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
2636ac27a0ecSDave Kleikamp 		smp_rmb();
2637ac27a0ecSDave Kleikamp 
2638ac27a0ecSDave Kleikamp 		/*
26395e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
26405e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
26415e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
2642ac27a0ecSDave Kleikamp 		 */
2643ac27a0ecSDave Kleikamp 
2644ac27a0ecSDave Kleikamp 		/*
2645ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
2646ac27a0ecSDave Kleikamp 		 * overhead
2647ac27a0ecSDave Kleikamp 		 */
2648ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
2649ac27a0ecSDave Kleikamp 
2650ac27a0ecSDave Kleikamp 		/*
2651ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
2652ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
2653ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
2654ac27a0ecSDave Kleikamp 		 */
2655ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
2656617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
2657617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
2658ac27a0ecSDave Kleikamp 			cond_resched();
2659ac27a0ecSDave Kleikamp 		}
2660ac27a0ecSDave Kleikamp 
2661ac27a0ecSDave Kleikamp 		/*
2662ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
2663ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
2664ac27a0ecSDave Kleikamp 		 */
26655e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
26665e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
26675e70030dSBadari Pulavarty 		smp_wmb();
26685e70030dSBadari Pulavarty 		sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
2669ac27a0ecSDave Kleikamp 	}
2670ac27a0ecSDave Kleikamp 
2671617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
2672ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
26735e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
267452d9f3b4SPeter Zijlstra 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
26755e70030dSBadari Pulavarty 	es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
2676bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2677bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
2678ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
2679ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
268052d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
26815e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
2682617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
2683960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
2684960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2685960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2686960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
2687ac27a0ecSDave Kleikamp 	return 0;
2688ac27a0ecSDave Kleikamp }
2689ac27a0ecSDave Kleikamp 
2690ac27a0ecSDave Kleikamp /* Helper function for writing quotas on sync - we need to start transaction before quota file
2691ac27a0ecSDave Kleikamp  * is locked for write. Otherwise the are possible deadlocks:
2692ac27a0ecSDave Kleikamp  * Process 1                         Process 2
2693617ba13bSMingming Cao  * ext4_create()                     quota_sync()
2694dab291afSMingming Cao  *   jbd2_journal_start()                   write_dquot()
2695ac27a0ecSDave Kleikamp  *   DQUOT_INIT()                        down(dqio_mutex)
2696dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
2697ac27a0ecSDave Kleikamp  *
2698ac27a0ecSDave Kleikamp  */
2699ac27a0ecSDave Kleikamp 
2700ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2701ac27a0ecSDave Kleikamp 
2702ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
2703ac27a0ecSDave Kleikamp {
2704ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2705ac27a0ecSDave Kleikamp }
2706ac27a0ecSDave Kleikamp 
2707617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type)
2708ac27a0ecSDave Kleikamp {
2709ac27a0ecSDave Kleikamp 	handle_t *handle;
2710ac27a0ecSDave Kleikamp 	int ret, err;
2711ac27a0ecSDave Kleikamp 
2712ac27a0ecSDave Kleikamp 	/* We may create quota structure so we need to reserve enough blocks */
2713617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
2714ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2715ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2716ac27a0ecSDave Kleikamp 	ret = dquot_initialize(inode, type);
2717617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2718ac27a0ecSDave Kleikamp 	if (!ret)
2719ac27a0ecSDave Kleikamp 		ret = err;
2720ac27a0ecSDave Kleikamp 	return ret;
2721ac27a0ecSDave Kleikamp }
2722ac27a0ecSDave Kleikamp 
2723617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode)
2724ac27a0ecSDave Kleikamp {
2725ac27a0ecSDave Kleikamp 	handle_t *handle;
2726ac27a0ecSDave Kleikamp 	int ret, err;
2727ac27a0ecSDave Kleikamp 
2728ac27a0ecSDave Kleikamp 	/* We may delete quota structure so we need to reserve enough blocks */
2729617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
2730ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2731ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2732ac27a0ecSDave Kleikamp 	ret = dquot_drop(inode);
2733617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2734ac27a0ecSDave Kleikamp 	if (!ret)
2735ac27a0ecSDave Kleikamp 		ret = err;
2736ac27a0ecSDave Kleikamp 	return ret;
2737ac27a0ecSDave Kleikamp }
2738ac27a0ecSDave Kleikamp 
2739617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
2740ac27a0ecSDave Kleikamp {
2741ac27a0ecSDave Kleikamp 	int ret, err;
2742ac27a0ecSDave Kleikamp 	handle_t *handle;
2743ac27a0ecSDave Kleikamp 	struct inode *inode;
2744ac27a0ecSDave Kleikamp 
2745ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
2746617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
2747617ba13bSMingming Cao 					EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2748ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2749ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2750ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
2751617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2752ac27a0ecSDave Kleikamp 	if (!ret)
2753ac27a0ecSDave Kleikamp 		ret = err;
2754ac27a0ecSDave Kleikamp 	return ret;
2755ac27a0ecSDave Kleikamp }
2756ac27a0ecSDave Kleikamp 
2757617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
2758ac27a0ecSDave Kleikamp {
2759ac27a0ecSDave Kleikamp 	int ret, err;
2760ac27a0ecSDave Kleikamp 	handle_t *handle;
2761ac27a0ecSDave Kleikamp 
2762617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
2763617ba13bSMingming Cao 					EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2764ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2765ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2766ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
2767617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2768ac27a0ecSDave Kleikamp 	if (!ret)
2769ac27a0ecSDave Kleikamp 		ret = err;
2770ac27a0ecSDave Kleikamp 	return ret;
2771ac27a0ecSDave Kleikamp }
2772ac27a0ecSDave Kleikamp 
2773617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
2774ac27a0ecSDave Kleikamp {
2775ac27a0ecSDave Kleikamp 	int ret, err;
2776ac27a0ecSDave Kleikamp 	handle_t *handle;
2777ac27a0ecSDave Kleikamp 
2778617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
2779617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
27809c3013e9SJan Kara 	if (IS_ERR(handle)) {
27819c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
27829c3013e9SJan Kara 		dquot_release(dquot);
2783ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
27849c3013e9SJan Kara 	}
2785ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
2786617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2787ac27a0ecSDave Kleikamp 	if (!ret)
2788ac27a0ecSDave Kleikamp 		ret = err;
2789ac27a0ecSDave Kleikamp 	return ret;
2790ac27a0ecSDave Kleikamp }
2791ac27a0ecSDave Kleikamp 
2792617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
2793ac27a0ecSDave Kleikamp {
2794ac27a0ecSDave Kleikamp 	/* Are we journalling quotas? */
2795617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2796617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2797ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
2798617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
2799ac27a0ecSDave Kleikamp 	} else {
2800ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
2801ac27a0ecSDave Kleikamp 	}
2802ac27a0ecSDave Kleikamp }
2803ac27a0ecSDave Kleikamp 
2804617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
2805ac27a0ecSDave Kleikamp {
2806ac27a0ecSDave Kleikamp 	int ret, err;
2807ac27a0ecSDave Kleikamp 	handle_t *handle;
2808ac27a0ecSDave Kleikamp 
2809ac27a0ecSDave Kleikamp 	/* Data block + inode block */
2810617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
2811ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2812ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2813ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
2814617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2815ac27a0ecSDave Kleikamp 	if (!ret)
2816ac27a0ecSDave Kleikamp 		ret = err;
2817ac27a0ecSDave Kleikamp 	return ret;
2818ac27a0ecSDave Kleikamp }
2819ac27a0ecSDave Kleikamp 
2820ac27a0ecSDave Kleikamp /*
2821ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
2822ac27a0ecSDave Kleikamp  * the quota file and such...
2823ac27a0ecSDave Kleikamp  */
2824617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
2825ac27a0ecSDave Kleikamp {
2826617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2827617ba13bSMingming Cao 			EXT4_SB(sb)->s_jquota_fmt, type);
2828ac27a0ecSDave Kleikamp }
2829ac27a0ecSDave Kleikamp 
2830ac27a0ecSDave Kleikamp /*
2831ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
2832ac27a0ecSDave Kleikamp  */
2833617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
2834ac27a0ecSDave Kleikamp 			 char *path)
2835ac27a0ecSDave Kleikamp {
2836ac27a0ecSDave Kleikamp 	int err;
2837ac27a0ecSDave Kleikamp 	struct nameidata nd;
2838ac27a0ecSDave Kleikamp 
2839ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
2840ac27a0ecSDave Kleikamp 		return -EINVAL;
2841ac27a0ecSDave Kleikamp 	/* Not journalling quota? */
2842617ba13bSMingming Cao 	if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2843617ba13bSMingming Cao 	    !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
2844ac27a0ecSDave Kleikamp 		return vfs_quota_on(sb, type, format_id, path);
2845ac27a0ecSDave Kleikamp 	err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2846ac27a0ecSDave Kleikamp 	if (err)
2847ac27a0ecSDave Kleikamp 		return err;
2848ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
2849ac27a0ecSDave Kleikamp 	if (nd.mnt->mnt_sb != sb) {
2850ac27a0ecSDave Kleikamp 		path_release(&nd);
2851ac27a0ecSDave Kleikamp 		return -EXDEV;
2852ac27a0ecSDave Kleikamp 	}
2853ac27a0ecSDave Kleikamp 	/* Quotafile not of fs root? */
2854ac27a0ecSDave Kleikamp 	if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2855ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
2856617ba13bSMingming Cao 			"EXT4-fs: Quota file not on filesystem root. "
2857ac27a0ecSDave Kleikamp 			"Journalled quota will not work.\n");
2858ac27a0ecSDave Kleikamp 	path_release(&nd);
2859ac27a0ecSDave Kleikamp 	return vfs_quota_on(sb, type, format_id, path);
2860ac27a0ecSDave Kleikamp }
2861ac27a0ecSDave Kleikamp 
2862ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
2863ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
2864ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
2865ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
2866617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
2867ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
2868ac27a0ecSDave Kleikamp {
2869ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
2870617ba13bSMingming Cao 	sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2871ac27a0ecSDave Kleikamp 	int err = 0;
2872ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
2873ac27a0ecSDave Kleikamp 	int tocopy;
2874ac27a0ecSDave Kleikamp 	size_t toread;
2875ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2876ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
2877ac27a0ecSDave Kleikamp 
2878ac27a0ecSDave Kleikamp 	if (off > i_size)
2879ac27a0ecSDave Kleikamp 		return 0;
2880ac27a0ecSDave Kleikamp 	if (off+len > i_size)
2881ac27a0ecSDave Kleikamp 		len = i_size-off;
2882ac27a0ecSDave Kleikamp 	toread = len;
2883ac27a0ecSDave Kleikamp 	while (toread > 0) {
2884ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
2885ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
2886617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
2887ac27a0ecSDave Kleikamp 		if (err)
2888ac27a0ecSDave Kleikamp 			return err;
2889ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
2890ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
2891ac27a0ecSDave Kleikamp 		else
2892ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
2893ac27a0ecSDave Kleikamp 		brelse(bh);
2894ac27a0ecSDave Kleikamp 		offset = 0;
2895ac27a0ecSDave Kleikamp 		toread -= tocopy;
2896ac27a0ecSDave Kleikamp 		data += tocopy;
2897ac27a0ecSDave Kleikamp 		blk++;
2898ac27a0ecSDave Kleikamp 	}
2899ac27a0ecSDave Kleikamp 	return len;
2900ac27a0ecSDave Kleikamp }
2901ac27a0ecSDave Kleikamp 
2902ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
2903ac27a0ecSDave Kleikamp  * enough credits) */
2904617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
2905ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
2906ac27a0ecSDave Kleikamp {
2907ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
2908617ba13bSMingming Cao 	sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2909ac27a0ecSDave Kleikamp 	int err = 0;
2910ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
2911ac27a0ecSDave Kleikamp 	int tocopy;
2912617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
2913ac27a0ecSDave Kleikamp 	size_t towrite = len;
2914ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2915ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
2916ac27a0ecSDave Kleikamp 
29179c3013e9SJan Kara 	if (!handle) {
29189c3013e9SJan Kara 		printk(KERN_WARNING "EXT4-fs: Quota write (off=%Lu, len=%Lu)"
29199c3013e9SJan Kara 			" cancelled because transaction is not started.\n",
29209c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
29219c3013e9SJan Kara 		return -EIO;
29229c3013e9SJan Kara 	}
2923ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2924ac27a0ecSDave Kleikamp 	while (towrite > 0) {
2925ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
2926ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
2927617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
2928ac27a0ecSDave Kleikamp 		if (!bh)
2929ac27a0ecSDave Kleikamp 			goto out;
2930ac27a0ecSDave Kleikamp 		if (journal_quota) {
2931617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
2932ac27a0ecSDave Kleikamp 			if (err) {
2933ac27a0ecSDave Kleikamp 				brelse(bh);
2934ac27a0ecSDave Kleikamp 				goto out;
2935ac27a0ecSDave Kleikamp 			}
2936ac27a0ecSDave Kleikamp 		}
2937ac27a0ecSDave Kleikamp 		lock_buffer(bh);
2938ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
2939ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
2940ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
2941ac27a0ecSDave Kleikamp 		if (journal_quota)
2942617ba13bSMingming Cao 			err = ext4_journal_dirty_metadata(handle, bh);
2943ac27a0ecSDave Kleikamp 		else {
2944ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
2945617ba13bSMingming Cao 			err = ext4_journal_dirty_data(handle, bh);
2946ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
2947ac27a0ecSDave Kleikamp 		}
2948ac27a0ecSDave Kleikamp 		brelse(bh);
2949ac27a0ecSDave Kleikamp 		if (err)
2950ac27a0ecSDave Kleikamp 			goto out;
2951ac27a0ecSDave Kleikamp 		offset = 0;
2952ac27a0ecSDave Kleikamp 		towrite -= tocopy;
2953ac27a0ecSDave Kleikamp 		data += tocopy;
2954ac27a0ecSDave Kleikamp 		blk++;
2955ac27a0ecSDave Kleikamp 	}
2956ac27a0ecSDave Kleikamp out:
2957ac27a0ecSDave Kleikamp 	if (len == towrite)
2958ac27a0ecSDave Kleikamp 		return err;
2959ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
2960ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
2961617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
2962ac27a0ecSDave Kleikamp 	}
2963ac27a0ecSDave Kleikamp 	inode->i_version++;
2964ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2965617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
2966ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
2967ac27a0ecSDave Kleikamp 	return len - towrite;
2968ac27a0ecSDave Kleikamp }
2969ac27a0ecSDave Kleikamp 
2970ac27a0ecSDave Kleikamp #endif
2971ac27a0ecSDave Kleikamp 
2972617ba13bSMingming Cao static int ext4_get_sb(struct file_system_type *fs_type,
2973ac27a0ecSDave Kleikamp 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2974ac27a0ecSDave Kleikamp {
2975617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
2976ac27a0ecSDave Kleikamp }
2977ac27a0ecSDave Kleikamp 
2978617ba13bSMingming Cao static struct file_system_type ext4dev_fs_type = {
2979ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
2980617ba13bSMingming Cao 	.name		= "ext4dev",
2981617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
2982ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
2983ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
2984ac27a0ecSDave Kleikamp };
2985ac27a0ecSDave Kleikamp 
2986617ba13bSMingming Cao static int __init init_ext4_fs(void)
2987ac27a0ecSDave Kleikamp {
2988617ba13bSMingming Cao 	int err = init_ext4_xattr();
2989ac27a0ecSDave Kleikamp 	if (err)
2990ac27a0ecSDave Kleikamp 		return err;
2991ac27a0ecSDave Kleikamp 	err = init_inodecache();
2992ac27a0ecSDave Kleikamp 	if (err)
2993ac27a0ecSDave Kleikamp 		goto out1;
2994617ba13bSMingming Cao 	err = register_filesystem(&ext4dev_fs_type);
2995ac27a0ecSDave Kleikamp 	if (err)
2996ac27a0ecSDave Kleikamp 		goto out;
2997ac27a0ecSDave Kleikamp 	return 0;
2998ac27a0ecSDave Kleikamp out:
2999ac27a0ecSDave Kleikamp 	destroy_inodecache();
3000ac27a0ecSDave Kleikamp out1:
3001617ba13bSMingming Cao 	exit_ext4_xattr();
3002ac27a0ecSDave Kleikamp 	return err;
3003ac27a0ecSDave Kleikamp }
3004ac27a0ecSDave Kleikamp 
3005617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
3006ac27a0ecSDave Kleikamp {
3007617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
3008ac27a0ecSDave Kleikamp 	destroy_inodecache();
3009617ba13bSMingming Cao 	exit_ext4_xattr();
3010ac27a0ecSDave Kleikamp }
3011ac27a0ecSDave Kleikamp 
3012ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3013617ba13bSMingming Cao MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3014ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
3015617ba13bSMingming Cao module_init(init_ext4_fs)
3016617ba13bSMingming Cao module_exit(exit_ext4_fs)
3017