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