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/buffer_head.h>
15 #include <linux/kthread.h>
16 #include <linux/parser.h>
17 #include <linux/mount.h>
18 #include <linux/seq_file.h>
19 #include <linux/proc_fs.h>
20 #include <linux/random.h>
21 #include <linux/exportfs.h>
22 #include <linux/blkdev.h>
23 #include <linux/quotaops.h>
24 #include <linux/f2fs_fs.h>
25 #include <linux/sysfs.h>
26 #include <linux/quota.h>
27 #include <linux/unicode.h>
28 #include <linux/part_stat.h>
29 #include <linux/zstd.h>
30 #include <linux/lz4.h>
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/f2fs.h>
42 static struct kmem_cache *f2fs_inode_cachep;
44 #ifdef CONFIG_F2FS_FAULT_INJECTION
46 const char *f2fs_fault_name[FAULT_MAX] = {
47 [FAULT_KMALLOC] = "kmalloc",
48 [FAULT_KVMALLOC] = "kvmalloc",
49 [FAULT_PAGE_ALLOC] = "page alloc",
50 [FAULT_PAGE_GET] = "page get",
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",
69 int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
72 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
77 atomic_set(&ffi->inject_ops, 0);
78 ffi->inject_rate = (int)rate;
82 if (type >= BIT(FAULT_MAX))
84 ffi->inject_type = (unsigned int)type;
88 memset(ffi, 0, sizeof(struct f2fs_fault_info));
91 "build fault injection attr: rate: %lu, type: 0x%lx",
97 /* f2fs-wide shrinker description */
98 static struct shrinker *f2fs_shrinker_info;
100 static int __init f2fs_init_shrinker(void)
102 f2fs_shrinker_info = shrinker_alloc(0, "f2fs-shrinker");
103 if (!f2fs_shrinker_info)
106 f2fs_shrinker_info->count_objects = f2fs_shrink_count;
107 f2fs_shrinker_info->scan_objects = f2fs_shrink_scan;
109 shrinker_register(f2fs_shrinker_info);
114 static void f2fs_exit_shrinker(void)
116 shrinker_free(f2fs_shrinker_info);
121 Opt_disable_roll_forward,
132 Opt_disable_ext_identify,
135 Opt_inline_xattr_size,
172 Opt_test_dummy_encryption,
174 Opt_checkpoint_disable,
175 Opt_checkpoint_disable_cap,
176 Opt_checkpoint_disable_cap_perc,
177 Opt_checkpoint_enable,
178 Opt_checkpoint_merge,
179 Opt_nocheckpoint_merge,
180 Opt_compress_algorithm,
181 Opt_compress_log_size,
182 Opt_compress_extension,
183 Opt_nocompress_extension,
192 Opt_age_extent_cache,
197 static match_table_t f2fs_tokens = {
198 {Opt_gc_background, "background_gc=%s"},
199 {Opt_disable_roll_forward, "disable_roll_forward"},
200 {Opt_norecovery, "norecovery"},
201 {Opt_discard, "discard"},
202 {Opt_nodiscard, "nodiscard"},
203 {Opt_noheap, "no_heap"},
205 {Opt_user_xattr, "user_xattr"},
206 {Opt_nouser_xattr, "nouser_xattr"},
208 {Opt_noacl, "noacl"},
209 {Opt_active_logs, "active_logs=%u"},
210 {Opt_disable_ext_identify, "disable_ext_identify"},
211 {Opt_inline_xattr, "inline_xattr"},
212 {Opt_noinline_xattr, "noinline_xattr"},
213 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
214 {Opt_inline_data, "inline_data"},
215 {Opt_inline_dentry, "inline_dentry"},
216 {Opt_noinline_dentry, "noinline_dentry"},
217 {Opt_flush_merge, "flush_merge"},
218 {Opt_noflush_merge, "noflush_merge"},
219 {Opt_barrier, "barrier"},
220 {Opt_nobarrier, "nobarrier"},
221 {Opt_fastboot, "fastboot"},
222 {Opt_extent_cache, "extent_cache"},
223 {Opt_noextent_cache, "noextent_cache"},
224 {Opt_noinline_data, "noinline_data"},
225 {Opt_data_flush, "data_flush"},
226 {Opt_reserve_root, "reserve_root=%u"},
227 {Opt_resgid, "resgid=%u"},
228 {Opt_resuid, "resuid=%u"},
229 {Opt_mode, "mode=%s"},
230 {Opt_fault_injection, "fault_injection=%u"},
231 {Opt_fault_type, "fault_type=%u"},
232 {Opt_lazytime, "lazytime"},
233 {Opt_nolazytime, "nolazytime"},
234 {Opt_quota, "quota"},
235 {Opt_noquota, "noquota"},
236 {Opt_usrquota, "usrquota"},
237 {Opt_grpquota, "grpquota"},
238 {Opt_prjquota, "prjquota"},
239 {Opt_usrjquota, "usrjquota=%s"},
240 {Opt_grpjquota, "grpjquota=%s"},
241 {Opt_prjjquota, "prjjquota=%s"},
242 {Opt_offusrjquota, "usrjquota="},
243 {Opt_offgrpjquota, "grpjquota="},
244 {Opt_offprjjquota, "prjjquota="},
245 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
246 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
247 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
248 {Opt_alloc, "alloc_mode=%s"},
249 {Opt_fsync, "fsync_mode=%s"},
250 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
251 {Opt_test_dummy_encryption, "test_dummy_encryption"},
252 {Opt_inlinecrypt, "inlinecrypt"},
253 {Opt_checkpoint_disable, "checkpoint=disable"},
254 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
255 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
256 {Opt_checkpoint_enable, "checkpoint=enable"},
257 {Opt_checkpoint_merge, "checkpoint_merge"},
258 {Opt_nocheckpoint_merge, "nocheckpoint_merge"},
259 {Opt_compress_algorithm, "compress_algorithm=%s"},
260 {Opt_compress_log_size, "compress_log_size=%u"},
261 {Opt_compress_extension, "compress_extension=%s"},
262 {Opt_nocompress_extension, "nocompress_extension=%s"},
263 {Opt_compress_chksum, "compress_chksum"},
264 {Opt_compress_mode, "compress_mode=%s"},
265 {Opt_compress_cache, "compress_cache"},
267 {Opt_gc_merge, "gc_merge"},
268 {Opt_nogc_merge, "nogc_merge"},
269 {Opt_discard_unit, "discard_unit=%s"},
270 {Opt_memory_mode, "memory=%s"},
271 {Opt_age_extent_cache, "age_extent_cache"},
272 {Opt_errors, "errors=%s"},
276 void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate,
277 const char *fmt, ...)
279 struct va_format vaf;
285 level = printk_get_level(fmt);
286 vaf.fmt = printk_skip_level(fmt);
289 printk_ratelimited("%c%cF2FS-fs (%s): %pV\n",
290 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
292 printk("%c%cF2FS-fs (%s): %pV\n",
293 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
298 #if IS_ENABLED(CONFIG_UNICODE)
299 static const struct f2fs_sb_encodings {
302 unsigned int version;
303 } f2fs_sb_encoding_map[] = {
304 {F2FS_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
307 static const struct f2fs_sb_encodings *
308 f2fs_sb_read_encoding(const struct f2fs_super_block *sb)
310 __u16 magic = le16_to_cpu(sb->s_encoding);
313 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
314 if (magic == f2fs_sb_encoding_map[i].magic)
315 return &f2fs_sb_encoding_map[i];
320 struct kmem_cache *f2fs_cf_name_slab;
321 static int __init f2fs_create_casefold_cache(void)
323 f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
325 return f2fs_cf_name_slab ? 0 : -ENOMEM;
328 static void f2fs_destroy_casefold_cache(void)
330 kmem_cache_destroy(f2fs_cf_name_slab);
333 static int __init f2fs_create_casefold_cache(void) { return 0; }
334 static void f2fs_destroy_casefold_cache(void) { }
337 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
339 block_t limit = min((sbi->user_block_count >> 3),
340 sbi->user_block_count - sbi->reserved_blocks);
343 if (test_opt(sbi, RESERVE_ROOT) &&
344 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
345 F2FS_OPTION(sbi).root_reserved_blocks = limit;
346 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
347 F2FS_OPTION(sbi).root_reserved_blocks);
349 if (!test_opt(sbi, RESERVE_ROOT) &&
350 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
351 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
352 !gid_eq(F2FS_OPTION(sbi).s_resgid,
353 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
354 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
355 from_kuid_munged(&init_user_ns,
356 F2FS_OPTION(sbi).s_resuid),
357 from_kgid_munged(&init_user_ns,
358 F2FS_OPTION(sbi).s_resgid));
361 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
363 if (!F2FS_OPTION(sbi).unusable_cap_perc)
366 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
367 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
369 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
370 F2FS_OPTION(sbi).unusable_cap_perc;
372 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
373 F2FS_OPTION(sbi).unusable_cap,
374 F2FS_OPTION(sbi).unusable_cap_perc);
377 static void init_once(void *foo)
379 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
381 inode_init_once(&fi->vfs_inode);
385 static const char * const quotatypes[] = INITQFNAMES;
386 #define QTYPE2NAME(t) (quotatypes[t])
387 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
390 struct f2fs_sb_info *sbi = F2FS_SB(sb);
394 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
395 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
398 if (f2fs_sb_has_quota_ino(sbi)) {
399 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
403 qname = match_strdup(args);
405 f2fs_err(sbi, "Not enough memory for storing quotafile name");
408 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
409 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
412 f2fs_err(sbi, "%s quota file already specified",
416 if (strchr(qname, '/')) {
417 f2fs_err(sbi, "quotafile must be on filesystem root");
420 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
428 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
430 struct f2fs_sb_info *sbi = F2FS_SB(sb);
432 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
433 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
436 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
437 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
441 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
444 * We do the test below only for project quotas. 'usrquota' and
445 * 'grpquota' mount options are allowed even without quota feature
446 * to support legacy quotas in quota files.
448 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
449 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
452 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
453 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
454 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
455 if (test_opt(sbi, USRQUOTA) &&
456 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
457 clear_opt(sbi, USRQUOTA);
459 if (test_opt(sbi, GRPQUOTA) &&
460 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
461 clear_opt(sbi, GRPQUOTA);
463 if (test_opt(sbi, PRJQUOTA) &&
464 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
465 clear_opt(sbi, PRJQUOTA);
467 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
468 test_opt(sbi, PRJQUOTA)) {
469 f2fs_err(sbi, "old and new quota format mixing");
473 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
474 f2fs_err(sbi, "journaled quota format not specified");
479 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
480 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
481 F2FS_OPTION(sbi).s_jquota_fmt = 0;
487 static int f2fs_set_test_dummy_encryption(struct super_block *sb,
489 const substring_t *arg,
492 struct f2fs_sb_info *sbi = F2FS_SB(sb);
493 struct fs_parameter param = {
494 .type = fs_value_is_string,
495 .string = arg->from ? arg->from : "",
497 struct fscrypt_dummy_policy *policy =
498 &F2FS_OPTION(sbi).dummy_enc_policy;
501 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
502 f2fs_warn(sbi, "test_dummy_encryption option not supported");
506 if (!f2fs_sb_has_encrypt(sbi)) {
507 f2fs_err(sbi, "Encrypt feature is off");
512 * This mount option is just for testing, and it's not worthwhile to
513 * implement the extra complexity (e.g. RCU protection) that would be
514 * needed to allow it to be set or changed during remount. We do allow
515 * it to be specified during remount, but only if there is no change.
517 if (is_remount && !fscrypt_is_dummy_policy_set(policy)) {
518 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
522 err = fscrypt_parse_test_dummy_encryption(¶m, policy);
526 "Can't change test_dummy_encryption on remount");
527 else if (err == -EINVAL)
528 f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
531 f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
535 f2fs_warn(sbi, "Test dummy encryption mode enabled");
539 #ifdef CONFIG_F2FS_FS_COMPRESSION
540 static bool is_compress_extension_exist(struct f2fs_sb_info *sbi,
541 const char *new_ext, bool is_ext)
543 unsigned char (*ext)[F2FS_EXTENSION_LEN];
548 ext = F2FS_OPTION(sbi).extensions;
549 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
551 ext = F2FS_OPTION(sbi).noextensions;
552 ext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
555 for (i = 0; i < ext_cnt; i++) {
556 if (!strcasecmp(new_ext, ext[i]))
564 * 1. The same extension name cannot not appear in both compress and non-compress extension
566 * 2. If the compress extension specifies all files, the types specified by the non-compress
567 * extension will be treated as special cases and will not be compressed.
568 * 3. Don't allow the non-compress extension specifies all files.
570 static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
572 unsigned char (*ext)[F2FS_EXTENSION_LEN];
573 unsigned char (*noext)[F2FS_EXTENSION_LEN];
574 int ext_cnt, noext_cnt, index = 0, no_index = 0;
576 ext = F2FS_OPTION(sbi).extensions;
577 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
578 noext = F2FS_OPTION(sbi).noextensions;
579 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
584 for (no_index = 0; no_index < noext_cnt; no_index++) {
585 if (!strcasecmp("*", noext[no_index])) {
586 f2fs_info(sbi, "Don't allow the nocompress extension specifies all files");
589 for (index = 0; index < ext_cnt; index++) {
590 if (!strcasecmp(ext[index], noext[no_index])) {
591 f2fs_info(sbi, "Don't allow the same extension %s appear in both compress and nocompress extension",
600 #ifdef CONFIG_F2FS_FS_LZ4
601 static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
603 #ifdef CONFIG_F2FS_FS_LZ4HC
606 if (strlen(str) == 3) {
607 F2FS_OPTION(sbi).compress_level = 0;
614 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
617 if (kstrtouint(str + 1, 10, &level))
620 if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
621 f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
625 F2FS_OPTION(sbi).compress_level = level;
628 if (strlen(str) == 3) {
629 F2FS_OPTION(sbi).compress_level = 0;
632 f2fs_info(sbi, "kernel doesn't support lz4hc compression");
638 #ifdef CONFIG_F2FS_FS_ZSTD
639 static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
644 if (strlen(str) == len) {
645 F2FS_OPTION(sbi).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
652 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
655 if (kstrtoint(str + 1, 10, &level))
658 /* f2fs does not support negative compress level now */
660 f2fs_info(sbi, "do not support negative compress level: %d", level);
664 if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
665 f2fs_info(sbi, "invalid zstd compress level: %d", level);
669 F2FS_OPTION(sbi).compress_level = level;
675 static int parse_options(struct super_block *sb, char *options, bool is_remount)
677 struct f2fs_sb_info *sbi = F2FS_SB(sb);
678 substring_t args[MAX_OPT_ARGS];
679 #ifdef CONFIG_F2FS_FS_COMPRESSION
680 unsigned char (*ext)[F2FS_EXTENSION_LEN];
681 unsigned char (*noext)[F2FS_EXTENSION_LEN];
682 int ext_cnt, noext_cnt;
693 while ((p = strsep(&options, ",")) != NULL) {
699 * Initialize args struct so we know whether arg was
700 * found; some options take optional arguments.
702 args[0].to = args[0].from = NULL;
703 token = match_token(p, f2fs_tokens, args);
706 case Opt_gc_background:
707 name = match_strdup(&args[0]);
711 if (!strcmp(name, "on")) {
712 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
713 } else if (!strcmp(name, "off")) {
714 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
715 } else if (!strcmp(name, "sync")) {
716 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
723 case Opt_disable_roll_forward:
724 set_opt(sbi, DISABLE_ROLL_FORWARD);
727 /* this option mounts f2fs with ro */
728 set_opt(sbi, NORECOVERY);
729 if (!f2fs_readonly(sb))
733 if (!f2fs_hw_support_discard(sbi)) {
734 f2fs_warn(sbi, "device does not support discard");
737 set_opt(sbi, DISCARD);
740 if (f2fs_hw_should_discard(sbi)) {
741 f2fs_warn(sbi, "discard is required for zoned block devices");
744 clear_opt(sbi, DISCARD);
748 f2fs_warn(sbi, "heap/no_heap options were deprecated");
750 #ifdef CONFIG_F2FS_FS_XATTR
752 set_opt(sbi, XATTR_USER);
754 case Opt_nouser_xattr:
755 clear_opt(sbi, XATTR_USER);
757 case Opt_inline_xattr:
758 set_opt(sbi, INLINE_XATTR);
760 case Opt_noinline_xattr:
761 clear_opt(sbi, INLINE_XATTR);
763 case Opt_inline_xattr_size:
764 if (args->from && match_int(args, &arg))
766 set_opt(sbi, INLINE_XATTR_SIZE);
767 F2FS_OPTION(sbi).inline_xattr_size = arg;
771 f2fs_info(sbi, "user_xattr options not supported");
773 case Opt_nouser_xattr:
774 f2fs_info(sbi, "nouser_xattr options not supported");
776 case Opt_inline_xattr:
777 f2fs_info(sbi, "inline_xattr options not supported");
779 case Opt_noinline_xattr:
780 f2fs_info(sbi, "noinline_xattr options not supported");
783 #ifdef CONFIG_F2FS_FS_POSIX_ACL
785 set_opt(sbi, POSIX_ACL);
788 clear_opt(sbi, POSIX_ACL);
792 f2fs_info(sbi, "acl options not supported");
795 f2fs_info(sbi, "noacl 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 clear_opt(sbi, READ_EXTENT_CACHE);
839 case Opt_noinline_data:
840 clear_opt(sbi, INLINE_DATA);
843 set_opt(sbi, DATA_FLUSH);
845 case Opt_reserve_root:
846 if (args->from && match_int(args, &arg))
848 if (test_opt(sbi, RESERVE_ROOT)) {
849 f2fs_info(sbi, "Preserve previous reserve_root=%u",
850 F2FS_OPTION(sbi).root_reserved_blocks);
852 F2FS_OPTION(sbi).root_reserved_blocks = arg;
853 set_opt(sbi, RESERVE_ROOT);
857 if (args->from && match_int(args, &arg))
859 uid = make_kuid(current_user_ns(), arg);
860 if (!uid_valid(uid)) {
861 f2fs_err(sbi, "Invalid uid value %d", arg);
864 F2FS_OPTION(sbi).s_resuid = uid;
867 if (args->from && match_int(args, &arg))
869 gid = make_kgid(current_user_ns(), arg);
870 if (!gid_valid(gid)) {
871 f2fs_err(sbi, "Invalid gid value %d", arg);
874 F2FS_OPTION(sbi).s_resgid = gid;
877 name = match_strdup(&args[0]);
881 if (!strcmp(name, "adaptive")) {
882 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
883 } else if (!strcmp(name, "lfs")) {
884 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
885 } else if (!strcmp(name, "fragment:segment")) {
886 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_SEG;
887 } else if (!strcmp(name, "fragment:block")) {
888 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_BLK;
895 #ifdef CONFIG_F2FS_FAULT_INJECTION
896 case Opt_fault_injection:
897 if (args->from && match_int(args, &arg))
899 if (f2fs_build_fault_attr(sbi, arg,
900 F2FS_ALL_FAULT_TYPE))
902 set_opt(sbi, FAULT_INJECTION);
906 if (args->from && match_int(args, &arg))
908 if (f2fs_build_fault_attr(sbi, 0, arg))
910 set_opt(sbi, FAULT_INJECTION);
913 case Opt_fault_injection:
914 f2fs_info(sbi, "fault_injection options not supported");
918 f2fs_info(sbi, "fault_type options not supported");
922 sb->s_flags |= SB_LAZYTIME;
925 sb->s_flags &= ~SB_LAZYTIME;
930 set_opt(sbi, USRQUOTA);
933 set_opt(sbi, GRPQUOTA);
936 set_opt(sbi, PRJQUOTA);
939 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
944 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
949 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
953 case Opt_offusrjquota:
954 ret = f2fs_clear_qf_name(sb, USRQUOTA);
958 case Opt_offgrpjquota:
959 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
963 case Opt_offprjjquota:
964 ret = f2fs_clear_qf_name(sb, 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(sb, p, &args[0],
1039 case Opt_inlinecrypt:
1040 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1041 sb->s_flags |= SB_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 strcpy(ext[ext_cnt], name);
1168 F2FS_OPTION(sbi).compress_ext_cnt++;
1171 case Opt_nocompress_extension:
1172 if (!f2fs_sb_has_compression(sbi)) {
1173 f2fs_info(sbi, "Image doesn't support compression");
1176 name = match_strdup(&args[0]);
1180 noext = F2FS_OPTION(sbi).noextensions;
1181 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
1183 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1184 noext_cnt >= COMPRESS_EXT_NUM) {
1186 "invalid extension length/number");
1191 if (is_compress_extension_exist(sbi, name, false)) {
1196 strcpy(noext[noext_cnt], name);
1197 F2FS_OPTION(sbi).nocompress_ext_cnt++;
1200 case Opt_compress_chksum:
1201 if (!f2fs_sb_has_compression(sbi)) {
1202 f2fs_info(sbi, "Image doesn't support compression");
1205 F2FS_OPTION(sbi).compress_chksum = true;
1207 case Opt_compress_mode:
1208 if (!f2fs_sb_has_compression(sbi)) {
1209 f2fs_info(sbi, "Image doesn't support compression");
1212 name = match_strdup(&args[0]);
1215 if (!strcmp(name, "fs")) {
1216 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1217 } else if (!strcmp(name, "user")) {
1218 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1225 case Opt_compress_cache:
1226 if (!f2fs_sb_has_compression(sbi)) {
1227 f2fs_info(sbi, "Image doesn't support compression");
1230 set_opt(sbi, COMPRESS_CACHE);
1233 case Opt_compress_algorithm:
1234 case Opt_compress_log_size:
1235 case Opt_compress_extension:
1236 case Opt_nocompress_extension:
1237 case Opt_compress_chksum:
1238 case Opt_compress_mode:
1239 case Opt_compress_cache:
1240 f2fs_info(sbi, "compression options not supported");
1247 set_opt(sbi, GC_MERGE);
1249 case Opt_nogc_merge:
1250 clear_opt(sbi, GC_MERGE);
1252 case Opt_discard_unit:
1253 name = match_strdup(&args[0]);
1256 if (!strcmp(name, "block")) {
1257 F2FS_OPTION(sbi).discard_unit =
1259 } else if (!strcmp(name, "segment")) {
1260 F2FS_OPTION(sbi).discard_unit =
1261 DISCARD_UNIT_SEGMENT;
1262 } else if (!strcmp(name, "section")) {
1263 F2FS_OPTION(sbi).discard_unit =
1264 DISCARD_UNIT_SECTION;
1271 case Opt_memory_mode:
1272 name = match_strdup(&args[0]);
1275 if (!strcmp(name, "normal")) {
1276 F2FS_OPTION(sbi).memory_mode =
1278 } else if (!strcmp(name, "low")) {
1279 F2FS_OPTION(sbi).memory_mode =
1287 case Opt_age_extent_cache:
1288 set_opt(sbi, AGE_EXTENT_CACHE);
1291 name = match_strdup(&args[0]);
1294 if (!strcmp(name, "remount-ro")) {
1295 F2FS_OPTION(sbi).errors =
1296 MOUNT_ERRORS_READONLY;
1297 } else if (!strcmp(name, "continue")) {
1298 F2FS_OPTION(sbi).errors =
1299 MOUNT_ERRORS_CONTINUE;
1300 } else if (!strcmp(name, "panic")) {
1301 F2FS_OPTION(sbi).errors =
1310 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1317 if (f2fs_check_quota_options(sbi))
1320 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
1321 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1324 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
1325 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1329 #if !IS_ENABLED(CONFIG_UNICODE)
1330 if (f2fs_sb_has_casefold(sbi)) {
1332 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1337 * The BLKZONED feature indicates that the drive was formatted with
1338 * zone alignment optimization. This is optional for host-aware
1339 * devices, but mandatory for host-managed zoned block devices.
1341 if (f2fs_sb_has_blkzoned(sbi)) {
1342 #ifdef CONFIG_BLK_DEV_ZONED
1343 if (F2FS_OPTION(sbi).discard_unit !=
1344 DISCARD_UNIT_SECTION) {
1345 f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default");
1346 F2FS_OPTION(sbi).discard_unit =
1347 DISCARD_UNIT_SECTION;
1350 if (F2FS_OPTION(sbi).fs_mode != FS_MODE_LFS) {
1351 f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature");
1355 f2fs_err(sbi, "Zoned block device support is not enabled");
1360 #ifdef CONFIG_F2FS_FS_COMPRESSION
1361 if (f2fs_test_compress_extension(sbi)) {
1362 f2fs_err(sbi, "invalid compress or nocompress extension");
1367 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
1368 int min_size, max_size;
1370 if (!f2fs_sb_has_extra_attr(sbi) ||
1371 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
1372 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1375 if (!test_opt(sbi, INLINE_XATTR)) {
1376 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1380 min_size = MIN_INLINE_XATTR_SIZE;
1381 max_size = MAX_INLINE_XATTR_SIZE;
1383 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1384 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
1385 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1386 min_size, max_size);
1391 if (test_opt(sbi, ATGC) && f2fs_lfs_mode(sbi)) {
1392 f2fs_err(sbi, "LFS is not compatible with ATGC");
1396 if (f2fs_is_readonly(sbi) && test_opt(sbi, FLUSH_MERGE)) {
1397 f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
1401 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1402 f2fs_err(sbi, "Allow to mount readonly mode only");
1408 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1410 struct f2fs_inode_info *fi;
1412 if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC))
1415 fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO);
1419 init_once((void *) fi);
1421 /* Initialize f2fs-specific inode info */
1422 atomic_set(&fi->dirty_pages, 0);
1423 atomic_set(&fi->i_compr_blocks, 0);
1424 init_f2fs_rwsem(&fi->i_sem);
1425 spin_lock_init(&fi->i_size_lock);
1426 INIT_LIST_HEAD(&fi->dirty_list);
1427 INIT_LIST_HEAD(&fi->gdirty_list);
1428 init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
1429 init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
1430 init_f2fs_rwsem(&fi->i_xattr_sem);
1432 /* Will be used by directory only */
1433 fi->i_dir_level = F2FS_SB(sb)->dir_level;
1435 return &fi->vfs_inode;
1438 static int f2fs_drop_inode(struct inode *inode)
1440 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1444 * during filesystem shutdown, if checkpoint is disabled,
1445 * drop useless meta/node dirty pages.
1447 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1448 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1449 inode->i_ino == F2FS_META_INO(sbi)) {
1450 trace_f2fs_drop_inode(inode, 1);
1456 * This is to avoid a deadlock condition like below.
1457 * writeback_single_inode(inode)
1458 * - f2fs_write_data_page
1459 * - f2fs_gc -> iput -> evict
1460 * - inode_wait_for_writeback(inode)
1462 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
1463 if (!inode->i_nlink && !is_bad_inode(inode)) {
1464 /* to avoid evict_inode call simultaneously */
1465 atomic_inc(&inode->i_count);
1466 spin_unlock(&inode->i_lock);
1468 /* should remain fi->extent_tree for writepage */
1469 f2fs_destroy_extent_node(inode);
1471 sb_start_intwrite(inode->i_sb);
1472 f2fs_i_size_write(inode, 0);
1474 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1475 inode, NULL, 0, DATA);
1476 truncate_inode_pages_final(inode->i_mapping);
1478 if (F2FS_HAS_BLOCKS(inode))
1479 f2fs_truncate(inode);
1481 sb_end_intwrite(inode->i_sb);
1483 spin_lock(&inode->i_lock);
1484 atomic_dec(&inode->i_count);
1486 trace_f2fs_drop_inode(inode, 0);
1489 ret = generic_drop_inode(inode);
1491 ret = fscrypt_drop_inode(inode);
1492 trace_f2fs_drop_inode(inode, ret);
1496 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1498 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1501 spin_lock(&sbi->inode_lock[DIRTY_META]);
1502 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1505 set_inode_flag(inode, FI_DIRTY_INODE);
1506 stat_inc_dirty_inode(sbi, DIRTY_META);
1508 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1509 list_add_tail(&F2FS_I(inode)->gdirty_list,
1510 &sbi->inode_list[DIRTY_META]);
1511 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1513 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1517 void f2fs_inode_synced(struct inode *inode)
1519 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1521 spin_lock(&sbi->inode_lock[DIRTY_META]);
1522 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1523 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1526 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1527 list_del_init(&F2FS_I(inode)->gdirty_list);
1528 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1530 clear_inode_flag(inode, FI_DIRTY_INODE);
1531 clear_inode_flag(inode, FI_AUTO_RECOVER);
1532 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1533 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1537 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1539 * We should call set_dirty_inode to write the dirty inode through write_inode.
1541 static void f2fs_dirty_inode(struct inode *inode, int flags)
1543 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1545 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1546 inode->i_ino == F2FS_META_INO(sbi))
1549 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1550 clear_inode_flag(inode, FI_AUTO_RECOVER);
1552 f2fs_inode_dirtied(inode, false);
1555 static void f2fs_free_inode(struct inode *inode)
1557 fscrypt_free_inode(inode);
1558 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1561 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1563 percpu_counter_destroy(&sbi->total_valid_inode_count);
1564 percpu_counter_destroy(&sbi->rf_node_block_count);
1565 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1568 static void destroy_device_list(struct f2fs_sb_info *sbi)
1572 for (i = 0; i < sbi->s_ndevs; i++) {
1574 bdev_fput(FDEV(i).bdev_file);
1575 #ifdef CONFIG_BLK_DEV_ZONED
1576 kvfree(FDEV(i).blkz_seq);
1582 static void f2fs_put_super(struct super_block *sb)
1584 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1589 /* unregister procfs/sysfs entries in advance to avoid race case */
1590 f2fs_unregister_sysfs(sbi);
1592 f2fs_quota_off_umount(sb);
1594 /* prevent remaining shrinker jobs */
1595 mutex_lock(&sbi->umount_mutex);
1598 * flush all issued checkpoints and stop checkpoint issue thread.
1599 * after then, all checkpoints should be done by each process context.
1601 f2fs_stop_ckpt_thread(sbi);
1604 * We don't need to do checkpoint when superblock is clean.
1605 * But, the previous checkpoint was not done by umount, it needs to do
1606 * clean checkpoint again.
1608 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1609 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1610 struct cp_control cpc = {
1611 .reason = CP_UMOUNT,
1613 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1614 err = f2fs_write_checkpoint(sbi, &cpc);
1617 /* be sure to wait for any on-going discard commands */
1618 done = f2fs_issue_discard_timeout(sbi);
1619 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
1620 struct cp_control cpc = {
1621 .reason = CP_UMOUNT | CP_TRIMMED,
1623 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1624 err = f2fs_write_checkpoint(sbi, &cpc);
1628 * normally superblock is clean, so we need to release this.
1629 * In addition, EIO will skip do checkpoint, we need this as well.
1631 f2fs_release_ino_entry(sbi, true);
1633 f2fs_leave_shrinker(sbi);
1634 mutex_unlock(&sbi->umount_mutex);
1636 /* our cp_error case, we can wait for any writeback page */
1637 f2fs_flush_merged_writes(sbi);
1639 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1641 if (err || f2fs_cp_error(sbi)) {
1642 truncate_inode_pages_final(NODE_MAPPING(sbi));
1643 truncate_inode_pages_final(META_MAPPING(sbi));
1646 for (i = 0; i < NR_COUNT_TYPE; i++) {
1647 if (!get_pages(sbi, i))
1649 f2fs_err(sbi, "detect filesystem reference count leak during "
1650 "umount, type: %d, count: %lld", i, get_pages(sbi, i));
1651 f2fs_bug_on(sbi, 1);
1654 f2fs_bug_on(sbi, sbi->fsync_node_num);
1656 f2fs_destroy_compress_inode(sbi);
1658 iput(sbi->node_inode);
1659 sbi->node_inode = NULL;
1661 iput(sbi->meta_inode);
1662 sbi->meta_inode = NULL;
1665 * iput() can update stat information, if f2fs_write_checkpoint()
1666 * above failed with error.
1668 f2fs_destroy_stats(sbi);
1670 /* destroy f2fs internal modules */
1671 f2fs_destroy_node_manager(sbi);
1672 f2fs_destroy_segment_manager(sbi);
1674 /* flush s_error_work before sbi destroy */
1675 flush_work(&sbi->s_error_work);
1677 f2fs_destroy_post_read_wq(sbi);
1681 if (sbi->s_chksum_driver)
1682 crypto_free_shash(sbi->s_chksum_driver);
1683 kfree(sbi->raw_super);
1685 f2fs_destroy_page_array_cache(sbi);
1686 f2fs_destroy_xattr_caches(sbi);
1688 for (i = 0; i < MAXQUOTAS; i++)
1689 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1691 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1692 destroy_percpu_info(sbi);
1693 f2fs_destroy_iostat(sbi);
1694 for (i = 0; i < NR_PAGE_TYPE; i++)
1695 kvfree(sbi->write_io[i]);
1696 #if IS_ENABLED(CONFIG_UNICODE)
1697 utf8_unload(sb->s_encoding);
1701 int f2fs_sync_fs(struct super_block *sb, int sync)
1703 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1706 if (unlikely(f2fs_cp_error(sbi)))
1708 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1711 trace_f2fs_sync_fs(sb, sync);
1713 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1717 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1718 err = f2fs_issue_checkpoint(sbi);
1724 static int f2fs_freeze(struct super_block *sb)
1726 if (f2fs_readonly(sb))
1729 /* IO error happened before */
1730 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1733 /* must be clean, since sync_filesystem() was already called */
1734 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1737 /* Let's flush checkpoints and stop the thread. */
1738 f2fs_flush_ckpt_thread(F2FS_SB(sb));
1740 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
1741 set_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1745 static int f2fs_unfreeze(struct super_block *sb)
1747 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1752 static int f2fs_statfs_project(struct super_block *sb,
1753 kprojid_t projid, struct kstatfs *buf)
1756 struct dquot *dquot;
1760 qid = make_kqid_projid(projid);
1761 dquot = dqget(sb, qid);
1763 return PTR_ERR(dquot);
1764 spin_lock(&dquot->dq_dqb_lock);
1766 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1767 dquot->dq_dqb.dqb_bhardlimit);
1769 limit >>= sb->s_blocksize_bits;
1771 if (limit && buf->f_blocks > limit) {
1772 curblock = (dquot->dq_dqb.dqb_curspace +
1773 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1774 buf->f_blocks = limit;
1775 buf->f_bfree = buf->f_bavail =
1776 (buf->f_blocks > curblock) ?
1777 (buf->f_blocks - curblock) : 0;
1780 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1781 dquot->dq_dqb.dqb_ihardlimit);
1783 if (limit && buf->f_files > limit) {
1784 buf->f_files = limit;
1786 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1787 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1790 spin_unlock(&dquot->dq_dqb_lock);
1796 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1798 struct super_block *sb = dentry->d_sb;
1799 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1800 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1801 block_t total_count, user_block_count, start_count;
1802 u64 avail_node_count;
1803 unsigned int total_valid_node_count;
1805 total_count = le64_to_cpu(sbi->raw_super->block_count);
1806 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1807 buf->f_type = F2FS_SUPER_MAGIC;
1808 buf->f_bsize = sbi->blocksize;
1810 buf->f_blocks = total_count - start_count;
1812 spin_lock(&sbi->stat_lock);
1814 user_block_count = sbi->user_block_count;
1815 total_valid_node_count = valid_node_count(sbi);
1816 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1817 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1818 sbi->current_reserved_blocks;
1820 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1823 buf->f_bfree -= sbi->unusable_block_count;
1824 spin_unlock(&sbi->stat_lock);
1826 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1827 buf->f_bavail = buf->f_bfree -
1828 F2FS_OPTION(sbi).root_reserved_blocks;
1832 if (avail_node_count > user_block_count) {
1833 buf->f_files = user_block_count;
1834 buf->f_ffree = buf->f_bavail;
1836 buf->f_files = avail_node_count;
1837 buf->f_ffree = min(avail_node_count - total_valid_node_count,
1841 buf->f_namelen = F2FS_NAME_LEN;
1842 buf->f_fsid = u64_to_fsid(id);
1845 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1846 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1847 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1853 static inline void f2fs_show_quota_options(struct seq_file *seq,
1854 struct super_block *sb)
1857 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1859 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1862 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1873 seq_printf(seq, ",jqfmt=%s", fmtname);
1876 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1877 seq_show_option(seq, "usrjquota",
1878 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1880 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1881 seq_show_option(seq, "grpjquota",
1882 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1884 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1885 seq_show_option(seq, "prjjquota",
1886 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1890 #ifdef CONFIG_F2FS_FS_COMPRESSION
1891 static inline void f2fs_show_compress_options(struct seq_file *seq,
1892 struct super_block *sb)
1894 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1898 if (!f2fs_sb_has_compression(sbi))
1901 switch (F2FS_OPTION(sbi).compress_algorithm) {
1911 case COMPRESS_LZORLE:
1912 algtype = "lzo-rle";
1915 seq_printf(seq, ",compress_algorithm=%s", algtype);
1917 if (F2FS_OPTION(sbi).compress_level)
1918 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1920 seq_printf(seq, ",compress_log_size=%u",
1921 F2FS_OPTION(sbi).compress_log_size);
1923 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1924 seq_printf(seq, ",compress_extension=%s",
1925 F2FS_OPTION(sbi).extensions[i]);
1928 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
1929 seq_printf(seq, ",nocompress_extension=%s",
1930 F2FS_OPTION(sbi).noextensions[i]);
1933 if (F2FS_OPTION(sbi).compress_chksum)
1934 seq_puts(seq, ",compress_chksum");
1936 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1937 seq_printf(seq, ",compress_mode=%s", "fs");
1938 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1939 seq_printf(seq, ",compress_mode=%s", "user");
1941 if (test_opt(sbi, COMPRESS_CACHE))
1942 seq_puts(seq, ",compress_cache");
1946 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1948 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1950 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1951 seq_printf(seq, ",background_gc=%s", "sync");
1952 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1953 seq_printf(seq, ",background_gc=%s", "on");
1954 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
1955 seq_printf(seq, ",background_gc=%s", "off");
1957 if (test_opt(sbi, GC_MERGE))
1958 seq_puts(seq, ",gc_merge");
1960 seq_puts(seq, ",nogc_merge");
1962 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1963 seq_puts(seq, ",disable_roll_forward");
1964 if (test_opt(sbi, NORECOVERY))
1965 seq_puts(seq, ",norecovery");
1966 if (test_opt(sbi, DISCARD)) {
1967 seq_puts(seq, ",discard");
1968 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
1969 seq_printf(seq, ",discard_unit=%s", "block");
1970 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
1971 seq_printf(seq, ",discard_unit=%s", "segment");
1972 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
1973 seq_printf(seq, ",discard_unit=%s", "section");
1975 seq_puts(seq, ",nodiscard");
1977 #ifdef CONFIG_F2FS_FS_XATTR
1978 if (test_opt(sbi, XATTR_USER))
1979 seq_puts(seq, ",user_xattr");
1981 seq_puts(seq, ",nouser_xattr");
1982 if (test_opt(sbi, INLINE_XATTR))
1983 seq_puts(seq, ",inline_xattr");
1985 seq_puts(seq, ",noinline_xattr");
1986 if (test_opt(sbi, INLINE_XATTR_SIZE))
1987 seq_printf(seq, ",inline_xattr_size=%u",
1988 F2FS_OPTION(sbi).inline_xattr_size);
1990 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1991 if (test_opt(sbi, POSIX_ACL))
1992 seq_puts(seq, ",acl");
1994 seq_puts(seq, ",noacl");
1996 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1997 seq_puts(seq, ",disable_ext_identify");
1998 if (test_opt(sbi, INLINE_DATA))
1999 seq_puts(seq, ",inline_data");
2001 seq_puts(seq, ",noinline_data");
2002 if (test_opt(sbi, INLINE_DENTRY))
2003 seq_puts(seq, ",inline_dentry");
2005 seq_puts(seq, ",noinline_dentry");
2006 if (test_opt(sbi, FLUSH_MERGE))
2007 seq_puts(seq, ",flush_merge");
2009 seq_puts(seq, ",noflush_merge");
2010 if (test_opt(sbi, NOBARRIER))
2011 seq_puts(seq, ",nobarrier");
2013 seq_puts(seq, ",barrier");
2014 if (test_opt(sbi, FASTBOOT))
2015 seq_puts(seq, ",fastboot");
2016 if (test_opt(sbi, READ_EXTENT_CACHE))
2017 seq_puts(seq, ",extent_cache");
2019 seq_puts(seq, ",noextent_cache");
2020 if (test_opt(sbi, AGE_EXTENT_CACHE))
2021 seq_puts(seq, ",age_extent_cache");
2022 if (test_opt(sbi, DATA_FLUSH))
2023 seq_puts(seq, ",data_flush");
2025 seq_puts(seq, ",mode=");
2026 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
2027 seq_puts(seq, "adaptive");
2028 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
2029 seq_puts(seq, "lfs");
2030 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
2031 seq_puts(seq, "fragment:segment");
2032 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2033 seq_puts(seq, "fragment:block");
2034 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
2035 if (test_opt(sbi, RESERVE_ROOT))
2036 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
2037 F2FS_OPTION(sbi).root_reserved_blocks,
2038 from_kuid_munged(&init_user_ns,
2039 F2FS_OPTION(sbi).s_resuid),
2040 from_kgid_munged(&init_user_ns,
2041 F2FS_OPTION(sbi).s_resgid));
2042 #ifdef CONFIG_F2FS_FAULT_INJECTION
2043 if (test_opt(sbi, FAULT_INJECTION)) {
2044 seq_printf(seq, ",fault_injection=%u",
2045 F2FS_OPTION(sbi).fault_info.inject_rate);
2046 seq_printf(seq, ",fault_type=%u",
2047 F2FS_OPTION(sbi).fault_info.inject_type);
2051 if (test_opt(sbi, QUOTA))
2052 seq_puts(seq, ",quota");
2053 if (test_opt(sbi, USRQUOTA))
2054 seq_puts(seq, ",usrquota");
2055 if (test_opt(sbi, GRPQUOTA))
2056 seq_puts(seq, ",grpquota");
2057 if (test_opt(sbi, PRJQUOTA))
2058 seq_puts(seq, ",prjquota");
2060 f2fs_show_quota_options(seq, sbi->sb);
2062 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
2064 if (sbi->sb->s_flags & SB_INLINECRYPT)
2065 seq_puts(seq, ",inlinecrypt");
2067 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
2068 seq_printf(seq, ",alloc_mode=%s", "default");
2069 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2070 seq_printf(seq, ",alloc_mode=%s", "reuse");
2072 if (test_opt(sbi, DISABLE_CHECKPOINT))
2073 seq_printf(seq, ",checkpoint=disable:%u",
2074 F2FS_OPTION(sbi).unusable_cap);
2075 if (test_opt(sbi, MERGE_CHECKPOINT))
2076 seq_puts(seq, ",checkpoint_merge");
2078 seq_puts(seq, ",nocheckpoint_merge");
2079 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
2080 seq_printf(seq, ",fsync_mode=%s", "posix");
2081 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
2082 seq_printf(seq, ",fsync_mode=%s", "strict");
2083 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
2084 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
2086 #ifdef CONFIG_F2FS_FS_COMPRESSION
2087 f2fs_show_compress_options(seq, sbi->sb);
2090 if (test_opt(sbi, ATGC))
2091 seq_puts(seq, ",atgc");
2093 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
2094 seq_printf(seq, ",memory=%s", "normal");
2095 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
2096 seq_printf(seq, ",memory=%s", "low");
2098 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2099 seq_printf(seq, ",errors=%s", "remount-ro");
2100 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
2101 seq_printf(seq, ",errors=%s", "continue");
2102 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
2103 seq_printf(seq, ",errors=%s", "panic");
2108 static void default_options(struct f2fs_sb_info *sbi, bool remount)
2110 /* init some FS parameters */
2112 set_opt(sbi, READ_EXTENT_CACHE);
2113 clear_opt(sbi, DISABLE_CHECKPOINT);
2115 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2116 set_opt(sbi, DISCARD);
2118 if (f2fs_sb_has_blkzoned(sbi))
2119 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2121 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2124 if (f2fs_sb_has_readonly(sbi))
2125 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
2127 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
2129 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
2130 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
2131 SMALL_VOLUME_SEGMENTS)
2132 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2134 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
2135 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
2136 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2137 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2138 if (f2fs_sb_has_compression(sbi)) {
2139 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
2140 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
2141 F2FS_OPTION(sbi).compress_ext_cnt = 0;
2142 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
2144 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
2145 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
2146 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
2148 set_opt(sbi, INLINE_XATTR);
2149 set_opt(sbi, INLINE_DATA);
2150 set_opt(sbi, INLINE_DENTRY);
2151 set_opt(sbi, MERGE_CHECKPOINT);
2152 F2FS_OPTION(sbi).unusable_cap = 0;
2153 sbi->sb->s_flags |= SB_LAZYTIME;
2154 if (!f2fs_is_readonly(sbi))
2155 set_opt(sbi, FLUSH_MERGE);
2156 if (f2fs_sb_has_blkzoned(sbi))
2157 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
2159 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
2161 #ifdef CONFIG_F2FS_FS_XATTR
2162 set_opt(sbi, XATTR_USER);
2164 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2165 set_opt(sbi, POSIX_ACL);
2168 f2fs_build_fault_attr(sbi, 0, 0);
2172 static int f2fs_enable_quotas(struct super_block *sb);
2175 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
2177 unsigned int s_flags = sbi->sb->s_flags;
2178 struct cp_control cpc;
2179 unsigned int gc_mode = sbi->gc_mode;
2184 if (s_flags & SB_RDONLY) {
2185 f2fs_err(sbi, "checkpoint=disable on readonly fs");
2188 sbi->sb->s_flags |= SB_ACTIVE;
2190 /* check if we need more GC first */
2191 unusable = f2fs_get_unusable_blocks(sbi);
2192 if (!f2fs_disable_cp_again(sbi, unusable))
2195 f2fs_update_time(sbi, DISABLE_TIME);
2197 sbi->gc_mode = GC_URGENT_HIGH;
2199 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
2200 struct f2fs_gc_control gc_control = {
2201 .victim_segno = NULL_SEGNO,
2202 .init_gc_type = FG_GC,
2203 .should_migrate_blocks = false,
2204 .err_gc_skipped = true,
2206 .nr_free_secs = 1 };
2208 f2fs_down_write(&sbi->gc_lock);
2209 stat_inc_gc_call_count(sbi, FOREGROUND);
2210 err = f2fs_gc(sbi, &gc_control);
2211 if (err == -ENODATA) {
2215 if (err && err != -EAGAIN)
2219 ret = sync_filesystem(sbi->sb);
2221 err = ret ? ret : err;
2225 unusable = f2fs_get_unusable_blocks(sbi);
2226 if (f2fs_disable_cp_again(sbi, unusable)) {
2232 f2fs_down_write(&sbi->gc_lock);
2233 cpc.reason = CP_PAUSE;
2234 set_sbi_flag(sbi, SBI_CP_DISABLED);
2235 stat_inc_cp_call_count(sbi, TOTAL_CALL);
2236 err = f2fs_write_checkpoint(sbi, &cpc);
2240 spin_lock(&sbi->stat_lock);
2241 sbi->unusable_block_count = unusable;
2242 spin_unlock(&sbi->stat_lock);
2245 f2fs_up_write(&sbi->gc_lock);
2247 sbi->gc_mode = gc_mode;
2248 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2252 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
2254 int retry = DEFAULT_RETRY_IO_COUNT;
2256 /* we should flush all the data to keep data consistency */
2258 sync_inodes_sb(sbi->sb);
2259 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2260 } while (get_pages(sbi, F2FS_DIRTY_DATA) && retry--);
2262 if (unlikely(retry < 0))
2263 f2fs_warn(sbi, "checkpoint=enable has some unwritten data.");
2265 f2fs_down_write(&sbi->gc_lock);
2266 f2fs_dirty_to_prefree(sbi);
2268 clear_sbi_flag(sbi, SBI_CP_DISABLED);
2269 set_sbi_flag(sbi, SBI_IS_DIRTY);
2270 f2fs_up_write(&sbi->gc_lock);
2272 f2fs_sync_fs(sbi->sb, 1);
2274 /* Let's ensure there's no pending checkpoint anymore */
2275 f2fs_flush_ckpt_thread(sbi);
2278 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
2280 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2281 struct f2fs_mount_info org_mount_opt;
2282 unsigned long old_sb_flags;
2284 bool need_restart_gc = false, need_stop_gc = false;
2285 bool need_restart_flush = false, need_stop_flush = false;
2286 bool need_restart_discard = false, need_stop_discard = false;
2287 bool need_enable_checkpoint = false, need_disable_checkpoint = false;
2288 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
2289 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
2290 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
2291 bool no_atgc = !test_opt(sbi, ATGC);
2292 bool no_discard = !test_opt(sbi, DISCARD);
2293 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
2294 bool block_unit_discard = f2fs_block_unit_discard(sbi);
2300 * Save the old mount options in case we
2301 * need to restore them.
2303 org_mount_opt = sbi->mount_opt;
2304 old_sb_flags = sb->s_flags;
2307 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
2308 for (i = 0; i < MAXQUOTAS; i++) {
2309 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2310 org_mount_opt.s_qf_names[i] =
2311 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
2313 if (!org_mount_opt.s_qf_names[i]) {
2314 for (j = 0; j < i; j++)
2315 kfree(org_mount_opt.s_qf_names[j]);
2319 org_mount_opt.s_qf_names[i] = NULL;
2324 /* recover superblocks we couldn't write due to previous RO mount */
2325 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
2326 err = f2fs_commit_super(sbi, false);
2327 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2330 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2333 default_options(sbi, true);
2335 /* parse mount options */
2336 err = parse_options(sb, data, true);
2340 #ifdef CONFIG_BLK_DEV_ZONED
2341 if (f2fs_sb_has_blkzoned(sbi) &&
2342 sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
2344 "zoned: max open zones %u is too small, need at least %u open zones",
2345 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
2351 /* flush outstanding errors before changing fs state */
2352 flush_work(&sbi->s_error_work);
2355 * Previous and new state of filesystem is RO,
2356 * so skip checking GC and FLUSH_MERGE conditions.
2358 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
2361 if (f2fs_dev_is_readonly(sbi) && !(*flags & SB_RDONLY)) {
2367 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
2368 err = dquot_suspend(sb, -1);
2371 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
2372 /* dquot_resume needs RW */
2373 sb->s_flags &= ~SB_RDONLY;
2374 if (sb_any_quota_suspended(sb)) {
2375 dquot_resume(sb, -1);
2376 } else if (f2fs_sb_has_quota_ino(sbi)) {
2377 err = f2fs_enable_quotas(sb);
2383 if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
2385 f2fs_warn(sbi, "LFS is not compatible with IPU");
2389 /* disallow enable atgc dynamically */
2390 if (no_atgc == !!test_opt(sbi, ATGC)) {
2392 f2fs_warn(sbi, "switch atgc option is not allowed");
2396 /* disallow enable/disable extent_cache dynamically */
2397 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
2399 f2fs_warn(sbi, "switch extent_cache option is not allowed");
2402 /* disallow enable/disable age extent_cache dynamically */
2403 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2405 f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
2409 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2411 f2fs_warn(sbi, "switch compress_cache option is not allowed");
2415 if (block_unit_discard != f2fs_block_unit_discard(sbi)) {
2417 f2fs_warn(sbi, "switch discard_unit option is not allowed");
2421 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2423 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
2428 * We stop the GC thread if FS is mounted as RO
2429 * or if background_gc = off is passed in mount
2430 * option. Also sync the filesystem.
2432 if ((*flags & SB_RDONLY) ||
2433 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2434 !test_opt(sbi, GC_MERGE))) {
2435 if (sbi->gc_thread) {
2436 f2fs_stop_gc_thread(sbi);
2437 need_restart_gc = true;
2439 } else if (!sbi->gc_thread) {
2440 err = f2fs_start_gc_thread(sbi);
2443 need_stop_gc = true;
2446 if (*flags & SB_RDONLY) {
2449 set_sbi_flag(sbi, SBI_IS_DIRTY);
2450 set_sbi_flag(sbi, SBI_IS_CLOSE);
2451 f2fs_sync_fs(sb, 1);
2452 clear_sbi_flag(sbi, SBI_IS_CLOSE);
2456 * We stop issue flush thread if FS is mounted as RO
2457 * or if flush_merge is not passed in mount option.
2459 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2460 clear_opt(sbi, FLUSH_MERGE);
2461 f2fs_destroy_flush_cmd_control(sbi, false);
2462 need_restart_flush = true;
2464 err = f2fs_create_flush_cmd_control(sbi);
2467 need_stop_flush = true;
2470 if (no_discard == !!test_opt(sbi, DISCARD)) {
2471 if (test_opt(sbi, DISCARD)) {
2472 err = f2fs_start_discard_thread(sbi);
2475 need_stop_discard = true;
2477 f2fs_stop_discard_thread(sbi);
2478 f2fs_issue_discard_timeout(sbi);
2479 need_restart_discard = true;
2483 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
2484 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2485 err = f2fs_disable_checkpoint(sbi);
2487 goto restore_discard;
2488 need_enable_checkpoint = true;
2490 f2fs_enable_checkpoint(sbi);
2491 need_disable_checkpoint = true;
2496 * Place this routine at the end, since a new checkpoint would be
2497 * triggered while remount and we need to take care of it before
2498 * returning from remount.
2500 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2501 !test_opt(sbi, MERGE_CHECKPOINT)) {
2502 f2fs_stop_ckpt_thread(sbi);
2504 /* Flush if the prevous checkpoint, if exists. */
2505 f2fs_flush_ckpt_thread(sbi);
2507 err = f2fs_start_ckpt_thread(sbi);
2510 "Failed to start F2FS issue_checkpoint_thread (%d)",
2512 goto restore_checkpoint;
2518 /* Release old quota file names */
2519 for (i = 0; i < MAXQUOTAS; i++)
2520 kfree(org_mount_opt.s_qf_names[i]);
2522 /* Update the POSIXACL Flag */
2523 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2524 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2526 limit_reserve_root(sbi);
2527 adjust_unusable_cap_perc(sbi);
2528 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
2531 if (need_enable_checkpoint) {
2532 f2fs_enable_checkpoint(sbi);
2533 } else if (need_disable_checkpoint) {
2534 if (f2fs_disable_checkpoint(sbi))
2535 f2fs_warn(sbi, "checkpoint has not been disabled");
2538 if (need_restart_discard) {
2539 if (f2fs_start_discard_thread(sbi))
2540 f2fs_warn(sbi, "discard has been stopped");
2541 } else if (need_stop_discard) {
2542 f2fs_stop_discard_thread(sbi);
2545 if (need_restart_flush) {
2546 if (f2fs_create_flush_cmd_control(sbi))
2547 f2fs_warn(sbi, "background flush thread has stopped");
2548 } else if (need_stop_flush) {
2549 clear_opt(sbi, FLUSH_MERGE);
2550 f2fs_destroy_flush_cmd_control(sbi, false);
2553 if (need_restart_gc) {
2554 if (f2fs_start_gc_thread(sbi))
2555 f2fs_warn(sbi, "background gc thread has stopped");
2556 } else if (need_stop_gc) {
2557 f2fs_stop_gc_thread(sbi);
2561 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
2562 for (i = 0; i < MAXQUOTAS; i++) {
2563 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2564 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
2567 sbi->mount_opt = org_mount_opt;
2568 sb->s_flags = old_sb_flags;
2572 static void f2fs_shutdown(struct super_block *sb)
2574 f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false);
2578 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
2580 /* need to recovery orphan */
2581 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
2583 /* need to recovery data */
2584 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2586 if (test_opt(sbi, NORECOVERY))
2588 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
2591 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
2593 bool readonly = f2fs_readonly(sbi->sb);
2595 if (!f2fs_need_recovery(sbi))
2598 /* it doesn't need to check f2fs_sb_has_readonly() */
2599 if (f2fs_hw_is_readonly(sbi))
2603 sbi->sb->s_flags &= ~SB_RDONLY;
2604 set_sbi_flag(sbi, SBI_IS_WRITABLE);
2608 * Turn on quotas which were not enabled for read-only mounts if
2609 * filesystem has quota feature, so that they are updated correctly.
2611 return f2fs_enable_quota_files(sbi, readonly);
2614 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
2618 f2fs_quota_off_umount(sbi->sb);
2620 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
2621 clear_sbi_flag(sbi, SBI_IS_WRITABLE);
2622 sbi->sb->s_flags |= SB_RDONLY;
2626 /* Read data from quotafile */
2627 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2628 size_t len, loff_t off)
2630 struct inode *inode = sb_dqopt(sb)->files[type];
2631 struct address_space *mapping = inode->i_mapping;
2632 block_t blkidx = F2FS_BYTES_TO_BLK(off);
2633 int offset = off & (sb->s_blocksize - 1);
2636 loff_t i_size = i_size_read(inode);
2642 if (off + len > i_size)
2645 while (toread > 0) {
2646 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2648 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
2650 if (PTR_ERR(page) == -ENOMEM) {
2651 memalloc_retry_wait(GFP_NOFS);
2654 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2655 return PTR_ERR(page);
2660 if (unlikely(page->mapping != mapping)) {
2661 f2fs_put_page(page, 1);
2664 if (unlikely(!PageUptodate(page))) {
2665 f2fs_put_page(page, 1);
2666 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2670 memcpy_from_page(data, page, offset, tocopy);
2671 f2fs_put_page(page, 1);
2681 /* Write to quotafile */
2682 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2683 const char *data, size_t len, loff_t off)
2685 struct inode *inode = sb_dqopt(sb)->files[type];
2686 struct address_space *mapping = inode->i_mapping;
2687 const struct address_space_operations *a_ops = mapping->a_ops;
2688 int offset = off & (sb->s_blocksize - 1);
2689 size_t towrite = len;
2691 void *fsdata = NULL;
2695 while (towrite > 0) {
2696 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2699 err = a_ops->write_begin(NULL, mapping, off, tocopy,
2701 if (unlikely(err)) {
2702 if (err == -ENOMEM) {
2703 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2706 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2710 memcpy_to_page(page, offset, data, tocopy);
2712 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2723 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2724 f2fs_mark_inode_dirty_sync(inode, false);
2725 return len - towrite;
2728 int f2fs_dquot_initialize(struct inode *inode)
2730 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
2733 return dquot_initialize(inode);
2736 static struct dquot __rcu **f2fs_get_dquots(struct inode *inode)
2738 return F2FS_I(inode)->i_dquot;
2741 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2743 return &F2FS_I(inode)->i_reserved_quota;
2746 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2748 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2749 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2753 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2754 F2FS_OPTION(sbi).s_jquota_fmt, type);
2757 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2762 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2763 err = f2fs_enable_quotas(sbi->sb);
2765 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2771 for (i = 0; i < MAXQUOTAS; i++) {
2772 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2773 err = f2fs_quota_on_mount(sbi, i);
2778 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2785 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2788 struct inode *qf_inode;
2789 unsigned long qf_inum;
2790 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
2793 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2795 qf_inum = f2fs_qf_ino(sb, type);
2799 qf_inode = f2fs_iget(sb, qf_inum);
2800 if (IS_ERR(qf_inode)) {
2801 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2802 return PTR_ERR(qf_inode);
2805 /* Don't account quota for quota files to avoid recursion */
2806 inode_lock(qf_inode);
2807 qf_inode->i_flags |= S_NOQUOTA;
2809 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
2810 F2FS_I(qf_inode)->i_flags |= qf_flag;
2811 f2fs_set_inode_flags(qf_inode);
2813 inode_unlock(qf_inode);
2815 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2820 static int f2fs_enable_quotas(struct super_block *sb)
2822 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2824 unsigned long qf_inum;
2825 bool quota_mopt[MAXQUOTAS] = {
2826 test_opt(sbi, USRQUOTA),
2827 test_opt(sbi, GRPQUOTA),
2828 test_opt(sbi, PRJQUOTA),
2831 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2832 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2836 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2838 for (type = 0; type < MAXQUOTAS; type++) {
2839 qf_inum = f2fs_qf_ino(sb, type);
2841 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2842 DQUOT_USAGE_ENABLED |
2843 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2845 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2847 for (type--; type >= 0; type--)
2848 dquot_quota_off(sb, type);
2849 set_sbi_flag(F2FS_SB(sb),
2850 SBI_QUOTA_NEED_REPAIR);
2858 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2860 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2861 struct address_space *mapping = dqopt->files[type]->i_mapping;
2864 ret = dquot_writeback_dquots(sbi->sb, type);
2868 ret = filemap_fdatawrite(mapping);
2872 /* if we are using journalled quota */
2873 if (is_journalled_quota(sbi))
2876 ret = filemap_fdatawait(mapping);
2878 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2881 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2885 int f2fs_quota_sync(struct super_block *sb, int type)
2887 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2888 struct quota_info *dqopt = sb_dqopt(sb);
2893 * Now when everything is written we can discard the pagecache so
2894 * that userspace sees the changes.
2896 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2898 if (type != -1 && cnt != type)
2901 if (!sb_has_quota_active(sb, cnt))
2904 if (!f2fs_sb_has_quota_ino(sbi))
2905 inode_lock(dqopt->files[cnt]);
2910 * f2fs_down_read(quota_sem)
2911 * dquot_writeback_dquots()
2914 * f2fs_down_read(quota_sem)
2917 f2fs_down_read(&sbi->quota_sem);
2919 ret = f2fs_quota_sync_file(sbi, cnt);
2921 f2fs_up_read(&sbi->quota_sem);
2922 f2fs_unlock_op(sbi);
2924 if (!f2fs_sb_has_quota_ino(sbi))
2925 inode_unlock(dqopt->files[cnt]);
2933 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2934 const struct path *path)
2936 struct inode *inode;
2939 /* if quota sysfile exists, deny enabling quota with specific file */
2940 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2941 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2945 if (path->dentry->d_sb != sb)
2948 err = f2fs_quota_sync(sb, type);
2952 inode = d_inode(path->dentry);
2954 err = filemap_fdatawrite(inode->i_mapping);
2958 err = filemap_fdatawait(inode->i_mapping);
2962 err = dquot_quota_on(sb, type, format_id, path);
2967 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
2968 f2fs_set_inode_flags(inode);
2969 inode_unlock(inode);
2970 f2fs_mark_inode_dirty_sync(inode, false);
2975 static int __f2fs_quota_off(struct super_block *sb, int type)
2977 struct inode *inode = sb_dqopt(sb)->files[type];
2980 if (!inode || !igrab(inode))
2981 return dquot_quota_off(sb, type);
2983 err = f2fs_quota_sync(sb, type);
2987 err = dquot_quota_off(sb, type);
2988 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
2992 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
2993 f2fs_set_inode_flags(inode);
2994 inode_unlock(inode);
2995 f2fs_mark_inode_dirty_sync(inode, false);
3001 static int f2fs_quota_off(struct super_block *sb, int type)
3003 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3006 err = __f2fs_quota_off(sb, type);
3009 * quotactl can shutdown journalled quota, result in inconsistence
3010 * between quota record and fs data by following updates, tag the
3011 * flag to let fsck be aware of it.
3013 if (is_journalled_quota(sbi))
3014 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3018 void f2fs_quota_off_umount(struct super_block *sb)
3023 for (type = 0; type < MAXQUOTAS; type++) {
3024 err = __f2fs_quota_off(sb, type);
3026 int ret = dquot_quota_off(sb, type);
3028 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
3030 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3034 * In case of checkpoint=disable, we must flush quota blocks.
3035 * This can cause NULL exception for node_inode in end_io, since
3036 * put_super already dropped it.
3038 sync_filesystem(sb);
3041 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
3043 struct quota_info *dqopt = sb_dqopt(sb);
3046 for (type = 0; type < MAXQUOTAS; type++) {
3047 if (!dqopt->files[type])
3049 f2fs_inode_synced(dqopt->files[type]);
3053 static int f2fs_dquot_commit(struct dquot *dquot)
3055 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3058 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
3059 ret = dquot_commit(dquot);
3061 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3062 f2fs_up_read(&sbi->quota_sem);
3066 static int f2fs_dquot_acquire(struct dquot *dquot)
3068 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3071 f2fs_down_read(&sbi->quota_sem);
3072 ret = dquot_acquire(dquot);
3074 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3075 f2fs_up_read(&sbi->quota_sem);
3079 static int f2fs_dquot_release(struct dquot *dquot)
3081 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3082 int ret = dquot_release(dquot);
3085 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3089 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
3091 struct super_block *sb = dquot->dq_sb;
3092 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3093 int ret = dquot_mark_dquot_dirty(dquot);
3095 /* if we are using journalled quota */
3096 if (is_journalled_quota(sbi))
3097 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
3102 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
3104 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3105 int ret = dquot_commit_info(sb, type);
3108 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3112 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
3114 *projid = F2FS_I(inode)->i_projid;
3118 static const struct dquot_operations f2fs_quota_operations = {
3119 .get_reserved_space = f2fs_get_reserved_space,
3120 .write_dquot = f2fs_dquot_commit,
3121 .acquire_dquot = f2fs_dquot_acquire,
3122 .release_dquot = f2fs_dquot_release,
3123 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
3124 .write_info = f2fs_dquot_commit_info,
3125 .alloc_dquot = dquot_alloc,
3126 .destroy_dquot = dquot_destroy,
3127 .get_projid = f2fs_get_projid,
3128 .get_next_id = dquot_get_next_id,
3131 static const struct quotactl_ops f2fs_quotactl_ops = {
3132 .quota_on = f2fs_quota_on,
3133 .quota_off = f2fs_quota_off,
3134 .quota_sync = f2fs_quota_sync,
3135 .get_state = dquot_get_state,
3136 .set_info = dquot_set_dqinfo,
3137 .get_dqblk = dquot_get_dqblk,
3138 .set_dqblk = dquot_set_dqblk,
3139 .get_nextdqblk = dquot_get_next_dqblk,
3142 int f2fs_dquot_initialize(struct inode *inode)
3147 int f2fs_quota_sync(struct super_block *sb, int type)
3152 void f2fs_quota_off_umount(struct super_block *sb)
3157 static const struct super_operations f2fs_sops = {
3158 .alloc_inode = f2fs_alloc_inode,
3159 .free_inode = f2fs_free_inode,
3160 .drop_inode = f2fs_drop_inode,
3161 .write_inode = f2fs_write_inode,
3162 .dirty_inode = f2fs_dirty_inode,
3163 .show_options = f2fs_show_options,
3165 .quota_read = f2fs_quota_read,
3166 .quota_write = f2fs_quota_write,
3167 .get_dquots = f2fs_get_dquots,
3169 .evict_inode = f2fs_evict_inode,
3170 .put_super = f2fs_put_super,
3171 .sync_fs = f2fs_sync_fs,
3172 .freeze_fs = f2fs_freeze,
3173 .unfreeze_fs = f2fs_unfreeze,
3174 .statfs = f2fs_statfs,
3175 .remount_fs = f2fs_remount,
3176 .shutdown = f2fs_shutdown,
3179 #ifdef CONFIG_FS_ENCRYPTION
3180 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
3182 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3183 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3187 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
3190 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3193 * Encrypting the root directory is not allowed because fsck
3194 * expects lost+found directory to exist and remain unencrypted
3195 * if LOST_FOUND feature is enabled.
3198 if (f2fs_sb_has_lost_found(sbi) &&
3199 inode->i_ino == F2FS_ROOT_INO(sbi))
3202 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3203 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3204 ctx, len, fs_data, XATTR_CREATE);
3207 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
3209 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
3212 static bool f2fs_has_stable_inodes(struct super_block *sb)
3217 static struct block_device **f2fs_get_devices(struct super_block *sb,
3218 unsigned int *num_devs)
3220 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3221 struct block_device **devs;
3224 if (!f2fs_is_multi_device(sbi))
3227 devs = kmalloc_array(sbi->s_ndevs, sizeof(*devs), GFP_KERNEL);
3229 return ERR_PTR(-ENOMEM);
3231 for (i = 0; i < sbi->s_ndevs; i++)
3232 devs[i] = FDEV(i).bdev;
3233 *num_devs = sbi->s_ndevs;
3237 static const struct fscrypt_operations f2fs_cryptops = {
3238 .needs_bounce_pages = 1,
3239 .has_32bit_inodes = 1,
3240 .supports_subblock_data_units = 1,
3241 .legacy_key_prefix = "f2fs:",
3242 .get_context = f2fs_get_context,
3243 .set_context = f2fs_set_context,
3244 .get_dummy_policy = f2fs_get_dummy_policy,
3245 .empty_dir = f2fs_empty_dir,
3246 .has_stable_inodes = f2fs_has_stable_inodes,
3247 .get_devices = f2fs_get_devices,
3251 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
3252 u64 ino, u32 generation)
3254 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3255 struct inode *inode;
3257 if (f2fs_check_nid_range(sbi, ino))
3258 return ERR_PTR(-ESTALE);
3261 * f2fs_iget isn't quite right if the inode is currently unallocated!
3262 * However f2fs_iget currently does appropriate checks to handle stale
3263 * inodes so everything is OK.
3265 inode = f2fs_iget(sb, ino);
3267 return ERR_CAST(inode);
3268 if (unlikely(generation && inode->i_generation != generation)) {
3269 /* we didn't find the right inode.. */
3271 return ERR_PTR(-ESTALE);
3276 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
3277 int fh_len, int fh_type)
3279 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3280 f2fs_nfs_get_inode);
3283 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
3284 int fh_len, int fh_type)
3286 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3287 f2fs_nfs_get_inode);
3290 static const struct export_operations f2fs_export_ops = {
3291 .encode_fh = generic_encode_ino32_fh,
3292 .fh_to_dentry = f2fs_fh_to_dentry,
3293 .fh_to_parent = f2fs_fh_to_parent,
3294 .get_parent = f2fs_get_parent,
3297 loff_t max_file_blocks(struct inode *inode)
3303 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
3304 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
3305 * space in inode.i_addr, it will be more safe to reassign
3309 if (inode && f2fs_compressed_file(inode))
3310 leaf_count = ADDRS_PER_BLOCK(inode);
3312 leaf_count = DEF_ADDRS_PER_BLOCK;
3314 /* two direct node blocks */
3315 result += (leaf_count * 2);
3317 /* two indirect node blocks */
3318 leaf_count *= NIDS_PER_BLOCK;
3319 result += (leaf_count * 2);
3321 /* one double indirect node block */
3322 leaf_count *= NIDS_PER_BLOCK;
3323 result += leaf_count;
3326 * For compatibility with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{64,32} with
3327 * a 4K crypto data unit, we must restrict the max filesize to what can
3328 * fit within U32_MAX + 1 data units.
3331 result = min(result, (((loff_t)U32_MAX + 1) * 4096) >> F2FS_BLKSIZE_BITS);
3336 static int __f2fs_commit_super(struct buffer_head *bh,
3337 struct f2fs_super_block *super)
3341 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
3342 set_buffer_dirty(bh);
3345 /* it's rare case, we can do fua all the time */
3346 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
3349 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
3350 struct buffer_head *bh)
3352 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3353 (bh->b_data + F2FS_SUPER_OFFSET);
3354 struct super_block *sb = sbi->sb;
3355 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3356 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
3357 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
3358 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
3359 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
3360 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3361 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
3362 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
3363 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
3364 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
3365 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3366 u32 segment_count = le32_to_cpu(raw_super->segment_count);
3367 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3368 u64 main_end_blkaddr = main_blkaddr +
3369 (segment_count_main << log_blocks_per_seg);
3370 u64 seg_end_blkaddr = segment0_blkaddr +
3371 (segment_count << log_blocks_per_seg);
3373 if (segment0_blkaddr != cp_blkaddr) {
3374 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
3375 segment0_blkaddr, cp_blkaddr);
3379 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
3381 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
3382 cp_blkaddr, sit_blkaddr,
3383 segment_count_ckpt << log_blocks_per_seg);
3387 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
3389 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
3390 sit_blkaddr, nat_blkaddr,
3391 segment_count_sit << log_blocks_per_seg);
3395 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
3397 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
3398 nat_blkaddr, ssa_blkaddr,
3399 segment_count_nat << log_blocks_per_seg);
3403 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
3405 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
3406 ssa_blkaddr, main_blkaddr,
3407 segment_count_ssa << log_blocks_per_seg);
3411 if (main_end_blkaddr > seg_end_blkaddr) {
3412 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3413 main_blkaddr, seg_end_blkaddr,
3414 segment_count_main << log_blocks_per_seg);
3416 } else if (main_end_blkaddr < seg_end_blkaddr) {
3420 /* fix in-memory information all the time */
3421 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
3422 segment0_blkaddr) >> log_blocks_per_seg);
3424 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
3425 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3428 err = __f2fs_commit_super(bh, NULL);
3429 res = err ? "failed" : "done";
3431 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3432 res, main_blkaddr, seg_end_blkaddr,
3433 segment_count_main << log_blocks_per_seg);
3440 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
3441 struct buffer_head *bh)
3443 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
3444 block_t total_sections, blocks_per_seg;
3445 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3446 (bh->b_data + F2FS_SUPER_OFFSET);
3447 size_t crc_offset = 0;
3450 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
3451 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
3452 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
3456 /* Check checksum_offset and crc in superblock */
3457 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
3458 crc_offset = le32_to_cpu(raw_super->checksum_offset);
3460 offsetof(struct f2fs_super_block, crc)) {
3461 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
3463 return -EFSCORRUPTED;
3465 crc = le32_to_cpu(raw_super->crc);
3466 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
3467 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
3468 return -EFSCORRUPTED;
3472 /* only support block_size equals to PAGE_SIZE */
3473 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3474 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3475 le32_to_cpu(raw_super->log_blocksize),
3477 return -EFSCORRUPTED;
3480 /* check log blocks per segment */
3481 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
3482 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
3483 le32_to_cpu(raw_super->log_blocks_per_seg));
3484 return -EFSCORRUPTED;
3487 /* Currently, support 512/1024/2048/4096/16K bytes sector size */
3488 if (le32_to_cpu(raw_super->log_sectorsize) >
3489 F2FS_MAX_LOG_SECTOR_SIZE ||
3490 le32_to_cpu(raw_super->log_sectorsize) <
3491 F2FS_MIN_LOG_SECTOR_SIZE) {
3492 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3493 le32_to_cpu(raw_super->log_sectorsize));
3494 return -EFSCORRUPTED;
3496 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3497 le32_to_cpu(raw_super->log_sectorsize) !=
3498 F2FS_MAX_LOG_SECTOR_SIZE) {
3499 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3500 le32_to_cpu(raw_super->log_sectors_per_block),
3501 le32_to_cpu(raw_super->log_sectorsize));
3502 return -EFSCORRUPTED;
3505 segment_count = le32_to_cpu(raw_super->segment_count);
3506 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3507 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3508 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3509 total_sections = le32_to_cpu(raw_super->section_count);
3511 /* blocks_per_seg should be 512, given the above check */
3512 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
3514 if (segment_count > F2FS_MAX_SEGMENT ||
3515 segment_count < F2FS_MIN_SEGMENTS) {
3516 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
3517 return -EFSCORRUPTED;
3520 if (total_sections > segment_count_main || total_sections < 1 ||
3521 segs_per_sec > segment_count || !segs_per_sec) {
3522 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3523 segment_count, total_sections, segs_per_sec);
3524 return -EFSCORRUPTED;
3527 if (segment_count_main != total_sections * segs_per_sec) {
3528 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3529 segment_count_main, total_sections, segs_per_sec);
3530 return -EFSCORRUPTED;
3533 if ((segment_count / segs_per_sec) < total_sections) {
3534 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3535 segment_count, segs_per_sec, total_sections);
3536 return -EFSCORRUPTED;
3539 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
3540 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3541 segment_count, le64_to_cpu(raw_super->block_count));
3542 return -EFSCORRUPTED;
3545 if (RDEV(0).path[0]) {
3546 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3549 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3550 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3553 if (segment_count != dev_seg_count) {
3554 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3555 segment_count, dev_seg_count);
3556 return -EFSCORRUPTED;
3559 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3560 !bdev_is_zoned(sbi->sb->s_bdev)) {
3561 f2fs_info(sbi, "Zoned block device path is missing");
3562 return -EFSCORRUPTED;
3566 if (secs_per_zone > total_sections || !secs_per_zone) {
3567 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3568 secs_per_zone, total_sections);
3569 return -EFSCORRUPTED;
3571 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3572 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3573 (le32_to_cpu(raw_super->extension_count) +
3574 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
3575 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3576 le32_to_cpu(raw_super->extension_count),
3577 raw_super->hot_ext_count,
3578 F2FS_MAX_EXTENSION);
3579 return -EFSCORRUPTED;
3582 if (le32_to_cpu(raw_super->cp_payload) >=
3583 (blocks_per_seg - F2FS_CP_PACKS -
3584 NR_CURSEG_PERSIST_TYPE)) {
3585 f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
3586 le32_to_cpu(raw_super->cp_payload),
3587 blocks_per_seg - F2FS_CP_PACKS -
3588 NR_CURSEG_PERSIST_TYPE);
3589 return -EFSCORRUPTED;
3592 /* check reserved ino info */
3593 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3594 le32_to_cpu(raw_super->meta_ino) != 2 ||
3595 le32_to_cpu(raw_super->root_ino) != 3) {
3596 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3597 le32_to_cpu(raw_super->node_ino),
3598 le32_to_cpu(raw_super->meta_ino),
3599 le32_to_cpu(raw_super->root_ino));
3600 return -EFSCORRUPTED;
3603 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
3604 if (sanity_check_area_boundary(sbi, bh))
3605 return -EFSCORRUPTED;
3610 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
3612 unsigned int total, fsmeta;
3613 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3614 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3615 unsigned int ovp_segments, reserved_segments;
3616 unsigned int main_segs, blocks_per_seg;
3617 unsigned int sit_segs, nat_segs;
3618 unsigned int sit_bitmap_size, nat_bitmap_size;
3619 unsigned int log_blocks_per_seg;
3620 unsigned int segment_count_main;
3621 unsigned int cp_pack_start_sum, cp_payload;
3622 block_t user_block_count, valid_user_blocks;
3623 block_t avail_node_count, valid_node_count;
3624 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
3627 total = le32_to_cpu(raw_super->segment_count);
3628 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
3629 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3631 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3633 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3634 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3636 if (unlikely(fsmeta >= total))
3639 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3640 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3642 if (!f2fs_sb_has_readonly(sbi) &&
3643 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
3644 ovp_segments == 0 || reserved_segments == 0)) {
3645 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
3648 user_block_count = le64_to_cpu(ckpt->user_block_count);
3649 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3650 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
3651 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3652 if (!user_block_count || user_block_count >=
3653 segment_count_main << log_blocks_per_seg) {
3654 f2fs_err(sbi, "Wrong user_block_count: %u",
3659 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3660 if (valid_user_blocks > user_block_count) {
3661 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3662 valid_user_blocks, user_block_count);
3666 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
3667 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
3668 if (valid_node_count > avail_node_count) {
3669 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3670 valid_node_count, avail_node_count);
3674 main_segs = le32_to_cpu(raw_super->segment_count_main);
3675 blocks_per_seg = BLKS_PER_SEG(sbi);
3677 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3678 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3679 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3682 if (f2fs_sb_has_readonly(sbi))
3685 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3686 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3687 le32_to_cpu(ckpt->cur_node_segno[j])) {
3688 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3690 le32_to_cpu(ckpt->cur_node_segno[i]));
3696 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3697 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3698 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3701 if (f2fs_sb_has_readonly(sbi))
3704 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3705 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3706 le32_to_cpu(ckpt->cur_data_segno[j])) {
3707 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3709 le32_to_cpu(ckpt->cur_data_segno[i]));
3714 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3715 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3716 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3717 le32_to_cpu(ckpt->cur_data_segno[j])) {
3718 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3720 le32_to_cpu(ckpt->cur_node_segno[i]));
3726 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3727 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3729 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3730 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3731 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3732 sit_bitmap_size, nat_bitmap_size);
3736 cp_pack_start_sum = __start_sum_addr(sbi);
3737 cp_payload = __cp_payload(sbi);
3738 if (cp_pack_start_sum < cp_payload + 1 ||
3739 cp_pack_start_sum > blocks_per_seg - 1 -
3740 NR_CURSEG_PERSIST_TYPE) {
3741 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3746 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3747 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3748 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3749 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3750 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3751 le32_to_cpu(ckpt->checksum_offset));
3755 nat_blocks = nat_segs << log_blocks_per_seg;
3756 nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
3757 nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3758 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
3759 (cp_payload + F2FS_CP_PACKS +
3760 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
3761 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
3762 cp_payload, nat_bits_blocks);
3766 if (unlikely(f2fs_cp_error(sbi))) {
3767 f2fs_err(sbi, "A bug case: need to run fsck");
3773 static void init_sb_info(struct f2fs_sb_info *sbi)
3775 struct f2fs_super_block *raw_super = sbi->raw_super;
3778 sbi->log_sectors_per_block =
3779 le32_to_cpu(raw_super->log_sectors_per_block);
3780 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3781 sbi->blocksize = BIT(sbi->log_blocksize);
3782 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3783 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
3784 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3785 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3786 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3787 sbi->total_node_count = SEGS_TO_BLKS(sbi,
3788 ((le32_to_cpu(raw_super->segment_count_nat) / 2) *
3789 NAT_ENTRY_PER_BLOCK));
3790 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3791 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3792 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3793 sbi->cur_victim_sec = NULL_SECNO;
3794 sbi->gc_mode = GC_NORMAL;
3795 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3796 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3797 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3798 sbi->migration_granularity = SEGS_PER_SEC(sbi);
3799 sbi->seq_file_ra_mul = MIN_RA_MUL;
3800 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
3801 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
3802 spin_lock_init(&sbi->gc_remaining_trials_lock);
3803 atomic64_set(&sbi->current_atomic_write, 0);
3805 sbi->dir_level = DEF_DIR_LEVEL;
3806 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3807 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3808 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3809 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3810 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3811 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3812 DEF_UMOUNT_DISCARD_TIMEOUT;
3813 clear_sbi_flag(sbi, SBI_NEED_FSCK);
3815 for (i = 0; i < NR_COUNT_TYPE; i++)
3816 atomic_set(&sbi->nr_pages[i], 0);
3818 for (i = 0; i < META; i++)
3819 atomic_set(&sbi->wb_sync_req[i], 0);
3821 INIT_LIST_HEAD(&sbi->s_list);
3822 mutex_init(&sbi->umount_mutex);
3823 init_f2fs_rwsem(&sbi->io_order_lock);
3824 spin_lock_init(&sbi->cp_lock);
3826 sbi->dirty_device = 0;
3827 spin_lock_init(&sbi->dev_lock);
3829 init_f2fs_rwsem(&sbi->sb_lock);
3830 init_f2fs_rwsem(&sbi->pin_sem);
3833 static int init_percpu_info(struct f2fs_sb_info *sbi)
3837 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3841 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
3843 goto err_valid_block;
3845 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3848 goto err_node_block;
3852 percpu_counter_destroy(&sbi->rf_node_block_count);
3854 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3858 #ifdef CONFIG_BLK_DEV_ZONED
3860 struct f2fs_report_zones_args {
3861 struct f2fs_sb_info *sbi;
3862 struct f2fs_dev_info *dev;
3865 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3868 struct f2fs_report_zones_args *rz_args = data;
3869 block_t unusable_blocks = (zone->len - zone->capacity) >>
3870 F2FS_LOG_SECTORS_PER_BLOCK;
3872 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3875 set_bit(idx, rz_args->dev->blkz_seq);
3876 if (!rz_args->sbi->unusable_blocks_per_sec) {
3877 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
3880 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
3881 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
3887 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
3889 struct block_device *bdev = FDEV(devi).bdev;
3890 sector_t nr_sectors = bdev_nr_sectors(bdev);
3891 struct f2fs_report_zones_args rep_zone_arg;
3893 unsigned int max_open_zones;
3896 if (!f2fs_sb_has_blkzoned(sbi))
3899 if (bdev_is_zoned(FDEV(devi).bdev)) {
3900 max_open_zones = bdev_max_open_zones(bdev);
3901 if (max_open_zones && (max_open_zones < sbi->max_open_zones))
3902 sbi->max_open_zones = max_open_zones;
3903 if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
3905 "zoned: max open zones %u is too small, need at least %u open zones",
3906 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
3911 zone_sectors = bdev_zone_sectors(bdev);
3912 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
3913 SECTOR_TO_BLOCK(zone_sectors))
3915 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
3916 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
3917 sbi->blocks_per_blkz);
3918 if (nr_sectors & (zone_sectors - 1))
3919 FDEV(devi).nr_blkz++;
3921 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
3922 BITS_TO_LONGS(FDEV(devi).nr_blkz)
3923 * sizeof(unsigned long),
3925 if (!FDEV(devi).blkz_seq)
3928 rep_zone_arg.sbi = sbi;
3929 rep_zone_arg.dev = &FDEV(devi);
3931 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3940 * Read f2fs raw super block.
3941 * Because we have two copies of super block, so read both of them
3942 * to get the first valid one. If any one of them is broken, we pass
3943 * them recovery flag back to the caller.
3945 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3946 struct f2fs_super_block **raw_super,
3947 int *valid_super_block, int *recovery)
3949 struct super_block *sb = sbi->sb;
3951 struct buffer_head *bh;
3952 struct f2fs_super_block *super;
3955 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3959 for (block = 0; block < 2; block++) {
3960 bh = sb_bread(sb, block);
3962 f2fs_err(sbi, "Unable to read %dth superblock",
3969 /* sanity checking of raw super */
3970 err = sanity_check_raw_super(sbi, bh);
3972 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3980 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3982 *valid_super_block = block;
3988 /* No valid superblock */
3997 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3999 struct buffer_head *bh;
4003 if ((recover && f2fs_readonly(sbi->sb)) ||
4004 f2fs_hw_is_readonly(sbi)) {
4005 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
4009 /* we should update superblock crc here */
4010 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
4011 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
4012 offsetof(struct f2fs_super_block, crc));
4013 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
4016 /* write back-up superblock first */
4017 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
4020 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4023 /* if we are in recovery path, skip writing valid superblock */
4027 /* write current valid superblock */
4028 bh = sb_bread(sbi->sb, sbi->valid_super_block);
4031 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4036 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
4038 unsigned long flags;
4040 spin_lock_irqsave(&sbi->error_lock, flags);
4041 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
4042 sbi->stop_reason[reason]++;
4043 spin_unlock_irqrestore(&sbi->error_lock, flags);
4046 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
4048 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4049 unsigned long flags;
4052 f2fs_down_write(&sbi->sb_lock);
4054 spin_lock_irqsave(&sbi->error_lock, flags);
4055 if (sbi->error_dirty) {
4056 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4058 sbi->error_dirty = false;
4060 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
4061 spin_unlock_irqrestore(&sbi->error_lock, flags);
4063 err = f2fs_commit_super(sbi, false);
4065 f2fs_up_write(&sbi->sb_lock);
4067 f2fs_err_ratelimited(sbi,
4068 "f2fs_commit_super fails to record stop_reason, err:%d",
4072 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
4074 unsigned long flags;
4076 spin_lock_irqsave(&sbi->error_lock, flags);
4077 if (!test_bit(flag, (unsigned long *)sbi->errors)) {
4078 set_bit(flag, (unsigned long *)sbi->errors);
4079 sbi->error_dirty = true;
4081 spin_unlock_irqrestore(&sbi->error_lock, flags);
4084 static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
4086 unsigned long flags;
4087 bool need_update = false;
4089 spin_lock_irqsave(&sbi->error_lock, flags);
4090 if (sbi->error_dirty) {
4091 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4093 sbi->error_dirty = false;
4096 spin_unlock_irqrestore(&sbi->error_lock, flags);
4101 static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error)
4105 f2fs_down_write(&sbi->sb_lock);
4107 if (!f2fs_update_errors(sbi))
4110 err = f2fs_commit_super(sbi, false);
4112 f2fs_err_ratelimited(sbi,
4113 "f2fs_commit_super fails to record errors:%u, err:%d",
4116 f2fs_up_write(&sbi->sb_lock);
4119 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4121 f2fs_save_errors(sbi, error);
4122 f2fs_record_errors(sbi, error);
4125 void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error)
4127 f2fs_save_errors(sbi, error);
4129 if (!sbi->error_dirty)
4131 if (!test_bit(error, (unsigned long *)sbi->errors))
4133 schedule_work(&sbi->s_error_work);
4136 static bool system_going_down(void)
4138 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4139 || system_state == SYSTEM_RESTART;
4142 void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason,
4145 struct super_block *sb = sbi->sb;
4146 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
4147 bool continue_fs = !shutdown &&
4148 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
4150 set_ckpt_flags(sbi, CP_ERROR_FLAG);
4152 if (!f2fs_hw_is_readonly(sbi)) {
4153 save_stop_reason(sbi, reason);
4155 if (irq_context && !shutdown)
4156 schedule_work(&sbi->s_error_work);
4158 f2fs_record_stop_reason(sbi);
4162 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4163 * could panic during 'reboot -f' as the underlying device got already
4166 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
4167 !shutdown && !system_going_down() &&
4168 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
4169 panic("F2FS-fs (device %s): panic forced after error\n",
4173 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
4176 * Continue filesystem operators if errors=continue. Should not set
4177 * RO by shutdown, since RO bypasses thaw_super which can hang the
4180 if (continue_fs || f2fs_readonly(sb) || shutdown) {
4181 f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason);
4185 f2fs_warn(sbi, "Remounting filesystem read-only");
4187 * Make sure updated value of ->s_mount_flags will be visible before
4191 sb->s_flags |= SB_RDONLY;
4194 static void f2fs_record_error_work(struct work_struct *work)
4196 struct f2fs_sb_info *sbi = container_of(work,
4197 struct f2fs_sb_info, s_error_work);
4199 f2fs_record_stop_reason(sbi);
4202 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
4204 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4205 unsigned int max_devices = MAX_DEVICES;
4206 unsigned int logical_blksize;
4207 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
4210 /* Initialize single device information */
4211 if (!RDEV(0).path[0]) {
4212 if (!bdev_is_zoned(sbi->sb->s_bdev))
4218 * Initialize multiple devices information, or single
4219 * zoned block device information.
4221 sbi->devs = f2fs_kzalloc(sbi,
4222 array_size(max_devices,
4223 sizeof(struct f2fs_dev_info)),
4228 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
4229 sbi->aligned_blksize = true;
4230 #ifdef CONFIG_BLK_DEV_ZONED
4231 sbi->max_open_zones = UINT_MAX;
4234 for (i = 0; i < max_devices; i++) {
4236 FDEV(0).bdev_file = sbi->sb->s_bdev_file;
4237 else if (!RDEV(i).path[0])
4240 if (max_devices > 1) {
4241 /* Multi-device mount */
4242 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
4243 FDEV(i).total_segments =
4244 le32_to_cpu(RDEV(i).total_segments);
4246 FDEV(i).start_blk = 0;
4247 FDEV(i).end_blk = FDEV(i).start_blk +
4249 FDEV(i).total_segments) - 1 +
4250 le32_to_cpu(raw_super->segment0_blkaddr);
4252 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
4253 FDEV(i).end_blk = FDEV(i).start_blk +
4255 FDEV(i).total_segments) - 1;
4256 FDEV(i).bdev_file = bdev_file_open_by_path(
4257 FDEV(i).path, mode, sbi->sb, NULL);
4260 if (IS_ERR(FDEV(i).bdev_file))
4261 return PTR_ERR(FDEV(i).bdev_file);
4263 FDEV(i).bdev = file_bdev(FDEV(i).bdev_file);
4264 /* to release errored devices */
4265 sbi->s_ndevs = i + 1;
4267 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
4268 sbi->aligned_blksize = false;
4270 #ifdef CONFIG_BLK_DEV_ZONED
4271 if (bdev_is_zoned(FDEV(i).bdev)) {
4272 if (!f2fs_sb_has_blkzoned(sbi)) {
4273 f2fs_err(sbi, "Zoned block device feature not enabled");
4276 if (init_blkz_info(sbi, i)) {
4277 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
4280 if (max_devices == 1)
4282 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: Host-managed)",
4284 FDEV(i).total_segments,
4285 FDEV(i).start_blk, FDEV(i).end_blk);
4289 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
4291 FDEV(i).total_segments,
4292 FDEV(i).start_blk, FDEV(i).end_blk);
4297 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
4299 #if IS_ENABLED(CONFIG_UNICODE)
4300 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
4301 const struct f2fs_sb_encodings *encoding_info;
4302 struct unicode_map *encoding;
4303 __u16 encoding_flags;
4305 encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
4306 if (!encoding_info) {
4308 "Encoding requested by superblock is unknown");
4312 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
4313 encoding = utf8_load(encoding_info->version);
4314 if (IS_ERR(encoding)) {
4316 "can't mount with superblock charset: %s-%u.%u.%u "
4317 "not supported by the kernel. flags: 0x%x.",
4318 encoding_info->name,
4319 unicode_major(encoding_info->version),
4320 unicode_minor(encoding_info->version),
4321 unicode_rev(encoding_info->version),
4323 return PTR_ERR(encoding);
4325 f2fs_info(sbi, "Using encoding defined by superblock: "
4326 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4327 unicode_major(encoding_info->version),
4328 unicode_minor(encoding_info->version),
4329 unicode_rev(encoding_info->version),
4332 sbi->sb->s_encoding = encoding;
4333 sbi->sb->s_encoding_flags = encoding_flags;
4336 if (f2fs_sb_has_casefold(sbi)) {
4337 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
4344 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
4346 /* adjust parameters according to the volume size */
4347 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
4348 if (f2fs_block_unit_discard(sbi))
4349 SM_I(sbi)->dcc_info->discard_granularity =
4350 MIN_DISCARD_GRANULARITY;
4351 if (!f2fs_lfs_mode(sbi))
4352 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
4353 BIT(F2FS_IPU_HONOR_OPU_WRITE);
4356 sbi->readdir_ra = true;
4359 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
4361 struct f2fs_sb_info *sbi;
4362 struct f2fs_super_block *raw_super;
4365 bool skip_recovery = false, need_fsck = false;
4366 char *options = NULL;
4367 int recovery, i, valid_super_block;
4368 struct curseg_info *seg_i;
4371 bool quota_enabled = false;
4377 valid_super_block = -1;
4380 /* allocate memory for f2fs-specific super block info */
4381 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
4387 /* initialize locks within allocated memory */
4388 init_f2fs_rwsem(&sbi->gc_lock);
4389 mutex_init(&sbi->writepages);
4390 init_f2fs_rwsem(&sbi->cp_global_sem);
4391 init_f2fs_rwsem(&sbi->node_write);
4392 init_f2fs_rwsem(&sbi->node_change);
4393 spin_lock_init(&sbi->stat_lock);
4394 init_f2fs_rwsem(&sbi->cp_rwsem);
4395 init_f2fs_rwsem(&sbi->quota_sem);
4396 init_waitqueue_head(&sbi->cp_wait);
4397 spin_lock_init(&sbi->error_lock);
4399 for (i = 0; i < NR_INODE_TYPE; i++) {
4400 INIT_LIST_HEAD(&sbi->inode_list[i]);
4401 spin_lock_init(&sbi->inode_lock[i]);
4403 mutex_init(&sbi->flush_lock);
4405 /* Load the checksum driver */
4406 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
4407 if (IS_ERR(sbi->s_chksum_driver)) {
4408 f2fs_err(sbi, "Cannot load crc32 driver.");
4409 err = PTR_ERR(sbi->s_chksum_driver);
4410 sbi->s_chksum_driver = NULL;
4414 /* set a block size */
4415 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
4416 f2fs_err(sbi, "unable to set blocksize");
4420 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
4425 sb->s_fs_info = sbi;
4426 sbi->raw_super = raw_super;
4428 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
4429 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
4430 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
4432 /* precompute checksum seed for metadata */
4433 if (f2fs_sb_has_inode_chksum(sbi))
4434 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
4435 sizeof(raw_super->uuid));
4437 default_options(sbi, false);
4438 /* parse mount options */
4439 options = kstrdup((const char *)data, GFP_KERNEL);
4440 if (data && !options) {
4445 err = parse_options(sb, options, false);
4449 sb->s_maxbytes = max_file_blocks(NULL) <<
4450 le32_to_cpu(raw_super->log_blocksize);
4451 sb->s_max_links = F2FS_LINK_MAX;
4453 err = f2fs_setup_casefold(sbi);
4458 sb->dq_op = &f2fs_quota_operations;
4459 sb->s_qcop = &f2fs_quotactl_ops;
4460 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4462 if (f2fs_sb_has_quota_ino(sbi)) {
4463 for (i = 0; i < MAXQUOTAS; i++) {
4464 if (f2fs_qf_ino(sbi->sb, i))
4465 sbi->nquota_files++;
4470 sb->s_op = &f2fs_sops;
4471 #ifdef CONFIG_FS_ENCRYPTION
4472 sb->s_cop = &f2fs_cryptops;
4474 #ifdef CONFIG_FS_VERITY
4475 sb->s_vop = &f2fs_verityops;
4477 sb->s_xattr = f2fs_xattr_handlers;
4478 sb->s_export_op = &f2fs_export_ops;
4479 sb->s_magic = F2FS_SUPER_MAGIC;
4480 sb->s_time_gran = 1;
4481 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4482 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
4483 super_set_uuid(sb, (void *) raw_super->uuid, sizeof(raw_super->uuid));
4484 sb->s_iflags |= SB_I_CGROUPWB;
4486 /* init f2fs-specific super block info */
4487 sbi->valid_super_block = valid_super_block;
4489 /* disallow all the data/node/meta page writes */
4490 set_sbi_flag(sbi, SBI_POR_DOING);
4492 err = f2fs_init_write_merge_io(sbi);
4498 err = f2fs_init_iostat(sbi);
4502 err = init_percpu_info(sbi);
4506 /* init per sbi slab cache */
4507 err = f2fs_init_xattr_caches(sbi);
4510 err = f2fs_init_page_array_cache(sbi);
4512 goto free_xattr_cache;
4514 /* get an inode for meta space */
4515 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
4516 if (IS_ERR(sbi->meta_inode)) {
4517 f2fs_err(sbi, "Failed to read F2FS meta data inode");
4518 err = PTR_ERR(sbi->meta_inode);
4519 goto free_page_array_cache;
4522 err = f2fs_get_valid_checkpoint(sbi);
4524 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
4525 goto free_meta_inode;
4528 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
4529 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
4530 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
4531 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4532 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
4535 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
4536 set_sbi_flag(sbi, SBI_NEED_FSCK);
4538 /* Initialize device list */
4539 err = f2fs_scan_devices(sbi);
4541 f2fs_err(sbi, "Failed to find devices");
4545 err = f2fs_init_post_read_wq(sbi);
4547 f2fs_err(sbi, "Failed to initialize post read workqueue");
4551 sbi->total_valid_node_count =
4552 le32_to_cpu(sbi->ckpt->valid_node_count);
4553 percpu_counter_set(&sbi->total_valid_inode_count,
4554 le32_to_cpu(sbi->ckpt->valid_inode_count));
4555 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
4556 sbi->total_valid_block_count =
4557 le64_to_cpu(sbi->ckpt->valid_block_count);
4558 sbi->last_valid_block_count = sbi->total_valid_block_count;
4559 sbi->reserved_blocks = 0;
4560 sbi->current_reserved_blocks = 0;
4561 limit_reserve_root(sbi);
4562 adjust_unusable_cap_perc(sbi);
4564 f2fs_init_extent_cache_info(sbi);
4566 f2fs_init_ino_entry_info(sbi);
4568 f2fs_init_fsync_node_info(sbi);
4570 /* setup checkpoint request control and start checkpoint issue thread */
4571 f2fs_init_ckpt_req_control(sbi);
4572 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
4573 test_opt(sbi, MERGE_CHECKPOINT)) {
4574 err = f2fs_start_ckpt_thread(sbi);
4577 "Failed to start F2FS issue_checkpoint_thread (%d)",
4579 goto stop_ckpt_thread;
4583 /* setup f2fs internal modules */
4584 err = f2fs_build_segment_manager(sbi);
4586 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
4590 err = f2fs_build_node_manager(sbi);
4592 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
4597 /* For write statistics */
4598 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
4600 /* Read accumulated write IO statistics if exists */
4601 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
4602 if (__exist_node_summaries(sbi))
4603 sbi->kbytes_written =
4604 le64_to_cpu(seg_i->journal->info.kbytes_written);
4606 f2fs_build_gc_manager(sbi);
4608 err = f2fs_build_stats(sbi);
4612 /* get an inode for node space */
4613 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
4614 if (IS_ERR(sbi->node_inode)) {
4615 f2fs_err(sbi, "Failed to read node inode");
4616 err = PTR_ERR(sbi->node_inode);
4620 /* read root inode and dentry */
4621 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
4623 f2fs_err(sbi, "Failed to read root inode");
4624 err = PTR_ERR(root);
4625 goto free_node_inode;
4627 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
4628 !root->i_size || !root->i_nlink) {
4631 goto free_node_inode;
4634 generic_set_sb_d_ops(sb);
4635 sb->s_root = d_make_root(root); /* allocate root dentry */
4638 goto free_node_inode;
4641 err = f2fs_init_compress_inode(sbi);
4643 goto free_root_inode;
4645 err = f2fs_register_sysfs(sbi);
4647 goto free_compress_inode;
4650 /* Enable quota usage during mount */
4651 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
4652 err = f2fs_enable_quotas(sb);
4654 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
4657 quota_enabled = f2fs_recover_quota_begin(sbi);
4659 /* if there are any orphan inodes, free them */
4660 err = f2fs_recover_orphan_inodes(sbi);
4664 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
4665 goto reset_checkpoint;
4667 /* recover fsynced data */
4668 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
4669 !test_opt(sbi, NORECOVERY)) {
4671 * mount should be failed, when device has readonly mode, and
4672 * previous checkpoint was not done by clean system shutdown.
4674 if (f2fs_hw_is_readonly(sbi)) {
4675 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4676 err = f2fs_recover_fsync_data(sbi, true);
4679 f2fs_err(sbi, "Need to recover fsync data, but "
4680 "write access unavailable, please try "
4681 "mount w/ disable_roll_forward or norecovery");
4686 f2fs_info(sbi, "write access unavailable, skipping recovery");
4687 goto reset_checkpoint;
4691 set_sbi_flag(sbi, SBI_NEED_FSCK);
4694 goto reset_checkpoint;
4696 err = f2fs_recover_fsync_data(sbi, false);
4699 skip_recovery = true;
4701 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4706 err = f2fs_recover_fsync_data(sbi, true);
4708 if (!f2fs_readonly(sb) && err > 0) {
4710 f2fs_err(sbi, "Need to recover fsync data");
4716 f2fs_recover_quota_end(sbi, quota_enabled);
4720 * If the f2fs is not readonly and fsync data recovery succeeds,
4721 * check zoned block devices' write pointer consistency.
4723 if (f2fs_sb_has_blkzoned(sbi) && !f2fs_readonly(sb)) {
4726 f2fs_notice(sbi, "Checking entire write pointers");
4727 err2 = f2fs_check_write_pointer(sbi);
4734 err = f2fs_init_inmem_curseg(sbi);
4736 goto sync_free_meta;
4738 /* f2fs_recover_fsync_data() cleared this already */
4739 clear_sbi_flag(sbi, SBI_POR_DOING);
4741 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4742 err = f2fs_disable_checkpoint(sbi);
4744 goto sync_free_meta;
4745 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4746 f2fs_enable_checkpoint(sbi);
4750 * If filesystem is not mounted as read-only then
4751 * do start the gc_thread.
4753 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4754 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
4755 /* After POR, we can run background GC thread.*/
4756 err = f2fs_start_gc_thread(sbi);
4758 goto sync_free_meta;
4762 /* recover broken superblock */
4764 err = f2fs_commit_super(sbi, true);
4765 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4766 sbi->valid_super_block ? 1 : 2, err);
4769 f2fs_join_shrinker(sbi);
4771 f2fs_tuning_parameters(sbi);
4773 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4774 cur_cp_version(F2FS_CKPT(sbi)));
4775 f2fs_update_time(sbi, CP_TIME);
4776 f2fs_update_time(sbi, REQ_TIME);
4777 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4781 /* safe to flush all the data */
4782 sync_filesystem(sbi->sb);
4787 f2fs_truncate_quota_inode_pages(sb);
4788 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
4789 f2fs_quota_off_umount(sbi->sb);
4792 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4793 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4794 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4795 * falls into an infinite loop in f2fs_sync_meta_pages().
4797 truncate_inode_pages_final(META_MAPPING(sbi));
4798 /* evict some inodes being cached by GC */
4800 f2fs_unregister_sysfs(sbi);
4801 free_compress_inode:
4802 f2fs_destroy_compress_inode(sbi);
4807 f2fs_release_ino_entry(sbi, true);
4808 truncate_inode_pages_final(NODE_MAPPING(sbi));
4809 iput(sbi->node_inode);
4810 sbi->node_inode = NULL;
4812 f2fs_destroy_stats(sbi);
4814 /* stop discard thread before destroying node manager */
4815 f2fs_stop_discard_thread(sbi);
4816 f2fs_destroy_node_manager(sbi);
4818 f2fs_destroy_segment_manager(sbi);
4820 f2fs_stop_ckpt_thread(sbi);
4821 /* flush s_error_work before sbi destroy */
4822 flush_work(&sbi->s_error_work);
4823 f2fs_destroy_post_read_wq(sbi);
4825 destroy_device_list(sbi);
4828 make_bad_inode(sbi->meta_inode);
4829 iput(sbi->meta_inode);
4830 sbi->meta_inode = NULL;
4831 free_page_array_cache:
4832 f2fs_destroy_page_array_cache(sbi);
4834 f2fs_destroy_xattr_caches(sbi);
4836 destroy_percpu_info(sbi);
4838 f2fs_destroy_iostat(sbi);
4840 for (i = 0; i < NR_PAGE_TYPE; i++)
4841 kvfree(sbi->write_io[i]);
4843 #if IS_ENABLED(CONFIG_UNICODE)
4844 utf8_unload(sb->s_encoding);
4845 sb->s_encoding = NULL;
4849 for (i = 0; i < MAXQUOTAS; i++)
4850 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4852 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
4857 if (sbi->s_chksum_driver)
4858 crypto_free_shash(sbi->s_chksum_driver);
4860 sb->s_fs_info = NULL;
4862 /* give only one another chance */
4863 if (retry_cnt > 0 && skip_recovery) {
4865 shrink_dcache_sb(sb);
4871 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
4872 const char *dev_name, void *data)
4874 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
4877 static void kill_f2fs_super(struct super_block *sb)
4879 struct f2fs_sb_info *sbi = F2FS_SB(sb);
4882 set_sbi_flag(sbi, SBI_IS_CLOSE);
4883 f2fs_stop_gc_thread(sbi);
4884 f2fs_stop_discard_thread(sbi);
4886 #ifdef CONFIG_F2FS_FS_COMPRESSION
4888 * latter evict_inode() can bypass checking and invalidating
4889 * compress inode cache.
4891 if (test_opt(sbi, COMPRESS_CACHE))
4892 truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
4895 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
4896 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4897 struct cp_control cpc = {
4898 .reason = CP_UMOUNT,
4900 stat_inc_cp_call_count(sbi, TOTAL_CALL);
4901 f2fs_write_checkpoint(sbi, &cpc);
4904 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
4905 sb->s_flags &= ~SB_RDONLY;
4907 kill_block_super(sb);
4908 /* Release block devices last, after fscrypt_destroy_keyring(). */
4910 destroy_device_list(sbi);
4912 sb->s_fs_info = NULL;
4916 static struct file_system_type f2fs_fs_type = {
4917 .owner = THIS_MODULE,
4919 .mount = f2fs_mount,
4920 .kill_sb = kill_f2fs_super,
4921 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
4923 MODULE_ALIAS_FS("f2fs");
4925 static int __init init_inodecache(void)
4927 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
4928 sizeof(struct f2fs_inode_info), 0,
4929 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
4930 return f2fs_inode_cachep ? 0 : -ENOMEM;
4933 static void destroy_inodecache(void)
4936 * Make sure all delayed rcu free inodes are flushed before we
4940 kmem_cache_destroy(f2fs_inode_cachep);
4943 static int __init init_f2fs_fs(void)
4947 err = init_inodecache();
4950 err = f2fs_create_node_manager_caches();
4952 goto free_inodecache;
4953 err = f2fs_create_segment_manager_caches();
4955 goto free_node_manager_caches;
4956 err = f2fs_create_checkpoint_caches();
4958 goto free_segment_manager_caches;
4959 err = f2fs_create_recovery_cache();
4961 goto free_checkpoint_caches;
4962 err = f2fs_create_extent_cache();
4964 goto free_recovery_cache;
4965 err = f2fs_create_garbage_collection_cache();
4967 goto free_extent_cache;
4968 err = f2fs_init_sysfs();
4970 goto free_garbage_collection_cache;
4971 err = f2fs_init_shrinker();
4974 err = register_filesystem(&f2fs_fs_type);
4977 f2fs_create_root_stats();
4978 err = f2fs_init_post_read_processing();
4980 goto free_root_stats;
4981 err = f2fs_init_iostat_processing();
4983 goto free_post_read;
4984 err = f2fs_init_bio_entry_cache();
4987 err = f2fs_init_bioset();
4989 goto free_bio_entry_cache;
4990 err = f2fs_init_compress_mempool();
4993 err = f2fs_init_compress_cache();
4995 goto free_compress_mempool;
4996 err = f2fs_create_casefold_cache();
4998 goto free_compress_cache;
5000 free_compress_cache:
5001 f2fs_destroy_compress_cache();
5002 free_compress_mempool:
5003 f2fs_destroy_compress_mempool();
5005 f2fs_destroy_bioset();
5006 free_bio_entry_cache:
5007 f2fs_destroy_bio_entry_cache();
5009 f2fs_destroy_iostat_processing();
5011 f2fs_destroy_post_read_processing();
5013 f2fs_destroy_root_stats();
5014 unregister_filesystem(&f2fs_fs_type);
5016 f2fs_exit_shrinker();
5019 free_garbage_collection_cache:
5020 f2fs_destroy_garbage_collection_cache();
5022 f2fs_destroy_extent_cache();
5023 free_recovery_cache:
5024 f2fs_destroy_recovery_cache();
5025 free_checkpoint_caches:
5026 f2fs_destroy_checkpoint_caches();
5027 free_segment_manager_caches:
5028 f2fs_destroy_segment_manager_caches();
5029 free_node_manager_caches:
5030 f2fs_destroy_node_manager_caches();
5032 destroy_inodecache();
5037 static void __exit exit_f2fs_fs(void)
5039 f2fs_destroy_casefold_cache();
5040 f2fs_destroy_compress_cache();
5041 f2fs_destroy_compress_mempool();
5042 f2fs_destroy_bioset();
5043 f2fs_destroy_bio_entry_cache();
5044 f2fs_destroy_iostat_processing();
5045 f2fs_destroy_post_read_processing();
5046 f2fs_destroy_root_stats();
5047 unregister_filesystem(&f2fs_fs_type);
5048 f2fs_exit_shrinker();
5050 f2fs_destroy_garbage_collection_cache();
5051 f2fs_destroy_extent_cache();
5052 f2fs_destroy_recovery_cache();
5053 f2fs_destroy_checkpoint_caches();
5054 f2fs_destroy_segment_manager_caches();
5055 f2fs_destroy_node_manager_caches();
5056 destroy_inodecache();
5059 module_init(init_f2fs_fs)
5060 module_exit(exit_f2fs_fs)
5062 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
5063 MODULE_DESCRIPTION("Flash Friendly File System");
5064 MODULE_LICENSE("GPL");
5065 MODULE_SOFTDEP("pre: crc32");