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