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