xref: /linux/fs/ext4/super.c (revision 90c7201b97bb7ac5a4e2605abc0efb5fdfb957f0)
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 
539f6200bbSTheodore Ts'o struct proc_dir_entry *ext4_proc_root;
543197ebdbSTheodore Ts'o static struct kset *ext4_kset;
559f6200bbSTheodore Ts'o 
56617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
57ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
58e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
59617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
60617ba13bSMingming Cao 					struct ext4_super_block *es);
61617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
62617ba13bSMingming Cao 				   struct ext4_super_block *es);
63617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
64617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
65ac27a0ecSDave Kleikamp 				     char nbuf[16]);
66617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
67617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
68c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
69617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb);
70c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
71ba69f9abSJan Kara static int ext4_get_sb(struct file_system_type *fs_type, int flags,
72ba69f9abSJan Kara 		       const char *dev_name, void *data, struct vfsmount *mnt);
73ac27a0ecSDave Kleikamp 
74ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
75ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
76ba69f9abSJan Kara 	.owner		= THIS_MODULE,
77ba69f9abSJan Kara 	.name		= "ext3",
78ba69f9abSJan Kara 	.get_sb		= ext4_get_sb,
79ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
80ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
81ba69f9abSJan Kara };
82ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
83ba69f9abSJan Kara #else
84ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0)
85ba69f9abSJan Kara #endif
86bd81d8eeSLaurent Vivier 
878fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
888fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
89bd81d8eeSLaurent Vivier {
903a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
918fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
928fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
93bd81d8eeSLaurent Vivier }
94bd81d8eeSLaurent Vivier 
958fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
968fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
97bd81d8eeSLaurent Vivier {
985272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
998fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1008fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
101bd81d8eeSLaurent Vivier }
102bd81d8eeSLaurent Vivier 
1038fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
1048fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
105bd81d8eeSLaurent Vivier {
1065272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
1078fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
1088fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
109bd81d8eeSLaurent Vivier }
110bd81d8eeSLaurent Vivier 
111560671a0SAneesh Kumar K.V __u32 ext4_free_blks_count(struct super_block *sb,
112560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
113560671a0SAneesh Kumar K.V {
114560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
115560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
116560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
117560671a0SAneesh Kumar K.V }
118560671a0SAneesh Kumar K.V 
119560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
120560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
121560671a0SAneesh Kumar K.V {
122560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
123560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
124560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
125560671a0SAneesh Kumar K.V }
126560671a0SAneesh Kumar K.V 
127560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
128560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
129560671a0SAneesh Kumar K.V {
130560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
131560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
132560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
133560671a0SAneesh Kumar K.V }
134560671a0SAneesh Kumar K.V 
135560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
136560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
137560671a0SAneesh Kumar K.V {
138560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
139560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
140560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
141560671a0SAneesh Kumar K.V }
142560671a0SAneesh Kumar K.V 
1438fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
1448fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
145bd81d8eeSLaurent Vivier {
1463a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
1478fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1488fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
149bd81d8eeSLaurent Vivier }
150bd81d8eeSLaurent Vivier 
1518fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
1528fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
153bd81d8eeSLaurent Vivier {
1545272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
1558fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1568fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
157bd81d8eeSLaurent Vivier }
158bd81d8eeSLaurent Vivier 
1598fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
1608fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
161bd81d8eeSLaurent Vivier {
1625272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
1638fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
1648fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
165bd81d8eeSLaurent Vivier }
166bd81d8eeSLaurent Vivier 
167560671a0SAneesh Kumar K.V void ext4_free_blks_set(struct super_block *sb,
168560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
169560671a0SAneesh Kumar K.V {
170560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
171560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
172560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
173560671a0SAneesh Kumar K.V }
174560671a0SAneesh Kumar K.V 
175560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
176560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
177560671a0SAneesh Kumar K.V {
178560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
179560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
180560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
181560671a0SAneesh Kumar K.V }
182560671a0SAneesh Kumar K.V 
183560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
184560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
185560671a0SAneesh Kumar K.V {
186560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
187560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
188560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
189560671a0SAneesh Kumar K.V }
190560671a0SAneesh Kumar K.V 
191560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
192560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
193560671a0SAneesh Kumar K.V {
194560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
195560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
196560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
197560671a0SAneesh Kumar K.V }
198560671a0SAneesh Kumar K.V 
199d3d1faf6SCurt Wohlgemuth 
200d3d1faf6SCurt Wohlgemuth /* Just increment the non-pointer handle value */
201d3d1faf6SCurt Wohlgemuth static handle_t *ext4_get_nojournal(void)
202d3d1faf6SCurt Wohlgemuth {
203d3d1faf6SCurt Wohlgemuth 	handle_t *handle = current->journal_info;
204d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
205d3d1faf6SCurt Wohlgemuth 
206d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
207d3d1faf6SCurt Wohlgemuth 
208d3d1faf6SCurt Wohlgemuth 	ref_cnt++;
209d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
210d3d1faf6SCurt Wohlgemuth 
211d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
212d3d1faf6SCurt Wohlgemuth 	return handle;
213d3d1faf6SCurt Wohlgemuth }
214d3d1faf6SCurt Wohlgemuth 
215d3d1faf6SCurt Wohlgemuth 
216d3d1faf6SCurt Wohlgemuth /* Decrement the non-pointer handle value */
217d3d1faf6SCurt Wohlgemuth static void ext4_put_nojournal(handle_t *handle)
218d3d1faf6SCurt Wohlgemuth {
219d3d1faf6SCurt Wohlgemuth 	unsigned long ref_cnt = (unsigned long)handle;
220d3d1faf6SCurt Wohlgemuth 
221d3d1faf6SCurt Wohlgemuth 	BUG_ON(ref_cnt == 0);
222d3d1faf6SCurt Wohlgemuth 
223d3d1faf6SCurt Wohlgemuth 	ref_cnt--;
224d3d1faf6SCurt Wohlgemuth 	handle = (handle_t *)ref_cnt;
225d3d1faf6SCurt Wohlgemuth 
226d3d1faf6SCurt Wohlgemuth 	current->journal_info = handle;
227d3d1faf6SCurt Wohlgemuth }
228d3d1faf6SCurt Wohlgemuth 
229ac27a0ecSDave Kleikamp /*
230dab291afSMingming Cao  * Wrappers for jbd2_journal_start/end.
231ac27a0ecSDave Kleikamp  *
232ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
233ac27a0ecSDave Kleikamp  * journal_end calls result in the superblock being marked dirty, so
234ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
235ac27a0ecSDave Kleikamp  * appropriate.
236ac27a0ecSDave Kleikamp  */
237617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
238ac27a0ecSDave Kleikamp {
239ac27a0ecSDave Kleikamp 	journal_t *journal;
240ac27a0ecSDave Kleikamp 
241ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
242ac27a0ecSDave Kleikamp 		return ERR_PTR(-EROFS);
243ac27a0ecSDave Kleikamp 
2446b0310fbSEric Sandeen 	vfs_check_frozen(sb, SB_FREEZE_WRITE);
245ac27a0ecSDave Kleikamp 	/* Special case here: if the journal has aborted behind our
246ac27a0ecSDave Kleikamp 	 * backs (eg. EIO in the commit thread), then we still need to
247ac27a0ecSDave Kleikamp 	 * take the FS itself readonly cleanly. */
248617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
2490390131bSFrank Mayhar 	if (journal) {
250ac27a0ecSDave Kleikamp 		if (is_journal_aborted(journal)) {
251c67d859eSTheodore Ts'o 			ext4_abort(sb, "Detected aborted journal");
252ac27a0ecSDave Kleikamp 			return ERR_PTR(-EROFS);
253ac27a0ecSDave Kleikamp 		}
254dab291afSMingming Cao 		return jbd2_journal_start(journal, nblocks);
255ac27a0ecSDave Kleikamp 	}
256d3d1faf6SCurt Wohlgemuth 	return ext4_get_nojournal();
2570390131bSFrank Mayhar }
258ac27a0ecSDave Kleikamp 
259ac27a0ecSDave Kleikamp /*
260ac27a0ecSDave Kleikamp  * The only special thing we need to do here is to make sure that all
261dab291afSMingming Cao  * jbd2_journal_stop calls result in the superblock being marked dirty, so
262ac27a0ecSDave Kleikamp  * that sync() will call the filesystem's write_super callback if
263ac27a0ecSDave Kleikamp  * appropriate.
264ac27a0ecSDave Kleikamp  */
265617ba13bSMingming Cao int __ext4_journal_stop(const char *where, handle_t *handle)
266ac27a0ecSDave Kleikamp {
267ac27a0ecSDave Kleikamp 	struct super_block *sb;
268ac27a0ecSDave Kleikamp 	int err;
269ac27a0ecSDave Kleikamp 	int rc;
270ac27a0ecSDave Kleikamp 
2710390131bSFrank Mayhar 	if (!ext4_handle_valid(handle)) {
272d3d1faf6SCurt Wohlgemuth 		ext4_put_nojournal(handle);
2730390131bSFrank Mayhar 		return 0;
2740390131bSFrank Mayhar 	}
275ac27a0ecSDave Kleikamp 	sb = handle->h_transaction->t_journal->j_private;
276ac27a0ecSDave Kleikamp 	err = handle->h_err;
277dab291afSMingming Cao 	rc = jbd2_journal_stop(handle);
278ac27a0ecSDave Kleikamp 
279ac27a0ecSDave Kleikamp 	if (!err)
280ac27a0ecSDave Kleikamp 		err = rc;
281ac27a0ecSDave Kleikamp 	if (err)
282617ba13bSMingming Cao 		__ext4_std_error(sb, where, err);
283ac27a0ecSDave Kleikamp 	return err;
284ac27a0ecSDave Kleikamp }
285ac27a0ecSDave Kleikamp 
286*90c7201bSTheodore Ts'o void ext4_journal_abort_handle(const char *caller, unsigned int line,
287*90c7201bSTheodore Ts'o 			       const char *err_fn, struct buffer_head *bh,
288*90c7201bSTheodore Ts'o 			       handle_t *handle, int err)
289ac27a0ecSDave Kleikamp {
290ac27a0ecSDave Kleikamp 	char nbuf[16];
291617ba13bSMingming Cao 	const char *errstr = ext4_decode_error(NULL, err, nbuf);
292ac27a0ecSDave Kleikamp 
2930390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
2940390131bSFrank Mayhar 
295ac27a0ecSDave Kleikamp 	if (bh)
296ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "abort");
297ac27a0ecSDave Kleikamp 
298ac27a0ecSDave Kleikamp 	if (!handle->h_err)
299ac27a0ecSDave Kleikamp 		handle->h_err = err;
300ac27a0ecSDave Kleikamp 
301ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
302ac27a0ecSDave Kleikamp 		return;
303ac27a0ecSDave Kleikamp 
304*90c7201bSTheodore Ts'o 	printk(KERN_ERR "%s:%d: aborting transaction: %s in %s\n",
305*90c7201bSTheodore Ts'o 	       caller, line, errstr, err_fn);
306ac27a0ecSDave Kleikamp 
307dab291afSMingming Cao 	jbd2_journal_abort_handle(handle);
308ac27a0ecSDave Kleikamp }
309ac27a0ecSDave Kleikamp 
310ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
311ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
312ac27a0ecSDave Kleikamp  *
313ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
314617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
315ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
316ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
317ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
318ac27a0ecSDave Kleikamp  * write out the superblock safely.
319ac27a0ecSDave Kleikamp  *
320dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
321d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
322ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
323ac27a0ecSDave Kleikamp  */
324ac27a0ecSDave Kleikamp 
325617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
326ac27a0ecSDave Kleikamp {
327617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
328ac27a0ecSDave Kleikamp 
329617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
330617ba13bSMingming Cao 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
331ac27a0ecSDave Kleikamp 
332ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
333ac27a0ecSDave Kleikamp 		return;
334ac27a0ecSDave Kleikamp 
335ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
336617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
337ac27a0ecSDave Kleikamp 
3384ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
339ac27a0ecSDave Kleikamp 		if (journal)
340dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
341ac27a0ecSDave Kleikamp 	}
342ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_RO)) {
343b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
344ac27a0ecSDave Kleikamp 		sb->s_flags |= MS_RDONLY;
345ac27a0ecSDave Kleikamp 	}
346e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
347ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
348617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
349ac27a0ecSDave Kleikamp 			sb->s_id);
350ac27a0ecSDave Kleikamp }
351ac27a0ecSDave Kleikamp 
35212062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
353ac27a0ecSDave Kleikamp 		const char *fmt, ...)
354ac27a0ecSDave Kleikamp {
355ac27a0ecSDave Kleikamp 	va_list args;
356ac27a0ecSDave Kleikamp 
357ac27a0ecSDave Kleikamp 	va_start(args, fmt);
358617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
359ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
360ac27a0ecSDave Kleikamp 	printk("\n");
361ac27a0ecSDave Kleikamp 	va_end(args);
362ac27a0ecSDave Kleikamp 
363617ba13bSMingming Cao 	ext4_handle_error(sb);
364ac27a0ecSDave Kleikamp }
365ac27a0ecSDave Kleikamp 
366273df556SFrank Mayhar void ext4_error_inode(const char *function, struct inode *inode,
367273df556SFrank Mayhar 		      const char *fmt, ...)
368273df556SFrank Mayhar {
369273df556SFrank Mayhar 	va_list args;
370273df556SFrank Mayhar 
371273df556SFrank Mayhar 	va_start(args, fmt);
372273df556SFrank Mayhar 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: inode #%lu: (comm %s) ",
373273df556SFrank Mayhar 	       inode->i_sb->s_id, function, inode->i_ino, current->comm);
374273df556SFrank Mayhar 	vprintk(fmt, args);
375273df556SFrank Mayhar 	printk("\n");
376273df556SFrank Mayhar 	va_end(args);
377273df556SFrank Mayhar 
378273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
379273df556SFrank Mayhar }
380273df556SFrank Mayhar 
381273df556SFrank Mayhar void ext4_error_file(const char *function, struct file *file,
382273df556SFrank Mayhar 		     const char *fmt, ...)
383273df556SFrank Mayhar {
384273df556SFrank Mayhar 	va_list args;
385273df556SFrank Mayhar 	struct inode *inode = file->f_dentry->d_inode;
386273df556SFrank Mayhar 	char pathname[80], *path;
387273df556SFrank Mayhar 
388273df556SFrank Mayhar 	va_start(args, fmt);
389273df556SFrank Mayhar 	path = d_path(&(file->f_path), pathname, sizeof(pathname));
390273df556SFrank Mayhar 	if (!path)
391273df556SFrank Mayhar 		path = "(unknown)";
392273df556SFrank Mayhar 	printk(KERN_CRIT
393273df556SFrank Mayhar 	       "EXT4-fs error (device %s): %s: inode #%lu (comm %s path %s): ",
394273df556SFrank Mayhar 	       inode->i_sb->s_id, function, inode->i_ino, current->comm, path);
395273df556SFrank Mayhar 	vprintk(fmt, args);
396273df556SFrank Mayhar 	printk("\n");
397273df556SFrank Mayhar 	va_end(args);
398273df556SFrank Mayhar 
399273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
400273df556SFrank Mayhar }
401273df556SFrank Mayhar 
402617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno,
403ac27a0ecSDave Kleikamp 				     char nbuf[16])
404ac27a0ecSDave Kleikamp {
405ac27a0ecSDave Kleikamp 	char *errstr = NULL;
406ac27a0ecSDave Kleikamp 
407ac27a0ecSDave Kleikamp 	switch (errno) {
408ac27a0ecSDave Kleikamp 	case -EIO:
409ac27a0ecSDave Kleikamp 		errstr = "IO failure";
410ac27a0ecSDave Kleikamp 		break;
411ac27a0ecSDave Kleikamp 	case -ENOMEM:
412ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
413ac27a0ecSDave Kleikamp 		break;
414ac27a0ecSDave Kleikamp 	case -EROFS:
41578f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
41678f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
417ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
418ac27a0ecSDave Kleikamp 		else
419ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
420ac27a0ecSDave Kleikamp 		break;
421ac27a0ecSDave Kleikamp 	default:
422ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
423ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
424ac27a0ecSDave Kleikamp 		 * NULL. */
425ac27a0ecSDave Kleikamp 		if (nbuf) {
426ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
427ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
428ac27a0ecSDave Kleikamp 				errstr = nbuf;
429ac27a0ecSDave Kleikamp 		}
430ac27a0ecSDave Kleikamp 		break;
431ac27a0ecSDave Kleikamp 	}
432ac27a0ecSDave Kleikamp 
433ac27a0ecSDave Kleikamp 	return errstr;
434ac27a0ecSDave Kleikamp }
435ac27a0ecSDave Kleikamp 
436617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
437ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
438ac27a0ecSDave Kleikamp 
4392b2d6d01STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, int errno)
440ac27a0ecSDave Kleikamp {
441ac27a0ecSDave Kleikamp 	char nbuf[16];
442ac27a0ecSDave Kleikamp 	const char *errstr;
443ac27a0ecSDave Kleikamp 
444ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
445ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
446ac27a0ecSDave Kleikamp 	 * an error. */
447ac27a0ecSDave Kleikamp 	if (errno == -EROFS && journal_current_handle() == NULL &&
448ac27a0ecSDave Kleikamp 	    (sb->s_flags & MS_RDONLY))
449ac27a0ecSDave Kleikamp 		return;
450ac27a0ecSDave Kleikamp 
451617ba13bSMingming Cao 	errstr = ext4_decode_error(sb, errno, nbuf);
452617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
453ac27a0ecSDave Kleikamp 	       sb->s_id, function, errstr);
454ac27a0ecSDave Kleikamp 
455617ba13bSMingming Cao 	ext4_handle_error(sb);
456ac27a0ecSDave Kleikamp }
457ac27a0ecSDave Kleikamp 
458ac27a0ecSDave Kleikamp /*
459617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
460ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
461ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
462ac27a0ecSDave Kleikamp  *
463ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
464ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
465ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
466ac27a0ecSDave Kleikamp  */
467ac27a0ecSDave Kleikamp 
468c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
469ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
470ac27a0ecSDave Kleikamp {
471ac27a0ecSDave Kleikamp 	va_list args;
472ac27a0ecSDave Kleikamp 
473ac27a0ecSDave Kleikamp 	va_start(args, fmt);
474617ba13bSMingming Cao 	printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
475ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
476ac27a0ecSDave Kleikamp 	printk("\n");
477ac27a0ecSDave Kleikamp 	va_end(args);
478ac27a0ecSDave Kleikamp 
479ac27a0ecSDave Kleikamp 	if (test_opt(sb, ERRORS_PANIC))
480617ba13bSMingming Cao 		panic("EXT4-fs panic from previous error\n");
481ac27a0ecSDave Kleikamp 
482ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
483ac27a0ecSDave Kleikamp 		return;
484ac27a0ecSDave Kleikamp 
485b31e1552SEric Sandeen 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
486617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
487ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_RDONLY;
4884ab2f15bSTheodore Ts'o 	EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
489ef2cabf7SHidehiro Kawai 	if (EXT4_SB(sb)->s_journal)
490dab291afSMingming Cao 		jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
491ac27a0ecSDave Kleikamp }
492ac27a0ecSDave Kleikamp 
493b31e1552SEric Sandeen void ext4_msg (struct super_block * sb, const char *prefix,
494b31e1552SEric Sandeen 		   const char *fmt, ...)
495b31e1552SEric Sandeen {
496b31e1552SEric Sandeen 	va_list args;
497b31e1552SEric Sandeen 
498b31e1552SEric Sandeen 	va_start(args, fmt);
499b31e1552SEric Sandeen 	printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
500b31e1552SEric Sandeen 	vprintk(fmt, args);
501b31e1552SEric Sandeen 	printk("\n");
502b31e1552SEric Sandeen 	va_end(args);
503b31e1552SEric Sandeen }
504b31e1552SEric Sandeen 
50512062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
506ac27a0ecSDave Kleikamp 		  const char *fmt, ...)
507ac27a0ecSDave Kleikamp {
508ac27a0ecSDave Kleikamp 	va_list args;
509ac27a0ecSDave Kleikamp 
510ac27a0ecSDave Kleikamp 	va_start(args, fmt);
511617ba13bSMingming Cao 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
512ac27a0ecSDave Kleikamp 	       sb->s_id, function);
513ac27a0ecSDave Kleikamp 	vprintk(fmt, args);
514ac27a0ecSDave Kleikamp 	printk("\n");
515ac27a0ecSDave Kleikamp 	va_end(args);
516ac27a0ecSDave Kleikamp }
517ac27a0ecSDave Kleikamp 
518e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
519e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
520e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
521e29136f8STheodore Ts'o 			     const char *fmt, ...)
5225d1b1b3fSAneesh Kumar K.V __releases(bitlock)
5235d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
5245d1b1b3fSAneesh Kumar K.V {
5255d1b1b3fSAneesh Kumar K.V 	va_list args;
5265d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5275d1b1b3fSAneesh Kumar K.V 
5285d1b1b3fSAneesh Kumar K.V 	va_start(args, fmt);
529e29136f8STheodore Ts'o 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u",
530e29136f8STheodore Ts'o 	       sb->s_id, function, line, grp);
531e29136f8STheodore Ts'o 	if (ino)
532e29136f8STheodore Ts'o 		printk("inode %lu: ", ino);
533e29136f8STheodore Ts'o 	if (block)
534e29136f8STheodore Ts'o 		printk("block %llu:", (unsigned long long) block);
5355d1b1b3fSAneesh Kumar K.V 	vprintk(fmt, args);
5365d1b1b3fSAneesh Kumar K.V 	printk("\n");
5375d1b1b3fSAneesh Kumar K.V 	va_end(args);
5385d1b1b3fSAneesh Kumar K.V 
5395d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
5405d1b1b3fSAneesh Kumar K.V 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5415d1b1b3fSAneesh Kumar K.V 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
542e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
5435d1b1b3fSAneesh Kumar K.V 		return;
5445d1b1b3fSAneesh Kumar K.V 	}
5455d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
5465d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
5475d1b1b3fSAneesh Kumar K.V 	/*
5485d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
5495d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
5505d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
5515d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
5525d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
5535d1b1b3fSAneesh Kumar K.V 	 * ext4_grp_locked_error() to distinguish beween the
5545d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
5555d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
5565d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
5575d1b1b3fSAneesh Kumar K.V 	 */
5585d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
5595d1b1b3fSAneesh Kumar K.V 	return;
5605d1b1b3fSAneesh Kumar K.V }
5615d1b1b3fSAneesh Kumar K.V 
562617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
563ac27a0ecSDave Kleikamp {
564617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
565ac27a0ecSDave Kleikamp 
566617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
567ac27a0ecSDave Kleikamp 		return;
568ac27a0ecSDave Kleikamp 
56912062dddSEric Sandeen 	ext4_warning(sb,
570ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
571ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
572617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
573ac27a0ecSDave Kleikamp 
574617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
575617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
576617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
577ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
578ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
579ac27a0ecSDave Kleikamp 
580ac27a0ecSDave Kleikamp 	/*
581ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
582ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
583ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
584ac27a0ecSDave Kleikamp 	 */
585ac27a0ecSDave Kleikamp }
586ac27a0ecSDave Kleikamp 
587ac27a0ecSDave Kleikamp /*
588ac27a0ecSDave Kleikamp  * Open the external journal device
589ac27a0ecSDave Kleikamp  */
590b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
591ac27a0ecSDave Kleikamp {
592ac27a0ecSDave Kleikamp 	struct block_device *bdev;
593ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
594ac27a0ecSDave Kleikamp 
595ac27a0ecSDave Kleikamp 	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
596ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
597ac27a0ecSDave Kleikamp 		goto fail;
598ac27a0ecSDave Kleikamp 	return bdev;
599ac27a0ecSDave Kleikamp 
600ac27a0ecSDave Kleikamp fail:
601b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
602ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
603ac27a0ecSDave Kleikamp 	return NULL;
604ac27a0ecSDave Kleikamp }
605ac27a0ecSDave Kleikamp 
606ac27a0ecSDave Kleikamp /*
607ac27a0ecSDave Kleikamp  * Release the journal device
608ac27a0ecSDave Kleikamp  */
609617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev)
610ac27a0ecSDave Kleikamp {
611ac27a0ecSDave Kleikamp 	bd_release(bdev);
6129a1c3542SAl Viro 	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
613ac27a0ecSDave Kleikamp }
614ac27a0ecSDave Kleikamp 
615617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
616ac27a0ecSDave Kleikamp {
617ac27a0ecSDave Kleikamp 	struct block_device *bdev;
618ac27a0ecSDave Kleikamp 	int ret = -ENODEV;
619ac27a0ecSDave Kleikamp 
620ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
621ac27a0ecSDave Kleikamp 	if (bdev) {
622617ba13bSMingming Cao 		ret = ext4_blkdev_put(bdev);
623ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
624ac27a0ecSDave Kleikamp 	}
625ac27a0ecSDave Kleikamp 	return ret;
626ac27a0ecSDave Kleikamp }
627ac27a0ecSDave Kleikamp 
628ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
629ac27a0ecSDave Kleikamp {
630617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
631ac27a0ecSDave Kleikamp }
632ac27a0ecSDave Kleikamp 
633617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
634ac27a0ecSDave Kleikamp {
635ac27a0ecSDave Kleikamp 	struct list_head *l;
636ac27a0ecSDave Kleikamp 
637b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
638ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
639ac27a0ecSDave Kleikamp 
640ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
641ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
642ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
643ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
644ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
645ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
646ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
647ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
648ac27a0ecSDave Kleikamp 	}
649ac27a0ecSDave Kleikamp }
650ac27a0ecSDave Kleikamp 
651617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
652ac27a0ecSDave Kleikamp {
653617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
654617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
655ef2cabf7SHidehiro Kawai 	int i, err;
656ac27a0ecSDave Kleikamp 
657e0ccfd95SChristoph Hellwig 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
658e0ccfd95SChristoph Hellwig 
6594c0425ffSMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
6604c0425ffSMingming Cao 	destroy_workqueue(sbi->dio_unwritten_wq);
6614c0425ffSMingming Cao 
662a9e220f8SAl Viro 	lock_super(sb);
6636cfd0148SChristoph Hellwig 	lock_kernel();
6648c85e125SChristoph Hellwig 	if (sb->s_dirt)
665ebc1ac16SChristoph Hellwig 		ext4_commit_super(sb, 1);
6668c85e125SChristoph Hellwig 
6670390131bSFrank Mayhar 	if (sbi->s_journal) {
668ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
66947b4a50bSJan Kara 		sbi->s_journal = NULL;
670ef2cabf7SHidehiro Kawai 		if (err < 0)
671c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
6720390131bSFrank Mayhar 	}
673d4edac31SJosef Bacik 
674d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
675d4edac31SJosef Bacik 	ext4_mb_release(sb);
676d4edac31SJosef Bacik 	ext4_ext_release(sb);
677d4edac31SJosef Bacik 	ext4_xattr_put_super(sb);
678d4edac31SJosef Bacik 
679ac27a0ecSDave Kleikamp 	if (!(sb->s_flags & MS_RDONLY)) {
680617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
681ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
682e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
683ac27a0ecSDave Kleikamp 	}
684240799cdSTheodore Ts'o 	if (sbi->s_proc) {
6859f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
686240799cdSTheodore Ts'o 	}
6873197ebdbSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
688ac27a0ecSDave Kleikamp 
689ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
690ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
691ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
692c5ca7c76STheodore Ts'o 	if (is_vmalloc_addr(sbi->s_flex_groups))
693c5ca7c76STheodore Ts'o 		vfree(sbi->s_flex_groups);
694c5ca7c76STheodore Ts'o 	else
695772cb7c8SJose R. Santos 		kfree(sbi->s_flex_groups);
696ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
697ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
698ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
6996bc6e63fSAneesh Kumar K.V 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
700ac27a0ecSDave Kleikamp 	brelse(sbi->s_sbh);
701ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
702ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
703ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
704ac27a0ecSDave Kleikamp #endif
705ac27a0ecSDave Kleikamp 
706ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
707ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
708ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
709ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
710ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
711ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
712ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
713ac27a0ecSDave Kleikamp 
714f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
715ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
716ac27a0ecSDave Kleikamp 		/*
717ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
718ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
719ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
720ac27a0ecSDave Kleikamp 		 */
721ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
722f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
723617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
724ac27a0ecSDave Kleikamp 	}
725ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
7263197ebdbSTheodore Ts'o 	/*
7273197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
7283197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
7293197ebdbSTheodore Ts'o 	 */
7303197ebdbSTheodore Ts'o 	unlock_kernel();
7313197ebdbSTheodore Ts'o 	unlock_super(sb);
7323197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
7333197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
734705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
735ac27a0ecSDave Kleikamp 	kfree(sbi);
736ac27a0ecSDave Kleikamp }
737ac27a0ecSDave Kleikamp 
738e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
739ac27a0ecSDave Kleikamp 
740ac27a0ecSDave Kleikamp /*
741ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
742ac27a0ecSDave Kleikamp  */
743617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
744ac27a0ecSDave Kleikamp {
745617ba13bSMingming Cao 	struct ext4_inode_info *ei;
746ac27a0ecSDave Kleikamp 
747e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
748ac27a0ecSDave Kleikamp 	if (!ei)
749ac27a0ecSDave Kleikamp 		return NULL;
7500b8e58a1SAndreas Dilger 
751ac27a0ecSDave Kleikamp 	ei->vfs_inode.i_version = 1;
75291246c00SAneesh Kumar K.V 	ei->vfs_inode.i_data.writeback_index = 0;
753a86c6181SAlex Tomas 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
754c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
755c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
7560390131bSFrank Mayhar 	/*
7570390131bSFrank Mayhar 	 * Note:  We can be called before EXT4_SB(sb)->s_journal is set,
7580390131bSFrank Mayhar 	 * therefore it can be null here.  Don't check it, just initialize
7590390131bSFrank Mayhar 	 * jinode.
7600390131bSFrank Mayhar 	 */
761678aaf48SJan Kara 	jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
762d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
763d2a17637SMingming Cao 	ei->i_reserved_meta_blocks = 0;
764d2a17637SMingming Cao 	ei->i_allocated_meta_blocks = 0;
7659d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
766d2a17637SMingming Cao 	ei->i_delalloc_reserved_flag = 0;
767d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
768a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
769a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
770a9e7f447SDmitry Monakhov #endif
771c7064ef1SJiaying Zhang 	INIT_LIST_HEAD(&ei->i_completed_io_list);
772744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
7738d5d02e6SMingming Cao 	ei->cur_aio_dio = NULL;
774b436b9beSJan Kara 	ei->i_sync_tid = 0;
775b436b9beSJan Kara 	ei->i_datasync_tid = 0;
7760b8e58a1SAndreas Dilger 
777ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
778ac27a0ecSDave Kleikamp }
779ac27a0ecSDave Kleikamp 
780617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
781ac27a0ecSDave Kleikamp {
7829f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
783b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
784b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
785b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
7869f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
7879f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
7889f7dd93dSVasily Averin 				true);
7899f7dd93dSVasily Averin 		dump_stack();
7909f7dd93dSVasily Averin 	}
791617ba13bSMingming Cao 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
792ac27a0ecSDave Kleikamp }
793ac27a0ecSDave Kleikamp 
79451cc5068SAlexey Dobriyan static void init_once(void *foo)
795ac27a0ecSDave Kleikamp {
796617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
797ac27a0ecSDave Kleikamp 
798ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
79903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
800ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
801ac27a0ecSDave Kleikamp #endif
8020e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
803ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
804ac27a0ecSDave Kleikamp }
805ac27a0ecSDave Kleikamp 
806ac27a0ecSDave Kleikamp static int init_inodecache(void)
807ac27a0ecSDave Kleikamp {
808617ba13bSMingming Cao 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
809617ba13bSMingming Cao 					     sizeof(struct ext4_inode_info),
810ac27a0ecSDave Kleikamp 					     0, (SLAB_RECLAIM_ACCOUNT|
811ac27a0ecSDave Kleikamp 						SLAB_MEM_SPREAD),
81220c2df83SPaul Mundt 					     init_once);
813617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
814ac27a0ecSDave Kleikamp 		return -ENOMEM;
815ac27a0ecSDave Kleikamp 	return 0;
816ac27a0ecSDave Kleikamp }
817ac27a0ecSDave Kleikamp 
818ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
819ac27a0ecSDave Kleikamp {
820617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
821ac27a0ecSDave Kleikamp }
822ac27a0ecSDave Kleikamp 
823617ba13bSMingming Cao static void ext4_clear_inode(struct inode *inode)
824ac27a0ecSDave Kleikamp {
8259f754758SChristoph Hellwig 	dquot_drop(inode);
826c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
8270390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode))
828678aaf48SJan Kara 		jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
829678aaf48SJan Kara 				       &EXT4_I(inode)->jinode);
830ac27a0ecSDave Kleikamp }
831ac27a0ecSDave Kleikamp 
8322b2d6d01STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
8332b2d6d01STheodore Ts'o 					   struct super_block *sb)
834ac27a0ecSDave Kleikamp {
835ac27a0ecSDave Kleikamp #if defined(CONFIG_QUOTA)
836617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
837ac27a0ecSDave Kleikamp 
8385a20bdfcSJan Kara 	if (sbi->s_jquota_fmt) {
8395a20bdfcSJan Kara 		char *fmtname = "";
8405a20bdfcSJan Kara 
8415a20bdfcSJan Kara 		switch (sbi->s_jquota_fmt) {
8425a20bdfcSJan Kara 		case QFMT_VFS_OLD:
8435a20bdfcSJan Kara 			fmtname = "vfsold";
8445a20bdfcSJan Kara 			break;
8455a20bdfcSJan Kara 		case QFMT_VFS_V0:
8465a20bdfcSJan Kara 			fmtname = "vfsv0";
8475a20bdfcSJan Kara 			break;
8485a20bdfcSJan Kara 		case QFMT_VFS_V1:
8495a20bdfcSJan Kara 			fmtname = "vfsv1";
8505a20bdfcSJan Kara 			break;
8515a20bdfcSJan Kara 		}
8525a20bdfcSJan Kara 		seq_printf(seq, ",jqfmt=%s", fmtname);
8535a20bdfcSJan Kara 	}
854ac27a0ecSDave Kleikamp 
855ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA])
856ac27a0ecSDave Kleikamp 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
857ac27a0ecSDave Kleikamp 
858ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[GRPQUOTA])
859ac27a0ecSDave Kleikamp 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
860ac27a0ecSDave Kleikamp 
861482a7425SDmitry Monakhov 	if (test_opt(sb, USRQUOTA))
862ac27a0ecSDave Kleikamp 		seq_puts(seq, ",usrquota");
863ac27a0ecSDave Kleikamp 
864482a7425SDmitry Monakhov 	if (test_opt(sb, GRPQUOTA))
865ac27a0ecSDave Kleikamp 		seq_puts(seq, ",grpquota");
866ac27a0ecSDave Kleikamp #endif
867ac27a0ecSDave Kleikamp }
868ac27a0ecSDave Kleikamp 
869d9c9bef1SMiklos Szeredi /*
870d9c9bef1SMiklos Szeredi  * Show an option if
871d9c9bef1SMiklos Szeredi  *  - it's set to a non-default value OR
872d9c9bef1SMiklos Szeredi  *  - if the per-sb default is different from the global default
873d9c9bef1SMiklos Szeredi  */
874617ba13bSMingming Cao static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
875ac27a0ecSDave Kleikamp {
876aa22df2cSAneesh Kumar K.V 	int def_errors;
877aa22df2cSAneesh Kumar K.V 	unsigned long def_mount_opts;
878ac27a0ecSDave Kleikamp 	struct super_block *sb = vfs->mnt_sb;
879d9c9bef1SMiklos Szeredi 	struct ext4_sb_info *sbi = EXT4_SB(sb);
880d9c9bef1SMiklos Szeredi 	struct ext4_super_block *es = sbi->s_es;
881d9c9bef1SMiklos Szeredi 
882d9c9bef1SMiklos Szeredi 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
883aa22df2cSAneesh Kumar K.V 	def_errors     = le16_to_cpu(es->s_errors);
884d9c9bef1SMiklos Szeredi 
885d9c9bef1SMiklos Szeredi 	if (sbi->s_sb_block != 1)
886d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
887d9c9bef1SMiklos Szeredi 	if (test_opt(sb, MINIX_DF))
888d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",minixdf");
889aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
890d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",grpid");
891d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
892d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nogrpid");
893d9c9bef1SMiklos Szeredi 	if (sbi->s_resuid != EXT4_DEF_RESUID ||
894d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
895d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
896d9c9bef1SMiklos Szeredi 	}
897d9c9bef1SMiklos Szeredi 	if (sbi->s_resgid != EXT4_DEF_RESGID ||
898d9c9bef1SMiklos Szeredi 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
899d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
900d9c9bef1SMiklos Szeredi 	}
901bb4f397aSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_RO)) {
902d9c9bef1SMiklos Szeredi 		if (def_errors == EXT4_ERRORS_PANIC ||
903bb4f397aSAneesh Kumar K.V 		    def_errors == EXT4_ERRORS_CONTINUE) {
904d9c9bef1SMiklos Szeredi 			seq_puts(seq, ",errors=remount-ro");
905bb4f397aSAneesh Kumar K.V 		}
906bb4f397aSAneesh Kumar K.V 	}
907aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
908bb4f397aSAneesh Kumar K.V 		seq_puts(seq, ",errors=continue");
909aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
910d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",errors=panic");
911aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
912d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouid32");
913aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
914d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",debug");
915d9c9bef1SMiklos Szeredi 	if (test_opt(sb, OLDALLOC))
916d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",oldalloc");
91703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
918aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, XATTR_USER) &&
919aa22df2cSAneesh Kumar K.V 		!(def_mount_opts & EXT4_DEFM_XATTR_USER))
920d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",user_xattr");
921d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, XATTR_USER) &&
922d9c9bef1SMiklos Szeredi 	    (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
923d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",nouser_xattr");
924d9c9bef1SMiklos Szeredi 	}
925d9c9bef1SMiklos Szeredi #endif
92603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
927aa22df2cSAneesh Kumar K.V 	if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
928d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",acl");
929d9c9bef1SMiklos Szeredi 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
930d9c9bef1SMiklos Szeredi 		seq_puts(seq, ",noacl");
931d9c9bef1SMiklos Szeredi #endif
93230773840STheodore Ts'o 	if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
933d9c9bef1SMiklos Szeredi 		seq_printf(seq, ",commit=%u",
934d9c9bef1SMiklos Szeredi 			   (unsigned) (sbi->s_commit_interval / HZ));
935d9c9bef1SMiklos Szeredi 	}
93630773840STheodore Ts'o 	if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
93730773840STheodore Ts'o 		seq_printf(seq, ",min_batch_time=%u",
93830773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
93930773840STheodore Ts'o 	}
94030773840STheodore Ts'o 	if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
94130773840STheodore Ts'o 		seq_printf(seq, ",max_batch_time=%u",
94230773840STheodore Ts'o 			   (unsigned) sbi->s_min_batch_time);
94330773840STheodore Ts'o 	}
94430773840STheodore Ts'o 
945571640caSEric Sandeen 	/*
946571640caSEric Sandeen 	 * We're changing the default of barrier mount option, so
947571640caSEric Sandeen 	 * let's always display its mount state so it's clear what its
948571640caSEric Sandeen 	 * status is.
949571640caSEric Sandeen 	 */
950571640caSEric Sandeen 	seq_puts(seq, ",barrier=");
951571640caSEric Sandeen 	seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
952cd0b6a39STheodore Ts'o 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
953cd0b6a39STheodore Ts'o 		seq_puts(seq, ",journal_async_commit");
95439a4badeSJan Kara 	else if (test_opt(sb, JOURNAL_CHECKSUM))
95539a4badeSJan Kara 		seq_puts(seq, ",journal_checksum");
95625ec56b5SJean Noel Cordenner 	if (test_opt(sb, I_VERSION))
95725ec56b5SJean Noel Cordenner 		seq_puts(seq, ",i_version");
958dd919b98SAneesh Kumar K.V 	if (!test_opt(sb, DELALLOC))
959dd919b98SAneesh Kumar K.V 		seq_puts(seq, ",nodelalloc");
960dd919b98SAneesh Kumar K.V 
961ac27a0ecSDave Kleikamp 
962cb45bbe4SMiklos Szeredi 	if (sbi->s_stripe)
963cb45bbe4SMiklos Szeredi 		seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
964aa22df2cSAneesh Kumar K.V 	/*
965aa22df2cSAneesh Kumar K.V 	 * journal mode get enabled in different ways
966aa22df2cSAneesh Kumar K.V 	 * So just print the value even if we didn't specify it
967aa22df2cSAneesh Kumar K.V 	 */
968617ba13bSMingming Cao 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
969ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=journal");
970617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
971ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=ordered");
972617ba13bSMingming Cao 	else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
973ac27a0ecSDave Kleikamp 		seq_puts(seq, ",data=writeback");
974ac27a0ecSDave Kleikamp 
975240799cdSTheodore Ts'o 	if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
976240799cdSTheodore Ts'o 		seq_printf(seq, ",inode_readahead_blks=%u",
977240799cdSTheodore Ts'o 			   sbi->s_inode_readahead_blks);
978240799cdSTheodore Ts'o 
9795bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
9805bf5683aSHidehiro Kawai 		seq_puts(seq, ",data_err=abort");
9815bf5683aSHidehiro Kawai 
982afd4672dSTheodore Ts'o 	if (test_opt(sb, NO_AUTO_DA_ALLOC))
98306705bffSTheodore Ts'o 		seq_puts(seq, ",noauto_da_alloc");
984afd4672dSTheodore Ts'o 
9855328e635SEric Sandeen 	if (test_opt(sb, DISCARD))
9865328e635SEric Sandeen 		seq_puts(seq, ",discard");
9875328e635SEric Sandeen 
988e3bb52aeSEric Sandeen 	if (test_opt(sb, NOLOAD))
989e3bb52aeSEric Sandeen 		seq_puts(seq, ",norecovery");
990e3bb52aeSEric Sandeen 
991744692dcSJiaying Zhang 	if (test_opt(sb, DIOREAD_NOLOCK))
992744692dcSJiaying Zhang 		seq_puts(seq, ",dioread_nolock");
993744692dcSJiaying Zhang 
994617ba13bSMingming Cao 	ext4_show_quota_options(seq, sb);
9950b8e58a1SAndreas Dilger 
996ac27a0ecSDave Kleikamp 	return 0;
997ac27a0ecSDave Kleikamp }
998ac27a0ecSDave Kleikamp 
9991b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
10001b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1001ac27a0ecSDave Kleikamp {
1002ac27a0ecSDave Kleikamp 	struct inode *inode;
1003ac27a0ecSDave Kleikamp 
1004617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1005ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1006617ba13bSMingming Cao 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1007ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1008ac27a0ecSDave Kleikamp 
1009ac27a0ecSDave Kleikamp 	/* iget isn't really right if the inode is currently unallocated!!
1010ac27a0ecSDave Kleikamp 	 *
1011617ba13bSMingming Cao 	 * ext4_read_inode will return a bad_inode if the inode had been
1012ac27a0ecSDave Kleikamp 	 * deleted, so we should be safe.
1013ac27a0ecSDave Kleikamp 	 *
1014ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1015ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1016ac27a0ecSDave Kleikamp 	 */
10171d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
10181d1fe1eeSDavid Howells 	if (IS_ERR(inode))
10191d1fe1eeSDavid Howells 		return ERR_CAST(inode);
10201d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1021ac27a0ecSDave Kleikamp 		iput(inode);
1022ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1023ac27a0ecSDave Kleikamp 	}
10241b961ac0SChristoph Hellwig 
10251b961ac0SChristoph Hellwig 	return inode;
1026ac27a0ecSDave Kleikamp }
10271b961ac0SChristoph Hellwig 
10281b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
10291b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10301b961ac0SChristoph Hellwig {
10311b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
10321b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
10331b961ac0SChristoph Hellwig }
10341b961ac0SChristoph Hellwig 
10351b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
10361b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
10371b961ac0SChristoph Hellwig {
10381b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
10391b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1040ac27a0ecSDave Kleikamp }
1041ac27a0ecSDave Kleikamp 
1042c39a7f84SToshiyuki Okajima /*
1043c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1044c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1045c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1046c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1047c39a7f84SToshiyuki Okajima  */
10480b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
10490b8e58a1SAndreas Dilger 				 gfp_t wait)
1050c39a7f84SToshiyuki Okajima {
1051c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1052c39a7f84SToshiyuki Okajima 
1053c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1054c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1055c39a7f84SToshiyuki Okajima 		return 0;
1056c39a7f84SToshiyuki Okajima 	if (journal)
1057c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1058c39a7f84SToshiyuki Okajima 							wait & ~__GFP_WAIT);
1059c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1060c39a7f84SToshiyuki Okajima }
1061c39a7f84SToshiyuki Okajima 
1062ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1063ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1064ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1065ac27a0ecSDave Kleikamp 
1066617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1067617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1068617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1069617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1070617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
10716f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1072307ae18aSChristoph Hellwig 				char *path);
1073617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1074617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1075ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1076617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1077ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
1078ac27a0ecSDave Kleikamp 
107961e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
1080a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
108160e58e0fSMingming Cao 	.get_reserved_space = ext4_get_reserved_space,
1082a9e7f447SDmitry Monakhov #endif
1083617ba13bSMingming Cao 	.write_dquot	= ext4_write_dquot,
1084617ba13bSMingming Cao 	.acquire_dquot	= ext4_acquire_dquot,
1085617ba13bSMingming Cao 	.release_dquot	= ext4_release_dquot,
1086617ba13bSMingming Cao 	.mark_dirty	= ext4_mark_dquot_dirty,
1087a5b5ee32SJan Kara 	.write_info	= ext4_write_info,
1088a5b5ee32SJan Kara 	.alloc_dquot	= dquot_alloc,
1089a5b5ee32SJan Kara 	.destroy_dquot	= dquot_destroy,
1090ac27a0ecSDave Kleikamp };
1091ac27a0ecSDave Kleikamp 
10920d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1093617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1094287a8095SChristoph Hellwig 	.quota_off	= dquot_quota_off,
1095287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
1096287a8095SChristoph Hellwig 	.get_info	= dquot_get_dqinfo,
1097287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1098287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
1099287a8095SChristoph Hellwig 	.set_dqblk	= dquot_set_dqblk
1100ac27a0ecSDave Kleikamp };
1101ac27a0ecSDave Kleikamp #endif
1102ac27a0ecSDave Kleikamp 
1103ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1104617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1105617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1106617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1107617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
1108617ba13bSMingming Cao 	.delete_inode	= ext4_delete_inode,
1109617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1110617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1111c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1112c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1113617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1114617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1115617ba13bSMingming Cao 	.clear_inode	= ext4_clear_inode,
1116617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1117ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1118617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1119617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
1120ac27a0ecSDave Kleikamp #endif
1121c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1122ac27a0ecSDave Kleikamp };
1123ac27a0ecSDave Kleikamp 
11249ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = {
11259ca92389STheodore Ts'o 	.alloc_inode	= ext4_alloc_inode,
11269ca92389STheodore Ts'o 	.destroy_inode	= ext4_destroy_inode,
11279ca92389STheodore Ts'o 	.write_inode	= ext4_write_inode,
11289ca92389STheodore Ts'o 	.dirty_inode	= ext4_dirty_inode,
11299ca92389STheodore Ts'o 	.delete_inode	= ext4_delete_inode,
11309ca92389STheodore Ts'o 	.write_super	= ext4_write_super,
11319ca92389STheodore Ts'o 	.put_super	= ext4_put_super,
11329ca92389STheodore Ts'o 	.statfs		= ext4_statfs,
11339ca92389STheodore Ts'o 	.remount_fs	= ext4_remount,
11349ca92389STheodore Ts'o 	.clear_inode	= ext4_clear_inode,
11359ca92389STheodore Ts'o 	.show_options	= ext4_show_options,
11369ca92389STheodore Ts'o #ifdef CONFIG_QUOTA
11379ca92389STheodore Ts'o 	.quota_read	= ext4_quota_read,
11389ca92389STheodore Ts'o 	.quota_write	= ext4_quota_write,
11399ca92389STheodore Ts'o #endif
11409ca92389STheodore Ts'o 	.bdev_try_to_free_page = bdev_try_to_free_page,
11419ca92389STheodore Ts'o };
11429ca92389STheodore Ts'o 
114339655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
11441b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
11451b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1146617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1147ac27a0ecSDave Kleikamp };
1148ac27a0ecSDave Kleikamp 
1149ac27a0ecSDave Kleikamp enum {
1150ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1151ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
115201436ef2STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1153ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
115406705bffSTheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
115530773840STheodore Ts'o 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1156c3191067STheodore Ts'o 	Opt_journal_update, Opt_journal_dev,
1157818d276cSGirish Shilamkar 	Opt_journal_checksum, Opt_journal_async_commit,
1158ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1159296c355cSTheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore,
1160ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
11615a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
11625a20bdfcSJan Kara 	Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
11635a20bdfcSJan Kara 	Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
116401436ef2STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc,
11656fd058f7STheodore Ts'o 	Opt_block_validity, Opt_noblock_validity,
11665328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1167744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
11685328e635SEric Sandeen 	Opt_discard, Opt_nodiscard,
1169ac27a0ecSDave Kleikamp };
1170ac27a0ecSDave Kleikamp 
1171a447c093SSteven Whitehouse static const match_table_t tokens = {
1172ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1173ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1174ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1175ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1176ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1177ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1178ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1179ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1180ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1181ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1182ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1183ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1184ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1185ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
1186ac27a0ecSDave Kleikamp 	{Opt_oldalloc, "oldalloc"},
1187ac27a0ecSDave Kleikamp 	{Opt_orlov, "orlov"},
1188ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1189ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1190ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1191ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1192ac27a0ecSDave Kleikamp 	{Opt_noload, "noload"},
1193e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
1194ac27a0ecSDave Kleikamp 	{Opt_nobh, "nobh"},
1195ac27a0ecSDave Kleikamp 	{Opt_bh, "bh"},
1196ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
119730773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
119830773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1199ac27a0ecSDave Kleikamp 	{Opt_journal_update, "journal=update"},
1200ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1201818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1202818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1203ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1204ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1205ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1206ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
12075bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
12085bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1209ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1210ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1211ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1212ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1213ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1214ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
12155a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1216ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1217ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1218ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1219ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
1220ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
122106705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
122206705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
122325ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1224c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
1225ac27a0ecSDave Kleikamp 	{Opt_resize, "resize"},
122664769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1227dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
12286fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
12296fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1230240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1231b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1232afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
123306705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
123406705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1235744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1236744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
12375328e635SEric Sandeen 	{Opt_discard, "discard"},
12385328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1239f3f12faaSJosef Bacik 	{Opt_err, NULL},
1240ac27a0ecSDave Kleikamp };
1241ac27a0ecSDave Kleikamp 
1242617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1243ac27a0ecSDave Kleikamp {
1244617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1245ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1246ac27a0ecSDave Kleikamp 
1247ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1248ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
12490b8e58a1SAndreas Dilger 
1250ac27a0ecSDave Kleikamp 	options += 3;
12510b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1252ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1253ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
12544776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1255ac27a0ecSDave Kleikamp 		       (char *) *data);
1256ac27a0ecSDave Kleikamp 		return 1;
1257ac27a0ecSDave Kleikamp 	}
1258ac27a0ecSDave Kleikamp 	if (*options == ',')
1259ac27a0ecSDave Kleikamp 		options++;
1260ac27a0ecSDave Kleikamp 	*data = (void *) options;
12610b8e58a1SAndreas Dilger 
1262ac27a0ecSDave Kleikamp 	return sb_block;
1263ac27a0ecSDave Kleikamp }
1264ac27a0ecSDave Kleikamp 
1265b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1266437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1267437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1268b3881f74STheodore Ts'o 
126956c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
127056c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
127156c50f11SDmitry Monakhov {
127256c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
127356c50f11SDmitry Monakhov 	char *qname;
127456c50f11SDmitry Monakhov 
127556c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
127656c50f11SDmitry Monakhov 		!sbi->s_qf_names[qtype]) {
127756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
127856c50f11SDmitry Monakhov 			"Cannot change journaled "
127956c50f11SDmitry Monakhov 			"quota options when quota turned on");
128056c50f11SDmitry Monakhov 		return 0;
128156c50f11SDmitry Monakhov 	}
128256c50f11SDmitry Monakhov 	qname = match_strdup(args);
128356c50f11SDmitry Monakhov 	if (!qname) {
128456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
128556c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
128656c50f11SDmitry Monakhov 		return 0;
128756c50f11SDmitry Monakhov 	}
128856c50f11SDmitry Monakhov 	if (sbi->s_qf_names[qtype] &&
128956c50f11SDmitry Monakhov 		strcmp(sbi->s_qf_names[qtype], qname)) {
129056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
129156c50f11SDmitry Monakhov 			"%s quota file already specified", QTYPE2NAME(qtype));
129256c50f11SDmitry Monakhov 		kfree(qname);
129356c50f11SDmitry Monakhov 		return 0;
129456c50f11SDmitry Monakhov 	}
129556c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = qname;
129656c50f11SDmitry Monakhov 	if (strchr(sbi->s_qf_names[qtype], '/')) {
129756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
129856c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
129956c50f11SDmitry Monakhov 		kfree(sbi->s_qf_names[qtype]);
130056c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype] = NULL;
130156c50f11SDmitry Monakhov 		return 0;
130256c50f11SDmitry Monakhov 	}
130356c50f11SDmitry Monakhov 	set_opt(sbi->s_mount_opt, QUOTA);
130456c50f11SDmitry Monakhov 	return 1;
130556c50f11SDmitry Monakhov }
130656c50f11SDmitry Monakhov 
130756c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
130856c50f11SDmitry Monakhov {
130956c50f11SDmitry Monakhov 
131056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
131156c50f11SDmitry Monakhov 
131256c50f11SDmitry Monakhov 	if (sb_any_quota_loaded(sb) &&
131356c50f11SDmitry Monakhov 		sbi->s_qf_names[qtype]) {
131456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
131556c50f11SDmitry Monakhov 			" when quota turned on");
131656c50f11SDmitry Monakhov 		return 0;
131756c50f11SDmitry Monakhov 	}
131856c50f11SDmitry Monakhov 	/*
131956c50f11SDmitry Monakhov 	 * The space will be released later when all options are confirmed
132056c50f11SDmitry Monakhov 	 * to be correct
132156c50f11SDmitry Monakhov 	 */
132256c50f11SDmitry Monakhov 	sbi->s_qf_names[qtype] = NULL;
132356c50f11SDmitry Monakhov 	return 1;
132456c50f11SDmitry Monakhov }
132556c50f11SDmitry Monakhov #endif
132656c50f11SDmitry Monakhov 
1327ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
1328c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
1329b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
1330617ba13bSMingming Cao 			 ext4_fsblk_t *n_blocks_count, int is_remount)
1331ac27a0ecSDave Kleikamp {
1332617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1333ac27a0ecSDave Kleikamp 	char *p;
1334ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
1335ac27a0ecSDave Kleikamp 	int data_opt = 0;
1336ac27a0ecSDave Kleikamp 	int option;
1337ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
133856c50f11SDmitry Monakhov 	int qfmt;
1339ac27a0ecSDave Kleikamp #endif
1340ac27a0ecSDave Kleikamp 
1341ac27a0ecSDave Kleikamp 	if (!options)
1342ac27a0ecSDave Kleikamp 		return 1;
1343ac27a0ecSDave Kleikamp 
1344ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
1345ac27a0ecSDave Kleikamp 		int token;
1346ac27a0ecSDave Kleikamp 		if (!*p)
1347ac27a0ecSDave Kleikamp 			continue;
1348ac27a0ecSDave Kleikamp 
134915121c18SEric Sandeen 		/*
135015121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
135115121c18SEric Sandeen 		 * found; some options take optional arguments.
135215121c18SEric Sandeen 		 */
135315121c18SEric Sandeen 		args[0].to = args[0].from = 0;
1354ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
1355ac27a0ecSDave Kleikamp 		switch (token) {
1356ac27a0ecSDave Kleikamp 		case Opt_bsd_df:
1357437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1358ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, MINIX_DF);
1359ac27a0ecSDave Kleikamp 			break;
1360ac27a0ecSDave Kleikamp 		case Opt_minix_df:
1361437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1362ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, MINIX_DF);
1363437ca0fdSDmitry Monakhov 
1364ac27a0ecSDave Kleikamp 			break;
1365ac27a0ecSDave Kleikamp 		case Opt_grpid:
1366437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1367ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPID);
1368437ca0fdSDmitry Monakhov 
1369ac27a0ecSDave Kleikamp 			break;
1370ac27a0ecSDave Kleikamp 		case Opt_nogrpid:
1371437ca0fdSDmitry Monakhov 			ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
1372ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPID);
1373437ca0fdSDmitry Monakhov 
1374ac27a0ecSDave Kleikamp 			break;
1375ac27a0ecSDave Kleikamp 		case Opt_resuid:
1376ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1377ac27a0ecSDave Kleikamp 				return 0;
1378ac27a0ecSDave Kleikamp 			sbi->s_resuid = option;
1379ac27a0ecSDave Kleikamp 			break;
1380ac27a0ecSDave Kleikamp 		case Opt_resgid:
1381ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1382ac27a0ecSDave Kleikamp 				return 0;
1383ac27a0ecSDave Kleikamp 			sbi->s_resgid = option;
1384ac27a0ecSDave Kleikamp 			break;
1385ac27a0ecSDave Kleikamp 		case Opt_sb:
1386ac27a0ecSDave Kleikamp 			/* handled by get_sb_block() instead of here */
1387ac27a0ecSDave Kleikamp 			/* *sb_block = match_int(&args[0]); */
1388ac27a0ecSDave Kleikamp 			break;
1389ac27a0ecSDave Kleikamp 		case Opt_err_panic:
1390ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1391ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1392ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1393ac27a0ecSDave Kleikamp 			break;
1394ac27a0ecSDave Kleikamp 		case Opt_err_ro:
1395ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1396ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1397ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_RO);
1398ac27a0ecSDave Kleikamp 			break;
1399ac27a0ecSDave Kleikamp 		case Opt_err_cont:
1400ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_RO);
1401ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1402ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ERRORS_CONT);
1403ac27a0ecSDave Kleikamp 			break;
1404ac27a0ecSDave Kleikamp 		case Opt_nouid32:
1405ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NO_UID32);
1406ac27a0ecSDave Kleikamp 			break;
1407ac27a0ecSDave Kleikamp 		case Opt_debug:
1408ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, DEBUG);
1409ac27a0ecSDave Kleikamp 			break;
1410ac27a0ecSDave Kleikamp 		case Opt_oldalloc:
1411ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, OLDALLOC);
1412ac27a0ecSDave Kleikamp 			break;
1413ac27a0ecSDave Kleikamp 		case Opt_orlov:
1414ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, OLDALLOC);
1415ac27a0ecSDave Kleikamp 			break;
141603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
1417ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1418ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, XATTR_USER);
1419ac27a0ecSDave Kleikamp 			break;
1420ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1421ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, XATTR_USER);
1422ac27a0ecSDave Kleikamp 			break;
1423ac27a0ecSDave Kleikamp #else
1424ac27a0ecSDave Kleikamp 		case Opt_user_xattr:
1425ac27a0ecSDave Kleikamp 		case Opt_nouser_xattr:
1426b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1427ac27a0ecSDave Kleikamp 			break;
1428ac27a0ecSDave Kleikamp #endif
142903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
1430ac27a0ecSDave Kleikamp 		case Opt_acl:
1431ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, POSIX_ACL);
1432ac27a0ecSDave Kleikamp 			break;
1433ac27a0ecSDave Kleikamp 		case Opt_noacl:
1434ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
1435ac27a0ecSDave Kleikamp 			break;
1436ac27a0ecSDave Kleikamp #else
1437ac27a0ecSDave Kleikamp 		case Opt_acl:
1438ac27a0ecSDave Kleikamp 		case Opt_noacl:
1439b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1440ac27a0ecSDave Kleikamp 			break;
1441ac27a0ecSDave Kleikamp #endif
1442ac27a0ecSDave Kleikamp 		case Opt_journal_update:
1443ac27a0ecSDave Kleikamp 			/* @@@ FIXME */
1444ac27a0ecSDave Kleikamp 			/* Eventually we will want to be able to create
1445ac27a0ecSDave Kleikamp 			   a journal file here.  For now, only allow the
1446ac27a0ecSDave Kleikamp 			   user to specify an existing inode to be the
1447ac27a0ecSDave Kleikamp 			   journal file. */
1448ac27a0ecSDave Kleikamp 			if (is_remount) {
1449b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1450b31e1552SEric Sandeen 					 "Cannot specify journal on remount");
1451ac27a0ecSDave Kleikamp 				return 0;
1452ac27a0ecSDave Kleikamp 			}
1453ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1454ac27a0ecSDave Kleikamp 			break;
1455ac27a0ecSDave Kleikamp 		case Opt_journal_dev:
1456ac27a0ecSDave Kleikamp 			if (is_remount) {
1457b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1458b31e1552SEric Sandeen 					"Cannot specify journal on remount");
1459ac27a0ecSDave Kleikamp 				return 0;
1460ac27a0ecSDave Kleikamp 			}
1461ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1462ac27a0ecSDave Kleikamp 				return 0;
1463ac27a0ecSDave Kleikamp 			*journal_devnum = option;
1464ac27a0ecSDave Kleikamp 			break;
1465818d276cSGirish Shilamkar 		case Opt_journal_checksum:
1466d4da6c9cSLinus Torvalds 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1467d4da6c9cSLinus Torvalds 			break;
1468818d276cSGirish Shilamkar 		case Opt_journal_async_commit:
1469818d276cSGirish Shilamkar 			set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1470d4da6c9cSLinus Torvalds 			set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1471818d276cSGirish Shilamkar 			break;
1472ac27a0ecSDave Kleikamp 		case Opt_noload:
1473ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, NOLOAD);
1474ac27a0ecSDave Kleikamp 			break;
1475ac27a0ecSDave Kleikamp 		case Opt_commit:
1476ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option))
1477ac27a0ecSDave Kleikamp 				return 0;
1478ac27a0ecSDave Kleikamp 			if (option < 0)
1479ac27a0ecSDave Kleikamp 				return 0;
1480ac27a0ecSDave Kleikamp 			if (option == 0)
1481cd02ff0bSMingming Cao 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1482ac27a0ecSDave Kleikamp 			sbi->s_commit_interval = HZ * option;
1483ac27a0ecSDave Kleikamp 			break;
148430773840STheodore Ts'o 		case Opt_max_batch_time:
148530773840STheodore Ts'o 			if (match_int(&args[0], &option))
148630773840STheodore Ts'o 				return 0;
148730773840STheodore Ts'o 			if (option < 0)
148830773840STheodore Ts'o 				return 0;
148930773840STheodore Ts'o 			if (option == 0)
149030773840STheodore Ts'o 				option = EXT4_DEF_MAX_BATCH_TIME;
149130773840STheodore Ts'o 			sbi->s_max_batch_time = option;
149230773840STheodore Ts'o 			break;
149330773840STheodore Ts'o 		case Opt_min_batch_time:
149430773840STheodore Ts'o 			if (match_int(&args[0], &option))
149530773840STheodore Ts'o 				return 0;
149630773840STheodore Ts'o 			if (option < 0)
149730773840STheodore Ts'o 				return 0;
149830773840STheodore Ts'o 			sbi->s_min_batch_time = option;
149930773840STheodore Ts'o 			break;
1500ac27a0ecSDave Kleikamp 		case Opt_data_journal:
1501617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_JOURNAL_DATA;
1502ac27a0ecSDave Kleikamp 			goto datacheck;
1503ac27a0ecSDave Kleikamp 		case Opt_data_ordered:
1504617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_ORDERED_DATA;
1505ac27a0ecSDave Kleikamp 			goto datacheck;
1506ac27a0ecSDave Kleikamp 		case Opt_data_writeback:
1507617ba13bSMingming Cao 			data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1508ac27a0ecSDave Kleikamp 		datacheck:
1509ac27a0ecSDave Kleikamp 			if (is_remount) {
1510482a7425SDmitry Monakhov 				if (test_opt(sb, DATA_FLAGS) != data_opt) {
1511b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
1512b31e1552SEric Sandeen 						"Cannot change data mode on remount");
1513ac27a0ecSDave Kleikamp 					return 0;
1514ac27a0ecSDave Kleikamp 				}
1515ac27a0ecSDave Kleikamp 			} else {
1516482a7425SDmitry Monakhov 				clear_opt(sbi->s_mount_opt, DATA_FLAGS);
1517ac27a0ecSDave Kleikamp 				sbi->s_mount_opt |= data_opt;
1518ac27a0ecSDave Kleikamp 			}
1519ac27a0ecSDave Kleikamp 			break;
15205bf5683aSHidehiro Kawai 		case Opt_data_err_abort:
15215bf5683aSHidehiro Kawai 			set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
15225bf5683aSHidehiro Kawai 			break;
15235bf5683aSHidehiro Kawai 		case Opt_data_err_ignore:
15245bf5683aSHidehiro Kawai 			clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
15255bf5683aSHidehiro Kawai 			break;
1526ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1527ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
152856c50f11SDmitry Monakhov 			if (!set_qf_name(sb, USRQUOTA, &args[0]))
152956c50f11SDmitry Monakhov 				return 0;
153056c50f11SDmitry Monakhov 			break;
1531ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
153256c50f11SDmitry Monakhov 			if (!set_qf_name(sb, GRPQUOTA, &args[0]))
1533ac27a0ecSDave Kleikamp 				return 0;
1534ac27a0ecSDave Kleikamp 			break;
1535ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
153656c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, USRQUOTA))
1537ac27a0ecSDave Kleikamp 				return 0;
1538ac27a0ecSDave Kleikamp 			break;
153956c50f11SDmitry Monakhov 		case Opt_offgrpjquota:
154056c50f11SDmitry Monakhov 			if (!clear_qf_name(sb, GRPQUOTA))
154156c50f11SDmitry Monakhov 				return 0;
154256c50f11SDmitry Monakhov 			break;
154356c50f11SDmitry Monakhov 
1544ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1545dfc5d03fSJan Kara 			qfmt = QFMT_VFS_OLD;
1546dfc5d03fSJan Kara 			goto set_qf_format;
1547ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
1548dfc5d03fSJan Kara 			qfmt = QFMT_VFS_V0;
15495a20bdfcSJan Kara 			goto set_qf_format;
15505a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
15515a20bdfcSJan Kara 			qfmt = QFMT_VFS_V1;
1552dfc5d03fSJan Kara set_qf_format:
155317bd13b3SJan Kara 			if (sb_any_quota_loaded(sb) &&
1554dfc5d03fSJan Kara 			    sbi->s_jquota_fmt != qfmt) {
1555b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change "
1556dfc5d03fSJan Kara 					"journaled quota options when "
1557b31e1552SEric Sandeen 					"quota turned on");
1558dfc5d03fSJan Kara 				return 0;
1559dfc5d03fSJan Kara 			}
1560dfc5d03fSJan Kara 			sbi->s_jquota_fmt = qfmt;
1561ac27a0ecSDave Kleikamp 			break;
1562ac27a0ecSDave Kleikamp 		case Opt_quota:
1563ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1564ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1565ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, USRQUOTA);
1566ac27a0ecSDave Kleikamp 			break;
1567ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1568ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, QUOTA);
1569ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, GRPQUOTA);
1570ac27a0ecSDave Kleikamp 			break;
1571ac27a0ecSDave Kleikamp 		case Opt_noquota:
157217bd13b3SJan Kara 			if (sb_any_quota_loaded(sb)) {
1573b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "Cannot change quota "
1574b31e1552SEric Sandeen 					"options when quota turned on");
1575ac27a0ecSDave Kleikamp 				return 0;
1576ac27a0ecSDave Kleikamp 			}
1577ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, QUOTA);
1578ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1579ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1580ac27a0ecSDave Kleikamp 			break;
1581ac27a0ecSDave Kleikamp #else
1582ac27a0ecSDave Kleikamp 		case Opt_quota:
1583ac27a0ecSDave Kleikamp 		case Opt_usrquota:
1584ac27a0ecSDave Kleikamp 		case Opt_grpquota:
1585b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1586b31e1552SEric Sandeen 				"quota options not supported");
1587cd59e7b9SJan Kara 			break;
1588ac27a0ecSDave Kleikamp 		case Opt_usrjquota:
1589ac27a0ecSDave Kleikamp 		case Opt_grpjquota:
1590ac27a0ecSDave Kleikamp 		case Opt_offusrjquota:
1591ac27a0ecSDave Kleikamp 		case Opt_offgrpjquota:
1592ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsold:
1593ac27a0ecSDave Kleikamp 		case Opt_jqfmt_vfsv0:
15945a20bdfcSJan Kara 		case Opt_jqfmt_vfsv1:
1595b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1596b31e1552SEric Sandeen 				"journaled quota options not supported");
1597ac27a0ecSDave Kleikamp 			break;
1598ac27a0ecSDave Kleikamp 		case Opt_noquota:
1599ac27a0ecSDave Kleikamp 			break;
1600ac27a0ecSDave Kleikamp #endif
1601ac27a0ecSDave Kleikamp 		case Opt_abort:
16024ab2f15bSTheodore Ts'o 			sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1603ac27a0ecSDave Kleikamp 			break;
160406705bffSTheodore Ts'o 		case Opt_nobarrier:
160506705bffSTheodore Ts'o 			clear_opt(sbi->s_mount_opt, BARRIER);
160606705bffSTheodore Ts'o 			break;
1607ac27a0ecSDave Kleikamp 		case Opt_barrier:
160815121c18SEric Sandeen 			if (args[0].from) {
160915121c18SEric Sandeen 				if (match_int(&args[0], &option))
161015121c18SEric Sandeen 					return 0;
161115121c18SEric Sandeen 			} else
161215121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1613ac27a0ecSDave Kleikamp 			if (option)
1614ac27a0ecSDave Kleikamp 				set_opt(sbi->s_mount_opt, BARRIER);
1615ac27a0ecSDave Kleikamp 			else
1616ac27a0ecSDave Kleikamp 				clear_opt(sbi->s_mount_opt, BARRIER);
1617ac27a0ecSDave Kleikamp 			break;
1618ac27a0ecSDave Kleikamp 		case Opt_ignore:
1619ac27a0ecSDave Kleikamp 			break;
1620ac27a0ecSDave Kleikamp 		case Opt_resize:
1621ac27a0ecSDave Kleikamp 			if (!is_remount) {
1622b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1623b31e1552SEric Sandeen 					"resize option only available "
1624b31e1552SEric Sandeen 					"for remount");
1625ac27a0ecSDave Kleikamp 				return 0;
1626ac27a0ecSDave Kleikamp 			}
1627ac27a0ecSDave Kleikamp 			if (match_int(&args[0], &option) != 0)
1628ac27a0ecSDave Kleikamp 				return 0;
1629ac27a0ecSDave Kleikamp 			*n_blocks_count = option;
1630ac27a0ecSDave Kleikamp 			break;
1631ac27a0ecSDave Kleikamp 		case Opt_nobh:
1632206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1633206f7ab4SChristoph Hellwig 				 "Ignoring deprecated nobh option");
1634ac27a0ecSDave Kleikamp 			break;
1635ac27a0ecSDave Kleikamp 		case Opt_bh:
1636206f7ab4SChristoph Hellwig 			ext4_msg(sb, KERN_WARNING,
1637206f7ab4SChristoph Hellwig 				 "Ignoring deprecated bh option");
1638ac27a0ecSDave Kleikamp 			break;
163925ec56b5SJean Noel Cordenner 		case Opt_i_version:
164025ec56b5SJean Noel Cordenner 			set_opt(sbi->s_mount_opt, I_VERSION);
164125ec56b5SJean Noel Cordenner 			sb->s_flags |= MS_I_VERSION;
164225ec56b5SJean Noel Cordenner 			break;
1643dd919b98SAneesh Kumar K.V 		case Opt_nodelalloc:
1644dd919b98SAneesh Kumar K.V 			clear_opt(sbi->s_mount_opt, DELALLOC);
1645dd919b98SAneesh Kumar K.V 			break;
1646c9de560dSAlex Tomas 		case Opt_stripe:
1647c9de560dSAlex Tomas 			if (match_int(&args[0], &option))
1648c9de560dSAlex Tomas 				return 0;
1649c9de560dSAlex Tomas 			if (option < 0)
1650c9de560dSAlex Tomas 				return 0;
1651c9de560dSAlex Tomas 			sbi->s_stripe = option;
1652c9de560dSAlex Tomas 			break;
165364769240SAlex Tomas 		case Opt_delalloc:
165464769240SAlex Tomas 			set_opt(sbi->s_mount_opt, DELALLOC);
165564769240SAlex Tomas 			break;
16566fd058f7STheodore Ts'o 		case Opt_block_validity:
16576fd058f7STheodore Ts'o 			set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
16586fd058f7STheodore Ts'o 			break;
16596fd058f7STheodore Ts'o 		case Opt_noblock_validity:
16606fd058f7STheodore Ts'o 			clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
16616fd058f7STheodore Ts'o 			break;
1662240799cdSTheodore Ts'o 		case Opt_inode_readahead_blks:
1663240799cdSTheodore Ts'o 			if (match_int(&args[0], &option))
1664240799cdSTheodore Ts'o 				return 0;
1665240799cdSTheodore Ts'o 			if (option < 0 || option > (1 << 30))
1666240799cdSTheodore Ts'o 				return 0;
1667f7c43950STheodore Ts'o 			if (!is_power_of_2(option)) {
1668b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
1669b31e1552SEric Sandeen 					 "EXT4-fs: inode_readahead_blks"
1670b31e1552SEric Sandeen 					 " must be a power of 2");
16713197ebdbSTheodore Ts'o 				return 0;
16723197ebdbSTheodore Ts'o 			}
1673240799cdSTheodore Ts'o 			sbi->s_inode_readahead_blks = option;
1674240799cdSTheodore Ts'o 			break;
1675b3881f74STheodore Ts'o 		case Opt_journal_ioprio:
1676b3881f74STheodore Ts'o 			if (match_int(&args[0], &option))
1677b3881f74STheodore Ts'o 				return 0;
1678b3881f74STheodore Ts'o 			if (option < 0 || option > 7)
1679b3881f74STheodore Ts'o 				break;
1680b3881f74STheodore Ts'o 			*journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1681b3881f74STheodore Ts'o 							    option);
1682b3881f74STheodore Ts'o 			break;
168306705bffSTheodore Ts'o 		case Opt_noauto_da_alloc:
168406705bffSTheodore Ts'o 			set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
168506705bffSTheodore Ts'o 			break;
1686afd4672dSTheodore Ts'o 		case Opt_auto_da_alloc:
168715121c18SEric Sandeen 			if (args[0].from) {
168815121c18SEric Sandeen 				if (match_int(&args[0], &option))
168915121c18SEric Sandeen 					return 0;
169015121c18SEric Sandeen 			} else
169115121c18SEric Sandeen 				option = 1;	/* No argument, default to 1 */
1692afd4672dSTheodore Ts'o 			if (option)
1693afd4672dSTheodore Ts'o 				clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1694afd4672dSTheodore Ts'o 			else
1695afd4672dSTheodore Ts'o 				set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1696afd4672dSTheodore Ts'o 			break;
16975328e635SEric Sandeen 		case Opt_discard:
16985328e635SEric Sandeen 			set_opt(sbi->s_mount_opt, DISCARD);
16995328e635SEric Sandeen 			break;
17005328e635SEric Sandeen 		case Opt_nodiscard:
17015328e635SEric Sandeen 			clear_opt(sbi->s_mount_opt, DISCARD);
17025328e635SEric Sandeen 			break;
1703744692dcSJiaying Zhang 		case Opt_dioread_nolock:
1704744692dcSJiaying Zhang 			set_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1705744692dcSJiaying Zhang 			break;
1706744692dcSJiaying Zhang 		case Opt_dioread_lock:
1707744692dcSJiaying Zhang 			clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1708744692dcSJiaying Zhang 			break;
1709ac27a0ecSDave Kleikamp 		default:
1710b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1711b31e1552SEric Sandeen 			       "Unrecognized mount option \"%s\" "
1712b31e1552SEric Sandeen 			       "or missing value", p);
1713ac27a0ecSDave Kleikamp 			return 0;
1714ac27a0ecSDave Kleikamp 		}
1715ac27a0ecSDave Kleikamp 	}
1716ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1717ac27a0ecSDave Kleikamp 	if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1718482a7425SDmitry Monakhov 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1719ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, USRQUOTA);
1720ac27a0ecSDave Kleikamp 
1721482a7425SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1722ac27a0ecSDave Kleikamp 			clear_opt(sbi->s_mount_opt, GRPQUOTA);
1723ac27a0ecSDave Kleikamp 
172456c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1725b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
1726b31e1552SEric Sandeen 					"format mixing");
1727ac27a0ecSDave Kleikamp 			return 0;
1728ac27a0ecSDave Kleikamp 		}
1729ac27a0ecSDave Kleikamp 
1730ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
1731b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1732b31e1552SEric Sandeen 					"not specified");
1733ac27a0ecSDave Kleikamp 			return 0;
1734ac27a0ecSDave Kleikamp 		}
1735ac27a0ecSDave Kleikamp 	} else {
1736ac27a0ecSDave Kleikamp 		if (sbi->s_jquota_fmt) {
1737b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
17382c8be6b2SJan Kara 					"specified with no journaling "
1739b31e1552SEric Sandeen 					"enabled");
1740ac27a0ecSDave Kleikamp 			return 0;
1741ac27a0ecSDave Kleikamp 		}
1742ac27a0ecSDave Kleikamp 	}
1743ac27a0ecSDave Kleikamp #endif
1744ac27a0ecSDave Kleikamp 	return 1;
1745ac27a0ecSDave Kleikamp }
1746ac27a0ecSDave Kleikamp 
1747617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1748ac27a0ecSDave Kleikamp 			    int read_only)
1749ac27a0ecSDave Kleikamp {
1750617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1751ac27a0ecSDave Kleikamp 	int res = 0;
1752ac27a0ecSDave Kleikamp 
1753617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1754b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1755b31e1552SEric Sandeen 			 "forcing read-only mode");
1756ac27a0ecSDave Kleikamp 		res = MS_RDONLY;
1757ac27a0ecSDave Kleikamp 	}
1758ac27a0ecSDave Kleikamp 	if (read_only)
1759ac27a0ecSDave Kleikamp 		return res;
1760617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1761b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1762b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1763617ba13bSMingming Cao 	else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1764b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1765b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
1766b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1767ac27a0ecSDave Kleikamp 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1768ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
1769ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1770b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1771b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
1772b31e1552SEric Sandeen 			 "running e2fsck is recommended");
1773ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
1774ac27a0ecSDave Kleikamp 		(le32_to_cpu(es->s_lastcheck) +
1775ac27a0ecSDave Kleikamp 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1776b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
1777b31e1552SEric Sandeen 			 "warning: checktime reached, "
1778b31e1552SEric Sandeen 			 "running e2fsck is recommended");
17790390131bSFrank Mayhar 	if (!sbi->s_journal)
1780216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1781ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1782617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1783e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
1784ac27a0ecSDave Kleikamp 	es->s_mtime = cpu_to_le32(get_seconds());
1785617ba13bSMingming Cao 	ext4_update_dynamic_rev(sb);
17860390131bSFrank Mayhar 	if (sbi->s_journal)
1787617ba13bSMingming Cao 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1788ac27a0ecSDave Kleikamp 
1789e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
1790ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
1791a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
17927f4520ccSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x]\n",
1793ac27a0ecSDave Kleikamp 			sb->s_blocksize,
1794ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
1795617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
1796617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
1797ac27a0ecSDave Kleikamp 			sbi->s_mount_opt);
1798ac27a0ecSDave Kleikamp 
1799ac27a0ecSDave Kleikamp 	return res;
1800ac27a0ecSDave Kleikamp }
1801ac27a0ecSDave Kleikamp 
1802772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
1803772cb7c8SJose R. Santos {
1804772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1805772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
1806772cb7c8SJose R. Santos 	ext4_group_t flex_group_count;
1807772cb7c8SJose R. Santos 	ext4_group_t flex_group;
1808772cb7c8SJose R. Santos 	int groups_per_flex = 0;
1809c5ca7c76STheodore Ts'o 	size_t size;
1810772cb7c8SJose R. Santos 	int i;
1811772cb7c8SJose R. Santos 
1812503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1813503358aeSTheodore Ts'o 	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1814503358aeSTheodore Ts'o 
1815503358aeSTheodore Ts'o 	if (groups_per_flex < 2) {
1816772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
1817772cb7c8SJose R. Santos 		return 1;
1818772cb7c8SJose R. Santos 	}
1819772cb7c8SJose R. Santos 
1820c62a11fdSFrederic Bohe 	/* We allocate both existing and potentially added groups */
1821c62a11fdSFrederic Bohe 	flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1822d94e99a6SAneesh Kumar K.V 			((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1823d94e99a6SAneesh Kumar K.V 			      EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1824c5ca7c76STheodore Ts'o 	size = flex_group_count * sizeof(struct flex_groups);
1825c5ca7c76STheodore Ts'o 	sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1826c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups == NULL) {
1827c5ca7c76STheodore Ts'o 		sbi->s_flex_groups = vmalloc(size);
1828c5ca7c76STheodore Ts'o 		if (sbi->s_flex_groups)
1829c5ca7c76STheodore Ts'o 			memset(sbi->s_flex_groups, 0, size);
1830c5ca7c76STheodore Ts'o 	}
1831772cb7c8SJose R. Santos 	if (sbi->s_flex_groups == NULL) {
1832b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory for "
1833b31e1552SEric Sandeen 				"%u flex groups", flex_group_count);
1834772cb7c8SJose R. Santos 		goto failed;
1835772cb7c8SJose R. Santos 	}
1836772cb7c8SJose R. Santos 
1837772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
183888b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
1839772cb7c8SJose R. Santos 
1840772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
18417ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
18427ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
18437ad9bb65STheodore Ts'o 		atomic_add(ext4_free_blks_count(sb, gdp),
18447ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_blocks);
18457ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
18467ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
1847772cb7c8SJose R. Santos 	}
1848772cb7c8SJose R. Santos 
1849772cb7c8SJose R. Santos 	return 1;
1850772cb7c8SJose R. Santos failed:
1851772cb7c8SJose R. Santos 	return 0;
1852772cb7c8SJose R. Santos }
1853772cb7c8SJose R. Santos 
1854717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1855717d50e4SAndreas Dilger 			    struct ext4_group_desc *gdp)
1856717d50e4SAndreas Dilger {
1857717d50e4SAndreas Dilger 	__u16 crc = 0;
1858717d50e4SAndreas Dilger 
1859717d50e4SAndreas Dilger 	if (sbi->s_es->s_feature_ro_compat &
1860717d50e4SAndreas Dilger 	    cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1861717d50e4SAndreas Dilger 		int offset = offsetof(struct ext4_group_desc, bg_checksum);
1862717d50e4SAndreas Dilger 		__le32 le_group = cpu_to_le32(block_group);
1863717d50e4SAndreas Dilger 
1864717d50e4SAndreas Dilger 		crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1865717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1866717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp, offset);
1867717d50e4SAndreas Dilger 		offset += sizeof(gdp->bg_checksum); /* skip checksum */
1868717d50e4SAndreas Dilger 		/* for checksum of struct ext4_group_desc do the rest...*/
1869717d50e4SAndreas Dilger 		if ((sbi->s_es->s_feature_incompat &
1870717d50e4SAndreas Dilger 		     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1871717d50e4SAndreas Dilger 		    offset < le16_to_cpu(sbi->s_es->s_desc_size))
1872717d50e4SAndreas Dilger 			crc = crc16(crc, (__u8 *)gdp + offset,
1873717d50e4SAndreas Dilger 				    le16_to_cpu(sbi->s_es->s_desc_size) -
1874717d50e4SAndreas Dilger 					offset);
1875717d50e4SAndreas Dilger 	}
1876717d50e4SAndreas Dilger 
1877717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
1878717d50e4SAndreas Dilger }
1879717d50e4SAndreas Dilger 
1880717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1881717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
1882717d50e4SAndreas Dilger {
1883717d50e4SAndreas Dilger 	if ((sbi->s_es->s_feature_ro_compat &
1884717d50e4SAndreas Dilger 	     cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1885717d50e4SAndreas Dilger 	    (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1886717d50e4SAndreas Dilger 		return 0;
1887717d50e4SAndreas Dilger 
1888717d50e4SAndreas Dilger 	return 1;
1889717d50e4SAndreas Dilger }
1890717d50e4SAndreas Dilger 
1891ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1892617ba13bSMingming Cao static int ext4_check_descriptors(struct super_block *sb)
1893ac27a0ecSDave Kleikamp {
1894617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1895617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1896617ba13bSMingming Cao 	ext4_fsblk_t last_block;
1897bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
1898bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
1899bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
1900ce421581SJose R. Santos 	int flexbg_flag = 0;
1901fd2d4291SAvantika Mathur 	ext4_group_t i;
1902ac27a0ecSDave Kleikamp 
1903ce421581SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1904ce421581SJose R. Santos 		flexbg_flag = 1;
1905ce421581SJose R. Santos 
1906617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
1907ac27a0ecSDave Kleikamp 
1908197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
1909197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1910197cd65aSAkinobu Mita 
1911ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
1912bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
1913ac27a0ecSDave Kleikamp 		else
1914ac27a0ecSDave Kleikamp 			last_block = first_block +
1915617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
1916ac27a0ecSDave Kleikamp 
19178fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
19182b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
1919b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1920a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
1921b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
1922ac27a0ecSDave Kleikamp 			return 0;
1923ac27a0ecSDave Kleikamp 		}
19248fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
19252b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
1926b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1927a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
1928b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
1929ac27a0ecSDave Kleikamp 			return 0;
1930ac27a0ecSDave Kleikamp 		}
19318fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
1932bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
19332b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
1934b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1935a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
1936b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
1937ac27a0ecSDave Kleikamp 			return 0;
1938ac27a0ecSDave Kleikamp 		}
1939955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
1940717d50e4SAndreas Dilger 		if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1941b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1942b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
1943fd2d4291SAvantika Mathur 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1944fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
19457ee1ec4cSLi Zefan 			if (!(sb->s_flags & MS_RDONLY)) {
1946955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
1947717d50e4SAndreas Dilger 				return 0;
1948717d50e4SAndreas Dilger 			}
19497ee1ec4cSLi Zefan 		}
1950955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
1951ce421581SJose R. Santos 		if (!flexbg_flag)
1952617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
1953ac27a0ecSDave Kleikamp 	}
1954ac27a0ecSDave Kleikamp 
1955bd81d8eeSLaurent Vivier 	ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1956617ba13bSMingming Cao 	sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
1957ac27a0ecSDave Kleikamp 	return 1;
1958ac27a0ecSDave Kleikamp }
1959ac27a0ecSDave Kleikamp 
1960617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1961ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
1962ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
1963ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
1964ac27a0ecSDave Kleikamp  *
1965ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
1966ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
1967ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
1968ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
1969ac27a0ecSDave Kleikamp  *
1970ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
1971ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
1972ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
1973617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1974ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
1975ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
1976ac27a0ecSDave Kleikamp  */
1977617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
1978617ba13bSMingming Cao 				struct ext4_super_block *es)
1979ac27a0ecSDave Kleikamp {
1980ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
1981ac27a0ecSDave Kleikamp 	int nr_orphans = 0, nr_truncates = 0;
1982ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1983ac27a0ecSDave Kleikamp 	int i;
1984ac27a0ecSDave Kleikamp #endif
1985ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
1986ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
1987ac27a0ecSDave Kleikamp 		return;
1988ac27a0ecSDave Kleikamp 	}
1989ac27a0ecSDave Kleikamp 
1990a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
1991b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
1992b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
1993a8f48a95SEric Sandeen 		return;
1994a8f48a95SEric Sandeen 	}
1995a8f48a95SEric Sandeen 
1996617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1997ac27a0ecSDave Kleikamp 		if (es->s_last_orphan)
1998ac27a0ecSDave Kleikamp 			jbd_debug(1, "Errors on filesystem, "
1999ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2000ac27a0ecSDave Kleikamp 		es->s_last_orphan = 0;
2001ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2002ac27a0ecSDave Kleikamp 		return;
2003ac27a0ecSDave Kleikamp 	}
2004ac27a0ecSDave Kleikamp 
2005ac27a0ecSDave Kleikamp 	if (s_flags & MS_RDONLY) {
2006b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2007ac27a0ecSDave Kleikamp 		sb->s_flags &= ~MS_RDONLY;
2008ac27a0ecSDave Kleikamp 	}
2009ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2010ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
2011ac27a0ecSDave Kleikamp 	sb->s_flags |= MS_ACTIVE;
2012ac27a0ecSDave Kleikamp 	/* Turn on quotas so that they are updated correctly */
2013ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2014617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2015617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
2016ac27a0ecSDave Kleikamp 			if (ret < 0)
2017b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2018b31e1552SEric Sandeen 					"Cannot turn on journaled "
2019b31e1552SEric Sandeen 					"quota: error %d", ret);
2020ac27a0ecSDave Kleikamp 		}
2021ac27a0ecSDave Kleikamp 	}
2022ac27a0ecSDave Kleikamp #endif
2023ac27a0ecSDave Kleikamp 
2024ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2025ac27a0ecSDave Kleikamp 		struct inode *inode;
2026ac27a0ecSDave Kleikamp 
202797bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
202897bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2029ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2030ac27a0ecSDave Kleikamp 			break;
2031ac27a0ecSDave Kleikamp 		}
2032ac27a0ecSDave Kleikamp 
2033617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2034871a2931SChristoph Hellwig 		dquot_initialize(inode);
2035ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2036b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2037b31e1552SEric Sandeen 				"%s: truncating inode %lu to %lld bytes",
203846e665e9SHarvey Harrison 				__func__, inode->i_ino, inode->i_size);
2039e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2040ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
2041617ba13bSMingming Cao 			ext4_truncate(inode);
2042ac27a0ecSDave Kleikamp 			nr_truncates++;
2043ac27a0ecSDave Kleikamp 		} else {
2044b31e1552SEric Sandeen 			ext4_msg(sb, KERN_DEBUG,
2045b31e1552SEric Sandeen 				"%s: deleting unreferenced inode %lu",
204646e665e9SHarvey Harrison 				__func__, inode->i_ino);
2047ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2048ac27a0ecSDave Kleikamp 				  inode->i_ino);
2049ac27a0ecSDave Kleikamp 			nr_orphans++;
2050ac27a0ecSDave Kleikamp 		}
2051ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2052ac27a0ecSDave Kleikamp 	}
2053ac27a0ecSDave Kleikamp 
2054ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2055ac27a0ecSDave Kleikamp 
2056ac27a0ecSDave Kleikamp 	if (nr_orphans)
2057b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2058b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2059ac27a0ecSDave Kleikamp 	if (nr_truncates)
2060b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2061b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2062ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2063ac27a0ecSDave Kleikamp 	/* Turn quotas off */
2064ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
2065ac27a0ecSDave Kleikamp 		if (sb_dqopt(sb)->files[i])
2066287a8095SChristoph Hellwig 			dquot_quota_off(sb, i);
2067ac27a0ecSDave Kleikamp 	}
2068ac27a0ecSDave Kleikamp #endif
2069ac27a0ecSDave Kleikamp 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2070ac27a0ecSDave Kleikamp }
20710b8e58a1SAndreas Dilger 
2072cd2291a4SEric Sandeen /*
2073cd2291a4SEric Sandeen  * Maximal extent format file size.
2074cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2075cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2076cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2077cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2078cd2291a4SEric Sandeen  *
2079cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2080cd2291a4SEric Sandeen  */
2081f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2082cd2291a4SEric Sandeen {
2083cd2291a4SEric Sandeen 	loff_t res;
2084cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2085cd2291a4SEric Sandeen 
2086cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2087f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2088cd2291a4SEric Sandeen 		/*
208990c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2090cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2091cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2092cd2291a4SEric Sandeen 		 */
2093cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2094cd2291a4SEric Sandeen 
2095cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2096cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2097cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2098cd2291a4SEric Sandeen 	}
2099cd2291a4SEric Sandeen 
2100cd2291a4SEric Sandeen 	/* 32-bit extent-start container, ee_block */
2101cd2291a4SEric Sandeen 	res = 1LL << 32;
2102cd2291a4SEric Sandeen 	res <<= blkbits;
2103cd2291a4SEric Sandeen 	res -= 1;
2104cd2291a4SEric Sandeen 
2105cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2106cd2291a4SEric Sandeen 	if (res > upper_limit)
2107cd2291a4SEric Sandeen 		res = upper_limit;
2108cd2291a4SEric Sandeen 
2109cd2291a4SEric Sandeen 	return res;
2110cd2291a4SEric Sandeen }
2111ac27a0ecSDave Kleikamp 
2112ac27a0ecSDave Kleikamp /*
2113cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
21140fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
21150fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2116ac27a0ecSDave Kleikamp  */
2117f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2118ac27a0ecSDave Kleikamp {
2119617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
21200fc1b451SAneesh Kumar K.V 	int meta_blocks;
21210fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
21220b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
21230b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
21240b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
21250b8e58a1SAndreas Dilger 	 *
21260b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
21270b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
21280fc1b451SAneesh Kumar K.V 	 */
21290fc1b451SAneesh Kumar K.V 
2130f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
21310fc1b451SAneesh Kumar K.V 		/*
213290c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
21330b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
21340b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
21350fc1b451SAneesh Kumar K.V 		 */
21360fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
21370fc1b451SAneesh Kumar K.V 
21380fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
21390fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
21400fc1b451SAneesh Kumar K.V 
21410fc1b451SAneesh Kumar K.V 	} else {
21428180a562SAneesh Kumar K.V 		/*
21438180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
21448180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
21458180a562SAneesh Kumar K.V 		 * represent total number of blocks in
21468180a562SAneesh Kumar K.V 		 * file system block size
21478180a562SAneesh Kumar K.V 		 */
21480fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
21490fc1b451SAneesh Kumar K.V 
21500fc1b451SAneesh Kumar K.V 	}
21510fc1b451SAneesh Kumar K.V 
21520fc1b451SAneesh Kumar K.V 	/* indirect blocks */
21530fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
21540fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
21550fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
21560fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
21570fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
21580fc1b451SAneesh Kumar K.V 
21590fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
21600fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2161ac27a0ecSDave Kleikamp 
2162ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2163ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2164ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2165ac27a0ecSDave Kleikamp 	res <<= bits;
2166ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2167ac27a0ecSDave Kleikamp 		res = upper_limit;
21680fc1b451SAneesh Kumar K.V 
21690fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
21700fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
21710fc1b451SAneesh Kumar K.V 
2172ac27a0ecSDave Kleikamp 	return res;
2173ac27a0ecSDave Kleikamp }
2174ac27a0ecSDave Kleikamp 
2175617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
217670bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2177ac27a0ecSDave Kleikamp {
2178617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2179fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2180ac27a0ecSDave Kleikamp 	int has_super = 0;
2181ac27a0ecSDave Kleikamp 
2182ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2183ac27a0ecSDave Kleikamp 
2184617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2185ac27a0ecSDave Kleikamp 	    nr < first_meta_bg)
218670bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2187ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2188617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2189ac27a0ecSDave Kleikamp 		has_super = 1;
21900b8e58a1SAndreas Dilger 
2191617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2192ac27a0ecSDave Kleikamp }
2193ac27a0ecSDave Kleikamp 
2194c9de560dSAlex Tomas /**
2195c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2196c9de560dSAlex Tomas  * @sbi: In memory super block info
2197c9de560dSAlex Tomas  *
2198c9de560dSAlex Tomas  * If we have specified it via mount option, then
2199c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2200c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2201c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2202c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2203c9de560dSAlex Tomas  *
2204c9de560dSAlex Tomas  */
2205c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2206c9de560dSAlex Tomas {
2207c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2208c9de560dSAlex Tomas 	unsigned long stripe_width =
2209c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2210c9de560dSAlex Tomas 
2211c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2212c9de560dSAlex Tomas 		return sbi->s_stripe;
2213c9de560dSAlex Tomas 
2214c9de560dSAlex Tomas 	if (stripe_width <= sbi->s_blocks_per_group)
2215c9de560dSAlex Tomas 		return stripe_width;
2216c9de560dSAlex Tomas 
2217c9de560dSAlex Tomas 	if (stride <= sbi->s_blocks_per_group)
2218c9de560dSAlex Tomas 		return stride;
2219c9de560dSAlex Tomas 
2220c9de560dSAlex Tomas 	return 0;
2221c9de560dSAlex Tomas }
2222ac27a0ecSDave Kleikamp 
22233197ebdbSTheodore Ts'o /* sysfs supprt */
22243197ebdbSTheodore Ts'o 
22253197ebdbSTheodore Ts'o struct ext4_attr {
22263197ebdbSTheodore Ts'o 	struct attribute attr;
22273197ebdbSTheodore Ts'o 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
22283197ebdbSTheodore Ts'o 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
22293197ebdbSTheodore Ts'o 			 const char *, size_t);
22303197ebdbSTheodore Ts'o 	int offset;
22313197ebdbSTheodore Ts'o };
22323197ebdbSTheodore Ts'o 
22333197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf,
22343197ebdbSTheodore Ts'o 		unsigned long max, unsigned long *value)
22353197ebdbSTheodore Ts'o {
22363197ebdbSTheodore Ts'o 	char *endp;
22373197ebdbSTheodore Ts'o 
2238e7d2860bSAndré Goddard Rosa 	*value = simple_strtoul(skip_spaces(buf), &endp, 0);
2239e7d2860bSAndré Goddard Rosa 	endp = skip_spaces(endp);
22403197ebdbSTheodore Ts'o 	if (*endp || *value > max)
22413197ebdbSTheodore Ts'o 		return -EINVAL;
22423197ebdbSTheodore Ts'o 
22433197ebdbSTheodore Ts'o 	return 0;
22443197ebdbSTheodore Ts'o }
22453197ebdbSTheodore Ts'o 
22463197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
22473197ebdbSTheodore Ts'o 					      struct ext4_sb_info *sbi,
22483197ebdbSTheodore Ts'o 					      char *buf)
22493197ebdbSTheodore Ts'o {
22503197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
22513197ebdbSTheodore Ts'o 			(s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
22523197ebdbSTheodore Ts'o }
22533197ebdbSTheodore Ts'o 
22543197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a,
22553197ebdbSTheodore Ts'o 					 struct ext4_sb_info *sbi, char *buf)
22563197ebdbSTheodore Ts'o {
22573197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
22583197ebdbSTheodore Ts'o 
22593197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
22603197ebdbSTheodore Ts'o 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
22613197ebdbSTheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
22623197ebdbSTheodore Ts'o }
22633197ebdbSTheodore Ts'o 
22643197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
22653197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi, char *buf)
22663197ebdbSTheodore Ts'o {
22673197ebdbSTheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
22683197ebdbSTheodore Ts'o 
22693197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2270a6b43e38SAndrew Morton 			(unsigned long long)(sbi->s_kbytes_written +
22713197ebdbSTheodore Ts'o 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2272a6b43e38SAndrew Morton 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
22733197ebdbSTheodore Ts'o }
22743197ebdbSTheodore Ts'o 
22753197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
22763197ebdbSTheodore Ts'o 					  struct ext4_sb_info *sbi,
22773197ebdbSTheodore Ts'o 					  const char *buf, size_t count)
22783197ebdbSTheodore Ts'o {
22793197ebdbSTheodore Ts'o 	unsigned long t;
22803197ebdbSTheodore Ts'o 
22813197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0x40000000, &t))
22823197ebdbSTheodore Ts'o 		return -EINVAL;
22833197ebdbSTheodore Ts'o 
2284f7c43950STheodore Ts'o 	if (!is_power_of_2(t))
22853197ebdbSTheodore Ts'o 		return -EINVAL;
22863197ebdbSTheodore Ts'o 
22873197ebdbSTheodore Ts'o 	sbi->s_inode_readahead_blks = t;
22883197ebdbSTheodore Ts'o 	return count;
22893197ebdbSTheodore Ts'o }
22903197ebdbSTheodore Ts'o 
22913197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a,
22923197ebdbSTheodore Ts'o 			   struct ext4_sb_info *sbi, char *buf)
22933197ebdbSTheodore Ts'o {
22943197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
22953197ebdbSTheodore Ts'o 
22963197ebdbSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
22973197ebdbSTheodore Ts'o }
22983197ebdbSTheodore Ts'o 
22993197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a,
23003197ebdbSTheodore Ts'o 			    struct ext4_sb_info *sbi,
23013197ebdbSTheodore Ts'o 			    const char *buf, size_t count)
23023197ebdbSTheodore Ts'o {
23033197ebdbSTheodore Ts'o 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
23043197ebdbSTheodore Ts'o 	unsigned long t;
23053197ebdbSTheodore Ts'o 
23063197ebdbSTheodore Ts'o 	if (parse_strtoul(buf, 0xffffffff, &t))
23073197ebdbSTheodore Ts'o 		return -EINVAL;
23083197ebdbSTheodore Ts'o 	*ui = t;
23093197ebdbSTheodore Ts'o 	return count;
23103197ebdbSTheodore Ts'o }
23113197ebdbSTheodore Ts'o 
23123197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
23133197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
23143197ebdbSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
23153197ebdbSTheodore Ts'o 	.show	= _show,					\
23163197ebdbSTheodore Ts'o 	.store	= _store,					\
23173197ebdbSTheodore Ts'o 	.offset = offsetof(struct ext4_sb_info, _elname),	\
23183197ebdbSTheodore Ts'o }
23193197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \
23203197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
23213197ebdbSTheodore Ts'o 
23223197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
23233197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
23243197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
23253197ebdbSTheodore Ts'o 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
23263197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
23273197ebdbSTheodore Ts'o 
23283197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks);
23293197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes);
23303197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes);
23313197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
23323197ebdbSTheodore Ts'o 		 inode_readahead_blks_store, s_inode_readahead_blks);
233311013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
23343197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
23353197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
23363197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
23373197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
23383197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
23393197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
234055138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
23413197ebdbSTheodore Ts'o 
23423197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = {
23433197ebdbSTheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
23443197ebdbSTheodore Ts'o 	ATTR_LIST(session_write_kbytes),
23453197ebdbSTheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
23463197ebdbSTheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
234711013911SAndreas Dilger 	ATTR_LIST(inode_goal),
23483197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stats),
23493197ebdbSTheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
23503197ebdbSTheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
23513197ebdbSTheodore Ts'o 	ATTR_LIST(mb_order2_req),
23523197ebdbSTheodore Ts'o 	ATTR_LIST(mb_stream_req),
23533197ebdbSTheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
235455138e0bSTheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
23553197ebdbSTheodore Ts'o 	NULL,
23563197ebdbSTheodore Ts'o };
23573197ebdbSTheodore Ts'o 
23583197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
23593197ebdbSTheodore Ts'o 			      struct attribute *attr, char *buf)
23603197ebdbSTheodore Ts'o {
23613197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
23623197ebdbSTheodore Ts'o 						s_kobj);
23633197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
23643197ebdbSTheodore Ts'o 
23653197ebdbSTheodore Ts'o 	return a->show ? a->show(a, sbi, buf) : 0;
23663197ebdbSTheodore Ts'o }
23673197ebdbSTheodore Ts'o 
23683197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
23693197ebdbSTheodore Ts'o 			       struct attribute *attr,
23703197ebdbSTheodore Ts'o 			       const char *buf, size_t len)
23713197ebdbSTheodore Ts'o {
23723197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
23733197ebdbSTheodore Ts'o 						s_kobj);
23743197ebdbSTheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
23753197ebdbSTheodore Ts'o 
23763197ebdbSTheodore Ts'o 	return a->store ? a->store(a, sbi, buf, len) : 0;
23773197ebdbSTheodore Ts'o }
23783197ebdbSTheodore Ts'o 
23793197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
23803197ebdbSTheodore Ts'o {
23813197ebdbSTheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
23823197ebdbSTheodore Ts'o 						s_kobj);
23833197ebdbSTheodore Ts'o 	complete(&sbi->s_kobj_unregister);
23843197ebdbSTheodore Ts'o }
23853197ebdbSTheodore Ts'o 
23863197ebdbSTheodore Ts'o 
238752cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = {
23883197ebdbSTheodore Ts'o 	.show	= ext4_attr_show,
23893197ebdbSTheodore Ts'o 	.store	= ext4_attr_store,
23903197ebdbSTheodore Ts'o };
23913197ebdbSTheodore Ts'o 
23923197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = {
23933197ebdbSTheodore Ts'o 	.default_attrs	= ext4_attrs,
23943197ebdbSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
23953197ebdbSTheodore Ts'o 	.release	= ext4_sb_release,
23963197ebdbSTheodore Ts'o };
23973197ebdbSTheodore Ts'o 
2398a13fb1a4SEric Sandeen /*
2399a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2400a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2401a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2402a13fb1a4SEric Sandeen  * 0 if it cannot be.
2403a13fb1a4SEric Sandeen  */
2404a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2405a13fb1a4SEric Sandeen {
2406a13fb1a4SEric Sandeen 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2407a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2408a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2409a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2410a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2411a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2412a13fb1a4SEric Sandeen 		return 0;
2413a13fb1a4SEric Sandeen 	}
2414a13fb1a4SEric Sandeen 
2415a13fb1a4SEric Sandeen 	if (readonly)
2416a13fb1a4SEric Sandeen 		return 1;
2417a13fb1a4SEric Sandeen 
2418a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2419a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2420a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2421a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2422a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2423a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2424a13fb1a4SEric Sandeen 		return 0;
2425a13fb1a4SEric Sandeen 	}
2426a13fb1a4SEric Sandeen 	/*
2427a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2428a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2429a13fb1a4SEric Sandeen 	 */
2430a13fb1a4SEric Sandeen 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2431a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2432a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2433a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2434a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2435a13fb1a4SEric Sandeen 			return 0;
2436a13fb1a4SEric Sandeen 		}
2437a13fb1a4SEric Sandeen 	}
2438a13fb1a4SEric Sandeen 	return 1;
2439a13fb1a4SEric Sandeen }
2440a13fb1a4SEric Sandeen 
2441617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
24427477827fSAneesh Kumar K.V 				__releases(kernel_lock)
24437477827fSAneesh Kumar K.V 				__acquires(kernel_lock)
2444ac27a0ecSDave Kleikamp {
2445d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
2446ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
2447617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
2448617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2449617ba13bSMingming Cao 	ext4_fsblk_t block;
2450617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
245170bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
2452ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
2453ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
2454ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
2455ac27a0ecSDave Kleikamp 	struct inode *root;
24569f6200bbSTheodore Ts'o 	char *cp;
24570390131bSFrank Mayhar 	const char *descr;
24581d1fe1eeSDavid Howells 	int ret = -EINVAL;
2459ac27a0ecSDave Kleikamp 	int blocksize;
24604ec11028STheodore Ts'o 	unsigned int db_count;
24614ec11028STheodore Ts'o 	unsigned int i;
2462f287a1a5STheodore Ts'o 	int needs_recovery, has_huge_files;
2463bd81d8eeSLaurent Vivier 	__u64 blocks_count;
2464833f4077SPeter Zijlstra 	int err;
2465b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
2466ac27a0ecSDave Kleikamp 
2467ac27a0ecSDave Kleikamp 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2468ac27a0ecSDave Kleikamp 	if (!sbi)
2469ac27a0ecSDave Kleikamp 		return -ENOMEM;
2470705895b6SPekka Enberg 
2471705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
2472705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2473705895b6SPekka Enberg 	if (!sbi->s_blockgroup_lock) {
2474705895b6SPekka Enberg 		kfree(sbi);
2475705895b6SPekka Enberg 		return -ENOMEM;
2476705895b6SPekka Enberg 	}
2477ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
2478ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = 0;
2479617ba13bSMingming Cao 	sbi->s_resuid = EXT4_DEF_RESUID;
2480617ba13bSMingming Cao 	sbi->s_resgid = EXT4_DEF_RESGID;
2481240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2482d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
2483afc32f7eSTheodore Ts'o 	sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2484afc32f7eSTheodore Ts'o 						      sectors[1]);
2485ac27a0ecSDave Kleikamp 
2486ac27a0ecSDave Kleikamp 	unlock_kernel();
2487ac27a0ecSDave Kleikamp 
24889f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
24899f6200bbSTheodore Ts'o 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
24909f6200bbSTheodore Ts'o 		*cp = '!';
24919f6200bbSTheodore Ts'o 
2492617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2493ac27a0ecSDave Kleikamp 	if (!blocksize) {
2494b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
2495ac27a0ecSDave Kleikamp 		goto out_fail;
2496ac27a0ecSDave Kleikamp 	}
2497ac27a0ecSDave Kleikamp 
2498ac27a0ecSDave Kleikamp 	/*
2499617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
2500ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
2501ac27a0ecSDave Kleikamp 	 */
2502617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
250370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
250470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
2505ac27a0ecSDave Kleikamp 	} else {
250670bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
2507ac27a0ecSDave Kleikamp 	}
2508ac27a0ecSDave Kleikamp 
250970bbb3e0SAndrew Morton 	if (!(bh = sb_bread(sb, logical_sb_block))) {
2510b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
2511ac27a0ecSDave Kleikamp 		goto out_fail;
2512ac27a0ecSDave Kleikamp 	}
2513ac27a0ecSDave Kleikamp 	/*
2514ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
2515617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
2516ac27a0ecSDave Kleikamp 	 */
2517617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2518ac27a0ecSDave Kleikamp 	sbi->s_es = es;
2519ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
2520617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
2521617ba13bSMingming Cao 		goto cantfind_ext4;
2522afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
2523ac27a0ecSDave Kleikamp 
2524ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
2525ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2526617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
2527ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, DEBUG);
2528437ca0fdSDmitry Monakhov 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
2529437ca0fdSDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
2530437ca0fdSDmitry Monakhov 			"2.6.38");
2531ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, GRPID);
2532437ca0fdSDmitry Monakhov 	}
2533617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
2534ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, NO_UID32);
253503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR
2536617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2537ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, XATTR_USER);
25382e7842b8SHugh Dickins #endif
253903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
2540617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_ACL)
2541ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, POSIX_ACL);
25422e7842b8SHugh Dickins #endif
2543617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2544482a7425SDmitry Monakhov 		set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2545617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2546482a7425SDmitry Monakhov 		set_opt(sbi->s_mount_opt, ORDERED_DATA);
2547617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2548482a7425SDmitry Monakhov 		set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2549ac27a0ecSDave Kleikamp 
2550617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2551ac27a0ecSDave Kleikamp 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2552bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2553ceea16bfSDmitry Mishin 		set_opt(sbi->s_mount_opt, ERRORS_CONT);
2554bb4f397aSAneesh Kumar K.V 	else
2555bb4f397aSAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, ERRORS_RO);
2556ac27a0ecSDave Kleikamp 
2557ac27a0ecSDave Kleikamp 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2558ac27a0ecSDave Kleikamp 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
255930773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
256030773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
256130773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2562ac27a0ecSDave Kleikamp 
2563571640caSEric Sandeen 	set_opt(sbi->s_mount_opt, BARRIER);
2564ac27a0ecSDave Kleikamp 
25651e2462f9SMingming Cao 	/*
2566dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
2567dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
2568dd919b98SAneesh Kumar K.V 	 */
2569ba69f9abSJan Kara 	if (!IS_EXT3_SB(sb))
2570dd919b98SAneesh Kumar K.V 		set_opt(sbi->s_mount_opt, DELALLOC);
2571dd919b98SAneesh Kumar K.V 
2572b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
2573b3881f74STheodore Ts'o 			   &journal_ioprio, NULL, 0))
2574ac27a0ecSDave Kleikamp 		goto failed_mount;
2575ac27a0ecSDave Kleikamp 
2576ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2577482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
2578ac27a0ecSDave Kleikamp 
2579617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2580617ba13bSMingming Cao 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2581617ba13bSMingming Cao 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2582617ba13bSMingming Cao 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2583b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2584b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
2585b31e1552SEric Sandeen 		       "running e2fsck is recommended");
2586469108ffSTheodore Tso 
2587469108ffSTheodore Tso 	/*
2588ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
2589ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
2590ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
2591ac27a0ecSDave Kleikamp 	 */
2592a13fb1a4SEric Sandeen 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
2593ac27a0ecSDave Kleikamp 		goto failed_mount;
2594a13fb1a4SEric Sandeen 
2595ac27a0ecSDave Kleikamp 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2596ac27a0ecSDave Kleikamp 
2597617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2598617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
2599b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2600b31e1552SEric Sandeen 		       "Unsupported filesystem blocksize %d", blocksize);
2601ac27a0ecSDave Kleikamp 		goto failed_mount;
2602ac27a0ecSDave Kleikamp 	}
2603ac27a0ecSDave Kleikamp 
2604ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
2605ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
2606ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
2607b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
2608ce40733cSAneesh Kumar K.V 					blocksize);
2609ac27a0ecSDave Kleikamp 			goto failed_mount;
2610ac27a0ecSDave Kleikamp 		}
2611ac27a0ecSDave Kleikamp 
2612ac27a0ecSDave Kleikamp 		brelse(bh);
261370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
261470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
261570bbb3e0SAndrew Morton 		bh = sb_bread(sb, logical_sb_block);
2616ac27a0ecSDave Kleikamp 		if (!bh) {
2617b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2618b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
2619ac27a0ecSDave Kleikamp 			goto failed_mount;
2620ac27a0ecSDave Kleikamp 		}
2621617ba13bSMingming Cao 		es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2622ac27a0ecSDave Kleikamp 		sbi->s_es = es;
2623617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2624b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2625b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
2626ac27a0ecSDave Kleikamp 			goto failed_mount;
2627ac27a0ecSDave Kleikamp 		}
2628ac27a0ecSDave Kleikamp 	}
2629ac27a0ecSDave Kleikamp 
2630a13fb1a4SEric Sandeen 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2631a13fb1a4SEric Sandeen 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2632f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2633f287a1a5STheodore Ts'o 						      has_huge_files);
2634f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2635ac27a0ecSDave Kleikamp 
2636617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2637617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2638617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2639ac27a0ecSDave Kleikamp 	} else {
2640ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2641ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2642617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
26431330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
2644ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
2645b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2646b31e1552SEric Sandeen 			       "unsupported inode size: %d",
2647ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
2648ac27a0ecSDave Kleikamp 			goto failed_mount;
2649ac27a0ecSDave Kleikamp 		}
2650ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2651ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2652ac27a0ecSDave Kleikamp 	}
26530b8e58a1SAndreas Dilger 
26540d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
26550d1ee42fSAlexandre Ratchov 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
26568fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
26570d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2658d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
2659b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2660b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
26610d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
26620d1ee42fSAlexandre Ratchov 			goto failed_mount;
26630d1ee42fSAlexandre Ratchov 		}
26640d1ee42fSAlexandre Ratchov 	} else
26650d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
26660b8e58a1SAndreas Dilger 
2667ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2668ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2669b47b6f38SAndries E. Brouwer 	if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2670617ba13bSMingming Cao 		goto cantfind_ext4;
26710b8e58a1SAndreas Dilger 
2672617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2673ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
2674617ba13bSMingming Cao 		goto cantfind_ext4;
2675ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
2676ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
26770d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2678ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
2679ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
2680e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2681e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
26820b8e58a1SAndreas Dilger 
2683ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
2684ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2685ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
2686f99b2589STheodore Ts'o 	i = le32_to_cpu(es->s_flags);
2687f99b2589STheodore Ts'o 	if (i & EXT2_FLAGS_UNSIGNED_HASH)
2688f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2689f99b2589STheodore Ts'o 	else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2690f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
2691f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2692f99b2589STheodore Ts'o 		sbi->s_hash_unsigned = 3;
2693f99b2589STheodore Ts'o #else
2694f99b2589STheodore Ts'o 		es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2695f99b2589STheodore Ts'o #endif
2696f99b2589STheodore Ts'o 		sb->s_dirt = 1;
2697f99b2589STheodore Ts'o 	}
2698ac27a0ecSDave Kleikamp 
2699ac27a0ecSDave Kleikamp 	if (sbi->s_blocks_per_group > blocksize * 8) {
2700b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2701b31e1552SEric Sandeen 		       "#blocks per group too big: %lu",
2702ac27a0ecSDave Kleikamp 		       sbi->s_blocks_per_group);
2703ac27a0ecSDave Kleikamp 		goto failed_mount;
2704ac27a0ecSDave Kleikamp 	}
2705ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_group > blocksize * 8) {
2706b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2707b31e1552SEric Sandeen 		       "#inodes per group too big: %lu",
2708ac27a0ecSDave Kleikamp 		       sbi->s_inodes_per_group);
2709ac27a0ecSDave Kleikamp 		goto failed_mount;
2710ac27a0ecSDave Kleikamp 	}
2711ac27a0ecSDave Kleikamp 
2712bf43d84bSEric Sandeen 	/*
2713bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
2714bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
2715bf43d84bSEric Sandeen 	 */
2716bf43d84bSEric Sandeen 	if ((ext4_blocks_count(es) >
2717bf43d84bSEric Sandeen 	     (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2718bf43d84bSEric Sandeen 	    (ext4_blocks_count(es) >
2719bf43d84bSEric Sandeen 	     (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
2720b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
2721bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
2722ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
272390c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
2724bf43d84bSEric Sandeen 		ret = -EFBIG;
2725ac27a0ecSDave Kleikamp 		goto failed_mount;
2726ac27a0ecSDave Kleikamp 	}
2727ac27a0ecSDave Kleikamp 
2728617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2729617ba13bSMingming Cao 		goto cantfind_ext4;
2730e7c95593SEric Sandeen 
27310f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
27320f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
27330f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2734b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2735b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
27360f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
27370f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
27380f2ddca6SFrom: Thiemo Nagel 	}
27390f2ddca6SFrom: Thiemo Nagel 
27404ec11028STheodore Ts'o 	/*
27414ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
27424ec11028STheodore Ts'o 	 * of the filesystem.
27434ec11028STheodore Ts'o 	 */
27444ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2745b31e1552SEric Sandeen                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2746b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
2747e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
27484ec11028STheodore Ts'o 			 ext4_blocks_count(es));
2749e7c95593SEric Sandeen 		goto failed_mount;
2750e7c95593SEric Sandeen 	}
2751bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
2752bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
2753bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
2754bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
27554ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2756b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
27574ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
2758b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
27594ec11028STheodore Ts'o 		       ext4_blocks_count(es),
27604ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
27614ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
27624ec11028STheodore Ts'o 		goto failed_mount;
27634ec11028STheodore Ts'o 	}
2764bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
2765fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2766fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
2767617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2768617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
2769ac27a0ecSDave Kleikamp 	sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2770ac27a0ecSDave Kleikamp 				    GFP_KERNEL);
2771ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
2772b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
2773ac27a0ecSDave Kleikamp 		goto failed_mount;
2774ac27a0ecSDave Kleikamp 	}
2775ac27a0ecSDave Kleikamp 
27763244fcb1SAlexander Beregalov #ifdef CONFIG_PROC_FS
27779f6200bbSTheodore Ts'o 	if (ext4_proc_root)
27789f6200bbSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
27793244fcb1SAlexander Beregalov #endif
2780240799cdSTheodore Ts'o 
2781705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
2782ac27a0ecSDave Kleikamp 
2783ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
278470bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
2785ac27a0ecSDave Kleikamp 		sbi->s_group_desc[i] = sb_bread(sb, block);
2786ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
2787b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2788b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
2789ac27a0ecSDave Kleikamp 			db_count = i;
2790ac27a0ecSDave Kleikamp 			goto failed_mount2;
2791ac27a0ecSDave Kleikamp 		}
2792ac27a0ecSDave Kleikamp 	}
2793617ba13bSMingming Cao 	if (!ext4_check_descriptors(sb)) {
2794b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
2795ac27a0ecSDave Kleikamp 		goto failed_mount2;
2796ac27a0ecSDave Kleikamp 	}
2797772cb7c8SJose R. Santos 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2798772cb7c8SJose R. Santos 		if (!ext4_fill_flex_info(sb)) {
2799b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2800b31e1552SEric Sandeen 			       "unable to initialize "
2801b31e1552SEric Sandeen 			       "flex_bg meta info!");
2802772cb7c8SJose R. Santos 			goto failed_mount2;
2803772cb7c8SJose R. Santos 		}
2804772cb7c8SJose R. Santos 
2805ac27a0ecSDave Kleikamp 	sbi->s_gdb_count = db_count;
2806ac27a0ecSDave Kleikamp 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2807ac27a0ecSDave Kleikamp 	spin_lock_init(&sbi->s_next_gen_lock);
2808ac27a0ecSDave Kleikamp 
2809c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
281055138e0bSTheodore Ts'o 	sbi->s_max_writeback_mb_bump = 128;
2811c9de560dSAlex Tomas 
2812ac27a0ecSDave Kleikamp 	/*
2813ac27a0ecSDave Kleikamp 	 * set up enough so that it can read an inode
2814ac27a0ecSDave Kleikamp 	 */
28159ca92389STheodore Ts'o 	if (!test_opt(sb, NOLOAD) &&
28169ca92389STheodore Ts'o 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2817617ba13bSMingming Cao 		sb->s_op = &ext4_sops;
28189ca92389STheodore Ts'o 	else
28199ca92389STheodore Ts'o 		sb->s_op = &ext4_nojournal_sops;
2820617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
2821617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
2822ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2823617ba13bSMingming Cao 	sb->s_qcop = &ext4_qctl_operations;
2824617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
2825ac27a0ecSDave Kleikamp #endif
2826ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
28273b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
282832ed5058STheodore Ts'o 	mutex_init(&sbi->s_resize_lock);
2829ac27a0ecSDave Kleikamp 
2830ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
2831ac27a0ecSDave Kleikamp 
2832ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
2833617ba13bSMingming Cao 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
2834617ba13bSMingming Cao 				    EXT4_FEATURE_INCOMPAT_RECOVER));
2835ac27a0ecSDave Kleikamp 
2836ac27a0ecSDave Kleikamp 	/*
2837ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
2838ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
2839ac27a0ecSDave Kleikamp 	 */
2840ac27a0ecSDave Kleikamp 	if (!test_opt(sb, NOLOAD) &&
2841617ba13bSMingming Cao 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2842617ba13bSMingming Cao 		if (ext4_load_journal(sb, es, journal_devnum))
2843ac27a0ecSDave Kleikamp 			goto failed_mount3;
28440390131bSFrank Mayhar 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
28450390131bSFrank Mayhar 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2846b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
2847b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
2848744692dcSJiaying Zhang 		goto failed_mount_wq;
2849ac27a0ecSDave Kleikamp 	} else {
28500390131bSFrank Mayhar 		clear_opt(sbi->s_mount_opt, DATA_FLAGS);
28510390131bSFrank Mayhar 		set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
28520390131bSFrank Mayhar 		sbi->s_journal = NULL;
28530390131bSFrank Mayhar 		needs_recovery = 0;
28540390131bSFrank Mayhar 		goto no_journal;
2855ac27a0ecSDave Kleikamp 	}
2856ac27a0ecSDave Kleikamp 
2857eb40a09cSJose R. Santos 	if (ext4_blocks_count(es) > 0xffffffffULL &&
2858eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2859eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
2860b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
2861744692dcSJiaying Zhang 		goto failed_mount_wq;
2862eb40a09cSJose R. Santos 	}
2863eb40a09cSJose R. Santos 
2864d4da6c9cSLinus Torvalds 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2865d4da6c9cSLinus Torvalds 		jbd2_journal_set_features(sbi->s_journal,
2866d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2867d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2868d4da6c9cSLinus Torvalds 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2869818d276cSGirish Shilamkar 		jbd2_journal_set_features(sbi->s_journal,
2870818d276cSGirish Shilamkar 				JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2871818d276cSGirish Shilamkar 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2872818d276cSGirish Shilamkar 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2873d4da6c9cSLinus Torvalds 	} else {
2874d4da6c9cSLinus Torvalds 		jbd2_journal_clear_features(sbi->s_journal,
2875d4da6c9cSLinus Torvalds 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2876d4da6c9cSLinus Torvalds 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2877d4da6c9cSLinus Torvalds 	}
2878818d276cSGirish Shilamkar 
2879ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
2880ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
2881ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
2882ac27a0ecSDave Kleikamp 	case 0:
2883ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
288463f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
288563f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
288663f57933SAndrew Morton 		 */
2887dab291afSMingming Cao 		if (jbd2_journal_check_available_features
2888dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2889ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, ORDERED_DATA);
2890ac27a0ecSDave Kleikamp 		else
2891ac27a0ecSDave Kleikamp 			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2892ac27a0ecSDave Kleikamp 		break;
2893ac27a0ecSDave Kleikamp 
2894617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
2895617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
2896dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
2897dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2898b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
2899b31e1552SEric Sandeen 			       "requested data journaling mode");
2900744692dcSJiaying Zhang 			goto failed_mount_wq;
2901ac27a0ecSDave Kleikamp 		}
2902ac27a0ecSDave Kleikamp 	default:
2903ac27a0ecSDave Kleikamp 		break;
2904ac27a0ecSDave Kleikamp 	}
2905b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
2906ac27a0ecSDave Kleikamp 
29070390131bSFrank Mayhar no_journal:
290884061e07SDmitry Monakhov 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
290984061e07SDmitry Monakhov 				  ext4_count_free_blocks(sb));
291084061e07SDmitry Monakhov 	if (!err)
291184061e07SDmitry Monakhov 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
291284061e07SDmitry Monakhov 					  ext4_count_free_inodes(sb));
291384061e07SDmitry Monakhov 	if (!err)
291484061e07SDmitry Monakhov 		err = percpu_counter_init(&sbi->s_dirs_counter,
291584061e07SDmitry Monakhov 					  ext4_count_dirs(sb));
291684061e07SDmitry Monakhov 	if (!err)
291784061e07SDmitry Monakhov 		err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
291884061e07SDmitry Monakhov 	if (err) {
291984061e07SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "insufficient memory");
292084061e07SDmitry Monakhov 		goto failed_mount_wq;
292184061e07SDmitry Monakhov 	}
2922206f7ab4SChristoph Hellwig 
29234c0425ffSMingming Cao 	EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
29244c0425ffSMingming Cao 	if (!EXT4_SB(sb)->dio_unwritten_wq) {
29254c0425ffSMingming Cao 		printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
29264c0425ffSMingming Cao 		goto failed_mount_wq;
29274c0425ffSMingming Cao 	}
29284c0425ffSMingming Cao 
2929ac27a0ecSDave Kleikamp 	/*
2930dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
2931ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
2932ac27a0ecSDave Kleikamp 	 */
2933ac27a0ecSDave Kleikamp 
29341d1fe1eeSDavid Howells 	root = ext4_iget(sb, EXT4_ROOT_INO);
29351d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
2936b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
29371d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
2938ac27a0ecSDave Kleikamp 		goto failed_mount4;
2939ac27a0ecSDave Kleikamp 	}
2940ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
29411d1fe1eeSDavid Howells 		iput(root);
2942b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
2943ac27a0ecSDave Kleikamp 		goto failed_mount4;
2944ac27a0ecSDave Kleikamp 	}
29451d1fe1eeSDavid Howells 	sb->s_root = d_alloc_root(root);
29461d1fe1eeSDavid Howells 	if (!sb->s_root) {
2947b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
29481d1fe1eeSDavid Howells 		iput(root);
29491d1fe1eeSDavid Howells 		ret = -ENOMEM;
29501d1fe1eeSDavid Howells 		goto failed_mount4;
29511d1fe1eeSDavid Howells 	}
2952ac27a0ecSDave Kleikamp 
2953617ba13bSMingming Cao 	ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2954ef7f3835SKalpak Shah 
2955ef7f3835SKalpak Shah 	/* determine the minimum size of new large inodes, if present */
2956ef7f3835SKalpak Shah 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2957ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2958ef7f3835SKalpak Shah 						     EXT4_GOOD_OLD_INODE_SIZE;
2959ef7f3835SKalpak Shah 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2960ef7f3835SKalpak Shah 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2961ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2962ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_want_extra_isize))
2963ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2964ef7f3835SKalpak Shah 					le16_to_cpu(es->s_want_extra_isize);
2965ef7f3835SKalpak Shah 			if (sbi->s_want_extra_isize <
2966ef7f3835SKalpak Shah 			    le16_to_cpu(es->s_min_extra_isize))
2967ef7f3835SKalpak Shah 				sbi->s_want_extra_isize =
2968ef7f3835SKalpak Shah 					le16_to_cpu(es->s_min_extra_isize);
2969ef7f3835SKalpak Shah 		}
2970ef7f3835SKalpak Shah 	}
2971ef7f3835SKalpak Shah 	/* Check if enough inode space is available */
2972ef7f3835SKalpak Shah 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2973ef7f3835SKalpak Shah 							sbi->s_inode_size) {
2974ef7f3835SKalpak Shah 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2975ef7f3835SKalpak Shah 						       EXT4_GOOD_OLD_INODE_SIZE;
2976b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
2977b31e1552SEric Sandeen 			 "available");
2978ef7f3835SKalpak Shah 	}
2979ef7f3835SKalpak Shah 
298090576c0bSTheodore Ts'o 	if (test_opt(sb, DELALLOC) &&
298190576c0bSTheodore Ts'o 	    (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
2982b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2983b31e1552SEric Sandeen 			 "requested data journaling mode");
2984c2774d84SAneesh Kumar K.V 		clear_opt(sbi->s_mount_opt, DELALLOC);
298590576c0bSTheodore Ts'o 	}
2986744692dcSJiaying Zhang 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2987744692dcSJiaying Zhang 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2988744692dcSJiaying Zhang 			ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2989744692dcSJiaying Zhang 				"option - requested data journaling mode");
2990744692dcSJiaying Zhang 			clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2991744692dcSJiaying Zhang 		}
2992744692dcSJiaying Zhang 		if (sb->s_blocksize < PAGE_SIZE) {
2993744692dcSJiaying Zhang 			ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2994744692dcSJiaying Zhang 				"option - block size is too small");
2995744692dcSJiaying Zhang 			clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2996744692dcSJiaying Zhang 		}
2997744692dcSJiaying Zhang 	}
2998c2774d84SAneesh Kumar K.V 
29996fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
30006fd058f7STheodore Ts'o 	if (err) {
3001b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
3002fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
30036fd058f7STheodore Ts'o 		goto failed_mount4;
30046fd058f7STheodore Ts'o 	}
30056fd058f7STheodore Ts'o 
3006c2774d84SAneesh Kumar K.V 	ext4_ext_init(sb);
3007c2774d84SAneesh Kumar K.V 	err = ext4_mb_init(sb, needs_recovery);
3008c2774d84SAneesh Kumar K.V 	if (err) {
3009b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
3010c2774d84SAneesh Kumar K.V 			 err);
3011c2774d84SAneesh Kumar K.V 		goto failed_mount4;
3012c2774d84SAneesh Kumar K.V 	}
3013c2774d84SAneesh Kumar K.V 
30143197ebdbSTheodore Ts'o 	sbi->s_kobj.kset = ext4_kset;
30153197ebdbSTheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
30163197ebdbSTheodore Ts'o 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
30173197ebdbSTheodore Ts'o 				   "%s", sb->s_id);
30183197ebdbSTheodore Ts'o 	if (err) {
30193197ebdbSTheodore Ts'o 		ext4_mb_release(sb);
30203197ebdbSTheodore Ts'o 		ext4_ext_release(sb);
30213197ebdbSTheodore Ts'o 		goto failed_mount4;
30223197ebdbSTheodore Ts'o 	};
30233197ebdbSTheodore Ts'o 
3024617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3025617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
3026617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
30270390131bSFrank Mayhar 	if (needs_recovery) {
3028b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
3029617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
30300390131bSFrank Mayhar 	}
30310390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
30320390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
30330390131bSFrank Mayhar 			descr = " journalled data mode";
30340390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
30350390131bSFrank Mayhar 			descr = " ordered data mode";
30360390131bSFrank Mayhar 		else
30370390131bSFrank Mayhar 			descr = " writeback data mode";
30380390131bSFrank Mayhar 	} else
30390390131bSFrank Mayhar 		descr = "out journal";
30400390131bSFrank Mayhar 
3041d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
3042d4c402d9SCurt Wohlgemuth 		"Opts: %s", descr, orig_data);
3043ac27a0ecSDave Kleikamp 
3044ac27a0ecSDave Kleikamp 	lock_kernel();
3045d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
3046ac27a0ecSDave Kleikamp 	return 0;
3047ac27a0ecSDave Kleikamp 
3048617ba13bSMingming Cao cantfind_ext4:
3049ac27a0ecSDave Kleikamp 	if (!silent)
3050b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
3051ac27a0ecSDave Kleikamp 	goto failed_mount;
3052ac27a0ecSDave Kleikamp 
3053ac27a0ecSDave Kleikamp failed_mount4:
3054b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
30554c0425ffSMingming Cao 	destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
30564c0425ffSMingming Cao failed_mount_wq:
30576fd058f7STheodore Ts'o 	ext4_release_system_zone(sb);
30580390131bSFrank Mayhar 	if (sbi->s_journal) {
3059dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
306047b4a50bSJan Kara 		sbi->s_journal = NULL;
30610390131bSFrank Mayhar 	}
306284061e07SDmitry Monakhov 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
306384061e07SDmitry Monakhov 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
306484061e07SDmitry Monakhov 	percpu_counter_destroy(&sbi->s_dirs_counter);
306584061e07SDmitry Monakhov 	percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
3066ac27a0ecSDave Kleikamp failed_mount3:
3067c5ca7c76STheodore Ts'o 	if (sbi->s_flex_groups) {
3068c5ca7c76STheodore Ts'o 		if (is_vmalloc_addr(sbi->s_flex_groups))
3069c5ca7c76STheodore Ts'o 			vfree(sbi->s_flex_groups);
3070c5ca7c76STheodore Ts'o 		else
3071c5ca7c76STheodore Ts'o 			kfree(sbi->s_flex_groups);
3072c5ca7c76STheodore Ts'o 	}
3073ac27a0ecSDave Kleikamp failed_mount2:
3074ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
3075ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
3076ac27a0ecSDave Kleikamp 	kfree(sbi->s_group_desc);
3077ac27a0ecSDave Kleikamp failed_mount:
3078240799cdSTheodore Ts'o 	if (sbi->s_proc) {
30799f6200bbSTheodore Ts'o 		remove_proc_entry(sb->s_id, ext4_proc_root);
3080240799cdSTheodore Ts'o 	}
3081ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3082ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3083ac27a0ecSDave Kleikamp 		kfree(sbi->s_qf_names[i]);
3084ac27a0ecSDave Kleikamp #endif
3085617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
3086ac27a0ecSDave Kleikamp 	brelse(bh);
3087ac27a0ecSDave Kleikamp out_fail:
3088ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
3089f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
3090ac27a0ecSDave Kleikamp 	kfree(sbi);
3091ac27a0ecSDave Kleikamp 	lock_kernel();
3092d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
30931d1fe1eeSDavid Howells 	return ret;
3094ac27a0ecSDave Kleikamp }
3095ac27a0ecSDave Kleikamp 
3096ac27a0ecSDave Kleikamp /*
3097ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
3098ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
3099ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
3100ac27a0ecSDave Kleikamp  */
3101617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
3102ac27a0ecSDave Kleikamp {
3103617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3104ac27a0ecSDave Kleikamp 
3105ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
310630773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
310730773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
3108ac27a0ecSDave Kleikamp 
3109ac27a0ecSDave Kleikamp 	spin_lock(&journal->j_state_lock);
3110ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
3111dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
3112ac27a0ecSDave Kleikamp 	else
3113dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
31145bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
31155bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
31165bf5683aSHidehiro Kawai 	else
31175bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
3118ac27a0ecSDave Kleikamp 	spin_unlock(&journal->j_state_lock);
3119ac27a0ecSDave Kleikamp }
3120ac27a0ecSDave Kleikamp 
3121617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb,
3122ac27a0ecSDave Kleikamp 				   unsigned int journal_inum)
3123ac27a0ecSDave Kleikamp {
3124ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
3125ac27a0ecSDave Kleikamp 	journal_t *journal;
3126ac27a0ecSDave Kleikamp 
31270390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
31280390131bSFrank Mayhar 
3129ac27a0ecSDave Kleikamp 	/* First, test for the existence of a valid inode on disk.  Bad
3130ac27a0ecSDave Kleikamp 	 * things happen if we iget() an unused inode, as the subsequent
3131ac27a0ecSDave Kleikamp 	 * iput() will try to delete it. */
3132ac27a0ecSDave Kleikamp 
31331d1fe1eeSDavid Howells 	journal_inode = ext4_iget(sb, journal_inum);
31341d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
3135b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
3136ac27a0ecSDave Kleikamp 		return NULL;
3137ac27a0ecSDave Kleikamp 	}
3138ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
3139ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
3140ac27a0ecSDave Kleikamp 		iput(journal_inode);
3141b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3142ac27a0ecSDave Kleikamp 		return NULL;
3143ac27a0ecSDave Kleikamp 	}
3144ac27a0ecSDave Kleikamp 
3145e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3146ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
31471d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
3148b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
3149ac27a0ecSDave Kleikamp 		iput(journal_inode);
3150ac27a0ecSDave Kleikamp 		return NULL;
3151ac27a0ecSDave Kleikamp 	}
3152ac27a0ecSDave Kleikamp 
3153dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
3154ac27a0ecSDave Kleikamp 	if (!journal) {
3155b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3156ac27a0ecSDave Kleikamp 		iput(journal_inode);
3157ac27a0ecSDave Kleikamp 		return NULL;
3158ac27a0ecSDave Kleikamp 	}
3159ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3160617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3161ac27a0ecSDave Kleikamp 	return journal;
3162ac27a0ecSDave Kleikamp }
3163ac27a0ecSDave Kleikamp 
3164617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
3165ac27a0ecSDave Kleikamp 				       dev_t j_dev)
3166ac27a0ecSDave Kleikamp {
3167ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3168ac27a0ecSDave Kleikamp 	journal_t *journal;
3169617ba13bSMingming Cao 	ext4_fsblk_t start;
3170617ba13bSMingming Cao 	ext4_fsblk_t len;
3171ac27a0ecSDave Kleikamp 	int hblock, blocksize;
3172617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
3173ac27a0ecSDave Kleikamp 	unsigned long offset;
3174617ba13bSMingming Cao 	struct ext4_super_block *es;
3175ac27a0ecSDave Kleikamp 	struct block_device *bdev;
3176ac27a0ecSDave Kleikamp 
31770390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
31780390131bSFrank Mayhar 
3179b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
3180ac27a0ecSDave Kleikamp 	if (bdev == NULL)
3181ac27a0ecSDave Kleikamp 		return NULL;
3182ac27a0ecSDave Kleikamp 
3183ac27a0ecSDave Kleikamp 	if (bd_claim(bdev, sb)) {
3184b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3185b31e1552SEric Sandeen 			"failed to claim external journal device");
31869a1c3542SAl Viro 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
3187ac27a0ecSDave Kleikamp 		return NULL;
3188ac27a0ecSDave Kleikamp 	}
3189ac27a0ecSDave Kleikamp 
3190ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
3191e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
3192ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
3193b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3194b31e1552SEric Sandeen 			"blocksize too small for journal device");
3195ac27a0ecSDave Kleikamp 		goto out_bdev;
3196ac27a0ecSDave Kleikamp 	}
3197ac27a0ecSDave Kleikamp 
3198617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3199617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3200ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
3201ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
3202b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3203b31e1552SEric Sandeen 		       "external journal");
3204ac27a0ecSDave Kleikamp 		goto out_bdev;
3205ac27a0ecSDave Kleikamp 	}
3206ac27a0ecSDave Kleikamp 
3207617ba13bSMingming Cao 	es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3208617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3209ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
3210617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3211b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
3212b31e1552SEric Sandeen 					"bad superblock");
3213ac27a0ecSDave Kleikamp 		brelse(bh);
3214ac27a0ecSDave Kleikamp 		goto out_bdev;
3215ac27a0ecSDave Kleikamp 	}
3216ac27a0ecSDave Kleikamp 
3217617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3218b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3219ac27a0ecSDave Kleikamp 		brelse(bh);
3220ac27a0ecSDave Kleikamp 		goto out_bdev;
3221ac27a0ecSDave Kleikamp 	}
3222ac27a0ecSDave Kleikamp 
3223bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
3224ac27a0ecSDave Kleikamp 	start = sb_block + 1;
3225ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
3226ac27a0ecSDave Kleikamp 
3227dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3228ac27a0ecSDave Kleikamp 					start, len, blocksize);
3229ac27a0ecSDave Kleikamp 	if (!journal) {
3230b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
3231ac27a0ecSDave Kleikamp 		goto out_bdev;
3232ac27a0ecSDave Kleikamp 	}
3233ac27a0ecSDave Kleikamp 	journal->j_private = sb;
3234ac27a0ecSDave Kleikamp 	ll_rw_block(READ, 1, &journal->j_sb_buffer);
3235ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
3236ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
3237b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3238ac27a0ecSDave Kleikamp 		goto out_journal;
3239ac27a0ecSDave Kleikamp 	}
3240ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3241b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
3242b31e1552SEric Sandeen 					"user (unsupported) - %d",
3243ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
3244ac27a0ecSDave Kleikamp 		goto out_journal;
3245ac27a0ecSDave Kleikamp 	}
3246617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
3247617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
3248ac27a0ecSDave Kleikamp 	return journal;
32490b8e58a1SAndreas Dilger 
3250ac27a0ecSDave Kleikamp out_journal:
3251dab291afSMingming Cao 	jbd2_journal_destroy(journal);
3252ac27a0ecSDave Kleikamp out_bdev:
3253617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
3254ac27a0ecSDave Kleikamp 	return NULL;
3255ac27a0ecSDave Kleikamp }
3256ac27a0ecSDave Kleikamp 
3257617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
3258617ba13bSMingming Cao 			     struct ext4_super_block *es,
3259ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
3260ac27a0ecSDave Kleikamp {
3261ac27a0ecSDave Kleikamp 	journal_t *journal;
3262ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3263ac27a0ecSDave Kleikamp 	dev_t journal_dev;
3264ac27a0ecSDave Kleikamp 	int err = 0;
3265ac27a0ecSDave Kleikamp 	int really_read_only;
3266ac27a0ecSDave Kleikamp 
32670390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
32680390131bSFrank Mayhar 
3269ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3270ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3271b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3272b31e1552SEric Sandeen 			"numbers have changed");
3273ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
3274ac27a0ecSDave Kleikamp 	} else
3275ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3276ac27a0ecSDave Kleikamp 
3277ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
3278ac27a0ecSDave Kleikamp 
3279ac27a0ecSDave Kleikamp 	/*
3280ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
3281ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
3282ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
3283ac27a0ecSDave Kleikamp 	 */
3284617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3285ac27a0ecSDave Kleikamp 		if (sb->s_flags & MS_RDONLY) {
3286b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
3287b31e1552SEric Sandeen 					"required on readonly filesystem");
3288ac27a0ecSDave Kleikamp 			if (really_read_only) {
3289b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
3290b31e1552SEric Sandeen 					"unavailable, cannot proceed");
3291ac27a0ecSDave Kleikamp 				return -EROFS;
3292ac27a0ecSDave Kleikamp 			}
3293b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
3294b31e1552SEric Sandeen 			       "be enabled during recovery");
3295ac27a0ecSDave Kleikamp 		}
3296ac27a0ecSDave Kleikamp 	}
3297ac27a0ecSDave Kleikamp 
3298ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
3299b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3300b31e1552SEric Sandeen 		       "and inode journals!");
3301ac27a0ecSDave Kleikamp 		return -EINVAL;
3302ac27a0ecSDave Kleikamp 	}
3303ac27a0ecSDave Kleikamp 
3304ac27a0ecSDave Kleikamp 	if (journal_inum) {
3305617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
3306ac27a0ecSDave Kleikamp 			return -EINVAL;
3307ac27a0ecSDave Kleikamp 	} else {
3308617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3309ac27a0ecSDave Kleikamp 			return -EINVAL;
3310ac27a0ecSDave Kleikamp 	}
3311ac27a0ecSDave Kleikamp 
331290576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
3313b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
33144776004fSTheodore Ts'o 
3315ac27a0ecSDave Kleikamp 	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3316dab291afSMingming Cao 		err = jbd2_journal_update_format(journal);
3317ac27a0ecSDave Kleikamp 		if (err)  {
3318b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error updating journal");
3319dab291afSMingming Cao 			jbd2_journal_destroy(journal);
3320ac27a0ecSDave Kleikamp 			return err;
3321ac27a0ecSDave Kleikamp 		}
3322ac27a0ecSDave Kleikamp 	}
3323ac27a0ecSDave Kleikamp 
3324617ba13bSMingming Cao 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3325dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
3326ac27a0ecSDave Kleikamp 	if (!err)
3327dab291afSMingming Cao 		err = jbd2_journal_load(journal);
3328ac27a0ecSDave Kleikamp 
3329ac27a0ecSDave Kleikamp 	if (err) {
3330b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
3331dab291afSMingming Cao 		jbd2_journal_destroy(journal);
3332ac27a0ecSDave Kleikamp 		return err;
3333ac27a0ecSDave Kleikamp 	}
3334ac27a0ecSDave Kleikamp 
3335617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
3336617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
3337ac27a0ecSDave Kleikamp 
3338ac27a0ecSDave Kleikamp 	if (journal_devnum &&
3339ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3340ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
3341ac27a0ecSDave Kleikamp 
3342ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
3343e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3344ac27a0ecSDave Kleikamp 	}
3345ac27a0ecSDave Kleikamp 
3346ac27a0ecSDave Kleikamp 	return 0;
3347ac27a0ecSDave Kleikamp }
3348ac27a0ecSDave Kleikamp 
3349e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
3350ac27a0ecSDave Kleikamp {
3351e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3352617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3353c4be0c1dSTakashi Sato 	int error = 0;
3354ac27a0ecSDave Kleikamp 
3355ac27a0ecSDave Kleikamp 	if (!sbh)
3356c4be0c1dSTakashi Sato 		return error;
3357914258bfSTheodore Ts'o 	if (buffer_write_io_error(sbh)) {
3358914258bfSTheodore Ts'o 		/*
3359914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the
3360914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
3361914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
3362914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
3363914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
3364914258bfSTheodore Ts'o 		 * write and hope for the best.
3365914258bfSTheodore Ts'o 		 */
3366b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
3367b31e1552SEric Sandeen 		       "superblock detected");
3368914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
3369914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
3370914258bfSTheodore Ts'o 	}
337171290b36STheodore Ts'o 	/*
337271290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
337371290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
337471290b36STheodore Ts'o 	 * write time when we are mounting the root file system
337571290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
337671290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
337771290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
337871290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
337971290b36STheodore Ts'o 	 * to complain and force a full file system check.
338071290b36STheodore Ts'o 	 */
338171290b36STheodore Ts'o 	if (!(sb->s_flags & MS_RDONLY))
3382ac27a0ecSDave Kleikamp 		es->s_wtime = cpu_to_le32(get_seconds());
3383afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
3384afc32f7eSTheodore Ts'o 		cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3385afc32f7eSTheodore Ts'o 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3386afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
33875d1b1b3fSAneesh Kumar K.V 	ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
33885d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeblocks_counter));
33895d1b1b3fSAneesh Kumar K.V 	es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
33905d1b1b3fSAneesh Kumar K.V 					&EXT4_SB(sb)->s_freeinodes_counter));
33917234ab2aSTheodore Ts'o 	sb->s_dirt = 0;
3392ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
3393ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
3394914258bfSTheodore Ts'o 	if (sync) {
3395c4be0c1dSTakashi Sato 		error = sync_dirty_buffer(sbh);
3396c4be0c1dSTakashi Sato 		if (error)
3397c4be0c1dSTakashi Sato 			return error;
3398c4be0c1dSTakashi Sato 
3399c4be0c1dSTakashi Sato 		error = buffer_write_io_error(sbh);
3400c4be0c1dSTakashi Sato 		if (error) {
3401b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
3402b31e1552SEric Sandeen 			       "superblock");
3403914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
3404914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
3405914258bfSTheodore Ts'o 		}
3406914258bfSTheodore Ts'o 	}
3407c4be0c1dSTakashi Sato 	return error;
3408ac27a0ecSDave Kleikamp }
3409ac27a0ecSDave Kleikamp 
3410ac27a0ecSDave Kleikamp /*
3411ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
3412ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
3413ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
3414ac27a0ecSDave Kleikamp  */
3415617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
3416617ba13bSMingming Cao 					struct ext4_super_block *es)
3417ac27a0ecSDave Kleikamp {
3418617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
3419ac27a0ecSDave Kleikamp 
34200390131bSFrank Mayhar 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
34210390131bSFrank Mayhar 		BUG_ON(journal != NULL);
34220390131bSFrank Mayhar 		return;
34230390131bSFrank Mayhar 	}
3424dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
34257ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
34267ffe1ea8SHidehiro Kawai 		goto out;
34277ffe1ea8SHidehiro Kawai 
3428617ba13bSMingming Cao 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3429ac27a0ecSDave Kleikamp 	    sb->s_flags & MS_RDONLY) {
3430617ba13bSMingming Cao 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3431e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3432ac27a0ecSDave Kleikamp 	}
34337ffe1ea8SHidehiro Kawai 
34347ffe1ea8SHidehiro Kawai out:
3435dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
3436ac27a0ecSDave Kleikamp }
3437ac27a0ecSDave Kleikamp 
3438ac27a0ecSDave Kleikamp /*
3439ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
3440ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
3441ac27a0ecSDave Kleikamp  * main filesystem now.
3442ac27a0ecSDave Kleikamp  */
3443617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
3444617ba13bSMingming Cao 				   struct ext4_super_block *es)
3445ac27a0ecSDave Kleikamp {
3446ac27a0ecSDave Kleikamp 	journal_t *journal;
3447ac27a0ecSDave Kleikamp 	int j_errno;
3448ac27a0ecSDave Kleikamp 	const char *errstr;
3449ac27a0ecSDave Kleikamp 
34500390131bSFrank Mayhar 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
34510390131bSFrank Mayhar 
3452617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
3453ac27a0ecSDave Kleikamp 
3454ac27a0ecSDave Kleikamp 	/*
3455ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
3456617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
3457ac27a0ecSDave Kleikamp 	 */
3458ac27a0ecSDave Kleikamp 
3459dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
3460ac27a0ecSDave Kleikamp 	if (j_errno) {
3461ac27a0ecSDave Kleikamp 		char nbuf[16];
3462ac27a0ecSDave Kleikamp 
3463617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
346412062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
3465ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
346612062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
3467ac27a0ecSDave Kleikamp 
3468617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3469617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3470e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
3471ac27a0ecSDave Kleikamp 
3472dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
3473ac27a0ecSDave Kleikamp 	}
3474ac27a0ecSDave Kleikamp }
3475ac27a0ecSDave Kleikamp 
3476ac27a0ecSDave Kleikamp /*
3477ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
3478ac27a0ecSDave Kleikamp  * and wait on the commit.
3479ac27a0ecSDave Kleikamp  */
3480617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
3481ac27a0ecSDave Kleikamp {
3482ac27a0ecSDave Kleikamp 	journal_t *journal;
34830390131bSFrank Mayhar 	int ret = 0;
3484ac27a0ecSDave Kleikamp 
3485ac27a0ecSDave Kleikamp 	if (sb->s_flags & MS_RDONLY)
3486ac27a0ecSDave Kleikamp 		return 0;
3487ac27a0ecSDave Kleikamp 
3488617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
34896b0310fbSEric Sandeen 	if (journal) {
34906b0310fbSEric Sandeen 		vfs_check_frozen(sb, SB_FREEZE_WRITE);
3491617ba13bSMingming Cao 		ret = ext4_journal_force_commit(journal);
34926b0310fbSEric Sandeen 	}
34930390131bSFrank Mayhar 
3494ac27a0ecSDave Kleikamp 	return ret;
3495ac27a0ecSDave Kleikamp }
3496ac27a0ecSDave Kleikamp 
3497617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb)
3498ac27a0ecSDave Kleikamp {
3499ebc1ac16SChristoph Hellwig 	lock_super(sb);
3500e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3501ebc1ac16SChristoph Hellwig 	unlock_super(sb);
35020390131bSFrank Mayhar }
3503ac27a0ecSDave Kleikamp 
3504617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
3505ac27a0ecSDave Kleikamp {
350614ce0cb4STheodore Ts'o 	int ret = 0;
35079eddacf9SJan Kara 	tid_t target;
35088d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3509ac27a0ecSDave Kleikamp 
35109bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
35118d5d02e6SMingming Cao 	flush_workqueue(sbi->dio_unwritten_wq);
35128d5d02e6SMingming Cao 	if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
3513ac27a0ecSDave Kleikamp 		if (wait)
35148d5d02e6SMingming Cao 			jbd2_log_wait_commit(sbi->s_journal, target);
35150390131bSFrank Mayhar 	}
351614ce0cb4STheodore Ts'o 	return ret;
3517ac27a0ecSDave Kleikamp }
3518ac27a0ecSDave Kleikamp 
3519ac27a0ecSDave Kleikamp /*
3520ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
3521ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
3522ac27a0ecSDave Kleikamp  */
3523c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
3524ac27a0ecSDave Kleikamp {
3525c4be0c1dSTakashi Sato 	int error = 0;
3526c4be0c1dSTakashi Sato 	journal_t *journal;
3527ac27a0ecSDave Kleikamp 
35289ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
35299ca92389STheodore Ts'o 		return 0;
35309ca92389STheodore Ts'o 
3531c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
3532ac27a0ecSDave Kleikamp 
3533ac27a0ecSDave Kleikamp 	/* Now we set up the journal barrier. */
3534dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
35357ffe1ea8SHidehiro Kawai 
35367ffe1ea8SHidehiro Kawai 	/*
35379ca92389STheodore Ts'o 	 * Don't clear the needs_recovery flag if we failed to flush
35389ca92389STheodore Ts'o 	 * the journal.
35397ffe1ea8SHidehiro Kawai 	 */
3540c4be0c1dSTakashi Sato 	error = jbd2_journal_flush(journal);
35416b0310fbSEric Sandeen 	if (error < 0)
35426b0310fbSEric Sandeen 		goto out;
3543ac27a0ecSDave Kleikamp 
3544ac27a0ecSDave Kleikamp 	/* Journal blocked and flushed, clear needs_recovery flag. */
3545617ba13bSMingming Cao 	EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3546e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
35476b0310fbSEric Sandeen out:
35486b0310fbSEric Sandeen 	/* we rely on s_frozen to stop further updates */
35496b0310fbSEric Sandeen 	jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
35506b0310fbSEric Sandeen 	return error;
3551ac27a0ecSDave Kleikamp }
3552ac27a0ecSDave Kleikamp 
3553ac27a0ecSDave Kleikamp /*
3554ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
3555ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
3556ac27a0ecSDave Kleikamp  */
3557c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
3558ac27a0ecSDave Kleikamp {
35599ca92389STheodore Ts'o 	if (sb->s_flags & MS_RDONLY)
35609ca92389STheodore Ts'o 		return 0;
35619ca92389STheodore Ts'o 
3562ac27a0ecSDave Kleikamp 	lock_super(sb);
35639ca92389STheodore Ts'o 	/* Reset the needs_recovery flag before the fs is unlocked. */
3564617ba13bSMingming Cao 	EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3565e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
3566ac27a0ecSDave Kleikamp 	unlock_super(sb);
3567c4be0c1dSTakashi Sato 	return 0;
3568ac27a0ecSDave Kleikamp }
3569ac27a0ecSDave Kleikamp 
3570617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
3571ac27a0ecSDave Kleikamp {
3572617ba13bSMingming Cao 	struct ext4_super_block *es;
3573617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3574617ba13bSMingming Cao 	ext4_fsblk_t n_blocks_count = 0;
3575ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
3576617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
3577c79d967dSChristoph Hellwig 	int enable_quota = 0;
35788a266467STheodore Ts'o 	ext4_group_t g;
3579b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3580ac27a0ecSDave Kleikamp 	int err;
3581ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3582ac27a0ecSDave Kleikamp 	int i;
3583ac27a0ecSDave Kleikamp #endif
3584d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3585ac27a0ecSDave Kleikamp 
3586337eb00aSAlessio Igor Bogani 	lock_kernel();
3587337eb00aSAlessio Igor Bogani 
3588ac27a0ecSDave Kleikamp 	/* Store the original options */
3589bbd6851aSAl Viro 	lock_super(sb);
3590ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
3591ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
3592ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
3593ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
3594ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
359530773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
359630773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
3597ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3598ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3599ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3600ac27a0ecSDave Kleikamp 		old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3601ac27a0ecSDave Kleikamp #endif
3602b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3603b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
3604ac27a0ecSDave Kleikamp 
3605ac27a0ecSDave Kleikamp 	/*
3606ac27a0ecSDave Kleikamp 	 * Allow the "check" option to be passed as a remount option.
3607ac27a0ecSDave Kleikamp 	 */
3608b3881f74STheodore Ts'o 	if (!parse_options(data, sb, NULL, &journal_ioprio,
3609b3881f74STheodore Ts'o 			   &n_blocks_count, 1)) {
3610ac27a0ecSDave Kleikamp 		err = -EINVAL;
3611ac27a0ecSDave Kleikamp 		goto restore_opts;
3612ac27a0ecSDave Kleikamp 	}
3613ac27a0ecSDave Kleikamp 
36144ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
3615c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
3616ac27a0ecSDave Kleikamp 
3617ac27a0ecSDave Kleikamp 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3618482a7425SDmitry Monakhov 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3619ac27a0ecSDave Kleikamp 
3620ac27a0ecSDave Kleikamp 	es = sbi->s_es;
3621ac27a0ecSDave Kleikamp 
3622b3881f74STheodore Ts'o 	if (sbi->s_journal) {
3623617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
3624b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3625b3881f74STheodore Ts'o 	}
3626ac27a0ecSDave Kleikamp 
3627ac27a0ecSDave Kleikamp 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3628bd81d8eeSLaurent Vivier 		n_blocks_count > ext4_blocks_count(es)) {
36294ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
3630ac27a0ecSDave Kleikamp 			err = -EROFS;
3631ac27a0ecSDave Kleikamp 			goto restore_opts;
3632ac27a0ecSDave Kleikamp 		}
3633ac27a0ecSDave Kleikamp 
3634ac27a0ecSDave Kleikamp 		if (*flags & MS_RDONLY) {
36350f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
36360f0dd62fSChristoph Hellwig 			if (err < 0)
3637c79d967dSChristoph Hellwig 				goto restore_opts;
3638c79d967dSChristoph Hellwig 
3639ac27a0ecSDave Kleikamp 			/*
3640ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
3641ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
3642ac27a0ecSDave Kleikamp 			 */
3643ac27a0ecSDave Kleikamp 			sb->s_flags |= MS_RDONLY;
3644ac27a0ecSDave Kleikamp 
3645ac27a0ecSDave Kleikamp 			/*
3646ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
3647ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
3648ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
3649ac27a0ecSDave Kleikamp 			 */
3650617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3651617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
3652ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
3653ac27a0ecSDave Kleikamp 
3654a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
3655617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
3656ac27a0ecSDave Kleikamp 		} else {
3657a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
3658a13fb1a4SEric Sandeen 			if (!ext4_feature_set_ok(sb, 0)) {
3659ac27a0ecSDave Kleikamp 				err = -EROFS;
3660ac27a0ecSDave Kleikamp 				goto restore_opts;
3661ac27a0ecSDave Kleikamp 			}
3662ead6596bSEric Sandeen 			/*
36638a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
36640b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
36658a266467STheodore Ts'o 			 */
36668a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
36678a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
36688a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
36698a266467STheodore Ts'o 
36708a266467STheodore Ts'o 				if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3671b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
3672b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
36738a266467STheodore Ts'o 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
36748a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
36758a266467STheodore Ts'o 					err = -EINVAL;
36768a266467STheodore Ts'o 					goto restore_opts;
36778a266467STheodore Ts'o 				}
36788a266467STheodore Ts'o 			}
36798a266467STheodore Ts'o 
36808a266467STheodore Ts'o 			/*
3681ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
3682ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
3683ead6596bSEric Sandeen 			 * require a full umount/remount for now.
3684ead6596bSEric Sandeen 			 */
3685ead6596bSEric Sandeen 			if (es->s_last_orphan) {
3686b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
3687ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
3688ead6596bSEric Sandeen 				       "orphan inode list.  Please "
3689b31e1552SEric Sandeen 				       "umount/remount instead");
3690ead6596bSEric Sandeen 				err = -EINVAL;
3691ead6596bSEric Sandeen 				goto restore_opts;
3692ead6596bSEric Sandeen 			}
3693ead6596bSEric Sandeen 
3694ac27a0ecSDave Kleikamp 			/*
3695ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
3696ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
3697ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
3698ac27a0ecSDave Kleikamp 			 * the partition.)
3699ac27a0ecSDave Kleikamp 			 */
37000390131bSFrank Mayhar 			if (sbi->s_journal)
3701617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
3702ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
3703617ba13bSMingming Cao 			if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3704ac27a0ecSDave Kleikamp 				goto restore_opts;
3705617ba13bSMingming Cao 			if (!ext4_setup_super(sb, es, 0))
3706ac27a0ecSDave Kleikamp 				sb->s_flags &= ~MS_RDONLY;
3707c79d967dSChristoph Hellwig 			enable_quota = 1;
3708ac27a0ecSDave Kleikamp 		}
3709ac27a0ecSDave Kleikamp 	}
37106fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
37110390131bSFrank Mayhar 	if (sbi->s_journal == NULL)
3712e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
37130390131bSFrank Mayhar 
3714ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3715ac27a0ecSDave Kleikamp 	/* Release old quota file names */
3716ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++)
3717ac27a0ecSDave Kleikamp 		if (old_opts.s_qf_names[i] &&
3718ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3719ac27a0ecSDave Kleikamp 			kfree(old_opts.s_qf_names[i]);
3720ac27a0ecSDave Kleikamp #endif
3721bbd6851aSAl Viro 	unlock_super(sb);
3722337eb00aSAlessio Igor Bogani 	unlock_kernel();
3723c79d967dSChristoph Hellwig 	if (enable_quota)
37240f0dd62fSChristoph Hellwig 		dquot_resume(sb, -1);
3725d4c402d9SCurt Wohlgemuth 
3726d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
3727d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
3728ac27a0ecSDave Kleikamp 	return 0;
37290b8e58a1SAndreas Dilger 
3730ac27a0ecSDave Kleikamp restore_opts:
3731ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
3732ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
3733ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
3734ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
3735ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
373630773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
373730773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
3738ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3739ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3740ac27a0ecSDave Kleikamp 	for (i = 0; i < MAXQUOTAS; i++) {
3741ac27a0ecSDave Kleikamp 		if (sbi->s_qf_names[i] &&
3742ac27a0ecSDave Kleikamp 		    old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3743ac27a0ecSDave Kleikamp 			kfree(sbi->s_qf_names[i]);
3744ac27a0ecSDave Kleikamp 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3745ac27a0ecSDave Kleikamp 	}
3746ac27a0ecSDave Kleikamp #endif
3747bbd6851aSAl Viro 	unlock_super(sb);
3748337eb00aSAlessio Igor Bogani 	unlock_kernel();
3749d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
3750ac27a0ecSDave Kleikamp 	return err;
3751ac27a0ecSDave Kleikamp }
3752ac27a0ecSDave Kleikamp 
3753617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3754ac27a0ecSDave Kleikamp {
3755ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
3756617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3757617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
3758960cc398SPekka Enberg 	u64 fsid;
3759ac27a0ecSDave Kleikamp 
37605e70030dSBadari Pulavarty 	if (test_opt(sb, MINIX_DF)) {
37615e70030dSBadari Pulavarty 		sbi->s_overhead_last = 0;
37626bc9feffSAneesh Kumar K.V 	} else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
37638df9675fSTheodore Ts'o 		ext4_group_t i, ngroups = ext4_get_groups_count(sb);
37645e70030dSBadari Pulavarty 		ext4_fsblk_t overhead = 0;
3765ac27a0ecSDave Kleikamp 
3766ac27a0ecSDave Kleikamp 		/*
37675e70030dSBadari Pulavarty 		 * Compute the overhead (FS structures).  This is constant
37685e70030dSBadari Pulavarty 		 * for a given filesystem unless the number of block groups
37695e70030dSBadari Pulavarty 		 * changes so we cache the previous value until it does.
3770ac27a0ecSDave Kleikamp 		 */
3771ac27a0ecSDave Kleikamp 
3772ac27a0ecSDave Kleikamp 		/*
3773ac27a0ecSDave Kleikamp 		 * All of the blocks before first_data_block are
3774ac27a0ecSDave Kleikamp 		 * overhead
3775ac27a0ecSDave Kleikamp 		 */
3776ac27a0ecSDave Kleikamp 		overhead = le32_to_cpu(es->s_first_data_block);
3777ac27a0ecSDave Kleikamp 
3778ac27a0ecSDave Kleikamp 		/*
3779ac27a0ecSDave Kleikamp 		 * Add the overhead attributed to the superblock and
3780ac27a0ecSDave Kleikamp 		 * block group descriptors.  If the sparse superblocks
3781ac27a0ecSDave Kleikamp 		 * feature is turned on, then not all groups have this.
3782ac27a0ecSDave Kleikamp 		 */
3783ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3784617ba13bSMingming Cao 			overhead += ext4_bg_has_super(sb, i) +
3785617ba13bSMingming Cao 				ext4_bg_num_gdb(sb, i);
3786ac27a0ecSDave Kleikamp 			cond_resched();
3787ac27a0ecSDave Kleikamp 		}
3788ac27a0ecSDave Kleikamp 
3789ac27a0ecSDave Kleikamp 		/*
3790ac27a0ecSDave Kleikamp 		 * Every block group has an inode bitmap, a block
3791ac27a0ecSDave Kleikamp 		 * bitmap, and an inode table.
3792ac27a0ecSDave Kleikamp 		 */
37935e70030dSBadari Pulavarty 		overhead += ngroups * (2 + sbi->s_itb_per_group);
37945e70030dSBadari Pulavarty 		sbi->s_overhead_last = overhead;
37955e70030dSBadari Pulavarty 		smp_wmb();
37966bc9feffSAneesh Kumar K.V 		sbi->s_blocks_last = ext4_blocks_count(es);
3797ac27a0ecSDave Kleikamp 	}
3798ac27a0ecSDave Kleikamp 
3799617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
3800ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
38015e70030dSBadari Pulavarty 	buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
38026bc6e63fSAneesh Kumar K.V 	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
38036bc6e63fSAneesh Kumar K.V 		       percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3804bd81d8eeSLaurent Vivier 	buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3805bd81d8eeSLaurent Vivier 	if (buf->f_bfree < ext4_r_blocks_count(es))
3806ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
3807ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
380852d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3809617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
3810960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
3811960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3812960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3813960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
38140b8e58a1SAndreas Dilger 
3815ac27a0ecSDave Kleikamp 	return 0;
3816ac27a0ecSDave Kleikamp }
3817ac27a0ecSDave Kleikamp 
38180b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction
38190b8e58a1SAndreas Dilger  * before quota file is locked for write. Otherwise the are possible deadlocks:
3820ac27a0ecSDave Kleikamp  * Process 1                         Process 2
3821617ba13bSMingming Cao  * ext4_create()                     quota_sync()
3822dab291afSMingming Cao  *   jbd2_journal_start()                  write_dquot()
3823871a2931SChristoph Hellwig  *   dquot_initialize()                         down(dqio_mutex)
3824dab291afSMingming Cao  *     down(dqio_mutex)                    jbd2_journal_start()
3825ac27a0ecSDave Kleikamp  *
3826ac27a0ecSDave Kleikamp  */
3827ac27a0ecSDave Kleikamp 
3828ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3829ac27a0ecSDave Kleikamp 
3830ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
3831ac27a0ecSDave Kleikamp {
3832ac27a0ecSDave Kleikamp 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3833ac27a0ecSDave Kleikamp }
3834ac27a0ecSDave Kleikamp 
3835617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
3836ac27a0ecSDave Kleikamp {
3837ac27a0ecSDave Kleikamp 	int ret, err;
3838ac27a0ecSDave Kleikamp 	handle_t *handle;
3839ac27a0ecSDave Kleikamp 	struct inode *inode;
3840ac27a0ecSDave Kleikamp 
3841ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
3842617ba13bSMingming Cao 	handle = ext4_journal_start(inode,
3843617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3844ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3845ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3846ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
3847617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3848ac27a0ecSDave Kleikamp 	if (!ret)
3849ac27a0ecSDave Kleikamp 		ret = err;
3850ac27a0ecSDave Kleikamp 	return ret;
3851ac27a0ecSDave Kleikamp }
3852ac27a0ecSDave Kleikamp 
3853617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
3854ac27a0ecSDave Kleikamp {
3855ac27a0ecSDave Kleikamp 	int ret, err;
3856ac27a0ecSDave Kleikamp 	handle_t *handle;
3857ac27a0ecSDave Kleikamp 
3858617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3859617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3860ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3861ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3862ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
3863617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3864ac27a0ecSDave Kleikamp 	if (!ret)
3865ac27a0ecSDave Kleikamp 		ret = err;
3866ac27a0ecSDave Kleikamp 	return ret;
3867ac27a0ecSDave Kleikamp }
3868ac27a0ecSDave Kleikamp 
3869617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
3870ac27a0ecSDave Kleikamp {
3871ac27a0ecSDave Kleikamp 	int ret, err;
3872ac27a0ecSDave Kleikamp 	handle_t *handle;
3873ac27a0ecSDave Kleikamp 
3874617ba13bSMingming Cao 	handle = ext4_journal_start(dquot_to_inode(dquot),
3875617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
38769c3013e9SJan Kara 	if (IS_ERR(handle)) {
38779c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
38789c3013e9SJan Kara 		dquot_release(dquot);
3879ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
38809c3013e9SJan Kara 	}
3881ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
3882617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3883ac27a0ecSDave Kleikamp 	if (!ret)
3884ac27a0ecSDave Kleikamp 		ret = err;
3885ac27a0ecSDave Kleikamp 	return ret;
3886ac27a0ecSDave Kleikamp }
3887ac27a0ecSDave Kleikamp 
3888617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
3889ac27a0ecSDave Kleikamp {
38902c8be6b2SJan Kara 	/* Are we journaling quotas? */
3891617ba13bSMingming Cao 	if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3892617ba13bSMingming Cao 	    EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3893ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
3894617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
3895ac27a0ecSDave Kleikamp 	} else {
3896ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
3897ac27a0ecSDave Kleikamp 	}
3898ac27a0ecSDave Kleikamp }
3899ac27a0ecSDave Kleikamp 
3900617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
3901ac27a0ecSDave Kleikamp {
3902ac27a0ecSDave Kleikamp 	int ret, err;
3903ac27a0ecSDave Kleikamp 	handle_t *handle;
3904ac27a0ecSDave Kleikamp 
3905ac27a0ecSDave Kleikamp 	/* Data block + inode block */
3906617ba13bSMingming Cao 	handle = ext4_journal_start(sb->s_root->d_inode, 2);
3907ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
3908ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
3909ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
3910617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
3911ac27a0ecSDave Kleikamp 	if (!ret)
3912ac27a0ecSDave Kleikamp 		ret = err;
3913ac27a0ecSDave Kleikamp 	return ret;
3914ac27a0ecSDave Kleikamp }
3915ac27a0ecSDave Kleikamp 
3916ac27a0ecSDave Kleikamp /*
3917ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
3918ac27a0ecSDave Kleikamp  * the quota file and such...
3919ac27a0ecSDave Kleikamp  */
3920617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
3921ac27a0ecSDave Kleikamp {
3922287a8095SChristoph Hellwig 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3923617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
3924ac27a0ecSDave Kleikamp }
3925ac27a0ecSDave Kleikamp 
3926ac27a0ecSDave Kleikamp /*
3927ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
3928ac27a0ecSDave Kleikamp  */
3929617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3930307ae18aSChristoph Hellwig 			 char *name)
3931ac27a0ecSDave Kleikamp {
3932ac27a0ecSDave Kleikamp 	int err;
39338264613dSAl Viro 	struct path path;
3934ac27a0ecSDave Kleikamp 
3935ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
3936ac27a0ecSDave Kleikamp 		return -EINVAL;
39370623543bSJan Kara 
39388264613dSAl Viro 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3939ac27a0ecSDave Kleikamp 	if (err)
3940ac27a0ecSDave Kleikamp 		return err;
39410623543bSJan Kara 
3942ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
39438264613dSAl Viro 	if (path.mnt->mnt_sb != sb) {
39448264613dSAl Viro 		path_put(&path);
3945ac27a0ecSDave Kleikamp 		return -EXDEV;
3946ac27a0ecSDave Kleikamp 	}
39470623543bSJan Kara 	/* Journaling quota? */
39480623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
39492b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
39508264613dSAl Viro 		if (path.dentry->d_parent != sb->s_root)
3951b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
3952b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
3953b31e1552SEric Sandeen 				"Journaled quota will not work");
39540623543bSJan Kara 	}
39550623543bSJan Kara 
39560623543bSJan Kara 	/*
39570623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
39580623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
39590623543bSJan Kara 	 */
39600390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
39610390131bSFrank Mayhar 	    ext4_should_journal_data(path.dentry->d_inode)) {
39620623543bSJan Kara 		/*
39630623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
39640623543bSJan Kara 		 * otherwise be livelocked...
39650623543bSJan Kara 		 */
39660623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
39677ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
39680623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
39697ffe1ea8SHidehiro Kawai 		if (err) {
39708264613dSAl Viro 			path_put(&path);
39717ffe1ea8SHidehiro Kawai 			return err;
39727ffe1ea8SHidehiro Kawai 		}
39730623543bSJan Kara 	}
39740623543bSJan Kara 
3975287a8095SChristoph Hellwig 	err = dquot_quota_on_path(sb, type, format_id, &path);
39768264613dSAl Viro 	path_put(&path);
397777e69dacSAl Viro 	return err;
3978ac27a0ecSDave Kleikamp }
3979ac27a0ecSDave Kleikamp 
3980ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
3981ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
3982ac27a0ecSDave Kleikamp  * itself serializes the operations (and noone else should touch the files)
3983ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
3984617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3985ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
3986ac27a0ecSDave Kleikamp {
3987ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
3988725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3989ac27a0ecSDave Kleikamp 	int err = 0;
3990ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
3991ac27a0ecSDave Kleikamp 	int tocopy;
3992ac27a0ecSDave Kleikamp 	size_t toread;
3993ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3994ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
3995ac27a0ecSDave Kleikamp 
3996ac27a0ecSDave Kleikamp 	if (off > i_size)
3997ac27a0ecSDave Kleikamp 		return 0;
3998ac27a0ecSDave Kleikamp 	if (off+len > i_size)
3999ac27a0ecSDave Kleikamp 		len = i_size-off;
4000ac27a0ecSDave Kleikamp 	toread = len;
4001ac27a0ecSDave Kleikamp 	while (toread > 0) {
4002ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
4003ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
4004617ba13bSMingming Cao 		bh = ext4_bread(NULL, inode, blk, 0, &err);
4005ac27a0ecSDave Kleikamp 		if (err)
4006ac27a0ecSDave Kleikamp 			return err;
4007ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
4008ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
4009ac27a0ecSDave Kleikamp 		else
4010ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
4011ac27a0ecSDave Kleikamp 		brelse(bh);
4012ac27a0ecSDave Kleikamp 		offset = 0;
4013ac27a0ecSDave Kleikamp 		toread -= tocopy;
4014ac27a0ecSDave Kleikamp 		data += tocopy;
4015ac27a0ecSDave Kleikamp 		blk++;
4016ac27a0ecSDave Kleikamp 	}
4017ac27a0ecSDave Kleikamp 	return len;
4018ac27a0ecSDave Kleikamp }
4019ac27a0ecSDave Kleikamp 
4020ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
4021ac27a0ecSDave Kleikamp  * enough credits) */
4022617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
4023ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
4024ac27a0ecSDave Kleikamp {
4025ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
4026725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4027ac27a0ecSDave Kleikamp 	int err = 0;
4028ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
4029617ba13bSMingming Cao 	int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
4030ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4031ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
4032ac27a0ecSDave Kleikamp 
40330390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
4034b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4035b31e1552SEric Sandeen 			" cancelled because transaction is not started",
40369c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
40379c3013e9SJan Kara 		return -EIO;
40389c3013e9SJan Kara 	}
403967eeb568SDmitry Monakhov 	/*
404067eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
404167eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
404267eeb568SDmitry Monakhov 	 */
404367eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
404467eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
404567eeb568SDmitry Monakhov 			" cancelled because not block aligned",
404667eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
404767eeb568SDmitry Monakhov 		return -EIO;
404867eeb568SDmitry Monakhov 	}
404967eeb568SDmitry Monakhov 
4050ac27a0ecSDave Kleikamp 	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
4051617ba13bSMingming Cao 	bh = ext4_bread(handle, inode, blk, 1, &err);
4052ac27a0ecSDave Kleikamp 	if (!bh)
4053ac27a0ecSDave Kleikamp 		goto out;
4054ac27a0ecSDave Kleikamp 	if (journal_quota) {
4055617ba13bSMingming Cao 		err = ext4_journal_get_write_access(handle, bh);
4056ac27a0ecSDave Kleikamp 		if (err) {
4057ac27a0ecSDave Kleikamp 			brelse(bh);
4058ac27a0ecSDave Kleikamp 			goto out;
4059ac27a0ecSDave Kleikamp 		}
4060ac27a0ecSDave Kleikamp 	}
4061ac27a0ecSDave Kleikamp 	lock_buffer(bh);
406267eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
4063ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
4064ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
4065ac27a0ecSDave Kleikamp 	if (journal_quota)
40660390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bh);
4067ac27a0ecSDave Kleikamp 	else {
4068ac27a0ecSDave Kleikamp 		/* Always do at least ordered writes for quotas */
4069678aaf48SJan Kara 		err = ext4_jbd2_file_inode(handle, inode);
4070ac27a0ecSDave Kleikamp 		mark_buffer_dirty(bh);
4071ac27a0ecSDave Kleikamp 	}
4072ac27a0ecSDave Kleikamp 	brelse(bh);
4073ac27a0ecSDave Kleikamp out:
407467eeb568SDmitry Monakhov 	if (err) {
40754d04e4fbSJan Kara 		mutex_unlock(&inode->i_mutex);
4076ac27a0ecSDave Kleikamp 		return err;
40774d04e4fbSJan Kara 	}
407867eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
407967eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
4080617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
4081ac27a0ecSDave Kleikamp 	}
4082ac27a0ecSDave Kleikamp 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
4083617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
4084ac27a0ecSDave Kleikamp 	mutex_unlock(&inode->i_mutex);
408567eeb568SDmitry Monakhov 	return len;
4086ac27a0ecSDave Kleikamp }
4087ac27a0ecSDave Kleikamp 
4088ac27a0ecSDave Kleikamp #endif
4089ac27a0ecSDave Kleikamp 
40900b8e58a1SAndreas Dilger static int ext4_get_sb(struct file_system_type *fs_type, int flags,
40910b8e58a1SAndreas Dilger 		       const char *dev_name, void *data, struct vfsmount *mnt)
4092ac27a0ecSDave Kleikamp {
4093617ba13bSMingming Cao 	return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
4094ac27a0ecSDave Kleikamp }
4095ac27a0ecSDave Kleikamp 
409637f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
409724b58424STheodore Ts'o static struct file_system_type ext2_fs_type = {
409824b58424STheodore Ts'o 	.owner		= THIS_MODULE,
409924b58424STheodore Ts'o 	.name		= "ext2",
410024b58424STheodore Ts'o 	.get_sb		= ext4_get_sb,
410124b58424STheodore Ts'o 	.kill_sb	= kill_block_super,
410224b58424STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
410324b58424STheodore Ts'o };
410424b58424STheodore Ts'o 
410524b58424STheodore Ts'o static inline void register_as_ext2(void)
410624b58424STheodore Ts'o {
410724b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
410824b58424STheodore Ts'o 	if (err)
410924b58424STheodore Ts'o 		printk(KERN_WARNING
411024b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
411124b58424STheodore Ts'o }
411224b58424STheodore Ts'o 
411324b58424STheodore Ts'o static inline void unregister_as_ext2(void)
411424b58424STheodore Ts'o {
411524b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
411624b58424STheodore Ts'o }
411751b7e3c9STheodore Ts'o MODULE_ALIAS("ext2");
411824b58424STheodore Ts'o #else
411924b58424STheodore Ts'o static inline void register_as_ext2(void) { }
412024b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
412124b58424STheodore Ts'o #endif
412224b58424STheodore Ts'o 
412337f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
412424b58424STheodore Ts'o static inline void register_as_ext3(void)
412524b58424STheodore Ts'o {
412624b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
412724b58424STheodore Ts'o 	if (err)
412824b58424STheodore Ts'o 		printk(KERN_WARNING
412924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
413024b58424STheodore Ts'o }
413124b58424STheodore Ts'o 
413224b58424STheodore Ts'o static inline void unregister_as_ext3(void)
413324b58424STheodore Ts'o {
413424b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
413524b58424STheodore Ts'o }
413651b7e3c9STheodore Ts'o MODULE_ALIAS("ext3");
413724b58424STheodore Ts'o #else
413824b58424STheodore Ts'o static inline void register_as_ext3(void) { }
413924b58424STheodore Ts'o static inline void unregister_as_ext3(void) { }
414024b58424STheodore Ts'o #endif
414124b58424STheodore Ts'o 
414203010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
4143ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
414403010a33STheodore Ts'o 	.name		= "ext4",
4145617ba13bSMingming Cao 	.get_sb		= ext4_get_sb,
4146ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
4147ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
4148ac27a0ecSDave Kleikamp };
4149ac27a0ecSDave Kleikamp 
4150617ba13bSMingming Cao static int __init init_ext4_fs(void)
4151ac27a0ecSDave Kleikamp {
4152c9de560dSAlex Tomas 	int err;
4153c9de560dSAlex Tomas 
415412e9b892SDmitry Monakhov 	ext4_check_flag_values();
41556fd058f7STheodore Ts'o 	err = init_ext4_system_zone();
41566fd058f7STheodore Ts'o 	if (err)
41576fd058f7STheodore Ts'o 		return err;
41583197ebdbSTheodore Ts'o 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
41593197ebdbSTheodore Ts'o 	if (!ext4_kset)
41606fd058f7STheodore Ts'o 		goto out4;
41619f6200bbSTheodore Ts'o 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
4162c9de560dSAlex Tomas 	err = init_ext4_mballoc();
4163ac27a0ecSDave Kleikamp 	if (err)
41646fd058f7STheodore Ts'o 		goto out3;
4165c9de560dSAlex Tomas 
4166c9de560dSAlex Tomas 	err = init_ext4_xattr();
4167c9de560dSAlex Tomas 	if (err)
4168c9de560dSAlex Tomas 		goto out2;
4169ac27a0ecSDave Kleikamp 	err = init_inodecache();
4170ac27a0ecSDave Kleikamp 	if (err)
4171ac27a0ecSDave Kleikamp 		goto out1;
417224b58424STheodore Ts'o 	register_as_ext2();
417324b58424STheodore Ts'o 	register_as_ext3();
417403010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
4175ac27a0ecSDave Kleikamp 	if (err)
4176ac27a0ecSDave Kleikamp 		goto out;
4177ac27a0ecSDave Kleikamp 	return 0;
4178ac27a0ecSDave Kleikamp out:
417924b58424STheodore Ts'o 	unregister_as_ext2();
418024b58424STheodore Ts'o 	unregister_as_ext3();
4181ac27a0ecSDave Kleikamp 	destroy_inodecache();
4182ac27a0ecSDave Kleikamp out1:
4183617ba13bSMingming Cao 	exit_ext4_xattr();
4184c9de560dSAlex Tomas out2:
4185c9de560dSAlex Tomas 	exit_ext4_mballoc();
41866fd058f7STheodore Ts'o out3:
41876fd058f7STheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
41886fd058f7STheodore Ts'o 	kset_unregister(ext4_kset);
41896fd058f7STheodore Ts'o out4:
41906fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4191ac27a0ecSDave Kleikamp 	return err;
4192ac27a0ecSDave Kleikamp }
4193ac27a0ecSDave Kleikamp 
4194617ba13bSMingming Cao static void __exit exit_ext4_fs(void)
4195ac27a0ecSDave Kleikamp {
419624b58424STheodore Ts'o 	unregister_as_ext2();
419724b58424STheodore Ts'o 	unregister_as_ext3();
419803010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
4199ac27a0ecSDave Kleikamp 	destroy_inodecache();
4200617ba13bSMingming Cao 	exit_ext4_xattr();
4201c9de560dSAlex Tomas 	exit_ext4_mballoc();
42029f6200bbSTheodore Ts'o 	remove_proc_entry("fs/ext4", NULL);
42033197ebdbSTheodore Ts'o 	kset_unregister(ext4_kset);
42046fd058f7STheodore Ts'o 	exit_ext4_system_zone();
4205ac27a0ecSDave Kleikamp }
4206ac27a0ecSDave Kleikamp 
4207ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
420883982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
4209ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
4210617ba13bSMingming Cao module_init(init_ext4_fs)
4211617ba13bSMingming Cao module_exit(exit_ext4_fs)
4212