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
550 static bool is_compress_extension_exist(struct f2fs_sb_info *sbi,
551 const char *new_ext, bool is_ext)
553 unsigned char (*ext)[F2FS_EXTENSION_LEN];
558 ext = F2FS_OPTION(sbi).extensions;
559 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
561 ext = F2FS_OPTION(sbi).noextensions;
562 ext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
565 for (i = 0; i < ext_cnt; i++) {
566 if (!strcasecmp(new_ext, ext[i]))
574 * 1. The same extension name cannot not appear in both compress and non-compress extension
576 * 2. If the compress extension specifies all files, the types specified by the non-compress
577 * extension will be treated as special cases and will not be compressed.
578 * 3. Don't allow the non-compress extension specifies all files.
580 static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
582 unsigned char (*ext)[F2FS_EXTENSION_LEN];
583 unsigned char (*noext)[F2FS_EXTENSION_LEN];
584 int ext_cnt, noext_cnt, index = 0, no_index = 0;
586 ext = F2FS_OPTION(sbi).extensions;
587 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
588 noext = F2FS_OPTION(sbi).noextensions;
589 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
594 for (no_index = 0; no_index < noext_cnt; no_index++) {
595 if (!strcasecmp("*", noext[no_index])) {
596 f2fs_info(sbi, "Don't allow the nocompress extension specifies all files");
599 for (index = 0; index < ext_cnt; index++) {
600 if (!strcasecmp(ext[index], noext[no_index])) {
601 f2fs_info(sbi, "Don't allow the same extension %s appear in both compress and nocompress extension",
610 #ifdef CONFIG_F2FS_FS_LZ4
611 static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
613 #ifdef CONFIG_F2FS_FS_LZ4HC
616 if (strlen(str) == 3) {
617 F2FS_OPTION(sbi).compress_level = 0;
624 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
627 if (kstrtouint(str + 1, 10, &level))
630 if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
631 f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
635 F2FS_OPTION(sbi).compress_level = level;
638 if (strlen(str) == 3) {
639 F2FS_OPTION(sbi).compress_level = 0;
642 f2fs_info(sbi, "kernel doesn't support lz4hc compression");
648 #ifdef CONFIG_F2FS_FS_ZSTD
649 static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
654 if (strlen(str) == len) {
655 F2FS_OPTION(sbi).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
662 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
665 if (kstrtouint(str + 1, 10, &level))
668 if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
669 f2fs_info(sbi, "invalid zstd compress level: %d", level);
673 F2FS_OPTION(sbi).compress_level = level;
679 static int parse_options(struct super_block *sb, char *options, bool is_remount)
681 struct f2fs_sb_info *sbi = F2FS_SB(sb);
682 substring_t args[MAX_OPT_ARGS];
683 #ifdef CONFIG_F2FS_FS_COMPRESSION
684 unsigned char (*ext)[F2FS_EXTENSION_LEN];
685 unsigned char (*noext)[F2FS_EXTENSION_LEN];
686 int ext_cnt, noext_cnt;
697 while ((p = strsep(&options, ",")) != NULL) {
703 * Initialize args struct so we know whether arg was
704 * found; some options take optional arguments.
706 args[0].to = args[0].from = NULL;
707 token = match_token(p, f2fs_tokens, args);
710 case Opt_gc_background:
711 name = match_strdup(&args[0]);
715 if (!strcmp(name, "on")) {
716 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
717 } else if (!strcmp(name, "off")) {
718 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
719 } else if (!strcmp(name, "sync")) {
720 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
727 case Opt_disable_roll_forward:
728 set_opt(sbi, DISABLE_ROLL_FORWARD);
731 /* this option mounts f2fs with ro */
732 set_opt(sbi, NORECOVERY);
733 if (!f2fs_readonly(sb))
737 if (!f2fs_hw_support_discard(sbi)) {
738 f2fs_warn(sbi, "device does not support discard");
741 set_opt(sbi, DISCARD);
744 if (f2fs_hw_should_discard(sbi)) {
745 f2fs_warn(sbi, "discard is required for zoned block devices");
748 clear_opt(sbi, DISCARD);
751 set_opt(sbi, NOHEAP);
754 clear_opt(sbi, NOHEAP);
756 #ifdef CONFIG_F2FS_FS_XATTR
758 set_opt(sbi, XATTR_USER);
760 case Opt_nouser_xattr:
761 clear_opt(sbi, XATTR_USER);
763 case Opt_inline_xattr:
764 set_opt(sbi, INLINE_XATTR);
766 case Opt_noinline_xattr:
767 clear_opt(sbi, INLINE_XATTR);
769 case Opt_inline_xattr_size:
770 if (args->from && match_int(args, &arg))
772 set_opt(sbi, INLINE_XATTR_SIZE);
773 F2FS_OPTION(sbi).inline_xattr_size = arg;
777 f2fs_info(sbi, "user_xattr options not supported");
779 case Opt_nouser_xattr:
780 f2fs_info(sbi, "nouser_xattr options not supported");
782 case Opt_inline_xattr:
783 f2fs_info(sbi, "inline_xattr options not supported");
785 case Opt_noinline_xattr:
786 f2fs_info(sbi, "noinline_xattr options not supported");
789 #ifdef CONFIG_F2FS_FS_POSIX_ACL
791 set_opt(sbi, POSIX_ACL);
794 clear_opt(sbi, POSIX_ACL);
798 f2fs_info(sbi, "acl options not supported");
801 f2fs_info(sbi, "noacl options not supported");
804 case Opt_active_logs:
805 if (args->from && match_int(args, &arg))
807 if (arg != 2 && arg != 4 &&
808 arg != NR_CURSEG_PERSIST_TYPE)
810 F2FS_OPTION(sbi).active_logs = arg;
812 case Opt_disable_ext_identify:
813 set_opt(sbi, DISABLE_EXT_IDENTIFY);
815 case Opt_inline_data:
816 set_opt(sbi, INLINE_DATA);
818 case Opt_inline_dentry:
819 set_opt(sbi, INLINE_DENTRY);
821 case Opt_noinline_dentry:
822 clear_opt(sbi, INLINE_DENTRY);
824 case Opt_flush_merge:
825 set_opt(sbi, FLUSH_MERGE);
827 case Opt_noflush_merge:
828 clear_opt(sbi, FLUSH_MERGE);
831 set_opt(sbi, NOBARRIER);
834 clear_opt(sbi, NOBARRIER);
837 set_opt(sbi, FASTBOOT);
839 case Opt_extent_cache:
840 set_opt(sbi, READ_EXTENT_CACHE);
842 case Opt_noextent_cache:
843 clear_opt(sbi, READ_EXTENT_CACHE);
845 case Opt_noinline_data:
846 clear_opt(sbi, INLINE_DATA);
849 set_opt(sbi, DATA_FLUSH);
851 case Opt_reserve_root:
852 if (args->from && match_int(args, &arg))
854 if (test_opt(sbi, RESERVE_ROOT)) {
855 f2fs_info(sbi, "Preserve previous reserve_root=%u",
856 F2FS_OPTION(sbi).root_reserved_blocks);
858 F2FS_OPTION(sbi).root_reserved_blocks = arg;
859 set_opt(sbi, RESERVE_ROOT);
863 if (args->from && match_int(args, &arg))
865 uid = make_kuid(current_user_ns(), arg);
866 if (!uid_valid(uid)) {
867 f2fs_err(sbi, "Invalid uid value %d", arg);
870 F2FS_OPTION(sbi).s_resuid = uid;
873 if (args->from && match_int(args, &arg))
875 gid = make_kgid(current_user_ns(), arg);
876 if (!gid_valid(gid)) {
877 f2fs_err(sbi, "Invalid gid value %d", arg);
880 F2FS_OPTION(sbi).s_resgid = gid;
883 name = match_strdup(&args[0]);
887 if (!strcmp(name, "adaptive")) {
888 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
889 } else if (!strcmp(name, "lfs")) {
890 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
891 } else if (!strcmp(name, "fragment:segment")) {
892 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_SEG;
893 } else if (!strcmp(name, "fragment:block")) {
894 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_BLK;
901 case Opt_io_size_bits:
902 if (args->from && match_int(args, &arg))
904 if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_VECS)) {
905 f2fs_warn(sbi, "Not support %ld, larger than %d",
906 BIT(arg), BIO_MAX_VECS);
909 F2FS_OPTION(sbi).write_io_size_bits = arg;
911 #ifdef CONFIG_F2FS_FAULT_INJECTION
912 case Opt_fault_injection:
913 if (args->from && match_int(args, &arg))
915 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
916 set_opt(sbi, FAULT_INJECTION);
920 if (args->from && match_int(args, &arg))
922 f2fs_build_fault_attr(sbi, 0, arg);
923 set_opt(sbi, FAULT_INJECTION);
926 case Opt_fault_injection:
927 f2fs_info(sbi, "fault_injection options not supported");
931 f2fs_info(sbi, "fault_type options not supported");
935 sb->s_flags |= SB_LAZYTIME;
938 sb->s_flags &= ~SB_LAZYTIME;
943 set_opt(sbi, USRQUOTA);
946 set_opt(sbi, GRPQUOTA);
949 set_opt(sbi, PRJQUOTA);
952 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
957 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
962 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
966 case Opt_offusrjquota:
967 ret = f2fs_clear_qf_name(sb, USRQUOTA);
971 case Opt_offgrpjquota:
972 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
976 case Opt_offprjjquota:
977 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
981 case Opt_jqfmt_vfsold:
982 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
984 case Opt_jqfmt_vfsv0:
985 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
987 case Opt_jqfmt_vfsv1:
988 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
991 clear_opt(sbi, QUOTA);
992 clear_opt(sbi, USRQUOTA);
993 clear_opt(sbi, GRPQUOTA);
994 clear_opt(sbi, PRJQUOTA);
1004 case Opt_offusrjquota:
1005 case Opt_offgrpjquota:
1006 case Opt_offprjjquota:
1007 case Opt_jqfmt_vfsold:
1008 case Opt_jqfmt_vfsv0:
1009 case Opt_jqfmt_vfsv1:
1011 f2fs_info(sbi, "quota operations not supported");
1015 name = match_strdup(&args[0]);
1019 if (!strcmp(name, "default")) {
1020 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1021 } else if (!strcmp(name, "reuse")) {
1022 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
1030 name = match_strdup(&args[0]);
1033 if (!strcmp(name, "posix")) {
1034 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1035 } else if (!strcmp(name, "strict")) {
1036 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
1037 } else if (!strcmp(name, "nobarrier")) {
1038 F2FS_OPTION(sbi).fsync_mode =
1039 FSYNC_MODE_NOBARRIER;
1046 case Opt_test_dummy_encryption:
1047 ret = f2fs_set_test_dummy_encryption(sb, p, &args[0],
1052 case Opt_inlinecrypt:
1053 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1054 sb->s_flags |= SB_INLINECRYPT;
1056 f2fs_info(sbi, "inline encryption not supported");
1059 case Opt_checkpoint_disable_cap_perc:
1060 if (args->from && match_int(args, &arg))
1062 if (arg < 0 || arg > 100)
1064 F2FS_OPTION(sbi).unusable_cap_perc = arg;
1065 set_opt(sbi, DISABLE_CHECKPOINT);
1067 case Opt_checkpoint_disable_cap:
1068 if (args->from && match_int(args, &arg))
1070 F2FS_OPTION(sbi).unusable_cap = arg;
1071 set_opt(sbi, DISABLE_CHECKPOINT);
1073 case Opt_checkpoint_disable:
1074 set_opt(sbi, DISABLE_CHECKPOINT);
1076 case Opt_checkpoint_enable:
1077 clear_opt(sbi, DISABLE_CHECKPOINT);
1079 case Opt_checkpoint_merge:
1080 set_opt(sbi, MERGE_CHECKPOINT);
1082 case Opt_nocheckpoint_merge:
1083 clear_opt(sbi, MERGE_CHECKPOINT);
1085 #ifdef CONFIG_F2FS_FS_COMPRESSION
1086 case Opt_compress_algorithm:
1087 if (!f2fs_sb_has_compression(sbi)) {
1088 f2fs_info(sbi, "Image doesn't support compression");
1091 name = match_strdup(&args[0]);
1094 if (!strcmp(name, "lzo")) {
1095 #ifdef CONFIG_F2FS_FS_LZO
1096 F2FS_OPTION(sbi).compress_level = 0;
1097 F2FS_OPTION(sbi).compress_algorithm =
1100 f2fs_info(sbi, "kernel doesn't support lzo compression");
1102 } else if (!strncmp(name, "lz4", 3)) {
1103 #ifdef CONFIG_F2FS_FS_LZ4
1104 ret = f2fs_set_lz4hc_level(sbi, name);
1109 F2FS_OPTION(sbi).compress_algorithm =
1112 f2fs_info(sbi, "kernel doesn't support lz4 compression");
1114 } else if (!strncmp(name, "zstd", 4)) {
1115 #ifdef CONFIG_F2FS_FS_ZSTD
1116 ret = f2fs_set_zstd_level(sbi, name);
1121 F2FS_OPTION(sbi).compress_algorithm =
1124 f2fs_info(sbi, "kernel doesn't support zstd compression");
1126 } else if (!strcmp(name, "lzo-rle")) {
1127 #ifdef CONFIG_F2FS_FS_LZORLE
1128 F2FS_OPTION(sbi).compress_level = 0;
1129 F2FS_OPTION(sbi).compress_algorithm =
1132 f2fs_info(sbi, "kernel doesn't support lzorle compression");
1140 case Opt_compress_log_size:
1141 if (!f2fs_sb_has_compression(sbi)) {
1142 f2fs_info(sbi, "Image doesn't support compression");
1145 if (args->from && match_int(args, &arg))
1147 if (arg < MIN_COMPRESS_LOG_SIZE ||
1148 arg > MAX_COMPRESS_LOG_SIZE) {
1150 "Compress cluster log size is out of range");
1153 F2FS_OPTION(sbi).compress_log_size = arg;
1155 case Opt_compress_extension:
1156 if (!f2fs_sb_has_compression(sbi)) {
1157 f2fs_info(sbi, "Image doesn't support compression");
1160 name = match_strdup(&args[0]);
1164 ext = F2FS_OPTION(sbi).extensions;
1165 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1167 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1168 ext_cnt >= COMPRESS_EXT_NUM) {
1170 "invalid extension length/number");
1175 if (is_compress_extension_exist(sbi, name, true)) {
1180 strcpy(ext[ext_cnt], name);
1181 F2FS_OPTION(sbi).compress_ext_cnt++;
1184 case Opt_nocompress_extension:
1185 if (!f2fs_sb_has_compression(sbi)) {
1186 f2fs_info(sbi, "Image doesn't support compression");
1189 name = match_strdup(&args[0]);
1193 noext = F2FS_OPTION(sbi).noextensions;
1194 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
1196 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1197 noext_cnt >= COMPRESS_EXT_NUM) {
1199 "invalid extension length/number");
1204 if (is_compress_extension_exist(sbi, name, false)) {
1209 strcpy(noext[noext_cnt], name);
1210 F2FS_OPTION(sbi).nocompress_ext_cnt++;
1213 case Opt_compress_chksum:
1214 if (!f2fs_sb_has_compression(sbi)) {
1215 f2fs_info(sbi, "Image doesn't support compression");
1218 F2FS_OPTION(sbi).compress_chksum = true;
1220 case Opt_compress_mode:
1221 if (!f2fs_sb_has_compression(sbi)) {
1222 f2fs_info(sbi, "Image doesn't support compression");
1225 name = match_strdup(&args[0]);
1228 if (!strcmp(name, "fs")) {
1229 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1230 } else if (!strcmp(name, "user")) {
1231 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1238 case Opt_compress_cache:
1239 if (!f2fs_sb_has_compression(sbi)) {
1240 f2fs_info(sbi, "Image doesn't support compression");
1243 set_opt(sbi, COMPRESS_CACHE);
1246 case Opt_compress_algorithm:
1247 case Opt_compress_log_size:
1248 case Opt_compress_extension:
1249 case Opt_nocompress_extension:
1250 case Opt_compress_chksum:
1251 case Opt_compress_mode:
1252 case Opt_compress_cache:
1253 f2fs_info(sbi, "compression options not supported");
1260 set_opt(sbi, GC_MERGE);
1262 case Opt_nogc_merge:
1263 clear_opt(sbi, GC_MERGE);
1265 case Opt_discard_unit:
1266 name = match_strdup(&args[0]);
1269 if (!strcmp(name, "block")) {
1270 F2FS_OPTION(sbi).discard_unit =
1272 } else if (!strcmp(name, "segment")) {
1273 F2FS_OPTION(sbi).discard_unit =
1274 DISCARD_UNIT_SEGMENT;
1275 } else if (!strcmp(name, "section")) {
1276 F2FS_OPTION(sbi).discard_unit =
1277 DISCARD_UNIT_SECTION;
1284 case Opt_memory_mode:
1285 name = match_strdup(&args[0]);
1288 if (!strcmp(name, "normal")) {
1289 F2FS_OPTION(sbi).memory_mode =
1291 } else if (!strcmp(name, "low")) {
1292 F2FS_OPTION(sbi).memory_mode =
1300 case Opt_age_extent_cache:
1301 set_opt(sbi, AGE_EXTENT_CACHE);
1304 name = match_strdup(&args[0]);
1307 if (!strcmp(name, "remount-ro")) {
1308 F2FS_OPTION(sbi).errors =
1309 MOUNT_ERRORS_READONLY;
1310 } else if (!strcmp(name, "continue")) {
1311 F2FS_OPTION(sbi).errors =
1312 MOUNT_ERRORS_CONTINUE;
1313 } else if (!strcmp(name, "panic")) {
1314 F2FS_OPTION(sbi).errors =
1323 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1330 if (f2fs_check_quota_options(sbi))
1333 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
1334 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1337 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
1338 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1342 #if !IS_ENABLED(CONFIG_UNICODE)
1343 if (f2fs_sb_has_casefold(sbi)) {
1345 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1350 * The BLKZONED feature indicates that the drive was formatted with
1351 * zone alignment optimization. This is optional for host-aware
1352 * devices, but mandatory for host-managed zoned block devices.
1354 if (f2fs_sb_has_blkzoned(sbi)) {
1355 #ifdef CONFIG_BLK_DEV_ZONED
1356 if (F2FS_OPTION(sbi).discard_unit !=
1357 DISCARD_UNIT_SECTION) {
1358 f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default");
1359 F2FS_OPTION(sbi).discard_unit =
1360 DISCARD_UNIT_SECTION;
1363 if (F2FS_OPTION(sbi).fs_mode != FS_MODE_LFS) {
1364 f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature");
1368 f2fs_err(sbi, "Zoned block device support is not enabled");
1373 #ifdef CONFIG_F2FS_FS_COMPRESSION
1374 if (f2fs_test_compress_extension(sbi)) {
1375 f2fs_err(sbi, "invalid compress or nocompress extension");
1380 if (F2FS_IO_SIZE_BITS(sbi) && !f2fs_lfs_mode(sbi)) {
1381 f2fs_err(sbi, "Should set mode=lfs with %luKB-sized IO",
1382 F2FS_IO_SIZE_KB(sbi));
1386 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
1387 int min_size, max_size;
1389 if (!f2fs_sb_has_extra_attr(sbi) ||
1390 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
1391 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1394 if (!test_opt(sbi, INLINE_XATTR)) {
1395 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1399 min_size = MIN_INLINE_XATTR_SIZE;
1400 max_size = MAX_INLINE_XATTR_SIZE;
1402 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1403 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
1404 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1405 min_size, max_size);
1410 if (test_opt(sbi, DISABLE_CHECKPOINT) && f2fs_lfs_mode(sbi)) {
1411 f2fs_err(sbi, "LFS is not compatible with checkpoint=disable");
1415 if (test_opt(sbi, ATGC) && f2fs_lfs_mode(sbi)) {
1416 f2fs_err(sbi, "LFS is not compatible with ATGC");
1420 if (f2fs_is_readonly(sbi) && test_opt(sbi, FLUSH_MERGE)) {
1421 f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
1425 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1426 f2fs_err(sbi, "Allow to mount readonly mode only");
1432 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1434 struct f2fs_inode_info *fi;
1436 if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC))
1439 fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO);
1443 init_once((void *) fi);
1445 /* Initialize f2fs-specific inode info */
1446 atomic_set(&fi->dirty_pages, 0);
1447 atomic_set(&fi->i_compr_blocks, 0);
1448 init_f2fs_rwsem(&fi->i_sem);
1449 spin_lock_init(&fi->i_size_lock);
1450 INIT_LIST_HEAD(&fi->dirty_list);
1451 INIT_LIST_HEAD(&fi->gdirty_list);
1452 init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
1453 init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
1454 init_f2fs_rwsem(&fi->i_xattr_sem);
1456 /* Will be used by directory only */
1457 fi->i_dir_level = F2FS_SB(sb)->dir_level;
1459 return &fi->vfs_inode;
1462 static int f2fs_drop_inode(struct inode *inode)
1464 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1468 * during filesystem shutdown, if checkpoint is disabled,
1469 * drop useless meta/node dirty pages.
1471 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1472 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1473 inode->i_ino == F2FS_META_INO(sbi)) {
1474 trace_f2fs_drop_inode(inode, 1);
1480 * This is to avoid a deadlock condition like below.
1481 * writeback_single_inode(inode)
1482 * - f2fs_write_data_page
1483 * - f2fs_gc -> iput -> evict
1484 * - inode_wait_for_writeback(inode)
1486 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
1487 if (!inode->i_nlink && !is_bad_inode(inode)) {
1488 /* to avoid evict_inode call simultaneously */
1489 atomic_inc(&inode->i_count);
1490 spin_unlock(&inode->i_lock);
1492 /* should remain fi->extent_tree for writepage */
1493 f2fs_destroy_extent_node(inode);
1495 sb_start_intwrite(inode->i_sb);
1496 f2fs_i_size_write(inode, 0);
1498 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1499 inode, NULL, 0, DATA);
1500 truncate_inode_pages_final(inode->i_mapping);
1502 if (F2FS_HAS_BLOCKS(inode))
1503 f2fs_truncate(inode);
1505 sb_end_intwrite(inode->i_sb);
1507 spin_lock(&inode->i_lock);
1508 atomic_dec(&inode->i_count);
1510 trace_f2fs_drop_inode(inode, 0);
1513 ret = generic_drop_inode(inode);
1515 ret = fscrypt_drop_inode(inode);
1516 trace_f2fs_drop_inode(inode, ret);
1520 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1522 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1525 spin_lock(&sbi->inode_lock[DIRTY_META]);
1526 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1529 set_inode_flag(inode, FI_DIRTY_INODE);
1530 stat_inc_dirty_inode(sbi, DIRTY_META);
1532 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1533 list_add_tail(&F2FS_I(inode)->gdirty_list,
1534 &sbi->inode_list[DIRTY_META]);
1535 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1537 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1541 void f2fs_inode_synced(struct inode *inode)
1543 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1545 spin_lock(&sbi->inode_lock[DIRTY_META]);
1546 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1547 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1550 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1551 list_del_init(&F2FS_I(inode)->gdirty_list);
1552 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1554 clear_inode_flag(inode, FI_DIRTY_INODE);
1555 clear_inode_flag(inode, FI_AUTO_RECOVER);
1556 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1557 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1561 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1563 * We should call set_dirty_inode to write the dirty inode through write_inode.
1565 static void f2fs_dirty_inode(struct inode *inode, int flags)
1567 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1569 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1570 inode->i_ino == F2FS_META_INO(sbi))
1573 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1574 clear_inode_flag(inode, FI_AUTO_RECOVER);
1576 f2fs_inode_dirtied(inode, false);
1579 static void f2fs_free_inode(struct inode *inode)
1581 fscrypt_free_inode(inode);
1582 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1585 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1587 percpu_counter_destroy(&sbi->total_valid_inode_count);
1588 percpu_counter_destroy(&sbi->rf_node_block_count);
1589 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1592 static void destroy_device_list(struct f2fs_sb_info *sbi)
1596 for (i = 0; i < sbi->s_ndevs; i++) {
1598 blkdev_put(FDEV(i).bdev, sbi->sb);
1599 #ifdef CONFIG_BLK_DEV_ZONED
1600 kvfree(FDEV(i).blkz_seq);
1606 static void f2fs_put_super(struct super_block *sb)
1608 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1613 /* unregister procfs/sysfs entries in advance to avoid race case */
1614 f2fs_unregister_sysfs(sbi);
1616 f2fs_quota_off_umount(sb);
1618 /* prevent remaining shrinker jobs */
1619 mutex_lock(&sbi->umount_mutex);
1622 * flush all issued checkpoints and stop checkpoint issue thread.
1623 * after then, all checkpoints should be done by each process context.
1625 f2fs_stop_ckpt_thread(sbi);
1628 * We don't need to do checkpoint when superblock is clean.
1629 * But, the previous checkpoint was not done by umount, it needs to do
1630 * clean checkpoint again.
1632 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1633 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1634 struct cp_control cpc = {
1635 .reason = CP_UMOUNT,
1637 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1638 err = f2fs_write_checkpoint(sbi, &cpc);
1641 /* be sure to wait for any on-going discard commands */
1642 done = f2fs_issue_discard_timeout(sbi);
1643 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
1644 struct cp_control cpc = {
1645 .reason = CP_UMOUNT | CP_TRIMMED,
1647 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1648 err = f2fs_write_checkpoint(sbi, &cpc);
1652 * normally superblock is clean, so we need to release this.
1653 * In addition, EIO will skip do checkpoint, we need this as well.
1655 f2fs_release_ino_entry(sbi, true);
1657 f2fs_leave_shrinker(sbi);
1658 mutex_unlock(&sbi->umount_mutex);
1660 /* our cp_error case, we can wait for any writeback page */
1661 f2fs_flush_merged_writes(sbi);
1663 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1665 if (err || f2fs_cp_error(sbi)) {
1666 truncate_inode_pages_final(NODE_MAPPING(sbi));
1667 truncate_inode_pages_final(META_MAPPING(sbi));
1670 for (i = 0; i < NR_COUNT_TYPE; i++) {
1671 if (!get_pages(sbi, i))
1673 f2fs_err(sbi, "detect filesystem reference count leak during "
1674 "umount, type: %d, count: %lld", i, get_pages(sbi, i));
1675 f2fs_bug_on(sbi, 1);
1678 f2fs_bug_on(sbi, sbi->fsync_node_num);
1680 f2fs_destroy_compress_inode(sbi);
1682 iput(sbi->node_inode);
1683 sbi->node_inode = NULL;
1685 iput(sbi->meta_inode);
1686 sbi->meta_inode = NULL;
1689 * iput() can update stat information, if f2fs_write_checkpoint()
1690 * above failed with error.
1692 f2fs_destroy_stats(sbi);
1694 /* destroy f2fs internal modules */
1695 f2fs_destroy_node_manager(sbi);
1696 f2fs_destroy_segment_manager(sbi);
1698 /* flush s_error_work before sbi destroy */
1699 flush_work(&sbi->s_error_work);
1701 f2fs_destroy_post_read_wq(sbi);
1705 sb->s_fs_info = NULL;
1706 if (sbi->s_chksum_driver)
1707 crypto_free_shash(sbi->s_chksum_driver);
1708 kfree(sbi->raw_super);
1710 destroy_device_list(sbi);
1711 f2fs_destroy_page_array_cache(sbi);
1712 f2fs_destroy_xattr_caches(sbi);
1713 mempool_destroy(sbi->write_io_dummy);
1715 for (i = 0; i < MAXQUOTAS; i++)
1716 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1718 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1719 destroy_percpu_info(sbi);
1720 f2fs_destroy_iostat(sbi);
1721 for (i = 0; i < NR_PAGE_TYPE; i++)
1722 kvfree(sbi->write_io[i]);
1723 #if IS_ENABLED(CONFIG_UNICODE)
1724 utf8_unload(sb->s_encoding);
1729 int f2fs_sync_fs(struct super_block *sb, int sync)
1731 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1734 if (unlikely(f2fs_cp_error(sbi)))
1736 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1739 trace_f2fs_sync_fs(sb, sync);
1741 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1745 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1746 err = f2fs_issue_checkpoint(sbi);
1752 static int f2fs_freeze(struct super_block *sb)
1754 if (f2fs_readonly(sb))
1757 /* IO error happened before */
1758 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1761 /* must be clean, since sync_filesystem() was already called */
1762 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1765 /* Let's flush checkpoints and stop the thread. */
1766 f2fs_flush_ckpt_thread(F2FS_SB(sb));
1768 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
1769 set_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1773 static int f2fs_unfreeze(struct super_block *sb)
1775 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1780 static int f2fs_statfs_project(struct super_block *sb,
1781 kprojid_t projid, struct kstatfs *buf)
1784 struct dquot *dquot;
1788 qid = make_kqid_projid(projid);
1789 dquot = dqget(sb, qid);
1791 return PTR_ERR(dquot);
1792 spin_lock(&dquot->dq_dqb_lock);
1794 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1795 dquot->dq_dqb.dqb_bhardlimit);
1797 limit >>= sb->s_blocksize_bits;
1799 if (limit && buf->f_blocks > limit) {
1800 curblock = (dquot->dq_dqb.dqb_curspace +
1801 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1802 buf->f_blocks = limit;
1803 buf->f_bfree = buf->f_bavail =
1804 (buf->f_blocks > curblock) ?
1805 (buf->f_blocks - curblock) : 0;
1808 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1809 dquot->dq_dqb.dqb_ihardlimit);
1811 if (limit && buf->f_files > limit) {
1812 buf->f_files = limit;
1814 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1815 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1818 spin_unlock(&dquot->dq_dqb_lock);
1824 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1826 struct super_block *sb = dentry->d_sb;
1827 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1828 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1829 block_t total_count, user_block_count, start_count;
1830 u64 avail_node_count;
1831 unsigned int total_valid_node_count;
1833 total_count = le64_to_cpu(sbi->raw_super->block_count);
1834 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1835 buf->f_type = F2FS_SUPER_MAGIC;
1836 buf->f_bsize = sbi->blocksize;
1838 buf->f_blocks = total_count - start_count;
1840 spin_lock(&sbi->stat_lock);
1842 user_block_count = sbi->user_block_count;
1843 total_valid_node_count = valid_node_count(sbi);
1844 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1845 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1846 sbi->current_reserved_blocks;
1848 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1851 buf->f_bfree -= sbi->unusable_block_count;
1852 spin_unlock(&sbi->stat_lock);
1854 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1855 buf->f_bavail = buf->f_bfree -
1856 F2FS_OPTION(sbi).root_reserved_blocks;
1860 if (avail_node_count > user_block_count) {
1861 buf->f_files = user_block_count;
1862 buf->f_ffree = buf->f_bavail;
1864 buf->f_files = avail_node_count;
1865 buf->f_ffree = min(avail_node_count - total_valid_node_count,
1869 buf->f_namelen = F2FS_NAME_LEN;
1870 buf->f_fsid = u64_to_fsid(id);
1873 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1874 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1875 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1881 static inline void f2fs_show_quota_options(struct seq_file *seq,
1882 struct super_block *sb)
1885 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1887 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1890 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1901 seq_printf(seq, ",jqfmt=%s", fmtname);
1904 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1905 seq_show_option(seq, "usrjquota",
1906 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1908 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1909 seq_show_option(seq, "grpjquota",
1910 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1912 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1913 seq_show_option(seq, "prjjquota",
1914 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1918 #ifdef CONFIG_F2FS_FS_COMPRESSION
1919 static inline void f2fs_show_compress_options(struct seq_file *seq,
1920 struct super_block *sb)
1922 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1926 if (!f2fs_sb_has_compression(sbi))
1929 switch (F2FS_OPTION(sbi).compress_algorithm) {
1939 case COMPRESS_LZORLE:
1940 algtype = "lzo-rle";
1943 seq_printf(seq, ",compress_algorithm=%s", algtype);
1945 if (F2FS_OPTION(sbi).compress_level)
1946 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1948 seq_printf(seq, ",compress_log_size=%u",
1949 F2FS_OPTION(sbi).compress_log_size);
1951 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1952 seq_printf(seq, ",compress_extension=%s",
1953 F2FS_OPTION(sbi).extensions[i]);
1956 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
1957 seq_printf(seq, ",nocompress_extension=%s",
1958 F2FS_OPTION(sbi).noextensions[i]);
1961 if (F2FS_OPTION(sbi).compress_chksum)
1962 seq_puts(seq, ",compress_chksum");
1964 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1965 seq_printf(seq, ",compress_mode=%s", "fs");
1966 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1967 seq_printf(seq, ",compress_mode=%s", "user");
1969 if (test_opt(sbi, COMPRESS_CACHE))
1970 seq_puts(seq, ",compress_cache");
1974 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1976 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1978 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1979 seq_printf(seq, ",background_gc=%s", "sync");
1980 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1981 seq_printf(seq, ",background_gc=%s", "on");
1982 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
1983 seq_printf(seq, ",background_gc=%s", "off");
1985 if (test_opt(sbi, GC_MERGE))
1986 seq_puts(seq, ",gc_merge");
1988 seq_puts(seq, ",nogc_merge");
1990 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1991 seq_puts(seq, ",disable_roll_forward");
1992 if (test_opt(sbi, NORECOVERY))
1993 seq_puts(seq, ",norecovery");
1994 if (test_opt(sbi, DISCARD)) {
1995 seq_puts(seq, ",discard");
1996 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
1997 seq_printf(seq, ",discard_unit=%s", "block");
1998 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
1999 seq_printf(seq, ",discard_unit=%s", "segment");
2000 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2001 seq_printf(seq, ",discard_unit=%s", "section");
2003 seq_puts(seq, ",nodiscard");
2005 if (test_opt(sbi, NOHEAP))
2006 seq_puts(seq, ",no_heap");
2008 seq_puts(seq, ",heap");
2009 #ifdef CONFIG_F2FS_FS_XATTR
2010 if (test_opt(sbi, XATTR_USER))
2011 seq_puts(seq, ",user_xattr");
2013 seq_puts(seq, ",nouser_xattr");
2014 if (test_opt(sbi, INLINE_XATTR))
2015 seq_puts(seq, ",inline_xattr");
2017 seq_puts(seq, ",noinline_xattr");
2018 if (test_opt(sbi, INLINE_XATTR_SIZE))
2019 seq_printf(seq, ",inline_xattr_size=%u",
2020 F2FS_OPTION(sbi).inline_xattr_size);
2022 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2023 if (test_opt(sbi, POSIX_ACL))
2024 seq_puts(seq, ",acl");
2026 seq_puts(seq, ",noacl");
2028 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
2029 seq_puts(seq, ",disable_ext_identify");
2030 if (test_opt(sbi, INLINE_DATA))
2031 seq_puts(seq, ",inline_data");
2033 seq_puts(seq, ",noinline_data");
2034 if (test_opt(sbi, INLINE_DENTRY))
2035 seq_puts(seq, ",inline_dentry");
2037 seq_puts(seq, ",noinline_dentry");
2038 if (test_opt(sbi, FLUSH_MERGE))
2039 seq_puts(seq, ",flush_merge");
2041 seq_puts(seq, ",noflush_merge");
2042 if (test_opt(sbi, NOBARRIER))
2043 seq_puts(seq, ",nobarrier");
2045 seq_puts(seq, ",barrier");
2046 if (test_opt(sbi, FASTBOOT))
2047 seq_puts(seq, ",fastboot");
2048 if (test_opt(sbi, READ_EXTENT_CACHE))
2049 seq_puts(seq, ",extent_cache");
2051 seq_puts(seq, ",noextent_cache");
2052 if (test_opt(sbi, AGE_EXTENT_CACHE))
2053 seq_puts(seq, ",age_extent_cache");
2054 if (test_opt(sbi, DATA_FLUSH))
2055 seq_puts(seq, ",data_flush");
2057 seq_puts(seq, ",mode=");
2058 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
2059 seq_puts(seq, "adaptive");
2060 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
2061 seq_puts(seq, "lfs");
2062 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
2063 seq_puts(seq, "fragment:segment");
2064 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2065 seq_puts(seq, "fragment:block");
2066 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
2067 if (test_opt(sbi, RESERVE_ROOT))
2068 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
2069 F2FS_OPTION(sbi).root_reserved_blocks,
2070 from_kuid_munged(&init_user_ns,
2071 F2FS_OPTION(sbi).s_resuid),
2072 from_kgid_munged(&init_user_ns,
2073 F2FS_OPTION(sbi).s_resgid));
2074 if (F2FS_IO_SIZE_BITS(sbi))
2075 seq_printf(seq, ",io_bits=%u",
2076 F2FS_OPTION(sbi).write_io_size_bits);
2077 #ifdef CONFIG_F2FS_FAULT_INJECTION
2078 if (test_opt(sbi, FAULT_INJECTION)) {
2079 seq_printf(seq, ",fault_injection=%u",
2080 F2FS_OPTION(sbi).fault_info.inject_rate);
2081 seq_printf(seq, ",fault_type=%u",
2082 F2FS_OPTION(sbi).fault_info.inject_type);
2086 if (test_opt(sbi, QUOTA))
2087 seq_puts(seq, ",quota");
2088 if (test_opt(sbi, USRQUOTA))
2089 seq_puts(seq, ",usrquota");
2090 if (test_opt(sbi, GRPQUOTA))
2091 seq_puts(seq, ",grpquota");
2092 if (test_opt(sbi, PRJQUOTA))
2093 seq_puts(seq, ",prjquota");
2095 f2fs_show_quota_options(seq, sbi->sb);
2097 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
2099 if (sbi->sb->s_flags & SB_INLINECRYPT)
2100 seq_puts(seq, ",inlinecrypt");
2102 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
2103 seq_printf(seq, ",alloc_mode=%s", "default");
2104 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2105 seq_printf(seq, ",alloc_mode=%s", "reuse");
2107 if (test_opt(sbi, DISABLE_CHECKPOINT))
2108 seq_printf(seq, ",checkpoint=disable:%u",
2109 F2FS_OPTION(sbi).unusable_cap);
2110 if (test_opt(sbi, MERGE_CHECKPOINT))
2111 seq_puts(seq, ",checkpoint_merge");
2113 seq_puts(seq, ",nocheckpoint_merge");
2114 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
2115 seq_printf(seq, ",fsync_mode=%s", "posix");
2116 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
2117 seq_printf(seq, ",fsync_mode=%s", "strict");
2118 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
2119 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
2121 #ifdef CONFIG_F2FS_FS_COMPRESSION
2122 f2fs_show_compress_options(seq, sbi->sb);
2125 if (test_opt(sbi, ATGC))
2126 seq_puts(seq, ",atgc");
2128 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
2129 seq_printf(seq, ",memory=%s", "normal");
2130 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
2131 seq_printf(seq, ",memory=%s", "low");
2133 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2134 seq_printf(seq, ",errors=%s", "remount-ro");
2135 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
2136 seq_printf(seq, ",errors=%s", "continue");
2137 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
2138 seq_printf(seq, ",errors=%s", "panic");
2143 static void default_options(struct f2fs_sb_info *sbi, bool remount)
2145 /* init some FS parameters */
2147 set_opt(sbi, READ_EXTENT_CACHE);
2148 clear_opt(sbi, DISABLE_CHECKPOINT);
2150 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2151 set_opt(sbi, DISCARD);
2153 if (f2fs_sb_has_blkzoned(sbi))
2154 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2156 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2159 if (f2fs_sb_has_readonly(sbi))
2160 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
2162 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
2164 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
2165 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
2166 SMALL_VOLUME_SEGMENTS)
2167 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2169 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
2170 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
2171 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2172 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2173 if (f2fs_sb_has_compression(sbi)) {
2174 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
2175 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
2176 F2FS_OPTION(sbi).compress_ext_cnt = 0;
2177 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
2179 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
2180 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
2181 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
2183 sbi->sb->s_flags &= ~SB_INLINECRYPT;
2185 set_opt(sbi, INLINE_XATTR);
2186 set_opt(sbi, INLINE_DATA);
2187 set_opt(sbi, INLINE_DENTRY);
2188 set_opt(sbi, NOHEAP);
2189 set_opt(sbi, MERGE_CHECKPOINT);
2190 F2FS_OPTION(sbi).unusable_cap = 0;
2191 sbi->sb->s_flags |= SB_LAZYTIME;
2192 if (!f2fs_is_readonly(sbi))
2193 set_opt(sbi, FLUSH_MERGE);
2194 if (f2fs_sb_has_blkzoned(sbi))
2195 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
2197 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
2199 #ifdef CONFIG_F2FS_FS_XATTR
2200 set_opt(sbi, XATTR_USER);
2202 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2203 set_opt(sbi, POSIX_ACL);
2206 f2fs_build_fault_attr(sbi, 0, 0);
2210 static int f2fs_enable_quotas(struct super_block *sb);
2213 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
2215 unsigned int s_flags = sbi->sb->s_flags;
2216 struct cp_control cpc;
2217 unsigned int gc_mode = sbi->gc_mode;
2222 if (s_flags & SB_RDONLY) {
2223 f2fs_err(sbi, "checkpoint=disable on readonly fs");
2226 sbi->sb->s_flags |= SB_ACTIVE;
2228 /* check if we need more GC first */
2229 unusable = f2fs_get_unusable_blocks(sbi);
2230 if (!f2fs_disable_cp_again(sbi, unusable))
2233 f2fs_update_time(sbi, DISABLE_TIME);
2235 sbi->gc_mode = GC_URGENT_HIGH;
2237 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
2238 struct f2fs_gc_control gc_control = {
2239 .victim_segno = NULL_SEGNO,
2240 .init_gc_type = FG_GC,
2241 .should_migrate_blocks = false,
2242 .err_gc_skipped = true,
2243 .nr_free_secs = 1 };
2245 f2fs_down_write(&sbi->gc_lock);
2246 stat_inc_gc_call_count(sbi, FOREGROUND);
2247 err = f2fs_gc(sbi, &gc_control);
2248 if (err == -ENODATA) {
2252 if (err && err != -EAGAIN)
2256 ret = sync_filesystem(sbi->sb);
2258 err = ret ? ret : err;
2262 unusable = f2fs_get_unusable_blocks(sbi);
2263 if (f2fs_disable_cp_again(sbi, unusable)) {
2269 f2fs_down_write(&sbi->gc_lock);
2270 cpc.reason = CP_PAUSE;
2271 set_sbi_flag(sbi, SBI_CP_DISABLED);
2272 stat_inc_cp_call_count(sbi, TOTAL_CALL);
2273 err = f2fs_write_checkpoint(sbi, &cpc);
2277 spin_lock(&sbi->stat_lock);
2278 sbi->unusable_block_count = unusable;
2279 spin_unlock(&sbi->stat_lock);
2282 f2fs_up_write(&sbi->gc_lock);
2284 sbi->gc_mode = gc_mode;
2285 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2289 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
2291 int retry = DEFAULT_RETRY_IO_COUNT;
2293 /* we should flush all the data to keep data consistency */
2295 sync_inodes_sb(sbi->sb);
2296 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2297 } while (get_pages(sbi, F2FS_DIRTY_DATA) && retry--);
2299 if (unlikely(retry < 0))
2300 f2fs_warn(sbi, "checkpoint=enable has some unwritten data.");
2302 f2fs_down_write(&sbi->gc_lock);
2303 f2fs_dirty_to_prefree(sbi);
2305 clear_sbi_flag(sbi, SBI_CP_DISABLED);
2306 set_sbi_flag(sbi, SBI_IS_DIRTY);
2307 f2fs_up_write(&sbi->gc_lock);
2309 f2fs_sync_fs(sbi->sb, 1);
2311 /* Let's ensure there's no pending checkpoint anymore */
2312 f2fs_flush_ckpt_thread(sbi);
2315 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
2317 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2318 struct f2fs_mount_info org_mount_opt;
2319 unsigned long old_sb_flags;
2321 bool need_restart_gc = false, need_stop_gc = false;
2322 bool need_restart_ckpt = false, need_stop_ckpt = false;
2323 bool need_restart_flush = false, need_stop_flush = false;
2324 bool need_restart_discard = false, need_stop_discard = false;
2325 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
2326 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
2327 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
2328 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
2329 bool no_atgc = !test_opt(sbi, ATGC);
2330 bool no_discard = !test_opt(sbi, DISCARD);
2331 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
2332 bool block_unit_discard = f2fs_block_unit_discard(sbi);
2338 * Save the old mount options in case we
2339 * need to restore them.
2341 org_mount_opt = sbi->mount_opt;
2342 old_sb_flags = sb->s_flags;
2345 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
2346 for (i = 0; i < MAXQUOTAS; i++) {
2347 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2348 org_mount_opt.s_qf_names[i] =
2349 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
2351 if (!org_mount_opt.s_qf_names[i]) {
2352 for (j = 0; j < i; j++)
2353 kfree(org_mount_opt.s_qf_names[j]);
2357 org_mount_opt.s_qf_names[i] = NULL;
2362 /* recover superblocks we couldn't write due to previous RO mount */
2363 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
2364 err = f2fs_commit_super(sbi, false);
2365 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2368 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2371 default_options(sbi, true);
2373 /* parse mount options */
2374 err = parse_options(sb, data, true);
2378 /* flush outstanding errors before changing fs state */
2379 flush_work(&sbi->s_error_work);
2382 * Previous and new state of filesystem is RO,
2383 * so skip checking GC and FLUSH_MERGE conditions.
2385 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
2388 if (f2fs_dev_is_readonly(sbi) && !(*flags & SB_RDONLY)) {
2394 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
2395 err = dquot_suspend(sb, -1);
2398 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
2399 /* dquot_resume needs RW */
2400 sb->s_flags &= ~SB_RDONLY;
2401 if (sb_any_quota_suspended(sb)) {
2402 dquot_resume(sb, -1);
2403 } else if (f2fs_sb_has_quota_ino(sbi)) {
2404 err = f2fs_enable_quotas(sb);
2410 if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
2412 f2fs_warn(sbi, "LFS is not compatible with IPU");
2416 /* disallow enable atgc dynamically */
2417 if (no_atgc == !!test_opt(sbi, ATGC)) {
2419 f2fs_warn(sbi, "switch atgc option is not allowed");
2423 /* disallow enable/disable extent_cache dynamically */
2424 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
2426 f2fs_warn(sbi, "switch extent_cache option is not allowed");
2429 /* disallow enable/disable age extent_cache dynamically */
2430 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2432 f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
2436 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
2438 f2fs_warn(sbi, "switch io_bits option is not allowed");
2442 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2444 f2fs_warn(sbi, "switch compress_cache option is not allowed");
2448 if (block_unit_discard != f2fs_block_unit_discard(sbi)) {
2450 f2fs_warn(sbi, "switch discard_unit option is not allowed");
2454 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2456 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
2461 * We stop the GC thread if FS is mounted as RO
2462 * or if background_gc = off is passed in mount
2463 * option. Also sync the filesystem.
2465 if ((*flags & SB_RDONLY) ||
2466 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2467 !test_opt(sbi, GC_MERGE))) {
2468 if (sbi->gc_thread) {
2469 f2fs_stop_gc_thread(sbi);
2470 need_restart_gc = true;
2472 } else if (!sbi->gc_thread) {
2473 err = f2fs_start_gc_thread(sbi);
2476 need_stop_gc = true;
2479 if (*flags & SB_RDONLY) {
2482 set_sbi_flag(sbi, SBI_IS_DIRTY);
2483 set_sbi_flag(sbi, SBI_IS_CLOSE);
2484 f2fs_sync_fs(sb, 1);
2485 clear_sbi_flag(sbi, SBI_IS_CLOSE);
2488 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2489 !test_opt(sbi, MERGE_CHECKPOINT)) {
2490 f2fs_stop_ckpt_thread(sbi);
2491 need_restart_ckpt = true;
2493 /* Flush if the prevous checkpoint, if exists. */
2494 f2fs_flush_ckpt_thread(sbi);
2496 err = f2fs_start_ckpt_thread(sbi);
2499 "Failed to start F2FS issue_checkpoint_thread (%d)",
2503 need_stop_ckpt = true;
2507 * We stop issue flush thread if FS is mounted as RO
2508 * or if flush_merge is not passed in mount option.
2510 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2511 clear_opt(sbi, FLUSH_MERGE);
2512 f2fs_destroy_flush_cmd_control(sbi, false);
2513 need_restart_flush = true;
2515 err = f2fs_create_flush_cmd_control(sbi);
2518 need_stop_flush = true;
2521 if (no_discard == !!test_opt(sbi, DISCARD)) {
2522 if (test_opt(sbi, DISCARD)) {
2523 err = f2fs_start_discard_thread(sbi);
2526 need_stop_discard = true;
2528 f2fs_stop_discard_thread(sbi);
2529 f2fs_issue_discard_timeout(sbi);
2530 need_restart_discard = true;
2534 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
2535 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2536 err = f2fs_disable_checkpoint(sbi);
2538 goto restore_discard;
2540 f2fs_enable_checkpoint(sbi);
2546 /* Release old quota file names */
2547 for (i = 0; i < MAXQUOTAS; i++)
2548 kfree(org_mount_opt.s_qf_names[i]);
2550 /* Update the POSIXACL Flag */
2551 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2552 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2554 limit_reserve_root(sbi);
2555 adjust_unusable_cap_perc(sbi);
2556 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
2559 if (need_restart_discard) {
2560 if (f2fs_start_discard_thread(sbi))
2561 f2fs_warn(sbi, "discard has been stopped");
2562 } else if (need_stop_discard) {
2563 f2fs_stop_discard_thread(sbi);
2566 if (need_restart_flush) {
2567 if (f2fs_create_flush_cmd_control(sbi))
2568 f2fs_warn(sbi, "background flush thread has stopped");
2569 } else if (need_stop_flush) {
2570 clear_opt(sbi, FLUSH_MERGE);
2571 f2fs_destroy_flush_cmd_control(sbi, false);
2574 if (need_restart_ckpt) {
2575 if (f2fs_start_ckpt_thread(sbi))
2576 f2fs_warn(sbi, "background ckpt thread has stopped");
2577 } else if (need_stop_ckpt) {
2578 f2fs_stop_ckpt_thread(sbi);
2581 if (need_restart_gc) {
2582 if (f2fs_start_gc_thread(sbi))
2583 f2fs_warn(sbi, "background gc thread has stopped");
2584 } else if (need_stop_gc) {
2585 f2fs_stop_gc_thread(sbi);
2589 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
2590 for (i = 0; i < MAXQUOTAS; i++) {
2591 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2592 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
2595 sbi->mount_opt = org_mount_opt;
2596 sb->s_flags = old_sb_flags;
2601 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
2603 /* need to recovery orphan */
2604 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
2606 /* need to recovery data */
2607 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2609 if (test_opt(sbi, NORECOVERY))
2611 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
2614 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
2616 bool readonly = f2fs_readonly(sbi->sb);
2618 if (!f2fs_need_recovery(sbi))
2621 /* it doesn't need to check f2fs_sb_has_readonly() */
2622 if (f2fs_hw_is_readonly(sbi))
2626 sbi->sb->s_flags &= ~SB_RDONLY;
2627 set_sbi_flag(sbi, SBI_IS_WRITABLE);
2631 * Turn on quotas which were not enabled for read-only mounts if
2632 * filesystem has quota feature, so that they are updated correctly.
2634 return f2fs_enable_quota_files(sbi, readonly);
2637 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
2641 f2fs_quota_off_umount(sbi->sb);
2643 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
2644 clear_sbi_flag(sbi, SBI_IS_WRITABLE);
2645 sbi->sb->s_flags |= SB_RDONLY;
2649 /* Read data from quotafile */
2650 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2651 size_t len, loff_t off)
2653 struct inode *inode = sb_dqopt(sb)->files[type];
2654 struct address_space *mapping = inode->i_mapping;
2655 block_t blkidx = F2FS_BYTES_TO_BLK(off);
2656 int offset = off & (sb->s_blocksize - 1);
2659 loff_t i_size = i_size_read(inode);
2665 if (off + len > i_size)
2668 while (toread > 0) {
2669 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2671 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
2673 if (PTR_ERR(page) == -ENOMEM) {
2674 memalloc_retry_wait(GFP_NOFS);
2677 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2678 return PTR_ERR(page);
2683 if (unlikely(page->mapping != mapping)) {
2684 f2fs_put_page(page, 1);
2687 if (unlikely(!PageUptodate(page))) {
2688 f2fs_put_page(page, 1);
2689 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2693 memcpy_from_page(data, page, offset, tocopy);
2694 f2fs_put_page(page, 1);
2704 /* Write to quotafile */
2705 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2706 const char *data, size_t len, loff_t off)
2708 struct inode *inode = sb_dqopt(sb)->files[type];
2709 struct address_space *mapping = inode->i_mapping;
2710 const struct address_space_operations *a_ops = mapping->a_ops;
2711 int offset = off & (sb->s_blocksize - 1);
2712 size_t towrite = len;
2714 void *fsdata = NULL;
2718 while (towrite > 0) {
2719 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2722 err = a_ops->write_begin(NULL, mapping, off, tocopy,
2724 if (unlikely(err)) {
2725 if (err == -ENOMEM) {
2726 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2729 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2733 memcpy_to_page(page, offset, data, tocopy);
2735 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2746 inode->i_mtime = inode_set_ctime_current(inode);
2747 f2fs_mark_inode_dirty_sync(inode, false);
2748 return len - towrite;
2751 int f2fs_dquot_initialize(struct inode *inode)
2753 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
2756 return dquot_initialize(inode);
2759 static struct dquot **f2fs_get_dquots(struct inode *inode)
2761 return F2FS_I(inode)->i_dquot;
2764 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2766 return &F2FS_I(inode)->i_reserved_quota;
2769 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2771 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2772 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2776 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2777 F2FS_OPTION(sbi).s_jquota_fmt, type);
2780 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2785 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2786 err = f2fs_enable_quotas(sbi->sb);
2788 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2794 for (i = 0; i < MAXQUOTAS; i++) {
2795 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2796 err = f2fs_quota_on_mount(sbi, i);
2801 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2808 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2811 struct inode *qf_inode;
2812 unsigned long qf_inum;
2813 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
2816 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2818 qf_inum = f2fs_qf_ino(sb, type);
2822 qf_inode = f2fs_iget(sb, qf_inum);
2823 if (IS_ERR(qf_inode)) {
2824 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2825 return PTR_ERR(qf_inode);
2828 /* Don't account quota for quota files to avoid recursion */
2829 inode_lock(qf_inode);
2830 qf_inode->i_flags |= S_NOQUOTA;
2832 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
2833 F2FS_I(qf_inode)->i_flags |= qf_flag;
2834 f2fs_set_inode_flags(qf_inode);
2836 inode_unlock(qf_inode);
2838 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2843 static int f2fs_enable_quotas(struct super_block *sb)
2845 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2847 unsigned long qf_inum;
2848 bool quota_mopt[MAXQUOTAS] = {
2849 test_opt(sbi, USRQUOTA),
2850 test_opt(sbi, GRPQUOTA),
2851 test_opt(sbi, PRJQUOTA),
2854 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2855 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2859 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2861 for (type = 0; type < MAXQUOTAS; type++) {
2862 qf_inum = f2fs_qf_ino(sb, type);
2864 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2865 DQUOT_USAGE_ENABLED |
2866 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2868 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2870 for (type--; type >= 0; type--)
2871 dquot_quota_off(sb, type);
2872 set_sbi_flag(F2FS_SB(sb),
2873 SBI_QUOTA_NEED_REPAIR);
2881 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2883 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2884 struct address_space *mapping = dqopt->files[type]->i_mapping;
2887 ret = dquot_writeback_dquots(sbi->sb, type);
2891 ret = filemap_fdatawrite(mapping);
2895 /* if we are using journalled quota */
2896 if (is_journalled_quota(sbi))
2899 ret = filemap_fdatawait(mapping);
2901 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2904 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2908 int f2fs_quota_sync(struct super_block *sb, int type)
2910 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2911 struct quota_info *dqopt = sb_dqopt(sb);
2916 * Now when everything is written we can discard the pagecache so
2917 * that userspace sees the changes.
2919 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2921 if (type != -1 && cnt != type)
2924 if (!sb_has_quota_active(sb, cnt))
2927 if (!f2fs_sb_has_quota_ino(sbi))
2928 inode_lock(dqopt->files[cnt]);
2933 * f2fs_down_read(quota_sem)
2934 * dquot_writeback_dquots()
2937 * f2fs_down_read(quota_sem)
2940 f2fs_down_read(&sbi->quota_sem);
2942 ret = f2fs_quota_sync_file(sbi, cnt);
2944 f2fs_up_read(&sbi->quota_sem);
2945 f2fs_unlock_op(sbi);
2947 if (!f2fs_sb_has_quota_ino(sbi))
2948 inode_unlock(dqopt->files[cnt]);
2956 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2957 const struct path *path)
2959 struct inode *inode;
2962 /* if quota sysfile exists, deny enabling quota with specific file */
2963 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2964 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2968 if (path->dentry->d_sb != sb)
2971 err = f2fs_quota_sync(sb, type);
2975 inode = d_inode(path->dentry);
2977 err = filemap_fdatawrite(inode->i_mapping);
2981 err = filemap_fdatawait(inode->i_mapping);
2985 err = dquot_quota_on(sb, type, format_id, path);
2990 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
2991 f2fs_set_inode_flags(inode);
2992 inode_unlock(inode);
2993 f2fs_mark_inode_dirty_sync(inode, false);
2998 static int __f2fs_quota_off(struct super_block *sb, int type)
3000 struct inode *inode = sb_dqopt(sb)->files[type];
3003 if (!inode || !igrab(inode))
3004 return dquot_quota_off(sb, type);
3006 err = f2fs_quota_sync(sb, type);
3010 err = dquot_quota_off(sb, type);
3011 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
3015 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
3016 f2fs_set_inode_flags(inode);
3017 inode_unlock(inode);
3018 f2fs_mark_inode_dirty_sync(inode, false);
3024 static int f2fs_quota_off(struct super_block *sb, int type)
3026 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3029 err = __f2fs_quota_off(sb, type);
3032 * quotactl can shutdown journalled quota, result in inconsistence
3033 * between quota record and fs data by following updates, tag the
3034 * flag to let fsck be aware of it.
3036 if (is_journalled_quota(sbi))
3037 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3041 void f2fs_quota_off_umount(struct super_block *sb)
3046 for (type = 0; type < MAXQUOTAS; type++) {
3047 err = __f2fs_quota_off(sb, type);
3049 int ret = dquot_quota_off(sb, type);
3051 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
3053 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3057 * In case of checkpoint=disable, we must flush quota blocks.
3058 * This can cause NULL exception for node_inode in end_io, since
3059 * put_super already dropped it.
3061 sync_filesystem(sb);
3064 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
3066 struct quota_info *dqopt = sb_dqopt(sb);
3069 for (type = 0; type < MAXQUOTAS; type++) {
3070 if (!dqopt->files[type])
3072 f2fs_inode_synced(dqopt->files[type]);
3076 static int f2fs_dquot_commit(struct dquot *dquot)
3078 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3081 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
3082 ret = dquot_commit(dquot);
3084 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3085 f2fs_up_read(&sbi->quota_sem);
3089 static int f2fs_dquot_acquire(struct dquot *dquot)
3091 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3094 f2fs_down_read(&sbi->quota_sem);
3095 ret = dquot_acquire(dquot);
3097 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3098 f2fs_up_read(&sbi->quota_sem);
3102 static int f2fs_dquot_release(struct dquot *dquot)
3104 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3105 int ret = dquot_release(dquot);
3108 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3112 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
3114 struct super_block *sb = dquot->dq_sb;
3115 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3116 int ret = dquot_mark_dquot_dirty(dquot);
3118 /* if we are using journalled quota */
3119 if (is_journalled_quota(sbi))
3120 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
3125 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
3127 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3128 int ret = dquot_commit_info(sb, type);
3131 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3135 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
3137 *projid = F2FS_I(inode)->i_projid;
3141 static const struct dquot_operations f2fs_quota_operations = {
3142 .get_reserved_space = f2fs_get_reserved_space,
3143 .write_dquot = f2fs_dquot_commit,
3144 .acquire_dquot = f2fs_dquot_acquire,
3145 .release_dquot = f2fs_dquot_release,
3146 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
3147 .write_info = f2fs_dquot_commit_info,
3148 .alloc_dquot = dquot_alloc,
3149 .destroy_dquot = dquot_destroy,
3150 .get_projid = f2fs_get_projid,
3151 .get_next_id = dquot_get_next_id,
3154 static const struct quotactl_ops f2fs_quotactl_ops = {
3155 .quota_on = f2fs_quota_on,
3156 .quota_off = f2fs_quota_off,
3157 .quota_sync = f2fs_quota_sync,
3158 .get_state = dquot_get_state,
3159 .set_info = dquot_set_dqinfo,
3160 .get_dqblk = dquot_get_dqblk,
3161 .set_dqblk = dquot_set_dqblk,
3162 .get_nextdqblk = dquot_get_next_dqblk,
3165 int f2fs_dquot_initialize(struct inode *inode)
3170 int f2fs_quota_sync(struct super_block *sb, int type)
3175 void f2fs_quota_off_umount(struct super_block *sb)
3180 static const struct super_operations f2fs_sops = {
3181 .alloc_inode = f2fs_alloc_inode,
3182 .free_inode = f2fs_free_inode,
3183 .drop_inode = f2fs_drop_inode,
3184 .write_inode = f2fs_write_inode,
3185 .dirty_inode = f2fs_dirty_inode,
3186 .show_options = f2fs_show_options,
3188 .quota_read = f2fs_quota_read,
3189 .quota_write = f2fs_quota_write,
3190 .get_dquots = f2fs_get_dquots,
3192 .evict_inode = f2fs_evict_inode,
3193 .put_super = f2fs_put_super,
3194 .sync_fs = f2fs_sync_fs,
3195 .freeze_fs = f2fs_freeze,
3196 .unfreeze_fs = f2fs_unfreeze,
3197 .statfs = f2fs_statfs,
3198 .remount_fs = f2fs_remount,
3201 #ifdef CONFIG_FS_ENCRYPTION
3202 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
3204 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3205 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3209 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
3212 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3215 * Encrypting the root directory is not allowed because fsck
3216 * expects lost+found directory to exist and remain unencrypted
3217 * if LOST_FOUND feature is enabled.
3220 if (f2fs_sb_has_lost_found(sbi) &&
3221 inode->i_ino == F2FS_ROOT_INO(sbi))
3224 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3225 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3226 ctx, len, fs_data, XATTR_CREATE);
3229 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
3231 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
3234 static bool f2fs_has_stable_inodes(struct super_block *sb)
3239 static void f2fs_get_ino_and_lblk_bits(struct super_block *sb,
3240 int *ino_bits_ret, int *lblk_bits_ret)
3242 *ino_bits_ret = 8 * sizeof(nid_t);
3243 *lblk_bits_ret = 8 * sizeof(block_t);
3246 static struct block_device **f2fs_get_devices(struct super_block *sb,
3247 unsigned int *num_devs)
3249 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3250 struct block_device **devs;
3253 if (!f2fs_is_multi_device(sbi))
3256 devs = kmalloc_array(sbi->s_ndevs, sizeof(*devs), GFP_KERNEL);
3258 return ERR_PTR(-ENOMEM);
3260 for (i = 0; i < sbi->s_ndevs; i++)
3261 devs[i] = FDEV(i).bdev;
3262 *num_devs = sbi->s_ndevs;
3266 static const struct fscrypt_operations f2fs_cryptops = {
3267 .key_prefix = "f2fs:",
3268 .get_context = f2fs_get_context,
3269 .set_context = f2fs_set_context,
3270 .get_dummy_policy = f2fs_get_dummy_policy,
3271 .empty_dir = f2fs_empty_dir,
3272 .has_stable_inodes = f2fs_has_stable_inodes,
3273 .get_ino_and_lblk_bits = f2fs_get_ino_and_lblk_bits,
3274 .get_devices = f2fs_get_devices,
3278 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
3279 u64 ino, u32 generation)
3281 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3282 struct inode *inode;
3284 if (f2fs_check_nid_range(sbi, ino))
3285 return ERR_PTR(-ESTALE);
3288 * f2fs_iget isn't quite right if the inode is currently unallocated!
3289 * However f2fs_iget currently does appropriate checks to handle stale
3290 * inodes so everything is OK.
3292 inode = f2fs_iget(sb, ino);
3294 return ERR_CAST(inode);
3295 if (unlikely(generation && inode->i_generation != generation)) {
3296 /* we didn't find the right inode.. */
3298 return ERR_PTR(-ESTALE);
3303 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
3304 int fh_len, int fh_type)
3306 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3307 f2fs_nfs_get_inode);
3310 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
3311 int fh_len, int fh_type)
3313 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3314 f2fs_nfs_get_inode);
3317 static const struct export_operations f2fs_export_ops = {
3318 .fh_to_dentry = f2fs_fh_to_dentry,
3319 .fh_to_parent = f2fs_fh_to_parent,
3320 .get_parent = f2fs_get_parent,
3323 loff_t max_file_blocks(struct inode *inode)
3329 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
3330 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
3331 * space in inode.i_addr, it will be more safe to reassign
3335 if (inode && f2fs_compressed_file(inode))
3336 leaf_count = ADDRS_PER_BLOCK(inode);
3338 leaf_count = DEF_ADDRS_PER_BLOCK;
3340 /* two direct node blocks */
3341 result += (leaf_count * 2);
3343 /* two indirect node blocks */
3344 leaf_count *= NIDS_PER_BLOCK;
3345 result += (leaf_count * 2);
3347 /* one double indirect node block */
3348 leaf_count *= NIDS_PER_BLOCK;
3349 result += leaf_count;
3354 static int __f2fs_commit_super(struct buffer_head *bh,
3355 struct f2fs_super_block *super)
3359 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
3360 set_buffer_dirty(bh);
3363 /* it's rare case, we can do fua all the time */
3364 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
3367 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
3368 struct buffer_head *bh)
3370 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3371 (bh->b_data + F2FS_SUPER_OFFSET);
3372 struct super_block *sb = sbi->sb;
3373 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3374 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
3375 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
3376 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
3377 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
3378 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3379 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
3380 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
3381 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
3382 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
3383 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3384 u32 segment_count = le32_to_cpu(raw_super->segment_count);
3385 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3386 u64 main_end_blkaddr = main_blkaddr +
3387 (segment_count_main << log_blocks_per_seg);
3388 u64 seg_end_blkaddr = segment0_blkaddr +
3389 (segment_count << log_blocks_per_seg);
3391 if (segment0_blkaddr != cp_blkaddr) {
3392 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
3393 segment0_blkaddr, cp_blkaddr);
3397 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
3399 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
3400 cp_blkaddr, sit_blkaddr,
3401 segment_count_ckpt << log_blocks_per_seg);
3405 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
3407 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
3408 sit_blkaddr, nat_blkaddr,
3409 segment_count_sit << log_blocks_per_seg);
3413 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
3415 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
3416 nat_blkaddr, ssa_blkaddr,
3417 segment_count_nat << log_blocks_per_seg);
3421 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
3423 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
3424 ssa_blkaddr, main_blkaddr,
3425 segment_count_ssa << log_blocks_per_seg);
3429 if (main_end_blkaddr > seg_end_blkaddr) {
3430 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3431 main_blkaddr, seg_end_blkaddr,
3432 segment_count_main << log_blocks_per_seg);
3434 } else if (main_end_blkaddr < seg_end_blkaddr) {
3438 /* fix in-memory information all the time */
3439 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
3440 segment0_blkaddr) >> log_blocks_per_seg);
3442 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
3443 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3446 err = __f2fs_commit_super(bh, NULL);
3447 res = err ? "failed" : "done";
3449 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3450 res, main_blkaddr, seg_end_blkaddr,
3451 segment_count_main << log_blocks_per_seg);
3458 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
3459 struct buffer_head *bh)
3461 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
3462 block_t total_sections, blocks_per_seg;
3463 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3464 (bh->b_data + F2FS_SUPER_OFFSET);
3465 size_t crc_offset = 0;
3468 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
3469 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
3470 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
3474 /* Check checksum_offset and crc in superblock */
3475 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
3476 crc_offset = le32_to_cpu(raw_super->checksum_offset);
3478 offsetof(struct f2fs_super_block, crc)) {
3479 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
3481 return -EFSCORRUPTED;
3483 crc = le32_to_cpu(raw_super->crc);
3484 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
3485 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
3486 return -EFSCORRUPTED;
3490 /* Currently, support only 4KB block size */
3491 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3492 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3493 le32_to_cpu(raw_super->log_blocksize),
3495 return -EFSCORRUPTED;
3498 /* check log blocks per segment */
3499 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
3500 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
3501 le32_to_cpu(raw_super->log_blocks_per_seg));
3502 return -EFSCORRUPTED;
3505 /* Currently, support 512/1024/2048/4096 bytes sector size */
3506 if (le32_to_cpu(raw_super->log_sectorsize) >
3507 F2FS_MAX_LOG_SECTOR_SIZE ||
3508 le32_to_cpu(raw_super->log_sectorsize) <
3509 F2FS_MIN_LOG_SECTOR_SIZE) {
3510 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3511 le32_to_cpu(raw_super->log_sectorsize));
3512 return -EFSCORRUPTED;
3514 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3515 le32_to_cpu(raw_super->log_sectorsize) !=
3516 F2FS_MAX_LOG_SECTOR_SIZE) {
3517 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3518 le32_to_cpu(raw_super->log_sectors_per_block),
3519 le32_to_cpu(raw_super->log_sectorsize));
3520 return -EFSCORRUPTED;
3523 segment_count = le32_to_cpu(raw_super->segment_count);
3524 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3525 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3526 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3527 total_sections = le32_to_cpu(raw_super->section_count);
3529 /* blocks_per_seg should be 512, given the above check */
3530 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
3532 if (segment_count > F2FS_MAX_SEGMENT ||
3533 segment_count < F2FS_MIN_SEGMENTS) {
3534 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
3535 return -EFSCORRUPTED;
3538 if (total_sections > segment_count_main || total_sections < 1 ||
3539 segs_per_sec > segment_count || !segs_per_sec) {
3540 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3541 segment_count, total_sections, segs_per_sec);
3542 return -EFSCORRUPTED;
3545 if (segment_count_main != total_sections * segs_per_sec) {
3546 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3547 segment_count_main, total_sections, segs_per_sec);
3548 return -EFSCORRUPTED;
3551 if ((segment_count / segs_per_sec) < total_sections) {
3552 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3553 segment_count, segs_per_sec, total_sections);
3554 return -EFSCORRUPTED;
3557 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
3558 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3559 segment_count, le64_to_cpu(raw_super->block_count));
3560 return -EFSCORRUPTED;
3563 if (RDEV(0).path[0]) {
3564 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3567 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3568 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3571 if (segment_count != dev_seg_count) {
3572 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3573 segment_count, dev_seg_count);
3574 return -EFSCORRUPTED;
3577 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3578 !bdev_is_zoned(sbi->sb->s_bdev)) {
3579 f2fs_info(sbi, "Zoned block device path is missing");
3580 return -EFSCORRUPTED;
3584 if (secs_per_zone > total_sections || !secs_per_zone) {
3585 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3586 secs_per_zone, total_sections);
3587 return -EFSCORRUPTED;
3589 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3590 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3591 (le32_to_cpu(raw_super->extension_count) +
3592 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
3593 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3594 le32_to_cpu(raw_super->extension_count),
3595 raw_super->hot_ext_count,
3596 F2FS_MAX_EXTENSION);
3597 return -EFSCORRUPTED;
3600 if (le32_to_cpu(raw_super->cp_payload) >=
3601 (blocks_per_seg - F2FS_CP_PACKS -
3602 NR_CURSEG_PERSIST_TYPE)) {
3603 f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
3604 le32_to_cpu(raw_super->cp_payload),
3605 blocks_per_seg - F2FS_CP_PACKS -
3606 NR_CURSEG_PERSIST_TYPE);
3607 return -EFSCORRUPTED;
3610 /* check reserved ino info */
3611 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3612 le32_to_cpu(raw_super->meta_ino) != 2 ||
3613 le32_to_cpu(raw_super->root_ino) != 3) {
3614 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3615 le32_to_cpu(raw_super->node_ino),
3616 le32_to_cpu(raw_super->meta_ino),
3617 le32_to_cpu(raw_super->root_ino));
3618 return -EFSCORRUPTED;
3621 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
3622 if (sanity_check_area_boundary(sbi, bh))
3623 return -EFSCORRUPTED;
3628 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
3630 unsigned int total, fsmeta;
3631 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3632 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3633 unsigned int ovp_segments, reserved_segments;
3634 unsigned int main_segs, blocks_per_seg;
3635 unsigned int sit_segs, nat_segs;
3636 unsigned int sit_bitmap_size, nat_bitmap_size;
3637 unsigned int log_blocks_per_seg;
3638 unsigned int segment_count_main;
3639 unsigned int cp_pack_start_sum, cp_payload;
3640 block_t user_block_count, valid_user_blocks;
3641 block_t avail_node_count, valid_node_count;
3642 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
3645 total = le32_to_cpu(raw_super->segment_count);
3646 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
3647 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3649 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3651 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3652 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3654 if (unlikely(fsmeta >= total))
3657 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3658 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3660 if (!f2fs_sb_has_readonly(sbi) &&
3661 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
3662 ovp_segments == 0 || reserved_segments == 0)) {
3663 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
3666 user_block_count = le64_to_cpu(ckpt->user_block_count);
3667 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3668 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
3669 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3670 if (!user_block_count || user_block_count >=
3671 segment_count_main << log_blocks_per_seg) {
3672 f2fs_err(sbi, "Wrong user_block_count: %u",
3677 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3678 if (valid_user_blocks > user_block_count) {
3679 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3680 valid_user_blocks, user_block_count);
3684 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
3685 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
3686 if (valid_node_count > avail_node_count) {
3687 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3688 valid_node_count, avail_node_count);
3692 main_segs = le32_to_cpu(raw_super->segment_count_main);
3693 blocks_per_seg = sbi->blocks_per_seg;
3695 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3696 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3697 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3700 if (f2fs_sb_has_readonly(sbi))
3703 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3704 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3705 le32_to_cpu(ckpt->cur_node_segno[j])) {
3706 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3708 le32_to_cpu(ckpt->cur_node_segno[i]));
3714 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3715 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3716 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3719 if (f2fs_sb_has_readonly(sbi))
3722 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3723 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3724 le32_to_cpu(ckpt->cur_data_segno[j])) {
3725 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3727 le32_to_cpu(ckpt->cur_data_segno[i]));
3732 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3733 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3734 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3735 le32_to_cpu(ckpt->cur_data_segno[j])) {
3736 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3738 le32_to_cpu(ckpt->cur_node_segno[i]));
3744 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3745 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3747 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3748 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3749 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3750 sit_bitmap_size, nat_bitmap_size);
3754 cp_pack_start_sum = __start_sum_addr(sbi);
3755 cp_payload = __cp_payload(sbi);
3756 if (cp_pack_start_sum < cp_payload + 1 ||
3757 cp_pack_start_sum > blocks_per_seg - 1 -
3758 NR_CURSEG_PERSIST_TYPE) {
3759 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3764 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3765 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3766 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3767 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3768 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3769 le32_to_cpu(ckpt->checksum_offset));
3773 nat_blocks = nat_segs << log_blocks_per_seg;
3774 nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
3775 nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3776 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
3777 (cp_payload + F2FS_CP_PACKS +
3778 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
3779 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
3780 cp_payload, nat_bits_blocks);
3784 if (unlikely(f2fs_cp_error(sbi))) {
3785 f2fs_err(sbi, "A bug case: need to run fsck");
3791 static void init_sb_info(struct f2fs_sb_info *sbi)
3793 struct f2fs_super_block *raw_super = sbi->raw_super;
3796 sbi->log_sectors_per_block =
3797 le32_to_cpu(raw_super->log_sectors_per_block);
3798 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3799 sbi->blocksize = BIT(sbi->log_blocksize);
3800 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3801 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
3802 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3803 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3804 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3805 sbi->total_node_count =
3806 (le32_to_cpu(raw_super->segment_count_nat) / 2)
3807 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
3808 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3809 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3810 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3811 sbi->cur_victim_sec = NULL_SECNO;
3812 sbi->gc_mode = GC_NORMAL;
3813 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3814 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3815 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3816 sbi->migration_granularity = sbi->segs_per_sec;
3817 sbi->seq_file_ra_mul = MIN_RA_MUL;
3818 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
3819 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
3820 spin_lock_init(&sbi->gc_remaining_trials_lock);
3821 atomic64_set(&sbi->current_atomic_write, 0);
3823 sbi->dir_level = DEF_DIR_LEVEL;
3824 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3825 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3826 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3827 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3828 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3829 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3830 DEF_UMOUNT_DISCARD_TIMEOUT;
3831 clear_sbi_flag(sbi, SBI_NEED_FSCK);
3833 for (i = 0; i < NR_COUNT_TYPE; i++)
3834 atomic_set(&sbi->nr_pages[i], 0);
3836 for (i = 0; i < META; i++)
3837 atomic_set(&sbi->wb_sync_req[i], 0);
3839 INIT_LIST_HEAD(&sbi->s_list);
3840 mutex_init(&sbi->umount_mutex);
3841 init_f2fs_rwsem(&sbi->io_order_lock);
3842 spin_lock_init(&sbi->cp_lock);
3844 sbi->dirty_device = 0;
3845 spin_lock_init(&sbi->dev_lock);
3847 init_f2fs_rwsem(&sbi->sb_lock);
3848 init_f2fs_rwsem(&sbi->pin_sem);
3851 static int init_percpu_info(struct f2fs_sb_info *sbi)
3855 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3859 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
3861 goto err_valid_block;
3863 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3866 goto err_node_block;
3870 percpu_counter_destroy(&sbi->rf_node_block_count);
3872 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3876 #ifdef CONFIG_BLK_DEV_ZONED
3878 struct f2fs_report_zones_args {
3879 struct f2fs_sb_info *sbi;
3880 struct f2fs_dev_info *dev;
3883 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3886 struct f2fs_report_zones_args *rz_args = data;
3887 block_t unusable_blocks = (zone->len - zone->capacity) >>
3888 F2FS_LOG_SECTORS_PER_BLOCK;
3890 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3893 set_bit(idx, rz_args->dev->blkz_seq);
3894 if (!rz_args->sbi->unusable_blocks_per_sec) {
3895 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
3898 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
3899 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
3905 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
3907 struct block_device *bdev = FDEV(devi).bdev;
3908 sector_t nr_sectors = bdev_nr_sectors(bdev);
3909 struct f2fs_report_zones_args rep_zone_arg;
3913 if (!f2fs_sb_has_blkzoned(sbi))
3916 zone_sectors = bdev_zone_sectors(bdev);
3917 if (!is_power_of_2(zone_sectors)) {
3918 f2fs_err(sbi, "F2FS does not support non power of 2 zone sizes\n");
3922 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
3923 SECTOR_TO_BLOCK(zone_sectors))
3925 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
3926 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
3927 sbi->blocks_per_blkz);
3928 if (nr_sectors & (zone_sectors - 1))
3929 FDEV(devi).nr_blkz++;
3931 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
3932 BITS_TO_LONGS(FDEV(devi).nr_blkz)
3933 * sizeof(unsigned long),
3935 if (!FDEV(devi).blkz_seq)
3938 rep_zone_arg.sbi = sbi;
3939 rep_zone_arg.dev = &FDEV(devi);
3941 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3950 * Read f2fs raw super block.
3951 * Because we have two copies of super block, so read both of them
3952 * to get the first valid one. If any one of them is broken, we pass
3953 * them recovery flag back to the caller.
3955 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3956 struct f2fs_super_block **raw_super,
3957 int *valid_super_block, int *recovery)
3959 struct super_block *sb = sbi->sb;
3961 struct buffer_head *bh;
3962 struct f2fs_super_block *super;
3965 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3969 for (block = 0; block < 2; block++) {
3970 bh = sb_bread(sb, block);
3972 f2fs_err(sbi, "Unable to read %dth superblock",
3979 /* sanity checking of raw super */
3980 err = sanity_check_raw_super(sbi, bh);
3982 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3990 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3992 *valid_super_block = block;
3998 /* No valid superblock */
4007 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
4009 struct buffer_head *bh;
4013 if ((recover && f2fs_readonly(sbi->sb)) ||
4014 f2fs_hw_is_readonly(sbi)) {
4015 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
4019 /* we should update superblock crc here */
4020 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
4021 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
4022 offsetof(struct f2fs_super_block, crc));
4023 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
4026 /* write back-up superblock first */
4027 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
4030 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4033 /* if we are in recovery path, skip writing valid superblock */
4037 /* write current valid superblock */
4038 bh = sb_bread(sbi->sb, sbi->valid_super_block);
4041 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4046 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
4048 unsigned long flags;
4050 spin_lock_irqsave(&sbi->error_lock, flags);
4051 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
4052 sbi->stop_reason[reason]++;
4053 spin_unlock_irqrestore(&sbi->error_lock, flags);
4056 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
4058 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4059 unsigned long flags;
4062 f2fs_down_write(&sbi->sb_lock);
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;
4070 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
4071 spin_unlock_irqrestore(&sbi->error_lock, flags);
4073 err = f2fs_commit_super(sbi, false);
4075 f2fs_up_write(&sbi->sb_lock);
4077 f2fs_err(sbi, "f2fs_commit_super fails to record err:%d", err);
4080 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
4082 unsigned long flags;
4084 spin_lock_irqsave(&sbi->error_lock, flags);
4085 if (!test_bit(flag, (unsigned long *)sbi->errors)) {
4086 set_bit(flag, (unsigned long *)sbi->errors);
4087 sbi->error_dirty = true;
4089 spin_unlock_irqrestore(&sbi->error_lock, flags);
4092 static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
4094 unsigned long flags;
4095 bool need_update = false;
4097 spin_lock_irqsave(&sbi->error_lock, flags);
4098 if (sbi->error_dirty) {
4099 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4101 sbi->error_dirty = false;
4104 spin_unlock_irqrestore(&sbi->error_lock, flags);
4109 static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error)
4113 f2fs_down_write(&sbi->sb_lock);
4115 if (!f2fs_update_errors(sbi))
4118 err = f2fs_commit_super(sbi, false);
4120 f2fs_err(sbi, "f2fs_commit_super fails to record errors:%u, err:%d",
4123 f2fs_up_write(&sbi->sb_lock);
4126 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4128 f2fs_save_errors(sbi, error);
4129 f2fs_record_errors(sbi, error);
4132 void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error)
4134 f2fs_save_errors(sbi, error);
4136 if (!sbi->error_dirty)
4138 if (!test_bit(error, (unsigned long *)sbi->errors))
4140 schedule_work(&sbi->s_error_work);
4143 static bool system_going_down(void)
4145 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4146 || system_state == SYSTEM_RESTART;
4149 void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason,
4152 struct super_block *sb = sbi->sb;
4153 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
4154 bool continue_fs = !shutdown &&
4155 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
4157 set_ckpt_flags(sbi, CP_ERROR_FLAG);
4159 if (!f2fs_hw_is_readonly(sbi)) {
4160 save_stop_reason(sbi, reason);
4162 if (irq_context && !shutdown)
4163 schedule_work(&sbi->s_error_work);
4165 f2fs_record_stop_reason(sbi);
4169 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4170 * could panic during 'reboot -f' as the underlying device got already
4173 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
4174 !shutdown && !system_going_down() &&
4175 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
4176 panic("F2FS-fs (device %s): panic forced after error\n",
4180 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
4182 /* continue filesystem operators if errors=continue */
4183 if (continue_fs || f2fs_readonly(sb))
4186 f2fs_warn(sbi, "Remounting filesystem read-only");
4188 * Make sure updated value of ->s_mount_flags will be visible before
4192 sb->s_flags |= SB_RDONLY;
4195 static void f2fs_record_error_work(struct work_struct *work)
4197 struct f2fs_sb_info *sbi = container_of(work,
4198 struct f2fs_sb_info, s_error_work);
4200 f2fs_record_stop_reason(sbi);
4203 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
4205 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4206 unsigned int max_devices = MAX_DEVICES;
4207 unsigned int logical_blksize;
4208 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
4211 /* Initialize single device information */
4212 if (!RDEV(0).path[0]) {
4213 if (!bdev_is_zoned(sbi->sb->s_bdev))
4219 * Initialize multiple devices information, or single
4220 * zoned block device information.
4222 sbi->devs = f2fs_kzalloc(sbi,
4223 array_size(max_devices,
4224 sizeof(struct f2fs_dev_info)),
4229 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
4230 sbi->aligned_blksize = true;
4232 for (i = 0; i < max_devices; i++) {
4234 FDEV(0).bdev = sbi->sb->s_bdev;
4235 else if (!RDEV(i).path[0])
4238 if (max_devices > 1) {
4239 /* Multi-device mount */
4240 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
4241 FDEV(i).total_segments =
4242 le32_to_cpu(RDEV(i).total_segments);
4244 FDEV(i).start_blk = 0;
4245 FDEV(i).end_blk = FDEV(i).start_blk +
4246 (FDEV(i).total_segments <<
4247 sbi->log_blocks_per_seg) - 1 +
4248 le32_to_cpu(raw_super->segment0_blkaddr);
4250 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
4251 FDEV(i).end_blk = FDEV(i).start_blk +
4252 (FDEV(i).total_segments <<
4253 sbi->log_blocks_per_seg) - 1;
4254 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
4255 mode, sbi->sb, NULL);
4258 if (IS_ERR(FDEV(i).bdev))
4259 return PTR_ERR(FDEV(i).bdev);
4261 /* to release errored devices */
4262 sbi->s_ndevs = i + 1;
4264 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
4265 sbi->aligned_blksize = false;
4267 #ifdef CONFIG_BLK_DEV_ZONED
4268 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
4269 !f2fs_sb_has_blkzoned(sbi)) {
4270 f2fs_err(sbi, "Zoned block device feature not enabled");
4273 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
4274 if (init_blkz_info(sbi, i)) {
4275 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
4278 if (max_devices == 1)
4280 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
4282 FDEV(i).total_segments,
4283 FDEV(i).start_blk, FDEV(i).end_blk,
4284 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
4285 "Host-aware" : "Host-managed");
4289 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
4291 FDEV(i).total_segments,
4292 FDEV(i).start_blk, FDEV(i).end_blk);
4295 "IO Block Size: %8ld KB", F2FS_IO_SIZE_KB(sbi));
4299 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
4301 #if IS_ENABLED(CONFIG_UNICODE)
4302 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
4303 const struct f2fs_sb_encodings *encoding_info;
4304 struct unicode_map *encoding;
4305 __u16 encoding_flags;
4307 encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
4308 if (!encoding_info) {
4310 "Encoding requested by superblock is unknown");
4314 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
4315 encoding = utf8_load(encoding_info->version);
4316 if (IS_ERR(encoding)) {
4318 "can't mount with superblock charset: %s-%u.%u.%u "
4319 "not supported by the kernel. flags: 0x%x.",
4320 encoding_info->name,
4321 unicode_major(encoding_info->version),
4322 unicode_minor(encoding_info->version),
4323 unicode_rev(encoding_info->version),
4325 return PTR_ERR(encoding);
4327 f2fs_info(sbi, "Using encoding defined by superblock: "
4328 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4329 unicode_major(encoding_info->version),
4330 unicode_minor(encoding_info->version),
4331 unicode_rev(encoding_info->version),
4334 sbi->sb->s_encoding = encoding;
4335 sbi->sb->s_encoding_flags = encoding_flags;
4338 if (f2fs_sb_has_casefold(sbi)) {
4339 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
4346 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
4348 /* adjust parameters according to the volume size */
4349 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
4350 if (f2fs_block_unit_discard(sbi))
4351 SM_I(sbi)->dcc_info->discard_granularity =
4352 MIN_DISCARD_GRANULARITY;
4353 if (!f2fs_lfs_mode(sbi))
4354 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
4355 BIT(F2FS_IPU_HONOR_OPU_WRITE);
4358 sbi->readdir_ra = true;
4361 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
4363 struct f2fs_sb_info *sbi;
4364 struct f2fs_super_block *raw_super;
4367 bool skip_recovery = false, need_fsck = false;
4368 char *options = NULL;
4369 int recovery, i, valid_super_block;
4370 struct curseg_info *seg_i;
4373 bool quota_enabled = false;
4379 valid_super_block = -1;
4382 /* allocate memory for f2fs-specific super block info */
4383 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
4389 /* initialize locks within allocated memory */
4390 init_f2fs_rwsem(&sbi->gc_lock);
4391 mutex_init(&sbi->writepages);
4392 init_f2fs_rwsem(&sbi->cp_global_sem);
4393 init_f2fs_rwsem(&sbi->node_write);
4394 init_f2fs_rwsem(&sbi->node_change);
4395 spin_lock_init(&sbi->stat_lock);
4396 init_f2fs_rwsem(&sbi->cp_rwsem);
4397 init_f2fs_rwsem(&sbi->quota_sem);
4398 init_waitqueue_head(&sbi->cp_wait);
4399 spin_lock_init(&sbi->error_lock);
4401 for (i = 0; i < NR_INODE_TYPE; i++) {
4402 INIT_LIST_HEAD(&sbi->inode_list[i]);
4403 spin_lock_init(&sbi->inode_lock[i]);
4405 mutex_init(&sbi->flush_lock);
4407 /* Load the checksum driver */
4408 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
4409 if (IS_ERR(sbi->s_chksum_driver)) {
4410 f2fs_err(sbi, "Cannot load crc32 driver.");
4411 err = PTR_ERR(sbi->s_chksum_driver);
4412 sbi->s_chksum_driver = NULL;
4416 /* set a block size */
4417 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
4418 f2fs_err(sbi, "unable to set blocksize");
4422 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
4427 sb->s_fs_info = sbi;
4428 sbi->raw_super = raw_super;
4430 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
4431 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
4432 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
4434 /* precompute checksum seed for metadata */
4435 if (f2fs_sb_has_inode_chksum(sbi))
4436 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
4437 sizeof(raw_super->uuid));
4439 default_options(sbi, false);
4440 /* parse mount options */
4441 options = kstrdup((const char *)data, GFP_KERNEL);
4442 if (data && !options) {
4447 err = parse_options(sb, options, false);
4451 sb->s_maxbytes = max_file_blocks(NULL) <<
4452 le32_to_cpu(raw_super->log_blocksize);
4453 sb->s_max_links = F2FS_LINK_MAX;
4455 err = f2fs_setup_casefold(sbi);
4460 sb->dq_op = &f2fs_quota_operations;
4461 sb->s_qcop = &f2fs_quotactl_ops;
4462 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4464 if (f2fs_sb_has_quota_ino(sbi)) {
4465 for (i = 0; i < MAXQUOTAS; i++) {
4466 if (f2fs_qf_ino(sbi->sb, i))
4467 sbi->nquota_files++;
4472 sb->s_op = &f2fs_sops;
4473 #ifdef CONFIG_FS_ENCRYPTION
4474 sb->s_cop = &f2fs_cryptops;
4476 #ifdef CONFIG_FS_VERITY
4477 sb->s_vop = &f2fs_verityops;
4479 sb->s_xattr = f2fs_xattr_handlers;
4480 sb->s_export_op = &f2fs_export_ops;
4481 sb->s_magic = F2FS_SUPER_MAGIC;
4482 sb->s_time_gran = 1;
4483 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4484 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
4485 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
4486 sb->s_iflags |= SB_I_CGROUPWB;
4488 /* init f2fs-specific super block info */
4489 sbi->valid_super_block = valid_super_block;
4491 /* disallow all the data/node/meta page writes */
4492 set_sbi_flag(sbi, SBI_POR_DOING);
4494 err = f2fs_init_write_merge_io(sbi);
4500 err = f2fs_init_iostat(sbi);
4504 err = init_percpu_info(sbi);
4508 if (F2FS_IO_ALIGNED(sbi)) {
4509 sbi->write_io_dummy =
4510 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
4511 if (!sbi->write_io_dummy) {
4517 /* init per sbi slab cache */
4518 err = f2fs_init_xattr_caches(sbi);
4521 err = f2fs_init_page_array_cache(sbi);
4523 goto free_xattr_cache;
4525 /* get an inode for meta space */
4526 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
4527 if (IS_ERR(sbi->meta_inode)) {
4528 f2fs_err(sbi, "Failed to read F2FS meta data inode");
4529 err = PTR_ERR(sbi->meta_inode);
4530 goto free_page_array_cache;
4533 err = f2fs_get_valid_checkpoint(sbi);
4535 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
4536 goto free_meta_inode;
4539 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
4540 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
4541 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
4542 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4543 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
4546 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
4547 set_sbi_flag(sbi, SBI_NEED_FSCK);
4549 /* Initialize device list */
4550 err = f2fs_scan_devices(sbi);
4552 f2fs_err(sbi, "Failed to find devices");
4556 err = f2fs_init_post_read_wq(sbi);
4558 f2fs_err(sbi, "Failed to initialize post read workqueue");
4562 sbi->total_valid_node_count =
4563 le32_to_cpu(sbi->ckpt->valid_node_count);
4564 percpu_counter_set(&sbi->total_valid_inode_count,
4565 le32_to_cpu(sbi->ckpt->valid_inode_count));
4566 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
4567 sbi->total_valid_block_count =
4568 le64_to_cpu(sbi->ckpt->valid_block_count);
4569 sbi->last_valid_block_count = sbi->total_valid_block_count;
4570 sbi->reserved_blocks = 0;
4571 sbi->current_reserved_blocks = 0;
4572 limit_reserve_root(sbi);
4573 adjust_unusable_cap_perc(sbi);
4575 f2fs_init_extent_cache_info(sbi);
4577 f2fs_init_ino_entry_info(sbi);
4579 f2fs_init_fsync_node_info(sbi);
4581 /* setup checkpoint request control and start checkpoint issue thread */
4582 f2fs_init_ckpt_req_control(sbi);
4583 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
4584 test_opt(sbi, MERGE_CHECKPOINT)) {
4585 err = f2fs_start_ckpt_thread(sbi);
4588 "Failed to start F2FS issue_checkpoint_thread (%d)",
4590 goto stop_ckpt_thread;
4594 /* setup f2fs internal modules */
4595 err = f2fs_build_segment_manager(sbi);
4597 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
4601 err = f2fs_build_node_manager(sbi);
4603 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
4608 err = adjust_reserved_segment(sbi);
4612 /* For write statistics */
4613 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
4615 /* Read accumulated write IO statistics if exists */
4616 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
4617 if (__exist_node_summaries(sbi))
4618 sbi->kbytes_written =
4619 le64_to_cpu(seg_i->journal->info.kbytes_written);
4621 f2fs_build_gc_manager(sbi);
4623 err = f2fs_build_stats(sbi);
4627 /* get an inode for node space */
4628 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
4629 if (IS_ERR(sbi->node_inode)) {
4630 f2fs_err(sbi, "Failed to read node inode");
4631 err = PTR_ERR(sbi->node_inode);
4635 /* read root inode and dentry */
4636 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
4638 f2fs_err(sbi, "Failed to read root inode");
4639 err = PTR_ERR(root);
4640 goto free_node_inode;
4642 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
4643 !root->i_size || !root->i_nlink) {
4646 goto free_node_inode;
4649 sb->s_root = d_make_root(root); /* allocate root dentry */
4652 goto free_node_inode;
4655 err = f2fs_init_compress_inode(sbi);
4657 goto free_root_inode;
4659 err = f2fs_register_sysfs(sbi);
4661 goto free_compress_inode;
4664 /* Enable quota usage during mount */
4665 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
4666 err = f2fs_enable_quotas(sb);
4668 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
4671 quota_enabled = f2fs_recover_quota_begin(sbi);
4673 /* if there are any orphan inodes, free them */
4674 err = f2fs_recover_orphan_inodes(sbi);
4678 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
4679 goto reset_checkpoint;
4681 /* recover fsynced data */
4682 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
4683 !test_opt(sbi, NORECOVERY)) {
4685 * mount should be failed, when device has readonly mode, and
4686 * previous checkpoint was not done by clean system shutdown.
4688 if (f2fs_hw_is_readonly(sbi)) {
4689 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4690 err = f2fs_recover_fsync_data(sbi, true);
4693 f2fs_err(sbi, "Need to recover fsync data, but "
4694 "write access unavailable, please try "
4695 "mount w/ disable_roll_forward or norecovery");
4700 f2fs_info(sbi, "write access unavailable, skipping recovery");
4701 goto reset_checkpoint;
4705 set_sbi_flag(sbi, SBI_NEED_FSCK);
4708 goto reset_checkpoint;
4710 err = f2fs_recover_fsync_data(sbi, false);
4713 skip_recovery = true;
4715 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4720 err = f2fs_recover_fsync_data(sbi, true);
4722 if (!f2fs_readonly(sb) && err > 0) {
4724 f2fs_err(sbi, "Need to recover fsync data");
4730 f2fs_recover_quota_end(sbi, quota_enabled);
4734 * If the f2fs is not readonly and fsync data recovery succeeds,
4735 * check zoned block devices' write pointer consistency.
4737 if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
4738 err = f2fs_check_write_pointer(sbi);
4744 f2fs_init_inmem_curseg(sbi);
4746 /* f2fs_recover_fsync_data() cleared this already */
4747 clear_sbi_flag(sbi, SBI_POR_DOING);
4749 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4750 err = f2fs_disable_checkpoint(sbi);
4752 goto sync_free_meta;
4753 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4754 f2fs_enable_checkpoint(sbi);
4758 * If filesystem is not mounted as read-only then
4759 * do start the gc_thread.
4761 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4762 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
4763 /* After POR, we can run background GC thread.*/
4764 err = f2fs_start_gc_thread(sbi);
4766 goto sync_free_meta;
4770 /* recover broken superblock */
4772 err = f2fs_commit_super(sbi, true);
4773 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4774 sbi->valid_super_block ? 1 : 2, err);
4777 f2fs_join_shrinker(sbi);
4779 f2fs_tuning_parameters(sbi);
4781 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4782 cur_cp_version(F2FS_CKPT(sbi)));
4783 f2fs_update_time(sbi, CP_TIME);
4784 f2fs_update_time(sbi, REQ_TIME);
4785 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4789 /* safe to flush all the data */
4790 sync_filesystem(sbi->sb);
4795 f2fs_truncate_quota_inode_pages(sb);
4796 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
4797 f2fs_quota_off_umount(sbi->sb);
4800 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4801 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4802 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4803 * falls into an infinite loop in f2fs_sync_meta_pages().
4805 truncate_inode_pages_final(META_MAPPING(sbi));
4806 /* evict some inodes being cached by GC */
4808 f2fs_unregister_sysfs(sbi);
4809 free_compress_inode:
4810 f2fs_destroy_compress_inode(sbi);
4815 f2fs_release_ino_entry(sbi, true);
4816 truncate_inode_pages_final(NODE_MAPPING(sbi));
4817 iput(sbi->node_inode);
4818 sbi->node_inode = NULL;
4820 f2fs_destroy_stats(sbi);
4822 /* stop discard thread before destroying node manager */
4823 f2fs_stop_discard_thread(sbi);
4824 f2fs_destroy_node_manager(sbi);
4826 f2fs_destroy_segment_manager(sbi);
4828 f2fs_stop_ckpt_thread(sbi);
4829 /* flush s_error_work before sbi destroy */
4830 flush_work(&sbi->s_error_work);
4831 f2fs_destroy_post_read_wq(sbi);
4833 destroy_device_list(sbi);
4836 make_bad_inode(sbi->meta_inode);
4837 iput(sbi->meta_inode);
4838 sbi->meta_inode = NULL;
4839 free_page_array_cache:
4840 f2fs_destroy_page_array_cache(sbi);
4842 f2fs_destroy_xattr_caches(sbi);
4844 mempool_destroy(sbi->write_io_dummy);
4846 destroy_percpu_info(sbi);
4848 f2fs_destroy_iostat(sbi);
4850 for (i = 0; i < NR_PAGE_TYPE; i++)
4851 kvfree(sbi->write_io[i]);
4853 #if IS_ENABLED(CONFIG_UNICODE)
4854 utf8_unload(sb->s_encoding);
4855 sb->s_encoding = NULL;
4859 for (i = 0; i < MAXQUOTAS; i++)
4860 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4862 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
4867 if (sbi->s_chksum_driver)
4868 crypto_free_shash(sbi->s_chksum_driver);
4871 /* give only one another chance */
4872 if (retry_cnt > 0 && skip_recovery) {
4874 shrink_dcache_sb(sb);
4880 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
4881 const char *dev_name, void *data)
4883 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
4886 static void kill_f2fs_super(struct super_block *sb)
4889 struct f2fs_sb_info *sbi = F2FS_SB(sb);
4891 set_sbi_flag(sbi, SBI_IS_CLOSE);
4892 f2fs_stop_gc_thread(sbi);
4893 f2fs_stop_discard_thread(sbi);
4895 #ifdef CONFIG_F2FS_FS_COMPRESSION
4897 * latter evict_inode() can bypass checking and invalidating
4898 * compress inode cache.
4900 if (test_opt(sbi, COMPRESS_CACHE))
4901 truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
4904 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
4905 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4906 struct cp_control cpc = {
4907 .reason = CP_UMOUNT,
4909 stat_inc_cp_call_count(sbi, TOTAL_CALL);
4910 f2fs_write_checkpoint(sbi, &cpc);
4913 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
4914 sb->s_flags &= ~SB_RDONLY;
4916 kill_block_super(sb);
4919 static struct file_system_type f2fs_fs_type = {
4920 .owner = THIS_MODULE,
4922 .mount = f2fs_mount,
4923 .kill_sb = kill_f2fs_super,
4924 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
4926 MODULE_ALIAS_FS("f2fs");
4928 static int __init init_inodecache(void)
4930 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
4931 sizeof(struct f2fs_inode_info), 0,
4932 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
4933 return f2fs_inode_cachep ? 0 : -ENOMEM;
4936 static void destroy_inodecache(void)
4939 * Make sure all delayed rcu free inodes are flushed before we
4943 kmem_cache_destroy(f2fs_inode_cachep);
4946 static int __init init_f2fs_fs(void)
4950 if (PAGE_SIZE != F2FS_BLKSIZE) {
4951 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
4952 PAGE_SIZE, F2FS_BLKSIZE);
4956 err = init_inodecache();
4959 err = f2fs_create_node_manager_caches();
4961 goto free_inodecache;
4962 err = f2fs_create_segment_manager_caches();
4964 goto free_node_manager_caches;
4965 err = f2fs_create_checkpoint_caches();
4967 goto free_segment_manager_caches;
4968 err = f2fs_create_recovery_cache();
4970 goto free_checkpoint_caches;
4971 err = f2fs_create_extent_cache();
4973 goto free_recovery_cache;
4974 err = f2fs_create_garbage_collection_cache();
4976 goto free_extent_cache;
4977 err = f2fs_init_sysfs();
4979 goto free_garbage_collection_cache;
4980 err = register_shrinker(&f2fs_shrinker_info, "f2fs-shrinker");
4983 err = register_filesystem(&f2fs_fs_type);
4986 f2fs_create_root_stats();
4987 err = f2fs_init_post_read_processing();
4989 goto free_root_stats;
4990 err = f2fs_init_iostat_processing();
4992 goto free_post_read;
4993 err = f2fs_init_bio_entry_cache();
4996 err = f2fs_init_bioset();
4998 goto free_bio_entry_cache;
4999 err = f2fs_init_compress_mempool();
5002 err = f2fs_init_compress_cache();
5004 goto free_compress_mempool;
5005 err = f2fs_create_casefold_cache();
5007 goto free_compress_cache;
5009 free_compress_cache:
5010 f2fs_destroy_compress_cache();
5011 free_compress_mempool:
5012 f2fs_destroy_compress_mempool();
5014 f2fs_destroy_bioset();
5015 free_bio_entry_cache:
5016 f2fs_destroy_bio_entry_cache();
5018 f2fs_destroy_iostat_processing();
5020 f2fs_destroy_post_read_processing();
5022 f2fs_destroy_root_stats();
5023 unregister_filesystem(&f2fs_fs_type);
5025 unregister_shrinker(&f2fs_shrinker_info);
5028 free_garbage_collection_cache:
5029 f2fs_destroy_garbage_collection_cache();
5031 f2fs_destroy_extent_cache();
5032 free_recovery_cache:
5033 f2fs_destroy_recovery_cache();
5034 free_checkpoint_caches:
5035 f2fs_destroy_checkpoint_caches();
5036 free_segment_manager_caches:
5037 f2fs_destroy_segment_manager_caches();
5038 free_node_manager_caches:
5039 f2fs_destroy_node_manager_caches();
5041 destroy_inodecache();
5046 static void __exit exit_f2fs_fs(void)
5048 f2fs_destroy_casefold_cache();
5049 f2fs_destroy_compress_cache();
5050 f2fs_destroy_compress_mempool();
5051 f2fs_destroy_bioset();
5052 f2fs_destroy_bio_entry_cache();
5053 f2fs_destroy_iostat_processing();
5054 f2fs_destroy_post_read_processing();
5055 f2fs_destroy_root_stats();
5056 unregister_filesystem(&f2fs_fs_type);
5057 unregister_shrinker(&f2fs_shrinker_info);
5059 f2fs_destroy_garbage_collection_cache();
5060 f2fs_destroy_extent_cache();
5061 f2fs_destroy_recovery_cache();
5062 f2fs_destroy_checkpoint_caches();
5063 f2fs_destroy_segment_manager_caches();
5064 f2fs_destroy_node_manager_caches();
5065 destroy_inodecache();
5068 module_init(init_f2fs_fs)
5069 module_exit(exit_f2fs_fs)
5071 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
5072 MODULE_DESCRIPTION("Flash Friendly File System");
5073 MODULE_LICENSE("GPL");
5074 MODULE_SOFTDEP("pre: crc32");