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