xref: /openbmc/linux/fs/f2fs/super.c (revision 6197e5b7)
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/statfs.h>
12 #include <linux/buffer_head.h>
13 #include <linux/backing-dev.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 
31 #include "f2fs.h"
32 #include "node.h"
33 #include "segment.h"
34 #include "xattr.h"
35 #include "gc.h"
36 
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/f2fs.h>
39 
40 static struct kmem_cache *f2fs_inode_cachep;
41 
42 #ifdef CONFIG_F2FS_FAULT_INJECTION
43 
44 const char *f2fs_fault_name[FAULT_MAX] = {
45 	[FAULT_KMALLOC]		= "kmalloc",
46 	[FAULT_KVMALLOC]	= "kvmalloc",
47 	[FAULT_PAGE_ALLOC]	= "page alloc",
48 	[FAULT_PAGE_GET]	= "page get",
49 	[FAULT_ALLOC_NID]	= "alloc nid",
50 	[FAULT_ORPHAN]		= "orphan",
51 	[FAULT_BLOCK]		= "no more block",
52 	[FAULT_DIR_DEPTH]	= "too big dir depth",
53 	[FAULT_EVICT_INODE]	= "evict_inode fail",
54 	[FAULT_TRUNCATE]	= "truncate fail",
55 	[FAULT_READ_IO]		= "read IO error",
56 	[FAULT_CHECKPOINT]	= "checkpoint error",
57 	[FAULT_DISCARD]		= "discard error",
58 	[FAULT_WRITE_IO]	= "write IO error",
59 };
60 
61 void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
62 							unsigned int type)
63 {
64 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
65 
66 	if (rate) {
67 		atomic_set(&ffi->inject_ops, 0);
68 		ffi->inject_rate = rate;
69 	}
70 
71 	if (type)
72 		ffi->inject_type = type;
73 
74 	if (!rate && !type)
75 		memset(ffi, 0, sizeof(struct f2fs_fault_info));
76 }
77 #endif
78 
79 /* f2fs-wide shrinker description */
80 static struct shrinker f2fs_shrinker_info = {
81 	.scan_objects = f2fs_shrink_scan,
82 	.count_objects = f2fs_shrink_count,
83 	.seeks = DEFAULT_SEEKS,
84 };
85 
86 enum {
87 	Opt_gc_background,
88 	Opt_disable_roll_forward,
89 	Opt_norecovery,
90 	Opt_discard,
91 	Opt_nodiscard,
92 	Opt_noheap,
93 	Opt_heap,
94 	Opt_user_xattr,
95 	Opt_nouser_xattr,
96 	Opt_acl,
97 	Opt_noacl,
98 	Opt_active_logs,
99 	Opt_disable_ext_identify,
100 	Opt_inline_xattr,
101 	Opt_noinline_xattr,
102 	Opt_inline_xattr_size,
103 	Opt_inline_data,
104 	Opt_inline_dentry,
105 	Opt_noinline_dentry,
106 	Opt_flush_merge,
107 	Opt_noflush_merge,
108 	Opt_nobarrier,
109 	Opt_fastboot,
110 	Opt_extent_cache,
111 	Opt_noextent_cache,
112 	Opt_noinline_data,
113 	Opt_data_flush,
114 	Opt_reserve_root,
115 	Opt_resgid,
116 	Opt_resuid,
117 	Opt_mode,
118 	Opt_io_size_bits,
119 	Opt_fault_injection,
120 	Opt_fault_type,
121 	Opt_lazytime,
122 	Opt_nolazytime,
123 	Opt_quota,
124 	Opt_noquota,
125 	Opt_usrquota,
126 	Opt_grpquota,
127 	Opt_prjquota,
128 	Opt_usrjquota,
129 	Opt_grpjquota,
130 	Opt_prjjquota,
131 	Opt_offusrjquota,
132 	Opt_offgrpjquota,
133 	Opt_offprjjquota,
134 	Opt_jqfmt_vfsold,
135 	Opt_jqfmt_vfsv0,
136 	Opt_jqfmt_vfsv1,
137 	Opt_whint,
138 	Opt_alloc,
139 	Opt_fsync,
140 	Opt_test_dummy_encryption,
141 	Opt_inlinecrypt,
142 	Opt_checkpoint_disable,
143 	Opt_checkpoint_disable_cap,
144 	Opt_checkpoint_disable_cap_perc,
145 	Opt_checkpoint_enable,
146 	Opt_checkpoint_merge,
147 	Opt_nocheckpoint_merge,
148 	Opt_compress_algorithm,
149 	Opt_compress_log_size,
150 	Opt_compress_extension,
151 	Opt_compress_chksum,
152 	Opt_compress_mode,
153 	Opt_atgc,
154 	Opt_err,
155 };
156 
157 static match_table_t f2fs_tokens = {
158 	{Opt_gc_background, "background_gc=%s"},
159 	{Opt_disable_roll_forward, "disable_roll_forward"},
160 	{Opt_norecovery, "norecovery"},
161 	{Opt_discard, "discard"},
162 	{Opt_nodiscard, "nodiscard"},
163 	{Opt_noheap, "no_heap"},
164 	{Opt_heap, "heap"},
165 	{Opt_user_xattr, "user_xattr"},
166 	{Opt_nouser_xattr, "nouser_xattr"},
167 	{Opt_acl, "acl"},
168 	{Opt_noacl, "noacl"},
169 	{Opt_active_logs, "active_logs=%u"},
170 	{Opt_disable_ext_identify, "disable_ext_identify"},
171 	{Opt_inline_xattr, "inline_xattr"},
172 	{Opt_noinline_xattr, "noinline_xattr"},
173 	{Opt_inline_xattr_size, "inline_xattr_size=%u"},
174 	{Opt_inline_data, "inline_data"},
175 	{Opt_inline_dentry, "inline_dentry"},
176 	{Opt_noinline_dentry, "noinline_dentry"},
177 	{Opt_flush_merge, "flush_merge"},
178 	{Opt_noflush_merge, "noflush_merge"},
179 	{Opt_nobarrier, "nobarrier"},
180 	{Opt_fastboot, "fastboot"},
181 	{Opt_extent_cache, "extent_cache"},
182 	{Opt_noextent_cache, "noextent_cache"},
183 	{Opt_noinline_data, "noinline_data"},
184 	{Opt_data_flush, "data_flush"},
185 	{Opt_reserve_root, "reserve_root=%u"},
186 	{Opt_resgid, "resgid=%u"},
187 	{Opt_resuid, "resuid=%u"},
188 	{Opt_mode, "mode=%s"},
189 	{Opt_io_size_bits, "io_bits=%u"},
190 	{Opt_fault_injection, "fault_injection=%u"},
191 	{Opt_fault_type, "fault_type=%u"},
192 	{Opt_lazytime, "lazytime"},
193 	{Opt_nolazytime, "nolazytime"},
194 	{Opt_quota, "quota"},
195 	{Opt_noquota, "noquota"},
196 	{Opt_usrquota, "usrquota"},
197 	{Opt_grpquota, "grpquota"},
198 	{Opt_prjquota, "prjquota"},
199 	{Opt_usrjquota, "usrjquota=%s"},
200 	{Opt_grpjquota, "grpjquota=%s"},
201 	{Opt_prjjquota, "prjjquota=%s"},
202 	{Opt_offusrjquota, "usrjquota="},
203 	{Opt_offgrpjquota, "grpjquota="},
204 	{Opt_offprjjquota, "prjjquota="},
205 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
206 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
207 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
208 	{Opt_whint, "whint_mode=%s"},
209 	{Opt_alloc, "alloc_mode=%s"},
210 	{Opt_fsync, "fsync_mode=%s"},
211 	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
212 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
213 	{Opt_inlinecrypt, "inlinecrypt"},
214 	{Opt_checkpoint_disable, "checkpoint=disable"},
215 	{Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
216 	{Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
217 	{Opt_checkpoint_enable, "checkpoint=enable"},
218 	{Opt_checkpoint_merge, "checkpoint_merge"},
219 	{Opt_nocheckpoint_merge, "nocheckpoint_merge"},
220 	{Opt_compress_algorithm, "compress_algorithm=%s"},
221 	{Opt_compress_log_size, "compress_log_size=%u"},
222 	{Opt_compress_extension, "compress_extension=%s"},
223 	{Opt_compress_chksum, "compress_chksum"},
224 	{Opt_compress_mode, "compress_mode=%s"},
225 	{Opt_atgc, "atgc"},
226 	{Opt_err, NULL},
227 };
228 
229 void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...)
230 {
231 	struct va_format vaf;
232 	va_list args;
233 	int level;
234 
235 	va_start(args, fmt);
236 
237 	level = printk_get_level(fmt);
238 	vaf.fmt = printk_skip_level(fmt);
239 	vaf.va = &args;
240 	printk("%c%cF2FS-fs (%s): %pV\n",
241 	       KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
242 
243 	va_end(args);
244 }
245 
246 #ifdef CONFIG_UNICODE
247 static const struct f2fs_sb_encodings {
248 	__u16 magic;
249 	char *name;
250 	char *version;
251 } f2fs_sb_encoding_map[] = {
252 	{F2FS_ENC_UTF8_12_1, "utf8", "12.1.0"},
253 };
254 
255 static int f2fs_sb_read_encoding(const struct f2fs_super_block *sb,
256 				 const struct f2fs_sb_encodings **encoding,
257 				 __u16 *flags)
258 {
259 	__u16 magic = le16_to_cpu(sb->s_encoding);
260 	int i;
261 
262 	for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
263 		if (magic == f2fs_sb_encoding_map[i].magic)
264 			break;
265 
266 	if (i >= ARRAY_SIZE(f2fs_sb_encoding_map))
267 		return -EINVAL;
268 
269 	*encoding = &f2fs_sb_encoding_map[i];
270 	*flags = le16_to_cpu(sb->s_encoding_flags);
271 
272 	return 0;
273 }
274 #endif
275 
276 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
277 {
278 	block_t limit = min((sbi->user_block_count << 1) / 1000,
279 			sbi->user_block_count - sbi->reserved_blocks);
280 
281 	/* limit is 0.2% */
282 	if (test_opt(sbi, RESERVE_ROOT) &&
283 			F2FS_OPTION(sbi).root_reserved_blocks > limit) {
284 		F2FS_OPTION(sbi).root_reserved_blocks = limit;
285 		f2fs_info(sbi, "Reduce reserved blocks for root = %u",
286 			  F2FS_OPTION(sbi).root_reserved_blocks);
287 	}
288 	if (!test_opt(sbi, RESERVE_ROOT) &&
289 		(!uid_eq(F2FS_OPTION(sbi).s_resuid,
290 				make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
291 		!gid_eq(F2FS_OPTION(sbi).s_resgid,
292 				make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
293 		f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
294 			  from_kuid_munged(&init_user_ns,
295 					   F2FS_OPTION(sbi).s_resuid),
296 			  from_kgid_munged(&init_user_ns,
297 					   F2FS_OPTION(sbi).s_resgid));
298 }
299 
300 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
301 {
302 	if (!F2FS_OPTION(sbi).unusable_cap_perc)
303 		return;
304 
305 	if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
306 		F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
307 	else
308 		F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
309 					F2FS_OPTION(sbi).unusable_cap_perc;
310 
311 	f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
312 			F2FS_OPTION(sbi).unusable_cap,
313 			F2FS_OPTION(sbi).unusable_cap_perc);
314 }
315 
316 static void init_once(void *foo)
317 {
318 	struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
319 
320 	inode_init_once(&fi->vfs_inode);
321 }
322 
323 #ifdef CONFIG_QUOTA
324 static const char * const quotatypes[] = INITQFNAMES;
325 #define QTYPE2NAME(t) (quotatypes[t])
326 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
327 							substring_t *args)
328 {
329 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
330 	char *qname;
331 	int ret = -EINVAL;
332 
333 	if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
334 		f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
335 		return -EINVAL;
336 	}
337 	if (f2fs_sb_has_quota_ino(sbi)) {
338 		f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
339 		return 0;
340 	}
341 
342 	qname = match_strdup(args);
343 	if (!qname) {
344 		f2fs_err(sbi, "Not enough memory for storing quotafile name");
345 		return -ENOMEM;
346 	}
347 	if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
348 		if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
349 			ret = 0;
350 		else
351 			f2fs_err(sbi, "%s quota file already specified",
352 				 QTYPE2NAME(qtype));
353 		goto errout;
354 	}
355 	if (strchr(qname, '/')) {
356 		f2fs_err(sbi, "quotafile must be on filesystem root");
357 		goto errout;
358 	}
359 	F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
360 	set_opt(sbi, QUOTA);
361 	return 0;
362 errout:
363 	kfree(qname);
364 	return ret;
365 }
366 
367 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
368 {
369 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
370 
371 	if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
372 		f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
373 		return -EINVAL;
374 	}
375 	kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
376 	F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
377 	return 0;
378 }
379 
380 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
381 {
382 	/*
383 	 * We do the test below only for project quotas. 'usrquota' and
384 	 * 'grpquota' mount options are allowed even without quota feature
385 	 * to support legacy quotas in quota files.
386 	 */
387 	if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
388 		f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
389 		return -1;
390 	}
391 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
392 			F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
393 			F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
394 		if (test_opt(sbi, USRQUOTA) &&
395 				F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
396 			clear_opt(sbi, USRQUOTA);
397 
398 		if (test_opt(sbi, GRPQUOTA) &&
399 				F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
400 			clear_opt(sbi, GRPQUOTA);
401 
402 		if (test_opt(sbi, PRJQUOTA) &&
403 				F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
404 			clear_opt(sbi, PRJQUOTA);
405 
406 		if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
407 				test_opt(sbi, PRJQUOTA)) {
408 			f2fs_err(sbi, "old and new quota format mixing");
409 			return -1;
410 		}
411 
412 		if (!F2FS_OPTION(sbi).s_jquota_fmt) {
413 			f2fs_err(sbi, "journaled quota format not specified");
414 			return -1;
415 		}
416 	}
417 
418 	if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
419 		f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
420 		F2FS_OPTION(sbi).s_jquota_fmt = 0;
421 	}
422 	return 0;
423 }
424 #endif
425 
426 static int f2fs_set_test_dummy_encryption(struct super_block *sb,
427 					  const char *opt,
428 					  const substring_t *arg,
429 					  bool is_remount)
430 {
431 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
432 #ifdef CONFIG_FS_ENCRYPTION
433 	int err;
434 
435 	if (!f2fs_sb_has_encrypt(sbi)) {
436 		f2fs_err(sbi, "Encrypt feature is off");
437 		return -EINVAL;
438 	}
439 
440 	/*
441 	 * This mount option is just for testing, and it's not worthwhile to
442 	 * implement the extra complexity (e.g. RCU protection) that would be
443 	 * needed to allow it to be set or changed during remount.  We do allow
444 	 * it to be specified during remount, but only if there is no change.
445 	 */
446 	if (is_remount && !F2FS_OPTION(sbi).dummy_enc_policy.policy) {
447 		f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
448 		return -EINVAL;
449 	}
450 	err = fscrypt_set_test_dummy_encryption(
451 		sb, arg->from, &F2FS_OPTION(sbi).dummy_enc_policy);
452 	if (err) {
453 		if (err == -EEXIST)
454 			f2fs_warn(sbi,
455 				  "Can't change test_dummy_encryption on remount");
456 		else if (err == -EINVAL)
457 			f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
458 				  opt);
459 		else
460 			f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
461 				  opt, err);
462 		return -EINVAL;
463 	}
464 	f2fs_warn(sbi, "Test dummy encryption mode enabled");
465 #else
466 	f2fs_warn(sbi, "Test dummy encryption mount option ignored");
467 #endif
468 	return 0;
469 }
470 
471 #ifdef CONFIG_F2FS_FS_COMPRESSION
472 #ifdef CONFIG_F2FS_FS_LZ4
473 static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
474 {
475 #ifdef CONFIG_F2FS_FS_LZ4HC
476 	unsigned int level;
477 #endif
478 
479 	if (strlen(str) == 3) {
480 		F2FS_OPTION(sbi).compress_level = 0;
481 		return 0;
482 	}
483 
484 #ifdef CONFIG_F2FS_FS_LZ4HC
485 	str += 3;
486 
487 	if (str[0] != ':') {
488 		f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
489 		return -EINVAL;
490 	}
491 	if (kstrtouint(str + 1, 10, &level))
492 		return -EINVAL;
493 
494 	if (level < LZ4HC_MIN_CLEVEL || level > LZ4HC_MAX_CLEVEL) {
495 		f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
496 		return -EINVAL;
497 	}
498 
499 	F2FS_OPTION(sbi).compress_level = level;
500 	return 0;
501 #else
502 	f2fs_info(sbi, "kernel doesn't support lz4hc compression");
503 	return -EINVAL;
504 #endif
505 }
506 #endif
507 
508 #ifdef CONFIG_F2FS_FS_ZSTD
509 static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
510 {
511 	unsigned int level;
512 	int len = 4;
513 
514 	if (strlen(str) == len) {
515 		F2FS_OPTION(sbi).compress_level = 0;
516 		return 0;
517 	}
518 
519 	str += len;
520 
521 	if (str[0] != ':') {
522 		f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
523 		return -EINVAL;
524 	}
525 	if (kstrtouint(str + 1, 10, &level))
526 		return -EINVAL;
527 
528 	if (!level || level > ZSTD_maxCLevel()) {
529 		f2fs_info(sbi, "invalid zstd compress level: %d", level);
530 		return -EINVAL;
531 	}
532 
533 	F2FS_OPTION(sbi).compress_level = level;
534 	return 0;
535 }
536 #endif
537 #endif
538 
539 static int parse_options(struct super_block *sb, char *options, bool is_remount)
540 {
541 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
542 	substring_t args[MAX_OPT_ARGS];
543 #ifdef CONFIG_F2FS_FS_COMPRESSION
544 	unsigned char (*ext)[F2FS_EXTENSION_LEN];
545 	int ext_cnt;
546 #endif
547 	char *p, *name;
548 	int arg = 0;
549 	kuid_t uid;
550 	kgid_t gid;
551 	int ret;
552 
553 	if (!options)
554 		return 0;
555 
556 	while ((p = strsep(&options, ",")) != NULL) {
557 		int token;
558 		if (!*p)
559 			continue;
560 		/*
561 		 * Initialize args struct so we know whether arg was
562 		 * found; some options take optional arguments.
563 		 */
564 		args[0].to = args[0].from = NULL;
565 		token = match_token(p, f2fs_tokens, args);
566 
567 		switch (token) {
568 		case Opt_gc_background:
569 			name = match_strdup(&args[0]);
570 
571 			if (!name)
572 				return -ENOMEM;
573 			if (!strcmp(name, "on")) {
574 				F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
575 			} else if (!strcmp(name, "off")) {
576 				F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
577 			} else if (!strcmp(name, "sync")) {
578 				F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
579 			} else {
580 				kfree(name);
581 				return -EINVAL;
582 			}
583 			kfree(name);
584 			break;
585 		case Opt_disable_roll_forward:
586 			set_opt(sbi, DISABLE_ROLL_FORWARD);
587 			break;
588 		case Opt_norecovery:
589 			/* this option mounts f2fs with ro */
590 			set_opt(sbi, NORECOVERY);
591 			if (!f2fs_readonly(sb))
592 				return -EINVAL;
593 			break;
594 		case Opt_discard:
595 			set_opt(sbi, DISCARD);
596 			break;
597 		case Opt_nodiscard:
598 			if (f2fs_sb_has_blkzoned(sbi)) {
599 				f2fs_warn(sbi, "discard is required for zoned block devices");
600 				return -EINVAL;
601 			}
602 			clear_opt(sbi, DISCARD);
603 			break;
604 		case Opt_noheap:
605 			set_opt(sbi, NOHEAP);
606 			break;
607 		case Opt_heap:
608 			clear_opt(sbi, NOHEAP);
609 			break;
610 #ifdef CONFIG_F2FS_FS_XATTR
611 		case Opt_user_xattr:
612 			set_opt(sbi, XATTR_USER);
613 			break;
614 		case Opt_nouser_xattr:
615 			clear_opt(sbi, XATTR_USER);
616 			break;
617 		case Opt_inline_xattr:
618 			set_opt(sbi, INLINE_XATTR);
619 			break;
620 		case Opt_noinline_xattr:
621 			clear_opt(sbi, INLINE_XATTR);
622 			break;
623 		case Opt_inline_xattr_size:
624 			if (args->from && match_int(args, &arg))
625 				return -EINVAL;
626 			set_opt(sbi, INLINE_XATTR_SIZE);
627 			F2FS_OPTION(sbi).inline_xattr_size = arg;
628 			break;
629 #else
630 		case Opt_user_xattr:
631 			f2fs_info(sbi, "user_xattr options not supported");
632 			break;
633 		case Opt_nouser_xattr:
634 			f2fs_info(sbi, "nouser_xattr options not supported");
635 			break;
636 		case Opt_inline_xattr:
637 			f2fs_info(sbi, "inline_xattr options not supported");
638 			break;
639 		case Opt_noinline_xattr:
640 			f2fs_info(sbi, "noinline_xattr options not supported");
641 			break;
642 #endif
643 #ifdef CONFIG_F2FS_FS_POSIX_ACL
644 		case Opt_acl:
645 			set_opt(sbi, POSIX_ACL);
646 			break;
647 		case Opt_noacl:
648 			clear_opt(sbi, POSIX_ACL);
649 			break;
650 #else
651 		case Opt_acl:
652 			f2fs_info(sbi, "acl options not supported");
653 			break;
654 		case Opt_noacl:
655 			f2fs_info(sbi, "noacl options not supported");
656 			break;
657 #endif
658 		case Opt_active_logs:
659 			if (args->from && match_int(args, &arg))
660 				return -EINVAL;
661 			if (arg != 2 && arg != 4 &&
662 				arg != NR_CURSEG_PERSIST_TYPE)
663 				return -EINVAL;
664 			F2FS_OPTION(sbi).active_logs = arg;
665 			break;
666 		case Opt_disable_ext_identify:
667 			set_opt(sbi, DISABLE_EXT_IDENTIFY);
668 			break;
669 		case Opt_inline_data:
670 			set_opt(sbi, INLINE_DATA);
671 			break;
672 		case Opt_inline_dentry:
673 			set_opt(sbi, INLINE_DENTRY);
674 			break;
675 		case Opt_noinline_dentry:
676 			clear_opt(sbi, INLINE_DENTRY);
677 			break;
678 		case Opt_flush_merge:
679 			set_opt(sbi, FLUSH_MERGE);
680 			break;
681 		case Opt_noflush_merge:
682 			clear_opt(sbi, FLUSH_MERGE);
683 			break;
684 		case Opt_nobarrier:
685 			set_opt(sbi, NOBARRIER);
686 			break;
687 		case Opt_fastboot:
688 			set_opt(sbi, FASTBOOT);
689 			break;
690 		case Opt_extent_cache:
691 			set_opt(sbi, EXTENT_CACHE);
692 			break;
693 		case Opt_noextent_cache:
694 			clear_opt(sbi, EXTENT_CACHE);
695 			break;
696 		case Opt_noinline_data:
697 			clear_opt(sbi, INLINE_DATA);
698 			break;
699 		case Opt_data_flush:
700 			set_opt(sbi, DATA_FLUSH);
701 			break;
702 		case Opt_reserve_root:
703 			if (args->from && match_int(args, &arg))
704 				return -EINVAL;
705 			if (test_opt(sbi, RESERVE_ROOT)) {
706 				f2fs_info(sbi, "Preserve previous reserve_root=%u",
707 					  F2FS_OPTION(sbi).root_reserved_blocks);
708 			} else {
709 				F2FS_OPTION(sbi).root_reserved_blocks = arg;
710 				set_opt(sbi, RESERVE_ROOT);
711 			}
712 			break;
713 		case Opt_resuid:
714 			if (args->from && match_int(args, &arg))
715 				return -EINVAL;
716 			uid = make_kuid(current_user_ns(), arg);
717 			if (!uid_valid(uid)) {
718 				f2fs_err(sbi, "Invalid uid value %d", arg);
719 				return -EINVAL;
720 			}
721 			F2FS_OPTION(sbi).s_resuid = uid;
722 			break;
723 		case Opt_resgid:
724 			if (args->from && match_int(args, &arg))
725 				return -EINVAL;
726 			gid = make_kgid(current_user_ns(), arg);
727 			if (!gid_valid(gid)) {
728 				f2fs_err(sbi, "Invalid gid value %d", arg);
729 				return -EINVAL;
730 			}
731 			F2FS_OPTION(sbi).s_resgid = gid;
732 			break;
733 		case Opt_mode:
734 			name = match_strdup(&args[0]);
735 
736 			if (!name)
737 				return -ENOMEM;
738 			if (!strcmp(name, "adaptive")) {
739 				if (f2fs_sb_has_blkzoned(sbi)) {
740 					f2fs_warn(sbi, "adaptive mode is not allowed with zoned block device feature");
741 					kfree(name);
742 					return -EINVAL;
743 				}
744 				F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
745 			} else if (!strcmp(name, "lfs")) {
746 				F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
747 			} else {
748 				kfree(name);
749 				return -EINVAL;
750 			}
751 			kfree(name);
752 			break;
753 		case Opt_io_size_bits:
754 			if (args->from && match_int(args, &arg))
755 				return -EINVAL;
756 			if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_PAGES)) {
757 				f2fs_warn(sbi, "Not support %d, larger than %d",
758 					  1 << arg, BIO_MAX_PAGES);
759 				return -EINVAL;
760 			}
761 			F2FS_OPTION(sbi).write_io_size_bits = arg;
762 			break;
763 #ifdef CONFIG_F2FS_FAULT_INJECTION
764 		case Opt_fault_injection:
765 			if (args->from && match_int(args, &arg))
766 				return -EINVAL;
767 			f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
768 			set_opt(sbi, FAULT_INJECTION);
769 			break;
770 
771 		case Opt_fault_type:
772 			if (args->from && match_int(args, &arg))
773 				return -EINVAL;
774 			f2fs_build_fault_attr(sbi, 0, arg);
775 			set_opt(sbi, FAULT_INJECTION);
776 			break;
777 #else
778 		case Opt_fault_injection:
779 			f2fs_info(sbi, "fault_injection options not supported");
780 			break;
781 
782 		case Opt_fault_type:
783 			f2fs_info(sbi, "fault_type options not supported");
784 			break;
785 #endif
786 		case Opt_lazytime:
787 			sb->s_flags |= SB_LAZYTIME;
788 			break;
789 		case Opt_nolazytime:
790 			sb->s_flags &= ~SB_LAZYTIME;
791 			break;
792 #ifdef CONFIG_QUOTA
793 		case Opt_quota:
794 		case Opt_usrquota:
795 			set_opt(sbi, USRQUOTA);
796 			break;
797 		case Opt_grpquota:
798 			set_opt(sbi, GRPQUOTA);
799 			break;
800 		case Opt_prjquota:
801 			set_opt(sbi, PRJQUOTA);
802 			break;
803 		case Opt_usrjquota:
804 			ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
805 			if (ret)
806 				return ret;
807 			break;
808 		case Opt_grpjquota:
809 			ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
810 			if (ret)
811 				return ret;
812 			break;
813 		case Opt_prjjquota:
814 			ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
815 			if (ret)
816 				return ret;
817 			break;
818 		case Opt_offusrjquota:
819 			ret = f2fs_clear_qf_name(sb, USRQUOTA);
820 			if (ret)
821 				return ret;
822 			break;
823 		case Opt_offgrpjquota:
824 			ret = f2fs_clear_qf_name(sb, GRPQUOTA);
825 			if (ret)
826 				return ret;
827 			break;
828 		case Opt_offprjjquota:
829 			ret = f2fs_clear_qf_name(sb, PRJQUOTA);
830 			if (ret)
831 				return ret;
832 			break;
833 		case Opt_jqfmt_vfsold:
834 			F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
835 			break;
836 		case Opt_jqfmt_vfsv0:
837 			F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
838 			break;
839 		case Opt_jqfmt_vfsv1:
840 			F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
841 			break;
842 		case Opt_noquota:
843 			clear_opt(sbi, QUOTA);
844 			clear_opt(sbi, USRQUOTA);
845 			clear_opt(sbi, GRPQUOTA);
846 			clear_opt(sbi, PRJQUOTA);
847 			break;
848 #else
849 		case Opt_quota:
850 		case Opt_usrquota:
851 		case Opt_grpquota:
852 		case Opt_prjquota:
853 		case Opt_usrjquota:
854 		case Opt_grpjquota:
855 		case Opt_prjjquota:
856 		case Opt_offusrjquota:
857 		case Opt_offgrpjquota:
858 		case Opt_offprjjquota:
859 		case Opt_jqfmt_vfsold:
860 		case Opt_jqfmt_vfsv0:
861 		case Opt_jqfmt_vfsv1:
862 		case Opt_noquota:
863 			f2fs_info(sbi, "quota operations not supported");
864 			break;
865 #endif
866 		case Opt_whint:
867 			name = match_strdup(&args[0]);
868 			if (!name)
869 				return -ENOMEM;
870 			if (!strcmp(name, "user-based")) {
871 				F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
872 			} else if (!strcmp(name, "off")) {
873 				F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
874 			} else if (!strcmp(name, "fs-based")) {
875 				F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
876 			} else {
877 				kfree(name);
878 				return -EINVAL;
879 			}
880 			kfree(name);
881 			break;
882 		case Opt_alloc:
883 			name = match_strdup(&args[0]);
884 			if (!name)
885 				return -ENOMEM;
886 
887 			if (!strcmp(name, "default")) {
888 				F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
889 			} else if (!strcmp(name, "reuse")) {
890 				F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
891 			} else {
892 				kfree(name);
893 				return -EINVAL;
894 			}
895 			kfree(name);
896 			break;
897 		case Opt_fsync:
898 			name = match_strdup(&args[0]);
899 			if (!name)
900 				return -ENOMEM;
901 			if (!strcmp(name, "posix")) {
902 				F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
903 			} else if (!strcmp(name, "strict")) {
904 				F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
905 			} else if (!strcmp(name, "nobarrier")) {
906 				F2FS_OPTION(sbi).fsync_mode =
907 							FSYNC_MODE_NOBARRIER;
908 			} else {
909 				kfree(name);
910 				return -EINVAL;
911 			}
912 			kfree(name);
913 			break;
914 		case Opt_test_dummy_encryption:
915 			ret = f2fs_set_test_dummy_encryption(sb, p, &args[0],
916 							     is_remount);
917 			if (ret)
918 				return ret;
919 			break;
920 		case Opt_inlinecrypt:
921 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
922 			sb->s_flags |= SB_INLINECRYPT;
923 #else
924 			f2fs_info(sbi, "inline encryption not supported");
925 #endif
926 			break;
927 		case Opt_checkpoint_disable_cap_perc:
928 			if (args->from && match_int(args, &arg))
929 				return -EINVAL;
930 			if (arg < 0 || arg > 100)
931 				return -EINVAL;
932 			F2FS_OPTION(sbi).unusable_cap_perc = arg;
933 			set_opt(sbi, DISABLE_CHECKPOINT);
934 			break;
935 		case Opt_checkpoint_disable_cap:
936 			if (args->from && match_int(args, &arg))
937 				return -EINVAL;
938 			F2FS_OPTION(sbi).unusable_cap = arg;
939 			set_opt(sbi, DISABLE_CHECKPOINT);
940 			break;
941 		case Opt_checkpoint_disable:
942 			set_opt(sbi, DISABLE_CHECKPOINT);
943 			break;
944 		case Opt_checkpoint_enable:
945 			clear_opt(sbi, DISABLE_CHECKPOINT);
946 			break;
947 		case Opt_checkpoint_merge:
948 			set_opt(sbi, MERGE_CHECKPOINT);
949 			break;
950 		case Opt_nocheckpoint_merge:
951 			clear_opt(sbi, MERGE_CHECKPOINT);
952 			break;
953 #ifdef CONFIG_F2FS_FS_COMPRESSION
954 		case Opt_compress_algorithm:
955 			if (!f2fs_sb_has_compression(sbi)) {
956 				f2fs_info(sbi, "Image doesn't support compression");
957 				break;
958 			}
959 			name = match_strdup(&args[0]);
960 			if (!name)
961 				return -ENOMEM;
962 			if (!strcmp(name, "lzo")) {
963 #ifdef CONFIG_F2FS_FS_LZO
964 				F2FS_OPTION(sbi).compress_level = 0;
965 				F2FS_OPTION(sbi).compress_algorithm =
966 								COMPRESS_LZO;
967 #else
968 				f2fs_info(sbi, "kernel doesn't support lzo compression");
969 #endif
970 			} else if (!strncmp(name, "lz4", 3)) {
971 #ifdef CONFIG_F2FS_FS_LZ4
972 				ret = f2fs_set_lz4hc_level(sbi, name);
973 				if (ret) {
974 					kfree(name);
975 					return -EINVAL;
976 				}
977 				F2FS_OPTION(sbi).compress_algorithm =
978 								COMPRESS_LZ4;
979 #else
980 				f2fs_info(sbi, "kernel doesn't support lz4 compression");
981 #endif
982 			} else if (!strncmp(name, "zstd", 4)) {
983 #ifdef CONFIG_F2FS_FS_ZSTD
984 				ret = f2fs_set_zstd_level(sbi, name);
985 				if (ret) {
986 					kfree(name);
987 					return -EINVAL;
988 				}
989 				F2FS_OPTION(sbi).compress_algorithm =
990 								COMPRESS_ZSTD;
991 #else
992 				f2fs_info(sbi, "kernel doesn't support zstd compression");
993 #endif
994 			} else if (!strcmp(name, "lzo-rle")) {
995 #ifdef CONFIG_F2FS_FS_LZORLE
996 				F2FS_OPTION(sbi).compress_level = 0;
997 				F2FS_OPTION(sbi).compress_algorithm =
998 								COMPRESS_LZORLE;
999 #else
1000 				f2fs_info(sbi, "kernel doesn't support lzorle compression");
1001 #endif
1002 			} else {
1003 				kfree(name);
1004 				return -EINVAL;
1005 			}
1006 			kfree(name);
1007 			break;
1008 		case Opt_compress_log_size:
1009 			if (!f2fs_sb_has_compression(sbi)) {
1010 				f2fs_info(sbi, "Image doesn't support compression");
1011 				break;
1012 			}
1013 			if (args->from && match_int(args, &arg))
1014 				return -EINVAL;
1015 			if (arg < MIN_COMPRESS_LOG_SIZE ||
1016 				arg > MAX_COMPRESS_LOG_SIZE) {
1017 				f2fs_err(sbi,
1018 					"Compress cluster log size is out of range");
1019 				return -EINVAL;
1020 			}
1021 			F2FS_OPTION(sbi).compress_log_size = arg;
1022 			break;
1023 		case Opt_compress_extension:
1024 			if (!f2fs_sb_has_compression(sbi)) {
1025 				f2fs_info(sbi, "Image doesn't support compression");
1026 				break;
1027 			}
1028 			name = match_strdup(&args[0]);
1029 			if (!name)
1030 				return -ENOMEM;
1031 
1032 			ext = F2FS_OPTION(sbi).extensions;
1033 			ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1034 
1035 			if (strlen(name) >= F2FS_EXTENSION_LEN ||
1036 				ext_cnt >= COMPRESS_EXT_NUM) {
1037 				f2fs_err(sbi,
1038 					"invalid extension length/number");
1039 				kfree(name);
1040 				return -EINVAL;
1041 			}
1042 
1043 			strcpy(ext[ext_cnt], name);
1044 			F2FS_OPTION(sbi).compress_ext_cnt++;
1045 			kfree(name);
1046 			break;
1047 		case Opt_compress_chksum:
1048 			F2FS_OPTION(sbi).compress_chksum = true;
1049 			break;
1050 		case Opt_compress_mode:
1051 			name = match_strdup(&args[0]);
1052 			if (!name)
1053 				return -ENOMEM;
1054 			if (!strcmp(name, "fs")) {
1055 				F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1056 			} else if (!strcmp(name, "user")) {
1057 				F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1058 			} else {
1059 				kfree(name);
1060 				return -EINVAL;
1061 			}
1062 			kfree(name);
1063 			break;
1064 #else
1065 		case Opt_compress_algorithm:
1066 		case Opt_compress_log_size:
1067 		case Opt_compress_extension:
1068 		case Opt_compress_chksum:
1069 		case Opt_compress_mode:
1070 			f2fs_info(sbi, "compression options not supported");
1071 			break;
1072 #endif
1073 		case Opt_atgc:
1074 			set_opt(sbi, ATGC);
1075 			break;
1076 		default:
1077 			f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1078 				 p);
1079 			return -EINVAL;
1080 		}
1081 	}
1082 #ifdef CONFIG_QUOTA
1083 	if (f2fs_check_quota_options(sbi))
1084 		return -EINVAL;
1085 #else
1086 	if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
1087 		f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1088 		return -EINVAL;
1089 	}
1090 	if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
1091 		f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1092 		return -EINVAL;
1093 	}
1094 #endif
1095 #ifndef CONFIG_UNICODE
1096 	if (f2fs_sb_has_casefold(sbi)) {
1097 		f2fs_err(sbi,
1098 			"Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1099 		return -EINVAL;
1100 	}
1101 #endif
1102 	/*
1103 	 * The BLKZONED feature indicates that the drive was formatted with
1104 	 * zone alignment optimization. This is optional for host-aware
1105 	 * devices, but mandatory for host-managed zoned block devices.
1106 	 */
1107 #ifndef CONFIG_BLK_DEV_ZONED
1108 	if (f2fs_sb_has_blkzoned(sbi)) {
1109 		f2fs_err(sbi, "Zoned block device support is not enabled");
1110 		return -EINVAL;
1111 	}
1112 #endif
1113 
1114 	if (F2FS_IO_SIZE_BITS(sbi) && !f2fs_lfs_mode(sbi)) {
1115 		f2fs_err(sbi, "Should set mode=lfs with %uKB-sized IO",
1116 			 F2FS_IO_SIZE_KB(sbi));
1117 		return -EINVAL;
1118 	}
1119 
1120 	if (test_opt(sbi, INLINE_XATTR_SIZE)) {
1121 		int min_size, max_size;
1122 
1123 		if (!f2fs_sb_has_extra_attr(sbi) ||
1124 			!f2fs_sb_has_flexible_inline_xattr(sbi)) {
1125 			f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1126 			return -EINVAL;
1127 		}
1128 		if (!test_opt(sbi, INLINE_XATTR)) {
1129 			f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1130 			return -EINVAL;
1131 		}
1132 
1133 		min_size = sizeof(struct f2fs_xattr_header) / sizeof(__le32);
1134 		max_size = MAX_INLINE_XATTR_SIZE;
1135 
1136 		if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1137 				F2FS_OPTION(sbi).inline_xattr_size > max_size) {
1138 			f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1139 				 min_size, max_size);
1140 			return -EINVAL;
1141 		}
1142 	}
1143 
1144 	if (test_opt(sbi, DISABLE_CHECKPOINT) && f2fs_lfs_mode(sbi)) {
1145 		f2fs_err(sbi, "LFS not compatible with checkpoint=disable\n");
1146 		return -EINVAL;
1147 	}
1148 
1149 	/* Not pass down write hints if the number of active logs is lesser
1150 	 * than NR_CURSEG_PERSIST_TYPE.
1151 	 */
1152 	if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
1153 		F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1154 	return 0;
1155 }
1156 
1157 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1158 {
1159 	struct f2fs_inode_info *fi;
1160 
1161 	fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
1162 	if (!fi)
1163 		return NULL;
1164 
1165 	init_once((void *) fi);
1166 
1167 	/* Initialize f2fs-specific inode info */
1168 	atomic_set(&fi->dirty_pages, 0);
1169 	atomic_set(&fi->i_compr_blocks, 0);
1170 	init_rwsem(&fi->i_sem);
1171 	spin_lock_init(&fi->i_size_lock);
1172 	INIT_LIST_HEAD(&fi->dirty_list);
1173 	INIT_LIST_HEAD(&fi->gdirty_list);
1174 	INIT_LIST_HEAD(&fi->inmem_ilist);
1175 	INIT_LIST_HEAD(&fi->inmem_pages);
1176 	mutex_init(&fi->inmem_lock);
1177 	init_rwsem(&fi->i_gc_rwsem[READ]);
1178 	init_rwsem(&fi->i_gc_rwsem[WRITE]);
1179 	init_rwsem(&fi->i_mmap_sem);
1180 	init_rwsem(&fi->i_xattr_sem);
1181 
1182 	/* Will be used by directory only */
1183 	fi->i_dir_level = F2FS_SB(sb)->dir_level;
1184 
1185 	return &fi->vfs_inode;
1186 }
1187 
1188 static int f2fs_drop_inode(struct inode *inode)
1189 {
1190 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1191 	int ret;
1192 
1193 	/*
1194 	 * during filesystem shutdown, if checkpoint is disabled,
1195 	 * drop useless meta/node dirty pages.
1196 	 */
1197 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1198 		if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1199 			inode->i_ino == F2FS_META_INO(sbi)) {
1200 			trace_f2fs_drop_inode(inode, 1);
1201 			return 1;
1202 		}
1203 	}
1204 
1205 	/*
1206 	 * This is to avoid a deadlock condition like below.
1207 	 * writeback_single_inode(inode)
1208 	 *  - f2fs_write_data_page
1209 	 *    - f2fs_gc -> iput -> evict
1210 	 *       - inode_wait_for_writeback(inode)
1211 	 */
1212 	if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
1213 		if (!inode->i_nlink && !is_bad_inode(inode)) {
1214 			/* to avoid evict_inode call simultaneously */
1215 			atomic_inc(&inode->i_count);
1216 			spin_unlock(&inode->i_lock);
1217 
1218 			/* some remained atomic pages should discarded */
1219 			if (f2fs_is_atomic_file(inode))
1220 				f2fs_drop_inmem_pages(inode);
1221 
1222 			/* should remain fi->extent_tree for writepage */
1223 			f2fs_destroy_extent_node(inode);
1224 
1225 			sb_start_intwrite(inode->i_sb);
1226 			f2fs_i_size_write(inode, 0);
1227 
1228 			f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1229 					inode, NULL, 0, DATA);
1230 			truncate_inode_pages_final(inode->i_mapping);
1231 
1232 			if (F2FS_HAS_BLOCKS(inode))
1233 				f2fs_truncate(inode);
1234 
1235 			sb_end_intwrite(inode->i_sb);
1236 
1237 			spin_lock(&inode->i_lock);
1238 			atomic_dec(&inode->i_count);
1239 		}
1240 		trace_f2fs_drop_inode(inode, 0);
1241 		return 0;
1242 	}
1243 	ret = generic_drop_inode(inode);
1244 	if (!ret)
1245 		ret = fscrypt_drop_inode(inode);
1246 	trace_f2fs_drop_inode(inode, ret);
1247 	return ret;
1248 }
1249 
1250 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1251 {
1252 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1253 	int ret = 0;
1254 
1255 	spin_lock(&sbi->inode_lock[DIRTY_META]);
1256 	if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1257 		ret = 1;
1258 	} else {
1259 		set_inode_flag(inode, FI_DIRTY_INODE);
1260 		stat_inc_dirty_inode(sbi, DIRTY_META);
1261 	}
1262 	if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1263 		list_add_tail(&F2FS_I(inode)->gdirty_list,
1264 				&sbi->inode_list[DIRTY_META]);
1265 		inc_page_count(sbi, F2FS_DIRTY_IMETA);
1266 	}
1267 	spin_unlock(&sbi->inode_lock[DIRTY_META]);
1268 	return ret;
1269 }
1270 
1271 void f2fs_inode_synced(struct inode *inode)
1272 {
1273 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1274 
1275 	spin_lock(&sbi->inode_lock[DIRTY_META]);
1276 	if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1277 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
1278 		return;
1279 	}
1280 	if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1281 		list_del_init(&F2FS_I(inode)->gdirty_list);
1282 		dec_page_count(sbi, F2FS_DIRTY_IMETA);
1283 	}
1284 	clear_inode_flag(inode, FI_DIRTY_INODE);
1285 	clear_inode_flag(inode, FI_AUTO_RECOVER);
1286 	stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1287 	spin_unlock(&sbi->inode_lock[DIRTY_META]);
1288 }
1289 
1290 /*
1291  * f2fs_dirty_inode() is called from __mark_inode_dirty()
1292  *
1293  * We should call set_dirty_inode to write the dirty inode through write_inode.
1294  */
1295 static void f2fs_dirty_inode(struct inode *inode, int flags)
1296 {
1297 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1298 
1299 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1300 			inode->i_ino == F2FS_META_INO(sbi))
1301 		return;
1302 
1303 	if (flags == I_DIRTY_TIME)
1304 		return;
1305 
1306 	if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1307 		clear_inode_flag(inode, FI_AUTO_RECOVER);
1308 
1309 	f2fs_inode_dirtied(inode, false);
1310 }
1311 
1312 static void f2fs_free_inode(struct inode *inode)
1313 {
1314 	fscrypt_free_inode(inode);
1315 	kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1316 }
1317 
1318 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1319 {
1320 	percpu_counter_destroy(&sbi->alloc_valid_block_count);
1321 	percpu_counter_destroy(&sbi->total_valid_inode_count);
1322 }
1323 
1324 static void destroy_device_list(struct f2fs_sb_info *sbi)
1325 {
1326 	int i;
1327 
1328 	for (i = 0; i < sbi->s_ndevs; i++) {
1329 		blkdev_put(FDEV(i).bdev, FMODE_EXCL);
1330 #ifdef CONFIG_BLK_DEV_ZONED
1331 		kvfree(FDEV(i).blkz_seq);
1332 		kfree(FDEV(i).zone_capacity_blocks);
1333 #endif
1334 	}
1335 	kvfree(sbi->devs);
1336 }
1337 
1338 static void f2fs_put_super(struct super_block *sb)
1339 {
1340 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1341 	int i;
1342 	bool dropped;
1343 
1344 	/* unregister procfs/sysfs entries in advance to avoid race case */
1345 	f2fs_unregister_sysfs(sbi);
1346 
1347 	f2fs_quota_off_umount(sb);
1348 
1349 	/* prevent remaining shrinker jobs */
1350 	mutex_lock(&sbi->umount_mutex);
1351 
1352 	/*
1353 	 * flush all issued checkpoints and stop checkpoint issue thread.
1354 	 * after then, all checkpoints should be done by each process context.
1355 	 */
1356 	f2fs_stop_ckpt_thread(sbi);
1357 
1358 	/*
1359 	 * We don't need to do checkpoint when superblock is clean.
1360 	 * But, the previous checkpoint was not done by umount, it needs to do
1361 	 * clean checkpoint again.
1362 	 */
1363 	if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1364 			!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1365 		struct cp_control cpc = {
1366 			.reason = CP_UMOUNT,
1367 		};
1368 		f2fs_write_checkpoint(sbi, &cpc);
1369 	}
1370 
1371 	/* be sure to wait for any on-going discard commands */
1372 	dropped = f2fs_issue_discard_timeout(sbi);
1373 
1374 	if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1375 					!sbi->discard_blks && !dropped) {
1376 		struct cp_control cpc = {
1377 			.reason = CP_UMOUNT | CP_TRIMMED,
1378 		};
1379 		f2fs_write_checkpoint(sbi, &cpc);
1380 	}
1381 
1382 	/*
1383 	 * normally superblock is clean, so we need to release this.
1384 	 * In addition, EIO will skip do checkpoint, we need this as well.
1385 	 */
1386 	f2fs_release_ino_entry(sbi, true);
1387 
1388 	f2fs_leave_shrinker(sbi);
1389 	mutex_unlock(&sbi->umount_mutex);
1390 
1391 	/* our cp_error case, we can wait for any writeback page */
1392 	f2fs_flush_merged_writes(sbi);
1393 
1394 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1395 
1396 	f2fs_bug_on(sbi, sbi->fsync_node_num);
1397 
1398 	iput(sbi->node_inode);
1399 	sbi->node_inode = NULL;
1400 
1401 	iput(sbi->meta_inode);
1402 	sbi->meta_inode = NULL;
1403 
1404 	/*
1405 	 * iput() can update stat information, if f2fs_write_checkpoint()
1406 	 * above failed with error.
1407 	 */
1408 	f2fs_destroy_stats(sbi);
1409 
1410 	/* destroy f2fs internal modules */
1411 	f2fs_destroy_node_manager(sbi);
1412 	f2fs_destroy_segment_manager(sbi);
1413 
1414 	f2fs_destroy_post_read_wq(sbi);
1415 
1416 	kvfree(sbi->ckpt);
1417 
1418 	sb->s_fs_info = NULL;
1419 	if (sbi->s_chksum_driver)
1420 		crypto_free_shash(sbi->s_chksum_driver);
1421 	kfree(sbi->raw_super);
1422 
1423 	destroy_device_list(sbi);
1424 	f2fs_destroy_page_array_cache(sbi);
1425 	f2fs_destroy_xattr_caches(sbi);
1426 	mempool_destroy(sbi->write_io_dummy);
1427 #ifdef CONFIG_QUOTA
1428 	for (i = 0; i < MAXQUOTAS; i++)
1429 		kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1430 #endif
1431 	fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1432 	destroy_percpu_info(sbi);
1433 	for (i = 0; i < NR_PAGE_TYPE; i++)
1434 		kvfree(sbi->write_io[i]);
1435 #ifdef CONFIG_UNICODE
1436 	utf8_unload(sb->s_encoding);
1437 #endif
1438 	kfree(sbi);
1439 }
1440 
1441 int f2fs_sync_fs(struct super_block *sb, int sync)
1442 {
1443 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1444 	int err = 0;
1445 
1446 	if (unlikely(f2fs_cp_error(sbi)))
1447 		return 0;
1448 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1449 		return 0;
1450 
1451 	trace_f2fs_sync_fs(sb, sync);
1452 
1453 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1454 		return -EAGAIN;
1455 
1456 	if (sync)
1457 		err = f2fs_issue_checkpoint(sbi);
1458 
1459 	return err;
1460 }
1461 
1462 static int f2fs_freeze(struct super_block *sb)
1463 {
1464 	if (f2fs_readonly(sb))
1465 		return 0;
1466 
1467 	/* IO error happened before */
1468 	if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1469 		return -EIO;
1470 
1471 	/* must be clean, since sync_filesystem() was already called */
1472 	if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1473 		return -EINVAL;
1474 
1475 	/* ensure no checkpoint required */
1476 	if (!llist_empty(&F2FS_SB(sb)->cprc_info.issue_list))
1477 		return -EINVAL;
1478 	return 0;
1479 }
1480 
1481 static int f2fs_unfreeze(struct super_block *sb)
1482 {
1483 	return 0;
1484 }
1485 
1486 #ifdef CONFIG_QUOTA
1487 static int f2fs_statfs_project(struct super_block *sb,
1488 				kprojid_t projid, struct kstatfs *buf)
1489 {
1490 	struct kqid qid;
1491 	struct dquot *dquot;
1492 	u64 limit;
1493 	u64 curblock;
1494 
1495 	qid = make_kqid_projid(projid);
1496 	dquot = dqget(sb, qid);
1497 	if (IS_ERR(dquot))
1498 		return PTR_ERR(dquot);
1499 	spin_lock(&dquot->dq_dqb_lock);
1500 
1501 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1502 					dquot->dq_dqb.dqb_bhardlimit);
1503 	if (limit)
1504 		limit >>= sb->s_blocksize_bits;
1505 
1506 	if (limit && buf->f_blocks > limit) {
1507 		curblock = (dquot->dq_dqb.dqb_curspace +
1508 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1509 		buf->f_blocks = limit;
1510 		buf->f_bfree = buf->f_bavail =
1511 			(buf->f_blocks > curblock) ?
1512 			 (buf->f_blocks - curblock) : 0;
1513 	}
1514 
1515 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1516 					dquot->dq_dqb.dqb_ihardlimit);
1517 
1518 	if (limit && buf->f_files > limit) {
1519 		buf->f_files = limit;
1520 		buf->f_ffree =
1521 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1522 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1523 	}
1524 
1525 	spin_unlock(&dquot->dq_dqb_lock);
1526 	dqput(dquot);
1527 	return 0;
1528 }
1529 #endif
1530 
1531 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1532 {
1533 	struct super_block *sb = dentry->d_sb;
1534 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1535 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1536 	block_t total_count, user_block_count, start_count;
1537 	u64 avail_node_count;
1538 
1539 	total_count = le64_to_cpu(sbi->raw_super->block_count);
1540 	user_block_count = sbi->user_block_count;
1541 	start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1542 	buf->f_type = F2FS_SUPER_MAGIC;
1543 	buf->f_bsize = sbi->blocksize;
1544 
1545 	buf->f_blocks = total_count - start_count;
1546 	buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1547 						sbi->current_reserved_blocks;
1548 
1549 	spin_lock(&sbi->stat_lock);
1550 	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1551 		buf->f_bfree = 0;
1552 	else
1553 		buf->f_bfree -= sbi->unusable_block_count;
1554 	spin_unlock(&sbi->stat_lock);
1555 
1556 	if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1557 		buf->f_bavail = buf->f_bfree -
1558 				F2FS_OPTION(sbi).root_reserved_blocks;
1559 	else
1560 		buf->f_bavail = 0;
1561 
1562 	avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1563 
1564 	if (avail_node_count > user_block_count) {
1565 		buf->f_files = user_block_count;
1566 		buf->f_ffree = buf->f_bavail;
1567 	} else {
1568 		buf->f_files = avail_node_count;
1569 		buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1570 					buf->f_bavail);
1571 	}
1572 
1573 	buf->f_namelen = F2FS_NAME_LEN;
1574 	buf->f_fsid    = u64_to_fsid(id);
1575 
1576 #ifdef CONFIG_QUOTA
1577 	if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1578 			sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1579 		f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1580 	}
1581 #endif
1582 	return 0;
1583 }
1584 
1585 static inline void f2fs_show_quota_options(struct seq_file *seq,
1586 					   struct super_block *sb)
1587 {
1588 #ifdef CONFIG_QUOTA
1589 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1590 
1591 	if (F2FS_OPTION(sbi).s_jquota_fmt) {
1592 		char *fmtname = "";
1593 
1594 		switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1595 		case QFMT_VFS_OLD:
1596 			fmtname = "vfsold";
1597 			break;
1598 		case QFMT_VFS_V0:
1599 			fmtname = "vfsv0";
1600 			break;
1601 		case QFMT_VFS_V1:
1602 			fmtname = "vfsv1";
1603 			break;
1604 		}
1605 		seq_printf(seq, ",jqfmt=%s", fmtname);
1606 	}
1607 
1608 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1609 		seq_show_option(seq, "usrjquota",
1610 			F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1611 
1612 	if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1613 		seq_show_option(seq, "grpjquota",
1614 			F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1615 
1616 	if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1617 		seq_show_option(seq, "prjjquota",
1618 			F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1619 #endif
1620 }
1621 
1622 static inline void f2fs_show_compress_options(struct seq_file *seq,
1623 							struct super_block *sb)
1624 {
1625 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1626 	char *algtype = "";
1627 	int i;
1628 
1629 	if (!f2fs_sb_has_compression(sbi))
1630 		return;
1631 
1632 	switch (F2FS_OPTION(sbi).compress_algorithm) {
1633 	case COMPRESS_LZO:
1634 		algtype = "lzo";
1635 		break;
1636 	case COMPRESS_LZ4:
1637 		algtype = "lz4";
1638 		break;
1639 	case COMPRESS_ZSTD:
1640 		algtype = "zstd";
1641 		break;
1642 	case COMPRESS_LZORLE:
1643 		algtype = "lzo-rle";
1644 		break;
1645 	}
1646 	seq_printf(seq, ",compress_algorithm=%s", algtype);
1647 
1648 	if (F2FS_OPTION(sbi).compress_level)
1649 		seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1650 
1651 	seq_printf(seq, ",compress_log_size=%u",
1652 			F2FS_OPTION(sbi).compress_log_size);
1653 
1654 	for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1655 		seq_printf(seq, ",compress_extension=%s",
1656 			F2FS_OPTION(sbi).extensions[i]);
1657 	}
1658 
1659 	if (F2FS_OPTION(sbi).compress_chksum)
1660 		seq_puts(seq, ",compress_chksum");
1661 
1662 	if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1663 		seq_printf(seq, ",compress_mode=%s", "fs");
1664 	else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1665 		seq_printf(seq, ",compress_mode=%s", "user");
1666 }
1667 
1668 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1669 {
1670 	struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1671 
1672 	if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1673 		seq_printf(seq, ",background_gc=%s", "sync");
1674 	else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1675 		seq_printf(seq, ",background_gc=%s", "on");
1676 	else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
1677 		seq_printf(seq, ",background_gc=%s", "off");
1678 
1679 	if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1680 		seq_puts(seq, ",disable_roll_forward");
1681 	if (test_opt(sbi, NORECOVERY))
1682 		seq_puts(seq, ",norecovery");
1683 	if (test_opt(sbi, DISCARD))
1684 		seq_puts(seq, ",discard");
1685 	else
1686 		seq_puts(seq, ",nodiscard");
1687 	if (test_opt(sbi, NOHEAP))
1688 		seq_puts(seq, ",no_heap");
1689 	else
1690 		seq_puts(seq, ",heap");
1691 #ifdef CONFIG_F2FS_FS_XATTR
1692 	if (test_opt(sbi, XATTR_USER))
1693 		seq_puts(seq, ",user_xattr");
1694 	else
1695 		seq_puts(seq, ",nouser_xattr");
1696 	if (test_opt(sbi, INLINE_XATTR))
1697 		seq_puts(seq, ",inline_xattr");
1698 	else
1699 		seq_puts(seq, ",noinline_xattr");
1700 	if (test_opt(sbi, INLINE_XATTR_SIZE))
1701 		seq_printf(seq, ",inline_xattr_size=%u",
1702 					F2FS_OPTION(sbi).inline_xattr_size);
1703 #endif
1704 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1705 	if (test_opt(sbi, POSIX_ACL))
1706 		seq_puts(seq, ",acl");
1707 	else
1708 		seq_puts(seq, ",noacl");
1709 #endif
1710 	if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1711 		seq_puts(seq, ",disable_ext_identify");
1712 	if (test_opt(sbi, INLINE_DATA))
1713 		seq_puts(seq, ",inline_data");
1714 	else
1715 		seq_puts(seq, ",noinline_data");
1716 	if (test_opt(sbi, INLINE_DENTRY))
1717 		seq_puts(seq, ",inline_dentry");
1718 	else
1719 		seq_puts(seq, ",noinline_dentry");
1720 	if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
1721 		seq_puts(seq, ",flush_merge");
1722 	if (test_opt(sbi, NOBARRIER))
1723 		seq_puts(seq, ",nobarrier");
1724 	if (test_opt(sbi, FASTBOOT))
1725 		seq_puts(seq, ",fastboot");
1726 	if (test_opt(sbi, EXTENT_CACHE))
1727 		seq_puts(seq, ",extent_cache");
1728 	else
1729 		seq_puts(seq, ",noextent_cache");
1730 	if (test_opt(sbi, DATA_FLUSH))
1731 		seq_puts(seq, ",data_flush");
1732 
1733 	seq_puts(seq, ",mode=");
1734 	if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
1735 		seq_puts(seq, "adaptive");
1736 	else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
1737 		seq_puts(seq, "lfs");
1738 	seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
1739 	if (test_opt(sbi, RESERVE_ROOT))
1740 		seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1741 				F2FS_OPTION(sbi).root_reserved_blocks,
1742 				from_kuid_munged(&init_user_ns,
1743 					F2FS_OPTION(sbi).s_resuid),
1744 				from_kgid_munged(&init_user_ns,
1745 					F2FS_OPTION(sbi).s_resgid));
1746 	if (F2FS_IO_SIZE_BITS(sbi))
1747 		seq_printf(seq, ",io_bits=%u",
1748 				F2FS_OPTION(sbi).write_io_size_bits);
1749 #ifdef CONFIG_F2FS_FAULT_INJECTION
1750 	if (test_opt(sbi, FAULT_INJECTION)) {
1751 		seq_printf(seq, ",fault_injection=%u",
1752 				F2FS_OPTION(sbi).fault_info.inject_rate);
1753 		seq_printf(seq, ",fault_type=%u",
1754 				F2FS_OPTION(sbi).fault_info.inject_type);
1755 	}
1756 #endif
1757 #ifdef CONFIG_QUOTA
1758 	if (test_opt(sbi, QUOTA))
1759 		seq_puts(seq, ",quota");
1760 	if (test_opt(sbi, USRQUOTA))
1761 		seq_puts(seq, ",usrquota");
1762 	if (test_opt(sbi, GRPQUOTA))
1763 		seq_puts(seq, ",grpquota");
1764 	if (test_opt(sbi, PRJQUOTA))
1765 		seq_puts(seq, ",prjquota");
1766 #endif
1767 	f2fs_show_quota_options(seq, sbi->sb);
1768 	if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
1769 		seq_printf(seq, ",whint_mode=%s", "user-based");
1770 	else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
1771 		seq_printf(seq, ",whint_mode=%s", "fs-based");
1772 
1773 	fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
1774 
1775 	if (sbi->sb->s_flags & SB_INLINECRYPT)
1776 		seq_puts(seq, ",inlinecrypt");
1777 
1778 	if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
1779 		seq_printf(seq, ",alloc_mode=%s", "default");
1780 	else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
1781 		seq_printf(seq, ",alloc_mode=%s", "reuse");
1782 
1783 	if (test_opt(sbi, DISABLE_CHECKPOINT))
1784 		seq_printf(seq, ",checkpoint=disable:%u",
1785 				F2FS_OPTION(sbi).unusable_cap);
1786 	if (test_opt(sbi, MERGE_CHECKPOINT))
1787 		seq_puts(seq, ",checkpoint_merge");
1788 	else
1789 		seq_puts(seq, ",nocheckpoint_merge");
1790 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
1791 		seq_printf(seq, ",fsync_mode=%s", "posix");
1792 	else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
1793 		seq_printf(seq, ",fsync_mode=%s", "strict");
1794 	else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1795 		seq_printf(seq, ",fsync_mode=%s", "nobarrier");
1796 
1797 #ifdef CONFIG_F2FS_FS_COMPRESSION
1798 	f2fs_show_compress_options(seq, sbi->sb);
1799 #endif
1800 
1801 	if (test_opt(sbi, ATGC))
1802 		seq_puts(seq, ",atgc");
1803 	return 0;
1804 }
1805 
1806 static void default_options(struct f2fs_sb_info *sbi)
1807 {
1808 	/* init some FS parameters */
1809 	F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
1810 	F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1811 	F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1812 	F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1813 	F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1814 	F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1815 	F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
1816 	F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
1817 	F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
1818 	F2FS_OPTION(sbi).compress_ext_cnt = 0;
1819 	F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1820 	F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
1821 
1822 	sbi->sb->s_flags &= ~SB_INLINECRYPT;
1823 
1824 	set_opt(sbi, INLINE_XATTR);
1825 	set_opt(sbi, INLINE_DATA);
1826 	set_opt(sbi, INLINE_DENTRY);
1827 	set_opt(sbi, EXTENT_CACHE);
1828 	set_opt(sbi, NOHEAP);
1829 	clear_opt(sbi, DISABLE_CHECKPOINT);
1830 	F2FS_OPTION(sbi).unusable_cap = 0;
1831 	sbi->sb->s_flags |= SB_LAZYTIME;
1832 	set_opt(sbi, FLUSH_MERGE);
1833 	set_opt(sbi, DISCARD);
1834 	if (f2fs_sb_has_blkzoned(sbi))
1835 		F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
1836 	else
1837 		F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
1838 
1839 #ifdef CONFIG_F2FS_FS_XATTR
1840 	set_opt(sbi, XATTR_USER);
1841 #endif
1842 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1843 	set_opt(sbi, POSIX_ACL);
1844 #endif
1845 
1846 	f2fs_build_fault_attr(sbi, 0, 0);
1847 }
1848 
1849 #ifdef CONFIG_QUOTA
1850 static int f2fs_enable_quotas(struct super_block *sb);
1851 #endif
1852 
1853 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
1854 {
1855 	unsigned int s_flags = sbi->sb->s_flags;
1856 	struct cp_control cpc;
1857 	int err = 0;
1858 	int ret;
1859 	block_t unusable;
1860 
1861 	if (s_flags & SB_RDONLY) {
1862 		f2fs_err(sbi, "checkpoint=disable on readonly fs");
1863 		return -EINVAL;
1864 	}
1865 	sbi->sb->s_flags |= SB_ACTIVE;
1866 
1867 	f2fs_update_time(sbi, DISABLE_TIME);
1868 
1869 	while (!f2fs_time_over(sbi, DISABLE_TIME)) {
1870 		down_write(&sbi->gc_lock);
1871 		err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1872 		if (err == -ENODATA) {
1873 			err = 0;
1874 			break;
1875 		}
1876 		if (err && err != -EAGAIN)
1877 			break;
1878 	}
1879 
1880 	ret = sync_filesystem(sbi->sb);
1881 	if (ret || err) {
1882 		err = ret ? ret: err;
1883 		goto restore_flag;
1884 	}
1885 
1886 	unusable = f2fs_get_unusable_blocks(sbi);
1887 	if (f2fs_disable_cp_again(sbi, unusable)) {
1888 		err = -EAGAIN;
1889 		goto restore_flag;
1890 	}
1891 
1892 	down_write(&sbi->gc_lock);
1893 	cpc.reason = CP_PAUSE;
1894 	set_sbi_flag(sbi, SBI_CP_DISABLED);
1895 	err = f2fs_write_checkpoint(sbi, &cpc);
1896 	if (err)
1897 		goto out_unlock;
1898 
1899 	spin_lock(&sbi->stat_lock);
1900 	sbi->unusable_block_count = unusable;
1901 	spin_unlock(&sbi->stat_lock);
1902 
1903 out_unlock:
1904 	up_write(&sbi->gc_lock);
1905 restore_flag:
1906 	sbi->sb->s_flags = s_flags;	/* Restore SB_RDONLY status */
1907 	return err;
1908 }
1909 
1910 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
1911 {
1912 	/* we should flush all the data to keep data consistency */
1913 	sync_inodes_sb(sbi->sb);
1914 
1915 	down_write(&sbi->gc_lock);
1916 	f2fs_dirty_to_prefree(sbi);
1917 
1918 	clear_sbi_flag(sbi, SBI_CP_DISABLED);
1919 	set_sbi_flag(sbi, SBI_IS_DIRTY);
1920 	up_write(&sbi->gc_lock);
1921 
1922 	f2fs_sync_fs(sbi->sb, 1);
1923 }
1924 
1925 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1926 {
1927 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1928 	struct f2fs_mount_info org_mount_opt;
1929 	unsigned long old_sb_flags;
1930 	int err;
1931 	bool need_restart_gc = false;
1932 	bool need_stop_gc = false;
1933 	bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
1934 	bool disable_checkpoint = test_opt(sbi, DISABLE_CHECKPOINT);
1935 	bool no_io_align = !F2FS_IO_ALIGNED(sbi);
1936 	bool no_atgc = !test_opt(sbi, ATGC);
1937 	bool checkpoint_changed;
1938 #ifdef CONFIG_QUOTA
1939 	int i, j;
1940 #endif
1941 
1942 	/*
1943 	 * Save the old mount options in case we
1944 	 * need to restore them.
1945 	 */
1946 	org_mount_opt = sbi->mount_opt;
1947 	old_sb_flags = sb->s_flags;
1948 
1949 #ifdef CONFIG_QUOTA
1950 	org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
1951 	for (i = 0; i < MAXQUOTAS; i++) {
1952 		if (F2FS_OPTION(sbi).s_qf_names[i]) {
1953 			org_mount_opt.s_qf_names[i] =
1954 				kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1955 				GFP_KERNEL);
1956 			if (!org_mount_opt.s_qf_names[i]) {
1957 				for (j = 0; j < i; j++)
1958 					kfree(org_mount_opt.s_qf_names[j]);
1959 				return -ENOMEM;
1960 			}
1961 		} else {
1962 			org_mount_opt.s_qf_names[i] = NULL;
1963 		}
1964 	}
1965 #endif
1966 
1967 	/* recover superblocks we couldn't write due to previous RO mount */
1968 	if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
1969 		err = f2fs_commit_super(sbi, false);
1970 		f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
1971 			  err);
1972 		if (!err)
1973 			clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1974 	}
1975 
1976 	default_options(sbi);
1977 
1978 	/* parse mount options */
1979 	err = parse_options(sb, data, true);
1980 	if (err)
1981 		goto restore_opts;
1982 	checkpoint_changed =
1983 			disable_checkpoint != test_opt(sbi, DISABLE_CHECKPOINT);
1984 
1985 	/*
1986 	 * Previous and new state of filesystem is RO,
1987 	 * so skip checking GC and FLUSH_MERGE conditions.
1988 	 */
1989 	if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
1990 		goto skip;
1991 
1992 #ifdef CONFIG_QUOTA
1993 	if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
1994 		err = dquot_suspend(sb, -1);
1995 		if (err < 0)
1996 			goto restore_opts;
1997 	} else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
1998 		/* dquot_resume needs RW */
1999 		sb->s_flags &= ~SB_RDONLY;
2000 		if (sb_any_quota_suspended(sb)) {
2001 			dquot_resume(sb, -1);
2002 		} else if (f2fs_sb_has_quota_ino(sbi)) {
2003 			err = f2fs_enable_quotas(sb);
2004 			if (err)
2005 				goto restore_opts;
2006 		}
2007 	}
2008 #endif
2009 	/* disallow enable atgc dynamically */
2010 	if (no_atgc == !!test_opt(sbi, ATGC)) {
2011 		err = -EINVAL;
2012 		f2fs_warn(sbi, "switch atgc option is not allowed");
2013 		goto restore_opts;
2014 	}
2015 
2016 	/* disallow enable/disable extent_cache dynamically */
2017 	if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
2018 		err = -EINVAL;
2019 		f2fs_warn(sbi, "switch extent_cache option is not allowed");
2020 		goto restore_opts;
2021 	}
2022 
2023 	if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
2024 		err = -EINVAL;
2025 		f2fs_warn(sbi, "switch io_bits option is not allowed");
2026 		goto restore_opts;
2027 	}
2028 
2029 	if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2030 		err = -EINVAL;
2031 		f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
2032 		goto restore_opts;
2033 	}
2034 
2035 	/*
2036 	 * We stop the GC thread if FS is mounted as RO
2037 	 * or if background_gc = off is passed in mount
2038 	 * option. Also sync the filesystem.
2039 	 */
2040 	if ((*flags & SB_RDONLY) ||
2041 			F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF) {
2042 		if (sbi->gc_thread) {
2043 			f2fs_stop_gc_thread(sbi);
2044 			need_restart_gc = true;
2045 		}
2046 	} else if (!sbi->gc_thread) {
2047 		err = f2fs_start_gc_thread(sbi);
2048 		if (err)
2049 			goto restore_opts;
2050 		need_stop_gc = true;
2051 	}
2052 
2053 	if (*flags & SB_RDONLY ||
2054 		F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
2055 		sync_inodes_sb(sb);
2056 
2057 		set_sbi_flag(sbi, SBI_IS_DIRTY);
2058 		set_sbi_flag(sbi, SBI_IS_CLOSE);
2059 		f2fs_sync_fs(sb, 1);
2060 		clear_sbi_flag(sbi, SBI_IS_CLOSE);
2061 	}
2062 
2063 	if (checkpoint_changed) {
2064 		if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2065 			err = f2fs_disable_checkpoint(sbi);
2066 			if (err)
2067 				goto restore_gc;
2068 		} else {
2069 			f2fs_enable_checkpoint(sbi);
2070 		}
2071 	}
2072 
2073 	if (!test_opt(sbi, DISABLE_CHECKPOINT) &&
2074 			test_opt(sbi, MERGE_CHECKPOINT)) {
2075 		err = f2fs_start_ckpt_thread(sbi);
2076 		if (err) {
2077 			f2fs_err(sbi,
2078 			    "Failed to start F2FS issue_checkpoint_thread (%d)",
2079 			    err);
2080 			goto restore_gc;
2081 		}
2082 	} else {
2083 		f2fs_stop_ckpt_thread(sbi);
2084 	}
2085 
2086 	/*
2087 	 * We stop issue flush thread if FS is mounted as RO
2088 	 * or if flush_merge is not passed in mount option.
2089 	 */
2090 	if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2091 		clear_opt(sbi, FLUSH_MERGE);
2092 		f2fs_destroy_flush_cmd_control(sbi, false);
2093 	} else {
2094 		err = f2fs_create_flush_cmd_control(sbi);
2095 		if (err)
2096 			goto restore_gc;
2097 	}
2098 skip:
2099 #ifdef CONFIG_QUOTA
2100 	/* Release old quota file names */
2101 	for (i = 0; i < MAXQUOTAS; i++)
2102 		kfree(org_mount_opt.s_qf_names[i]);
2103 #endif
2104 	/* Update the POSIXACL Flag */
2105 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2106 		(test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2107 
2108 	limit_reserve_root(sbi);
2109 	adjust_unusable_cap_perc(sbi);
2110 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
2111 	return 0;
2112 restore_gc:
2113 	if (need_restart_gc) {
2114 		if (f2fs_start_gc_thread(sbi))
2115 			f2fs_warn(sbi, "background gc thread has stopped");
2116 	} else if (need_stop_gc) {
2117 		f2fs_stop_gc_thread(sbi);
2118 	}
2119 restore_opts:
2120 #ifdef CONFIG_QUOTA
2121 	F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
2122 	for (i = 0; i < MAXQUOTAS; i++) {
2123 		kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2124 		F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
2125 	}
2126 #endif
2127 	sbi->mount_opt = org_mount_opt;
2128 	sb->s_flags = old_sb_flags;
2129 	return err;
2130 }
2131 
2132 #ifdef CONFIG_QUOTA
2133 /* Read data from quotafile */
2134 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2135 			       size_t len, loff_t off)
2136 {
2137 	struct inode *inode = sb_dqopt(sb)->files[type];
2138 	struct address_space *mapping = inode->i_mapping;
2139 	block_t blkidx = F2FS_BYTES_TO_BLK(off);
2140 	int offset = off & (sb->s_blocksize - 1);
2141 	int tocopy;
2142 	size_t toread;
2143 	loff_t i_size = i_size_read(inode);
2144 	struct page *page;
2145 	char *kaddr;
2146 
2147 	if (off > i_size)
2148 		return 0;
2149 
2150 	if (off + len > i_size)
2151 		len = i_size - off;
2152 	toread = len;
2153 	while (toread > 0) {
2154 		tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2155 repeat:
2156 		page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
2157 		if (IS_ERR(page)) {
2158 			if (PTR_ERR(page) == -ENOMEM) {
2159 				congestion_wait(BLK_RW_ASYNC,
2160 						DEFAULT_IO_TIMEOUT);
2161 				goto repeat;
2162 			}
2163 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2164 			return PTR_ERR(page);
2165 		}
2166 
2167 		lock_page(page);
2168 
2169 		if (unlikely(page->mapping != mapping)) {
2170 			f2fs_put_page(page, 1);
2171 			goto repeat;
2172 		}
2173 		if (unlikely(!PageUptodate(page))) {
2174 			f2fs_put_page(page, 1);
2175 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2176 			return -EIO;
2177 		}
2178 
2179 		kaddr = kmap_atomic(page);
2180 		memcpy(data, kaddr + offset, tocopy);
2181 		kunmap_atomic(kaddr);
2182 		f2fs_put_page(page, 1);
2183 
2184 		offset = 0;
2185 		toread -= tocopy;
2186 		data += tocopy;
2187 		blkidx++;
2188 	}
2189 	return len;
2190 }
2191 
2192 /* Write to quotafile */
2193 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2194 				const char *data, size_t len, loff_t off)
2195 {
2196 	struct inode *inode = sb_dqopt(sb)->files[type];
2197 	struct address_space *mapping = inode->i_mapping;
2198 	const struct address_space_operations *a_ops = mapping->a_ops;
2199 	int offset = off & (sb->s_blocksize - 1);
2200 	size_t towrite = len;
2201 	struct page *page;
2202 	void *fsdata = NULL;
2203 	char *kaddr;
2204 	int err = 0;
2205 	int tocopy;
2206 
2207 	while (towrite > 0) {
2208 		tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2209 								towrite);
2210 retry:
2211 		err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
2212 							&page, &fsdata);
2213 		if (unlikely(err)) {
2214 			if (err == -ENOMEM) {
2215 				congestion_wait(BLK_RW_ASYNC,
2216 						DEFAULT_IO_TIMEOUT);
2217 				goto retry;
2218 			}
2219 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2220 			break;
2221 		}
2222 
2223 		kaddr = kmap_atomic(page);
2224 		memcpy(kaddr + offset, data, tocopy);
2225 		kunmap_atomic(kaddr);
2226 		flush_dcache_page(page);
2227 
2228 		a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2229 						page, fsdata);
2230 		offset = 0;
2231 		towrite -= tocopy;
2232 		off += tocopy;
2233 		data += tocopy;
2234 		cond_resched();
2235 	}
2236 
2237 	if (len == towrite)
2238 		return err;
2239 	inode->i_mtime = inode->i_ctime = current_time(inode);
2240 	f2fs_mark_inode_dirty_sync(inode, false);
2241 	return len - towrite;
2242 }
2243 
2244 static struct dquot **f2fs_get_dquots(struct inode *inode)
2245 {
2246 	return F2FS_I(inode)->i_dquot;
2247 }
2248 
2249 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2250 {
2251 	return &F2FS_I(inode)->i_reserved_quota;
2252 }
2253 
2254 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2255 {
2256 	if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2257 		f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2258 		return 0;
2259 	}
2260 
2261 	return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2262 					F2FS_OPTION(sbi).s_jquota_fmt, type);
2263 }
2264 
2265 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2266 {
2267 	int enabled = 0;
2268 	int i, err;
2269 
2270 	if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2271 		err = f2fs_enable_quotas(sbi->sb);
2272 		if (err) {
2273 			f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2274 			return 0;
2275 		}
2276 		return 1;
2277 	}
2278 
2279 	for (i = 0; i < MAXQUOTAS; i++) {
2280 		if (F2FS_OPTION(sbi).s_qf_names[i]) {
2281 			err = f2fs_quota_on_mount(sbi, i);
2282 			if (!err) {
2283 				enabled = 1;
2284 				continue;
2285 			}
2286 			f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2287 				 err, i);
2288 		}
2289 	}
2290 	return enabled;
2291 }
2292 
2293 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2294 			     unsigned int flags)
2295 {
2296 	struct inode *qf_inode;
2297 	unsigned long qf_inum;
2298 	int err;
2299 
2300 	BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2301 
2302 	qf_inum = f2fs_qf_ino(sb, type);
2303 	if (!qf_inum)
2304 		return -EPERM;
2305 
2306 	qf_inode = f2fs_iget(sb, qf_inum);
2307 	if (IS_ERR(qf_inode)) {
2308 		f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2309 		return PTR_ERR(qf_inode);
2310 	}
2311 
2312 	/* Don't account quota for quota files to avoid recursion */
2313 	qf_inode->i_flags |= S_NOQUOTA;
2314 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2315 	iput(qf_inode);
2316 	return err;
2317 }
2318 
2319 static int f2fs_enable_quotas(struct super_block *sb)
2320 {
2321 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2322 	int type, err = 0;
2323 	unsigned long qf_inum;
2324 	bool quota_mopt[MAXQUOTAS] = {
2325 		test_opt(sbi, USRQUOTA),
2326 		test_opt(sbi, GRPQUOTA),
2327 		test_opt(sbi, PRJQUOTA),
2328 	};
2329 
2330 	if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2331 		f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2332 		return 0;
2333 	}
2334 
2335 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2336 
2337 	for (type = 0; type < MAXQUOTAS; type++) {
2338 		qf_inum = f2fs_qf_ino(sb, type);
2339 		if (qf_inum) {
2340 			err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2341 				DQUOT_USAGE_ENABLED |
2342 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2343 			if (err) {
2344 				f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2345 					 type, err);
2346 				for (type--; type >= 0; type--)
2347 					dquot_quota_off(sb, type);
2348 				set_sbi_flag(F2FS_SB(sb),
2349 						SBI_QUOTA_NEED_REPAIR);
2350 				return err;
2351 			}
2352 		}
2353 	}
2354 	return 0;
2355 }
2356 
2357 int f2fs_quota_sync(struct super_block *sb, int type)
2358 {
2359 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2360 	struct quota_info *dqopt = sb_dqopt(sb);
2361 	int cnt;
2362 	int ret;
2363 
2364 	/*
2365 	 * do_quotactl
2366 	 *  f2fs_quota_sync
2367 	 *  down_read(quota_sem)
2368 	 *  dquot_writeback_dquots()
2369 	 *  f2fs_dquot_commit
2370 	 *                            block_operation
2371 	 *                            down_read(quota_sem)
2372 	 */
2373 	f2fs_lock_op(sbi);
2374 
2375 	down_read(&sbi->quota_sem);
2376 	ret = dquot_writeback_dquots(sb, type);
2377 	if (ret)
2378 		goto out;
2379 
2380 	/*
2381 	 * Now when everything is written we can discard the pagecache so
2382 	 * that userspace sees the changes.
2383 	 */
2384 	for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2385 		struct address_space *mapping;
2386 
2387 		if (type != -1 && cnt != type)
2388 			continue;
2389 		if (!sb_has_quota_active(sb, cnt))
2390 			continue;
2391 
2392 		mapping = dqopt->files[cnt]->i_mapping;
2393 
2394 		ret = filemap_fdatawrite(mapping);
2395 		if (ret)
2396 			goto out;
2397 
2398 		/* if we are using journalled quota */
2399 		if (is_journalled_quota(sbi))
2400 			continue;
2401 
2402 		ret = filemap_fdatawait(mapping);
2403 		if (ret)
2404 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2405 
2406 		inode_lock(dqopt->files[cnt]);
2407 		truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2408 		inode_unlock(dqopt->files[cnt]);
2409 	}
2410 out:
2411 	if (ret)
2412 		set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2413 	up_read(&sbi->quota_sem);
2414 	f2fs_unlock_op(sbi);
2415 	return ret;
2416 }
2417 
2418 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2419 							const struct path *path)
2420 {
2421 	struct inode *inode;
2422 	int err;
2423 
2424 	/* if quota sysfile exists, deny enabling quota with specific file */
2425 	if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2426 		f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2427 		return -EBUSY;
2428 	}
2429 
2430 	err = f2fs_quota_sync(sb, type);
2431 	if (err)
2432 		return err;
2433 
2434 	err = dquot_quota_on(sb, type, format_id, path);
2435 	if (err)
2436 		return err;
2437 
2438 	inode = d_inode(path->dentry);
2439 
2440 	inode_lock(inode);
2441 	F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
2442 	f2fs_set_inode_flags(inode);
2443 	inode_unlock(inode);
2444 	f2fs_mark_inode_dirty_sync(inode, false);
2445 
2446 	return 0;
2447 }
2448 
2449 static int __f2fs_quota_off(struct super_block *sb, int type)
2450 {
2451 	struct inode *inode = sb_dqopt(sb)->files[type];
2452 	int err;
2453 
2454 	if (!inode || !igrab(inode))
2455 		return dquot_quota_off(sb, type);
2456 
2457 	err = f2fs_quota_sync(sb, type);
2458 	if (err)
2459 		goto out_put;
2460 
2461 	err = dquot_quota_off(sb, type);
2462 	if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
2463 		goto out_put;
2464 
2465 	inode_lock(inode);
2466 	F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
2467 	f2fs_set_inode_flags(inode);
2468 	inode_unlock(inode);
2469 	f2fs_mark_inode_dirty_sync(inode, false);
2470 out_put:
2471 	iput(inode);
2472 	return err;
2473 }
2474 
2475 static int f2fs_quota_off(struct super_block *sb, int type)
2476 {
2477 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2478 	int err;
2479 
2480 	err = __f2fs_quota_off(sb, type);
2481 
2482 	/*
2483 	 * quotactl can shutdown journalled quota, result in inconsistence
2484 	 * between quota record and fs data by following updates, tag the
2485 	 * flag to let fsck be aware of it.
2486 	 */
2487 	if (is_journalled_quota(sbi))
2488 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2489 	return err;
2490 }
2491 
2492 void f2fs_quota_off_umount(struct super_block *sb)
2493 {
2494 	int type;
2495 	int err;
2496 
2497 	for (type = 0; type < MAXQUOTAS; type++) {
2498 		err = __f2fs_quota_off(sb, type);
2499 		if (err) {
2500 			int ret = dquot_quota_off(sb, type);
2501 
2502 			f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
2503 				 type, err, ret);
2504 			set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2505 		}
2506 	}
2507 	/*
2508 	 * In case of checkpoint=disable, we must flush quota blocks.
2509 	 * This can cause NULL exception for node_inode in end_io, since
2510 	 * put_super already dropped it.
2511 	 */
2512 	sync_filesystem(sb);
2513 }
2514 
2515 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
2516 {
2517 	struct quota_info *dqopt = sb_dqopt(sb);
2518 	int type;
2519 
2520 	for (type = 0; type < MAXQUOTAS; type++) {
2521 		if (!dqopt->files[type])
2522 			continue;
2523 		f2fs_inode_synced(dqopt->files[type]);
2524 	}
2525 }
2526 
2527 static int f2fs_dquot_commit(struct dquot *dquot)
2528 {
2529 	struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2530 	int ret;
2531 
2532 	down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
2533 	ret = dquot_commit(dquot);
2534 	if (ret < 0)
2535 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2536 	up_read(&sbi->quota_sem);
2537 	return ret;
2538 }
2539 
2540 static int f2fs_dquot_acquire(struct dquot *dquot)
2541 {
2542 	struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2543 	int ret;
2544 
2545 	down_read(&sbi->quota_sem);
2546 	ret = dquot_acquire(dquot);
2547 	if (ret < 0)
2548 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2549 	up_read(&sbi->quota_sem);
2550 	return ret;
2551 }
2552 
2553 static int f2fs_dquot_release(struct dquot *dquot)
2554 {
2555 	struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2556 	int ret = dquot_release(dquot);
2557 
2558 	if (ret < 0)
2559 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2560 	return ret;
2561 }
2562 
2563 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
2564 {
2565 	struct super_block *sb = dquot->dq_sb;
2566 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2567 	int ret = dquot_mark_dquot_dirty(dquot);
2568 
2569 	/* if we are using journalled quota */
2570 	if (is_journalled_quota(sbi))
2571 		set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
2572 
2573 	return ret;
2574 }
2575 
2576 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
2577 {
2578 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2579 	int ret = dquot_commit_info(sb, type);
2580 
2581 	if (ret < 0)
2582 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2583 	return ret;
2584 }
2585 
2586 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
2587 {
2588 	*projid = F2FS_I(inode)->i_projid;
2589 	return 0;
2590 }
2591 
2592 static const struct dquot_operations f2fs_quota_operations = {
2593 	.get_reserved_space = f2fs_get_reserved_space,
2594 	.write_dquot	= f2fs_dquot_commit,
2595 	.acquire_dquot	= f2fs_dquot_acquire,
2596 	.release_dquot	= f2fs_dquot_release,
2597 	.mark_dirty	= f2fs_dquot_mark_dquot_dirty,
2598 	.write_info	= f2fs_dquot_commit_info,
2599 	.alloc_dquot	= dquot_alloc,
2600 	.destroy_dquot	= dquot_destroy,
2601 	.get_projid	= f2fs_get_projid,
2602 	.get_next_id	= dquot_get_next_id,
2603 };
2604 
2605 static const struct quotactl_ops f2fs_quotactl_ops = {
2606 	.quota_on	= f2fs_quota_on,
2607 	.quota_off	= f2fs_quota_off,
2608 	.quota_sync	= f2fs_quota_sync,
2609 	.get_state	= dquot_get_state,
2610 	.set_info	= dquot_set_dqinfo,
2611 	.get_dqblk	= dquot_get_dqblk,
2612 	.set_dqblk	= dquot_set_dqblk,
2613 	.get_nextdqblk	= dquot_get_next_dqblk,
2614 };
2615 #else
2616 int f2fs_quota_sync(struct super_block *sb, int type)
2617 {
2618 	return 0;
2619 }
2620 
2621 void f2fs_quota_off_umount(struct super_block *sb)
2622 {
2623 }
2624 #endif
2625 
2626 static const struct super_operations f2fs_sops = {
2627 	.alloc_inode	= f2fs_alloc_inode,
2628 	.free_inode	= f2fs_free_inode,
2629 	.drop_inode	= f2fs_drop_inode,
2630 	.write_inode	= f2fs_write_inode,
2631 	.dirty_inode	= f2fs_dirty_inode,
2632 	.show_options	= f2fs_show_options,
2633 #ifdef CONFIG_QUOTA
2634 	.quota_read	= f2fs_quota_read,
2635 	.quota_write	= f2fs_quota_write,
2636 	.get_dquots	= f2fs_get_dquots,
2637 #endif
2638 	.evict_inode	= f2fs_evict_inode,
2639 	.put_super	= f2fs_put_super,
2640 	.sync_fs	= f2fs_sync_fs,
2641 	.freeze_fs	= f2fs_freeze,
2642 	.unfreeze_fs	= f2fs_unfreeze,
2643 	.statfs		= f2fs_statfs,
2644 	.remount_fs	= f2fs_remount,
2645 };
2646 
2647 #ifdef CONFIG_FS_ENCRYPTION
2648 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
2649 {
2650 	return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2651 				F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2652 				ctx, len, NULL);
2653 }
2654 
2655 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
2656 							void *fs_data)
2657 {
2658 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2659 
2660 	/*
2661 	 * Encrypting the root directory is not allowed because fsck
2662 	 * expects lost+found directory to exist and remain unencrypted
2663 	 * if LOST_FOUND feature is enabled.
2664 	 *
2665 	 */
2666 	if (f2fs_sb_has_lost_found(sbi) &&
2667 			inode->i_ino == F2FS_ROOT_INO(sbi))
2668 		return -EPERM;
2669 
2670 	return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2671 				F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2672 				ctx, len, fs_data, XATTR_CREATE);
2673 }
2674 
2675 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
2676 {
2677 	return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
2678 }
2679 
2680 static bool f2fs_has_stable_inodes(struct super_block *sb)
2681 {
2682 	return true;
2683 }
2684 
2685 static void f2fs_get_ino_and_lblk_bits(struct super_block *sb,
2686 				       int *ino_bits_ret, int *lblk_bits_ret)
2687 {
2688 	*ino_bits_ret = 8 * sizeof(nid_t);
2689 	*lblk_bits_ret = 8 * sizeof(block_t);
2690 }
2691 
2692 static int f2fs_get_num_devices(struct super_block *sb)
2693 {
2694 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2695 
2696 	if (f2fs_is_multi_device(sbi))
2697 		return sbi->s_ndevs;
2698 	return 1;
2699 }
2700 
2701 static void f2fs_get_devices(struct super_block *sb,
2702 			     struct request_queue **devs)
2703 {
2704 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2705 	int i;
2706 
2707 	for (i = 0; i < sbi->s_ndevs; i++)
2708 		devs[i] = bdev_get_queue(FDEV(i).bdev);
2709 }
2710 
2711 static const struct fscrypt_operations f2fs_cryptops = {
2712 	.key_prefix		= "f2fs:",
2713 	.get_context		= f2fs_get_context,
2714 	.set_context		= f2fs_set_context,
2715 	.get_dummy_policy	= f2fs_get_dummy_policy,
2716 	.empty_dir		= f2fs_empty_dir,
2717 	.max_namelen		= F2FS_NAME_LEN,
2718 	.has_stable_inodes	= f2fs_has_stable_inodes,
2719 	.get_ino_and_lblk_bits	= f2fs_get_ino_and_lblk_bits,
2720 	.get_num_devices	= f2fs_get_num_devices,
2721 	.get_devices		= f2fs_get_devices,
2722 };
2723 #endif
2724 
2725 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2726 		u64 ino, u32 generation)
2727 {
2728 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
2729 	struct inode *inode;
2730 
2731 	if (f2fs_check_nid_range(sbi, ino))
2732 		return ERR_PTR(-ESTALE);
2733 
2734 	/*
2735 	 * f2fs_iget isn't quite right if the inode is currently unallocated!
2736 	 * However f2fs_iget currently does appropriate checks to handle stale
2737 	 * inodes so everything is OK.
2738 	 */
2739 	inode = f2fs_iget(sb, ino);
2740 	if (IS_ERR(inode))
2741 		return ERR_CAST(inode);
2742 	if (unlikely(generation && inode->i_generation != generation)) {
2743 		/* we didn't find the right inode.. */
2744 		iput(inode);
2745 		return ERR_PTR(-ESTALE);
2746 	}
2747 	return inode;
2748 }
2749 
2750 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2751 		int fh_len, int fh_type)
2752 {
2753 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2754 				    f2fs_nfs_get_inode);
2755 }
2756 
2757 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2758 		int fh_len, int fh_type)
2759 {
2760 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2761 				    f2fs_nfs_get_inode);
2762 }
2763 
2764 static const struct export_operations f2fs_export_ops = {
2765 	.fh_to_dentry = f2fs_fh_to_dentry,
2766 	.fh_to_parent = f2fs_fh_to_parent,
2767 	.get_parent = f2fs_get_parent,
2768 };
2769 
2770 loff_t max_file_blocks(struct inode *inode)
2771 {
2772 	loff_t result = 0;
2773 	loff_t leaf_count;
2774 
2775 	/*
2776 	 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
2777 	 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
2778 	 * space in inode.i_addr, it will be more safe to reassign
2779 	 * result as zero.
2780 	 */
2781 
2782 	if (inode && f2fs_compressed_file(inode))
2783 		leaf_count = ADDRS_PER_BLOCK(inode);
2784 	else
2785 		leaf_count = DEF_ADDRS_PER_BLOCK;
2786 
2787 	/* two direct node blocks */
2788 	result += (leaf_count * 2);
2789 
2790 	/* two indirect node blocks */
2791 	leaf_count *= NIDS_PER_BLOCK;
2792 	result += (leaf_count * 2);
2793 
2794 	/* one double indirect node block */
2795 	leaf_count *= NIDS_PER_BLOCK;
2796 	result += leaf_count;
2797 
2798 	return result;
2799 }
2800 
2801 static int __f2fs_commit_super(struct buffer_head *bh,
2802 			struct f2fs_super_block *super)
2803 {
2804 	lock_buffer(bh);
2805 	if (super)
2806 		memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
2807 	set_buffer_dirty(bh);
2808 	unlock_buffer(bh);
2809 
2810 	/* it's rare case, we can do fua all the time */
2811 	return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
2812 }
2813 
2814 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
2815 					struct buffer_head *bh)
2816 {
2817 	struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2818 					(bh->b_data + F2FS_SUPER_OFFSET);
2819 	struct super_block *sb = sbi->sb;
2820 	u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2821 	u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2822 	u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2823 	u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2824 	u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2825 	u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2826 	u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2827 	u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2828 	u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2829 	u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2830 	u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2831 	u32 segment_count = le32_to_cpu(raw_super->segment_count);
2832 	u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2833 	u64 main_end_blkaddr = main_blkaddr +
2834 				(segment_count_main << log_blocks_per_seg);
2835 	u64 seg_end_blkaddr = segment0_blkaddr +
2836 				(segment_count << log_blocks_per_seg);
2837 
2838 	if (segment0_blkaddr != cp_blkaddr) {
2839 		f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2840 			  segment0_blkaddr, cp_blkaddr);
2841 		return true;
2842 	}
2843 
2844 	if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2845 							sit_blkaddr) {
2846 		f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2847 			  cp_blkaddr, sit_blkaddr,
2848 			  segment_count_ckpt << log_blocks_per_seg);
2849 		return true;
2850 	}
2851 
2852 	if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2853 							nat_blkaddr) {
2854 		f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2855 			  sit_blkaddr, nat_blkaddr,
2856 			  segment_count_sit << log_blocks_per_seg);
2857 		return true;
2858 	}
2859 
2860 	if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2861 							ssa_blkaddr) {
2862 		f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2863 			  nat_blkaddr, ssa_blkaddr,
2864 			  segment_count_nat << log_blocks_per_seg);
2865 		return true;
2866 	}
2867 
2868 	if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2869 							main_blkaddr) {
2870 		f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2871 			  ssa_blkaddr, main_blkaddr,
2872 			  segment_count_ssa << log_blocks_per_seg);
2873 		return true;
2874 	}
2875 
2876 	if (main_end_blkaddr > seg_end_blkaddr) {
2877 		f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
2878 			  main_blkaddr, seg_end_blkaddr,
2879 			  segment_count_main << log_blocks_per_seg);
2880 		return true;
2881 	} else if (main_end_blkaddr < seg_end_blkaddr) {
2882 		int err = 0;
2883 		char *res;
2884 
2885 		/* fix in-memory information all the time */
2886 		raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2887 				segment0_blkaddr) >> log_blocks_per_seg);
2888 
2889 		if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
2890 			set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2891 			res = "internally";
2892 		} else {
2893 			err = __f2fs_commit_super(bh, NULL);
2894 			res = err ? "failed" : "done";
2895 		}
2896 		f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
2897 			  res, main_blkaddr, seg_end_blkaddr,
2898 			  segment_count_main << log_blocks_per_seg);
2899 		if (err)
2900 			return true;
2901 	}
2902 	return false;
2903 }
2904 
2905 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2906 				struct buffer_head *bh)
2907 {
2908 	block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
2909 	block_t total_sections, blocks_per_seg;
2910 	struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2911 					(bh->b_data + F2FS_SUPER_OFFSET);
2912 	size_t crc_offset = 0;
2913 	__u32 crc = 0;
2914 
2915 	if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
2916 		f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
2917 			  F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2918 		return -EINVAL;
2919 	}
2920 
2921 	/* Check checksum_offset and crc in superblock */
2922 	if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
2923 		crc_offset = le32_to_cpu(raw_super->checksum_offset);
2924 		if (crc_offset !=
2925 			offsetof(struct f2fs_super_block, crc)) {
2926 			f2fs_info(sbi, "Invalid SB checksum offset: %zu",
2927 				  crc_offset);
2928 			return -EFSCORRUPTED;
2929 		}
2930 		crc = le32_to_cpu(raw_super->crc);
2931 		if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
2932 			f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
2933 			return -EFSCORRUPTED;
2934 		}
2935 	}
2936 
2937 	/* Currently, support only 4KB block size */
2938 	if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
2939 		f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
2940 			  le32_to_cpu(raw_super->log_blocksize),
2941 			  F2FS_BLKSIZE_BITS);
2942 		return -EFSCORRUPTED;
2943 	}
2944 
2945 	/* check log blocks per segment */
2946 	if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2947 		f2fs_info(sbi, "Invalid log blocks per segment (%u)",
2948 			  le32_to_cpu(raw_super->log_blocks_per_seg));
2949 		return -EFSCORRUPTED;
2950 	}
2951 
2952 	/* Currently, support 512/1024/2048/4096 bytes sector size */
2953 	if (le32_to_cpu(raw_super->log_sectorsize) >
2954 				F2FS_MAX_LOG_SECTOR_SIZE ||
2955 		le32_to_cpu(raw_super->log_sectorsize) <
2956 				F2FS_MIN_LOG_SECTOR_SIZE) {
2957 		f2fs_info(sbi, "Invalid log sectorsize (%u)",
2958 			  le32_to_cpu(raw_super->log_sectorsize));
2959 		return -EFSCORRUPTED;
2960 	}
2961 	if (le32_to_cpu(raw_super->log_sectors_per_block) +
2962 		le32_to_cpu(raw_super->log_sectorsize) !=
2963 			F2FS_MAX_LOG_SECTOR_SIZE) {
2964 		f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
2965 			  le32_to_cpu(raw_super->log_sectors_per_block),
2966 			  le32_to_cpu(raw_super->log_sectorsize));
2967 		return -EFSCORRUPTED;
2968 	}
2969 
2970 	segment_count = le32_to_cpu(raw_super->segment_count);
2971 	segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2972 	segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2973 	secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2974 	total_sections = le32_to_cpu(raw_super->section_count);
2975 
2976 	/* blocks_per_seg should be 512, given the above check */
2977 	blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2978 
2979 	if (segment_count > F2FS_MAX_SEGMENT ||
2980 				segment_count < F2FS_MIN_SEGMENTS) {
2981 		f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
2982 		return -EFSCORRUPTED;
2983 	}
2984 
2985 	if (total_sections > segment_count_main || total_sections < 1 ||
2986 			segs_per_sec > segment_count || !segs_per_sec) {
2987 		f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
2988 			  segment_count, total_sections, segs_per_sec);
2989 		return -EFSCORRUPTED;
2990 	}
2991 
2992 	if (segment_count_main != total_sections * segs_per_sec) {
2993 		f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
2994 			  segment_count_main, total_sections, segs_per_sec);
2995 		return -EFSCORRUPTED;
2996 	}
2997 
2998 	if ((segment_count / segs_per_sec) < total_sections) {
2999 		f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3000 			  segment_count, segs_per_sec, total_sections);
3001 		return -EFSCORRUPTED;
3002 	}
3003 
3004 	if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
3005 		f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3006 			  segment_count, le64_to_cpu(raw_super->block_count));
3007 		return -EFSCORRUPTED;
3008 	}
3009 
3010 	if (RDEV(0).path[0]) {
3011 		block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3012 		int i = 1;
3013 
3014 		while (i < MAX_DEVICES && RDEV(i).path[0]) {
3015 			dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3016 			i++;
3017 		}
3018 		if (segment_count != dev_seg_count) {
3019 			f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3020 					segment_count, dev_seg_count);
3021 			return -EFSCORRUPTED;
3022 		}
3023 	} else {
3024 		if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3025 					!bdev_is_zoned(sbi->sb->s_bdev)) {
3026 			f2fs_info(sbi, "Zoned block device path is missing");
3027 			return -EFSCORRUPTED;
3028 		}
3029 	}
3030 
3031 	if (secs_per_zone > total_sections || !secs_per_zone) {
3032 		f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3033 			  secs_per_zone, total_sections);
3034 		return -EFSCORRUPTED;
3035 	}
3036 	if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3037 			raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3038 			(le32_to_cpu(raw_super->extension_count) +
3039 			raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
3040 		f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3041 			  le32_to_cpu(raw_super->extension_count),
3042 			  raw_super->hot_ext_count,
3043 			  F2FS_MAX_EXTENSION);
3044 		return -EFSCORRUPTED;
3045 	}
3046 
3047 	if (le32_to_cpu(raw_super->cp_payload) >
3048 				(blocks_per_seg - F2FS_CP_PACKS)) {
3049 		f2fs_info(sbi, "Insane cp_payload (%u > %u)",
3050 			  le32_to_cpu(raw_super->cp_payload),
3051 			  blocks_per_seg - F2FS_CP_PACKS);
3052 		return -EFSCORRUPTED;
3053 	}
3054 
3055 	/* check reserved ino info */
3056 	if (le32_to_cpu(raw_super->node_ino) != 1 ||
3057 		le32_to_cpu(raw_super->meta_ino) != 2 ||
3058 		le32_to_cpu(raw_super->root_ino) != 3) {
3059 		f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3060 			  le32_to_cpu(raw_super->node_ino),
3061 			  le32_to_cpu(raw_super->meta_ino),
3062 			  le32_to_cpu(raw_super->root_ino));
3063 		return -EFSCORRUPTED;
3064 	}
3065 
3066 	/* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
3067 	if (sanity_check_area_boundary(sbi, bh))
3068 		return -EFSCORRUPTED;
3069 
3070 	return 0;
3071 }
3072 
3073 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
3074 {
3075 	unsigned int total, fsmeta;
3076 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3077 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3078 	unsigned int ovp_segments, reserved_segments;
3079 	unsigned int main_segs, blocks_per_seg;
3080 	unsigned int sit_segs, nat_segs;
3081 	unsigned int sit_bitmap_size, nat_bitmap_size;
3082 	unsigned int log_blocks_per_seg;
3083 	unsigned int segment_count_main;
3084 	unsigned int cp_pack_start_sum, cp_payload;
3085 	block_t user_block_count, valid_user_blocks;
3086 	block_t avail_node_count, valid_node_count;
3087 	int i, j;
3088 
3089 	total = le32_to_cpu(raw_super->segment_count);
3090 	fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
3091 	sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3092 	fsmeta += sit_segs;
3093 	nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3094 	fsmeta += nat_segs;
3095 	fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3096 	fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3097 
3098 	if (unlikely(fsmeta >= total))
3099 		return 1;
3100 
3101 	ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3102 	reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3103 
3104 	if (unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
3105 			ovp_segments == 0 || reserved_segments == 0)) {
3106 		f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
3107 		return 1;
3108 	}
3109 
3110 	user_block_count = le64_to_cpu(ckpt->user_block_count);
3111 	segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3112 	log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3113 	if (!user_block_count || user_block_count >=
3114 			segment_count_main << log_blocks_per_seg) {
3115 		f2fs_err(sbi, "Wrong user_block_count: %u",
3116 			 user_block_count);
3117 		return 1;
3118 	}
3119 
3120 	valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3121 	if (valid_user_blocks > user_block_count) {
3122 		f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3123 			 valid_user_blocks, user_block_count);
3124 		return 1;
3125 	}
3126 
3127 	valid_node_count = le32_to_cpu(ckpt->valid_node_count);
3128 	avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
3129 	if (valid_node_count > avail_node_count) {
3130 		f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3131 			 valid_node_count, avail_node_count);
3132 		return 1;
3133 	}
3134 
3135 	main_segs = le32_to_cpu(raw_super->segment_count_main);
3136 	blocks_per_seg = sbi->blocks_per_seg;
3137 
3138 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3139 		if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3140 			le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3141 			return 1;
3142 		for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3143 			if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3144 				le32_to_cpu(ckpt->cur_node_segno[j])) {
3145 				f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3146 					 i, j,
3147 					 le32_to_cpu(ckpt->cur_node_segno[i]));
3148 				return 1;
3149 			}
3150 		}
3151 	}
3152 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3153 		if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3154 			le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3155 			return 1;
3156 		for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3157 			if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3158 				le32_to_cpu(ckpt->cur_data_segno[j])) {
3159 				f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3160 					 i, j,
3161 					 le32_to_cpu(ckpt->cur_data_segno[i]));
3162 				return 1;
3163 			}
3164 		}
3165 	}
3166 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3167 		for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3168 			if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3169 				le32_to_cpu(ckpt->cur_data_segno[j])) {
3170 				f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3171 					 i, j,
3172 					 le32_to_cpu(ckpt->cur_node_segno[i]));
3173 				return 1;
3174 			}
3175 		}
3176 	}
3177 
3178 	sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3179 	nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3180 
3181 	if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3182 		nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3183 		f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3184 			 sit_bitmap_size, nat_bitmap_size);
3185 		return 1;
3186 	}
3187 
3188 	cp_pack_start_sum = __start_sum_addr(sbi);
3189 	cp_payload = __cp_payload(sbi);
3190 	if (cp_pack_start_sum < cp_payload + 1 ||
3191 		cp_pack_start_sum > blocks_per_seg - 1 -
3192 			NR_CURSEG_PERSIST_TYPE) {
3193 		f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3194 			 cp_pack_start_sum);
3195 		return 1;
3196 	}
3197 
3198 	if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3199 		le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3200 		f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3201 			  "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3202 			  "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3203 			  le32_to_cpu(ckpt->checksum_offset));
3204 		return 1;
3205 	}
3206 
3207 	if (unlikely(f2fs_cp_error(sbi))) {
3208 		f2fs_err(sbi, "A bug case: need to run fsck");
3209 		return 1;
3210 	}
3211 	return 0;
3212 }
3213 
3214 static void init_sb_info(struct f2fs_sb_info *sbi)
3215 {
3216 	struct f2fs_super_block *raw_super = sbi->raw_super;
3217 	int i;
3218 
3219 	sbi->log_sectors_per_block =
3220 		le32_to_cpu(raw_super->log_sectors_per_block);
3221 	sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3222 	sbi->blocksize = 1 << sbi->log_blocksize;
3223 	sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3224 	sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
3225 	sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3226 	sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3227 	sbi->total_sections = le32_to_cpu(raw_super->section_count);
3228 	sbi->total_node_count =
3229 		(le32_to_cpu(raw_super->segment_count_nat) / 2)
3230 			* sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
3231 	F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3232 	F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3233 	F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3234 	sbi->cur_victim_sec = NULL_SECNO;
3235 	sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3236 	sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3237 	sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3238 	sbi->migration_granularity = sbi->segs_per_sec;
3239 
3240 	sbi->dir_level = DEF_DIR_LEVEL;
3241 	sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3242 	sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3243 	sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3244 	sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3245 	sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3246 	sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3247 				DEF_UMOUNT_DISCARD_TIMEOUT;
3248 	clear_sbi_flag(sbi, SBI_NEED_FSCK);
3249 
3250 	for (i = 0; i < NR_COUNT_TYPE; i++)
3251 		atomic_set(&sbi->nr_pages[i], 0);
3252 
3253 	for (i = 0; i < META; i++)
3254 		atomic_set(&sbi->wb_sync_req[i], 0);
3255 
3256 	INIT_LIST_HEAD(&sbi->s_list);
3257 	mutex_init(&sbi->umount_mutex);
3258 	init_rwsem(&sbi->io_order_lock);
3259 	spin_lock_init(&sbi->cp_lock);
3260 
3261 	sbi->dirty_device = 0;
3262 	spin_lock_init(&sbi->dev_lock);
3263 
3264 	init_rwsem(&sbi->sb_lock);
3265 	init_rwsem(&sbi->pin_sem);
3266 }
3267 
3268 static int init_percpu_info(struct f2fs_sb_info *sbi)
3269 {
3270 	int err;
3271 
3272 	err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3273 	if (err)
3274 		return err;
3275 
3276 	err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3277 								GFP_KERNEL);
3278 	if (err)
3279 		percpu_counter_destroy(&sbi->alloc_valid_block_count);
3280 
3281 	return err;
3282 }
3283 
3284 #ifdef CONFIG_BLK_DEV_ZONED
3285 
3286 struct f2fs_report_zones_args {
3287 	struct f2fs_dev_info *dev;
3288 	bool zone_cap_mismatch;
3289 };
3290 
3291 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3292 			      void *data)
3293 {
3294 	struct f2fs_report_zones_args *rz_args = data;
3295 
3296 	if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3297 		return 0;
3298 
3299 	set_bit(idx, rz_args->dev->blkz_seq);
3300 	rz_args->dev->zone_capacity_blocks[idx] = zone->capacity >>
3301 						F2FS_LOG_SECTORS_PER_BLOCK;
3302 	if (zone->len != zone->capacity && !rz_args->zone_cap_mismatch)
3303 		rz_args->zone_cap_mismatch = true;
3304 
3305 	return 0;
3306 }
3307 
3308 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
3309 {
3310 	struct block_device *bdev = FDEV(devi).bdev;
3311 	sector_t nr_sectors = bdev_nr_sectors(bdev);
3312 	struct f2fs_report_zones_args rep_zone_arg;
3313 	int ret;
3314 
3315 	if (!f2fs_sb_has_blkzoned(sbi))
3316 		return 0;
3317 
3318 	if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
3319 				SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
3320 		return -EINVAL;
3321 	sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
3322 	if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
3323 				__ilog2_u32(sbi->blocks_per_blkz))
3324 		return -EINVAL;
3325 	sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
3326 	FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
3327 					sbi->log_blocks_per_blkz;
3328 	if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
3329 		FDEV(devi).nr_blkz++;
3330 
3331 	FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
3332 					BITS_TO_LONGS(FDEV(devi).nr_blkz)
3333 					* sizeof(unsigned long),
3334 					GFP_KERNEL);
3335 	if (!FDEV(devi).blkz_seq)
3336 		return -ENOMEM;
3337 
3338 	/* Get block zones type and zone-capacity */
3339 	FDEV(devi).zone_capacity_blocks = f2fs_kzalloc(sbi,
3340 					FDEV(devi).nr_blkz * sizeof(block_t),
3341 					GFP_KERNEL);
3342 	if (!FDEV(devi).zone_capacity_blocks)
3343 		return -ENOMEM;
3344 
3345 	rep_zone_arg.dev = &FDEV(devi);
3346 	rep_zone_arg.zone_cap_mismatch = false;
3347 
3348 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3349 				  &rep_zone_arg);
3350 	if (ret < 0)
3351 		return ret;
3352 
3353 	if (!rep_zone_arg.zone_cap_mismatch) {
3354 		kfree(FDEV(devi).zone_capacity_blocks);
3355 		FDEV(devi).zone_capacity_blocks = NULL;
3356 	}
3357 
3358 	return 0;
3359 }
3360 #endif
3361 
3362 /*
3363  * Read f2fs raw super block.
3364  * Because we have two copies of super block, so read both of them
3365  * to get the first valid one. If any one of them is broken, we pass
3366  * them recovery flag back to the caller.
3367  */
3368 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3369 			struct f2fs_super_block **raw_super,
3370 			int *valid_super_block, int *recovery)
3371 {
3372 	struct super_block *sb = sbi->sb;
3373 	int block;
3374 	struct buffer_head *bh;
3375 	struct f2fs_super_block *super;
3376 	int err = 0;
3377 
3378 	super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3379 	if (!super)
3380 		return -ENOMEM;
3381 
3382 	for (block = 0; block < 2; block++) {
3383 		bh = sb_bread(sb, block);
3384 		if (!bh) {
3385 			f2fs_err(sbi, "Unable to read %dth superblock",
3386 				 block + 1);
3387 			err = -EIO;
3388 			*recovery = 1;
3389 			continue;
3390 		}
3391 
3392 		/* sanity checking of raw super */
3393 		err = sanity_check_raw_super(sbi, bh);
3394 		if (err) {
3395 			f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3396 				 block + 1);
3397 			brelse(bh);
3398 			*recovery = 1;
3399 			continue;
3400 		}
3401 
3402 		if (!*raw_super) {
3403 			memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3404 							sizeof(*super));
3405 			*valid_super_block = block;
3406 			*raw_super = super;
3407 		}
3408 		brelse(bh);
3409 	}
3410 
3411 	/* No valid superblock */
3412 	if (!*raw_super)
3413 		kfree(super);
3414 	else
3415 		err = 0;
3416 
3417 	return err;
3418 }
3419 
3420 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3421 {
3422 	struct buffer_head *bh;
3423 	__u32 crc = 0;
3424 	int err;
3425 
3426 	if ((recover && f2fs_readonly(sbi->sb)) ||
3427 				bdev_read_only(sbi->sb->s_bdev)) {
3428 		set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3429 		return -EROFS;
3430 	}
3431 
3432 	/* we should update superblock crc here */
3433 	if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
3434 		crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3435 				offsetof(struct f2fs_super_block, crc));
3436 		F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3437 	}
3438 
3439 	/* write back-up superblock first */
3440 	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
3441 	if (!bh)
3442 		return -EIO;
3443 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3444 	brelse(bh);
3445 
3446 	/* if we are in recovery path, skip writing valid superblock */
3447 	if (recover || err)
3448 		return err;
3449 
3450 	/* write current valid superblock */
3451 	bh = sb_bread(sbi->sb, sbi->valid_super_block);
3452 	if (!bh)
3453 		return -EIO;
3454 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3455 	brelse(bh);
3456 	return err;
3457 }
3458 
3459 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
3460 {
3461 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3462 	unsigned int max_devices = MAX_DEVICES;
3463 	int i;
3464 
3465 	/* Initialize single device information */
3466 	if (!RDEV(0).path[0]) {
3467 		if (!bdev_is_zoned(sbi->sb->s_bdev))
3468 			return 0;
3469 		max_devices = 1;
3470 	}
3471 
3472 	/*
3473 	 * Initialize multiple devices information, or single
3474 	 * zoned block device information.
3475 	 */
3476 	sbi->devs = f2fs_kzalloc(sbi,
3477 				 array_size(max_devices,
3478 					    sizeof(struct f2fs_dev_info)),
3479 				 GFP_KERNEL);
3480 	if (!sbi->devs)
3481 		return -ENOMEM;
3482 
3483 	for (i = 0; i < max_devices; i++) {
3484 
3485 		if (i > 0 && !RDEV(i).path[0])
3486 			break;
3487 
3488 		if (max_devices == 1) {
3489 			/* Single zoned block device mount */
3490 			FDEV(0).bdev =
3491 				blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
3492 					sbi->sb->s_mode, sbi->sb->s_type);
3493 		} else {
3494 			/* Multi-device mount */
3495 			memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
3496 			FDEV(i).total_segments =
3497 				le32_to_cpu(RDEV(i).total_segments);
3498 			if (i == 0) {
3499 				FDEV(i).start_blk = 0;
3500 				FDEV(i).end_blk = FDEV(i).start_blk +
3501 				    (FDEV(i).total_segments <<
3502 				    sbi->log_blocks_per_seg) - 1 +
3503 				    le32_to_cpu(raw_super->segment0_blkaddr);
3504 			} else {
3505 				FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
3506 				FDEV(i).end_blk = FDEV(i).start_blk +
3507 					(FDEV(i).total_segments <<
3508 					sbi->log_blocks_per_seg) - 1;
3509 			}
3510 			FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3511 					sbi->sb->s_mode, sbi->sb->s_type);
3512 		}
3513 		if (IS_ERR(FDEV(i).bdev))
3514 			return PTR_ERR(FDEV(i).bdev);
3515 
3516 		/* to release errored devices */
3517 		sbi->s_ndevs = i + 1;
3518 
3519 #ifdef CONFIG_BLK_DEV_ZONED
3520 		if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
3521 				!f2fs_sb_has_blkzoned(sbi)) {
3522 			f2fs_err(sbi, "Zoned block device feature not enabled\n");
3523 			return -EINVAL;
3524 		}
3525 		if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
3526 			if (init_blkz_info(sbi, i)) {
3527 				f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
3528 				return -EINVAL;
3529 			}
3530 			if (max_devices == 1)
3531 				break;
3532 			f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
3533 				  i, FDEV(i).path,
3534 				  FDEV(i).total_segments,
3535 				  FDEV(i).start_blk, FDEV(i).end_blk,
3536 				  bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
3537 				  "Host-aware" : "Host-managed");
3538 			continue;
3539 		}
3540 #endif
3541 		f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
3542 			  i, FDEV(i).path,
3543 			  FDEV(i).total_segments,
3544 			  FDEV(i).start_blk, FDEV(i).end_blk);
3545 	}
3546 	f2fs_info(sbi,
3547 		  "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
3548 	return 0;
3549 }
3550 
3551 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
3552 {
3553 #ifdef CONFIG_UNICODE
3554 	if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
3555 		const struct f2fs_sb_encodings *encoding_info;
3556 		struct unicode_map *encoding;
3557 		__u16 encoding_flags;
3558 
3559 		if (f2fs_sb_read_encoding(sbi->raw_super, &encoding_info,
3560 					  &encoding_flags)) {
3561 			f2fs_err(sbi,
3562 				 "Encoding requested by superblock is unknown");
3563 			return -EINVAL;
3564 		}
3565 
3566 		encoding = utf8_load(encoding_info->version);
3567 		if (IS_ERR(encoding)) {
3568 			f2fs_err(sbi,
3569 				 "can't mount with superblock charset: %s-%s "
3570 				 "not supported by the kernel. flags: 0x%x.",
3571 				 encoding_info->name, encoding_info->version,
3572 				 encoding_flags);
3573 			return PTR_ERR(encoding);
3574 		}
3575 		f2fs_info(sbi, "Using encoding defined by superblock: "
3576 			 "%s-%s with flags 0x%hx", encoding_info->name,
3577 			 encoding_info->version?:"\b", encoding_flags);
3578 
3579 		sbi->sb->s_encoding = encoding;
3580 		sbi->sb->s_encoding_flags = encoding_flags;
3581 	}
3582 #else
3583 	if (f2fs_sb_has_casefold(sbi)) {
3584 		f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
3585 		return -EINVAL;
3586 	}
3587 #endif
3588 	return 0;
3589 }
3590 
3591 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
3592 {
3593 	struct f2fs_sm_info *sm_i = SM_I(sbi);
3594 
3595 	/* adjust parameters according to the volume size */
3596 	if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
3597 		F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
3598 		sm_i->dcc_info->discard_granularity = 1;
3599 		sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
3600 	}
3601 
3602 	sbi->readdir_ra = 1;
3603 }
3604 
3605 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
3606 {
3607 	struct f2fs_sb_info *sbi;
3608 	struct f2fs_super_block *raw_super;
3609 	struct inode *root;
3610 	int err;
3611 	bool skip_recovery = false, need_fsck = false;
3612 	char *options = NULL;
3613 	int recovery, i, valid_super_block;
3614 	struct curseg_info *seg_i;
3615 	int retry_cnt = 1;
3616 
3617 try_onemore:
3618 	err = -EINVAL;
3619 	raw_super = NULL;
3620 	valid_super_block = -1;
3621 	recovery = 0;
3622 
3623 	/* allocate memory for f2fs-specific super block info */
3624 	sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
3625 	if (!sbi)
3626 		return -ENOMEM;
3627 
3628 	sbi->sb = sb;
3629 
3630 	/* Load the checksum driver */
3631 	sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
3632 	if (IS_ERR(sbi->s_chksum_driver)) {
3633 		f2fs_err(sbi, "Cannot load crc32 driver.");
3634 		err = PTR_ERR(sbi->s_chksum_driver);
3635 		sbi->s_chksum_driver = NULL;
3636 		goto free_sbi;
3637 	}
3638 
3639 	/* set a block size */
3640 	if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
3641 		f2fs_err(sbi, "unable to set blocksize");
3642 		goto free_sbi;
3643 	}
3644 
3645 	err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
3646 								&recovery);
3647 	if (err)
3648 		goto free_sbi;
3649 
3650 	sb->s_fs_info = sbi;
3651 	sbi->raw_super = raw_super;
3652 
3653 	/* precompute checksum seed for metadata */
3654 	if (f2fs_sb_has_inode_chksum(sbi))
3655 		sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
3656 						sizeof(raw_super->uuid));
3657 
3658 	default_options(sbi);
3659 	/* parse mount options */
3660 	options = kstrdup((const char *)data, GFP_KERNEL);
3661 	if (data && !options) {
3662 		err = -ENOMEM;
3663 		goto free_sb_buf;
3664 	}
3665 
3666 	err = parse_options(sb, options, false);
3667 	if (err)
3668 		goto free_options;
3669 
3670 	sb->s_maxbytes = max_file_blocks(NULL) <<
3671 				le32_to_cpu(raw_super->log_blocksize);
3672 	sb->s_max_links = F2FS_LINK_MAX;
3673 
3674 	err = f2fs_setup_casefold(sbi);
3675 	if (err)
3676 		goto free_options;
3677 
3678 #ifdef CONFIG_QUOTA
3679 	sb->dq_op = &f2fs_quota_operations;
3680 	sb->s_qcop = &f2fs_quotactl_ops;
3681 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
3682 
3683 	if (f2fs_sb_has_quota_ino(sbi)) {
3684 		for (i = 0; i < MAXQUOTAS; i++) {
3685 			if (f2fs_qf_ino(sbi->sb, i))
3686 				sbi->nquota_files++;
3687 		}
3688 	}
3689 #endif
3690 
3691 	sb->s_op = &f2fs_sops;
3692 #ifdef CONFIG_FS_ENCRYPTION
3693 	sb->s_cop = &f2fs_cryptops;
3694 #endif
3695 #ifdef CONFIG_FS_VERITY
3696 	sb->s_vop = &f2fs_verityops;
3697 #endif
3698 	sb->s_xattr = f2fs_xattr_handlers;
3699 	sb->s_export_op = &f2fs_export_ops;
3700 	sb->s_magic = F2FS_SUPER_MAGIC;
3701 	sb->s_time_gran = 1;
3702 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
3703 		(test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
3704 	memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
3705 	sb->s_iflags |= SB_I_CGROUPWB;
3706 
3707 	/* init f2fs-specific super block info */
3708 	sbi->valid_super_block = valid_super_block;
3709 	init_rwsem(&sbi->gc_lock);
3710 	mutex_init(&sbi->writepages);
3711 	init_rwsem(&sbi->cp_global_sem);
3712 	init_rwsem(&sbi->node_write);
3713 	init_rwsem(&sbi->node_change);
3714 
3715 	/* disallow all the data/node/meta page writes */
3716 	set_sbi_flag(sbi, SBI_POR_DOING);
3717 	spin_lock_init(&sbi->stat_lock);
3718 
3719 	/* init iostat info */
3720 	spin_lock_init(&sbi->iostat_lock);
3721 	sbi->iostat_enable = false;
3722 	sbi->iostat_period_ms = DEFAULT_IOSTAT_PERIOD_MS;
3723 
3724 	for (i = 0; i < NR_PAGE_TYPE; i++) {
3725 		int n = (i == META) ? 1: NR_TEMP_TYPE;
3726 		int j;
3727 
3728 		sbi->write_io[i] =
3729 			f2fs_kmalloc(sbi,
3730 				     array_size(n,
3731 						sizeof(struct f2fs_bio_info)),
3732 				     GFP_KERNEL);
3733 		if (!sbi->write_io[i]) {
3734 			err = -ENOMEM;
3735 			goto free_bio_info;
3736 		}
3737 
3738 		for (j = HOT; j < n; j++) {
3739 			init_rwsem(&sbi->write_io[i][j].io_rwsem);
3740 			sbi->write_io[i][j].sbi = sbi;
3741 			sbi->write_io[i][j].bio = NULL;
3742 			spin_lock_init(&sbi->write_io[i][j].io_lock);
3743 			INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
3744 			INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
3745 			init_rwsem(&sbi->write_io[i][j].bio_list_lock);
3746 		}
3747 	}
3748 
3749 	init_rwsem(&sbi->cp_rwsem);
3750 	init_rwsem(&sbi->quota_sem);
3751 	init_waitqueue_head(&sbi->cp_wait);
3752 	init_sb_info(sbi);
3753 
3754 	err = init_percpu_info(sbi);
3755 	if (err)
3756 		goto free_bio_info;
3757 
3758 	if (F2FS_IO_ALIGNED(sbi)) {
3759 		sbi->write_io_dummy =
3760 			mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
3761 		if (!sbi->write_io_dummy) {
3762 			err = -ENOMEM;
3763 			goto free_percpu;
3764 		}
3765 	}
3766 
3767 	/* init per sbi slab cache */
3768 	err = f2fs_init_xattr_caches(sbi);
3769 	if (err)
3770 		goto free_io_dummy;
3771 	err = f2fs_init_page_array_cache(sbi);
3772 	if (err)
3773 		goto free_xattr_cache;
3774 
3775 	/* get an inode for meta space */
3776 	sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
3777 	if (IS_ERR(sbi->meta_inode)) {
3778 		f2fs_err(sbi, "Failed to read F2FS meta data inode");
3779 		err = PTR_ERR(sbi->meta_inode);
3780 		goto free_page_array_cache;
3781 	}
3782 
3783 	err = f2fs_get_valid_checkpoint(sbi);
3784 	if (err) {
3785 		f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
3786 		goto free_meta_inode;
3787 	}
3788 
3789 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
3790 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3791 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
3792 		set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3793 		sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
3794 	}
3795 
3796 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
3797 		set_sbi_flag(sbi, SBI_NEED_FSCK);
3798 
3799 	/* Initialize device list */
3800 	err = f2fs_scan_devices(sbi);
3801 	if (err) {
3802 		f2fs_err(sbi, "Failed to find devices");
3803 		goto free_devices;
3804 	}
3805 
3806 	err = f2fs_init_post_read_wq(sbi);
3807 	if (err) {
3808 		f2fs_err(sbi, "Failed to initialize post read workqueue");
3809 		goto free_devices;
3810 	}
3811 
3812 	sbi->total_valid_node_count =
3813 				le32_to_cpu(sbi->ckpt->valid_node_count);
3814 	percpu_counter_set(&sbi->total_valid_inode_count,
3815 				le32_to_cpu(sbi->ckpt->valid_inode_count));
3816 	sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
3817 	sbi->total_valid_block_count =
3818 				le64_to_cpu(sbi->ckpt->valid_block_count);
3819 	sbi->last_valid_block_count = sbi->total_valid_block_count;
3820 	sbi->reserved_blocks = 0;
3821 	sbi->current_reserved_blocks = 0;
3822 	limit_reserve_root(sbi);
3823 	adjust_unusable_cap_perc(sbi);
3824 
3825 	for (i = 0; i < NR_INODE_TYPE; i++) {
3826 		INIT_LIST_HEAD(&sbi->inode_list[i]);
3827 		spin_lock_init(&sbi->inode_lock[i]);
3828 	}
3829 	mutex_init(&sbi->flush_lock);
3830 
3831 	f2fs_init_extent_cache_info(sbi);
3832 
3833 	f2fs_init_ino_entry_info(sbi);
3834 
3835 	f2fs_init_fsync_node_info(sbi);
3836 
3837 	/* setup checkpoint request control and start checkpoint issue thread */
3838 	f2fs_init_ckpt_req_control(sbi);
3839 	if (!test_opt(sbi, DISABLE_CHECKPOINT) &&
3840 			test_opt(sbi, MERGE_CHECKPOINT)) {
3841 		err = f2fs_start_ckpt_thread(sbi);
3842 		if (err) {
3843 			f2fs_err(sbi,
3844 			    "Failed to start F2FS issue_checkpoint_thread (%d)",
3845 			    err);
3846 			goto stop_ckpt_thread;
3847 		}
3848 	}
3849 
3850 	/* setup f2fs internal modules */
3851 	err = f2fs_build_segment_manager(sbi);
3852 	if (err) {
3853 		f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
3854 			 err);
3855 		goto free_sm;
3856 	}
3857 	err = f2fs_build_node_manager(sbi);
3858 	if (err) {
3859 		f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
3860 			 err);
3861 		goto free_nm;
3862 	}
3863 
3864 	/* For write statistics */
3865 	sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
3866 
3867 	/* Read accumulated write IO statistics if exists */
3868 	seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3869 	if (__exist_node_summaries(sbi))
3870 		sbi->kbytes_written =
3871 			le64_to_cpu(seg_i->journal->info.kbytes_written);
3872 
3873 	f2fs_build_gc_manager(sbi);
3874 
3875 	err = f2fs_build_stats(sbi);
3876 	if (err)
3877 		goto free_nm;
3878 
3879 	/* get an inode for node space */
3880 	sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3881 	if (IS_ERR(sbi->node_inode)) {
3882 		f2fs_err(sbi, "Failed to read node inode");
3883 		err = PTR_ERR(sbi->node_inode);
3884 		goto free_stats;
3885 	}
3886 
3887 	/* read root inode and dentry */
3888 	root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3889 	if (IS_ERR(root)) {
3890 		f2fs_err(sbi, "Failed to read root inode");
3891 		err = PTR_ERR(root);
3892 		goto free_node_inode;
3893 	}
3894 	if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3895 			!root->i_size || !root->i_nlink) {
3896 		iput(root);
3897 		err = -EINVAL;
3898 		goto free_node_inode;
3899 	}
3900 
3901 	sb->s_root = d_make_root(root); /* allocate root dentry */
3902 	if (!sb->s_root) {
3903 		err = -ENOMEM;
3904 		goto free_node_inode;
3905 	}
3906 
3907 	err = f2fs_register_sysfs(sbi);
3908 	if (err)
3909 		goto free_root_inode;
3910 
3911 #ifdef CONFIG_QUOTA
3912 	/* Enable quota usage during mount */
3913 	if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
3914 		err = f2fs_enable_quotas(sb);
3915 		if (err)
3916 			f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
3917 	}
3918 #endif
3919 	/* if there are any orphan inodes, free them */
3920 	err = f2fs_recover_orphan_inodes(sbi);
3921 	if (err)
3922 		goto free_meta;
3923 
3924 	if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
3925 		goto reset_checkpoint;
3926 
3927 	/* recover fsynced data */
3928 	if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
3929 			!test_opt(sbi, NORECOVERY)) {
3930 		/*
3931 		 * mount should be failed, when device has readonly mode, and
3932 		 * previous checkpoint was not done by clean system shutdown.
3933 		 */
3934 		if (f2fs_hw_is_readonly(sbi)) {
3935 			if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))
3936 				f2fs_err(sbi, "Need to recover fsync data, but write access unavailable");
3937 			else
3938 				f2fs_info(sbi, "write access unavailable, skipping recovery");
3939 			goto reset_checkpoint;
3940 		}
3941 
3942 		if (need_fsck)
3943 			set_sbi_flag(sbi, SBI_NEED_FSCK);
3944 
3945 		if (skip_recovery)
3946 			goto reset_checkpoint;
3947 
3948 		err = f2fs_recover_fsync_data(sbi, false);
3949 		if (err < 0) {
3950 			if (err != -ENOMEM)
3951 				skip_recovery = true;
3952 			need_fsck = true;
3953 			f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
3954 				 err);
3955 			goto free_meta;
3956 		}
3957 	} else {
3958 		err = f2fs_recover_fsync_data(sbi, true);
3959 
3960 		if (!f2fs_readonly(sb) && err > 0) {
3961 			err = -EINVAL;
3962 			f2fs_err(sbi, "Need to recover fsync data");
3963 			goto free_meta;
3964 		}
3965 	}
3966 
3967 	/*
3968 	 * If the f2fs is not readonly and fsync data recovery succeeds,
3969 	 * check zoned block devices' write pointer consistency.
3970 	 */
3971 	if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
3972 		err = f2fs_check_write_pointer(sbi);
3973 		if (err)
3974 			goto free_meta;
3975 	}
3976 
3977 reset_checkpoint:
3978 	f2fs_init_inmem_curseg(sbi);
3979 
3980 	/* f2fs_recover_fsync_data() cleared this already */
3981 	clear_sbi_flag(sbi, SBI_POR_DOING);
3982 
3983 	if (test_opt(sbi, DISABLE_CHECKPOINT)) {
3984 		err = f2fs_disable_checkpoint(sbi);
3985 		if (err)
3986 			goto sync_free_meta;
3987 	} else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
3988 		f2fs_enable_checkpoint(sbi);
3989 	}
3990 
3991 	/*
3992 	 * If filesystem is not mounted as read-only then
3993 	 * do start the gc_thread.
3994 	 */
3995 	if (F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF && !f2fs_readonly(sb)) {
3996 		/* After POR, we can run background GC thread.*/
3997 		err = f2fs_start_gc_thread(sbi);
3998 		if (err)
3999 			goto sync_free_meta;
4000 	}
4001 	kvfree(options);
4002 
4003 	/* recover broken superblock */
4004 	if (recovery) {
4005 		err = f2fs_commit_super(sbi, true);
4006 		f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4007 			  sbi->valid_super_block ? 1 : 2, err);
4008 	}
4009 
4010 	f2fs_join_shrinker(sbi);
4011 
4012 	f2fs_tuning_parameters(sbi);
4013 
4014 	f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4015 		    cur_cp_version(F2FS_CKPT(sbi)));
4016 	f2fs_update_time(sbi, CP_TIME);
4017 	f2fs_update_time(sbi, REQ_TIME);
4018 	clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4019 	return 0;
4020 
4021 sync_free_meta:
4022 	/* safe to flush all the data */
4023 	sync_filesystem(sbi->sb);
4024 	retry_cnt = 0;
4025 
4026 free_meta:
4027 #ifdef CONFIG_QUOTA
4028 	f2fs_truncate_quota_inode_pages(sb);
4029 	if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
4030 		f2fs_quota_off_umount(sbi->sb);
4031 #endif
4032 	/*
4033 	 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4034 	 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4035 	 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4036 	 * falls into an infinite loop in f2fs_sync_meta_pages().
4037 	 */
4038 	truncate_inode_pages_final(META_MAPPING(sbi));
4039 	/* evict some inodes being cached by GC */
4040 	evict_inodes(sb);
4041 	f2fs_unregister_sysfs(sbi);
4042 free_root_inode:
4043 	dput(sb->s_root);
4044 	sb->s_root = NULL;
4045 free_node_inode:
4046 	f2fs_release_ino_entry(sbi, true);
4047 	truncate_inode_pages_final(NODE_MAPPING(sbi));
4048 	iput(sbi->node_inode);
4049 	sbi->node_inode = NULL;
4050 free_stats:
4051 	f2fs_destroy_stats(sbi);
4052 free_nm:
4053 	f2fs_destroy_node_manager(sbi);
4054 free_sm:
4055 	f2fs_destroy_segment_manager(sbi);
4056 	f2fs_destroy_post_read_wq(sbi);
4057 stop_ckpt_thread:
4058 	f2fs_stop_ckpt_thread(sbi);
4059 free_devices:
4060 	destroy_device_list(sbi);
4061 	kvfree(sbi->ckpt);
4062 free_meta_inode:
4063 	make_bad_inode(sbi->meta_inode);
4064 	iput(sbi->meta_inode);
4065 	sbi->meta_inode = NULL;
4066 free_page_array_cache:
4067 	f2fs_destroy_page_array_cache(sbi);
4068 free_xattr_cache:
4069 	f2fs_destroy_xattr_caches(sbi);
4070 free_io_dummy:
4071 	mempool_destroy(sbi->write_io_dummy);
4072 free_percpu:
4073 	destroy_percpu_info(sbi);
4074 free_bio_info:
4075 	for (i = 0; i < NR_PAGE_TYPE; i++)
4076 		kvfree(sbi->write_io[i]);
4077 
4078 #ifdef CONFIG_UNICODE
4079 	utf8_unload(sb->s_encoding);
4080 	sb->s_encoding = NULL;
4081 #endif
4082 free_options:
4083 #ifdef CONFIG_QUOTA
4084 	for (i = 0; i < MAXQUOTAS; i++)
4085 		kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4086 #endif
4087 	fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
4088 	kvfree(options);
4089 free_sb_buf:
4090 	kfree(raw_super);
4091 free_sbi:
4092 	if (sbi->s_chksum_driver)
4093 		crypto_free_shash(sbi->s_chksum_driver);
4094 	kfree(sbi);
4095 
4096 	/* give only one another chance */
4097 	if (retry_cnt > 0 && skip_recovery) {
4098 		retry_cnt--;
4099 		shrink_dcache_sb(sb);
4100 		goto try_onemore;
4101 	}
4102 	return err;
4103 }
4104 
4105 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
4106 			const char *dev_name, void *data)
4107 {
4108 	return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
4109 }
4110 
4111 static void kill_f2fs_super(struct super_block *sb)
4112 {
4113 	if (sb->s_root) {
4114 		struct f2fs_sb_info *sbi = F2FS_SB(sb);
4115 
4116 		set_sbi_flag(sbi, SBI_IS_CLOSE);
4117 		f2fs_stop_gc_thread(sbi);
4118 		f2fs_stop_discard_thread(sbi);
4119 
4120 		if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
4121 				!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4122 			struct cp_control cpc = {
4123 				.reason = CP_UMOUNT,
4124 			};
4125 			f2fs_write_checkpoint(sbi, &cpc);
4126 		}
4127 
4128 		if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
4129 			sb->s_flags &= ~SB_RDONLY;
4130 	}
4131 	kill_block_super(sb);
4132 }
4133 
4134 static struct file_system_type f2fs_fs_type = {
4135 	.owner		= THIS_MODULE,
4136 	.name		= "f2fs",
4137 	.mount		= f2fs_mount,
4138 	.kill_sb	= kill_f2fs_super,
4139 	.fs_flags	= FS_REQUIRES_DEV,
4140 };
4141 MODULE_ALIAS_FS("f2fs");
4142 
4143 static int __init init_inodecache(void)
4144 {
4145 	f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
4146 			sizeof(struct f2fs_inode_info), 0,
4147 			SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
4148 	if (!f2fs_inode_cachep)
4149 		return -ENOMEM;
4150 	return 0;
4151 }
4152 
4153 static void destroy_inodecache(void)
4154 {
4155 	/*
4156 	 * Make sure all delayed rcu free inodes are flushed before we
4157 	 * destroy cache.
4158 	 */
4159 	rcu_barrier();
4160 	kmem_cache_destroy(f2fs_inode_cachep);
4161 }
4162 
4163 static int __init init_f2fs_fs(void)
4164 {
4165 	int err;
4166 
4167 	if (PAGE_SIZE != F2FS_BLKSIZE) {
4168 		printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
4169 				PAGE_SIZE, F2FS_BLKSIZE);
4170 		return -EINVAL;
4171 	}
4172 
4173 	err = init_inodecache();
4174 	if (err)
4175 		goto fail;
4176 	err = f2fs_create_node_manager_caches();
4177 	if (err)
4178 		goto free_inodecache;
4179 	err = f2fs_create_segment_manager_caches();
4180 	if (err)
4181 		goto free_node_manager_caches;
4182 	err = f2fs_create_checkpoint_caches();
4183 	if (err)
4184 		goto free_segment_manager_caches;
4185 	err = f2fs_create_extent_cache();
4186 	if (err)
4187 		goto free_checkpoint_caches;
4188 	err = f2fs_create_garbage_collection_cache();
4189 	if (err)
4190 		goto free_extent_cache;
4191 	err = f2fs_init_sysfs();
4192 	if (err)
4193 		goto free_garbage_collection_cache;
4194 	err = register_shrinker(&f2fs_shrinker_info);
4195 	if (err)
4196 		goto free_sysfs;
4197 	err = register_filesystem(&f2fs_fs_type);
4198 	if (err)
4199 		goto free_shrinker;
4200 	f2fs_create_root_stats();
4201 	err = f2fs_init_post_read_processing();
4202 	if (err)
4203 		goto free_root_stats;
4204 	err = f2fs_init_bio_entry_cache();
4205 	if (err)
4206 		goto free_post_read;
4207 	err = f2fs_init_bioset();
4208 	if (err)
4209 		goto free_bio_enrty_cache;
4210 	err = f2fs_init_compress_mempool();
4211 	if (err)
4212 		goto free_bioset;
4213 	err = f2fs_init_compress_cache();
4214 	if (err)
4215 		goto free_compress_mempool;
4216 	return 0;
4217 free_compress_mempool:
4218 	f2fs_destroy_compress_mempool();
4219 free_bioset:
4220 	f2fs_destroy_bioset();
4221 free_bio_enrty_cache:
4222 	f2fs_destroy_bio_entry_cache();
4223 free_post_read:
4224 	f2fs_destroy_post_read_processing();
4225 free_root_stats:
4226 	f2fs_destroy_root_stats();
4227 	unregister_filesystem(&f2fs_fs_type);
4228 free_shrinker:
4229 	unregister_shrinker(&f2fs_shrinker_info);
4230 free_sysfs:
4231 	f2fs_exit_sysfs();
4232 free_garbage_collection_cache:
4233 	f2fs_destroy_garbage_collection_cache();
4234 free_extent_cache:
4235 	f2fs_destroy_extent_cache();
4236 free_checkpoint_caches:
4237 	f2fs_destroy_checkpoint_caches();
4238 free_segment_manager_caches:
4239 	f2fs_destroy_segment_manager_caches();
4240 free_node_manager_caches:
4241 	f2fs_destroy_node_manager_caches();
4242 free_inodecache:
4243 	destroy_inodecache();
4244 fail:
4245 	return err;
4246 }
4247 
4248 static void __exit exit_f2fs_fs(void)
4249 {
4250 	f2fs_destroy_compress_cache();
4251 	f2fs_destroy_compress_mempool();
4252 	f2fs_destroy_bioset();
4253 	f2fs_destroy_bio_entry_cache();
4254 	f2fs_destroy_post_read_processing();
4255 	f2fs_destroy_root_stats();
4256 	unregister_filesystem(&f2fs_fs_type);
4257 	unregister_shrinker(&f2fs_shrinker_info);
4258 	f2fs_exit_sysfs();
4259 	f2fs_destroy_garbage_collection_cache();
4260 	f2fs_destroy_extent_cache();
4261 	f2fs_destroy_checkpoint_caches();
4262 	f2fs_destroy_segment_manager_caches();
4263 	f2fs_destroy_node_manager_caches();
4264 	destroy_inodecache();
4265 }
4266 
4267 module_init(init_f2fs_fs)
4268 module_exit(exit_f2fs_fs)
4269 
4270 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
4271 MODULE_DESCRIPTION("Flash Friendly File System");
4272 MODULE_LICENSE("GPL");
4273 
4274