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