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