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