xref: /linux/fs/f2fs/super.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/super.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/fs.h>
11 #include <linux/fs_context.h>
12 #include <linux/sched/mm.h>
13 #include <linux/statfs.h>
14 #include <linux/kthread.h>
15 #include <linux/parser.h>
16 #include <linux/mount.h>
17 #include <linux/seq_file.h>
18 #include <linux/proc_fs.h>
19 #include <linux/random.h>
20 #include <linux/exportfs.h>
21 #include <linux/blkdev.h>
22 #include <linux/quotaops.h>
23 #include <linux/f2fs_fs.h>
24 #include <linux/sysfs.h>
25 #include <linux/quota.h>
26 #include <linux/unicode.h>
27 #include <linux/part_stat.h>
28 #include <linux/zstd.h>
29 #include <linux/lz4.h>
30 #include <linux/ctype.h>
31 #include <linux/fs_parser.h>
32 #include <linux/fserror.h>
33 
34 #include "f2fs.h"
35 #include "node.h"
36 #include "segment.h"
37 #include "xattr.h"
38 #include "gc.h"
39 #include "iostat.h"
40 
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/f2fs.h>
43 
44 static struct kmem_cache *f2fs_inode_cachep;
45 
46 #ifdef CONFIG_F2FS_FAULT_INJECTION
47 
48 const char *f2fs_fault_name[FAULT_MAX] = {
49 	[FAULT_KMALLOC]			= "kmalloc",
50 	[FAULT_KVMALLOC]		= "kvmalloc",
51 	[FAULT_PAGE_ALLOC]		= "page alloc",
52 	[FAULT_PAGE_GET]		= "page get",
53 	[FAULT_ALLOC_BIO]		= "alloc bio(obsolete)",
54 	[FAULT_ALLOC_NID]		= "alloc nid",
55 	[FAULT_ORPHAN]			= "orphan",
56 	[FAULT_BLOCK]			= "no more block",
57 	[FAULT_DIR_DEPTH]		= "too big dir depth",
58 	[FAULT_EVICT_INODE]		= "evict_inode fail",
59 	[FAULT_TRUNCATE]		= "truncate fail",
60 	[FAULT_READ_IO]			= "read IO error",
61 	[FAULT_CHECKPOINT]		= "checkpoint error",
62 	[FAULT_DISCARD]			= "discard error",
63 	[FAULT_WRITE_IO]		= "write IO error",
64 	[FAULT_SLAB_ALLOC]		= "slab alloc",
65 	[FAULT_DQUOT_INIT]		= "dquot initialize",
66 	[FAULT_LOCK_OP]			= "lock_op",
67 	[FAULT_BLKADDR_VALIDITY]	= "invalid blkaddr",
68 	[FAULT_BLKADDR_CONSISTENCE]	= "inconsistent blkaddr",
69 	[FAULT_NO_SEGMENT]		= "no free segment",
70 	[FAULT_INCONSISTENT_FOOTER]	= "inconsistent footer",
71 	[FAULT_ATOMIC_TIMEOUT]		= "atomic timeout",
72 	[FAULT_VMALLOC]			= "vmalloc",
73 	[FAULT_LOCK_TIMEOUT]		= "lock timeout",
74 	[FAULT_SKIP_WRITE]		= "skip write",
75 };
76 
77 int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
78 				unsigned long type, enum fault_option fo)
79 {
80 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
81 
82 	if (fo & FAULT_ALL) {
83 		memset(ffi, 0, sizeof(struct f2fs_fault_info));
84 		return 0;
85 	}
86 
87 	if (fo & FAULT_RATE) {
88 		if (rate > INT_MAX)
89 			return -EINVAL;
90 		atomic_set(&ffi->inject_ops, 0);
91 		ffi->inject_rate = (int)rate;
92 		f2fs_info(sbi, "build fault injection rate: %lu", rate);
93 	}
94 
95 	if (fo & FAULT_TYPE) {
96 		if (type >= BIT(FAULT_MAX))
97 			return -EINVAL;
98 		ffi->inject_type = (unsigned int)type;
99 		f2fs_info(sbi, "build fault injection type: 0x%lx", type);
100 	}
101 
102 	if (fo & FAULT_TIMEOUT) {
103 		if (type >= TIMEOUT_TYPE_MAX)
104 			return -EINVAL;
105 		ffi->inject_lock_timeout = (unsigned int)type;
106 		f2fs_info(sbi, "build fault timeout injection type: 0x%lx", type);
107 	}
108 
109 	return 0;
110 }
111 
112 static void inject_timeout(struct f2fs_sb_info *sbi)
113 {
114 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
115 	enum f2fs_timeout_type type = ffi->inject_lock_timeout;
116 	unsigned long start_time = jiffies;
117 	unsigned long timeout = HZ;
118 
119 	switch (type) {
120 	case TIMEOUT_TYPE_RUNNING:
121 		while (!time_after(jiffies, start_time + timeout)) {
122 			if (fatal_signal_pending(current))
123 				return;
124 			;
125 		}
126 		break;
127 	case TIMEOUT_TYPE_IO_SLEEP:
128 		f2fs_schedule_timeout_killable(timeout, true);
129 		break;
130 	case TIMEOUT_TYPE_NONIO_SLEEP:
131 		f2fs_schedule_timeout_killable(timeout, false);
132 		break;
133 	case TIMEOUT_TYPE_RUNNABLE:
134 		while (!time_after(jiffies, start_time + timeout)) {
135 			if (fatal_signal_pending(current))
136 				return;
137 			schedule();
138 		}
139 		break;
140 	default:
141 		return;
142 	}
143 }
144 
145 void f2fs_simulate_lock_timeout(struct f2fs_sb_info *sbi)
146 {
147 	struct f2fs_lock_context lc;
148 
149 	f2fs_lock_op(sbi, &lc);
150 	inject_timeout(sbi);
151 	f2fs_unlock_op(sbi, &lc);
152 }
153 #endif
154 
155 /* f2fs-wide shrinker description */
156 static struct shrinker *f2fs_shrinker_info;
157 
158 static int __init f2fs_init_shrinker(void)
159 {
160 	f2fs_shrinker_info = shrinker_alloc(0, "f2fs-shrinker");
161 	if (!f2fs_shrinker_info)
162 		return -ENOMEM;
163 
164 	f2fs_shrinker_info->count_objects = f2fs_shrink_count;
165 	f2fs_shrinker_info->scan_objects = f2fs_shrink_scan;
166 
167 	shrinker_register(f2fs_shrinker_info);
168 
169 	return 0;
170 }
171 
172 static void f2fs_exit_shrinker(void)
173 {
174 	shrinker_free(f2fs_shrinker_info);
175 }
176 
177 enum {
178 	Opt_gc_background,
179 	Opt_disable_roll_forward,
180 	Opt_norecovery,
181 	Opt_discard,
182 	Opt_noheap,
183 	Opt_heap,
184 	Opt_user_xattr,
185 	Opt_acl,
186 	Opt_active_logs,
187 	Opt_disable_ext_identify,
188 	Opt_inline_xattr,
189 	Opt_inline_xattr_size,
190 	Opt_inline_data,
191 	Opt_inline_dentry,
192 	Opt_flush_merge,
193 	Opt_barrier,
194 	Opt_fastboot,
195 	Opt_extent_cache,
196 	Opt_data_flush,
197 	Opt_reserve_root,
198 	Opt_reserve_node,
199 	Opt_resgid,
200 	Opt_resuid,
201 	Opt_mode,
202 	Opt_fault_injection,
203 	Opt_fault_type,
204 	Opt_lazytime,
205 	Opt_quota,
206 	Opt_usrquota,
207 	Opt_grpquota,
208 	Opt_prjquota,
209 	Opt_usrjquota,
210 	Opt_grpjquota,
211 	Opt_prjjquota,
212 	Opt_alloc,
213 	Opt_fsync,
214 	Opt_test_dummy_encryption,
215 	Opt_inlinecrypt,
216 	Opt_checkpoint_disable,
217 	Opt_checkpoint_disable_cap,
218 	Opt_checkpoint_disable_cap_perc,
219 	Opt_checkpoint_enable,
220 	Opt_checkpoint_merge,
221 	Opt_compress_algorithm,
222 	Opt_compress_log_size,
223 	Opt_nocompress_extension,
224 	Opt_compress_extension,
225 	Opt_compress_chksum,
226 	Opt_compress_mode,
227 	Opt_compress_cache,
228 	Opt_atgc,
229 	Opt_gc_merge,
230 	Opt_discard_unit,
231 	Opt_memory_mode,
232 	Opt_age_extent_cache,
233 	Opt_errors,
234 	Opt_nat_bits,
235 	Opt_jqfmt,
236 	Opt_checkpoint,
237 	Opt_lookup_mode,
238 	Opt_err,
239 };
240 
241 static const struct constant_table f2fs_param_background_gc[] = {
242 	{"on",		BGGC_MODE_ON},
243 	{"off",		BGGC_MODE_OFF},
244 	{"sync",	BGGC_MODE_SYNC},
245 	{}
246 };
247 
248 static const struct constant_table f2fs_param_mode[] = {
249 	{"adaptive",		FS_MODE_ADAPTIVE},
250 	{"lfs",			FS_MODE_LFS},
251 	{"fragment:segment",	FS_MODE_FRAGMENT_SEG},
252 	{"fragment:block",	FS_MODE_FRAGMENT_BLK},
253 	{}
254 };
255 
256 static const struct constant_table f2fs_param_jqfmt[] = {
257 	{"vfsold",	QFMT_VFS_OLD},
258 	{"vfsv0",	QFMT_VFS_V0},
259 	{"vfsv1",	QFMT_VFS_V1},
260 	{}
261 };
262 
263 static const struct constant_table f2fs_param_alloc_mode[] = {
264 	{"default",	ALLOC_MODE_DEFAULT},
265 	{"reuse",	ALLOC_MODE_REUSE},
266 	{}
267 };
268 static const struct constant_table f2fs_param_fsync_mode[] = {
269 	{"posix",	FSYNC_MODE_POSIX},
270 	{"strict",	FSYNC_MODE_STRICT},
271 	{"nobarrier",	FSYNC_MODE_NOBARRIER},
272 	{}
273 };
274 
275 static const struct constant_table f2fs_param_compress_mode[] = {
276 	{"fs",		COMPR_MODE_FS},
277 	{"user",	COMPR_MODE_USER},
278 	{}
279 };
280 
281 static const struct constant_table f2fs_param_discard_unit[] = {
282 	{"block",	DISCARD_UNIT_BLOCK},
283 	{"segment",	DISCARD_UNIT_SEGMENT},
284 	{"section",	DISCARD_UNIT_SECTION},
285 	{}
286 };
287 
288 static const struct constant_table f2fs_param_memory_mode[] = {
289 	{"normal",	MEMORY_MODE_NORMAL},
290 	{"low",		MEMORY_MODE_LOW},
291 	{}
292 };
293 
294 static const struct constant_table f2fs_param_errors[] = {
295 	{"remount-ro",	MOUNT_ERRORS_READONLY},
296 	{"continue",	MOUNT_ERRORS_CONTINUE},
297 	{"panic",	MOUNT_ERRORS_PANIC},
298 	{}
299 };
300 
301 static const struct constant_table f2fs_param_lookup_mode[] = {
302 	{"perf",	LOOKUP_PERF},
303 	{"compat",	LOOKUP_COMPAT},
304 	{"auto",	LOOKUP_AUTO},
305 	{}
306 };
307 
308 static const struct fs_parameter_spec f2fs_param_specs[] = {
309 	fsparam_enum("background_gc", Opt_gc_background, f2fs_param_background_gc),
310 	fsparam_flag("disable_roll_forward", Opt_disable_roll_forward),
311 	fsparam_flag("norecovery", Opt_norecovery),
312 	fsparam_flag_no("discard", Opt_discard),
313 	fsparam_flag("no_heap", Opt_noheap),
314 	fsparam_flag("heap", Opt_heap),
315 	fsparam_flag_no("user_xattr", Opt_user_xattr),
316 	fsparam_flag_no("acl", Opt_acl),
317 	fsparam_s32("active_logs", Opt_active_logs),
318 	fsparam_flag("disable_ext_identify", Opt_disable_ext_identify),
319 	fsparam_flag_no("inline_xattr", Opt_inline_xattr),
320 	fsparam_s32("inline_xattr_size", Opt_inline_xattr_size),
321 	fsparam_flag_no("inline_data", Opt_inline_data),
322 	fsparam_flag_no("inline_dentry", Opt_inline_dentry),
323 	fsparam_flag_no("flush_merge", Opt_flush_merge),
324 	fsparam_flag_no("barrier", Opt_barrier),
325 	fsparam_flag("fastboot", Opt_fastboot),
326 	fsparam_flag_no("extent_cache", Opt_extent_cache),
327 	fsparam_flag("data_flush", Opt_data_flush),
328 	fsparam_u32("reserve_root", Opt_reserve_root),
329 	fsparam_u32("reserve_node", Opt_reserve_node),
330 	fsparam_gid("resgid", Opt_resgid),
331 	fsparam_uid("resuid", Opt_resuid),
332 	fsparam_enum("mode", Opt_mode, f2fs_param_mode),
333 	fsparam_s32("fault_injection", Opt_fault_injection),
334 	fsparam_u32("fault_type", Opt_fault_type),
335 	fsparam_flag_no("lazytime", Opt_lazytime),
336 	fsparam_flag_no("quota", Opt_quota),
337 	fsparam_flag("usrquota", Opt_usrquota),
338 	fsparam_flag("grpquota", Opt_grpquota),
339 	fsparam_flag("prjquota", Opt_prjquota),
340 	fsparam_string("usrjquota", Opt_usrjquota),
341 	fsparam_flag("usrjquota", Opt_usrjquota),
342 	fsparam_string("grpjquota", Opt_grpjquota),
343 	fsparam_flag("grpjquota", Opt_grpjquota),
344 	fsparam_string("prjjquota", Opt_prjjquota),
345 	fsparam_flag("prjjquota", Opt_prjjquota),
346 	fsparam_flag("nat_bits", Opt_nat_bits),
347 	fsparam_enum("jqfmt", Opt_jqfmt, f2fs_param_jqfmt),
348 	fsparam_enum("alloc_mode", Opt_alloc, f2fs_param_alloc_mode),
349 	fsparam_enum("fsync_mode", Opt_fsync, f2fs_param_fsync_mode),
350 	fsparam_string("test_dummy_encryption", Opt_test_dummy_encryption),
351 	fsparam_flag("test_dummy_encryption", Opt_test_dummy_encryption),
352 	fsparam_flag("inlinecrypt", Opt_inlinecrypt),
353 	fsparam_string("checkpoint", Opt_checkpoint),
354 	fsparam_flag_no("checkpoint_merge", Opt_checkpoint_merge),
355 	fsparam_string("compress_algorithm", Opt_compress_algorithm),
356 	fsparam_u32("compress_log_size", Opt_compress_log_size),
357 	fsparam_string("compress_extension", Opt_compress_extension),
358 	fsparam_string("nocompress_extension", Opt_nocompress_extension),
359 	fsparam_flag("compress_chksum", Opt_compress_chksum),
360 	fsparam_enum("compress_mode", Opt_compress_mode, f2fs_param_compress_mode),
361 	fsparam_flag("compress_cache", Opt_compress_cache),
362 	fsparam_flag("atgc", Opt_atgc),
363 	fsparam_flag_no("gc_merge", Opt_gc_merge),
364 	fsparam_enum("discard_unit", Opt_discard_unit, f2fs_param_discard_unit),
365 	fsparam_enum("memory", Opt_memory_mode, f2fs_param_memory_mode),
366 	fsparam_flag("age_extent_cache", Opt_age_extent_cache),
367 	fsparam_enum("errors", Opt_errors, f2fs_param_errors),
368 	fsparam_enum("lookup_mode", Opt_lookup_mode, f2fs_param_lookup_mode),
369 	{}
370 };
371 
372 /* Resort to a match_table for this interestingly formatted option */
373 static match_table_t f2fs_checkpoint_tokens = {
374 	{Opt_checkpoint_disable, "disable"},
375 	{Opt_checkpoint_disable_cap, "disable:%u"},
376 	{Opt_checkpoint_disable_cap_perc, "disable:%u%%"},
377 	{Opt_checkpoint_enable, "enable"},
378 	{Opt_err, NULL},
379 };
380 
381 #define F2FS_SPEC_background_gc			(1 << 0)
382 #define F2FS_SPEC_inline_xattr_size		(1 << 1)
383 #define F2FS_SPEC_active_logs			(1 << 2)
384 #define F2FS_SPEC_reserve_root			(1 << 3)
385 #define F2FS_SPEC_resgid			(1 << 4)
386 #define F2FS_SPEC_resuid			(1 << 5)
387 #define F2FS_SPEC_mode				(1 << 6)
388 #define F2FS_SPEC_fault_injection		(1 << 7)
389 #define F2FS_SPEC_fault_type			(1 << 8)
390 #define F2FS_SPEC_jqfmt				(1 << 9)
391 #define F2FS_SPEC_alloc_mode			(1 << 10)
392 #define F2FS_SPEC_fsync_mode			(1 << 11)
393 #define F2FS_SPEC_checkpoint_disable_cap	(1 << 12)
394 #define F2FS_SPEC_checkpoint_disable_cap_perc	(1 << 13)
395 #define F2FS_SPEC_compress_level		(1 << 14)
396 #define F2FS_SPEC_compress_algorithm		(1 << 15)
397 #define F2FS_SPEC_compress_log_size		(1 << 16)
398 #define F2FS_SPEC_compress_extension		(1 << 17)
399 #define F2FS_SPEC_nocompress_extension		(1 << 18)
400 #define F2FS_SPEC_compress_chksum		(1 << 19)
401 #define F2FS_SPEC_compress_mode			(1 << 20)
402 #define F2FS_SPEC_discard_unit			(1 << 21)
403 #define F2FS_SPEC_memory_mode			(1 << 22)
404 #define F2FS_SPEC_errors			(1 << 23)
405 #define F2FS_SPEC_lookup_mode			(1 << 24)
406 #define F2FS_SPEC_reserve_node			(1 << 25)
407 
408 struct f2fs_fs_context {
409 	struct f2fs_mount_info info;
410 	unsigned long long opt_mask;	/* Bits changed */
411 	unsigned int	spec_mask;
412 	unsigned short	qname_mask;
413 };
414 
415 #define F2FS_CTX_INFO(ctx)	((ctx)->info)
416 
417 static inline void ctx_set_opt(struct f2fs_fs_context *ctx,
418 			       enum f2fs_mount_opt flag)
419 {
420 	ctx->info.opt |= BIT(flag);
421 	ctx->opt_mask |= BIT(flag);
422 }
423 
424 static inline void ctx_clear_opt(struct f2fs_fs_context *ctx,
425 				 enum f2fs_mount_opt flag)
426 {
427 	ctx->info.opt &= ~BIT(flag);
428 	ctx->opt_mask |= BIT(flag);
429 }
430 
431 static inline bool ctx_test_opt(struct f2fs_fs_context *ctx,
432 				enum f2fs_mount_opt flag)
433 {
434 	return ctx->info.opt & BIT(flag);
435 }
436 
437 void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate,
438 					const char *fmt, ...)
439 {
440 	struct va_format vaf;
441 	va_list args;
442 	int level;
443 
444 	va_start(args, fmt);
445 
446 	level = printk_get_level(fmt);
447 	vaf.fmt = printk_skip_level(fmt);
448 	vaf.va = &args;
449 	if (limit_rate)
450 		if (sbi)
451 			printk_ratelimited("%c%cF2FS-fs (%s): %pV\n",
452 				KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
453 		else
454 			printk_ratelimited("%c%cF2FS-fs: %pV\n",
455 				KERN_SOH_ASCII, level, &vaf);
456 	else
457 		if (sbi)
458 			printk("%c%cF2FS-fs (%s): %pV\n",
459 				KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
460 		else
461 			printk("%c%cF2FS-fs: %pV\n",
462 				KERN_SOH_ASCII, level, &vaf);
463 
464 	va_end(args);
465 }
466 
467 #if IS_ENABLED(CONFIG_UNICODE)
468 static const struct f2fs_sb_encodings {
469 	__u16 magic;
470 	char *name;
471 	unsigned int version;
472 } f2fs_sb_encoding_map[] = {
473 	{F2FS_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
474 };
475 
476 static const struct f2fs_sb_encodings *
477 f2fs_sb_read_encoding(const struct f2fs_super_block *sb)
478 {
479 	__u16 magic = le16_to_cpu(sb->s_encoding);
480 	int i;
481 
482 	for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
483 		if (magic == f2fs_sb_encoding_map[i].magic)
484 			return &f2fs_sb_encoding_map[i];
485 
486 	return NULL;
487 }
488 
489 struct kmem_cache *f2fs_cf_name_slab;
490 static int __init f2fs_create_casefold_cache(void)
491 {
492 	f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
493 						   F2FS_NAME_LEN);
494 	return f2fs_cf_name_slab ? 0 : -ENOMEM;
495 }
496 
497 static void f2fs_destroy_casefold_cache(void)
498 {
499 	kmem_cache_destroy(f2fs_cf_name_slab);
500 }
501 #else
502 static int __init f2fs_create_casefold_cache(void) { return 0; }
503 static void f2fs_destroy_casefold_cache(void) { }
504 #endif
505 
506 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
507 {
508 	block_t block_limit = min((sbi->user_block_count >> 3),
509 			sbi->user_block_count - sbi->reserved_blocks);
510 	block_t node_limit = sbi->total_node_count >> 3;
511 
512 	/* limit is 12.5% */
513 	if (test_opt(sbi, RESERVE_ROOT) &&
514 			F2FS_OPTION(sbi).root_reserved_blocks > block_limit) {
515 		F2FS_OPTION(sbi).root_reserved_blocks = block_limit;
516 		f2fs_info(sbi, "Reduce reserved blocks for root = %u",
517 			  F2FS_OPTION(sbi).root_reserved_blocks);
518 	}
519 	if (test_opt(sbi, RESERVE_NODE) &&
520 			F2FS_OPTION(sbi).root_reserved_nodes > node_limit) {
521 		F2FS_OPTION(sbi).root_reserved_nodes = node_limit;
522 		f2fs_info(sbi, "Reduce reserved nodes for root = %u",
523 			  F2FS_OPTION(sbi).root_reserved_nodes);
524 	}
525 	if (!test_opt(sbi, RESERVE_ROOT) && !test_opt(sbi, RESERVE_NODE) &&
526 		(!uid_eq(F2FS_OPTION(sbi).s_resuid,
527 				make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
528 		!gid_eq(F2FS_OPTION(sbi).s_resgid,
529 				make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
530 		f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root"
531 				" and reserve_node",
532 			  from_kuid_munged(&init_user_ns,
533 					   F2FS_OPTION(sbi).s_resuid),
534 			  from_kgid_munged(&init_user_ns,
535 					   F2FS_OPTION(sbi).s_resgid));
536 }
537 
538 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
539 {
540 	if (!F2FS_OPTION(sbi).unusable_cap_perc)
541 		return;
542 
543 	if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
544 		F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
545 	else
546 		F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
547 					F2FS_OPTION(sbi).unusable_cap_perc;
548 
549 	f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
550 			F2FS_OPTION(sbi).unusable_cap,
551 			F2FS_OPTION(sbi).unusable_cap_perc);
552 }
553 
554 static void init_once(void *foo)
555 {
556 	struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
557 
558 	inode_init_once(&fi->vfs_inode);
559 #ifdef CONFIG_FS_ENCRYPTION
560 	fi->i_crypt_info = NULL;
561 #endif
562 }
563 
564 #ifdef CONFIG_QUOTA
565 static const char * const quotatypes[] = INITQFNAMES;
566 #define QTYPE2NAME(t) (quotatypes[t])
567 /*
568  * Note the name of the specified quota file.
569  */
570 static int f2fs_note_qf_name(struct fs_context *fc, int qtype,
571 			     struct fs_parameter *param)
572 {
573 	struct f2fs_fs_context *ctx = fc->fs_private;
574 	char *qname;
575 
576 	if (param->size < 1) {
577 		f2fs_err(NULL, "Missing quota name");
578 		return -EINVAL;
579 	}
580 	if (strchr(param->string, '/')) {
581 		f2fs_err(NULL, "quotafile must be on filesystem root");
582 		return -EINVAL;
583 	}
584 	if (ctx->info.s_qf_names[qtype]) {
585 		if (strcmp(ctx->info.s_qf_names[qtype], param->string) != 0) {
586 			f2fs_err(NULL, "Quota file already specified");
587 			return -EINVAL;
588 		}
589 		return 0;
590 	}
591 
592 	qname = kmemdup_nul(param->string, param->size, GFP_KERNEL);
593 	if (!qname) {
594 		f2fs_err(NULL, "Not enough memory for storing quotafile name");
595 		return -ENOMEM;
596 	}
597 	F2FS_CTX_INFO(ctx).s_qf_names[qtype] = qname;
598 	ctx->qname_mask |= 1 << qtype;
599 	return 0;
600 }
601 
602 /*
603  * Clear the name of the specified quota file.
604  */
605 static int f2fs_unnote_qf_name(struct fs_context *fc, int qtype)
606 {
607 	struct f2fs_fs_context *ctx = fc->fs_private;
608 
609 	kfree(ctx->info.s_qf_names[qtype]);
610 	ctx->info.s_qf_names[qtype] = NULL;
611 	ctx->qname_mask |= 1 << qtype;
612 	return 0;
613 }
614 
615 static void f2fs_unnote_qf_name_all(struct fs_context *fc)
616 {
617 	int i;
618 
619 	for (i = 0; i < MAXQUOTAS; i++)
620 		f2fs_unnote_qf_name(fc, i);
621 }
622 #endif
623 
624 static int f2fs_parse_test_dummy_encryption(const struct fs_parameter *param,
625 					    struct f2fs_fs_context *ctx)
626 {
627 	int err;
628 
629 	if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
630 		f2fs_warn(NULL, "test_dummy_encryption option not supported");
631 		return -EINVAL;
632 	}
633 	err = fscrypt_parse_test_dummy_encryption(param,
634 					&ctx->info.dummy_enc_policy);
635 	if (err) {
636 		if (err == -EINVAL)
637 			f2fs_warn(NULL, "Value of option \"%s\" is unrecognized",
638 				  param->key);
639 		else if (err == -EEXIST)
640 			f2fs_warn(NULL, "Conflicting test_dummy_encryption options");
641 		else
642 			f2fs_warn(NULL, "Error processing option \"%s\" [%d]",
643 				  param->key, err);
644 		return -EINVAL;
645 	}
646 	return 0;
647 }
648 
649 #ifdef CONFIG_F2FS_FS_COMPRESSION
650 static bool is_compress_extension_exist(struct f2fs_mount_info *info,
651 					const char *new_ext, bool is_ext)
652 {
653 	unsigned char (*ext)[F2FS_EXTENSION_LEN];
654 	int ext_cnt;
655 	int i;
656 
657 	if (is_ext) {
658 		ext = info->extensions;
659 		ext_cnt = info->compress_ext_cnt;
660 	} else {
661 		ext = info->noextensions;
662 		ext_cnt = info->nocompress_ext_cnt;
663 	}
664 
665 	for (i = 0; i < ext_cnt; i++) {
666 		if (!strcasecmp(new_ext, ext[i]))
667 			return true;
668 	}
669 
670 	return false;
671 }
672 
673 /*
674  * 1. The same extension name cannot not appear in both compress and non-compress extension
675  * at the same time.
676  * 2. If the compress extension specifies all files, the types specified by the non-compress
677  * extension will be treated as special cases and will not be compressed.
678  * 3. Don't allow the non-compress extension specifies all files.
679  */
680 static int f2fs_test_compress_extension(unsigned char (*noext)[F2FS_EXTENSION_LEN],
681 					int noext_cnt,
682 					unsigned char (*ext)[F2FS_EXTENSION_LEN],
683 					int ext_cnt)
684 {
685 	int index = 0, no_index = 0;
686 
687 	if (!noext_cnt)
688 		return 0;
689 
690 	for (no_index = 0; no_index < noext_cnt; no_index++) {
691 		if (strlen(noext[no_index]) == 0)
692 			continue;
693 		if (!strcasecmp("*", noext[no_index])) {
694 			f2fs_info(NULL, "Don't allow the nocompress extension specifies all files");
695 			return -EINVAL;
696 		}
697 		for (index = 0; index < ext_cnt; index++) {
698 			if (strlen(ext[index]) == 0)
699 				continue;
700 			if (!strcasecmp(ext[index], noext[no_index])) {
701 				f2fs_info(NULL, "Don't allow the same extension %s appear in both compress and nocompress extension",
702 						ext[index]);
703 				return -EINVAL;
704 			}
705 		}
706 	}
707 	return 0;
708 }
709 
710 #ifdef CONFIG_F2FS_FS_LZ4
711 static int f2fs_set_lz4hc_level(struct f2fs_fs_context *ctx, const char *str)
712 {
713 #ifdef CONFIG_F2FS_FS_LZ4HC
714 	unsigned int level;
715 
716 	if (strlen(str) == 3) {
717 		F2FS_CTX_INFO(ctx).compress_level = 0;
718 		ctx->spec_mask |= F2FS_SPEC_compress_level;
719 		return 0;
720 	}
721 
722 	str += 3;
723 
724 	if (str[0] != ':') {
725 		f2fs_info(NULL, "wrong format, e.g. <alg_name>:<compr_level>");
726 		return -EINVAL;
727 	}
728 	if (kstrtouint(str + 1, 10, &level))
729 		return -EINVAL;
730 
731 	if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
732 		f2fs_info(NULL, "invalid lz4hc compress level: %d", level);
733 		return -EINVAL;
734 	}
735 
736 	F2FS_CTX_INFO(ctx).compress_level = level;
737 	ctx->spec_mask |= F2FS_SPEC_compress_level;
738 	return 0;
739 #else
740 	if (strlen(str) == 3) {
741 		F2FS_CTX_INFO(ctx).compress_level = 0;
742 		ctx->spec_mask |= F2FS_SPEC_compress_level;
743 		return 0;
744 	}
745 	f2fs_info(NULL, "kernel doesn't support lz4hc compression");
746 	return -EINVAL;
747 #endif
748 }
749 #endif
750 
751 #ifdef CONFIG_F2FS_FS_ZSTD
752 static int f2fs_set_zstd_level(struct f2fs_fs_context *ctx, const char *str)
753 {
754 	int level;
755 	int len = 4;
756 
757 	if (strlen(str) == len) {
758 		F2FS_CTX_INFO(ctx).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
759 		ctx->spec_mask |= F2FS_SPEC_compress_level;
760 		return 0;
761 	}
762 
763 	str += len;
764 
765 	if (str[0] != ':') {
766 		f2fs_info(NULL, "wrong format, e.g. <alg_name>:<compr_level>");
767 		return -EINVAL;
768 	}
769 	if (kstrtoint(str + 1, 10, &level))
770 		return -EINVAL;
771 
772 	/* f2fs does not support negative compress level now */
773 	if (level < 0) {
774 		f2fs_info(NULL, "do not support negative compress level: %d", level);
775 		return -ERANGE;
776 	}
777 
778 	if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
779 		f2fs_info(NULL, "invalid zstd compress level: %d", level);
780 		return -EINVAL;
781 	}
782 
783 	F2FS_CTX_INFO(ctx).compress_level = level;
784 	ctx->spec_mask |= F2FS_SPEC_compress_level;
785 	return 0;
786 }
787 #endif
788 #endif
789 
790 static int f2fs_parse_param(struct fs_context *fc, struct fs_parameter *param)
791 {
792 	struct f2fs_fs_context *ctx = fc->fs_private;
793 #ifdef CONFIG_F2FS_FS_COMPRESSION
794 	unsigned char (*ext)[F2FS_EXTENSION_LEN];
795 	unsigned char (*noext)[F2FS_EXTENSION_LEN];
796 	int ext_cnt, noext_cnt;
797 	char *name;
798 #endif
799 	substring_t args[MAX_OPT_ARGS];
800 	struct fs_parse_result result;
801 	int token, ret, arg;
802 
803 	token = fs_parse(fc, f2fs_param_specs, param, &result);
804 	if (token < 0)
805 		return token;
806 
807 	switch (token) {
808 	case Opt_gc_background:
809 		F2FS_CTX_INFO(ctx).bggc_mode = result.uint_32;
810 		ctx->spec_mask |= F2FS_SPEC_background_gc;
811 		break;
812 	case Opt_disable_roll_forward:
813 		ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_ROLL_FORWARD);
814 		break;
815 	case Opt_norecovery:
816 		/* requires ro mount, checked in f2fs_validate_options */
817 		ctx_set_opt(ctx, F2FS_MOUNT_NORECOVERY);
818 		break;
819 	case Opt_discard:
820 		if (result.negated)
821 			ctx_clear_opt(ctx, F2FS_MOUNT_DISCARD);
822 		else
823 			ctx_set_opt(ctx, F2FS_MOUNT_DISCARD);
824 		break;
825 	case Opt_noheap:
826 	case Opt_heap:
827 		f2fs_warn(NULL, "heap/no_heap options were deprecated");
828 		break;
829 #ifdef CONFIG_F2FS_FS_XATTR
830 	case Opt_user_xattr:
831 		if (result.negated)
832 			ctx_clear_opt(ctx, F2FS_MOUNT_XATTR_USER);
833 		else
834 			ctx_set_opt(ctx, F2FS_MOUNT_XATTR_USER);
835 		break;
836 	case Opt_inline_xattr:
837 		if (result.negated)
838 			ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_XATTR);
839 		else
840 			ctx_set_opt(ctx, F2FS_MOUNT_INLINE_XATTR);
841 		break;
842 	case Opt_inline_xattr_size:
843 		if (result.int_32 < MIN_INLINE_XATTR_SIZE ||
844 			result.int_32 > MAX_INLINE_XATTR_SIZE) {
845 			f2fs_err(NULL, "inline xattr size is out of range: %u ~ %u",
846 				 (u32)MIN_INLINE_XATTR_SIZE, (u32)MAX_INLINE_XATTR_SIZE);
847 			return -EINVAL;
848 		}
849 		ctx_set_opt(ctx, F2FS_MOUNT_INLINE_XATTR_SIZE);
850 		F2FS_CTX_INFO(ctx).inline_xattr_size = result.int_32;
851 		ctx->spec_mask |= F2FS_SPEC_inline_xattr_size;
852 		break;
853 #else
854 	case Opt_user_xattr:
855 	case Opt_inline_xattr:
856 	case Opt_inline_xattr_size:
857 		f2fs_info(NULL, "%s options not supported", param->key);
858 		break;
859 #endif
860 #ifdef CONFIG_F2FS_FS_POSIX_ACL
861 	case Opt_acl:
862 		if (result.negated)
863 			ctx_clear_opt(ctx, F2FS_MOUNT_POSIX_ACL);
864 		else
865 			ctx_set_opt(ctx, F2FS_MOUNT_POSIX_ACL);
866 		break;
867 #else
868 	case Opt_acl:
869 		f2fs_info(NULL, "%s options not supported", param->key);
870 		break;
871 #endif
872 	case Opt_active_logs:
873 		if (result.int_32 != 2 && result.int_32 != 4 &&
874 			result.int_32 != NR_CURSEG_PERSIST_TYPE)
875 			return -EINVAL;
876 		ctx->spec_mask |= F2FS_SPEC_active_logs;
877 		F2FS_CTX_INFO(ctx).active_logs = result.int_32;
878 		break;
879 	case Opt_disable_ext_identify:
880 		ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_EXT_IDENTIFY);
881 		break;
882 	case Opt_inline_data:
883 		if (result.negated)
884 			ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_DATA);
885 		else
886 			ctx_set_opt(ctx, F2FS_MOUNT_INLINE_DATA);
887 		break;
888 	case Opt_inline_dentry:
889 		if (result.negated)
890 			ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_DENTRY);
891 		else
892 			ctx_set_opt(ctx, F2FS_MOUNT_INLINE_DENTRY);
893 		break;
894 	case Opt_flush_merge:
895 		if (result.negated)
896 			ctx_clear_opt(ctx, F2FS_MOUNT_FLUSH_MERGE);
897 		else
898 			ctx_set_opt(ctx, F2FS_MOUNT_FLUSH_MERGE);
899 		break;
900 	case Opt_barrier:
901 		if (result.negated)
902 			ctx_set_opt(ctx, F2FS_MOUNT_NOBARRIER);
903 		else
904 			ctx_clear_opt(ctx, F2FS_MOUNT_NOBARRIER);
905 		break;
906 	case Opt_fastboot:
907 		ctx_set_opt(ctx, F2FS_MOUNT_FASTBOOT);
908 		break;
909 	case Opt_extent_cache:
910 		if (result.negated)
911 			ctx_clear_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE);
912 		else
913 			ctx_set_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE);
914 		break;
915 	case Opt_data_flush:
916 		ctx_set_opt(ctx, F2FS_MOUNT_DATA_FLUSH);
917 		break;
918 	case Opt_reserve_root:
919 		ctx_set_opt(ctx, F2FS_MOUNT_RESERVE_ROOT);
920 		F2FS_CTX_INFO(ctx).root_reserved_blocks = result.uint_32;
921 		ctx->spec_mask |= F2FS_SPEC_reserve_root;
922 		break;
923 	case Opt_reserve_node:
924 		ctx_set_opt(ctx, F2FS_MOUNT_RESERVE_NODE);
925 		F2FS_CTX_INFO(ctx).root_reserved_nodes = result.uint_32;
926 		ctx->spec_mask |= F2FS_SPEC_reserve_node;
927 		break;
928 	case Opt_resuid:
929 		F2FS_CTX_INFO(ctx).s_resuid = result.uid;
930 		ctx->spec_mask |= F2FS_SPEC_resuid;
931 		break;
932 	case Opt_resgid:
933 		F2FS_CTX_INFO(ctx).s_resgid = result.gid;
934 		ctx->spec_mask |= F2FS_SPEC_resgid;
935 		break;
936 	case Opt_mode:
937 		F2FS_CTX_INFO(ctx).fs_mode = result.uint_32;
938 		ctx->spec_mask |= F2FS_SPEC_mode;
939 		break;
940 #ifdef CONFIG_F2FS_FAULT_INJECTION
941 	case Opt_fault_injection:
942 		F2FS_CTX_INFO(ctx).fault_info.inject_rate = result.int_32;
943 		ctx->spec_mask |= F2FS_SPEC_fault_injection;
944 		ctx_set_opt(ctx, F2FS_MOUNT_FAULT_INJECTION);
945 		break;
946 
947 	case Opt_fault_type:
948 		if (result.uint_32 > BIT(FAULT_MAX))
949 			return -EINVAL;
950 		F2FS_CTX_INFO(ctx).fault_info.inject_type = result.uint_32;
951 		ctx->spec_mask |= F2FS_SPEC_fault_type;
952 		ctx_set_opt(ctx, F2FS_MOUNT_FAULT_INJECTION);
953 		break;
954 #else
955 	case Opt_fault_injection:
956 	case Opt_fault_type:
957 		f2fs_info(NULL, "%s options not supported", param->key);
958 		break;
959 #endif
960 	case Opt_lazytime:
961 		if (result.negated)
962 			ctx_clear_opt(ctx, F2FS_MOUNT_LAZYTIME);
963 		else
964 			ctx_set_opt(ctx, F2FS_MOUNT_LAZYTIME);
965 		break;
966 #ifdef CONFIG_QUOTA
967 	case Opt_quota:
968 		if (result.negated) {
969 			ctx_clear_opt(ctx, F2FS_MOUNT_QUOTA);
970 			ctx_clear_opt(ctx, F2FS_MOUNT_USRQUOTA);
971 			ctx_clear_opt(ctx, F2FS_MOUNT_GRPQUOTA);
972 			ctx_clear_opt(ctx, F2FS_MOUNT_PRJQUOTA);
973 		} else
974 			ctx_set_opt(ctx, F2FS_MOUNT_USRQUOTA);
975 		break;
976 	case Opt_usrquota:
977 		ctx_set_opt(ctx, F2FS_MOUNT_USRQUOTA);
978 		break;
979 	case Opt_grpquota:
980 		ctx_set_opt(ctx, F2FS_MOUNT_GRPQUOTA);
981 		break;
982 	case Opt_prjquota:
983 		ctx_set_opt(ctx, F2FS_MOUNT_PRJQUOTA);
984 		break;
985 	case Opt_usrjquota:
986 		if (param->type == fs_value_is_string && *param->string)
987 			ret = f2fs_note_qf_name(fc, USRQUOTA, param);
988 		else
989 			ret = f2fs_unnote_qf_name(fc, USRQUOTA);
990 		if (ret)
991 			return ret;
992 		break;
993 	case Opt_grpjquota:
994 		if (param->type == fs_value_is_string && *param->string)
995 			ret = f2fs_note_qf_name(fc, GRPQUOTA, param);
996 		else
997 			ret = f2fs_unnote_qf_name(fc, GRPQUOTA);
998 		if (ret)
999 			return ret;
1000 		break;
1001 	case Opt_prjjquota:
1002 		if (param->type == fs_value_is_string && *param->string)
1003 			ret = f2fs_note_qf_name(fc, PRJQUOTA, param);
1004 		else
1005 			ret = f2fs_unnote_qf_name(fc, PRJQUOTA);
1006 		if (ret)
1007 			return ret;
1008 		break;
1009 	case Opt_jqfmt:
1010 		F2FS_CTX_INFO(ctx).s_jquota_fmt = result.int_32;
1011 		ctx->spec_mask |= F2FS_SPEC_jqfmt;
1012 		break;
1013 #else
1014 	case Opt_quota:
1015 	case Opt_usrquota:
1016 	case Opt_grpquota:
1017 	case Opt_prjquota:
1018 	case Opt_usrjquota:
1019 	case Opt_grpjquota:
1020 	case Opt_prjjquota:
1021 		f2fs_info(NULL, "quota operations not supported");
1022 		break;
1023 #endif
1024 	case Opt_alloc:
1025 		F2FS_CTX_INFO(ctx).alloc_mode = result.uint_32;
1026 		ctx->spec_mask |= F2FS_SPEC_alloc_mode;
1027 		break;
1028 	case Opt_fsync:
1029 		F2FS_CTX_INFO(ctx).fsync_mode = result.uint_32;
1030 		ctx->spec_mask |= F2FS_SPEC_fsync_mode;
1031 		break;
1032 	case Opt_test_dummy_encryption:
1033 		ret = f2fs_parse_test_dummy_encryption(param, ctx);
1034 		if (ret)
1035 			return ret;
1036 		break;
1037 	case Opt_inlinecrypt:
1038 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1039 		ctx_set_opt(ctx, F2FS_MOUNT_INLINECRYPT);
1040 #else
1041 		f2fs_info(NULL, "inline encryption not supported");
1042 #endif
1043 		break;
1044 	case Opt_checkpoint:
1045 		/*
1046 		 * Initialize args struct so we know whether arg was
1047 		 * found; some options take optional arguments.
1048 		 */
1049 		args[0].from = args[0].to = NULL;
1050 		arg = 0;
1051 
1052 		/* revert to match_table for checkpoint= options */
1053 		token = match_token(param->string, f2fs_checkpoint_tokens, args);
1054 		switch (token) {
1055 		case Opt_checkpoint_disable_cap_perc:
1056 			if (args->from && match_int(args, &arg))
1057 				return -EINVAL;
1058 			if (arg < 0 || arg > 100)
1059 				return -EINVAL;
1060 			F2FS_CTX_INFO(ctx).unusable_cap_perc = arg;
1061 			ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap_perc;
1062 			ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
1063 			break;
1064 		case Opt_checkpoint_disable_cap:
1065 			if (args->from && match_int(args, &arg))
1066 				return -EINVAL;
1067 			F2FS_CTX_INFO(ctx).unusable_cap = arg;
1068 			ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap;
1069 			ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
1070 			break;
1071 		case Opt_checkpoint_disable:
1072 			ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
1073 			break;
1074 		case Opt_checkpoint_enable:
1075 			F2FS_CTX_INFO(ctx).unusable_cap_perc = 0;
1076 			ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap_perc;
1077 			F2FS_CTX_INFO(ctx).unusable_cap = 0;
1078 			ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap;
1079 			ctx_clear_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
1080 			break;
1081 		default:
1082 			return -EINVAL;
1083 		}
1084 		break;
1085 	case Opt_checkpoint_merge:
1086 		if (result.negated)
1087 			ctx_clear_opt(ctx, F2FS_MOUNT_MERGE_CHECKPOINT);
1088 		else
1089 			ctx_set_opt(ctx, F2FS_MOUNT_MERGE_CHECKPOINT);
1090 		break;
1091 #ifdef CONFIG_F2FS_FS_COMPRESSION
1092 	case Opt_compress_algorithm:
1093 		name = param->string;
1094 		if (!strcmp(name, "lzo")) {
1095 #ifdef CONFIG_F2FS_FS_LZO
1096 			F2FS_CTX_INFO(ctx).compress_level = 0;
1097 			F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZO;
1098 			ctx->spec_mask |= F2FS_SPEC_compress_level;
1099 			ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
1100 #else
1101 			f2fs_info(NULL, "kernel doesn't support lzo compression");
1102 #endif
1103 		} else if (!strncmp(name, "lz4", 3)) {
1104 #ifdef CONFIG_F2FS_FS_LZ4
1105 			ret = f2fs_set_lz4hc_level(ctx, name);
1106 			if (ret)
1107 				return -EINVAL;
1108 			F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZ4;
1109 			ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
1110 #else
1111 			f2fs_info(NULL, "kernel doesn't support lz4 compression");
1112 #endif
1113 		} else if (!strncmp(name, "zstd", 4)) {
1114 #ifdef CONFIG_F2FS_FS_ZSTD
1115 			ret = f2fs_set_zstd_level(ctx, name);
1116 			if (ret)
1117 				return -EINVAL;
1118 			F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_ZSTD;
1119 			ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
1120 #else
1121 			f2fs_info(NULL, "kernel doesn't support zstd compression");
1122 #endif
1123 		} else if (!strcmp(name, "lzo-rle")) {
1124 #ifdef CONFIG_F2FS_FS_LZORLE
1125 			F2FS_CTX_INFO(ctx).compress_level = 0;
1126 			F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZORLE;
1127 			ctx->spec_mask |= F2FS_SPEC_compress_level;
1128 			ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
1129 #else
1130 			f2fs_info(NULL, "kernel doesn't support lzorle compression");
1131 #endif
1132 		} else
1133 			return -EINVAL;
1134 		break;
1135 	case Opt_compress_log_size:
1136 		if (result.uint_32 < MIN_COMPRESS_LOG_SIZE ||
1137 		    result.uint_32 > MAX_COMPRESS_LOG_SIZE) {
1138 			f2fs_err(NULL,
1139 				"Compress cluster log size is out of range");
1140 			return -EINVAL;
1141 		}
1142 		F2FS_CTX_INFO(ctx).compress_log_size = result.uint_32;
1143 		ctx->spec_mask |= F2FS_SPEC_compress_log_size;
1144 		break;
1145 	case Opt_compress_extension:
1146 		name = param->string;
1147 		ext = F2FS_CTX_INFO(ctx).extensions;
1148 		ext_cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt;
1149 
1150 		if (strlen(name) >= F2FS_EXTENSION_LEN ||
1151 		    ext_cnt >= COMPRESS_EXT_NUM) {
1152 			f2fs_err(NULL, "invalid extension length/number");
1153 			return -EINVAL;
1154 		}
1155 
1156 		if (is_compress_extension_exist(&ctx->info, name, true))
1157 			break;
1158 
1159 		ret = strscpy(ext[ext_cnt], name, F2FS_EXTENSION_LEN);
1160 		if (ret < 0)
1161 			return ret;
1162 		F2FS_CTX_INFO(ctx).compress_ext_cnt++;
1163 		ctx->spec_mask |= F2FS_SPEC_compress_extension;
1164 		break;
1165 	case Opt_nocompress_extension:
1166 		name = param->string;
1167 		noext = F2FS_CTX_INFO(ctx).noextensions;
1168 		noext_cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt;
1169 
1170 		if (strlen(name) >= F2FS_EXTENSION_LEN ||
1171 			noext_cnt >= COMPRESS_EXT_NUM) {
1172 			f2fs_err(NULL, "invalid extension length/number");
1173 			return -EINVAL;
1174 		}
1175 
1176 		if (is_compress_extension_exist(&ctx->info, name, false))
1177 			break;
1178 
1179 		ret = strscpy(noext[noext_cnt], name, F2FS_EXTENSION_LEN);
1180 		if (ret < 0)
1181 			return ret;
1182 		F2FS_CTX_INFO(ctx).nocompress_ext_cnt++;
1183 		ctx->spec_mask |= F2FS_SPEC_nocompress_extension;
1184 		break;
1185 	case Opt_compress_chksum:
1186 		F2FS_CTX_INFO(ctx).compress_chksum = true;
1187 		ctx->spec_mask |= F2FS_SPEC_compress_chksum;
1188 		break;
1189 	case Opt_compress_mode:
1190 		F2FS_CTX_INFO(ctx).compress_mode = result.uint_32;
1191 		ctx->spec_mask |= F2FS_SPEC_compress_mode;
1192 		break;
1193 	case Opt_compress_cache:
1194 		ctx_set_opt(ctx, F2FS_MOUNT_COMPRESS_CACHE);
1195 		break;
1196 #else
1197 	case Opt_compress_algorithm:
1198 	case Opt_compress_log_size:
1199 	case Opt_compress_extension:
1200 	case Opt_nocompress_extension:
1201 	case Opt_compress_chksum:
1202 	case Opt_compress_mode:
1203 	case Opt_compress_cache:
1204 		f2fs_info(NULL, "compression options not supported");
1205 		break;
1206 #endif
1207 	case Opt_atgc:
1208 		ctx_set_opt(ctx, F2FS_MOUNT_ATGC);
1209 		break;
1210 	case Opt_gc_merge:
1211 		if (result.negated)
1212 			ctx_clear_opt(ctx, F2FS_MOUNT_GC_MERGE);
1213 		else
1214 			ctx_set_opt(ctx, F2FS_MOUNT_GC_MERGE);
1215 		break;
1216 	case Opt_discard_unit:
1217 		F2FS_CTX_INFO(ctx).discard_unit = result.uint_32;
1218 		ctx->spec_mask |= F2FS_SPEC_discard_unit;
1219 		break;
1220 	case Opt_memory_mode:
1221 		F2FS_CTX_INFO(ctx).memory_mode = result.uint_32;
1222 		ctx->spec_mask |= F2FS_SPEC_memory_mode;
1223 		break;
1224 	case Opt_age_extent_cache:
1225 		ctx_set_opt(ctx, F2FS_MOUNT_AGE_EXTENT_CACHE);
1226 		break;
1227 	case Opt_errors:
1228 		F2FS_CTX_INFO(ctx).errors = result.uint_32;
1229 		ctx->spec_mask |= F2FS_SPEC_errors;
1230 		break;
1231 	case Opt_nat_bits:
1232 		ctx_set_opt(ctx, F2FS_MOUNT_NAT_BITS);
1233 		break;
1234 	case Opt_lookup_mode:
1235 		F2FS_CTX_INFO(ctx).lookup_mode = result.uint_32;
1236 		ctx->spec_mask |= F2FS_SPEC_lookup_mode;
1237 		break;
1238 	}
1239 	return 0;
1240 }
1241 
1242 /*
1243  * Check quota settings consistency.
1244  */
1245 static int f2fs_check_quota_consistency(struct fs_context *fc,
1246 					struct super_block *sb)
1247 {
1248 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1249  #ifdef CONFIG_QUOTA
1250 	struct f2fs_fs_context *ctx = fc->fs_private;
1251 	bool quota_feature = f2fs_sb_has_quota_ino(sbi);
1252 	bool quota_turnon = sb_any_quota_loaded(sb);
1253 	char *old_qname, *new_qname;
1254 	bool usr_qf_name, grp_qf_name, prj_qf_name, usrquota, grpquota, prjquota;
1255 	int i;
1256 
1257 	/*
1258 	 * We do the test below only for project quotas. 'usrquota' and
1259 	 * 'grpquota' mount options are allowed even without quota feature
1260 	 * to support legacy quotas in quota files.
1261 	 */
1262 	if (ctx_test_opt(ctx, F2FS_MOUNT_PRJQUOTA) &&
1263 			!f2fs_sb_has_project_quota(sbi)) {
1264 		f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
1265 		return -EINVAL;
1266 	}
1267 
1268 	if (ctx->qname_mask) {
1269 		for (i = 0; i < MAXQUOTAS; i++) {
1270 			if (!(ctx->qname_mask & (1 << i)))
1271 				continue;
1272 
1273 			old_qname = F2FS_OPTION(sbi).s_qf_names[i];
1274 			new_qname = F2FS_CTX_INFO(ctx).s_qf_names[i];
1275 			if (quota_turnon &&
1276 				!!old_qname != !!new_qname)
1277 				goto err_jquota_change;
1278 
1279 			if (old_qname) {
1280 				if (!new_qname) {
1281 					f2fs_info(sbi, "remove qf_name %s",
1282 								old_qname);
1283 					continue;
1284 				} else if (strcmp(old_qname, new_qname) == 0) {
1285 					ctx->qname_mask &= ~(1 << i);
1286 					continue;
1287 				}
1288 				goto err_jquota_specified;
1289 			}
1290 
1291 			if (quota_feature) {
1292 				f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
1293 				ctx->qname_mask &= ~(1 << i);
1294 				kfree(F2FS_CTX_INFO(ctx).s_qf_names[i]);
1295 				F2FS_CTX_INFO(ctx).s_qf_names[i] = NULL;
1296 			}
1297 		}
1298 	}
1299 
1300 	/* Make sure we don't mix old and new quota format */
1301 	usr_qf_name = F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
1302 			F2FS_CTX_INFO(ctx).s_qf_names[USRQUOTA];
1303 	grp_qf_name = F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
1304 			F2FS_CTX_INFO(ctx).s_qf_names[GRPQUOTA];
1305 	prj_qf_name = F2FS_OPTION(sbi).s_qf_names[PRJQUOTA] ||
1306 			F2FS_CTX_INFO(ctx).s_qf_names[PRJQUOTA];
1307 	usrquota = test_opt(sbi, USRQUOTA) ||
1308 			ctx_test_opt(ctx, F2FS_MOUNT_USRQUOTA);
1309 	grpquota = test_opt(sbi, GRPQUOTA) ||
1310 			ctx_test_opt(ctx, F2FS_MOUNT_GRPQUOTA);
1311 	prjquota = test_opt(sbi, PRJQUOTA) ||
1312 			ctx_test_opt(ctx, F2FS_MOUNT_PRJQUOTA);
1313 
1314 	if (usr_qf_name) {
1315 		ctx_clear_opt(ctx, F2FS_MOUNT_USRQUOTA);
1316 		usrquota = false;
1317 	}
1318 	if (grp_qf_name) {
1319 		ctx_clear_opt(ctx, F2FS_MOUNT_GRPQUOTA);
1320 		grpquota = false;
1321 	}
1322 	if (prj_qf_name) {
1323 		ctx_clear_opt(ctx, F2FS_MOUNT_PRJQUOTA);
1324 		prjquota = false;
1325 	}
1326 	if (usr_qf_name || grp_qf_name || prj_qf_name) {
1327 		if (grpquota || usrquota || prjquota) {
1328 			f2fs_err(sbi, "old and new quota format mixing");
1329 			return -EINVAL;
1330 		}
1331 		if (!(ctx->spec_mask & F2FS_SPEC_jqfmt ||
1332 				F2FS_OPTION(sbi).s_jquota_fmt)) {
1333 			f2fs_err(sbi, "journaled quota format not specified");
1334 			return -EINVAL;
1335 		}
1336 	}
1337 	return 0;
1338 
1339 err_jquota_change:
1340 	f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
1341 	return -EINVAL;
1342 err_jquota_specified:
1343 	f2fs_err(sbi, "%s quota file already specified",
1344 		 QTYPE2NAME(i));
1345 	return -EINVAL;
1346 
1347 #else
1348 	if (f2fs_readonly(sbi->sb))
1349 		return 0;
1350 	if (f2fs_sb_has_quota_ino(sbi)) {
1351 		f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1352 		return -EINVAL;
1353 	}
1354 	if (f2fs_sb_has_project_quota(sbi)) {
1355 		f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1356 		return -EINVAL;
1357 	}
1358 
1359 	return 0;
1360 #endif
1361 }
1362 
1363 static int f2fs_check_test_dummy_encryption(struct fs_context *fc,
1364 					    struct super_block *sb)
1365 {
1366 	struct f2fs_fs_context *ctx = fc->fs_private;
1367 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1368 
1369 	if (!fscrypt_is_dummy_policy_set(&F2FS_CTX_INFO(ctx).dummy_enc_policy))
1370 		return 0;
1371 
1372 	if (!f2fs_sb_has_encrypt(sbi)) {
1373 		f2fs_err(sbi, "Encrypt feature is off");
1374 		return -EINVAL;
1375 	}
1376 
1377 	/*
1378 	 * This mount option is just for testing, and it's not worthwhile to
1379 	 * implement the extra complexity (e.g. RCU protection) that would be
1380 	 * needed to allow it to be set or changed during remount.  We do allow
1381 	 * it to be specified during remount, but only if there is no change.
1382 	 */
1383 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
1384 		if (fscrypt_dummy_policies_equal(&F2FS_OPTION(sbi).dummy_enc_policy,
1385 				&F2FS_CTX_INFO(ctx).dummy_enc_policy))
1386 			return 0;
1387 		f2fs_warn(sbi, "Can't set or change test_dummy_encryption on remount");
1388 		return -EINVAL;
1389 	}
1390 	return 0;
1391 }
1392 
1393 static inline bool test_compression_spec(unsigned int mask)
1394 {
1395 	return mask & (F2FS_SPEC_compress_algorithm
1396 			| F2FS_SPEC_compress_log_size
1397 			| F2FS_SPEC_compress_extension
1398 			| F2FS_SPEC_nocompress_extension
1399 			| F2FS_SPEC_compress_chksum
1400 			| F2FS_SPEC_compress_mode);
1401 }
1402 
1403 static inline void clear_compression_spec(struct f2fs_fs_context *ctx)
1404 {
1405 	ctx->spec_mask &= ~(F2FS_SPEC_compress_algorithm
1406 						| F2FS_SPEC_compress_log_size
1407 						| F2FS_SPEC_compress_extension
1408 						| F2FS_SPEC_nocompress_extension
1409 						| F2FS_SPEC_compress_chksum
1410 						| F2FS_SPEC_compress_mode);
1411 }
1412 
1413 static int f2fs_check_compression(struct fs_context *fc,
1414 				  struct super_block *sb)
1415 {
1416 #ifdef CONFIG_F2FS_FS_COMPRESSION
1417 	struct f2fs_fs_context *ctx = fc->fs_private;
1418 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1419 	int i, cnt;
1420 
1421 	if (!f2fs_sb_has_compression(sbi)) {
1422 		if (test_compression_spec(ctx->spec_mask) ||
1423 			ctx_test_opt(ctx, F2FS_MOUNT_COMPRESS_CACHE))
1424 			f2fs_info(sbi, "Image doesn't support compression");
1425 		clear_compression_spec(ctx);
1426 		ctx->opt_mask &= ~BIT(F2FS_MOUNT_COMPRESS_CACHE);
1427 		return 0;
1428 	}
1429 	if (ctx->spec_mask & F2FS_SPEC_compress_extension) {
1430 		cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt;
1431 		for (i = 0; i < F2FS_CTX_INFO(ctx).compress_ext_cnt; i++) {
1432 			if (is_compress_extension_exist(&F2FS_OPTION(sbi),
1433 					F2FS_CTX_INFO(ctx).extensions[i], true)) {
1434 				F2FS_CTX_INFO(ctx).extensions[i][0] = '\0';
1435 				cnt--;
1436 			}
1437 		}
1438 		if (F2FS_OPTION(sbi).compress_ext_cnt + cnt > COMPRESS_EXT_NUM) {
1439 			f2fs_err(sbi, "invalid extension length/number");
1440 			return -EINVAL;
1441 		}
1442 	}
1443 	if (ctx->spec_mask & F2FS_SPEC_nocompress_extension) {
1444 		cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt;
1445 		for (i = 0; i < F2FS_CTX_INFO(ctx).nocompress_ext_cnt; i++) {
1446 			if (is_compress_extension_exist(&F2FS_OPTION(sbi),
1447 					F2FS_CTX_INFO(ctx).noextensions[i], false)) {
1448 				F2FS_CTX_INFO(ctx).noextensions[i][0] = '\0';
1449 				cnt--;
1450 			}
1451 		}
1452 		if (F2FS_OPTION(sbi).nocompress_ext_cnt + cnt > COMPRESS_EXT_NUM) {
1453 			f2fs_err(sbi, "invalid noextension length/number");
1454 			return -EINVAL;
1455 		}
1456 	}
1457 
1458 	if (f2fs_test_compress_extension(F2FS_CTX_INFO(ctx).noextensions,
1459 				F2FS_CTX_INFO(ctx).nocompress_ext_cnt,
1460 				F2FS_CTX_INFO(ctx).extensions,
1461 				F2FS_CTX_INFO(ctx).compress_ext_cnt)) {
1462 		f2fs_err(sbi, "new noextensions conflicts with new extensions");
1463 		return -EINVAL;
1464 	}
1465 	if (f2fs_test_compress_extension(F2FS_CTX_INFO(ctx).noextensions,
1466 				F2FS_CTX_INFO(ctx).nocompress_ext_cnt,
1467 				F2FS_OPTION(sbi).extensions,
1468 				F2FS_OPTION(sbi).compress_ext_cnt)) {
1469 		f2fs_err(sbi, "new noextensions conflicts with old extensions");
1470 		return -EINVAL;
1471 	}
1472 	if (f2fs_test_compress_extension(F2FS_OPTION(sbi).noextensions,
1473 				F2FS_OPTION(sbi).nocompress_ext_cnt,
1474 				F2FS_CTX_INFO(ctx).extensions,
1475 				F2FS_CTX_INFO(ctx).compress_ext_cnt)) {
1476 		f2fs_err(sbi, "new extensions conflicts with old noextensions");
1477 		return -EINVAL;
1478 	}
1479 #endif
1480 	return 0;
1481 }
1482 
1483 static int f2fs_check_opt_consistency(struct fs_context *fc,
1484 				      struct super_block *sb)
1485 {
1486 	struct f2fs_fs_context *ctx = fc->fs_private;
1487 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1488 	int err;
1489 
1490 	if (ctx_test_opt(ctx, F2FS_MOUNT_NORECOVERY) && !f2fs_readonly(sb))
1491 		return -EINVAL;
1492 
1493 	if (f2fs_hw_should_discard(sbi) &&
1494 			(ctx->opt_mask & BIT(F2FS_MOUNT_DISCARD)) &&
1495 			!ctx_test_opt(ctx, F2FS_MOUNT_DISCARD)) {
1496 		f2fs_warn(sbi, "discard is required for zoned block devices");
1497 		return -EINVAL;
1498 	}
1499 
1500 	if (!f2fs_hw_support_discard(sbi) &&
1501 			(ctx->opt_mask & BIT(F2FS_MOUNT_DISCARD)) &&
1502 			ctx_test_opt(ctx, F2FS_MOUNT_DISCARD)) {
1503 		f2fs_warn(sbi, "device does not support discard");
1504 		ctx_clear_opt(ctx, F2FS_MOUNT_DISCARD);
1505 		ctx->opt_mask &= ~BIT(F2FS_MOUNT_DISCARD);
1506 	}
1507 
1508 	if (f2fs_sb_has_device_alias(sbi) &&
1509 			(ctx->opt_mask & BIT(F2FS_MOUNT_READ_EXTENT_CACHE)) &&
1510 			!ctx_test_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE)) {
1511 		f2fs_err(sbi, "device aliasing requires extent cache");
1512 		return -EINVAL;
1513 	}
1514 
1515 	if (test_opt(sbi, RESERVE_ROOT) &&
1516 			(ctx->opt_mask & BIT(F2FS_MOUNT_RESERVE_ROOT)) &&
1517 			ctx_test_opt(ctx, F2FS_MOUNT_RESERVE_ROOT)) {
1518 		f2fs_info(sbi, "Preserve previous reserve_root=%u",
1519 			F2FS_OPTION(sbi).root_reserved_blocks);
1520 		ctx_clear_opt(ctx, F2FS_MOUNT_RESERVE_ROOT);
1521 		ctx->opt_mask &= ~BIT(F2FS_MOUNT_RESERVE_ROOT);
1522 		ctx->spec_mask &= ~F2FS_SPEC_reserve_root;
1523 	}
1524 	if (test_opt(sbi, RESERVE_NODE) &&
1525 			(ctx->opt_mask & BIT(F2FS_MOUNT_RESERVE_NODE)) &&
1526 			ctx_test_opt(ctx, F2FS_MOUNT_RESERVE_NODE)) {
1527 		f2fs_info(sbi, "Preserve previous reserve_node=%u",
1528 			F2FS_OPTION(sbi).root_reserved_nodes);
1529 		ctx_clear_opt(ctx, F2FS_MOUNT_RESERVE_NODE);
1530 		ctx->opt_mask &= ~BIT(F2FS_MOUNT_RESERVE_NODE);
1531 		ctx->spec_mask &= ~F2FS_SPEC_reserve_node;
1532 	}
1533 
1534 	err = f2fs_check_test_dummy_encryption(fc, sb);
1535 	if (err)
1536 		return err;
1537 
1538 	err = f2fs_check_compression(fc, sb);
1539 	if (err)
1540 		return err;
1541 
1542 	err = f2fs_check_quota_consistency(fc, sb);
1543 	if (err)
1544 		return err;
1545 
1546 	if (!IS_ENABLED(CONFIG_UNICODE) && f2fs_sb_has_casefold(sbi)) {
1547 		f2fs_err(sbi,
1548 			"Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1549 		return -EINVAL;
1550 	}
1551 
1552 	/*
1553 	 * The BLKZONED feature indicates that the drive was formatted with
1554 	 * zone alignment optimization. This is optional for host-aware
1555 	 * devices, but mandatory for host-managed zoned block devices.
1556 	 */
1557 	if (f2fs_sb_has_blkzoned(sbi)) {
1558 		if (F2FS_CTX_INFO(ctx).bggc_mode == BGGC_MODE_OFF) {
1559 			f2fs_warn(sbi, "zoned devices need bggc");
1560 			return -EINVAL;
1561 		}
1562 #ifdef CONFIG_BLK_DEV_ZONED
1563 		if ((ctx->spec_mask & F2FS_SPEC_discard_unit) &&
1564 		F2FS_CTX_INFO(ctx).discard_unit != DISCARD_UNIT_SECTION) {
1565 			f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default");
1566 			F2FS_CTX_INFO(ctx).discard_unit = DISCARD_UNIT_SECTION;
1567 		}
1568 
1569 		if ((ctx->spec_mask & F2FS_SPEC_mode) &&
1570 		F2FS_CTX_INFO(ctx).fs_mode != FS_MODE_LFS) {
1571 			f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature");
1572 			return -EINVAL;
1573 		}
1574 #else
1575 		f2fs_err(sbi, "Zoned block device support is not enabled");
1576 		return -EINVAL;
1577 #endif
1578 	}
1579 
1580 	if (ctx_test_opt(ctx, F2FS_MOUNT_INLINE_XATTR_SIZE)) {
1581 		if (!f2fs_sb_has_extra_attr(sbi) ||
1582 			!f2fs_sb_has_flexible_inline_xattr(sbi)) {
1583 			f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1584 			return -EINVAL;
1585 		}
1586 		if (!ctx_test_opt(ctx, F2FS_MOUNT_INLINE_XATTR) && !test_opt(sbi, INLINE_XATTR)) {
1587 			f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1588 			return -EINVAL;
1589 		}
1590 	}
1591 
1592 	if (ctx_test_opt(ctx, F2FS_MOUNT_ATGC) &&
1593 	    F2FS_CTX_INFO(ctx).fs_mode == FS_MODE_LFS) {
1594 		f2fs_err(sbi, "LFS is not compatible with ATGC");
1595 		return -EINVAL;
1596 	}
1597 
1598 	if (f2fs_is_readonly(sbi) && ctx_test_opt(ctx, F2FS_MOUNT_FLUSH_MERGE)) {
1599 		f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
1600 		return -EINVAL;
1601 	}
1602 
1603 	if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1604 		f2fs_err(sbi, "Allow to mount readonly mode only");
1605 		return -EROFS;
1606 	}
1607 	return 0;
1608 }
1609 
1610 static void f2fs_apply_quota_options(struct fs_context *fc,
1611 				     struct super_block *sb)
1612 {
1613 #ifdef CONFIG_QUOTA
1614 	struct f2fs_fs_context *ctx = fc->fs_private;
1615 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1616 	bool quota_feature = f2fs_sb_has_quota_ino(sbi);
1617 	char *qname;
1618 	int i;
1619 
1620 	if (quota_feature)
1621 		return;
1622 
1623 	for (i = 0; i < MAXQUOTAS; i++) {
1624 		if (!(ctx->qname_mask & (1 << i)))
1625 			continue;
1626 
1627 		qname = F2FS_CTX_INFO(ctx).s_qf_names[i];
1628 		if (qname) {
1629 			qname = kstrdup(F2FS_CTX_INFO(ctx).s_qf_names[i],
1630 					GFP_KERNEL | __GFP_NOFAIL);
1631 			set_opt(sbi, QUOTA);
1632 		}
1633 		F2FS_OPTION(sbi).s_qf_names[i] = qname;
1634 	}
1635 
1636 	if (ctx->spec_mask & F2FS_SPEC_jqfmt)
1637 		F2FS_OPTION(sbi).s_jquota_fmt = F2FS_CTX_INFO(ctx).s_jquota_fmt;
1638 
1639 	if (quota_feature && F2FS_OPTION(sbi).s_jquota_fmt) {
1640 		f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
1641 		F2FS_OPTION(sbi).s_jquota_fmt = 0;
1642 	}
1643 #endif
1644 }
1645 
1646 static void f2fs_apply_test_dummy_encryption(struct fs_context *fc,
1647 					     struct super_block *sb)
1648 {
1649 	struct f2fs_fs_context *ctx = fc->fs_private;
1650 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1651 
1652 	if (!fscrypt_is_dummy_policy_set(&F2FS_CTX_INFO(ctx).dummy_enc_policy) ||
1653 		/* if already set, it was already verified to be the same */
1654 		fscrypt_is_dummy_policy_set(&F2FS_OPTION(sbi).dummy_enc_policy))
1655 		return;
1656 	swap(F2FS_OPTION(sbi).dummy_enc_policy, F2FS_CTX_INFO(ctx).dummy_enc_policy);
1657 	f2fs_warn(sbi, "Test dummy encryption mode enabled");
1658 }
1659 
1660 static void f2fs_apply_compression(struct fs_context *fc,
1661 				   struct super_block *sb)
1662 {
1663 #ifdef CONFIG_F2FS_FS_COMPRESSION
1664 	struct f2fs_fs_context *ctx = fc->fs_private;
1665 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1666 	unsigned char (*ctx_ext)[F2FS_EXTENSION_LEN];
1667 	unsigned char (*sbi_ext)[F2FS_EXTENSION_LEN];
1668 	int ctx_cnt, sbi_cnt, i;
1669 
1670 	if (ctx->spec_mask & F2FS_SPEC_compress_level)
1671 		F2FS_OPTION(sbi).compress_level =
1672 					F2FS_CTX_INFO(ctx).compress_level;
1673 	if (ctx->spec_mask & F2FS_SPEC_compress_algorithm)
1674 		F2FS_OPTION(sbi).compress_algorithm =
1675 					F2FS_CTX_INFO(ctx).compress_algorithm;
1676 	if (ctx->spec_mask & F2FS_SPEC_compress_log_size)
1677 		F2FS_OPTION(sbi).compress_log_size =
1678 					F2FS_CTX_INFO(ctx).compress_log_size;
1679 	if (ctx->spec_mask & F2FS_SPEC_compress_chksum)
1680 		F2FS_OPTION(sbi).compress_chksum =
1681 					F2FS_CTX_INFO(ctx).compress_chksum;
1682 	if (ctx->spec_mask & F2FS_SPEC_compress_mode)
1683 		F2FS_OPTION(sbi).compress_mode =
1684 					F2FS_CTX_INFO(ctx).compress_mode;
1685 	if (ctx->spec_mask & F2FS_SPEC_compress_extension) {
1686 		ctx_ext = F2FS_CTX_INFO(ctx).extensions;
1687 		ctx_cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt;
1688 		sbi_ext = F2FS_OPTION(sbi).extensions;
1689 		sbi_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1690 		for (i = 0; i < ctx_cnt; i++) {
1691 			if (strlen(ctx_ext[i]) == 0)
1692 				continue;
1693 			strscpy(sbi_ext[sbi_cnt], ctx_ext[i]);
1694 			sbi_cnt++;
1695 		}
1696 		F2FS_OPTION(sbi).compress_ext_cnt = sbi_cnt;
1697 	}
1698 	if (ctx->spec_mask & F2FS_SPEC_nocompress_extension) {
1699 		ctx_ext = F2FS_CTX_INFO(ctx).noextensions;
1700 		ctx_cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt;
1701 		sbi_ext = F2FS_OPTION(sbi).noextensions;
1702 		sbi_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
1703 		for (i = 0; i < ctx_cnt; i++) {
1704 			if (strlen(ctx_ext[i]) == 0)
1705 				continue;
1706 			strscpy(sbi_ext[sbi_cnt], ctx_ext[i]);
1707 			sbi_cnt++;
1708 		}
1709 		F2FS_OPTION(sbi).nocompress_ext_cnt = sbi_cnt;
1710 	}
1711 #endif
1712 }
1713 
1714 static void f2fs_apply_options(struct fs_context *fc, struct super_block *sb)
1715 {
1716 	struct f2fs_fs_context *ctx = fc->fs_private;
1717 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1718 
1719 	F2FS_OPTION(sbi).opt &= ~ctx->opt_mask;
1720 	F2FS_OPTION(sbi).opt |= F2FS_CTX_INFO(ctx).opt;
1721 
1722 	if (ctx->spec_mask & F2FS_SPEC_background_gc)
1723 		F2FS_OPTION(sbi).bggc_mode = F2FS_CTX_INFO(ctx).bggc_mode;
1724 	if (ctx->spec_mask & F2FS_SPEC_inline_xattr_size)
1725 		F2FS_OPTION(sbi).inline_xattr_size =
1726 					F2FS_CTX_INFO(ctx).inline_xattr_size;
1727 	if (ctx->spec_mask & F2FS_SPEC_active_logs)
1728 		F2FS_OPTION(sbi).active_logs = F2FS_CTX_INFO(ctx).active_logs;
1729 	if (ctx->spec_mask & F2FS_SPEC_reserve_root)
1730 		F2FS_OPTION(sbi).root_reserved_blocks =
1731 					F2FS_CTX_INFO(ctx).root_reserved_blocks;
1732 	if (ctx->spec_mask & F2FS_SPEC_reserve_node)
1733 		F2FS_OPTION(sbi).root_reserved_nodes =
1734 					F2FS_CTX_INFO(ctx).root_reserved_nodes;
1735 	if (ctx->spec_mask & F2FS_SPEC_resgid)
1736 		F2FS_OPTION(sbi).s_resgid = F2FS_CTX_INFO(ctx).s_resgid;
1737 	if (ctx->spec_mask & F2FS_SPEC_resuid)
1738 		F2FS_OPTION(sbi).s_resuid = F2FS_CTX_INFO(ctx).s_resuid;
1739 	if (ctx->spec_mask & F2FS_SPEC_mode)
1740 		F2FS_OPTION(sbi).fs_mode = F2FS_CTX_INFO(ctx).fs_mode;
1741 #ifdef CONFIG_F2FS_FAULT_INJECTION
1742 	if (ctx->spec_mask & F2FS_SPEC_fault_injection)
1743 		(void)f2fs_build_fault_attr(sbi,
1744 		F2FS_CTX_INFO(ctx).fault_info.inject_rate, 0, FAULT_RATE);
1745 	if (ctx->spec_mask & F2FS_SPEC_fault_type)
1746 		(void)f2fs_build_fault_attr(sbi, 0,
1747 			F2FS_CTX_INFO(ctx).fault_info.inject_type, FAULT_TYPE);
1748 #endif
1749 	if (ctx->spec_mask & F2FS_SPEC_alloc_mode)
1750 		F2FS_OPTION(sbi).alloc_mode = F2FS_CTX_INFO(ctx).alloc_mode;
1751 	if (ctx->spec_mask & F2FS_SPEC_fsync_mode)
1752 		F2FS_OPTION(sbi).fsync_mode = F2FS_CTX_INFO(ctx).fsync_mode;
1753 	if (ctx->spec_mask & F2FS_SPEC_checkpoint_disable_cap)
1754 		F2FS_OPTION(sbi).unusable_cap = F2FS_CTX_INFO(ctx).unusable_cap;
1755 	if (ctx->spec_mask & F2FS_SPEC_checkpoint_disable_cap_perc)
1756 		F2FS_OPTION(sbi).unusable_cap_perc =
1757 					F2FS_CTX_INFO(ctx).unusable_cap_perc;
1758 	if (ctx->spec_mask & F2FS_SPEC_discard_unit)
1759 		F2FS_OPTION(sbi).discard_unit = F2FS_CTX_INFO(ctx).discard_unit;
1760 	if (ctx->spec_mask & F2FS_SPEC_memory_mode)
1761 		F2FS_OPTION(sbi).memory_mode = F2FS_CTX_INFO(ctx).memory_mode;
1762 	if (ctx->spec_mask & F2FS_SPEC_errors)
1763 		F2FS_OPTION(sbi).errors = F2FS_CTX_INFO(ctx).errors;
1764 	if (ctx->spec_mask & F2FS_SPEC_lookup_mode)
1765 		F2FS_OPTION(sbi).lookup_mode = F2FS_CTX_INFO(ctx).lookup_mode;
1766 
1767 	f2fs_apply_compression(fc, sb);
1768 	f2fs_apply_test_dummy_encryption(fc, sb);
1769 	f2fs_apply_quota_options(fc, sb);
1770 }
1771 
1772 static int f2fs_sanity_check_options(struct f2fs_sb_info *sbi, bool remount)
1773 {
1774 	if (f2fs_sb_has_device_alias(sbi) &&
1775 	    !test_opt(sbi, READ_EXTENT_CACHE)) {
1776 		f2fs_err(sbi, "device aliasing requires extent cache");
1777 		return -EINVAL;
1778 	}
1779 
1780 	if (!remount)
1781 		return 0;
1782 
1783 #ifdef CONFIG_BLK_DEV_ZONED
1784 	if (f2fs_sb_has_blkzoned(sbi) &&
1785 	    sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
1786 		f2fs_err(sbi,
1787 			"zoned: max open zones %u is too small, need at least %u open zones",
1788 				 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
1789 		return -EINVAL;
1790 	}
1791 #endif
1792 	if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
1793 		f2fs_warn(sbi, "LFS is not compatible with IPU");
1794 		return -EINVAL;
1795 	}
1796 	return 0;
1797 }
1798 
1799 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1800 {
1801 	struct f2fs_inode_info *fi;
1802 
1803 	if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC))
1804 		return NULL;
1805 
1806 	fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO);
1807 	if (!fi)
1808 		return NULL;
1809 
1810 	init_once((void *) fi);
1811 
1812 	/* Initialize f2fs-specific inode info */
1813 	atomic_set(&fi->dirty_pages, 0);
1814 	atomic_set(&fi->i_compr_blocks, 0);
1815 	atomic_set(&fi->open_count, 0);
1816 	atomic_set(&fi->writeback, 0);
1817 	init_f2fs_rwsem(&fi->i_sem);
1818 	spin_lock_init(&fi->i_size_lock);
1819 	INIT_LIST_HEAD(&fi->dirty_list);
1820 	INIT_LIST_HEAD(&fi->gdirty_list);
1821 	INIT_LIST_HEAD(&fi->gdonate_list);
1822 	init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
1823 	init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
1824 	init_f2fs_rwsem(&fi->i_xattr_sem);
1825 
1826 	/* Will be used by directory only */
1827 	fi->i_dir_level = F2FS_SB(sb)->dir_level;
1828 
1829 	return &fi->vfs_inode;
1830 }
1831 
1832 static int f2fs_drop_inode(struct inode *inode)
1833 {
1834 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1835 	int ret;
1836 
1837 	/*
1838 	 * during filesystem shutdown, if checkpoint is disabled,
1839 	 * drop useless meta/node dirty pages.
1840 	 */
1841 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1842 		if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1843 			inode->i_ino == F2FS_META_INO(sbi)) {
1844 			trace_f2fs_drop_inode(inode, 1);
1845 			return 1;
1846 		}
1847 	}
1848 
1849 	/*
1850 	 * This is to avoid a deadlock condition like below.
1851 	 * writeback_single_inode(inode)
1852 	 *  - f2fs_write_data_page
1853 	 *    - f2fs_gc -> iput -> evict
1854 	 *       - inode_wait_for_writeback(inode)
1855 	 */
1856 	if ((!inode_unhashed(inode) && inode_state_read(inode) & I_SYNC)) {
1857 		if (!inode->i_nlink && !is_bad_inode(inode)) {
1858 			/* to avoid evict_inode call simultaneously */
1859 			__iget(inode);
1860 			spin_unlock(&inode->i_lock);
1861 
1862 			/* should remain fi->extent_tree for writepage */
1863 			f2fs_destroy_extent_node(inode);
1864 
1865 			sb_start_intwrite(inode->i_sb);
1866 			f2fs_i_size_write(inode, 0);
1867 
1868 			f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1869 					inode, NULL, 0, DATA);
1870 			truncate_inode_pages_final(inode->i_mapping);
1871 
1872 			if (F2FS_HAS_BLOCKS(inode))
1873 				f2fs_truncate(inode);
1874 
1875 			sb_end_intwrite(inode->i_sb);
1876 
1877 			spin_lock(&inode->i_lock);
1878 			atomic_dec(&inode->i_count);
1879 		}
1880 		trace_f2fs_drop_inode(inode, 0);
1881 		return 0;
1882 	}
1883 	ret = inode_generic_drop(inode);
1884 	if (!ret)
1885 		ret = fscrypt_drop_inode(inode);
1886 	trace_f2fs_drop_inode(inode, ret);
1887 	return ret;
1888 }
1889 
1890 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1891 {
1892 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1893 	int ret = 0;
1894 
1895 	spin_lock(&sbi->inode_lock[DIRTY_META]);
1896 	if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1897 		ret = 1;
1898 	} else {
1899 		set_inode_flag(inode, FI_DIRTY_INODE);
1900 		stat_inc_dirty_inode(sbi, DIRTY_META);
1901 	}
1902 	if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1903 		list_add_tail(&F2FS_I(inode)->gdirty_list,
1904 				&sbi->inode_list[DIRTY_META]);
1905 		inc_page_count(sbi, F2FS_DIRTY_IMETA);
1906 	}
1907 	spin_unlock(&sbi->inode_lock[DIRTY_META]);
1908 
1909 	/* if atomic write is not committed, set inode w/ atomic dirty */
1910 	if (!ret && f2fs_is_atomic_file(inode) &&
1911 			!is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
1912 		set_inode_flag(inode, FI_ATOMIC_DIRTIED);
1913 
1914 	return ret;
1915 }
1916 
1917 void f2fs_inode_synced(struct inode *inode)
1918 {
1919 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1920 
1921 	spin_lock(&sbi->inode_lock[DIRTY_META]);
1922 	if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1923 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
1924 		return;
1925 	}
1926 	if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1927 		list_del_init(&F2FS_I(inode)->gdirty_list);
1928 		dec_page_count(sbi, F2FS_DIRTY_IMETA);
1929 	}
1930 	clear_inode_flag(inode, FI_DIRTY_INODE);
1931 	clear_inode_flag(inode, FI_AUTO_RECOVER);
1932 	stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1933 	spin_unlock(&sbi->inode_lock[DIRTY_META]);
1934 }
1935 
1936 /*
1937  * f2fs_dirty_inode() is called from __mark_inode_dirty()
1938  *
1939  * We should call set_dirty_inode to write the dirty inode through write_inode.
1940  */
1941 static void f2fs_dirty_inode(struct inode *inode, int flags)
1942 {
1943 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1944 
1945 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1946 			inode->i_ino == F2FS_META_INO(sbi))
1947 		return;
1948 
1949 	if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1950 		clear_inode_flag(inode, FI_AUTO_RECOVER);
1951 
1952 	f2fs_inode_dirtied(inode, false);
1953 }
1954 
1955 static void f2fs_free_inode(struct inode *inode)
1956 {
1957 	fscrypt_free_inode(inode);
1958 	kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1959 }
1960 
1961 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1962 {
1963 	percpu_counter_destroy(&sbi->total_valid_inode_count);
1964 	percpu_counter_destroy(&sbi->rf_node_block_count);
1965 	percpu_counter_destroy(&sbi->alloc_valid_block_count);
1966 }
1967 
1968 static void destroy_device_list(struct f2fs_sb_info *sbi)
1969 {
1970 	int i;
1971 
1972 	for (i = 0; i < sbi->s_ndevs; i++) {
1973 		if (i > 0)
1974 			fs_bdev_file_release(FDEV(i).bdev_file, sbi->sb);
1975 #ifdef CONFIG_BLK_DEV_ZONED
1976 		kvfree(FDEV(i).blkz_seq);
1977 #endif
1978 	}
1979 	kvfree(sbi->devs);
1980 }
1981 
1982 static void f2fs_put_super(struct super_block *sb)
1983 {
1984 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1985 	int i;
1986 	int err = 0;
1987 	bool done;
1988 
1989 	/* unregister procfs/sysfs entries in advance to avoid race case */
1990 	f2fs_unregister_sysfs(sbi);
1991 
1992 	f2fs_quota_off_umount(sb);
1993 
1994 	/* prevent remaining shrinker jobs */
1995 	mutex_lock(&sbi->umount_mutex);
1996 
1997 	/*
1998 	 * flush all issued checkpoints and stop checkpoint issue thread.
1999 	 * after then, all checkpoints should be done by each process context.
2000 	 */
2001 	f2fs_stop_ckpt_thread(sbi);
2002 
2003 	/*
2004 	 * We don't need to do checkpoint when superblock is clean.
2005 	 * But, the previous checkpoint was not done by umount, it needs to do
2006 	 * clean checkpoint again.
2007 	 */
2008 	if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
2009 			!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
2010 		struct cp_control cpc = {
2011 			.reason = CP_UMOUNT,
2012 		};
2013 		stat_inc_cp_call_count(sbi, TOTAL_CALL);
2014 		err = f2fs_write_checkpoint(sbi, &cpc);
2015 	}
2016 
2017 	/* be sure to wait for any on-going discard commands */
2018 	done = f2fs_issue_discard_timeout(sbi, true);
2019 	if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
2020 		struct cp_control cpc = {
2021 			.reason = CP_UMOUNT | CP_TRIMMED,
2022 		};
2023 		stat_inc_cp_call_count(sbi, TOTAL_CALL);
2024 		err = f2fs_write_checkpoint(sbi, &cpc);
2025 	}
2026 
2027 	/*
2028 	 * normally superblock is clean, so we need to release this.
2029 	 * In addition, EIO will skip do checkpoint, we need this as well.
2030 	 */
2031 	f2fs_release_ino_entry(sbi, true);
2032 
2033 	f2fs_leave_shrinker(sbi);
2034 	mutex_unlock(&sbi->umount_mutex);
2035 
2036 	/* our cp_error case, we can wait for any writeback page */
2037 	f2fs_flush_merged_writes(sbi);
2038 
2039 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
2040 
2041 	if (err || f2fs_cp_error(sbi)) {
2042 		truncate_inode_pages_final(NODE_MAPPING(sbi));
2043 		truncate_inode_pages_final(META_MAPPING(sbi));
2044 	}
2045 
2046 	f2fs_bug_on(sbi, sbi->fsync_node_num);
2047 
2048 	f2fs_destroy_compress_inode(sbi);
2049 
2050 	iput(sbi->node_inode);
2051 	sbi->node_inode = NULL;
2052 
2053 	iput(sbi->meta_inode);
2054 	sbi->meta_inode = NULL;
2055 
2056 	/* Should check the page counts after dropping all node/meta pages */
2057 	for (i = 0; i < NR_COUNT_TYPE; i++) {
2058 		if (!get_pages(sbi, i))
2059 			continue;
2060 		f2fs_err(sbi, "detect filesystem reference count leak during "
2061 			"umount, type: %d, count: %lld", i, get_pages(sbi, i));
2062 		f2fs_bug_on(sbi, 1);
2063 	}
2064 
2065 	/*
2066 	 * iput() can update stat information, if f2fs_write_checkpoint()
2067 	 * above failed with error.
2068 	 */
2069 	f2fs_destroy_stats(sbi);
2070 
2071 	/* destroy f2fs internal modules */
2072 	f2fs_destroy_node_manager(sbi);
2073 	f2fs_destroy_segment_manager(sbi);
2074 
2075 	/* flush s_error_work before sbi destroy */
2076 	flush_work(&sbi->s_error_work);
2077 
2078 	f2fs_destroy_wq(sbi);
2079 
2080 	kvfree(sbi->ckpt);
2081 
2082 	kfree(sbi->raw_super);
2083 
2084 	f2fs_destroy_page_array_cache(sbi);
2085 #ifdef CONFIG_QUOTA
2086 	for (i = 0; i < MAXQUOTAS; i++)
2087 		kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2088 #endif
2089 	fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
2090 	destroy_percpu_info(sbi);
2091 	f2fs_destroy_iostat(sbi);
2092 	for (i = 0; i < NR_PAGE_TYPE; i++)
2093 		kfree(sbi->write_io[i]);
2094 #if IS_ENABLED(CONFIG_UNICODE)
2095 	utf8_unload(sb->s_encoding);
2096 #endif
2097 	sync_blockdev(sb->s_bdev);
2098 	invalidate_bdev(sb->s_bdev);
2099 	for (i = 1; i < sbi->s_ndevs; i++) {
2100 		sync_blockdev(FDEV(i).bdev);
2101 		invalidate_bdev(FDEV(i).bdev);
2102 	}
2103 }
2104 
2105 int f2fs_sync_fs(struct super_block *sb, int sync)
2106 {
2107 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2108 	int err = 0;
2109 
2110 	if (unlikely(f2fs_cp_error(sbi)))
2111 		return 0;
2112 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2113 		return 0;
2114 
2115 	trace_f2fs_sync_fs(sb, sync);
2116 
2117 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2118 		return -EAGAIN;
2119 
2120 	if (sync) {
2121 		stat_inc_cp_call_count(sbi, TOTAL_CALL);
2122 		err = f2fs_issue_checkpoint(sbi);
2123 	}
2124 
2125 	return err;
2126 }
2127 
2128 static int f2fs_freeze(struct super_block *sb)
2129 {
2130 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2131 
2132 	if (f2fs_readonly(sb))
2133 		return 0;
2134 
2135 	/* IO error happened before */
2136 	if (unlikely(f2fs_cp_error(sbi)))
2137 		return -EIO;
2138 
2139 	/* must be clean, since sync_filesystem() was already called */
2140 	if (is_sbi_flag_set(sbi, SBI_IS_DIRTY))
2141 		return -EINVAL;
2142 
2143 	sbi->umount_lock_holder = current;
2144 
2145 	/* Let's flush checkpoints and stop the thread. */
2146 	f2fs_flush_ckpt_thread(sbi);
2147 
2148 	sbi->umount_lock_holder = NULL;
2149 
2150 	/* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
2151 	set_sbi_flag(sbi, SBI_IS_FREEZING);
2152 	return 0;
2153 }
2154 
2155 static int f2fs_unfreeze(struct super_block *sb)
2156 {
2157 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2158 
2159 	/*
2160 	 * It will update discard_max_bytes of mounted lvm device to zero
2161 	 * after creating snapshot on this lvm device, let's drop all
2162 	 * remained discards.
2163 	 * We don't need to disable real-time discard because discard_max_bytes
2164 	 * will recover after removal of snapshot.
2165 	 */
2166 	if (test_opt(sbi, DISCARD) && !f2fs_hw_support_discard(sbi))
2167 		f2fs_issue_discard_timeout(sbi, true);
2168 
2169 	clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
2170 	return 0;
2171 }
2172 
2173 #ifdef CONFIG_QUOTA
2174 static int f2fs_statfs_project(struct super_block *sb,
2175 				kprojid_t projid, struct kstatfs *buf)
2176 {
2177 	struct kqid qid;
2178 	struct dquot *dquot;
2179 	u64 limit;
2180 	u64 curblock;
2181 
2182 	qid = make_kqid_projid(projid);
2183 	dquot = dqget(sb, qid);
2184 	if (IS_ERR(dquot))
2185 		return PTR_ERR(dquot);
2186 	spin_lock(&dquot->dq_dqb_lock);
2187 
2188 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
2189 					dquot->dq_dqb.dqb_bhardlimit);
2190 	limit >>= sb->s_blocksize_bits;
2191 
2192 	if (limit) {
2193 		uint64_t remaining = 0;
2194 
2195 		curblock = (dquot->dq_dqb.dqb_curspace +
2196 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
2197 		if (limit > curblock)
2198 			remaining = limit - curblock;
2199 
2200 		buf->f_blocks = min(buf->f_blocks, limit);
2201 		buf->f_bfree = min(buf->f_bfree, remaining);
2202 		buf->f_bavail = min(buf->f_bavail, remaining);
2203 	}
2204 
2205 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
2206 					dquot->dq_dqb.dqb_ihardlimit);
2207 
2208 	if (limit) {
2209 		uint64_t remaining = 0;
2210 
2211 		if (limit > dquot->dq_dqb.dqb_curinodes)
2212 			remaining = limit - dquot->dq_dqb.dqb_curinodes;
2213 
2214 		buf->f_files = min(buf->f_files, limit);
2215 		buf->f_ffree = min(buf->f_ffree, remaining);
2216 	}
2217 
2218 	spin_unlock(&dquot->dq_dqb_lock);
2219 	dqput(dquot);
2220 	return 0;
2221 }
2222 #endif
2223 
2224 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
2225 {
2226 	struct super_block *sb = dentry->d_sb;
2227 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2228 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
2229 	block_t total_count, user_block_count, start_count;
2230 	u64 avail_node_count;
2231 	unsigned int total_valid_node_count;
2232 
2233 	total_count = le64_to_cpu(sbi->raw_super->block_count);
2234 	start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
2235 	buf->f_type = F2FS_SUPER_MAGIC;
2236 	buf->f_bsize = sbi->blocksize;
2237 
2238 	buf->f_blocks = total_count - start_count;
2239 
2240 	spin_lock(&sbi->stat_lock);
2241 	if (sbi->carve_out)
2242 		buf->f_blocks -= sbi->current_reserved_blocks;
2243 	user_block_count = sbi->user_block_count;
2244 	total_valid_node_count = valid_node_count(sbi);
2245 	avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
2246 	buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
2247 						sbi->current_reserved_blocks;
2248 
2249 	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
2250 		buf->f_bfree = 0;
2251 	else
2252 		buf->f_bfree -= sbi->unusable_block_count;
2253 	spin_unlock(&sbi->stat_lock);
2254 
2255 	if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
2256 		buf->f_bavail = buf->f_bfree -
2257 				F2FS_OPTION(sbi).root_reserved_blocks;
2258 	else
2259 		buf->f_bavail = 0;
2260 
2261 	if (avail_node_count > user_block_count) {
2262 		buf->f_files = user_block_count;
2263 		buf->f_ffree = buf->f_bavail;
2264 	} else {
2265 		buf->f_files = avail_node_count;
2266 		buf->f_ffree = min(avail_node_count - total_valid_node_count,
2267 					buf->f_bavail);
2268 	}
2269 
2270 	buf->f_namelen = F2FS_NAME_LEN;
2271 	buf->f_fsid    = u64_to_fsid(id);
2272 
2273 #ifdef CONFIG_QUOTA
2274 	if (is_inode_flag_set(d_inode(dentry), FI_PROJ_INHERIT) &&
2275 			sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
2276 		f2fs_statfs_project(sb, F2FS_I(d_inode(dentry))->i_projid, buf);
2277 	}
2278 #endif
2279 	return 0;
2280 }
2281 
2282 static inline void f2fs_show_quota_options(struct seq_file *seq,
2283 					   struct super_block *sb)
2284 {
2285 #ifdef CONFIG_QUOTA
2286 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2287 
2288 	if (F2FS_OPTION(sbi).s_jquota_fmt) {
2289 		char *fmtname = "";
2290 
2291 		switch (F2FS_OPTION(sbi).s_jquota_fmt) {
2292 		case QFMT_VFS_OLD:
2293 			fmtname = "vfsold";
2294 			break;
2295 		case QFMT_VFS_V0:
2296 			fmtname = "vfsv0";
2297 			break;
2298 		case QFMT_VFS_V1:
2299 			fmtname = "vfsv1";
2300 			break;
2301 		}
2302 		seq_printf(seq, ",jqfmt=%s", fmtname);
2303 	}
2304 
2305 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
2306 		seq_show_option(seq, "usrjquota",
2307 			F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
2308 
2309 	if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
2310 		seq_show_option(seq, "grpjquota",
2311 			F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
2312 
2313 	if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
2314 		seq_show_option(seq, "prjjquota",
2315 			F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
2316 #endif
2317 }
2318 
2319 #ifdef CONFIG_F2FS_FS_COMPRESSION
2320 static inline void f2fs_show_compress_options(struct seq_file *seq,
2321 							struct super_block *sb)
2322 {
2323 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2324 	char *algtype = "";
2325 	int i;
2326 
2327 	if (!f2fs_sb_has_compression(sbi))
2328 		return;
2329 
2330 	switch (F2FS_OPTION(sbi).compress_algorithm) {
2331 	case COMPRESS_LZO:
2332 		algtype = "lzo";
2333 		break;
2334 	case COMPRESS_LZ4:
2335 		algtype = "lz4";
2336 		break;
2337 	case COMPRESS_ZSTD:
2338 		algtype = "zstd";
2339 		break;
2340 	case COMPRESS_LZORLE:
2341 		algtype = "lzo-rle";
2342 		break;
2343 	}
2344 	seq_printf(seq, ",compress_algorithm=%s", algtype);
2345 
2346 	if (F2FS_OPTION(sbi).compress_level)
2347 		seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
2348 
2349 	seq_printf(seq, ",compress_log_size=%u",
2350 			F2FS_OPTION(sbi).compress_log_size);
2351 
2352 	for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
2353 		seq_printf(seq, ",compress_extension=%s",
2354 			F2FS_OPTION(sbi).extensions[i]);
2355 	}
2356 
2357 	for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
2358 		seq_printf(seq, ",nocompress_extension=%s",
2359 			F2FS_OPTION(sbi).noextensions[i]);
2360 	}
2361 
2362 	if (F2FS_OPTION(sbi).compress_chksum)
2363 		seq_puts(seq, ",compress_chksum");
2364 
2365 	if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
2366 		seq_printf(seq, ",compress_mode=%s", "fs");
2367 	else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
2368 		seq_printf(seq, ",compress_mode=%s", "user");
2369 
2370 	if (test_opt(sbi, COMPRESS_CACHE))
2371 		seq_puts(seq, ",compress_cache");
2372 }
2373 #endif
2374 
2375 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
2376 {
2377 	struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
2378 
2379 	if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
2380 		seq_printf(seq, ",background_gc=%s", "sync");
2381 	else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
2382 		seq_printf(seq, ",background_gc=%s", "on");
2383 	else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
2384 		seq_printf(seq, ",background_gc=%s", "off");
2385 
2386 	if (test_opt(sbi, GC_MERGE))
2387 		seq_puts(seq, ",gc_merge");
2388 	else
2389 		seq_puts(seq, ",nogc_merge");
2390 
2391 	if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2392 		seq_puts(seq, ",disable_roll_forward");
2393 	if (test_opt(sbi, NORECOVERY))
2394 		seq_puts(seq, ",norecovery");
2395 	if (test_opt(sbi, DISCARD)) {
2396 		seq_puts(seq, ",discard");
2397 		if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
2398 			seq_printf(seq, ",discard_unit=%s", "block");
2399 		else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2400 			seq_printf(seq, ",discard_unit=%s", "segment");
2401 		else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2402 			seq_printf(seq, ",discard_unit=%s", "section");
2403 	} else {
2404 		seq_puts(seq, ",nodiscard");
2405 	}
2406 #ifdef CONFIG_F2FS_FS_XATTR
2407 	if (test_opt(sbi, XATTR_USER))
2408 		seq_puts(seq, ",user_xattr");
2409 	else
2410 		seq_puts(seq, ",nouser_xattr");
2411 	if (test_opt(sbi, INLINE_XATTR))
2412 		seq_puts(seq, ",inline_xattr");
2413 	else
2414 		seq_puts(seq, ",noinline_xattr");
2415 	if (test_opt(sbi, INLINE_XATTR_SIZE))
2416 		seq_printf(seq, ",inline_xattr_size=%u",
2417 					F2FS_OPTION(sbi).inline_xattr_size);
2418 #endif
2419 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2420 	if (test_opt(sbi, POSIX_ACL))
2421 		seq_puts(seq, ",acl");
2422 	else
2423 		seq_puts(seq, ",noacl");
2424 #endif
2425 	if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
2426 		seq_puts(seq, ",disable_ext_identify");
2427 	if (test_opt(sbi, INLINE_DATA))
2428 		seq_puts(seq, ",inline_data");
2429 	else
2430 		seq_puts(seq, ",noinline_data");
2431 	if (test_opt(sbi, INLINE_DENTRY))
2432 		seq_puts(seq, ",inline_dentry");
2433 	else
2434 		seq_puts(seq, ",noinline_dentry");
2435 	if (test_opt(sbi, FLUSH_MERGE))
2436 		seq_puts(seq, ",flush_merge");
2437 	else
2438 		seq_puts(seq, ",noflush_merge");
2439 	if (test_opt(sbi, NOBARRIER))
2440 		seq_puts(seq, ",nobarrier");
2441 	else
2442 		seq_puts(seq, ",barrier");
2443 	if (test_opt(sbi, FASTBOOT))
2444 		seq_puts(seq, ",fastboot");
2445 	if (test_opt(sbi, READ_EXTENT_CACHE))
2446 		seq_puts(seq, ",extent_cache");
2447 	else
2448 		seq_puts(seq, ",noextent_cache");
2449 	if (test_opt(sbi, AGE_EXTENT_CACHE))
2450 		seq_puts(seq, ",age_extent_cache");
2451 	if (test_opt(sbi, DATA_FLUSH))
2452 		seq_puts(seq, ",data_flush");
2453 
2454 	seq_puts(seq, ",mode=");
2455 	if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
2456 		seq_puts(seq, "adaptive");
2457 	else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
2458 		seq_puts(seq, "lfs");
2459 	else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
2460 		seq_puts(seq, "fragment:segment");
2461 	else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2462 		seq_puts(seq, "fragment:block");
2463 	seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
2464 	if (test_opt(sbi, RESERVE_ROOT) || test_opt(sbi, RESERVE_NODE))
2465 		seq_printf(seq, ",reserve_root=%u,reserve_node=%u,resuid=%u,"
2466 				"resgid=%u",
2467 				F2FS_OPTION(sbi).root_reserved_blocks,
2468 				F2FS_OPTION(sbi).root_reserved_nodes,
2469 				from_kuid_munged(&init_user_ns,
2470 					F2FS_OPTION(sbi).s_resuid),
2471 				from_kgid_munged(&init_user_ns,
2472 					F2FS_OPTION(sbi).s_resgid));
2473 #ifdef CONFIG_F2FS_FAULT_INJECTION
2474 	if (test_opt(sbi, FAULT_INJECTION)) {
2475 		seq_printf(seq, ",fault_injection=%u",
2476 				F2FS_OPTION(sbi).fault_info.inject_rate);
2477 		seq_printf(seq, ",fault_type=%u",
2478 				F2FS_OPTION(sbi).fault_info.inject_type);
2479 	}
2480 #endif
2481 #ifdef CONFIG_QUOTA
2482 	if (test_opt(sbi, QUOTA))
2483 		seq_puts(seq, ",quota");
2484 	if (test_opt(sbi, USRQUOTA))
2485 		seq_puts(seq, ",usrquota");
2486 	if (test_opt(sbi, GRPQUOTA))
2487 		seq_puts(seq, ",grpquota");
2488 	if (test_opt(sbi, PRJQUOTA))
2489 		seq_puts(seq, ",prjquota");
2490 #endif
2491 	f2fs_show_quota_options(seq, sbi->sb);
2492 
2493 	fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
2494 
2495 	if (sbi->sb->s_flags & SB_INLINECRYPT)
2496 		seq_puts(seq, ",inlinecrypt");
2497 
2498 	if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
2499 		seq_printf(seq, ",alloc_mode=%s", "default");
2500 	else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2501 		seq_printf(seq, ",alloc_mode=%s", "reuse");
2502 
2503 	if (test_opt(sbi, DISABLE_CHECKPOINT))
2504 		seq_printf(seq, ",checkpoint=disable:%u",
2505 				F2FS_OPTION(sbi).unusable_cap);
2506 	if (test_opt(sbi, MERGE_CHECKPOINT))
2507 		seq_puts(seq, ",checkpoint_merge");
2508 	else
2509 		seq_puts(seq, ",nocheckpoint_merge");
2510 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
2511 		seq_printf(seq, ",fsync_mode=%s", "posix");
2512 	else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
2513 		seq_printf(seq, ",fsync_mode=%s", "strict");
2514 	else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
2515 		seq_printf(seq, ",fsync_mode=%s", "nobarrier");
2516 
2517 #ifdef CONFIG_F2FS_FS_COMPRESSION
2518 	f2fs_show_compress_options(seq, sbi->sb);
2519 #endif
2520 
2521 	if (test_opt(sbi, ATGC))
2522 		seq_puts(seq, ",atgc");
2523 
2524 	if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
2525 		seq_printf(seq, ",memory=%s", "normal");
2526 	else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
2527 		seq_printf(seq, ",memory=%s", "low");
2528 
2529 	if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2530 		seq_printf(seq, ",errors=%s", "remount-ro");
2531 	else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
2532 		seq_printf(seq, ",errors=%s", "continue");
2533 	else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
2534 		seq_printf(seq, ",errors=%s", "panic");
2535 
2536 	if (test_opt(sbi, NAT_BITS))
2537 		seq_puts(seq, ",nat_bits");
2538 
2539 	if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_PERF)
2540 		seq_show_option(seq, "lookup_mode", "perf");
2541 	else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_COMPAT)
2542 		seq_show_option(seq, "lookup_mode", "compat");
2543 	else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_AUTO)
2544 		seq_show_option(seq, "lookup_mode", "auto");
2545 
2546 	return 0;
2547 }
2548 
2549 static void default_options(struct f2fs_sb_info *sbi, bool remount)
2550 {
2551 	/* init some FS parameters */
2552 	if (!remount) {
2553 		set_opt(sbi, READ_EXTENT_CACHE);
2554 		clear_opt(sbi, DISABLE_CHECKPOINT);
2555 
2556 		if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2557 			set_opt(sbi, DISCARD);
2558 
2559 		if (f2fs_sb_has_blkzoned(sbi))
2560 			F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2561 		else
2562 			F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2563 	}
2564 
2565 	if (f2fs_sb_has_readonly(sbi))
2566 		F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
2567 	else
2568 		F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
2569 
2570 	F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
2571 	if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
2572 							SMALL_VOLUME_SEGMENTS)
2573 		F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2574 	else
2575 		F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
2576 	F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
2577 	F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2578 	F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2579 	if (f2fs_sb_has_compression(sbi)) {
2580 		F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
2581 		F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
2582 		F2FS_OPTION(sbi).compress_ext_cnt = 0;
2583 		F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
2584 	}
2585 	F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
2586 	F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
2587 	F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
2588 
2589 	set_opt(sbi, INLINE_XATTR);
2590 	set_opt(sbi, INLINE_DATA);
2591 	set_opt(sbi, INLINE_DENTRY);
2592 	set_opt(sbi, MERGE_CHECKPOINT);
2593 	set_opt(sbi, LAZYTIME);
2594 	F2FS_OPTION(sbi).unusable_cap = 0;
2595 	if (!f2fs_is_readonly(sbi))
2596 		set_opt(sbi, FLUSH_MERGE);
2597 	if (f2fs_sb_has_blkzoned(sbi))
2598 		F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
2599 	else
2600 		F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
2601 
2602 #ifdef CONFIG_F2FS_FS_XATTR
2603 	set_opt(sbi, XATTR_USER);
2604 #endif
2605 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2606 	set_opt(sbi, POSIX_ACL);
2607 #endif
2608 
2609 	f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL);
2610 
2611 	F2FS_OPTION(sbi).lookup_mode = LOOKUP_PERF;
2612 }
2613 
2614 #ifdef CONFIG_QUOTA
2615 static int f2fs_enable_quotas(struct super_block *sb);
2616 #endif
2617 
2618 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
2619 {
2620 	unsigned int s_flags = sbi->sb->s_flags;
2621 	struct cp_control cpc;
2622 	struct f2fs_lock_context lc;
2623 	unsigned int gc_mode = sbi->gc_mode;
2624 	int err = 0;
2625 	int ret;
2626 	block_t unusable;
2627 
2628 	if (s_flags & SB_RDONLY) {
2629 		f2fs_err(sbi, "checkpoint=disable on readonly fs");
2630 		return -EINVAL;
2631 	}
2632 	sbi->sb->s_flags |= SB_ACTIVE;
2633 
2634 	/* check if we need more GC first */
2635 	unusable = f2fs_get_unusable_blocks(sbi);
2636 
2637 	f2fs_info(sbi, "%s starts, unusable: %u", __func__, unusable);
2638 
2639 	if (!f2fs_disable_cp_again(sbi, unusable))
2640 		goto skip_gc;
2641 
2642 	f2fs_update_time(sbi, DISABLE_TIME);
2643 
2644 	sbi->gc_mode = GC_URGENT_HIGH;
2645 
2646 	f2fs_info(sbi, "%s: run f2fs_gc() to migrate blocks", __func__);
2647 
2648 	while (!f2fs_time_over(sbi, DISABLE_TIME)) {
2649 		struct f2fs_gc_control gc_control = {
2650 			.victim_segno = NULL_SEGNO,
2651 			.init_gc_type = FG_GC,
2652 			.should_migrate_blocks = false,
2653 			.err_gc_skipped = true,
2654 			.no_bg_gc = true,
2655 			.nr_free_secs = 1 };
2656 
2657 		f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
2658 		stat_inc_gc_call_count(sbi, FOREGROUND);
2659 		err = f2fs_gc(sbi, &gc_control);
2660 		if (err == -ENODATA) {
2661 			err = 0;
2662 			break;
2663 		}
2664 		if (err && err != -EAGAIN)
2665 			break;
2666 	}
2667 
2668 	f2fs_info(sbi, "%s: call sync_filesystem() to persist meta: %lld, node: %lld, data: %lld",
2669 			__func__,
2670 			get_pages(sbi, F2FS_DIRTY_META),
2671 			get_pages(sbi, F2FS_DIRTY_NODES),
2672 			get_pages(sbi, F2FS_DIRTY_DATA));
2673 
2674 	ret = sync_filesystem(sbi->sb);
2675 	if (ret || err) {
2676 		err = ret ? ret : err;
2677 		goto restore_flag;
2678 	}
2679 
2680 	unusable = f2fs_get_unusable_blocks(sbi);
2681 	if (f2fs_disable_cp_again(sbi, unusable)) {
2682 		err = -EAGAIN;
2683 		goto restore_flag;
2684 	}
2685 
2686 skip_gc:
2687 	f2fs_info(sbi, "%s: call f2fs_write_checkpoint(), meta: %lld, node: %lld, data: %lld",
2688 			__func__,
2689 			get_pages(sbi, F2FS_DIRTY_META),
2690 			get_pages(sbi, F2FS_DIRTY_NODES),
2691 			get_pages(sbi, F2FS_DIRTY_DATA));
2692 
2693 	f2fs_down_write_trace(&sbi->gc_lock, &lc);
2694 	cpc.reason = CP_PAUSE;
2695 	set_sbi_flag(sbi, SBI_CP_DISABLED);
2696 	stat_inc_cp_call_count(sbi, TOTAL_CALL);
2697 	err = f2fs_write_checkpoint(sbi, &cpc);
2698 	if (err)
2699 		goto out_unlock;
2700 
2701 	spin_lock(&sbi->stat_lock);
2702 	sbi->unusable_block_count = unusable;
2703 	spin_unlock(&sbi->stat_lock);
2704 
2705 out_unlock:
2706 	f2fs_up_write_trace(&sbi->gc_lock, &lc);
2707 restore_flag:
2708 	sbi->gc_mode = gc_mode;
2709 	sbi->sb->s_flags = s_flags;	/* Restore SB_RDONLY status */
2710 	f2fs_info(sbi, "%s finishes, err:%d", __func__, err);
2711 	return err;
2712 }
2713 
2714 static int f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
2715 {
2716 	int retry = MAX_FLUSH_RETRY_COUNT;
2717 	long long start, writeback, end;
2718 	int ret;
2719 	struct f2fs_lock_context lc;
2720 	long long skipped_write, dirty_data;
2721 
2722 	f2fs_info(sbi, "f2fs_enable_checkpoint() starts, meta: %lld, node: %lld, data: %lld",
2723 					get_pages(sbi, F2FS_DIRTY_META),
2724 					get_pages(sbi, F2FS_DIRTY_NODES),
2725 					get_pages(sbi, F2FS_DIRTY_DATA));
2726 
2727 	start = ktime_get();
2728 
2729 	set_sbi_flag(sbi, SBI_ENABLE_CHECKPOINT);
2730 
2731 	/* we should flush all the data to keep data consistency */
2732 	do {
2733 		skipped_write = get_pages(sbi, F2FS_SKIPPED_WRITE);
2734 		dirty_data = get_pages(sbi, F2FS_DIRTY_DATA);
2735 
2736 		sync_inodes_sb(sbi->sb);
2737 		f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
2738 
2739 		f2fs_info(sbi, "sync_inode_sb done, dirty_data: %lld, %lld, "
2740 				"skipped write: %lld, %lld, retry: %d",
2741 				get_pages(sbi, F2FS_DIRTY_DATA),
2742 				dirty_data,
2743 				get_pages(sbi, F2FS_SKIPPED_WRITE),
2744 				skipped_write, retry);
2745 
2746 		/*
2747 		 * sync_inodes_sb() has retry logic, so let's check dirty_data
2748 		 * in prior to skipped_write in case there is no dirty data.
2749 		 */
2750 		if (!get_pages(sbi, F2FS_DIRTY_DATA))
2751 			break;
2752 		if (get_pages(sbi, F2FS_SKIPPED_WRITE) == skipped_write)
2753 			break;
2754 	} while (retry--);
2755 
2756 	clear_sbi_flag(sbi, SBI_ENABLE_CHECKPOINT);
2757 
2758 	writeback = ktime_get();
2759 
2760 	if (unlikely(get_pages(sbi, F2FS_DIRTY_DATA) ||
2761 			get_pages(sbi, F2FS_SKIPPED_WRITE)))
2762 		f2fs_warn(sbi, "checkpoint=enable unwritten data: %lld, skipped data: %lld, retry: %d",
2763 				get_pages(sbi, F2FS_DIRTY_DATA),
2764 				get_pages(sbi, F2FS_SKIPPED_WRITE), retry);
2765 
2766 	if (get_pages(sbi, F2FS_SKIPPED_WRITE))
2767 		atomic_set(&sbi->nr_pages[F2FS_SKIPPED_WRITE], 0);
2768 
2769 	f2fs_down_write_trace(&sbi->gc_lock, &lc);
2770 	f2fs_dirty_to_prefree(sbi);
2771 
2772 	clear_sbi_flag(sbi, SBI_CP_DISABLED);
2773 	set_sbi_flag(sbi, SBI_IS_DIRTY);
2774 	f2fs_up_write_trace(&sbi->gc_lock, &lc);
2775 
2776 	ret = f2fs_sync_fs(sbi->sb, 1);
2777 	if (ret)
2778 		f2fs_err(sbi, "%s sync_fs failed, ret: %d", __func__, ret);
2779 
2780 	/* Let's ensure there's no pending checkpoint anymore */
2781 	f2fs_flush_ckpt_thread(sbi);
2782 
2783 	end = ktime_get();
2784 
2785 	f2fs_info(sbi, "f2fs_enable_checkpoint() finishes, writeback:%llu, sync:%llu",
2786 					ktime_ms_delta(writeback, start),
2787 					ktime_ms_delta(end, writeback));
2788 	return ret;
2789 }
2790 
2791 static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
2792 {
2793 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2794 	struct f2fs_mount_info org_mount_opt;
2795 	unsigned long old_sb_flags;
2796 	unsigned int flags = fc->sb_flags;
2797 	int err;
2798 	bool need_restart_gc = false, need_stop_gc = false;
2799 	bool need_restart_flush = false, need_stop_flush = false;
2800 	bool need_restart_discard = false, need_stop_discard = false;
2801 	bool need_enable_checkpoint = false, need_disable_checkpoint = false;
2802 	bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
2803 	bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
2804 	bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
2805 	bool no_atgc = !test_opt(sbi, ATGC);
2806 	bool no_discard = !test_opt(sbi, DISCARD);
2807 	bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
2808 	bool block_unit_discard = f2fs_block_unit_discard(sbi);
2809 	bool no_nat_bits = !test_opt(sbi, NAT_BITS);
2810 #ifdef CONFIG_QUOTA
2811 	int i, j;
2812 #endif
2813 
2814 	/*
2815 	 * Save the old mount options in case we
2816 	 * need to restore them.
2817 	 */
2818 	org_mount_opt = sbi->mount_opt;
2819 	old_sb_flags = sb->s_flags;
2820 
2821 	sbi->umount_lock_holder = current;
2822 
2823 #ifdef CONFIG_QUOTA
2824 	org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
2825 	for (i = 0; i < MAXQUOTAS; i++) {
2826 		if (F2FS_OPTION(sbi).s_qf_names[i]) {
2827 			org_mount_opt.s_qf_names[i] =
2828 				kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
2829 				GFP_KERNEL);
2830 			if (!org_mount_opt.s_qf_names[i]) {
2831 				for (j = 0; j < i; j++)
2832 					kfree(org_mount_opt.s_qf_names[j]);
2833 				return -ENOMEM;
2834 			}
2835 		} else {
2836 			org_mount_opt.s_qf_names[i] = NULL;
2837 		}
2838 	}
2839 #endif
2840 
2841 	/* recover superblocks we couldn't write due to previous RO mount */
2842 	if (!(flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
2843 		err = f2fs_commit_super(sbi, false);
2844 		f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2845 			  err);
2846 		if (!err)
2847 			clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2848 	}
2849 
2850 	default_options(sbi, true);
2851 
2852 	err = f2fs_check_opt_consistency(fc, sb);
2853 	if (err)
2854 		goto restore_opts;
2855 
2856 	f2fs_apply_options(fc, sb);
2857 
2858 	err = f2fs_sanity_check_options(sbi, true);
2859 	if (err)
2860 		goto restore_opts;
2861 
2862 	/* flush outstanding errors before changing fs state */
2863 	flush_work(&sbi->s_error_work);
2864 
2865 	/*
2866 	 * Previous and new state of filesystem is RO,
2867 	 * so skip checking GC and FLUSH_MERGE conditions.
2868 	 */
2869 	if (f2fs_readonly(sb) && (flags & SB_RDONLY))
2870 		goto skip;
2871 
2872 	if (f2fs_dev_is_readonly(sbi) && !(flags & SB_RDONLY)) {
2873 		err = -EROFS;
2874 		goto restore_opts;
2875 	}
2876 
2877 #ifdef CONFIG_QUOTA
2878 	if (!f2fs_readonly(sb) && (flags & SB_RDONLY)) {
2879 		err = dquot_suspend(sb, -1);
2880 		if (err < 0)
2881 			goto restore_opts;
2882 	} else if (f2fs_readonly(sb) && !(flags & SB_RDONLY)) {
2883 		/* dquot_resume needs RW */
2884 		sb->s_flags &= ~SB_RDONLY;
2885 		if (sb_any_quota_suspended(sb)) {
2886 			dquot_resume(sb, -1);
2887 		} else if (f2fs_sb_has_quota_ino(sbi)) {
2888 			err = f2fs_enable_quotas(sb);
2889 			if (err)
2890 				goto restore_opts;
2891 		}
2892 	}
2893 #endif
2894 	/* disallow enable atgc dynamically */
2895 	if (no_atgc == !!test_opt(sbi, ATGC)) {
2896 		err = -EINVAL;
2897 		f2fs_warn(sbi, "switch atgc option is not allowed");
2898 		goto restore_opts;
2899 	}
2900 
2901 	/* disallow enable/disable extent_cache dynamically */
2902 	if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
2903 		err = -EINVAL;
2904 		f2fs_warn(sbi, "switch extent_cache option is not allowed");
2905 		goto restore_opts;
2906 	}
2907 	/* disallow enable/disable age extent_cache dynamically */
2908 	if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2909 		err = -EINVAL;
2910 		f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
2911 		goto restore_opts;
2912 	}
2913 
2914 	if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2915 		err = -EINVAL;
2916 		f2fs_warn(sbi, "switch compress_cache option is not allowed");
2917 		goto restore_opts;
2918 	}
2919 
2920 	if (block_unit_discard != f2fs_block_unit_discard(sbi)) {
2921 		err = -EINVAL;
2922 		f2fs_warn(sbi, "switch discard_unit option is not allowed");
2923 		goto restore_opts;
2924 	}
2925 
2926 	if (no_nat_bits == !!test_opt(sbi, NAT_BITS)) {
2927 		err = -EINVAL;
2928 		f2fs_warn(sbi, "switch nat_bits option is not allowed");
2929 		goto restore_opts;
2930 	}
2931 
2932 	if ((flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2933 		err = -EINVAL;
2934 		f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
2935 		goto restore_opts;
2936 	}
2937 
2938 	/*
2939 	 * We stop the GC thread if FS is mounted as RO
2940 	 * or if background_gc = off is passed in mount
2941 	 * option. Also sync the filesystem.
2942 	 */
2943 	if ((flags & SB_RDONLY) ||
2944 			(F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2945 			!test_opt(sbi, GC_MERGE))) {
2946 		if (sbi->gc_thread) {
2947 			f2fs_stop_gc_thread(sbi);
2948 			need_restart_gc = true;
2949 		}
2950 	} else if (!sbi->gc_thread) {
2951 		err = f2fs_start_gc_thread(sbi);
2952 		if (err)
2953 			goto restore_opts;
2954 		need_stop_gc = true;
2955 	}
2956 
2957 	if (flags & SB_RDONLY) {
2958 		sync_inodes_sb(sb);
2959 
2960 		set_sbi_flag(sbi, SBI_IS_DIRTY);
2961 		set_sbi_flag(sbi, SBI_IS_CLOSE);
2962 		f2fs_sync_fs(sb, 1);
2963 		clear_sbi_flag(sbi, SBI_IS_CLOSE);
2964 	}
2965 
2966 	/*
2967 	 * We stop issue flush thread if FS is mounted as RO
2968 	 * or if flush_merge is not passed in mount option.
2969 	 */
2970 	if ((flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2971 		clear_opt(sbi, FLUSH_MERGE);
2972 		f2fs_destroy_flush_cmd_control(sbi, false);
2973 		need_restart_flush = true;
2974 	} else {
2975 		err = f2fs_create_flush_cmd_control(sbi);
2976 		if (err)
2977 			goto restore_gc;
2978 		need_stop_flush = true;
2979 	}
2980 
2981 	if (no_discard == !!test_opt(sbi, DISCARD)) {
2982 		if (test_opt(sbi, DISCARD)) {
2983 			err = f2fs_start_discard_thread(sbi);
2984 			if (err)
2985 				goto restore_flush;
2986 			need_stop_discard = true;
2987 		} else {
2988 			f2fs_stop_discard_thread(sbi);
2989 			/*
2990 			 * f2fs_ioc_fitrim() won't race w/ "remount ro"
2991 			 * so it's safe to check discard_cmd_cnt in
2992 			 * f2fs_issue_discard_timeout().
2993 			 */
2994 			f2fs_issue_discard_timeout(sbi, flags & SB_RDONLY);
2995 			need_restart_discard = true;
2996 		}
2997 	}
2998 
2999 	adjust_unusable_cap_perc(sbi);
3000 	if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
3001 		if (test_opt(sbi, DISABLE_CHECKPOINT)) {
3002 			err = f2fs_disable_checkpoint(sbi);
3003 			if (err)
3004 				goto restore_discard;
3005 			need_enable_checkpoint = true;
3006 		} else {
3007 			err = f2fs_enable_checkpoint(sbi);
3008 			if (err)
3009 				goto restore_discard;
3010 			need_disable_checkpoint = true;
3011 		}
3012 	}
3013 
3014 	/*
3015 	 * Place this routine at the end, since a new checkpoint would be
3016 	 * triggered while remount and we need to take care of it before
3017 	 * returning from remount.
3018 	 */
3019 	if ((flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
3020 			!test_opt(sbi, MERGE_CHECKPOINT)) {
3021 		f2fs_stop_ckpt_thread(sbi);
3022 	} else {
3023 		/* Flush if the previous checkpoint, if exists. */
3024 		f2fs_flush_ckpt_thread(sbi);
3025 
3026 		err = f2fs_start_ckpt_thread(sbi);
3027 		if (err) {
3028 			f2fs_err(sbi,
3029 			    "Failed to start F2FS issue_checkpoint_thread (%d)",
3030 			    err);
3031 			goto restore_checkpoint;
3032 		}
3033 	}
3034 
3035 skip:
3036 #ifdef CONFIG_QUOTA
3037 	/* Release old quota file names */
3038 	for (i = 0; i < MAXQUOTAS; i++)
3039 		kfree(org_mount_opt.s_qf_names[i]);
3040 #endif
3041 	/* Update the POSIXACL Flag */
3042 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
3043 		(test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
3044 
3045 	limit_reserve_root(sbi);
3046 	fc->sb_flags = (flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
3047 
3048 	sbi->umount_lock_holder = NULL;
3049 	return 0;
3050 restore_checkpoint:
3051 	if (need_enable_checkpoint) {
3052 		if (f2fs_enable_checkpoint(sbi))
3053 			f2fs_warn(sbi, "checkpoint has not been enabled");
3054 	} else if (need_disable_checkpoint) {
3055 		if (f2fs_disable_checkpoint(sbi))
3056 			f2fs_warn(sbi, "checkpoint has not been disabled");
3057 	}
3058 restore_discard:
3059 	if (need_restart_discard) {
3060 		if (f2fs_start_discard_thread(sbi))
3061 			f2fs_warn(sbi, "discard has been stopped");
3062 	} else if (need_stop_discard) {
3063 		f2fs_stop_discard_thread(sbi);
3064 	}
3065 restore_flush:
3066 	if (need_restart_flush) {
3067 		if (f2fs_create_flush_cmd_control(sbi))
3068 			f2fs_warn(sbi, "background flush thread has stopped");
3069 	} else if (need_stop_flush) {
3070 		clear_opt(sbi, FLUSH_MERGE);
3071 		f2fs_destroy_flush_cmd_control(sbi, false);
3072 	}
3073 restore_gc:
3074 	if (need_restart_gc) {
3075 		if (f2fs_start_gc_thread(sbi))
3076 			f2fs_warn(sbi, "background gc thread has stopped");
3077 	} else if (need_stop_gc) {
3078 		f2fs_stop_gc_thread(sbi);
3079 	}
3080 restore_opts:
3081 #ifdef CONFIG_QUOTA
3082 	F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
3083 	for (i = 0; i < MAXQUOTAS; i++) {
3084 		kfree(F2FS_OPTION(sbi).s_qf_names[i]);
3085 		F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
3086 	}
3087 #endif
3088 	sbi->mount_opt = org_mount_opt;
3089 	sb->s_flags = old_sb_flags;
3090 
3091 	sbi->umount_lock_holder = NULL;
3092 	return err;
3093 }
3094 
3095 static void f2fs_shutdown(struct super_block *sb)
3096 {
3097 	f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false);
3098 }
3099 
3100 #ifdef CONFIG_QUOTA
3101 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
3102 {
3103 	/* need to recovery orphan */
3104 	if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
3105 		return true;
3106 	/* need to recovery data */
3107 	if (test_opt(sbi, DISABLE_ROLL_FORWARD))
3108 		return false;
3109 	if (test_opt(sbi, NORECOVERY))
3110 		return false;
3111 	return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
3112 }
3113 
3114 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
3115 {
3116 	bool readonly = f2fs_readonly(sbi->sb);
3117 
3118 	if (!f2fs_need_recovery(sbi))
3119 		return false;
3120 
3121 	/* it doesn't need to check f2fs_sb_has_readonly() */
3122 	if (f2fs_hw_is_readonly(sbi))
3123 		return false;
3124 
3125 	if (readonly) {
3126 		sbi->sb->s_flags &= ~SB_RDONLY;
3127 		set_sbi_flag(sbi, SBI_IS_WRITABLE);
3128 	}
3129 
3130 	/*
3131 	 * Turn on quotas which were not enabled for read-only mounts if
3132 	 * filesystem has quota feature, so that they are updated correctly.
3133 	 */
3134 	return f2fs_enable_quota_files(sbi, readonly);
3135 }
3136 
3137 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
3138 						bool quota_enabled)
3139 {
3140 	if (quota_enabled)
3141 		f2fs_quota_off_umount(sbi->sb);
3142 
3143 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
3144 		clear_sbi_flag(sbi, SBI_IS_WRITABLE);
3145 		sbi->sb->s_flags |= SB_RDONLY;
3146 	}
3147 }
3148 
3149 /* Read data from quotafile */
3150 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
3151 			       size_t len, loff_t off)
3152 {
3153 	struct inode *inode = sb_dqopt(sb)->files[type];
3154 	struct address_space *mapping = inode->i_mapping;
3155 	int tocopy;
3156 	size_t toread;
3157 	loff_t i_size = i_size_read(inode);
3158 
3159 	if (off > i_size)
3160 		return 0;
3161 
3162 	if (off + len > i_size)
3163 		len = i_size - off;
3164 	toread = len;
3165 	while (toread > 0) {
3166 		struct folio *folio;
3167 		size_t offset;
3168 
3169 repeat:
3170 		folio = mapping_read_folio_gfp(mapping, off >> PAGE_SHIFT,
3171 				GFP_NOFS);
3172 		if (IS_ERR(folio)) {
3173 			if (PTR_ERR(folio) == -ENOMEM) {
3174 				memalloc_retry_wait(GFP_NOFS);
3175 				goto repeat;
3176 			}
3177 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3178 			return PTR_ERR(folio);
3179 		}
3180 		offset = offset_in_folio(folio, off);
3181 		tocopy = min(folio_size(folio) - offset, toread);
3182 
3183 		folio_lock(folio);
3184 
3185 		if (unlikely(folio->mapping != mapping)) {
3186 			f2fs_folio_put(folio, true);
3187 			goto repeat;
3188 		}
3189 
3190 		/*
3191 		 * should never happen, just leave f2fs_bug_on() here to catch
3192 		 * any potential bug.
3193 		 */
3194 		f2fs_bug_on(F2FS_SB(sb), !folio_test_uptodate(folio));
3195 
3196 		memcpy_from_folio(data, folio, offset, tocopy);
3197 		f2fs_folio_put(folio, true);
3198 
3199 		toread -= tocopy;
3200 		data += tocopy;
3201 		off += tocopy;
3202 	}
3203 	return len;
3204 }
3205 
3206 /* Write to quotafile */
3207 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
3208 				const char *data, size_t len, loff_t off)
3209 {
3210 	struct inode *inode = sb_dqopt(sb)->files[type];
3211 	struct address_space *mapping = inode->i_mapping;
3212 	const struct address_space_operations *a_ops = mapping->a_ops;
3213 	int offset = off & (sb->s_blocksize - 1);
3214 	size_t towrite = len;
3215 	struct folio *folio;
3216 	void *fsdata = NULL;
3217 	int err = 0;
3218 	int tocopy;
3219 
3220 	while (towrite > 0) {
3221 		tocopy = min_t(unsigned long, sb->s_blocksize - offset,
3222 								towrite);
3223 retry:
3224 		err = a_ops->write_begin(NULL, mapping, off, tocopy,
3225 							&folio, &fsdata);
3226 		if (unlikely(err)) {
3227 			if (err == -ENOMEM) {
3228 				memalloc_retry_wait(GFP_NOFS);
3229 				goto retry;
3230 			}
3231 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3232 			break;
3233 		}
3234 
3235 		memcpy_to_folio(folio, offset_in_folio(folio, off), data, tocopy);
3236 
3237 		a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
3238 						folio, fsdata);
3239 		offset = 0;
3240 		towrite -= tocopy;
3241 		off += tocopy;
3242 		data += tocopy;
3243 		cond_resched();
3244 	}
3245 
3246 	if (len == towrite)
3247 		return err;
3248 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
3249 	f2fs_mark_inode_dirty_sync(inode, false);
3250 	return len - towrite;
3251 }
3252 
3253 int f2fs_dquot_initialize(struct inode *inode)
3254 {
3255 	if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
3256 		return -ESRCH;
3257 
3258 	return dquot_initialize(inode);
3259 }
3260 
3261 static struct dquot __rcu **f2fs_get_dquots(struct inode *inode)
3262 {
3263 	return F2FS_I(inode)->i_dquot;
3264 }
3265 
3266 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
3267 {
3268 	return &F2FS_I(inode)->i_reserved_quota;
3269 }
3270 
3271 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
3272 {
3273 	if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
3274 		f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
3275 		return 0;
3276 	}
3277 
3278 	return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
3279 					F2FS_OPTION(sbi).s_jquota_fmt, type);
3280 }
3281 
3282 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
3283 {
3284 	int enabled = 0;
3285 	int i, err;
3286 
3287 	if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
3288 		err = f2fs_enable_quotas(sbi->sb);
3289 		if (err) {
3290 			f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
3291 			return 0;
3292 		}
3293 		return 1;
3294 	}
3295 
3296 	for (i = 0; i < MAXQUOTAS; i++) {
3297 		if (F2FS_OPTION(sbi).s_qf_names[i]) {
3298 			err = f2fs_quota_on_mount(sbi, i);
3299 			if (!err) {
3300 				enabled = 1;
3301 				continue;
3302 			}
3303 			f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
3304 				 err, i);
3305 		}
3306 	}
3307 	return enabled;
3308 }
3309 
3310 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
3311 			     unsigned int flags, unsigned long qf_inum)
3312 {
3313 	struct inode *qf_inode;
3314 	unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
3315 	int err;
3316 
3317 	qf_inode = f2fs_iget(sb, qf_inum);
3318 	if (IS_ERR(qf_inode)) {
3319 		f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
3320 		return PTR_ERR(qf_inode);
3321 	}
3322 
3323 	/* Don't account quota for quota files to avoid recursion */
3324 	inode_lock(qf_inode);
3325 	qf_inode->i_flags |= S_NOQUOTA;
3326 
3327 	if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
3328 		F2FS_I(qf_inode)->i_flags |= qf_flag;
3329 		f2fs_set_inode_flags(qf_inode);
3330 	}
3331 	inode_unlock(qf_inode);
3332 
3333 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
3334 	iput(qf_inode);
3335 	return err;
3336 }
3337 
3338 static int f2fs_enable_quotas(struct super_block *sb)
3339 {
3340 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
3341 	int type, err = 0;
3342 	unsigned long qf_inum;
3343 	bool quota_mopt[MAXQUOTAS] = {
3344 		test_opt(sbi, USRQUOTA),
3345 		test_opt(sbi, GRPQUOTA),
3346 		test_opt(sbi, PRJQUOTA),
3347 	};
3348 
3349 	if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
3350 		f2fs_err(sbi, "quota file may be corrupted, skip loading it");
3351 		return 0;
3352 	}
3353 
3354 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
3355 
3356 	for (type = 0; type < MAXQUOTAS; type++) {
3357 		qf_inum = f2fs_qf_ino(sb, type);
3358 		if (qf_inum) {
3359 			err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
3360 				DQUOT_USAGE_ENABLED |
3361 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0), qf_inum);
3362 			if (err) {
3363 				f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
3364 					 type, err);
3365 				for (type--; type >= 0; type--)
3366 					dquot_quota_off(sb, type);
3367 				set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3368 				return err;
3369 			}
3370 		}
3371 	}
3372 	return 0;
3373 }
3374 
3375 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
3376 {
3377 	struct quota_info *dqopt = sb_dqopt(sbi->sb);
3378 	struct address_space *mapping = dqopt->files[type]->i_mapping;
3379 	int ret = 0;
3380 
3381 	ret = dquot_writeback_dquots(sbi->sb, type);
3382 	if (ret)
3383 		goto out;
3384 
3385 	ret = filemap_fdatawrite(mapping);
3386 	if (ret)
3387 		goto out;
3388 
3389 	/* if we are using journalled quota */
3390 	if (is_journalled_quota(sbi))
3391 		goto out;
3392 
3393 	ret = filemap_fdatawait(mapping);
3394 
3395 	truncate_inode_pages(&dqopt->files[type]->i_data, 0);
3396 out:
3397 	if (ret)
3398 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3399 	return ret;
3400 }
3401 
3402 int f2fs_do_quota_sync(struct super_block *sb, int type)
3403 {
3404 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
3405 	struct quota_info *dqopt = sb_dqopt(sb);
3406 	int cnt;
3407 	int ret = 0;
3408 
3409 	/*
3410 	 * Now when everything is written we can discard the pagecache so
3411 	 * that userspace sees the changes.
3412 	 */
3413 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
3414 		struct f2fs_lock_context lc;
3415 
3416 		if (type != -1 && cnt != type)
3417 			continue;
3418 
3419 		if (!sb_has_quota_active(sb, cnt))
3420 			continue;
3421 
3422 		if (!f2fs_sb_has_quota_ino(sbi))
3423 			inode_lock(dqopt->files[cnt]);
3424 
3425 		/*
3426 		 * do_quotactl
3427 		 *  f2fs_quota_sync
3428 		 *  f2fs_down_read(quota_sem)
3429 		 *  dquot_writeback_dquots()
3430 		 *  f2fs_dquot_commit
3431 		 *			      block_operation
3432 		 *			      f2fs_down_read(quota_sem)
3433 		 */
3434 		f2fs_lock_op(sbi, &lc);
3435 		f2fs_down_read(&sbi->quota_sem);
3436 
3437 		ret = f2fs_quota_sync_file(sbi, cnt);
3438 
3439 		f2fs_up_read(&sbi->quota_sem);
3440 		f2fs_unlock_op(sbi, &lc);
3441 
3442 		if (!f2fs_sb_has_quota_ino(sbi))
3443 			inode_unlock(dqopt->files[cnt]);
3444 
3445 		if (ret)
3446 			break;
3447 	}
3448 	return ret;
3449 }
3450 
3451 static int f2fs_quota_sync(struct super_block *sb, int type)
3452 {
3453 	int ret;
3454 
3455 	F2FS_SB(sb)->umount_lock_holder = current;
3456 	ret = f2fs_do_quota_sync(sb, type);
3457 	F2FS_SB(sb)->umount_lock_holder = NULL;
3458 	return ret;
3459 }
3460 
3461 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
3462 							const struct path *path)
3463 {
3464 	struct inode *inode;
3465 	int err = 0;
3466 
3467 	/* if quota sysfile exists, deny enabling quota with specific file */
3468 	if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
3469 		f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
3470 		return -EBUSY;
3471 	}
3472 
3473 	if (path->dentry->d_sb != sb)
3474 		return -EXDEV;
3475 
3476 	F2FS_SB(sb)->umount_lock_holder = current;
3477 
3478 	err = f2fs_do_quota_sync(sb, type);
3479 	if (err)
3480 		goto out;
3481 
3482 	inode = d_inode(path->dentry);
3483 
3484 	err = filemap_fdatawrite(inode->i_mapping);
3485 	if (err)
3486 		goto out;
3487 
3488 	err = filemap_fdatawait(inode->i_mapping);
3489 	if (err)
3490 		goto out;
3491 
3492 	err = dquot_quota_on(sb, type, format_id, path);
3493 	if (err)
3494 		goto out;
3495 
3496 	inode_lock(inode);
3497 	F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
3498 	f2fs_set_inode_flags(inode);
3499 	inode_unlock(inode);
3500 	f2fs_mark_inode_dirty_sync(inode, false);
3501 out:
3502 	F2FS_SB(sb)->umount_lock_holder = NULL;
3503 	return err;
3504 }
3505 
3506 static int __f2fs_quota_off(struct super_block *sb, int type)
3507 {
3508 	struct inode *inode = sb_dqopt(sb)->files[type];
3509 	int err;
3510 
3511 	if (!inode || !igrab(inode))
3512 		return dquot_quota_off(sb, type);
3513 
3514 	err = f2fs_do_quota_sync(sb, type);
3515 	if (err)
3516 		goto out_put;
3517 
3518 	err = dquot_quota_off(sb, type);
3519 	if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
3520 		goto out_put;
3521 
3522 	inode_lock(inode);
3523 	F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
3524 	f2fs_set_inode_flags(inode);
3525 	inode_unlock(inode);
3526 	f2fs_mark_inode_dirty_sync(inode, false);
3527 out_put:
3528 	iput(inode);
3529 	return err;
3530 }
3531 
3532 static int f2fs_quota_off(struct super_block *sb, int type)
3533 {
3534 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
3535 	int err;
3536 
3537 	F2FS_SB(sb)->umount_lock_holder = current;
3538 
3539 	err = __f2fs_quota_off(sb, type);
3540 
3541 	/*
3542 	 * quotactl can shutdown journalled quota, result in inconsistence
3543 	 * between quota record and fs data by following updates, tag the
3544 	 * flag to let fsck be aware of it.
3545 	 */
3546 	if (is_journalled_quota(sbi))
3547 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3548 
3549 	F2FS_SB(sb)->umount_lock_holder = NULL;
3550 
3551 	return err;
3552 }
3553 
3554 void f2fs_quota_off_umount(struct super_block *sb)
3555 {
3556 	int type;
3557 	int err;
3558 
3559 	for (type = 0; type < MAXQUOTAS; type++) {
3560 		err = __f2fs_quota_off(sb, type);
3561 		if (err) {
3562 			int ret = dquot_quota_off(sb, type);
3563 
3564 			f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
3565 				 type, err, ret);
3566 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3567 		}
3568 	}
3569 	/*
3570 	 * In case of checkpoint=disable, we must flush quota blocks.
3571 	 * This can cause NULL exception for node_inode in end_io, since
3572 	 * put_super already dropped it.
3573 	 */
3574 	sync_filesystem(sb);
3575 }
3576 
3577 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
3578 {
3579 	struct quota_info *dqopt = sb_dqopt(sb);
3580 	int type;
3581 
3582 	for (type = 0; type < MAXQUOTAS; type++) {
3583 		if (!dqopt->files[type])
3584 			continue;
3585 		f2fs_inode_synced(dqopt->files[type]);
3586 	}
3587 }
3588 
3589 static int f2fs_dquot_commit(struct dquot *dquot)
3590 {
3591 	struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3592 	int ret;
3593 
3594 	f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
3595 	ret = dquot_commit(dquot);
3596 	if (ret < 0)
3597 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3598 	f2fs_up_read(&sbi->quota_sem);
3599 	return ret;
3600 }
3601 
3602 static int f2fs_dquot_acquire(struct dquot *dquot)
3603 {
3604 	struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3605 	int ret;
3606 
3607 	f2fs_down_read(&sbi->quota_sem);
3608 	ret = dquot_acquire(dquot);
3609 	if (ret < 0)
3610 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3611 	f2fs_up_read(&sbi->quota_sem);
3612 	return ret;
3613 }
3614 
3615 static int f2fs_dquot_release(struct dquot *dquot)
3616 {
3617 	struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3618 	int ret = dquot_release(dquot);
3619 
3620 	if (ret < 0)
3621 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3622 	return ret;
3623 }
3624 
3625 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
3626 {
3627 	struct super_block *sb = dquot->dq_sb;
3628 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
3629 	int ret = dquot_mark_dquot_dirty(dquot);
3630 
3631 	/* if we are using journalled quota */
3632 	if (is_journalled_quota(sbi))
3633 		set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
3634 
3635 	return ret;
3636 }
3637 
3638 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
3639 {
3640 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
3641 	int ret = dquot_commit_info(sb, type);
3642 
3643 	if (ret < 0)
3644 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3645 	return ret;
3646 }
3647 
3648 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
3649 {
3650 	*projid = F2FS_I(inode)->i_projid;
3651 	return 0;
3652 }
3653 
3654 static const struct dquot_operations f2fs_quota_operations = {
3655 	.get_reserved_space = f2fs_get_reserved_space,
3656 	.write_dquot	= f2fs_dquot_commit,
3657 	.acquire_dquot	= f2fs_dquot_acquire,
3658 	.release_dquot	= f2fs_dquot_release,
3659 	.mark_dirty	= f2fs_dquot_mark_dquot_dirty,
3660 	.write_info	= f2fs_dquot_commit_info,
3661 	.alloc_dquot	= dquot_alloc,
3662 	.destroy_dquot	= dquot_destroy,
3663 	.get_projid	= f2fs_get_projid,
3664 	.get_next_id	= dquot_get_next_id,
3665 };
3666 
3667 static const struct quotactl_ops f2fs_quotactl_ops = {
3668 	.quota_on	= f2fs_quota_on,
3669 	.quota_off	= f2fs_quota_off,
3670 	.quota_sync	= f2fs_quota_sync,
3671 	.get_state	= dquot_get_state,
3672 	.set_info	= dquot_set_dqinfo,
3673 	.get_dqblk	= dquot_get_dqblk,
3674 	.set_dqblk	= dquot_set_dqblk,
3675 	.get_nextdqblk	= dquot_get_next_dqblk,
3676 };
3677 #else
3678 int f2fs_dquot_initialize(struct inode *inode)
3679 {
3680 	return 0;
3681 }
3682 
3683 int f2fs_do_quota_sync(struct super_block *sb, int type)
3684 {
3685 	return 0;
3686 }
3687 
3688 void f2fs_quota_off_umount(struct super_block *sb)
3689 {
3690 }
3691 #endif
3692 
3693 static const struct super_operations f2fs_sops = {
3694 	.alloc_inode	= f2fs_alloc_inode,
3695 	.free_inode	= f2fs_free_inode,
3696 	.drop_inode	= f2fs_drop_inode,
3697 	.write_inode	= f2fs_write_inode,
3698 	.dirty_inode	= f2fs_dirty_inode,
3699 	.show_options	= f2fs_show_options,
3700 #ifdef CONFIG_QUOTA
3701 	.quota_read	= f2fs_quota_read,
3702 	.quota_write	= f2fs_quota_write,
3703 	.get_dquots	= f2fs_get_dquots,
3704 #endif
3705 	.evict_inode	= f2fs_evict_inode,
3706 	.put_super	= f2fs_put_super,
3707 	.sync_fs	= f2fs_sync_fs,
3708 	.freeze_fs	= f2fs_freeze,
3709 	.unfreeze_fs	= f2fs_unfreeze,
3710 	.statfs		= f2fs_statfs,
3711 	.shutdown	= f2fs_shutdown,
3712 };
3713 
3714 #ifdef CONFIG_FS_ENCRYPTION
3715 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
3716 {
3717 	return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3718 				F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3719 				ctx, len, NULL);
3720 }
3721 
3722 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
3723 							void *fs_data)
3724 {
3725 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3726 
3727 	/*
3728 	 * Encrypting the root directory is not allowed because fsck
3729 	 * expects lost+found directory to exist and remain unencrypted
3730 	 * if LOST_FOUND feature is enabled.
3731 	 *
3732 	 */
3733 	if (f2fs_sb_has_lost_found(sbi) &&
3734 			inode->i_ino == F2FS_ROOT_INO(sbi))
3735 		return -EPERM;
3736 
3737 	return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3738 				F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3739 				ctx, len, fs_data, XATTR_CREATE);
3740 }
3741 
3742 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
3743 {
3744 	return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
3745 }
3746 
3747 static bool f2fs_has_stable_inodes(struct super_block *sb)
3748 {
3749 	return true;
3750 }
3751 
3752 static unsigned int
3753 f2fs_get_devices(struct super_block *sb,
3754 		 struct block_device *devs[FSCRYPT_MAX_DEVICES])
3755 {
3756 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
3757 	int ndevs;
3758 	int i;
3759 
3760 	static_assert(MAX_DEVICES <= FSCRYPT_MAX_DEVICES);
3761 
3762 	if (!f2fs_is_multi_device(sbi)) {
3763 		devs[0] = sb->s_bdev;
3764 		return 1;
3765 	}
3766 	ndevs = sbi->s_ndevs;
3767 	if (WARN_ON_ONCE(ndevs > FSCRYPT_MAX_DEVICES))
3768 		ndevs = FSCRYPT_MAX_DEVICES;
3769 
3770 	for (i = 0; i < ndevs; i++)
3771 		devs[i] = FDEV(i).bdev;
3772 	return ndevs;
3773 }
3774 
3775 static const struct fscrypt_operations f2fs_cryptops = {
3776 	.inode_info_offs	= (int)offsetof(struct f2fs_inode_info, i_crypt_info) -
3777 				  (int)offsetof(struct f2fs_inode_info, vfs_inode),
3778 	.is_block_based		= 1,
3779 	.has_32bit_inodes	= 1,
3780 	.supports_subblock_data_units = 1,
3781 	.legacy_key_prefix	= "f2fs:",
3782 	.get_context		= f2fs_get_context,
3783 	.set_context		= f2fs_set_context,
3784 	.get_dummy_policy	= f2fs_get_dummy_policy,
3785 	.empty_dir		= f2fs_empty_dir,
3786 	.has_stable_inodes	= f2fs_has_stable_inodes,
3787 	.get_devices		= f2fs_get_devices,
3788 };
3789 #endif /* CONFIG_FS_ENCRYPTION */
3790 
3791 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
3792 		u64 ino, u32 generation)
3793 {
3794 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
3795 	struct inode *inode;
3796 
3797 	if (f2fs_check_nid_range(sbi, ino))
3798 		return ERR_PTR(-ESTALE);
3799 
3800 	/*
3801 	 * f2fs_iget isn't quite right if the inode is currently unallocated!
3802 	 * However f2fs_iget currently does appropriate checks to handle stale
3803 	 * inodes so everything is OK.
3804 	 */
3805 	inode = f2fs_iget(sb, ino);
3806 	if (IS_ERR(inode))
3807 		return ERR_CAST(inode);
3808 	if (unlikely(generation && inode->i_generation != generation)) {
3809 		/* we didn't find the right inode.. */
3810 		iput(inode);
3811 		return ERR_PTR(-ESTALE);
3812 	}
3813 	return inode;
3814 }
3815 
3816 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
3817 		int fh_len, int fh_type)
3818 {
3819 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3820 				    f2fs_nfs_get_inode);
3821 }
3822 
3823 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
3824 		int fh_len, int fh_type)
3825 {
3826 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3827 				    f2fs_nfs_get_inode);
3828 }
3829 
3830 static const struct export_operations f2fs_export_ops = {
3831 	.encode_fh = generic_encode_ino32_fh,
3832 	.fh_to_dentry = f2fs_fh_to_dentry,
3833 	.fh_to_parent = f2fs_fh_to_parent,
3834 	.get_parent = f2fs_get_parent,
3835 };
3836 
3837 loff_t max_file_blocks(struct inode *inode)
3838 {
3839 	loff_t result = 0;
3840 	loff_t leaf_count;
3841 
3842 	/*
3843 	 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
3844 	 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
3845 	 * space in inode.i_addr, it will be more safe to reassign
3846 	 * result as zero.
3847 	 */
3848 
3849 	if (inode && f2fs_compressed_file(inode))
3850 		leaf_count = ADDRS_PER_BLOCK(inode);
3851 	else
3852 		leaf_count = DEF_ADDRS_PER_BLOCK;
3853 
3854 	/* two direct node blocks */
3855 	result += (leaf_count * 2);
3856 
3857 	/* two indirect node blocks */
3858 	leaf_count *= NIDS_PER_BLOCK;
3859 	result += (leaf_count * 2);
3860 
3861 	/* one double indirect node block */
3862 	leaf_count *= NIDS_PER_BLOCK;
3863 	result += leaf_count;
3864 
3865 	/*
3866 	 * For compatibility with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{64,32} with
3867 	 * a 4K crypto data unit, we must restrict the max filesize to what can
3868 	 * fit within U32_MAX + 1 data units.
3869 	 */
3870 
3871 	result = umin(result, F2FS_BYTES_TO_BLK(((loff_t)U32_MAX + 1) * 4096));
3872 
3873 	return result;
3874 }
3875 
3876 static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio,
3877 						pgoff_t index, bool update)
3878 {
3879 	struct bio *bio;
3880 	/* it's rare case, we can do fua all the time */
3881 	blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA;
3882 	int ret;
3883 
3884 	folio_lock(folio);
3885 	folio_wait_writeback(folio);
3886 	if (update)
3887 		memcpy(F2FS_SUPER_BLOCK(folio, index), F2FS_RAW_SUPER(sbi),
3888 					sizeof(struct f2fs_super_block));
3889 	folio_mark_dirty(folio);
3890 	folio_clear_dirty_for_io(folio);
3891 	folio_start_writeback(folio);
3892 	folio_unlock(folio);
3893 
3894 	bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS);
3895 
3896 	/* it doesn't need to set crypto context for superblock update */
3897 	bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio->index);
3898 
3899 	if (!bio_add_folio(bio, folio, folio_size(folio), 0))
3900 		f2fs_bug_on(sbi, 1);
3901 
3902 	ret = submit_bio_wait(bio);
3903 	bio_put(bio);
3904 	folio_end_writeback(folio);
3905 
3906 	return ret;
3907 }
3908 
3909 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
3910 					struct folio *folio, pgoff_t index)
3911 {
3912 	struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
3913 	struct super_block *sb = sbi->sb;
3914 	u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3915 	u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
3916 	u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
3917 	u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
3918 	u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
3919 	u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3920 	u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
3921 	u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
3922 	u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
3923 	u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
3924 	u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3925 	u32 segment_count = le32_to_cpu(raw_super->segment_count);
3926 	u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3927 	u64 main_end_blkaddr = main_blkaddr +
3928 				((u64)segment_count_main << log_blocks_per_seg);
3929 	u64 seg_end_blkaddr = segment0_blkaddr +
3930 				((u64)segment_count << log_blocks_per_seg);
3931 
3932 	if (segment0_blkaddr != cp_blkaddr) {
3933 		f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
3934 			  segment0_blkaddr, cp_blkaddr);
3935 		return true;
3936 	}
3937 
3938 	if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
3939 							sit_blkaddr) {
3940 		f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
3941 			  cp_blkaddr, sit_blkaddr,
3942 			  segment_count_ckpt << log_blocks_per_seg);
3943 		return true;
3944 	}
3945 
3946 	if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
3947 							nat_blkaddr) {
3948 		f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
3949 			  sit_blkaddr, nat_blkaddr,
3950 			  segment_count_sit << log_blocks_per_seg);
3951 		return true;
3952 	}
3953 
3954 	if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
3955 							ssa_blkaddr) {
3956 		f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
3957 			  nat_blkaddr, ssa_blkaddr,
3958 			  segment_count_nat << log_blocks_per_seg);
3959 		return true;
3960 	}
3961 
3962 	if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
3963 							main_blkaddr) {
3964 		f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
3965 			  ssa_blkaddr, main_blkaddr,
3966 			  segment_count_ssa << log_blocks_per_seg);
3967 		return true;
3968 	}
3969 
3970 	if (main_end_blkaddr > seg_end_blkaddr) {
3971 		f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3972 			  main_blkaddr, seg_end_blkaddr,
3973 			  segment_count_main << log_blocks_per_seg);
3974 		return true;
3975 	} else if (main_end_blkaddr < seg_end_blkaddr) {
3976 		int err = 0;
3977 		char *res;
3978 
3979 		/* fix in-memory information all the time */
3980 		raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
3981 				segment0_blkaddr) >> log_blocks_per_seg);
3982 
3983 		if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
3984 			set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3985 			res = "internally";
3986 		} else {
3987 			err = __f2fs_commit_super(sbi, folio, index, false);
3988 			res = err ? "failed" : "done";
3989 		}
3990 		f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3991 			  res, main_blkaddr, seg_end_blkaddr,
3992 			  segment_count_main << log_blocks_per_seg);
3993 		if (err)
3994 			return true;
3995 	}
3996 	return false;
3997 }
3998 
3999 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
4000 					struct folio *folio, pgoff_t index)
4001 {
4002 	block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
4003 	block_t total_sections, blocks_per_seg;
4004 	struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
4005 	size_t crc_offset = 0;
4006 	__u32 crc = 0;
4007 
4008 	if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
4009 		f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
4010 			  F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
4011 		return -EINVAL;
4012 	}
4013 
4014 	/* Check checksum_offset and crc in superblock */
4015 	if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
4016 		crc_offset = le32_to_cpu(raw_super->checksum_offset);
4017 		if (crc_offset !=
4018 			offsetof(struct f2fs_super_block, crc)) {
4019 			f2fs_info(sbi, "Invalid SB checksum offset: %zu",
4020 				  crc_offset);
4021 			return -EFSCORRUPTED;
4022 		}
4023 		crc = le32_to_cpu(raw_super->crc);
4024 		if (crc != f2fs_crc32(raw_super, crc_offset)) {
4025 			f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
4026 			return -EFSCORRUPTED;
4027 		}
4028 	}
4029 
4030 	/* only support block_size equals to PAGE_SIZE */
4031 	if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
4032 		f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
4033 			  le32_to_cpu(raw_super->log_blocksize),
4034 			  F2FS_BLKSIZE_BITS);
4035 		return -EFSCORRUPTED;
4036 	}
4037 
4038 	/* check log blocks per segment */
4039 	if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
4040 		f2fs_info(sbi, "Invalid log blocks per segment (%u)",
4041 			  le32_to_cpu(raw_super->log_blocks_per_seg));
4042 		return -EFSCORRUPTED;
4043 	}
4044 
4045 	/* Currently, support 512/1024/2048/4096/16K bytes sector size */
4046 	if (le32_to_cpu(raw_super->log_sectorsize) >
4047 				F2FS_MAX_LOG_SECTOR_SIZE ||
4048 		le32_to_cpu(raw_super->log_sectorsize) <
4049 				F2FS_MIN_LOG_SECTOR_SIZE) {
4050 		f2fs_info(sbi, "Invalid log sectorsize (%u)",
4051 			  le32_to_cpu(raw_super->log_sectorsize));
4052 		return -EFSCORRUPTED;
4053 	}
4054 	if (le32_to_cpu(raw_super->log_sectors_per_block) +
4055 		le32_to_cpu(raw_super->log_sectorsize) !=
4056 			F2FS_MAX_LOG_SECTOR_SIZE) {
4057 		f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
4058 			  le32_to_cpu(raw_super->log_sectors_per_block),
4059 			  le32_to_cpu(raw_super->log_sectorsize));
4060 		return -EFSCORRUPTED;
4061 	}
4062 
4063 	segment_count = le32_to_cpu(raw_super->segment_count);
4064 	segment_count_main = le32_to_cpu(raw_super->segment_count_main);
4065 	segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
4066 	secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
4067 	total_sections = le32_to_cpu(raw_super->section_count);
4068 
4069 	/* blocks_per_seg should be 512, given the above check */
4070 	blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
4071 
4072 	if (segment_count > F2FS_MAX_SEGMENT ||
4073 				segment_count < F2FS_MIN_SEGMENTS) {
4074 		f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
4075 		return -EFSCORRUPTED;
4076 	}
4077 
4078 	if (total_sections > segment_count_main || total_sections < 1 ||
4079 			segs_per_sec > segment_count || !segs_per_sec) {
4080 		f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
4081 			  segment_count, total_sections, segs_per_sec);
4082 		return -EFSCORRUPTED;
4083 	}
4084 
4085 	if (segment_count_main != total_sections * segs_per_sec) {
4086 		f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
4087 			  segment_count_main, total_sections, segs_per_sec);
4088 		return -EFSCORRUPTED;
4089 	}
4090 
4091 	if ((segment_count / segs_per_sec) < total_sections) {
4092 		f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
4093 			  segment_count, segs_per_sec, total_sections);
4094 		return -EFSCORRUPTED;
4095 	}
4096 
4097 	if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
4098 		f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
4099 			  segment_count, le64_to_cpu(raw_super->block_count));
4100 		return -EFSCORRUPTED;
4101 	}
4102 
4103 	if (RDEV(0).path[0]) {
4104 		block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
4105 		int i = 1;
4106 
4107 		while (i < MAX_DEVICES && RDEV(i).path[0]) {
4108 			dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
4109 			i++;
4110 		}
4111 		if (segment_count != dev_seg_count) {
4112 			f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
4113 					segment_count, dev_seg_count);
4114 			return -EFSCORRUPTED;
4115 		}
4116 	} else {
4117 		if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
4118 					!bdev_is_zoned(sbi->sb->s_bdev)) {
4119 			f2fs_info(sbi, "Zoned block device path is missing");
4120 			return -EFSCORRUPTED;
4121 		}
4122 	}
4123 
4124 	if (secs_per_zone > total_sections || !secs_per_zone) {
4125 		f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
4126 			  secs_per_zone, total_sections);
4127 		return -EFSCORRUPTED;
4128 	}
4129 	if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
4130 			raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
4131 			(le32_to_cpu(raw_super->extension_count) +
4132 			raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
4133 		f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
4134 			  le32_to_cpu(raw_super->extension_count),
4135 			  raw_super->hot_ext_count,
4136 			  F2FS_MAX_EXTENSION);
4137 		return -EFSCORRUPTED;
4138 	}
4139 
4140 	if (le32_to_cpu(raw_super->cp_payload) >=
4141 				(blocks_per_seg - F2FS_CP_PACKS -
4142 				NR_CURSEG_PERSIST_TYPE)) {
4143 		f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
4144 			  le32_to_cpu(raw_super->cp_payload),
4145 			  blocks_per_seg - F2FS_CP_PACKS -
4146 			  NR_CURSEG_PERSIST_TYPE);
4147 		return -EFSCORRUPTED;
4148 	}
4149 
4150 	/* check reserved ino info */
4151 	if (le32_to_cpu(raw_super->node_ino) != 1 ||
4152 		le32_to_cpu(raw_super->meta_ino) != 2 ||
4153 		le32_to_cpu(raw_super->root_ino) != 3) {
4154 		f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
4155 			  le32_to_cpu(raw_super->node_ino),
4156 			  le32_to_cpu(raw_super->meta_ino),
4157 			  le32_to_cpu(raw_super->root_ino));
4158 		return -EFSCORRUPTED;
4159 	}
4160 
4161 	/* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
4162 	if (sanity_check_area_boundary(sbi, folio, index))
4163 		return -EFSCORRUPTED;
4164 
4165 	return 0;
4166 }
4167 
4168 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
4169 {
4170 	unsigned int total, fsmeta;
4171 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4172 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
4173 	unsigned int ovp_segments, reserved_segments;
4174 	unsigned int main_segs, blocks_per_seg;
4175 	unsigned int sit_segs, nat_segs;
4176 	unsigned int sit_bitmap_size, nat_bitmap_size;
4177 	unsigned int log_blocks_per_seg;
4178 	unsigned int segment_count_main;
4179 	unsigned int cp_pack_start_sum, cp_payload;
4180 	block_t user_block_count, valid_user_blocks;
4181 	block_t avail_node_count, valid_node_count;
4182 	unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
4183 	unsigned int sit_blk_cnt;
4184 	int i, j;
4185 
4186 	total = le32_to_cpu(raw_super->segment_count);
4187 	fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
4188 	sit_segs = le32_to_cpu(raw_super->segment_count_sit);
4189 	fsmeta += sit_segs;
4190 	nat_segs = le32_to_cpu(raw_super->segment_count_nat);
4191 	fsmeta += nat_segs;
4192 	fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
4193 	fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
4194 
4195 	if (unlikely(fsmeta >= total))
4196 		return 1;
4197 
4198 	ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
4199 	reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
4200 
4201 	if (!f2fs_sb_has_readonly(sbi) &&
4202 			unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
4203 			ovp_segments == 0 || reserved_segments == 0)) {
4204 		f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
4205 		return 1;
4206 	}
4207 	user_block_count = le64_to_cpu(ckpt->user_block_count);
4208 	segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
4209 			(f2fs_sb_has_readonly(sbi) ? 1 : 0);
4210 	log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
4211 	if (!user_block_count || user_block_count >=
4212 			segment_count_main << log_blocks_per_seg) {
4213 		f2fs_err(sbi, "Wrong user_block_count: %u",
4214 			 user_block_count);
4215 		return 1;
4216 	}
4217 
4218 	valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
4219 	if (valid_user_blocks > user_block_count) {
4220 		f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
4221 			 valid_user_blocks, user_block_count);
4222 		return 1;
4223 	}
4224 
4225 	valid_node_count = le32_to_cpu(ckpt->valid_node_count);
4226 	avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
4227 	if (valid_node_count > avail_node_count) {
4228 		f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
4229 			 valid_node_count, avail_node_count);
4230 		return 1;
4231 	}
4232 
4233 	main_segs = le32_to_cpu(raw_super->segment_count_main);
4234 	blocks_per_seg = BLKS_PER_SEG(sbi);
4235 
4236 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
4237 		if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
4238 			le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
4239 			return 1;
4240 
4241 		if (f2fs_sb_has_readonly(sbi))
4242 			goto check_data;
4243 
4244 		for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
4245 			if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
4246 				le32_to_cpu(ckpt->cur_node_segno[j])) {
4247 				f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
4248 					 i, j,
4249 					 le32_to_cpu(ckpt->cur_node_segno[i]));
4250 				return 1;
4251 			}
4252 		}
4253 	}
4254 check_data:
4255 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
4256 		if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
4257 			le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
4258 			return 1;
4259 
4260 		if (f2fs_sb_has_readonly(sbi))
4261 			goto skip_cross;
4262 
4263 		for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
4264 			if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
4265 				le32_to_cpu(ckpt->cur_data_segno[j])) {
4266 				f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
4267 					 i, j,
4268 					 le32_to_cpu(ckpt->cur_data_segno[i]));
4269 				return 1;
4270 			}
4271 		}
4272 	}
4273 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
4274 		for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
4275 			if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
4276 				le32_to_cpu(ckpt->cur_data_segno[j])) {
4277 				f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
4278 					 i, j,
4279 					 le32_to_cpu(ckpt->cur_node_segno[i]));
4280 				return 1;
4281 			}
4282 		}
4283 	}
4284 skip_cross:
4285 	sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
4286 	nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
4287 
4288 	if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
4289 		nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
4290 		f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
4291 			 sit_bitmap_size, nat_bitmap_size);
4292 		return 1;
4293 	}
4294 
4295 	sit_blk_cnt = DIV_ROUND_UP(main_segs, SIT_ENTRY_PER_BLOCK);
4296 	if (sit_bitmap_size * 8 < sit_blk_cnt) {
4297 		f2fs_err(sbi, "Wrong bitmap size: sit: %u, sit_blk_cnt:%u",
4298 			 sit_bitmap_size, sit_blk_cnt);
4299 		return 1;
4300 	}
4301 
4302 	cp_pack_start_sum = __start_sum_addr(sbi);
4303 	cp_payload = __cp_payload(sbi);
4304 	if (cp_pack_start_sum < cp_payload + 1 ||
4305 		cp_pack_start_sum > blocks_per_seg - 1 -
4306 			NR_CURSEG_PERSIST_TYPE) {
4307 		f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
4308 			 cp_pack_start_sum);
4309 		return 1;
4310 	}
4311 
4312 	if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
4313 		le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
4314 		f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
4315 			  "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
4316 			  "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
4317 			  le32_to_cpu(ckpt->checksum_offset));
4318 		return 1;
4319 	}
4320 
4321 	nat_blocks = nat_segs << log_blocks_per_seg;
4322 	nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
4323 	nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
4324 	if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
4325 		(cp_payload + F2FS_CP_PACKS +
4326 		NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
4327 		f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
4328 			  cp_payload, nat_bits_blocks);
4329 		return 1;
4330 	}
4331 
4332 	if (unlikely(f2fs_cp_error(sbi))) {
4333 		f2fs_err(sbi, "A bug case: need to run fsck");
4334 		return 1;
4335 	}
4336 	return 0;
4337 }
4338 
4339 static void init_sb_info(struct f2fs_sb_info *sbi)
4340 {
4341 	struct f2fs_super_block *raw_super = sbi->raw_super;
4342 	int i;
4343 
4344 	sbi->log_sectors_per_block =
4345 		le32_to_cpu(raw_super->log_sectors_per_block);
4346 	sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
4347 	sbi->blocksize = BIT(sbi->log_blocksize);
4348 	sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
4349 	sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
4350 	sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
4351 	sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
4352 	sbi->total_sections = le32_to_cpu(raw_super->section_count);
4353 	sbi->total_node_count = SEGS_TO_BLKS(sbi,
4354 			((le32_to_cpu(raw_super->segment_count_nat) / 2) *
4355 			NAT_ENTRY_PER_BLOCK));
4356 	sbi->allocate_section_hint = le32_to_cpu(raw_super->section_count);
4357 	sbi->allocate_section_policy = ALLOCATE_FORWARD_NOHINT;
4358 	F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
4359 	F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
4360 	F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
4361 	sbi->cur_victim_sec = NULL_SECNO;
4362 	sbi->gc_mode = GC_NORMAL;
4363 	sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
4364 	sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
4365 	sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
4366 	sbi->migration_granularity = SEGS_PER_SEC(sbi);
4367 	sbi->migration_window_granularity = f2fs_sb_has_blkzoned(sbi) ?
4368 		DEF_MIGRATION_WINDOW_GRANULARITY_ZONED : SEGS_PER_SEC(sbi);
4369 	sbi->seq_file_ra_mul = MIN_RA_MUL;
4370 	sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
4371 	sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
4372 	spin_lock_init(&sbi->gc_remaining_trials_lock);
4373 	atomic64_set(&sbi->current_atomic_write, 0);
4374 	sbi->max_lock_elapsed_time = MAX_LOCK_ELAPSED_TIME;
4375 	sbi->adjust_lock_priority = 0;
4376 	sbi->lock_duration_priority = F2FS_DEFAULT_TASK_PRIORITY;
4377 	sbi->critical_task_priority = F2FS_CRITICAL_TASK_PRIORITY;
4378 
4379 	sbi->sum_blocksize = f2fs_sb_has_packed_ssa(sbi) ?
4380 		4096 : sbi->blocksize;
4381 	sbi->sums_per_block = sbi->blocksize / sbi->sum_blocksize;
4382 	sbi->entries_in_sum = sbi->sum_blocksize / 8;
4383 	sbi->sum_entry_size = SUMMARY_SIZE * sbi->entries_in_sum;
4384 	sbi->sum_journal_size = sbi->sum_blocksize - SUM_FOOTER_SIZE -
4385 		sbi->sum_entry_size;
4386 	sbi->nat_journal_entries = (sbi->sum_journal_size - 2) /
4387 		sizeof(struct nat_journal_entry);
4388 	sbi->sit_journal_entries = (sbi->sum_journal_size - 2) /
4389 		sizeof(struct sit_journal_entry);
4390 
4391 	sbi->dir_level = DEF_DIR_LEVEL;
4392 	sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
4393 	sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
4394 	sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
4395 	sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
4396 	sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
4397 	sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
4398 				DEF_UMOUNT_DISCARD_TIMEOUT;
4399 	clear_sbi_flag(sbi, SBI_NEED_FSCK);
4400 
4401 	for (i = 0; i < NR_COUNT_TYPE; i++)
4402 		atomic_set(&sbi->nr_pages[i], 0);
4403 
4404 	for (i = 0; i < META; i++)
4405 		atomic_set(&sbi->wb_sync_req[i], 0);
4406 
4407 	INIT_LIST_HEAD(&sbi->s_list);
4408 	mutex_init(&sbi->umount_mutex);
4409 	init_f2fs_rwsem(&sbi->io_order_lock);
4410 	spin_lock_init(&sbi->cp_lock);
4411 
4412 	sbi->dirty_device = 0;
4413 	spin_lock_init(&sbi->dev_lock);
4414 
4415 	init_f2fs_rwsem(&sbi->sb_lock);
4416 	init_f2fs_rwsem(&sbi->pin_sem);
4417 }
4418 
4419 static int init_percpu_info(struct f2fs_sb_info *sbi)
4420 {
4421 	int err;
4422 
4423 	err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
4424 	if (err)
4425 		return err;
4426 
4427 	err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
4428 	if (err)
4429 		goto err_valid_block;
4430 
4431 	err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
4432 								GFP_KERNEL);
4433 	if (err)
4434 		goto err_node_block;
4435 	return 0;
4436 
4437 err_node_block:
4438 	percpu_counter_destroy(&sbi->rf_node_block_count);
4439 err_valid_block:
4440 	percpu_counter_destroy(&sbi->alloc_valid_block_count);
4441 	return err;
4442 }
4443 
4444 #ifdef CONFIG_BLK_DEV_ZONED
4445 
4446 struct f2fs_report_zones_args {
4447 	struct f2fs_sb_info *sbi;
4448 	struct f2fs_dev_info *dev;
4449 };
4450 
4451 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
4452 			      void *data)
4453 {
4454 	struct f2fs_report_zones_args *rz_args = data;
4455 	block_t unusable_blocks = (zone->len - zone->capacity) >>
4456 					F2FS_LOG_SECTORS_PER_BLOCK;
4457 
4458 	if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
4459 		return 0;
4460 
4461 	set_bit(idx, rz_args->dev->blkz_seq);
4462 	if (!rz_args->sbi->unusable_blocks_per_sec) {
4463 		rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
4464 		return 0;
4465 	}
4466 	if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
4467 		f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
4468 		return -EINVAL;
4469 	}
4470 	return 0;
4471 }
4472 
4473 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
4474 {
4475 	struct block_device *bdev = FDEV(devi).bdev;
4476 	sector_t nr_sectors = bdev_nr_sectors(bdev);
4477 	struct f2fs_report_zones_args rep_zone_arg;
4478 	u64 zone_sectors;
4479 	unsigned int max_open_zones;
4480 	int ret;
4481 
4482 	if (!f2fs_sb_has_blkzoned(sbi))
4483 		return 0;
4484 
4485 	if (bdev_is_zoned(FDEV(devi).bdev)) {
4486 		max_open_zones = bdev_max_open_zones(bdev);
4487 		if (max_open_zones && (max_open_zones < sbi->max_open_zones))
4488 			sbi->max_open_zones = max_open_zones;
4489 		if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
4490 			f2fs_err(sbi,
4491 				"zoned: max open zones %u is too small, need at least %u open zones",
4492 				sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
4493 			return -EINVAL;
4494 		}
4495 	}
4496 
4497 	zone_sectors = bdev_zone_sectors(bdev);
4498 	if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
4499 				SECTOR_TO_BLOCK(zone_sectors))
4500 		return -EINVAL;
4501 	sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
4502 	FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
4503 					sbi->blocks_per_blkz);
4504 	if (nr_sectors & (zone_sectors - 1))
4505 		FDEV(devi).nr_blkz++;
4506 
4507 	FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
4508 					BITS_TO_LONGS(FDEV(devi).nr_blkz)
4509 					* sizeof(unsigned long),
4510 					GFP_KERNEL);
4511 	if (!FDEV(devi).blkz_seq)
4512 		return -ENOMEM;
4513 
4514 	rep_zone_arg.sbi = sbi;
4515 	rep_zone_arg.dev = &FDEV(devi);
4516 
4517 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
4518 				  &rep_zone_arg);
4519 	if (ret < 0)
4520 		return ret;
4521 	return 0;
4522 }
4523 #endif
4524 
4525 /*
4526  * Read f2fs raw super block.
4527  * Because we have two copies of super block, so read both of them
4528  * to get the first valid one. If any one of them is broken, we pass
4529  * them recovery flag back to the caller.
4530  */
4531 static int read_raw_super_block(struct f2fs_sb_info *sbi,
4532 			struct f2fs_super_block **raw_super,
4533 			int *valid_super_block, int *recovery)
4534 {
4535 	struct super_block *sb = sbi->sb;
4536 	int block;
4537 	struct folio *folio;
4538 	struct f2fs_super_block *super;
4539 	int err = 0;
4540 
4541 	super = kzalloc_obj(struct f2fs_super_block);
4542 	if (!super)
4543 		return -ENOMEM;
4544 
4545 	for (block = 0; block < 2; block++) {
4546 		folio = read_mapping_folio(sb->s_bdev->bd_mapping, block, NULL);
4547 		if (IS_ERR(folio)) {
4548 			f2fs_err(sbi, "Unable to read %dth superblock",
4549 				 block + 1);
4550 			err = PTR_ERR(folio);
4551 			*recovery = 1;
4552 			continue;
4553 		}
4554 
4555 		/* sanity checking of raw super */
4556 		err = sanity_check_raw_super(sbi, folio, block);
4557 		if (err) {
4558 			f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
4559 				 block + 1);
4560 			folio_put(folio);
4561 			*recovery = 1;
4562 			continue;
4563 		}
4564 
4565 		if (!*raw_super) {
4566 			memcpy(super, F2FS_SUPER_BLOCK(folio, block),
4567 							sizeof(*super));
4568 			*valid_super_block = block;
4569 			*raw_super = super;
4570 		}
4571 		folio_put(folio);
4572 	}
4573 
4574 	/* No valid superblock */
4575 	if (!*raw_super)
4576 		kfree(super);
4577 	else
4578 		err = 0;
4579 
4580 	return err;
4581 }
4582 
4583 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
4584 {
4585 	struct folio *folio;
4586 	pgoff_t index;
4587 	__u32 crc = 0;
4588 	int err;
4589 
4590 	if ((recover && f2fs_readonly(sbi->sb)) ||
4591 				f2fs_hw_is_readonly(sbi)) {
4592 		set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
4593 		return -EROFS;
4594 	}
4595 
4596 	/* we should update superblock crc here */
4597 	if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
4598 		crc = f2fs_crc32(F2FS_RAW_SUPER(sbi),
4599 				offsetof(struct f2fs_super_block, crc));
4600 		F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
4601 	}
4602 
4603 	/* write back-up superblock first */
4604 	index = sbi->valid_super_block ? 0 : 1;
4605 	folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
4606 	if (IS_ERR(folio))
4607 		return PTR_ERR(folio);
4608 	err = __f2fs_commit_super(sbi, folio, index, true);
4609 	folio_put(folio);
4610 
4611 	/* if we are in recovery path, skip writing valid superblock */
4612 	if (recover || err)
4613 		return err;
4614 
4615 	/* write current valid superblock */
4616 	index = sbi->valid_super_block;
4617 	folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
4618 	if (IS_ERR(folio))
4619 		return PTR_ERR(folio);
4620 	err = __f2fs_commit_super(sbi, folio, index, true);
4621 	folio_put(folio);
4622 	return err;
4623 }
4624 
4625 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
4626 {
4627 	unsigned long flags;
4628 
4629 	spin_lock_irqsave(&sbi->error_lock, flags);
4630 	if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
4631 		sbi->stop_reason[reason]++;
4632 	sbi->stop_reason_dirty = true;
4633 	spin_unlock_irqrestore(&sbi->error_lock, flags);
4634 }
4635 
4636 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
4637 {
4638 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4639 	unsigned long flags;
4640 	bool report_shutdown = false;
4641 	int err;
4642 
4643 	f2fs_down_write(&sbi->sb_lock);
4644 
4645 	spin_lock_irqsave(&sbi->error_lock, flags);
4646 	if (sbi->error_dirty) {
4647 		memcpy(raw_super->s_errors, sbi->errors, MAX_F2FS_ERRORS);
4648 		sbi->error_dirty = false;
4649 	}
4650 	memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
4651 	if (sbi->stop_reason_dirty) {
4652 		report_shutdown = true;
4653 		sbi->stop_reason_dirty = false;
4654 	}
4655 	spin_unlock_irqrestore(&sbi->error_lock, flags);
4656 
4657 	err = f2fs_commit_super(sbi, false);
4658 
4659 	f2fs_up_write(&sbi->sb_lock);
4660 	if (err)
4661 		f2fs_err_ratelimited(sbi,
4662 			"f2fs_commit_super fails to record stop_reason, err:%d",
4663 			err);
4664 
4665 	if (report_shutdown)
4666 		fserror_report_shutdown(sbi->sb, GFP_NOFS);
4667 }
4668 
4669 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
4670 {
4671 	unsigned long flags;
4672 
4673 	spin_lock_irqsave(&sbi->error_lock, flags);
4674 	if (!test_bit(flag, (unsigned long *)sbi->errors)) {
4675 		set_bit(flag, (unsigned long *)sbi->errors);
4676 		sbi->error_dirty = true;
4677 	}
4678 	spin_unlock_irqrestore(&sbi->error_lock, flags);
4679 }
4680 
4681 static void f2fs_report_fserror(struct f2fs_sb_info *sbi, unsigned char error)
4682 {
4683 	switch (error) {
4684 	case ERROR_INVALID_BLKADDR:
4685 	case ERROR_CORRUPTED_INODE:
4686 	case ERROR_INCONSISTENT_SUMMARY:
4687 	case ERROR_INCONSISTENT_SUM_TYPE:
4688 	case ERROR_CORRUPTED_JOURNAL:
4689 	case ERROR_INCONSISTENT_NODE_COUNT:
4690 	case ERROR_INCONSISTENT_BLOCK_COUNT:
4691 	case ERROR_INVALID_CURSEG:
4692 	case ERROR_INCONSISTENT_SIT:
4693 	case ERROR_INVALID_NODE_REFERENCE:
4694 	case ERROR_INCONSISTENT_NAT:
4695 		fserror_report_metadata(sbi->sb, -EFSCORRUPTED, GFP_NOFS);
4696 		break;
4697 	default:
4698 		return;
4699 	}
4700 }
4701 
4702 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4703 {
4704 	f2fs_save_errors(sbi, error);
4705 
4706 	if (!sbi->error_dirty)
4707 		return;
4708 	if (!test_bit(error, (unsigned long *)sbi->errors))
4709 		return;
4710 	schedule_work(&sbi->s_error_work);
4711 
4712 	f2fs_report_fserror(sbi, error);
4713 }
4714 
4715 static bool system_going_down(void)
4716 {
4717 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4718 		|| system_state == SYSTEM_RESTART;
4719 }
4720 
4721 static void f2fs_handle_critical_error(struct f2fs_sb_info *sbi,
4722 						unsigned char reason)
4723 {
4724 	struct super_block *sb = sbi->sb;
4725 	bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
4726 	bool continue_fs = !shutdown &&
4727 			F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
4728 
4729 	set_ckpt_flags(sbi, CP_ERROR_FLAG);
4730 
4731 	if (!f2fs_hw_is_readonly(sbi)) {
4732 		save_stop_reason(sbi, reason);
4733 
4734 		/*
4735 		 * always create an asynchronous task to record stop_reason
4736 		 * in order to avoid potential deadlock when running into
4737 		 * f2fs_record_stop_reason() synchronously.
4738 		 */
4739 		schedule_work(&sbi->s_error_work);
4740 	}
4741 
4742 	/*
4743 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4744 	 * could panic during 'reboot -f' as the underlying device got already
4745 	 * disabled.
4746 	 */
4747 	if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
4748 				!shutdown && !system_going_down() &&
4749 				!is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
4750 		panic("F2FS-fs (device %s): panic forced after error\n",
4751 							sb->s_id);
4752 
4753 	if (shutdown)
4754 		set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
4755 	else
4756 		dump_stack();
4757 
4758 	/*
4759 	 * Continue filesystem operators if errors=continue. Should not set
4760 	 * RO by shutdown, since RO bypasses thaw_super which can hang the
4761 	 * system.
4762 	 */
4763 	if (continue_fs || f2fs_readonly(sb) || shutdown) {
4764 		f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason);
4765 		return;
4766 	}
4767 
4768 	f2fs_warn(sbi, "Remounting filesystem read-only");
4769 
4770 	/*
4771 	 * We have already set CP_ERROR_FLAG flag to stop all updates
4772 	 * to filesystem, so it doesn't need to set SB_RDONLY flag here
4773 	 * because the flag should be set covered w/ sb->s_umount semaphore
4774 	 * via remount procedure, otherwise, it will confuse code like
4775 	 * freeze_super() which will lead to deadlocks and other problems.
4776 	 */
4777 }
4778 
4779 void f2fs_fault_report(struct super_block *sb, unsigned int err_code,
4780 			const char *func, unsigned int data)
4781 {
4782 	trace_f2fs_fault_report(sb, err_code, func, data);
4783 }
4784 
4785 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
4786 						unsigned char reason)
4787 {
4788 	if (reason != STOP_CP_REASON_SHUTDOWN)
4789 		f2fs_fault_report(sbi->sb, REPORT_FAULT_STOP_CP, __func__, reason);
4790 
4791 	f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL);
4792 	if (!end_io)
4793 		f2fs_flush_merged_writes(sbi);
4794 	f2fs_handle_critical_error(sbi, reason);
4795 }
4796 
4797 
4798 static void f2fs_record_error_work(struct work_struct *work)
4799 {
4800 	struct f2fs_sb_info *sbi = container_of(work,
4801 					struct f2fs_sb_info, s_error_work);
4802 
4803 	f2fs_record_stop_reason(sbi);
4804 }
4805 
4806 static inline unsigned int get_first_seq_zone_segno(struct f2fs_sb_info *sbi)
4807 {
4808 #ifdef CONFIG_BLK_DEV_ZONED
4809 	unsigned int zoneno, total_zones;
4810 	int devi;
4811 
4812 	if (!f2fs_sb_has_blkzoned(sbi))
4813 		return NULL_SEGNO;
4814 
4815 	for (devi = 0; devi < sbi->s_ndevs; devi++) {
4816 		if (!bdev_is_zoned(FDEV(devi).bdev))
4817 			continue;
4818 
4819 		total_zones = GET_ZONE_FROM_SEG(sbi, FDEV(devi).total_segments);
4820 
4821 		for (zoneno = 0; zoneno < total_zones; zoneno++) {
4822 			unsigned int segs, blks;
4823 
4824 			if (!f2fs_zone_is_seq(sbi, devi, zoneno))
4825 				continue;
4826 
4827 			segs = GET_SEG_FROM_SEC(sbi,
4828 					zoneno * sbi->secs_per_zone);
4829 			blks = SEGS_TO_BLKS(sbi, segs);
4830 			return GET_SEGNO(sbi, FDEV(devi).start_blk + blks);
4831 		}
4832 	}
4833 #endif
4834 	return NULL_SEGNO;
4835 }
4836 
4837 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
4838 {
4839 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4840 	unsigned int max_devices = MAX_DEVICES;
4841 	unsigned int logical_blksize;
4842 	blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
4843 	int i;
4844 
4845 	/* Initialize single device information */
4846 	if (!RDEV(0).path[0]) {
4847 		if (!bdev_is_zoned(sbi->sb->s_bdev))
4848 			return 0;
4849 		max_devices = 1;
4850 	}
4851 
4852 	/*
4853 	 * Initialize multiple devices information, or single
4854 	 * zoned block device information.
4855 	 */
4856 	sbi->devs = f2fs_kzalloc(sbi,
4857 				 array_size(max_devices,
4858 					    sizeof(struct f2fs_dev_info)),
4859 				 GFP_KERNEL);
4860 	if (!sbi->devs)
4861 		return -ENOMEM;
4862 
4863 	logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
4864 	sbi->aligned_blksize = true;
4865 	sbi->bggc_io_aware = AWARE_ALL_IO;
4866 #ifdef CONFIG_BLK_DEV_ZONED
4867 	sbi->max_open_zones = UINT_MAX;
4868 	sbi->blkzone_alloc_policy = BLKZONE_ALLOC_PRIOR_SEQ;
4869 	sbi->bggc_io_aware = AWARE_READ_IO;
4870 #endif
4871 
4872 	for (i = 0; i < max_devices; i++) {
4873 		if (max_devices == 1) {
4874 			FDEV(i).total_segments =
4875 				le32_to_cpu(raw_super->segment_count_main);
4876 			FDEV(i).start_blk = 0;
4877 			FDEV(i).end_blk = FDEV(i).total_segments *
4878 						BLKS_PER_SEG(sbi);
4879 		}
4880 
4881 		if (i == 0)
4882 			FDEV(0).bdev_file = sbi->sb->s_bdev_file;
4883 		else if (!RDEV(i).path[0])
4884 			break;
4885 
4886 		if (max_devices > 1) {
4887 			/* Multi-device mount */
4888 			memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
4889 			FDEV(i).total_segments =
4890 				le32_to_cpu(RDEV(i).total_segments);
4891 			if (i == 0) {
4892 				FDEV(i).start_blk = 0;
4893 				FDEV(i).end_blk = FDEV(i).start_blk +
4894 					SEGS_TO_BLKS(sbi,
4895 					FDEV(i).total_segments) - 1 +
4896 					le32_to_cpu(raw_super->segment0_blkaddr);
4897 				sbi->allocate_section_hint = FDEV(i).total_segments /
4898 							SEGS_PER_SEC(sbi);
4899 			} else {
4900 				FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
4901 				FDEV(i).end_blk = FDEV(i).start_blk +
4902 						SEGS_TO_BLKS(sbi,
4903 						FDEV(i).total_segments) - 1;
4904 				FDEV(i).bdev_file = fs_bdev_file_open_by_path(
4905 					FDEV(i).path, mode, sbi->sb, sbi->sb);
4906 			}
4907 		}
4908 		if (IS_ERR(FDEV(i).bdev_file))
4909 			return PTR_ERR(FDEV(i).bdev_file);
4910 
4911 		FDEV(i).bdev = file_bdev(FDEV(i).bdev_file);
4912 		/* to release errored devices */
4913 		sbi->s_ndevs = i + 1;
4914 
4915 		if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
4916 			sbi->aligned_blksize = false;
4917 
4918 #ifdef CONFIG_BLK_DEV_ZONED
4919 		if (bdev_is_zoned(FDEV(i).bdev)) {
4920 			if (!f2fs_sb_has_blkzoned(sbi)) {
4921 				f2fs_err(sbi, "Zoned block device feature not enabled");
4922 				return -EINVAL;
4923 			}
4924 			if (init_blkz_info(sbi, i)) {
4925 				f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
4926 				return -EINVAL;
4927 			}
4928 			if (max_devices == 1)
4929 				break;
4930 			f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: Host-managed)",
4931 				  i, FDEV(i).path,
4932 				  FDEV(i).total_segments,
4933 				  FDEV(i).start_blk, FDEV(i).end_blk);
4934 			continue;
4935 		}
4936 #endif
4937 		f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
4938 			  i, FDEV(i).path,
4939 			  FDEV(i).total_segments,
4940 			  FDEV(i).start_blk, FDEV(i).end_blk);
4941 	}
4942 	return 0;
4943 }
4944 
4945 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
4946 {
4947 #if IS_ENABLED(CONFIG_UNICODE)
4948 	if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
4949 		const struct f2fs_sb_encodings *encoding_info;
4950 		struct unicode_map *encoding;
4951 		__u16 encoding_flags;
4952 
4953 		encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
4954 		if (!encoding_info) {
4955 			f2fs_err(sbi,
4956 				 "Encoding requested by superblock is unknown");
4957 			return -EINVAL;
4958 		}
4959 
4960 		encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
4961 		encoding = utf8_load(encoding_info->version);
4962 		if (IS_ERR(encoding)) {
4963 			f2fs_err(sbi,
4964 				 "can't mount with superblock charset: %s-%u.%u.%u "
4965 				 "not supported by the kernel. flags: 0x%x.",
4966 				 encoding_info->name,
4967 				 unicode_major(encoding_info->version),
4968 				 unicode_minor(encoding_info->version),
4969 				 unicode_rev(encoding_info->version),
4970 				 encoding_flags);
4971 			return PTR_ERR(encoding);
4972 		}
4973 		f2fs_info(sbi, "Using encoding defined by superblock: "
4974 			 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4975 			 unicode_major(encoding_info->version),
4976 			 unicode_minor(encoding_info->version),
4977 			 unicode_rev(encoding_info->version),
4978 			 encoding_flags);
4979 
4980 		sbi->sb->s_encoding = encoding;
4981 		sbi->sb->s_encoding_flags = encoding_flags;
4982 	}
4983 #else
4984 	if (f2fs_sb_has_casefold(sbi)) {
4985 		f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
4986 		return -EINVAL;
4987 	}
4988 #endif
4989 	return 0;
4990 }
4991 
4992 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
4993 {
4994 	/* adjust parameters according to the volume size */
4995 	if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
4996 		if (f2fs_block_unit_discard(sbi))
4997 			SM_I(sbi)->dcc_info->discard_granularity =
4998 						MIN_DISCARD_GRANULARITY;
4999 		if (!f2fs_lfs_mode(sbi))
5000 			SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
5001 						BIT(F2FS_IPU_HONOR_OPU_WRITE);
5002 	}
5003 
5004 	sbi->readdir_ra = true;
5005 }
5006 
5007 static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
5008 {
5009 	struct f2fs_fs_context *ctx = fc->fs_private;
5010 	struct f2fs_sb_info *sbi;
5011 	struct f2fs_super_block *raw_super;
5012 	struct inode *root;
5013 	int err;
5014 	bool skip_recovery = false, need_fsck = false;
5015 	int recovery, i, valid_super_block;
5016 	struct curseg_info *seg_i;
5017 	int retry_cnt = 1;
5018 #ifdef CONFIG_QUOTA
5019 	bool quota_enabled = false;
5020 #endif
5021 
5022 try_onemore:
5023 	err = -EINVAL;
5024 	raw_super = NULL;
5025 	valid_super_block = -1;
5026 	recovery = 0;
5027 
5028 	/* allocate memory for f2fs-specific super block info */
5029 	sbi = kzalloc_obj(struct f2fs_sb_info);
5030 	if (!sbi)
5031 		return -ENOMEM;
5032 
5033 	sbi->sb = sb;
5034 
5035 	/* initialize locks within allocated memory */
5036 	init_f2fs_rwsem_trace(&sbi->gc_lock, sbi, LOCK_NAME_GC_LOCK);
5037 	mutex_init(&sbi->writepages);
5038 	init_f2fs_rwsem_trace(&sbi->cp_global_sem, sbi, LOCK_NAME_CP_GLOBAL);
5039 #ifdef CONFIG_DEBUG_LOCK_ALLOC
5040 	lockdep_register_key(&sbi->cp_global_sem_key);
5041 	lockdep_set_class(&sbi->cp_global_sem.internal_rwsem,
5042 					&sbi->cp_global_sem_key);
5043 #endif
5044 	init_f2fs_rwsem_trace(&sbi->node_write, sbi, LOCK_NAME_NODE_WRITE);
5045 	init_f2fs_rwsem_trace(&sbi->node_change, sbi, LOCK_NAME_NODE_CHANGE);
5046 	spin_lock_init(&sbi->stat_lock);
5047 	init_f2fs_rwsem_trace(&sbi->cp_rwsem, sbi, LOCK_NAME_CP_RWSEM);
5048 	init_f2fs_rwsem(&sbi->quota_sem);
5049 	init_waitqueue_head(&sbi->cp_wait);
5050 	spin_lock_init(&sbi->error_lock);
5051 
5052 	for (i = 0; i < NR_INODE_TYPE; i++) {
5053 		INIT_LIST_HEAD(&sbi->inode_list[i]);
5054 		spin_lock_init(&sbi->inode_lock[i]);
5055 	}
5056 	mutex_init(&sbi->flush_lock);
5057 
5058 	/* set a block size */
5059 	if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
5060 		f2fs_err(sbi, "unable to set blocksize");
5061 		goto free_sbi;
5062 	}
5063 
5064 	err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
5065 								&recovery);
5066 	if (err)
5067 		goto free_sbi;
5068 
5069 	sb->s_fs_info = sbi;
5070 	sbi->raw_super = raw_super;
5071 
5072 	INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
5073 	memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
5074 	memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
5075 
5076 	/* precompute checksum seed for metadata */
5077 	if (f2fs_sb_has_inode_chksum(sbi))
5078 		sbi->s_chksum_seed = f2fs_chksum(~0, raw_super->uuid,
5079 						 sizeof(raw_super->uuid));
5080 
5081 	default_options(sbi, false);
5082 
5083 	err = f2fs_check_opt_consistency(fc, sb);
5084 	if (err)
5085 		goto free_sb_buf;
5086 
5087 	f2fs_apply_options(fc, sb);
5088 
5089 	err = f2fs_sanity_check_options(sbi, false);
5090 	if (err)
5091 		goto free_options;
5092 
5093 	sb->s_maxbytes = max_file_blocks(NULL) <<
5094 				le32_to_cpu(raw_super->log_blocksize);
5095 	sb->s_max_links = F2FS_LINK_MAX;
5096 
5097 	err = f2fs_setup_casefold(sbi);
5098 	if (err)
5099 		goto free_options;
5100 
5101 #ifdef CONFIG_QUOTA
5102 	sb->dq_op = &f2fs_quota_operations;
5103 	sb->s_qcop = &f2fs_quotactl_ops;
5104 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
5105 
5106 	if (f2fs_sb_has_quota_ino(sbi)) {
5107 		for (i = 0; i < MAXQUOTAS; i++) {
5108 			if (f2fs_qf_ino(sbi->sb, i))
5109 				sbi->nquota_files++;
5110 		}
5111 	}
5112 #endif
5113 
5114 	sb->s_op = &f2fs_sops;
5115 #ifdef CONFIG_FS_ENCRYPTION
5116 	sb->s_cop = &f2fs_cryptops;
5117 #endif
5118 #ifdef CONFIG_FS_VERITY
5119 	sb->s_vop = &f2fs_verityops;
5120 #endif
5121 	sb->s_xattr = f2fs_xattr_handlers;
5122 	sb->s_export_op = &f2fs_export_ops;
5123 	sb->s_magic = F2FS_SUPER_MAGIC;
5124 	sb->s_time_gran = 1;
5125 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
5126 		(test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
5127 	if (test_opt(sbi, INLINECRYPT))
5128 		sb->s_flags |= SB_INLINECRYPT;
5129 
5130 	if (test_opt(sbi, LAZYTIME))
5131 		sb->s_flags |= SB_LAZYTIME;
5132 	else
5133 		sb->s_flags &= ~SB_LAZYTIME;
5134 
5135 	super_set_uuid(sb, (void *) raw_super->uuid, sizeof(raw_super->uuid));
5136 	super_set_sysfs_name_bdev(sb);
5137 	sb->s_iflags |= SB_I_CGROUPWB;
5138 
5139 	/* init f2fs-specific super block info */
5140 	sbi->valid_super_block = valid_super_block;
5141 
5142 	/* disallow all the data/node/meta page writes */
5143 	set_sbi_flag(sbi, SBI_POR_DOING);
5144 
5145 	err = f2fs_init_write_merge_io(sbi);
5146 	if (err)
5147 		goto free_bio_info;
5148 
5149 	init_sb_info(sbi);
5150 
5151 	err = f2fs_init_iostat(sbi);
5152 	if (err)
5153 		goto free_bio_info;
5154 
5155 	err = init_percpu_info(sbi);
5156 	if (err)
5157 		goto free_iostat;
5158 
5159 	err = f2fs_init_page_array_cache(sbi);
5160 	if (err)
5161 		goto free_percpu;
5162 
5163 	/* get an inode for meta space */
5164 	sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
5165 	if (IS_ERR(sbi->meta_inode)) {
5166 		f2fs_err(sbi, "Failed to read F2FS meta data inode");
5167 		err = PTR_ERR(sbi->meta_inode);
5168 		goto free_page_array_cache;
5169 	}
5170 
5171 	err = f2fs_get_valid_checkpoint(sbi);
5172 	if (err) {
5173 		f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
5174 		goto free_meta_inode;
5175 	}
5176 
5177 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
5178 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
5179 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
5180 		set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
5181 		sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
5182 	}
5183 
5184 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
5185 		set_sbi_flag(sbi, SBI_NEED_FSCK);
5186 
5187 	/* Initialize device list */
5188 	err = f2fs_scan_devices(sbi);
5189 	if (err) {
5190 		f2fs_err(sbi, "Failed to find devices");
5191 		goto free_devices;
5192 	}
5193 
5194 	err = f2fs_init_wq(sbi);
5195 	if (err) {
5196 		f2fs_err(sbi, "Failed to create workqueue");
5197 		goto free_devices;
5198 	}
5199 
5200 	sbi->total_valid_node_count =
5201 				le32_to_cpu(sbi->ckpt->valid_node_count);
5202 	percpu_counter_set(&sbi->total_valid_inode_count,
5203 				le32_to_cpu(sbi->ckpt->valid_inode_count));
5204 	sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
5205 	sbi->total_valid_block_count =
5206 				le64_to_cpu(sbi->ckpt->valid_block_count);
5207 	sbi->last_valid_block_count = sbi->total_valid_block_count;
5208 	sbi->reserved_blocks = 0;
5209 	sbi->current_reserved_blocks = 0;
5210 	limit_reserve_root(sbi);
5211 	adjust_unusable_cap_perc(sbi);
5212 
5213 	f2fs_init_extent_cache_info(sbi);
5214 
5215 	f2fs_init_ino_entry_info(sbi);
5216 
5217 	f2fs_init_fsync_node_info(sbi);
5218 
5219 	/* setup checkpoint request control and start checkpoint issue thread */
5220 	f2fs_init_ckpt_req_control(sbi);
5221 	if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
5222 			test_opt(sbi, MERGE_CHECKPOINT)) {
5223 		err = f2fs_start_ckpt_thread(sbi);
5224 		if (err) {
5225 			f2fs_err(sbi,
5226 			    "Failed to start F2FS issue_checkpoint_thread (%d)",
5227 			    err);
5228 			goto stop_ckpt_thread;
5229 		}
5230 	}
5231 
5232 	/* setup f2fs internal modules */
5233 	err = f2fs_build_segment_manager(sbi);
5234 	if (err) {
5235 		f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
5236 			 err);
5237 		goto free_sm;
5238 	}
5239 	err = f2fs_build_node_manager(sbi);
5240 	if (err) {
5241 		f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
5242 			 err);
5243 		goto free_nm;
5244 	}
5245 
5246 	/* For write statistics */
5247 	sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
5248 
5249 	/* get segno of first zoned block device */
5250 	sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi);
5251 
5252 	sbi->reserved_pin_section = f2fs_sb_has_blkzoned(sbi) ?
5253 			ZONED_PIN_SEC_REQUIRED_COUNT :
5254 			GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi));
5255 
5256 	/* Read accumulated write IO statistics if exists */
5257 	seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
5258 	if (__exist_node_summaries(sbi))
5259 		sbi->kbytes_written =
5260 			le64_to_cpu(seg_i->journal->info.kbytes_written);
5261 
5262 	f2fs_build_gc_manager(sbi);
5263 
5264 	err = f2fs_build_stats(sbi);
5265 	if (err)
5266 		goto free_nm;
5267 
5268 	/* get an inode for node space */
5269 	sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
5270 	if (IS_ERR(sbi->node_inode)) {
5271 		f2fs_err(sbi, "Failed to read node inode");
5272 		err = PTR_ERR(sbi->node_inode);
5273 		goto free_stats;
5274 	}
5275 
5276 	/* read root inode and dentry */
5277 	root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
5278 	if (IS_ERR(root)) {
5279 		f2fs_err(sbi, "Failed to read root inode");
5280 		err = PTR_ERR(root);
5281 		goto free_node_inode;
5282 	}
5283 	if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
5284 			!root->i_size || !root->i_nlink) {
5285 		iput(root);
5286 		err = -EINVAL;
5287 		goto free_node_inode;
5288 	}
5289 
5290 	generic_set_sb_d_ops(sb);
5291 	sb->s_root = d_make_root(root); /* allocate root dentry */
5292 	if (!sb->s_root) {
5293 		err = -ENOMEM;
5294 		goto free_node_inode;
5295 	}
5296 
5297 	err = f2fs_init_compress_inode(sbi);
5298 	if (err)
5299 		goto free_root_inode;
5300 
5301 	err = f2fs_register_sysfs(sbi);
5302 	if (err)
5303 		goto free_compress_inode;
5304 
5305 	sbi->umount_lock_holder = current;
5306 #ifdef CONFIG_QUOTA
5307 	/* Enable quota usage during mount */
5308 	if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
5309 		err = f2fs_enable_quotas(sb);
5310 		if (err)
5311 			f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
5312 	}
5313 
5314 	quota_enabled = f2fs_recover_quota_begin(sbi);
5315 #endif
5316 	/* if there are any orphan inodes, free them */
5317 	err = f2fs_recover_orphan_inodes(sbi);
5318 	if (err)
5319 		goto free_meta;
5320 
5321 	if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))) {
5322 		skip_recovery = true;
5323 		goto reset_checkpoint;
5324 	}
5325 
5326 	/* recover fsynced data */
5327 	if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
5328 			!test_opt(sbi, NORECOVERY)) {
5329 		/*
5330 		 * mount should be failed, when device has readonly mode, and
5331 		 * previous checkpoint was not done by clean system shutdown.
5332 		 */
5333 		if (f2fs_hw_is_readonly(sbi)) {
5334 			if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
5335 				err = f2fs_recover_fsync_data(sbi, true);
5336 				if (err > 0) {
5337 					err = -EROFS;
5338 					f2fs_err(sbi, "Need to recover fsync data, but "
5339 						"write access unavailable, please try "
5340 						"mount w/ disable_roll_forward or norecovery");
5341 				}
5342 				if (err < 0)
5343 					goto free_meta;
5344 			}
5345 			f2fs_info(sbi, "write access unavailable, skipping recovery");
5346 			goto reset_checkpoint;
5347 		}
5348 
5349 		if (need_fsck)
5350 			set_sbi_flag(sbi, SBI_NEED_FSCK);
5351 
5352 		if (skip_recovery)
5353 			goto reset_checkpoint;
5354 
5355 		err = f2fs_recover_fsync_data(sbi, false);
5356 		if (err < 0) {
5357 			if (err != -ENOMEM)
5358 				skip_recovery = true;
5359 			need_fsck = true;
5360 			f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
5361 				 err);
5362 			goto free_meta;
5363 		}
5364 	} else {
5365 		err = f2fs_recover_fsync_data(sbi, true);
5366 		if (err > 0) {
5367 			if (!f2fs_readonly(sb)) {
5368 				f2fs_err(sbi, "Need to recover fsync data");
5369 				err = -EINVAL;
5370 				goto free_meta;
5371 			} else {
5372 				f2fs_info(sbi, "drop all fsynced data");
5373 				err = 0;
5374 			}
5375 		}
5376 	}
5377 
5378 reset_checkpoint:
5379 #ifdef CONFIG_QUOTA
5380 	f2fs_recover_quota_end(sbi, quota_enabled);
5381 #endif
5382 	/*
5383 	 * If the f2fs is not readonly and fsync data recovery succeeds,
5384 	 * write pointer consistency of cursegs and other zones are already
5385 	 * checked and fixed during recovery. However, if recovery fails,
5386 	 * write pointers are left untouched, and retry-mount should check
5387 	 * them here.
5388 	 */
5389 	if (skip_recovery)
5390 		err = f2fs_check_and_fix_write_pointer(sbi);
5391 	if (err)
5392 		goto free_meta;
5393 
5394 	/* f2fs_recover_fsync_data() cleared this already */
5395 	clear_sbi_flag(sbi, SBI_POR_DOING);
5396 
5397 	err = f2fs_init_inmem_curseg(sbi);
5398 	if (err)
5399 		goto sync_free_meta;
5400 
5401 	if (test_opt(sbi, DISABLE_CHECKPOINT))
5402 		err = f2fs_disable_checkpoint(sbi);
5403 	else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))
5404 		err = f2fs_enable_checkpoint(sbi);
5405 	if (err)
5406 		goto sync_free_meta;
5407 
5408 	/*
5409 	 * If filesystem is not mounted as read-only then
5410 	 * do start the gc_thread.
5411 	 */
5412 	if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
5413 		test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
5414 		/* After POR, we can run background GC thread.*/
5415 		err = f2fs_start_gc_thread(sbi);
5416 		if (err)
5417 			goto sync_free_meta;
5418 	}
5419 
5420 	/* recover broken superblock */
5421 	if (recovery) {
5422 		err = f2fs_commit_super(sbi, true);
5423 		f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
5424 			  sbi->valid_super_block ? 1 : 2, err);
5425 	}
5426 
5427 	f2fs_join_shrinker(sbi);
5428 
5429 	f2fs_tuning_parameters(sbi);
5430 
5431 	f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
5432 		    cur_cp_version(F2FS_CKPT(sbi)));
5433 	f2fs_update_time(sbi, CP_TIME);
5434 	f2fs_update_time(sbi, REQ_TIME);
5435 	clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
5436 
5437 	sbi->umount_lock_holder = NULL;
5438 	return 0;
5439 
5440 sync_free_meta:
5441 	/* safe to flush all the data */
5442 	sync_filesystem(sbi->sb);
5443 	retry_cnt = 0;
5444 
5445 free_meta:
5446 #ifdef CONFIG_QUOTA
5447 	f2fs_truncate_quota_inode_pages(sb);
5448 	if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
5449 		f2fs_quota_off_umount(sbi->sb);
5450 #endif
5451 	/*
5452 	 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
5453 	 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
5454 	 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
5455 	 * falls into an infinite loop in f2fs_sync_meta_pages().
5456 	 */
5457 	truncate_inode_pages_final(META_MAPPING(sbi));
5458 	/* evict some inodes being cached by GC */
5459 	evict_inodes(sb);
5460 	f2fs_unregister_sysfs(sbi);
5461 free_compress_inode:
5462 	f2fs_destroy_compress_inode(sbi);
5463 free_root_inode:
5464 	dput(sb->s_root);
5465 	sb->s_root = NULL;
5466 free_node_inode:
5467 	f2fs_release_ino_entry(sbi, true);
5468 	truncate_inode_pages_final(NODE_MAPPING(sbi));
5469 	iput(sbi->node_inode);
5470 	sbi->node_inode = NULL;
5471 free_stats:
5472 	f2fs_destroy_stats(sbi);
5473 free_nm:
5474 	/* stop discard thread before destroying node manager */
5475 	f2fs_stop_discard_thread(sbi);
5476 	f2fs_destroy_node_manager(sbi);
5477 free_sm:
5478 	f2fs_destroy_segment_manager(sbi);
5479 stop_ckpt_thread:
5480 	f2fs_stop_ckpt_thread(sbi);
5481 	/* flush s_error_work before sbi destroy */
5482 	flush_work(&sbi->s_error_work);
5483 	f2fs_destroy_wq(sbi);
5484 free_devices:
5485 	destroy_device_list(sbi);
5486 	kvfree(sbi->ckpt);
5487 free_meta_inode:
5488 	make_bad_inode(sbi->meta_inode);
5489 	iput(sbi->meta_inode);
5490 	sbi->meta_inode = NULL;
5491 free_page_array_cache:
5492 	f2fs_destroy_page_array_cache(sbi);
5493 free_percpu:
5494 	destroy_percpu_info(sbi);
5495 free_iostat:
5496 	f2fs_destroy_iostat(sbi);
5497 free_bio_info:
5498 	for (i = 0; i < NR_PAGE_TYPE; i++)
5499 		kfree(sbi->write_io[i]);
5500 
5501 #if IS_ENABLED(CONFIG_UNICODE)
5502 	utf8_unload(sb->s_encoding);
5503 	sb->s_encoding = NULL;
5504 #endif
5505 free_options:
5506 #ifdef CONFIG_QUOTA
5507 	for (i = 0; i < MAXQUOTAS; i++)
5508 		kfree(F2FS_OPTION(sbi).s_qf_names[i]);
5509 #endif
5510 	/* no need to free dummy_enc_policy, we just keep it in ctx when failed */
5511 	swap(F2FS_CTX_INFO(ctx).dummy_enc_policy, F2FS_OPTION(sbi).dummy_enc_policy);
5512 free_sb_buf:
5513 	kfree(raw_super);
5514 free_sbi:
5515 #ifdef CONFIG_DEBUG_LOCK_ALLOC
5516 	lockdep_unregister_key(&sbi->cp_global_sem_key);
5517 #endif
5518 	kfree(sbi);
5519 	sb->s_fs_info = NULL;
5520 
5521 	/* give only one another chance */
5522 	if (retry_cnt > 0 && skip_recovery) {
5523 		retry_cnt--;
5524 		shrink_dcache_sb(sb);
5525 		goto try_onemore;
5526 	}
5527 	return err;
5528 }
5529 
5530 static int f2fs_get_tree(struct fs_context *fc)
5531 {
5532 	return get_tree_bdev(fc, f2fs_fill_super);
5533 }
5534 
5535 static int f2fs_reconfigure(struct fs_context *fc)
5536 {
5537 	struct super_block *sb = fc->root->d_sb;
5538 
5539 	return __f2fs_remount(fc, sb);
5540 }
5541 
5542 static void f2fs_fc_free(struct fs_context *fc)
5543 {
5544 	struct f2fs_fs_context *ctx = fc->fs_private;
5545 
5546 	if (!ctx)
5547 		return;
5548 
5549 #ifdef CONFIG_QUOTA
5550 	f2fs_unnote_qf_name_all(fc);
5551 #endif
5552 	fscrypt_free_dummy_policy(&F2FS_CTX_INFO(ctx).dummy_enc_policy);
5553 	kfree(ctx);
5554 }
5555 
5556 static const struct fs_context_operations f2fs_context_ops = {
5557 	.parse_param	= f2fs_parse_param,
5558 	.get_tree	= f2fs_get_tree,
5559 	.reconfigure = f2fs_reconfigure,
5560 	.free	= f2fs_fc_free,
5561 };
5562 
5563 static void kill_f2fs_super(struct super_block *sb)
5564 {
5565 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
5566 
5567 	if (sb->s_root) {
5568 		sbi->umount_lock_holder = current;
5569 
5570 		set_sbi_flag(sbi, SBI_IS_CLOSE);
5571 		f2fs_stop_gc_thread(sbi);
5572 		f2fs_stop_discard_thread(sbi);
5573 
5574 #ifdef CONFIG_F2FS_FS_COMPRESSION
5575 		/*
5576 		 * latter evict_inode() can bypass checking and invalidating
5577 		 * compress inode cache.
5578 		 */
5579 		if (test_opt(sbi, COMPRESS_CACHE))
5580 			truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
5581 #endif
5582 
5583 		if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
5584 				!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
5585 			struct cp_control cpc = {
5586 				.reason = CP_UMOUNT,
5587 			};
5588 			stat_inc_cp_call_count(sbi, TOTAL_CALL);
5589 			f2fs_write_checkpoint(sbi, &cpc);
5590 		}
5591 
5592 		if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
5593 			sb->s_flags &= ~SB_RDONLY;
5594 	}
5595 	kill_block_super(sb);
5596 	/* Release block devices last, after fscrypt_destroy_keyring(). */
5597 	if (sbi) {
5598 		destroy_device_list(sbi);
5599 #ifdef CONFIG_DEBUG_LOCK_ALLOC
5600 		lockdep_unregister_key(&sbi->cp_global_sem_key);
5601 #endif
5602 		kfree(sbi);
5603 		sb->s_fs_info = NULL;
5604 	}
5605 }
5606 
5607 static int f2fs_init_fs_context(struct fs_context *fc)
5608 {
5609 	struct f2fs_fs_context *ctx;
5610 
5611 	ctx = kzalloc_obj(struct f2fs_fs_context);
5612 	if (!ctx)
5613 		return -ENOMEM;
5614 
5615 	fc->fs_private = ctx;
5616 	fc->ops = &f2fs_context_ops;
5617 
5618 	return 0;
5619 }
5620 
5621 static struct file_system_type f2fs_fs_type = {
5622 	.owner		= THIS_MODULE,
5623 	.name		= "f2fs",
5624 	.init_fs_context = f2fs_init_fs_context,
5625 	.kill_sb	= kill_f2fs_super,
5626 	.fs_flags	= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
5627 };
5628 MODULE_ALIAS_FS("f2fs");
5629 
5630 static int __init init_inodecache(void)
5631 {
5632 	f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
5633 			sizeof(struct f2fs_inode_info), 0,
5634 			SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
5635 	return f2fs_inode_cachep ? 0 : -ENOMEM;
5636 }
5637 
5638 static void destroy_inodecache(void)
5639 {
5640 	/*
5641 	 * Make sure all delayed rcu free inodes are flushed before we
5642 	 * destroy cache.
5643 	 */
5644 	rcu_barrier();
5645 	kmem_cache_destroy(f2fs_inode_cachep);
5646 }
5647 
5648 static int __init init_f2fs_fs(void)
5649 {
5650 	int err;
5651 
5652 	err = init_inodecache();
5653 	if (err)
5654 		goto fail;
5655 	err = f2fs_create_node_manager_caches();
5656 	if (err)
5657 		goto free_inodecache;
5658 	err = f2fs_create_segment_manager_caches();
5659 	if (err)
5660 		goto free_node_manager_caches;
5661 	err = f2fs_create_checkpoint_caches();
5662 	if (err)
5663 		goto free_segment_manager_caches;
5664 	err = f2fs_create_recovery_cache();
5665 	if (err)
5666 		goto free_checkpoint_caches;
5667 	err = f2fs_create_extent_cache();
5668 	if (err)
5669 		goto free_recovery_cache;
5670 	err = f2fs_create_garbage_collection_cache();
5671 	if (err)
5672 		goto free_extent_cache;
5673 	err = f2fs_init_sysfs();
5674 	if (err)
5675 		goto free_garbage_collection_cache;
5676 	err = f2fs_init_shrinker();
5677 	if (err)
5678 		goto free_sysfs;
5679 	f2fs_create_root_stats();
5680 	err = f2fs_init_post_read_processing();
5681 	if (err)
5682 		goto free_root_stats;
5683 	err = f2fs_init_iostat_processing();
5684 	if (err)
5685 		goto free_post_read;
5686 	err = f2fs_init_bio_entry_cache();
5687 	if (err)
5688 		goto free_iostat;
5689 	err = f2fs_init_bioset();
5690 	if (err)
5691 		goto free_bio_entry_cache;
5692 	err = f2fs_init_compress_mempool();
5693 	if (err)
5694 		goto free_bioset;
5695 	err = f2fs_init_compress_cache();
5696 	if (err)
5697 		goto free_compress_mempool;
5698 	err = f2fs_create_casefold_cache();
5699 	if (err)
5700 		goto free_compress_cache;
5701 	err = f2fs_init_xattr_cache();
5702 	if (err)
5703 		goto free_casefold_cache;
5704 	err = register_filesystem(&f2fs_fs_type);
5705 	if (err)
5706 		goto free_xattr_cache;
5707 	return 0;
5708 free_xattr_cache:
5709 	f2fs_destroy_xattr_cache();
5710 free_casefold_cache:
5711 	f2fs_destroy_casefold_cache();
5712 free_compress_cache:
5713 	f2fs_destroy_compress_cache();
5714 free_compress_mempool:
5715 	f2fs_destroy_compress_mempool();
5716 free_bioset:
5717 	f2fs_destroy_bioset();
5718 free_bio_entry_cache:
5719 	f2fs_destroy_bio_entry_cache();
5720 free_iostat:
5721 	f2fs_destroy_iostat_processing();
5722 free_post_read:
5723 	f2fs_destroy_post_read_processing();
5724 free_root_stats:
5725 	f2fs_destroy_root_stats();
5726 	f2fs_exit_shrinker();
5727 free_sysfs:
5728 	f2fs_exit_sysfs();
5729 free_garbage_collection_cache:
5730 	f2fs_destroy_garbage_collection_cache();
5731 free_extent_cache:
5732 	f2fs_destroy_extent_cache();
5733 free_recovery_cache:
5734 	f2fs_destroy_recovery_cache();
5735 free_checkpoint_caches:
5736 	f2fs_destroy_checkpoint_caches();
5737 free_segment_manager_caches:
5738 	f2fs_destroy_segment_manager_caches();
5739 free_node_manager_caches:
5740 	f2fs_destroy_node_manager_caches();
5741 free_inodecache:
5742 	destroy_inodecache();
5743 fail:
5744 	return err;
5745 }
5746 
5747 static void __exit exit_f2fs_fs(void)
5748 {
5749 	unregister_filesystem(&f2fs_fs_type);
5750 	f2fs_destroy_xattr_cache();
5751 	f2fs_destroy_casefold_cache();
5752 	f2fs_destroy_compress_cache();
5753 	f2fs_destroy_compress_mempool();
5754 	f2fs_destroy_bioset();
5755 	f2fs_destroy_bio_entry_cache();
5756 	f2fs_destroy_iostat_processing();
5757 	f2fs_destroy_post_read_processing();
5758 	f2fs_destroy_root_stats();
5759 	f2fs_exit_shrinker();
5760 	f2fs_exit_sysfs();
5761 	f2fs_destroy_garbage_collection_cache();
5762 	f2fs_destroy_extent_cache();
5763 	f2fs_destroy_recovery_cache();
5764 	f2fs_destroy_checkpoint_caches();
5765 	f2fs_destroy_segment_manager_caches();
5766 	f2fs_destroy_node_manager_caches();
5767 	destroy_inodecache();
5768 }
5769 
5770 module_init(init_f2fs_fs)
5771 module_exit(exit_f2fs_fs)
5772 
5773 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
5774 MODULE_DESCRIPTION("Flash Friendly File System");
5775 MODULE_LICENSE("GPL");
5776