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] = "invalid blkaddr",
67 void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
70 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
73 atomic_set(&ffi->inject_ops, 0);
74 ffi->inject_rate = rate;
78 ffi->inject_type = type;
81 memset(ffi, 0, sizeof(struct f2fs_fault_info));
85 /* f2fs-wide shrinker description */
86 static struct shrinker f2fs_shrinker_info = {
87 .scan_objects = f2fs_shrink_scan,
88 .count_objects = f2fs_shrink_count,
89 .seeks = DEFAULT_SEEKS,
94 Opt_disable_roll_forward,
105 Opt_disable_ext_identify,
108 Opt_inline_xattr_size,
146 Opt_test_dummy_encryption,
148 Opt_checkpoint_disable,
149 Opt_checkpoint_disable_cap,
150 Opt_checkpoint_disable_cap_perc,
151 Opt_checkpoint_enable,
152 Opt_checkpoint_merge,
153 Opt_nocheckpoint_merge,
154 Opt_compress_algorithm,
155 Opt_compress_log_size,
156 Opt_compress_extension,
157 Opt_nocompress_extension,
166 Opt_age_extent_cache,
171 static match_table_t f2fs_tokens = {
172 {Opt_gc_background, "background_gc=%s"},
173 {Opt_disable_roll_forward, "disable_roll_forward"},
174 {Opt_norecovery, "norecovery"},
175 {Opt_discard, "discard"},
176 {Opt_nodiscard, "nodiscard"},
177 {Opt_noheap, "no_heap"},
179 {Opt_user_xattr, "user_xattr"},
180 {Opt_nouser_xattr, "nouser_xattr"},
182 {Opt_noacl, "noacl"},
183 {Opt_active_logs, "active_logs=%u"},
184 {Opt_disable_ext_identify, "disable_ext_identify"},
185 {Opt_inline_xattr, "inline_xattr"},
186 {Opt_noinline_xattr, "noinline_xattr"},
187 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
188 {Opt_inline_data, "inline_data"},
189 {Opt_inline_dentry, "inline_dentry"},
190 {Opt_noinline_dentry, "noinline_dentry"},
191 {Opt_flush_merge, "flush_merge"},
192 {Opt_noflush_merge, "noflush_merge"},
193 {Opt_barrier, "barrier"},
194 {Opt_nobarrier, "nobarrier"},
195 {Opt_fastboot, "fastboot"},
196 {Opt_extent_cache, "extent_cache"},
197 {Opt_noextent_cache, "noextent_cache"},
198 {Opt_noinline_data, "noinline_data"},
199 {Opt_data_flush, "data_flush"},
200 {Opt_reserve_root, "reserve_root=%u"},
201 {Opt_resgid, "resgid=%u"},
202 {Opt_resuid, "resuid=%u"},
203 {Opt_mode, "mode=%s"},
204 {Opt_io_size_bits, "io_bits=%u"},
205 {Opt_fault_injection, "fault_injection=%u"},
206 {Opt_fault_type, "fault_type=%u"},
207 {Opt_lazytime, "lazytime"},
208 {Opt_nolazytime, "nolazytime"},
209 {Opt_quota, "quota"},
210 {Opt_noquota, "noquota"},
211 {Opt_usrquota, "usrquota"},
212 {Opt_grpquota, "grpquota"},
213 {Opt_prjquota, "prjquota"},
214 {Opt_usrjquota, "usrjquota=%s"},
215 {Opt_grpjquota, "grpjquota=%s"},
216 {Opt_prjjquota, "prjjquota=%s"},
217 {Opt_offusrjquota, "usrjquota="},
218 {Opt_offgrpjquota, "grpjquota="},
219 {Opt_offprjjquota, "prjjquota="},
220 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
221 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
222 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
223 {Opt_alloc, "alloc_mode=%s"},
224 {Opt_fsync, "fsync_mode=%s"},
225 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
226 {Opt_test_dummy_encryption, "test_dummy_encryption"},
227 {Opt_inlinecrypt, "inlinecrypt"},
228 {Opt_checkpoint_disable, "checkpoint=disable"},
229 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
230 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
231 {Opt_checkpoint_enable, "checkpoint=enable"},
232 {Opt_checkpoint_merge, "checkpoint_merge"},
233 {Opt_nocheckpoint_merge, "nocheckpoint_merge"},
234 {Opt_compress_algorithm, "compress_algorithm=%s"},
235 {Opt_compress_log_size, "compress_log_size=%u"},
236 {Opt_compress_extension, "compress_extension=%s"},
237 {Opt_nocompress_extension, "nocompress_extension=%s"},
238 {Opt_compress_chksum, "compress_chksum"},
239 {Opt_compress_mode, "compress_mode=%s"},
240 {Opt_compress_cache, "compress_cache"},
242 {Opt_gc_merge, "gc_merge"},
243 {Opt_nogc_merge, "nogc_merge"},
244 {Opt_discard_unit, "discard_unit=%s"},
245 {Opt_memory_mode, "memory=%s"},
246 {Opt_age_extent_cache, "age_extent_cache"},
247 {Opt_errors, "errors=%s"},
251 void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...)
253 struct va_format vaf;
259 level = printk_get_level(fmt);
260 vaf.fmt = printk_skip_level(fmt);
262 printk("%c%cF2FS-fs (%s): %pV\n",
263 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
268 #if IS_ENABLED(CONFIG_UNICODE)
269 static const struct f2fs_sb_encodings {
272 unsigned int version;
273 } f2fs_sb_encoding_map[] = {
274 {F2FS_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
277 static const struct f2fs_sb_encodings *
278 f2fs_sb_read_encoding(const struct f2fs_super_block *sb)
280 __u16 magic = le16_to_cpu(sb->s_encoding);
283 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
284 if (magic == f2fs_sb_encoding_map[i].magic)
285 return &f2fs_sb_encoding_map[i];
290 struct kmem_cache *f2fs_cf_name_slab;
291 static int __init f2fs_create_casefold_cache(void)
293 f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
295 return f2fs_cf_name_slab ? 0 : -ENOMEM;
298 static void f2fs_destroy_casefold_cache(void)
300 kmem_cache_destroy(f2fs_cf_name_slab);
303 static int __init f2fs_create_casefold_cache(void) { return 0; }
304 static void f2fs_destroy_casefold_cache(void) { }
307 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
309 block_t limit = min((sbi->user_block_count >> 3),
310 sbi->user_block_count - sbi->reserved_blocks);
313 if (test_opt(sbi, RESERVE_ROOT) &&
314 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
315 F2FS_OPTION(sbi).root_reserved_blocks = limit;
316 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
317 F2FS_OPTION(sbi).root_reserved_blocks);
319 if (!test_opt(sbi, RESERVE_ROOT) &&
320 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
321 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
322 !gid_eq(F2FS_OPTION(sbi).s_resgid,
323 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
324 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
325 from_kuid_munged(&init_user_ns,
326 F2FS_OPTION(sbi).s_resuid),
327 from_kgid_munged(&init_user_ns,
328 F2FS_OPTION(sbi).s_resgid));
331 static inline int adjust_reserved_segment(struct f2fs_sb_info *sbi)
333 unsigned int sec_blks = sbi->blocks_per_seg * sbi->segs_per_sec;
334 unsigned int avg_vblocks;
335 unsigned int wanted_reserved_segments;
336 block_t avail_user_block_count;
338 if (!F2FS_IO_ALIGNED(sbi))
341 /* average valid block count in section in worst case */
342 avg_vblocks = sec_blks / F2FS_IO_SIZE(sbi);
345 * we need enough free space when migrating one section in worst case
347 wanted_reserved_segments = (F2FS_IO_SIZE(sbi) / avg_vblocks) *
348 reserved_segments(sbi);
349 wanted_reserved_segments -= reserved_segments(sbi);
351 avail_user_block_count = sbi->user_block_count -
352 sbi->current_reserved_blocks -
353 F2FS_OPTION(sbi).root_reserved_blocks;
355 if (wanted_reserved_segments * sbi->blocks_per_seg >
356 avail_user_block_count) {
357 f2fs_err(sbi, "IO align feature can't grab additional reserved segment: %u, available segments: %u",
358 wanted_reserved_segments,
359 avail_user_block_count >> sbi->log_blocks_per_seg);
363 SM_I(sbi)->additional_reserved_segments = wanted_reserved_segments;
365 f2fs_info(sbi, "IO align feature needs additional reserved segment: %u",
366 wanted_reserved_segments);
371 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
373 if (!F2FS_OPTION(sbi).unusable_cap_perc)
376 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
377 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
379 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
380 F2FS_OPTION(sbi).unusable_cap_perc;
382 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
383 F2FS_OPTION(sbi).unusable_cap,
384 F2FS_OPTION(sbi).unusable_cap_perc);
387 static void init_once(void *foo)
389 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
391 inode_init_once(&fi->vfs_inode);
395 static const char * const quotatypes[] = INITQFNAMES;
396 #define QTYPE2NAME(t) (quotatypes[t])
397 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
400 struct f2fs_sb_info *sbi = F2FS_SB(sb);
404 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
405 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
408 if (f2fs_sb_has_quota_ino(sbi)) {
409 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
413 qname = match_strdup(args);
415 f2fs_err(sbi, "Not enough memory for storing quotafile name");
418 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
419 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
422 f2fs_err(sbi, "%s quota file already specified",
426 if (strchr(qname, '/')) {
427 f2fs_err(sbi, "quotafile must be on filesystem root");
430 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
438 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
440 struct f2fs_sb_info *sbi = F2FS_SB(sb);
442 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
443 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
446 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
447 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
451 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
454 * We do the test below only for project quotas. 'usrquota' and
455 * 'grpquota' mount options are allowed even without quota feature
456 * to support legacy quotas in quota files.
458 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
459 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
462 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
463 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
464 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
465 if (test_opt(sbi, USRQUOTA) &&
466 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
467 clear_opt(sbi, USRQUOTA);
469 if (test_opt(sbi, GRPQUOTA) &&
470 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
471 clear_opt(sbi, GRPQUOTA);
473 if (test_opt(sbi, PRJQUOTA) &&
474 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
475 clear_opt(sbi, PRJQUOTA);
477 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
478 test_opt(sbi, PRJQUOTA)) {
479 f2fs_err(sbi, "old and new quota format mixing");
483 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
484 f2fs_err(sbi, "journaled quota format not specified");
489 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
490 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
491 F2FS_OPTION(sbi).s_jquota_fmt = 0;
497 static int f2fs_set_test_dummy_encryption(struct super_block *sb,
499 const substring_t *arg,
502 struct f2fs_sb_info *sbi = F2FS_SB(sb);
503 struct fs_parameter param = {
504 .type = fs_value_is_string,
505 .string = arg->from ? arg->from : "",
507 struct fscrypt_dummy_policy *policy =
508 &F2FS_OPTION(sbi).dummy_enc_policy;
511 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
512 f2fs_warn(sbi, "test_dummy_encryption option not supported");
516 if (!f2fs_sb_has_encrypt(sbi)) {
517 f2fs_err(sbi, "Encrypt feature is off");
522 * This mount option is just for testing, and it's not worthwhile to
523 * implement the extra complexity (e.g. RCU protection) that would be
524 * needed to allow it to be set or changed during remount. We do allow
525 * it to be specified during remount, but only if there is no change.
527 if (is_remount && !fscrypt_is_dummy_policy_set(policy)) {
528 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
532 err = fscrypt_parse_test_dummy_encryption(¶m, policy);
536 "Can't change test_dummy_encryption on remount");
537 else if (err == -EINVAL)
538 f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
541 f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
545 f2fs_warn(sbi, "Test dummy encryption mode enabled");
549 #ifdef CONFIG_F2FS_FS_COMPRESSION
551 * 1. The same extension name cannot not appear in both compress and non-compress extension
553 * 2. If the compress extension specifies all files, the types specified by the non-compress
554 * extension will be treated as special cases and will not be compressed.
555 * 3. Don't allow the non-compress extension specifies all files.
557 static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
559 unsigned char (*ext)[F2FS_EXTENSION_LEN];
560 unsigned char (*noext)[F2FS_EXTENSION_LEN];
561 int ext_cnt, noext_cnt, index = 0, no_index = 0;
563 ext = F2FS_OPTION(sbi).extensions;
564 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
565 noext = F2FS_OPTION(sbi).noextensions;
566 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
571 for (no_index = 0; no_index < noext_cnt; no_index++) {
572 if (!strcasecmp("*", noext[no_index])) {
573 f2fs_info(sbi, "Don't allow the nocompress extension specifies all files");
576 for (index = 0; index < ext_cnt; index++) {
577 if (!strcasecmp(ext[index], noext[no_index])) {
578 f2fs_info(sbi, "Don't allow the same extension %s appear in both compress and nocompress extension",
587 #ifdef CONFIG_F2FS_FS_LZ4
588 static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
590 #ifdef CONFIG_F2FS_FS_LZ4HC
593 if (strlen(str) == 3) {
594 F2FS_OPTION(sbi).compress_level = 0;
601 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
604 if (kstrtouint(str + 1, 10, &level))
607 if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
608 f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
612 F2FS_OPTION(sbi).compress_level = level;
615 if (strlen(str) == 3) {
616 F2FS_OPTION(sbi).compress_level = 0;
619 f2fs_info(sbi, "kernel doesn't support lz4hc compression");
625 #ifdef CONFIG_F2FS_FS_ZSTD
626 static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
631 if (strlen(str) == len) {
632 F2FS_OPTION(sbi).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
639 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
642 if (kstrtouint(str + 1, 10, &level))
645 if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
646 f2fs_info(sbi, "invalid zstd compress level: %d", level);
650 F2FS_OPTION(sbi).compress_level = level;
656 static int parse_options(struct super_block *sb, char *options, bool is_remount)
658 struct f2fs_sb_info *sbi = F2FS_SB(sb);
659 substring_t args[MAX_OPT_ARGS];
660 #ifdef CONFIG_F2FS_FS_COMPRESSION
661 unsigned char (*ext)[F2FS_EXTENSION_LEN];
662 unsigned char (*noext)[F2FS_EXTENSION_LEN];
663 int ext_cnt, noext_cnt;
674 while ((p = strsep(&options, ",")) != NULL) {
680 * Initialize args struct so we know whether arg was
681 * found; some options take optional arguments.
683 args[0].to = args[0].from = NULL;
684 token = match_token(p, f2fs_tokens, args);
687 case Opt_gc_background:
688 name = match_strdup(&args[0]);
692 if (!strcmp(name, "on")) {
693 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
694 } else if (!strcmp(name, "off")) {
695 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
696 } else if (!strcmp(name, "sync")) {
697 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
704 case Opt_disable_roll_forward:
705 set_opt(sbi, DISABLE_ROLL_FORWARD);
708 /* this option mounts f2fs with ro */
709 set_opt(sbi, NORECOVERY);
710 if (!f2fs_readonly(sb))
714 if (!f2fs_hw_support_discard(sbi)) {
715 f2fs_warn(sbi, "device does not support discard");
718 set_opt(sbi, DISCARD);
721 if (f2fs_hw_should_discard(sbi)) {
722 f2fs_warn(sbi, "discard is required for zoned block devices");
725 clear_opt(sbi, DISCARD);
728 set_opt(sbi, NOHEAP);
731 clear_opt(sbi, NOHEAP);
733 #ifdef CONFIG_F2FS_FS_XATTR
735 set_opt(sbi, XATTR_USER);
737 case Opt_nouser_xattr:
738 clear_opt(sbi, XATTR_USER);
740 case Opt_inline_xattr:
741 set_opt(sbi, INLINE_XATTR);
743 case Opt_noinline_xattr:
744 clear_opt(sbi, INLINE_XATTR);
746 case Opt_inline_xattr_size:
747 if (args->from && match_int(args, &arg))
749 set_opt(sbi, INLINE_XATTR_SIZE);
750 F2FS_OPTION(sbi).inline_xattr_size = arg;
754 f2fs_info(sbi, "user_xattr options not supported");
756 case Opt_nouser_xattr:
757 f2fs_info(sbi, "nouser_xattr options not supported");
759 case Opt_inline_xattr:
760 f2fs_info(sbi, "inline_xattr options not supported");
762 case Opt_noinline_xattr:
763 f2fs_info(sbi, "noinline_xattr options not supported");
766 #ifdef CONFIG_F2FS_FS_POSIX_ACL
768 set_opt(sbi, POSIX_ACL);
771 clear_opt(sbi, POSIX_ACL);
775 f2fs_info(sbi, "acl options not supported");
778 f2fs_info(sbi, "noacl options not supported");
781 case Opt_active_logs:
782 if (args->from && match_int(args, &arg))
784 if (arg != 2 && arg != 4 &&
785 arg != NR_CURSEG_PERSIST_TYPE)
787 F2FS_OPTION(sbi).active_logs = arg;
789 case Opt_disable_ext_identify:
790 set_opt(sbi, DISABLE_EXT_IDENTIFY);
792 case Opt_inline_data:
793 set_opt(sbi, INLINE_DATA);
795 case Opt_inline_dentry:
796 set_opt(sbi, INLINE_DENTRY);
798 case Opt_noinline_dentry:
799 clear_opt(sbi, INLINE_DENTRY);
801 case Opt_flush_merge:
802 set_opt(sbi, FLUSH_MERGE);
804 case Opt_noflush_merge:
805 clear_opt(sbi, FLUSH_MERGE);
808 set_opt(sbi, NOBARRIER);
811 clear_opt(sbi, NOBARRIER);
814 set_opt(sbi, FASTBOOT);
816 case Opt_extent_cache:
817 set_opt(sbi, READ_EXTENT_CACHE);
819 case Opt_noextent_cache:
820 clear_opt(sbi, READ_EXTENT_CACHE);
822 case Opt_noinline_data:
823 clear_opt(sbi, INLINE_DATA);
826 set_opt(sbi, DATA_FLUSH);
828 case Opt_reserve_root:
829 if (args->from && match_int(args, &arg))
831 if (test_opt(sbi, RESERVE_ROOT)) {
832 f2fs_info(sbi, "Preserve previous reserve_root=%u",
833 F2FS_OPTION(sbi).root_reserved_blocks);
835 F2FS_OPTION(sbi).root_reserved_blocks = arg;
836 set_opt(sbi, RESERVE_ROOT);
840 if (args->from && match_int(args, &arg))
842 uid = make_kuid(current_user_ns(), arg);
843 if (!uid_valid(uid)) {
844 f2fs_err(sbi, "Invalid uid value %d", arg);
847 F2FS_OPTION(sbi).s_resuid = uid;
850 if (args->from && match_int(args, &arg))
852 gid = make_kgid(current_user_ns(), arg);
853 if (!gid_valid(gid)) {
854 f2fs_err(sbi, "Invalid gid value %d", arg);
857 F2FS_OPTION(sbi).s_resgid = gid;
860 name = match_strdup(&args[0]);
864 if (!strcmp(name, "adaptive")) {
865 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
866 } else if (!strcmp(name, "lfs")) {
867 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
868 } else if (!strcmp(name, "fragment:segment")) {
869 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_SEG;
870 } else if (!strcmp(name, "fragment:block")) {
871 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_BLK;
878 case Opt_io_size_bits:
879 if (args->from && match_int(args, &arg))
881 if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_VECS)) {
882 f2fs_warn(sbi, "Not support %ld, larger than %d",
883 BIT(arg), BIO_MAX_VECS);
886 F2FS_OPTION(sbi).write_io_size_bits = arg;
888 #ifdef CONFIG_F2FS_FAULT_INJECTION
889 case Opt_fault_injection:
890 if (args->from && match_int(args, &arg))
892 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
893 set_opt(sbi, FAULT_INJECTION);
897 if (args->from && match_int(args, &arg))
899 f2fs_build_fault_attr(sbi, 0, arg);
900 set_opt(sbi, FAULT_INJECTION);
903 case Opt_fault_injection:
904 f2fs_info(sbi, "fault_injection options not supported");
908 f2fs_info(sbi, "fault_type options not supported");
912 sb->s_flags |= SB_LAZYTIME;
915 sb->s_flags &= ~SB_LAZYTIME;
920 set_opt(sbi, USRQUOTA);
923 set_opt(sbi, GRPQUOTA);
926 set_opt(sbi, PRJQUOTA);
929 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
934 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
939 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
943 case Opt_offusrjquota:
944 ret = f2fs_clear_qf_name(sb, USRQUOTA);
948 case Opt_offgrpjquota:
949 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
953 case Opt_offprjjquota:
954 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
958 case Opt_jqfmt_vfsold:
959 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
961 case Opt_jqfmt_vfsv0:
962 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
964 case Opt_jqfmt_vfsv1:
965 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
968 clear_opt(sbi, QUOTA);
969 clear_opt(sbi, USRQUOTA);
970 clear_opt(sbi, GRPQUOTA);
971 clear_opt(sbi, PRJQUOTA);
981 case Opt_offusrjquota:
982 case Opt_offgrpjquota:
983 case Opt_offprjjquota:
984 case Opt_jqfmt_vfsold:
985 case Opt_jqfmt_vfsv0:
986 case Opt_jqfmt_vfsv1:
988 f2fs_info(sbi, "quota operations not supported");
992 name = match_strdup(&args[0]);
996 if (!strcmp(name, "default")) {
997 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
998 } else if (!strcmp(name, "reuse")) {
999 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
1007 name = match_strdup(&args[0]);
1010 if (!strcmp(name, "posix")) {
1011 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1012 } else if (!strcmp(name, "strict")) {
1013 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
1014 } else if (!strcmp(name, "nobarrier")) {
1015 F2FS_OPTION(sbi).fsync_mode =
1016 FSYNC_MODE_NOBARRIER;
1023 case Opt_test_dummy_encryption:
1024 ret = f2fs_set_test_dummy_encryption(sb, p, &args[0],
1029 case Opt_inlinecrypt:
1030 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1031 sb->s_flags |= SB_INLINECRYPT;
1033 f2fs_info(sbi, "inline encryption not supported");
1036 case Opt_checkpoint_disable_cap_perc:
1037 if (args->from && match_int(args, &arg))
1039 if (arg < 0 || arg > 100)
1041 F2FS_OPTION(sbi).unusable_cap_perc = arg;
1042 set_opt(sbi, DISABLE_CHECKPOINT);
1044 case Opt_checkpoint_disable_cap:
1045 if (args->from && match_int(args, &arg))
1047 F2FS_OPTION(sbi).unusable_cap = arg;
1048 set_opt(sbi, DISABLE_CHECKPOINT);
1050 case Opt_checkpoint_disable:
1051 set_opt(sbi, DISABLE_CHECKPOINT);
1053 case Opt_checkpoint_enable:
1054 clear_opt(sbi, DISABLE_CHECKPOINT);
1056 case Opt_checkpoint_merge:
1057 set_opt(sbi, MERGE_CHECKPOINT);
1059 case Opt_nocheckpoint_merge:
1060 clear_opt(sbi, MERGE_CHECKPOINT);
1062 #ifdef CONFIG_F2FS_FS_COMPRESSION
1063 case Opt_compress_algorithm:
1064 if (!f2fs_sb_has_compression(sbi)) {
1065 f2fs_info(sbi, "Image doesn't support compression");
1068 name = match_strdup(&args[0]);
1071 if (!strcmp(name, "lzo")) {
1072 #ifdef CONFIG_F2FS_FS_LZO
1073 F2FS_OPTION(sbi).compress_level = 0;
1074 F2FS_OPTION(sbi).compress_algorithm =
1077 f2fs_info(sbi, "kernel doesn't support lzo compression");
1079 } else if (!strncmp(name, "lz4", 3)) {
1080 #ifdef CONFIG_F2FS_FS_LZ4
1081 ret = f2fs_set_lz4hc_level(sbi, name);
1086 F2FS_OPTION(sbi).compress_algorithm =
1089 f2fs_info(sbi, "kernel doesn't support lz4 compression");
1091 } else if (!strncmp(name, "zstd", 4)) {
1092 #ifdef CONFIG_F2FS_FS_ZSTD
1093 ret = f2fs_set_zstd_level(sbi, name);
1098 F2FS_OPTION(sbi).compress_algorithm =
1101 f2fs_info(sbi, "kernel doesn't support zstd compression");
1103 } else if (!strcmp(name, "lzo-rle")) {
1104 #ifdef CONFIG_F2FS_FS_LZORLE
1105 F2FS_OPTION(sbi).compress_level = 0;
1106 F2FS_OPTION(sbi).compress_algorithm =
1109 f2fs_info(sbi, "kernel doesn't support lzorle compression");
1117 case Opt_compress_log_size:
1118 if (!f2fs_sb_has_compression(sbi)) {
1119 f2fs_info(sbi, "Image doesn't support compression");
1122 if (args->from && match_int(args, &arg))
1124 if (arg < MIN_COMPRESS_LOG_SIZE ||
1125 arg > MAX_COMPRESS_LOG_SIZE) {
1127 "Compress cluster log size is out of range");
1130 F2FS_OPTION(sbi).compress_log_size = arg;
1132 case Opt_compress_extension:
1133 if (!f2fs_sb_has_compression(sbi)) {
1134 f2fs_info(sbi, "Image doesn't support compression");
1137 name = match_strdup(&args[0]);
1141 ext = F2FS_OPTION(sbi).extensions;
1142 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1144 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1145 ext_cnt >= COMPRESS_EXT_NUM) {
1147 "invalid extension length/number");
1152 strcpy(ext[ext_cnt], name);
1153 F2FS_OPTION(sbi).compress_ext_cnt++;
1156 case Opt_nocompress_extension:
1157 if (!f2fs_sb_has_compression(sbi)) {
1158 f2fs_info(sbi, "Image doesn't support compression");
1161 name = match_strdup(&args[0]);
1165 noext = F2FS_OPTION(sbi).noextensions;
1166 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
1168 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1169 noext_cnt >= COMPRESS_EXT_NUM) {
1171 "invalid extension length/number");
1176 strcpy(noext[noext_cnt], name);
1177 F2FS_OPTION(sbi).nocompress_ext_cnt++;
1180 case Opt_compress_chksum:
1181 if (!f2fs_sb_has_compression(sbi)) {
1182 f2fs_info(sbi, "Image doesn't support compression");
1185 F2FS_OPTION(sbi).compress_chksum = true;
1187 case Opt_compress_mode:
1188 if (!f2fs_sb_has_compression(sbi)) {
1189 f2fs_info(sbi, "Image doesn't support compression");
1192 name = match_strdup(&args[0]);
1195 if (!strcmp(name, "fs")) {
1196 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1197 } else if (!strcmp(name, "user")) {
1198 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1205 case Opt_compress_cache:
1206 if (!f2fs_sb_has_compression(sbi)) {
1207 f2fs_info(sbi, "Image doesn't support compression");
1210 set_opt(sbi, COMPRESS_CACHE);
1213 case Opt_compress_algorithm:
1214 case Opt_compress_log_size:
1215 case Opt_compress_extension:
1216 case Opt_nocompress_extension:
1217 case Opt_compress_chksum:
1218 case Opt_compress_mode:
1219 case Opt_compress_cache:
1220 f2fs_info(sbi, "compression options not supported");
1227 set_opt(sbi, GC_MERGE);
1229 case Opt_nogc_merge:
1230 clear_opt(sbi, GC_MERGE);
1232 case Opt_discard_unit:
1233 name = match_strdup(&args[0]);
1236 if (!strcmp(name, "block")) {
1237 F2FS_OPTION(sbi).discard_unit =
1239 } else if (!strcmp(name, "segment")) {
1240 F2FS_OPTION(sbi).discard_unit =
1241 DISCARD_UNIT_SEGMENT;
1242 } else if (!strcmp(name, "section")) {
1243 F2FS_OPTION(sbi).discard_unit =
1244 DISCARD_UNIT_SECTION;
1251 case Opt_memory_mode:
1252 name = match_strdup(&args[0]);
1255 if (!strcmp(name, "normal")) {
1256 F2FS_OPTION(sbi).memory_mode =
1258 } else if (!strcmp(name, "low")) {
1259 F2FS_OPTION(sbi).memory_mode =
1267 case Opt_age_extent_cache:
1268 set_opt(sbi, AGE_EXTENT_CACHE);
1271 name = match_strdup(&args[0]);
1274 if (!strcmp(name, "remount-ro")) {
1275 F2FS_OPTION(sbi).errors =
1276 MOUNT_ERRORS_READONLY;
1277 } else if (!strcmp(name, "continue")) {
1278 F2FS_OPTION(sbi).errors =
1279 MOUNT_ERRORS_CONTINUE;
1280 } else if (!strcmp(name, "panic")) {
1281 F2FS_OPTION(sbi).errors =
1290 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1297 if (f2fs_check_quota_options(sbi))
1300 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
1301 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1304 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
1305 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1309 #if !IS_ENABLED(CONFIG_UNICODE)
1310 if (f2fs_sb_has_casefold(sbi)) {
1312 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1317 * The BLKZONED feature indicates that the drive was formatted with
1318 * zone alignment optimization. This is optional for host-aware
1319 * devices, but mandatory for host-managed zoned block devices.
1321 if (f2fs_sb_has_blkzoned(sbi)) {
1322 #ifdef CONFIG_BLK_DEV_ZONED
1323 if (F2FS_OPTION(sbi).discard_unit !=
1324 DISCARD_UNIT_SECTION) {
1325 f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default");
1326 F2FS_OPTION(sbi).discard_unit =
1327 DISCARD_UNIT_SECTION;
1330 if (F2FS_OPTION(sbi).fs_mode != FS_MODE_LFS) {
1331 f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature");
1335 f2fs_err(sbi, "Zoned block device support is not enabled");
1340 #ifdef CONFIG_F2FS_FS_COMPRESSION
1341 if (f2fs_test_compress_extension(sbi)) {
1342 f2fs_err(sbi, "invalid compress or nocompress extension");
1347 if (F2FS_IO_SIZE_BITS(sbi) && !f2fs_lfs_mode(sbi)) {
1348 f2fs_err(sbi, "Should set mode=lfs with %luKB-sized IO",
1349 F2FS_IO_SIZE_KB(sbi));
1353 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
1354 int min_size, max_size;
1356 if (!f2fs_sb_has_extra_attr(sbi) ||
1357 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
1358 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1361 if (!test_opt(sbi, INLINE_XATTR)) {
1362 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1366 min_size = MIN_INLINE_XATTR_SIZE;
1367 max_size = MAX_INLINE_XATTR_SIZE;
1369 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1370 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
1371 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1372 min_size, max_size);
1377 if (test_opt(sbi, DISABLE_CHECKPOINT) && f2fs_lfs_mode(sbi)) {
1378 f2fs_err(sbi, "LFS is not compatible with checkpoint=disable");
1382 if (test_opt(sbi, ATGC) && f2fs_lfs_mode(sbi)) {
1383 f2fs_err(sbi, "LFS is not compatible with ATGC");
1387 if (f2fs_is_readonly(sbi) && test_opt(sbi, FLUSH_MERGE)) {
1388 f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
1392 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1393 f2fs_err(sbi, "Allow to mount readonly mode only");
1399 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1401 struct f2fs_inode_info *fi;
1403 if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC))
1406 fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO);
1410 init_once((void *) fi);
1412 /* Initialize f2fs-specific inode info */
1413 atomic_set(&fi->dirty_pages, 0);
1414 atomic_set(&fi->i_compr_blocks, 0);
1415 init_f2fs_rwsem(&fi->i_sem);
1416 spin_lock_init(&fi->i_size_lock);
1417 INIT_LIST_HEAD(&fi->dirty_list);
1418 INIT_LIST_HEAD(&fi->gdirty_list);
1419 init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
1420 init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
1421 init_f2fs_rwsem(&fi->i_xattr_sem);
1423 /* Will be used by directory only */
1424 fi->i_dir_level = F2FS_SB(sb)->dir_level;
1426 return &fi->vfs_inode;
1429 static int f2fs_drop_inode(struct inode *inode)
1431 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1435 * during filesystem shutdown, if checkpoint is disabled,
1436 * drop useless meta/node dirty pages.
1438 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1439 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1440 inode->i_ino == F2FS_META_INO(sbi)) {
1441 trace_f2fs_drop_inode(inode, 1);
1447 * This is to avoid a deadlock condition like below.
1448 * writeback_single_inode(inode)
1449 * - f2fs_write_data_page
1450 * - f2fs_gc -> iput -> evict
1451 * - inode_wait_for_writeback(inode)
1453 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
1454 if (!inode->i_nlink && !is_bad_inode(inode)) {
1455 /* to avoid evict_inode call simultaneously */
1456 atomic_inc(&inode->i_count);
1457 spin_unlock(&inode->i_lock);
1459 /* should remain fi->extent_tree for writepage */
1460 f2fs_destroy_extent_node(inode);
1462 sb_start_intwrite(inode->i_sb);
1463 f2fs_i_size_write(inode, 0);
1465 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1466 inode, NULL, 0, DATA);
1467 truncate_inode_pages_final(inode->i_mapping);
1469 if (F2FS_HAS_BLOCKS(inode))
1470 f2fs_truncate(inode);
1472 sb_end_intwrite(inode->i_sb);
1474 spin_lock(&inode->i_lock);
1475 atomic_dec(&inode->i_count);
1477 trace_f2fs_drop_inode(inode, 0);
1480 ret = generic_drop_inode(inode);
1482 ret = fscrypt_drop_inode(inode);
1483 trace_f2fs_drop_inode(inode, ret);
1487 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1489 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1492 spin_lock(&sbi->inode_lock[DIRTY_META]);
1493 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1496 set_inode_flag(inode, FI_DIRTY_INODE);
1497 stat_inc_dirty_inode(sbi, DIRTY_META);
1499 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1500 list_add_tail(&F2FS_I(inode)->gdirty_list,
1501 &sbi->inode_list[DIRTY_META]);
1502 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1504 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1508 void f2fs_inode_synced(struct inode *inode)
1510 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1512 spin_lock(&sbi->inode_lock[DIRTY_META]);
1513 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1514 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1517 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1518 list_del_init(&F2FS_I(inode)->gdirty_list);
1519 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1521 clear_inode_flag(inode, FI_DIRTY_INODE);
1522 clear_inode_flag(inode, FI_AUTO_RECOVER);
1523 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1524 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1528 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1530 * We should call set_dirty_inode to write the dirty inode through write_inode.
1532 static void f2fs_dirty_inode(struct inode *inode, int flags)
1534 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1536 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1537 inode->i_ino == F2FS_META_INO(sbi))
1540 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1541 clear_inode_flag(inode, FI_AUTO_RECOVER);
1543 f2fs_inode_dirtied(inode, false);
1546 static void f2fs_free_inode(struct inode *inode)
1548 fscrypt_free_inode(inode);
1549 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1552 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1554 percpu_counter_destroy(&sbi->total_valid_inode_count);
1555 percpu_counter_destroy(&sbi->rf_node_block_count);
1556 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1559 static void destroy_device_list(struct f2fs_sb_info *sbi)
1563 for (i = 0; i < sbi->s_ndevs; i++) {
1565 blkdev_put(FDEV(i).bdev, sbi->sb);
1566 #ifdef CONFIG_BLK_DEV_ZONED
1567 kvfree(FDEV(i).blkz_seq);
1573 static void f2fs_put_super(struct super_block *sb)
1575 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1580 /* unregister procfs/sysfs entries in advance to avoid race case */
1581 f2fs_unregister_sysfs(sbi);
1583 f2fs_quota_off_umount(sb);
1585 /* prevent remaining shrinker jobs */
1586 mutex_lock(&sbi->umount_mutex);
1589 * flush all issued checkpoints and stop checkpoint issue thread.
1590 * after then, all checkpoints should be done by each process context.
1592 f2fs_stop_ckpt_thread(sbi);
1595 * We don't need to do checkpoint when superblock is clean.
1596 * But, the previous checkpoint was not done by umount, it needs to do
1597 * clean checkpoint again.
1599 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1600 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1601 struct cp_control cpc = {
1602 .reason = CP_UMOUNT,
1604 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1605 err = f2fs_write_checkpoint(sbi, &cpc);
1608 /* be sure to wait for any on-going discard commands */
1609 done = f2fs_issue_discard_timeout(sbi);
1610 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
1611 struct cp_control cpc = {
1612 .reason = CP_UMOUNT | CP_TRIMMED,
1614 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1615 err = f2fs_write_checkpoint(sbi, &cpc);
1619 * normally superblock is clean, so we need to release this.
1620 * In addition, EIO will skip do checkpoint, we need this as well.
1622 f2fs_release_ino_entry(sbi, true);
1624 f2fs_leave_shrinker(sbi);
1625 mutex_unlock(&sbi->umount_mutex);
1627 /* our cp_error case, we can wait for any writeback page */
1628 f2fs_flush_merged_writes(sbi);
1630 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1633 truncate_inode_pages_final(NODE_MAPPING(sbi));
1634 truncate_inode_pages_final(META_MAPPING(sbi));
1637 for (i = 0; i < NR_COUNT_TYPE; i++) {
1638 if (!get_pages(sbi, i))
1640 f2fs_err(sbi, "detect filesystem reference count leak during "
1641 "umount, type: %d, count: %lld", i, get_pages(sbi, i));
1642 f2fs_bug_on(sbi, 1);
1645 f2fs_bug_on(sbi, sbi->fsync_node_num);
1647 f2fs_destroy_compress_inode(sbi);
1649 iput(sbi->node_inode);
1650 sbi->node_inode = NULL;
1652 iput(sbi->meta_inode);
1653 sbi->meta_inode = NULL;
1656 * iput() can update stat information, if f2fs_write_checkpoint()
1657 * above failed with error.
1659 f2fs_destroy_stats(sbi);
1661 /* destroy f2fs internal modules */
1662 f2fs_destroy_node_manager(sbi);
1663 f2fs_destroy_segment_manager(sbi);
1665 /* flush s_error_work before sbi destroy */
1666 flush_work(&sbi->s_error_work);
1668 f2fs_destroy_post_read_wq(sbi);
1672 sb->s_fs_info = NULL;
1673 if (sbi->s_chksum_driver)
1674 crypto_free_shash(sbi->s_chksum_driver);
1675 kfree(sbi->raw_super);
1677 destroy_device_list(sbi);
1678 f2fs_destroy_page_array_cache(sbi);
1679 f2fs_destroy_xattr_caches(sbi);
1680 mempool_destroy(sbi->write_io_dummy);
1682 for (i = 0; i < MAXQUOTAS; i++)
1683 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1685 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1686 destroy_percpu_info(sbi);
1687 f2fs_destroy_iostat(sbi);
1688 for (i = 0; i < NR_PAGE_TYPE; i++)
1689 kvfree(sbi->write_io[i]);
1690 #if IS_ENABLED(CONFIG_UNICODE)
1691 utf8_unload(sb->s_encoding);
1696 int f2fs_sync_fs(struct super_block *sb, int sync)
1698 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1701 if (unlikely(f2fs_cp_error(sbi)))
1703 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1706 trace_f2fs_sync_fs(sb, sync);
1708 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1712 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1713 err = f2fs_issue_checkpoint(sbi);
1719 static int f2fs_freeze(struct super_block *sb)
1721 if (f2fs_readonly(sb))
1724 /* IO error happened before */
1725 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1728 /* must be clean, since sync_filesystem() was already called */
1729 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1732 /* Let's flush checkpoints and stop the thread. */
1733 f2fs_flush_ckpt_thread(F2FS_SB(sb));
1735 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
1736 set_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1740 static int f2fs_unfreeze(struct super_block *sb)
1742 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1747 static int f2fs_statfs_project(struct super_block *sb,
1748 kprojid_t projid, struct kstatfs *buf)
1751 struct dquot *dquot;
1755 qid = make_kqid_projid(projid);
1756 dquot = dqget(sb, qid);
1758 return PTR_ERR(dquot);
1759 spin_lock(&dquot->dq_dqb_lock);
1761 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1762 dquot->dq_dqb.dqb_bhardlimit);
1764 limit >>= sb->s_blocksize_bits;
1766 if (limit && buf->f_blocks > limit) {
1767 curblock = (dquot->dq_dqb.dqb_curspace +
1768 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1769 buf->f_blocks = limit;
1770 buf->f_bfree = buf->f_bavail =
1771 (buf->f_blocks > curblock) ?
1772 (buf->f_blocks - curblock) : 0;
1775 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1776 dquot->dq_dqb.dqb_ihardlimit);
1778 if (limit && buf->f_files > limit) {
1779 buf->f_files = limit;
1781 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1782 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1785 spin_unlock(&dquot->dq_dqb_lock);
1791 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1793 struct super_block *sb = dentry->d_sb;
1794 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1795 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1796 block_t total_count, user_block_count, start_count;
1797 u64 avail_node_count;
1798 unsigned int total_valid_node_count;
1800 total_count = le64_to_cpu(sbi->raw_super->block_count);
1801 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1802 buf->f_type = F2FS_SUPER_MAGIC;
1803 buf->f_bsize = sbi->blocksize;
1805 buf->f_blocks = total_count - start_count;
1807 spin_lock(&sbi->stat_lock);
1809 user_block_count = sbi->user_block_count;
1810 total_valid_node_count = valid_node_count(sbi);
1811 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1812 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1813 sbi->current_reserved_blocks;
1815 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1818 buf->f_bfree -= sbi->unusable_block_count;
1819 spin_unlock(&sbi->stat_lock);
1821 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1822 buf->f_bavail = buf->f_bfree -
1823 F2FS_OPTION(sbi).root_reserved_blocks;
1827 if (avail_node_count > user_block_count) {
1828 buf->f_files = user_block_count;
1829 buf->f_ffree = buf->f_bavail;
1831 buf->f_files = avail_node_count;
1832 buf->f_ffree = min(avail_node_count - total_valid_node_count,
1836 buf->f_namelen = F2FS_NAME_LEN;
1837 buf->f_fsid = u64_to_fsid(id);
1840 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1841 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1842 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1848 static inline void f2fs_show_quota_options(struct seq_file *seq,
1849 struct super_block *sb)
1852 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1854 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1857 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1868 seq_printf(seq, ",jqfmt=%s", fmtname);
1871 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1872 seq_show_option(seq, "usrjquota",
1873 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1875 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1876 seq_show_option(seq, "grpjquota",
1877 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1879 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1880 seq_show_option(seq, "prjjquota",
1881 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1885 #ifdef CONFIG_F2FS_FS_COMPRESSION
1886 static inline void f2fs_show_compress_options(struct seq_file *seq,
1887 struct super_block *sb)
1889 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1893 if (!f2fs_sb_has_compression(sbi))
1896 switch (F2FS_OPTION(sbi).compress_algorithm) {
1906 case COMPRESS_LZORLE:
1907 algtype = "lzo-rle";
1910 seq_printf(seq, ",compress_algorithm=%s", algtype);
1912 if (F2FS_OPTION(sbi).compress_level)
1913 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1915 seq_printf(seq, ",compress_log_size=%u",
1916 F2FS_OPTION(sbi).compress_log_size);
1918 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1919 seq_printf(seq, ",compress_extension=%s",
1920 F2FS_OPTION(sbi).extensions[i]);
1923 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
1924 seq_printf(seq, ",nocompress_extension=%s",
1925 F2FS_OPTION(sbi).noextensions[i]);
1928 if (F2FS_OPTION(sbi).compress_chksum)
1929 seq_puts(seq, ",compress_chksum");
1931 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1932 seq_printf(seq, ",compress_mode=%s", "fs");
1933 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1934 seq_printf(seq, ",compress_mode=%s", "user");
1936 if (test_opt(sbi, COMPRESS_CACHE))
1937 seq_puts(seq, ",compress_cache");
1941 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1943 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1945 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1946 seq_printf(seq, ",background_gc=%s", "sync");
1947 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1948 seq_printf(seq, ",background_gc=%s", "on");
1949 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
1950 seq_printf(seq, ",background_gc=%s", "off");
1952 if (test_opt(sbi, GC_MERGE))
1953 seq_puts(seq, ",gc_merge");
1955 seq_puts(seq, ",nogc_merge");
1957 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1958 seq_puts(seq, ",disable_roll_forward");
1959 if (test_opt(sbi, NORECOVERY))
1960 seq_puts(seq, ",norecovery");
1961 if (test_opt(sbi, DISCARD)) {
1962 seq_puts(seq, ",discard");
1963 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
1964 seq_printf(seq, ",discard_unit=%s", "block");
1965 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
1966 seq_printf(seq, ",discard_unit=%s", "segment");
1967 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
1968 seq_printf(seq, ",discard_unit=%s", "section");
1970 seq_puts(seq, ",nodiscard");
1972 if (test_opt(sbi, NOHEAP))
1973 seq_puts(seq, ",no_heap");
1975 seq_puts(seq, ",heap");
1976 #ifdef CONFIG_F2FS_FS_XATTR
1977 if (test_opt(sbi, XATTR_USER))
1978 seq_puts(seq, ",user_xattr");
1980 seq_puts(seq, ",nouser_xattr");
1981 if (test_opt(sbi, INLINE_XATTR))
1982 seq_puts(seq, ",inline_xattr");
1984 seq_puts(seq, ",noinline_xattr");
1985 if (test_opt(sbi, INLINE_XATTR_SIZE))
1986 seq_printf(seq, ",inline_xattr_size=%u",
1987 F2FS_OPTION(sbi).inline_xattr_size);
1989 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1990 if (test_opt(sbi, POSIX_ACL))
1991 seq_puts(seq, ",acl");
1993 seq_puts(seq, ",noacl");
1995 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1996 seq_puts(seq, ",disable_ext_identify");
1997 if (test_opt(sbi, INLINE_DATA))
1998 seq_puts(seq, ",inline_data");
2000 seq_puts(seq, ",noinline_data");
2001 if (test_opt(sbi, INLINE_DENTRY))
2002 seq_puts(seq, ",inline_dentry");
2004 seq_puts(seq, ",noinline_dentry");
2005 if (test_opt(sbi, FLUSH_MERGE))
2006 seq_puts(seq, ",flush_merge");
2008 seq_puts(seq, ",noflush_merge");
2009 if (test_opt(sbi, NOBARRIER))
2010 seq_puts(seq, ",nobarrier");
2012 seq_puts(seq, ",barrier");
2013 if (test_opt(sbi, FASTBOOT))
2014 seq_puts(seq, ",fastboot");
2015 if (test_opt(sbi, READ_EXTENT_CACHE))
2016 seq_puts(seq, ",extent_cache");
2018 seq_puts(seq, ",noextent_cache");
2019 if (test_opt(sbi, AGE_EXTENT_CACHE))
2020 seq_puts(seq, ",age_extent_cache");
2021 if (test_opt(sbi, DATA_FLUSH))
2022 seq_puts(seq, ",data_flush");
2024 seq_puts(seq, ",mode=");
2025 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
2026 seq_puts(seq, "adaptive");
2027 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
2028 seq_puts(seq, "lfs");
2029 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
2030 seq_puts(seq, "fragment:segment");
2031 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2032 seq_puts(seq, "fragment:block");
2033 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
2034 if (test_opt(sbi, RESERVE_ROOT))
2035 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
2036 F2FS_OPTION(sbi).root_reserved_blocks,
2037 from_kuid_munged(&init_user_ns,
2038 F2FS_OPTION(sbi).s_resuid),
2039 from_kgid_munged(&init_user_ns,
2040 F2FS_OPTION(sbi).s_resgid));
2041 if (F2FS_IO_SIZE_BITS(sbi))
2042 seq_printf(seq, ",io_bits=%u",
2043 F2FS_OPTION(sbi).write_io_size_bits);
2044 #ifdef CONFIG_F2FS_FAULT_INJECTION
2045 if (test_opt(sbi, FAULT_INJECTION)) {
2046 seq_printf(seq, ",fault_injection=%u",
2047 F2FS_OPTION(sbi).fault_info.inject_rate);
2048 seq_printf(seq, ",fault_type=%u",
2049 F2FS_OPTION(sbi).fault_info.inject_type);
2053 if (test_opt(sbi, QUOTA))
2054 seq_puts(seq, ",quota");
2055 if (test_opt(sbi, USRQUOTA))
2056 seq_puts(seq, ",usrquota");
2057 if (test_opt(sbi, GRPQUOTA))
2058 seq_puts(seq, ",grpquota");
2059 if (test_opt(sbi, PRJQUOTA))
2060 seq_puts(seq, ",prjquota");
2062 f2fs_show_quota_options(seq, sbi->sb);
2064 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
2066 if (sbi->sb->s_flags & SB_INLINECRYPT)
2067 seq_puts(seq, ",inlinecrypt");
2069 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
2070 seq_printf(seq, ",alloc_mode=%s", "default");
2071 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2072 seq_printf(seq, ",alloc_mode=%s", "reuse");
2074 if (test_opt(sbi, DISABLE_CHECKPOINT))
2075 seq_printf(seq, ",checkpoint=disable:%u",
2076 F2FS_OPTION(sbi).unusable_cap);
2077 if (test_opt(sbi, MERGE_CHECKPOINT))
2078 seq_puts(seq, ",checkpoint_merge");
2080 seq_puts(seq, ",nocheckpoint_merge");
2081 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
2082 seq_printf(seq, ",fsync_mode=%s", "posix");
2083 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
2084 seq_printf(seq, ",fsync_mode=%s", "strict");
2085 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
2086 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
2088 #ifdef CONFIG_F2FS_FS_COMPRESSION
2089 f2fs_show_compress_options(seq, sbi->sb);
2092 if (test_opt(sbi, ATGC))
2093 seq_puts(seq, ",atgc");
2095 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
2096 seq_printf(seq, ",memory=%s", "normal");
2097 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
2098 seq_printf(seq, ",memory=%s", "low");
2100 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2101 seq_printf(seq, ",errors=%s", "remount-ro");
2102 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
2103 seq_printf(seq, ",errors=%s", "continue");
2104 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
2105 seq_printf(seq, ",errors=%s", "panic");
2110 static void default_options(struct f2fs_sb_info *sbi, bool remount)
2112 /* init some FS parameters */
2114 set_opt(sbi, READ_EXTENT_CACHE);
2115 clear_opt(sbi, DISABLE_CHECKPOINT);
2117 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2118 set_opt(sbi, DISCARD);
2120 if (f2fs_sb_has_blkzoned(sbi))
2121 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2123 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2126 if (f2fs_sb_has_readonly(sbi))
2127 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
2129 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
2131 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
2132 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
2133 SMALL_VOLUME_SEGMENTS)
2134 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2136 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
2137 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
2138 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2139 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2140 if (f2fs_sb_has_compression(sbi)) {
2141 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
2142 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
2143 F2FS_OPTION(sbi).compress_ext_cnt = 0;
2144 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
2146 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
2147 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
2148 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
2150 sbi->sb->s_flags &= ~SB_INLINECRYPT;
2152 set_opt(sbi, INLINE_XATTR);
2153 set_opt(sbi, INLINE_DATA);
2154 set_opt(sbi, INLINE_DENTRY);
2155 set_opt(sbi, NOHEAP);
2156 set_opt(sbi, MERGE_CHECKPOINT);
2157 F2FS_OPTION(sbi).unusable_cap = 0;
2158 sbi->sb->s_flags |= SB_LAZYTIME;
2159 if (!f2fs_is_readonly(sbi))
2160 set_opt(sbi, FLUSH_MERGE);
2161 if (f2fs_sb_has_blkzoned(sbi))
2162 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
2164 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
2166 #ifdef CONFIG_F2FS_FS_XATTR
2167 set_opt(sbi, XATTR_USER);
2169 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2170 set_opt(sbi, POSIX_ACL);
2173 f2fs_build_fault_attr(sbi, 0, 0);
2177 static int f2fs_enable_quotas(struct super_block *sb);
2180 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
2182 unsigned int s_flags = sbi->sb->s_flags;
2183 struct cp_control cpc;
2184 unsigned int gc_mode = sbi->gc_mode;
2189 if (s_flags & SB_RDONLY) {
2190 f2fs_err(sbi, "checkpoint=disable on readonly fs");
2193 sbi->sb->s_flags |= SB_ACTIVE;
2195 /* check if we need more GC first */
2196 unusable = f2fs_get_unusable_blocks(sbi);
2197 if (!f2fs_disable_cp_again(sbi, unusable))
2200 f2fs_update_time(sbi, DISABLE_TIME);
2202 sbi->gc_mode = GC_URGENT_HIGH;
2204 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
2205 struct f2fs_gc_control gc_control = {
2206 .victim_segno = NULL_SEGNO,
2207 .init_gc_type = FG_GC,
2208 .should_migrate_blocks = false,
2209 .err_gc_skipped = true,
2210 .nr_free_secs = 1 };
2212 f2fs_down_write(&sbi->gc_lock);
2213 stat_inc_gc_call_count(sbi, FOREGROUND);
2214 err = f2fs_gc(sbi, &gc_control);
2215 if (err == -ENODATA) {
2219 if (err && err != -EAGAIN)
2223 ret = sync_filesystem(sbi->sb);
2225 err = ret ? ret : err;
2229 unusable = f2fs_get_unusable_blocks(sbi);
2230 if (f2fs_disable_cp_again(sbi, unusable)) {
2236 f2fs_down_write(&sbi->gc_lock);
2237 cpc.reason = CP_PAUSE;
2238 set_sbi_flag(sbi, SBI_CP_DISABLED);
2239 stat_inc_cp_call_count(sbi, TOTAL_CALL);
2240 err = f2fs_write_checkpoint(sbi, &cpc);
2244 spin_lock(&sbi->stat_lock);
2245 sbi->unusable_block_count = unusable;
2246 spin_unlock(&sbi->stat_lock);
2249 f2fs_up_write(&sbi->gc_lock);
2251 sbi->gc_mode = gc_mode;
2252 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2256 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
2258 int retry = DEFAULT_RETRY_IO_COUNT;
2260 /* we should flush all the data to keep data consistency */
2262 sync_inodes_sb(sbi->sb);
2263 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2264 } while (get_pages(sbi, F2FS_DIRTY_DATA) && retry--);
2266 if (unlikely(retry < 0))
2267 f2fs_warn(sbi, "checkpoint=enable has some unwritten data.");
2269 f2fs_down_write(&sbi->gc_lock);
2270 f2fs_dirty_to_prefree(sbi);
2272 clear_sbi_flag(sbi, SBI_CP_DISABLED);
2273 set_sbi_flag(sbi, SBI_IS_DIRTY);
2274 f2fs_up_write(&sbi->gc_lock);
2276 f2fs_sync_fs(sbi->sb, 1);
2278 /* Let's ensure there's no pending checkpoint anymore */
2279 f2fs_flush_ckpt_thread(sbi);
2282 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
2284 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2285 struct f2fs_mount_info org_mount_opt;
2286 unsigned long old_sb_flags;
2288 bool need_restart_gc = false, need_stop_gc = false;
2289 bool need_restart_ckpt = false, need_stop_ckpt = false;
2290 bool need_restart_flush = false, need_stop_flush = false;
2291 bool need_restart_discard = false, need_stop_discard = false;
2292 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
2293 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
2294 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
2295 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
2296 bool no_atgc = !test_opt(sbi, ATGC);
2297 bool no_discard = !test_opt(sbi, DISCARD);
2298 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
2299 bool block_unit_discard = f2fs_block_unit_discard(sbi);
2305 * Save the old mount options in case we
2306 * need to restore them.
2308 org_mount_opt = sbi->mount_opt;
2309 old_sb_flags = sb->s_flags;
2312 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
2313 for (i = 0; i < MAXQUOTAS; i++) {
2314 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2315 org_mount_opt.s_qf_names[i] =
2316 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
2318 if (!org_mount_opt.s_qf_names[i]) {
2319 for (j = 0; j < i; j++)
2320 kfree(org_mount_opt.s_qf_names[j]);
2324 org_mount_opt.s_qf_names[i] = NULL;
2329 /* recover superblocks we couldn't write due to previous RO mount */
2330 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
2331 err = f2fs_commit_super(sbi, false);
2332 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2335 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2338 default_options(sbi, true);
2340 /* parse mount options */
2341 err = parse_options(sb, data, true);
2345 /* flush outstanding errors before changing fs state */
2346 flush_work(&sbi->s_error_work);
2349 * Previous and new state of filesystem is RO,
2350 * so skip checking GC and FLUSH_MERGE conditions.
2352 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
2355 if (f2fs_dev_is_readonly(sbi) && !(*flags & SB_RDONLY)) {
2361 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
2362 err = dquot_suspend(sb, -1);
2365 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
2366 /* dquot_resume needs RW */
2367 sb->s_flags &= ~SB_RDONLY;
2368 if (sb_any_quota_suspended(sb)) {
2369 dquot_resume(sb, -1);
2370 } else if (f2fs_sb_has_quota_ino(sbi)) {
2371 err = f2fs_enable_quotas(sb);
2377 if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
2379 f2fs_warn(sbi, "LFS is not compatible with IPU");
2383 /* disallow enable atgc dynamically */
2384 if (no_atgc == !!test_opt(sbi, ATGC)) {
2386 f2fs_warn(sbi, "switch atgc option is not allowed");
2390 /* disallow enable/disable extent_cache dynamically */
2391 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
2393 f2fs_warn(sbi, "switch extent_cache option is not allowed");
2396 /* disallow enable/disable age extent_cache dynamically */
2397 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2399 f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
2403 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
2405 f2fs_warn(sbi, "switch io_bits 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);
2455 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2456 !test_opt(sbi, MERGE_CHECKPOINT)) {
2457 f2fs_stop_ckpt_thread(sbi);
2458 need_restart_ckpt = true;
2460 /* Flush if the prevous checkpoint, if exists. */
2461 f2fs_flush_ckpt_thread(sbi);
2463 err = f2fs_start_ckpt_thread(sbi);
2466 "Failed to start F2FS issue_checkpoint_thread (%d)",
2470 need_stop_ckpt = true;
2474 * We stop issue flush thread if FS is mounted as RO
2475 * or if flush_merge is not passed in mount option.
2477 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2478 clear_opt(sbi, FLUSH_MERGE);
2479 f2fs_destroy_flush_cmd_control(sbi, false);
2480 need_restart_flush = true;
2482 err = f2fs_create_flush_cmd_control(sbi);
2485 need_stop_flush = true;
2488 if (no_discard == !!test_opt(sbi, DISCARD)) {
2489 if (test_opt(sbi, DISCARD)) {
2490 err = f2fs_start_discard_thread(sbi);
2493 need_stop_discard = true;
2495 f2fs_stop_discard_thread(sbi);
2496 f2fs_issue_discard_timeout(sbi);
2497 need_restart_discard = true;
2501 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
2502 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2503 err = f2fs_disable_checkpoint(sbi);
2505 goto restore_discard;
2507 f2fs_enable_checkpoint(sbi);
2513 /* Release old quota file names */
2514 for (i = 0; i < MAXQUOTAS; i++)
2515 kfree(org_mount_opt.s_qf_names[i]);
2517 /* Update the POSIXACL Flag */
2518 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2519 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2521 limit_reserve_root(sbi);
2522 adjust_unusable_cap_perc(sbi);
2523 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
2526 if (need_restart_discard) {
2527 if (f2fs_start_discard_thread(sbi))
2528 f2fs_warn(sbi, "discard has been stopped");
2529 } else if (need_stop_discard) {
2530 f2fs_stop_discard_thread(sbi);
2533 if (need_restart_flush) {
2534 if (f2fs_create_flush_cmd_control(sbi))
2535 f2fs_warn(sbi, "background flush thread has stopped");
2536 } else if (need_stop_flush) {
2537 clear_opt(sbi, FLUSH_MERGE);
2538 f2fs_destroy_flush_cmd_control(sbi, false);
2541 if (need_restart_ckpt) {
2542 if (f2fs_start_ckpt_thread(sbi))
2543 f2fs_warn(sbi, "background ckpt thread has stopped");
2544 } else if (need_stop_ckpt) {
2545 f2fs_stop_ckpt_thread(sbi);
2548 if (need_restart_gc) {
2549 if (f2fs_start_gc_thread(sbi))
2550 f2fs_warn(sbi, "background gc thread has stopped");
2551 } else if (need_stop_gc) {
2552 f2fs_stop_gc_thread(sbi);
2556 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
2557 for (i = 0; i < MAXQUOTAS; i++) {
2558 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2559 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
2562 sbi->mount_opt = org_mount_opt;
2563 sb->s_flags = old_sb_flags;
2568 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
2570 /* need to recovery orphan */
2571 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
2573 /* need to recovery data */
2574 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2576 if (test_opt(sbi, NORECOVERY))
2578 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
2581 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
2583 bool readonly = f2fs_readonly(sbi->sb);
2585 if (!f2fs_need_recovery(sbi))
2588 /* it doesn't need to check f2fs_sb_has_readonly() */
2589 if (f2fs_hw_is_readonly(sbi))
2593 sbi->sb->s_flags &= ~SB_RDONLY;
2594 set_sbi_flag(sbi, SBI_IS_WRITABLE);
2598 * Turn on quotas which were not enabled for read-only mounts if
2599 * filesystem has quota feature, so that they are updated correctly.
2601 return f2fs_enable_quota_files(sbi, readonly);
2604 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
2608 f2fs_quota_off_umount(sbi->sb);
2610 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
2611 clear_sbi_flag(sbi, SBI_IS_WRITABLE);
2612 sbi->sb->s_flags |= SB_RDONLY;
2616 /* Read data from quotafile */
2617 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2618 size_t len, loff_t off)
2620 struct inode *inode = sb_dqopt(sb)->files[type];
2621 struct address_space *mapping = inode->i_mapping;
2622 block_t blkidx = F2FS_BYTES_TO_BLK(off);
2623 int offset = off & (sb->s_blocksize - 1);
2626 loff_t i_size = i_size_read(inode);
2632 if (off + len > i_size)
2635 while (toread > 0) {
2636 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2638 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
2640 if (PTR_ERR(page) == -ENOMEM) {
2641 memalloc_retry_wait(GFP_NOFS);
2644 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2645 return PTR_ERR(page);
2650 if (unlikely(page->mapping != mapping)) {
2651 f2fs_put_page(page, 1);
2654 if (unlikely(!PageUptodate(page))) {
2655 f2fs_put_page(page, 1);
2656 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2660 memcpy_from_page(data, page, offset, tocopy);
2661 f2fs_put_page(page, 1);
2671 /* Write to quotafile */
2672 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2673 const char *data, size_t len, loff_t off)
2675 struct inode *inode = sb_dqopt(sb)->files[type];
2676 struct address_space *mapping = inode->i_mapping;
2677 const struct address_space_operations *a_ops = mapping->a_ops;
2678 int offset = off & (sb->s_blocksize - 1);
2679 size_t towrite = len;
2681 void *fsdata = NULL;
2685 while (towrite > 0) {
2686 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2689 err = a_ops->write_begin(NULL, mapping, off, tocopy,
2691 if (unlikely(err)) {
2692 if (err == -ENOMEM) {
2693 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2696 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2700 memcpy_to_page(page, offset, data, tocopy);
2702 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2713 inode->i_mtime = inode_set_ctime_current(inode);
2714 f2fs_mark_inode_dirty_sync(inode, false);
2715 return len - towrite;
2718 int f2fs_dquot_initialize(struct inode *inode)
2720 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
2723 return dquot_initialize(inode);
2726 static struct dquot **f2fs_get_dquots(struct inode *inode)
2728 return F2FS_I(inode)->i_dquot;
2731 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2733 return &F2FS_I(inode)->i_reserved_quota;
2736 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2738 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2739 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2743 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2744 F2FS_OPTION(sbi).s_jquota_fmt, type);
2747 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2752 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2753 err = f2fs_enable_quotas(sbi->sb);
2755 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2761 for (i = 0; i < MAXQUOTAS; i++) {
2762 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2763 err = f2fs_quota_on_mount(sbi, i);
2768 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2775 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2778 struct inode *qf_inode;
2779 unsigned long qf_inum;
2780 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
2783 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2785 qf_inum = f2fs_qf_ino(sb, type);
2789 qf_inode = f2fs_iget(sb, qf_inum);
2790 if (IS_ERR(qf_inode)) {
2791 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2792 return PTR_ERR(qf_inode);
2795 /* Don't account quota for quota files to avoid recursion */
2796 inode_lock(qf_inode);
2797 qf_inode->i_flags |= S_NOQUOTA;
2799 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
2800 F2FS_I(qf_inode)->i_flags |= qf_flag;
2801 f2fs_set_inode_flags(qf_inode);
2803 inode_unlock(qf_inode);
2805 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2810 static int f2fs_enable_quotas(struct super_block *sb)
2812 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2814 unsigned long qf_inum;
2815 bool quota_mopt[MAXQUOTAS] = {
2816 test_opt(sbi, USRQUOTA),
2817 test_opt(sbi, GRPQUOTA),
2818 test_opt(sbi, PRJQUOTA),
2821 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2822 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2826 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2828 for (type = 0; type < MAXQUOTAS; type++) {
2829 qf_inum = f2fs_qf_ino(sb, type);
2831 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2832 DQUOT_USAGE_ENABLED |
2833 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2835 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2837 for (type--; type >= 0; type--)
2838 dquot_quota_off(sb, type);
2839 set_sbi_flag(F2FS_SB(sb),
2840 SBI_QUOTA_NEED_REPAIR);
2848 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2850 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2851 struct address_space *mapping = dqopt->files[type]->i_mapping;
2854 ret = dquot_writeback_dquots(sbi->sb, type);
2858 ret = filemap_fdatawrite(mapping);
2862 /* if we are using journalled quota */
2863 if (is_journalled_quota(sbi))
2866 ret = filemap_fdatawait(mapping);
2868 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2871 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2875 int f2fs_quota_sync(struct super_block *sb, int type)
2877 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2878 struct quota_info *dqopt = sb_dqopt(sb);
2883 * Now when everything is written we can discard the pagecache so
2884 * that userspace sees the changes.
2886 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2888 if (type != -1 && cnt != type)
2891 if (!sb_has_quota_active(sb, cnt))
2894 if (!f2fs_sb_has_quota_ino(sbi))
2895 inode_lock(dqopt->files[cnt]);
2900 * f2fs_down_read(quota_sem)
2901 * dquot_writeback_dquots()
2904 * f2fs_down_read(quota_sem)
2907 f2fs_down_read(&sbi->quota_sem);
2909 ret = f2fs_quota_sync_file(sbi, cnt);
2911 f2fs_up_read(&sbi->quota_sem);
2912 f2fs_unlock_op(sbi);
2914 if (!f2fs_sb_has_quota_ino(sbi))
2915 inode_unlock(dqopt->files[cnt]);
2923 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2924 const struct path *path)
2926 struct inode *inode;
2929 /* if quota sysfile exists, deny enabling quota with specific file */
2930 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2931 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2935 if (path->dentry->d_sb != sb)
2938 err = f2fs_quota_sync(sb, type);
2942 inode = d_inode(path->dentry);
2944 err = filemap_fdatawrite(inode->i_mapping);
2948 err = filemap_fdatawait(inode->i_mapping);
2952 err = dquot_quota_on(sb, type, format_id, path);
2957 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
2958 f2fs_set_inode_flags(inode);
2959 inode_unlock(inode);
2960 f2fs_mark_inode_dirty_sync(inode, false);
2965 static int __f2fs_quota_off(struct super_block *sb, int type)
2967 struct inode *inode = sb_dqopt(sb)->files[type];
2970 if (!inode || !igrab(inode))
2971 return dquot_quota_off(sb, type);
2973 err = f2fs_quota_sync(sb, type);
2977 err = dquot_quota_off(sb, type);
2978 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
2982 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
2983 f2fs_set_inode_flags(inode);
2984 inode_unlock(inode);
2985 f2fs_mark_inode_dirty_sync(inode, false);
2991 static int f2fs_quota_off(struct super_block *sb, int type)
2993 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2996 err = __f2fs_quota_off(sb, type);
2999 * quotactl can shutdown journalled quota, result in inconsistence
3000 * between quota record and fs data by following updates, tag the
3001 * flag to let fsck be aware of it.
3003 if (is_journalled_quota(sbi))
3004 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3008 void f2fs_quota_off_umount(struct super_block *sb)
3013 for (type = 0; type < MAXQUOTAS; type++) {
3014 err = __f2fs_quota_off(sb, type);
3016 int ret = dquot_quota_off(sb, type);
3018 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
3020 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3024 * In case of checkpoint=disable, we must flush quota blocks.
3025 * This can cause NULL exception for node_inode in end_io, since
3026 * put_super already dropped it.
3028 sync_filesystem(sb);
3031 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
3033 struct quota_info *dqopt = sb_dqopt(sb);
3036 for (type = 0; type < MAXQUOTAS; type++) {
3037 if (!dqopt->files[type])
3039 f2fs_inode_synced(dqopt->files[type]);
3043 static int f2fs_dquot_commit(struct dquot *dquot)
3045 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3048 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
3049 ret = dquot_commit(dquot);
3051 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3052 f2fs_up_read(&sbi->quota_sem);
3056 static int f2fs_dquot_acquire(struct dquot *dquot)
3058 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3061 f2fs_down_read(&sbi->quota_sem);
3062 ret = dquot_acquire(dquot);
3064 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3065 f2fs_up_read(&sbi->quota_sem);
3069 static int f2fs_dquot_release(struct dquot *dquot)
3071 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3072 int ret = dquot_release(dquot);
3075 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3079 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
3081 struct super_block *sb = dquot->dq_sb;
3082 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3083 int ret = dquot_mark_dquot_dirty(dquot);
3085 /* if we are using journalled quota */
3086 if (is_journalled_quota(sbi))
3087 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
3092 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
3094 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3095 int ret = dquot_commit_info(sb, type);
3098 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3102 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
3104 *projid = F2FS_I(inode)->i_projid;
3108 static const struct dquot_operations f2fs_quota_operations = {
3109 .get_reserved_space = f2fs_get_reserved_space,
3110 .write_dquot = f2fs_dquot_commit,
3111 .acquire_dquot = f2fs_dquot_acquire,
3112 .release_dquot = f2fs_dquot_release,
3113 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
3114 .write_info = f2fs_dquot_commit_info,
3115 .alloc_dquot = dquot_alloc,
3116 .destroy_dquot = dquot_destroy,
3117 .get_projid = f2fs_get_projid,
3118 .get_next_id = dquot_get_next_id,
3121 static const struct quotactl_ops f2fs_quotactl_ops = {
3122 .quota_on = f2fs_quota_on,
3123 .quota_off = f2fs_quota_off,
3124 .quota_sync = f2fs_quota_sync,
3125 .get_state = dquot_get_state,
3126 .set_info = dquot_set_dqinfo,
3127 .get_dqblk = dquot_get_dqblk,
3128 .set_dqblk = dquot_set_dqblk,
3129 .get_nextdqblk = dquot_get_next_dqblk,
3132 int f2fs_dquot_initialize(struct inode *inode)
3137 int f2fs_quota_sync(struct super_block *sb, int type)
3142 void f2fs_quota_off_umount(struct super_block *sb)
3147 static const struct super_operations f2fs_sops = {
3148 .alloc_inode = f2fs_alloc_inode,
3149 .free_inode = f2fs_free_inode,
3150 .drop_inode = f2fs_drop_inode,
3151 .write_inode = f2fs_write_inode,
3152 .dirty_inode = f2fs_dirty_inode,
3153 .show_options = f2fs_show_options,
3155 .quota_read = f2fs_quota_read,
3156 .quota_write = f2fs_quota_write,
3157 .get_dquots = f2fs_get_dquots,
3159 .evict_inode = f2fs_evict_inode,
3160 .put_super = f2fs_put_super,
3161 .sync_fs = f2fs_sync_fs,
3162 .freeze_fs = f2fs_freeze,
3163 .unfreeze_fs = f2fs_unfreeze,
3164 .statfs = f2fs_statfs,
3165 .remount_fs = f2fs_remount,
3168 #ifdef CONFIG_FS_ENCRYPTION
3169 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
3171 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3172 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3176 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
3179 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3182 * Encrypting the root directory is not allowed because fsck
3183 * expects lost+found directory to exist and remain unencrypted
3184 * if LOST_FOUND feature is enabled.
3187 if (f2fs_sb_has_lost_found(sbi) &&
3188 inode->i_ino == F2FS_ROOT_INO(sbi))
3191 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3192 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3193 ctx, len, fs_data, XATTR_CREATE);
3196 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
3198 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
3201 static bool f2fs_has_stable_inodes(struct super_block *sb)
3206 static void f2fs_get_ino_and_lblk_bits(struct super_block *sb,
3207 int *ino_bits_ret, int *lblk_bits_ret)
3209 *ino_bits_ret = 8 * sizeof(nid_t);
3210 *lblk_bits_ret = 8 * sizeof(block_t);
3213 static struct block_device **f2fs_get_devices(struct super_block *sb,
3214 unsigned int *num_devs)
3216 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3217 struct block_device **devs;
3220 if (!f2fs_is_multi_device(sbi))
3223 devs = kmalloc_array(sbi->s_ndevs, sizeof(*devs), GFP_KERNEL);
3225 return ERR_PTR(-ENOMEM);
3227 for (i = 0; i < sbi->s_ndevs; i++)
3228 devs[i] = FDEV(i).bdev;
3229 *num_devs = sbi->s_ndevs;
3233 static const struct fscrypt_operations f2fs_cryptops = {
3234 .key_prefix = "f2fs:",
3235 .get_context = f2fs_get_context,
3236 .set_context = f2fs_set_context,
3237 .get_dummy_policy = f2fs_get_dummy_policy,
3238 .empty_dir = f2fs_empty_dir,
3239 .has_stable_inodes = f2fs_has_stable_inodes,
3240 .get_ino_and_lblk_bits = f2fs_get_ino_and_lblk_bits,
3241 .get_devices = f2fs_get_devices,
3245 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
3246 u64 ino, u32 generation)
3248 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3249 struct inode *inode;
3251 if (f2fs_check_nid_range(sbi, ino))
3252 return ERR_PTR(-ESTALE);
3255 * f2fs_iget isn't quite right if the inode is currently unallocated!
3256 * However f2fs_iget currently does appropriate checks to handle stale
3257 * inodes so everything is OK.
3259 inode = f2fs_iget(sb, ino);
3261 return ERR_CAST(inode);
3262 if (unlikely(generation && inode->i_generation != generation)) {
3263 /* we didn't find the right inode.. */
3265 return ERR_PTR(-ESTALE);
3270 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
3271 int fh_len, int fh_type)
3273 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3274 f2fs_nfs_get_inode);
3277 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
3278 int fh_len, int fh_type)
3280 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3281 f2fs_nfs_get_inode);
3284 static const struct export_operations f2fs_export_ops = {
3285 .fh_to_dentry = f2fs_fh_to_dentry,
3286 .fh_to_parent = f2fs_fh_to_parent,
3287 .get_parent = f2fs_get_parent,
3290 loff_t max_file_blocks(struct inode *inode)
3296 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
3297 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
3298 * space in inode.i_addr, it will be more safe to reassign
3302 if (inode && f2fs_compressed_file(inode))
3303 leaf_count = ADDRS_PER_BLOCK(inode);
3305 leaf_count = DEF_ADDRS_PER_BLOCK;
3307 /* two direct node blocks */
3308 result += (leaf_count * 2);
3310 /* two indirect node blocks */
3311 leaf_count *= NIDS_PER_BLOCK;
3312 result += (leaf_count * 2);
3314 /* one double indirect node block */
3315 leaf_count *= NIDS_PER_BLOCK;
3316 result += leaf_count;
3321 static int __f2fs_commit_super(struct buffer_head *bh,
3322 struct f2fs_super_block *super)
3326 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
3327 set_buffer_dirty(bh);
3330 /* it's rare case, we can do fua all the time */
3331 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
3334 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
3335 struct buffer_head *bh)
3337 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3338 (bh->b_data + F2FS_SUPER_OFFSET);
3339 struct super_block *sb = sbi->sb;
3340 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3341 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
3342 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
3343 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
3344 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
3345 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3346 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
3347 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
3348 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
3349 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
3350 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3351 u32 segment_count = le32_to_cpu(raw_super->segment_count);
3352 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3353 u64 main_end_blkaddr = main_blkaddr +
3354 (segment_count_main << log_blocks_per_seg);
3355 u64 seg_end_blkaddr = segment0_blkaddr +
3356 (segment_count << log_blocks_per_seg);
3358 if (segment0_blkaddr != cp_blkaddr) {
3359 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
3360 segment0_blkaddr, cp_blkaddr);
3364 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
3366 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
3367 cp_blkaddr, sit_blkaddr,
3368 segment_count_ckpt << log_blocks_per_seg);
3372 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
3374 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
3375 sit_blkaddr, nat_blkaddr,
3376 segment_count_sit << log_blocks_per_seg);
3380 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
3382 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
3383 nat_blkaddr, ssa_blkaddr,
3384 segment_count_nat << log_blocks_per_seg);
3388 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
3390 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
3391 ssa_blkaddr, main_blkaddr,
3392 segment_count_ssa << log_blocks_per_seg);
3396 if (main_end_blkaddr > seg_end_blkaddr) {
3397 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3398 main_blkaddr, seg_end_blkaddr,
3399 segment_count_main << log_blocks_per_seg);
3401 } else if (main_end_blkaddr < seg_end_blkaddr) {
3405 /* fix in-memory information all the time */
3406 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
3407 segment0_blkaddr) >> log_blocks_per_seg);
3409 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
3410 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3413 err = __f2fs_commit_super(bh, NULL);
3414 res = err ? "failed" : "done";
3416 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3417 res, main_blkaddr, seg_end_blkaddr,
3418 segment_count_main << log_blocks_per_seg);
3425 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
3426 struct buffer_head *bh)
3428 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
3429 block_t total_sections, blocks_per_seg;
3430 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3431 (bh->b_data + F2FS_SUPER_OFFSET);
3432 size_t crc_offset = 0;
3435 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
3436 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
3437 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
3441 /* Check checksum_offset and crc in superblock */
3442 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
3443 crc_offset = le32_to_cpu(raw_super->checksum_offset);
3445 offsetof(struct f2fs_super_block, crc)) {
3446 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
3448 return -EFSCORRUPTED;
3450 crc = le32_to_cpu(raw_super->crc);
3451 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
3452 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
3453 return -EFSCORRUPTED;
3457 /* Currently, support only 4KB block size */
3458 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3459 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3460 le32_to_cpu(raw_super->log_blocksize),
3462 return -EFSCORRUPTED;
3465 /* check log blocks per segment */
3466 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
3467 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
3468 le32_to_cpu(raw_super->log_blocks_per_seg));
3469 return -EFSCORRUPTED;
3472 /* Currently, support 512/1024/2048/4096 bytes sector size */
3473 if (le32_to_cpu(raw_super->log_sectorsize) >
3474 F2FS_MAX_LOG_SECTOR_SIZE ||
3475 le32_to_cpu(raw_super->log_sectorsize) <
3476 F2FS_MIN_LOG_SECTOR_SIZE) {
3477 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3478 le32_to_cpu(raw_super->log_sectorsize));
3479 return -EFSCORRUPTED;
3481 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3482 le32_to_cpu(raw_super->log_sectorsize) !=
3483 F2FS_MAX_LOG_SECTOR_SIZE) {
3484 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3485 le32_to_cpu(raw_super->log_sectors_per_block),
3486 le32_to_cpu(raw_super->log_sectorsize));
3487 return -EFSCORRUPTED;
3490 segment_count = le32_to_cpu(raw_super->segment_count);
3491 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3492 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3493 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3494 total_sections = le32_to_cpu(raw_super->section_count);
3496 /* blocks_per_seg should be 512, given the above check */
3497 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
3499 if (segment_count > F2FS_MAX_SEGMENT ||
3500 segment_count < F2FS_MIN_SEGMENTS) {
3501 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
3502 return -EFSCORRUPTED;
3505 if (total_sections > segment_count_main || total_sections < 1 ||
3506 segs_per_sec > segment_count || !segs_per_sec) {
3507 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3508 segment_count, total_sections, segs_per_sec);
3509 return -EFSCORRUPTED;
3512 if (segment_count_main != total_sections * segs_per_sec) {
3513 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3514 segment_count_main, total_sections, segs_per_sec);
3515 return -EFSCORRUPTED;
3518 if ((segment_count / segs_per_sec) < total_sections) {
3519 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3520 segment_count, segs_per_sec, total_sections);
3521 return -EFSCORRUPTED;
3524 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
3525 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3526 segment_count, le64_to_cpu(raw_super->block_count));
3527 return -EFSCORRUPTED;
3530 if (RDEV(0).path[0]) {
3531 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3534 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3535 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3538 if (segment_count != dev_seg_count) {
3539 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3540 segment_count, dev_seg_count);
3541 return -EFSCORRUPTED;
3544 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3545 !bdev_is_zoned(sbi->sb->s_bdev)) {
3546 f2fs_info(sbi, "Zoned block device path is missing");
3547 return -EFSCORRUPTED;
3551 if (secs_per_zone > total_sections || !secs_per_zone) {
3552 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3553 secs_per_zone, total_sections);
3554 return -EFSCORRUPTED;
3556 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3557 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3558 (le32_to_cpu(raw_super->extension_count) +
3559 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
3560 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3561 le32_to_cpu(raw_super->extension_count),
3562 raw_super->hot_ext_count,
3563 F2FS_MAX_EXTENSION);
3564 return -EFSCORRUPTED;
3567 if (le32_to_cpu(raw_super->cp_payload) >=
3568 (blocks_per_seg - F2FS_CP_PACKS -
3569 NR_CURSEG_PERSIST_TYPE)) {
3570 f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
3571 le32_to_cpu(raw_super->cp_payload),
3572 blocks_per_seg - F2FS_CP_PACKS -
3573 NR_CURSEG_PERSIST_TYPE);
3574 return -EFSCORRUPTED;
3577 /* check reserved ino info */
3578 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3579 le32_to_cpu(raw_super->meta_ino) != 2 ||
3580 le32_to_cpu(raw_super->root_ino) != 3) {
3581 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3582 le32_to_cpu(raw_super->node_ino),
3583 le32_to_cpu(raw_super->meta_ino),
3584 le32_to_cpu(raw_super->root_ino));
3585 return -EFSCORRUPTED;
3588 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
3589 if (sanity_check_area_boundary(sbi, bh))
3590 return -EFSCORRUPTED;
3595 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
3597 unsigned int total, fsmeta;
3598 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3599 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3600 unsigned int ovp_segments, reserved_segments;
3601 unsigned int main_segs, blocks_per_seg;
3602 unsigned int sit_segs, nat_segs;
3603 unsigned int sit_bitmap_size, nat_bitmap_size;
3604 unsigned int log_blocks_per_seg;
3605 unsigned int segment_count_main;
3606 unsigned int cp_pack_start_sum, cp_payload;
3607 block_t user_block_count, valid_user_blocks;
3608 block_t avail_node_count, valid_node_count;
3609 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
3612 total = le32_to_cpu(raw_super->segment_count);
3613 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
3614 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3616 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3618 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3619 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3621 if (unlikely(fsmeta >= total))
3624 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3625 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3627 if (!f2fs_sb_has_readonly(sbi) &&
3628 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
3629 ovp_segments == 0 || reserved_segments == 0)) {
3630 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
3633 user_block_count = le64_to_cpu(ckpt->user_block_count);
3634 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3635 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
3636 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3637 if (!user_block_count || user_block_count >=
3638 segment_count_main << log_blocks_per_seg) {
3639 f2fs_err(sbi, "Wrong user_block_count: %u",
3644 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3645 if (valid_user_blocks > user_block_count) {
3646 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3647 valid_user_blocks, user_block_count);
3651 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
3652 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
3653 if (valid_node_count > avail_node_count) {
3654 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3655 valid_node_count, avail_node_count);
3659 main_segs = le32_to_cpu(raw_super->segment_count_main);
3660 blocks_per_seg = sbi->blocks_per_seg;
3662 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3663 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3664 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3667 if (f2fs_sb_has_readonly(sbi))
3670 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3671 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3672 le32_to_cpu(ckpt->cur_node_segno[j])) {
3673 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3675 le32_to_cpu(ckpt->cur_node_segno[i]));
3681 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3682 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3683 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3686 if (f2fs_sb_has_readonly(sbi))
3689 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3690 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3691 le32_to_cpu(ckpt->cur_data_segno[j])) {
3692 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3694 le32_to_cpu(ckpt->cur_data_segno[i]));
3699 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3700 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3701 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3702 le32_to_cpu(ckpt->cur_data_segno[j])) {
3703 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3705 le32_to_cpu(ckpt->cur_node_segno[i]));
3711 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3712 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3714 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3715 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3716 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3717 sit_bitmap_size, nat_bitmap_size);
3721 cp_pack_start_sum = __start_sum_addr(sbi);
3722 cp_payload = __cp_payload(sbi);
3723 if (cp_pack_start_sum < cp_payload + 1 ||
3724 cp_pack_start_sum > blocks_per_seg - 1 -
3725 NR_CURSEG_PERSIST_TYPE) {
3726 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3731 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3732 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3733 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3734 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3735 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3736 le32_to_cpu(ckpt->checksum_offset));
3740 nat_blocks = nat_segs << log_blocks_per_seg;
3741 nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
3742 nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3743 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
3744 (cp_payload + F2FS_CP_PACKS +
3745 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
3746 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
3747 cp_payload, nat_bits_blocks);
3751 if (unlikely(f2fs_cp_error(sbi))) {
3752 f2fs_err(sbi, "A bug case: need to run fsck");
3758 static void init_sb_info(struct f2fs_sb_info *sbi)
3760 struct f2fs_super_block *raw_super = sbi->raw_super;
3763 sbi->log_sectors_per_block =
3764 le32_to_cpu(raw_super->log_sectors_per_block);
3765 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3766 sbi->blocksize = BIT(sbi->log_blocksize);
3767 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3768 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
3769 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3770 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3771 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3772 sbi->total_node_count =
3773 (le32_to_cpu(raw_super->segment_count_nat) / 2)
3774 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
3775 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3776 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3777 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3778 sbi->cur_victim_sec = NULL_SECNO;
3779 sbi->gc_mode = GC_NORMAL;
3780 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3781 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3782 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3783 sbi->migration_granularity = sbi->segs_per_sec;
3784 sbi->seq_file_ra_mul = MIN_RA_MUL;
3785 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
3786 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
3787 spin_lock_init(&sbi->gc_remaining_trials_lock);
3788 atomic64_set(&sbi->current_atomic_write, 0);
3790 sbi->dir_level = DEF_DIR_LEVEL;
3791 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3792 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3793 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3794 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3795 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3796 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3797 DEF_UMOUNT_DISCARD_TIMEOUT;
3798 clear_sbi_flag(sbi, SBI_NEED_FSCK);
3800 for (i = 0; i < NR_COUNT_TYPE; i++)
3801 atomic_set(&sbi->nr_pages[i], 0);
3803 for (i = 0; i < META; i++)
3804 atomic_set(&sbi->wb_sync_req[i], 0);
3806 INIT_LIST_HEAD(&sbi->s_list);
3807 mutex_init(&sbi->umount_mutex);
3808 init_f2fs_rwsem(&sbi->io_order_lock);
3809 spin_lock_init(&sbi->cp_lock);
3811 sbi->dirty_device = 0;
3812 spin_lock_init(&sbi->dev_lock);
3814 init_f2fs_rwsem(&sbi->sb_lock);
3815 init_f2fs_rwsem(&sbi->pin_sem);
3818 static int init_percpu_info(struct f2fs_sb_info *sbi)
3822 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3826 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
3828 goto err_valid_block;
3830 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3833 goto err_node_block;
3837 percpu_counter_destroy(&sbi->rf_node_block_count);
3839 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3843 #ifdef CONFIG_BLK_DEV_ZONED
3845 struct f2fs_report_zones_args {
3846 struct f2fs_sb_info *sbi;
3847 struct f2fs_dev_info *dev;
3850 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3853 struct f2fs_report_zones_args *rz_args = data;
3854 block_t unusable_blocks = (zone->len - zone->capacity) >>
3855 F2FS_LOG_SECTORS_PER_BLOCK;
3857 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3860 set_bit(idx, rz_args->dev->blkz_seq);
3861 if (!rz_args->sbi->unusable_blocks_per_sec) {
3862 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
3865 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
3866 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
3872 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
3874 struct block_device *bdev = FDEV(devi).bdev;
3875 sector_t nr_sectors = bdev_nr_sectors(bdev);
3876 struct f2fs_report_zones_args rep_zone_arg;
3880 if (!f2fs_sb_has_blkzoned(sbi))
3883 zone_sectors = bdev_zone_sectors(bdev);
3884 if (!is_power_of_2(zone_sectors)) {
3885 f2fs_err(sbi, "F2FS does not support non power of 2 zone sizes\n");
3889 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
3890 SECTOR_TO_BLOCK(zone_sectors))
3892 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
3893 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
3894 sbi->blocks_per_blkz);
3895 if (nr_sectors & (zone_sectors - 1))
3896 FDEV(devi).nr_blkz++;
3898 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
3899 BITS_TO_LONGS(FDEV(devi).nr_blkz)
3900 * sizeof(unsigned long),
3902 if (!FDEV(devi).blkz_seq)
3905 rep_zone_arg.sbi = sbi;
3906 rep_zone_arg.dev = &FDEV(devi);
3908 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3917 * Read f2fs raw super block.
3918 * Because we have two copies of super block, so read both of them
3919 * to get the first valid one. If any one of them is broken, we pass
3920 * them recovery flag back to the caller.
3922 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3923 struct f2fs_super_block **raw_super,
3924 int *valid_super_block, int *recovery)
3926 struct super_block *sb = sbi->sb;
3928 struct buffer_head *bh;
3929 struct f2fs_super_block *super;
3932 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3936 for (block = 0; block < 2; block++) {
3937 bh = sb_bread(sb, block);
3939 f2fs_err(sbi, "Unable to read %dth superblock",
3946 /* sanity checking of raw super */
3947 err = sanity_check_raw_super(sbi, bh);
3949 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3957 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3959 *valid_super_block = block;
3965 /* No valid superblock */
3974 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3976 struct buffer_head *bh;
3980 if ((recover && f2fs_readonly(sbi->sb)) ||
3981 f2fs_hw_is_readonly(sbi)) {
3982 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3986 /* we should update superblock crc here */
3987 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
3988 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3989 offsetof(struct f2fs_super_block, crc));
3990 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3993 /* write back-up superblock first */
3994 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
3997 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4000 /* if we are in recovery path, skip writing valid superblock */
4004 /* write current valid superblock */
4005 bh = sb_bread(sbi->sb, sbi->valid_super_block);
4008 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4013 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
4015 unsigned long flags;
4017 spin_lock_irqsave(&sbi->error_lock, flags);
4018 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
4019 sbi->stop_reason[reason]++;
4020 spin_unlock_irqrestore(&sbi->error_lock, flags);
4023 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
4025 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4026 unsigned long flags;
4029 f2fs_down_write(&sbi->sb_lock);
4031 spin_lock_irqsave(&sbi->error_lock, flags);
4032 if (sbi->error_dirty) {
4033 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4035 sbi->error_dirty = false;
4037 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
4038 spin_unlock_irqrestore(&sbi->error_lock, flags);
4040 err = f2fs_commit_super(sbi, false);
4042 f2fs_up_write(&sbi->sb_lock);
4044 f2fs_err(sbi, "f2fs_commit_super fails to record err:%d", err);
4047 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
4049 unsigned long flags;
4051 spin_lock_irqsave(&sbi->error_lock, flags);
4052 if (!test_bit(flag, (unsigned long *)sbi->errors)) {
4053 set_bit(flag, (unsigned long *)sbi->errors);
4054 sbi->error_dirty = true;
4056 spin_unlock_irqrestore(&sbi->error_lock, flags);
4059 static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
4061 unsigned long flags;
4062 bool need_update = false;
4064 spin_lock_irqsave(&sbi->error_lock, flags);
4065 if (sbi->error_dirty) {
4066 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4068 sbi->error_dirty = false;
4071 spin_unlock_irqrestore(&sbi->error_lock, flags);
4076 static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error)
4080 f2fs_down_write(&sbi->sb_lock);
4082 if (!f2fs_update_errors(sbi))
4085 err = f2fs_commit_super(sbi, false);
4087 f2fs_err(sbi, "f2fs_commit_super fails to record errors:%u, err:%d",
4090 f2fs_up_write(&sbi->sb_lock);
4093 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4095 f2fs_save_errors(sbi, error);
4096 f2fs_record_errors(sbi, error);
4099 void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error)
4101 f2fs_save_errors(sbi, error);
4103 if (!sbi->error_dirty)
4105 if (!test_bit(error, (unsigned long *)sbi->errors))
4107 schedule_work(&sbi->s_error_work);
4110 static bool system_going_down(void)
4112 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4113 || system_state == SYSTEM_RESTART;
4116 void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason,
4119 struct super_block *sb = sbi->sb;
4120 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
4121 bool continue_fs = !shutdown &&
4122 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
4124 set_ckpt_flags(sbi, CP_ERROR_FLAG);
4126 if (!f2fs_hw_is_readonly(sbi)) {
4127 save_stop_reason(sbi, reason);
4129 if (irq_context && !shutdown)
4130 schedule_work(&sbi->s_error_work);
4132 f2fs_record_stop_reason(sbi);
4136 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4137 * could panic during 'reboot -f' as the underlying device got already
4140 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
4141 !shutdown && !system_going_down() &&
4142 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
4143 panic("F2FS-fs (device %s): panic forced after error\n",
4147 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
4149 /* continue filesystem operators if errors=continue */
4150 if (continue_fs || f2fs_readonly(sb))
4153 f2fs_warn(sbi, "Remounting filesystem read-only");
4155 * Make sure updated value of ->s_mount_flags will be visible before
4159 sb->s_flags |= SB_RDONLY;
4162 static void f2fs_record_error_work(struct work_struct *work)
4164 struct f2fs_sb_info *sbi = container_of(work,
4165 struct f2fs_sb_info, s_error_work);
4167 f2fs_record_stop_reason(sbi);
4170 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
4172 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4173 unsigned int max_devices = MAX_DEVICES;
4174 unsigned int logical_blksize;
4175 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
4178 /* Initialize single device information */
4179 if (!RDEV(0).path[0]) {
4180 if (!bdev_is_zoned(sbi->sb->s_bdev))
4186 * Initialize multiple devices information, or single
4187 * zoned block device information.
4189 sbi->devs = f2fs_kzalloc(sbi,
4190 array_size(max_devices,
4191 sizeof(struct f2fs_dev_info)),
4196 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
4197 sbi->aligned_blksize = true;
4199 for (i = 0; i < max_devices; i++) {
4201 FDEV(0).bdev = sbi->sb->s_bdev;
4202 else if (!RDEV(i).path[0])
4205 if (max_devices > 1) {
4206 /* Multi-device mount */
4207 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
4208 FDEV(i).total_segments =
4209 le32_to_cpu(RDEV(i).total_segments);
4211 FDEV(i).start_blk = 0;
4212 FDEV(i).end_blk = FDEV(i).start_blk +
4213 (FDEV(i).total_segments <<
4214 sbi->log_blocks_per_seg) - 1 +
4215 le32_to_cpu(raw_super->segment0_blkaddr);
4217 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
4218 FDEV(i).end_blk = FDEV(i).start_blk +
4219 (FDEV(i).total_segments <<
4220 sbi->log_blocks_per_seg) - 1;
4221 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
4222 mode, sbi->sb, NULL);
4225 if (IS_ERR(FDEV(i).bdev))
4226 return PTR_ERR(FDEV(i).bdev);
4228 /* to release errored devices */
4229 sbi->s_ndevs = i + 1;
4231 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
4232 sbi->aligned_blksize = false;
4234 #ifdef CONFIG_BLK_DEV_ZONED
4235 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
4236 !f2fs_sb_has_blkzoned(sbi)) {
4237 f2fs_err(sbi, "Zoned block device feature not enabled");
4240 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
4241 if (init_blkz_info(sbi, i)) {
4242 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
4245 if (max_devices == 1)
4247 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
4249 FDEV(i).total_segments,
4250 FDEV(i).start_blk, FDEV(i).end_blk,
4251 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
4252 "Host-aware" : "Host-managed");
4256 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
4258 FDEV(i).total_segments,
4259 FDEV(i).start_blk, FDEV(i).end_blk);
4262 "IO Block Size: %8ld KB", F2FS_IO_SIZE_KB(sbi));
4266 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
4268 #if IS_ENABLED(CONFIG_UNICODE)
4269 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
4270 const struct f2fs_sb_encodings *encoding_info;
4271 struct unicode_map *encoding;
4272 __u16 encoding_flags;
4274 encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
4275 if (!encoding_info) {
4277 "Encoding requested by superblock is unknown");
4281 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
4282 encoding = utf8_load(encoding_info->version);
4283 if (IS_ERR(encoding)) {
4285 "can't mount with superblock charset: %s-%u.%u.%u "
4286 "not supported by the kernel. flags: 0x%x.",
4287 encoding_info->name,
4288 unicode_major(encoding_info->version),
4289 unicode_minor(encoding_info->version),
4290 unicode_rev(encoding_info->version),
4292 return PTR_ERR(encoding);
4294 f2fs_info(sbi, "Using encoding defined by superblock: "
4295 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4296 unicode_major(encoding_info->version),
4297 unicode_minor(encoding_info->version),
4298 unicode_rev(encoding_info->version),
4301 sbi->sb->s_encoding = encoding;
4302 sbi->sb->s_encoding_flags = encoding_flags;
4305 if (f2fs_sb_has_casefold(sbi)) {
4306 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
4313 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
4315 /* adjust parameters according to the volume size */
4316 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
4317 if (f2fs_block_unit_discard(sbi))
4318 SM_I(sbi)->dcc_info->discard_granularity =
4319 MIN_DISCARD_GRANULARITY;
4320 if (!f2fs_lfs_mode(sbi))
4321 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
4322 BIT(F2FS_IPU_HONOR_OPU_WRITE);
4325 sbi->readdir_ra = true;
4328 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
4330 struct f2fs_sb_info *sbi;
4331 struct f2fs_super_block *raw_super;
4334 bool skip_recovery = false, need_fsck = false;
4335 char *options = NULL;
4336 int recovery, i, valid_super_block;
4337 struct curseg_info *seg_i;
4340 bool quota_enabled = false;
4346 valid_super_block = -1;
4349 /* allocate memory for f2fs-specific super block info */
4350 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
4356 /* initialize locks within allocated memory */
4357 init_f2fs_rwsem(&sbi->gc_lock);
4358 mutex_init(&sbi->writepages);
4359 init_f2fs_rwsem(&sbi->cp_global_sem);
4360 init_f2fs_rwsem(&sbi->node_write);
4361 init_f2fs_rwsem(&sbi->node_change);
4362 spin_lock_init(&sbi->stat_lock);
4363 init_f2fs_rwsem(&sbi->cp_rwsem);
4364 init_f2fs_rwsem(&sbi->quota_sem);
4365 init_waitqueue_head(&sbi->cp_wait);
4366 spin_lock_init(&sbi->error_lock);
4368 for (i = 0; i < NR_INODE_TYPE; i++) {
4369 INIT_LIST_HEAD(&sbi->inode_list[i]);
4370 spin_lock_init(&sbi->inode_lock[i]);
4372 mutex_init(&sbi->flush_lock);
4374 /* Load the checksum driver */
4375 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
4376 if (IS_ERR(sbi->s_chksum_driver)) {
4377 f2fs_err(sbi, "Cannot load crc32 driver.");
4378 err = PTR_ERR(sbi->s_chksum_driver);
4379 sbi->s_chksum_driver = NULL;
4383 /* set a block size */
4384 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
4385 f2fs_err(sbi, "unable to set blocksize");
4389 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
4394 sb->s_fs_info = sbi;
4395 sbi->raw_super = raw_super;
4397 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
4398 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
4399 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
4401 /* precompute checksum seed for metadata */
4402 if (f2fs_sb_has_inode_chksum(sbi))
4403 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
4404 sizeof(raw_super->uuid));
4406 default_options(sbi, false);
4407 /* parse mount options */
4408 options = kstrdup((const char *)data, GFP_KERNEL);
4409 if (data && !options) {
4414 err = parse_options(sb, options, false);
4418 sb->s_maxbytes = max_file_blocks(NULL) <<
4419 le32_to_cpu(raw_super->log_blocksize);
4420 sb->s_max_links = F2FS_LINK_MAX;
4422 err = f2fs_setup_casefold(sbi);
4427 sb->dq_op = &f2fs_quota_operations;
4428 sb->s_qcop = &f2fs_quotactl_ops;
4429 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4431 if (f2fs_sb_has_quota_ino(sbi)) {
4432 for (i = 0; i < MAXQUOTAS; i++) {
4433 if (f2fs_qf_ino(sbi->sb, i))
4434 sbi->nquota_files++;
4439 sb->s_op = &f2fs_sops;
4440 #ifdef CONFIG_FS_ENCRYPTION
4441 sb->s_cop = &f2fs_cryptops;
4443 #ifdef CONFIG_FS_VERITY
4444 sb->s_vop = &f2fs_verityops;
4446 sb->s_xattr = f2fs_xattr_handlers;
4447 sb->s_export_op = &f2fs_export_ops;
4448 sb->s_magic = F2FS_SUPER_MAGIC;
4449 sb->s_time_gran = 1;
4450 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4451 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
4452 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
4453 sb->s_iflags |= SB_I_CGROUPWB;
4455 /* init f2fs-specific super block info */
4456 sbi->valid_super_block = valid_super_block;
4458 /* disallow all the data/node/meta page writes */
4459 set_sbi_flag(sbi, SBI_POR_DOING);
4461 err = f2fs_init_write_merge_io(sbi);
4467 err = f2fs_init_iostat(sbi);
4471 err = init_percpu_info(sbi);
4475 if (F2FS_IO_ALIGNED(sbi)) {
4476 sbi->write_io_dummy =
4477 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
4478 if (!sbi->write_io_dummy) {
4484 /* init per sbi slab cache */
4485 err = f2fs_init_xattr_caches(sbi);
4488 err = f2fs_init_page_array_cache(sbi);
4490 goto free_xattr_cache;
4492 /* get an inode for meta space */
4493 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
4494 if (IS_ERR(sbi->meta_inode)) {
4495 f2fs_err(sbi, "Failed to read F2FS meta data inode");
4496 err = PTR_ERR(sbi->meta_inode);
4497 goto free_page_array_cache;
4500 err = f2fs_get_valid_checkpoint(sbi);
4502 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
4503 goto free_meta_inode;
4506 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
4507 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
4508 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
4509 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4510 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
4513 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
4514 set_sbi_flag(sbi, SBI_NEED_FSCK);
4516 /* Initialize device list */
4517 err = f2fs_scan_devices(sbi);
4519 f2fs_err(sbi, "Failed to find devices");
4523 err = f2fs_init_post_read_wq(sbi);
4525 f2fs_err(sbi, "Failed to initialize post read workqueue");
4529 sbi->total_valid_node_count =
4530 le32_to_cpu(sbi->ckpt->valid_node_count);
4531 percpu_counter_set(&sbi->total_valid_inode_count,
4532 le32_to_cpu(sbi->ckpt->valid_inode_count));
4533 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
4534 sbi->total_valid_block_count =
4535 le64_to_cpu(sbi->ckpt->valid_block_count);
4536 sbi->last_valid_block_count = sbi->total_valid_block_count;
4537 sbi->reserved_blocks = 0;
4538 sbi->current_reserved_blocks = 0;
4539 limit_reserve_root(sbi);
4540 adjust_unusable_cap_perc(sbi);
4542 f2fs_init_extent_cache_info(sbi);
4544 f2fs_init_ino_entry_info(sbi);
4546 f2fs_init_fsync_node_info(sbi);
4548 /* setup checkpoint request control and start checkpoint issue thread */
4549 f2fs_init_ckpt_req_control(sbi);
4550 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
4551 test_opt(sbi, MERGE_CHECKPOINT)) {
4552 err = f2fs_start_ckpt_thread(sbi);
4555 "Failed to start F2FS issue_checkpoint_thread (%d)",
4557 goto stop_ckpt_thread;
4561 /* setup f2fs internal modules */
4562 err = f2fs_build_segment_manager(sbi);
4564 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
4568 err = f2fs_build_node_manager(sbi);
4570 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
4575 err = adjust_reserved_segment(sbi);
4579 /* For write statistics */
4580 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
4582 /* Read accumulated write IO statistics if exists */
4583 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
4584 if (__exist_node_summaries(sbi))
4585 sbi->kbytes_written =
4586 le64_to_cpu(seg_i->journal->info.kbytes_written);
4588 f2fs_build_gc_manager(sbi);
4590 err = f2fs_build_stats(sbi);
4594 /* get an inode for node space */
4595 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
4596 if (IS_ERR(sbi->node_inode)) {
4597 f2fs_err(sbi, "Failed to read node inode");
4598 err = PTR_ERR(sbi->node_inode);
4602 /* read root inode and dentry */
4603 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
4605 f2fs_err(sbi, "Failed to read root inode");
4606 err = PTR_ERR(root);
4607 goto free_node_inode;
4609 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
4610 !root->i_size || !root->i_nlink) {
4613 goto free_node_inode;
4616 sb->s_root = d_make_root(root); /* allocate root dentry */
4619 goto free_node_inode;
4622 err = f2fs_init_compress_inode(sbi);
4624 goto free_root_inode;
4626 err = f2fs_register_sysfs(sbi);
4628 goto free_compress_inode;
4631 /* Enable quota usage during mount */
4632 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
4633 err = f2fs_enable_quotas(sb);
4635 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
4638 quota_enabled = f2fs_recover_quota_begin(sbi);
4640 /* if there are any orphan inodes, free them */
4641 err = f2fs_recover_orphan_inodes(sbi);
4645 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
4646 goto reset_checkpoint;
4648 /* recover fsynced data */
4649 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
4650 !test_opt(sbi, NORECOVERY)) {
4652 * mount should be failed, when device has readonly mode, and
4653 * previous checkpoint was not done by clean system shutdown.
4655 if (f2fs_hw_is_readonly(sbi)) {
4656 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4657 err = f2fs_recover_fsync_data(sbi, true);
4660 f2fs_err(sbi, "Need to recover fsync data, but "
4661 "write access unavailable, please try "
4662 "mount w/ disable_roll_forward or norecovery");
4667 f2fs_info(sbi, "write access unavailable, skipping recovery");
4668 goto reset_checkpoint;
4672 set_sbi_flag(sbi, SBI_NEED_FSCK);
4675 goto reset_checkpoint;
4677 err = f2fs_recover_fsync_data(sbi, false);
4680 skip_recovery = true;
4682 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4687 err = f2fs_recover_fsync_data(sbi, true);
4689 if (!f2fs_readonly(sb) && err > 0) {
4691 f2fs_err(sbi, "Need to recover fsync data");
4697 f2fs_recover_quota_end(sbi, quota_enabled);
4701 * If the f2fs is not readonly and fsync data recovery succeeds,
4702 * check zoned block devices' write pointer consistency.
4704 if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
4705 err = f2fs_check_write_pointer(sbi);
4711 f2fs_init_inmem_curseg(sbi);
4713 /* f2fs_recover_fsync_data() cleared this already */
4714 clear_sbi_flag(sbi, SBI_POR_DOING);
4716 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4717 err = f2fs_disable_checkpoint(sbi);
4719 goto sync_free_meta;
4720 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4721 f2fs_enable_checkpoint(sbi);
4725 * If filesystem is not mounted as read-only then
4726 * do start the gc_thread.
4728 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4729 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
4730 /* After POR, we can run background GC thread.*/
4731 err = f2fs_start_gc_thread(sbi);
4733 goto sync_free_meta;
4737 /* recover broken superblock */
4739 err = f2fs_commit_super(sbi, true);
4740 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4741 sbi->valid_super_block ? 1 : 2, err);
4744 f2fs_join_shrinker(sbi);
4746 f2fs_tuning_parameters(sbi);
4748 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4749 cur_cp_version(F2FS_CKPT(sbi)));
4750 f2fs_update_time(sbi, CP_TIME);
4751 f2fs_update_time(sbi, REQ_TIME);
4752 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4756 /* safe to flush all the data */
4757 sync_filesystem(sbi->sb);
4762 f2fs_truncate_quota_inode_pages(sb);
4763 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
4764 f2fs_quota_off_umount(sbi->sb);
4767 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4768 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4769 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4770 * falls into an infinite loop in f2fs_sync_meta_pages().
4772 truncate_inode_pages_final(META_MAPPING(sbi));
4773 /* evict some inodes being cached by GC */
4775 f2fs_unregister_sysfs(sbi);
4776 free_compress_inode:
4777 f2fs_destroy_compress_inode(sbi);
4782 f2fs_release_ino_entry(sbi, true);
4783 truncate_inode_pages_final(NODE_MAPPING(sbi));
4784 iput(sbi->node_inode);
4785 sbi->node_inode = NULL;
4787 f2fs_destroy_stats(sbi);
4789 /* stop discard thread before destroying node manager */
4790 f2fs_stop_discard_thread(sbi);
4791 f2fs_destroy_node_manager(sbi);
4793 f2fs_destroy_segment_manager(sbi);
4795 f2fs_stop_ckpt_thread(sbi);
4796 /* flush s_error_work before sbi destroy */
4797 flush_work(&sbi->s_error_work);
4798 f2fs_destroy_post_read_wq(sbi);
4800 destroy_device_list(sbi);
4803 make_bad_inode(sbi->meta_inode);
4804 iput(sbi->meta_inode);
4805 sbi->meta_inode = NULL;
4806 free_page_array_cache:
4807 f2fs_destroy_page_array_cache(sbi);
4809 f2fs_destroy_xattr_caches(sbi);
4811 mempool_destroy(sbi->write_io_dummy);
4813 destroy_percpu_info(sbi);
4815 f2fs_destroy_iostat(sbi);
4817 for (i = 0; i < NR_PAGE_TYPE; i++)
4818 kvfree(sbi->write_io[i]);
4820 #if IS_ENABLED(CONFIG_UNICODE)
4821 utf8_unload(sb->s_encoding);
4822 sb->s_encoding = NULL;
4826 for (i = 0; i < MAXQUOTAS; i++)
4827 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4829 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
4834 if (sbi->s_chksum_driver)
4835 crypto_free_shash(sbi->s_chksum_driver);
4838 /* give only one another chance */
4839 if (retry_cnt > 0 && skip_recovery) {
4841 shrink_dcache_sb(sb);
4847 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
4848 const char *dev_name, void *data)
4850 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
4853 static void kill_f2fs_super(struct super_block *sb)
4856 struct f2fs_sb_info *sbi = F2FS_SB(sb);
4858 set_sbi_flag(sbi, SBI_IS_CLOSE);
4859 f2fs_stop_gc_thread(sbi);
4860 f2fs_stop_discard_thread(sbi);
4862 #ifdef CONFIG_F2FS_FS_COMPRESSION
4864 * latter evict_inode() can bypass checking and invalidating
4865 * compress inode cache.
4867 if (test_opt(sbi, COMPRESS_CACHE))
4868 truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
4871 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
4872 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4873 struct cp_control cpc = {
4874 .reason = CP_UMOUNT,
4876 stat_inc_cp_call_count(sbi, TOTAL_CALL);
4877 f2fs_write_checkpoint(sbi, &cpc);
4880 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
4881 sb->s_flags &= ~SB_RDONLY;
4883 kill_block_super(sb);
4886 static struct file_system_type f2fs_fs_type = {
4887 .owner = THIS_MODULE,
4889 .mount = f2fs_mount,
4890 .kill_sb = kill_f2fs_super,
4891 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
4893 MODULE_ALIAS_FS("f2fs");
4895 static int __init init_inodecache(void)
4897 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
4898 sizeof(struct f2fs_inode_info), 0,
4899 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
4900 return f2fs_inode_cachep ? 0 : -ENOMEM;
4903 static void destroy_inodecache(void)
4906 * Make sure all delayed rcu free inodes are flushed before we
4910 kmem_cache_destroy(f2fs_inode_cachep);
4913 static int __init init_f2fs_fs(void)
4917 if (PAGE_SIZE != F2FS_BLKSIZE) {
4918 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
4919 PAGE_SIZE, F2FS_BLKSIZE);
4923 err = init_inodecache();
4926 err = f2fs_create_node_manager_caches();
4928 goto free_inodecache;
4929 err = f2fs_create_segment_manager_caches();
4931 goto free_node_manager_caches;
4932 err = f2fs_create_checkpoint_caches();
4934 goto free_segment_manager_caches;
4935 err = f2fs_create_recovery_cache();
4937 goto free_checkpoint_caches;
4938 err = f2fs_create_extent_cache();
4940 goto free_recovery_cache;
4941 err = f2fs_create_garbage_collection_cache();
4943 goto free_extent_cache;
4944 err = f2fs_init_sysfs();
4946 goto free_garbage_collection_cache;
4947 err = register_shrinker(&f2fs_shrinker_info, "f2fs-shrinker");
4950 err = register_filesystem(&f2fs_fs_type);
4953 f2fs_create_root_stats();
4954 err = f2fs_init_post_read_processing();
4956 goto free_root_stats;
4957 err = f2fs_init_iostat_processing();
4959 goto free_post_read;
4960 err = f2fs_init_bio_entry_cache();
4963 err = f2fs_init_bioset();
4965 goto free_bio_entry_cache;
4966 err = f2fs_init_compress_mempool();
4969 err = f2fs_init_compress_cache();
4971 goto free_compress_mempool;
4972 err = f2fs_create_casefold_cache();
4974 goto free_compress_cache;
4976 free_compress_cache:
4977 f2fs_destroy_compress_cache();
4978 free_compress_mempool:
4979 f2fs_destroy_compress_mempool();
4981 f2fs_destroy_bioset();
4982 free_bio_entry_cache:
4983 f2fs_destroy_bio_entry_cache();
4985 f2fs_destroy_iostat_processing();
4987 f2fs_destroy_post_read_processing();
4989 f2fs_destroy_root_stats();
4990 unregister_filesystem(&f2fs_fs_type);
4992 unregister_shrinker(&f2fs_shrinker_info);
4995 free_garbage_collection_cache:
4996 f2fs_destroy_garbage_collection_cache();
4998 f2fs_destroy_extent_cache();
4999 free_recovery_cache:
5000 f2fs_destroy_recovery_cache();
5001 free_checkpoint_caches:
5002 f2fs_destroy_checkpoint_caches();
5003 free_segment_manager_caches:
5004 f2fs_destroy_segment_manager_caches();
5005 free_node_manager_caches:
5006 f2fs_destroy_node_manager_caches();
5008 destroy_inodecache();
5013 static void __exit exit_f2fs_fs(void)
5015 f2fs_destroy_casefold_cache();
5016 f2fs_destroy_compress_cache();
5017 f2fs_destroy_compress_mempool();
5018 f2fs_destroy_bioset();
5019 f2fs_destroy_bio_entry_cache();
5020 f2fs_destroy_iostat_processing();
5021 f2fs_destroy_post_read_processing();
5022 f2fs_destroy_root_stats();
5023 unregister_filesystem(&f2fs_fs_type);
5024 unregister_shrinker(&f2fs_shrinker_info);
5026 f2fs_destroy_garbage_collection_cache();
5027 f2fs_destroy_extent_cache();
5028 f2fs_destroy_recovery_cache();
5029 f2fs_destroy_checkpoint_caches();
5030 f2fs_destroy_segment_manager_caches();
5031 f2fs_destroy_node_manager_caches();
5032 destroy_inodecache();
5035 module_init(init_f2fs_fs)
5036 module_exit(exit_f2fs_fs)
5038 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
5039 MODULE_DESCRIPTION("Flash Friendly File System");
5040 MODULE_LICENSE("GPL");
5041 MODULE_SOFTDEP("pre: crc32");