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