xref: /linux/fs/ext4/super.c (revision a8f48a95619cbce8f85423480e7d0a1bf971a62b)
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>
32ac27a0ecSDave Kleikamp #include <linux/vfs.h>
33ac27a0ecSDave Kleikamp #include <linux/random.h>
34ac27a0ecSDave Kleikamp #include <linux/mount.h>
35ac27a0ecSDave Kleikamp #include <linux/namei.h>
36ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
37ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
38ac27a0ecSDave Kleikamp 
39ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
40ac27a0ecSDave Kleikamp 
41ac27a0ecSDave Kleikamp #include "xattr.h"
42ac27a0ecSDave Kleikamp #include "acl.h"
43ac27a0ecSDave Kleikamp #include "namei.h"
44ac27a0ecSDave Kleikamp 
45617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
46ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
47617ba13bSMingming Cao static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
48ac27a0ecSDave Kleikamp 			       unsigned int);
49617ba13bSMingming Cao static void ext4_commit_super (struct super_block * sb,
50617ba13bSMingming Cao 			       struct ext4_super_block * es,
51ac27a0ecSDave Kleikamp 			       int sync);
52617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block * sb,
53617ba13bSMingming Cao 					struct ext4_super_block * es);
54617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block * sb,
55617ba13bSMingming Cao 				   struct ext4_super_block * es);
56617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
57617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block * sb, int errno,
58ac27a0ecSDave Kleikamp 				     char nbuf[16]);
59617ba13bSMingming Cao static int ext4_remount (struct super_block * sb, int * flags, char * data);
60617ba13bSMingming Cao static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
61617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb);
62617ba13bSMingming Cao static void ext4_write_super (struct super_block * sb);
63617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb);
64ac27a0ecSDave Kleikamp 
65bd81d8eeSLaurent Vivier 
668fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
678fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
68bd81d8eeSLaurent Vivier {
69bd81d8eeSLaurent Vivier 	return le32_to_cpu(bg->bg_block_bitmap) |
708fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
718fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
72bd81d8eeSLaurent Vivier }
73bd81d8eeSLaurent Vivier 
748fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
758fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
76bd81d8eeSLaurent Vivier {
77bd81d8eeSLaurent Vivier 	return le32_to_cpu(bg->bg_inode_bitmap) |
788fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
798fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
80bd81d8eeSLaurent Vivier }
81bd81d8eeSLaurent Vivier 
828fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
838fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
84bd81d8eeSLaurent Vivier {
85bd81d8eeSLaurent Vivier 	return le32_to_cpu(bg->bg_inode_table) |
868fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
878fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
88bd81d8eeSLaurent Vivier }
89bd81d8eeSLaurent Vivier 
908fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
918fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
92bd81d8eeSLaurent Vivier {
93bd81d8eeSLaurent Vivier 	bg->bg_block_bitmap = cpu_to_le32((u32)blk);
948fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
958fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
96bd81d8eeSLaurent Vivier }
97bd81d8eeSLaurent Vivier 
988fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
998fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
100bd81d8eeSLaurent Vivier {
101bd81d8eeSLaurent Vivier 	bg->bg_inode_bitmap  = cpu_to_le32((u32)blk);
1028fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1038fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
104bd81d8eeSLaurent Vivier }
105bd81d8eeSLaurent Vivier 
1068fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1078fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
108bd81d8eeSLaurent Vivier {
109bd81d8eeSLaurent Vivier 	bg->bg_inode_table = cpu_to_le32((u32)blk);
1108fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1118fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
112bd81d8eeSLaurent Vivier }
113bd81d8eeSLaurent Vivier 
114ac27a0ecSDave Kleikamp /*
115dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
116ac27a0ecSDave Kleikamp  *
117ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
118ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
119ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
120ac27a0ecSDave Kleikamp  * appropriate.
121ac27a0ecSDave Kleikamp  */
122617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
123ac27a0ecSDave Kleikamp {
124ac27a0ecSDave Kleikamp 	journal_t *journal;
125ac27a0ecSDave Kleikamp 
126ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
127ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
128ac27a0ecSDave Kleikamp 
129ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
130ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
131ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
132617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
133ac27a0ecSDave Kleikamp 	if (is_journal_aborted(journal)) {
134617ba13bSMingming Cao 		ext4_abort(sb, __FUNCTION__,
135ac27a0ecSDave Kleikamp 			   "Detected aborted journal");
136ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
137ac27a0ecSDave Kleikamp 	}
138ac27a0ecSDave Kleikamp 
139dab291afSMingming Cao 	return jbd2_journal_start(journal, nblocks);
140ac27a0ecSDave Kleikamp }
141ac27a0ecSDave Kleikamp 
142ac27a0ecSDave Kleikamp /*
143ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
144dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
145ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
146ac27a0ecSDave Kleikamp  * appropriate.
147ac27a0ecSDave Kleikamp  */
148617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
149ac27a0ecSDave Kleikamp {
150ac27a0ecSDave Kleikamp 	struct super_block *sb;
151ac27a0ecSDave Kleikamp 	int err;
152ac27a0ecSDave Kleikamp 	int rc;
153ac27a0ecSDave Kleikamp 
154ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
155ac27a0ecSDave Kleikamp 	err = handle->h_err;
156dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
157ac27a0ecSDave Kleikamp 
158ac27a0ecSDave Kleikamp 	if (!err)
159ac27a0ecSDave Kleikamp 		err = rc;
160ac27a0ecSDave Kleikamp 	if (err)
161617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
162ac27a0ecSDave Kleikamp 	return err;
163ac27a0ecSDave Kleikamp }
164ac27a0ecSDave Kleikamp 
165617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
166ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
167ac27a0ecSDave Kleikamp {
168ac27a0ecSDave Kleikamp 	char nbuf[16];
169617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
170ac27a0ecSDave Kleikamp 
171ac27a0ecSDave Kleikamp 	if (bh)
172ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
173ac27a0ecSDave Kleikamp 
174ac27a0ecSDave Kleikamp 	if (!handle->h_err)
175ac27a0ecSDave Kleikamp 		handle->h_err = err;
176ac27a0ecSDave Kleikamp 
177ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
178ac27a0ecSDave Kleikamp 		return;
179ac27a0ecSDave Kleikamp 
180ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
181ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
182ac27a0ecSDave Kleikamp 
183dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
184ac27a0ecSDave Kleikamp }
185ac27a0ecSDave Kleikamp 
186ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
187ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
188ac27a0ecSDave Kleikamp  *
189ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
190617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
191ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
192ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
193ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
194ac27a0ecSDave Kleikamp  * write out the superblock safely.
195ac27a0ecSDave Kleikamp  *
196dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
197ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
198ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
199ac27a0ecSDave Kleikamp  */
200ac27a0ecSDave Kleikamp 
201617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
202ac27a0ecSDave Kleikamp {
203617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
204ac27a0ecSDave Kleikamp 
205617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
206617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
207ac27a0ecSDave Kleikamp 
208ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
209ac27a0ecSDave Kleikamp 		return;
210ac27a0ecSDave Kleikamp 
211ac27a0ecSDave Kleikamp 	if (!test_opt (sb, ERRORS_CONT)) {
212617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
213ac27a0ecSDave Kleikamp 
214617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
215ac27a0ecSDave Kleikamp 		if (journal)
216dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
217ac27a0ecSDave Kleikamp 	}
218ac27a0ecSDave Kleikamp 	if (test_opt (sb, ERRORS_RO)) {
219ac27a0ecSDave Kleikamp 		printk (KERN_CRIT "Remounting filesystem read-only\n");
220ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
221ac27a0ecSDave Kleikamp 	}
222617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
223ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
224617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
225ac27a0ecSDave Kleikamp 			sb->s_id);
226ac27a0ecSDave Kleikamp }
227ac27a0ecSDave Kleikamp 
228617ba13bSMingming Cao void ext4_error (struct super_block * sb, const char * function,
229ac27a0ecSDave Kleikamp 		 const char * fmt, ...)
230ac27a0ecSDave Kleikamp {
231ac27a0ecSDave Kleikamp 	va_list args;
232ac27a0ecSDave Kleikamp 
233ac27a0ecSDave Kleikamp 	va_start(args, fmt);
234617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
235ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
236ac27a0ecSDave Kleikamp 	printk("\n");
237ac27a0ecSDave Kleikamp 	va_end(args);
238ac27a0ecSDave Kleikamp 
239617ba13bSMingming Cao 	ext4_handle_error(sb);
240ac27a0ecSDave Kleikamp }
241ac27a0ecSDave Kleikamp 
242617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block * sb, int errno,
243ac27a0ecSDave Kleikamp 				     char nbuf[16])
244ac27a0ecSDave Kleikamp {
245ac27a0ecSDave Kleikamp 	char *errstr = NULL;
246ac27a0ecSDave Kleikamp 
247ac27a0ecSDave Kleikamp 	switch (errno) {
248ac27a0ecSDave Kleikamp 	case -EIO:
249ac27a0ecSDave Kleikamp 		errstr = "IO failure";
250ac27a0ecSDave Kleikamp 		break;
251ac27a0ecSDave Kleikamp 	case -ENOMEM:
252ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
253ac27a0ecSDave Kleikamp 		break;
254ac27a0ecSDave Kleikamp 	case -EROFS:
255dab291afSMingming Cao 		if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
256ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
257ac27a0ecSDave Kleikamp 		else
258ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
259ac27a0ecSDave Kleikamp 		break;
260ac27a0ecSDave Kleikamp 	default:
261ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
262ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
263ac27a0ecSDave Kleikamp 		 * NULL. */
264ac27a0ecSDave Kleikamp 		if (nbuf) {
265ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
266ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
267ac27a0ecSDave Kleikamp 				errstr = nbuf;
268ac27a0ecSDave Kleikamp 		}
269ac27a0ecSDave Kleikamp 		break;
270ac27a0ecSDave Kleikamp 	}
271ac27a0ecSDave Kleikamp 
272ac27a0ecSDave Kleikamp 	return errstr;
273ac27a0ecSDave Kleikamp }
274ac27a0ecSDave Kleikamp 
275617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
276ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
277ac27a0ecSDave Kleikamp 
278617ba13bSMingming Cao void __ext4_std_error (struct super_block * sb, const char * function,
279ac27a0ecSDave Kleikamp 		       int errno)
280ac27a0ecSDave Kleikamp {
281ac27a0ecSDave Kleikamp 	char nbuf[16];
282ac27a0ecSDave Kleikamp 	const char *errstr;
283ac27a0ecSDave Kleikamp 
284ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
285ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
286ac27a0ecSDave Kleikamp 	 * an error. */
287ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
288ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
289ac27a0ecSDave Kleikamp 		return;
290ac27a0ecSDave Kleikamp 
291617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
292617ba13bSMingming Cao 	printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
293ac27a0ecSDave Kleikamp 		sb->s_id, function, errstr);
294ac27a0ecSDave Kleikamp 
295617ba13bSMingming Cao 	ext4_handle_error(sb);
296ac27a0ecSDave Kleikamp }
297ac27a0ecSDave Kleikamp 
298ac27a0ecSDave Kleikamp /*
299617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
300ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
301ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
302ac27a0ecSDave Kleikamp  *
303ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
304ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
305ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
306ac27a0ecSDave Kleikamp  */
307ac27a0ecSDave Kleikamp 
308617ba13bSMingming Cao void ext4_abort (struct super_block * sb, const char * function,
309ac27a0ecSDave Kleikamp 		 const char * fmt, ...)
310ac27a0ecSDave Kleikamp {
311ac27a0ecSDave Kleikamp 	va_list args;
312ac27a0ecSDave Kleikamp 
313617ba13bSMingming Cao 	printk (KERN_CRIT "ext4_abort called.\n");
314ac27a0ecSDave Kleikamp 
315ac27a0ecSDave Kleikamp 	va_start(args, fmt);
316617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
317ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
318ac27a0ecSDave Kleikamp 	printk("\n");
319ac27a0ecSDave Kleikamp 	va_end(args);
320ac27a0ecSDave Kleikamp 
321ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
322617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
323ac27a0ecSDave Kleikamp 
324ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
325ac27a0ecSDave Kleikamp 		return;
326ac27a0ecSDave Kleikamp 
327ac27a0ecSDave Kleikamp 	printk(KERN_CRIT "Remounting filesystem read-only\n");
328617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
329ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
330617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
331dab291afSMingming Cao 	jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
332ac27a0ecSDave Kleikamp }
333ac27a0ecSDave Kleikamp 
334617ba13bSMingming Cao void ext4_warning (struct super_block * sb, const char * function,
335ac27a0ecSDave Kleikamp 		   const char * fmt, ...)
336ac27a0ecSDave Kleikamp {
337ac27a0ecSDave Kleikamp 	va_list args;
338ac27a0ecSDave Kleikamp 
339ac27a0ecSDave Kleikamp 	va_start(args, fmt);
340617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
341ac27a0ecSDave Kleikamp 	       sb->s_id, function);
342ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
343ac27a0ecSDave Kleikamp 	printk("\n");
344ac27a0ecSDave Kleikamp 	va_end(args);
345ac27a0ecSDave Kleikamp }
346ac27a0ecSDave Kleikamp 
347617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
348ac27a0ecSDave Kleikamp {
349617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
350ac27a0ecSDave Kleikamp 
351617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
352ac27a0ecSDave Kleikamp 		return;
353ac27a0ecSDave Kleikamp 
354617ba13bSMingming Cao 	ext4_warning(sb, __FUNCTION__,
355ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
356ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
357617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
358ac27a0ecSDave Kleikamp 
359617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
360617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
361617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
362ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
363ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
364ac27a0ecSDave Kleikamp 
365ac27a0ecSDave Kleikamp 	/*
366ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
367ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
368ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
369ac27a0ecSDave Kleikamp 	 */
370ac27a0ecSDave Kleikamp }
371ac27a0ecSDave Kleikamp 
372ac27a0ecSDave Kleikamp /*
373ac27a0ecSDave Kleikamp  * Open the external journal device
374ac27a0ecSDave Kleikamp  */
375617ba13bSMingming Cao static struct block_device *ext4_blkdev_get(dev_t dev)
376ac27a0ecSDave Kleikamp {
377ac27a0ecSDave Kleikamp 	struct block_device *bdev;
378ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
379ac27a0ecSDave Kleikamp 
380ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
381ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
382ac27a0ecSDave Kleikamp 		goto fail;
383ac27a0ecSDave Kleikamp 	return bdev;
384ac27a0ecSDave Kleikamp 
385ac27a0ecSDave Kleikamp fail:
386617ba13bSMingming Cao 	printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
387ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
388ac27a0ecSDave Kleikamp 	return NULL;
389ac27a0ecSDave Kleikamp }
390ac27a0ecSDave Kleikamp 
391ac27a0ecSDave Kleikamp /*
392ac27a0ecSDave Kleikamp  * Release the journal device
393ac27a0ecSDave Kleikamp  */
394617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
395ac27a0ecSDave Kleikamp {
396ac27a0ecSDave Kleikamp 	bd_release(bdev);
397ac27a0ecSDave Kleikamp 	return blkdev_put(bdev);
398ac27a0ecSDave Kleikamp }
399ac27a0ecSDave Kleikamp 
400617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
401ac27a0ecSDave Kleikamp {
402ac27a0ecSDave Kleikamp 	struct block_device *bdev;
403ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
404ac27a0ecSDave Kleikamp 
405ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
406ac27a0ecSDave Kleikamp 	if (bdev) {
407617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
408ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
409ac27a0ecSDave Kleikamp 	}
410ac27a0ecSDave Kleikamp 	return ret;
411ac27a0ecSDave Kleikamp }
412ac27a0ecSDave Kleikamp 
413ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
414ac27a0ecSDave Kleikamp {
415617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
416ac27a0ecSDave Kleikamp }
417ac27a0ecSDave Kleikamp 
418617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
419ac27a0ecSDave Kleikamp {
420ac27a0ecSDave Kleikamp 	struct list_head *l;
421ac27a0ecSDave Kleikamp 
422ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb orphan head is %d\n",
423ac27a0ecSDave Kleikamp 	       le32_to_cpu(sbi->s_es->s_last_orphan));
424ac27a0ecSDave Kleikamp 
425ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
426ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
427ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
428ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
429ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
430ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
431ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
432ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
433ac27a0ecSDave Kleikamp 	}
434ac27a0ecSDave Kleikamp }
435ac27a0ecSDave Kleikamp 
436617ba13bSMingming Cao static void ext4_put_super (struct super_block * sb)
437ac27a0ecSDave Kleikamp {
438617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
439617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
440ac27a0ecSDave Kleikamp 	int i;
441ac27a0ecSDave Kleikamp 
442a86c6181SAlex Tomas 	ext4_ext_release(sb);
443617ba13bSMingming Cao 	ext4_xattr_put_super(sb);
444dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
445ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
446617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
447ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
448ac27a0ecSDave Kleikamp 		BUFFER_TRACE(sbi->s_sbh, "marking dirty");
449ac27a0ecSDave Kleikamp 		mark_buffer_dirty(sbi->s_sbh);
450617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
451ac27a0ecSDave Kleikamp 	}
452ac27a0ecSDave Kleikamp 
453ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
454ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
455ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
456ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
457ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
458ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
459ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
460ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
461ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
462ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
463ac27a0ecSDave Kleikamp #endif
464ac27a0ecSDave Kleikamp 
465ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
466ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
467ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
468ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
469ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
470ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
471ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
472ac27a0ecSDave Kleikamp 
473ac27a0ecSDave Kleikamp 	invalidate_bdev(sb->s_bdev, 0);
474ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
475ac27a0ecSDave Kleikamp 		/*
476ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
477ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
478ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
479ac27a0ecSDave Kleikamp 		 */
480ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
481ac27a0ecSDave Kleikamp 		invalidate_bdev(sbi->journal_bdev, 0);
482617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
483ac27a0ecSDave Kleikamp 	}
484ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
485ac27a0ecSDave Kleikamp 	kfree(sbi);
486ac27a0ecSDave Kleikamp 	return;
487ac27a0ecSDave Kleikamp }
488ac27a0ecSDave Kleikamp 
489e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
490ac27a0ecSDave Kleikamp 
491ac27a0ecSDave Kleikamp /*
492ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
493ac27a0ecSDave Kleikamp  */
494617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
495ac27a0ecSDave Kleikamp {
496617ba13bSMingming Cao 	struct ext4_inode_info *ei;
497ac27a0ecSDave Kleikamp 
498e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
499ac27a0ecSDave Kleikamp 	if (!ei)
500ac27a0ecSDave Kleikamp 		return NULL;
501617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
502617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
503617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
504ac27a0ecSDave Kleikamp #endif
505ac27a0ecSDave Kleikamp 	ei->i_block_alloc_info = NULL;
506ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
507a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
508ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
509ac27a0ecSDave Kleikamp }
510ac27a0ecSDave Kleikamp 
511617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
512ac27a0ecSDave Kleikamp {
513617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
514ac27a0ecSDave Kleikamp }
515ac27a0ecSDave Kleikamp 
516e18b890bSChristoph Lameter static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
517ac27a0ecSDave Kleikamp {
518617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
519ac27a0ecSDave Kleikamp 
520ac27a0ecSDave Kleikamp 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
521ac27a0ecSDave Kleikamp 	    SLAB_CTOR_CONSTRUCTOR) {
522ac27a0ecSDave Kleikamp 		INIT_LIST_HEAD(&ei->i_orphan);
523617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
524ac27a0ecSDave Kleikamp 		init_rwsem(&ei->xattr_sem);
525ac27a0ecSDave Kleikamp #endif
526ac27a0ecSDave Kleikamp 		mutex_init(&ei->truncate_mutex);
527ac27a0ecSDave Kleikamp 		inode_init_once(&ei->vfs_inode);
528ac27a0ecSDave Kleikamp 	}
529ac27a0ecSDave Kleikamp }
530ac27a0ecSDave Kleikamp 
531ac27a0ecSDave Kleikamp static int init_inodecache(void)
532ac27a0ecSDave Kleikamp {
533617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
534617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
535ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
536ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
537ac27a0ecSDave Kleikamp 					     init_once, NULL);
538617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
539ac27a0ecSDave Kleikamp 		return -ENOMEM;
540ac27a0ecSDave Kleikamp 	return 0;
541ac27a0ecSDave Kleikamp }
542ac27a0ecSDave Kleikamp 
543ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
544ac27a0ecSDave Kleikamp {
545617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
546ac27a0ecSDave Kleikamp }
547ac27a0ecSDave Kleikamp 
548617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
549ac27a0ecSDave Kleikamp {
550617ba13bSMingming Cao 	struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
551617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
552617ba13bSMingming Cao 	if (EXT4_I(inode)->i_acl &&
553617ba13bSMingming Cao 			EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
554617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_acl);
555617ba13bSMingming Cao 		EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
556ac27a0ecSDave Kleikamp 	}
557617ba13bSMingming Cao 	if (EXT4_I(inode)->i_default_acl &&
558617ba13bSMingming Cao 			EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
559617ba13bSMingming Cao 		posix_acl_release(EXT4_I(inode)->i_default_acl);
560617ba13bSMingming Cao 		EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
561ac27a0ecSDave Kleikamp 	}
562ac27a0ecSDave Kleikamp #endif
563617ba13bSMingming Cao 	ext4_discard_reservation(inode);
564617ba13bSMingming Cao 	EXT4_I(inode)->i_block_alloc_info = NULL;
565ac27a0ecSDave Kleikamp 	if (unlikely(rsv))
566ac27a0ecSDave Kleikamp 		kfree(rsv);
567ac27a0ecSDave Kleikamp }
568ac27a0ecSDave Kleikamp 
569617ba13bSMingming Cao static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
570ac27a0ecSDave Kleikamp {
571ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
572617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
573ac27a0ecSDave Kleikamp 
574ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
575ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
576ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
577ac27a0ecSDave Kleikamp 
578ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
579ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
580ac27a0ecSDave Kleikamp 
581ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
582ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
583ac27a0ecSDave Kleikamp 
584617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
585ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
586ac27a0ecSDave Kleikamp 
587617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
588ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
589ac27a0ecSDave Kleikamp #endif
590ac27a0ecSDave Kleikamp }
591ac27a0ecSDave Kleikamp 
592617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
593ac27a0ecSDave Kleikamp {
594ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
595ac27a0ecSDave Kleikamp 
596617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
597ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
598617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
599ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
600617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
601ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
602ac27a0ecSDave Kleikamp 
603617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
604ac27a0ecSDave Kleikamp 
605ac27a0ecSDave Kleikamp 	return 0;
606ac27a0ecSDave Kleikamp }
607ac27a0ecSDave Kleikamp 
608ac27a0ecSDave Kleikamp 
609617ba13bSMingming Cao static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
610ac27a0ecSDave Kleikamp {
611ac27a0ecSDave Kleikamp 	__u32 *objp = vobjp;
612ac27a0ecSDave Kleikamp 	unsigned long ino = objp[0];
613ac27a0ecSDave Kleikamp 	__u32 generation = objp[1];
614ac27a0ecSDave Kleikamp 	struct inode *inode;
615ac27a0ecSDave Kleikamp 	struct dentry *result;
616ac27a0ecSDave Kleikamp 
617617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
618ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
619617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
620ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
621ac27a0ecSDave Kleikamp 
622ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
623ac27a0ecSDave Kleikamp 	 *
624617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
625ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
626ac27a0ecSDave Kleikamp 	 *
627ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
628ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
629ac27a0ecSDave Kleikamp 	 */
630ac27a0ecSDave Kleikamp 	inode = iget(sb, ino);
631ac27a0ecSDave Kleikamp 	if (inode == NULL)
632ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
633ac27a0ecSDave Kleikamp 	if (is_bad_inode(inode) ||
634ac27a0ecSDave Kleikamp 	    (generation && inode->i_generation != generation)) {
635ac27a0ecSDave Kleikamp 		iput(inode);
636ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
637ac27a0ecSDave Kleikamp 	}
638ac27a0ecSDave Kleikamp 	/* now to find a dentry.
639ac27a0ecSDave Kleikamp 	 * If possible, get a well-connected one
640ac27a0ecSDave Kleikamp 	 */
641ac27a0ecSDave Kleikamp 	result = d_alloc_anon(inode);
642ac27a0ecSDave Kleikamp 	if (!result) {
643ac27a0ecSDave Kleikamp 		iput(inode);
644ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
645ac27a0ecSDave Kleikamp 	}
646ac27a0ecSDave Kleikamp 	return result;
647ac27a0ecSDave Kleikamp }
648ac27a0ecSDave Kleikamp 
649ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
650ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
651ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
652ac27a0ecSDave Kleikamp 
653617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type);
654617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode);
655617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
656617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
657617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
658617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
659617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
660617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
661617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
662617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
663ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
664617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
665ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
666ac27a0ecSDave Kleikamp 
667617ba13bSMingming Cao static struct dquot_operations ext4_quota_operations = {
668617ba13bSMingming Cao 	.initialize	= ext4_dquot_initialize,
669617ba13bSMingming Cao 	.drop		= ext4_dquot_drop,
670ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
671ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
672ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
673ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
674ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
675617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
676617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
677617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
678617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
679617ba13bSMingming Cao 	.write_info	= ext4_write_info
680ac27a0ecSDave Kleikamp };
681ac27a0ecSDave Kleikamp 
682617ba13bSMingming Cao static struct quotactl_ops ext4_qctl_operations = {
683617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
684ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
685ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
686ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
687ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
688ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
689ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
690ac27a0ecSDave Kleikamp };
691ac27a0ecSDave Kleikamp #endif
692ac27a0ecSDave Kleikamp 
693617ba13bSMingming Cao static struct super_operations ext4_sops = {
694617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
695617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
696617ba13bSMingming Cao 	.read_inode	= ext4_read_inode,
697617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
698617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
699617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
700617ba13bSMingming Cao 	.put_super	= ext4_put_super,
701617ba13bSMingming Cao 	.write_super	= ext4_write_super,
702617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
703617ba13bSMingming Cao 	.write_super_lockfs = ext4_write_super_lockfs,
704617ba13bSMingming Cao 	.unlockfs	= ext4_unlockfs,
705617ba13bSMingming Cao 	.statfs		= ext4_statfs,
706617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
707617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
708617ba13bSMingming Cao 	.show_options	= ext4_show_options,
709ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
710617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
711617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
712ac27a0ecSDave Kleikamp #endif
713ac27a0ecSDave Kleikamp };
714ac27a0ecSDave Kleikamp 
715617ba13bSMingming Cao static struct export_operations ext4_export_ops = {
716617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
717617ba13bSMingming Cao 	.get_dentry = ext4_get_dentry,
718ac27a0ecSDave Kleikamp };
719ac27a0ecSDave Kleikamp 
720ac27a0ecSDave Kleikamp enum {
721ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
722ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
723ac27a0ecSDave Kleikamp 	Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
724ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
725ac27a0ecSDave Kleikamp 	Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
726ac27a0ecSDave Kleikamp 	Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
727ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
728ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
729ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
730ac27a0ecSDave Kleikamp 	Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
731a86c6181SAlex Tomas 	Opt_grpquota, Opt_extents,
732ac27a0ecSDave Kleikamp };
733ac27a0ecSDave Kleikamp 
734ac27a0ecSDave Kleikamp static match_table_t tokens = {
735ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
736ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
737ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
738ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
739ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
740ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
741ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
742ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
743ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
744ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
745ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
746ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
747ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
748ac27a0ecSDave Kleikamp 	{Opt_nocheck, "nocheck"},
749ac27a0ecSDave Kleikamp 	{Opt_nocheck, "check=none"},
750ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
751ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
752ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
753ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
754ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
755ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
756ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
757ac27a0ecSDave Kleikamp 	{Opt_reservation, "reservation"},
758ac27a0ecSDave Kleikamp 	{Opt_noreservation, "noreservation"},
759ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
760ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
761ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
762ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
763ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
764ac27a0ecSDave Kleikamp 	{Opt_journal_inum, "journal=%u"},
765ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
766ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
767ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
768ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
769ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
770ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
771ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
772ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
773ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
774ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
775ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
776ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
777ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
778ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
779ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
780ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
781a86c6181SAlex Tomas 	{Opt_extents, "extents"},
782ac27a0ecSDave Kleikamp 	{Opt_err, NULL},
783ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
784ac27a0ecSDave Kleikamp };
785ac27a0ecSDave Kleikamp 
786617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
787ac27a0ecSDave Kleikamp {
788617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
789ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
790ac27a0ecSDave Kleikamp 
791ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
792ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
793ac27a0ecSDave Kleikamp 	options += 3;
794617ba13bSMingming Cao 	/*todo: use simple_strtoll with >32bit ext4 */
795ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
796ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
797617ba13bSMingming Cao 		printk("EXT4-fs: Invalid sb specification: %s\n",
798ac27a0ecSDave Kleikamp 		       (char *) *data);
799ac27a0ecSDave Kleikamp 		return 1;
800ac27a0ecSDave Kleikamp 	}
801ac27a0ecSDave Kleikamp 	if (*options == ',')
802ac27a0ecSDave Kleikamp 		options++;
803ac27a0ecSDave Kleikamp 	*data = (void *) options;
804ac27a0ecSDave Kleikamp 	return sb_block;
805ac27a0ecSDave Kleikamp }
806ac27a0ecSDave Kleikamp 
807ac27a0ecSDave Kleikamp static int parse_options (char *options, struct super_block *sb,
808ac27a0ecSDave Kleikamp 			  unsigned int *inum, unsigned long *journal_devnum,
809617ba13bSMingming Cao 			  ext4_fsblk_t *n_blocks_count, int is_remount)
810ac27a0ecSDave Kleikamp {
811617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
812ac27a0ecSDave Kleikamp 	char * p;
813ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
814ac27a0ecSDave Kleikamp 	int data_opt = 0;
815ac27a0ecSDave Kleikamp 	int option;
816ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
817ac27a0ecSDave Kleikamp 	int qtype;
818ac27a0ecSDave Kleikamp 	char *qname;
819ac27a0ecSDave Kleikamp #endif
820ac27a0ecSDave Kleikamp 
821ac27a0ecSDave Kleikamp 	if (!options)
822ac27a0ecSDave Kleikamp 		return 1;
823ac27a0ecSDave Kleikamp 
824ac27a0ecSDave Kleikamp 	while ((p = strsep (&options, ",")) != NULL) {
825ac27a0ecSDave Kleikamp 		int token;
826ac27a0ecSDave Kleikamp 		if (!*p)
827ac27a0ecSDave Kleikamp 			continue;
828ac27a0ecSDave Kleikamp 
829ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
830ac27a0ecSDave Kleikamp 		switch (token) {
831ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
832ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, MINIX_DF);
833ac27a0ecSDave Kleikamp 			break;
834ac27a0ecSDave Kleikamp 		case Opt_minix_df:
835ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, MINIX_DF);
836ac27a0ecSDave Kleikamp 			break;
837ac27a0ecSDave Kleikamp 		case Opt_grpid:
838ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, GRPID);
839ac27a0ecSDave Kleikamp 			break;
840ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
841ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, GRPID);
842ac27a0ecSDave Kleikamp 			break;
843ac27a0ecSDave Kleikamp 		case Opt_resuid:
844ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
845ac27a0ecSDave Kleikamp 				return 0;
846ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
847ac27a0ecSDave Kleikamp 			break;
848ac27a0ecSDave Kleikamp 		case Opt_resgid:
849ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
850ac27a0ecSDave Kleikamp 				return 0;
851ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
852ac27a0ecSDave Kleikamp 			break;
853ac27a0ecSDave Kleikamp 		case Opt_sb:
854ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
855ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
856ac27a0ecSDave Kleikamp 			break;
857ac27a0ecSDave Kleikamp 		case Opt_err_panic:
858ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
859ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_RO);
860ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_PANIC);
861ac27a0ecSDave Kleikamp 			break;
862ac27a0ecSDave Kleikamp 		case Opt_err_ro:
863ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
864ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
865ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_RO);
866ac27a0ecSDave Kleikamp 			break;
867ac27a0ecSDave Kleikamp 		case Opt_err_cont:
868ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_RO);
869ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
870ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, ERRORS_CONT);
871ac27a0ecSDave Kleikamp 			break;
872ac27a0ecSDave Kleikamp 		case Opt_nouid32:
873ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, NO_UID32);
874ac27a0ecSDave Kleikamp 			break;
875ac27a0ecSDave Kleikamp 		case Opt_nocheck:
876ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, CHECK);
877ac27a0ecSDave Kleikamp 			break;
878ac27a0ecSDave Kleikamp 		case Opt_debug:
879ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, DEBUG);
880ac27a0ecSDave Kleikamp 			break;
881ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
882ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, OLDALLOC);
883ac27a0ecSDave Kleikamp 			break;
884ac27a0ecSDave Kleikamp 		case Opt_orlov:
885ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, OLDALLOC);
886ac27a0ecSDave Kleikamp 			break;
887617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_XATTR
888ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
889ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, XATTR_USER);
890ac27a0ecSDave Kleikamp 			break;
891ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
892ac27a0ecSDave Kleikamp 			clear_opt (sbi->s_mount_opt, XATTR_USER);
893ac27a0ecSDave Kleikamp 			break;
894ac27a0ecSDave Kleikamp #else
895ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
896ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
897617ba13bSMingming Cao 			printk("EXT4 (no)user_xattr options not supported\n");
898ac27a0ecSDave Kleikamp 			break;
899ac27a0ecSDave Kleikamp #endif
900617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
901ac27a0ecSDave Kleikamp 		case Opt_acl:
902ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
903ac27a0ecSDave Kleikamp 			break;
904ac27a0ecSDave Kleikamp 		case Opt_noacl:
905ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
906ac27a0ecSDave Kleikamp 			break;
907ac27a0ecSDave Kleikamp #else
908ac27a0ecSDave Kleikamp 		case Opt_acl:
909ac27a0ecSDave Kleikamp 		case Opt_noacl:
910617ba13bSMingming Cao 			printk("EXT4 (no)acl options not supported\n");
911ac27a0ecSDave Kleikamp 			break;
912ac27a0ecSDave Kleikamp #endif
913ac27a0ecSDave Kleikamp 		case Opt_reservation:
914ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, RESERVATION);
915ac27a0ecSDave Kleikamp 			break;
916ac27a0ecSDave Kleikamp 		case Opt_noreservation:
917ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, RESERVATION);
918ac27a0ecSDave Kleikamp 			break;
919ac27a0ecSDave Kleikamp 		case Opt_journal_update:
920ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
921ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
922ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
923ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
924ac27a0ecSDave Kleikamp 			   journal file. */
925ac27a0ecSDave Kleikamp 			if (is_remount) {
926617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
927ac27a0ecSDave Kleikamp 				       "journal on remount\n");
928ac27a0ecSDave Kleikamp 				return 0;
929ac27a0ecSDave Kleikamp 			}
930ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
931ac27a0ecSDave Kleikamp 			break;
932ac27a0ecSDave Kleikamp 		case Opt_journal_inum:
933ac27a0ecSDave Kleikamp 			if (is_remount) {
934617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
935ac27a0ecSDave Kleikamp 				       "journal on remount\n");
936ac27a0ecSDave Kleikamp 				return 0;
937ac27a0ecSDave Kleikamp 			}
938ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
939ac27a0ecSDave Kleikamp 				return 0;
940ac27a0ecSDave Kleikamp 			*inum = option;
941ac27a0ecSDave Kleikamp 			break;
942ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
943ac27a0ecSDave Kleikamp 			if (is_remount) {
944617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: cannot specify "
945ac27a0ecSDave Kleikamp 				       "journal on remount\n");
946ac27a0ecSDave Kleikamp 				return 0;
947ac27a0ecSDave Kleikamp 			}
948ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
949ac27a0ecSDave Kleikamp 				return 0;
950ac27a0ecSDave Kleikamp 			*journal_devnum = option;
951ac27a0ecSDave Kleikamp 			break;
952ac27a0ecSDave Kleikamp 		case Opt_noload:
953ac27a0ecSDave Kleikamp 			set_opt (sbi->s_mount_opt, NOLOAD);
954ac27a0ecSDave Kleikamp 			break;
955ac27a0ecSDave Kleikamp 		case Opt_commit:
956ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
957ac27a0ecSDave Kleikamp 				return 0;
958ac27a0ecSDave Kleikamp 			if (option < 0)
959ac27a0ecSDave Kleikamp 				return 0;
960ac27a0ecSDave Kleikamp 			if (option == 0)
961ac27a0ecSDave Kleikamp 				option = JBD_DEFAULT_MAX_COMMIT_AGE;
962ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
963ac27a0ecSDave Kleikamp 			break;
964ac27a0ecSDave Kleikamp 		case Opt_data_journal:
965617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
966ac27a0ecSDave Kleikamp 			goto datacheck;
967ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
968617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
969ac27a0ecSDave Kleikamp 			goto datacheck;
970ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
971617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
972ac27a0ecSDave Kleikamp 		datacheck:
973ac27a0ecSDave Kleikamp 			if (is_remount) {
974617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
975ac27a0ecSDave Kleikamp 						!= data_opt) {
976ac27a0ecSDave Kleikamp 					printk(KERN_ERR
977617ba13bSMingming Cao 						"EXT4-fs: cannot change data "
978ac27a0ecSDave Kleikamp 						"mode on remount\n");
979ac27a0ecSDave Kleikamp 					return 0;
980ac27a0ecSDave Kleikamp 				}
981ac27a0ecSDave Kleikamp 			} else {
982617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
983ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
984ac27a0ecSDave Kleikamp 			}
985ac27a0ecSDave Kleikamp 			break;
986ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
987ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
988ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
989ac27a0ecSDave Kleikamp 			goto set_qf_name;
990ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
991ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
992ac27a0ecSDave Kleikamp set_qf_name:
993ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
994ac27a0ecSDave Kleikamp 				printk(KERN_ERR
995617ba13bSMingming Cao 					"EXT4-fs: Cannot change journalled "
996ac27a0ecSDave Kleikamp 					"quota options when quota turned on.\n");
997ac27a0ecSDave Kleikamp 				return 0;
998ac27a0ecSDave Kleikamp 			}
999ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1000ac27a0ecSDave Kleikamp 			if (!qname) {
1001ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1002617ba13bSMingming Cao 					"EXT4-fs: not enough memory for "
1003ac27a0ecSDave Kleikamp 					"storing quotafile name.\n");
1004ac27a0ecSDave Kleikamp 				return 0;
1005ac27a0ecSDave Kleikamp 			}
1006ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1007ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1008ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1009617ba13bSMingming Cao 					"EXT4-fs: %s quota file already "
1010ac27a0ecSDave Kleikamp 					"specified.\n", QTYPE2NAME(qtype));
1011ac27a0ecSDave Kleikamp 				kfree(qname);
1012ac27a0ecSDave Kleikamp 				return 0;
1013ac27a0ecSDave Kleikamp 			}
1014ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1015ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1016ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1017617ba13bSMingming Cao 					"EXT4-fs: quotafile must be on "
1018ac27a0ecSDave Kleikamp 					"filesystem root.\n");
1019ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1020ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1021ac27a0ecSDave Kleikamp 				return 0;
1022ac27a0ecSDave Kleikamp 			}
1023ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1024ac27a0ecSDave Kleikamp 			break;
1025ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1026ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1027ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1028ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1029ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1030ac27a0ecSDave Kleikamp clear_qf_name:
1031ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1032617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change "
1033ac27a0ecSDave Kleikamp 					"journalled quota options when "
1034ac27a0ecSDave Kleikamp 					"quota turned on.\n");
1035ac27a0ecSDave Kleikamp 				return 0;
1036ac27a0ecSDave Kleikamp 			}
1037ac27a0ecSDave Kleikamp 			/*
1038ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1039ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1040ac27a0ecSDave Kleikamp 			 */
1041ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1042ac27a0ecSDave Kleikamp 			break;
1043ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1044ac27a0ecSDave Kleikamp 			sbi->s_jquota_fmt = QFMT_VFS_OLD;
1045ac27a0ecSDave Kleikamp 			break;
1046ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1047ac27a0ecSDave Kleikamp 			sbi->s_jquota_fmt = QFMT_VFS_V0;
1048ac27a0ecSDave Kleikamp 			break;
1049ac27a0ecSDave Kleikamp 		case Opt_quota:
1050ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1051ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1052ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1053ac27a0ecSDave Kleikamp 			break;
1054ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1055ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1056ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1057ac27a0ecSDave Kleikamp 			break;
1058ac27a0ecSDave Kleikamp 		case Opt_noquota:
1059ac27a0ecSDave Kleikamp 			if (sb_any_quota_enabled(sb)) {
1060617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: Cannot change quota "
1061ac27a0ecSDave Kleikamp 					"options when quota turned on.\n");
1062ac27a0ecSDave Kleikamp 				return 0;
1063ac27a0ecSDave Kleikamp 			}
1064ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1065ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1066ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1067ac27a0ecSDave Kleikamp 			break;
1068ac27a0ecSDave Kleikamp #else
1069ac27a0ecSDave Kleikamp 		case Opt_quota:
1070ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1071ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1072ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1073ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1074ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1075ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1076ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1077ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1078ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1079617ba13bSMingming Cao 				"EXT4-fs: journalled quota options not "
1080ac27a0ecSDave Kleikamp 				"supported.\n");
1081ac27a0ecSDave Kleikamp 			break;
1082ac27a0ecSDave Kleikamp 		case Opt_noquota:
1083ac27a0ecSDave Kleikamp 			break;
1084ac27a0ecSDave Kleikamp #endif
1085ac27a0ecSDave Kleikamp 		case Opt_abort:
1086ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ABORT);
1087ac27a0ecSDave Kleikamp 			break;
1088ac27a0ecSDave Kleikamp 		case Opt_barrier:
1089ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1090ac27a0ecSDave Kleikamp 				return 0;
1091ac27a0ecSDave Kleikamp 			if (option)
1092ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1093ac27a0ecSDave Kleikamp 			else
1094ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1095ac27a0ecSDave Kleikamp 			break;
1096ac27a0ecSDave Kleikamp 		case Opt_ignore:
1097ac27a0ecSDave Kleikamp 			break;
1098ac27a0ecSDave Kleikamp 		case Opt_resize:
1099ac27a0ecSDave Kleikamp 			if (!is_remount) {
1100617ba13bSMingming Cao 				printk("EXT4-fs: resize option only available "
1101ac27a0ecSDave Kleikamp 					"for remount\n");
1102ac27a0ecSDave Kleikamp 				return 0;
1103ac27a0ecSDave Kleikamp 			}
1104ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1105ac27a0ecSDave Kleikamp 				return 0;
1106ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1107ac27a0ecSDave Kleikamp 			break;
1108ac27a0ecSDave Kleikamp 		case Opt_nobh:
1109ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1110ac27a0ecSDave Kleikamp 			break;
1111ac27a0ecSDave Kleikamp 		case Opt_bh:
1112ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1113ac27a0ecSDave Kleikamp 			break;
1114a86c6181SAlex Tomas 		case Opt_extents:
1115a86c6181SAlex Tomas 			set_opt (sbi->s_mount_opt, EXTENTS);
1116a86c6181SAlex Tomas 			break;
1117ac27a0ecSDave Kleikamp 		default:
1118ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1119617ba13bSMingming Cao 				"EXT4-fs: Unrecognized mount option \"%s\" "
1120ac27a0ecSDave Kleikamp 				"or missing value\n", p);
1121ac27a0ecSDave Kleikamp 			return 0;
1122ac27a0ecSDave Kleikamp 		}
1123ac27a0ecSDave Kleikamp 	}
1124ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1125ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1126617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1127ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1128ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1129ac27a0ecSDave Kleikamp 
1130617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1131ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1132ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1133ac27a0ecSDave Kleikamp 
1134ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1135617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1136ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1137617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1138617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: old and new quota "
1139ac27a0ecSDave Kleikamp 					"format mixing.\n");
1140ac27a0ecSDave Kleikamp 			return 0;
1141ac27a0ecSDave Kleikamp 		}
1142ac27a0ecSDave Kleikamp 
1143ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1144617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: journalled quota format "
1145ac27a0ecSDave Kleikamp 					"not specified.\n");
1146ac27a0ecSDave Kleikamp 			return 0;
1147ac27a0ecSDave Kleikamp 		}
1148ac27a0ecSDave Kleikamp 	} else {
1149ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1150617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: journalled quota format "
1151ac27a0ecSDave Kleikamp 					"specified with no journalling "
1152ac27a0ecSDave Kleikamp 					"enabled.\n");
1153ac27a0ecSDave Kleikamp 			return 0;
1154ac27a0ecSDave Kleikamp 		}
1155ac27a0ecSDave Kleikamp 	}
1156ac27a0ecSDave Kleikamp #endif
1157ac27a0ecSDave Kleikamp 	return 1;
1158ac27a0ecSDave Kleikamp }
1159ac27a0ecSDave Kleikamp 
1160617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1161ac27a0ecSDave Kleikamp 			    int read_only)
1162ac27a0ecSDave Kleikamp {
1163617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1164ac27a0ecSDave Kleikamp 	int res = 0;
1165ac27a0ecSDave Kleikamp 
1166617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1167617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs warning: revision level too high, "
1168ac27a0ecSDave Kleikamp 			"forcing read-only mode\n");
1169ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1170ac27a0ecSDave Kleikamp 	}
1171ac27a0ecSDave Kleikamp 	if (read_only)
1172ac27a0ecSDave Kleikamp 		return res;
1173617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1174617ba13bSMingming Cao 		printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1175ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1176617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1177ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1178617ba13bSMingming Cao 			"EXT4-fs warning: mounting fs with errors, "
1179ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1180ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1181ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1182ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1183ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1184617ba13bSMingming Cao 			"EXT4-fs warning: maximal mount count reached, "
1185ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1186ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1187ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1188ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1189ac27a0ecSDave Kleikamp 		printk (KERN_WARNING
1190617ba13bSMingming Cao 			"EXT4-fs warning: checktime reached, "
1191ac27a0ecSDave Kleikamp 			"running e2fsck is recommended\n");
1192ac27a0ecSDave Kleikamp #if 0
1193ac27a0ecSDave Kleikamp 		/* @@@ We _will_ want to clear the valid bit if we find
119463f57933SAndrew Morton 		 * inconsistencies, to force a fsck at reboot.  But for
119563f57933SAndrew Morton 		 * a plain journaled filesystem we can keep it set as
119663f57933SAndrew Morton 		 * valid forever! :)
119763f57933SAndrew Morton 		 */
1198617ba13bSMingming Cao 	es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
1199ac27a0ecSDave Kleikamp #endif
1200ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1201617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1202ac27a0ecSDave Kleikamp 	es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1203ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1204617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
1205617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1206ac27a0ecSDave Kleikamp 
1207617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
1208ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1209617ba13bSMingming Cao 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1210ac27a0ecSDave Kleikamp 				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
1211ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1212ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1213617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1214617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1215ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1216ac27a0ecSDave Kleikamp 
1217617ba13bSMingming Cao 	printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1218617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
1219ac27a0ecSDave Kleikamp 		char b[BDEVNAME_SIZE];
1220ac27a0ecSDave Kleikamp 
1221ac27a0ecSDave Kleikamp 		printk("external journal on %s\n",
1222617ba13bSMingming Cao 			bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
1223ac27a0ecSDave Kleikamp 	} else {
1224ac27a0ecSDave Kleikamp 		printk("internal journal\n");
1225ac27a0ecSDave Kleikamp 	}
1226ac27a0ecSDave Kleikamp 	return res;
1227ac27a0ecSDave Kleikamp }
1228ac27a0ecSDave Kleikamp 
1229ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1230617ba13bSMingming Cao static int ext4_check_descriptors (struct super_block * sb)
1231ac27a0ecSDave Kleikamp {
1232617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1233617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1234617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1235bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1236bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1237bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1238617ba13bSMingming Cao 	struct ext4_group_desc * gdp = NULL;
1239ac27a0ecSDave Kleikamp 	int desc_block = 0;
1240ac27a0ecSDave Kleikamp 	int i;
1241ac27a0ecSDave Kleikamp 
1242617ba13bSMingming Cao 	ext4_debug ("Checking group descriptors");
1243ac27a0ecSDave Kleikamp 
1244ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_groups_count; i++)
1245ac27a0ecSDave Kleikamp 	{
1246ac27a0ecSDave Kleikamp 		if (i == sbi->s_groups_count - 1)
1247bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1248ac27a0ecSDave Kleikamp 		else
1249ac27a0ecSDave Kleikamp 			last_block = first_block +
1250617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1251ac27a0ecSDave Kleikamp 
1252617ba13bSMingming Cao 		if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1253617ba13bSMingming Cao 			gdp = (struct ext4_group_desc *)
1254ac27a0ecSDave Kleikamp 					sbi->s_group_desc[desc_block++]->b_data;
12558fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
1256bd81d8eeSLaurent Vivier 		if (block_bitmap < first_block || block_bitmap > last_block)
1257ac27a0ecSDave Kleikamp 		{
1258617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1259ac27a0ecSDave Kleikamp 				    "Block bitmap for group %d"
12602ae02107SMingming Cao 				    " not in group (block %llu)!",
1261bd81d8eeSLaurent Vivier 				    i, block_bitmap);
1262ac27a0ecSDave Kleikamp 			return 0;
1263ac27a0ecSDave Kleikamp 		}
12648fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
1265bd81d8eeSLaurent Vivier 		if (inode_bitmap < first_block || inode_bitmap > last_block)
1266ac27a0ecSDave Kleikamp 		{
1267617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1268ac27a0ecSDave Kleikamp 				    "Inode bitmap for group %d"
12692ae02107SMingming Cao 				    " not in group (block %llu)!",
1270bd81d8eeSLaurent Vivier 				    i, inode_bitmap);
1271ac27a0ecSDave Kleikamp 			return 0;
1272ac27a0ecSDave Kleikamp 		}
12738fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1274bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
1275bd81d8eeSLaurent Vivier 		    inode_table + sbi->s_itb_per_group > last_block)
1276ac27a0ecSDave Kleikamp 		{
1277617ba13bSMingming Cao 			ext4_error (sb, "ext4_check_descriptors",
1278ac27a0ecSDave Kleikamp 				    "Inode table for group %d"
12792ae02107SMingming Cao 				    " not in group (block %llu)!",
1280bd81d8eeSLaurent Vivier 				    i, inode_table);
1281ac27a0ecSDave Kleikamp 			return 0;
1282ac27a0ecSDave Kleikamp 		}
1283617ba13bSMingming Cao 		first_block += EXT4_BLOCKS_PER_GROUP(sb);
12840d1ee42fSAlexandre Ratchov 		gdp = (struct ext4_group_desc *)
12850d1ee42fSAlexandre Ratchov 			((__u8 *)gdp + EXT4_DESC_SIZE(sb));
1286ac27a0ecSDave Kleikamp 	}
1287ac27a0ecSDave Kleikamp 
1288bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1289617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
1290ac27a0ecSDave Kleikamp 	return 1;
1291ac27a0ecSDave Kleikamp }
1292ac27a0ecSDave Kleikamp 
1293ac27a0ecSDave Kleikamp 
1294617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1295ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1296ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1297ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1298ac27a0ecSDave Kleikamp  *
1299ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1300ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1301ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1302ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1303ac27a0ecSDave Kleikamp  *
1304ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1305ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1306ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1307617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1308ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1309ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1310ac27a0ecSDave Kleikamp  */
1311617ba13bSMingming Cao static void ext4_orphan_cleanup (struct super_block * sb,
1312617ba13bSMingming Cao 				 struct ext4_super_block * es)
1313ac27a0ecSDave Kleikamp {
1314ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1315ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1316ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1317ac27a0ecSDave Kleikamp 	int i;
1318ac27a0ecSDave Kleikamp #endif
1319ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1320ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1321ac27a0ecSDave Kleikamp 		return;
1322ac27a0ecSDave Kleikamp 	}
1323ac27a0ecSDave Kleikamp 
1324*a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1325*a8f48a95SEric Sandeen 		printk(KERN_ERR "EXT4-fs: write access "
1326*a8f48a95SEric Sandeen 			"unavailable, skipping orphan cleanup.\n");
1327*a8f48a95SEric Sandeen 		return;
1328*a8f48a95SEric Sandeen 	}
1329*a8f48a95SEric Sandeen 
1330617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1331ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1332ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1333ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1334ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1335ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1336ac27a0ecSDave Kleikamp 		return;
1337ac27a0ecSDave Kleikamp 	}
1338ac27a0ecSDave Kleikamp 
1339ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1340617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1341ac27a0ecSDave Kleikamp 		       sb->s_id);
1342ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1343ac27a0ecSDave Kleikamp 	}
1344ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1345ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1346ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1347ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1348ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1349617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1350617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1351ac27a0ecSDave Kleikamp 			if (ret < 0)
1352ac27a0ecSDave Kleikamp 				printk(KERN_ERR
1353617ba13bSMingming Cao 					"EXT4-fs: Cannot turn on journalled "
1354ac27a0ecSDave Kleikamp 					"quota: error %d\n", ret);
1355ac27a0ecSDave Kleikamp 		}
1356ac27a0ecSDave Kleikamp 	}
1357ac27a0ecSDave Kleikamp #endif
1358ac27a0ecSDave Kleikamp 
1359ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1360ac27a0ecSDave Kleikamp 		struct inode *inode;
1361ac27a0ecSDave Kleikamp 
1362ac27a0ecSDave Kleikamp 		if (!(inode =
1363617ba13bSMingming Cao 		      ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
1364ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1365ac27a0ecSDave Kleikamp 			break;
1366ac27a0ecSDave Kleikamp 		}
1367ac27a0ecSDave Kleikamp 
1368617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1369ac27a0ecSDave Kleikamp 		DQUOT_INIT(inode);
1370ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1371ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1372ac27a0ecSDave Kleikamp 				"%s: truncating inode %lu to %Ld bytes\n",
1373ac27a0ecSDave Kleikamp 				__FUNCTION__, inode->i_ino, inode->i_size);
1374ac27a0ecSDave Kleikamp 			jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1375ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1376617ba13bSMingming Cao 			ext4_truncate(inode);
1377ac27a0ecSDave Kleikamp 			nr_truncates++;
1378ac27a0ecSDave Kleikamp 		} else {
1379ac27a0ecSDave Kleikamp 			printk(KERN_DEBUG
1380ac27a0ecSDave Kleikamp 				"%s: deleting unreferenced inode %lu\n",
1381ac27a0ecSDave Kleikamp 				__FUNCTION__, inode->i_ino);
1382ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1383ac27a0ecSDave Kleikamp 				  inode->i_ino);
1384ac27a0ecSDave Kleikamp 			nr_orphans++;
1385ac27a0ecSDave Kleikamp 		}
1386ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1387ac27a0ecSDave Kleikamp 	}
1388ac27a0ecSDave Kleikamp 
1389ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x)==1) ? "" : "s"
1390ac27a0ecSDave Kleikamp 
1391ac27a0ecSDave Kleikamp 	if (nr_orphans)
1392617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1393ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_orphans));
1394ac27a0ecSDave Kleikamp 	if (nr_truncates)
1395617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1396ac27a0ecSDave Kleikamp 		       sb->s_id, PLURAL(nr_truncates));
1397ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1398ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1399ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1400ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
1401ac27a0ecSDave Kleikamp 			vfs_quota_off(sb, i);
1402ac27a0ecSDave Kleikamp 	}
1403ac27a0ecSDave Kleikamp #endif
1404ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1405ac27a0ecSDave Kleikamp }
1406ac27a0ecSDave Kleikamp 
1407ac27a0ecSDave Kleikamp #define log2(n) ffz(~(n))
1408ac27a0ecSDave Kleikamp 
1409ac27a0ecSDave Kleikamp /*
1410ac27a0ecSDave Kleikamp  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
1411ac27a0ecSDave Kleikamp  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1412ac27a0ecSDave Kleikamp  * We need to be 1 filesystem block less than the 2^32 sector limit.
1413ac27a0ecSDave Kleikamp  */
1414617ba13bSMingming Cao static loff_t ext4_max_size(int bits)
1415ac27a0ecSDave Kleikamp {
1416617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
1417ac27a0ecSDave Kleikamp 	/* This constant is calculated to be the largest file size for a
1418ac27a0ecSDave Kleikamp 	 * dense, 4k-blocksize file such that the total number of
1419ac27a0ecSDave Kleikamp 	 * sectors in the file, including data and all indirect blocks,
1420ac27a0ecSDave Kleikamp 	 * does not exceed 2^32. */
1421ac27a0ecSDave Kleikamp 	const loff_t upper_limit = 0x1ff7fffd000LL;
1422ac27a0ecSDave Kleikamp 
1423ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
1424ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
1425ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
1426ac27a0ecSDave Kleikamp 	res <<= bits;
1427ac27a0ecSDave Kleikamp 	if (res > upper_limit)
1428ac27a0ecSDave Kleikamp 		res = upper_limit;
1429ac27a0ecSDave Kleikamp 	return res;
1430ac27a0ecSDave Kleikamp }
1431ac27a0ecSDave Kleikamp 
1432617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
143370bbb3e0SAndrew Morton 				ext4_fsblk_t logical_sb_block, int nr)
1434ac27a0ecSDave Kleikamp {
1435617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1436ac27a0ecSDave Kleikamp 	unsigned long bg, first_meta_bg;
1437ac27a0ecSDave Kleikamp 	int has_super = 0;
1438ac27a0ecSDave Kleikamp 
1439ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1440ac27a0ecSDave Kleikamp 
1441617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1442ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
144370bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
1444ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
1445617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
1446ac27a0ecSDave Kleikamp 		has_super = 1;
1447617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
1448ac27a0ecSDave Kleikamp }
1449ac27a0ecSDave Kleikamp 
1450ac27a0ecSDave Kleikamp 
1451617ba13bSMingming Cao static int ext4_fill_super (struct super_block *sb, void *data, int silent)
1452ac27a0ecSDave Kleikamp {
1453ac27a0ecSDave Kleikamp 	struct buffer_head * bh;
1454617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
1455617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1456617ba13bSMingming Cao 	ext4_fsblk_t block;
1457617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
145870bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
1459ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
1460ac27a0ecSDave Kleikamp 	unsigned int journal_inum = 0;
1461ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
1462ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
1463ac27a0ecSDave Kleikamp 	struct inode *root;
1464ac27a0ecSDave Kleikamp 	int blocksize;
1465ac27a0ecSDave Kleikamp 	int hblock;
1466ac27a0ecSDave Kleikamp 	int db_count;
1467ac27a0ecSDave Kleikamp 	int i;
1468ac27a0ecSDave Kleikamp 	int needs_recovery;
1469ac27a0ecSDave Kleikamp 	__le32 features;
1470bd81d8eeSLaurent Vivier 	__u64 blocks_count;
1471ac27a0ecSDave Kleikamp 
1472ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1473ac27a0ecSDave Kleikamp 	if (!sbi)
1474ac27a0ecSDave Kleikamp 		return -ENOMEM;
1475ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
1476ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
1477617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
1478617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
1479ac27a0ecSDave Kleikamp 
1480ac27a0ecSDave Kleikamp 	unlock_kernel();
1481ac27a0ecSDave Kleikamp 
1482617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1483ac27a0ecSDave Kleikamp 	if (!blocksize) {
1484617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1485ac27a0ecSDave Kleikamp 		goto out_fail;
1486ac27a0ecSDave Kleikamp 	}
1487ac27a0ecSDave Kleikamp 
1488ac27a0ecSDave Kleikamp 	/*
1489617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
1490ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
1491ac27a0ecSDave Kleikamp 	 */
1492617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
149370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
149470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
1495ac27a0ecSDave Kleikamp 	} else {
149670bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
1497ac27a0ecSDave Kleikamp 	}
1498ac27a0ecSDave Kleikamp 
149970bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
1500617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
1501ac27a0ecSDave Kleikamp 		goto out_fail;
1502ac27a0ecSDave Kleikamp 	}
1503ac27a0ecSDave Kleikamp 	/*
1504ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
1505617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
1506ac27a0ecSDave Kleikamp 	 */
1507617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1508ac27a0ecSDave Kleikamp 	sbi->s_es = es;
1509ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
1510617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
1511617ba13bSMingming Cao 		goto cantfind_ext4;
1512ac27a0ecSDave Kleikamp 
1513ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
1514ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1515617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
1516ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
1517617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1518ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
1519617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
1520ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
1521617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1522ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
1523617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
1524ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
1525617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1526617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1527617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1528617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1529617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1530617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1531ac27a0ecSDave Kleikamp 
1532617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1533ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1534617ba13bSMingming Cao 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
1535ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_RO);
1536ceea16bfSDmitry Mishin 	else
1537ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
1538ac27a0ecSDave Kleikamp 
1539ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1540ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1541ac27a0ecSDave Kleikamp 
1542ac27a0ecSDave Kleikamp 	set_opt(sbi->s_mount_opt, RESERVATION);
1543ac27a0ecSDave Kleikamp 
1544ac27a0ecSDave Kleikamp 	if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1545ac27a0ecSDave Kleikamp 			    NULL, 0))
1546ac27a0ecSDave Kleikamp 		goto failed_mount;
1547ac27a0ecSDave Kleikamp 
1548ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1549617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1550ac27a0ecSDave Kleikamp 
1551617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1552617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1553617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1554617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1555ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
1556617ba13bSMingming Cao 		       "EXT4-fs warning: feature flags set on rev 0 fs, "
1557ac27a0ecSDave Kleikamp 		       "running e2fsck is recommended\n");
1558ac27a0ecSDave Kleikamp 	/*
1559ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
1560ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
1561ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
1562ac27a0ecSDave Kleikamp 	 */
1563617ba13bSMingming Cao 	features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
1564ac27a0ecSDave Kleikamp 	if (features) {
1565617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
1566ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
1567ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
1568ac27a0ecSDave Kleikamp 		goto failed_mount;
1569ac27a0ecSDave Kleikamp 	}
1570617ba13bSMingming Cao 	features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
1571ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY) && features) {
1572617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
1573ac27a0ecSDave Kleikamp 		       "unsupported optional features (%x).\n",
1574ac27a0ecSDave Kleikamp 		       sb->s_id, le32_to_cpu(features));
1575ac27a0ecSDave Kleikamp 		goto failed_mount;
1576ac27a0ecSDave Kleikamp 	}
1577ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1578ac27a0ecSDave Kleikamp 
1579617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1580617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
1581ac27a0ecSDave Kleikamp 		printk(KERN_ERR
1582617ba13bSMingming Cao 		       "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
1583ac27a0ecSDave Kleikamp 		       blocksize, sb->s_id);
1584ac27a0ecSDave Kleikamp 		goto failed_mount;
1585ac27a0ecSDave Kleikamp 	}
1586ac27a0ecSDave Kleikamp 
1587ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(sb->s_bdev);
1588ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
1589ac27a0ecSDave Kleikamp 		/*
1590ac27a0ecSDave Kleikamp 		 * Make sure the blocksize for the filesystem is larger
1591ac27a0ecSDave Kleikamp 		 * than the hardware sectorsize for the machine.
1592ac27a0ecSDave Kleikamp 		 */
1593ac27a0ecSDave Kleikamp 		if (blocksize < hblock) {
1594617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
1595ac27a0ecSDave Kleikamp 			       "device blocksize %d.\n", blocksize, hblock);
1596ac27a0ecSDave Kleikamp 			goto failed_mount;
1597ac27a0ecSDave Kleikamp 		}
1598ac27a0ecSDave Kleikamp 
1599ac27a0ecSDave Kleikamp 		brelse (bh);
1600ac27a0ecSDave Kleikamp 		sb_set_blocksize(sb, blocksize);
160170bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
160270bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
160370bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
1604ac27a0ecSDave Kleikamp 		if (!bh) {
1605ac27a0ecSDave Kleikamp 			printk(KERN_ERR
1606617ba13bSMingming Cao 			       "EXT4-fs: Can't read superblock on 2nd try.\n");
1607ac27a0ecSDave Kleikamp 			goto failed_mount;
1608ac27a0ecSDave Kleikamp 		}
1609617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
1610ac27a0ecSDave Kleikamp 		sbi->s_es = es;
1611617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
1612ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1613617ba13bSMingming Cao 				"EXT4-fs: Magic mismatch, very weird !\n");
1614ac27a0ecSDave Kleikamp 			goto failed_mount;
1615ac27a0ecSDave Kleikamp 		}
1616ac27a0ecSDave Kleikamp 	}
1617ac27a0ecSDave Kleikamp 
1618617ba13bSMingming Cao 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
1619ac27a0ecSDave Kleikamp 
1620617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1621617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1622617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
1623ac27a0ecSDave Kleikamp 	} else {
1624ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1625ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1626617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
1627ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1628ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
1629ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1630617ba13bSMingming Cao 				"EXT4-fs: unsupported inode size: %d\n",
1631ac27a0ecSDave Kleikamp 				sbi->s_inode_size);
1632ac27a0ecSDave Kleikamp 			goto failed_mount;
1633ac27a0ecSDave Kleikamp 		}
1634ac27a0ecSDave Kleikamp 	}
1635617ba13bSMingming Cao 	sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
1636ac27a0ecSDave Kleikamp 				   le32_to_cpu(es->s_log_frag_size);
1637ac27a0ecSDave Kleikamp 	if (blocksize != sbi->s_frag_size) {
1638ac27a0ecSDave Kleikamp 		printk(KERN_ERR
1639617ba13bSMingming Cao 		       "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
1640ac27a0ecSDave Kleikamp 		       sbi->s_frag_size, blocksize);
1641ac27a0ecSDave Kleikamp 		goto failed_mount;
1642ac27a0ecSDave Kleikamp 	}
16430d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
16440d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
16458fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
16460d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
16470d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size & (sbi->s_desc_size - 1)) {
16480d1ee42fSAlexandre Ratchov 			printk(KERN_ERR
16498fadc143SAlexandre Ratchov 			       "EXT4-fs: unsupported descriptor size %lu\n",
16500d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
16510d1ee42fSAlexandre Ratchov 			goto failed_mount;
16520d1ee42fSAlexandre Ratchov 		}
16530d1ee42fSAlexandre Ratchov 	} else
16540d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
1655ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1656ac27a0ecSDave Kleikamp 	sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1657ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1658617ba13bSMingming Cao 	if (EXT4_INODE_SIZE(sb) == 0)
1659617ba13bSMingming Cao 		goto cantfind_ext4;
1660617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
1661ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
1662617ba13bSMingming Cao 		goto cantfind_ext4;
1663ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
1664ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
16650d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
1666ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
1667ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
1668617ba13bSMingming Cao 	sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1669617ba13bSMingming Cao 	sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
1670ac27a0ecSDave Kleikamp 	for (i=0; i < 4; i++)
1671ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1672ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
1673ac27a0ecSDave Kleikamp 
1674ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
1675ac27a0ecSDave Kleikamp 		printk (KERN_ERR
1676617ba13bSMingming Cao 			"EXT4-fs: #blocks per group too big: %lu\n",
1677ac27a0ecSDave Kleikamp 			sbi->s_blocks_per_group);
1678ac27a0ecSDave Kleikamp 		goto failed_mount;
1679ac27a0ecSDave Kleikamp 	}
1680ac27a0ecSDave Kleikamp 	if (sbi->s_frags_per_group > blocksize * 8) {
1681ac27a0ecSDave Kleikamp 		printk (KERN_ERR
1682617ba13bSMingming Cao 			"EXT4-fs: #fragments per group too big: %lu\n",
1683ac27a0ecSDave Kleikamp 			sbi->s_frags_per_group);
1684ac27a0ecSDave Kleikamp 		goto failed_mount;
1685ac27a0ecSDave Kleikamp 	}
1686ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
1687ac27a0ecSDave Kleikamp 		printk (KERN_ERR
1688617ba13bSMingming Cao 			"EXT4-fs: #inodes per group too big: %lu\n",
1689ac27a0ecSDave Kleikamp 			sbi->s_inodes_per_group);
1690ac27a0ecSDave Kleikamp 		goto failed_mount;
1691ac27a0ecSDave Kleikamp 	}
1692ac27a0ecSDave Kleikamp 
1693bd81d8eeSLaurent Vivier 	if (ext4_blocks_count(es) >
1694ac27a0ecSDave Kleikamp 		    (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1695617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
1696ac27a0ecSDave Kleikamp 			" too large to mount safely\n", sb->s_id);
1697ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
1698617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
1699ac27a0ecSDave Kleikamp 					"enabled\n");
1700ac27a0ecSDave Kleikamp 		goto failed_mount;
1701ac27a0ecSDave Kleikamp 	}
1702ac27a0ecSDave Kleikamp 
1703617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1704617ba13bSMingming Cao 		goto cantfind_ext4;
1705bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
1706bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
1707bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
1708bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1709bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
1710617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1711617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
1712ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1713ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
1714ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
1715617ba13bSMingming Cao 		printk (KERN_ERR "EXT4-fs: not enough memory\n");
1716ac27a0ecSDave Kleikamp 		goto failed_mount;
1717ac27a0ecSDave Kleikamp 	}
1718ac27a0ecSDave Kleikamp 
1719ac27a0ecSDave Kleikamp 	bgl_lock_init(&sbi->s_blockgroup_lock);
1720ac27a0ecSDave Kleikamp 
1721ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
172270bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
1723ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
1724ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
1725617ba13bSMingming Cao 			printk (KERN_ERR "EXT4-fs: "
1726ac27a0ecSDave Kleikamp 				"can't read group descriptor %d\n", i);
1727ac27a0ecSDave Kleikamp 			db_count = i;
1728ac27a0ecSDave Kleikamp 			goto failed_mount2;
1729ac27a0ecSDave Kleikamp 		}
1730ac27a0ecSDave Kleikamp 	}
1731617ba13bSMingming Cao 	if (!ext4_check_descriptors (sb)) {
1732617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
1733ac27a0ecSDave Kleikamp 		goto failed_mount2;
1734ac27a0ecSDave Kleikamp 	}
1735ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
1736ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1737ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
1738ac27a0ecSDave Kleikamp 
1739ac27a0ecSDave Kleikamp 	percpu_counter_init(&sbi->s_freeblocks_counter,
1740617ba13bSMingming Cao 		ext4_count_free_blocks(sb));
1741ac27a0ecSDave Kleikamp 	percpu_counter_init(&sbi->s_freeinodes_counter,
1742617ba13bSMingming Cao 		ext4_count_free_inodes(sb));
1743ac27a0ecSDave Kleikamp 	percpu_counter_init(&sbi->s_dirs_counter,
1744617ba13bSMingming Cao 		ext4_count_dirs(sb));
1745ac27a0ecSDave Kleikamp 
1746ac27a0ecSDave Kleikamp 	/* per fileystem reservation list head & lock */
1747ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_rsv_window_lock);
1748ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_root = RB_ROOT;
1749ac27a0ecSDave Kleikamp 	/* Add a single, static dummy reservation to the start of the
1750ac27a0ecSDave Kleikamp 	 * reservation window list --- it gives us a placeholder for
1751ac27a0ecSDave Kleikamp 	 * append-at-start-of-list which makes the allocation logic
1752ac27a0ecSDave Kleikamp 	 * _much_ simpler. */
1753617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1754617ba13bSMingming Cao 	sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1755ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1756ac27a0ecSDave Kleikamp 	sbi->s_rsv_window_head.rsv_goal_size = 0;
1757617ba13bSMingming Cao 	ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
1758ac27a0ecSDave Kleikamp 
1759ac27a0ecSDave Kleikamp 	/*
1760ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
1761ac27a0ecSDave Kleikamp 	 */
1762617ba13bSMingming Cao 	sb->s_op = &ext4_sops;
1763617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
1764617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
1765ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1766617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
1767617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
1768ac27a0ecSDave Kleikamp #endif
1769ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1770ac27a0ecSDave Kleikamp 
1771ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
1772ac27a0ecSDave Kleikamp 
1773ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
1774617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
1775617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
1776ac27a0ecSDave Kleikamp 
1777ac27a0ecSDave Kleikamp 	/*
1778ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
1779ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
1780ac27a0ecSDave Kleikamp 	 */
1781ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
1782617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1783617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
1784ac27a0ecSDave Kleikamp 			goto failed_mount3;
1785ac27a0ecSDave Kleikamp 	} else if (journal_inum) {
1786617ba13bSMingming Cao 		if (ext4_create_journal(sb, es, journal_inum))
1787ac27a0ecSDave Kleikamp 			goto failed_mount3;
1788ac27a0ecSDave Kleikamp 	} else {
1789ac27a0ecSDave Kleikamp 		if (!silent)
1790ac27a0ecSDave Kleikamp 			printk (KERN_ERR
1791617ba13bSMingming Cao 				"ext4: No journal on filesystem on %s\n",
1792ac27a0ecSDave Kleikamp 				sb->s_id);
1793ac27a0ecSDave Kleikamp 		goto failed_mount3;
1794ac27a0ecSDave Kleikamp 	}
1795ac27a0ecSDave Kleikamp 
1796ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
1797ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
1798ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
1799ac27a0ecSDave Kleikamp 	case 0:
1800ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
180163f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
180263f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
180363f57933SAndrew Morton 		 */
1804dab291afSMingming Cao 		if (jbd2_journal_check_available_features
1805dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
1806ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
1807ac27a0ecSDave Kleikamp 		else
1808ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1809ac27a0ecSDave Kleikamp 		break;
1810ac27a0ecSDave Kleikamp 
1811617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
1812617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
1813dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
1814dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
1815617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: Journal does not support "
1816ac27a0ecSDave Kleikamp 			       "requested data journaling mode\n");
1817ac27a0ecSDave Kleikamp 			goto failed_mount4;
1818ac27a0ecSDave Kleikamp 		}
1819ac27a0ecSDave Kleikamp 	default:
1820ac27a0ecSDave Kleikamp 		break;
1821ac27a0ecSDave Kleikamp 	}
1822ac27a0ecSDave Kleikamp 
1823ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
1824617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1825617ba13bSMingming Cao 			printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
1826ac27a0ecSDave Kleikamp 				"its supported only with writeback mode\n");
1827ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1828ac27a0ecSDave Kleikamp 		}
1829ac27a0ecSDave Kleikamp 	}
1830ac27a0ecSDave Kleikamp 	/*
1831dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
1832ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
1833ac27a0ecSDave Kleikamp 	 */
1834ac27a0ecSDave Kleikamp 
1835617ba13bSMingming Cao 	root = iget(sb, EXT4_ROOT_INO);
1836ac27a0ecSDave Kleikamp 	sb->s_root = d_alloc_root(root);
1837ac27a0ecSDave Kleikamp 	if (!sb->s_root) {
1838617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: get root inode failed\n");
1839ac27a0ecSDave Kleikamp 		iput(root);
1840ac27a0ecSDave Kleikamp 		goto failed_mount4;
1841ac27a0ecSDave Kleikamp 	}
1842ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1843ac27a0ecSDave Kleikamp 		dput(sb->s_root);
1844ac27a0ecSDave Kleikamp 		sb->s_root = NULL;
1845617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
1846ac27a0ecSDave Kleikamp 		goto failed_mount4;
1847ac27a0ecSDave Kleikamp 	}
1848ac27a0ecSDave Kleikamp 
1849617ba13bSMingming Cao 	ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1850ac27a0ecSDave Kleikamp 	/*
1851ac27a0ecSDave Kleikamp 	 * akpm: core read_super() calls in here with the superblock locked.
1852ac27a0ecSDave Kleikamp 	 * That deadlocks, because orphan cleanup needs to lock the superblock
1853ac27a0ecSDave Kleikamp 	 * in numerous places.  Here we just pop the lock - it's relatively
1854ac27a0ecSDave Kleikamp 	 * harmless, because we are now ready to accept write_super() requests,
1855ac27a0ecSDave Kleikamp 	 * and aviro says that's the only reason for hanging onto the
1856ac27a0ecSDave Kleikamp 	 * superblock lock.
1857ac27a0ecSDave Kleikamp 	 */
1858617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1859617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
1860617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
1861ac27a0ecSDave Kleikamp 	if (needs_recovery)
1862617ba13bSMingming Cao 		printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1863617ba13bSMingming Cao 	ext4_mark_recovery_complete(sb, es);
1864617ba13bSMingming Cao 	printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1865617ba13bSMingming Cao 		test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1866617ba13bSMingming Cao 		test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
1867ac27a0ecSDave Kleikamp 		"writeback");
1868ac27a0ecSDave Kleikamp 
1869a86c6181SAlex Tomas 	ext4_ext_init(sb);
1870a86c6181SAlex Tomas 
1871ac27a0ecSDave Kleikamp 	lock_kernel();
1872ac27a0ecSDave Kleikamp 	return 0;
1873ac27a0ecSDave Kleikamp 
1874617ba13bSMingming Cao cantfind_ext4:
1875ac27a0ecSDave Kleikamp 	if (!silent)
1876617ba13bSMingming Cao 		printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
1877ac27a0ecSDave Kleikamp 		       sb->s_id);
1878ac27a0ecSDave Kleikamp 	goto failed_mount;
1879ac27a0ecSDave Kleikamp 
1880ac27a0ecSDave Kleikamp failed_mount4:
1881dab291afSMingming Cao 	jbd2_journal_destroy(sbi->s_journal);
1882ac27a0ecSDave Kleikamp failed_mount3:
1883ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
1884ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1885ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
1886ac27a0ecSDave Kleikamp failed_mount2:
1887ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
1888ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
1889ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
1890ac27a0ecSDave Kleikamp failed_mount:
1891ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1892ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
1893ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
1894ac27a0ecSDave Kleikamp #endif
1895617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
1896ac27a0ecSDave Kleikamp 	brelse(bh);
1897ac27a0ecSDave Kleikamp out_fail:
1898ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
1899ac27a0ecSDave Kleikamp 	kfree(sbi);
1900ac27a0ecSDave Kleikamp 	lock_kernel();
1901ac27a0ecSDave Kleikamp 	return -EINVAL;
1902ac27a0ecSDave Kleikamp }
1903ac27a0ecSDave Kleikamp 
1904ac27a0ecSDave Kleikamp /*
1905ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
1906ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
1907ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
1908ac27a0ecSDave Kleikamp  */
1909617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
1910ac27a0ecSDave Kleikamp {
1911617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1912ac27a0ecSDave Kleikamp 
1913ac27a0ecSDave Kleikamp 	if (sbi->s_commit_interval)
1914ac27a0ecSDave Kleikamp 		journal->j_commit_interval = sbi->s_commit_interval;
1915617ba13bSMingming Cao 	/* We could also set up an ext4-specific default for the commit
1916ac27a0ecSDave Kleikamp 	 * interval here, but for now we'll just fall back to the jbd
1917ac27a0ecSDave Kleikamp 	 * default. */
1918ac27a0ecSDave Kleikamp 
1919ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
1920ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
1921dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
1922ac27a0ecSDave Kleikamp 	else
1923dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
1924ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
1925ac27a0ecSDave Kleikamp }
1926ac27a0ecSDave Kleikamp 
1927617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
1928ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
1929ac27a0ecSDave Kleikamp {
1930ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
1931ac27a0ecSDave Kleikamp 	journal_t *journal;
1932ac27a0ecSDave Kleikamp 
1933ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
1934ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
1935ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
1936ac27a0ecSDave Kleikamp 
1937ac27a0ecSDave Kleikamp 	journal_inode = iget(sb, journal_inum);
1938ac27a0ecSDave Kleikamp 	if (!journal_inode) {
1939617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: no journal found.\n");
1940ac27a0ecSDave Kleikamp 		return NULL;
1941ac27a0ecSDave Kleikamp 	}
1942ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
1943ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
1944ac27a0ecSDave Kleikamp 		iput(journal_inode);
1945617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
1946ac27a0ecSDave Kleikamp 		return NULL;
1947ac27a0ecSDave Kleikamp 	}
1948ac27a0ecSDave Kleikamp 
1949ac27a0ecSDave Kleikamp 	jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1950ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
1951ac27a0ecSDave Kleikamp 	if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
1952617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
1953ac27a0ecSDave Kleikamp 		iput(journal_inode);
1954ac27a0ecSDave Kleikamp 		return NULL;
1955ac27a0ecSDave Kleikamp 	}
1956ac27a0ecSDave Kleikamp 
1957dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
1958ac27a0ecSDave Kleikamp 	if (!journal) {
1959617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
1960ac27a0ecSDave Kleikamp 		iput(journal_inode);
1961ac27a0ecSDave Kleikamp 		return NULL;
1962ac27a0ecSDave Kleikamp 	}
1963ac27a0ecSDave Kleikamp 	journal->j_private = sb;
1964617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
1965ac27a0ecSDave Kleikamp 	return journal;
1966ac27a0ecSDave Kleikamp }
1967ac27a0ecSDave Kleikamp 
1968617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
1969ac27a0ecSDave Kleikamp 				       dev_t j_dev)
1970ac27a0ecSDave Kleikamp {
1971ac27a0ecSDave Kleikamp 	struct buffer_head * bh;
1972ac27a0ecSDave Kleikamp 	journal_t *journal;
1973617ba13bSMingming Cao 	ext4_fsblk_t start;
1974617ba13bSMingming Cao 	ext4_fsblk_t len;
1975ac27a0ecSDave Kleikamp 	int hblock, blocksize;
1976617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
1977ac27a0ecSDave Kleikamp 	unsigned long offset;
1978617ba13bSMingming Cao 	struct ext4_super_block * es;
1979ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1980ac27a0ecSDave Kleikamp 
1981617ba13bSMingming Cao 	bdev = ext4_blkdev_get(j_dev);
1982ac27a0ecSDave Kleikamp 	if (bdev == NULL)
1983ac27a0ecSDave Kleikamp 		return NULL;
1984ac27a0ecSDave Kleikamp 
1985ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
1986ac27a0ecSDave Kleikamp 		printk(KERN_ERR
1987617ba13bSMingming Cao 		        "EXT4: failed to claim external journal device.\n");
1988ac27a0ecSDave Kleikamp 		blkdev_put(bdev);
1989ac27a0ecSDave Kleikamp 		return NULL;
1990ac27a0ecSDave Kleikamp 	}
1991ac27a0ecSDave Kleikamp 
1992ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
1993ac27a0ecSDave Kleikamp 	hblock = bdev_hardsect_size(bdev);
1994ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
1995ac27a0ecSDave Kleikamp 		printk(KERN_ERR
1996617ba13bSMingming Cao 			"EXT4-fs: blocksize too small for journal device.\n");
1997ac27a0ecSDave Kleikamp 		goto out_bdev;
1998ac27a0ecSDave Kleikamp 	}
1999ac27a0ecSDave Kleikamp 
2000617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2001617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2002ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
2003ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
2004617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2005ac27a0ecSDave Kleikamp 		       "external journal\n");
2006ac27a0ecSDave Kleikamp 		goto out_bdev;
2007ac27a0ecSDave Kleikamp 	}
2008ac27a0ecSDave Kleikamp 
2009617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2010617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2011ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
2012617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2013617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: external journal has "
2014ac27a0ecSDave Kleikamp 					"bad superblock\n");
2015ac27a0ecSDave Kleikamp 		brelse(bh);
2016ac27a0ecSDave Kleikamp 		goto out_bdev;
2017ac27a0ecSDave Kleikamp 	}
2018ac27a0ecSDave Kleikamp 
2019617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2020617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2021ac27a0ecSDave Kleikamp 		brelse(bh);
2022ac27a0ecSDave Kleikamp 		goto out_bdev;
2023ac27a0ecSDave Kleikamp 	}
2024ac27a0ecSDave Kleikamp 
2025bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
2026ac27a0ecSDave Kleikamp 	start = sb_block + 1;
2027ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
2028ac27a0ecSDave Kleikamp 
2029dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2030ac27a0ecSDave Kleikamp 					start, len, blocksize);
2031ac27a0ecSDave Kleikamp 	if (!journal) {
2032617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2033ac27a0ecSDave Kleikamp 		goto out_bdev;
2034ac27a0ecSDave Kleikamp 	}
2035ac27a0ecSDave Kleikamp 	journal->j_private = sb;
2036ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
2037ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
2038ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
2039617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2040ac27a0ecSDave Kleikamp 		goto out_journal;
2041ac27a0ecSDave Kleikamp 	}
2042ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2043617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: External journal has more than one "
2044ac27a0ecSDave Kleikamp 					"user (unsupported) - %d\n",
2045ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
2046ac27a0ecSDave Kleikamp 		goto out_journal;
2047ac27a0ecSDave Kleikamp 	}
2048617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
2049617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
2050ac27a0ecSDave Kleikamp 	return journal;
2051ac27a0ecSDave Kleikamp out_journal:
2052dab291afSMingming Cao 	jbd2_journal_destroy(journal);
2053ac27a0ecSDave Kleikamp out_bdev:
2054617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
2055ac27a0ecSDave Kleikamp 	return NULL;
2056ac27a0ecSDave Kleikamp }
2057ac27a0ecSDave Kleikamp 
2058617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
2059617ba13bSMingming Cao 			     struct ext4_super_block *es,
2060ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
2061ac27a0ecSDave Kleikamp {
2062ac27a0ecSDave Kleikamp 	journal_t *journal;
2063ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2064ac27a0ecSDave Kleikamp 	dev_t journal_dev;
2065ac27a0ecSDave Kleikamp 	int err = 0;
2066ac27a0ecSDave Kleikamp 	int really_read_only;
2067ac27a0ecSDave Kleikamp 
2068ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2069ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2070617ba13bSMingming Cao 		printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2071ac27a0ecSDave Kleikamp 			"numbers have changed\n");
2072ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
2073ac27a0ecSDave Kleikamp 	} else
2074ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2075ac27a0ecSDave Kleikamp 
2076ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
2077ac27a0ecSDave Kleikamp 
2078ac27a0ecSDave Kleikamp 	/*
2079ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
2080ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
2081ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
2082ac27a0ecSDave Kleikamp 	 */
2083ac27a0ecSDave Kleikamp 
2084617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2085ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
2086617ba13bSMingming Cao 			printk(KERN_INFO "EXT4-fs: INFO: recovery "
2087ac27a0ecSDave Kleikamp 					"required on readonly filesystem.\n");
2088ac27a0ecSDave Kleikamp 			if (really_read_only) {
2089617ba13bSMingming Cao 				printk(KERN_ERR "EXT4-fs: write access "
2090ac27a0ecSDave Kleikamp 					"unavailable, cannot proceed.\n");
2091ac27a0ecSDave Kleikamp 				return -EROFS;
2092ac27a0ecSDave Kleikamp 			}
2093617ba13bSMingming Cao 			printk (KERN_INFO "EXT4-fs: write access will "
2094ac27a0ecSDave Kleikamp 					"be enabled during recovery.\n");
2095ac27a0ecSDave Kleikamp 		}
2096ac27a0ecSDave Kleikamp 	}
2097ac27a0ecSDave Kleikamp 
2098ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
2099617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2100ac27a0ecSDave Kleikamp 		       "and inode journals!\n");
2101ac27a0ecSDave Kleikamp 		return -EINVAL;
2102ac27a0ecSDave Kleikamp 	}
2103ac27a0ecSDave Kleikamp 
2104ac27a0ecSDave Kleikamp 	if (journal_inum) {
2105617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
2106ac27a0ecSDave Kleikamp 			return -EINVAL;
2107ac27a0ecSDave Kleikamp 	} else {
2108617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2109ac27a0ecSDave Kleikamp 			return -EINVAL;
2110ac27a0ecSDave Kleikamp 	}
2111ac27a0ecSDave Kleikamp 
2112ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2113dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
2114ac27a0ecSDave Kleikamp 		if (err)  {
2115617ba13bSMingming Cao 			printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2116dab291afSMingming Cao 			jbd2_journal_destroy(journal);
2117ac27a0ecSDave Kleikamp 			return err;
2118ac27a0ecSDave Kleikamp 		}
2119ac27a0ecSDave Kleikamp 	}
2120ac27a0ecSDave Kleikamp 
2121617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2122dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
2123ac27a0ecSDave Kleikamp 	if (!err)
2124dab291afSMingming Cao 		err = jbd2_journal_load(journal);
2125ac27a0ecSDave Kleikamp 
2126ac27a0ecSDave Kleikamp 	if (err) {
2127617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2128dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2129ac27a0ecSDave Kleikamp 		return err;
2130ac27a0ecSDave Kleikamp 	}
2131ac27a0ecSDave Kleikamp 
2132617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2133617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
2134ac27a0ecSDave Kleikamp 
2135ac27a0ecSDave Kleikamp 	if (journal_devnum &&
2136ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2137ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
2138ac27a0ecSDave Kleikamp 		sb->s_dirt = 1;
2139ac27a0ecSDave Kleikamp 
2140ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
2141617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2142ac27a0ecSDave Kleikamp 	}
2143ac27a0ecSDave Kleikamp 
2144ac27a0ecSDave Kleikamp 	return 0;
2145ac27a0ecSDave Kleikamp }
2146ac27a0ecSDave Kleikamp 
2147617ba13bSMingming Cao static int ext4_create_journal(struct super_block * sb,
2148617ba13bSMingming Cao 			       struct ext4_super_block * es,
2149ac27a0ecSDave Kleikamp 			       unsigned int journal_inum)
2150ac27a0ecSDave Kleikamp {
2151ac27a0ecSDave Kleikamp 	journal_t *journal;
2152ac27a0ecSDave Kleikamp 
2153ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY) {
2154617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2155ac27a0ecSDave Kleikamp 				"create journal.\n");
2156ac27a0ecSDave Kleikamp 		return -EROFS;
2157ac27a0ecSDave Kleikamp 	}
2158ac27a0ecSDave Kleikamp 
2159617ba13bSMingming Cao 	if (!(journal = ext4_get_journal(sb, journal_inum)))
2160ac27a0ecSDave Kleikamp 		return -EINVAL;
2161ac27a0ecSDave Kleikamp 
2162617ba13bSMingming Cao 	printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2163ac27a0ecSDave Kleikamp 	       journal_inum);
2164ac27a0ecSDave Kleikamp 
2165dab291afSMingming Cao 	if (jbd2_journal_create(journal)) {
2166617ba13bSMingming Cao 		printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2167dab291afSMingming Cao 		jbd2_journal_destroy(journal);
2168ac27a0ecSDave Kleikamp 		return -EIO;
2169ac27a0ecSDave Kleikamp 	}
2170ac27a0ecSDave Kleikamp 
2171617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
2172ac27a0ecSDave Kleikamp 
2173617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
2174617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2175617ba13bSMingming Cao 	EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2176ac27a0ecSDave Kleikamp 
2177ac27a0ecSDave Kleikamp 	es->s_journal_inum = cpu_to_le32(journal_inum);
2178ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
2179ac27a0ecSDave Kleikamp 
2180ac27a0ecSDave Kleikamp 	/* Make sure we flush the recovery flag to disk. */
2181617ba13bSMingming Cao 	ext4_commit_super(sb, es, 1);
2182ac27a0ecSDave Kleikamp 
2183ac27a0ecSDave Kleikamp 	return 0;
2184ac27a0ecSDave Kleikamp }
2185ac27a0ecSDave Kleikamp 
2186617ba13bSMingming Cao static void ext4_commit_super (struct super_block * sb,
2187617ba13bSMingming Cao 			       struct ext4_super_block * es,
2188ac27a0ecSDave Kleikamp 			       int sync)
2189ac27a0ecSDave Kleikamp {
2190617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2191ac27a0ecSDave Kleikamp 
2192ac27a0ecSDave Kleikamp 	if (!sbh)
2193ac27a0ecSDave Kleikamp 		return;
2194ac27a0ecSDave Kleikamp 	es->s_wtime = cpu_to_le32(get_seconds());
2195bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2196617ba13bSMingming Cao 	es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2197ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
2198ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
2199ac27a0ecSDave Kleikamp 	if (sync)
2200ac27a0ecSDave Kleikamp 		sync_dirty_buffer(sbh);
2201ac27a0ecSDave Kleikamp }
2202ac27a0ecSDave Kleikamp 
2203ac27a0ecSDave Kleikamp 
2204ac27a0ecSDave Kleikamp /*
2205ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
2206ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
2207ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
2208ac27a0ecSDave Kleikamp  */
2209617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block * sb,
2210617ba13bSMingming Cao 					struct ext4_super_block * es)
2211ac27a0ecSDave Kleikamp {
2212617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
2213ac27a0ecSDave Kleikamp 
2214dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
2215dab291afSMingming Cao 	jbd2_journal_flush(journal);
2216617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2217ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
2218617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2219ac27a0ecSDave Kleikamp 		sb->s_dirt = 0;
2220617ba13bSMingming Cao 		ext4_commit_super(sb, es, 1);
2221ac27a0ecSDave Kleikamp 	}
2222dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
2223ac27a0ecSDave Kleikamp }
2224ac27a0ecSDave Kleikamp 
2225ac27a0ecSDave Kleikamp /*
2226ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
2227ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
2228ac27a0ecSDave Kleikamp  * main filesystem now.
2229ac27a0ecSDave Kleikamp  */
2230617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block * sb,
2231617ba13bSMingming Cao 				   struct ext4_super_block * es)
2232ac27a0ecSDave Kleikamp {
2233ac27a0ecSDave Kleikamp 	journal_t *journal;
2234ac27a0ecSDave Kleikamp 	int j_errno;
2235ac27a0ecSDave Kleikamp 	const char *errstr;
2236ac27a0ecSDave Kleikamp 
2237617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2238ac27a0ecSDave Kleikamp 
2239ac27a0ecSDave Kleikamp 	/*
2240ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
2241617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
2242ac27a0ecSDave Kleikamp 	 */
2243ac27a0ecSDave Kleikamp 
2244dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
2245ac27a0ecSDave Kleikamp 	if (j_errno) {
2246ac27a0ecSDave Kleikamp 		char nbuf[16];
2247ac27a0ecSDave Kleikamp 
2248617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
2249617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
2250ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
2251617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
2252ac27a0ecSDave Kleikamp 			     "filesystem check.");
2253ac27a0ecSDave Kleikamp 
2254617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2255617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2256617ba13bSMingming Cao 		ext4_commit_super (sb, es, 1);
2257ac27a0ecSDave Kleikamp 
2258dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
2259ac27a0ecSDave Kleikamp 	}
2260ac27a0ecSDave Kleikamp }
2261ac27a0ecSDave Kleikamp 
2262ac27a0ecSDave Kleikamp /*
2263ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
2264ac27a0ecSDave Kleikamp  * and wait on the commit.
2265ac27a0ecSDave Kleikamp  */
2266617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
2267ac27a0ecSDave Kleikamp {
2268ac27a0ecSDave Kleikamp 	journal_t *journal;
2269ac27a0ecSDave Kleikamp 	int ret;
2270ac27a0ecSDave Kleikamp 
2271ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
2272ac27a0ecSDave Kleikamp 		return 0;
2273ac27a0ecSDave Kleikamp 
2274617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2275ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2276617ba13bSMingming Cao 	ret = ext4_journal_force_commit(journal);
2277ac27a0ecSDave Kleikamp 	return ret;
2278ac27a0ecSDave Kleikamp }
2279ac27a0ecSDave Kleikamp 
2280ac27a0ecSDave Kleikamp /*
2281617ba13bSMingming Cao  * Ext4 always journals updates to the superblock itself, so we don't
2282ac27a0ecSDave Kleikamp  * have to propagate any other updates to the superblock on disk at this
2283ac27a0ecSDave Kleikamp  * point.  Just start an async writeback to get the buffers on their way
2284ac27a0ecSDave Kleikamp  * to the disk.
2285ac27a0ecSDave Kleikamp  *
2286ac27a0ecSDave Kleikamp  * This implicitly triggers the writebehind on sync().
2287ac27a0ecSDave Kleikamp  */
2288ac27a0ecSDave Kleikamp 
2289617ba13bSMingming Cao static void ext4_write_super (struct super_block * sb)
2290ac27a0ecSDave Kleikamp {
2291ac27a0ecSDave Kleikamp 	if (mutex_trylock(&sb->s_lock) != 0)
2292ac27a0ecSDave Kleikamp 		BUG();
2293ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2294ac27a0ecSDave Kleikamp }
2295ac27a0ecSDave Kleikamp 
2296617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
2297ac27a0ecSDave Kleikamp {
2298ac27a0ecSDave Kleikamp 	tid_t target;
2299ac27a0ecSDave Kleikamp 
2300ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2301dab291afSMingming Cao 	if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
2302ac27a0ecSDave Kleikamp 		if (wait)
2303dab291afSMingming Cao 			jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
2304ac27a0ecSDave Kleikamp 	}
2305ac27a0ecSDave Kleikamp 	return 0;
2306ac27a0ecSDave Kleikamp }
2307ac27a0ecSDave Kleikamp 
2308ac27a0ecSDave Kleikamp /*
2309ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
2310ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
2311ac27a0ecSDave Kleikamp  */
2312617ba13bSMingming Cao static void ext4_write_super_lockfs(struct super_block *sb)
2313ac27a0ecSDave Kleikamp {
2314ac27a0ecSDave Kleikamp 	sb->s_dirt = 0;
2315ac27a0ecSDave Kleikamp 
2316ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2317617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
2318ac27a0ecSDave Kleikamp 
2319ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
2320dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
2321dab291afSMingming Cao 		jbd2_journal_flush(journal);
2322ac27a0ecSDave Kleikamp 
2323ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
2324617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2325617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2326ac27a0ecSDave Kleikamp 	}
2327ac27a0ecSDave Kleikamp }
2328ac27a0ecSDave Kleikamp 
2329ac27a0ecSDave Kleikamp /*
2330ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
2331ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
2332ac27a0ecSDave Kleikamp  */
2333617ba13bSMingming Cao static void ext4_unlockfs(struct super_block *sb)
2334ac27a0ecSDave Kleikamp {
2335ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
2336ac27a0ecSDave Kleikamp 		lock_super(sb);
2337ac27a0ecSDave Kleikamp 		/* Reser the needs_recovery flag before the fs is unlocked. */
2338617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2339617ba13bSMingming Cao 		ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2340ac27a0ecSDave Kleikamp 		unlock_super(sb);
2341dab291afSMingming Cao 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2342ac27a0ecSDave Kleikamp 	}
2343ac27a0ecSDave Kleikamp }
2344ac27a0ecSDave Kleikamp 
2345617ba13bSMingming Cao static int ext4_remount (struct super_block * sb, int * flags, char * data)
2346ac27a0ecSDave Kleikamp {
2347617ba13bSMingming Cao 	struct ext4_super_block * es;
2348617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2349617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
2350ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
2351617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
2352ac27a0ecSDave Kleikamp 	int err;
2353ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2354ac27a0ecSDave Kleikamp 	int i;
2355ac27a0ecSDave Kleikamp #endif
2356ac27a0ecSDave Kleikamp 
2357ac27a0ecSDave Kleikamp 	/* Store the original options */
2358ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
2359ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
2360ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
2361ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
2362ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
2363ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2364ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2365ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2366ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2367ac27a0ecSDave Kleikamp #endif
2368ac27a0ecSDave Kleikamp 
2369ac27a0ecSDave Kleikamp 	/*
2370ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
2371ac27a0ecSDave Kleikamp 	 */
2372ac27a0ecSDave Kleikamp 	if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2373ac27a0ecSDave Kleikamp 		err = -EINVAL;
2374ac27a0ecSDave Kleikamp 		goto restore_opts;
2375ac27a0ecSDave Kleikamp 	}
2376ac27a0ecSDave Kleikamp 
2377617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2378617ba13bSMingming Cao 		ext4_abort(sb, __FUNCTION__, "Abort forced by user");
2379ac27a0ecSDave Kleikamp 
2380ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2381617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2382ac27a0ecSDave Kleikamp 
2383ac27a0ecSDave Kleikamp 	es = sbi->s_es;
2384ac27a0ecSDave Kleikamp 
2385617ba13bSMingming Cao 	ext4_init_journal_params(sb, sbi->s_journal);
2386ac27a0ecSDave Kleikamp 
2387ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2388bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
2389617ba13bSMingming Cao 		if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
2390ac27a0ecSDave Kleikamp 			err = -EROFS;
2391ac27a0ecSDave Kleikamp 			goto restore_opts;
2392ac27a0ecSDave Kleikamp 		}
2393ac27a0ecSDave Kleikamp 
2394ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
2395ac27a0ecSDave Kleikamp 			/*
2396ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
2397ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
2398ac27a0ecSDave Kleikamp 			 */
2399ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
2400ac27a0ecSDave Kleikamp 
2401ac27a0ecSDave Kleikamp 			/*
2402ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
2403ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
2404ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
2405ac27a0ecSDave Kleikamp 			 */
2406617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2407617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
2408ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
2409ac27a0ecSDave Kleikamp 
2410617ba13bSMingming Cao 			ext4_mark_recovery_complete(sb, es);
2411ac27a0ecSDave Kleikamp 		} else {
2412ac27a0ecSDave Kleikamp 			__le32 ret;
2413617ba13bSMingming Cao 			if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2414617ba13bSMingming Cao 					~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2415617ba13bSMingming Cao 				printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2416ac27a0ecSDave Kleikamp 				       "remount RDWR because of unsupported "
2417ac27a0ecSDave Kleikamp 				       "optional features (%x).\n",
2418ac27a0ecSDave Kleikamp 				       sb->s_id, le32_to_cpu(ret));
2419ac27a0ecSDave Kleikamp 				err = -EROFS;
2420ac27a0ecSDave Kleikamp 				goto restore_opts;
2421ac27a0ecSDave Kleikamp 			}
2422ac27a0ecSDave Kleikamp 			/*
2423ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
2424ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
2425ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
2426ac27a0ecSDave Kleikamp 			 * the partition.)
2427ac27a0ecSDave Kleikamp 			 */
2428617ba13bSMingming Cao 			ext4_clear_journal_err(sb, es);
2429ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
2430617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
2431ac27a0ecSDave Kleikamp 				goto restore_opts;
2432617ba13bSMingming Cao 			if (!ext4_setup_super (sb, es, 0))
2433ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
2434ac27a0ecSDave Kleikamp 		}
2435ac27a0ecSDave Kleikamp 	}
2436ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2437ac27a0ecSDave Kleikamp 	/* Release old quota file names */
2438ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2439ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
2440ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2441ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
2442ac27a0ecSDave Kleikamp #endif
2443ac27a0ecSDave Kleikamp 	return 0;
2444ac27a0ecSDave Kleikamp restore_opts:
2445ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
2446ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
2447ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
2448ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
2449ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
2450ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2451ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2452ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2453ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
2454ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2455ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
2456ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2457ac27a0ecSDave Kleikamp 	}
2458ac27a0ecSDave Kleikamp #endif
2459ac27a0ecSDave Kleikamp 	return err;
2460ac27a0ecSDave Kleikamp }
2461ac27a0ecSDave Kleikamp 
2462617ba13bSMingming Cao static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
2463ac27a0ecSDave Kleikamp {
2464ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
2465617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2466617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
2467617ba13bSMingming Cao 	ext4_fsblk_t overhead;
2468ac27a0ecSDave Kleikamp 	int i;
2469960cc398SPekka Enberg 	u64 fsid;
2470ac27a0ecSDave Kleikamp 
2471ac27a0ecSDave Kleikamp 	if (test_opt (sb, MINIX_DF))
2472ac27a0ecSDave Kleikamp 		overhead = 0;
2473ac27a0ecSDave Kleikamp 	else {
2474ac27a0ecSDave Kleikamp 		unsigned long ngroups;
2475617ba13bSMingming Cao 		ngroups = EXT4_SB(sb)->s_groups_count;
2476ac27a0ecSDave Kleikamp 		smp_rmb();
2477ac27a0ecSDave Kleikamp 
2478ac27a0ecSDave Kleikamp 		/*
2479ac27a0ecSDave Kleikamp 		 * Compute the overhead (FS structures)
2480ac27a0ecSDave Kleikamp 		 */
2481ac27a0ecSDave Kleikamp 
2482ac27a0ecSDave Kleikamp 		/*
2483ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
2484ac27a0ecSDave Kleikamp 		 * overhead
2485ac27a0ecSDave Kleikamp 		 */
2486ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
2487ac27a0ecSDave Kleikamp 
2488ac27a0ecSDave Kleikamp 		/*
2489ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
2490ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
2491ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
2492ac27a0ecSDave Kleikamp 		 */
2493ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
2494617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
2495617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
2496ac27a0ecSDave Kleikamp 			cond_resched();
2497ac27a0ecSDave Kleikamp 		}
2498ac27a0ecSDave Kleikamp 
2499ac27a0ecSDave Kleikamp 		/*
2500ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
2501ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
2502ac27a0ecSDave Kleikamp 		 */
2503617ba13bSMingming Cao 		overhead += (ngroups * (2 + EXT4_SB(sb)->s_itb_per_group));
2504ac27a0ecSDave Kleikamp 	}
2505ac27a0ecSDave Kleikamp 
2506617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
2507ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
2508bd81d8eeSLaurent Vivier 	buf->f_blocks = ext4_blocks_count(es) - overhead;
2509ac27a0ecSDave Kleikamp 	buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
2510bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2511bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
2512ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
2513ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
2514ac27a0ecSDave Kleikamp 	buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
2515617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
2516960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
2517960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2518960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2519960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
2520ac27a0ecSDave Kleikamp 	return 0;
2521ac27a0ecSDave Kleikamp }
2522ac27a0ecSDave Kleikamp 
2523ac27a0ecSDave Kleikamp /* Helper function for writing quotas on sync - we need to start transaction before quota file
2524ac27a0ecSDave Kleikamp  * is locked for write. Otherwise the are possible deadlocks:
2525ac27a0ecSDave Kleikamp  * Process 1                         Process 2
2526617ba13bSMingming Cao  * ext4_create()                     quota_sync()
2527dab291afSMingming Cao  *   jbd2_journal_start()                   write_dquot()
2528ac27a0ecSDave Kleikamp  *   DQUOT_INIT()                        down(dqio_mutex)
2529dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
2530ac27a0ecSDave Kleikamp  *
2531ac27a0ecSDave Kleikamp  */
2532ac27a0ecSDave Kleikamp 
2533ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2534ac27a0ecSDave Kleikamp 
2535ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
2536ac27a0ecSDave Kleikamp {
2537ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2538ac27a0ecSDave Kleikamp }
2539ac27a0ecSDave Kleikamp 
2540617ba13bSMingming Cao static int ext4_dquot_initialize(struct inode *inode, int type)
2541ac27a0ecSDave Kleikamp {
2542ac27a0ecSDave Kleikamp 	handle_t *handle;
2543ac27a0ecSDave Kleikamp 	int ret, err;
2544ac27a0ecSDave Kleikamp 
2545ac27a0ecSDave Kleikamp 	/* We may create quota structure so we need to reserve enough blocks */
2546617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
2547ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2548ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2549ac27a0ecSDave Kleikamp 	ret = dquot_initialize(inode, type);
2550617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2551ac27a0ecSDave Kleikamp 	if (!ret)
2552ac27a0ecSDave Kleikamp 		ret = err;
2553ac27a0ecSDave Kleikamp 	return ret;
2554ac27a0ecSDave Kleikamp }
2555ac27a0ecSDave Kleikamp 
2556617ba13bSMingming Cao static int ext4_dquot_drop(struct inode *inode)
2557ac27a0ecSDave Kleikamp {
2558ac27a0ecSDave Kleikamp 	handle_t *handle;
2559ac27a0ecSDave Kleikamp 	int ret, err;
2560ac27a0ecSDave Kleikamp 
2561ac27a0ecSDave Kleikamp 	/* We may delete quota structure so we need to reserve enough blocks */
2562617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
2563ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2564ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2565ac27a0ecSDave Kleikamp 	ret = dquot_drop(inode);
2566617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2567ac27a0ecSDave Kleikamp 	if (!ret)
2568ac27a0ecSDave Kleikamp 		ret = err;
2569ac27a0ecSDave Kleikamp 	return ret;
2570ac27a0ecSDave Kleikamp }
2571ac27a0ecSDave Kleikamp 
2572617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
2573ac27a0ecSDave Kleikamp {
2574ac27a0ecSDave Kleikamp 	int ret, err;
2575ac27a0ecSDave Kleikamp 	handle_t *handle;
2576ac27a0ecSDave Kleikamp 	struct inode *inode;
2577ac27a0ecSDave Kleikamp 
2578ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
2579617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
2580617ba13bSMingming Cao 					EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2581ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2582ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2583ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
2584617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2585ac27a0ecSDave Kleikamp 	if (!ret)
2586ac27a0ecSDave Kleikamp 		ret = err;
2587ac27a0ecSDave Kleikamp 	return ret;
2588ac27a0ecSDave Kleikamp }
2589ac27a0ecSDave Kleikamp 
2590617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
2591ac27a0ecSDave Kleikamp {
2592ac27a0ecSDave Kleikamp 	int ret, err;
2593ac27a0ecSDave Kleikamp 	handle_t *handle;
2594ac27a0ecSDave Kleikamp 
2595617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
2596617ba13bSMingming Cao 					EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2597ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2598ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2599ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
2600617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2601ac27a0ecSDave Kleikamp 	if (!ret)
2602ac27a0ecSDave Kleikamp 		ret = err;
2603ac27a0ecSDave Kleikamp 	return ret;
2604ac27a0ecSDave Kleikamp }
2605ac27a0ecSDave Kleikamp 
2606617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
2607ac27a0ecSDave Kleikamp {
2608ac27a0ecSDave Kleikamp 	int ret, err;
2609ac27a0ecSDave Kleikamp 	handle_t *handle;
2610ac27a0ecSDave Kleikamp 
2611617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
2612617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2613ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2614ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2615ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
2616617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2617ac27a0ecSDave Kleikamp 	if (!ret)
2618ac27a0ecSDave Kleikamp 		ret = err;
2619ac27a0ecSDave Kleikamp 	return ret;
2620ac27a0ecSDave Kleikamp }
2621ac27a0ecSDave Kleikamp 
2622617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
2623ac27a0ecSDave Kleikamp {
2624ac27a0ecSDave Kleikamp 	/* Are we journalling quotas? */
2625617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2626617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2627ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
2628617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
2629ac27a0ecSDave Kleikamp 	} else {
2630ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
2631ac27a0ecSDave Kleikamp 	}
2632ac27a0ecSDave Kleikamp }
2633ac27a0ecSDave Kleikamp 
2634617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
2635ac27a0ecSDave Kleikamp {
2636ac27a0ecSDave Kleikamp 	int ret, err;
2637ac27a0ecSDave Kleikamp 	handle_t *handle;
2638ac27a0ecSDave Kleikamp 
2639ac27a0ecSDave Kleikamp 	/* Data block + inode block */
2640617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
2641ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
2642ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
2643ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
2644617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2645ac27a0ecSDave Kleikamp 	if (!ret)
2646ac27a0ecSDave Kleikamp 		ret = err;
2647ac27a0ecSDave Kleikamp 	return ret;
2648ac27a0ecSDave Kleikamp }
2649ac27a0ecSDave Kleikamp 
2650ac27a0ecSDave Kleikamp /*
2651ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
2652ac27a0ecSDave Kleikamp  * the quota file and such...
2653ac27a0ecSDave Kleikamp  */
2654617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
2655ac27a0ecSDave Kleikamp {
2656617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2657617ba13bSMingming Cao 			EXT4_SB(sb)->s_jquota_fmt, type);
2658ac27a0ecSDave Kleikamp }
2659ac27a0ecSDave Kleikamp 
2660ac27a0ecSDave Kleikamp /*
2661ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
2662ac27a0ecSDave Kleikamp  */
2663617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
2664ac27a0ecSDave Kleikamp 			 char *path)
2665ac27a0ecSDave Kleikamp {
2666ac27a0ecSDave Kleikamp 	int err;
2667ac27a0ecSDave Kleikamp 	struct nameidata nd;
2668ac27a0ecSDave Kleikamp 
2669ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
2670ac27a0ecSDave Kleikamp 		return -EINVAL;
2671ac27a0ecSDave Kleikamp 	/* Not journalling quota? */
2672617ba13bSMingming Cao 	if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2673617ba13bSMingming Cao 	    !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
2674ac27a0ecSDave Kleikamp 		return vfs_quota_on(sb, type, format_id, path);
2675ac27a0ecSDave Kleikamp 	err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2676ac27a0ecSDave Kleikamp 	if (err)
2677ac27a0ecSDave Kleikamp 		return err;
2678ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
2679ac27a0ecSDave Kleikamp 	if (nd.mnt->mnt_sb != sb) {
2680ac27a0ecSDave Kleikamp 		path_release(&nd);
2681ac27a0ecSDave Kleikamp 		return -EXDEV;
2682ac27a0ecSDave Kleikamp 	}
2683ac27a0ecSDave Kleikamp 	/* Quotafile not of fs root? */
2684ac27a0ecSDave Kleikamp 	if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2685ac27a0ecSDave Kleikamp 		printk(KERN_WARNING
2686617ba13bSMingming Cao 			"EXT4-fs: Quota file not on filesystem root. "
2687ac27a0ecSDave Kleikamp 			"Journalled quota will not work.\n");
2688ac27a0ecSDave Kleikamp 	path_release(&nd);
2689ac27a0ecSDave Kleikamp 	return vfs_quota_on(sb, type, format_id, path);
2690ac27a0ecSDave Kleikamp }
2691ac27a0ecSDave Kleikamp 
2692ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
2693ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
2694ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
2695ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
2696617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
2697ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
2698ac27a0ecSDave Kleikamp {
2699ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
2700617ba13bSMingming Cao 	sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2701ac27a0ecSDave Kleikamp 	int err = 0;
2702ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
2703ac27a0ecSDave Kleikamp 	int tocopy;
2704ac27a0ecSDave Kleikamp 	size_t toread;
2705ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2706ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
2707ac27a0ecSDave Kleikamp 
2708ac27a0ecSDave Kleikamp 	if (off > i_size)
2709ac27a0ecSDave Kleikamp 		return 0;
2710ac27a0ecSDave Kleikamp 	if (off+len > i_size)
2711ac27a0ecSDave Kleikamp 		len = i_size-off;
2712ac27a0ecSDave Kleikamp 	toread = len;
2713ac27a0ecSDave Kleikamp 	while (toread > 0) {
2714ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
2715ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
2716617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
2717ac27a0ecSDave Kleikamp 		if (err)
2718ac27a0ecSDave Kleikamp 			return err;
2719ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
2720ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
2721ac27a0ecSDave Kleikamp 		else
2722ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
2723ac27a0ecSDave Kleikamp 		brelse(bh);
2724ac27a0ecSDave Kleikamp 		offset = 0;
2725ac27a0ecSDave Kleikamp 		toread -= tocopy;
2726ac27a0ecSDave Kleikamp 		data += tocopy;
2727ac27a0ecSDave Kleikamp 		blk++;
2728ac27a0ecSDave Kleikamp 	}
2729ac27a0ecSDave Kleikamp 	return len;
2730ac27a0ecSDave Kleikamp }
2731ac27a0ecSDave Kleikamp 
2732ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
2733ac27a0ecSDave Kleikamp  * enough credits) */
2734617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
2735ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
2736ac27a0ecSDave Kleikamp {
2737ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
2738617ba13bSMingming Cao 	sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2739ac27a0ecSDave Kleikamp 	int err = 0;
2740ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
2741ac27a0ecSDave Kleikamp 	int tocopy;
2742617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
2743ac27a0ecSDave Kleikamp 	size_t towrite = len;
2744ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2745ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
2746ac27a0ecSDave Kleikamp 
2747ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2748ac27a0ecSDave Kleikamp 	while (towrite > 0) {
2749ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
2750ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
2751617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
2752ac27a0ecSDave Kleikamp 		if (!bh)
2753ac27a0ecSDave Kleikamp 			goto out;
2754ac27a0ecSDave Kleikamp 		if (journal_quota) {
2755617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
2756ac27a0ecSDave Kleikamp 			if (err) {
2757ac27a0ecSDave Kleikamp 				brelse(bh);
2758ac27a0ecSDave Kleikamp 				goto out;
2759ac27a0ecSDave Kleikamp 			}
2760ac27a0ecSDave Kleikamp 		}
2761ac27a0ecSDave Kleikamp 		lock_buffer(bh);
2762ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
2763ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
2764ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
2765ac27a0ecSDave Kleikamp 		if (journal_quota)
2766617ba13bSMingming Cao 			err = ext4_journal_dirty_metadata(handle, bh);
2767ac27a0ecSDave Kleikamp 		else {
2768ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
2769617ba13bSMingming Cao 			err = ext4_journal_dirty_data(handle, bh);
2770ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
2771ac27a0ecSDave Kleikamp 		}
2772ac27a0ecSDave Kleikamp 		brelse(bh);
2773ac27a0ecSDave Kleikamp 		if (err)
2774ac27a0ecSDave Kleikamp 			goto out;
2775ac27a0ecSDave Kleikamp 		offset = 0;
2776ac27a0ecSDave Kleikamp 		towrite -= tocopy;
2777ac27a0ecSDave Kleikamp 		data += tocopy;
2778ac27a0ecSDave Kleikamp 		blk++;
2779ac27a0ecSDave Kleikamp 	}
2780ac27a0ecSDave Kleikamp out:
2781ac27a0ecSDave Kleikamp 	if (len == towrite)
2782ac27a0ecSDave Kleikamp 		return err;
2783ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
2784ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
2785617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
2786ac27a0ecSDave Kleikamp 	}
2787ac27a0ecSDave Kleikamp 	inode->i_version++;
2788ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2789617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
2790ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
2791ac27a0ecSDave Kleikamp 	return len - towrite;
2792ac27a0ecSDave Kleikamp }
2793ac27a0ecSDave Kleikamp 
2794ac27a0ecSDave Kleikamp #endif
2795ac27a0ecSDave Kleikamp 
2796617ba13bSMingming Cao static int ext4_get_sb(struct file_system_type *fs_type,
2797ac27a0ecSDave Kleikamp 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2798ac27a0ecSDave Kleikamp {
2799617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
2800ac27a0ecSDave Kleikamp }
2801ac27a0ecSDave Kleikamp 
2802617ba13bSMingming Cao static struct file_system_type ext4dev_fs_type = {
2803ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
2804617ba13bSMingming Cao 	.name		= "ext4dev",
2805617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
2806ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
2807ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
2808ac27a0ecSDave Kleikamp };
2809ac27a0ecSDave Kleikamp 
2810617ba13bSMingming Cao static int __init init_ext4_fs(void)
2811ac27a0ecSDave Kleikamp {
2812617ba13bSMingming Cao 	int err = init_ext4_xattr();
2813ac27a0ecSDave Kleikamp 	if (err)
2814ac27a0ecSDave Kleikamp 		return err;
2815ac27a0ecSDave Kleikamp 	err = init_inodecache();
2816ac27a0ecSDave Kleikamp 	if (err)
2817ac27a0ecSDave Kleikamp 		goto out1;
2818617ba13bSMingming Cao 	err = register_filesystem(&ext4dev_fs_type);
2819ac27a0ecSDave Kleikamp 	if (err)
2820ac27a0ecSDave Kleikamp 		goto out;
2821ac27a0ecSDave Kleikamp 	return 0;
2822ac27a0ecSDave Kleikamp out:
2823ac27a0ecSDave Kleikamp 	destroy_inodecache();
2824ac27a0ecSDave Kleikamp out1:
2825617ba13bSMingming Cao 	exit_ext4_xattr();
2826ac27a0ecSDave Kleikamp 	return err;
2827ac27a0ecSDave Kleikamp }
2828ac27a0ecSDave Kleikamp 
2829617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
2830ac27a0ecSDave Kleikamp {
2831617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
2832ac27a0ecSDave Kleikamp 	destroy_inodecache();
2833617ba13bSMingming Cao 	exit_ext4_xattr();
2834ac27a0ecSDave Kleikamp }
2835ac27a0ecSDave Kleikamp 
2836ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2837617ba13bSMingming Cao MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
2838ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
2839617ba13bSMingming Cao module_init(init_ext4_fs)
2840617ba13bSMingming Cao module_exit(exit_ext4_fs)
2841