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