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