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