xref: /linux/fs/ext4/super.c (revision 55138e0bc29c0751e2152df9ad35deea542f29b3)
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>
23c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
24dab291afSMingming Cao #include <linux/jbd2.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
28ac27a0ecSDave Kleikamp #include <linux/parser.h>
29ac27a0ecSDave Kleikamp #include <linux/smp_lock.h>
30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
31a5694255SChristoph Hellwig #include <linux/exportfs.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>
389f6200bbSTheodore Ts'o #include <linux/proc_fs.h>
393197ebdbSTheodore Ts'o #include <linux/ctype.h>
401330593eSVignesh Babu #include <linux/log2.h>
41717d50e4SAndreas Dilger #include <linux/crc16.h>
42ac27a0ecSDave Kleikamp #include <asm/uaccess.h>
43ac27a0ecSDave Kleikamp 
443dcf5451SChristoph Hellwig #include "ext4.h"
453dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
46ac27a0ecSDave Kleikamp #include "xattr.h"
47ac27a0ecSDave Kleikamp #include "acl.h"
483661d286STheodore Ts'o #include "mballoc.h"
49ac27a0ecSDave Kleikamp 
509bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
519bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
529bffad1eSTheodore Ts'o 
53f4033903SCurt Wohlgemuth static int default_mb_history_length = 1000;
54f4033903SCurt Wohlgemuth 
55f4033903SCurt Wohlgemuth module_param_named(default_mb_history_length, default_mb_history_length,
56f4033903SCurt Wohlgemuth 		   int, 0644);
57f4033903SCurt Wohlgemuth MODULE_PARM_DESC(default_mb_history_length,
58f4033903SCurt Wohlgemuth 		 "Default number of entries saved for mb_history");
59f4033903SCurt Wohlgemuth 
609f6200bbSTheodore Ts'o struct proc_dir_entry *ext4_proc_root;
613197ebdbSTheodore Ts'o static struct kset *ext4_kset;
629f6200bbSTheodore Ts'o 
63617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
64ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
65e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
66617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
67617ba13bSMingming Cao 					struct ext4_super_block *es);
68617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
69617ba13bSMingming Cao 				   struct ext4_super_block *es);
70617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
71617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
72ac27a0ecSDave Kleikamp 				     char nbuf[16]);
73617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
74617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
75c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
76617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
78ac27a0ecSDave Kleikamp 
79bd81d8eeSLaurent Vivier 
808fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
818fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
82bd81d8eeSLaurent Vivier {
833a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
848fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
858fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
86bd81d8eeSLaurent Vivier }
87bd81d8eeSLaurent Vivier 
888fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
898fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
90bd81d8eeSLaurent Vivier {
915272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
928fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
938fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
94bd81d8eeSLaurent Vivier }
95bd81d8eeSLaurent Vivier 
968fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
978fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
98bd81d8eeSLaurent Vivier {
995272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
1008fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1018fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
102bd81d8eeSLaurent Vivier }
103bd81d8eeSLaurent Vivier 
104560671a0SAneesh Kumar K.V __u32 ext4_free_blks_count(struct super_block *sb,
105560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
106560671a0SAneesh Kumar K.V {
107560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
108560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
109560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
110560671a0SAneesh Kumar K.V }
111560671a0SAneesh Kumar K.V 
112560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
113560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
114560671a0SAneesh Kumar K.V {
115560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
116560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
117560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
118560671a0SAneesh Kumar K.V }
119560671a0SAneesh Kumar K.V 
120560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
121560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
122560671a0SAneesh Kumar K.V {
123560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
124560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
125560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
126560671a0SAneesh Kumar K.V }
127560671a0SAneesh Kumar K.V 
128560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
129560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
130560671a0SAneesh Kumar K.V {
131560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
132560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
133560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
134560671a0SAneesh Kumar K.V }
135560671a0SAneesh Kumar K.V 
1368fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
1378fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
138bd81d8eeSLaurent Vivier {
1393a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1408fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1418fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
142bd81d8eeSLaurent Vivier }
143bd81d8eeSLaurent Vivier 
1448fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1458fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
146bd81d8eeSLaurent Vivier {
1475272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1488fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1498fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
150bd81d8eeSLaurent Vivier }
151bd81d8eeSLaurent Vivier 
1528fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1538fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
154bd81d8eeSLaurent Vivier {
1555272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1568fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1578fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
158bd81d8eeSLaurent Vivier }
159bd81d8eeSLaurent Vivier 
160560671a0SAneesh Kumar K.V void ext4_free_blks_set(struct super_block *sb,
161560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
162560671a0SAneesh Kumar K.V {
163560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
164560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
165560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
166560671a0SAneesh Kumar K.V }
167560671a0SAneesh Kumar K.V 
168560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
169560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
170560671a0SAneesh Kumar K.V {
171560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
172560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
173560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
174560671a0SAneesh Kumar K.V }
175560671a0SAneesh Kumar K.V 
176560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
177560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
178560671a0SAneesh Kumar K.V {
179560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
180560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
181560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
182560671a0SAneesh Kumar K.V }
183560671a0SAneesh Kumar K.V 
184560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
185560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
186560671a0SAneesh Kumar K.V {
187560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
188560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
189560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
190560671a0SAneesh Kumar K.V }
191560671a0SAneesh Kumar K.V 
192ac27a0ecSDave Kleikamp /*
193dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
194ac27a0ecSDave Kleikamp  *
195ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
196ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
197ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
198ac27a0ecSDave Kleikamp  * appropriate.
199ac27a0ecSDave Kleikamp  */
200617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
201ac27a0ecSDave Kleikamp {
202ac27a0ecSDave Kleikamp 	journal_t *journal;
203ac27a0ecSDave Kleikamp 
204ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
205ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
206ac27a0ecSDave Kleikamp 
207ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
208ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
209ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
210617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2110390131bSFrank Mayhar 	if (journal) {
212ac27a0ecSDave Kleikamp 		if (is_journal_aborted(journal)) {
2130b8e58a1SAndreas Dilger 			ext4_abort(sb, __func__, "Detected aborted journal");
214ac27a0ecSDave Kleikamp 			return ERR_PTR(-EROFS);
215ac27a0ecSDave Kleikamp 		}
216dab291afSMingming Cao 		return jbd2_journal_start(journal, nblocks);
217ac27a0ecSDave Kleikamp 	}
2180390131bSFrank Mayhar 	/*
2190390131bSFrank Mayhar 	 * We're not journaling, return the appropriate indication.
2200390131bSFrank Mayhar 	 */
2210390131bSFrank Mayhar 	current->journal_info = EXT4_NOJOURNAL_HANDLE;
2220390131bSFrank Mayhar 	return current->journal_info;
2230390131bSFrank Mayhar }
224ac27a0ecSDave Kleikamp 
225ac27a0ecSDave Kleikamp /*
226ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
227dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
228ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
229ac27a0ecSDave Kleikamp  * appropriate.
230ac27a0ecSDave Kleikamp  */
231617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
232ac27a0ecSDave Kleikamp {
233ac27a0ecSDave Kleikamp 	struct super_block *sb;
234ac27a0ecSDave Kleikamp 	int err;
235ac27a0ecSDave Kleikamp 	int rc;
236ac27a0ecSDave Kleikamp 
2370390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
2380390131bSFrank Mayhar 		/*
2390390131bSFrank Mayhar 		 * Do this here since we don't call jbd2_journal_stop() in
2400390131bSFrank Mayhar 		 * no-journal mode.
2410390131bSFrank Mayhar 		 */
2420390131bSFrank Mayhar 		current->journal_info = NULL;
2430390131bSFrank Mayhar 		return 0;
2440390131bSFrank Mayhar 	}
245ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
246ac27a0ecSDave Kleikamp 	err = handle->h_err;
247dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
248ac27a0ecSDave Kleikamp 
249ac27a0ecSDave Kleikamp 	if (!err)
250ac27a0ecSDave Kleikamp 		err = rc;
251ac27a0ecSDave Kleikamp 	if (err)
252617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
253ac27a0ecSDave Kleikamp 	return err;
254ac27a0ecSDave Kleikamp }
255ac27a0ecSDave Kleikamp 
256617ba13bSMingming Cao void ext4_journal_abort_handle(const char *caller, const char *err_fn,
257ac27a0ecSDave Kleikamp 		struct buffer_head *bh, handle_t *handle, int err)
258ac27a0ecSDave Kleikamp {
259ac27a0ecSDave Kleikamp 	char nbuf[16];
260617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
261ac27a0ecSDave Kleikamp 
2620390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
2630390131bSFrank Mayhar 
264ac27a0ecSDave Kleikamp 	if (bh)
265ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
266ac27a0ecSDave Kleikamp 
267ac27a0ecSDave Kleikamp 	if (!handle->h_err)
268ac27a0ecSDave Kleikamp 		handle->h_err = err;
269ac27a0ecSDave Kleikamp 
270ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
271ac27a0ecSDave Kleikamp 		return;
272ac27a0ecSDave Kleikamp 
273ac27a0ecSDave Kleikamp 	printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
274ac27a0ecSDave Kleikamp 	       caller, errstr, err_fn);
275ac27a0ecSDave Kleikamp 
276dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
277ac27a0ecSDave Kleikamp }
278ac27a0ecSDave Kleikamp 
279ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
280ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
281ac27a0ecSDave Kleikamp  *
282ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
283617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
284ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
285ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
286ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
287ac27a0ecSDave Kleikamp  * write out the superblock safely.
288ac27a0ecSDave Kleikamp  *
289dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
290ac27a0ecSDave Kleikamp  * the journal instead.  On recovery, the journal will compain about
291ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
292ac27a0ecSDave Kleikamp  */
293ac27a0ecSDave Kleikamp 
294617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
295ac27a0ecSDave Kleikamp {
296617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
297ac27a0ecSDave Kleikamp 
298617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
299617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
300ac27a0ecSDave Kleikamp 
301ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
302ac27a0ecSDave Kleikamp 		return;
303ac27a0ecSDave Kleikamp 
304ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
305617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
306ac27a0ecSDave Kleikamp 
3074ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
308ac27a0ecSDave Kleikamp 		if (journal)
309dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
310ac27a0ecSDave Kleikamp 	}
311ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
312b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
313ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
314ac27a0ecSDave Kleikamp 	}
315e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
316ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
317617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
318ac27a0ecSDave Kleikamp 			sb->s_id);
319ac27a0ecSDave Kleikamp }
320ac27a0ecSDave Kleikamp 
321617ba13bSMingming Cao void ext4_error(struct super_block *sb, const char *function,
322ac27a0ecSDave Kleikamp 		const char *fmt, ...)
323ac27a0ecSDave Kleikamp {
324ac27a0ecSDave Kleikamp 	va_list args;
325ac27a0ecSDave Kleikamp 
326ac27a0ecSDave Kleikamp 	va_start(args, fmt);
327617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
328ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
329ac27a0ecSDave Kleikamp 	printk("\n");
330ac27a0ecSDave Kleikamp 	va_end(args);
331ac27a0ecSDave Kleikamp 
332617ba13bSMingming Cao 	ext4_handle_error(sb);
333ac27a0ecSDave Kleikamp }
334ac27a0ecSDave Kleikamp 
335617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
336ac27a0ecSDave Kleikamp 				     char nbuf[16])
337ac27a0ecSDave Kleikamp {
338ac27a0ecSDave Kleikamp 	char *errstr = NULL;
339ac27a0ecSDave Kleikamp 
340ac27a0ecSDave Kleikamp 	switch (errno) {
341ac27a0ecSDave Kleikamp 	case -EIO:
342ac27a0ecSDave Kleikamp 		errstr = "IO failure";
343ac27a0ecSDave Kleikamp 		break;
344ac27a0ecSDave Kleikamp 	case -ENOMEM:
345ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
346ac27a0ecSDave Kleikamp 		break;
347ac27a0ecSDave Kleikamp 	case -EROFS:
34878f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
34978f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
350ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
351ac27a0ecSDave Kleikamp 		else
352ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
353ac27a0ecSDave Kleikamp 		break;
354ac27a0ecSDave Kleikamp 	default:
355ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
356ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
357ac27a0ecSDave Kleikamp 		 * NULL. */
358ac27a0ecSDave Kleikamp 		if (nbuf) {
359ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
360ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
361ac27a0ecSDave Kleikamp 				errstr = nbuf;
362ac27a0ecSDave Kleikamp 		}
363ac27a0ecSDave Kleikamp 		break;
364ac27a0ecSDave Kleikamp 	}
365ac27a0ecSDave Kleikamp 
366ac27a0ecSDave Kleikamp 	return errstr;
367ac27a0ecSDave Kleikamp }
368ac27a0ecSDave Kleikamp 
369617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
370ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
371ac27a0ecSDave Kleikamp 
3722b2d6d01STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, int errno)
373ac27a0ecSDave Kleikamp {
374ac27a0ecSDave Kleikamp 	char nbuf[16];
375ac27a0ecSDave Kleikamp 	const char *errstr;
376ac27a0ecSDave Kleikamp 
377ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
378ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
379ac27a0ecSDave Kleikamp 	 * an error. */
380ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
381ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
382ac27a0ecSDave Kleikamp 		return;
383ac27a0ecSDave Kleikamp 
384617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
385617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
386ac27a0ecSDave Kleikamp 	       sb->s_id, function, errstr);
387ac27a0ecSDave Kleikamp 
388617ba13bSMingming Cao 	ext4_handle_error(sb);
389ac27a0ecSDave Kleikamp }
390ac27a0ecSDave Kleikamp 
391ac27a0ecSDave Kleikamp /*
392617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
393ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
394ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
395ac27a0ecSDave Kleikamp  *
396ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
397ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
398ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
399ac27a0ecSDave Kleikamp  */
400ac27a0ecSDave Kleikamp 
401617ba13bSMingming Cao void ext4_abort(struct super_block *sb, const char *function,
402ac27a0ecSDave Kleikamp 		const char *fmt, ...)
403ac27a0ecSDave Kleikamp {
404ac27a0ecSDave Kleikamp 	va_list args;
405ac27a0ecSDave Kleikamp 
406ac27a0ecSDave Kleikamp 	va_start(args, fmt);
407617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
408ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
409ac27a0ecSDave Kleikamp 	printk("\n");
410ac27a0ecSDave Kleikamp 	va_end(args);
411ac27a0ecSDave Kleikamp 
412ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
413617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
414ac27a0ecSDave Kleikamp 
415ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
416ac27a0ecSDave Kleikamp 		return;
417ac27a0ecSDave Kleikamp 
418b31e1552SEric Sandeen 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
419617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
420ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
4214ab2f15bSTheodore Ts'o 	EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
422ef2cabf7SHidehiro Kawai 	if (EXT4_SB(sb)->s_journal)
423dab291afSMingming Cao 		jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
424ac27a0ecSDave Kleikamp }
425ac27a0ecSDave Kleikamp 
426b31e1552SEric Sandeen void ext4_msg (struct super_block * sb, const char *prefix,
427b31e1552SEric Sandeen 		   const char *fmt, ...)
428b31e1552SEric Sandeen {
429b31e1552SEric Sandeen 	va_list args;
430b31e1552SEric Sandeen 
431b31e1552SEric Sandeen 	va_start(args, fmt);
432b31e1552SEric Sandeen 	printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
433b31e1552SEric Sandeen 	vprintk(fmt, args);
434b31e1552SEric Sandeen 	printk("\n");
435b31e1552SEric Sandeen 	va_end(args);
436b31e1552SEric Sandeen }
437b31e1552SEric Sandeen 
438617ba13bSMingming Cao void ext4_warning(struct super_block *sb, const char *function,
439ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
440ac27a0ecSDave Kleikamp {
441ac27a0ecSDave Kleikamp 	va_list args;
442ac27a0ecSDave Kleikamp 
443ac27a0ecSDave Kleikamp 	va_start(args, fmt);
444617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
445ac27a0ecSDave Kleikamp 	       sb->s_id, function);
446ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
447ac27a0ecSDave Kleikamp 	printk("\n");
448ac27a0ecSDave Kleikamp 	va_end(args);
449ac27a0ecSDave Kleikamp }
450ac27a0ecSDave Kleikamp 
4515d1b1b3fSAneesh Kumar K.V void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
4525d1b1b3fSAneesh Kumar K.V 			   const char *function, const char *fmt, ...)
4535d1b1b3fSAneesh Kumar K.V __releases(bitlock)
4545d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
4555d1b1b3fSAneesh Kumar K.V {
4565d1b1b3fSAneesh Kumar K.V 	va_list args;
4575d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4585d1b1b3fSAneesh Kumar K.V 
4595d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
4605d1b1b3fSAneesh Kumar K.V 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
4615d1b1b3fSAneesh Kumar K.V 	vprintk(fmt, args);
4625d1b1b3fSAneesh Kumar K.V 	printk("\n");
4635d1b1b3fSAneesh Kumar K.V 	va_end(args);
4645d1b1b3fSAneesh Kumar K.V 
4655d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
4665d1b1b3fSAneesh Kumar K.V 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4675d1b1b3fSAneesh Kumar K.V 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
468e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
4695d1b1b3fSAneesh Kumar K.V 		return;
4705d1b1b3fSAneesh Kumar K.V 	}
4715d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
4725d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
4735d1b1b3fSAneesh Kumar K.V 	/*
4745d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
4755d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
4765d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
4775d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
4785d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
4795d1b1b3fSAneesh Kumar K.V 	 * ext4_grp_locked_error() to distinguish beween the
4805d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
4815d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
4825d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
4835d1b1b3fSAneesh Kumar K.V 	 */
4845d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
4855d1b1b3fSAneesh Kumar K.V 	return;
4865d1b1b3fSAneesh Kumar K.V }
4875d1b1b3fSAneesh Kumar K.V 
488617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
489ac27a0ecSDave Kleikamp {
490617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
491ac27a0ecSDave Kleikamp 
492617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
493ac27a0ecSDave Kleikamp 		return;
494ac27a0ecSDave Kleikamp 
49546e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
496ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
497ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
498617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
499ac27a0ecSDave Kleikamp 
500617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
501617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
502617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
503ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
504ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
505ac27a0ecSDave Kleikamp 
506ac27a0ecSDave Kleikamp 	/*
507ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
508ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
509ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
510ac27a0ecSDave Kleikamp 	 */
511ac27a0ecSDave Kleikamp }
512ac27a0ecSDave Kleikamp 
513ac27a0ecSDave Kleikamp /*
514ac27a0ecSDave Kleikamp  * Open the external journal device
515ac27a0ecSDave Kleikamp  */
516b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
517ac27a0ecSDave Kleikamp {
518ac27a0ecSDave Kleikamp 	struct block_device *bdev;
519ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
520ac27a0ecSDave Kleikamp 
521ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
522ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
523ac27a0ecSDave Kleikamp 		goto fail;
524ac27a0ecSDave Kleikamp 	return bdev;
525ac27a0ecSDave Kleikamp 
526ac27a0ecSDave Kleikamp fail:
527b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
528ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
529ac27a0ecSDave Kleikamp 	return NULL;
530ac27a0ecSDave Kleikamp }
531ac27a0ecSDave Kleikamp 
532ac27a0ecSDave Kleikamp /*
533ac27a0ecSDave Kleikamp  * Release the journal device
534ac27a0ecSDave Kleikamp  */
535617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
536ac27a0ecSDave Kleikamp {
537ac27a0ecSDave Kleikamp 	bd_release(bdev);
5389a1c3542SAl Viro 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
539ac27a0ecSDave Kleikamp }
540ac27a0ecSDave Kleikamp 
541617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
542ac27a0ecSDave Kleikamp {
543ac27a0ecSDave Kleikamp 	struct block_device *bdev;
544ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
545ac27a0ecSDave Kleikamp 
546ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
547ac27a0ecSDave Kleikamp 	if (bdev) {
548617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
549ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
550ac27a0ecSDave Kleikamp 	}
551ac27a0ecSDave Kleikamp 	return ret;
552ac27a0ecSDave Kleikamp }
553ac27a0ecSDave Kleikamp 
554ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
555ac27a0ecSDave Kleikamp {
556617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
557ac27a0ecSDave Kleikamp }
558ac27a0ecSDave Kleikamp 
559617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
560ac27a0ecSDave Kleikamp {
561ac27a0ecSDave Kleikamp 	struct list_head *l;
562ac27a0ecSDave Kleikamp 
563b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
564ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
565ac27a0ecSDave Kleikamp 
566ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
567ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
568ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
569ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
570ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
571ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
572ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
573ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
574ac27a0ecSDave Kleikamp 	}
575ac27a0ecSDave Kleikamp }
576ac27a0ecSDave Kleikamp 
577617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
578ac27a0ecSDave Kleikamp {
579617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
580617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
581ef2cabf7SHidehiro Kawai 	int i, err;
582ac27a0ecSDave Kleikamp 
583a9e220f8SAl Viro 	lock_super(sb);
5846cfd0148SChristoph Hellwig 	lock_kernel();
5858c85e125SChristoph Hellwig 	if (sb->s_dirt)
586ebc1ac16SChristoph Hellwig 		ext4_commit_super(sb, 1);
5878c85e125SChristoph Hellwig 
5886fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
589c9de560dSAlex Tomas 	ext4_mb_release(sb);
590a86c6181SAlex Tomas 	ext4_ext_release(sb);
591617ba13bSMingming Cao 	ext4_xattr_put_super(sb);
5920390131bSFrank Mayhar 	if (sbi->s_journal) {
593ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
59447b4a50bSJan Kara 		sbi->s_journal = NULL;
595ef2cabf7SHidehiro Kawai 		if (err < 0)
5960390131bSFrank Mayhar 			ext4_abort(sb, __func__,
5970390131bSFrank Mayhar 				   "Couldn't clean up the journal");
5980390131bSFrank Mayhar 	}
599ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
600617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
601ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
602e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
603ac27a0ecSDave Kleikamp 	}
604240799cdSTheodore Ts'o 	if (sbi->s_proc) {
6059f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
606240799cdSTheodore Ts'o 	}
6073197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
608ac27a0ecSDave Kleikamp 
609ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
610ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
611ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
612c5ca7c76STheodore Ts'o 	if (is_vmalloc_addr(sbi->s_flex_groups))
613c5ca7c76STheodore Ts'o 		vfree(sbi->s_flex_groups);
614c5ca7c76STheodore Ts'o 	else
615772cb7c8SJose R. Santos 		kfree(sbi->s_flex_groups);
616ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
617ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
618ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
6196bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
620ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
621ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
622ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
623ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
624ac27a0ecSDave Kleikamp #endif
625ac27a0ecSDave Kleikamp 
626ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
627ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
628ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
629ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
630ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
631ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
632ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
633ac27a0ecSDave Kleikamp 
634f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
635ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
636ac27a0ecSDave Kleikamp 		/*
637ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
638ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
639ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
640ac27a0ecSDave Kleikamp 		 */
641ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
642f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
643617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
644ac27a0ecSDave Kleikamp 	}
645ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
6463197ebdbSTheodore Ts'o 	/*
6473197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
6483197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
6493197ebdbSTheodore Ts'o 	 */
6503197ebdbSTheodore Ts'o 	unlock_kernel();
6513197ebdbSTheodore Ts'o 	unlock_super(sb);
6523197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
6533197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
654705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
655ac27a0ecSDave Kleikamp 	kfree(sbi);
656ac27a0ecSDave Kleikamp }
657ac27a0ecSDave Kleikamp 
658e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
659ac27a0ecSDave Kleikamp 
660ac27a0ecSDave Kleikamp /*
661ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
662ac27a0ecSDave Kleikamp  */
663617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
664ac27a0ecSDave Kleikamp {
665617ba13bSMingming Cao 	struct ext4_inode_info *ei;
666ac27a0ecSDave Kleikamp 
667e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
668ac27a0ecSDave Kleikamp 	if (!ei)
669ac27a0ecSDave Kleikamp 		return NULL;
6700b8e58a1SAndreas Dilger 
671ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
67291246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
673a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
674c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
675c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
6760390131bSFrank Mayhar 	/*
6770390131bSFrank Mayhar 	 * Note:  We can be called before EXT4_SB(sb)->s_journal is set,
6780390131bSFrank Mayhar 	 * therefore it can be null here.  Don't check it, just initialize
6790390131bSFrank Mayhar 	 * jinode.
6800390131bSFrank Mayhar 	 */
681678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
682d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
683d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
684d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
685d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
686d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
6870b8e58a1SAndreas Dilger 
688ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
689ac27a0ecSDave Kleikamp }
690ac27a0ecSDave Kleikamp 
691617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
692ac27a0ecSDave Kleikamp {
6939f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
694b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
695b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
696b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
6979f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
6989f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
6999f7dd93dSVasily Averin 				true);
7009f7dd93dSVasily Averin 		dump_stack();
7019f7dd93dSVasily Averin 	}
702617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
703ac27a0ecSDave Kleikamp }
704ac27a0ecSDave Kleikamp 
70551cc5068SAlexey Dobriyan static void init_once(void *foo)
706ac27a0ecSDave Kleikamp {
707617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
708ac27a0ecSDave Kleikamp 
709ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
71003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
711ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
712ac27a0ecSDave Kleikamp #endif
7130e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
714ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
715ac27a0ecSDave Kleikamp }
716ac27a0ecSDave Kleikamp 
717ac27a0ecSDave Kleikamp static int init_inodecache(void)
718ac27a0ecSDave Kleikamp {
719617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
720617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
721ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
722ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
72320c2df83SPaul Mundt 					     init_once);
724617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
725ac27a0ecSDave Kleikamp 		return -ENOMEM;
726ac27a0ecSDave Kleikamp 	return 0;
727ac27a0ecSDave Kleikamp }
728ac27a0ecSDave Kleikamp 
729ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
730ac27a0ecSDave Kleikamp {
731617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
732ac27a0ecSDave Kleikamp }
733ac27a0ecSDave Kleikamp 
734617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
735ac27a0ecSDave Kleikamp {
736c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
7370390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode))
738678aaf48SJan Kara 		jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
739678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
740ac27a0ecSDave Kleikamp }
741ac27a0ecSDave Kleikamp 
7422b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
7432b2d6d01STheodore Ts'o 					   struct super_block *sb)
744ac27a0ecSDave Kleikamp {
745ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
746617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
747ac27a0ecSDave Kleikamp 
748ac27a0ecSDave Kleikamp 	if (sbi->s_jquota_fmt)
749ac27a0ecSDave Kleikamp 		seq_printf(seq, ",jqfmt=%s",
750ac27a0ecSDave Kleikamp 		(sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
751ac27a0ecSDave Kleikamp 
752ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
753ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
754ac27a0ecSDave Kleikamp 
755ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
756ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
757ac27a0ecSDave Kleikamp 
758617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
759ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
760ac27a0ecSDave Kleikamp 
761617ba13bSMingming Cao 	if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
762ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
763ac27a0ecSDave Kleikamp #endif
764ac27a0ecSDave Kleikamp }
765ac27a0ecSDave Kleikamp 
766d9c9bef1SMiklos Szeredi /*
767d9c9bef1SMiklos Szeredi  * Show an option if
768d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
769d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
770d9c9bef1SMiklos Szeredi  */
771617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
772ac27a0ecSDave Kleikamp {
773aa22df2cSAneesh Kumar K.V 	int def_errors;
774aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
775ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
776d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
777d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
778d9c9bef1SMiklos Szeredi 
779d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
780aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
781d9c9bef1SMiklos Szeredi 
782d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
783d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
784d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
785d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
786aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
787d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
788d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
789d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
790d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
791d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
792d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
793d9c9bef1SMiklos Szeredi 	}
794d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
795d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
796d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
797d9c9bef1SMiklos Szeredi 	}
798bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
799d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
800bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
801d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
802bb4f397aSAneesh Kumar K.V 		}
803bb4f397aSAneesh Kumar K.V 	}
804aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
805bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
806aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
807d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
808aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
809d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
810aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
811d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
812d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
813d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
81403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
815aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
816aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
817d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
818d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
819d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
820d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
821d9c9bef1SMiklos Szeredi 	}
822d9c9bef1SMiklos Szeredi #endif
82303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
824aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
825d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
826d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
827d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
828d9c9bef1SMiklos Szeredi #endif
82930773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
830d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
831d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
832d9c9bef1SMiklos Szeredi 	}
83330773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
83430773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
83530773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
83630773840STheodore Ts'o 	}
83730773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
83830773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
83930773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
84030773840STheodore Ts'o 	}
84130773840STheodore Ts'o 
842571640caSEric Sandeen 	/*
843571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
844571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
845571640caSEric Sandeen 	 * status is.
846571640caSEric Sandeen 	 */
847571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
848571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
849cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
850cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
851d9c9bef1SMiklos Szeredi 	if (test_opt(sb, NOBH))
852d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nobh");
85325ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
85425ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
855dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
856dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
857dd919b98SAneesh Kumar K.V 
858ac27a0ecSDave Kleikamp 
859cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
860cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
861aa22df2cSAneesh Kumar K.V 	/*
862aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
863aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
864aa22df2cSAneesh Kumar K.V 	 */
865617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
866ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
867617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
868ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
869617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
870ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
871ac27a0ecSDave Kleikamp 
872240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
873240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
874240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
875240799cdSTheodore Ts'o 
8765bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
8775bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
8785bf5683aSHidehiro Kawai 
879afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
88006705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
881afd4672dSTheodore Ts'o 
882617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
8830b8e58a1SAndreas Dilger 
884ac27a0ecSDave Kleikamp 	return 0;
885ac27a0ecSDave Kleikamp }
886ac27a0ecSDave Kleikamp 
8871b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
8881b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
889ac27a0ecSDave Kleikamp {
890ac27a0ecSDave Kleikamp 	struct inode *inode;
891ac27a0ecSDave Kleikamp 
892617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
893ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
894617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
895ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
896ac27a0ecSDave Kleikamp 
897ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
898ac27a0ecSDave Kleikamp 	 *
899617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
900ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
901ac27a0ecSDave Kleikamp 	 *
902ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
903ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
904ac27a0ecSDave Kleikamp 	 */
9051d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
9061d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9071d1fe1eeSDavid Howells 		return ERR_CAST(inode);
9081d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
909ac27a0ecSDave Kleikamp 		iput(inode);
910ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
911ac27a0ecSDave Kleikamp 	}
9121b961ac0SChristoph Hellwig 
9131b961ac0SChristoph Hellwig 	return inode;
914ac27a0ecSDave Kleikamp }
9151b961ac0SChristoph Hellwig 
9161b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
9171b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
9181b961ac0SChristoph Hellwig {
9191b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
9201b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
9211b961ac0SChristoph Hellwig }
9221b961ac0SChristoph Hellwig 
9231b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
9241b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
9251b961ac0SChristoph Hellwig {
9261b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
9271b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
928ac27a0ecSDave Kleikamp }
929ac27a0ecSDave Kleikamp 
930c39a7f84SToshiyuki Okajima /*
931c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
932c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
933c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
934c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
935c39a7f84SToshiyuki Okajima  */
9360b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
9370b8e58a1SAndreas Dilger 				 gfp_t wait)
938c39a7f84SToshiyuki Okajima {
939c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
940c39a7f84SToshiyuki Okajima 
941c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
942c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
943c39a7f84SToshiyuki Okajima 		return 0;
944c39a7f84SToshiyuki Okajima 	if (journal)
945c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
946c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
947c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
948c39a7f84SToshiyuki Okajima }
949c39a7f84SToshiyuki Okajima 
950ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
951ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
952ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
953ac27a0ecSDave Kleikamp 
954617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
955617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
956617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
957617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
958617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
9596f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
9606f28e087SJan Kara 				char *path, int remount);
961617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
962617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
963ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
964617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
965ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
966ac27a0ecSDave Kleikamp 
96761e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
968edf72453SJan Kara 	.initialize	= dquot_initialize,
969edf72453SJan Kara 	.drop		= dquot_drop,
970ac27a0ecSDave Kleikamp 	.alloc_space	= dquot_alloc_space,
97160e58e0fSMingming Cao 	.reserve_space	= dquot_reserve_space,
97260e58e0fSMingming Cao 	.claim_space	= dquot_claim_space,
97360e58e0fSMingming Cao 	.release_rsv	= dquot_release_reserved_space,
97460e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
975ac27a0ecSDave Kleikamp 	.alloc_inode	= dquot_alloc_inode,
976ac27a0ecSDave Kleikamp 	.free_space	= dquot_free_space,
977ac27a0ecSDave Kleikamp 	.free_inode	= dquot_free_inode,
978ac27a0ecSDave Kleikamp 	.transfer	= dquot_transfer,
979617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
980617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
981617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
982617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
983a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
984a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
985a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
986ac27a0ecSDave Kleikamp };
987ac27a0ecSDave Kleikamp 
9880d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
989617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
990ac27a0ecSDave Kleikamp 	.quota_off	= vfs_quota_off,
991ac27a0ecSDave Kleikamp 	.quota_sync	= vfs_quota_sync,
992ac27a0ecSDave Kleikamp 	.get_info	= vfs_get_dqinfo,
993ac27a0ecSDave Kleikamp 	.set_info	= vfs_set_dqinfo,
994ac27a0ecSDave Kleikamp 	.get_dqblk	= vfs_get_dqblk,
995ac27a0ecSDave Kleikamp 	.set_dqblk	= vfs_set_dqblk
996ac27a0ecSDave Kleikamp };
997ac27a0ecSDave Kleikamp #endif
998ac27a0ecSDave Kleikamp 
999ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1000617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1001617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1002617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1003617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
1004617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
1005617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1006617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1007c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1008c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1009617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1010617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1011617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
1012617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1013ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1014617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1015617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1016ac27a0ecSDave Kleikamp #endif
1017c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1018ac27a0ecSDave Kleikamp };
1019ac27a0ecSDave Kleikamp 
10209ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
10219ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
10229ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
10239ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
10249ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
10259ca92389STheodore Ts'o 	.delete_inode	= ext4_delete_inode,
10269ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
10279ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
10289ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
10299ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
10309ca92389STheodore Ts'o 	.clear_inode	= ext4_clear_inode,
10319ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
10329ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
10339ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
10349ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
10359ca92389STheodore Ts'o #endif
10369ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
10379ca92389STheodore Ts'o };
10389ca92389STheodore Ts'o 
103939655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
10401b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
10411b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1042617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1043ac27a0ecSDave Kleikamp };
1044ac27a0ecSDave Kleikamp 
1045ac27a0ecSDave Kleikamp enum {
1046ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1047ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
104801436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1049ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
105006705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
105130773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1052c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1053818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1054ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1055f4033903SCurt Wohlgemuth 	Opt_data_err_abort, Opt_data_err_ignore, Opt_mb_history_length,
1056ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1057ac27a0ecSDave Kleikamp 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
105806705bffSTheodore Ts'o 	Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize,
105906705bffSTheodore Ts'o 	Opt_usrquota, Opt_grpquota, Opt_i_version,
106001436ef2STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc,
10616fd058f7STheodore Ts'o 	Opt_block_validity, Opt_noblock_validity,
1062b3881f74STheodore Ts'o 	Opt_inode_readahead_blks, Opt_journal_ioprio
1063ac27a0ecSDave Kleikamp };
1064ac27a0ecSDave Kleikamp 
1065a447c093SSteven Whitehouse static const match_table_t tokens = {
1066ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1067ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1068ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1069ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1070ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1071ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1072ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1073ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1074ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1075ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1076ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1077ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1078ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1079ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1080ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1081ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1082ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1083ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1084ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1085ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1086ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1087ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1088ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1089ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
109030773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
109130773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1092ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1093ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1094818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1095818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1096ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1097ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1098ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1099ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
11005bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
11015bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1102f4033903SCurt Wohlgemuth 	{Opt_mb_history_length, "mb_history_length=%u"},
1103ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1104ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1105ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1106ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1107ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1108ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1109ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1110ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1111ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1112ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1113ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
111406705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
111506705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
111625ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1117c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1118ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
111964769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1120dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
11216fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
11226fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1123240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1124b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1125afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
112606705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
112706705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1128f3f12faaSJosef Bacik 	{Opt_err, NULL},
1129ac27a0ecSDave Kleikamp };
1130ac27a0ecSDave Kleikamp 
1131617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1132ac27a0ecSDave Kleikamp {
1133617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1134ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1135ac27a0ecSDave Kleikamp 
1136ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1137ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
11380b8e58a1SAndreas Dilger 
1139ac27a0ecSDave Kleikamp 	options += 3;
11400b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1141ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1142ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
11434776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1144ac27a0ecSDave Kleikamp 		       (char *) *data);
1145ac27a0ecSDave Kleikamp 		return 1;
1146ac27a0ecSDave Kleikamp 	}
1147ac27a0ecSDave Kleikamp 	if (*options == ',')
1148ac27a0ecSDave Kleikamp 		options++;
1149ac27a0ecSDave Kleikamp 	*data = (void *) options;
11500b8e58a1SAndreas Dilger 
1151ac27a0ecSDave Kleikamp 	return sb_block;
1152ac27a0ecSDave Kleikamp }
1153ac27a0ecSDave Kleikamp 
1154b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1155b3881f74STheodore Ts'o 
1156ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1157c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1158b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1159617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1160ac27a0ecSDave Kleikamp {
1161617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1162ac27a0ecSDave Kleikamp 	char *p;
1163ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1164ac27a0ecSDave Kleikamp 	int data_opt = 0;
1165ac27a0ecSDave Kleikamp 	int option;
1166ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1167dfc5d03fSJan Kara 	int qtype, qfmt;
1168ac27a0ecSDave Kleikamp 	char *qname;
1169ac27a0ecSDave Kleikamp #endif
1170ac27a0ecSDave Kleikamp 
1171ac27a0ecSDave Kleikamp 	if (!options)
1172ac27a0ecSDave Kleikamp 		return 1;
1173ac27a0ecSDave Kleikamp 
1174ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1175ac27a0ecSDave Kleikamp 		int token;
1176ac27a0ecSDave Kleikamp 		if (!*p)
1177ac27a0ecSDave Kleikamp 			continue;
1178ac27a0ecSDave Kleikamp 
1179ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1180ac27a0ecSDave Kleikamp 		switch (token) {
1181ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1182ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
1183ac27a0ecSDave Kleikamp 			break;
1184ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1185ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
1186ac27a0ecSDave Kleikamp 			break;
1187ac27a0ecSDave Kleikamp 		case Opt_grpid:
1188ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
1189ac27a0ecSDave Kleikamp 			break;
1190ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1191ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
1192ac27a0ecSDave Kleikamp 			break;
1193ac27a0ecSDave Kleikamp 		case Opt_resuid:
1194ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1195ac27a0ecSDave Kleikamp 				return 0;
1196ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1197ac27a0ecSDave Kleikamp 			break;
1198ac27a0ecSDave Kleikamp 		case Opt_resgid:
1199ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1200ac27a0ecSDave Kleikamp 				return 0;
1201ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1202ac27a0ecSDave Kleikamp 			break;
1203ac27a0ecSDave Kleikamp 		case Opt_sb:
1204ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1205ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1206ac27a0ecSDave Kleikamp 			break;
1207ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1208ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1209ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1210ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1211ac27a0ecSDave Kleikamp 			break;
1212ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1213ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1214ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1215ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1216ac27a0ecSDave Kleikamp 			break;
1217ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1218ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1219ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1220ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1221ac27a0ecSDave Kleikamp 			break;
1222ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1223ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1224ac27a0ecSDave Kleikamp 			break;
1225ac27a0ecSDave Kleikamp 		case Opt_debug:
1226ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1227ac27a0ecSDave Kleikamp 			break;
1228ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1229ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1230ac27a0ecSDave Kleikamp 			break;
1231ac27a0ecSDave Kleikamp 		case Opt_orlov:
1232ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1233ac27a0ecSDave Kleikamp 			break;
123403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1235ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1236ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1237ac27a0ecSDave Kleikamp 			break;
1238ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1239ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1240ac27a0ecSDave Kleikamp 			break;
1241ac27a0ecSDave Kleikamp #else
1242ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1243ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1244b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1245ac27a0ecSDave Kleikamp 			break;
1246ac27a0ecSDave Kleikamp #endif
124703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1248ac27a0ecSDave Kleikamp 		case Opt_acl:
1249ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1250ac27a0ecSDave Kleikamp 			break;
1251ac27a0ecSDave Kleikamp 		case Opt_noacl:
1252ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1253ac27a0ecSDave Kleikamp 			break;
1254ac27a0ecSDave Kleikamp #else
1255ac27a0ecSDave Kleikamp 		case Opt_acl:
1256ac27a0ecSDave Kleikamp 		case Opt_noacl:
1257b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1258ac27a0ecSDave Kleikamp 			break;
1259ac27a0ecSDave Kleikamp #endif
1260ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1261ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1262ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1263ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1264ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1265ac27a0ecSDave Kleikamp 			   journal file. */
1266ac27a0ecSDave Kleikamp 			if (is_remount) {
1267b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1268b31e1552SEric Sandeen 					 "Cannot specify journal on remount");
1269ac27a0ecSDave Kleikamp 				return 0;
1270ac27a0ecSDave Kleikamp 			}
1271ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1272ac27a0ecSDave Kleikamp 			break;
1273ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1274ac27a0ecSDave Kleikamp 			if (is_remount) {
1275b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1276b31e1552SEric Sandeen 					"Cannot specify journal on remount");
1277ac27a0ecSDave Kleikamp 				return 0;
1278ac27a0ecSDave Kleikamp 			}
1279ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1280ac27a0ecSDave Kleikamp 				return 0;
1281ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1282ac27a0ecSDave Kleikamp 			break;
1283818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1284d0646f7bSTheodore Ts'o 			break;	/* Kept for backwards compatibility */
1285818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1286818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1287818d276cSGirish Shilamkar 			break;
1288ac27a0ecSDave Kleikamp 		case Opt_noload:
1289ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1290ac27a0ecSDave Kleikamp 			break;
1291ac27a0ecSDave Kleikamp 		case Opt_commit:
1292ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1293ac27a0ecSDave Kleikamp 				return 0;
1294ac27a0ecSDave Kleikamp 			if (option < 0)
1295ac27a0ecSDave Kleikamp 				return 0;
1296ac27a0ecSDave Kleikamp 			if (option == 0)
1297cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1298ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1299ac27a0ecSDave Kleikamp 			break;
130030773840STheodore Ts'o 		case Opt_max_batch_time:
130130773840STheodore Ts'o 			if (match_int(&args[0], &option))
130230773840STheodore Ts'o 				return 0;
130330773840STheodore Ts'o 			if (option < 0)
130430773840STheodore Ts'o 				return 0;
130530773840STheodore Ts'o 			if (option == 0)
130630773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
130730773840STheodore Ts'o 			sbi->s_max_batch_time = option;
130830773840STheodore Ts'o 			break;
130930773840STheodore Ts'o 		case Opt_min_batch_time:
131030773840STheodore Ts'o 			if (match_int(&args[0], &option))
131130773840STheodore Ts'o 				return 0;
131230773840STheodore Ts'o 			if (option < 0)
131330773840STheodore Ts'o 				return 0;
131430773840STheodore Ts'o 			sbi->s_min_batch_time = option;
131530773840STheodore Ts'o 			break;
1316ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1317617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1318ac27a0ecSDave Kleikamp 			goto datacheck;
1319ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1320617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1321ac27a0ecSDave Kleikamp 			goto datacheck;
1322ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1323617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1324ac27a0ecSDave Kleikamp 		datacheck:
1325ac27a0ecSDave Kleikamp 			if (is_remount) {
1326617ba13bSMingming Cao 				if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1327ac27a0ecSDave Kleikamp 						!= data_opt) {
1328b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
1329b31e1552SEric Sandeen 						"Cannot change data mode on remount");
1330ac27a0ecSDave Kleikamp 					return 0;
1331ac27a0ecSDave Kleikamp 				}
1332ac27a0ecSDave Kleikamp 			} else {
1333617ba13bSMingming Cao 				sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1334ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1335ac27a0ecSDave Kleikamp 			}
1336ac27a0ecSDave Kleikamp 			break;
13375bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
13385bf5683aSHidehiro Kawai 			set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13395bf5683aSHidehiro Kawai 			break;
13405bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
13415bf5683aSHidehiro Kawai 			clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
13425bf5683aSHidehiro Kawai 			break;
1343f4033903SCurt Wohlgemuth 		case Opt_mb_history_length:
1344f4033903SCurt Wohlgemuth 			if (match_int(&args[0], &option))
1345f4033903SCurt Wohlgemuth 				return 0;
1346f4033903SCurt Wohlgemuth 			if (option < 0)
1347f4033903SCurt Wohlgemuth 				return 0;
1348f4033903SCurt Wohlgemuth 			sbi->s_mb_history_max = option;
1349f4033903SCurt Wohlgemuth 			break;
1350ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1351ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1352ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1353ac27a0ecSDave Kleikamp 			goto set_qf_name;
1354ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1355ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1356ac27a0ecSDave Kleikamp set_qf_name:
135717bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1358dfc5d03fSJan Kara 			    !sbi->s_qf_names[qtype]) {
1359b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1360b31e1552SEric Sandeen 				       "Cannot change journaled "
1361b31e1552SEric Sandeen 				       "quota options when quota turned on");
1362ac27a0ecSDave Kleikamp 				return 0;
1363ac27a0ecSDave Kleikamp 			}
1364ac27a0ecSDave Kleikamp 			qname = match_strdup(&args[0]);
1365ac27a0ecSDave Kleikamp 			if (!qname) {
1366b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1367b31e1552SEric Sandeen 					"Not enough memory for "
1368b31e1552SEric Sandeen 					"storing quotafile name");
1369ac27a0ecSDave Kleikamp 				return 0;
1370ac27a0ecSDave Kleikamp 			}
1371ac27a0ecSDave Kleikamp 			if (sbi->s_qf_names[qtype] &&
1372ac27a0ecSDave Kleikamp 			    strcmp(sbi->s_qf_names[qtype], qname)) {
1373b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1374b31e1552SEric Sandeen 					"%s quota file already "
1375b31e1552SEric Sandeen 					"specified", QTYPE2NAME(qtype));
1376ac27a0ecSDave Kleikamp 				kfree(qname);
1377ac27a0ecSDave Kleikamp 				return 0;
1378ac27a0ecSDave Kleikamp 			}
1379ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = qname;
1380ac27a0ecSDave Kleikamp 			if (strchr(sbi->s_qf_names[qtype], '/')) {
1381b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1382b31e1552SEric Sandeen 					"quotafile must be on "
1383b31e1552SEric Sandeen 					"filesystem root");
1384ac27a0ecSDave Kleikamp 				kfree(sbi->s_qf_names[qtype]);
1385ac27a0ecSDave Kleikamp 				sbi->s_qf_names[qtype] = NULL;
1386ac27a0ecSDave Kleikamp 				return 0;
1387ac27a0ecSDave Kleikamp 			}
1388ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1389ac27a0ecSDave Kleikamp 			break;
1390ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1391ac27a0ecSDave Kleikamp 			qtype = USRQUOTA;
1392ac27a0ecSDave Kleikamp 			goto clear_qf_name;
1393ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1394ac27a0ecSDave Kleikamp 			qtype = GRPQUOTA;
1395ac27a0ecSDave Kleikamp clear_qf_name:
139617bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1397dfc5d03fSJan Kara 			    sbi->s_qf_names[qtype]) {
1398b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
13992c8be6b2SJan Kara 					"journaled quota options when "
1400b31e1552SEric Sandeen 					"quota turned on");
1401ac27a0ecSDave Kleikamp 				return 0;
1402ac27a0ecSDave Kleikamp 			}
1403ac27a0ecSDave Kleikamp 			/*
1404ac27a0ecSDave Kleikamp 			 * The space will be released later when all options
1405ac27a0ecSDave Kleikamp 			 * are confirmed to be correct
1406ac27a0ecSDave Kleikamp 			 */
1407ac27a0ecSDave Kleikamp 			sbi->s_qf_names[qtype] = NULL;
1408ac27a0ecSDave Kleikamp 			break;
1409ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1410dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1411dfc5d03fSJan Kara 			goto set_qf_format;
1412ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1413dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
1414dfc5d03fSJan Kara set_qf_format:
141517bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1416dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1417b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
1418dfc5d03fSJan Kara 					"journaled quota options when "
1419b31e1552SEric Sandeen 					"quota turned on");
1420dfc5d03fSJan Kara 				return 0;
1421dfc5d03fSJan Kara 			}
1422dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1423ac27a0ecSDave Kleikamp 			break;
1424ac27a0ecSDave Kleikamp 		case Opt_quota:
1425ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1426ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1427ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1428ac27a0ecSDave Kleikamp 			break;
1429ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1430ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1431ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1432ac27a0ecSDave Kleikamp 			break;
1433ac27a0ecSDave Kleikamp 		case Opt_noquota:
143417bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1435b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change quota "
1436b31e1552SEric Sandeen 					"options when quota turned on");
1437ac27a0ecSDave Kleikamp 				return 0;
1438ac27a0ecSDave Kleikamp 			}
1439ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1440ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1441ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1442ac27a0ecSDave Kleikamp 			break;
1443ac27a0ecSDave Kleikamp #else
1444ac27a0ecSDave Kleikamp 		case Opt_quota:
1445ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1446ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1447b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1448b31e1552SEric Sandeen 				"quota options not supported");
1449cd59e7b9SJan Kara 			break;
1450ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1451ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1452ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1453ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1454ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1455ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1456b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1457b31e1552SEric Sandeen 				"journaled quota options not supported");
1458ac27a0ecSDave Kleikamp 			break;
1459ac27a0ecSDave Kleikamp 		case Opt_noquota:
1460ac27a0ecSDave Kleikamp 			break;
1461ac27a0ecSDave Kleikamp #endif
1462ac27a0ecSDave Kleikamp 		case Opt_abort:
14634ab2f15bSTheodore Ts'o 			sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1464ac27a0ecSDave Kleikamp 			break;
146506705bffSTheodore Ts'o 		case Opt_nobarrier:
146606705bffSTheodore Ts'o 			clear_opt(sbi->s_mount_opt, BARRIER);
146706705bffSTheodore Ts'o 			break;
1468ac27a0ecSDave Kleikamp 		case Opt_barrier:
146906705bffSTheodore Ts'o 			if (match_int(&args[0], &option)) {
147006705bffSTheodore Ts'o 				set_opt(sbi->s_mount_opt, BARRIER);
147106705bffSTheodore Ts'o 				break;
147206705bffSTheodore Ts'o 			}
1473ac27a0ecSDave Kleikamp 			if (option)
1474ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1475ac27a0ecSDave Kleikamp 			else
1476ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1477ac27a0ecSDave Kleikamp 			break;
1478ac27a0ecSDave Kleikamp 		case Opt_ignore:
1479ac27a0ecSDave Kleikamp 			break;
1480ac27a0ecSDave Kleikamp 		case Opt_resize:
1481ac27a0ecSDave Kleikamp 			if (!is_remount) {
1482b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1483b31e1552SEric Sandeen 					"resize option only available "
1484b31e1552SEric Sandeen 					"for remount");
1485ac27a0ecSDave Kleikamp 				return 0;
1486ac27a0ecSDave Kleikamp 			}
1487ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1488ac27a0ecSDave Kleikamp 				return 0;
1489ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1490ac27a0ecSDave Kleikamp 			break;
1491ac27a0ecSDave Kleikamp 		case Opt_nobh:
1492ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOBH);
1493ac27a0ecSDave Kleikamp 			break;
1494ac27a0ecSDave Kleikamp 		case Opt_bh:
1495ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
1496ac27a0ecSDave Kleikamp 			break;
149725ec56b5SJean Noel Cordenner 		case Opt_i_version:
149825ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
149925ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
150025ec56b5SJean Noel Cordenner 			break;
1501dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1502dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1503dd919b98SAneesh Kumar K.V 			break;
1504c9de560dSAlex Tomas 		case Opt_stripe:
1505c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1506c9de560dSAlex Tomas 				return 0;
1507c9de560dSAlex Tomas 			if (option < 0)
1508c9de560dSAlex Tomas 				return 0;
1509c9de560dSAlex Tomas 			sbi->s_stripe = option;
1510c9de560dSAlex Tomas 			break;
151164769240SAlex Tomas 		case Opt_delalloc:
151264769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
151364769240SAlex Tomas 			break;
15146fd058f7STheodore Ts'o 		case Opt_block_validity:
15156fd058f7STheodore Ts'o 			set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
15166fd058f7STheodore Ts'o 			break;
15176fd058f7STheodore Ts'o 		case Opt_noblock_validity:
15186fd058f7STheodore Ts'o 			clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
15196fd058f7STheodore Ts'o 			break;
1520240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1521240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1522240799cdSTheodore Ts'o 				return 0;
1523240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1524240799cdSTheodore Ts'o 				return 0;
1525f7c43950STheodore Ts'o 			if (!is_power_of_2(option)) {
1526b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1527b31e1552SEric Sandeen 					 "EXT4-fs: inode_readahead_blks"
1528b31e1552SEric Sandeen 					 " must be a power of 2");
15293197ebdbSTheodore Ts'o 				return 0;
15303197ebdbSTheodore Ts'o 			}
1531240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1532240799cdSTheodore Ts'o 			break;
1533b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1534b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1535b3881f74STheodore Ts'o 				return 0;
1536b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1537b3881f74STheodore Ts'o 				break;
1538b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1539b3881f74STheodore Ts'o 							    option);
1540b3881f74STheodore Ts'o 			break;
154106705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
154206705bffSTheodore Ts'o 			set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
154306705bffSTheodore Ts'o 			break;
1544afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
154506705bffSTheodore Ts'o 			if (match_int(&args[0], &option)) {
154606705bffSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
154706705bffSTheodore Ts'o 				break;
154806705bffSTheodore Ts'o 			}
1549afd4672dSTheodore Ts'o 			if (option)
1550afd4672dSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1551afd4672dSTheodore Ts'o 			else
1552afd4672dSTheodore Ts'o 				set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1553afd4672dSTheodore Ts'o 			break;
1554ac27a0ecSDave Kleikamp 		default:
1555b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1556b31e1552SEric Sandeen 			       "Unrecognized mount option \"%s\" "
1557b31e1552SEric Sandeen 			       "or missing value", p);
1558ac27a0ecSDave Kleikamp 			return 0;
1559ac27a0ecSDave Kleikamp 		}
1560ac27a0ecSDave Kleikamp 	}
1561ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1562ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1563617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1564ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[USRQUOTA])
1565ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1566ac27a0ecSDave Kleikamp 
1567617ba13bSMingming Cao 		if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1568ac27a0ecSDave Kleikamp 		     sbi->s_qf_names[GRPQUOTA])
1569ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1570ac27a0ecSDave Kleikamp 
1571ac27a0ecSDave Kleikamp 		if ((sbi->s_qf_names[USRQUOTA] &&
1572617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1573ac27a0ecSDave Kleikamp 		    (sbi->s_qf_names[GRPQUOTA] &&
1574617ba13bSMingming Cao 				(sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1575b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1576b31e1552SEric Sandeen 					"format mixing");
1577ac27a0ecSDave Kleikamp 			return 0;
1578ac27a0ecSDave Kleikamp 		}
1579ac27a0ecSDave Kleikamp 
1580ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1581b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1582b31e1552SEric Sandeen 					"not specified");
1583ac27a0ecSDave Kleikamp 			return 0;
1584ac27a0ecSDave Kleikamp 		}
1585ac27a0ecSDave Kleikamp 	} else {
1586ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1587b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
15882c8be6b2SJan Kara 					"specified with no journaling "
1589b31e1552SEric Sandeen 					"enabled");
1590ac27a0ecSDave Kleikamp 			return 0;
1591ac27a0ecSDave Kleikamp 		}
1592ac27a0ecSDave Kleikamp 	}
1593ac27a0ecSDave Kleikamp #endif
1594ac27a0ecSDave Kleikamp 	return 1;
1595ac27a0ecSDave Kleikamp }
1596ac27a0ecSDave Kleikamp 
1597617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1598ac27a0ecSDave Kleikamp 			    int read_only)
1599ac27a0ecSDave Kleikamp {
1600617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1601ac27a0ecSDave Kleikamp 	int res = 0;
1602ac27a0ecSDave Kleikamp 
1603617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1604b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1605b31e1552SEric Sandeen 			 "forcing read-only mode");
1606ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1607ac27a0ecSDave Kleikamp 	}
1608ac27a0ecSDave Kleikamp 	if (read_only)
1609ac27a0ecSDave Kleikamp 		return res;
1610617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1611b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1612b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1613617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1614b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1615b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1616b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1617ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1618ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1619ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1620b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1621b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1622b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1623ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1624ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1625ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1626b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1627b31e1552SEric Sandeen 			 "warning: checktime reached, "
1628b31e1552SEric Sandeen 			 "running e2fsck is recommended");
16290390131bSFrank Mayhar 	if (!sbi->s_journal)
1630216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1631ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1632617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1633e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1634ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1635617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
16360390131bSFrank Mayhar 	if (sbi->s_journal)
1637617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1638ac27a0ecSDave Kleikamp 
1639e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1640ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1641a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
16427f4520ccSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x]\n",
1643ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1644ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1645617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1646617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1647ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1648ac27a0ecSDave Kleikamp 
16490390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
1650b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%s journal on %s",
1651b31e1552SEric Sandeen 		       EXT4_SB(sb)->s_journal->j_inode ? "internal" :
165205496769STheodore Ts'o 		       "external", EXT4_SB(sb)->s_journal->j_devname);
16530390131bSFrank Mayhar 	} else {
1654b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "no journal");
16550390131bSFrank Mayhar 	}
1656ac27a0ecSDave Kleikamp 	return res;
1657ac27a0ecSDave Kleikamp }
1658ac27a0ecSDave Kleikamp 
1659772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1660772cb7c8SJose R. Santos {
1661772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1662772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1663772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1664772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1665772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1666c5ca7c76STheodore Ts'o 	size_t size;
1667772cb7c8SJose R. Santos 	int i;
1668772cb7c8SJose R. Santos 
1669772cb7c8SJose R. Santos 	if (!sbi->s_es->s_log_groups_per_flex) {
1670772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1671772cb7c8SJose R. Santos 		return 1;
1672772cb7c8SJose R. Santos 	}
1673772cb7c8SJose R. Santos 
1674772cb7c8SJose R. Santos 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1675772cb7c8SJose R. Santos 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1676772cb7c8SJose R. Santos 
1677c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1678c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1679d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1680d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1681c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
1682c5ca7c76STheodore Ts'o 	sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1683c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
1684c5ca7c76STheodore Ts'o 		sbi->s_flex_groups = vmalloc(size);
1685c5ca7c76STheodore Ts'o 		if (sbi->s_flex_groups)
1686c5ca7c76STheodore Ts'o 			memset(sbi->s_flex_groups, 0, size);
1687c5ca7c76STheodore Ts'o 	}
1688772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1689b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory for "
1690b31e1552SEric Sandeen 				"%u flex groups", flex_group_count);
1691772cb7c8SJose R. Santos 		goto failed;
1692772cb7c8SJose R. Santos 	}
1693772cb7c8SJose R. Santos 
1694772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
169588b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1696772cb7c8SJose R. Santos 
1697772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
16987ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
16997ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
17007ad9bb65STheodore Ts'o 		atomic_add(ext4_free_blks_count(sb, gdp),
17017ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_blocks);
17027ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
17037ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
1704772cb7c8SJose R. Santos 	}
1705772cb7c8SJose R. Santos 
1706772cb7c8SJose R. Santos 	return 1;
1707772cb7c8SJose R. Santos failed:
1708772cb7c8SJose R. Santos 	return 0;
1709772cb7c8SJose R. Santos }
1710772cb7c8SJose R. Santos 
1711717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1712717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1713717d50e4SAndreas Dilger {
1714717d50e4SAndreas Dilger 	__u16 crc = 0;
1715717d50e4SAndreas Dilger 
1716717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1717717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1718717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1719717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1720717d50e4SAndreas Dilger 
1721717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1722717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1723717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1724717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1725717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1726717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1727717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1728717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1729717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1730717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1731717d50e4SAndreas Dilger 					offset);
1732717d50e4SAndreas Dilger 	}
1733717d50e4SAndreas Dilger 
1734717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1735717d50e4SAndreas Dilger }
1736717d50e4SAndreas Dilger 
1737717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1738717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1739717d50e4SAndreas Dilger {
1740717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1741717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1742717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1743717d50e4SAndreas Dilger 		return 0;
1744717d50e4SAndreas Dilger 
1745717d50e4SAndreas Dilger 	return 1;
1746717d50e4SAndreas Dilger }
1747717d50e4SAndreas Dilger 
1748ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1749617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1750ac27a0ecSDave Kleikamp {
1751617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1752617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1753617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1754bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1755bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1756bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1757ce421581SJose R. Santos 	int flexbg_flag = 0;
1758fd2d4291SAvantika Mathur 	ext4_group_t i;
1759ac27a0ecSDave Kleikamp 
1760ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1761ce421581SJose R. Santos 		flexbg_flag = 1;
1762ce421581SJose R. Santos 
1763617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1764ac27a0ecSDave Kleikamp 
1765197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1766197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1767197cd65aSAkinobu Mita 
1768ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1769bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1770ac27a0ecSDave Kleikamp 		else
1771ac27a0ecSDave Kleikamp 			last_block = first_block +
1772617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1773ac27a0ecSDave Kleikamp 
17748fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
17752b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1776b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1777a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
1778b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
1779ac27a0ecSDave Kleikamp 			return 0;
1780ac27a0ecSDave Kleikamp 		}
17818fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
17822b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1783b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1784a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
1785b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
1786ac27a0ecSDave Kleikamp 			return 0;
1787ac27a0ecSDave Kleikamp 		}
17888fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1789bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
17902b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1791b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1792a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
1793b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
1794ac27a0ecSDave Kleikamp 			return 0;
1795ac27a0ecSDave Kleikamp 		}
1796955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
1797717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1798b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1799b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
1800fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1801fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
18027ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
1803955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
1804717d50e4SAndreas Dilger 				return 0;
1805717d50e4SAndreas Dilger 			}
18067ee1ec4cSLi Zefan 		}
1807955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
1808ce421581SJose R. Santos 		if (!flexbg_flag)
1809617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1810ac27a0ecSDave Kleikamp 	}
1811ac27a0ecSDave Kleikamp 
1812bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1813617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
1814ac27a0ecSDave Kleikamp 	return 1;
1815ac27a0ecSDave Kleikamp }
1816ac27a0ecSDave Kleikamp 
1817617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1818ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1819ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1820ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1821ac27a0ecSDave Kleikamp  *
1822ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1823ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1824ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1825ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1826ac27a0ecSDave Kleikamp  *
1827ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1828ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1829ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1830617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1831ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1832ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1833ac27a0ecSDave Kleikamp  */
1834617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1835617ba13bSMingming Cao 				struct ext4_super_block *es)
1836ac27a0ecSDave Kleikamp {
1837ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1838ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1839ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1840ac27a0ecSDave Kleikamp 	int i;
1841ac27a0ecSDave Kleikamp #endif
1842ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1843ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1844ac27a0ecSDave Kleikamp 		return;
1845ac27a0ecSDave Kleikamp 	}
1846ac27a0ecSDave Kleikamp 
1847a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1848b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
1849b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
1850a8f48a95SEric Sandeen 		return;
1851a8f48a95SEric Sandeen 	}
1852a8f48a95SEric Sandeen 
1853617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1854ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1855ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1856ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
1857ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
1858ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1859ac27a0ecSDave Kleikamp 		return;
1860ac27a0ecSDave Kleikamp 	}
1861ac27a0ecSDave Kleikamp 
1862ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
1863b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
1864ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
1865ac27a0ecSDave Kleikamp 	}
1866ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1867ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
1868ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
1869ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
1870ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1871617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
1872617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
1873ac27a0ecSDave Kleikamp 			if (ret < 0)
1874b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1875b31e1552SEric Sandeen 					"Cannot turn on journaled "
1876b31e1552SEric Sandeen 					"quota: error %d", ret);
1877ac27a0ecSDave Kleikamp 		}
1878ac27a0ecSDave Kleikamp 	}
1879ac27a0ecSDave Kleikamp #endif
1880ac27a0ecSDave Kleikamp 
1881ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
1882ac27a0ecSDave Kleikamp 		struct inode *inode;
1883ac27a0ecSDave Kleikamp 
188497bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
188597bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
1886ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
1887ac27a0ecSDave Kleikamp 			break;
1888ac27a0ecSDave Kleikamp 		}
1889ac27a0ecSDave Kleikamp 
1890617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1891a269eb18SJan Kara 		vfs_dq_init(inode);
1892ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
1893b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
1894b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
189546e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
1896e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1897ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
1898617ba13bSMingming Cao 			ext4_truncate(inode);
1899ac27a0ecSDave Kleikamp 			nr_truncates++;
1900ac27a0ecSDave Kleikamp 		} else {
1901b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
1902b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
190346e665e9SHarvey Harrison 				__func__, inode->i_ino);
1904ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
1905ac27a0ecSDave Kleikamp 				  inode->i_ino);
1906ac27a0ecSDave Kleikamp 			nr_orphans++;
1907ac27a0ecSDave Kleikamp 		}
1908ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
1909ac27a0ecSDave Kleikamp 	}
1910ac27a0ecSDave Kleikamp 
1911ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1912ac27a0ecSDave Kleikamp 
1913ac27a0ecSDave Kleikamp 	if (nr_orphans)
1914b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1915b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
1916ac27a0ecSDave Kleikamp 	if (nr_truncates)
1917b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1918b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
1919ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1920ac27a0ecSDave Kleikamp 	/* Turn quotas off */
1921ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
1922ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
19236f28e087SJan Kara 			vfs_quota_off(sb, i, 0);
1924ac27a0ecSDave Kleikamp 	}
1925ac27a0ecSDave Kleikamp #endif
1926ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1927ac27a0ecSDave Kleikamp }
19280b8e58a1SAndreas Dilger 
1929cd2291a4SEric Sandeen /*
1930cd2291a4SEric Sandeen  * Maximal extent format file size.
1931cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1932cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
1933cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1934cd2291a4SEric Sandeen  * so that won't be a limiting factor.
1935cd2291a4SEric Sandeen  *
1936cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1937cd2291a4SEric Sandeen  */
1938f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
1939cd2291a4SEric Sandeen {
1940cd2291a4SEric Sandeen 	loff_t res;
1941cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
1942cd2291a4SEric Sandeen 
1943cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
1944f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1945cd2291a4SEric Sandeen 		/*
194690c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
1947cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
1948cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
1949cd2291a4SEric Sandeen 		 */
1950cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
1951cd2291a4SEric Sandeen 
1952cd2291a4SEric Sandeen 		/* total blocks in file system block size */
1953cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
1954cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
1955cd2291a4SEric Sandeen 	}
1956cd2291a4SEric Sandeen 
1957cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
1958cd2291a4SEric Sandeen 	res = 1LL << 32;
1959cd2291a4SEric Sandeen 	res <<= blkbits;
1960cd2291a4SEric Sandeen 	res -= 1;
1961cd2291a4SEric Sandeen 
1962cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
1963cd2291a4SEric Sandeen 	if (res > upper_limit)
1964cd2291a4SEric Sandeen 		res = upper_limit;
1965cd2291a4SEric Sandeen 
1966cd2291a4SEric Sandeen 	return res;
1967cd2291a4SEric Sandeen }
1968ac27a0ecSDave Kleikamp 
1969ac27a0ecSDave Kleikamp /*
1970cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
19710fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
19720fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
1973ac27a0ecSDave Kleikamp  */
1974f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1975ac27a0ecSDave Kleikamp {
1976617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
19770fc1b451SAneesh Kumar K.V 	int meta_blocks;
19780fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
19790b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
19800b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
19810b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
19820b8e58a1SAndreas Dilger 	 *
19830b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
19840b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
19850fc1b451SAneesh Kumar K.V 	 */
19860fc1b451SAneesh Kumar K.V 
1987f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
19880fc1b451SAneesh Kumar K.V 		/*
198990c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
19900b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
19910b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
19920fc1b451SAneesh Kumar K.V 		 */
19930fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
19940fc1b451SAneesh Kumar K.V 
19950fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
19960fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
19970fc1b451SAneesh Kumar K.V 
19980fc1b451SAneesh Kumar K.V 	} else {
19998180a562SAneesh Kumar K.V 		/*
20008180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
20018180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
20028180a562SAneesh Kumar K.V 		 * represent total number of blocks in
20038180a562SAneesh Kumar K.V 		 * file system block size
20048180a562SAneesh Kumar K.V 		 */
20050fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
20060fc1b451SAneesh Kumar K.V 
20070fc1b451SAneesh Kumar K.V 	}
20080fc1b451SAneesh Kumar K.V 
20090fc1b451SAneesh Kumar K.V 	/* indirect blocks */
20100fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
20110fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
20120fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
20130fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
20140fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
20150fc1b451SAneesh Kumar K.V 
20160fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
20170fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2018ac27a0ecSDave Kleikamp 
2019ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2020ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2021ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2022ac27a0ecSDave Kleikamp 	res <<= bits;
2023ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2024ac27a0ecSDave Kleikamp 		res = upper_limit;
20250fc1b451SAneesh Kumar K.V 
20260fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
20270fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
20280fc1b451SAneesh Kumar K.V 
2029ac27a0ecSDave Kleikamp 	return res;
2030ac27a0ecSDave Kleikamp }
2031ac27a0ecSDave Kleikamp 
2032617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
203370bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2034ac27a0ecSDave Kleikamp {
2035617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2036fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2037ac27a0ecSDave Kleikamp 	int has_super = 0;
2038ac27a0ecSDave Kleikamp 
2039ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2040ac27a0ecSDave Kleikamp 
2041617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2042ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
204370bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2044ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2045617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2046ac27a0ecSDave Kleikamp 		has_super = 1;
20470b8e58a1SAndreas Dilger 
2048617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2049ac27a0ecSDave Kleikamp }
2050ac27a0ecSDave Kleikamp 
2051c9de560dSAlex Tomas /**
2052c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2053c9de560dSAlex Tomas  * @sbi: In memory super block info
2054c9de560dSAlex Tomas  *
2055c9de560dSAlex Tomas  * If we have specified it via mount option, then
2056c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2057c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2058c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2059c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2060c9de560dSAlex Tomas  *
2061c9de560dSAlex Tomas  */
2062c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2063c9de560dSAlex Tomas {
2064c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2065c9de560dSAlex Tomas 	unsigned long stripe_width =
2066c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2067c9de560dSAlex Tomas 
2068c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2069c9de560dSAlex Tomas 		return sbi->s_stripe;
2070c9de560dSAlex Tomas 
2071c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
2072c9de560dSAlex Tomas 		return stripe_width;
2073c9de560dSAlex Tomas 
2074c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
2075c9de560dSAlex Tomas 		return stride;
2076c9de560dSAlex Tomas 
2077c9de560dSAlex Tomas 	return 0;
2078c9de560dSAlex Tomas }
2079ac27a0ecSDave Kleikamp 
20803197ebdbSTheodore Ts'o /* sysfs supprt */
20813197ebdbSTheodore Ts'o 
20823197ebdbSTheodore Ts'o struct ext4_attr {
20833197ebdbSTheodore Ts'o 	struct attribute attr;
20843197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
20853197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
20863197ebdbSTheodore Ts'o 			 const char *, size_t);
20873197ebdbSTheodore Ts'o 	int offset;
20883197ebdbSTheodore Ts'o };
20893197ebdbSTheodore Ts'o 
20903197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
20913197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
20923197ebdbSTheodore Ts'o {
20933197ebdbSTheodore Ts'o 	char *endp;
20943197ebdbSTheodore Ts'o 
20953197ebdbSTheodore Ts'o 	while (*buf && isspace(*buf))
20963197ebdbSTheodore Ts'o 		buf++;
20973197ebdbSTheodore Ts'o 	*value = simple_strtoul(buf, &endp, 0);
20983197ebdbSTheodore Ts'o 	while (*endp && isspace(*endp))
20993197ebdbSTheodore Ts'o 		endp++;
21003197ebdbSTheodore Ts'o 	if (*endp || *value > max)
21013197ebdbSTheodore Ts'o 		return -EINVAL;
21023197ebdbSTheodore Ts'o 
21033197ebdbSTheodore Ts'o 	return 0;
21043197ebdbSTheodore Ts'o }
21053197ebdbSTheodore Ts'o 
21063197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
21073197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
21083197ebdbSTheodore Ts'o 					      char *buf)
21093197ebdbSTheodore Ts'o {
21103197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
21113197ebdbSTheodore Ts'o 			(s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
21123197ebdbSTheodore Ts'o }
21133197ebdbSTheodore Ts'o 
21143197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
21153197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
21163197ebdbSTheodore Ts'o {
21173197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
21183197ebdbSTheodore Ts'o 
21193197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
21203197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
21213197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
21223197ebdbSTheodore Ts'o }
21233197ebdbSTheodore Ts'o 
21243197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
21253197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
21263197ebdbSTheodore Ts'o {
21273197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
21283197ebdbSTheodore Ts'o 
21293197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
21303197ebdbSTheodore Ts'o 			sbi->s_kbytes_written +
21313197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
21323197ebdbSTheodore Ts'o 			  EXT4_SB(sb)->s_sectors_written_start) >> 1));
21333197ebdbSTheodore Ts'o }
21343197ebdbSTheodore Ts'o 
21353197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
21363197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
21373197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
21383197ebdbSTheodore Ts'o {
21393197ebdbSTheodore Ts'o 	unsigned long t;
21403197ebdbSTheodore Ts'o 
21413197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
21423197ebdbSTheodore Ts'o 		return -EINVAL;
21433197ebdbSTheodore Ts'o 
2144f7c43950STheodore Ts'o 	if (!is_power_of_2(t))
21453197ebdbSTheodore Ts'o 		return -EINVAL;
21463197ebdbSTheodore Ts'o 
21473197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
21483197ebdbSTheodore Ts'o 	return count;
21493197ebdbSTheodore Ts'o }
21503197ebdbSTheodore Ts'o 
21513197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
21523197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
21533197ebdbSTheodore Ts'o {
21543197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
21553197ebdbSTheodore Ts'o 
21563197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
21573197ebdbSTheodore Ts'o }
21583197ebdbSTheodore Ts'o 
21593197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
21603197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
21613197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
21623197ebdbSTheodore Ts'o {
21633197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
21643197ebdbSTheodore Ts'o 	unsigned long t;
21653197ebdbSTheodore Ts'o 
21663197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
21673197ebdbSTheodore Ts'o 		return -EINVAL;
21683197ebdbSTheodore Ts'o 	*ui = t;
21693197ebdbSTheodore Ts'o 	return count;
21703197ebdbSTheodore Ts'o }
21713197ebdbSTheodore Ts'o 
21723197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
21733197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
21743197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
21753197ebdbSTheodore Ts'o 	.show	= _show,					\
21763197ebdbSTheodore Ts'o 	.store	= _store,					\
21773197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
21783197ebdbSTheodore Ts'o }
21793197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
21803197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
21813197ebdbSTheodore Ts'o 
21823197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
21833197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
21843197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
21853197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
21863197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
21873197ebdbSTheodore Ts'o 
21883197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
21893197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
21903197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
21913197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
21923197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
219311013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
21943197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
21953197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
21963197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
21973197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
21983197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
21993197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2200*55138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
22013197ebdbSTheodore Ts'o 
22023197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
22033197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
22043197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
22053197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
22063197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
220711013911SAndreas Dilger 	ATTR_LIST(inode_goal),
22083197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
22093197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
22103197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
22113197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
22123197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
22133197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
2214*55138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
22153197ebdbSTheodore Ts'o 	NULL,
22163197ebdbSTheodore Ts'o };
22173197ebdbSTheodore Ts'o 
22183197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
22193197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
22203197ebdbSTheodore Ts'o {
22213197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22223197ebdbSTheodore Ts'o 						s_kobj);
22233197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
22243197ebdbSTheodore Ts'o 
22253197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
22263197ebdbSTheodore Ts'o }
22273197ebdbSTheodore Ts'o 
22283197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
22293197ebdbSTheodore Ts'o 			       struct attribute *attr,
22303197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
22313197ebdbSTheodore Ts'o {
22323197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22333197ebdbSTheodore Ts'o 						s_kobj);
22343197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
22353197ebdbSTheodore Ts'o 
22363197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
22373197ebdbSTheodore Ts'o }
22383197ebdbSTheodore Ts'o 
22393197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
22403197ebdbSTheodore Ts'o {
22413197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
22423197ebdbSTheodore Ts'o 						s_kobj);
22433197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
22443197ebdbSTheodore Ts'o }
22453197ebdbSTheodore Ts'o 
22463197ebdbSTheodore Ts'o 
22473197ebdbSTheodore Ts'o static struct sysfs_ops ext4_attr_ops = {
22483197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
22493197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
22503197ebdbSTheodore Ts'o };
22513197ebdbSTheodore Ts'o 
22523197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
22533197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
22543197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
22553197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
22563197ebdbSTheodore Ts'o };
22573197ebdbSTheodore Ts'o 
2258a13fb1a4SEric Sandeen /*
2259a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2260a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2261a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2262a13fb1a4SEric Sandeen  * 0 if it cannot be.
2263a13fb1a4SEric Sandeen  */
2264a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2265a13fb1a4SEric Sandeen {
2266a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2267a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2268a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2269a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2270a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2271a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2272a13fb1a4SEric Sandeen 		return 0;
2273a13fb1a4SEric Sandeen 	}
2274a13fb1a4SEric Sandeen 
2275a13fb1a4SEric Sandeen 	if (readonly)
2276a13fb1a4SEric Sandeen 		return 1;
2277a13fb1a4SEric Sandeen 
2278a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2279a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2280a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2281a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2282a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2283a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2284a13fb1a4SEric Sandeen 		return 0;
2285a13fb1a4SEric Sandeen 	}
2286a13fb1a4SEric Sandeen 	/*
2287a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2288a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2289a13fb1a4SEric Sandeen 	 */
2290a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2291a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2292a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2293a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2294a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2295a13fb1a4SEric Sandeen 			return 0;
2296a13fb1a4SEric Sandeen 		}
2297a13fb1a4SEric Sandeen 	}
2298a13fb1a4SEric Sandeen 	return 1;
2299a13fb1a4SEric Sandeen }
2300a13fb1a4SEric Sandeen 
2301617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
23027477827fSAneesh Kumar K.V 				__releases(kernel_lock)
23037477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
2304ac27a0ecSDave Kleikamp {
2305ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2306617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
2307617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2308617ba13bSMingming Cao 	ext4_fsblk_t block;
2309617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
231070bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
2311ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
2312ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
2313ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
2314ac27a0ecSDave Kleikamp 	struct inode *root;
23159f6200bbSTheodore Ts'o 	char *cp;
23160390131bSFrank Mayhar 	const char *descr;
23171d1fe1eeSDavid Howells 	int ret = -EINVAL;
2318ac27a0ecSDave Kleikamp 	int blocksize;
23194ec11028STheodore Ts'o 	unsigned int db_count;
23204ec11028STheodore Ts'o 	unsigned int i;
2321f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
2322bd81d8eeSLaurent Vivier 	__u64 blocks_count;
2323833f4077SPeter Zijlstra 	int err;
2324b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
2325ac27a0ecSDave Kleikamp 
2326ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2327ac27a0ecSDave Kleikamp 	if (!sbi)
2328ac27a0ecSDave Kleikamp 		return -ENOMEM;
2329705895b6SPekka Enberg 
2330705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
2331705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2332705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
2333705895b6SPekka Enberg 		kfree(sbi);
2334705895b6SPekka Enberg 		return -ENOMEM;
2335705895b6SPekka Enberg 	}
2336ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
2337ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
2338617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
2339617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
2340240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2341d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
2342afc32f7eSTheodore Ts'o 	sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2343afc32f7eSTheodore Ts'o 						      sectors[1]);
2344ac27a0ecSDave Kleikamp 
2345ac27a0ecSDave Kleikamp 	unlock_kernel();
2346ac27a0ecSDave Kleikamp 
23479f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
23489f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
23499f6200bbSTheodore Ts'o 		*cp = '!';
23509f6200bbSTheodore Ts'o 
2351617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2352ac27a0ecSDave Kleikamp 	if (!blocksize) {
2353b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
2354ac27a0ecSDave Kleikamp 		goto out_fail;
2355ac27a0ecSDave Kleikamp 	}
2356ac27a0ecSDave Kleikamp 
2357ac27a0ecSDave Kleikamp 	/*
2358617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
2359ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
2360ac27a0ecSDave Kleikamp 	 */
2361617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
236270bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
236370bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
2364ac27a0ecSDave Kleikamp 	} else {
236570bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
2366ac27a0ecSDave Kleikamp 	}
2367ac27a0ecSDave Kleikamp 
236870bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
2369b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
2370ac27a0ecSDave Kleikamp 		goto out_fail;
2371ac27a0ecSDave Kleikamp 	}
2372ac27a0ecSDave Kleikamp 	/*
2373ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
2374617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
2375ac27a0ecSDave Kleikamp 	 */
2376617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2377ac27a0ecSDave Kleikamp 	sbi->s_es = es;
2378ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
2379617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
2380617ba13bSMingming Cao 		goto cantfind_ext4;
2381afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
2382ac27a0ecSDave Kleikamp 
2383ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
2384ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2385617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
2386ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
2387617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2388ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
2389617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
2390ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
239103010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
2392617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2393ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
23942e7842b8SHugh Dickins #endif
239503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
2396617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
2397ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
23982e7842b8SHugh Dickins #endif
2399617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2400617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2401617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2402617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2403617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2404617ba13bSMingming Cao 		sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2405ac27a0ecSDave Kleikamp 
2406617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2407ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2408bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2409ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
2410bb4f397aSAneesh Kumar K.V 	else
2411bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
2412ac27a0ecSDave Kleikamp 
2413ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2414ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
241530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
241630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
241730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2418f4033903SCurt Wohlgemuth 	sbi->s_mb_history_max = default_mb_history_length;
2419ac27a0ecSDave Kleikamp 
2420571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
2421ac27a0ecSDave Kleikamp 
24221e2462f9SMingming Cao 	/*
2423dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
2424dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
2425dd919b98SAneesh Kumar K.V 	 */
2426dd919b98SAneesh Kumar K.V 	set_opt(sbi->s_mount_opt, DELALLOC);
2427dd919b98SAneesh Kumar K.V 
2428b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
2429b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
2430ac27a0ecSDave Kleikamp 		goto failed_mount;
2431ac27a0ecSDave Kleikamp 
2432ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2433617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2434ac27a0ecSDave Kleikamp 
2435617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2436617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2437617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2438617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2439b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2440b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
2441b31e1552SEric Sandeen 		       "running e2fsck is recommended");
2442469108ffSTheodore Tso 
2443469108ffSTheodore Tso 	/*
2444ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2445ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2446ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2447ac27a0ecSDave Kleikamp 	 */
2448a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
2449ac27a0ecSDave Kleikamp 		goto failed_mount;
2450a13fb1a4SEric Sandeen 
2451ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2452ac27a0ecSDave Kleikamp 
2453617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2454617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2455b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2456b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
2457ac27a0ecSDave Kleikamp 		goto failed_mount;
2458ac27a0ecSDave Kleikamp 	}
2459ac27a0ecSDave Kleikamp 
2460ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2461ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2462ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2463b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
2464ce40733cSAneesh Kumar K.V 					blocksize);
2465ac27a0ecSDave Kleikamp 			goto failed_mount;
2466ac27a0ecSDave Kleikamp 		}
2467ac27a0ecSDave Kleikamp 
2468ac27a0ecSDave Kleikamp 		brelse(bh);
246970bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
247070bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
247170bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2472ac27a0ecSDave Kleikamp 		if (!bh) {
2473b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2474b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
2475ac27a0ecSDave Kleikamp 			goto failed_mount;
2476ac27a0ecSDave Kleikamp 		}
2477617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2478ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2479617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2480b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2481b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
2482ac27a0ecSDave Kleikamp 			goto failed_mount;
2483ac27a0ecSDave Kleikamp 		}
2484ac27a0ecSDave Kleikamp 	}
2485ac27a0ecSDave Kleikamp 
2486a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2487a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2488f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2489f287a1a5STheodore Ts'o 						      has_huge_files);
2490f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2491ac27a0ecSDave Kleikamp 
2492617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2493617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2494617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2495ac27a0ecSDave Kleikamp 	} else {
2496ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2497ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2498617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
24991330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2500ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2501b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2502b31e1552SEric Sandeen 			       "unsupported inode size: %d",
2503ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2504ac27a0ecSDave Kleikamp 			goto failed_mount;
2505ac27a0ecSDave Kleikamp 		}
2506ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2507ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2508ac27a0ecSDave Kleikamp 	}
25090b8e58a1SAndreas Dilger 
25100d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
25110d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
25128fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
25130d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2514d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
2515b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2516b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
25170d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
25180d1ee42fSAlexandre Ratchov 			goto failed_mount;
25190d1ee42fSAlexandre Ratchov 		}
25200d1ee42fSAlexandre Ratchov 	} else
25210d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
25220b8e58a1SAndreas Dilger 
2523ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2524ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2525b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2526617ba13bSMingming Cao 		goto cantfind_ext4;
25270b8e58a1SAndreas Dilger 
2528617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2529ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2530617ba13bSMingming Cao 		goto cantfind_ext4;
2531ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2532ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
25330d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2534ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2535ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2536e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2537e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
25380b8e58a1SAndreas Dilger 
2539ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2540ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2541ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2542f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
2543f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
2544f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2545f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2546f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
2547f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2548f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2549f99b2589STheodore Ts'o #else
2550f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2551f99b2589STheodore Ts'o #endif
2552f99b2589STheodore Ts'o 		sb->s_dirt = 1;
2553f99b2589STheodore Ts'o 	}
2554ac27a0ecSDave Kleikamp 
2555ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2556b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2557b31e1552SEric Sandeen 		       "#blocks per group too big: %lu",
2558ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2559ac27a0ecSDave Kleikamp 		goto failed_mount;
2560ac27a0ecSDave Kleikamp 	}
2561ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2562b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2563b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
2564ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2565ac27a0ecSDave Kleikamp 		goto failed_mount;
2566ac27a0ecSDave Kleikamp 	}
2567ac27a0ecSDave Kleikamp 
2568bf43d84bSEric Sandeen 	/*
2569bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
2570bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
2571bf43d84bSEric Sandeen 	 */
2572bf43d84bSEric Sandeen 	if ((ext4_blocks_count(es) >
2573bf43d84bSEric Sandeen 	     (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2574bf43d84bSEric Sandeen 	    (ext4_blocks_count(es) >
2575bf43d84bSEric Sandeen 	     (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
2576b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
2577bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
2578ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
257990c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
2580bf43d84bSEric Sandeen 		ret = -EFBIG;
2581ac27a0ecSDave Kleikamp 		goto failed_mount;
2582ac27a0ecSDave Kleikamp 	}
2583ac27a0ecSDave Kleikamp 
2584617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2585617ba13bSMingming Cao 		goto cantfind_ext4;
2586e7c95593SEric Sandeen 
25870f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
25880f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
25890f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2590b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2591b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
25920f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
25930f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
25940f2ddca6SFrom: Thiemo Nagel 	}
25950f2ddca6SFrom: Thiemo Nagel 
25964ec11028STheodore Ts'o 	/*
25974ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
25984ec11028STheodore Ts'o 	 * of the filesystem.
25994ec11028STheodore Ts'o 	 */
26004ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2601b31e1552SEric Sandeen                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2602b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
2603e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
26044ec11028STheodore Ts'o 			 ext4_blocks_count(es));
2605e7c95593SEric Sandeen 		goto failed_mount;
2606e7c95593SEric Sandeen 	}
2607bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2608bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2609bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2610bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
26114ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2612b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
26134ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
2614b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
26154ec11028STheodore Ts'o 		       ext4_blocks_count(es),
26164ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
26174ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
26184ec11028STheodore Ts'o 		goto failed_mount;
26194ec11028STheodore Ts'o 	}
2620bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2621fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2622fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
2623617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2624617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2625ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2626ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2627ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2628b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
2629ac27a0ecSDave Kleikamp 		goto failed_mount;
2630ac27a0ecSDave Kleikamp 	}
2631ac27a0ecSDave Kleikamp 
26323244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
26339f6200bbSTheodore Ts'o 	if (ext4_proc_root)
26349f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
26353244fcb1SAlexander Beregalov #endif
2636240799cdSTheodore Ts'o 
2637705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
2638ac27a0ecSDave Kleikamp 
2639ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
264070bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2641ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2642ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2643b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2644b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
2645ac27a0ecSDave Kleikamp 			db_count = i;
2646ac27a0ecSDave Kleikamp 			goto failed_mount2;
2647ac27a0ecSDave Kleikamp 		}
2648ac27a0ecSDave Kleikamp 	}
2649617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2650b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
2651ac27a0ecSDave Kleikamp 		goto failed_mount2;
2652ac27a0ecSDave Kleikamp 	}
2653772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2654772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2655b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2656b31e1552SEric Sandeen 			       "unable to initialize "
2657b31e1552SEric Sandeen 			       "flex_bg meta info!");
2658772cb7c8SJose R. Santos 			goto failed_mount2;
2659772cb7c8SJose R. Santos 		}
2660772cb7c8SJose R. Santos 
2661ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2662ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2663ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2664ac27a0ecSDave Kleikamp 
2665833f4077SPeter Zijlstra 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
2666617ba13bSMingming Cao 			ext4_count_free_blocks(sb));
2667833f4077SPeter Zijlstra 	if (!err) {
2668833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
2669617ba13bSMingming Cao 				ext4_count_free_inodes(sb));
2670833f4077SPeter Zijlstra 	}
2671833f4077SPeter Zijlstra 	if (!err) {
2672833f4077SPeter Zijlstra 		err = percpu_counter_init(&sbi->s_dirs_counter,
2673617ba13bSMingming Cao 				ext4_count_dirs(sb));
2674833f4077SPeter Zijlstra 	}
26756bc6e63fSAneesh Kumar K.V 	if (!err) {
26766bc6e63fSAneesh Kumar K.V 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
26776bc6e63fSAneesh Kumar K.V 	}
2678833f4077SPeter Zijlstra 	if (err) {
2679b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "insufficient memory");
2680833f4077SPeter Zijlstra 		goto failed_mount3;
2681833f4077SPeter Zijlstra 	}
2682ac27a0ecSDave Kleikamp 
2683c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
2684*55138e0bSTheodore Ts'o 	sbi->s_max_writeback_mb_bump = 128;
2685c9de560dSAlex Tomas 
2686ac27a0ecSDave Kleikamp 	/*
2687ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2688ac27a0ecSDave Kleikamp 	 */
26899ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
26909ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2691617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
26929ca92389STheodore Ts'o 	else
26939ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
2694617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2695617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2696ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2697617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2698617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2699ac27a0ecSDave Kleikamp #endif
2700ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
27013b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
270232ed5058STheodore Ts'o 	mutex_init(&sbi->s_resize_lock);
2703ac27a0ecSDave Kleikamp 
2704ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2705ac27a0ecSDave Kleikamp 
2706ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2707617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2708617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2709ac27a0ecSDave Kleikamp 
2710ac27a0ecSDave Kleikamp 	/*
2711ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2712ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2713ac27a0ecSDave Kleikamp 	 */
2714ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2715617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2716617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2717ac27a0ecSDave Kleikamp 			goto failed_mount3;
2718624080edSTheodore Ts'o 		if (!(sb->s_flags & MS_RDONLY) &&
2719624080edSTheodore Ts'o 		    EXT4_SB(sb)->s_journal->j_failed_commit) {
2720b31e1552SEric Sandeen 			ext4_msg(sb, KERN_CRIT, "error: "
2721624080edSTheodore Ts'o 			       "ext4_fill_super: Journal transaction "
2722b31e1552SEric Sandeen 			       "%u is corrupt",
2723624080edSTheodore Ts'o 			       EXT4_SB(sb)->s_journal->j_failed_commit);
2724624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_RO)) {
2725b31e1552SEric Sandeen 				ext4_msg(sb, KERN_CRIT,
2726b31e1552SEric Sandeen 				       "Mounting filesystem read-only");
2727624080edSTheodore Ts'o 				sb->s_flags |= MS_RDONLY;
2728624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2729624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2730624080edSTheodore Ts'o 			}
2731624080edSTheodore Ts'o 			if (test_opt(sb, ERRORS_PANIC)) {
2732624080edSTheodore Ts'o 				EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2733624080edSTheodore Ts'o 				es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2734e2d67052STheodore Ts'o 				ext4_commit_super(sb, 1);
2735624080edSTheodore Ts'o 				goto failed_mount4;
2736624080edSTheodore Ts'o 			}
2737624080edSTheodore Ts'o 		}
27380390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
27390390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2740b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
2741b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
27420390131bSFrank Mayhar 		goto failed_mount4;
2743ac27a0ecSDave Kleikamp 	} else {
27440390131bSFrank Mayhar 		clear_opt(sbi->s_mount_opt, DATA_FLAGS);
27450390131bSFrank Mayhar 		set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
27460390131bSFrank Mayhar 		sbi->s_journal = NULL;
27470390131bSFrank Mayhar 		needs_recovery = 0;
27480390131bSFrank Mayhar 		goto no_journal;
2749ac27a0ecSDave Kleikamp 	}
2750ac27a0ecSDave Kleikamp 
2751eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2752eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2753eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2754b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
2755eb40a09cSJose R. Santos 		goto failed_mount4;
2756eb40a09cSJose R. Santos 	}
2757eb40a09cSJose R. Santos 
2758818d276cSGirish Shilamkar 	jbd2_journal_set_features(sbi->s_journal,
2759818d276cSGirish Shilamkar 				  JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2760d0646f7bSTheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
2761d0646f7bSTheodore Ts'o 		jbd2_journal_set_features(sbi->s_journal, 0, 0,
2762d0646f7bSTheodore Ts'o 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2763d0646f7bSTheodore Ts'o 	else
2764818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2765818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2766818d276cSGirish Shilamkar 
2767ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2768ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2769ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2770ac27a0ecSDave Kleikamp 	case 0:
2771ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
277263f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
277363f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
277463f57933SAndrew Morton 		 */
2775dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2776dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2777ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2778ac27a0ecSDave Kleikamp 		else
2779ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2780ac27a0ecSDave Kleikamp 		break;
2781ac27a0ecSDave Kleikamp 
2782617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2783617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2784dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2785dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2786b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
2787b31e1552SEric Sandeen 			       "requested data journaling mode");
2788ac27a0ecSDave Kleikamp 			goto failed_mount4;
2789ac27a0ecSDave Kleikamp 		}
2790ac27a0ecSDave Kleikamp 	default:
2791ac27a0ecSDave Kleikamp 		break;
2792ac27a0ecSDave Kleikamp 	}
2793b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
2794ac27a0ecSDave Kleikamp 
27950390131bSFrank Mayhar no_journal:
2796ac27a0ecSDave Kleikamp 
2797ac27a0ecSDave Kleikamp 	if (test_opt(sb, NOBH)) {
2798617ba13bSMingming Cao 		if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2799b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2800b31e1552SEric Sandeen 				"its supported only with writeback mode");
2801ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, NOBH);
2802ac27a0ecSDave Kleikamp 		}
2803ac27a0ecSDave Kleikamp 	}
2804ac27a0ecSDave Kleikamp 	/*
2805dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2806ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2807ac27a0ecSDave Kleikamp 	 */
2808ac27a0ecSDave Kleikamp 
28091d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
28101d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2811b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
28121d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2813ac27a0ecSDave Kleikamp 		goto failed_mount4;
2814ac27a0ecSDave Kleikamp 	}
2815ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
28161d1fe1eeSDavid Howells 		iput(root);
2817b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
2818ac27a0ecSDave Kleikamp 		goto failed_mount4;
2819ac27a0ecSDave Kleikamp 	}
28201d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
28211d1fe1eeSDavid Howells 	if (!sb->s_root) {
2822b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
28231d1fe1eeSDavid Howells 		iput(root);
28241d1fe1eeSDavid Howells 		ret = -ENOMEM;
28251d1fe1eeSDavid Howells 		goto failed_mount4;
28261d1fe1eeSDavid Howells 	}
2827ac27a0ecSDave Kleikamp 
2828617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2829ef7f3835SKalpak Shah 
2830ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2831ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2832ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2833ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2834ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2835ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2836ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2837ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2838ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2839ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2840ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2841ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2842ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2843ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2844ef7f3835SKalpak Shah 		}
2845ef7f3835SKalpak Shah 	}
2846ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2847ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2848ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2849ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2850ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2851b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
2852b31e1552SEric Sandeen 			 "available");
2853ef7f3835SKalpak Shah 	}
2854ef7f3835SKalpak Shah 
2855c2774d84SAneesh Kumar K.V 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2856b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2857b31e1552SEric Sandeen 			 "requested data journaling mode");
2858c2774d84SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
2859c2774d84SAneesh Kumar K.V 	} else if (test_opt(sb, DELALLOC))
2860b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "delayed allocation enabled");
2861c2774d84SAneesh Kumar K.V 
28626fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
28636fd058f7STheodore Ts'o 	if (err) {
2864b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
28656fd058f7STheodore Ts'o 			 "zone (%d)\n", err);
28666fd058f7STheodore Ts'o 		goto failed_mount4;
28676fd058f7STheodore Ts'o 	}
28686fd058f7STheodore Ts'o 
2869c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
2870c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
2871c2774d84SAneesh Kumar K.V 	if (err) {
2872b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2873c2774d84SAneesh Kumar K.V 			 err);
2874c2774d84SAneesh Kumar K.V 		goto failed_mount4;
2875c2774d84SAneesh Kumar K.V 	}
2876c2774d84SAneesh Kumar K.V 
28773197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
28783197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
28793197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
28803197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
28813197ebdbSTheodore Ts'o 	if (err) {
28823197ebdbSTheodore Ts'o 		ext4_mb_release(sb);
28833197ebdbSTheodore Ts'o 		ext4_ext_release(sb);
28843197ebdbSTheodore Ts'o 		goto failed_mount4;
28853197ebdbSTheodore Ts'o 	};
28863197ebdbSTheodore Ts'o 
2887617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2888617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
2889617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
28900390131bSFrank Mayhar 	if (needs_recovery) {
2891b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
2892617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
28930390131bSFrank Mayhar 	}
28940390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
28950390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
28960390131bSFrank Mayhar 			descr = " journalled data mode";
28970390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
28980390131bSFrank Mayhar 			descr = " ordered data mode";
28990390131bSFrank Mayhar 		else
29000390131bSFrank Mayhar 			descr = " writeback data mode";
29010390131bSFrank Mayhar 	} else
29020390131bSFrank Mayhar 		descr = "out journal";
29030390131bSFrank Mayhar 
2904b31e1552SEric Sandeen 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
2905ac27a0ecSDave Kleikamp 
2906ac27a0ecSDave Kleikamp 	lock_kernel();
2907ac27a0ecSDave Kleikamp 	return 0;
2908ac27a0ecSDave Kleikamp 
2909617ba13bSMingming Cao cantfind_ext4:
2910ac27a0ecSDave Kleikamp 	if (!silent)
2911b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
2912ac27a0ecSDave Kleikamp 	goto failed_mount;
2913ac27a0ecSDave Kleikamp 
2914ac27a0ecSDave Kleikamp failed_mount4:
2915b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
29166fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
29170390131bSFrank Mayhar 	if (sbi->s_journal) {
2918dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
291947b4a50bSJan Kara 		sbi->s_journal = NULL;
29200390131bSFrank Mayhar 	}
2921ac27a0ecSDave Kleikamp failed_mount3:
2922c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups) {
2923c5ca7c76STheodore Ts'o 		if (is_vmalloc_addr(sbi->s_flex_groups))
2924c5ca7c76STheodore Ts'o 			vfree(sbi->s_flex_groups);
2925c5ca7c76STheodore Ts'o 		else
2926c5ca7c76STheodore Ts'o 			kfree(sbi->s_flex_groups);
2927c5ca7c76STheodore Ts'o 	}
2928ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
2929ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
2930ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
29316bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2932ac27a0ecSDave Kleikamp failed_mount2:
2933ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
2934ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
2935ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
2936ac27a0ecSDave Kleikamp failed_mount:
2937240799cdSTheodore Ts'o 	if (sbi->s_proc) {
29389f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
2939240799cdSTheodore Ts'o 	}
2940ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2941ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
2942ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
2943ac27a0ecSDave Kleikamp #endif
2944617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
2945ac27a0ecSDave Kleikamp 	brelse(bh);
2946ac27a0ecSDave Kleikamp out_fail:
2947ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
2948f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
2949ac27a0ecSDave Kleikamp 	kfree(sbi);
2950ac27a0ecSDave Kleikamp 	lock_kernel();
29511d1fe1eeSDavid Howells 	return ret;
2952ac27a0ecSDave Kleikamp }
2953ac27a0ecSDave Kleikamp 
2954ac27a0ecSDave Kleikamp /*
2955ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
2956ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
2957ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
2958ac27a0ecSDave Kleikamp  */
2959617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2960ac27a0ecSDave Kleikamp {
2961617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2962ac27a0ecSDave Kleikamp 
2963ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
296430773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
296530773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
2966ac27a0ecSDave Kleikamp 
2967ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
2968ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
2969dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
2970ac27a0ecSDave Kleikamp 	else
2971dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
29725bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
29735bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
29745bf5683aSHidehiro Kawai 	else
29755bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2976ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
2977ac27a0ecSDave Kleikamp }
2978ac27a0ecSDave Kleikamp 
2979617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
2980ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
2981ac27a0ecSDave Kleikamp {
2982ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
2983ac27a0ecSDave Kleikamp 	journal_t *journal;
2984ac27a0ecSDave Kleikamp 
29850390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
29860390131bSFrank Mayhar 
2987ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
2988ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
2989ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
2990ac27a0ecSDave Kleikamp 
29911d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
29921d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
2993b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
2994ac27a0ecSDave Kleikamp 		return NULL;
2995ac27a0ecSDave Kleikamp 	}
2996ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
2997ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
2998ac27a0ecSDave Kleikamp 		iput(journal_inode);
2999b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3000ac27a0ecSDave Kleikamp 		return NULL;
3001ac27a0ecSDave Kleikamp 	}
3002ac27a0ecSDave Kleikamp 
3003e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3004ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
30051d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
3006b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
3007ac27a0ecSDave Kleikamp 		iput(journal_inode);
3008ac27a0ecSDave Kleikamp 		return NULL;
3009ac27a0ecSDave Kleikamp 	}
3010ac27a0ecSDave Kleikamp 
3011dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
3012ac27a0ecSDave Kleikamp 	if (!journal) {
3013b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3014ac27a0ecSDave Kleikamp 		iput(journal_inode);
3015ac27a0ecSDave Kleikamp 		return NULL;
3016ac27a0ecSDave Kleikamp 	}
3017ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3018617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3019ac27a0ecSDave Kleikamp 	return journal;
3020ac27a0ecSDave Kleikamp }
3021ac27a0ecSDave Kleikamp 
3022617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
3023ac27a0ecSDave Kleikamp 				       dev_t j_dev)
3024ac27a0ecSDave Kleikamp {
3025ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3026ac27a0ecSDave Kleikamp 	journal_t *journal;
3027617ba13bSMingming Cao 	ext4_fsblk_t start;
3028617ba13bSMingming Cao 	ext4_fsblk_t len;
3029ac27a0ecSDave Kleikamp 	int hblock, blocksize;
3030617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
3031ac27a0ecSDave Kleikamp 	unsigned long offset;
3032617ba13bSMingming Cao 	struct ext4_super_block *es;
3033ac27a0ecSDave Kleikamp 	struct block_device *bdev;
3034ac27a0ecSDave Kleikamp 
30350390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
30360390131bSFrank Mayhar 
3037b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
3038ac27a0ecSDave Kleikamp 	if (bdev == NULL)
3039ac27a0ecSDave Kleikamp 		return NULL;
3040ac27a0ecSDave Kleikamp 
3041ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
3042b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3043b31e1552SEric Sandeen 			"failed to claim external journal device");
30449a1c3542SAl Viro 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
3045ac27a0ecSDave Kleikamp 		return NULL;
3046ac27a0ecSDave Kleikamp 	}
3047ac27a0ecSDave Kleikamp 
3048ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
3049e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
3050ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
3051b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3052b31e1552SEric Sandeen 			"blocksize too small for journal device");
3053ac27a0ecSDave Kleikamp 		goto out_bdev;
3054ac27a0ecSDave Kleikamp 	}
3055ac27a0ecSDave Kleikamp 
3056617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3057617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3058ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3059ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3060b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3061b31e1552SEric Sandeen 		       "external journal");
3062ac27a0ecSDave Kleikamp 		goto out_bdev;
3063ac27a0ecSDave Kleikamp 	}
3064ac27a0ecSDave Kleikamp 
3065617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3066617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3067ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3068617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3069b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
3070b31e1552SEric Sandeen 					"bad superblock");
3071ac27a0ecSDave Kleikamp 		brelse(bh);
3072ac27a0ecSDave Kleikamp 		goto out_bdev;
3073ac27a0ecSDave Kleikamp 	}
3074ac27a0ecSDave Kleikamp 
3075617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3076b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3077ac27a0ecSDave Kleikamp 		brelse(bh);
3078ac27a0ecSDave Kleikamp 		goto out_bdev;
3079ac27a0ecSDave Kleikamp 	}
3080ac27a0ecSDave Kleikamp 
3081bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
3082ac27a0ecSDave Kleikamp 	start = sb_block + 1;
3083ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
3084ac27a0ecSDave Kleikamp 
3085dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3086ac27a0ecSDave Kleikamp 					start, len, blocksize);
3087ac27a0ecSDave Kleikamp 	if (!journal) {
3088b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
3089ac27a0ecSDave Kleikamp 		goto out_bdev;
3090ac27a0ecSDave Kleikamp 	}
3091ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3092ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
3093ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
3094ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
3095b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3096ac27a0ecSDave Kleikamp 		goto out_journal;
3097ac27a0ecSDave Kleikamp 	}
3098ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3099b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
3100b31e1552SEric Sandeen 					"user (unsupported) - %d",
3101ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
3102ac27a0ecSDave Kleikamp 		goto out_journal;
3103ac27a0ecSDave Kleikamp 	}
3104617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
3105617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3106ac27a0ecSDave Kleikamp 	return journal;
31070b8e58a1SAndreas Dilger 
3108ac27a0ecSDave Kleikamp out_journal:
3109dab291afSMingming Cao 	jbd2_journal_destroy(journal);
3110ac27a0ecSDave Kleikamp out_bdev:
3111617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
3112ac27a0ecSDave Kleikamp 	return NULL;
3113ac27a0ecSDave Kleikamp }
3114ac27a0ecSDave Kleikamp 
3115617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
3116617ba13bSMingming Cao 			     struct ext4_super_block *es,
3117ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
3118ac27a0ecSDave Kleikamp {
3119ac27a0ecSDave Kleikamp 	journal_t *journal;
3120ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3121ac27a0ecSDave Kleikamp 	dev_t journal_dev;
3122ac27a0ecSDave Kleikamp 	int err = 0;
3123ac27a0ecSDave Kleikamp 	int really_read_only;
3124ac27a0ecSDave Kleikamp 
31250390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
31260390131bSFrank Mayhar 
3127ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3128ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3129b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3130b31e1552SEric Sandeen 			"numbers have changed");
3131ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
3132ac27a0ecSDave Kleikamp 	} else
3133ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3134ac27a0ecSDave Kleikamp 
3135ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
3136ac27a0ecSDave Kleikamp 
3137ac27a0ecSDave Kleikamp 	/*
3138ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
3139ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
3140ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
3141ac27a0ecSDave Kleikamp 	 */
3142617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3143ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
3144b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
3145b31e1552SEric Sandeen 					"required on readonly filesystem");
3146ac27a0ecSDave Kleikamp 			if (really_read_only) {
3147b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
3148b31e1552SEric Sandeen 					"unavailable, cannot proceed");
3149ac27a0ecSDave Kleikamp 				return -EROFS;
3150ac27a0ecSDave Kleikamp 			}
3151b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
3152b31e1552SEric Sandeen 			       "be enabled during recovery");
3153ac27a0ecSDave Kleikamp 		}
3154ac27a0ecSDave Kleikamp 	}
3155ac27a0ecSDave Kleikamp 
3156ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
3157b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3158b31e1552SEric Sandeen 		       "and inode journals!");
3159ac27a0ecSDave Kleikamp 		return -EINVAL;
3160ac27a0ecSDave Kleikamp 	}
3161ac27a0ecSDave Kleikamp 
3162ac27a0ecSDave Kleikamp 	if (journal_inum) {
3163617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
3164ac27a0ecSDave Kleikamp 			return -EINVAL;
3165ac27a0ecSDave Kleikamp 	} else {
3166617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3167ac27a0ecSDave Kleikamp 			return -EINVAL;
3168ac27a0ecSDave Kleikamp 	}
3169ac27a0ecSDave Kleikamp 
31704776004fSTheodore Ts'o 	if (journal->j_flags & JBD2_BARRIER)
3171b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers enabled");
31724776004fSTheodore Ts'o 	else
3173b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
31744776004fSTheodore Ts'o 
3175ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3176dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
3177ac27a0ecSDave Kleikamp 		if (err)  {
3178b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error updating journal");
3179dab291afSMingming Cao 			jbd2_journal_destroy(journal);
3180ac27a0ecSDave Kleikamp 			return err;
3181ac27a0ecSDave Kleikamp 		}
3182ac27a0ecSDave Kleikamp 	}
3183ac27a0ecSDave Kleikamp 
3184617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3185dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
3186ac27a0ecSDave Kleikamp 	if (!err)
3187dab291afSMingming Cao 		err = jbd2_journal_load(journal);
3188ac27a0ecSDave Kleikamp 
3189ac27a0ecSDave Kleikamp 	if (err) {
3190b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
3191dab291afSMingming Cao 		jbd2_journal_destroy(journal);
3192ac27a0ecSDave Kleikamp 		return err;
3193ac27a0ecSDave Kleikamp 	}
3194ac27a0ecSDave Kleikamp 
3195617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
3196617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
3197ac27a0ecSDave Kleikamp 
3198ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3199ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3200ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
3201ac27a0ecSDave Kleikamp 
3202ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
3203e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3204ac27a0ecSDave Kleikamp 	}
3205ac27a0ecSDave Kleikamp 
3206ac27a0ecSDave Kleikamp 	return 0;
3207ac27a0ecSDave Kleikamp }
3208ac27a0ecSDave Kleikamp 
3209e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
3210ac27a0ecSDave Kleikamp {
3211e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3212617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3213c4be0c1dSTakashi Sato 	int error = 0;
3214ac27a0ecSDave Kleikamp 
3215ac27a0ecSDave Kleikamp 	if (!sbh)
3216c4be0c1dSTakashi Sato 		return error;
3217914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
3218914258bfSTheodore Ts'o 		/*
3219914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
3220914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
3221914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
3222914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
3223914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
3224914258bfSTheodore Ts'o 		 * write and hope for the best.
3225914258bfSTheodore Ts'o 		 */
3226b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
3227b31e1552SEric Sandeen 		       "superblock detected");
3228914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
3229914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
3230914258bfSTheodore Ts'o 	}
323171290b36STheodore Ts'o 	/*
323271290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
323371290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
323471290b36STheodore Ts'o 	 * write time when we are mounting the root file system
323571290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
323671290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
323771290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
323871290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
323971290b36STheodore Ts'o 	 * to complain and force a full file system check.
324071290b36STheodore Ts'o 	 */
324171290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
3242ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
3243afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
3244afc32f7eSTheodore Ts'o 		cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3245afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3246afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
32475d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
32485d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeblocks_counter));
32495d1b1b3fSAneesh Kumar K.V 	es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
32505d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeinodes_counter));
32517234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
3252ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
3253ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
3254914258bfSTheodore Ts'o 	if (sync) {
3255c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
3256c4be0c1dSTakashi Sato 		if (error)
3257c4be0c1dSTakashi Sato 			return error;
3258c4be0c1dSTakashi Sato 
3259c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
3260c4be0c1dSTakashi Sato 		if (error) {
3261b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
3262b31e1552SEric Sandeen 			       "superblock");
3263914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
3264914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
3265914258bfSTheodore Ts'o 		}
3266914258bfSTheodore Ts'o 	}
3267c4be0c1dSTakashi Sato 	return error;
3268ac27a0ecSDave Kleikamp }
3269ac27a0ecSDave Kleikamp 
3270ac27a0ecSDave Kleikamp /*
3271ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
3272ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
3273ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
3274ac27a0ecSDave Kleikamp  */
3275617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
3276617ba13bSMingming Cao 					struct ext4_super_block *es)
3277ac27a0ecSDave Kleikamp {
3278617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
3279ac27a0ecSDave Kleikamp 
32800390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
32810390131bSFrank Mayhar 		BUG_ON(journal != NULL);
32820390131bSFrank Mayhar 		return;
32830390131bSFrank Mayhar 	}
3284dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
32857ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
32867ffe1ea8SHidehiro Kawai 		goto out;
32877ffe1ea8SHidehiro Kawai 
3288617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3289ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
3290617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3291e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3292ac27a0ecSDave Kleikamp 	}
32937ffe1ea8SHidehiro Kawai 
32947ffe1ea8SHidehiro Kawai out:
3295dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
3296ac27a0ecSDave Kleikamp }
3297ac27a0ecSDave Kleikamp 
3298ac27a0ecSDave Kleikamp /*
3299ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
3300ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
3301ac27a0ecSDave Kleikamp  * main filesystem now.
3302ac27a0ecSDave Kleikamp  */
3303617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
3304617ba13bSMingming Cao 				   struct ext4_super_block *es)
3305ac27a0ecSDave Kleikamp {
3306ac27a0ecSDave Kleikamp 	journal_t *journal;
3307ac27a0ecSDave Kleikamp 	int j_errno;
3308ac27a0ecSDave Kleikamp 	const char *errstr;
3309ac27a0ecSDave Kleikamp 
33100390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
33110390131bSFrank Mayhar 
3312617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
3313ac27a0ecSDave Kleikamp 
3314ac27a0ecSDave Kleikamp 	/*
3315ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
3316617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
3317ac27a0ecSDave Kleikamp 	 */
3318ac27a0ecSDave Kleikamp 
3319dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
3320ac27a0ecSDave Kleikamp 	if (j_errno) {
3321ac27a0ecSDave Kleikamp 		char nbuf[16];
3322ac27a0ecSDave Kleikamp 
3323617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
332446e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Filesystem error recorded "
3325ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
332646e665e9SHarvey Harrison 		ext4_warning(sb, __func__, "Marking fs in need of "
3327ac27a0ecSDave Kleikamp 			     "filesystem check.");
3328ac27a0ecSDave Kleikamp 
3329617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3330617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3331e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3332ac27a0ecSDave Kleikamp 
3333dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
3334ac27a0ecSDave Kleikamp 	}
3335ac27a0ecSDave Kleikamp }
3336ac27a0ecSDave Kleikamp 
3337ac27a0ecSDave Kleikamp /*
3338ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
3339ac27a0ecSDave Kleikamp  * and wait on the commit.
3340ac27a0ecSDave Kleikamp  */
3341617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
3342ac27a0ecSDave Kleikamp {
3343ac27a0ecSDave Kleikamp 	journal_t *journal;
33440390131bSFrank Mayhar 	int ret = 0;
3345ac27a0ecSDave Kleikamp 
3346ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
3347ac27a0ecSDave Kleikamp 		return 0;
3348ac27a0ecSDave Kleikamp 
3349617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
33507234ab2aSTheodore Ts'o 	if (journal)
3351617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
33520390131bSFrank Mayhar 
3353ac27a0ecSDave Kleikamp 	return ret;
3354ac27a0ecSDave Kleikamp }
3355ac27a0ecSDave Kleikamp 
3356617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
3357ac27a0ecSDave Kleikamp {
3358ebc1ac16SChristoph Hellwig 	lock_super(sb);
3359e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3360ebc1ac16SChristoph Hellwig 	unlock_super(sb);
33610390131bSFrank Mayhar }
3362ac27a0ecSDave Kleikamp 
3363617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
3364ac27a0ecSDave Kleikamp {
336514ce0cb4STheodore Ts'o 	int ret = 0;
33669eddacf9SJan Kara 	tid_t target;
3367ac27a0ecSDave Kleikamp 
33689bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
33699ca92389STheodore Ts'o 	if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
3370ac27a0ecSDave Kleikamp 		if (wait)
33719ca92389STheodore Ts'o 			jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
33720390131bSFrank Mayhar 	}
337314ce0cb4STheodore Ts'o 	return ret;
3374ac27a0ecSDave Kleikamp }
3375ac27a0ecSDave Kleikamp 
3376ac27a0ecSDave Kleikamp /*
3377ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
3378ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
3379ac27a0ecSDave Kleikamp  */
3380c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
3381ac27a0ecSDave Kleikamp {
3382c4be0c1dSTakashi Sato 	int error = 0;
3383c4be0c1dSTakashi Sato 	journal_t *journal;
3384ac27a0ecSDave Kleikamp 
33859ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
33869ca92389STheodore Ts'o 		return 0;
33879ca92389STheodore Ts'o 
3388c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
3389ac27a0ecSDave Kleikamp 
3390ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
3391dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
33927ffe1ea8SHidehiro Kawai 
33937ffe1ea8SHidehiro Kawai 	/*
33949ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
33959ca92389STheodore Ts'o 	 * the journal.
33967ffe1ea8SHidehiro Kawai 	 */
3397c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
33989ca92389STheodore Ts'o 	if (error < 0) {
33999ca92389STheodore Ts'o 	out:
34009ca92389STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
34019ca92389STheodore Ts'o 		return error;
34020390131bSFrank Mayhar 	}
3403ac27a0ecSDave Kleikamp 
3404ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
3405617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3406e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
3407c4be0c1dSTakashi Sato 	if (error)
3408c4be0c1dSTakashi Sato 		goto out;
3409c4be0c1dSTakashi Sato 	return 0;
3410ac27a0ecSDave Kleikamp }
3411ac27a0ecSDave Kleikamp 
3412ac27a0ecSDave Kleikamp /*
3413ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
3414ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
3415ac27a0ecSDave Kleikamp  */
3416c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
3417ac27a0ecSDave Kleikamp {
34189ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
34199ca92389STheodore Ts'o 		return 0;
34209ca92389STheodore Ts'o 
3421ac27a0ecSDave Kleikamp 	lock_super(sb);
34229ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
3423617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3424e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3425ac27a0ecSDave Kleikamp 	unlock_super(sb);
3426dab291afSMingming Cao 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3427c4be0c1dSTakashi Sato 	return 0;
3428ac27a0ecSDave Kleikamp }
3429ac27a0ecSDave Kleikamp 
3430617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
3431ac27a0ecSDave Kleikamp {
3432617ba13bSMingming Cao 	struct ext4_super_block *es;
3433617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3434617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
3435ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
3436617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
34378a266467STheodore Ts'o 	ext4_group_t g;
3438b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3439ac27a0ecSDave Kleikamp 	int err;
3440ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3441ac27a0ecSDave Kleikamp 	int i;
3442ac27a0ecSDave Kleikamp #endif
3443ac27a0ecSDave Kleikamp 
3444337eb00aSAlessio Igor Bogani 	lock_kernel();
3445337eb00aSAlessio Igor Bogani 
3446ac27a0ecSDave Kleikamp 	/* Store the original options */
3447bbd6851aSAl Viro 	lock_super(sb);
3448ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
3449ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
3450ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
3451ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
3452ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
345330773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
345430773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
3455ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3456ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3457ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3458ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3459ac27a0ecSDave Kleikamp #endif
3460b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3461b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
3462ac27a0ecSDave Kleikamp 
3463ac27a0ecSDave Kleikamp 	/*
3464ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
3465ac27a0ecSDave Kleikamp 	 */
3466b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
3467b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
3468ac27a0ecSDave Kleikamp 		err = -EINVAL;
3469ac27a0ecSDave Kleikamp 		goto restore_opts;
3470ac27a0ecSDave Kleikamp 	}
3471ac27a0ecSDave Kleikamp 
34724ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
347346e665e9SHarvey Harrison 		ext4_abort(sb, __func__, "Abort forced by user");
3474ac27a0ecSDave Kleikamp 
3475ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3476617ba13bSMingming Cao 		((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3477ac27a0ecSDave Kleikamp 
3478ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3479ac27a0ecSDave Kleikamp 
3480b3881f74STheodore Ts'o 	if (sbi->s_journal) {
3481617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
3482b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3483b3881f74STheodore Ts'o 	}
3484ac27a0ecSDave Kleikamp 
3485ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3486bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
34874ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
3488ac27a0ecSDave Kleikamp 			err = -EROFS;
3489ac27a0ecSDave Kleikamp 			goto restore_opts;
3490ac27a0ecSDave Kleikamp 		}
3491ac27a0ecSDave Kleikamp 
3492ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
3493ac27a0ecSDave Kleikamp 			/*
3494ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3495ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3496ac27a0ecSDave Kleikamp 			 */
3497ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3498ac27a0ecSDave Kleikamp 
3499ac27a0ecSDave Kleikamp 			/*
3500ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3501ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3502ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3503ac27a0ecSDave Kleikamp 			 */
3504617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3505617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3506ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3507ac27a0ecSDave Kleikamp 
3508a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
3509617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
3510ac27a0ecSDave Kleikamp 		} else {
3511a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
3512a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
3513ac27a0ecSDave Kleikamp 				err = -EROFS;
3514ac27a0ecSDave Kleikamp 				goto restore_opts;
3515ac27a0ecSDave Kleikamp 			}
3516ead6596bSEric Sandeen 			/*
35178a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
35180b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
35198a266467STheodore Ts'o 			 */
35208a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
35218a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
35228a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
35238a266467STheodore Ts'o 
35248a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3525b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
3526b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
35278a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
35288a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
35298a266467STheodore Ts'o 					err = -EINVAL;
35308a266467STheodore Ts'o 					goto restore_opts;
35318a266467STheodore Ts'o 				}
35328a266467STheodore Ts'o 			}
35338a266467STheodore Ts'o 
35348a266467STheodore Ts'o 			/*
3535ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3536ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3537ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3538ead6596bSEric Sandeen 			 */
3539ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3540b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
3541ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3542ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3543b31e1552SEric Sandeen 				       "umount/remount instead");
3544ead6596bSEric Sandeen 				err = -EINVAL;
3545ead6596bSEric Sandeen 				goto restore_opts;
3546ead6596bSEric Sandeen 			}
3547ead6596bSEric Sandeen 
3548ac27a0ecSDave Kleikamp 			/*
3549ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3550ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3551ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3552ac27a0ecSDave Kleikamp 			 * the partition.)
3553ac27a0ecSDave Kleikamp 			 */
35540390131bSFrank Mayhar 			if (sbi->s_journal)
3555617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
3556ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3557617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3558ac27a0ecSDave Kleikamp 				goto restore_opts;
3559617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3560ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3561ac27a0ecSDave Kleikamp 		}
3562ac27a0ecSDave Kleikamp 	}
35636fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
35640390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
3565e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
35660390131bSFrank Mayhar 
3567ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3568ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3569ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3570ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3571ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3572ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3573ac27a0ecSDave Kleikamp #endif
3574bbd6851aSAl Viro 	unlock_super(sb);
3575337eb00aSAlessio Igor Bogani 	unlock_kernel();
3576ac27a0ecSDave Kleikamp 	return 0;
35770b8e58a1SAndreas Dilger 
3578ac27a0ecSDave Kleikamp restore_opts:
3579ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3580ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3581ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3582ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3583ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
358430773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
358530773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
3586ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3587ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3588ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3589ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3590ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3591ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3592ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3593ac27a0ecSDave Kleikamp 	}
3594ac27a0ecSDave Kleikamp #endif
3595bbd6851aSAl Viro 	unlock_super(sb);
3596337eb00aSAlessio Igor Bogani 	unlock_kernel();
3597ac27a0ecSDave Kleikamp 	return err;
3598ac27a0ecSDave Kleikamp }
3599ac27a0ecSDave Kleikamp 
3600617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3601ac27a0ecSDave Kleikamp {
3602ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3603617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3604617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3605960cc398SPekka Enberg 	u64 fsid;
3606ac27a0ecSDave Kleikamp 
36075e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
36085e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
36096bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
36108df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
36115e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3612ac27a0ecSDave Kleikamp 
3613ac27a0ecSDave Kleikamp 		/*
36145e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
36155e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
36165e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3617ac27a0ecSDave Kleikamp 		 */
3618ac27a0ecSDave Kleikamp 
3619ac27a0ecSDave Kleikamp 		/*
3620ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3621ac27a0ecSDave Kleikamp 		 * overhead
3622ac27a0ecSDave Kleikamp 		 */
3623ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3624ac27a0ecSDave Kleikamp 
3625ac27a0ecSDave Kleikamp 		/*
3626ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3627ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3628ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3629ac27a0ecSDave Kleikamp 		 */
3630ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3631617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3632617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3633ac27a0ecSDave Kleikamp 			cond_resched();
3634ac27a0ecSDave Kleikamp 		}
3635ac27a0ecSDave Kleikamp 
3636ac27a0ecSDave Kleikamp 		/*
3637ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3638ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3639ac27a0ecSDave Kleikamp 		 */
36405e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
36415e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
36425e70030dSBadari Pulavarty 		smp_wmb();
36436bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3644ac27a0ecSDave Kleikamp 	}
3645ac27a0ecSDave Kleikamp 
3646617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3647ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
36485e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
36496bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
36506bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3651308ba3ecSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, buf->f_bfree);
3652bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3653bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3654ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3655ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
365652d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
36575e70030dSBadari Pulavarty 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3658617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3659960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3660960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3661960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3662960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
36630b8e58a1SAndreas Dilger 
3664ac27a0ecSDave Kleikamp 	return 0;
3665ac27a0ecSDave Kleikamp }
3666ac27a0ecSDave Kleikamp 
36670b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
36680b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
3669ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3670617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3671dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
3672a269eb18SJan Kara  *   vfs_dq_init()                         down(dqio_mutex)
3673dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3674ac27a0ecSDave Kleikamp  *
3675ac27a0ecSDave Kleikamp  */
3676ac27a0ecSDave Kleikamp 
3677ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3678ac27a0ecSDave Kleikamp 
3679ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3680ac27a0ecSDave Kleikamp {
3681ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3682ac27a0ecSDave Kleikamp }
3683ac27a0ecSDave Kleikamp 
3684617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3685ac27a0ecSDave Kleikamp {
3686ac27a0ecSDave Kleikamp 	int ret, err;
3687ac27a0ecSDave Kleikamp 	handle_t *handle;
3688ac27a0ecSDave Kleikamp 	struct inode *inode;
3689ac27a0ecSDave Kleikamp 
3690ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3691617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3692617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3693ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3694ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3695ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3696617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3697ac27a0ecSDave Kleikamp 	if (!ret)
3698ac27a0ecSDave Kleikamp 		ret = err;
3699ac27a0ecSDave Kleikamp 	return ret;
3700ac27a0ecSDave Kleikamp }
3701ac27a0ecSDave Kleikamp 
3702617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3703ac27a0ecSDave Kleikamp {
3704ac27a0ecSDave Kleikamp 	int ret, err;
3705ac27a0ecSDave Kleikamp 	handle_t *handle;
3706ac27a0ecSDave Kleikamp 
3707617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3708617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3709ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3710ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3711ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3712617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3713ac27a0ecSDave Kleikamp 	if (!ret)
3714ac27a0ecSDave Kleikamp 		ret = err;
3715ac27a0ecSDave Kleikamp 	return ret;
3716ac27a0ecSDave Kleikamp }
3717ac27a0ecSDave Kleikamp 
3718617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3719ac27a0ecSDave Kleikamp {
3720ac27a0ecSDave Kleikamp 	int ret, err;
3721ac27a0ecSDave Kleikamp 	handle_t *handle;
3722ac27a0ecSDave Kleikamp 
3723617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3724617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
37259c3013e9SJan Kara 	if (IS_ERR(handle)) {
37269c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
37279c3013e9SJan Kara 		dquot_release(dquot);
3728ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
37299c3013e9SJan Kara 	}
3730ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3731617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3732ac27a0ecSDave Kleikamp 	if (!ret)
3733ac27a0ecSDave Kleikamp 		ret = err;
3734ac27a0ecSDave Kleikamp 	return ret;
3735ac27a0ecSDave Kleikamp }
3736ac27a0ecSDave Kleikamp 
3737617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3738ac27a0ecSDave Kleikamp {
37392c8be6b2SJan Kara 	/* Are we journaling quotas? */
3740617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3741617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3742ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3743617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3744ac27a0ecSDave Kleikamp 	} else {
3745ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3746ac27a0ecSDave Kleikamp 	}
3747ac27a0ecSDave Kleikamp }
3748ac27a0ecSDave Kleikamp 
3749617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3750ac27a0ecSDave Kleikamp {
3751ac27a0ecSDave Kleikamp 	int ret, err;
3752ac27a0ecSDave Kleikamp 	handle_t *handle;
3753ac27a0ecSDave Kleikamp 
3754ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3755617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3756ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3757ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3758ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3759617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3760ac27a0ecSDave Kleikamp 	if (!ret)
3761ac27a0ecSDave Kleikamp 		ret = err;
3762ac27a0ecSDave Kleikamp 	return ret;
3763ac27a0ecSDave Kleikamp }
3764ac27a0ecSDave Kleikamp 
3765ac27a0ecSDave Kleikamp /*
3766ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3767ac27a0ecSDave Kleikamp  * the quota file and such...
3768ac27a0ecSDave Kleikamp  */
3769617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3770ac27a0ecSDave Kleikamp {
3771617ba13bSMingming Cao 	return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3772617ba13bSMingming Cao 				  EXT4_SB(sb)->s_jquota_fmt, type);
3773ac27a0ecSDave Kleikamp }
3774ac27a0ecSDave Kleikamp 
3775ac27a0ecSDave Kleikamp /*
3776ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3777ac27a0ecSDave Kleikamp  */
3778617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
37798264613dSAl Viro 			 char *name, int remount)
3780ac27a0ecSDave Kleikamp {
3781ac27a0ecSDave Kleikamp 	int err;
37828264613dSAl Viro 	struct path path;
3783ac27a0ecSDave Kleikamp 
3784ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3785ac27a0ecSDave Kleikamp 		return -EINVAL;
37868264613dSAl Viro 	/* When remounting, no checks are needed and in fact, name is NULL */
37870623543bSJan Kara 	if (remount)
37888264613dSAl Viro 		return vfs_quota_on(sb, type, format_id, name, remount);
37890623543bSJan Kara 
37908264613dSAl Viro 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3791ac27a0ecSDave Kleikamp 	if (err)
3792ac27a0ecSDave Kleikamp 		return err;
37930623543bSJan Kara 
3794ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
37958264613dSAl Viro 	if (path.mnt->mnt_sb != sb) {
37968264613dSAl Viro 		path_put(&path);
3797ac27a0ecSDave Kleikamp 		return -EXDEV;
3798ac27a0ecSDave Kleikamp 	}
37990623543bSJan Kara 	/* Journaling quota? */
38000623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
38012b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
38028264613dSAl Viro 		if (path.dentry->d_parent != sb->s_root)
3803b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
3804b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
3805b31e1552SEric Sandeen 				"Journaled quota will not work");
38060623543bSJan Kara 	}
38070623543bSJan Kara 
38080623543bSJan Kara 	/*
38090623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
38100623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
38110623543bSJan Kara 	 */
38120390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
38130390131bSFrank Mayhar 	    ext4_should_journal_data(path.dentry->d_inode)) {
38140623543bSJan Kara 		/*
38150623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
38160623543bSJan Kara 		 * otherwise be livelocked...
38170623543bSJan Kara 		 */
38180623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
38197ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
38200623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
38217ffe1ea8SHidehiro Kawai 		if (err) {
38228264613dSAl Viro 			path_put(&path);
38237ffe1ea8SHidehiro Kawai 			return err;
38247ffe1ea8SHidehiro Kawai 		}
38250623543bSJan Kara 	}
38260623543bSJan Kara 
38278264613dSAl Viro 	err = vfs_quota_on_path(sb, type, format_id, &path);
38288264613dSAl Viro 	path_put(&path);
382977e69dacSAl Viro 	return err;
3830ac27a0ecSDave Kleikamp }
3831ac27a0ecSDave Kleikamp 
3832ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3833ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3834ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3835ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3836617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3837ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3838ac27a0ecSDave Kleikamp {
3839ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3840725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3841ac27a0ecSDave Kleikamp 	int err = 0;
3842ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3843ac27a0ecSDave Kleikamp 	int tocopy;
3844ac27a0ecSDave Kleikamp 	size_t toread;
3845ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3846ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3847ac27a0ecSDave Kleikamp 
3848ac27a0ecSDave Kleikamp 	if (off > i_size)
3849ac27a0ecSDave Kleikamp 		return 0;
3850ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3851ac27a0ecSDave Kleikamp 		len = i_size-off;
3852ac27a0ecSDave Kleikamp 	toread = len;
3853ac27a0ecSDave Kleikamp 	while (toread > 0) {
3854ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
3855ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
3856617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
3857ac27a0ecSDave Kleikamp 		if (err)
3858ac27a0ecSDave Kleikamp 			return err;
3859ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
3860ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
3861ac27a0ecSDave Kleikamp 		else
3862ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
3863ac27a0ecSDave Kleikamp 		brelse(bh);
3864ac27a0ecSDave Kleikamp 		offset = 0;
3865ac27a0ecSDave Kleikamp 		toread -= tocopy;
3866ac27a0ecSDave Kleikamp 		data += tocopy;
3867ac27a0ecSDave Kleikamp 		blk++;
3868ac27a0ecSDave Kleikamp 	}
3869ac27a0ecSDave Kleikamp 	return len;
3870ac27a0ecSDave Kleikamp }
3871ac27a0ecSDave Kleikamp 
3872ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
3873ac27a0ecSDave Kleikamp  * enough credits) */
3874617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
3875ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
3876ac27a0ecSDave Kleikamp {
3877ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3878725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3879ac27a0ecSDave Kleikamp 	int err = 0;
3880ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3881ac27a0ecSDave Kleikamp 	int tocopy;
3882617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3883ac27a0ecSDave Kleikamp 	size_t towrite = len;
3884ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3885ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
3886ac27a0ecSDave Kleikamp 
38870390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
3888b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3889b31e1552SEric Sandeen 			" cancelled because transaction is not started",
38909c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
38919c3013e9SJan Kara 		return -EIO;
38929c3013e9SJan Kara 	}
3893ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3894ac27a0ecSDave Kleikamp 	while (towrite > 0) {
3895ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < towrite ?
3896ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : towrite;
3897617ba13bSMingming Cao 		bh = ext4_bread(handle, inode, blk, 1, &err);
3898ac27a0ecSDave Kleikamp 		if (!bh)
3899ac27a0ecSDave Kleikamp 			goto out;
3900ac27a0ecSDave Kleikamp 		if (journal_quota) {
3901617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bh);
3902ac27a0ecSDave Kleikamp 			if (err) {
3903ac27a0ecSDave Kleikamp 				brelse(bh);
3904ac27a0ecSDave Kleikamp 				goto out;
3905ac27a0ecSDave Kleikamp 			}
3906ac27a0ecSDave Kleikamp 		}
3907ac27a0ecSDave Kleikamp 		lock_buffer(bh);
3908ac27a0ecSDave Kleikamp 		memcpy(bh->b_data+offset, data, tocopy);
3909ac27a0ecSDave Kleikamp 		flush_dcache_page(bh->b_page);
3910ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
3911ac27a0ecSDave Kleikamp 		if (journal_quota)
39120390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle, NULL, bh);
3913ac27a0ecSDave Kleikamp 		else {
3914ac27a0ecSDave Kleikamp 			/* Always do at least ordered writes for quotas */
3915678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3916ac27a0ecSDave Kleikamp 			mark_buffer_dirty(bh);
3917ac27a0ecSDave Kleikamp 		}
3918ac27a0ecSDave Kleikamp 		brelse(bh);
3919ac27a0ecSDave Kleikamp 		if (err)
3920ac27a0ecSDave Kleikamp 			goto out;
3921ac27a0ecSDave Kleikamp 		offset = 0;
3922ac27a0ecSDave Kleikamp 		towrite -= tocopy;
3923ac27a0ecSDave Kleikamp 		data += tocopy;
3924ac27a0ecSDave Kleikamp 		blk++;
3925ac27a0ecSDave Kleikamp 	}
3926ac27a0ecSDave Kleikamp out:
39274d04e4fbSJan Kara 	if (len == towrite) {
39284d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
3929ac27a0ecSDave Kleikamp 		return err;
39304d04e4fbSJan Kara 	}
3931ac27a0ecSDave Kleikamp 	if (inode->i_size < off+len-towrite) {
3932ac27a0ecSDave Kleikamp 		i_size_write(inode, off+len-towrite);
3933617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
3934ac27a0ecSDave Kleikamp 	}
3935ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3936617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3937ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
3938ac27a0ecSDave Kleikamp 	return len - towrite;
3939ac27a0ecSDave Kleikamp }
3940ac27a0ecSDave Kleikamp 
3941ac27a0ecSDave Kleikamp #endif
3942ac27a0ecSDave Kleikamp 
39430b8e58a1SAndreas Dilger static int ext4_get_sb(struct file_system_type *fs_type, int flags,
39440b8e58a1SAndreas Dilger 		       const char *dev_name, void *data, struct vfsmount *mnt)
3945ac27a0ecSDave Kleikamp {
3946617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
3947ac27a0ecSDave Kleikamp }
3948ac27a0ecSDave Kleikamp 
394903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
3950ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
395103010a33STheodore Ts'o 	.name		= "ext4",
3952617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
3953ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
3954ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
3955ac27a0ecSDave Kleikamp };
3956ac27a0ecSDave Kleikamp 
395703010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
39580b8e58a1SAndreas Dilger static int ext4dev_get_sb(struct file_system_type *fs_type, int flags,
39590b8e58a1SAndreas Dilger 			  const char *dev_name, void *data,struct vfsmount *mnt)
396003010a33STheodore Ts'o {
3961b31e1552SEric Sandeen 	printk(KERN_WARNING "EXT4-fs (%s): Update your userspace programs "
3962b31e1552SEric Sandeen 	       "to mount using ext4\n", dev_name);
3963b31e1552SEric Sandeen 	printk(KERN_WARNING "EXT4-fs (%s): ext4dev backwards compatibility "
3964b31e1552SEric Sandeen 	       "will go away by 2.6.31\n", dev_name);
396503010a33STheodore Ts'o 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
396603010a33STheodore Ts'o }
396703010a33STheodore Ts'o 
396803010a33STheodore Ts'o static struct file_system_type ext4dev_fs_type = {
396903010a33STheodore Ts'o 	.owner		= THIS_MODULE,
397003010a33STheodore Ts'o 	.name		= "ext4dev",
397103010a33STheodore Ts'o 	.get_sb		= ext4dev_get_sb,
397203010a33STheodore Ts'o 	.kill_sb	= kill_block_super,
397303010a33STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
397403010a33STheodore Ts'o };
397503010a33STheodore Ts'o MODULE_ALIAS("ext4dev");
397603010a33STheodore Ts'o #endif
397703010a33STheodore Ts'o 
3978617ba13bSMingming Cao static int __init init_ext4_fs(void)
3979ac27a0ecSDave Kleikamp {
3980c9de560dSAlex Tomas 	int err;
3981c9de560dSAlex Tomas 
39826fd058f7STheodore Ts'o 	err = init_ext4_system_zone();
39836fd058f7STheodore Ts'o 	if (err)
39846fd058f7STheodore Ts'o 		return err;
39853197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
39863197ebdbSTheodore Ts'o 	if (!ext4_kset)
39876fd058f7STheodore Ts'o 		goto out4;
39889f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3989c9de560dSAlex Tomas 	err = init_ext4_mballoc();
3990ac27a0ecSDave Kleikamp 	if (err)
39916fd058f7STheodore Ts'o 		goto out3;
3992c9de560dSAlex Tomas 
3993c9de560dSAlex Tomas 	err = init_ext4_xattr();
3994c9de560dSAlex Tomas 	if (err)
3995c9de560dSAlex Tomas 		goto out2;
3996ac27a0ecSDave Kleikamp 	err = init_inodecache();
3997ac27a0ecSDave Kleikamp 	if (err)
3998ac27a0ecSDave Kleikamp 		goto out1;
399903010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
4000ac27a0ecSDave Kleikamp 	if (err)
4001ac27a0ecSDave Kleikamp 		goto out;
400203010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
400303010a33STheodore Ts'o 	err = register_filesystem(&ext4dev_fs_type);
400403010a33STheodore Ts'o 	if (err) {
400503010a33STheodore Ts'o 		unregister_filesystem(&ext4_fs_type);
400603010a33STheodore Ts'o 		goto out;
400703010a33STheodore Ts'o 	}
400803010a33STheodore Ts'o #endif
4009ac27a0ecSDave Kleikamp 	return 0;
4010ac27a0ecSDave Kleikamp out:
4011ac27a0ecSDave Kleikamp 	destroy_inodecache();
4012ac27a0ecSDave Kleikamp out1:
4013617ba13bSMingming Cao 	exit_ext4_xattr();
4014c9de560dSAlex Tomas out2:
4015c9de560dSAlex Tomas 	exit_ext4_mballoc();
40166fd058f7STheodore Ts'o out3:
40176fd058f7STheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
40186fd058f7STheodore Ts'o 	kset_unregister(ext4_kset);
40196fd058f7STheodore Ts'o out4:
40206fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4021ac27a0ecSDave Kleikamp 	return err;
4022ac27a0ecSDave Kleikamp }
4023ac27a0ecSDave Kleikamp 
4024617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
4025ac27a0ecSDave Kleikamp {
402603010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
402703010a33STheodore Ts'o #ifdef CONFIG_EXT4DEV_COMPAT
4028617ba13bSMingming Cao 	unregister_filesystem(&ext4dev_fs_type);
402903010a33STheodore Ts'o #endif
4030ac27a0ecSDave Kleikamp 	destroy_inodecache();
4031617ba13bSMingming Cao 	exit_ext4_xattr();
4032c9de560dSAlex Tomas 	exit_ext4_mballoc();
40339f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
40343197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
40356fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4036ac27a0ecSDave Kleikamp }
4037ac27a0ecSDave Kleikamp 
4038ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
403983982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
4040ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
4041617ba13bSMingming Cao module_init(init_ext4_fs)
4042617ba13bSMingming Cao module_exit(exit_ext4_fs)
4043