xref: /openbmc/linux/fs/ext4/super.c (revision faf69551)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/ext4/super.c
4  *
5  * Copyright (C) 1992, 1993, 1994, 1995
6  * Remy Card (card@masi.ibp.fr)
7  * Laboratoire MASI - Institut Blaise Pascal
8  * Universite Pierre et Marie Curie (Paris VI)
9  *
10  *  from
11  *
12  *  linux/fs/minix/inode.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  Big-endian to little-endian byte-swapping/bitmaps by
17  *        David S. Miller (davem@caip.rutgers.edu), 1995
18  */
19 
20 #include <linux/module.h>
21 #include <linux/string.h>
22 #include <linux/fs.h>
23 #include <linux/time.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/blkdev.h>
28 #include <linux/backing-dev.h>
29 #include <linux/parser.h>
30 #include <linux/buffer_head.h>
31 #include <linux/exportfs.h>
32 #include <linux/vfs.h>
33 #include <linux/random.h>
34 #include <linux/mount.h>
35 #include <linux/namei.h>
36 #include <linux/quotaops.h>
37 #include <linux/seq_file.h>
38 #include <linux/ctype.h>
39 #include <linux/log2.h>
40 #include <linux/crc16.h>
41 #include <linux/dax.h>
42 #include <linux/cleancache.h>
43 #include <linux/uaccess.h>
44 #include <linux/iversion.h>
45 #include <linux/unicode.h>
46 #include <linux/part_stat.h>
47 #include <linux/kthread.h>
48 #include <linux/freezer.h>
49 #include <linux/fsnotify.h>
50 
51 #include "ext4.h"
52 #include "ext4_extents.h"	/* Needed for trace points definition */
53 #include "ext4_jbd2.h"
54 #include "xattr.h"
55 #include "acl.h"
56 #include "mballoc.h"
57 #include "fsmap.h"
58 
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/ext4.h>
61 
62 static struct ext4_lazy_init *ext4_li_info;
63 static DEFINE_MUTEX(ext4_li_mtx);
64 static struct ratelimit_state ext4_mount_msg_ratelimit;
65 
66 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
67 			     unsigned long journal_devnum);
68 static int ext4_show_options(struct seq_file *seq, struct dentry *root);
69 static void ext4_update_super(struct super_block *sb);
70 static int ext4_commit_super(struct super_block *sb);
71 static int ext4_mark_recovery_complete(struct super_block *sb,
72 					struct ext4_super_block *es);
73 static int ext4_clear_journal_err(struct super_block *sb,
74 				  struct ext4_super_block *es);
75 static int ext4_sync_fs(struct super_block *sb, int wait);
76 static int ext4_remount(struct super_block *sb, int *flags, char *data);
77 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
78 static int ext4_unfreeze(struct super_block *sb);
79 static int ext4_freeze(struct super_block *sb);
80 static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
81 		       const char *dev_name, void *data);
82 static inline int ext2_feature_set_ok(struct super_block *sb);
83 static inline int ext3_feature_set_ok(struct super_block *sb);
84 static void ext4_destroy_lazyinit_thread(void);
85 static void ext4_unregister_li_request(struct super_block *sb);
86 static void ext4_clear_request_list(void);
87 static struct inode *ext4_get_journal_inode(struct super_block *sb,
88 					    unsigned int journal_inum);
89 
90 /*
91  * Lock ordering
92  *
93  * page fault path:
94  * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
95  *   -> page lock -> i_data_sem (rw)
96  *
97  * buffered write path:
98  * sb_start_write -> i_mutex -> mmap_lock
99  * sb_start_write -> i_mutex -> transaction start -> page lock ->
100  *   i_data_sem (rw)
101  *
102  * truncate:
103  * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
104  *   page lock
105  * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start ->
106  *   i_data_sem (rw)
107  *
108  * direct IO:
109  * sb_start_write -> i_mutex -> mmap_lock
110  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
111  *
112  * writepages:
113  * transaction start -> page lock(s) -> i_data_sem (rw)
114  */
115 
116 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
117 static struct file_system_type ext2_fs_type = {
118 	.owner		= THIS_MODULE,
119 	.name		= "ext2",
120 	.mount		= ext4_mount,
121 	.kill_sb	= kill_block_super,
122 	.fs_flags	= FS_REQUIRES_DEV,
123 };
124 MODULE_ALIAS_FS("ext2");
125 MODULE_ALIAS("ext2");
126 #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
127 #else
128 #define IS_EXT2_SB(sb) (0)
129 #endif
130 
131 
132 static struct file_system_type ext3_fs_type = {
133 	.owner		= THIS_MODULE,
134 	.name		= "ext3",
135 	.mount		= ext4_mount,
136 	.kill_sb	= kill_block_super,
137 	.fs_flags	= FS_REQUIRES_DEV,
138 };
139 MODULE_ALIAS_FS("ext3");
140 MODULE_ALIAS("ext3");
141 #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
142 
143 
144 static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags,
145 				  bh_end_io_t *end_io)
146 {
147 	/*
148 	 * buffer's verified bit is no longer valid after reading from
149 	 * disk again due to write out error, clear it to make sure we
150 	 * recheck the buffer contents.
151 	 */
152 	clear_buffer_verified(bh);
153 
154 	bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
155 	get_bh(bh);
156 	submit_bh(REQ_OP_READ, op_flags, bh);
157 }
158 
159 void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
160 			 bh_end_io_t *end_io)
161 {
162 	BUG_ON(!buffer_locked(bh));
163 
164 	if (ext4_buffer_uptodate(bh)) {
165 		unlock_buffer(bh);
166 		return;
167 	}
168 	__ext4_read_bh(bh, op_flags, end_io);
169 }
170 
171 int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io)
172 {
173 	BUG_ON(!buffer_locked(bh));
174 
175 	if (ext4_buffer_uptodate(bh)) {
176 		unlock_buffer(bh);
177 		return 0;
178 	}
179 
180 	__ext4_read_bh(bh, op_flags, end_io);
181 
182 	wait_on_buffer(bh);
183 	if (buffer_uptodate(bh))
184 		return 0;
185 	return -EIO;
186 }
187 
188 int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait)
189 {
190 	if (trylock_buffer(bh)) {
191 		if (wait)
192 			return ext4_read_bh(bh, op_flags, NULL);
193 		ext4_read_bh_nowait(bh, op_flags, NULL);
194 		return 0;
195 	}
196 	if (wait) {
197 		wait_on_buffer(bh);
198 		if (buffer_uptodate(bh))
199 			return 0;
200 		return -EIO;
201 	}
202 	return 0;
203 }
204 
205 /*
206  * This works like __bread_gfp() except it uses ERR_PTR for error
207  * returns.  Currently with sb_bread it's impossible to distinguish
208  * between ENOMEM and EIO situations (since both result in a NULL
209  * return.
210  */
211 static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
212 					       sector_t block, int op_flags,
213 					       gfp_t gfp)
214 {
215 	struct buffer_head *bh;
216 	int ret;
217 
218 	bh = sb_getblk_gfp(sb, block, gfp);
219 	if (bh == NULL)
220 		return ERR_PTR(-ENOMEM);
221 	if (ext4_buffer_uptodate(bh))
222 		return bh;
223 
224 	ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
225 	if (ret) {
226 		put_bh(bh);
227 		return ERR_PTR(ret);
228 	}
229 	return bh;
230 }
231 
232 struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
233 				   int op_flags)
234 {
235 	return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE);
236 }
237 
238 struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
239 					    sector_t block)
240 {
241 	return __ext4_sb_bread_gfp(sb, block, 0, 0);
242 }
243 
244 void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
245 {
246 	struct buffer_head *bh = sb_getblk_gfp(sb, block, 0);
247 
248 	if (likely(bh)) {
249 		ext4_read_bh_lock(bh, REQ_RAHEAD, false);
250 		brelse(bh);
251 	}
252 }
253 
254 static int ext4_verify_csum_type(struct super_block *sb,
255 				 struct ext4_super_block *es)
256 {
257 	if (!ext4_has_feature_metadata_csum(sb))
258 		return 1;
259 
260 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
261 }
262 
263 static __le32 ext4_superblock_csum(struct super_block *sb,
264 				   struct ext4_super_block *es)
265 {
266 	struct ext4_sb_info *sbi = EXT4_SB(sb);
267 	int offset = offsetof(struct ext4_super_block, s_checksum);
268 	__u32 csum;
269 
270 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
271 
272 	return cpu_to_le32(csum);
273 }
274 
275 static int ext4_superblock_csum_verify(struct super_block *sb,
276 				       struct ext4_super_block *es)
277 {
278 	if (!ext4_has_metadata_csum(sb))
279 		return 1;
280 
281 	return es->s_checksum == ext4_superblock_csum(sb, es);
282 }
283 
284 void ext4_superblock_csum_set(struct super_block *sb)
285 {
286 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
287 
288 	if (!ext4_has_metadata_csum(sb))
289 		return;
290 
291 	es->s_checksum = ext4_superblock_csum(sb, es);
292 }
293 
294 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
295 			       struct ext4_group_desc *bg)
296 {
297 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
298 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
299 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
300 }
301 
302 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
303 			       struct ext4_group_desc *bg)
304 {
305 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
306 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
307 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
308 }
309 
310 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
311 			      struct ext4_group_desc *bg)
312 {
313 	return le32_to_cpu(bg->bg_inode_table_lo) |
314 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
315 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
316 }
317 
318 __u32 ext4_free_group_clusters(struct super_block *sb,
319 			       struct ext4_group_desc *bg)
320 {
321 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
322 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
323 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
324 }
325 
326 __u32 ext4_free_inodes_count(struct super_block *sb,
327 			      struct ext4_group_desc *bg)
328 {
329 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
330 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
331 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
332 }
333 
334 __u32 ext4_used_dirs_count(struct super_block *sb,
335 			      struct ext4_group_desc *bg)
336 {
337 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
338 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
339 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
340 }
341 
342 __u32 ext4_itable_unused_count(struct super_block *sb,
343 			      struct ext4_group_desc *bg)
344 {
345 	return le16_to_cpu(bg->bg_itable_unused_lo) |
346 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
347 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
348 }
349 
350 void ext4_block_bitmap_set(struct super_block *sb,
351 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
352 {
353 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
354 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
355 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
356 }
357 
358 void ext4_inode_bitmap_set(struct super_block *sb,
359 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
360 {
361 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
362 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
363 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
364 }
365 
366 void ext4_inode_table_set(struct super_block *sb,
367 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
368 {
369 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
370 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
371 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
372 }
373 
374 void ext4_free_group_clusters_set(struct super_block *sb,
375 				  struct ext4_group_desc *bg, __u32 count)
376 {
377 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
378 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
379 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
380 }
381 
382 void ext4_free_inodes_set(struct super_block *sb,
383 			  struct ext4_group_desc *bg, __u32 count)
384 {
385 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
386 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
387 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
388 }
389 
390 void ext4_used_dirs_set(struct super_block *sb,
391 			  struct ext4_group_desc *bg, __u32 count)
392 {
393 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
394 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
395 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
396 }
397 
398 void ext4_itable_unused_set(struct super_block *sb,
399 			  struct ext4_group_desc *bg, __u32 count)
400 {
401 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
402 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
403 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
404 }
405 
406 static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
407 {
408 	now = clamp_val(now, 0, (1ull << 40) - 1);
409 
410 	*lo = cpu_to_le32(lower_32_bits(now));
411 	*hi = upper_32_bits(now);
412 }
413 
414 static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
415 {
416 	return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
417 }
418 #define ext4_update_tstamp(es, tstamp) \
419 	__ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
420 			     ktime_get_real_seconds())
421 #define ext4_get_tstamp(es, tstamp) \
422 	__ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
423 
424 /*
425  * The del_gendisk() function uninitializes the disk-specific data
426  * structures, including the bdi structure, without telling anyone
427  * else.  Once this happens, any attempt to call mark_buffer_dirty()
428  * (for example, by ext4_commit_super), will cause a kernel OOPS.
429  * This is a kludge to prevent these oops until we can put in a proper
430  * hook in del_gendisk() to inform the VFS and file system layers.
431  */
432 static int block_device_ejected(struct super_block *sb)
433 {
434 	struct inode *bd_inode = sb->s_bdev->bd_inode;
435 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
436 
437 	return bdi->dev == NULL;
438 }
439 
440 static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
441 {
442 	struct super_block		*sb = journal->j_private;
443 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
444 	int				error = is_journal_aborted(journal);
445 	struct ext4_journal_cb_entry	*jce;
446 
447 	BUG_ON(txn->t_state == T_FINISHED);
448 
449 	ext4_process_freed_data(sb, txn->t_tid);
450 
451 	spin_lock(&sbi->s_md_lock);
452 	while (!list_empty(&txn->t_private_list)) {
453 		jce = list_entry(txn->t_private_list.next,
454 				 struct ext4_journal_cb_entry, jce_list);
455 		list_del_init(&jce->jce_list);
456 		spin_unlock(&sbi->s_md_lock);
457 		jce->jce_func(sb, jce, error);
458 		spin_lock(&sbi->s_md_lock);
459 	}
460 	spin_unlock(&sbi->s_md_lock);
461 }
462 
463 /*
464  * This writepage callback for write_cache_pages()
465  * takes care of a few cases after page cleaning.
466  *
467  * write_cache_pages() already checks for dirty pages
468  * and calls clear_page_dirty_for_io(), which we want,
469  * to write protect the pages.
470  *
471  * However, we may have to redirty a page (see below.)
472  */
473 static int ext4_journalled_writepage_callback(struct page *page,
474 					      struct writeback_control *wbc,
475 					      void *data)
476 {
477 	transaction_t *transaction = (transaction_t *) data;
478 	struct buffer_head *bh, *head;
479 	struct journal_head *jh;
480 
481 	bh = head = page_buffers(page);
482 	do {
483 		/*
484 		 * We have to redirty a page in these cases:
485 		 * 1) If buffer is dirty, it means the page was dirty because it
486 		 * contains a buffer that needs checkpointing. So the dirty bit
487 		 * needs to be preserved so that checkpointing writes the buffer
488 		 * properly.
489 		 * 2) If buffer is not part of the committing transaction
490 		 * (we may have just accidentally come across this buffer because
491 		 * inode range tracking is not exact) or if the currently running
492 		 * transaction already contains this buffer as well, dirty bit
493 		 * needs to be preserved so that the buffer gets writeprotected
494 		 * properly on running transaction's commit.
495 		 */
496 		jh = bh2jh(bh);
497 		if (buffer_dirty(bh) ||
498 		    (jh && (jh->b_transaction != transaction ||
499 			    jh->b_next_transaction))) {
500 			redirty_page_for_writepage(wbc, page);
501 			goto out;
502 		}
503 	} while ((bh = bh->b_this_page) != head);
504 
505 out:
506 	return AOP_WRITEPAGE_ACTIVATE;
507 }
508 
509 static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
510 {
511 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
512 	struct writeback_control wbc = {
513 		.sync_mode =  WB_SYNC_ALL,
514 		.nr_to_write = LONG_MAX,
515 		.range_start = jinode->i_dirty_start,
516 		.range_end = jinode->i_dirty_end,
517         };
518 
519 	return write_cache_pages(mapping, &wbc,
520 				 ext4_journalled_writepage_callback,
521 				 jinode->i_transaction);
522 }
523 
524 static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
525 {
526 	int ret;
527 
528 	if (ext4_should_journal_data(jinode->i_vfs_inode))
529 		ret = ext4_journalled_submit_inode_data_buffers(jinode);
530 	else
531 		ret = jbd2_journal_submit_inode_data_buffers(jinode);
532 
533 	return ret;
534 }
535 
536 static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
537 {
538 	int ret = 0;
539 
540 	if (!ext4_should_journal_data(jinode->i_vfs_inode))
541 		ret = jbd2_journal_finish_inode_data_buffers(jinode);
542 
543 	return ret;
544 }
545 
546 static bool system_going_down(void)
547 {
548 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
549 		|| system_state == SYSTEM_RESTART;
550 }
551 
552 struct ext4_err_translation {
553 	int code;
554 	int errno;
555 };
556 
557 #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
558 
559 static struct ext4_err_translation err_translation[] = {
560 	EXT4_ERR_TRANSLATE(EIO),
561 	EXT4_ERR_TRANSLATE(ENOMEM),
562 	EXT4_ERR_TRANSLATE(EFSBADCRC),
563 	EXT4_ERR_TRANSLATE(EFSCORRUPTED),
564 	EXT4_ERR_TRANSLATE(ENOSPC),
565 	EXT4_ERR_TRANSLATE(ENOKEY),
566 	EXT4_ERR_TRANSLATE(EROFS),
567 	EXT4_ERR_TRANSLATE(EFBIG),
568 	EXT4_ERR_TRANSLATE(EEXIST),
569 	EXT4_ERR_TRANSLATE(ERANGE),
570 	EXT4_ERR_TRANSLATE(EOVERFLOW),
571 	EXT4_ERR_TRANSLATE(EBUSY),
572 	EXT4_ERR_TRANSLATE(ENOTDIR),
573 	EXT4_ERR_TRANSLATE(ENOTEMPTY),
574 	EXT4_ERR_TRANSLATE(ESHUTDOWN),
575 	EXT4_ERR_TRANSLATE(EFAULT),
576 };
577 
578 static int ext4_errno_to_code(int errno)
579 {
580 	int i;
581 
582 	for (i = 0; i < ARRAY_SIZE(err_translation); i++)
583 		if (err_translation[i].errno == errno)
584 			return err_translation[i].code;
585 	return EXT4_ERR_UNKNOWN;
586 }
587 
588 static void save_error_info(struct super_block *sb, int error,
589 			    __u32 ino, __u64 block,
590 			    const char *func, unsigned int line)
591 {
592 	struct ext4_sb_info *sbi = EXT4_SB(sb);
593 
594 	/* We default to EFSCORRUPTED error... */
595 	if (error == 0)
596 		error = EFSCORRUPTED;
597 
598 	spin_lock(&sbi->s_error_lock);
599 	sbi->s_add_error_count++;
600 	sbi->s_last_error_code = error;
601 	sbi->s_last_error_line = line;
602 	sbi->s_last_error_ino = ino;
603 	sbi->s_last_error_block = block;
604 	sbi->s_last_error_func = func;
605 	sbi->s_last_error_time = ktime_get_real_seconds();
606 	if (!sbi->s_first_error_time) {
607 		sbi->s_first_error_code = error;
608 		sbi->s_first_error_line = line;
609 		sbi->s_first_error_ino = ino;
610 		sbi->s_first_error_block = block;
611 		sbi->s_first_error_func = func;
612 		sbi->s_first_error_time = sbi->s_last_error_time;
613 	}
614 	spin_unlock(&sbi->s_error_lock);
615 }
616 
617 /* Deal with the reporting of failure conditions on a filesystem such as
618  * inconsistencies detected or read IO failures.
619  *
620  * On ext2, we can store the error state of the filesystem in the
621  * superblock.  That is not possible on ext4, because we may have other
622  * write ordering constraints on the superblock which prevent us from
623  * writing it out straight away; and given that the journal is about to
624  * be aborted, we can't rely on the current, or future, transactions to
625  * write out the superblock safely.
626  *
627  * We'll just use the jbd2_journal_abort() error code to record an error in
628  * the journal instead.  On recovery, the journal will complain about
629  * that error until we've noted it down and cleared it.
630  *
631  * If force_ro is set, we unconditionally force the filesystem into an
632  * ABORT|READONLY state, unless the error response on the fs has been set to
633  * panic in which case we take the easy way out and panic immediately. This is
634  * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
635  * at a critical moment in log management.
636  */
637 static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
638 			      __u32 ino, __u64 block,
639 			      const char *func, unsigned int line)
640 {
641 	journal_t *journal = EXT4_SB(sb)->s_journal;
642 	bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
643 
644 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
645 	if (test_opt(sb, WARN_ON_ERROR))
646 		WARN_ON_ONCE(1);
647 
648 	if (!continue_fs && !sb_rdonly(sb)) {
649 		ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
650 		if (journal)
651 			jbd2_journal_abort(journal, -EIO);
652 	}
653 
654 	if (!bdev_read_only(sb->s_bdev)) {
655 		save_error_info(sb, error, ino, block, func, line);
656 		/*
657 		 * In case the fs should keep running, we need to writeout
658 		 * superblock through the journal. Due to lock ordering
659 		 * constraints, it may not be safe to do it right here so we
660 		 * defer superblock flushing to a workqueue.
661 		 */
662 		if (continue_fs && journal)
663 			schedule_work(&EXT4_SB(sb)->s_error_work);
664 		else
665 			ext4_commit_super(sb);
666 	}
667 
668 	/*
669 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
670 	 * could panic during 'reboot -f' as the underlying device got already
671 	 * disabled.
672 	 */
673 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
674 		panic("EXT4-fs (device %s): panic forced after error\n",
675 			sb->s_id);
676 	}
677 
678 	if (sb_rdonly(sb) || continue_fs)
679 		return;
680 
681 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
682 	/*
683 	 * Make sure updated value of ->s_mount_flags will be visible before
684 	 * ->s_flags update
685 	 */
686 	smp_wmb();
687 	sb->s_flags |= SB_RDONLY;
688 }
689 
690 static void flush_stashed_error_work(struct work_struct *work)
691 {
692 	struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
693 						s_error_work);
694 	journal_t *journal = sbi->s_journal;
695 	handle_t *handle;
696 
697 	/*
698 	 * If the journal is still running, we have to write out superblock
699 	 * through the journal to avoid collisions of other journalled sb
700 	 * updates.
701 	 *
702 	 * We use directly jbd2 functions here to avoid recursing back into
703 	 * ext4 error handling code during handling of previous errors.
704 	 */
705 	if (!sb_rdonly(sbi->s_sb) && journal) {
706 		struct buffer_head *sbh = sbi->s_sbh;
707 		handle = jbd2_journal_start(journal, 1);
708 		if (IS_ERR(handle))
709 			goto write_directly;
710 		if (jbd2_journal_get_write_access(handle, sbh)) {
711 			jbd2_journal_stop(handle);
712 			goto write_directly;
713 		}
714 		ext4_update_super(sbi->s_sb);
715 		if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
716 			ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
717 				 "superblock detected");
718 			clear_buffer_write_io_error(sbh);
719 			set_buffer_uptodate(sbh);
720 		}
721 
722 		if (jbd2_journal_dirty_metadata(handle, sbh)) {
723 			jbd2_journal_stop(handle);
724 			goto write_directly;
725 		}
726 		jbd2_journal_stop(handle);
727 		ext4_notify_error_sysfs(sbi);
728 		return;
729 	}
730 write_directly:
731 	/*
732 	 * Write through journal failed. Write sb directly to get error info
733 	 * out and hope for the best.
734 	 */
735 	ext4_commit_super(sbi->s_sb);
736 	ext4_notify_error_sysfs(sbi);
737 }
738 
739 #define ext4_error_ratelimit(sb)					\
740 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
741 			     "EXT4-fs error")
742 
743 void __ext4_error(struct super_block *sb, const char *function,
744 		  unsigned int line, bool force_ro, int error, __u64 block,
745 		  const char *fmt, ...)
746 {
747 	struct va_format vaf;
748 	va_list args;
749 
750 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
751 		return;
752 
753 	trace_ext4_error(sb, function, line);
754 	if (ext4_error_ratelimit(sb)) {
755 		va_start(args, fmt);
756 		vaf.fmt = fmt;
757 		vaf.va = &args;
758 		printk(KERN_CRIT
759 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
760 		       sb->s_id, function, line, current->comm, &vaf);
761 		va_end(args);
762 	}
763 	fsnotify_sb_error(sb, NULL, error ? error : EFSCORRUPTED);
764 
765 	ext4_handle_error(sb, force_ro, error, 0, block, function, line);
766 }
767 
768 void __ext4_error_inode(struct inode *inode, const char *function,
769 			unsigned int line, ext4_fsblk_t block, int error,
770 			const char *fmt, ...)
771 {
772 	va_list args;
773 	struct va_format vaf;
774 
775 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
776 		return;
777 
778 	trace_ext4_error(inode->i_sb, function, line);
779 	if (ext4_error_ratelimit(inode->i_sb)) {
780 		va_start(args, fmt);
781 		vaf.fmt = fmt;
782 		vaf.va = &args;
783 		if (block)
784 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
785 			       "inode #%lu: block %llu: comm %s: %pV\n",
786 			       inode->i_sb->s_id, function, line, inode->i_ino,
787 			       block, current->comm, &vaf);
788 		else
789 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
790 			       "inode #%lu: comm %s: %pV\n",
791 			       inode->i_sb->s_id, function, line, inode->i_ino,
792 			       current->comm, &vaf);
793 		va_end(args);
794 	}
795 	fsnotify_sb_error(inode->i_sb, inode, error ? error : EFSCORRUPTED);
796 
797 	ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
798 			  function, line);
799 }
800 
801 void __ext4_error_file(struct file *file, const char *function,
802 		       unsigned int line, ext4_fsblk_t block,
803 		       const char *fmt, ...)
804 {
805 	va_list args;
806 	struct va_format vaf;
807 	struct inode *inode = file_inode(file);
808 	char pathname[80], *path;
809 
810 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
811 		return;
812 
813 	trace_ext4_error(inode->i_sb, function, line);
814 	if (ext4_error_ratelimit(inode->i_sb)) {
815 		path = file_path(file, pathname, sizeof(pathname));
816 		if (IS_ERR(path))
817 			path = "(unknown)";
818 		va_start(args, fmt);
819 		vaf.fmt = fmt;
820 		vaf.va = &args;
821 		if (block)
822 			printk(KERN_CRIT
823 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
824 			       "block %llu: comm %s: path %s: %pV\n",
825 			       inode->i_sb->s_id, function, line, inode->i_ino,
826 			       block, current->comm, path, &vaf);
827 		else
828 			printk(KERN_CRIT
829 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
830 			       "comm %s: path %s: %pV\n",
831 			       inode->i_sb->s_id, function, line, inode->i_ino,
832 			       current->comm, path, &vaf);
833 		va_end(args);
834 	}
835 	fsnotify_sb_error(inode->i_sb, inode, EFSCORRUPTED);
836 
837 	ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
838 			  function, line);
839 }
840 
841 const char *ext4_decode_error(struct super_block *sb, int errno,
842 			      char nbuf[16])
843 {
844 	char *errstr = NULL;
845 
846 	switch (errno) {
847 	case -EFSCORRUPTED:
848 		errstr = "Corrupt filesystem";
849 		break;
850 	case -EFSBADCRC:
851 		errstr = "Filesystem failed CRC";
852 		break;
853 	case -EIO:
854 		errstr = "IO failure";
855 		break;
856 	case -ENOMEM:
857 		errstr = "Out of memory";
858 		break;
859 	case -EROFS:
860 		if (!sb || (EXT4_SB(sb)->s_journal &&
861 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
862 			errstr = "Journal has aborted";
863 		else
864 			errstr = "Readonly filesystem";
865 		break;
866 	default:
867 		/* If the caller passed in an extra buffer for unknown
868 		 * errors, textualise them now.  Else we just return
869 		 * NULL. */
870 		if (nbuf) {
871 			/* Check for truncated error codes... */
872 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
873 				errstr = nbuf;
874 		}
875 		break;
876 	}
877 
878 	return errstr;
879 }
880 
881 /* __ext4_std_error decodes expected errors from journaling functions
882  * automatically and invokes the appropriate error response.  */
883 
884 void __ext4_std_error(struct super_block *sb, const char *function,
885 		      unsigned int line, int errno)
886 {
887 	char nbuf[16];
888 	const char *errstr;
889 
890 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
891 		return;
892 
893 	/* Special case: if the error is EROFS, and we're not already
894 	 * inside a transaction, then there's really no point in logging
895 	 * an error. */
896 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
897 		return;
898 
899 	if (ext4_error_ratelimit(sb)) {
900 		errstr = ext4_decode_error(sb, errno, nbuf);
901 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
902 		       sb->s_id, function, line, errstr);
903 	}
904 	fsnotify_sb_error(sb, NULL, errno ? errno : EFSCORRUPTED);
905 
906 	ext4_handle_error(sb, false, -errno, 0, 0, function, line);
907 }
908 
909 void __ext4_msg(struct super_block *sb,
910 		const char *prefix, const char *fmt, ...)
911 {
912 	struct va_format vaf;
913 	va_list args;
914 
915 	atomic_inc(&EXT4_SB(sb)->s_msg_count);
916 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
917 		return;
918 
919 	va_start(args, fmt);
920 	vaf.fmt = fmt;
921 	vaf.va = &args;
922 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
923 	va_end(args);
924 }
925 
926 static int ext4_warning_ratelimit(struct super_block *sb)
927 {
928 	atomic_inc(&EXT4_SB(sb)->s_warning_count);
929 	return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
930 			    "EXT4-fs warning");
931 }
932 
933 void __ext4_warning(struct super_block *sb, const char *function,
934 		    unsigned int line, const char *fmt, ...)
935 {
936 	struct va_format vaf;
937 	va_list args;
938 
939 	if (!ext4_warning_ratelimit(sb))
940 		return;
941 
942 	va_start(args, fmt);
943 	vaf.fmt = fmt;
944 	vaf.va = &args;
945 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
946 	       sb->s_id, function, line, &vaf);
947 	va_end(args);
948 }
949 
950 void __ext4_warning_inode(const struct inode *inode, const char *function,
951 			  unsigned int line, const char *fmt, ...)
952 {
953 	struct va_format vaf;
954 	va_list args;
955 
956 	if (!ext4_warning_ratelimit(inode->i_sb))
957 		return;
958 
959 	va_start(args, fmt);
960 	vaf.fmt = fmt;
961 	vaf.va = &args;
962 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
963 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
964 	       function, line, inode->i_ino, current->comm, &vaf);
965 	va_end(args);
966 }
967 
968 void __ext4_grp_locked_error(const char *function, unsigned int line,
969 			     struct super_block *sb, ext4_group_t grp,
970 			     unsigned long ino, ext4_fsblk_t block,
971 			     const char *fmt, ...)
972 __releases(bitlock)
973 __acquires(bitlock)
974 {
975 	struct va_format vaf;
976 	va_list args;
977 
978 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
979 		return;
980 
981 	trace_ext4_error(sb, function, line);
982 	if (ext4_error_ratelimit(sb)) {
983 		va_start(args, fmt);
984 		vaf.fmt = fmt;
985 		vaf.va = &args;
986 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
987 		       sb->s_id, function, line, grp);
988 		if (ino)
989 			printk(KERN_CONT "inode %lu: ", ino);
990 		if (block)
991 			printk(KERN_CONT "block %llu:",
992 			       (unsigned long long) block);
993 		printk(KERN_CONT "%pV\n", &vaf);
994 		va_end(args);
995 	}
996 
997 	if (test_opt(sb, ERRORS_CONT)) {
998 		if (test_opt(sb, WARN_ON_ERROR))
999 			WARN_ON_ONCE(1);
1000 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
1001 		if (!bdev_read_only(sb->s_bdev)) {
1002 			save_error_info(sb, EFSCORRUPTED, ino, block, function,
1003 					line);
1004 			schedule_work(&EXT4_SB(sb)->s_error_work);
1005 		}
1006 		return;
1007 	}
1008 	ext4_unlock_group(sb, grp);
1009 	ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
1010 	/*
1011 	 * We only get here in the ERRORS_RO case; relocking the group
1012 	 * may be dangerous, but nothing bad will happen since the
1013 	 * filesystem will have already been marked read/only and the
1014 	 * journal has been aborted.  We return 1 as a hint to callers
1015 	 * who might what to use the return value from
1016 	 * ext4_grp_locked_error() to distinguish between the
1017 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
1018 	 * aggressively from the ext4 function in question, with a
1019 	 * more appropriate error code.
1020 	 */
1021 	ext4_lock_group(sb, grp);
1022 	return;
1023 }
1024 
1025 void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1026 				     ext4_group_t group,
1027 				     unsigned int flags)
1028 {
1029 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1030 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1031 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
1032 	int ret;
1033 
1034 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
1035 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1036 					    &grp->bb_state);
1037 		if (!ret)
1038 			percpu_counter_sub(&sbi->s_freeclusters_counter,
1039 					   grp->bb_free);
1040 	}
1041 
1042 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
1043 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
1044 					    &grp->bb_state);
1045 		if (!ret && gdp) {
1046 			int count;
1047 
1048 			count = ext4_free_inodes_count(sb, gdp);
1049 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1050 					   count);
1051 		}
1052 	}
1053 }
1054 
1055 void ext4_update_dynamic_rev(struct super_block *sb)
1056 {
1057 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1058 
1059 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
1060 		return;
1061 
1062 	ext4_warning(sb,
1063 		     "updating to rev %d because of new feature flag, "
1064 		     "running e2fsck is recommended",
1065 		     EXT4_DYNAMIC_REV);
1066 
1067 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1068 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1069 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1070 	/* leave es->s_feature_*compat flags alone */
1071 	/* es->s_uuid will be set by e2fsck if empty */
1072 
1073 	/*
1074 	 * The rest of the superblock fields should be zero, and if not it
1075 	 * means they are likely already in use, so leave them alone.  We
1076 	 * can leave it up to e2fsck to clean up any inconsistencies there.
1077 	 */
1078 }
1079 
1080 /*
1081  * Open the external journal device
1082  */
1083 static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1084 {
1085 	struct block_device *bdev;
1086 
1087 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1088 	if (IS_ERR(bdev))
1089 		goto fail;
1090 	return bdev;
1091 
1092 fail:
1093 	ext4_msg(sb, KERN_ERR,
1094 		 "failed to open journal device unknown-block(%u,%u) %ld",
1095 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1096 	return NULL;
1097 }
1098 
1099 /*
1100  * Release the journal device
1101  */
1102 static void ext4_blkdev_put(struct block_device *bdev)
1103 {
1104 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1105 }
1106 
1107 static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
1108 {
1109 	struct block_device *bdev;
1110 	bdev = sbi->s_journal_bdev;
1111 	if (bdev) {
1112 		ext4_blkdev_put(bdev);
1113 		sbi->s_journal_bdev = NULL;
1114 	}
1115 }
1116 
1117 static inline struct inode *orphan_list_entry(struct list_head *l)
1118 {
1119 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1120 }
1121 
1122 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1123 {
1124 	struct list_head *l;
1125 
1126 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1127 		 le32_to_cpu(sbi->s_es->s_last_orphan));
1128 
1129 	printk(KERN_ERR "sb_info orphan list:\n");
1130 	list_for_each(l, &sbi->s_orphan) {
1131 		struct inode *inode = orphan_list_entry(l);
1132 		printk(KERN_ERR "  "
1133 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1134 		       inode->i_sb->s_id, inode->i_ino, inode,
1135 		       inode->i_mode, inode->i_nlink,
1136 		       NEXT_ORPHAN(inode));
1137 	}
1138 }
1139 
1140 #ifdef CONFIG_QUOTA
1141 static int ext4_quota_off(struct super_block *sb, int type);
1142 
1143 static inline void ext4_quota_off_umount(struct super_block *sb)
1144 {
1145 	int type;
1146 
1147 	/* Use our quota_off function to clear inode flags etc. */
1148 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
1149 		ext4_quota_off(sb, type);
1150 }
1151 
1152 /*
1153  * This is a helper function which is used in the mount/remount
1154  * codepaths (which holds s_umount) to fetch the quota file name.
1155  */
1156 static inline char *get_qf_name(struct super_block *sb,
1157 				struct ext4_sb_info *sbi,
1158 				int type)
1159 {
1160 	return rcu_dereference_protected(sbi->s_qf_names[type],
1161 					 lockdep_is_held(&sb->s_umount));
1162 }
1163 #else
1164 static inline void ext4_quota_off_umount(struct super_block *sb)
1165 {
1166 }
1167 #endif
1168 
1169 static void ext4_put_super(struct super_block *sb)
1170 {
1171 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1172 	struct ext4_super_block *es = sbi->s_es;
1173 	struct buffer_head **group_desc;
1174 	struct flex_groups **flex_groups;
1175 	int aborted = 0;
1176 	int i, err;
1177 
1178 	ext4_unregister_li_request(sb);
1179 	ext4_quota_off_umount(sb);
1180 
1181 	flush_work(&sbi->s_error_work);
1182 	destroy_workqueue(sbi->rsv_conversion_wq);
1183 	ext4_release_orphan_info(sb);
1184 
1185 	/*
1186 	 * Unregister sysfs before destroying jbd2 journal.
1187 	 * Since we could still access attr_journal_task attribute via sysfs
1188 	 * path which could have sbi->s_journal->j_task as NULL
1189 	 */
1190 	ext4_unregister_sysfs(sb);
1191 
1192 	if (sbi->s_journal) {
1193 		aborted = is_journal_aborted(sbi->s_journal);
1194 		err = jbd2_journal_destroy(sbi->s_journal);
1195 		sbi->s_journal = NULL;
1196 		if ((err < 0) && !aborted) {
1197 			ext4_abort(sb, -err, "Couldn't clean up the journal");
1198 		}
1199 	}
1200 
1201 	ext4_es_unregister_shrinker(sbi);
1202 	del_timer_sync(&sbi->s_err_report);
1203 	ext4_release_system_zone(sb);
1204 	ext4_mb_release(sb);
1205 	ext4_ext_release(sb);
1206 
1207 	if (!sb_rdonly(sb) && !aborted) {
1208 		ext4_clear_feature_journal_needs_recovery(sb);
1209 		ext4_clear_feature_orphan_present(sb);
1210 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1211 	}
1212 	if (!sb_rdonly(sb))
1213 		ext4_commit_super(sb);
1214 
1215 	rcu_read_lock();
1216 	group_desc = rcu_dereference(sbi->s_group_desc);
1217 	for (i = 0; i < sbi->s_gdb_count; i++)
1218 		brelse(group_desc[i]);
1219 	kvfree(group_desc);
1220 	flex_groups = rcu_dereference(sbi->s_flex_groups);
1221 	if (flex_groups) {
1222 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
1223 			kvfree(flex_groups[i]);
1224 		kvfree(flex_groups);
1225 	}
1226 	rcu_read_unlock();
1227 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1228 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1229 	percpu_counter_destroy(&sbi->s_dirs_counter);
1230 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1231 	percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
1232 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
1233 #ifdef CONFIG_QUOTA
1234 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
1235 		kfree(get_qf_name(sb, sbi, i));
1236 #endif
1237 
1238 	/* Debugging code just in case the in-memory inode orphan list
1239 	 * isn't empty.  The on-disk one can be non-empty if we've
1240 	 * detected an error and taken the fs readonly, but the
1241 	 * in-memory list had better be clean by this point. */
1242 	if (!list_empty(&sbi->s_orphan))
1243 		dump_orphan_list(sb, sbi);
1244 	ASSERT(list_empty(&sbi->s_orphan));
1245 
1246 	sync_blockdev(sb->s_bdev);
1247 	invalidate_bdev(sb->s_bdev);
1248 	if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
1249 		/*
1250 		 * Invalidate the journal device's buffers.  We don't want them
1251 		 * floating about in memory - the physical journal device may
1252 		 * hotswapped, and it breaks the `ro-after' testing code.
1253 		 */
1254 		sync_blockdev(sbi->s_journal_bdev);
1255 		invalidate_bdev(sbi->s_journal_bdev);
1256 		ext4_blkdev_remove(sbi);
1257 	}
1258 
1259 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1260 	sbi->s_ea_inode_cache = NULL;
1261 
1262 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
1263 	sbi->s_ea_block_cache = NULL;
1264 
1265 	ext4_stop_mmpd(sbi);
1266 
1267 	brelse(sbi->s_sbh);
1268 	sb->s_fs_info = NULL;
1269 	/*
1270 	 * Now that we are completely done shutting down the
1271 	 * superblock, we need to actually destroy the kobject.
1272 	 */
1273 	kobject_put(&sbi->s_kobj);
1274 	wait_for_completion(&sbi->s_kobj_unregister);
1275 	if (sbi->s_chksum_driver)
1276 		crypto_free_shash(sbi->s_chksum_driver);
1277 	kfree(sbi->s_blockgroup_lock);
1278 	fs_put_dax(sbi->s_daxdev);
1279 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
1280 #ifdef CONFIG_UNICODE
1281 	utf8_unload(sb->s_encoding);
1282 #endif
1283 	kfree(sbi);
1284 }
1285 
1286 static struct kmem_cache *ext4_inode_cachep;
1287 
1288 /*
1289  * Called inside transaction, so use GFP_NOFS
1290  */
1291 static struct inode *ext4_alloc_inode(struct super_block *sb)
1292 {
1293 	struct ext4_inode_info *ei;
1294 
1295 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1296 	if (!ei)
1297 		return NULL;
1298 
1299 	inode_set_iversion(&ei->vfs_inode, 1);
1300 	spin_lock_init(&ei->i_raw_lock);
1301 	INIT_LIST_HEAD(&ei->i_prealloc_list);
1302 	atomic_set(&ei->i_prealloc_active, 0);
1303 	spin_lock_init(&ei->i_prealloc_lock);
1304 	ext4_es_init_tree(&ei->i_es_tree);
1305 	rwlock_init(&ei->i_es_lock);
1306 	INIT_LIST_HEAD(&ei->i_es_list);
1307 	ei->i_es_all_nr = 0;
1308 	ei->i_es_shk_nr = 0;
1309 	ei->i_es_shrink_lblk = 0;
1310 	ei->i_reserved_data_blocks = 0;
1311 	spin_lock_init(&(ei->i_block_reservation_lock));
1312 	ext4_init_pending_tree(&ei->i_pending_tree);
1313 #ifdef CONFIG_QUOTA
1314 	ei->i_reserved_quota = 0;
1315 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1316 #endif
1317 	ei->jinode = NULL;
1318 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1319 	spin_lock_init(&ei->i_completed_io_lock);
1320 	ei->i_sync_tid = 0;
1321 	ei->i_datasync_tid = 0;
1322 	atomic_set(&ei->i_unwritten, 0);
1323 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1324 	ext4_fc_init_inode(&ei->vfs_inode);
1325 	mutex_init(&ei->i_fc_lock);
1326 	return &ei->vfs_inode;
1327 }
1328 
1329 static int ext4_drop_inode(struct inode *inode)
1330 {
1331 	int drop = generic_drop_inode(inode);
1332 
1333 	if (!drop)
1334 		drop = fscrypt_drop_inode(inode);
1335 
1336 	trace_ext4_drop_inode(inode, drop);
1337 	return drop;
1338 }
1339 
1340 static void ext4_free_in_core_inode(struct inode *inode)
1341 {
1342 	fscrypt_free_inode(inode);
1343 	if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1344 		pr_warn("%s: inode %ld still in fc list",
1345 			__func__, inode->i_ino);
1346 	}
1347 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1348 }
1349 
1350 static void ext4_destroy_inode(struct inode *inode)
1351 {
1352 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1353 		ext4_msg(inode->i_sb, KERN_ERR,
1354 			 "Inode %lu (%p): orphan list check failed!",
1355 			 inode->i_ino, EXT4_I(inode));
1356 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
1357 				EXT4_I(inode), sizeof(struct ext4_inode_info),
1358 				true);
1359 		dump_stack();
1360 	}
1361 
1362 	if (EXT4_I(inode)->i_reserved_data_blocks)
1363 		ext4_msg(inode->i_sb, KERN_ERR,
1364 			 "Inode %lu (%p): i_reserved_data_blocks (%u) not cleared!",
1365 			 inode->i_ino, EXT4_I(inode),
1366 			 EXT4_I(inode)->i_reserved_data_blocks);
1367 }
1368 
1369 static void init_once(void *foo)
1370 {
1371 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1372 
1373 	INIT_LIST_HEAD(&ei->i_orphan);
1374 	init_rwsem(&ei->xattr_sem);
1375 	init_rwsem(&ei->i_data_sem);
1376 	inode_init_once(&ei->vfs_inode);
1377 	ext4_fc_init_inode(&ei->vfs_inode);
1378 }
1379 
1380 static int __init init_inodecache(void)
1381 {
1382 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1383 				sizeof(struct ext4_inode_info), 0,
1384 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1385 					SLAB_ACCOUNT),
1386 				offsetof(struct ext4_inode_info, i_data),
1387 				sizeof_field(struct ext4_inode_info, i_data),
1388 				init_once);
1389 	if (ext4_inode_cachep == NULL)
1390 		return -ENOMEM;
1391 	return 0;
1392 }
1393 
1394 static void destroy_inodecache(void)
1395 {
1396 	/*
1397 	 * Make sure all delayed rcu free inodes are flushed before we
1398 	 * destroy cache.
1399 	 */
1400 	rcu_barrier();
1401 	kmem_cache_destroy(ext4_inode_cachep);
1402 }
1403 
1404 void ext4_clear_inode(struct inode *inode)
1405 {
1406 	ext4_fc_del(inode);
1407 	invalidate_inode_buffers(inode);
1408 	clear_inode(inode);
1409 	ext4_discard_preallocations(inode, 0);
1410 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1411 	dquot_drop(inode);
1412 	if (EXT4_I(inode)->jinode) {
1413 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
1414 					       EXT4_I(inode)->jinode);
1415 		jbd2_free_inode(EXT4_I(inode)->jinode);
1416 		EXT4_I(inode)->jinode = NULL;
1417 	}
1418 	fscrypt_put_encryption_info(inode);
1419 	fsverity_cleanup_inode(inode);
1420 }
1421 
1422 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
1423 					u64 ino, u32 generation)
1424 {
1425 	struct inode *inode;
1426 
1427 	/*
1428 	 * Currently we don't know the generation for parent directory, so
1429 	 * a generation of 0 means "accept any"
1430 	 */
1431 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
1432 	if (IS_ERR(inode))
1433 		return ERR_CAST(inode);
1434 	if (generation && inode->i_generation != generation) {
1435 		iput(inode);
1436 		return ERR_PTR(-ESTALE);
1437 	}
1438 
1439 	return inode;
1440 }
1441 
1442 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
1443 					int fh_len, int fh_type)
1444 {
1445 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1446 				    ext4_nfs_get_inode);
1447 }
1448 
1449 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
1450 					int fh_len, int fh_type)
1451 {
1452 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1453 				    ext4_nfs_get_inode);
1454 }
1455 
1456 static int ext4_nfs_commit_metadata(struct inode *inode)
1457 {
1458 	struct writeback_control wbc = {
1459 		.sync_mode = WB_SYNC_ALL
1460 	};
1461 
1462 	trace_ext4_nfs_commit_metadata(inode);
1463 	return ext4_write_inode(inode, &wbc);
1464 }
1465 
1466 #ifdef CONFIG_FS_ENCRYPTION
1467 static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1468 {
1469 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1470 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1471 }
1472 
1473 static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1474 							void *fs_data)
1475 {
1476 	handle_t *handle = fs_data;
1477 	int res, res2, credits, retries = 0;
1478 
1479 	/*
1480 	 * Encrypting the root directory is not allowed because e2fsck expects
1481 	 * lost+found to exist and be unencrypted, and encrypting the root
1482 	 * directory would imply encrypting the lost+found directory as well as
1483 	 * the filename "lost+found" itself.
1484 	 */
1485 	if (inode->i_ino == EXT4_ROOT_INO)
1486 		return -EPERM;
1487 
1488 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
1489 		return -EINVAL;
1490 
1491 	if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1492 		return -EOPNOTSUPP;
1493 
1494 	res = ext4_convert_inline_data(inode);
1495 	if (res)
1496 		return res;
1497 
1498 	/*
1499 	 * If a journal handle was specified, then the encryption context is
1500 	 * being set on a new inode via inheritance and is part of a larger
1501 	 * transaction to create the inode.  Otherwise the encryption context is
1502 	 * being set on an existing inode in its own transaction.  Only in the
1503 	 * latter case should the "retry on ENOSPC" logic be used.
1504 	 */
1505 
1506 	if (handle) {
1507 		res = ext4_xattr_set_handle(handle, inode,
1508 					    EXT4_XATTR_INDEX_ENCRYPTION,
1509 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1510 					    ctx, len, 0);
1511 		if (!res) {
1512 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1513 			ext4_clear_inode_state(inode,
1514 					EXT4_STATE_MAY_INLINE_DATA);
1515 			/*
1516 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1517 			 * S_DAX may be disabled
1518 			 */
1519 			ext4_set_inode_flags(inode, false);
1520 		}
1521 		return res;
1522 	}
1523 
1524 	res = dquot_initialize(inode);
1525 	if (res)
1526 		return res;
1527 retry:
1528 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1529 				     &credits);
1530 	if (res)
1531 		return res;
1532 
1533 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1534 	if (IS_ERR(handle))
1535 		return PTR_ERR(handle);
1536 
1537 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
1538 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1539 				    ctx, len, 0);
1540 	if (!res) {
1541 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1542 		/*
1543 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1544 		 * S_DAX may be disabled
1545 		 */
1546 		ext4_set_inode_flags(inode, false);
1547 		res = ext4_mark_inode_dirty(handle, inode);
1548 		if (res)
1549 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1550 	}
1551 	res2 = ext4_journal_stop(handle);
1552 
1553 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
1554 		goto retry;
1555 	if (!res)
1556 		res = res2;
1557 	return res;
1558 }
1559 
1560 static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
1561 {
1562 	return EXT4_SB(sb)->s_dummy_enc_policy.policy;
1563 }
1564 
1565 static bool ext4_has_stable_inodes(struct super_block *sb)
1566 {
1567 	return ext4_has_feature_stable_inodes(sb);
1568 }
1569 
1570 static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1571 				       int *ino_bits_ret, int *lblk_bits_ret)
1572 {
1573 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1574 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1575 }
1576 
1577 static const struct fscrypt_operations ext4_cryptops = {
1578 	.key_prefix		= "ext4:",
1579 	.get_context		= ext4_get_context,
1580 	.set_context		= ext4_set_context,
1581 	.get_dummy_policy	= ext4_get_dummy_policy,
1582 	.empty_dir		= ext4_empty_dir,
1583 	.has_stable_inodes	= ext4_has_stable_inodes,
1584 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1585 };
1586 #endif
1587 
1588 #ifdef CONFIG_QUOTA
1589 static const char * const quotatypes[] = INITQFNAMES;
1590 #define QTYPE2NAME(t) (quotatypes[t])
1591 
1592 static int ext4_write_dquot(struct dquot *dquot);
1593 static int ext4_acquire_dquot(struct dquot *dquot);
1594 static int ext4_release_dquot(struct dquot *dquot);
1595 static int ext4_mark_dquot_dirty(struct dquot *dquot);
1596 static int ext4_write_info(struct super_block *sb, int type);
1597 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1598 			 const struct path *path);
1599 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1600 			       size_t len, loff_t off);
1601 static ssize_t ext4_quota_write(struct super_block *sb, int type,
1602 				const char *data, size_t len, loff_t off);
1603 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1604 			     unsigned int flags);
1605 
1606 static struct dquot **ext4_get_dquots(struct inode *inode)
1607 {
1608 	return EXT4_I(inode)->i_dquot;
1609 }
1610 
1611 static const struct dquot_operations ext4_quota_operations = {
1612 	.get_reserved_space	= ext4_get_reserved_space,
1613 	.write_dquot		= ext4_write_dquot,
1614 	.acquire_dquot		= ext4_acquire_dquot,
1615 	.release_dquot		= ext4_release_dquot,
1616 	.mark_dirty		= ext4_mark_dquot_dirty,
1617 	.write_info		= ext4_write_info,
1618 	.alloc_dquot		= dquot_alloc,
1619 	.destroy_dquot		= dquot_destroy,
1620 	.get_projid		= ext4_get_projid,
1621 	.get_inode_usage	= ext4_get_inode_usage,
1622 	.get_next_id		= dquot_get_next_id,
1623 };
1624 
1625 static const struct quotactl_ops ext4_qctl_operations = {
1626 	.quota_on	= ext4_quota_on,
1627 	.quota_off	= ext4_quota_off,
1628 	.quota_sync	= dquot_quota_sync,
1629 	.get_state	= dquot_get_state,
1630 	.set_info	= dquot_set_dqinfo,
1631 	.get_dqblk	= dquot_get_dqblk,
1632 	.set_dqblk	= dquot_set_dqblk,
1633 	.get_nextdqblk	= dquot_get_next_dqblk,
1634 };
1635 #endif
1636 
1637 static const struct super_operations ext4_sops = {
1638 	.alloc_inode	= ext4_alloc_inode,
1639 	.free_inode	= ext4_free_in_core_inode,
1640 	.destroy_inode	= ext4_destroy_inode,
1641 	.write_inode	= ext4_write_inode,
1642 	.dirty_inode	= ext4_dirty_inode,
1643 	.drop_inode	= ext4_drop_inode,
1644 	.evict_inode	= ext4_evict_inode,
1645 	.put_super	= ext4_put_super,
1646 	.sync_fs	= ext4_sync_fs,
1647 	.freeze_fs	= ext4_freeze,
1648 	.unfreeze_fs	= ext4_unfreeze,
1649 	.statfs		= ext4_statfs,
1650 	.remount_fs	= ext4_remount,
1651 	.show_options	= ext4_show_options,
1652 #ifdef CONFIG_QUOTA
1653 	.quota_read	= ext4_quota_read,
1654 	.quota_write	= ext4_quota_write,
1655 	.get_dquots	= ext4_get_dquots,
1656 #endif
1657 };
1658 
1659 static const struct export_operations ext4_export_ops = {
1660 	.fh_to_dentry = ext4_fh_to_dentry,
1661 	.fh_to_parent = ext4_fh_to_parent,
1662 	.get_parent = ext4_get_parent,
1663 	.commit_metadata = ext4_nfs_commit_metadata,
1664 };
1665 
1666 enum {
1667 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1668 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
1669 	Opt_nouid32, Opt_debug, Opt_removed,
1670 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1671 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1672 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1673 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1674 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1675 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1676 	Opt_inlinecrypt,
1677 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1678 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1679 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1680 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
1681 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1682 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1683 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1684 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
1685 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
1686 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1687 	Opt_dioread_nolock, Opt_dioread_lock,
1688 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1689 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1690 	Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
1691 #ifdef CONFIG_EXT4_DEBUG
1692 	Opt_fc_debug_max_replay, Opt_fc_debug_force
1693 #endif
1694 };
1695 
1696 static const match_table_t tokens = {
1697 	{Opt_bsd_df, "bsddf"},
1698 	{Opt_minix_df, "minixdf"},
1699 	{Opt_grpid, "grpid"},
1700 	{Opt_grpid, "bsdgroups"},
1701 	{Opt_nogrpid, "nogrpid"},
1702 	{Opt_nogrpid, "sysvgroups"},
1703 	{Opt_resgid, "resgid=%u"},
1704 	{Opt_resuid, "resuid=%u"},
1705 	{Opt_sb, "sb=%u"},
1706 	{Opt_err_cont, "errors=continue"},
1707 	{Opt_err_panic, "errors=panic"},
1708 	{Opt_err_ro, "errors=remount-ro"},
1709 	{Opt_nouid32, "nouid32"},
1710 	{Opt_debug, "debug"},
1711 	{Opt_removed, "oldalloc"},
1712 	{Opt_removed, "orlov"},
1713 	{Opt_user_xattr, "user_xattr"},
1714 	{Opt_nouser_xattr, "nouser_xattr"},
1715 	{Opt_acl, "acl"},
1716 	{Opt_noacl, "noacl"},
1717 	{Opt_noload, "norecovery"},
1718 	{Opt_noload, "noload"},
1719 	{Opt_removed, "nobh"},
1720 	{Opt_removed, "bh"},
1721 	{Opt_commit, "commit=%u"},
1722 	{Opt_min_batch_time, "min_batch_time=%u"},
1723 	{Opt_max_batch_time, "max_batch_time=%u"},
1724 	{Opt_journal_dev, "journal_dev=%u"},
1725 	{Opt_journal_path, "journal_path=%s"},
1726 	{Opt_journal_checksum, "journal_checksum"},
1727 	{Opt_nojournal_checksum, "nojournal_checksum"},
1728 	{Opt_journal_async_commit, "journal_async_commit"},
1729 	{Opt_abort, "abort"},
1730 	{Opt_data_journal, "data=journal"},
1731 	{Opt_data_ordered, "data=ordered"},
1732 	{Opt_data_writeback, "data=writeback"},
1733 	{Opt_data_err_abort, "data_err=abort"},
1734 	{Opt_data_err_ignore, "data_err=ignore"},
1735 	{Opt_offusrjquota, "usrjquota="},
1736 	{Opt_usrjquota, "usrjquota=%s"},
1737 	{Opt_offgrpjquota, "grpjquota="},
1738 	{Opt_grpjquota, "grpjquota=%s"},
1739 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1740 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1741 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1742 	{Opt_grpquota, "grpquota"},
1743 	{Opt_noquota, "noquota"},
1744 	{Opt_quota, "quota"},
1745 	{Opt_usrquota, "usrquota"},
1746 	{Opt_prjquota, "prjquota"},
1747 	{Opt_barrier, "barrier=%u"},
1748 	{Opt_barrier, "barrier"},
1749 	{Opt_nobarrier, "nobarrier"},
1750 	{Opt_i_version, "i_version"},
1751 	{Opt_dax, "dax"},
1752 	{Opt_dax_always, "dax=always"},
1753 	{Opt_dax_inode, "dax=inode"},
1754 	{Opt_dax_never, "dax=never"},
1755 	{Opt_stripe, "stripe=%u"},
1756 	{Opt_delalloc, "delalloc"},
1757 	{Opt_warn_on_error, "warn_on_error"},
1758 	{Opt_nowarn_on_error, "nowarn_on_error"},
1759 	{Opt_lazytime, "lazytime"},
1760 	{Opt_nolazytime, "nolazytime"},
1761 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1762 	{Opt_nodelalloc, "nodelalloc"},
1763 	{Opt_removed, "mblk_io_submit"},
1764 	{Opt_removed, "nomblk_io_submit"},
1765 	{Opt_block_validity, "block_validity"},
1766 	{Opt_noblock_validity, "noblock_validity"},
1767 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1768 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1769 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
1770 	{Opt_auto_da_alloc, "auto_da_alloc"},
1771 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1772 	{Opt_dioread_nolock, "dioread_nolock"},
1773 	{Opt_dioread_lock, "nodioread_nolock"},
1774 	{Opt_dioread_lock, "dioread_lock"},
1775 	{Opt_discard, "discard"},
1776 	{Opt_nodiscard, "nodiscard"},
1777 	{Opt_init_itable, "init_itable=%u"},
1778 	{Opt_init_itable, "init_itable"},
1779 	{Opt_noinit_itable, "noinit_itable"},
1780 #ifdef CONFIG_EXT4_DEBUG
1781 	{Opt_fc_debug_force, "fc_debug_force"},
1782 	{Opt_fc_debug_max_replay, "fc_debug_max_replay=%u"},
1783 #endif
1784 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1785 	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
1786 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1787 	{Opt_inlinecrypt, "inlinecrypt"},
1788 	{Opt_nombcache, "nombcache"},
1789 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1790 	{Opt_removed, "prefetch_block_bitmaps"},
1791 	{Opt_no_prefetch_block_bitmaps, "no_prefetch_block_bitmaps"},
1792 	{Opt_mb_optimize_scan, "mb_optimize_scan=%d"},
1793 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1794 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1795 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1796 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1797 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1798 	{Opt_err, NULL},
1799 };
1800 
1801 static ext4_fsblk_t get_sb_block(void **data)
1802 {
1803 	ext4_fsblk_t	sb_block;
1804 	char		*options = (char *) *data;
1805 
1806 	if (!options || strncmp(options, "sb=", 3) != 0)
1807 		return 1;	/* Default location */
1808 
1809 	options += 3;
1810 	/* TODO: use simple_strtoll with >32bit ext4 */
1811 	sb_block = simple_strtoul(options, &options, 0);
1812 	if (*options && *options != ',') {
1813 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1814 		       (char *) *data);
1815 		return 1;
1816 	}
1817 	if (*options == ',')
1818 		options++;
1819 	*data = (void *) options;
1820 
1821 	return sb_block;
1822 }
1823 
1824 #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1825 #define DEFAULT_MB_OPTIMIZE_SCAN	(-1)
1826 
1827 static const char deprecated_msg[] =
1828 	"Mount option \"%s\" will be removed by %s\n"
1829 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1830 
1831 #ifdef CONFIG_QUOTA
1832 static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1833 {
1834 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1835 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
1836 	int ret = -1;
1837 
1838 	if (sb_any_quota_loaded(sb) && !old_qname) {
1839 		ext4_msg(sb, KERN_ERR,
1840 			"Cannot change journaled "
1841 			"quota options when quota turned on");
1842 		return -1;
1843 	}
1844 	if (ext4_has_feature_quota(sb)) {
1845 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1846 			 "ignored when QUOTA feature is enabled");
1847 		return 1;
1848 	}
1849 	qname = match_strdup(args);
1850 	if (!qname) {
1851 		ext4_msg(sb, KERN_ERR,
1852 			"Not enough memory for storing quotafile name");
1853 		return -1;
1854 	}
1855 	if (old_qname) {
1856 		if (strcmp(old_qname, qname) == 0)
1857 			ret = 1;
1858 		else
1859 			ext4_msg(sb, KERN_ERR,
1860 				 "%s quota file already specified",
1861 				 QTYPE2NAME(qtype));
1862 		goto errout;
1863 	}
1864 	if (strchr(qname, '/')) {
1865 		ext4_msg(sb, KERN_ERR,
1866 			"quotafile must be on filesystem root");
1867 		goto errout;
1868 	}
1869 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1870 	set_opt(sb, QUOTA);
1871 	return 1;
1872 errout:
1873 	kfree(qname);
1874 	return ret;
1875 }
1876 
1877 static int clear_qf_name(struct super_block *sb, int qtype)
1878 {
1879 
1880 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1881 	char *old_qname = get_qf_name(sb, sbi, qtype);
1882 
1883 	if (sb_any_quota_loaded(sb) && old_qname) {
1884 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1885 			" when quota turned on");
1886 		return -1;
1887 	}
1888 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
1889 	synchronize_rcu();
1890 	kfree(old_qname);
1891 	return 1;
1892 }
1893 #endif
1894 
1895 #define MOPT_SET	0x0001
1896 #define MOPT_CLEAR	0x0002
1897 #define MOPT_NOSUPPORT	0x0004
1898 #define MOPT_EXPLICIT	0x0008
1899 #define MOPT_CLEAR_ERR	0x0010
1900 #define MOPT_GTE0	0x0020
1901 #ifdef CONFIG_QUOTA
1902 #define MOPT_Q		0
1903 #define MOPT_QFMT	0x0040
1904 #else
1905 #define MOPT_Q		MOPT_NOSUPPORT
1906 #define MOPT_QFMT	MOPT_NOSUPPORT
1907 #endif
1908 #define MOPT_DATAJ	0x0080
1909 #define MOPT_NO_EXT2	0x0100
1910 #define MOPT_NO_EXT3	0x0200
1911 #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1912 #define MOPT_STRING	0x0400
1913 #define MOPT_SKIP	0x0800
1914 #define	MOPT_2		0x1000
1915 
1916 static const struct mount_opts {
1917 	int	token;
1918 	int	mount_opt;
1919 	int	flags;
1920 } ext4_mount_opts[] = {
1921 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1922 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1923 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1924 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
1925 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1926 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
1927 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
1928 	 MOPT_EXT4_ONLY | MOPT_SET},
1929 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
1930 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1931 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1932 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
1933 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
1934 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1935 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
1936 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1937 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1938 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1939 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1940 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1941 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1942 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1943 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
1944 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
1945 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1946 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
1947 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
1948 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
1949 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
1950 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
1951 	 MOPT_NO_EXT2},
1952 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
1953 	 MOPT_NO_EXT2},
1954 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1955 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1956 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1957 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1958 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1959 	{Opt_commit, 0, MOPT_GTE0},
1960 	{Opt_max_batch_time, 0, MOPT_GTE0},
1961 	{Opt_min_batch_time, 0, MOPT_GTE0},
1962 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
1963 	{Opt_init_itable, 0, MOPT_GTE0},
1964 	{Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
1965 	{Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
1966 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1967 	{Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
1968 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1969 	{Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
1970 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1971 	{Opt_stripe, 0, MOPT_GTE0},
1972 	{Opt_resuid, 0, MOPT_GTE0},
1973 	{Opt_resgid, 0, MOPT_GTE0},
1974 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
1975 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
1976 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
1977 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1978 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1979 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
1980 	 MOPT_NO_EXT2 | MOPT_DATAJ},
1981 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
1982 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
1983 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1984 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
1985 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
1986 #else
1987 	{Opt_acl, 0, MOPT_NOSUPPORT},
1988 	{Opt_noacl, 0, MOPT_NOSUPPORT},
1989 #endif
1990 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
1991 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1992 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
1993 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
1994 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
1995 							MOPT_SET | MOPT_Q},
1996 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
1997 							MOPT_SET | MOPT_Q},
1998 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
1999 							MOPT_SET | MOPT_Q},
2000 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
2001 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
2002 							MOPT_CLEAR | MOPT_Q},
2003 	{Opt_usrjquota, 0, MOPT_Q | MOPT_STRING},
2004 	{Opt_grpjquota, 0, MOPT_Q | MOPT_STRING},
2005 	{Opt_offusrjquota, 0, MOPT_Q},
2006 	{Opt_offgrpjquota, 0, MOPT_Q},
2007 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
2008 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
2009 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
2010 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
2011 	{Opt_test_dummy_encryption, 0, MOPT_STRING},
2012 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
2013 	{Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
2014 	 MOPT_SET},
2015 	{Opt_mb_optimize_scan, EXT4_MOUNT2_MB_OPTIMIZE_SCAN, MOPT_GTE0},
2016 #ifdef CONFIG_EXT4_DEBUG
2017 	{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
2018 	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
2019 	{Opt_fc_debug_max_replay, 0, MOPT_GTE0},
2020 #endif
2021 	{Opt_err, 0, 0}
2022 };
2023 
2024 #ifdef CONFIG_UNICODE
2025 static const struct ext4_sb_encodings {
2026 	__u16 magic;
2027 	char *name;
2028 	char *version;
2029 } ext4_sb_encoding_map[] = {
2030 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
2031 };
2032 
2033 static int ext4_sb_read_encoding(const struct ext4_super_block *es,
2034 				 const struct ext4_sb_encodings **encoding,
2035 				 __u16 *flags)
2036 {
2037 	__u16 magic = le16_to_cpu(es->s_encoding);
2038 	int i;
2039 
2040 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
2041 		if (magic == ext4_sb_encoding_map[i].magic)
2042 			break;
2043 
2044 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
2045 		return -EINVAL;
2046 
2047 	*encoding = &ext4_sb_encoding_map[i];
2048 	*flags = le16_to_cpu(es->s_encoding_flags);
2049 
2050 	return 0;
2051 }
2052 #endif
2053 
2054 static int ext4_set_test_dummy_encryption(struct super_block *sb,
2055 					  const char *opt,
2056 					  const substring_t *arg,
2057 					  bool is_remount)
2058 {
2059 #ifdef CONFIG_FS_ENCRYPTION
2060 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2061 	int err;
2062 
2063 	/*
2064 	 * This mount option is just for testing, and it's not worthwhile to
2065 	 * implement the extra complexity (e.g. RCU protection) that would be
2066 	 * needed to allow it to be set or changed during remount.  We do allow
2067 	 * it to be specified during remount, but only if there is no change.
2068 	 */
2069 	if (is_remount && !sbi->s_dummy_enc_policy.policy) {
2070 		ext4_msg(sb, KERN_WARNING,
2071 			 "Can't set test_dummy_encryption on remount");
2072 		return -1;
2073 	}
2074 	err = fscrypt_set_test_dummy_encryption(sb, arg->from,
2075 						&sbi->s_dummy_enc_policy);
2076 	if (err) {
2077 		if (err == -EEXIST)
2078 			ext4_msg(sb, KERN_WARNING,
2079 				 "Can't change test_dummy_encryption on remount");
2080 		else if (err == -EINVAL)
2081 			ext4_msg(sb, KERN_WARNING,
2082 				 "Value of option \"%s\" is unrecognized", opt);
2083 		else
2084 			ext4_msg(sb, KERN_WARNING,
2085 				 "Error processing option \"%s\" [%d]",
2086 				 opt, err);
2087 		return -1;
2088 	}
2089 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2090 #else
2091 	ext4_msg(sb, KERN_WARNING,
2092 		 "Test dummy encryption mount option ignored");
2093 #endif
2094 	return 1;
2095 }
2096 
2097 struct ext4_parsed_options {
2098 	unsigned long journal_devnum;
2099 	unsigned int journal_ioprio;
2100 	int mb_optimize_scan;
2101 };
2102 
2103 static int handle_mount_opt(struct super_block *sb, char *opt, int token,
2104 			    substring_t *args, struct ext4_parsed_options *parsed_opts,
2105 			    int is_remount)
2106 {
2107 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2108 	const struct mount_opts *m;
2109 	kuid_t uid;
2110 	kgid_t gid;
2111 	int arg = 0;
2112 
2113 #ifdef CONFIG_QUOTA
2114 	if (token == Opt_usrjquota)
2115 		return set_qf_name(sb, USRQUOTA, &args[0]);
2116 	else if (token == Opt_grpjquota)
2117 		return set_qf_name(sb, GRPQUOTA, &args[0]);
2118 	else if (token == Opt_offusrjquota)
2119 		return clear_qf_name(sb, USRQUOTA);
2120 	else if (token == Opt_offgrpjquota)
2121 		return clear_qf_name(sb, GRPQUOTA);
2122 #endif
2123 	switch (token) {
2124 	case Opt_noacl:
2125 	case Opt_nouser_xattr:
2126 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
2127 		break;
2128 	case Opt_sb:
2129 		return 1;	/* handled by get_sb_block() */
2130 	case Opt_removed:
2131 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
2132 		return 1;
2133 	case Opt_abort:
2134 		ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
2135 		return 1;
2136 	case Opt_i_version:
2137 		sb->s_flags |= SB_I_VERSION;
2138 		return 1;
2139 	case Opt_lazytime:
2140 		sb->s_flags |= SB_LAZYTIME;
2141 		return 1;
2142 	case Opt_nolazytime:
2143 		sb->s_flags &= ~SB_LAZYTIME;
2144 		return 1;
2145 	case Opt_inlinecrypt:
2146 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
2147 		sb->s_flags |= SB_INLINECRYPT;
2148 #else
2149 		ext4_msg(sb, KERN_ERR, "inline encryption not supported");
2150 #endif
2151 		return 1;
2152 	}
2153 
2154 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
2155 		if (token == m->token)
2156 			break;
2157 
2158 	if (m->token == Opt_err) {
2159 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
2160 			 "or missing value", opt);
2161 		return -1;
2162 	}
2163 
2164 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
2165 		ext4_msg(sb, KERN_ERR,
2166 			 "Mount option \"%s\" incompatible with ext2", opt);
2167 		return -1;
2168 	}
2169 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
2170 		ext4_msg(sb, KERN_ERR,
2171 			 "Mount option \"%s\" incompatible with ext3", opt);
2172 		return -1;
2173 	}
2174 
2175 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
2176 		return -1;
2177 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
2178 		return -1;
2179 	if (m->flags & MOPT_EXPLICIT) {
2180 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2181 			set_opt2(sb, EXPLICIT_DELALLOC);
2182 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2183 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
2184 		} else
2185 			return -1;
2186 	}
2187 	if (m->flags & MOPT_CLEAR_ERR)
2188 		clear_opt(sb, ERRORS_MASK);
2189 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
2190 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
2191 			 "options when quota turned on");
2192 		return -1;
2193 	}
2194 
2195 	if (m->flags & MOPT_NOSUPPORT) {
2196 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
2197 	} else if (token == Opt_commit) {
2198 		if (arg == 0)
2199 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
2200 		else if (arg > INT_MAX / HZ) {
2201 			ext4_msg(sb, KERN_ERR,
2202 				 "Invalid commit interval %d, "
2203 				 "must be smaller than %d",
2204 				 arg, INT_MAX / HZ);
2205 			return -1;
2206 		}
2207 		sbi->s_commit_interval = HZ * arg;
2208 	} else if (token == Opt_debug_want_extra_isize) {
2209 		if ((arg & 1) ||
2210 		    (arg < 4) ||
2211 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
2212 			ext4_msg(sb, KERN_ERR,
2213 				 "Invalid want_extra_isize %d", arg);
2214 			return -1;
2215 		}
2216 		sbi->s_want_extra_isize = arg;
2217 	} else if (token == Opt_max_batch_time) {
2218 		sbi->s_max_batch_time = arg;
2219 	} else if (token == Opt_min_batch_time) {
2220 		sbi->s_min_batch_time = arg;
2221 	} else if (token == Opt_inode_readahead_blks) {
2222 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2223 			ext4_msg(sb, KERN_ERR,
2224 				 "EXT4-fs: inode_readahead_blks must be "
2225 				 "0 or a power of 2 smaller than 2^31");
2226 			return -1;
2227 		}
2228 		sbi->s_inode_readahead_blks = arg;
2229 	} else if (token == Opt_init_itable) {
2230 		set_opt(sb, INIT_INODE_TABLE);
2231 		if (!args->from)
2232 			arg = EXT4_DEF_LI_WAIT_MULT;
2233 		sbi->s_li_wait_mult = arg;
2234 	} else if (token == Opt_max_dir_size_kb) {
2235 		sbi->s_max_dir_size_kb = arg;
2236 #ifdef CONFIG_EXT4_DEBUG
2237 	} else if (token == Opt_fc_debug_max_replay) {
2238 		sbi->s_fc_debug_max_replay = arg;
2239 #endif
2240 	} else if (token == Opt_stripe) {
2241 		sbi->s_stripe = arg;
2242 	} else if (token == Opt_resuid) {
2243 		uid = make_kuid(current_user_ns(), arg);
2244 		if (!uid_valid(uid)) {
2245 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
2246 			return -1;
2247 		}
2248 		sbi->s_resuid = uid;
2249 	} else if (token == Opt_resgid) {
2250 		gid = make_kgid(current_user_ns(), arg);
2251 		if (!gid_valid(gid)) {
2252 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
2253 			return -1;
2254 		}
2255 		sbi->s_resgid = gid;
2256 	} else if (token == Opt_journal_dev) {
2257 		if (is_remount) {
2258 			ext4_msg(sb, KERN_ERR,
2259 				 "Cannot specify journal on remount");
2260 			return -1;
2261 		}
2262 		parsed_opts->journal_devnum = arg;
2263 	} else if (token == Opt_journal_path) {
2264 		char *journal_path;
2265 		struct inode *journal_inode;
2266 		struct path path;
2267 		int error;
2268 
2269 		if (is_remount) {
2270 			ext4_msg(sb, KERN_ERR,
2271 				 "Cannot specify journal on remount");
2272 			return -1;
2273 		}
2274 		journal_path = match_strdup(&args[0]);
2275 		if (!journal_path) {
2276 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2277 				"journal device string");
2278 			return -1;
2279 		}
2280 
2281 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2282 		if (error) {
2283 			ext4_msg(sb, KERN_ERR, "error: could not find "
2284 				"journal device path: error %d", error);
2285 			kfree(journal_path);
2286 			return -1;
2287 		}
2288 
2289 		journal_inode = d_inode(path.dentry);
2290 		if (!S_ISBLK(journal_inode->i_mode)) {
2291 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2292 				"is not a block device", journal_path);
2293 			path_put(&path);
2294 			kfree(journal_path);
2295 			return -1;
2296 		}
2297 
2298 		parsed_opts->journal_devnum = new_encode_dev(journal_inode->i_rdev);
2299 		path_put(&path);
2300 		kfree(journal_path);
2301 	} else if (token == Opt_journal_ioprio) {
2302 		if (arg > 7) {
2303 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
2304 				 " (must be 0-7)");
2305 			return -1;
2306 		}
2307 		parsed_opts->journal_ioprio =
2308 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
2309 	} else if (token == Opt_test_dummy_encryption) {
2310 		return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2311 						      is_remount);
2312 	} else if (m->flags & MOPT_DATAJ) {
2313 		if (is_remount) {
2314 			if (!sbi->s_journal)
2315 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
2316 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
2317 				ext4_msg(sb, KERN_ERR,
2318 					 "Cannot change data mode on remount");
2319 				return -1;
2320 			}
2321 		} else {
2322 			clear_opt(sb, DATA_FLAGS);
2323 			sbi->s_mount_opt |= m->mount_opt;
2324 		}
2325 #ifdef CONFIG_QUOTA
2326 	} else if (m->flags & MOPT_QFMT) {
2327 		if (sb_any_quota_loaded(sb) &&
2328 		    sbi->s_jquota_fmt != m->mount_opt) {
2329 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
2330 				 "quota options when quota turned on");
2331 			return -1;
2332 		}
2333 		if (ext4_has_feature_quota(sb)) {
2334 			ext4_msg(sb, KERN_INFO,
2335 				 "Quota format mount options ignored "
2336 				 "when QUOTA feature is enabled");
2337 			return 1;
2338 		}
2339 		sbi->s_jquota_fmt = m->mount_opt;
2340 #endif
2341 	} else if (token == Opt_dax || token == Opt_dax_always ||
2342 		   token == Opt_dax_inode || token == Opt_dax_never) {
2343 #ifdef CONFIG_FS_DAX
2344 		switch (token) {
2345 		case Opt_dax:
2346 		case Opt_dax_always:
2347 			if (is_remount &&
2348 			    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2349 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2350 			fail_dax_change_remount:
2351 				ext4_msg(sb, KERN_ERR, "can't change "
2352 					 "dax mount option while remounting");
2353 				return -1;
2354 			}
2355 			if (is_remount &&
2356 			    (test_opt(sb, DATA_FLAGS) ==
2357 			     EXT4_MOUNT_JOURNAL_DATA)) {
2358 				    ext4_msg(sb, KERN_ERR, "can't mount with "
2359 					     "both data=journal and dax");
2360 				    return -1;
2361 			}
2362 			ext4_msg(sb, KERN_WARNING,
2363 				"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2364 			sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
2365 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2366 			break;
2367 		case Opt_dax_never:
2368 			if (is_remount &&
2369 			    (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2370 			     (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2371 				goto fail_dax_change_remount;
2372 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2373 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2374 			break;
2375 		case Opt_dax_inode:
2376 			if (is_remount &&
2377 			    ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2378 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2379 			     !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2380 				goto fail_dax_change_remount;
2381 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2382 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2383 			/* Strictly for printing options */
2384 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
2385 			break;
2386 		}
2387 #else
2388 		ext4_msg(sb, KERN_INFO, "dax option not supported");
2389 		sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2390 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2391 		return -1;
2392 #endif
2393 	} else if (token == Opt_data_err_abort) {
2394 		sbi->s_mount_opt |= m->mount_opt;
2395 	} else if (token == Opt_data_err_ignore) {
2396 		sbi->s_mount_opt &= ~m->mount_opt;
2397 	} else if (token == Opt_mb_optimize_scan) {
2398 		if (arg != 0 && arg != 1) {
2399 			ext4_msg(sb, KERN_WARNING,
2400 				 "mb_optimize_scan should be set to 0 or 1.");
2401 			return -1;
2402 		}
2403 		parsed_opts->mb_optimize_scan = arg;
2404 	} else {
2405 		if (!args->from)
2406 			arg = 1;
2407 		if (m->flags & MOPT_CLEAR)
2408 			arg = !arg;
2409 		else if (unlikely(!(m->flags & MOPT_SET))) {
2410 			ext4_msg(sb, KERN_WARNING,
2411 				 "buggy handling of option %s", opt);
2412 			WARN_ON(1);
2413 			return -1;
2414 		}
2415 		if (m->flags & MOPT_2) {
2416 			if (arg != 0)
2417 				sbi->s_mount_opt2 |= m->mount_opt;
2418 			else
2419 				sbi->s_mount_opt2 &= ~m->mount_opt;
2420 		} else {
2421 			if (arg != 0)
2422 				sbi->s_mount_opt |= m->mount_opt;
2423 			else
2424 				sbi->s_mount_opt &= ~m->mount_opt;
2425 		}
2426 	}
2427 	return 1;
2428 }
2429 
2430 static int parse_options(char *options, struct super_block *sb,
2431 			 struct ext4_parsed_options *ret_opts,
2432 			 int is_remount)
2433 {
2434 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
2435 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2436 	substring_t args[MAX_OPT_ARGS];
2437 	int token;
2438 
2439 	if (!options)
2440 		return 1;
2441 
2442 	while ((p = strsep(&options, ",")) != NULL) {
2443 		if (!*p)
2444 			continue;
2445 		/*
2446 		 * Initialize args struct so we know whether arg was
2447 		 * found; some options take optional arguments.
2448 		 */
2449 		args[0].to = args[0].from = NULL;
2450 		token = match_token(p, tokens, args);
2451 		if (handle_mount_opt(sb, p, token, args, ret_opts,
2452 				     is_remount) < 0)
2453 			return 0;
2454 	}
2455 #ifdef CONFIG_QUOTA
2456 	/*
2457 	 * We do the test below only for project quotas. 'usrquota' and
2458 	 * 'grpquota' mount options are allowed even without quota feature
2459 	 * to support legacy quotas in quota files.
2460 	 */
2461 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
2462 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
2463 			 "Cannot enable project quota enforcement.");
2464 		return 0;
2465 	}
2466 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
2467 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
2468 	if (usr_qf_name || grp_qf_name) {
2469 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2470 			clear_opt(sb, USRQUOTA);
2471 
2472 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2473 			clear_opt(sb, GRPQUOTA);
2474 
2475 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2476 			ext4_msg(sb, KERN_ERR, "old and new quota "
2477 					"format mixing");
2478 			return 0;
2479 		}
2480 
2481 		if (!sbi->s_jquota_fmt) {
2482 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2483 					"not specified");
2484 			return 0;
2485 		}
2486 	}
2487 #endif
2488 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2489 		int blocksize =
2490 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2491 		if (blocksize < PAGE_SIZE)
2492 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2493 				 "experimental mount option 'dioread_nolock' "
2494 				 "for blocksize < PAGE_SIZE");
2495 	}
2496 	return 1;
2497 }
2498 
2499 static inline void ext4_show_quota_options(struct seq_file *seq,
2500 					   struct super_block *sb)
2501 {
2502 #if defined(CONFIG_QUOTA)
2503 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2504 	char *usr_qf_name, *grp_qf_name;
2505 
2506 	if (sbi->s_jquota_fmt) {
2507 		char *fmtname = "";
2508 
2509 		switch (sbi->s_jquota_fmt) {
2510 		case QFMT_VFS_OLD:
2511 			fmtname = "vfsold";
2512 			break;
2513 		case QFMT_VFS_V0:
2514 			fmtname = "vfsv0";
2515 			break;
2516 		case QFMT_VFS_V1:
2517 			fmtname = "vfsv1";
2518 			break;
2519 		}
2520 		seq_printf(seq, ",jqfmt=%s", fmtname);
2521 	}
2522 
2523 	rcu_read_lock();
2524 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
2525 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
2526 	if (usr_qf_name)
2527 		seq_show_option(seq, "usrjquota", usr_qf_name);
2528 	if (grp_qf_name)
2529 		seq_show_option(seq, "grpjquota", grp_qf_name);
2530 	rcu_read_unlock();
2531 #endif
2532 }
2533 
2534 static const char *token2str(int token)
2535 {
2536 	const struct match_token *t;
2537 
2538 	for (t = tokens; t->token != Opt_err; t++)
2539 		if (t->token == token && !strchr(t->pattern, '='))
2540 			break;
2541 	return t->pattern;
2542 }
2543 
2544 /*
2545  * Show an option if
2546  *  - it's set to a non-default value OR
2547  *  - if the per-sb default is different from the global default
2548  */
2549 static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
2550 			      int nodefs)
2551 {
2552 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2553 	struct ext4_super_block *es = sbi->s_es;
2554 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
2555 	const struct mount_opts *m;
2556 	char sep = nodefs ? '\n' : ',';
2557 
2558 #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
2559 #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
2560 
2561 	if (sbi->s_sb_block != 1)
2562 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
2563 
2564 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
2565 		int want_set = m->flags & MOPT_SET;
2566 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
2567 		    (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
2568 			continue;
2569 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
2570 			continue; /* skip if same as the default */
2571 		if ((want_set &&
2572 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
2573 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
2574 			continue; /* select Opt_noFoo vs Opt_Foo */
2575 		SEQ_OPTS_PRINT("%s", token2str(m->token));
2576 	}
2577 
2578 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
2579 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
2580 		SEQ_OPTS_PRINT("resuid=%u",
2581 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
2582 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
2583 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
2584 		SEQ_OPTS_PRINT("resgid=%u",
2585 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
2586 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
2587 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
2588 		SEQ_OPTS_PUTS("errors=remount-ro");
2589 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
2590 		SEQ_OPTS_PUTS("errors=continue");
2591 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
2592 		SEQ_OPTS_PUTS("errors=panic");
2593 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
2594 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
2595 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
2596 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
2597 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
2598 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2599 	if (sb->s_flags & SB_I_VERSION)
2600 		SEQ_OPTS_PUTS("i_version");
2601 	if (nodefs || sbi->s_stripe)
2602 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
2603 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
2604 			(sbi->s_mount_opt ^ def_mount_opt)) {
2605 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2606 			SEQ_OPTS_PUTS("data=journal");
2607 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2608 			SEQ_OPTS_PUTS("data=ordered");
2609 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
2610 			SEQ_OPTS_PUTS("data=writeback");
2611 	}
2612 	if (nodefs ||
2613 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
2614 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
2615 			       sbi->s_inode_readahead_blks);
2616 
2617 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
2618 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
2619 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2620 	if (nodefs || sbi->s_max_dir_size_kb)
2621 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
2622 	if (test_opt(sb, DATA_ERR_ABORT))
2623 		SEQ_OPTS_PUTS("data_err=abort");
2624 
2625 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
2626 
2627 	if (sb->s_flags & SB_INLINECRYPT)
2628 		SEQ_OPTS_PUTS("inlinecrypt");
2629 
2630 	if (test_opt(sb, DAX_ALWAYS)) {
2631 		if (IS_EXT2_SB(sb))
2632 			SEQ_OPTS_PUTS("dax");
2633 		else
2634 			SEQ_OPTS_PUTS("dax=always");
2635 	} else if (test_opt2(sb, DAX_NEVER)) {
2636 		SEQ_OPTS_PUTS("dax=never");
2637 	} else if (test_opt2(sb, DAX_INODE)) {
2638 		SEQ_OPTS_PUTS("dax=inode");
2639 	}
2640 	ext4_show_quota_options(seq, sb);
2641 	return 0;
2642 }
2643 
2644 static int ext4_show_options(struct seq_file *seq, struct dentry *root)
2645 {
2646 	return _ext4_show_options(seq, root->d_sb, 0);
2647 }
2648 
2649 int ext4_seq_options_show(struct seq_file *seq, void *offset)
2650 {
2651 	struct super_block *sb = seq->private;
2652 	int rc;
2653 
2654 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
2655 	rc = _ext4_show_options(seq, sb, 1);
2656 	seq_puts(seq, "\n");
2657 	return rc;
2658 }
2659 
2660 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2661 			    int read_only)
2662 {
2663 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2664 	int err = 0;
2665 
2666 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2667 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2668 			 "forcing read-only mode");
2669 		err = -EROFS;
2670 		goto done;
2671 	}
2672 	if (read_only)
2673 		goto done;
2674 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2675 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2676 			 "running e2fsck is recommended");
2677 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2678 		ext4_msg(sb, KERN_WARNING,
2679 			 "warning: mounting fs with errors, "
2680 			 "running e2fsck is recommended");
2681 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2682 		 le16_to_cpu(es->s_mnt_count) >=
2683 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2684 		ext4_msg(sb, KERN_WARNING,
2685 			 "warning: maximal mount count reached, "
2686 			 "running e2fsck is recommended");
2687 	else if (le32_to_cpu(es->s_checkinterval) &&
2688 		 (ext4_get_tstamp(es, s_lastcheck) +
2689 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2690 		ext4_msg(sb, KERN_WARNING,
2691 			 "warning: checktime reached, "
2692 			 "running e2fsck is recommended");
2693 	if (!sbi->s_journal)
2694 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2695 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2696 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2697 	le16_add_cpu(&es->s_mnt_count, 1);
2698 	ext4_update_tstamp(es, s_mtime);
2699 	if (sbi->s_journal) {
2700 		ext4_set_feature_journal_needs_recovery(sb);
2701 		if (ext4_has_feature_orphan_file(sb))
2702 			ext4_set_feature_orphan_present(sb);
2703 	}
2704 
2705 	err = ext4_commit_super(sb);
2706 done:
2707 	if (test_opt(sb, DEBUG))
2708 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2709 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2710 			sb->s_blocksize,
2711 			sbi->s_groups_count,
2712 			EXT4_BLOCKS_PER_GROUP(sb),
2713 			EXT4_INODES_PER_GROUP(sb),
2714 			sbi->s_mount_opt, sbi->s_mount_opt2);
2715 
2716 	cleancache_init_fs(sb);
2717 	return err;
2718 }
2719 
2720 int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2721 {
2722 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2723 	struct flex_groups **old_groups, **new_groups;
2724 	int size, i, j;
2725 
2726 	if (!sbi->s_log_groups_per_flex)
2727 		return 0;
2728 
2729 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2730 	if (size <= sbi->s_flex_groups_allocated)
2731 		return 0;
2732 
2733 	new_groups = kvzalloc(roundup_pow_of_two(size *
2734 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2735 	if (!new_groups) {
2736 		ext4_msg(sb, KERN_ERR,
2737 			 "not enough memory for %d flex group pointers", size);
2738 		return -ENOMEM;
2739 	}
2740 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
2741 		new_groups[i] = kvzalloc(roundup_pow_of_two(
2742 					 sizeof(struct flex_groups)),
2743 					 GFP_KERNEL);
2744 		if (!new_groups[i]) {
2745 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
2746 				kvfree(new_groups[j]);
2747 			kvfree(new_groups);
2748 			ext4_msg(sb, KERN_ERR,
2749 				 "not enough memory for %d flex groups", size);
2750 			return -ENOMEM;
2751 		}
2752 	}
2753 	rcu_read_lock();
2754 	old_groups = rcu_dereference(sbi->s_flex_groups);
2755 	if (old_groups)
2756 		memcpy(new_groups, old_groups,
2757 		       (sbi->s_flex_groups_allocated *
2758 			sizeof(struct flex_groups *)));
2759 	rcu_read_unlock();
2760 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
2761 	sbi->s_flex_groups_allocated = size;
2762 	if (old_groups)
2763 		ext4_kvfree_array_rcu(old_groups);
2764 	return 0;
2765 }
2766 
2767 static int ext4_fill_flex_info(struct super_block *sb)
2768 {
2769 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2770 	struct ext4_group_desc *gdp = NULL;
2771 	struct flex_groups *fg;
2772 	ext4_group_t flex_group;
2773 	int i, err;
2774 
2775 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2776 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2777 		sbi->s_log_groups_per_flex = 0;
2778 		return 1;
2779 	}
2780 
2781 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2782 	if (err)
2783 		goto failed;
2784 
2785 	for (i = 0; i < sbi->s_groups_count; i++) {
2786 		gdp = ext4_get_group_desc(sb, i, NULL);
2787 
2788 		flex_group = ext4_flex_group(sbi, i);
2789 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
2790 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
2791 		atomic64_add(ext4_free_group_clusters(sb, gdp),
2792 			     &fg->free_clusters);
2793 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2794 	}
2795 
2796 	return 1;
2797 failed:
2798 	return 0;
2799 }
2800 
2801 static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2802 				   struct ext4_group_desc *gdp)
2803 {
2804 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2805 	__u16 crc = 0;
2806 	__le32 le_group = cpu_to_le32(block_group);
2807 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2808 
2809 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2810 		/* Use new metadata_csum algorithm */
2811 		__u32 csum32;
2812 		__u16 dummy_csum = 0;
2813 
2814 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2815 				     sizeof(le_group));
2816 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2817 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2818 				     sizeof(dummy_csum));
2819 		offset += sizeof(dummy_csum);
2820 		if (offset < sbi->s_desc_size)
2821 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2822 					     sbi->s_desc_size - offset);
2823 
2824 		crc = csum32 & 0xFFFF;
2825 		goto out;
2826 	}
2827 
2828 	/* old crc16 code */
2829 	if (!ext4_has_feature_gdt_csum(sb))
2830 		return 0;
2831 
2832 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2833 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2834 	crc = crc16(crc, (__u8 *)gdp, offset);
2835 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2836 	/* for checksum of struct ext4_group_desc do the rest...*/
2837 	if (ext4_has_feature_64bit(sb) &&
2838 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2839 		crc = crc16(crc, (__u8 *)gdp + offset,
2840 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2841 				offset);
2842 
2843 out:
2844 	return cpu_to_le16(crc);
2845 }
2846 
2847 int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2848 				struct ext4_group_desc *gdp)
2849 {
2850 	if (ext4_has_group_desc_csum(sb) &&
2851 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2852 		return 0;
2853 
2854 	return 1;
2855 }
2856 
2857 void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2858 			      struct ext4_group_desc *gdp)
2859 {
2860 	if (!ext4_has_group_desc_csum(sb))
2861 		return;
2862 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2863 }
2864 
2865 /* Called at mount-time, super-block is locked */
2866 static int ext4_check_descriptors(struct super_block *sb,
2867 				  ext4_fsblk_t sb_block,
2868 				  ext4_group_t *first_not_zeroed)
2869 {
2870 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2871 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2872 	ext4_fsblk_t last_block;
2873 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2874 	ext4_fsblk_t block_bitmap;
2875 	ext4_fsblk_t inode_bitmap;
2876 	ext4_fsblk_t inode_table;
2877 	int flexbg_flag = 0;
2878 	ext4_group_t i, grp = sbi->s_groups_count;
2879 
2880 	if (ext4_has_feature_flex_bg(sb))
2881 		flexbg_flag = 1;
2882 
2883 	ext4_debug("Checking group descriptors");
2884 
2885 	for (i = 0; i < sbi->s_groups_count; i++) {
2886 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2887 
2888 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2889 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2890 		else
2891 			last_block = first_block +
2892 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2893 
2894 		if ((grp == sbi->s_groups_count) &&
2895 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2896 			grp = i;
2897 
2898 		block_bitmap = ext4_block_bitmap(sb, gdp);
2899 		if (block_bitmap == sb_block) {
2900 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2901 				 "Block bitmap for group %u overlaps "
2902 				 "superblock", i);
2903 			if (!sb_rdonly(sb))
2904 				return 0;
2905 		}
2906 		if (block_bitmap >= sb_block + 1 &&
2907 		    block_bitmap <= last_bg_block) {
2908 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2909 				 "Block bitmap for group %u overlaps "
2910 				 "block group descriptors", i);
2911 			if (!sb_rdonly(sb))
2912 				return 0;
2913 		}
2914 		if (block_bitmap < first_block || block_bitmap > last_block) {
2915 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2916 			       "Block bitmap for group %u not in group "
2917 			       "(block %llu)!", i, block_bitmap);
2918 			return 0;
2919 		}
2920 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2921 		if (inode_bitmap == sb_block) {
2922 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2923 				 "Inode bitmap for group %u overlaps "
2924 				 "superblock", i);
2925 			if (!sb_rdonly(sb))
2926 				return 0;
2927 		}
2928 		if (inode_bitmap >= sb_block + 1 &&
2929 		    inode_bitmap <= last_bg_block) {
2930 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2931 				 "Inode bitmap for group %u overlaps "
2932 				 "block group descriptors", i);
2933 			if (!sb_rdonly(sb))
2934 				return 0;
2935 		}
2936 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2937 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2938 			       "Inode bitmap for group %u not in group "
2939 			       "(block %llu)!", i, inode_bitmap);
2940 			return 0;
2941 		}
2942 		inode_table = ext4_inode_table(sb, gdp);
2943 		if (inode_table == sb_block) {
2944 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2945 				 "Inode table for group %u overlaps "
2946 				 "superblock", i);
2947 			if (!sb_rdonly(sb))
2948 				return 0;
2949 		}
2950 		if (inode_table >= sb_block + 1 &&
2951 		    inode_table <= last_bg_block) {
2952 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2953 				 "Inode table for group %u overlaps "
2954 				 "block group descriptors", i);
2955 			if (!sb_rdonly(sb))
2956 				return 0;
2957 		}
2958 		if (inode_table < first_block ||
2959 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2960 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2961 			       "Inode table for group %u not in group "
2962 			       "(block %llu)!", i, inode_table);
2963 			return 0;
2964 		}
2965 		ext4_lock_group(sb, i);
2966 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2967 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2968 				 "Checksum for group %u failed (%u!=%u)",
2969 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2970 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2971 			if (!sb_rdonly(sb)) {
2972 				ext4_unlock_group(sb, i);
2973 				return 0;
2974 			}
2975 		}
2976 		ext4_unlock_group(sb, i);
2977 		if (!flexbg_flag)
2978 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2979 	}
2980 	if (NULL != first_not_zeroed)
2981 		*first_not_zeroed = grp;
2982 	return 1;
2983 }
2984 
2985 /*
2986  * Maximal extent format file size.
2987  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2988  * extent format containers, within a sector_t, and within i_blocks
2989  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2990  * so that won't be a limiting factor.
2991  *
2992  * However there is other limiting factor. We do store extents in the form
2993  * of starting block and length, hence the resulting length of the extent
2994  * covering maximum file size must fit into on-disk format containers as
2995  * well. Given that length is always by 1 unit bigger than max unit (because
2996  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2997  *
2998  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2999  */
3000 static loff_t ext4_max_size(int blkbits, int has_huge_files)
3001 {
3002 	loff_t res;
3003 	loff_t upper_limit = MAX_LFS_FILESIZE;
3004 
3005 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
3006 
3007 	if (!has_huge_files) {
3008 		upper_limit = (1LL << 32) - 1;
3009 
3010 		/* total blocks in file system block size */
3011 		upper_limit >>= (blkbits - 9);
3012 		upper_limit <<= blkbits;
3013 	}
3014 
3015 	/*
3016 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
3017 	 * by one fs block, so ee_len can cover the extent of maximum file
3018 	 * size
3019 	 */
3020 	res = (1LL << 32) - 1;
3021 	res <<= blkbits;
3022 
3023 	/* Sanity check against vm- & vfs- imposed limits */
3024 	if (res > upper_limit)
3025 		res = upper_limit;
3026 
3027 	return res;
3028 }
3029 
3030 /*
3031  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
3032  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
3033  * We need to be 1 filesystem block less than the 2^48 sector limit.
3034  */
3035 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3036 {
3037 	unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
3038 	int meta_blocks;
3039 
3040 	/*
3041 	 * This is calculated to be the largest file size for a dense, block
3042 	 * mapped file such that the file's total number of 512-byte sectors,
3043 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
3044 	 *
3045 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
3046 	 * number of 512-byte sectors of the file.
3047 	 */
3048 	if (!has_huge_files) {
3049 		/*
3050 		 * !has_huge_files or implies that the inode i_block field
3051 		 * represents total file blocks in 2^32 512-byte sectors ==
3052 		 * size of vfs inode i_blocks * 8
3053 		 */
3054 		upper_limit = (1LL << 32) - 1;
3055 
3056 		/* total blocks in file system block size */
3057 		upper_limit >>= (bits - 9);
3058 
3059 	} else {
3060 		/*
3061 		 * We use 48 bit ext4_inode i_blocks
3062 		 * With EXT4_HUGE_FILE_FL set the i_blocks
3063 		 * represent total number of blocks in
3064 		 * file system block size
3065 		 */
3066 		upper_limit = (1LL << 48) - 1;
3067 
3068 	}
3069 
3070 	/* indirect blocks */
3071 	meta_blocks = 1;
3072 	/* double indirect blocks */
3073 	meta_blocks += 1 + (1LL << (bits-2));
3074 	/* tripple indirect blocks */
3075 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
3076 
3077 	upper_limit -= meta_blocks;
3078 	upper_limit <<= bits;
3079 
3080 	res += 1LL << (bits-2);
3081 	res += 1LL << (2*(bits-2));
3082 	res += 1LL << (3*(bits-2));
3083 	res <<= bits;
3084 	if (res > upper_limit)
3085 		res = upper_limit;
3086 
3087 	if (res > MAX_LFS_FILESIZE)
3088 		res = MAX_LFS_FILESIZE;
3089 
3090 	return (loff_t)res;
3091 }
3092 
3093 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
3094 				   ext4_fsblk_t logical_sb_block, int nr)
3095 {
3096 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3097 	ext4_group_t bg, first_meta_bg;
3098 	int has_super = 0;
3099 
3100 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3101 
3102 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
3103 		return logical_sb_block + nr + 1;
3104 	bg = sbi->s_desc_per_block * nr;
3105 	if (ext4_bg_has_super(sb, bg))
3106 		has_super = 1;
3107 
3108 	/*
3109 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3110 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3111 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3112 	 * compensate.
3113 	 */
3114 	if (sb->s_blocksize == 1024 && nr == 0 &&
3115 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3116 		has_super++;
3117 
3118 	return (has_super + ext4_group_first_block_no(sb, bg));
3119 }
3120 
3121 /**
3122  * ext4_get_stripe_size: Get the stripe size.
3123  * @sbi: In memory super block info
3124  *
3125  * If we have specified it via mount option, then
3126  * use the mount option value. If the value specified at mount time is
3127  * greater than the blocks per group use the super block value.
3128  * If the super block value is greater than blocks per group return 0.
3129  * Allocator needs it be less than blocks per group.
3130  *
3131  */
3132 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3133 {
3134 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3135 	unsigned long stripe_width =
3136 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
3137 	int ret;
3138 
3139 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
3140 		ret = sbi->s_stripe;
3141 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
3142 		ret = stripe_width;
3143 	else if (stride && stride <= sbi->s_blocks_per_group)
3144 		ret = stride;
3145 	else
3146 		ret = 0;
3147 
3148 	/*
3149 	 * If the stripe width is 1, this makes no sense and
3150 	 * we set it to 0 to turn off stripe handling code.
3151 	 */
3152 	if (ret <= 1)
3153 		ret = 0;
3154 
3155 	return ret;
3156 }
3157 
3158 /*
3159  * Check whether this filesystem can be mounted based on
3160  * the features present and the RDONLY/RDWR mount requested.
3161  * Returns 1 if this filesystem can be mounted as requested,
3162  * 0 if it cannot be.
3163  */
3164 int ext4_feature_set_ok(struct super_block *sb, int readonly)
3165 {
3166 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3167 		ext4_msg(sb, KERN_ERR,
3168 			"Couldn't mount because of "
3169 			"unsupported optional features (%x)",
3170 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3171 			~EXT4_FEATURE_INCOMPAT_SUPP));
3172 		return 0;
3173 	}
3174 
3175 #ifndef CONFIG_UNICODE
3176 	if (ext4_has_feature_casefold(sb)) {
3177 		ext4_msg(sb, KERN_ERR,
3178 			 "Filesystem with casefold feature cannot be "
3179 			 "mounted without CONFIG_UNICODE");
3180 		return 0;
3181 	}
3182 #endif
3183 
3184 	if (readonly)
3185 		return 1;
3186 
3187 	if (ext4_has_feature_readonly(sb)) {
3188 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
3189 		sb->s_flags |= SB_RDONLY;
3190 		return 1;
3191 	}
3192 
3193 	/* Check that feature set is OK for a read-write mount */
3194 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3195 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3196 			 "unsupported optional features (%x)",
3197 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3198 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3199 		return 0;
3200 	}
3201 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3202 		ext4_msg(sb, KERN_ERR,
3203 			 "Can't support bigalloc feature without "
3204 			 "extents feature\n");
3205 		return 0;
3206 	}
3207 
3208 #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3209 	if (!readonly && (ext4_has_feature_quota(sb) ||
3210 			  ext4_has_feature_project(sb))) {
3211 		ext4_msg(sb, KERN_ERR,
3212 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3213 		return 0;
3214 	}
3215 #endif  /* CONFIG_QUOTA */
3216 	return 1;
3217 }
3218 
3219 /*
3220  * This function is called once a day if we have errors logged
3221  * on the file system
3222  */
3223 static void print_daily_error_info(struct timer_list *t)
3224 {
3225 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3226 	struct super_block *sb = sbi->s_sb;
3227 	struct ext4_super_block *es = sbi->s_es;
3228 
3229 	if (es->s_error_count)
3230 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3231 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
3232 			 le32_to_cpu(es->s_error_count));
3233 	if (es->s_first_error_time) {
3234 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
3235 		       sb->s_id,
3236 		       ext4_get_tstamp(es, s_first_error_time),
3237 		       (int) sizeof(es->s_first_error_func),
3238 		       es->s_first_error_func,
3239 		       le32_to_cpu(es->s_first_error_line));
3240 		if (es->s_first_error_ino)
3241 			printk(KERN_CONT ": inode %u",
3242 			       le32_to_cpu(es->s_first_error_ino));
3243 		if (es->s_first_error_block)
3244 			printk(KERN_CONT ": block %llu", (unsigned long long)
3245 			       le64_to_cpu(es->s_first_error_block));
3246 		printk(KERN_CONT "\n");
3247 	}
3248 	if (es->s_last_error_time) {
3249 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
3250 		       sb->s_id,
3251 		       ext4_get_tstamp(es, s_last_error_time),
3252 		       (int) sizeof(es->s_last_error_func),
3253 		       es->s_last_error_func,
3254 		       le32_to_cpu(es->s_last_error_line));
3255 		if (es->s_last_error_ino)
3256 			printk(KERN_CONT ": inode %u",
3257 			       le32_to_cpu(es->s_last_error_ino));
3258 		if (es->s_last_error_block)
3259 			printk(KERN_CONT ": block %llu", (unsigned long long)
3260 			       le64_to_cpu(es->s_last_error_block));
3261 		printk(KERN_CONT "\n");
3262 	}
3263 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
3264 }
3265 
3266 /* Find next suitable group and run ext4_init_inode_table */
3267 static int ext4_run_li_request(struct ext4_li_request *elr)
3268 {
3269 	struct ext4_group_desc *gdp = NULL;
3270 	struct super_block *sb = elr->lr_super;
3271 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
3272 	ext4_group_t group = elr->lr_next_group;
3273 	unsigned int prefetch_ios = 0;
3274 	int ret = 0;
3275 	u64 start_time;
3276 
3277 	if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
3278 		elr->lr_next_group = ext4_mb_prefetch(sb, group,
3279 				EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
3280 		if (prefetch_ios)
3281 			ext4_mb_prefetch_fini(sb, elr->lr_next_group,
3282 					      prefetch_ios);
3283 		trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
3284 					    prefetch_ios);
3285 		if (group >= elr->lr_next_group) {
3286 			ret = 1;
3287 			if (elr->lr_first_not_zeroed != ngroups &&
3288 			    !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
3289 				elr->lr_next_group = elr->lr_first_not_zeroed;
3290 				elr->lr_mode = EXT4_LI_MODE_ITABLE;
3291 				ret = 0;
3292 			}
3293 		}
3294 		return ret;
3295 	}
3296 
3297 	for (; group < ngroups; group++) {
3298 		gdp = ext4_get_group_desc(sb, group, NULL);
3299 		if (!gdp) {
3300 			ret = 1;
3301 			break;
3302 		}
3303 
3304 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3305 			break;
3306 	}
3307 
3308 	if (group >= ngroups)
3309 		ret = 1;
3310 
3311 	if (!ret) {
3312 		start_time = ktime_get_real_ns();
3313 		ret = ext4_init_inode_table(sb, group,
3314 					    elr->lr_timeout ? 0 : 1);
3315 		trace_ext4_lazy_itable_init(sb, group);
3316 		if (elr->lr_timeout == 0) {
3317 			elr->lr_timeout = nsecs_to_jiffies((ktime_get_real_ns() - start_time) *
3318 				EXT4_SB(elr->lr_super)->s_li_wait_mult);
3319 		}
3320 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3321 		elr->lr_next_group = group + 1;
3322 	}
3323 	return ret;
3324 }
3325 
3326 /*
3327  * Remove lr_request from the list_request and free the
3328  * request structure. Should be called with li_list_mtx held
3329  */
3330 static void ext4_remove_li_request(struct ext4_li_request *elr)
3331 {
3332 	if (!elr)
3333 		return;
3334 
3335 	list_del(&elr->lr_request);
3336 	EXT4_SB(elr->lr_super)->s_li_request = NULL;
3337 	kfree(elr);
3338 }
3339 
3340 static void ext4_unregister_li_request(struct super_block *sb)
3341 {
3342 	mutex_lock(&ext4_li_mtx);
3343 	if (!ext4_li_info) {
3344 		mutex_unlock(&ext4_li_mtx);
3345 		return;
3346 	}
3347 
3348 	mutex_lock(&ext4_li_info->li_list_mtx);
3349 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3350 	mutex_unlock(&ext4_li_info->li_list_mtx);
3351 	mutex_unlock(&ext4_li_mtx);
3352 }
3353 
3354 static struct task_struct *ext4_lazyinit_task;
3355 
3356 /*
3357  * This is the function where ext4lazyinit thread lives. It walks
3358  * through the request list searching for next scheduled filesystem.
3359  * When such a fs is found, run the lazy initialization request
3360  * (ext4_rn_li_request) and keep track of the time spend in this
3361  * function. Based on that time we compute next schedule time of
3362  * the request. When walking through the list is complete, compute
3363  * next waking time and put itself into sleep.
3364  */
3365 static int ext4_lazyinit_thread(void *arg)
3366 {
3367 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3368 	struct list_head *pos, *n;
3369 	struct ext4_li_request *elr;
3370 	unsigned long next_wakeup, cur;
3371 
3372 	BUG_ON(NULL == eli);
3373 
3374 cont_thread:
3375 	while (true) {
3376 		next_wakeup = MAX_JIFFY_OFFSET;
3377 
3378 		mutex_lock(&eli->li_list_mtx);
3379 		if (list_empty(&eli->li_request_list)) {
3380 			mutex_unlock(&eli->li_list_mtx);
3381 			goto exit_thread;
3382 		}
3383 		list_for_each_safe(pos, n, &eli->li_request_list) {
3384 			int err = 0;
3385 			int progress = 0;
3386 			elr = list_entry(pos, struct ext4_li_request,
3387 					 lr_request);
3388 
3389 			if (time_before(jiffies, elr->lr_next_sched)) {
3390 				if (time_before(elr->lr_next_sched, next_wakeup))
3391 					next_wakeup = elr->lr_next_sched;
3392 				continue;
3393 			}
3394 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3395 				if (sb_start_write_trylock(elr->lr_super)) {
3396 					progress = 1;
3397 					/*
3398 					 * We hold sb->s_umount, sb can not
3399 					 * be removed from the list, it is
3400 					 * now safe to drop li_list_mtx
3401 					 */
3402 					mutex_unlock(&eli->li_list_mtx);
3403 					err = ext4_run_li_request(elr);
3404 					sb_end_write(elr->lr_super);
3405 					mutex_lock(&eli->li_list_mtx);
3406 					n = pos->next;
3407 				}
3408 				up_read((&elr->lr_super->s_umount));
3409 			}
3410 			/* error, remove the lazy_init job */
3411 			if (err) {
3412 				ext4_remove_li_request(elr);
3413 				continue;
3414 			}
3415 			if (!progress) {
3416 				elr->lr_next_sched = jiffies +
3417 					(prandom_u32()
3418 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3419 			}
3420 			if (time_before(elr->lr_next_sched, next_wakeup))
3421 				next_wakeup = elr->lr_next_sched;
3422 		}
3423 		mutex_unlock(&eli->li_list_mtx);
3424 
3425 		try_to_freeze();
3426 
3427 		cur = jiffies;
3428 		if ((time_after_eq(cur, next_wakeup)) ||
3429 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3430 			cond_resched();
3431 			continue;
3432 		}
3433 
3434 		schedule_timeout_interruptible(next_wakeup - cur);
3435 
3436 		if (kthread_should_stop()) {
3437 			ext4_clear_request_list();
3438 			goto exit_thread;
3439 		}
3440 	}
3441 
3442 exit_thread:
3443 	/*
3444 	 * It looks like the request list is empty, but we need
3445 	 * to check it under the li_list_mtx lock, to prevent any
3446 	 * additions into it, and of course we should lock ext4_li_mtx
3447 	 * to atomically free the list and ext4_li_info, because at
3448 	 * this point another ext4 filesystem could be registering
3449 	 * new one.
3450 	 */
3451 	mutex_lock(&ext4_li_mtx);
3452 	mutex_lock(&eli->li_list_mtx);
3453 	if (!list_empty(&eli->li_request_list)) {
3454 		mutex_unlock(&eli->li_list_mtx);
3455 		mutex_unlock(&ext4_li_mtx);
3456 		goto cont_thread;
3457 	}
3458 	mutex_unlock(&eli->li_list_mtx);
3459 	kfree(ext4_li_info);
3460 	ext4_li_info = NULL;
3461 	mutex_unlock(&ext4_li_mtx);
3462 
3463 	return 0;
3464 }
3465 
3466 static void ext4_clear_request_list(void)
3467 {
3468 	struct list_head *pos, *n;
3469 	struct ext4_li_request *elr;
3470 
3471 	mutex_lock(&ext4_li_info->li_list_mtx);
3472 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3473 		elr = list_entry(pos, struct ext4_li_request,
3474 				 lr_request);
3475 		ext4_remove_li_request(elr);
3476 	}
3477 	mutex_unlock(&ext4_li_info->li_list_mtx);
3478 }
3479 
3480 static int ext4_run_lazyinit_thread(void)
3481 {
3482 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
3483 					 ext4_li_info, "ext4lazyinit");
3484 	if (IS_ERR(ext4_lazyinit_task)) {
3485 		int err = PTR_ERR(ext4_lazyinit_task);
3486 		ext4_clear_request_list();
3487 		kfree(ext4_li_info);
3488 		ext4_li_info = NULL;
3489 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3490 				 "initialization thread\n",
3491 				 err);
3492 		return err;
3493 	}
3494 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3495 	return 0;
3496 }
3497 
3498 /*
3499  * Check whether it make sense to run itable init. thread or not.
3500  * If there is at least one uninitialized inode table, return
3501  * corresponding group number, else the loop goes through all
3502  * groups and return total number of groups.
3503  */
3504 static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3505 {
3506 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3507 	struct ext4_group_desc *gdp = NULL;
3508 
3509 	if (!ext4_has_group_desc_csum(sb))
3510 		return ngroups;
3511 
3512 	for (group = 0; group < ngroups; group++) {
3513 		gdp = ext4_get_group_desc(sb, group, NULL);
3514 		if (!gdp)
3515 			continue;
3516 
3517 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3518 			break;
3519 	}
3520 
3521 	return group;
3522 }
3523 
3524 static int ext4_li_info_new(void)
3525 {
3526 	struct ext4_lazy_init *eli = NULL;
3527 
3528 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3529 	if (!eli)
3530 		return -ENOMEM;
3531 
3532 	INIT_LIST_HEAD(&eli->li_request_list);
3533 	mutex_init(&eli->li_list_mtx);
3534 
3535 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3536 
3537 	ext4_li_info = eli;
3538 
3539 	return 0;
3540 }
3541 
3542 static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3543 					    ext4_group_t start)
3544 {
3545 	struct ext4_li_request *elr;
3546 
3547 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3548 	if (!elr)
3549 		return NULL;
3550 
3551 	elr->lr_super = sb;
3552 	elr->lr_first_not_zeroed = start;
3553 	if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) {
3554 		elr->lr_mode = EXT4_LI_MODE_ITABLE;
3555 		elr->lr_next_group = start;
3556 	} else {
3557 		elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
3558 	}
3559 
3560 	/*
3561 	 * Randomize first schedule time of the request to
3562 	 * spread the inode table initialization requests
3563 	 * better.
3564 	 */
3565 	elr->lr_next_sched = jiffies + (prandom_u32() %
3566 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3567 	return elr;
3568 }
3569 
3570 int ext4_register_li_request(struct super_block *sb,
3571 			     ext4_group_t first_not_zeroed)
3572 {
3573 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3574 	struct ext4_li_request *elr = NULL;
3575 	ext4_group_t ngroups = sbi->s_groups_count;
3576 	int ret = 0;
3577 
3578 	mutex_lock(&ext4_li_mtx);
3579 	if (sbi->s_li_request != NULL) {
3580 		/*
3581 		 * Reset timeout so it can be computed again, because
3582 		 * s_li_wait_mult might have changed.
3583 		 */
3584 		sbi->s_li_request->lr_timeout = 0;
3585 		goto out;
3586 	}
3587 
3588 	if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) &&
3589 	    (first_not_zeroed == ngroups || sb_rdonly(sb) ||
3590 	     !test_opt(sb, INIT_INODE_TABLE)))
3591 		goto out;
3592 
3593 	elr = ext4_li_request_new(sb, first_not_zeroed);
3594 	if (!elr) {
3595 		ret = -ENOMEM;
3596 		goto out;
3597 	}
3598 
3599 	if (NULL == ext4_li_info) {
3600 		ret = ext4_li_info_new();
3601 		if (ret)
3602 			goto out;
3603 	}
3604 
3605 	mutex_lock(&ext4_li_info->li_list_mtx);
3606 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3607 	mutex_unlock(&ext4_li_info->li_list_mtx);
3608 
3609 	sbi->s_li_request = elr;
3610 	/*
3611 	 * set elr to NULL here since it has been inserted to
3612 	 * the request_list and the removal and free of it is
3613 	 * handled by ext4_clear_request_list from now on.
3614 	 */
3615 	elr = NULL;
3616 
3617 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3618 		ret = ext4_run_lazyinit_thread();
3619 		if (ret)
3620 			goto out;
3621 	}
3622 out:
3623 	mutex_unlock(&ext4_li_mtx);
3624 	if (ret)
3625 		kfree(elr);
3626 	return ret;
3627 }
3628 
3629 /*
3630  * We do not need to lock anything since this is called on
3631  * module unload.
3632  */
3633 static void ext4_destroy_lazyinit_thread(void)
3634 {
3635 	/*
3636 	 * If thread exited earlier
3637 	 * there's nothing to be done.
3638 	 */
3639 	if (!ext4_li_info || !ext4_lazyinit_task)
3640 		return;
3641 
3642 	kthread_stop(ext4_lazyinit_task);
3643 }
3644 
3645 static int set_journal_csum_feature_set(struct super_block *sb)
3646 {
3647 	int ret = 1;
3648 	int compat, incompat;
3649 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3650 
3651 	if (ext4_has_metadata_csum(sb)) {
3652 		/* journal checksum v3 */
3653 		compat = 0;
3654 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
3655 	} else {
3656 		/* journal checksum v1 */
3657 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
3658 		incompat = 0;
3659 	}
3660 
3661 	jbd2_journal_clear_features(sbi->s_journal,
3662 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3663 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3664 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
3665 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3666 		ret = jbd2_journal_set_features(sbi->s_journal,
3667 				compat, 0,
3668 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3669 				incompat);
3670 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3671 		ret = jbd2_journal_set_features(sbi->s_journal,
3672 				compat, 0,
3673 				incompat);
3674 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3675 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3676 	} else {
3677 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3678 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3679 	}
3680 
3681 	return ret;
3682 }
3683 
3684 /*
3685  * Note: calculating the overhead so we can be compatible with
3686  * historical BSD practice is quite difficult in the face of
3687  * clusters/bigalloc.  This is because multiple metadata blocks from
3688  * different block group can end up in the same allocation cluster.
3689  * Calculating the exact overhead in the face of clustered allocation
3690  * requires either O(all block bitmaps) in memory or O(number of block
3691  * groups**2) in time.  We will still calculate the superblock for
3692  * older file systems --- and if we come across with a bigalloc file
3693  * system with zero in s_overhead_clusters the estimate will be close to
3694  * correct especially for very large cluster sizes --- but for newer
3695  * file systems, it's better to calculate this figure once at mkfs
3696  * time, and store it in the superblock.  If the superblock value is
3697  * present (even for non-bigalloc file systems), we will use it.
3698  */
3699 static int count_overhead(struct super_block *sb, ext4_group_t grp,
3700 			  char *buf)
3701 {
3702 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3703 	struct ext4_group_desc	*gdp;
3704 	ext4_fsblk_t		first_block, last_block, b;
3705 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3706 	int			s, j, count = 0;
3707 
3708 	if (!ext4_has_feature_bigalloc(sb))
3709 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
3710 			sbi->s_itb_per_group + 2);
3711 
3712 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3713 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3714 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3715 	for (i = 0; i < ngroups; i++) {
3716 		gdp = ext4_get_group_desc(sb, i, NULL);
3717 		b = ext4_block_bitmap(sb, gdp);
3718 		if (b >= first_block && b <= last_block) {
3719 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3720 			count++;
3721 		}
3722 		b = ext4_inode_bitmap(sb, gdp);
3723 		if (b >= first_block && b <= last_block) {
3724 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3725 			count++;
3726 		}
3727 		b = ext4_inode_table(sb, gdp);
3728 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3729 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3730 				int c = EXT4_B2C(sbi, b - first_block);
3731 				ext4_set_bit(c, buf);
3732 				count++;
3733 			}
3734 		if (i != grp)
3735 			continue;
3736 		s = 0;
3737 		if (ext4_bg_has_super(sb, grp)) {
3738 			ext4_set_bit(s++, buf);
3739 			count++;
3740 		}
3741 		j = ext4_bg_num_gdb(sb, grp);
3742 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3743 			ext4_error(sb, "Invalid number of block group "
3744 				   "descriptor blocks: %d", j);
3745 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3746 		}
3747 		count += j;
3748 		for (; j > 0; j--)
3749 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3750 	}
3751 	if (!count)
3752 		return 0;
3753 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3754 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3755 }
3756 
3757 /*
3758  * Compute the overhead and stash it in sbi->s_overhead
3759  */
3760 int ext4_calculate_overhead(struct super_block *sb)
3761 {
3762 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3763 	struct ext4_super_block *es = sbi->s_es;
3764 	struct inode *j_inode;
3765 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3766 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3767 	ext4_fsblk_t overhead = 0;
3768 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3769 
3770 	if (!buf)
3771 		return -ENOMEM;
3772 
3773 	/*
3774 	 * Compute the overhead (FS structures).  This is constant
3775 	 * for a given filesystem unless the number of block groups
3776 	 * changes so we cache the previous value until it does.
3777 	 */
3778 
3779 	/*
3780 	 * All of the blocks before first_data_block are overhead
3781 	 */
3782 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3783 
3784 	/*
3785 	 * Add the overhead found in each block group
3786 	 */
3787 	for (i = 0; i < ngroups; i++) {
3788 		int blks;
3789 
3790 		blks = count_overhead(sb, i, buf);
3791 		overhead += blks;
3792 		if (blks)
3793 			memset(buf, 0, PAGE_SIZE);
3794 		cond_resched();
3795 	}
3796 
3797 	/*
3798 	 * Add the internal journal blocks whether the journal has been
3799 	 * loaded or not
3800 	 */
3801 	if (sbi->s_journal && !sbi->s_journal_bdev)
3802 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
3803 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3804 		/* j_inum for internal journal is non-zero */
3805 		j_inode = ext4_get_journal_inode(sb, j_inum);
3806 		if (j_inode) {
3807 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
3808 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
3809 			iput(j_inode);
3810 		} else {
3811 			ext4_msg(sb, KERN_ERR, "can't get journal size");
3812 		}
3813 	}
3814 	sbi->s_overhead = overhead;
3815 	smp_wmb();
3816 	free_page((unsigned long) buf);
3817 	return 0;
3818 }
3819 
3820 static void ext4_set_resv_clusters(struct super_block *sb)
3821 {
3822 	ext4_fsblk_t resv_clusters;
3823 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3824 
3825 	/*
3826 	 * There's no need to reserve anything when we aren't using extents.
3827 	 * The space estimates are exact, there are no unwritten extents,
3828 	 * hole punching doesn't need new metadata... This is needed especially
3829 	 * to keep ext2/3 backward compatibility.
3830 	 */
3831 	if (!ext4_has_feature_extents(sb))
3832 		return;
3833 	/*
3834 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
3835 	 * This should cover the situations where we can not afford to run
3836 	 * out of space like for example punch hole, or converting
3837 	 * unwritten extents in delalloc path. In most cases such
3838 	 * allocation would require 1, or 2 blocks, higher numbers are
3839 	 * very rare.
3840 	 */
3841 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3842 			 sbi->s_cluster_bits);
3843 
3844 	do_div(resv_clusters, 50);
3845 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
3846 
3847 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
3848 }
3849 
3850 static const char *ext4_quota_mode(struct super_block *sb)
3851 {
3852 #ifdef CONFIG_QUOTA
3853 	if (!ext4_quota_capable(sb))
3854 		return "none";
3855 
3856 	if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
3857 		return "journalled";
3858 	else
3859 		return "writeback";
3860 #else
3861 	return "disabled";
3862 #endif
3863 }
3864 
3865 static void ext4_setup_csum_trigger(struct super_block *sb,
3866 				    enum ext4_journal_trigger_type type,
3867 				    void (*trigger)(
3868 					struct jbd2_buffer_trigger_type *type,
3869 					struct buffer_head *bh,
3870 					void *mapped_data,
3871 					size_t size))
3872 {
3873 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3874 
3875 	sbi->s_journal_triggers[type].sb = sb;
3876 	sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger;
3877 }
3878 
3879 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3880 {
3881 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3882 	char *orig_data = kstrdup(data, GFP_KERNEL);
3883 	struct buffer_head *bh, **group_desc;
3884 	struct ext4_super_block *es = NULL;
3885 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3886 	struct flex_groups **flex_groups;
3887 	ext4_fsblk_t block;
3888 	ext4_fsblk_t sb_block = get_sb_block(&data);
3889 	ext4_fsblk_t logical_sb_block;
3890 	unsigned long offset = 0;
3891 	unsigned long def_mount_opts;
3892 	struct inode *root;
3893 	const char *descr;
3894 	int ret = -ENOMEM;
3895 	int blocksize, clustersize;
3896 	unsigned int db_count;
3897 	unsigned int i;
3898 	int needs_recovery, has_huge_files;
3899 	__u64 blocks_count;
3900 	int err = 0;
3901 	ext4_group_t first_not_zeroed;
3902 	struct ext4_parsed_options parsed_opts;
3903 
3904 	/* Set defaults for the variables that will be set during parsing */
3905 	parsed_opts.journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3906 	parsed_opts.journal_devnum = 0;
3907 	parsed_opts.mb_optimize_scan = DEFAULT_MB_OPTIMIZE_SCAN;
3908 
3909 	if ((data && !orig_data) || !sbi)
3910 		goto out_free_base;
3911 
3912 	sbi->s_daxdev = dax_dev;
3913 	sbi->s_blockgroup_lock =
3914 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3915 	if (!sbi->s_blockgroup_lock)
3916 		goto out_free_base;
3917 
3918 	sb->s_fs_info = sbi;
3919 	sbi->s_sb = sb;
3920 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3921 	sbi->s_sb_block = sb_block;
3922 	sbi->s_sectors_written_start =
3923 		part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
3924 
3925 	/* Cleanup superblock name */
3926 	strreplace(sb->s_id, '/', '!');
3927 
3928 	/* -EINVAL is default */
3929 	ret = -EINVAL;
3930 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3931 	if (!blocksize) {
3932 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3933 		goto out_fail;
3934 	}
3935 
3936 	/*
3937 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3938 	 * block sizes.  We need to calculate the offset from buffer start.
3939 	 */
3940 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
3941 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
3942 		offset = do_div(logical_sb_block, blocksize);
3943 	} else {
3944 		logical_sb_block = sb_block;
3945 	}
3946 
3947 	bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
3948 	if (IS_ERR(bh)) {
3949 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3950 		ret = PTR_ERR(bh);
3951 		goto out_fail;
3952 	}
3953 	/*
3954 	 * Note: s_es must be initialized as soon as possible because
3955 	 *       some ext4 macro-instructions depend on its value
3956 	 */
3957 	es = (struct ext4_super_block *) (bh->b_data + offset);
3958 	sbi->s_es = es;
3959 	sb->s_magic = le16_to_cpu(es->s_magic);
3960 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3961 		goto cantfind_ext4;
3962 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3963 
3964 	/* Warn if metadata_csum and gdt_csum are both set. */
3965 	if (ext4_has_feature_metadata_csum(sb) &&
3966 	    ext4_has_feature_gdt_csum(sb))
3967 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3968 			     "redundant flags; please run fsck.");
3969 
3970 	/* Check for a known checksum algorithm */
3971 	if (!ext4_verify_csum_type(sb, es)) {
3972 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3973 			 "unknown checksum algorithm.");
3974 		silent = 1;
3975 		goto cantfind_ext4;
3976 	}
3977 	ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
3978 				ext4_orphan_file_block_trigger);
3979 
3980 	/* Load the checksum driver */
3981 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
3982 	if (IS_ERR(sbi->s_chksum_driver)) {
3983 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
3984 		ret = PTR_ERR(sbi->s_chksum_driver);
3985 		sbi->s_chksum_driver = NULL;
3986 		goto failed_mount;
3987 	}
3988 
3989 	/* Check superblock checksum */
3990 	if (!ext4_superblock_csum_verify(sb, es)) {
3991 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3992 			 "invalid superblock checksum.  Run e2fsck?");
3993 		silent = 1;
3994 		ret = -EFSBADCRC;
3995 		goto cantfind_ext4;
3996 	}
3997 
3998 	/* Precompute checksum seed for all metadata */
3999 	if (ext4_has_feature_csum_seed(sb))
4000 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4001 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4002 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4003 					       sizeof(es->s_uuid));
4004 
4005 	/* Set defaults before we parse the mount options */
4006 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4007 	set_opt(sb, INIT_INODE_TABLE);
4008 	if (def_mount_opts & EXT4_DEFM_DEBUG)
4009 		set_opt(sb, DEBUG);
4010 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4011 		set_opt(sb, GRPID);
4012 	if (def_mount_opts & EXT4_DEFM_UID16)
4013 		set_opt(sb, NO_UID32);
4014 	/* xattr user namespace & acls are now defaulted on */
4015 	set_opt(sb, XATTR_USER);
4016 #ifdef CONFIG_EXT4_FS_POSIX_ACL
4017 	set_opt(sb, POSIX_ACL);
4018 #endif
4019 	if (ext4_has_feature_fast_commit(sb))
4020 		set_opt2(sb, JOURNAL_FAST_COMMIT);
4021 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
4022 	if (ext4_has_metadata_csum(sb))
4023 		set_opt(sb, JOURNAL_CHECKSUM);
4024 
4025 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4026 		set_opt(sb, JOURNAL_DATA);
4027 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4028 		set_opt(sb, ORDERED_DATA);
4029 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4030 		set_opt(sb, WRITEBACK_DATA);
4031 
4032 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
4033 		set_opt(sb, ERRORS_PANIC);
4034 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
4035 		set_opt(sb, ERRORS_CONT);
4036 	else
4037 		set_opt(sb, ERRORS_RO);
4038 	/* block_validity enabled by default; disable with noblock_validity */
4039 	set_opt(sb, BLOCK_VALIDITY);
4040 	if (def_mount_opts & EXT4_DEFM_DISCARD)
4041 		set_opt(sb, DISCARD);
4042 
4043 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
4044 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
4045 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
4046 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
4047 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
4048 
4049 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4050 		set_opt(sb, BARRIER);
4051 
4052 	/*
4053 	 * enable delayed allocation by default
4054 	 * Use -o nodelalloc to turn it off
4055 	 */
4056 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
4057 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4058 		set_opt(sb, DELALLOC);
4059 
4060 	/*
4061 	 * set default s_li_wait_mult for lazyinit, for the case there is
4062 	 * no mount option specified.
4063 	 */
4064 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
4065 
4066 	if (le32_to_cpu(es->s_log_block_size) >
4067 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4068 		ext4_msg(sb, KERN_ERR,
4069 			 "Invalid log block size: %u",
4070 			 le32_to_cpu(es->s_log_block_size));
4071 		goto failed_mount;
4072 	}
4073 	if (le32_to_cpu(es->s_log_cluster_size) >
4074 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4075 		ext4_msg(sb, KERN_ERR,
4076 			 "Invalid log cluster size: %u",
4077 			 le32_to_cpu(es->s_log_cluster_size));
4078 		goto failed_mount;
4079 	}
4080 
4081 	blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
4082 
4083 	if (blocksize == PAGE_SIZE)
4084 		set_opt(sb, DIOREAD_NOLOCK);
4085 
4086 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4087 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4088 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4089 	} else {
4090 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4091 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4092 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4093 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4094 				 sbi->s_first_ino);
4095 			goto failed_mount;
4096 		}
4097 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
4098 		    (!is_power_of_2(sbi->s_inode_size)) ||
4099 		    (sbi->s_inode_size > blocksize)) {
4100 			ext4_msg(sb, KERN_ERR,
4101 			       "unsupported inode size: %d",
4102 			       sbi->s_inode_size);
4103 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
4104 			goto failed_mount;
4105 		}
4106 		/*
4107 		 * i_atime_extra is the last extra field available for
4108 		 * [acm]times in struct ext4_inode. Checking for that
4109 		 * field should suffice to ensure we have extra space
4110 		 * for all three.
4111 		 */
4112 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
4113 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
4114 			sb->s_time_gran = 1;
4115 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
4116 		} else {
4117 			sb->s_time_gran = NSEC_PER_SEC;
4118 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
4119 		}
4120 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
4121 	}
4122 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4123 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4124 			EXT4_GOOD_OLD_INODE_SIZE;
4125 		if (ext4_has_feature_extra_isize(sb)) {
4126 			unsigned v, max = (sbi->s_inode_size -
4127 					   EXT4_GOOD_OLD_INODE_SIZE);
4128 
4129 			v = le16_to_cpu(es->s_want_extra_isize);
4130 			if (v > max) {
4131 				ext4_msg(sb, KERN_ERR,
4132 					 "bad s_want_extra_isize: %d", v);
4133 				goto failed_mount;
4134 			}
4135 			if (sbi->s_want_extra_isize < v)
4136 				sbi->s_want_extra_isize = v;
4137 
4138 			v = le16_to_cpu(es->s_min_extra_isize);
4139 			if (v > max) {
4140 				ext4_msg(sb, KERN_ERR,
4141 					 "bad s_min_extra_isize: %d", v);
4142 				goto failed_mount;
4143 			}
4144 			if (sbi->s_want_extra_isize < v)
4145 				sbi->s_want_extra_isize = v;
4146 		}
4147 	}
4148 
4149 	if (sbi->s_es->s_mount_opts[0]) {
4150 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
4151 					      sizeof(sbi->s_es->s_mount_opts),
4152 					      GFP_KERNEL);
4153 		if (!s_mount_opts)
4154 			goto failed_mount;
4155 		if (!parse_options(s_mount_opts, sb, &parsed_opts, 0)) {
4156 			ext4_msg(sb, KERN_WARNING,
4157 				 "failed to parse options in superblock: %s",
4158 				 s_mount_opts);
4159 		}
4160 		kfree(s_mount_opts);
4161 	}
4162 	sbi->s_def_mount_opt = sbi->s_mount_opt;
4163 	if (!parse_options((char *) data, sb, &parsed_opts, 0))
4164 		goto failed_mount;
4165 
4166 #ifdef CONFIG_UNICODE
4167 	if (ext4_has_feature_casefold(sb) && !sb->s_encoding) {
4168 		const struct ext4_sb_encodings *encoding_info;
4169 		struct unicode_map *encoding;
4170 		__u16 encoding_flags;
4171 
4172 		if (ext4_sb_read_encoding(es, &encoding_info,
4173 					  &encoding_flags)) {
4174 			ext4_msg(sb, KERN_ERR,
4175 				 "Encoding requested by superblock is unknown");
4176 			goto failed_mount;
4177 		}
4178 
4179 		encoding = utf8_load(encoding_info->version);
4180 		if (IS_ERR(encoding)) {
4181 			ext4_msg(sb, KERN_ERR,
4182 				 "can't mount with superblock charset: %s-%s "
4183 				 "not supported by the kernel. flags: 0x%x.",
4184 				 encoding_info->name, encoding_info->version,
4185 				 encoding_flags);
4186 			goto failed_mount;
4187 		}
4188 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4189 			 "%s-%s with flags 0x%hx", encoding_info->name,
4190 			 encoding_info->version?:"\b", encoding_flags);
4191 
4192 		sb->s_encoding = encoding;
4193 		sb->s_encoding_flags = encoding_flags;
4194 	}
4195 #endif
4196 
4197 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4198 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, O_DIRECT and fast_commit support!\n");
4199 		/* can't mount with both data=journal and dioread_nolock. */
4200 		clear_opt(sb, DIOREAD_NOLOCK);
4201 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
4202 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4203 			ext4_msg(sb, KERN_ERR, "can't mount with "
4204 				 "both data=journal and delalloc");
4205 			goto failed_mount;
4206 		}
4207 		if (test_opt(sb, DAX_ALWAYS)) {
4208 			ext4_msg(sb, KERN_ERR, "can't mount with "
4209 				 "both data=journal and dax");
4210 			goto failed_mount;
4211 		}
4212 		if (ext4_has_feature_encrypt(sb)) {
4213 			ext4_msg(sb, KERN_WARNING,
4214 				 "encrypted files will use data=ordered "
4215 				 "instead of data journaling mode");
4216 		}
4217 		if (test_opt(sb, DELALLOC))
4218 			clear_opt(sb, DELALLOC);
4219 	} else {
4220 		sb->s_iflags |= SB_I_CGROUPWB;
4221 	}
4222 
4223 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4224 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4225 
4226 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4227 	    (ext4_has_compat_features(sb) ||
4228 	     ext4_has_ro_compat_features(sb) ||
4229 	     ext4_has_incompat_features(sb)))
4230 		ext4_msg(sb, KERN_WARNING,
4231 		       "feature flags set on rev 0 fs, "
4232 		       "running e2fsck is recommended");
4233 
4234 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4235 		set_opt2(sb, HURD_COMPAT);
4236 		if (ext4_has_feature_64bit(sb)) {
4237 			ext4_msg(sb, KERN_ERR,
4238 				 "The Hurd can't support 64-bit file systems");
4239 			goto failed_mount;
4240 		}
4241 
4242 		/*
4243 		 * ea_inode feature uses l_i_version field which is not
4244 		 * available in HURD_COMPAT mode.
4245 		 */
4246 		if (ext4_has_feature_ea_inode(sb)) {
4247 			ext4_msg(sb, KERN_ERR,
4248 				 "ea_inode feature is not supported for Hurd");
4249 			goto failed_mount;
4250 		}
4251 	}
4252 
4253 	if (IS_EXT2_SB(sb)) {
4254 		if (ext2_feature_set_ok(sb))
4255 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
4256 				 "using the ext4 subsystem");
4257 		else {
4258 			/*
4259 			 * If we're probing be silent, if this looks like
4260 			 * it's actually an ext[34] filesystem.
4261 			 */
4262 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4263 				goto failed_mount;
4264 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
4265 				 "to feature incompatibilities");
4266 			goto failed_mount;
4267 		}
4268 	}
4269 
4270 	if (IS_EXT3_SB(sb)) {
4271 		if (ext3_feature_set_ok(sb))
4272 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
4273 				 "using the ext4 subsystem");
4274 		else {
4275 			/*
4276 			 * If we're probing be silent, if this looks like
4277 			 * it's actually an ext4 filesystem.
4278 			 */
4279 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4280 				goto failed_mount;
4281 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
4282 				 "to feature incompatibilities");
4283 			goto failed_mount;
4284 		}
4285 	}
4286 
4287 	/*
4288 	 * Check feature flags regardless of the revision level, since we
4289 	 * previously didn't change the revision level when setting the flags,
4290 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4291 	 */
4292 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4293 		goto failed_mount;
4294 
4295 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
4296 		ext4_msg(sb, KERN_ERR,
4297 			 "Number of reserved GDT blocks insanely large: %d",
4298 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
4299 		goto failed_mount;
4300 	}
4301 
4302 	if (dax_supported(dax_dev, sb->s_bdev, blocksize, 0,
4303 			bdev_nr_sectors(sb->s_bdev)))
4304 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4305 
4306 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4307 		if (ext4_has_feature_inline_data(sb)) {
4308 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4309 					" that may contain inline data");
4310 			goto failed_mount;
4311 		}
4312 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
4313 			ext4_msg(sb, KERN_ERR,
4314 				"DAX unsupported by block device.");
4315 			goto failed_mount;
4316 		}
4317 	}
4318 
4319 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
4320 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
4321 			 es->s_encryption_level);
4322 		goto failed_mount;
4323 	}
4324 
4325 	if (sb->s_blocksize != blocksize) {
4326 		/*
4327 		 * bh must be released before kill_bdev(), otherwise
4328 		 * it won't be freed and its page also. kill_bdev()
4329 		 * is called by sb_set_blocksize().
4330 		 */
4331 		brelse(bh);
4332 		/* Validate the filesystem blocksize */
4333 		if (!sb_set_blocksize(sb, blocksize)) {
4334 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4335 					blocksize);
4336 			bh = NULL;
4337 			goto failed_mount;
4338 		}
4339 
4340 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
4341 		offset = do_div(logical_sb_block, blocksize);
4342 		bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
4343 		if (IS_ERR(bh)) {
4344 			ext4_msg(sb, KERN_ERR,
4345 			       "Can't read superblock on 2nd try");
4346 			ret = PTR_ERR(bh);
4347 			bh = NULL;
4348 			goto failed_mount;
4349 		}
4350 		es = (struct ext4_super_block *)(bh->b_data + offset);
4351 		sbi->s_es = es;
4352 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4353 			ext4_msg(sb, KERN_ERR,
4354 			       "Magic mismatch, very weird!");
4355 			goto failed_mount;
4356 		}
4357 	}
4358 
4359 	has_huge_files = ext4_has_feature_huge_file(sb);
4360 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4361 						      has_huge_files);
4362 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4363 
4364 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4365 	if (ext4_has_feature_64bit(sb)) {
4366 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
4367 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4368 		    !is_power_of_2(sbi->s_desc_size)) {
4369 			ext4_msg(sb, KERN_ERR,
4370 			       "unsupported descriptor size %lu",
4371 			       sbi->s_desc_size);
4372 			goto failed_mount;
4373 		}
4374 	} else
4375 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
4376 
4377 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4378 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
4379 
4380 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4381 	if (sbi->s_inodes_per_block == 0)
4382 		goto cantfind_ext4;
4383 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4384 	    sbi->s_inodes_per_group > blocksize * 8) {
4385 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4386 			 sbi->s_inodes_per_group);
4387 		goto failed_mount;
4388 	}
4389 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4390 					sbi->s_inodes_per_block;
4391 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4392 	sbi->s_sbh = bh;
4393 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4394 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4395 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
4396 
4397 	for (i = 0; i < 4; i++)
4398 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4399 	sbi->s_def_hash_version = es->s_def_hash_version;
4400 	if (ext4_has_feature_dir_index(sb)) {
4401 		i = le32_to_cpu(es->s_flags);
4402 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4403 			sbi->s_hash_unsigned = 3;
4404 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4405 #ifdef __CHAR_UNSIGNED__
4406 			if (!sb_rdonly(sb))
4407 				es->s_flags |=
4408 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4409 			sbi->s_hash_unsigned = 3;
4410 #else
4411 			if (!sb_rdonly(sb))
4412 				es->s_flags |=
4413 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4414 #endif
4415 		}
4416 	}
4417 
4418 	/* Handle clustersize */
4419 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4420 	if (ext4_has_feature_bigalloc(sb)) {
4421 		if (clustersize < blocksize) {
4422 			ext4_msg(sb, KERN_ERR,
4423 				 "cluster size (%d) smaller than "
4424 				 "block size (%d)", clustersize, blocksize);
4425 			goto failed_mount;
4426 		}
4427 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4428 			le32_to_cpu(es->s_log_block_size);
4429 		sbi->s_clusters_per_group =
4430 			le32_to_cpu(es->s_clusters_per_group);
4431 		if (sbi->s_clusters_per_group > blocksize * 8) {
4432 			ext4_msg(sb, KERN_ERR,
4433 				 "#clusters per group too big: %lu",
4434 				 sbi->s_clusters_per_group);
4435 			goto failed_mount;
4436 		}
4437 		if (sbi->s_blocks_per_group !=
4438 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4439 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4440 				 "clusters per group (%lu) inconsistent",
4441 				 sbi->s_blocks_per_group,
4442 				 sbi->s_clusters_per_group);
4443 			goto failed_mount;
4444 		}
4445 	} else {
4446 		if (clustersize != blocksize) {
4447 			ext4_msg(sb, KERN_ERR,
4448 				 "fragment/cluster size (%d) != "
4449 				 "block size (%d)", clustersize, blocksize);
4450 			goto failed_mount;
4451 		}
4452 		if (sbi->s_blocks_per_group > blocksize * 8) {
4453 			ext4_msg(sb, KERN_ERR,
4454 				 "#blocks per group too big: %lu",
4455 				 sbi->s_blocks_per_group);
4456 			goto failed_mount;
4457 		}
4458 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4459 		sbi->s_cluster_bits = 0;
4460 	}
4461 	sbi->s_cluster_ratio = clustersize / blocksize;
4462 
4463 	/* Do we have standard group size of clustersize * 8 blocks ? */
4464 	if (sbi->s_blocks_per_group == clustersize << 3)
4465 		set_opt2(sb, STD_GROUP_SIZE);
4466 
4467 	/*
4468 	 * Test whether we have more sectors than will fit in sector_t,
4469 	 * and whether the max offset is addressable by the page cache.
4470 	 */
4471 	err = generic_check_addressable(sb->s_blocksize_bits,
4472 					ext4_blocks_count(es));
4473 	if (err) {
4474 		ext4_msg(sb, KERN_ERR, "filesystem"
4475 			 " too large to mount safely on this system");
4476 		goto failed_mount;
4477 	}
4478 
4479 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4480 		goto cantfind_ext4;
4481 
4482 	/* check blocks count against device size */
4483 	blocks_count = sb_bdev_nr_blocks(sb);
4484 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4485 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4486 		       "exceeds size of device (%llu blocks)",
4487 		       ext4_blocks_count(es), blocks_count);
4488 		goto failed_mount;
4489 	}
4490 
4491 	/*
4492 	 * It makes no sense for the first data block to be beyond the end
4493 	 * of the filesystem.
4494 	 */
4495 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4496 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4497 			 "block %u is beyond end of filesystem (%llu)",
4498 			 le32_to_cpu(es->s_first_data_block),
4499 			 ext4_blocks_count(es));
4500 		goto failed_mount;
4501 	}
4502 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4503 	    (sbi->s_cluster_ratio == 1)) {
4504 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4505 			 "block is 0 with a 1k block and cluster size");
4506 		goto failed_mount;
4507 	}
4508 
4509 	blocks_count = (ext4_blocks_count(es) -
4510 			le32_to_cpu(es->s_first_data_block) +
4511 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4512 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4513 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4514 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
4515 		       "(block count %llu, first data block %u, "
4516 		       "blocks per group %lu)", blocks_count,
4517 		       ext4_blocks_count(es),
4518 		       le32_to_cpu(es->s_first_data_block),
4519 		       EXT4_BLOCKS_PER_GROUP(sb));
4520 		goto failed_mount;
4521 	}
4522 	sbi->s_groups_count = blocks_count;
4523 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4524 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
4525 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
4526 	    le32_to_cpu(es->s_inodes_count)) {
4527 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
4528 			 le32_to_cpu(es->s_inodes_count),
4529 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
4530 		ret = -EINVAL;
4531 		goto failed_mount;
4532 	}
4533 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4534 		   EXT4_DESC_PER_BLOCK(sb);
4535 	if (ext4_has_feature_meta_bg(sb)) {
4536 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
4537 			ext4_msg(sb, KERN_WARNING,
4538 				 "first meta block group too large: %u "
4539 				 "(group descriptor block count %u)",
4540 				 le32_to_cpu(es->s_first_meta_bg), db_count);
4541 			goto failed_mount;
4542 		}
4543 	}
4544 	rcu_assign_pointer(sbi->s_group_desc,
4545 			   kvmalloc_array(db_count,
4546 					  sizeof(struct buffer_head *),
4547 					  GFP_KERNEL));
4548 	if (sbi->s_group_desc == NULL) {
4549 		ext4_msg(sb, KERN_ERR, "not enough memory");
4550 		ret = -ENOMEM;
4551 		goto failed_mount;
4552 	}
4553 
4554 	bgl_lock_init(sbi->s_blockgroup_lock);
4555 
4556 	/* Pre-read the descriptors into the buffer cache */
4557 	for (i = 0; i < db_count; i++) {
4558 		block = descriptor_loc(sb, logical_sb_block, i);
4559 		ext4_sb_breadahead_unmovable(sb, block);
4560 	}
4561 
4562 	for (i = 0; i < db_count; i++) {
4563 		struct buffer_head *bh;
4564 
4565 		block = descriptor_loc(sb, logical_sb_block, i);
4566 		bh = ext4_sb_bread_unmovable(sb, block);
4567 		if (IS_ERR(bh)) {
4568 			ext4_msg(sb, KERN_ERR,
4569 			       "can't read group descriptor %d", i);
4570 			db_count = i;
4571 			ret = PTR_ERR(bh);
4572 			goto failed_mount2;
4573 		}
4574 		rcu_read_lock();
4575 		rcu_dereference(sbi->s_group_desc)[i] = bh;
4576 		rcu_read_unlock();
4577 	}
4578 	sbi->s_gdb_count = db_count;
4579 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4580 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
4581 		ret = -EFSCORRUPTED;
4582 		goto failed_mount2;
4583 	}
4584 
4585 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
4586 	spin_lock_init(&sbi->s_error_lock);
4587 	INIT_WORK(&sbi->s_error_work, flush_stashed_error_work);
4588 
4589 	/* Register extent status tree shrinker */
4590 	if (ext4_es_register_shrinker(sbi))
4591 		goto failed_mount3;
4592 
4593 	sbi->s_stripe = ext4_get_stripe_size(sbi);
4594 	sbi->s_extent_max_zeroout_kb = 32;
4595 
4596 	/*
4597 	 * set up enough so that it can read an inode
4598 	 */
4599 	sb->s_op = &ext4_sops;
4600 	sb->s_export_op = &ext4_export_ops;
4601 	sb->s_xattr = ext4_xattr_handlers;
4602 #ifdef CONFIG_FS_ENCRYPTION
4603 	sb->s_cop = &ext4_cryptops;
4604 #endif
4605 #ifdef CONFIG_FS_VERITY
4606 	sb->s_vop = &ext4_verityops;
4607 #endif
4608 #ifdef CONFIG_QUOTA
4609 	sb->dq_op = &ext4_quota_operations;
4610 	if (ext4_has_feature_quota(sb))
4611 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4612 	else
4613 		sb->s_qcop = &ext4_qctl_operations;
4614 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4615 #endif
4616 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4617 
4618 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
4619 	mutex_init(&sbi->s_orphan_lock);
4620 
4621 	/* Initialize fast commit stuff */
4622 	atomic_set(&sbi->s_fc_subtid, 0);
4623 	atomic_set(&sbi->s_fc_ineligible_updates, 0);
4624 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4625 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4626 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4627 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4628 	sbi->s_fc_bytes = 0;
4629 	ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
4630 	ext4_clear_mount_flag(sb, EXT4_MF_FC_COMMITTING);
4631 	spin_lock_init(&sbi->s_fc_lock);
4632 	memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
4633 	sbi->s_fc_replay_state.fc_regions = NULL;
4634 	sbi->s_fc_replay_state.fc_regions_size = 0;
4635 	sbi->s_fc_replay_state.fc_regions_used = 0;
4636 	sbi->s_fc_replay_state.fc_regions_valid = 0;
4637 	sbi->s_fc_replay_state.fc_modified_inodes = NULL;
4638 	sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
4639 	sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4640 
4641 	sb->s_root = NULL;
4642 
4643 	needs_recovery = (es->s_last_orphan != 0 ||
4644 			  ext4_has_feature_orphan_present(sb) ||
4645 			  ext4_has_feature_journal_needs_recovery(sb));
4646 
4647 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4648 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
4649 			goto failed_mount3a;
4650 
4651 	/*
4652 	 * The first inode we look at is the journal inode.  Don't try
4653 	 * root first: it may be modified in the journal!
4654 	 */
4655 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
4656 		err = ext4_load_journal(sb, es, parsed_opts.journal_devnum);
4657 		if (err)
4658 			goto failed_mount3a;
4659 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4660 		   ext4_has_feature_journal_needs_recovery(sb)) {
4661 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4662 		       "suppressed and not mounted read-only");
4663 		goto failed_mount_wq;
4664 	} else {
4665 		/* Nojournal mode, all journal mount options are illegal */
4666 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
4667 			ext4_msg(sb, KERN_ERR, "can't mount with "
4668 				 "journal_checksum, fs mounted w/o journal");
4669 			goto failed_mount_wq;
4670 		}
4671 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4672 			ext4_msg(sb, KERN_ERR, "can't mount with "
4673 				 "journal_async_commit, fs mounted w/o journal");
4674 			goto failed_mount_wq;
4675 		}
4676 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
4677 			ext4_msg(sb, KERN_ERR, "can't mount with "
4678 				 "commit=%lu, fs mounted w/o journal",
4679 				 sbi->s_commit_interval / HZ);
4680 			goto failed_mount_wq;
4681 		}
4682 		if (EXT4_MOUNT_DATA_FLAGS &
4683 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
4684 			ext4_msg(sb, KERN_ERR, "can't mount with "
4685 				 "data=, fs mounted w/o journal");
4686 			goto failed_mount_wq;
4687 		}
4688 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
4689 		clear_opt(sb, JOURNAL_CHECKSUM);
4690 		clear_opt(sb, DATA_FLAGS);
4691 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
4692 		sbi->s_journal = NULL;
4693 		needs_recovery = 0;
4694 		goto no_journal;
4695 	}
4696 
4697 	if (ext4_has_feature_64bit(sb) &&
4698 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4699 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4700 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4701 		goto failed_mount_wq;
4702 	}
4703 
4704 	if (!set_journal_csum_feature_set(sb)) {
4705 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
4706 			 "feature set");
4707 		goto failed_mount_wq;
4708 	}
4709 
4710 	if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
4711 		!jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4712 					  JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
4713 		ext4_msg(sb, KERN_ERR,
4714 			"Failed to set fast commit journal feature");
4715 		goto failed_mount_wq;
4716 	}
4717 
4718 	/* We have now updated the journal if required, so we can
4719 	 * validate the data journaling mode. */
4720 	switch (test_opt(sb, DATA_FLAGS)) {
4721 	case 0:
4722 		/* No mode set, assume a default based on the journal
4723 		 * capabilities: ORDERED_DATA if the journal can
4724 		 * cope, else JOURNAL_DATA
4725 		 */
4726 		if (jbd2_journal_check_available_features
4727 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4728 			set_opt(sb, ORDERED_DATA);
4729 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
4730 		} else {
4731 			set_opt(sb, JOURNAL_DATA);
4732 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
4733 		}
4734 		break;
4735 
4736 	case EXT4_MOUNT_ORDERED_DATA:
4737 	case EXT4_MOUNT_WRITEBACK_DATA:
4738 		if (!jbd2_journal_check_available_features
4739 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4740 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4741 			       "requested data journaling mode");
4742 			goto failed_mount_wq;
4743 		}
4744 		break;
4745 	default:
4746 		break;
4747 	}
4748 
4749 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4750 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4751 		ext4_msg(sb, KERN_ERR, "can't mount with "
4752 			"journal_async_commit in data=ordered mode");
4753 		goto failed_mount_wq;
4754 	}
4755 
4756 	set_task_ioprio(sbi->s_journal->j_task, parsed_opts.journal_ioprio);
4757 
4758 	sbi->s_journal->j_submit_inode_data_buffers =
4759 		ext4_journal_submit_inode_data_buffers;
4760 	sbi->s_journal->j_finish_inode_data_buffers =
4761 		ext4_journal_finish_inode_data_buffers;
4762 
4763 no_journal:
4764 	if (!test_opt(sb, NO_MBCACHE)) {
4765 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
4766 		if (!sbi->s_ea_block_cache) {
4767 			ext4_msg(sb, KERN_ERR,
4768 				 "Failed to create ea_block_cache");
4769 			goto failed_mount_wq;
4770 		}
4771 
4772 		if (ext4_has_feature_ea_inode(sb)) {
4773 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4774 			if (!sbi->s_ea_inode_cache) {
4775 				ext4_msg(sb, KERN_ERR,
4776 					 "Failed to create ea_inode_cache");
4777 				goto failed_mount_wq;
4778 			}
4779 		}
4780 	}
4781 
4782 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4783 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4784 		goto failed_mount_wq;
4785 	}
4786 
4787 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4788 	    !ext4_has_feature_encrypt(sb)) {
4789 		ext4_set_feature_encrypt(sb);
4790 		ext4_commit_super(sb);
4791 	}
4792 
4793 	/*
4794 	 * Get the # of file system overhead blocks from the
4795 	 * superblock if present.
4796 	 */
4797 	if (es->s_overhead_clusters)
4798 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4799 	else {
4800 		err = ext4_calculate_overhead(sb);
4801 		if (err)
4802 			goto failed_mount_wq;
4803 	}
4804 
4805 	/*
4806 	 * The maximum number of concurrent works can be high and
4807 	 * concurrency isn't really necessary.  Limit it to 1.
4808 	 */
4809 	EXT4_SB(sb)->rsv_conversion_wq =
4810 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
4811 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
4812 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
4813 		ret = -ENOMEM;
4814 		goto failed_mount4;
4815 	}
4816 
4817 	/*
4818 	 * The jbd2_journal_load will have done any necessary log recovery,
4819 	 * so we can safely mount the rest of the filesystem now.
4820 	 */
4821 
4822 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
4823 	if (IS_ERR(root)) {
4824 		ext4_msg(sb, KERN_ERR, "get root inode failed");
4825 		ret = PTR_ERR(root);
4826 		root = NULL;
4827 		goto failed_mount4;
4828 	}
4829 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4830 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
4831 		iput(root);
4832 		goto failed_mount4;
4833 	}
4834 
4835 	sb->s_root = d_make_root(root);
4836 	if (!sb->s_root) {
4837 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
4838 		ret = -ENOMEM;
4839 		goto failed_mount4;
4840 	}
4841 
4842 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4843 	if (ret == -EROFS) {
4844 		sb->s_flags |= SB_RDONLY;
4845 		ret = 0;
4846 	} else if (ret)
4847 		goto failed_mount4a;
4848 
4849 	ext4_set_resv_clusters(sb);
4850 
4851 	if (test_opt(sb, BLOCK_VALIDITY)) {
4852 		err = ext4_setup_system_zone(sb);
4853 		if (err) {
4854 			ext4_msg(sb, KERN_ERR, "failed to initialize system "
4855 				 "zone (%d)", err);
4856 			goto failed_mount4a;
4857 		}
4858 	}
4859 	ext4_fc_replay_cleanup(sb);
4860 
4861 	ext4_ext_init(sb);
4862 
4863 	/*
4864 	 * Enable optimize_scan if number of groups is > threshold. This can be
4865 	 * turned off by passing "mb_optimize_scan=0". This can also be
4866 	 * turned on forcefully by passing "mb_optimize_scan=1".
4867 	 */
4868 	if (parsed_opts.mb_optimize_scan == 1)
4869 		set_opt2(sb, MB_OPTIMIZE_SCAN);
4870 	else if (parsed_opts.mb_optimize_scan == 0)
4871 		clear_opt2(sb, MB_OPTIMIZE_SCAN);
4872 	else if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD)
4873 		set_opt2(sb, MB_OPTIMIZE_SCAN);
4874 
4875 	err = ext4_mb_init(sb);
4876 	if (err) {
4877 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4878 			 err);
4879 		goto failed_mount5;
4880 	}
4881 
4882 	/*
4883 	 * We can only set up the journal commit callback once
4884 	 * mballoc is initialized
4885 	 */
4886 	if (sbi->s_journal)
4887 		sbi->s_journal->j_commit_callback =
4888 			ext4_journal_commit_callback;
4889 
4890 	block = ext4_count_free_clusters(sb);
4891 	ext4_free_blocks_count_set(sbi->s_es,
4892 				   EXT4_C2B(sbi, block));
4893 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4894 				  GFP_KERNEL);
4895 	if (!err) {
4896 		unsigned long freei = ext4_count_free_inodes(sb);
4897 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
4898 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4899 					  GFP_KERNEL);
4900 	}
4901 	/*
4902 	 * Update the checksum after updating free space/inode
4903 	 * counters.  Otherwise the superblock can have an incorrect
4904 	 * checksum in the buffer cache until it is written out and
4905 	 * e2fsprogs programs trying to open a file system immediately
4906 	 * after it is mounted can fail.
4907 	 */
4908 	ext4_superblock_csum_set(sb);
4909 	if (!err)
4910 		err = percpu_counter_init(&sbi->s_dirs_counter,
4911 					  ext4_count_dirs(sb), GFP_KERNEL);
4912 	if (!err)
4913 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4914 					  GFP_KERNEL);
4915 	if (!err)
4916 		err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
4917 					  GFP_KERNEL);
4918 	if (!err)
4919 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
4920 
4921 	if (err) {
4922 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4923 		goto failed_mount6;
4924 	}
4925 
4926 	if (ext4_has_feature_flex_bg(sb))
4927 		if (!ext4_fill_flex_info(sb)) {
4928 			ext4_msg(sb, KERN_ERR,
4929 			       "unable to initialize "
4930 			       "flex_bg meta info!");
4931 			ret = -ENOMEM;
4932 			goto failed_mount6;
4933 		}
4934 
4935 	err = ext4_register_li_request(sb, first_not_zeroed);
4936 	if (err)
4937 		goto failed_mount6;
4938 
4939 	err = ext4_register_sysfs(sb);
4940 	if (err)
4941 		goto failed_mount7;
4942 
4943 	err = ext4_init_orphan_info(sb);
4944 	if (err)
4945 		goto failed_mount8;
4946 #ifdef CONFIG_QUOTA
4947 	/* Enable quota usage during mount. */
4948 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
4949 		err = ext4_enable_quotas(sb);
4950 		if (err)
4951 			goto failed_mount9;
4952 	}
4953 #endif  /* CONFIG_QUOTA */
4954 
4955 	/*
4956 	 * Save the original bdev mapping's wb_err value which could be
4957 	 * used to detect the metadata async write error.
4958 	 */
4959 	spin_lock_init(&sbi->s_bdev_wb_lock);
4960 	errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
4961 				 &sbi->s_bdev_wb_err);
4962 	sb->s_bdev->bd_super = sb;
4963 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4964 	ext4_orphan_cleanup(sb, es);
4965 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
4966 	if (needs_recovery) {
4967 		ext4_msg(sb, KERN_INFO, "recovery complete");
4968 		err = ext4_mark_recovery_complete(sb, es);
4969 		if (err)
4970 			goto failed_mount9;
4971 	}
4972 	if (EXT4_SB(sb)->s_journal) {
4973 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
4974 			descr = " journalled data mode";
4975 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
4976 			descr = " ordered data mode";
4977 		else
4978 			descr = " writeback data mode";
4979 	} else
4980 		descr = "out journal";
4981 
4982 	if (test_opt(sb, DISCARD)) {
4983 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
4984 		if (!blk_queue_discard(q))
4985 			ext4_msg(sb, KERN_WARNING,
4986 				 "mounting with \"discard\" option, but "
4987 				 "the device does not support discard");
4988 	}
4989 
4990 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4991 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
4992 			 "Opts: %.*s%s%s. Quota mode: %s.", descr,
4993 			 (int) sizeof(sbi->s_es->s_mount_opts),
4994 			 sbi->s_es->s_mount_opts,
4995 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data,
4996 			 ext4_quota_mode(sb));
4997 
4998 	if (es->s_error_count)
4999 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5000 
5001 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5002 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5003 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5004 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
5005 	atomic_set(&sbi->s_warning_count, 0);
5006 	atomic_set(&sbi->s_msg_count, 0);
5007 
5008 	kfree(orig_data);
5009 	return 0;
5010 
5011 cantfind_ext4:
5012 	if (!silent)
5013 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5014 	goto failed_mount;
5015 
5016 failed_mount9:
5017 	ext4_release_orphan_info(sb);
5018 failed_mount8:
5019 	ext4_unregister_sysfs(sb);
5020 	kobject_put(&sbi->s_kobj);
5021 failed_mount7:
5022 	ext4_unregister_li_request(sb);
5023 failed_mount6:
5024 	ext4_mb_release(sb);
5025 	rcu_read_lock();
5026 	flex_groups = rcu_dereference(sbi->s_flex_groups);
5027 	if (flex_groups) {
5028 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
5029 			kvfree(flex_groups[i]);
5030 		kvfree(flex_groups);
5031 	}
5032 	rcu_read_unlock();
5033 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
5034 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
5035 	percpu_counter_destroy(&sbi->s_dirs_counter);
5036 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
5037 	percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
5038 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
5039 failed_mount5:
5040 	ext4_ext_release(sb);
5041 	ext4_release_system_zone(sb);
5042 failed_mount4a:
5043 	dput(sb->s_root);
5044 	sb->s_root = NULL;
5045 failed_mount4:
5046 	ext4_msg(sb, KERN_ERR, "mount failed");
5047 	if (EXT4_SB(sb)->rsv_conversion_wq)
5048 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
5049 failed_mount_wq:
5050 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5051 	sbi->s_ea_inode_cache = NULL;
5052 
5053 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
5054 	sbi->s_ea_block_cache = NULL;
5055 
5056 	if (sbi->s_journal) {
5057 		/* flush s_error_work before journal destroy. */
5058 		flush_work(&sbi->s_error_work);
5059 		jbd2_journal_destroy(sbi->s_journal);
5060 		sbi->s_journal = NULL;
5061 	}
5062 failed_mount3a:
5063 	ext4_es_unregister_shrinker(sbi);
5064 failed_mount3:
5065 	/* flush s_error_work before sbi destroy */
5066 	flush_work(&sbi->s_error_work);
5067 	del_timer_sync(&sbi->s_err_report);
5068 	ext4_stop_mmpd(sbi);
5069 failed_mount2:
5070 	rcu_read_lock();
5071 	group_desc = rcu_dereference(sbi->s_group_desc);
5072 	for (i = 0; i < db_count; i++)
5073 		brelse(group_desc[i]);
5074 	kvfree(group_desc);
5075 	rcu_read_unlock();
5076 failed_mount:
5077 	if (sbi->s_chksum_driver)
5078 		crypto_free_shash(sbi->s_chksum_driver);
5079 
5080 #ifdef CONFIG_UNICODE
5081 	utf8_unload(sb->s_encoding);
5082 #endif
5083 
5084 #ifdef CONFIG_QUOTA
5085 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5086 		kfree(get_qf_name(sb, sbi, i));
5087 #endif
5088 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5089 	/* ext4_blkdev_remove() calls kill_bdev(), release bh before it. */
5090 	brelse(bh);
5091 	ext4_blkdev_remove(sbi);
5092 out_fail:
5093 	sb->s_fs_info = NULL;
5094 	kfree(sbi->s_blockgroup_lock);
5095 out_free_base:
5096 	kfree(sbi);
5097 	kfree(orig_data);
5098 	fs_put_dax(dax_dev);
5099 	return err ? err : ret;
5100 }
5101 
5102 /*
5103  * Setup any per-fs journal parameters now.  We'll do this both on
5104  * initial mount, once the journal has been initialised but before we've
5105  * done any recovery; and again on any subsequent remount.
5106  */
5107 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5108 {
5109 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5110 
5111 	journal->j_commit_interval = sbi->s_commit_interval;
5112 	journal->j_min_batch_time = sbi->s_min_batch_time;
5113 	journal->j_max_batch_time = sbi->s_max_batch_time;
5114 	ext4_fc_init(sb, journal);
5115 
5116 	write_lock(&journal->j_state_lock);
5117 	if (test_opt(sb, BARRIER))
5118 		journal->j_flags |= JBD2_BARRIER;
5119 	else
5120 		journal->j_flags &= ~JBD2_BARRIER;
5121 	if (test_opt(sb, DATA_ERR_ABORT))
5122 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
5123 	else
5124 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5125 	write_unlock(&journal->j_state_lock);
5126 }
5127 
5128 static struct inode *ext4_get_journal_inode(struct super_block *sb,
5129 					     unsigned int journal_inum)
5130 {
5131 	struct inode *journal_inode;
5132 
5133 	/*
5134 	 * Test for the existence of a valid inode on disk.  Bad things
5135 	 * happen if we iget() an unused inode, as the subsequent iput()
5136 	 * will try to delete it.
5137 	 */
5138 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
5139 	if (IS_ERR(journal_inode)) {
5140 		ext4_msg(sb, KERN_ERR, "no journal found");
5141 		return NULL;
5142 	}
5143 	if (!journal_inode->i_nlink) {
5144 		make_bad_inode(journal_inode);
5145 		iput(journal_inode);
5146 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5147 		return NULL;
5148 	}
5149 
5150 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
5151 		  journal_inode, journal_inode->i_size);
5152 	if (!S_ISREG(journal_inode->i_mode)) {
5153 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
5154 		iput(journal_inode);
5155 		return NULL;
5156 	}
5157 	return journal_inode;
5158 }
5159 
5160 static journal_t *ext4_get_journal(struct super_block *sb,
5161 				   unsigned int journal_inum)
5162 {
5163 	struct inode *journal_inode;
5164 	journal_t *journal;
5165 
5166 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5167 		return NULL;
5168 
5169 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
5170 	if (!journal_inode)
5171 		return NULL;
5172 
5173 	journal = jbd2_journal_init_inode(journal_inode);
5174 	if (!journal) {
5175 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5176 		iput(journal_inode);
5177 		return NULL;
5178 	}
5179 	journal->j_private = sb;
5180 	ext4_init_journal_params(sb, journal);
5181 	return journal;
5182 }
5183 
5184 static journal_t *ext4_get_dev_journal(struct super_block *sb,
5185 				       dev_t j_dev)
5186 {
5187 	struct buffer_head *bh;
5188 	journal_t *journal;
5189 	ext4_fsblk_t start;
5190 	ext4_fsblk_t len;
5191 	int hblock, blocksize;
5192 	ext4_fsblk_t sb_block;
5193 	unsigned long offset;
5194 	struct ext4_super_block *es;
5195 	struct block_device *bdev;
5196 
5197 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5198 		return NULL;
5199 
5200 	bdev = ext4_blkdev_get(j_dev, sb);
5201 	if (bdev == NULL)
5202 		return NULL;
5203 
5204 	blocksize = sb->s_blocksize;
5205 	hblock = bdev_logical_block_size(bdev);
5206 	if (blocksize < hblock) {
5207 		ext4_msg(sb, KERN_ERR,
5208 			"blocksize too small for journal device");
5209 		goto out_bdev;
5210 	}
5211 
5212 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5213 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5214 	set_blocksize(bdev, blocksize);
5215 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5216 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5217 		       "external journal");
5218 		goto out_bdev;
5219 	}
5220 
5221 	es = (struct ext4_super_block *) (bh->b_data + offset);
5222 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5223 	    !(le32_to_cpu(es->s_feature_incompat) &
5224 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5225 		ext4_msg(sb, KERN_ERR, "external journal has "
5226 					"bad superblock");
5227 		brelse(bh);
5228 		goto out_bdev;
5229 	}
5230 
5231 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5232 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5233 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5234 		ext4_msg(sb, KERN_ERR, "external journal has "
5235 				       "corrupt superblock");
5236 		brelse(bh);
5237 		goto out_bdev;
5238 	}
5239 
5240 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5241 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5242 		brelse(bh);
5243 		goto out_bdev;
5244 	}
5245 
5246 	len = ext4_blocks_count(es);
5247 	start = sb_block + 1;
5248 	brelse(bh);	/* we're done with the superblock */
5249 
5250 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5251 					start, len, blocksize);
5252 	if (!journal) {
5253 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5254 		goto out_bdev;
5255 	}
5256 	journal->j_private = sb;
5257 	if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5258 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5259 		goto out_journal;
5260 	}
5261 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5262 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5263 					"user (unsupported) - %d",
5264 			be32_to_cpu(journal->j_superblock->s_nr_users));
5265 		goto out_journal;
5266 	}
5267 	EXT4_SB(sb)->s_journal_bdev = bdev;
5268 	ext4_init_journal_params(sb, journal);
5269 	return journal;
5270 
5271 out_journal:
5272 	jbd2_journal_destroy(journal);
5273 out_bdev:
5274 	ext4_blkdev_put(bdev);
5275 	return NULL;
5276 }
5277 
5278 static int ext4_load_journal(struct super_block *sb,
5279 			     struct ext4_super_block *es,
5280 			     unsigned long journal_devnum)
5281 {
5282 	journal_t *journal;
5283 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5284 	dev_t journal_dev;
5285 	int err = 0;
5286 	int really_read_only;
5287 	int journal_dev_ro;
5288 
5289 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5290 		return -EFSCORRUPTED;
5291 
5292 	if (journal_devnum &&
5293 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5294 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5295 			"numbers have changed");
5296 		journal_dev = new_decode_dev(journal_devnum);
5297 	} else
5298 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5299 
5300 	if (journal_inum && journal_dev) {
5301 		ext4_msg(sb, KERN_ERR,
5302 			 "filesystem has both journal inode and journal device!");
5303 		return -EINVAL;
5304 	}
5305 
5306 	if (journal_inum) {
5307 		journal = ext4_get_journal(sb, journal_inum);
5308 		if (!journal)
5309 			return -EINVAL;
5310 	} else {
5311 		journal = ext4_get_dev_journal(sb, journal_dev);
5312 		if (!journal)
5313 			return -EINVAL;
5314 	}
5315 
5316 	journal_dev_ro = bdev_read_only(journal->j_dev);
5317 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5318 
5319 	if (journal_dev_ro && !sb_rdonly(sb)) {
5320 		ext4_msg(sb, KERN_ERR,
5321 			 "journal device read-only, try mounting with '-o ro'");
5322 		err = -EROFS;
5323 		goto err_out;
5324 	}
5325 
5326 	/*
5327 	 * Are we loading a blank journal or performing recovery after a
5328 	 * crash?  For recovery, we need to check in advance whether we
5329 	 * can get read-write access to the device.
5330 	 */
5331 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5332 		if (sb_rdonly(sb)) {
5333 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5334 					"required on readonly filesystem");
5335 			if (really_read_only) {
5336 				ext4_msg(sb, KERN_ERR, "write access "
5337 					"unavailable, cannot proceed "
5338 					"(try mounting with noload)");
5339 				err = -EROFS;
5340 				goto err_out;
5341 			}
5342 			ext4_msg(sb, KERN_INFO, "write access will "
5343 			       "be enabled during recovery");
5344 		}
5345 	}
5346 
5347 	if (!(journal->j_flags & JBD2_BARRIER))
5348 		ext4_msg(sb, KERN_INFO, "barriers disabled");
5349 
5350 	if (!ext4_has_feature_journal_needs_recovery(sb))
5351 		err = jbd2_journal_wipe(journal, !really_read_only);
5352 	if (!err) {
5353 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
5354 		if (save)
5355 			memcpy(save, ((char *) es) +
5356 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5357 		err = jbd2_journal_load(journal);
5358 		if (save)
5359 			memcpy(((char *) es) + EXT4_S_ERR_START,
5360 			       save, EXT4_S_ERR_LEN);
5361 		kfree(save);
5362 	}
5363 
5364 	if (err) {
5365 		ext4_msg(sb, KERN_ERR, "error loading journal");
5366 		goto err_out;
5367 	}
5368 
5369 	EXT4_SB(sb)->s_journal = journal;
5370 	err = ext4_clear_journal_err(sb, es);
5371 	if (err) {
5372 		EXT4_SB(sb)->s_journal = NULL;
5373 		jbd2_journal_destroy(journal);
5374 		return err;
5375 	}
5376 
5377 	if (!really_read_only && journal_devnum &&
5378 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5379 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5380 
5381 		/* Make sure we flush the recovery flag to disk. */
5382 		ext4_commit_super(sb);
5383 	}
5384 
5385 	return 0;
5386 
5387 err_out:
5388 	jbd2_journal_destroy(journal);
5389 	return err;
5390 }
5391 
5392 /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
5393 static void ext4_update_super(struct super_block *sb)
5394 {
5395 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5396 	struct ext4_super_block *es = sbi->s_es;
5397 	struct buffer_head *sbh = sbi->s_sbh;
5398 
5399 	lock_buffer(sbh);
5400 	/*
5401 	 * If the file system is mounted read-only, don't update the
5402 	 * superblock write time.  This avoids updating the superblock
5403 	 * write time when we are mounting the root file system
5404 	 * read/only but we need to replay the journal; at that point,
5405 	 * for people who are east of GMT and who make their clock
5406 	 * tick in localtime for Windows bug-for-bug compatibility,
5407 	 * the clock is set in the future, and this will cause e2fsck
5408 	 * to complain and force a full file system check.
5409 	 */
5410 	if (!(sb->s_flags & SB_RDONLY))
5411 		ext4_update_tstamp(es, s_wtime);
5412 	es->s_kbytes_written =
5413 		cpu_to_le64(sbi->s_kbytes_written +
5414 		    ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
5415 		      sbi->s_sectors_written_start) >> 1));
5416 	if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
5417 		ext4_free_blocks_count_set(es,
5418 			EXT4_C2B(sbi, percpu_counter_sum_positive(
5419 				&sbi->s_freeclusters_counter)));
5420 	if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
5421 		es->s_free_inodes_count =
5422 			cpu_to_le32(percpu_counter_sum_positive(
5423 				&sbi->s_freeinodes_counter));
5424 	/* Copy error information to the on-disk superblock */
5425 	spin_lock(&sbi->s_error_lock);
5426 	if (sbi->s_add_error_count > 0) {
5427 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5428 		if (!es->s_first_error_time && !es->s_first_error_time_hi) {
5429 			__ext4_update_tstamp(&es->s_first_error_time,
5430 					     &es->s_first_error_time_hi,
5431 					     sbi->s_first_error_time);
5432 			strncpy(es->s_first_error_func, sbi->s_first_error_func,
5433 				sizeof(es->s_first_error_func));
5434 			es->s_first_error_line =
5435 				cpu_to_le32(sbi->s_first_error_line);
5436 			es->s_first_error_ino =
5437 				cpu_to_le32(sbi->s_first_error_ino);
5438 			es->s_first_error_block =
5439 				cpu_to_le64(sbi->s_first_error_block);
5440 			es->s_first_error_errcode =
5441 				ext4_errno_to_code(sbi->s_first_error_code);
5442 		}
5443 		__ext4_update_tstamp(&es->s_last_error_time,
5444 				     &es->s_last_error_time_hi,
5445 				     sbi->s_last_error_time);
5446 		strncpy(es->s_last_error_func, sbi->s_last_error_func,
5447 			sizeof(es->s_last_error_func));
5448 		es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
5449 		es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
5450 		es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
5451 		es->s_last_error_errcode =
5452 				ext4_errno_to_code(sbi->s_last_error_code);
5453 		/*
5454 		 * Start the daily error reporting function if it hasn't been
5455 		 * started already
5456 		 */
5457 		if (!es->s_error_count)
5458 			mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);
5459 		le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
5460 		sbi->s_add_error_count = 0;
5461 	}
5462 	spin_unlock(&sbi->s_error_lock);
5463 
5464 	ext4_superblock_csum_set(sb);
5465 	unlock_buffer(sbh);
5466 }
5467 
5468 static int ext4_commit_super(struct super_block *sb)
5469 {
5470 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5471 	int error = 0;
5472 
5473 	if (!sbh)
5474 		return -EINVAL;
5475 	if (block_device_ejected(sb))
5476 		return -ENODEV;
5477 
5478 	ext4_update_super(sb);
5479 
5480 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
5481 		/*
5482 		 * Oh, dear.  A previous attempt to write the
5483 		 * superblock failed.  This could happen because the
5484 		 * USB device was yanked out.  Or it could happen to
5485 		 * be a transient write error and maybe the block will
5486 		 * be remapped.  Nothing we can do but to retry the
5487 		 * write and hope for the best.
5488 		 */
5489 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
5490 		       "superblock detected");
5491 		clear_buffer_write_io_error(sbh);
5492 		set_buffer_uptodate(sbh);
5493 	}
5494 	BUFFER_TRACE(sbh, "marking dirty");
5495 	mark_buffer_dirty(sbh);
5496 	error = __sync_dirty_buffer(sbh,
5497 		REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5498 	if (buffer_write_io_error(sbh)) {
5499 		ext4_msg(sb, KERN_ERR, "I/O error while writing "
5500 		       "superblock");
5501 		clear_buffer_write_io_error(sbh);
5502 		set_buffer_uptodate(sbh);
5503 	}
5504 	return error;
5505 }
5506 
5507 /*
5508  * Have we just finished recovery?  If so, and if we are mounting (or
5509  * remounting) the filesystem readonly, then we will end up with a
5510  * consistent fs on disk.  Record that fact.
5511  */
5512 static int ext4_mark_recovery_complete(struct super_block *sb,
5513 				       struct ext4_super_block *es)
5514 {
5515 	int err;
5516 	journal_t *journal = EXT4_SB(sb)->s_journal;
5517 
5518 	if (!ext4_has_feature_journal(sb)) {
5519 		if (journal != NULL) {
5520 			ext4_error(sb, "Journal got removed while the fs was "
5521 				   "mounted!");
5522 			return -EFSCORRUPTED;
5523 		}
5524 		return 0;
5525 	}
5526 	jbd2_journal_lock_updates(journal);
5527 	err = jbd2_journal_flush(journal, 0);
5528 	if (err < 0)
5529 		goto out;
5530 
5531 	if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) ||
5532 	    ext4_has_feature_orphan_present(sb))) {
5533 		if (!ext4_orphan_file_empty(sb)) {
5534 			ext4_error(sb, "Orphan file not empty on read-only fs.");
5535 			err = -EFSCORRUPTED;
5536 			goto out;
5537 		}
5538 		ext4_clear_feature_journal_needs_recovery(sb);
5539 		ext4_clear_feature_orphan_present(sb);
5540 		ext4_commit_super(sb);
5541 	}
5542 out:
5543 	jbd2_journal_unlock_updates(journal);
5544 	return err;
5545 }
5546 
5547 /*
5548  * If we are mounting (or read-write remounting) a filesystem whose journal
5549  * has recorded an error from a previous lifetime, move that error to the
5550  * main filesystem now.
5551  */
5552 static int ext4_clear_journal_err(struct super_block *sb,
5553 				   struct ext4_super_block *es)
5554 {
5555 	journal_t *journal;
5556 	int j_errno;
5557 	const char *errstr;
5558 
5559 	if (!ext4_has_feature_journal(sb)) {
5560 		ext4_error(sb, "Journal got removed while the fs was mounted!");
5561 		return -EFSCORRUPTED;
5562 	}
5563 
5564 	journal = EXT4_SB(sb)->s_journal;
5565 
5566 	/*
5567 	 * Now check for any error status which may have been recorded in the
5568 	 * journal by a prior ext4_error() or ext4_abort()
5569 	 */
5570 
5571 	j_errno = jbd2_journal_errno(journal);
5572 	if (j_errno) {
5573 		char nbuf[16];
5574 
5575 		errstr = ext4_decode_error(sb, j_errno, nbuf);
5576 		ext4_warning(sb, "Filesystem error recorded "
5577 			     "from previous mount: %s", errstr);
5578 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5579 
5580 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5581 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5582 		ext4_commit_super(sb);
5583 
5584 		jbd2_journal_clear_err(journal);
5585 		jbd2_journal_update_sb_errno(journal);
5586 	}
5587 	return 0;
5588 }
5589 
5590 /*
5591  * Force the running and committing transactions to commit,
5592  * and wait on the commit.
5593  */
5594 int ext4_force_commit(struct super_block *sb)
5595 {
5596 	journal_t *journal;
5597 
5598 	if (sb_rdonly(sb))
5599 		return 0;
5600 
5601 	journal = EXT4_SB(sb)->s_journal;
5602 	return ext4_journal_force_commit(journal);
5603 }
5604 
5605 static int ext4_sync_fs(struct super_block *sb, int wait)
5606 {
5607 	int ret = 0;
5608 	tid_t target;
5609 	bool needs_barrier = false;
5610 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5611 
5612 	if (unlikely(ext4_forced_shutdown(sbi)))
5613 		return 0;
5614 
5615 	trace_ext4_sync_fs(sb, wait);
5616 	flush_workqueue(sbi->rsv_conversion_wq);
5617 	/*
5618 	 * Writeback quota in non-journalled quota case - journalled quota has
5619 	 * no dirty dquots
5620 	 */
5621 	dquot_writeback_dquots(sb, -1);
5622 	/*
5623 	 * Data writeback is possible w/o journal transaction, so barrier must
5624 	 * being sent at the end of the function. But we can skip it if
5625 	 * transaction_commit will do it for us.
5626 	 */
5627 	if (sbi->s_journal) {
5628 		target = jbd2_get_latest_transaction(sbi->s_journal);
5629 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
5630 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
5631 			needs_barrier = true;
5632 
5633 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5634 			if (wait)
5635 				ret = jbd2_log_wait_commit(sbi->s_journal,
5636 							   target);
5637 		}
5638 	} else if (wait && test_opt(sb, BARRIER))
5639 		needs_barrier = true;
5640 	if (needs_barrier) {
5641 		int err;
5642 		err = blkdev_issue_flush(sb->s_bdev);
5643 		if (!ret)
5644 			ret = err;
5645 	}
5646 
5647 	return ret;
5648 }
5649 
5650 /*
5651  * LVM calls this function before a (read-only) snapshot is created.  This
5652  * gives us a chance to flush the journal completely and mark the fs clean.
5653  *
5654  * Note that only this function cannot bring a filesystem to be in a clean
5655  * state independently. It relies on upper layer to stop all data & metadata
5656  * modifications.
5657  */
5658 static int ext4_freeze(struct super_block *sb)
5659 {
5660 	int error = 0;
5661 	journal_t *journal;
5662 
5663 	if (sb_rdonly(sb))
5664 		return 0;
5665 
5666 	journal = EXT4_SB(sb)->s_journal;
5667 
5668 	if (journal) {
5669 		/* Now we set up the journal barrier. */
5670 		jbd2_journal_lock_updates(journal);
5671 
5672 		/*
5673 		 * Don't clear the needs_recovery flag if we failed to
5674 		 * flush the journal.
5675 		 */
5676 		error = jbd2_journal_flush(journal, 0);
5677 		if (error < 0)
5678 			goto out;
5679 
5680 		/* Journal blocked and flushed, clear needs_recovery flag. */
5681 		ext4_clear_feature_journal_needs_recovery(sb);
5682 		if (ext4_orphan_file_empty(sb))
5683 			ext4_clear_feature_orphan_present(sb);
5684 	}
5685 
5686 	error = ext4_commit_super(sb);
5687 out:
5688 	if (journal)
5689 		/* we rely on upper layer to stop further updates */
5690 		jbd2_journal_unlock_updates(journal);
5691 	return error;
5692 }
5693 
5694 /*
5695  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5696  * flag here, even though the filesystem is not technically dirty yet.
5697  */
5698 static int ext4_unfreeze(struct super_block *sb)
5699 {
5700 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
5701 		return 0;
5702 
5703 	if (EXT4_SB(sb)->s_journal) {
5704 		/* Reset the needs_recovery flag before the fs is unlocked. */
5705 		ext4_set_feature_journal_needs_recovery(sb);
5706 		if (ext4_has_feature_orphan_file(sb))
5707 			ext4_set_feature_orphan_present(sb);
5708 	}
5709 
5710 	ext4_commit_super(sb);
5711 	return 0;
5712 }
5713 
5714 /*
5715  * Structure to save mount options for ext4_remount's benefit
5716  */
5717 struct ext4_mount_options {
5718 	unsigned long s_mount_opt;
5719 	unsigned long s_mount_opt2;
5720 	kuid_t s_resuid;
5721 	kgid_t s_resgid;
5722 	unsigned long s_commit_interval;
5723 	u32 s_min_batch_time, s_max_batch_time;
5724 #ifdef CONFIG_QUOTA
5725 	int s_jquota_fmt;
5726 	char *s_qf_names[EXT4_MAXQUOTAS];
5727 #endif
5728 };
5729 
5730 static int ext4_remount(struct super_block *sb, int *flags, char *data)
5731 {
5732 	struct ext4_super_block *es;
5733 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5734 	unsigned long old_sb_flags, vfs_flags;
5735 	struct ext4_mount_options old_opts;
5736 	ext4_group_t g;
5737 	int err = 0;
5738 #ifdef CONFIG_QUOTA
5739 	int enable_quota = 0;
5740 	int i, j;
5741 	char *to_free[EXT4_MAXQUOTAS];
5742 #endif
5743 	char *orig_data = kstrdup(data, GFP_KERNEL);
5744 	struct ext4_parsed_options parsed_opts;
5745 
5746 	parsed_opts.journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5747 	parsed_opts.journal_devnum = 0;
5748 
5749 	if (data && !orig_data)
5750 		return -ENOMEM;
5751 
5752 	/* Store the original options */
5753 	old_sb_flags = sb->s_flags;
5754 	old_opts.s_mount_opt = sbi->s_mount_opt;
5755 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5756 	old_opts.s_resuid = sbi->s_resuid;
5757 	old_opts.s_resgid = sbi->s_resgid;
5758 	old_opts.s_commit_interval = sbi->s_commit_interval;
5759 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
5760 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5761 #ifdef CONFIG_QUOTA
5762 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5763 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5764 		if (sbi->s_qf_names[i]) {
5765 			char *qf_name = get_qf_name(sb, sbi, i);
5766 
5767 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
5768 			if (!old_opts.s_qf_names[i]) {
5769 				for (j = 0; j < i; j++)
5770 					kfree(old_opts.s_qf_names[j]);
5771 				kfree(orig_data);
5772 				return -ENOMEM;
5773 			}
5774 		} else
5775 			old_opts.s_qf_names[i] = NULL;
5776 #endif
5777 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5778 		parsed_opts.journal_ioprio =
5779 			sbi->s_journal->j_task->io_context->ioprio;
5780 
5781 	/*
5782 	 * Some options can be enabled by ext4 and/or by VFS mount flag
5783 	 * either way we need to make sure it matches in both *flags and
5784 	 * s_flags. Copy those selected flags from *flags to s_flags
5785 	 */
5786 	vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5787 	sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5788 
5789 	if (!parse_options(data, sb, &parsed_opts, 1)) {
5790 		err = -EINVAL;
5791 		goto restore_opts;
5792 	}
5793 
5794 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
5795 	    test_opt(sb, JOURNAL_CHECKSUM)) {
5796 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
5797 			 "during remount not supported; ignoring");
5798 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5799 	}
5800 
5801 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
5802 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
5803 			ext4_msg(sb, KERN_ERR, "can't mount with "
5804 				 "both data=journal and delalloc");
5805 			err = -EINVAL;
5806 			goto restore_opts;
5807 		}
5808 		if (test_opt(sb, DIOREAD_NOLOCK)) {
5809 			ext4_msg(sb, KERN_ERR, "can't mount with "
5810 				 "both data=journal and dioread_nolock");
5811 			err = -EINVAL;
5812 			goto restore_opts;
5813 		}
5814 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5815 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5816 			ext4_msg(sb, KERN_ERR, "can't mount with "
5817 				"journal_async_commit in data=ordered mode");
5818 			err = -EINVAL;
5819 			goto restore_opts;
5820 		}
5821 	}
5822 
5823 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5824 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5825 		err = -EINVAL;
5826 		goto restore_opts;
5827 	}
5828 
5829 	if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
5830 		ext4_abort(sb, ESHUTDOWN, "Abort forced by user");
5831 
5832 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
5833 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5834 
5835 	es = sbi->s_es;
5836 
5837 	if (sbi->s_journal) {
5838 		ext4_init_journal_params(sb, sbi->s_journal);
5839 		set_task_ioprio(sbi->s_journal->j_task, parsed_opts.journal_ioprio);
5840 	}
5841 
5842 	/* Flush outstanding errors before changing fs state */
5843 	flush_work(&sbi->s_error_work);
5844 
5845 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
5846 		if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) {
5847 			err = -EROFS;
5848 			goto restore_opts;
5849 		}
5850 
5851 		if (*flags & SB_RDONLY) {
5852 			err = sync_filesystem(sb);
5853 			if (err < 0)
5854 				goto restore_opts;
5855 			err = dquot_suspend(sb, -1);
5856 			if (err < 0)
5857 				goto restore_opts;
5858 
5859 			/*
5860 			 * First of all, the unconditional stuff we have to do
5861 			 * to disable replay of the journal when we next remount
5862 			 */
5863 			sb->s_flags |= SB_RDONLY;
5864 
5865 			/*
5866 			 * OK, test if we are remounting a valid rw partition
5867 			 * readonly, and if so set the rdonly flag and then
5868 			 * mark the partition as valid again.
5869 			 */
5870 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5871 			    (sbi->s_mount_state & EXT4_VALID_FS))
5872 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5873 
5874 			if (sbi->s_journal) {
5875 				/*
5876 				 * We let remount-ro finish even if marking fs
5877 				 * as clean failed...
5878 				 */
5879 				ext4_mark_recovery_complete(sb, es);
5880 			}
5881 		} else {
5882 			/* Make sure we can mount this feature set readwrite */
5883 			if (ext4_has_feature_readonly(sb) ||
5884 			    !ext4_feature_set_ok(sb, 0)) {
5885 				err = -EROFS;
5886 				goto restore_opts;
5887 			}
5888 			/*
5889 			 * Make sure the group descriptor checksums
5890 			 * are sane.  If they aren't, refuse to remount r/w.
5891 			 */
5892 			for (g = 0; g < sbi->s_groups_count; g++) {
5893 				struct ext4_group_desc *gdp =
5894 					ext4_get_group_desc(sb, g, NULL);
5895 
5896 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5897 					ext4_msg(sb, KERN_ERR,
5898 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5899 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
5900 					       le16_to_cpu(gdp->bg_checksum));
5901 					err = -EFSBADCRC;
5902 					goto restore_opts;
5903 				}
5904 			}
5905 
5906 			/*
5907 			 * If we have an unprocessed orphan list hanging
5908 			 * around from a previously readonly bdev mount,
5909 			 * require a full umount/remount for now.
5910 			 */
5911 			if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) {
5912 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5913 				       "remount RDWR because of unprocessed "
5914 				       "orphan inode list.  Please "
5915 				       "umount/remount instead");
5916 				err = -EINVAL;
5917 				goto restore_opts;
5918 			}
5919 
5920 			/*
5921 			 * Mounting a RDONLY partition read-write, so reread
5922 			 * and store the current valid flag.  (It may have
5923 			 * been changed by e2fsck since we originally mounted
5924 			 * the partition.)
5925 			 */
5926 			if (sbi->s_journal) {
5927 				err = ext4_clear_journal_err(sb, es);
5928 				if (err)
5929 					goto restore_opts;
5930 			}
5931 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5932 
5933 			err = ext4_setup_super(sb, es, 0);
5934 			if (err)
5935 				goto restore_opts;
5936 
5937 			sb->s_flags &= ~SB_RDONLY;
5938 			if (ext4_has_feature_mmp(sb))
5939 				if (ext4_multi_mount_protect(sb,
5940 						le64_to_cpu(es->s_mmp_block))) {
5941 					err = -EROFS;
5942 					goto restore_opts;
5943 				}
5944 #ifdef CONFIG_QUOTA
5945 			enable_quota = 1;
5946 #endif
5947 		}
5948 	}
5949 
5950 	/*
5951 	 * Reinitialize lazy itable initialization thread based on
5952 	 * current settings
5953 	 */
5954 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5955 		ext4_unregister_li_request(sb);
5956 	else {
5957 		ext4_group_t first_not_zeroed;
5958 		first_not_zeroed = ext4_has_uninit_itable(sb);
5959 		ext4_register_li_request(sb, first_not_zeroed);
5960 	}
5961 
5962 	/*
5963 	 * Handle creation of system zone data early because it can fail.
5964 	 * Releasing of existing data is done when we are sure remount will
5965 	 * succeed.
5966 	 */
5967 	if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
5968 		err = ext4_setup_system_zone(sb);
5969 		if (err)
5970 			goto restore_opts;
5971 	}
5972 
5973 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5974 		err = ext4_commit_super(sb);
5975 		if (err)
5976 			goto restore_opts;
5977 	}
5978 
5979 #ifdef CONFIG_QUOTA
5980 	/* Release old quota file names */
5981 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5982 		kfree(old_opts.s_qf_names[i]);
5983 	if (enable_quota) {
5984 		if (sb_any_quota_suspended(sb))
5985 			dquot_resume(sb, -1);
5986 		else if (ext4_has_feature_quota(sb)) {
5987 			err = ext4_enable_quotas(sb);
5988 			if (err)
5989 				goto restore_opts;
5990 		}
5991 	}
5992 #endif
5993 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
5994 		ext4_release_system_zone(sb);
5995 
5996 	if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
5997 		ext4_stop_mmpd(sbi);
5998 
5999 	/*
6000 	 * Some options can be enabled by ext4 and/or by VFS mount flag
6001 	 * either way we need to make sure it matches in both *flags and
6002 	 * s_flags. Copy those selected flags from s_flags to *flags
6003 	 */
6004 	*flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
6005 
6006 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s. Quota mode: %s.",
6007 		 orig_data, ext4_quota_mode(sb));
6008 	kfree(orig_data);
6009 	return 0;
6010 
6011 restore_opts:
6012 	sb->s_flags = old_sb_flags;
6013 	sbi->s_mount_opt = old_opts.s_mount_opt;
6014 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6015 	sbi->s_resuid = old_opts.s_resuid;
6016 	sbi->s_resgid = old_opts.s_resgid;
6017 	sbi->s_commit_interval = old_opts.s_commit_interval;
6018 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
6019 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
6020 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
6021 		ext4_release_system_zone(sb);
6022 #ifdef CONFIG_QUOTA
6023 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6024 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
6025 		to_free[i] = get_qf_name(sb, sbi, i);
6026 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6027 	}
6028 	synchronize_rcu();
6029 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
6030 		kfree(to_free[i]);
6031 #endif
6032 	if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
6033 		ext4_stop_mmpd(sbi);
6034 	kfree(orig_data);
6035 	return err;
6036 }
6037 
6038 #ifdef CONFIG_QUOTA
6039 static int ext4_statfs_project(struct super_block *sb,
6040 			       kprojid_t projid, struct kstatfs *buf)
6041 {
6042 	struct kqid qid;
6043 	struct dquot *dquot;
6044 	u64 limit;
6045 	u64 curblock;
6046 
6047 	qid = make_kqid_projid(projid);
6048 	dquot = dqget(sb, qid);
6049 	if (IS_ERR(dquot))
6050 		return PTR_ERR(dquot);
6051 	spin_lock(&dquot->dq_dqb_lock);
6052 
6053 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6054 			     dquot->dq_dqb.dqb_bhardlimit);
6055 	limit >>= sb->s_blocksize_bits;
6056 
6057 	if (limit && buf->f_blocks > limit) {
6058 		curblock = (dquot->dq_dqb.dqb_curspace +
6059 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6060 		buf->f_blocks = limit;
6061 		buf->f_bfree = buf->f_bavail =
6062 			(buf->f_blocks > curblock) ?
6063 			 (buf->f_blocks - curblock) : 0;
6064 	}
6065 
6066 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6067 			     dquot->dq_dqb.dqb_ihardlimit);
6068 	if (limit && buf->f_files > limit) {
6069 		buf->f_files = limit;
6070 		buf->f_ffree =
6071 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6072 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6073 	}
6074 
6075 	spin_unlock(&dquot->dq_dqb_lock);
6076 	dqput(dquot);
6077 	return 0;
6078 }
6079 #endif
6080 
6081 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6082 {
6083 	struct super_block *sb = dentry->d_sb;
6084 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6085 	struct ext4_super_block *es = sbi->s_es;
6086 	ext4_fsblk_t overhead = 0, resv_blocks;
6087 	s64 bfree;
6088 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6089 
6090 	if (!test_opt(sb, MINIX_DF))
6091 		overhead = sbi->s_overhead;
6092 
6093 	buf->f_type = EXT4_SUPER_MAGIC;
6094 	buf->f_bsize = sb->s_blocksize;
6095 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
6096 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
6097 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6098 	/* prevent underflow in case that few free space is available */
6099 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
6100 	buf->f_bavail = buf->f_bfree -
6101 			(ext4_r_blocks_count(es) + resv_blocks);
6102 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6103 		buf->f_bavail = 0;
6104 	buf->f_files = le32_to_cpu(es->s_inodes_count);
6105 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6106 	buf->f_namelen = EXT4_NAME_LEN;
6107 	buf->f_fsid = uuid_to_fsid(es->s_uuid);
6108 
6109 #ifdef CONFIG_QUOTA
6110 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6111 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
6112 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6113 #endif
6114 	return 0;
6115 }
6116 
6117 
6118 #ifdef CONFIG_QUOTA
6119 
6120 /*
6121  * Helper functions so that transaction is started before we acquire dqio_sem
6122  * to keep correct lock ordering of transaction > dqio_sem
6123  */
6124 static inline struct inode *dquot_to_inode(struct dquot *dquot)
6125 {
6126 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6127 }
6128 
6129 static int ext4_write_dquot(struct dquot *dquot)
6130 {
6131 	int ret, err;
6132 	handle_t *handle;
6133 	struct inode *inode;
6134 
6135 	inode = dquot_to_inode(dquot);
6136 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6137 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6138 	if (IS_ERR(handle))
6139 		return PTR_ERR(handle);
6140 	ret = dquot_commit(dquot);
6141 	err = ext4_journal_stop(handle);
6142 	if (!ret)
6143 		ret = err;
6144 	return ret;
6145 }
6146 
6147 static int ext4_acquire_dquot(struct dquot *dquot)
6148 {
6149 	int ret, err;
6150 	handle_t *handle;
6151 
6152 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6153 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6154 	if (IS_ERR(handle))
6155 		return PTR_ERR(handle);
6156 	ret = dquot_acquire(dquot);
6157 	err = ext4_journal_stop(handle);
6158 	if (!ret)
6159 		ret = err;
6160 	return ret;
6161 }
6162 
6163 static int ext4_release_dquot(struct dquot *dquot)
6164 {
6165 	int ret, err;
6166 	handle_t *handle;
6167 
6168 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6169 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
6170 	if (IS_ERR(handle)) {
6171 		/* Release dquot anyway to avoid endless cycle in dqput() */
6172 		dquot_release(dquot);
6173 		return PTR_ERR(handle);
6174 	}
6175 	ret = dquot_release(dquot);
6176 	err = ext4_journal_stop(handle);
6177 	if (!ret)
6178 		ret = err;
6179 	return ret;
6180 }
6181 
6182 static int ext4_mark_dquot_dirty(struct dquot *dquot)
6183 {
6184 	struct super_block *sb = dquot->dq_sb;
6185 
6186 	if (ext4_is_quota_journalled(sb)) {
6187 		dquot_mark_dquot_dirty(dquot);
6188 		return ext4_write_dquot(dquot);
6189 	} else {
6190 		return dquot_mark_dquot_dirty(dquot);
6191 	}
6192 }
6193 
6194 static int ext4_write_info(struct super_block *sb, int type)
6195 {
6196 	int ret, err;
6197 	handle_t *handle;
6198 
6199 	/* Data block + inode block */
6200 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
6201 	if (IS_ERR(handle))
6202 		return PTR_ERR(handle);
6203 	ret = dquot_commit_info(sb, type);
6204 	err = ext4_journal_stop(handle);
6205 	if (!ret)
6206 		ret = err;
6207 	return ret;
6208 }
6209 
6210 static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6211 {
6212 	struct ext4_inode_info *ei = EXT4_I(inode);
6213 
6214 	/* The first argument of lockdep_set_subclass has to be
6215 	 * *exactly* the same as the argument to init_rwsem() --- in
6216 	 * this case, in init_once() --- or lockdep gets unhappy
6217 	 * because the name of the lock is set using the
6218 	 * stringification of the argument to init_rwsem().
6219 	 */
6220 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
6221 	lockdep_set_subclass(&ei->i_data_sem, subclass);
6222 }
6223 
6224 /*
6225  * Standard function to be called on quota_on
6226  */
6227 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
6228 			 const struct path *path)
6229 {
6230 	int err;
6231 
6232 	if (!test_opt(sb, QUOTA))
6233 		return -EINVAL;
6234 
6235 	/* Quotafile not on the same filesystem? */
6236 	if (path->dentry->d_sb != sb)
6237 		return -EXDEV;
6238 
6239 	/* Quota already enabled for this file? */
6240 	if (IS_NOQUOTA(d_inode(path->dentry)))
6241 		return -EBUSY;
6242 
6243 	/* Journaling quota? */
6244 	if (EXT4_SB(sb)->s_qf_names[type]) {
6245 		/* Quotafile not in fs root? */
6246 		if (path->dentry->d_parent != sb->s_root)
6247 			ext4_msg(sb, KERN_WARNING,
6248 				"Quota file not on filesystem root. "
6249 				"Journaled quota will not work");
6250 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
6251 	} else {
6252 		/*
6253 		 * Clear the flag just in case mount options changed since
6254 		 * last time.
6255 		 */
6256 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
6257 	}
6258 
6259 	/*
6260 	 * When we journal data on quota file, we have to flush journal to see
6261 	 * all updates to the file when we bypass pagecache...
6262 	 */
6263 	if (EXT4_SB(sb)->s_journal &&
6264 	    ext4_should_journal_data(d_inode(path->dentry))) {
6265 		/*
6266 		 * We don't need to lock updates but journal_flush() could
6267 		 * otherwise be livelocked...
6268 		 */
6269 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
6270 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
6271 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6272 		if (err)
6273 			return err;
6274 	}
6275 
6276 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6277 	err = dquot_quota_on(sb, type, format_id, path);
6278 	if (err) {
6279 		lockdep_set_quota_inode(path->dentry->d_inode,
6280 					     I_DATA_SEM_NORMAL);
6281 	} else {
6282 		struct inode *inode = d_inode(path->dentry);
6283 		handle_t *handle;
6284 
6285 		/*
6286 		 * Set inode flags to prevent userspace from messing with quota
6287 		 * files. If this fails, we return success anyway since quotas
6288 		 * are already enabled and this is not a hard failure.
6289 		 */
6290 		inode_lock(inode);
6291 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6292 		if (IS_ERR(handle))
6293 			goto unlock_inode;
6294 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6295 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6296 				S_NOATIME | S_IMMUTABLE);
6297 		err = ext4_mark_inode_dirty(handle, inode);
6298 		ext4_journal_stop(handle);
6299 	unlock_inode:
6300 		inode_unlock(inode);
6301 	}
6302 	return err;
6303 }
6304 
6305 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
6306 			     unsigned int flags)
6307 {
6308 	int err;
6309 	struct inode *qf_inode;
6310 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
6311 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6312 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6313 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
6314 	};
6315 
6316 	BUG_ON(!ext4_has_feature_quota(sb));
6317 
6318 	if (!qf_inums[type])
6319 		return -EPERM;
6320 
6321 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
6322 	if (IS_ERR(qf_inode)) {
6323 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
6324 		return PTR_ERR(qf_inode);
6325 	}
6326 
6327 	/* Don't account quota for quota files to avoid recursion */
6328 	qf_inode->i_flags |= S_NOQUOTA;
6329 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
6330 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6331 	if (err)
6332 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
6333 	iput(qf_inode);
6334 
6335 	return err;
6336 }
6337 
6338 /* Enable usage tracking for all quota types. */
6339 int ext4_enable_quotas(struct super_block *sb)
6340 {
6341 	int type, err = 0;
6342 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
6343 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6344 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6345 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
6346 	};
6347 	bool quota_mopt[EXT4_MAXQUOTAS] = {
6348 		test_opt(sb, USRQUOTA),
6349 		test_opt(sb, GRPQUOTA),
6350 		test_opt(sb, PRJQUOTA),
6351 	};
6352 
6353 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6354 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
6355 		if (qf_inums[type]) {
6356 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
6357 				DQUOT_USAGE_ENABLED |
6358 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
6359 			if (err) {
6360 				ext4_warning(sb,
6361 					"Failed to enable quota tracking "
6362 					"(type=%d, err=%d). Please run "
6363 					"e2fsck to fix.", type, err);
6364 				for (type--; type >= 0; type--)
6365 					dquot_quota_off(sb, type);
6366 
6367 				return err;
6368 			}
6369 		}
6370 	}
6371 	return 0;
6372 }
6373 
6374 static int ext4_quota_off(struct super_block *sb, int type)
6375 {
6376 	struct inode *inode = sb_dqopt(sb)->files[type];
6377 	handle_t *handle;
6378 	int err;
6379 
6380 	/* Force all delayed allocation blocks to be allocated.
6381 	 * Caller already holds s_umount sem */
6382 	if (test_opt(sb, DELALLOC))
6383 		sync_filesystem(sb);
6384 
6385 	if (!inode || !igrab(inode))
6386 		goto out;
6387 
6388 	err = dquot_quota_off(sb, type);
6389 	if (err || ext4_has_feature_quota(sb))
6390 		goto out_put;
6391 
6392 	inode_lock(inode);
6393 	/*
6394 	 * Update modification times of quota files when userspace can
6395 	 * start looking at them. If we fail, we return success anyway since
6396 	 * this is not a hard failure and quotas are already disabled.
6397 	 */
6398 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6399 	if (IS_ERR(handle)) {
6400 		err = PTR_ERR(handle);
6401 		goto out_unlock;
6402 	}
6403 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6404 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6405 	inode->i_mtime = inode->i_ctime = current_time(inode);
6406 	err = ext4_mark_inode_dirty(handle, inode);
6407 	ext4_journal_stop(handle);
6408 out_unlock:
6409 	inode_unlock(inode);
6410 out_put:
6411 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6412 	iput(inode);
6413 	return err;
6414 out:
6415 	return dquot_quota_off(sb, type);
6416 }
6417 
6418 /* Read data from quotafile - avoid pagecache and such because we cannot afford
6419  * acquiring the locks... As quota files are never truncated and quota code
6420  * itself serializes the operations (and no one else should touch the files)
6421  * we don't have to be afraid of races */
6422 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6423 			       size_t len, loff_t off)
6424 {
6425 	struct inode *inode = sb_dqopt(sb)->files[type];
6426 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6427 	int offset = off & (sb->s_blocksize - 1);
6428 	int tocopy;
6429 	size_t toread;
6430 	struct buffer_head *bh;
6431 	loff_t i_size = i_size_read(inode);
6432 
6433 	if (off > i_size)
6434 		return 0;
6435 	if (off+len > i_size)
6436 		len = i_size-off;
6437 	toread = len;
6438 	while (toread > 0) {
6439 		tocopy = sb->s_blocksize - offset < toread ?
6440 				sb->s_blocksize - offset : toread;
6441 		bh = ext4_bread(NULL, inode, blk, 0);
6442 		if (IS_ERR(bh))
6443 			return PTR_ERR(bh);
6444 		if (!bh)	/* A hole? */
6445 			memset(data, 0, tocopy);
6446 		else
6447 			memcpy(data, bh->b_data+offset, tocopy);
6448 		brelse(bh);
6449 		offset = 0;
6450 		toread -= tocopy;
6451 		data += tocopy;
6452 		blk++;
6453 	}
6454 	return len;
6455 }
6456 
6457 /* Write to quotafile (we know the transaction is already started and has
6458  * enough credits) */
6459 static ssize_t ext4_quota_write(struct super_block *sb, int type,
6460 				const char *data, size_t len, loff_t off)
6461 {
6462 	struct inode *inode = sb_dqopt(sb)->files[type];
6463 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6464 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6465 	int retries = 0;
6466 	struct buffer_head *bh;
6467 	handle_t *handle = journal_current_handle();
6468 
6469 	if (EXT4_SB(sb)->s_journal && !handle) {
6470 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6471 			" cancelled because transaction is not started",
6472 			(unsigned long long)off, (unsigned long long)len);
6473 		return -EIO;
6474 	}
6475 	/*
6476 	 * Since we account only one data block in transaction credits,
6477 	 * then it is impossible to cross a block boundary.
6478 	 */
6479 	if (sb->s_blocksize - offset < len) {
6480 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6481 			" cancelled because not block aligned",
6482 			(unsigned long long)off, (unsigned long long)len);
6483 		return -EIO;
6484 	}
6485 
6486 	do {
6487 		bh = ext4_bread(handle, inode, blk,
6488 				EXT4_GET_BLOCKS_CREATE |
6489 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
6490 	} while (PTR_ERR(bh) == -ENOSPC &&
6491 		 ext4_should_retry_alloc(inode->i_sb, &retries));
6492 	if (IS_ERR(bh))
6493 		return PTR_ERR(bh);
6494 	if (!bh)
6495 		goto out;
6496 	BUFFER_TRACE(bh, "get write access");
6497 	err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
6498 	if (err) {
6499 		brelse(bh);
6500 		return err;
6501 	}
6502 	lock_buffer(bh);
6503 	memcpy(bh->b_data+offset, data, len);
6504 	flush_dcache_page(bh->b_page);
6505 	unlock_buffer(bh);
6506 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6507 	brelse(bh);
6508 out:
6509 	if (inode->i_size < off + len) {
6510 		i_size_write(inode, off + len);
6511 		EXT4_I(inode)->i_disksize = inode->i_size;
6512 		err2 = ext4_mark_inode_dirty(handle, inode);
6513 		if (unlikely(err2 && !err))
6514 			err = err2;
6515 	}
6516 	return err ? err : len;
6517 }
6518 #endif
6519 
6520 static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6521 		       const char *dev_name, void *data)
6522 {
6523 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6524 }
6525 
6526 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
6527 static inline void register_as_ext2(void)
6528 {
6529 	int err = register_filesystem(&ext2_fs_type);
6530 	if (err)
6531 		printk(KERN_WARNING
6532 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
6533 }
6534 
6535 static inline void unregister_as_ext2(void)
6536 {
6537 	unregister_filesystem(&ext2_fs_type);
6538 }
6539 
6540 static inline int ext2_feature_set_ok(struct super_block *sb)
6541 {
6542 	if (ext4_has_unknown_ext2_incompat_features(sb))
6543 		return 0;
6544 	if (sb_rdonly(sb))
6545 		return 1;
6546 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
6547 		return 0;
6548 	return 1;
6549 }
6550 #else
6551 static inline void register_as_ext2(void) { }
6552 static inline void unregister_as_ext2(void) { }
6553 static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
6554 #endif
6555 
6556 static inline void register_as_ext3(void)
6557 {
6558 	int err = register_filesystem(&ext3_fs_type);
6559 	if (err)
6560 		printk(KERN_WARNING
6561 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
6562 }
6563 
6564 static inline void unregister_as_ext3(void)
6565 {
6566 	unregister_filesystem(&ext3_fs_type);
6567 }
6568 
6569 static inline int ext3_feature_set_ok(struct super_block *sb)
6570 {
6571 	if (ext4_has_unknown_ext3_incompat_features(sb))
6572 		return 0;
6573 	if (!ext4_has_feature_journal(sb))
6574 		return 0;
6575 	if (sb_rdonly(sb))
6576 		return 1;
6577 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
6578 		return 0;
6579 	return 1;
6580 }
6581 
6582 static struct file_system_type ext4_fs_type = {
6583 	.owner		= THIS_MODULE,
6584 	.name		= "ext4",
6585 	.mount		= ext4_mount,
6586 	.kill_sb	= kill_block_super,
6587 	.fs_flags	= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
6588 };
6589 MODULE_ALIAS_FS("ext4");
6590 
6591 /* Shared across all ext4 file systems */
6592 wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6593 
6594 static int __init ext4_init_fs(void)
6595 {
6596 	int i, err;
6597 
6598 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
6599 	ext4_li_info = NULL;
6600 
6601 	/* Build-time check for flags consistency */
6602 	ext4_check_flag_values();
6603 
6604 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6605 		init_waitqueue_head(&ext4__ioend_wq[i]);
6606 
6607 	err = ext4_init_es();
6608 	if (err)
6609 		return err;
6610 
6611 	err = ext4_init_pending();
6612 	if (err)
6613 		goto out7;
6614 
6615 	err = ext4_init_post_read_processing();
6616 	if (err)
6617 		goto out6;
6618 
6619 	err = ext4_init_pageio();
6620 	if (err)
6621 		goto out5;
6622 
6623 	err = ext4_init_system_zone();
6624 	if (err)
6625 		goto out4;
6626 
6627 	err = ext4_init_sysfs();
6628 	if (err)
6629 		goto out3;
6630 
6631 	err = ext4_init_mballoc();
6632 	if (err)
6633 		goto out2;
6634 	err = init_inodecache();
6635 	if (err)
6636 		goto out1;
6637 
6638 	err = ext4_fc_init_dentry_cache();
6639 	if (err)
6640 		goto out05;
6641 
6642 	register_as_ext3();
6643 	register_as_ext2();
6644 	err = register_filesystem(&ext4_fs_type);
6645 	if (err)
6646 		goto out;
6647 
6648 	return 0;
6649 out:
6650 	unregister_as_ext2();
6651 	unregister_as_ext3();
6652 out05:
6653 	destroy_inodecache();
6654 out1:
6655 	ext4_exit_mballoc();
6656 out2:
6657 	ext4_exit_sysfs();
6658 out3:
6659 	ext4_exit_system_zone();
6660 out4:
6661 	ext4_exit_pageio();
6662 out5:
6663 	ext4_exit_post_read_processing();
6664 out6:
6665 	ext4_exit_pending();
6666 out7:
6667 	ext4_exit_es();
6668 
6669 	return err;
6670 }
6671 
6672 static void __exit ext4_exit_fs(void)
6673 {
6674 	ext4_destroy_lazyinit_thread();
6675 	unregister_as_ext2();
6676 	unregister_as_ext3();
6677 	unregister_filesystem(&ext4_fs_type);
6678 	destroy_inodecache();
6679 	ext4_exit_mballoc();
6680 	ext4_exit_sysfs();
6681 	ext4_exit_system_zone();
6682 	ext4_exit_pageio();
6683 	ext4_exit_post_read_processing();
6684 	ext4_exit_es();
6685 	ext4_exit_pending();
6686 }
6687 
6688 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
6689 MODULE_DESCRIPTION("Fourth Extended Filesystem");
6690 MODULE_LICENSE("GPL");
6691 MODULE_SOFTDEP("pre: crc32c");
6692 module_init(ext4_init_fs)
6693 module_exit(ext4_exit_fs)
6694