1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
8 #include <linux/module.h>
9 #include <linux/init.h>
11 #include <linux/fs_context.h>
12 #include <linux/sched/mm.h>
13 #include <linux/statfs.h>
14 #include <linux/kthread.h>
15 #include <linux/parser.h>
16 #include <linux/mount.h>
17 #include <linux/seq_file.h>
18 #include <linux/proc_fs.h>
19 #include <linux/random.h>
20 #include <linux/exportfs.h>
21 #include <linux/blkdev.h>
22 #include <linux/quotaops.h>
23 #include <linux/f2fs_fs.h>
24 #include <linux/sysfs.h>
25 #include <linux/quota.h>
26 #include <linux/unicode.h>
27 #include <linux/part_stat.h>
28 #include <linux/zstd.h>
29 #include <linux/lz4.h>
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/f2fs.h>
41 static struct kmem_cache *f2fs_inode_cachep;
43 #ifdef CONFIG_F2FS_FAULT_INJECTION
45 const char *f2fs_fault_name[FAULT_MAX] = {
46 [FAULT_KMALLOC] = "kmalloc",
47 [FAULT_KVMALLOC] = "kvmalloc",
48 [FAULT_PAGE_ALLOC] = "page alloc",
49 [FAULT_PAGE_GET] = "page get",
50 [FAULT_ALLOC_BIO] = "alloc bio(obsolete)",
51 [FAULT_ALLOC_NID] = "alloc nid",
52 [FAULT_ORPHAN] = "orphan",
53 [FAULT_BLOCK] = "no more block",
54 [FAULT_DIR_DEPTH] = "too big dir depth",
55 [FAULT_EVICT_INODE] = "evict_inode fail",
56 [FAULT_TRUNCATE] = "truncate fail",
57 [FAULT_READ_IO] = "read IO error",
58 [FAULT_CHECKPOINT] = "checkpoint error",
59 [FAULT_DISCARD] = "discard error",
60 [FAULT_WRITE_IO] = "write IO error",
61 [FAULT_SLAB_ALLOC] = "slab alloc",
62 [FAULT_DQUOT_INIT] = "dquot initialize",
63 [FAULT_LOCK_OP] = "lock_op",
64 [FAULT_BLKADDR_VALIDITY] = "invalid blkaddr",
65 [FAULT_BLKADDR_CONSISTENCE] = "inconsistent blkaddr",
66 [FAULT_NO_SEGMENT] = "no free segment",
67 [FAULT_INCONSISTENT_FOOTER] = "inconsistent footer",
68 [FAULT_TIMEOUT] = "timeout",
69 [FAULT_VMALLOC] = "vmalloc",
72 int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
73 unsigned long type, enum fault_option fo)
75 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
78 memset(ffi, 0, sizeof(struct f2fs_fault_info));
82 if (fo & FAULT_RATE) {
85 atomic_set(&ffi->inject_ops, 0);
86 ffi->inject_rate = (int)rate;
87 f2fs_info(sbi, "build fault injection rate: %lu", rate);
90 if (fo & FAULT_TYPE) {
91 if (type >= BIT(FAULT_MAX))
93 ffi->inject_type = (unsigned int)type;
94 f2fs_info(sbi, "build fault injection type: 0x%lx", type);
101 /* f2fs-wide shrinker description */
102 static struct shrinker *f2fs_shrinker_info;
104 static int __init f2fs_init_shrinker(void)
106 f2fs_shrinker_info = shrinker_alloc(0, "f2fs-shrinker");
107 if (!f2fs_shrinker_info)
110 f2fs_shrinker_info->count_objects = f2fs_shrink_count;
111 f2fs_shrinker_info->scan_objects = f2fs_shrink_scan;
113 shrinker_register(f2fs_shrinker_info);
118 static void f2fs_exit_shrinker(void)
120 shrinker_free(f2fs_shrinker_info);
125 Opt_disable_roll_forward,
136 Opt_disable_ext_identify,
139 Opt_inline_xattr_size,
176 Opt_test_dummy_encryption,
178 Opt_checkpoint_disable,
179 Opt_checkpoint_disable_cap,
180 Opt_checkpoint_disable_cap_perc,
181 Opt_checkpoint_enable,
182 Opt_checkpoint_merge,
183 Opt_nocheckpoint_merge,
184 Opt_compress_algorithm,
185 Opt_compress_log_size,
186 Opt_compress_extension,
187 Opt_nocompress_extension,
196 Opt_age_extent_cache,
202 static match_table_t f2fs_tokens = {
203 {Opt_gc_background, "background_gc=%s"},
204 {Opt_disable_roll_forward, "disable_roll_forward"},
205 {Opt_norecovery, "norecovery"},
206 {Opt_discard, "discard"},
207 {Opt_nodiscard, "nodiscard"},
208 {Opt_noheap, "no_heap"},
210 {Opt_user_xattr, "user_xattr"},
211 {Opt_nouser_xattr, "nouser_xattr"},
213 {Opt_noacl, "noacl"},
214 {Opt_active_logs, "active_logs=%u"},
215 {Opt_disable_ext_identify, "disable_ext_identify"},
216 {Opt_inline_xattr, "inline_xattr"},
217 {Opt_noinline_xattr, "noinline_xattr"},
218 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
219 {Opt_inline_data, "inline_data"},
220 {Opt_inline_dentry, "inline_dentry"},
221 {Opt_noinline_dentry, "noinline_dentry"},
222 {Opt_flush_merge, "flush_merge"},
223 {Opt_noflush_merge, "noflush_merge"},
224 {Opt_barrier, "barrier"},
225 {Opt_nobarrier, "nobarrier"},
226 {Opt_fastboot, "fastboot"},
227 {Opt_extent_cache, "extent_cache"},
228 {Opt_noextent_cache, "noextent_cache"},
229 {Opt_noinline_data, "noinline_data"},
230 {Opt_data_flush, "data_flush"},
231 {Opt_reserve_root, "reserve_root=%u"},
232 {Opt_resgid, "resgid=%u"},
233 {Opt_resuid, "resuid=%u"},
234 {Opt_mode, "mode=%s"},
235 {Opt_fault_injection, "fault_injection=%u"},
236 {Opt_fault_type, "fault_type=%u"},
237 {Opt_lazytime, "lazytime"},
238 {Opt_nolazytime, "nolazytime"},
239 {Opt_quota, "quota"},
240 {Opt_noquota, "noquota"},
241 {Opt_usrquota, "usrquota"},
242 {Opt_grpquota, "grpquota"},
243 {Opt_prjquota, "prjquota"},
244 {Opt_usrjquota, "usrjquota=%s"},
245 {Opt_grpjquota, "grpjquota=%s"},
246 {Opt_prjjquota, "prjjquota=%s"},
247 {Opt_offusrjquota, "usrjquota="},
248 {Opt_offgrpjquota, "grpjquota="},
249 {Opt_offprjjquota, "prjjquota="},
250 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
251 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
252 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
253 {Opt_alloc, "alloc_mode=%s"},
254 {Opt_fsync, "fsync_mode=%s"},
255 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
256 {Opt_test_dummy_encryption, "test_dummy_encryption"},
257 {Opt_inlinecrypt, "inlinecrypt"},
258 {Opt_checkpoint_disable, "checkpoint=disable"},
259 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
260 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
261 {Opt_checkpoint_enable, "checkpoint=enable"},
262 {Opt_checkpoint_merge, "checkpoint_merge"},
263 {Opt_nocheckpoint_merge, "nocheckpoint_merge"},
264 {Opt_compress_algorithm, "compress_algorithm=%s"},
265 {Opt_compress_log_size, "compress_log_size=%u"},
266 {Opt_compress_extension, "compress_extension=%s"},
267 {Opt_nocompress_extension, "nocompress_extension=%s"},
268 {Opt_compress_chksum, "compress_chksum"},
269 {Opt_compress_mode, "compress_mode=%s"},
270 {Opt_compress_cache, "compress_cache"},
272 {Opt_gc_merge, "gc_merge"},
273 {Opt_nogc_merge, "nogc_merge"},
274 {Opt_discard_unit, "discard_unit=%s"},
275 {Opt_memory_mode, "memory=%s"},
276 {Opt_age_extent_cache, "age_extent_cache"},
277 {Opt_errors, "errors=%s"},
278 {Opt_nat_bits, "nat_bits"},
282 void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate,
283 const char *fmt, ...)
285 struct va_format vaf;
291 level = printk_get_level(fmt);
292 vaf.fmt = printk_skip_level(fmt);
295 printk_ratelimited("%c%cF2FS-fs (%s): %pV\n",
296 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
298 printk("%c%cF2FS-fs (%s): %pV\n",
299 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
304 #if IS_ENABLED(CONFIG_UNICODE)
305 static const struct f2fs_sb_encodings {
308 unsigned int version;
309 } f2fs_sb_encoding_map[] = {
310 {F2FS_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
313 static const struct f2fs_sb_encodings *
314 f2fs_sb_read_encoding(const struct f2fs_super_block *sb)
316 __u16 magic = le16_to_cpu(sb->s_encoding);
319 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
320 if (magic == f2fs_sb_encoding_map[i].magic)
321 return &f2fs_sb_encoding_map[i];
326 struct kmem_cache *f2fs_cf_name_slab;
327 static int __init f2fs_create_casefold_cache(void)
329 f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
331 return f2fs_cf_name_slab ? 0 : -ENOMEM;
334 static void f2fs_destroy_casefold_cache(void)
336 kmem_cache_destroy(f2fs_cf_name_slab);
339 static int __init f2fs_create_casefold_cache(void) { return 0; }
340 static void f2fs_destroy_casefold_cache(void) { }
343 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
345 block_t limit = min((sbi->user_block_count >> 3),
346 sbi->user_block_count - sbi->reserved_blocks);
349 if (test_opt(sbi, RESERVE_ROOT) &&
350 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
351 F2FS_OPTION(sbi).root_reserved_blocks = limit;
352 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
353 F2FS_OPTION(sbi).root_reserved_blocks);
355 if (!test_opt(sbi, RESERVE_ROOT) &&
356 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
357 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
358 !gid_eq(F2FS_OPTION(sbi).s_resgid,
359 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
360 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
361 from_kuid_munged(&init_user_ns,
362 F2FS_OPTION(sbi).s_resuid),
363 from_kgid_munged(&init_user_ns,
364 F2FS_OPTION(sbi).s_resgid));
367 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
369 if (!F2FS_OPTION(sbi).unusable_cap_perc)
372 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
373 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
375 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
376 F2FS_OPTION(sbi).unusable_cap_perc;
378 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
379 F2FS_OPTION(sbi).unusable_cap,
380 F2FS_OPTION(sbi).unusable_cap_perc);
383 static void init_once(void *foo)
385 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
387 inode_init_once(&fi->vfs_inode);
391 static const char * const quotatypes[] = INITQFNAMES;
392 #define QTYPE2NAME(t) (quotatypes[t])
393 static int f2fs_set_qf_name(struct f2fs_sb_info *sbi, int qtype,
396 struct super_block *sb = sbi->sb;
400 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
401 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
404 if (f2fs_sb_has_quota_ino(sbi)) {
405 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
409 qname = match_strdup(args);
411 f2fs_err(sbi, "Not enough memory for storing quotafile name");
414 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
415 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
418 f2fs_err(sbi, "%s quota file already specified",
422 if (strchr(qname, '/')) {
423 f2fs_err(sbi, "quotafile must be on filesystem root");
426 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
434 static int f2fs_clear_qf_name(struct f2fs_sb_info *sbi, int qtype)
436 struct super_block *sb = sbi->sb;
438 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
439 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
442 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
443 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
447 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
450 * We do the test below only for project quotas. 'usrquota' and
451 * 'grpquota' mount options are allowed even without quota feature
452 * to support legacy quotas in quota files.
454 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
455 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
458 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
459 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
460 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
461 if (test_opt(sbi, USRQUOTA) &&
462 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
463 clear_opt(sbi, USRQUOTA);
465 if (test_opt(sbi, GRPQUOTA) &&
466 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
467 clear_opt(sbi, GRPQUOTA);
469 if (test_opt(sbi, PRJQUOTA) &&
470 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
471 clear_opt(sbi, PRJQUOTA);
473 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
474 test_opt(sbi, PRJQUOTA)) {
475 f2fs_err(sbi, "old and new quota format mixing");
479 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
480 f2fs_err(sbi, "journaled quota format not specified");
485 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
486 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
487 F2FS_OPTION(sbi).s_jquota_fmt = 0;
493 static int f2fs_set_test_dummy_encryption(struct f2fs_sb_info *sbi,
495 const substring_t *arg,
498 struct fs_parameter param = {
499 .type = fs_value_is_string,
500 .string = arg->from ? arg->from : "",
502 struct fscrypt_dummy_policy *policy =
503 &F2FS_OPTION(sbi).dummy_enc_policy;
506 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
507 f2fs_warn(sbi, "test_dummy_encryption option not supported");
511 if (!f2fs_sb_has_encrypt(sbi)) {
512 f2fs_err(sbi, "Encrypt feature is off");
517 * This mount option is just for testing, and it's not worthwhile to
518 * implement the extra complexity (e.g. RCU protection) that would be
519 * needed to allow it to be set or changed during remount. We do allow
520 * it to be specified during remount, but only if there is no change.
522 if (is_remount && !fscrypt_is_dummy_policy_set(policy)) {
523 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
527 err = fscrypt_parse_test_dummy_encryption(¶m, policy);
531 "Can't change test_dummy_encryption on remount");
532 else if (err == -EINVAL)
533 f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
536 f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
540 f2fs_warn(sbi, "Test dummy encryption mode enabled");
544 #ifdef CONFIG_F2FS_FS_COMPRESSION
545 static bool is_compress_extension_exist(struct f2fs_sb_info *sbi,
546 const char *new_ext, bool is_ext)
548 unsigned char (*ext)[F2FS_EXTENSION_LEN];
553 ext = F2FS_OPTION(sbi).extensions;
554 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
556 ext = F2FS_OPTION(sbi).noextensions;
557 ext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
560 for (i = 0; i < ext_cnt; i++) {
561 if (!strcasecmp(new_ext, ext[i]))
569 * 1. The same extension name cannot not appear in both compress and non-compress extension
571 * 2. If the compress extension specifies all files, the types specified by the non-compress
572 * extension will be treated as special cases and will not be compressed.
573 * 3. Don't allow the non-compress extension specifies all files.
575 static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
577 unsigned char (*ext)[F2FS_EXTENSION_LEN];
578 unsigned char (*noext)[F2FS_EXTENSION_LEN];
579 int ext_cnt, noext_cnt, index = 0, no_index = 0;
581 ext = F2FS_OPTION(sbi).extensions;
582 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
583 noext = F2FS_OPTION(sbi).noextensions;
584 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
589 for (no_index = 0; no_index < noext_cnt; no_index++) {
590 if (!strcasecmp("*", noext[no_index])) {
591 f2fs_info(sbi, "Don't allow the nocompress extension specifies all files");
594 for (index = 0; index < ext_cnt; index++) {
595 if (!strcasecmp(ext[index], noext[no_index])) {
596 f2fs_info(sbi, "Don't allow the same extension %s appear in both compress and nocompress extension",
605 #ifdef CONFIG_F2FS_FS_LZ4
606 static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
608 #ifdef CONFIG_F2FS_FS_LZ4HC
611 if (strlen(str) == 3) {
612 F2FS_OPTION(sbi).compress_level = 0;
619 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
622 if (kstrtouint(str + 1, 10, &level))
625 if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
626 f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
630 F2FS_OPTION(sbi).compress_level = level;
633 if (strlen(str) == 3) {
634 F2FS_OPTION(sbi).compress_level = 0;
637 f2fs_info(sbi, "kernel doesn't support lz4hc compression");
643 #ifdef CONFIG_F2FS_FS_ZSTD
644 static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
649 if (strlen(str) == len) {
650 F2FS_OPTION(sbi).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
657 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
660 if (kstrtoint(str + 1, 10, &level))
663 /* f2fs does not support negative compress level now */
665 f2fs_info(sbi, "do not support negative compress level: %d", level);
669 if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
670 f2fs_info(sbi, "invalid zstd compress level: %d", level);
674 F2FS_OPTION(sbi).compress_level = level;
680 static int parse_options(struct f2fs_sb_info *sbi, char *options, bool is_remount)
682 substring_t args[MAX_OPT_ARGS];
683 #ifdef CONFIG_F2FS_FS_COMPRESSION
684 unsigned char (*ext)[F2FS_EXTENSION_LEN];
685 unsigned char (*noext)[F2FS_EXTENSION_LEN];
686 int ext_cnt, noext_cnt;
697 while ((p = strsep(&options, ",")) != NULL) {
703 * Initialize args struct so we know whether arg was
704 * found; some options take optional arguments.
706 args[0].to = args[0].from = NULL;
707 token = match_token(p, f2fs_tokens, args);
710 case Opt_gc_background:
711 name = match_strdup(&args[0]);
715 if (!strcmp(name, "on")) {
716 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
717 } else if (!strcmp(name, "off")) {
718 if (f2fs_sb_has_blkzoned(sbi)) {
719 f2fs_warn(sbi, "zoned devices need bggc");
723 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
724 } else if (!strcmp(name, "sync")) {
725 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
732 case Opt_disable_roll_forward:
733 set_opt(sbi, DISABLE_ROLL_FORWARD);
736 /* requires ro mount, checked in f2fs_default_check */
737 set_opt(sbi, NORECOVERY);
740 if (!f2fs_hw_support_discard(sbi)) {
741 f2fs_warn(sbi, "device does not support discard");
744 set_opt(sbi, DISCARD);
747 if (f2fs_hw_should_discard(sbi)) {
748 f2fs_warn(sbi, "discard is required for zoned block devices");
751 clear_opt(sbi, DISCARD);
755 f2fs_warn(sbi, "heap/no_heap options were deprecated");
757 #ifdef CONFIG_F2FS_FS_XATTR
759 set_opt(sbi, XATTR_USER);
761 case Opt_nouser_xattr:
762 clear_opt(sbi, XATTR_USER);
764 case Opt_inline_xattr:
765 set_opt(sbi, INLINE_XATTR);
767 case Opt_noinline_xattr:
768 clear_opt(sbi, INLINE_XATTR);
770 case Opt_inline_xattr_size:
771 if (args->from && match_int(args, &arg))
773 set_opt(sbi, INLINE_XATTR_SIZE);
774 F2FS_OPTION(sbi).inline_xattr_size = arg;
778 case Opt_nouser_xattr:
779 case Opt_inline_xattr:
780 case Opt_noinline_xattr:
781 case Opt_inline_xattr_size:
782 f2fs_info(sbi, "xattr options not supported");
785 #ifdef CONFIG_F2FS_FS_POSIX_ACL
787 set_opt(sbi, POSIX_ACL);
790 clear_opt(sbi, POSIX_ACL);
795 f2fs_info(sbi, "acl options not supported");
798 case Opt_active_logs:
799 if (args->from && match_int(args, &arg))
801 if (arg != 2 && arg != 4 &&
802 arg != NR_CURSEG_PERSIST_TYPE)
804 F2FS_OPTION(sbi).active_logs = arg;
806 case Opt_disable_ext_identify:
807 set_opt(sbi, DISABLE_EXT_IDENTIFY);
809 case Opt_inline_data:
810 set_opt(sbi, INLINE_DATA);
812 case Opt_inline_dentry:
813 set_opt(sbi, INLINE_DENTRY);
815 case Opt_noinline_dentry:
816 clear_opt(sbi, INLINE_DENTRY);
818 case Opt_flush_merge:
819 set_opt(sbi, FLUSH_MERGE);
821 case Opt_noflush_merge:
822 clear_opt(sbi, FLUSH_MERGE);
825 set_opt(sbi, NOBARRIER);
828 clear_opt(sbi, NOBARRIER);
831 set_opt(sbi, FASTBOOT);
833 case Opt_extent_cache:
834 set_opt(sbi, READ_EXTENT_CACHE);
836 case Opt_noextent_cache:
837 if (f2fs_sb_has_device_alias(sbi)) {
838 f2fs_err(sbi, "device aliasing requires extent cache");
841 clear_opt(sbi, READ_EXTENT_CACHE);
843 case Opt_noinline_data:
844 clear_opt(sbi, INLINE_DATA);
847 set_opt(sbi, DATA_FLUSH);
849 case Opt_reserve_root:
850 if (args->from && match_int(args, &arg))
852 if (test_opt(sbi, RESERVE_ROOT)) {
853 f2fs_info(sbi, "Preserve previous reserve_root=%u",
854 F2FS_OPTION(sbi).root_reserved_blocks);
856 F2FS_OPTION(sbi).root_reserved_blocks = arg;
857 set_opt(sbi, RESERVE_ROOT);
861 if (args->from && match_int(args, &arg))
863 uid = make_kuid(current_user_ns(), arg);
864 if (!uid_valid(uid)) {
865 f2fs_err(sbi, "Invalid uid value %d", arg);
868 F2FS_OPTION(sbi).s_resuid = uid;
871 if (args->from && match_int(args, &arg))
873 gid = make_kgid(current_user_ns(), arg);
874 if (!gid_valid(gid)) {
875 f2fs_err(sbi, "Invalid gid value %d", arg);
878 F2FS_OPTION(sbi).s_resgid = gid;
881 name = match_strdup(&args[0]);
885 if (!strcmp(name, "adaptive")) {
886 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
887 } else if (!strcmp(name, "lfs")) {
888 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
889 } else if (!strcmp(name, "fragment:segment")) {
890 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_SEG;
891 } else if (!strcmp(name, "fragment:block")) {
892 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_BLK;
899 #ifdef CONFIG_F2FS_FAULT_INJECTION
900 case Opt_fault_injection:
901 if (args->from && match_int(args, &arg))
903 if (f2fs_build_fault_attr(sbi, arg, 0, FAULT_RATE))
905 set_opt(sbi, FAULT_INJECTION);
909 if (args->from && match_int(args, &arg))
911 if (f2fs_build_fault_attr(sbi, 0, arg, FAULT_TYPE))
913 set_opt(sbi, FAULT_INJECTION);
916 case Opt_fault_injection:
918 f2fs_info(sbi, "fault injection options not supported");
922 set_opt(sbi, LAZYTIME);
925 clear_opt(sbi, LAZYTIME);
930 set_opt(sbi, USRQUOTA);
933 set_opt(sbi, GRPQUOTA);
936 set_opt(sbi, PRJQUOTA);
939 ret = f2fs_set_qf_name(sbi, USRQUOTA, &args[0]);
944 ret = f2fs_set_qf_name(sbi, GRPQUOTA, &args[0]);
949 ret = f2fs_set_qf_name(sbi, PRJQUOTA, &args[0]);
953 case Opt_offusrjquota:
954 ret = f2fs_clear_qf_name(sbi, USRQUOTA);
958 case Opt_offgrpjquota:
959 ret = f2fs_clear_qf_name(sbi, GRPQUOTA);
963 case Opt_offprjjquota:
964 ret = f2fs_clear_qf_name(sbi, PRJQUOTA);
968 case Opt_jqfmt_vfsold:
969 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
971 case Opt_jqfmt_vfsv0:
972 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
974 case Opt_jqfmt_vfsv1:
975 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
978 clear_opt(sbi, QUOTA);
979 clear_opt(sbi, USRQUOTA);
980 clear_opt(sbi, GRPQUOTA);
981 clear_opt(sbi, PRJQUOTA);
991 case Opt_offusrjquota:
992 case Opt_offgrpjquota:
993 case Opt_offprjjquota:
994 case Opt_jqfmt_vfsold:
995 case Opt_jqfmt_vfsv0:
996 case Opt_jqfmt_vfsv1:
998 f2fs_info(sbi, "quota operations not supported");
1002 name = match_strdup(&args[0]);
1006 if (!strcmp(name, "default")) {
1007 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1008 } else if (!strcmp(name, "reuse")) {
1009 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
1017 name = match_strdup(&args[0]);
1020 if (!strcmp(name, "posix")) {
1021 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1022 } else if (!strcmp(name, "strict")) {
1023 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
1024 } else if (!strcmp(name, "nobarrier")) {
1025 F2FS_OPTION(sbi).fsync_mode =
1026 FSYNC_MODE_NOBARRIER;
1033 case Opt_test_dummy_encryption:
1034 ret = f2fs_set_test_dummy_encryption(sbi, p, &args[0],
1039 case Opt_inlinecrypt:
1040 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1041 set_opt(sbi, INLINECRYPT);
1043 f2fs_info(sbi, "inline encryption not supported");
1046 case Opt_checkpoint_disable_cap_perc:
1047 if (args->from && match_int(args, &arg))
1049 if (arg < 0 || arg > 100)
1051 F2FS_OPTION(sbi).unusable_cap_perc = arg;
1052 set_opt(sbi, DISABLE_CHECKPOINT);
1054 case Opt_checkpoint_disable_cap:
1055 if (args->from && match_int(args, &arg))
1057 F2FS_OPTION(sbi).unusable_cap = arg;
1058 set_opt(sbi, DISABLE_CHECKPOINT);
1060 case Opt_checkpoint_disable:
1061 set_opt(sbi, DISABLE_CHECKPOINT);
1063 case Opt_checkpoint_enable:
1064 clear_opt(sbi, DISABLE_CHECKPOINT);
1066 case Opt_checkpoint_merge:
1067 set_opt(sbi, MERGE_CHECKPOINT);
1069 case Opt_nocheckpoint_merge:
1070 clear_opt(sbi, MERGE_CHECKPOINT);
1072 #ifdef CONFIG_F2FS_FS_COMPRESSION
1073 case Opt_compress_algorithm:
1074 if (!f2fs_sb_has_compression(sbi)) {
1075 f2fs_info(sbi, "Image doesn't support compression");
1078 name = match_strdup(&args[0]);
1081 if (!strcmp(name, "lzo")) {
1082 #ifdef CONFIG_F2FS_FS_LZO
1083 F2FS_OPTION(sbi).compress_level = 0;
1084 F2FS_OPTION(sbi).compress_algorithm =
1087 f2fs_info(sbi, "kernel doesn't support lzo compression");
1089 } else if (!strncmp(name, "lz4", 3)) {
1090 #ifdef CONFIG_F2FS_FS_LZ4
1091 ret = f2fs_set_lz4hc_level(sbi, name);
1096 F2FS_OPTION(sbi).compress_algorithm =
1099 f2fs_info(sbi, "kernel doesn't support lz4 compression");
1101 } else if (!strncmp(name, "zstd", 4)) {
1102 #ifdef CONFIG_F2FS_FS_ZSTD
1103 ret = f2fs_set_zstd_level(sbi, name);
1108 F2FS_OPTION(sbi).compress_algorithm =
1111 f2fs_info(sbi, "kernel doesn't support zstd compression");
1113 } else if (!strcmp(name, "lzo-rle")) {
1114 #ifdef CONFIG_F2FS_FS_LZORLE
1115 F2FS_OPTION(sbi).compress_level = 0;
1116 F2FS_OPTION(sbi).compress_algorithm =
1119 f2fs_info(sbi, "kernel doesn't support lzorle compression");
1127 case Opt_compress_log_size:
1128 if (!f2fs_sb_has_compression(sbi)) {
1129 f2fs_info(sbi, "Image doesn't support compression");
1132 if (args->from && match_int(args, &arg))
1134 if (arg < MIN_COMPRESS_LOG_SIZE ||
1135 arg > MAX_COMPRESS_LOG_SIZE) {
1137 "Compress cluster log size is out of range");
1140 F2FS_OPTION(sbi).compress_log_size = arg;
1142 case Opt_compress_extension:
1143 if (!f2fs_sb_has_compression(sbi)) {
1144 f2fs_info(sbi, "Image doesn't support compression");
1147 name = match_strdup(&args[0]);
1151 ext = F2FS_OPTION(sbi).extensions;
1152 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1154 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1155 ext_cnt >= COMPRESS_EXT_NUM) {
1157 "invalid extension length/number");
1162 if (is_compress_extension_exist(sbi, name, true)) {
1167 ret = strscpy(ext[ext_cnt], name);
1172 F2FS_OPTION(sbi).compress_ext_cnt++;
1175 case Opt_nocompress_extension:
1176 if (!f2fs_sb_has_compression(sbi)) {
1177 f2fs_info(sbi, "Image doesn't support compression");
1180 name = match_strdup(&args[0]);
1184 noext = F2FS_OPTION(sbi).noextensions;
1185 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
1187 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1188 noext_cnt >= COMPRESS_EXT_NUM) {
1190 "invalid extension length/number");
1195 if (is_compress_extension_exist(sbi, name, false)) {
1200 ret = strscpy(noext[noext_cnt], name);
1205 F2FS_OPTION(sbi).nocompress_ext_cnt++;
1208 case Opt_compress_chksum:
1209 if (!f2fs_sb_has_compression(sbi)) {
1210 f2fs_info(sbi, "Image doesn't support compression");
1213 F2FS_OPTION(sbi).compress_chksum = true;
1215 case Opt_compress_mode:
1216 if (!f2fs_sb_has_compression(sbi)) {
1217 f2fs_info(sbi, "Image doesn't support compression");
1220 name = match_strdup(&args[0]);
1223 if (!strcmp(name, "fs")) {
1224 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1225 } else if (!strcmp(name, "user")) {
1226 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1233 case Opt_compress_cache:
1234 if (!f2fs_sb_has_compression(sbi)) {
1235 f2fs_info(sbi, "Image doesn't support compression");
1238 set_opt(sbi, COMPRESS_CACHE);
1241 case Opt_compress_algorithm:
1242 case Opt_compress_log_size:
1243 case Opt_compress_extension:
1244 case Opt_nocompress_extension:
1245 case Opt_compress_chksum:
1246 case Opt_compress_mode:
1247 case Opt_compress_cache:
1248 f2fs_info(sbi, "compression options not supported");
1255 set_opt(sbi, GC_MERGE);
1257 case Opt_nogc_merge:
1258 clear_opt(sbi, GC_MERGE);
1260 case Opt_discard_unit:
1261 name = match_strdup(&args[0]);
1264 if (!strcmp(name, "block")) {
1265 F2FS_OPTION(sbi).discard_unit =
1267 } else if (!strcmp(name, "segment")) {
1268 F2FS_OPTION(sbi).discard_unit =
1269 DISCARD_UNIT_SEGMENT;
1270 } else if (!strcmp(name, "section")) {
1271 F2FS_OPTION(sbi).discard_unit =
1272 DISCARD_UNIT_SECTION;
1279 case Opt_memory_mode:
1280 name = match_strdup(&args[0]);
1283 if (!strcmp(name, "normal")) {
1284 F2FS_OPTION(sbi).memory_mode =
1286 } else if (!strcmp(name, "low")) {
1287 F2FS_OPTION(sbi).memory_mode =
1295 case Opt_age_extent_cache:
1296 set_opt(sbi, AGE_EXTENT_CACHE);
1299 name = match_strdup(&args[0]);
1302 if (!strcmp(name, "remount-ro")) {
1303 F2FS_OPTION(sbi).errors =
1304 MOUNT_ERRORS_READONLY;
1305 } else if (!strcmp(name, "continue")) {
1306 F2FS_OPTION(sbi).errors =
1307 MOUNT_ERRORS_CONTINUE;
1308 } else if (!strcmp(name, "panic")) {
1309 F2FS_OPTION(sbi).errors =
1318 set_opt(sbi, NAT_BITS);
1321 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1329 static int f2fs_default_check(struct f2fs_sb_info *sbi)
1332 if (f2fs_check_quota_options(sbi))
1335 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
1336 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1339 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
1340 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1345 if (!IS_ENABLED(CONFIG_UNICODE) && f2fs_sb_has_casefold(sbi)) {
1347 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1352 * The BLKZONED feature indicates that the drive was formatted with
1353 * zone alignment optimization. This is optional for host-aware
1354 * devices, but mandatory for host-managed zoned block devices.
1356 if (f2fs_sb_has_blkzoned(sbi)) {
1357 #ifdef CONFIG_BLK_DEV_ZONED
1358 if (F2FS_OPTION(sbi).discard_unit !=
1359 DISCARD_UNIT_SECTION) {
1360 f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default");
1361 F2FS_OPTION(sbi).discard_unit =
1362 DISCARD_UNIT_SECTION;
1365 if (F2FS_OPTION(sbi).fs_mode != FS_MODE_LFS) {
1366 f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature");
1370 f2fs_err(sbi, "Zoned block device support is not enabled");
1375 #ifdef CONFIG_F2FS_FS_COMPRESSION
1376 if (f2fs_test_compress_extension(sbi)) {
1377 f2fs_err(sbi, "invalid compress or nocompress extension");
1382 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
1383 int min_size, max_size;
1385 if (!f2fs_sb_has_extra_attr(sbi) ||
1386 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
1387 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1390 if (!test_opt(sbi, INLINE_XATTR)) {
1391 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1395 min_size = MIN_INLINE_XATTR_SIZE;
1396 max_size = MAX_INLINE_XATTR_SIZE;
1398 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1399 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
1400 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1401 min_size, max_size);
1406 if (test_opt(sbi, ATGC) && f2fs_lfs_mode(sbi)) {
1407 f2fs_err(sbi, "LFS is not compatible with ATGC");
1411 if (f2fs_is_readonly(sbi) && test_opt(sbi, FLUSH_MERGE)) {
1412 f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
1416 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1417 f2fs_err(sbi, "Allow to mount readonly mode only");
1421 if (test_opt(sbi, NORECOVERY) && !f2fs_readonly(sbi->sb)) {
1422 f2fs_err(sbi, "norecovery requires readonly mount");
1429 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1431 struct f2fs_inode_info *fi;
1433 if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC))
1436 fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO);
1440 init_once((void *) fi);
1442 /* Initialize f2fs-specific inode info */
1443 atomic_set(&fi->dirty_pages, 0);
1444 atomic_set(&fi->i_compr_blocks, 0);
1445 init_f2fs_rwsem(&fi->i_sem);
1446 spin_lock_init(&fi->i_size_lock);
1447 INIT_LIST_HEAD(&fi->dirty_list);
1448 INIT_LIST_HEAD(&fi->gdirty_list);
1449 INIT_LIST_HEAD(&fi->gdonate_list);
1450 init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
1451 init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
1452 init_f2fs_rwsem(&fi->i_xattr_sem);
1454 /* Will be used by directory only */
1455 fi->i_dir_level = F2FS_SB(sb)->dir_level;
1457 return &fi->vfs_inode;
1460 static int f2fs_drop_inode(struct inode *inode)
1462 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1466 * during filesystem shutdown, if checkpoint is disabled,
1467 * drop useless meta/node dirty pages.
1469 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1470 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1471 inode->i_ino == F2FS_META_INO(sbi)) {
1472 trace_f2fs_drop_inode(inode, 1);
1478 * This is to avoid a deadlock condition like below.
1479 * writeback_single_inode(inode)
1480 * - f2fs_write_data_page
1481 * - f2fs_gc -> iput -> evict
1482 * - inode_wait_for_writeback(inode)
1484 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
1485 if (!inode->i_nlink && !is_bad_inode(inode)) {
1486 /* to avoid evict_inode call simultaneously */
1487 atomic_inc(&inode->i_count);
1488 spin_unlock(&inode->i_lock);
1490 /* should remain fi->extent_tree for writepage */
1491 f2fs_destroy_extent_node(inode);
1493 sb_start_intwrite(inode->i_sb);
1494 f2fs_i_size_write(inode, 0);
1496 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1497 inode, NULL, 0, DATA);
1498 truncate_inode_pages_final(inode->i_mapping);
1500 if (F2FS_HAS_BLOCKS(inode))
1501 f2fs_truncate(inode);
1503 sb_end_intwrite(inode->i_sb);
1505 spin_lock(&inode->i_lock);
1506 atomic_dec(&inode->i_count);
1508 trace_f2fs_drop_inode(inode, 0);
1511 ret = generic_drop_inode(inode);
1513 ret = fscrypt_drop_inode(inode);
1514 trace_f2fs_drop_inode(inode, ret);
1518 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1520 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1523 spin_lock(&sbi->inode_lock[DIRTY_META]);
1524 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1527 set_inode_flag(inode, FI_DIRTY_INODE);
1528 stat_inc_dirty_inode(sbi, DIRTY_META);
1530 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1531 list_add_tail(&F2FS_I(inode)->gdirty_list,
1532 &sbi->inode_list[DIRTY_META]);
1533 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1535 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1537 /* if atomic write is not committed, set inode w/ atomic dirty */
1538 if (!ret && f2fs_is_atomic_file(inode) &&
1539 !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
1540 set_inode_flag(inode, FI_ATOMIC_DIRTIED);
1545 void f2fs_inode_synced(struct inode *inode)
1547 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1549 spin_lock(&sbi->inode_lock[DIRTY_META]);
1550 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1551 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1554 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1555 list_del_init(&F2FS_I(inode)->gdirty_list);
1556 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1558 clear_inode_flag(inode, FI_DIRTY_INODE);
1559 clear_inode_flag(inode, FI_AUTO_RECOVER);
1560 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1561 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1565 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1567 * We should call set_dirty_inode to write the dirty inode through write_inode.
1569 static void f2fs_dirty_inode(struct inode *inode, int flags)
1571 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1573 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1574 inode->i_ino == F2FS_META_INO(sbi))
1577 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1578 clear_inode_flag(inode, FI_AUTO_RECOVER);
1580 f2fs_inode_dirtied(inode, false);
1583 static void f2fs_free_inode(struct inode *inode)
1585 fscrypt_free_inode(inode);
1586 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1589 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1591 percpu_counter_destroy(&sbi->total_valid_inode_count);
1592 percpu_counter_destroy(&sbi->rf_node_block_count);
1593 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1596 static void destroy_device_list(struct f2fs_sb_info *sbi)
1600 for (i = 0; i < sbi->s_ndevs; i++) {
1602 bdev_fput(FDEV(i).bdev_file);
1603 #ifdef CONFIG_BLK_DEV_ZONED
1604 kvfree(FDEV(i).blkz_seq);
1610 static void f2fs_put_super(struct super_block *sb)
1612 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1617 /* unregister procfs/sysfs entries in advance to avoid race case */
1618 f2fs_unregister_sysfs(sbi);
1620 f2fs_quota_off_umount(sb);
1622 /* prevent remaining shrinker jobs */
1623 mutex_lock(&sbi->umount_mutex);
1626 * flush all issued checkpoints and stop checkpoint issue thread.
1627 * after then, all checkpoints should be done by each process context.
1629 f2fs_stop_ckpt_thread(sbi);
1632 * We don't need to do checkpoint when superblock is clean.
1633 * But, the previous checkpoint was not done by umount, it needs to do
1634 * clean checkpoint again.
1636 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1637 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1638 struct cp_control cpc = {
1639 .reason = CP_UMOUNT,
1641 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1642 err = f2fs_write_checkpoint(sbi, &cpc);
1645 /* be sure to wait for any on-going discard commands */
1646 done = f2fs_issue_discard_timeout(sbi);
1647 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
1648 struct cp_control cpc = {
1649 .reason = CP_UMOUNT | CP_TRIMMED,
1651 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1652 err = f2fs_write_checkpoint(sbi, &cpc);
1656 * normally superblock is clean, so we need to release this.
1657 * In addition, EIO will skip do checkpoint, we need this as well.
1659 f2fs_release_ino_entry(sbi, true);
1661 f2fs_leave_shrinker(sbi);
1662 mutex_unlock(&sbi->umount_mutex);
1664 /* our cp_error case, we can wait for any writeback page */
1665 f2fs_flush_merged_writes(sbi);
1667 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1669 if (err || f2fs_cp_error(sbi)) {
1670 truncate_inode_pages_final(NODE_MAPPING(sbi));
1671 truncate_inode_pages_final(META_MAPPING(sbi));
1674 for (i = 0; i < NR_COUNT_TYPE; i++) {
1675 if (!get_pages(sbi, i))
1677 f2fs_err(sbi, "detect filesystem reference count leak during "
1678 "umount, type: %d, count: %lld", i, get_pages(sbi, i));
1679 f2fs_bug_on(sbi, 1);
1682 f2fs_bug_on(sbi, sbi->fsync_node_num);
1684 f2fs_destroy_compress_inode(sbi);
1686 iput(sbi->node_inode);
1687 sbi->node_inode = NULL;
1689 iput(sbi->meta_inode);
1690 sbi->meta_inode = NULL;
1693 * iput() can update stat information, if f2fs_write_checkpoint()
1694 * above failed with error.
1696 f2fs_destroy_stats(sbi);
1698 /* destroy f2fs internal modules */
1699 f2fs_destroy_node_manager(sbi);
1700 f2fs_destroy_segment_manager(sbi);
1702 /* flush s_error_work before sbi destroy */
1703 flush_work(&sbi->s_error_work);
1705 f2fs_destroy_post_read_wq(sbi);
1709 kfree(sbi->raw_super);
1711 f2fs_destroy_page_array_cache(sbi);
1712 f2fs_destroy_xattr_caches(sbi);
1714 for (i = 0; i < MAXQUOTAS; i++)
1715 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1717 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1718 destroy_percpu_info(sbi);
1719 f2fs_destroy_iostat(sbi);
1720 for (i = 0; i < NR_PAGE_TYPE; i++)
1721 kvfree(sbi->write_io[i]);
1722 #if IS_ENABLED(CONFIG_UNICODE)
1723 utf8_unload(sb->s_encoding);
1727 int f2fs_sync_fs(struct super_block *sb, int sync)
1729 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1732 if (unlikely(f2fs_cp_error(sbi)))
1734 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1737 trace_f2fs_sync_fs(sb, sync);
1739 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1743 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1744 err = f2fs_issue_checkpoint(sbi);
1750 static int f2fs_freeze(struct super_block *sb)
1752 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1754 if (f2fs_readonly(sb))
1757 /* IO error happened before */
1758 if (unlikely(f2fs_cp_error(sbi)))
1761 /* must be clean, since sync_filesystem() was already called */
1762 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY))
1765 sbi->umount_lock_holder = current;
1767 /* Let's flush checkpoints and stop the thread. */
1768 f2fs_flush_ckpt_thread(sbi);
1770 sbi->umount_lock_holder = NULL;
1772 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
1773 set_sbi_flag(sbi, SBI_IS_FREEZING);
1777 static int f2fs_unfreeze(struct super_block *sb)
1779 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1782 * It will update discard_max_bytes of mounted lvm device to zero
1783 * after creating snapshot on this lvm device, let's drop all
1784 * remained discards.
1785 * We don't need to disable real-time discard because discard_max_bytes
1786 * will recover after removal of snapshot.
1788 if (test_opt(sbi, DISCARD) && !f2fs_hw_support_discard(sbi))
1789 f2fs_issue_discard_timeout(sbi);
1791 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1796 static int f2fs_statfs_project(struct super_block *sb,
1797 kprojid_t projid, struct kstatfs *buf)
1800 struct dquot *dquot;
1804 qid = make_kqid_projid(projid);
1805 dquot = dqget(sb, qid);
1807 return PTR_ERR(dquot);
1808 spin_lock(&dquot->dq_dqb_lock);
1810 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1811 dquot->dq_dqb.dqb_bhardlimit);
1812 limit >>= sb->s_blocksize_bits;
1815 uint64_t remaining = 0;
1817 curblock = (dquot->dq_dqb.dqb_curspace +
1818 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1819 if (limit > curblock)
1820 remaining = limit - curblock;
1822 buf->f_blocks = min(buf->f_blocks, limit);
1823 buf->f_bfree = min(buf->f_bfree, remaining);
1824 buf->f_bavail = min(buf->f_bavail, remaining);
1827 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1828 dquot->dq_dqb.dqb_ihardlimit);
1831 uint64_t remaining = 0;
1833 if (limit > dquot->dq_dqb.dqb_curinodes)
1834 remaining = limit - dquot->dq_dqb.dqb_curinodes;
1836 buf->f_files = min(buf->f_files, limit);
1837 buf->f_ffree = min(buf->f_ffree, remaining);
1840 spin_unlock(&dquot->dq_dqb_lock);
1846 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1848 struct super_block *sb = dentry->d_sb;
1849 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1850 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1851 block_t total_count, user_block_count, start_count;
1852 u64 avail_node_count;
1853 unsigned int total_valid_node_count;
1855 total_count = le64_to_cpu(sbi->raw_super->block_count);
1856 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1857 buf->f_type = F2FS_SUPER_MAGIC;
1858 buf->f_bsize = sbi->blocksize;
1860 buf->f_blocks = total_count - start_count;
1862 spin_lock(&sbi->stat_lock);
1864 buf->f_blocks -= sbi->current_reserved_blocks;
1865 user_block_count = sbi->user_block_count;
1866 total_valid_node_count = valid_node_count(sbi);
1867 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1868 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1869 sbi->current_reserved_blocks;
1871 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1874 buf->f_bfree -= sbi->unusable_block_count;
1875 spin_unlock(&sbi->stat_lock);
1877 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1878 buf->f_bavail = buf->f_bfree -
1879 F2FS_OPTION(sbi).root_reserved_blocks;
1883 if (avail_node_count > user_block_count) {
1884 buf->f_files = user_block_count;
1885 buf->f_ffree = buf->f_bavail;
1887 buf->f_files = avail_node_count;
1888 buf->f_ffree = min(avail_node_count - total_valid_node_count,
1892 buf->f_namelen = F2FS_NAME_LEN;
1893 buf->f_fsid = u64_to_fsid(id);
1896 if (is_inode_flag_set(d_inode(dentry), FI_PROJ_INHERIT) &&
1897 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1898 f2fs_statfs_project(sb, F2FS_I(d_inode(dentry))->i_projid, buf);
1904 static inline void f2fs_show_quota_options(struct seq_file *seq,
1905 struct super_block *sb)
1908 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1910 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1913 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1924 seq_printf(seq, ",jqfmt=%s", fmtname);
1927 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1928 seq_show_option(seq, "usrjquota",
1929 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1931 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1932 seq_show_option(seq, "grpjquota",
1933 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1935 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1936 seq_show_option(seq, "prjjquota",
1937 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1941 #ifdef CONFIG_F2FS_FS_COMPRESSION
1942 static inline void f2fs_show_compress_options(struct seq_file *seq,
1943 struct super_block *sb)
1945 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1949 if (!f2fs_sb_has_compression(sbi))
1952 switch (F2FS_OPTION(sbi).compress_algorithm) {
1962 case COMPRESS_LZORLE:
1963 algtype = "lzo-rle";
1966 seq_printf(seq, ",compress_algorithm=%s", algtype);
1968 if (F2FS_OPTION(sbi).compress_level)
1969 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1971 seq_printf(seq, ",compress_log_size=%u",
1972 F2FS_OPTION(sbi).compress_log_size);
1974 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1975 seq_printf(seq, ",compress_extension=%s",
1976 F2FS_OPTION(sbi).extensions[i]);
1979 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
1980 seq_printf(seq, ",nocompress_extension=%s",
1981 F2FS_OPTION(sbi).noextensions[i]);
1984 if (F2FS_OPTION(sbi).compress_chksum)
1985 seq_puts(seq, ",compress_chksum");
1987 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1988 seq_printf(seq, ",compress_mode=%s", "fs");
1989 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1990 seq_printf(seq, ",compress_mode=%s", "user");
1992 if (test_opt(sbi, COMPRESS_CACHE))
1993 seq_puts(seq, ",compress_cache");
1997 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1999 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
2001 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
2002 seq_printf(seq, ",background_gc=%s", "sync");
2003 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
2004 seq_printf(seq, ",background_gc=%s", "on");
2005 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
2006 seq_printf(seq, ",background_gc=%s", "off");
2008 if (test_opt(sbi, GC_MERGE))
2009 seq_puts(seq, ",gc_merge");
2011 seq_puts(seq, ",nogc_merge");
2013 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2014 seq_puts(seq, ",disable_roll_forward");
2015 if (test_opt(sbi, NORECOVERY))
2016 seq_puts(seq, ",norecovery");
2017 if (test_opt(sbi, DISCARD)) {
2018 seq_puts(seq, ",discard");
2019 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
2020 seq_printf(seq, ",discard_unit=%s", "block");
2021 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2022 seq_printf(seq, ",discard_unit=%s", "segment");
2023 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2024 seq_printf(seq, ",discard_unit=%s", "section");
2026 seq_puts(seq, ",nodiscard");
2028 #ifdef CONFIG_F2FS_FS_XATTR
2029 if (test_opt(sbi, XATTR_USER))
2030 seq_puts(seq, ",user_xattr");
2032 seq_puts(seq, ",nouser_xattr");
2033 if (test_opt(sbi, INLINE_XATTR))
2034 seq_puts(seq, ",inline_xattr");
2036 seq_puts(seq, ",noinline_xattr");
2037 if (test_opt(sbi, INLINE_XATTR_SIZE))
2038 seq_printf(seq, ",inline_xattr_size=%u",
2039 F2FS_OPTION(sbi).inline_xattr_size);
2041 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2042 if (test_opt(sbi, POSIX_ACL))
2043 seq_puts(seq, ",acl");
2045 seq_puts(seq, ",noacl");
2047 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
2048 seq_puts(seq, ",disable_ext_identify");
2049 if (test_opt(sbi, INLINE_DATA))
2050 seq_puts(seq, ",inline_data");
2052 seq_puts(seq, ",noinline_data");
2053 if (test_opt(sbi, INLINE_DENTRY))
2054 seq_puts(seq, ",inline_dentry");
2056 seq_puts(seq, ",noinline_dentry");
2057 if (test_opt(sbi, FLUSH_MERGE))
2058 seq_puts(seq, ",flush_merge");
2060 seq_puts(seq, ",noflush_merge");
2061 if (test_opt(sbi, NOBARRIER))
2062 seq_puts(seq, ",nobarrier");
2064 seq_puts(seq, ",barrier");
2065 if (test_opt(sbi, FASTBOOT))
2066 seq_puts(seq, ",fastboot");
2067 if (test_opt(sbi, READ_EXTENT_CACHE))
2068 seq_puts(seq, ",extent_cache");
2070 seq_puts(seq, ",noextent_cache");
2071 if (test_opt(sbi, AGE_EXTENT_CACHE))
2072 seq_puts(seq, ",age_extent_cache");
2073 if (test_opt(sbi, DATA_FLUSH))
2074 seq_puts(seq, ",data_flush");
2076 seq_puts(seq, ",mode=");
2077 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
2078 seq_puts(seq, "adaptive");
2079 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
2080 seq_puts(seq, "lfs");
2081 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
2082 seq_puts(seq, "fragment:segment");
2083 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2084 seq_puts(seq, "fragment:block");
2085 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
2086 if (test_opt(sbi, RESERVE_ROOT))
2087 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
2088 F2FS_OPTION(sbi).root_reserved_blocks,
2089 from_kuid_munged(&init_user_ns,
2090 F2FS_OPTION(sbi).s_resuid),
2091 from_kgid_munged(&init_user_ns,
2092 F2FS_OPTION(sbi).s_resgid));
2093 #ifdef CONFIG_F2FS_FAULT_INJECTION
2094 if (test_opt(sbi, FAULT_INJECTION)) {
2095 seq_printf(seq, ",fault_injection=%u",
2096 F2FS_OPTION(sbi).fault_info.inject_rate);
2097 seq_printf(seq, ",fault_type=%u",
2098 F2FS_OPTION(sbi).fault_info.inject_type);
2102 if (test_opt(sbi, QUOTA))
2103 seq_puts(seq, ",quota");
2104 if (test_opt(sbi, USRQUOTA))
2105 seq_puts(seq, ",usrquota");
2106 if (test_opt(sbi, GRPQUOTA))
2107 seq_puts(seq, ",grpquota");
2108 if (test_opt(sbi, PRJQUOTA))
2109 seq_puts(seq, ",prjquota");
2111 f2fs_show_quota_options(seq, sbi->sb);
2113 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
2115 if (sbi->sb->s_flags & SB_INLINECRYPT)
2116 seq_puts(seq, ",inlinecrypt");
2118 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
2119 seq_printf(seq, ",alloc_mode=%s", "default");
2120 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2121 seq_printf(seq, ",alloc_mode=%s", "reuse");
2123 if (test_opt(sbi, DISABLE_CHECKPOINT))
2124 seq_printf(seq, ",checkpoint=disable:%u",
2125 F2FS_OPTION(sbi).unusable_cap);
2126 if (test_opt(sbi, MERGE_CHECKPOINT))
2127 seq_puts(seq, ",checkpoint_merge");
2129 seq_puts(seq, ",nocheckpoint_merge");
2130 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
2131 seq_printf(seq, ",fsync_mode=%s", "posix");
2132 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
2133 seq_printf(seq, ",fsync_mode=%s", "strict");
2134 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
2135 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
2137 #ifdef CONFIG_F2FS_FS_COMPRESSION
2138 f2fs_show_compress_options(seq, sbi->sb);
2141 if (test_opt(sbi, ATGC))
2142 seq_puts(seq, ",atgc");
2144 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
2145 seq_printf(seq, ",memory=%s", "normal");
2146 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
2147 seq_printf(seq, ",memory=%s", "low");
2149 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2150 seq_printf(seq, ",errors=%s", "remount-ro");
2151 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
2152 seq_printf(seq, ",errors=%s", "continue");
2153 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
2154 seq_printf(seq, ",errors=%s", "panic");
2156 if (test_opt(sbi, NAT_BITS))
2157 seq_puts(seq, ",nat_bits");
2162 static void default_options(struct f2fs_sb_info *sbi, bool remount)
2164 /* init some FS parameters */
2166 set_opt(sbi, READ_EXTENT_CACHE);
2167 clear_opt(sbi, DISABLE_CHECKPOINT);
2169 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2170 set_opt(sbi, DISCARD);
2172 if (f2fs_sb_has_blkzoned(sbi))
2173 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2175 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2178 if (f2fs_sb_has_readonly(sbi))
2179 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
2181 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
2183 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
2184 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
2185 SMALL_VOLUME_SEGMENTS)
2186 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2188 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
2189 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
2190 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2191 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2192 if (f2fs_sb_has_compression(sbi)) {
2193 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
2194 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
2195 F2FS_OPTION(sbi).compress_ext_cnt = 0;
2196 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
2198 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
2199 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
2200 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
2202 set_opt(sbi, INLINE_XATTR);
2203 set_opt(sbi, INLINE_DATA);
2204 set_opt(sbi, INLINE_DENTRY);
2205 set_opt(sbi, MERGE_CHECKPOINT);
2206 set_opt(sbi, LAZYTIME);
2207 F2FS_OPTION(sbi).unusable_cap = 0;
2208 if (!f2fs_is_readonly(sbi))
2209 set_opt(sbi, FLUSH_MERGE);
2210 if (f2fs_sb_has_blkzoned(sbi))
2211 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
2213 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
2215 #ifdef CONFIG_F2FS_FS_XATTR
2216 set_opt(sbi, XATTR_USER);
2218 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2219 set_opt(sbi, POSIX_ACL);
2222 f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL);
2226 static int f2fs_enable_quotas(struct super_block *sb);
2229 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
2231 unsigned int s_flags = sbi->sb->s_flags;
2232 struct cp_control cpc;
2233 unsigned int gc_mode = sbi->gc_mode;
2238 if (s_flags & SB_RDONLY) {
2239 f2fs_err(sbi, "checkpoint=disable on readonly fs");
2242 sbi->sb->s_flags |= SB_ACTIVE;
2244 /* check if we need more GC first */
2245 unusable = f2fs_get_unusable_blocks(sbi);
2246 if (!f2fs_disable_cp_again(sbi, unusable))
2249 f2fs_update_time(sbi, DISABLE_TIME);
2251 sbi->gc_mode = GC_URGENT_HIGH;
2253 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
2254 struct f2fs_gc_control gc_control = {
2255 .victim_segno = NULL_SEGNO,
2256 .init_gc_type = FG_GC,
2257 .should_migrate_blocks = false,
2258 .err_gc_skipped = true,
2260 .nr_free_secs = 1 };
2262 f2fs_down_write(&sbi->gc_lock);
2263 stat_inc_gc_call_count(sbi, FOREGROUND);
2264 err = f2fs_gc(sbi, &gc_control);
2265 if (err == -ENODATA) {
2269 if (err && err != -EAGAIN)
2273 ret = sync_filesystem(sbi->sb);
2275 err = ret ? ret : err;
2279 unusable = f2fs_get_unusable_blocks(sbi);
2280 if (f2fs_disable_cp_again(sbi, unusable)) {
2286 f2fs_down_write(&sbi->gc_lock);
2287 cpc.reason = CP_PAUSE;
2288 set_sbi_flag(sbi, SBI_CP_DISABLED);
2289 stat_inc_cp_call_count(sbi, TOTAL_CALL);
2290 err = f2fs_write_checkpoint(sbi, &cpc);
2294 spin_lock(&sbi->stat_lock);
2295 sbi->unusable_block_count = unusable;
2296 spin_unlock(&sbi->stat_lock);
2299 f2fs_up_write(&sbi->gc_lock);
2301 sbi->gc_mode = gc_mode;
2302 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2306 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
2308 int retry = DEFAULT_RETRY_IO_COUNT;
2310 /* we should flush all the data to keep data consistency */
2312 sync_inodes_sb(sbi->sb);
2313 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2314 } while (get_pages(sbi, F2FS_DIRTY_DATA) && retry--);
2316 if (unlikely(retry < 0))
2317 f2fs_warn(sbi, "checkpoint=enable has some unwritten data.");
2319 f2fs_down_write(&sbi->gc_lock);
2320 f2fs_dirty_to_prefree(sbi);
2322 clear_sbi_flag(sbi, SBI_CP_DISABLED);
2323 set_sbi_flag(sbi, SBI_IS_DIRTY);
2324 f2fs_up_write(&sbi->gc_lock);
2326 f2fs_sync_fs(sbi->sb, 1);
2328 /* Let's ensure there's no pending checkpoint anymore */
2329 f2fs_flush_ckpt_thread(sbi);
2332 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
2334 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2335 struct f2fs_mount_info org_mount_opt;
2336 unsigned long old_sb_flags;
2338 bool need_restart_gc = false, need_stop_gc = false;
2339 bool need_restart_flush = false, need_stop_flush = false;
2340 bool need_restart_discard = false, need_stop_discard = false;
2341 bool need_enable_checkpoint = false, need_disable_checkpoint = false;
2342 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
2343 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
2344 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
2345 bool no_atgc = !test_opt(sbi, ATGC);
2346 bool no_discard = !test_opt(sbi, DISCARD);
2347 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
2348 bool block_unit_discard = f2fs_block_unit_discard(sbi);
2349 bool no_nat_bits = !test_opt(sbi, NAT_BITS);
2355 * Save the old mount options in case we
2356 * need to restore them.
2358 org_mount_opt = sbi->mount_opt;
2359 old_sb_flags = sb->s_flags;
2361 sbi->umount_lock_holder = current;
2364 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
2365 for (i = 0; i < MAXQUOTAS; i++) {
2366 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2367 org_mount_opt.s_qf_names[i] =
2368 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
2370 if (!org_mount_opt.s_qf_names[i]) {
2371 for (j = 0; j < i; j++)
2372 kfree(org_mount_opt.s_qf_names[j]);
2376 org_mount_opt.s_qf_names[i] = NULL;
2381 /* recover superblocks we couldn't write due to previous RO mount */
2382 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
2383 err = f2fs_commit_super(sbi, false);
2384 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2387 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2390 default_options(sbi, true);
2392 /* parse mount options */
2393 err = parse_options(sbi, data, true);
2397 #ifdef CONFIG_BLK_DEV_ZONED
2398 if (f2fs_sb_has_blkzoned(sbi) &&
2399 sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
2401 "zoned: max open zones %u is too small, need at least %u open zones",
2402 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
2408 err = f2fs_default_check(sbi);
2412 /* flush outstanding errors before changing fs state */
2413 flush_work(&sbi->s_error_work);
2416 * Previous and new state of filesystem is RO,
2417 * so skip checking GC and FLUSH_MERGE conditions.
2419 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
2422 if (f2fs_dev_is_readonly(sbi) && !(*flags & SB_RDONLY)) {
2428 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
2429 err = dquot_suspend(sb, -1);
2432 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
2433 /* dquot_resume needs RW */
2434 sb->s_flags &= ~SB_RDONLY;
2435 if (sb_any_quota_suspended(sb)) {
2436 dquot_resume(sb, -1);
2437 } else if (f2fs_sb_has_quota_ino(sbi)) {
2438 err = f2fs_enable_quotas(sb);
2444 if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
2446 f2fs_warn(sbi, "LFS is not compatible with IPU");
2450 /* disallow enable atgc dynamically */
2451 if (no_atgc == !!test_opt(sbi, ATGC)) {
2453 f2fs_warn(sbi, "switch atgc option is not allowed");
2457 /* disallow enable/disable extent_cache dynamically */
2458 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
2460 f2fs_warn(sbi, "switch extent_cache option is not allowed");
2463 /* disallow enable/disable age extent_cache dynamically */
2464 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2466 f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
2470 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2472 f2fs_warn(sbi, "switch compress_cache option is not allowed");
2476 if (block_unit_discard != f2fs_block_unit_discard(sbi)) {
2478 f2fs_warn(sbi, "switch discard_unit option is not allowed");
2482 if (no_nat_bits == !!test_opt(sbi, NAT_BITS)) {
2484 f2fs_warn(sbi, "switch nat_bits option is not allowed");
2488 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2490 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
2495 * We stop the GC thread if FS is mounted as RO
2496 * or if background_gc = off is passed in mount
2497 * option. Also sync the filesystem.
2499 if ((*flags & SB_RDONLY) ||
2500 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2501 !test_opt(sbi, GC_MERGE))) {
2502 if (sbi->gc_thread) {
2503 f2fs_stop_gc_thread(sbi);
2504 need_restart_gc = true;
2506 } else if (!sbi->gc_thread) {
2507 err = f2fs_start_gc_thread(sbi);
2510 need_stop_gc = true;
2513 if (*flags & SB_RDONLY) {
2516 set_sbi_flag(sbi, SBI_IS_DIRTY);
2517 set_sbi_flag(sbi, SBI_IS_CLOSE);
2518 f2fs_sync_fs(sb, 1);
2519 clear_sbi_flag(sbi, SBI_IS_CLOSE);
2523 * We stop issue flush thread if FS is mounted as RO
2524 * or if flush_merge is not passed in mount option.
2526 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2527 clear_opt(sbi, FLUSH_MERGE);
2528 f2fs_destroy_flush_cmd_control(sbi, false);
2529 need_restart_flush = true;
2531 err = f2fs_create_flush_cmd_control(sbi);
2534 need_stop_flush = true;
2537 if (no_discard == !!test_opt(sbi, DISCARD)) {
2538 if (test_opt(sbi, DISCARD)) {
2539 err = f2fs_start_discard_thread(sbi);
2542 need_stop_discard = true;
2544 f2fs_stop_discard_thread(sbi);
2545 f2fs_issue_discard_timeout(sbi);
2546 need_restart_discard = true;
2550 adjust_unusable_cap_perc(sbi);
2551 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
2552 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2553 err = f2fs_disable_checkpoint(sbi);
2555 goto restore_discard;
2556 need_enable_checkpoint = true;
2558 f2fs_enable_checkpoint(sbi);
2559 need_disable_checkpoint = true;
2564 * Place this routine at the end, since a new checkpoint would be
2565 * triggered while remount and we need to take care of it before
2566 * returning from remount.
2568 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2569 !test_opt(sbi, MERGE_CHECKPOINT)) {
2570 f2fs_stop_ckpt_thread(sbi);
2572 /* Flush if the prevous checkpoint, if exists. */
2573 f2fs_flush_ckpt_thread(sbi);
2575 err = f2fs_start_ckpt_thread(sbi);
2578 "Failed to start F2FS issue_checkpoint_thread (%d)",
2580 goto restore_checkpoint;
2586 /* Release old quota file names */
2587 for (i = 0; i < MAXQUOTAS; i++)
2588 kfree(org_mount_opt.s_qf_names[i]);
2590 /* Update the POSIXACL Flag */
2591 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2592 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2594 limit_reserve_root(sbi);
2595 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
2597 sbi->umount_lock_holder = NULL;
2600 if (need_enable_checkpoint) {
2601 f2fs_enable_checkpoint(sbi);
2602 } else if (need_disable_checkpoint) {
2603 if (f2fs_disable_checkpoint(sbi))
2604 f2fs_warn(sbi, "checkpoint has not been disabled");
2607 if (need_restart_discard) {
2608 if (f2fs_start_discard_thread(sbi))
2609 f2fs_warn(sbi, "discard has been stopped");
2610 } else if (need_stop_discard) {
2611 f2fs_stop_discard_thread(sbi);
2614 if (need_restart_flush) {
2615 if (f2fs_create_flush_cmd_control(sbi))
2616 f2fs_warn(sbi, "background flush thread has stopped");
2617 } else if (need_stop_flush) {
2618 clear_opt(sbi, FLUSH_MERGE);
2619 f2fs_destroy_flush_cmd_control(sbi, false);
2622 if (need_restart_gc) {
2623 if (f2fs_start_gc_thread(sbi))
2624 f2fs_warn(sbi, "background gc thread has stopped");
2625 } else if (need_stop_gc) {
2626 f2fs_stop_gc_thread(sbi);
2630 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
2631 for (i = 0; i < MAXQUOTAS; i++) {
2632 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2633 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
2636 sbi->mount_opt = org_mount_opt;
2637 sb->s_flags = old_sb_flags;
2639 sbi->umount_lock_holder = NULL;
2643 static void f2fs_shutdown(struct super_block *sb)
2645 f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false);
2649 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
2651 /* need to recovery orphan */
2652 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
2654 /* need to recovery data */
2655 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2657 if (test_opt(sbi, NORECOVERY))
2659 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
2662 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
2664 bool readonly = f2fs_readonly(sbi->sb);
2666 if (!f2fs_need_recovery(sbi))
2669 /* it doesn't need to check f2fs_sb_has_readonly() */
2670 if (f2fs_hw_is_readonly(sbi))
2674 sbi->sb->s_flags &= ~SB_RDONLY;
2675 set_sbi_flag(sbi, SBI_IS_WRITABLE);
2679 * Turn on quotas which were not enabled for read-only mounts if
2680 * filesystem has quota feature, so that they are updated correctly.
2682 return f2fs_enable_quota_files(sbi, readonly);
2685 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
2689 f2fs_quota_off_umount(sbi->sb);
2691 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
2692 clear_sbi_flag(sbi, SBI_IS_WRITABLE);
2693 sbi->sb->s_flags |= SB_RDONLY;
2697 /* Read data from quotafile */
2698 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2699 size_t len, loff_t off)
2701 struct inode *inode = sb_dqopt(sb)->files[type];
2702 struct address_space *mapping = inode->i_mapping;
2705 loff_t i_size = i_size_read(inode);
2710 if (off + len > i_size)
2713 while (toread > 0) {
2714 struct folio *folio;
2718 folio = mapping_read_folio_gfp(mapping, off >> PAGE_SHIFT,
2720 if (IS_ERR(folio)) {
2721 if (PTR_ERR(folio) == -ENOMEM) {
2722 memalloc_retry_wait(GFP_NOFS);
2725 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2726 return PTR_ERR(folio);
2728 offset = offset_in_folio(folio, off);
2729 tocopy = min(folio_size(folio) - offset, toread);
2733 if (unlikely(folio->mapping != mapping)) {
2734 f2fs_folio_put(folio, true);
2739 * should never happen, just leave f2fs_bug_on() here to catch
2740 * any potential bug.
2742 f2fs_bug_on(F2FS_SB(sb), !folio_test_uptodate(folio));
2744 memcpy_from_folio(data, folio, offset, tocopy);
2745 f2fs_folio_put(folio, true);
2754 /* Write to quotafile */
2755 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2756 const char *data, size_t len, loff_t off)
2758 struct inode *inode = sb_dqopt(sb)->files[type];
2759 struct address_space *mapping = inode->i_mapping;
2760 const struct address_space_operations *a_ops = mapping->a_ops;
2761 int offset = off & (sb->s_blocksize - 1);
2762 size_t towrite = len;
2763 struct folio *folio;
2764 void *fsdata = NULL;
2768 while (towrite > 0) {
2769 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2772 err = a_ops->write_begin(NULL, mapping, off, tocopy,
2774 if (unlikely(err)) {
2775 if (err == -ENOMEM) {
2776 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2779 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2783 memcpy_to_folio(folio, offset_in_folio(folio, off), data, tocopy);
2785 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2796 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2797 f2fs_mark_inode_dirty_sync(inode, false);
2798 return len - towrite;
2801 int f2fs_dquot_initialize(struct inode *inode)
2803 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
2806 return dquot_initialize(inode);
2809 static struct dquot __rcu **f2fs_get_dquots(struct inode *inode)
2811 return F2FS_I(inode)->i_dquot;
2814 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2816 return &F2FS_I(inode)->i_reserved_quota;
2819 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2821 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2822 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2826 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2827 F2FS_OPTION(sbi).s_jquota_fmt, type);
2830 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2835 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2836 err = f2fs_enable_quotas(sbi->sb);
2838 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2844 for (i = 0; i < MAXQUOTAS; i++) {
2845 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2846 err = f2fs_quota_on_mount(sbi, i);
2851 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2858 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2861 struct inode *qf_inode;
2862 unsigned long qf_inum;
2863 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
2866 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2868 qf_inum = f2fs_qf_ino(sb, type);
2872 qf_inode = f2fs_iget(sb, qf_inum);
2873 if (IS_ERR(qf_inode)) {
2874 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2875 return PTR_ERR(qf_inode);
2878 /* Don't account quota for quota files to avoid recursion */
2879 inode_lock(qf_inode);
2880 qf_inode->i_flags |= S_NOQUOTA;
2882 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
2883 F2FS_I(qf_inode)->i_flags |= qf_flag;
2884 f2fs_set_inode_flags(qf_inode);
2886 inode_unlock(qf_inode);
2888 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2893 static int f2fs_enable_quotas(struct super_block *sb)
2895 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2897 unsigned long qf_inum;
2898 bool quota_mopt[MAXQUOTAS] = {
2899 test_opt(sbi, USRQUOTA),
2900 test_opt(sbi, GRPQUOTA),
2901 test_opt(sbi, PRJQUOTA),
2904 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2905 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2909 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2911 for (type = 0; type < MAXQUOTAS; type++) {
2912 qf_inum = f2fs_qf_ino(sb, type);
2914 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2915 DQUOT_USAGE_ENABLED |
2916 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2918 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2920 for (type--; type >= 0; type--)
2921 dquot_quota_off(sb, type);
2922 set_sbi_flag(F2FS_SB(sb),
2923 SBI_QUOTA_NEED_REPAIR);
2931 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2933 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2934 struct address_space *mapping = dqopt->files[type]->i_mapping;
2937 ret = dquot_writeback_dquots(sbi->sb, type);
2941 ret = filemap_fdatawrite(mapping);
2945 /* if we are using journalled quota */
2946 if (is_journalled_quota(sbi))
2949 ret = filemap_fdatawait(mapping);
2951 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2954 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2958 int f2fs_do_quota_sync(struct super_block *sb, int type)
2960 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2961 struct quota_info *dqopt = sb_dqopt(sb);
2966 * Now when everything is written we can discard the pagecache so
2967 * that userspace sees the changes.
2969 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2971 if (type != -1 && cnt != type)
2974 if (!sb_has_quota_active(sb, cnt))
2977 if (!f2fs_sb_has_quota_ino(sbi))
2978 inode_lock(dqopt->files[cnt]);
2983 * f2fs_down_read(quota_sem)
2984 * dquot_writeback_dquots()
2987 * f2fs_down_read(quota_sem)
2990 f2fs_down_read(&sbi->quota_sem);
2992 ret = f2fs_quota_sync_file(sbi, cnt);
2994 f2fs_up_read(&sbi->quota_sem);
2995 f2fs_unlock_op(sbi);
2997 if (!f2fs_sb_has_quota_ino(sbi))
2998 inode_unlock(dqopt->files[cnt]);
3006 static int f2fs_quota_sync(struct super_block *sb, int type)
3010 F2FS_SB(sb)->umount_lock_holder = current;
3011 ret = f2fs_do_quota_sync(sb, type);
3012 F2FS_SB(sb)->umount_lock_holder = NULL;
3016 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
3017 const struct path *path)
3019 struct inode *inode;
3022 /* if quota sysfile exists, deny enabling quota with specific file */
3023 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
3024 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
3028 if (path->dentry->d_sb != sb)
3031 F2FS_SB(sb)->umount_lock_holder = current;
3033 err = f2fs_do_quota_sync(sb, type);
3037 inode = d_inode(path->dentry);
3039 err = filemap_fdatawrite(inode->i_mapping);
3043 err = filemap_fdatawait(inode->i_mapping);
3047 err = dquot_quota_on(sb, type, format_id, path);
3052 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
3053 f2fs_set_inode_flags(inode);
3054 inode_unlock(inode);
3055 f2fs_mark_inode_dirty_sync(inode, false);
3057 F2FS_SB(sb)->umount_lock_holder = NULL;
3061 static int __f2fs_quota_off(struct super_block *sb, int type)
3063 struct inode *inode = sb_dqopt(sb)->files[type];
3066 if (!inode || !igrab(inode))
3067 return dquot_quota_off(sb, type);
3069 err = f2fs_do_quota_sync(sb, type);
3073 err = dquot_quota_off(sb, type);
3074 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
3078 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
3079 f2fs_set_inode_flags(inode);
3080 inode_unlock(inode);
3081 f2fs_mark_inode_dirty_sync(inode, false);
3087 static int f2fs_quota_off(struct super_block *sb, int type)
3089 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3092 F2FS_SB(sb)->umount_lock_holder = current;
3094 err = __f2fs_quota_off(sb, type);
3097 * quotactl can shutdown journalled quota, result in inconsistence
3098 * between quota record and fs data by following updates, tag the
3099 * flag to let fsck be aware of it.
3101 if (is_journalled_quota(sbi))
3102 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3104 F2FS_SB(sb)->umount_lock_holder = NULL;
3109 void f2fs_quota_off_umount(struct super_block *sb)
3114 for (type = 0; type < MAXQUOTAS; type++) {
3115 err = __f2fs_quota_off(sb, type);
3117 int ret = dquot_quota_off(sb, type);
3119 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
3121 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3125 * In case of checkpoint=disable, we must flush quota blocks.
3126 * This can cause NULL exception for node_inode in end_io, since
3127 * put_super already dropped it.
3129 sync_filesystem(sb);
3132 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
3134 struct quota_info *dqopt = sb_dqopt(sb);
3137 for (type = 0; type < MAXQUOTAS; type++) {
3138 if (!dqopt->files[type])
3140 f2fs_inode_synced(dqopt->files[type]);
3144 static int f2fs_dquot_commit(struct dquot *dquot)
3146 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3149 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
3150 ret = dquot_commit(dquot);
3152 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3153 f2fs_up_read(&sbi->quota_sem);
3157 static int f2fs_dquot_acquire(struct dquot *dquot)
3159 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3162 f2fs_down_read(&sbi->quota_sem);
3163 ret = dquot_acquire(dquot);
3165 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3166 f2fs_up_read(&sbi->quota_sem);
3170 static int f2fs_dquot_release(struct dquot *dquot)
3172 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3173 int ret = dquot_release(dquot);
3176 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3180 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
3182 struct super_block *sb = dquot->dq_sb;
3183 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3184 int ret = dquot_mark_dquot_dirty(dquot);
3186 /* if we are using journalled quota */
3187 if (is_journalled_quota(sbi))
3188 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
3193 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
3195 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3196 int ret = dquot_commit_info(sb, type);
3199 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3203 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
3205 *projid = F2FS_I(inode)->i_projid;
3209 static const struct dquot_operations f2fs_quota_operations = {
3210 .get_reserved_space = f2fs_get_reserved_space,
3211 .write_dquot = f2fs_dquot_commit,
3212 .acquire_dquot = f2fs_dquot_acquire,
3213 .release_dquot = f2fs_dquot_release,
3214 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
3215 .write_info = f2fs_dquot_commit_info,
3216 .alloc_dquot = dquot_alloc,
3217 .destroy_dquot = dquot_destroy,
3218 .get_projid = f2fs_get_projid,
3219 .get_next_id = dquot_get_next_id,
3222 static const struct quotactl_ops f2fs_quotactl_ops = {
3223 .quota_on = f2fs_quota_on,
3224 .quota_off = f2fs_quota_off,
3225 .quota_sync = f2fs_quota_sync,
3226 .get_state = dquot_get_state,
3227 .set_info = dquot_set_dqinfo,
3228 .get_dqblk = dquot_get_dqblk,
3229 .set_dqblk = dquot_set_dqblk,
3230 .get_nextdqblk = dquot_get_next_dqblk,
3233 int f2fs_dquot_initialize(struct inode *inode)
3238 int f2fs_do_quota_sync(struct super_block *sb, int type)
3243 void f2fs_quota_off_umount(struct super_block *sb)
3248 static const struct super_operations f2fs_sops = {
3249 .alloc_inode = f2fs_alloc_inode,
3250 .free_inode = f2fs_free_inode,
3251 .drop_inode = f2fs_drop_inode,
3252 .write_inode = f2fs_write_inode,
3253 .dirty_inode = f2fs_dirty_inode,
3254 .show_options = f2fs_show_options,
3256 .quota_read = f2fs_quota_read,
3257 .quota_write = f2fs_quota_write,
3258 .get_dquots = f2fs_get_dquots,
3260 .evict_inode = f2fs_evict_inode,
3261 .put_super = f2fs_put_super,
3262 .sync_fs = f2fs_sync_fs,
3263 .freeze_fs = f2fs_freeze,
3264 .unfreeze_fs = f2fs_unfreeze,
3265 .statfs = f2fs_statfs,
3266 .remount_fs = f2fs_remount,
3267 .shutdown = f2fs_shutdown,
3270 #ifdef CONFIG_FS_ENCRYPTION
3271 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
3273 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3274 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3278 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
3281 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3284 * Encrypting the root directory is not allowed because fsck
3285 * expects lost+found directory to exist and remain unencrypted
3286 * if LOST_FOUND feature is enabled.
3289 if (f2fs_sb_has_lost_found(sbi) &&
3290 inode->i_ino == F2FS_ROOT_INO(sbi))
3293 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3294 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3295 ctx, len, fs_data, XATTR_CREATE);
3298 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
3300 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
3303 static bool f2fs_has_stable_inodes(struct super_block *sb)
3308 static struct block_device **f2fs_get_devices(struct super_block *sb,
3309 unsigned int *num_devs)
3311 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3312 struct block_device **devs;
3315 if (!f2fs_is_multi_device(sbi))
3318 devs = kmalloc_array(sbi->s_ndevs, sizeof(*devs), GFP_KERNEL);
3320 return ERR_PTR(-ENOMEM);
3322 for (i = 0; i < sbi->s_ndevs; i++)
3323 devs[i] = FDEV(i).bdev;
3324 *num_devs = sbi->s_ndevs;
3328 static const struct fscrypt_operations f2fs_cryptops = {
3329 .needs_bounce_pages = 1,
3330 .has_32bit_inodes = 1,
3331 .supports_subblock_data_units = 1,
3332 .legacy_key_prefix = "f2fs:",
3333 .get_context = f2fs_get_context,
3334 .set_context = f2fs_set_context,
3335 .get_dummy_policy = f2fs_get_dummy_policy,
3336 .empty_dir = f2fs_empty_dir,
3337 .has_stable_inodes = f2fs_has_stable_inodes,
3338 .get_devices = f2fs_get_devices,
3342 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
3343 u64 ino, u32 generation)
3345 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3346 struct inode *inode;
3348 if (f2fs_check_nid_range(sbi, ino))
3349 return ERR_PTR(-ESTALE);
3352 * f2fs_iget isn't quite right if the inode is currently unallocated!
3353 * However f2fs_iget currently does appropriate checks to handle stale
3354 * inodes so everything is OK.
3356 inode = f2fs_iget(sb, ino);
3358 return ERR_CAST(inode);
3359 if (unlikely(generation && inode->i_generation != generation)) {
3360 /* we didn't find the right inode.. */
3362 return ERR_PTR(-ESTALE);
3367 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
3368 int fh_len, int fh_type)
3370 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3371 f2fs_nfs_get_inode);
3374 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
3375 int fh_len, int fh_type)
3377 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3378 f2fs_nfs_get_inode);
3381 static const struct export_operations f2fs_export_ops = {
3382 .encode_fh = generic_encode_ino32_fh,
3383 .fh_to_dentry = f2fs_fh_to_dentry,
3384 .fh_to_parent = f2fs_fh_to_parent,
3385 .get_parent = f2fs_get_parent,
3388 loff_t max_file_blocks(struct inode *inode)
3394 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
3395 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
3396 * space in inode.i_addr, it will be more safe to reassign
3400 if (inode && f2fs_compressed_file(inode))
3401 leaf_count = ADDRS_PER_BLOCK(inode);
3403 leaf_count = DEF_ADDRS_PER_BLOCK;
3405 /* two direct node blocks */
3406 result += (leaf_count * 2);
3408 /* two indirect node blocks */
3409 leaf_count *= NIDS_PER_BLOCK;
3410 result += (leaf_count * 2);
3412 /* one double indirect node block */
3413 leaf_count *= NIDS_PER_BLOCK;
3414 result += leaf_count;
3417 * For compatibility with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{64,32} with
3418 * a 4K crypto data unit, we must restrict the max filesize to what can
3419 * fit within U32_MAX + 1 data units.
3422 result = umin(result, F2FS_BYTES_TO_BLK(((loff_t)U32_MAX + 1) * 4096));
3427 static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio,
3428 pgoff_t index, bool update)
3431 /* it's rare case, we can do fua all the time */
3432 blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA;
3436 folio_wait_writeback(folio);
3438 memcpy(F2FS_SUPER_BLOCK(folio, index), F2FS_RAW_SUPER(sbi),
3439 sizeof(struct f2fs_super_block));
3440 folio_mark_dirty(folio);
3441 folio_clear_dirty_for_io(folio);
3442 folio_start_writeback(folio);
3443 folio_unlock(folio);
3445 bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS);
3447 /* it doesn't need to set crypto context for superblock update */
3448 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio->index);
3450 if (!bio_add_folio(bio, folio, folio_size(folio), 0))
3451 f2fs_bug_on(sbi, 1);
3453 ret = submit_bio_wait(bio);
3454 folio_end_writeback(folio);
3459 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
3460 struct folio *folio, pgoff_t index)
3462 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
3463 struct super_block *sb = sbi->sb;
3464 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3465 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
3466 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
3467 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
3468 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
3469 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3470 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
3471 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
3472 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
3473 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
3474 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3475 u32 segment_count = le32_to_cpu(raw_super->segment_count);
3476 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3477 u64 main_end_blkaddr = main_blkaddr +
3478 ((u64)segment_count_main << log_blocks_per_seg);
3479 u64 seg_end_blkaddr = segment0_blkaddr +
3480 ((u64)segment_count << log_blocks_per_seg);
3482 if (segment0_blkaddr != cp_blkaddr) {
3483 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
3484 segment0_blkaddr, cp_blkaddr);
3488 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
3490 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
3491 cp_blkaddr, sit_blkaddr,
3492 segment_count_ckpt << log_blocks_per_seg);
3496 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
3498 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
3499 sit_blkaddr, nat_blkaddr,
3500 segment_count_sit << log_blocks_per_seg);
3504 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
3506 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
3507 nat_blkaddr, ssa_blkaddr,
3508 segment_count_nat << log_blocks_per_seg);
3512 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
3514 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
3515 ssa_blkaddr, main_blkaddr,
3516 segment_count_ssa << log_blocks_per_seg);
3520 if (main_end_blkaddr > seg_end_blkaddr) {
3521 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3522 main_blkaddr, seg_end_blkaddr,
3523 segment_count_main << log_blocks_per_seg);
3525 } else if (main_end_blkaddr < seg_end_blkaddr) {
3529 /* fix in-memory information all the time */
3530 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
3531 segment0_blkaddr) >> log_blocks_per_seg);
3533 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
3534 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3537 err = __f2fs_commit_super(sbi, folio, index, false);
3538 res = err ? "failed" : "done";
3540 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3541 res, main_blkaddr, seg_end_blkaddr,
3542 segment_count_main << log_blocks_per_seg);
3549 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
3550 struct folio *folio, pgoff_t index)
3552 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
3553 block_t total_sections, blocks_per_seg;
3554 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
3555 size_t crc_offset = 0;
3558 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
3559 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
3560 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
3564 /* Check checksum_offset and crc in superblock */
3565 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
3566 crc_offset = le32_to_cpu(raw_super->checksum_offset);
3568 offsetof(struct f2fs_super_block, crc)) {
3569 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
3571 return -EFSCORRUPTED;
3573 crc = le32_to_cpu(raw_super->crc);
3574 if (crc != f2fs_crc32(raw_super, crc_offset)) {
3575 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
3576 return -EFSCORRUPTED;
3580 /* only support block_size equals to PAGE_SIZE */
3581 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3582 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3583 le32_to_cpu(raw_super->log_blocksize),
3585 return -EFSCORRUPTED;
3588 /* check log blocks per segment */
3589 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
3590 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
3591 le32_to_cpu(raw_super->log_blocks_per_seg));
3592 return -EFSCORRUPTED;
3595 /* Currently, support 512/1024/2048/4096/16K bytes sector size */
3596 if (le32_to_cpu(raw_super->log_sectorsize) >
3597 F2FS_MAX_LOG_SECTOR_SIZE ||
3598 le32_to_cpu(raw_super->log_sectorsize) <
3599 F2FS_MIN_LOG_SECTOR_SIZE) {
3600 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3601 le32_to_cpu(raw_super->log_sectorsize));
3602 return -EFSCORRUPTED;
3604 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3605 le32_to_cpu(raw_super->log_sectorsize) !=
3606 F2FS_MAX_LOG_SECTOR_SIZE) {
3607 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3608 le32_to_cpu(raw_super->log_sectors_per_block),
3609 le32_to_cpu(raw_super->log_sectorsize));
3610 return -EFSCORRUPTED;
3613 segment_count = le32_to_cpu(raw_super->segment_count);
3614 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3615 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3616 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3617 total_sections = le32_to_cpu(raw_super->section_count);
3619 /* blocks_per_seg should be 512, given the above check */
3620 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
3622 if (segment_count > F2FS_MAX_SEGMENT ||
3623 segment_count < F2FS_MIN_SEGMENTS) {
3624 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
3625 return -EFSCORRUPTED;
3628 if (total_sections > segment_count_main || total_sections < 1 ||
3629 segs_per_sec > segment_count || !segs_per_sec) {
3630 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3631 segment_count, total_sections, segs_per_sec);
3632 return -EFSCORRUPTED;
3635 if (segment_count_main != total_sections * segs_per_sec) {
3636 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3637 segment_count_main, total_sections, segs_per_sec);
3638 return -EFSCORRUPTED;
3641 if ((segment_count / segs_per_sec) < total_sections) {
3642 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3643 segment_count, segs_per_sec, total_sections);
3644 return -EFSCORRUPTED;
3647 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
3648 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3649 segment_count, le64_to_cpu(raw_super->block_count));
3650 return -EFSCORRUPTED;
3653 if (RDEV(0).path[0]) {
3654 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3657 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3658 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3661 if (segment_count != dev_seg_count) {
3662 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3663 segment_count, dev_seg_count);
3664 return -EFSCORRUPTED;
3667 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3668 !bdev_is_zoned(sbi->sb->s_bdev)) {
3669 f2fs_info(sbi, "Zoned block device path is missing");
3670 return -EFSCORRUPTED;
3674 if (secs_per_zone > total_sections || !secs_per_zone) {
3675 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3676 secs_per_zone, total_sections);
3677 return -EFSCORRUPTED;
3679 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3680 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3681 (le32_to_cpu(raw_super->extension_count) +
3682 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
3683 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3684 le32_to_cpu(raw_super->extension_count),
3685 raw_super->hot_ext_count,
3686 F2FS_MAX_EXTENSION);
3687 return -EFSCORRUPTED;
3690 if (le32_to_cpu(raw_super->cp_payload) >=
3691 (blocks_per_seg - F2FS_CP_PACKS -
3692 NR_CURSEG_PERSIST_TYPE)) {
3693 f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
3694 le32_to_cpu(raw_super->cp_payload),
3695 blocks_per_seg - F2FS_CP_PACKS -
3696 NR_CURSEG_PERSIST_TYPE);
3697 return -EFSCORRUPTED;
3700 /* check reserved ino info */
3701 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3702 le32_to_cpu(raw_super->meta_ino) != 2 ||
3703 le32_to_cpu(raw_super->root_ino) != 3) {
3704 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3705 le32_to_cpu(raw_super->node_ino),
3706 le32_to_cpu(raw_super->meta_ino),
3707 le32_to_cpu(raw_super->root_ino));
3708 return -EFSCORRUPTED;
3711 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
3712 if (sanity_check_area_boundary(sbi, folio, index))
3713 return -EFSCORRUPTED;
3718 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
3720 unsigned int total, fsmeta;
3721 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3722 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3723 unsigned int ovp_segments, reserved_segments;
3724 unsigned int main_segs, blocks_per_seg;
3725 unsigned int sit_segs, nat_segs;
3726 unsigned int sit_bitmap_size, nat_bitmap_size;
3727 unsigned int log_blocks_per_seg;
3728 unsigned int segment_count_main;
3729 unsigned int cp_pack_start_sum, cp_payload;
3730 block_t user_block_count, valid_user_blocks;
3731 block_t avail_node_count, valid_node_count;
3732 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
3733 unsigned int sit_blk_cnt;
3736 total = le32_to_cpu(raw_super->segment_count);
3737 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
3738 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3740 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3742 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3743 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3745 if (unlikely(fsmeta >= total))
3748 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3749 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3751 if (!f2fs_sb_has_readonly(sbi) &&
3752 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
3753 ovp_segments == 0 || reserved_segments == 0)) {
3754 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
3757 user_block_count = le64_to_cpu(ckpt->user_block_count);
3758 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3759 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
3760 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3761 if (!user_block_count || user_block_count >=
3762 segment_count_main << log_blocks_per_seg) {
3763 f2fs_err(sbi, "Wrong user_block_count: %u",
3768 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3769 if (valid_user_blocks > user_block_count) {
3770 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3771 valid_user_blocks, user_block_count);
3775 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
3776 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
3777 if (valid_node_count > avail_node_count) {
3778 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3779 valid_node_count, avail_node_count);
3783 main_segs = le32_to_cpu(raw_super->segment_count_main);
3784 blocks_per_seg = BLKS_PER_SEG(sbi);
3786 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3787 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3788 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3791 if (f2fs_sb_has_readonly(sbi))
3794 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3795 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3796 le32_to_cpu(ckpt->cur_node_segno[j])) {
3797 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3799 le32_to_cpu(ckpt->cur_node_segno[i]));
3805 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3806 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3807 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3810 if (f2fs_sb_has_readonly(sbi))
3813 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3814 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3815 le32_to_cpu(ckpt->cur_data_segno[j])) {
3816 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3818 le32_to_cpu(ckpt->cur_data_segno[i]));
3823 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3824 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3825 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3826 le32_to_cpu(ckpt->cur_data_segno[j])) {
3827 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3829 le32_to_cpu(ckpt->cur_node_segno[i]));
3835 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3836 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3838 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3839 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3840 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3841 sit_bitmap_size, nat_bitmap_size);
3845 sit_blk_cnt = DIV_ROUND_UP(main_segs, SIT_ENTRY_PER_BLOCK);
3846 if (sit_bitmap_size * 8 < sit_blk_cnt) {
3847 f2fs_err(sbi, "Wrong bitmap size: sit: %u, sit_blk_cnt:%u",
3848 sit_bitmap_size, sit_blk_cnt);
3852 cp_pack_start_sum = __start_sum_addr(sbi);
3853 cp_payload = __cp_payload(sbi);
3854 if (cp_pack_start_sum < cp_payload + 1 ||
3855 cp_pack_start_sum > blocks_per_seg - 1 -
3856 NR_CURSEG_PERSIST_TYPE) {
3857 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3862 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3863 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3864 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3865 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3866 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3867 le32_to_cpu(ckpt->checksum_offset));
3871 nat_blocks = nat_segs << log_blocks_per_seg;
3872 nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
3873 nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3874 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
3875 (cp_payload + F2FS_CP_PACKS +
3876 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
3877 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
3878 cp_payload, nat_bits_blocks);
3882 if (unlikely(f2fs_cp_error(sbi))) {
3883 f2fs_err(sbi, "A bug case: need to run fsck");
3889 static void init_sb_info(struct f2fs_sb_info *sbi)
3891 struct f2fs_super_block *raw_super = sbi->raw_super;
3894 sbi->log_sectors_per_block =
3895 le32_to_cpu(raw_super->log_sectors_per_block);
3896 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3897 sbi->blocksize = BIT(sbi->log_blocksize);
3898 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3899 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
3900 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3901 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3902 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3903 sbi->total_node_count = SEGS_TO_BLKS(sbi,
3904 ((le32_to_cpu(raw_super->segment_count_nat) / 2) *
3905 NAT_ENTRY_PER_BLOCK));
3906 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3907 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3908 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3909 sbi->cur_victim_sec = NULL_SECNO;
3910 sbi->gc_mode = GC_NORMAL;
3911 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3912 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3913 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3914 sbi->migration_granularity = SEGS_PER_SEC(sbi);
3915 sbi->migration_window_granularity = f2fs_sb_has_blkzoned(sbi) ?
3916 DEF_MIGRATION_WINDOW_GRANULARITY_ZONED : SEGS_PER_SEC(sbi);
3917 sbi->seq_file_ra_mul = MIN_RA_MUL;
3918 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
3919 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
3920 spin_lock_init(&sbi->gc_remaining_trials_lock);
3921 atomic64_set(&sbi->current_atomic_write, 0);
3923 sbi->dir_level = DEF_DIR_LEVEL;
3924 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3925 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3926 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3927 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3928 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3929 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3930 DEF_UMOUNT_DISCARD_TIMEOUT;
3931 clear_sbi_flag(sbi, SBI_NEED_FSCK);
3933 for (i = 0; i < NR_COUNT_TYPE; i++)
3934 atomic_set(&sbi->nr_pages[i], 0);
3936 for (i = 0; i < META; i++)
3937 atomic_set(&sbi->wb_sync_req[i], 0);
3939 INIT_LIST_HEAD(&sbi->s_list);
3940 mutex_init(&sbi->umount_mutex);
3941 init_f2fs_rwsem(&sbi->io_order_lock);
3942 spin_lock_init(&sbi->cp_lock);
3944 sbi->dirty_device = 0;
3945 spin_lock_init(&sbi->dev_lock);
3947 init_f2fs_rwsem(&sbi->sb_lock);
3948 init_f2fs_rwsem(&sbi->pin_sem);
3951 static int init_percpu_info(struct f2fs_sb_info *sbi)
3955 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3959 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
3961 goto err_valid_block;
3963 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3966 goto err_node_block;
3970 percpu_counter_destroy(&sbi->rf_node_block_count);
3972 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3976 #ifdef CONFIG_BLK_DEV_ZONED
3978 struct f2fs_report_zones_args {
3979 struct f2fs_sb_info *sbi;
3980 struct f2fs_dev_info *dev;
3983 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3986 struct f2fs_report_zones_args *rz_args = data;
3987 block_t unusable_blocks = (zone->len - zone->capacity) >>
3988 F2FS_LOG_SECTORS_PER_BLOCK;
3990 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3993 set_bit(idx, rz_args->dev->blkz_seq);
3994 if (!rz_args->sbi->unusable_blocks_per_sec) {
3995 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
3998 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
3999 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
4005 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
4007 struct block_device *bdev = FDEV(devi).bdev;
4008 sector_t nr_sectors = bdev_nr_sectors(bdev);
4009 struct f2fs_report_zones_args rep_zone_arg;
4011 unsigned int max_open_zones;
4014 if (!f2fs_sb_has_blkzoned(sbi))
4017 if (bdev_is_zoned(FDEV(devi).bdev)) {
4018 max_open_zones = bdev_max_open_zones(bdev);
4019 if (max_open_zones && (max_open_zones < sbi->max_open_zones))
4020 sbi->max_open_zones = max_open_zones;
4021 if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
4023 "zoned: max open zones %u is too small, need at least %u open zones",
4024 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
4029 zone_sectors = bdev_zone_sectors(bdev);
4030 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
4031 SECTOR_TO_BLOCK(zone_sectors))
4033 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
4034 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
4035 sbi->blocks_per_blkz);
4036 if (nr_sectors & (zone_sectors - 1))
4037 FDEV(devi).nr_blkz++;
4039 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
4040 BITS_TO_LONGS(FDEV(devi).nr_blkz)
4041 * sizeof(unsigned long),
4043 if (!FDEV(devi).blkz_seq)
4046 rep_zone_arg.sbi = sbi;
4047 rep_zone_arg.dev = &FDEV(devi);
4049 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
4058 * Read f2fs raw super block.
4059 * Because we have two copies of super block, so read both of them
4060 * to get the first valid one. If any one of them is broken, we pass
4061 * them recovery flag back to the caller.
4063 static int read_raw_super_block(struct f2fs_sb_info *sbi,
4064 struct f2fs_super_block **raw_super,
4065 int *valid_super_block, int *recovery)
4067 struct super_block *sb = sbi->sb;
4069 struct folio *folio;
4070 struct f2fs_super_block *super;
4073 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
4077 for (block = 0; block < 2; block++) {
4078 folio = read_mapping_folio(sb->s_bdev->bd_mapping, block, NULL);
4079 if (IS_ERR(folio)) {
4080 f2fs_err(sbi, "Unable to read %dth superblock",
4082 err = PTR_ERR(folio);
4087 /* sanity checking of raw super */
4088 err = sanity_check_raw_super(sbi, folio, block);
4090 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
4098 memcpy(super, F2FS_SUPER_BLOCK(folio, block),
4100 *valid_super_block = block;
4106 /* No valid superblock */
4115 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
4117 struct folio *folio;
4122 if ((recover && f2fs_readonly(sbi->sb)) ||
4123 f2fs_hw_is_readonly(sbi)) {
4124 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
4128 /* we should update superblock crc here */
4129 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
4130 crc = f2fs_crc32(F2FS_RAW_SUPER(sbi),
4131 offsetof(struct f2fs_super_block, crc));
4132 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
4135 /* write back-up superblock first */
4136 index = sbi->valid_super_block ? 0 : 1;
4137 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
4139 return PTR_ERR(folio);
4140 err = __f2fs_commit_super(sbi, folio, index, true);
4143 /* if we are in recovery path, skip writing valid superblock */
4147 /* write current valid superblock */
4148 index = sbi->valid_super_block;
4149 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
4151 return PTR_ERR(folio);
4152 err = __f2fs_commit_super(sbi, folio, index, true);
4157 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
4159 unsigned long flags;
4161 spin_lock_irqsave(&sbi->error_lock, flags);
4162 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
4163 sbi->stop_reason[reason]++;
4164 spin_unlock_irqrestore(&sbi->error_lock, flags);
4167 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
4169 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4170 unsigned long flags;
4173 f2fs_down_write(&sbi->sb_lock);
4175 spin_lock_irqsave(&sbi->error_lock, flags);
4176 if (sbi->error_dirty) {
4177 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4179 sbi->error_dirty = false;
4181 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
4182 spin_unlock_irqrestore(&sbi->error_lock, flags);
4184 err = f2fs_commit_super(sbi, false);
4186 f2fs_up_write(&sbi->sb_lock);
4188 f2fs_err_ratelimited(sbi,
4189 "f2fs_commit_super fails to record stop_reason, err:%d",
4193 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
4195 unsigned long flags;
4197 spin_lock_irqsave(&sbi->error_lock, flags);
4198 if (!test_bit(flag, (unsigned long *)sbi->errors)) {
4199 set_bit(flag, (unsigned long *)sbi->errors);
4200 sbi->error_dirty = true;
4202 spin_unlock_irqrestore(&sbi->error_lock, flags);
4205 static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
4207 unsigned long flags;
4208 bool need_update = false;
4210 spin_lock_irqsave(&sbi->error_lock, flags);
4211 if (sbi->error_dirty) {
4212 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4214 sbi->error_dirty = false;
4217 spin_unlock_irqrestore(&sbi->error_lock, flags);
4222 static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error)
4226 f2fs_down_write(&sbi->sb_lock);
4228 if (!f2fs_update_errors(sbi))
4231 err = f2fs_commit_super(sbi, false);
4233 f2fs_err_ratelimited(sbi,
4234 "f2fs_commit_super fails to record errors:%u, err:%d",
4237 f2fs_up_write(&sbi->sb_lock);
4240 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4242 f2fs_save_errors(sbi, error);
4243 f2fs_record_errors(sbi, error);
4246 void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error)
4248 f2fs_save_errors(sbi, error);
4250 if (!sbi->error_dirty)
4252 if (!test_bit(error, (unsigned long *)sbi->errors))
4254 schedule_work(&sbi->s_error_work);
4257 static bool system_going_down(void)
4259 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4260 || system_state == SYSTEM_RESTART;
4263 void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason)
4265 struct super_block *sb = sbi->sb;
4266 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
4267 bool continue_fs = !shutdown &&
4268 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
4270 set_ckpt_flags(sbi, CP_ERROR_FLAG);
4272 if (!f2fs_hw_is_readonly(sbi)) {
4273 save_stop_reason(sbi, reason);
4276 * always create an asynchronous task to record stop_reason
4277 * in order to avoid potential deadlock when running into
4278 * f2fs_record_stop_reason() synchronously.
4280 schedule_work(&sbi->s_error_work);
4284 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4285 * could panic during 'reboot -f' as the underlying device got already
4288 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
4289 !shutdown && !system_going_down() &&
4290 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
4291 panic("F2FS-fs (device %s): panic forced after error\n",
4295 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
4300 * Continue filesystem operators if errors=continue. Should not set
4301 * RO by shutdown, since RO bypasses thaw_super which can hang the
4304 if (continue_fs || f2fs_readonly(sb) || shutdown) {
4305 f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason);
4309 f2fs_warn(sbi, "Remounting filesystem read-only");
4312 * We have already set CP_ERROR_FLAG flag to stop all updates
4313 * to filesystem, so it doesn't need to set SB_RDONLY flag here
4314 * because the flag should be set covered w/ sb->s_umount semaphore
4315 * via remount procedure, otherwise, it will confuse code like
4316 * freeze_super() which will lead to deadlocks and other problems.
4320 static void f2fs_record_error_work(struct work_struct *work)
4322 struct f2fs_sb_info *sbi = container_of(work,
4323 struct f2fs_sb_info, s_error_work);
4325 f2fs_record_stop_reason(sbi);
4328 static inline unsigned int get_first_seq_zone_segno(struct f2fs_sb_info *sbi)
4330 #ifdef CONFIG_BLK_DEV_ZONED
4331 unsigned int zoneno, total_zones;
4334 if (!f2fs_sb_has_blkzoned(sbi))
4337 for (devi = 0; devi < sbi->s_ndevs; devi++) {
4338 if (!bdev_is_zoned(FDEV(devi).bdev))
4341 total_zones = GET_ZONE_FROM_SEG(sbi, FDEV(devi).total_segments);
4343 for (zoneno = 0; zoneno < total_zones; zoneno++) {
4344 unsigned int segs, blks;
4346 if (!f2fs_zone_is_seq(sbi, devi, zoneno))
4349 segs = GET_SEG_FROM_SEC(sbi,
4350 zoneno * sbi->secs_per_zone);
4351 blks = SEGS_TO_BLKS(sbi, segs);
4352 return GET_SEGNO(sbi, FDEV(devi).start_blk + blks);
4359 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
4361 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4362 unsigned int max_devices = MAX_DEVICES;
4363 unsigned int logical_blksize;
4364 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
4367 /* Initialize single device information */
4368 if (!RDEV(0).path[0]) {
4369 if (!bdev_is_zoned(sbi->sb->s_bdev))
4375 * Initialize multiple devices information, or single
4376 * zoned block device information.
4378 sbi->devs = f2fs_kzalloc(sbi,
4379 array_size(max_devices,
4380 sizeof(struct f2fs_dev_info)),
4385 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
4386 sbi->aligned_blksize = true;
4387 #ifdef CONFIG_BLK_DEV_ZONED
4388 sbi->max_open_zones = UINT_MAX;
4389 sbi->blkzone_alloc_policy = BLKZONE_ALLOC_PRIOR_SEQ;
4392 for (i = 0; i < max_devices; i++) {
4393 if (max_devices == 1) {
4394 FDEV(i).total_segments =
4395 le32_to_cpu(raw_super->segment_count_main);
4396 FDEV(i).start_blk = 0;
4397 FDEV(i).end_blk = FDEV(i).total_segments *
4402 FDEV(0).bdev_file = sbi->sb->s_bdev_file;
4403 else if (!RDEV(i).path[0])
4406 if (max_devices > 1) {
4407 /* Multi-device mount */
4408 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
4409 FDEV(i).total_segments =
4410 le32_to_cpu(RDEV(i).total_segments);
4412 FDEV(i).start_blk = 0;
4413 FDEV(i).end_blk = FDEV(i).start_blk +
4415 FDEV(i).total_segments) - 1 +
4416 le32_to_cpu(raw_super->segment0_blkaddr);
4418 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
4419 FDEV(i).end_blk = FDEV(i).start_blk +
4421 FDEV(i).total_segments) - 1;
4422 FDEV(i).bdev_file = bdev_file_open_by_path(
4423 FDEV(i).path, mode, sbi->sb, NULL);
4426 if (IS_ERR(FDEV(i).bdev_file))
4427 return PTR_ERR(FDEV(i).bdev_file);
4429 FDEV(i).bdev = file_bdev(FDEV(i).bdev_file);
4430 /* to release errored devices */
4431 sbi->s_ndevs = i + 1;
4433 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
4434 sbi->aligned_blksize = false;
4436 #ifdef CONFIG_BLK_DEV_ZONED
4437 if (bdev_is_zoned(FDEV(i).bdev)) {
4438 if (!f2fs_sb_has_blkzoned(sbi)) {
4439 f2fs_err(sbi, "Zoned block device feature not enabled");
4442 if (init_blkz_info(sbi, i)) {
4443 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
4446 if (max_devices == 1)
4448 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: Host-managed)",
4450 FDEV(i).total_segments,
4451 FDEV(i).start_blk, FDEV(i).end_blk);
4455 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
4457 FDEV(i).total_segments,
4458 FDEV(i).start_blk, FDEV(i).end_blk);
4463 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
4465 #if IS_ENABLED(CONFIG_UNICODE)
4466 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
4467 const struct f2fs_sb_encodings *encoding_info;
4468 struct unicode_map *encoding;
4469 __u16 encoding_flags;
4471 encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
4472 if (!encoding_info) {
4474 "Encoding requested by superblock is unknown");
4478 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
4479 encoding = utf8_load(encoding_info->version);
4480 if (IS_ERR(encoding)) {
4482 "can't mount with superblock charset: %s-%u.%u.%u "
4483 "not supported by the kernel. flags: 0x%x.",
4484 encoding_info->name,
4485 unicode_major(encoding_info->version),
4486 unicode_minor(encoding_info->version),
4487 unicode_rev(encoding_info->version),
4489 return PTR_ERR(encoding);
4491 f2fs_info(sbi, "Using encoding defined by superblock: "
4492 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4493 unicode_major(encoding_info->version),
4494 unicode_minor(encoding_info->version),
4495 unicode_rev(encoding_info->version),
4498 sbi->sb->s_encoding = encoding;
4499 sbi->sb->s_encoding_flags = encoding_flags;
4502 if (f2fs_sb_has_casefold(sbi)) {
4503 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
4510 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
4512 /* adjust parameters according to the volume size */
4513 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
4514 if (f2fs_block_unit_discard(sbi))
4515 SM_I(sbi)->dcc_info->discard_granularity =
4516 MIN_DISCARD_GRANULARITY;
4517 if (!f2fs_lfs_mode(sbi))
4518 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
4519 BIT(F2FS_IPU_HONOR_OPU_WRITE);
4522 sbi->readdir_ra = true;
4525 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
4527 struct f2fs_sb_info *sbi;
4528 struct f2fs_super_block *raw_super;
4531 bool skip_recovery = false, need_fsck = false;
4532 char *options = NULL;
4533 int recovery, i, valid_super_block;
4534 struct curseg_info *seg_i;
4537 bool quota_enabled = false;
4543 valid_super_block = -1;
4546 /* allocate memory for f2fs-specific super block info */
4547 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
4553 /* initialize locks within allocated memory */
4554 init_f2fs_rwsem(&sbi->gc_lock);
4555 mutex_init(&sbi->writepages);
4556 init_f2fs_rwsem(&sbi->cp_global_sem);
4557 init_f2fs_rwsem(&sbi->node_write);
4558 init_f2fs_rwsem(&sbi->node_change);
4559 spin_lock_init(&sbi->stat_lock);
4560 init_f2fs_rwsem(&sbi->cp_rwsem);
4561 init_f2fs_rwsem(&sbi->quota_sem);
4562 init_waitqueue_head(&sbi->cp_wait);
4563 spin_lock_init(&sbi->error_lock);
4565 for (i = 0; i < NR_INODE_TYPE; i++) {
4566 INIT_LIST_HEAD(&sbi->inode_list[i]);
4567 spin_lock_init(&sbi->inode_lock[i]);
4569 mutex_init(&sbi->flush_lock);
4571 /* set a block size */
4572 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
4573 f2fs_err(sbi, "unable to set blocksize");
4577 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
4582 sb->s_fs_info = sbi;
4583 sbi->raw_super = raw_super;
4585 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
4586 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
4587 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
4589 /* precompute checksum seed for metadata */
4590 if (f2fs_sb_has_inode_chksum(sbi))
4591 sbi->s_chksum_seed = f2fs_chksum(~0, raw_super->uuid,
4592 sizeof(raw_super->uuid));
4594 default_options(sbi, false);
4595 /* parse mount options */
4596 options = kstrdup((const char *)data, GFP_KERNEL);
4597 if (data && !options) {
4602 err = parse_options(sbi, options, false);
4606 err = f2fs_default_check(sbi);
4610 sb->s_maxbytes = max_file_blocks(NULL) <<
4611 le32_to_cpu(raw_super->log_blocksize);
4612 sb->s_max_links = F2FS_LINK_MAX;
4614 err = f2fs_setup_casefold(sbi);
4619 sb->dq_op = &f2fs_quota_operations;
4620 sb->s_qcop = &f2fs_quotactl_ops;
4621 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4623 if (f2fs_sb_has_quota_ino(sbi)) {
4624 for (i = 0; i < MAXQUOTAS; i++) {
4625 if (f2fs_qf_ino(sbi->sb, i))
4626 sbi->nquota_files++;
4631 sb->s_op = &f2fs_sops;
4632 #ifdef CONFIG_FS_ENCRYPTION
4633 sb->s_cop = &f2fs_cryptops;
4635 #ifdef CONFIG_FS_VERITY
4636 sb->s_vop = &f2fs_verityops;
4638 sb->s_xattr = f2fs_xattr_handlers;
4639 sb->s_export_op = &f2fs_export_ops;
4640 sb->s_magic = F2FS_SUPER_MAGIC;
4641 sb->s_time_gran = 1;
4642 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4643 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
4644 if (test_opt(sbi, INLINECRYPT))
4645 sb->s_flags |= SB_INLINECRYPT;
4647 if (test_opt(sbi, LAZYTIME))
4648 sb->s_flags |= SB_LAZYTIME;
4650 sb->s_flags &= ~SB_LAZYTIME;
4652 super_set_uuid(sb, (void *) raw_super->uuid, sizeof(raw_super->uuid));
4653 super_set_sysfs_name_bdev(sb);
4654 sb->s_iflags |= SB_I_CGROUPWB;
4656 /* init f2fs-specific super block info */
4657 sbi->valid_super_block = valid_super_block;
4659 /* disallow all the data/node/meta page writes */
4660 set_sbi_flag(sbi, SBI_POR_DOING);
4662 err = f2fs_init_write_merge_io(sbi);
4668 err = f2fs_init_iostat(sbi);
4672 err = init_percpu_info(sbi);
4676 /* init per sbi slab cache */
4677 err = f2fs_init_xattr_caches(sbi);
4680 err = f2fs_init_page_array_cache(sbi);
4682 goto free_xattr_cache;
4684 /* get an inode for meta space */
4685 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
4686 if (IS_ERR(sbi->meta_inode)) {
4687 f2fs_err(sbi, "Failed to read F2FS meta data inode");
4688 err = PTR_ERR(sbi->meta_inode);
4689 goto free_page_array_cache;
4692 err = f2fs_get_valid_checkpoint(sbi);
4694 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
4695 goto free_meta_inode;
4698 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
4699 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
4700 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
4701 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4702 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
4705 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
4706 set_sbi_flag(sbi, SBI_NEED_FSCK);
4708 /* Initialize device list */
4709 err = f2fs_scan_devices(sbi);
4711 f2fs_err(sbi, "Failed to find devices");
4715 err = f2fs_init_post_read_wq(sbi);
4717 f2fs_err(sbi, "Failed to initialize post read workqueue");
4721 sbi->total_valid_node_count =
4722 le32_to_cpu(sbi->ckpt->valid_node_count);
4723 percpu_counter_set(&sbi->total_valid_inode_count,
4724 le32_to_cpu(sbi->ckpt->valid_inode_count));
4725 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
4726 sbi->total_valid_block_count =
4727 le64_to_cpu(sbi->ckpt->valid_block_count);
4728 sbi->last_valid_block_count = sbi->total_valid_block_count;
4729 sbi->reserved_blocks = 0;
4730 sbi->current_reserved_blocks = 0;
4731 limit_reserve_root(sbi);
4732 adjust_unusable_cap_perc(sbi);
4734 f2fs_init_extent_cache_info(sbi);
4736 f2fs_init_ino_entry_info(sbi);
4738 f2fs_init_fsync_node_info(sbi);
4740 /* setup checkpoint request control and start checkpoint issue thread */
4741 f2fs_init_ckpt_req_control(sbi);
4742 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
4743 test_opt(sbi, MERGE_CHECKPOINT)) {
4744 err = f2fs_start_ckpt_thread(sbi);
4747 "Failed to start F2FS issue_checkpoint_thread (%d)",
4749 goto stop_ckpt_thread;
4753 /* setup f2fs internal modules */
4754 err = f2fs_build_segment_manager(sbi);
4756 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
4760 err = f2fs_build_node_manager(sbi);
4762 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
4767 /* For write statistics */
4768 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
4770 /* get segno of first zoned block device */
4771 sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi);
4773 /* Read accumulated write IO statistics if exists */
4774 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
4775 if (__exist_node_summaries(sbi))
4776 sbi->kbytes_written =
4777 le64_to_cpu(seg_i->journal->info.kbytes_written);
4779 f2fs_build_gc_manager(sbi);
4781 err = f2fs_build_stats(sbi);
4785 /* get an inode for node space */
4786 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
4787 if (IS_ERR(sbi->node_inode)) {
4788 f2fs_err(sbi, "Failed to read node inode");
4789 err = PTR_ERR(sbi->node_inode);
4793 /* read root inode and dentry */
4794 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
4796 f2fs_err(sbi, "Failed to read root inode");
4797 err = PTR_ERR(root);
4798 goto free_node_inode;
4800 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
4801 !root->i_size || !root->i_nlink) {
4804 goto free_node_inode;
4807 generic_set_sb_d_ops(sb);
4808 sb->s_root = d_make_root(root); /* allocate root dentry */
4811 goto free_node_inode;
4814 err = f2fs_init_compress_inode(sbi);
4816 goto free_root_inode;
4818 err = f2fs_register_sysfs(sbi);
4820 goto free_compress_inode;
4822 sbi->umount_lock_holder = current;
4824 /* Enable quota usage during mount */
4825 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
4826 err = f2fs_enable_quotas(sb);
4828 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
4831 quota_enabled = f2fs_recover_quota_begin(sbi);
4833 /* if there are any orphan inodes, free them */
4834 err = f2fs_recover_orphan_inodes(sbi);
4838 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))) {
4839 skip_recovery = true;
4840 goto reset_checkpoint;
4843 /* recover fsynced data */
4844 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
4845 !test_opt(sbi, NORECOVERY)) {
4847 * mount should be failed, when device has readonly mode, and
4848 * previous checkpoint was not done by clean system shutdown.
4850 if (f2fs_hw_is_readonly(sbi)) {
4851 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4852 err = f2fs_recover_fsync_data(sbi, true);
4855 f2fs_err(sbi, "Need to recover fsync data, but "
4856 "write access unavailable, please try "
4857 "mount w/ disable_roll_forward or norecovery");
4862 f2fs_info(sbi, "write access unavailable, skipping recovery");
4863 goto reset_checkpoint;
4867 set_sbi_flag(sbi, SBI_NEED_FSCK);
4870 goto reset_checkpoint;
4872 err = f2fs_recover_fsync_data(sbi, false);
4875 skip_recovery = true;
4877 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4882 err = f2fs_recover_fsync_data(sbi, true);
4884 if (!f2fs_readonly(sb) && err > 0) {
4886 f2fs_err(sbi, "Need to recover fsync data");
4893 f2fs_recover_quota_end(sbi, quota_enabled);
4896 * If the f2fs is not readonly and fsync data recovery succeeds,
4897 * write pointer consistency of cursegs and other zones are already
4898 * checked and fixed during recovery. However, if recovery fails,
4899 * write pointers are left untouched, and retry-mount should check
4903 err = f2fs_check_and_fix_write_pointer(sbi);
4907 /* f2fs_recover_fsync_data() cleared this already */
4908 clear_sbi_flag(sbi, SBI_POR_DOING);
4910 err = f2fs_init_inmem_curseg(sbi);
4912 goto sync_free_meta;
4914 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4915 err = f2fs_disable_checkpoint(sbi);
4917 goto sync_free_meta;
4918 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4919 f2fs_enable_checkpoint(sbi);
4923 * If filesystem is not mounted as read-only then
4924 * do start the gc_thread.
4926 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4927 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
4928 /* After POR, we can run background GC thread.*/
4929 err = f2fs_start_gc_thread(sbi);
4931 goto sync_free_meta;
4935 /* recover broken superblock */
4937 err = f2fs_commit_super(sbi, true);
4938 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4939 sbi->valid_super_block ? 1 : 2, err);
4942 f2fs_join_shrinker(sbi);
4944 f2fs_tuning_parameters(sbi);
4946 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4947 cur_cp_version(F2FS_CKPT(sbi)));
4948 f2fs_update_time(sbi, CP_TIME);
4949 f2fs_update_time(sbi, REQ_TIME);
4950 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4952 sbi->umount_lock_holder = NULL;
4956 /* safe to flush all the data */
4957 sync_filesystem(sbi->sb);
4962 f2fs_truncate_quota_inode_pages(sb);
4963 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
4964 f2fs_quota_off_umount(sbi->sb);
4967 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4968 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4969 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4970 * falls into an infinite loop in f2fs_sync_meta_pages().
4972 truncate_inode_pages_final(META_MAPPING(sbi));
4973 /* evict some inodes being cached by GC */
4975 f2fs_unregister_sysfs(sbi);
4976 free_compress_inode:
4977 f2fs_destroy_compress_inode(sbi);
4982 f2fs_release_ino_entry(sbi, true);
4983 truncate_inode_pages_final(NODE_MAPPING(sbi));
4984 iput(sbi->node_inode);
4985 sbi->node_inode = NULL;
4987 f2fs_destroy_stats(sbi);
4989 /* stop discard thread before destroying node manager */
4990 f2fs_stop_discard_thread(sbi);
4991 f2fs_destroy_node_manager(sbi);
4993 f2fs_destroy_segment_manager(sbi);
4995 f2fs_stop_ckpt_thread(sbi);
4996 /* flush s_error_work before sbi destroy */
4997 flush_work(&sbi->s_error_work);
4998 f2fs_destroy_post_read_wq(sbi);
5000 destroy_device_list(sbi);
5003 make_bad_inode(sbi->meta_inode);
5004 iput(sbi->meta_inode);
5005 sbi->meta_inode = NULL;
5006 free_page_array_cache:
5007 f2fs_destroy_page_array_cache(sbi);
5009 f2fs_destroy_xattr_caches(sbi);
5011 destroy_percpu_info(sbi);
5013 f2fs_destroy_iostat(sbi);
5015 for (i = 0; i < NR_PAGE_TYPE; i++)
5016 kvfree(sbi->write_io[i]);
5018 #if IS_ENABLED(CONFIG_UNICODE)
5019 utf8_unload(sb->s_encoding);
5020 sb->s_encoding = NULL;
5024 for (i = 0; i < MAXQUOTAS; i++)
5025 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
5027 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
5033 sb->s_fs_info = NULL;
5035 /* give only one another chance */
5036 if (retry_cnt > 0 && skip_recovery) {
5038 shrink_dcache_sb(sb);
5044 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
5045 const char *dev_name, void *data)
5047 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
5050 static void kill_f2fs_super(struct super_block *sb)
5052 struct f2fs_sb_info *sbi = F2FS_SB(sb);
5055 sbi->umount_lock_holder = current;
5057 set_sbi_flag(sbi, SBI_IS_CLOSE);
5058 f2fs_stop_gc_thread(sbi);
5059 f2fs_stop_discard_thread(sbi);
5061 #ifdef CONFIG_F2FS_FS_COMPRESSION
5063 * latter evict_inode() can bypass checking and invalidating
5064 * compress inode cache.
5066 if (test_opt(sbi, COMPRESS_CACHE))
5067 truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
5070 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
5071 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
5072 struct cp_control cpc = {
5073 .reason = CP_UMOUNT,
5075 stat_inc_cp_call_count(sbi, TOTAL_CALL);
5076 f2fs_write_checkpoint(sbi, &cpc);
5079 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
5080 sb->s_flags &= ~SB_RDONLY;
5082 kill_block_super(sb);
5083 /* Release block devices last, after fscrypt_destroy_keyring(). */
5085 destroy_device_list(sbi);
5087 sb->s_fs_info = NULL;
5091 static struct file_system_type f2fs_fs_type = {
5092 .owner = THIS_MODULE,
5094 .mount = f2fs_mount,
5095 .kill_sb = kill_f2fs_super,
5096 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
5098 MODULE_ALIAS_FS("f2fs");
5100 static int __init init_inodecache(void)
5102 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
5103 sizeof(struct f2fs_inode_info), 0,
5104 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
5105 return f2fs_inode_cachep ? 0 : -ENOMEM;
5108 static void destroy_inodecache(void)
5111 * Make sure all delayed rcu free inodes are flushed before we
5115 kmem_cache_destroy(f2fs_inode_cachep);
5118 static int __init init_f2fs_fs(void)
5122 err = init_inodecache();
5125 err = f2fs_create_node_manager_caches();
5127 goto free_inodecache;
5128 err = f2fs_create_segment_manager_caches();
5130 goto free_node_manager_caches;
5131 err = f2fs_create_checkpoint_caches();
5133 goto free_segment_manager_caches;
5134 err = f2fs_create_recovery_cache();
5136 goto free_checkpoint_caches;
5137 err = f2fs_create_extent_cache();
5139 goto free_recovery_cache;
5140 err = f2fs_create_garbage_collection_cache();
5142 goto free_extent_cache;
5143 err = f2fs_init_sysfs();
5145 goto free_garbage_collection_cache;
5146 err = f2fs_init_shrinker();
5149 f2fs_create_root_stats();
5150 err = f2fs_init_post_read_processing();
5152 goto free_root_stats;
5153 err = f2fs_init_iostat_processing();
5155 goto free_post_read;
5156 err = f2fs_init_bio_entry_cache();
5159 err = f2fs_init_bioset();
5161 goto free_bio_entry_cache;
5162 err = f2fs_init_compress_mempool();
5165 err = f2fs_init_compress_cache();
5167 goto free_compress_mempool;
5168 err = f2fs_create_casefold_cache();
5170 goto free_compress_cache;
5171 err = register_filesystem(&f2fs_fs_type);
5173 goto free_casefold_cache;
5175 free_casefold_cache:
5176 f2fs_destroy_casefold_cache();
5177 free_compress_cache:
5178 f2fs_destroy_compress_cache();
5179 free_compress_mempool:
5180 f2fs_destroy_compress_mempool();
5182 f2fs_destroy_bioset();
5183 free_bio_entry_cache:
5184 f2fs_destroy_bio_entry_cache();
5186 f2fs_destroy_iostat_processing();
5188 f2fs_destroy_post_read_processing();
5190 f2fs_destroy_root_stats();
5191 f2fs_exit_shrinker();
5194 free_garbage_collection_cache:
5195 f2fs_destroy_garbage_collection_cache();
5197 f2fs_destroy_extent_cache();
5198 free_recovery_cache:
5199 f2fs_destroy_recovery_cache();
5200 free_checkpoint_caches:
5201 f2fs_destroy_checkpoint_caches();
5202 free_segment_manager_caches:
5203 f2fs_destroy_segment_manager_caches();
5204 free_node_manager_caches:
5205 f2fs_destroy_node_manager_caches();
5207 destroy_inodecache();
5212 static void __exit exit_f2fs_fs(void)
5214 unregister_filesystem(&f2fs_fs_type);
5215 f2fs_destroy_casefold_cache();
5216 f2fs_destroy_compress_cache();
5217 f2fs_destroy_compress_mempool();
5218 f2fs_destroy_bioset();
5219 f2fs_destroy_bio_entry_cache();
5220 f2fs_destroy_iostat_processing();
5221 f2fs_destroy_post_read_processing();
5222 f2fs_destroy_root_stats();
5223 f2fs_exit_shrinker();
5225 f2fs_destroy_garbage_collection_cache();
5226 f2fs_destroy_extent_cache();
5227 f2fs_destroy_recovery_cache();
5228 f2fs_destroy_checkpoint_caches();
5229 f2fs_destroy_segment_manager_caches();
5230 f2fs_destroy_node_manager_caches();
5231 destroy_inodecache();
5234 module_init(init_f2fs_fs)
5235 module_exit(exit_f2fs_fs)
5237 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
5238 MODULE_DESCRIPTION("Flash Friendly File System");
5239 MODULE_LICENSE("GPL");