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