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