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/statfs.h>
12 #include <linux/buffer_head.h>
13 #include <linux/backing-dev.h>
14 #include <linux/kthread.h>
15 #include <linux/parser.h>
16 #include <linux/mount.h>
17 #include <linux/seq_file.h>
18 #include <linux/proc_fs.h>
19 #include <linux/random.h>
20 #include <linux/exportfs.h>
21 #include <linux/blkdev.h>
22 #include <linux/quotaops.h>
23 #include <linux/f2fs_fs.h>
24 #include <linux/sysfs.h>
25 #include <linux/quota.h>
26 #include <linux/unicode.h>
27 #include <linux/part_stat.h>
28 #include <linux/zstd.h>
29 #include <linux/lz4.h>
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/f2fs.h>
40 static struct kmem_cache *f2fs_inode_cachep;
42 #ifdef CONFIG_F2FS_FAULT_INJECTION
44 const char *f2fs_fault_name[FAULT_MAX] = {
45 [FAULT_KMALLOC] = "kmalloc",
46 [FAULT_KVMALLOC] = "kvmalloc",
47 [FAULT_PAGE_ALLOC] = "page alloc",
48 [FAULT_PAGE_GET] = "page get",
49 [FAULT_ALLOC_NID] = "alloc nid",
50 [FAULT_ORPHAN] = "orphan",
51 [FAULT_BLOCK] = "no more block",
52 [FAULT_DIR_DEPTH] = "too big dir depth",
53 [FAULT_EVICT_INODE] = "evict_inode fail",
54 [FAULT_TRUNCATE] = "truncate fail",
55 [FAULT_READ_IO] = "read IO error",
56 [FAULT_CHECKPOINT] = "checkpoint error",
57 [FAULT_DISCARD] = "discard error",
58 [FAULT_WRITE_IO] = "write IO error",
61 void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
64 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
67 atomic_set(&ffi->inject_ops, 0);
68 ffi->inject_rate = rate;
72 ffi->inject_type = type;
75 memset(ffi, 0, sizeof(struct f2fs_fault_info));
79 /* f2fs-wide shrinker description */
80 static struct shrinker f2fs_shrinker_info = {
81 .scan_objects = f2fs_shrink_scan,
82 .count_objects = f2fs_shrink_count,
83 .seeks = DEFAULT_SEEKS,
88 Opt_disable_roll_forward,
99 Opt_disable_ext_identify,
102 Opt_inline_xattr_size,
140 Opt_test_dummy_encryption,
142 Opt_checkpoint_disable,
143 Opt_checkpoint_disable_cap,
144 Opt_checkpoint_disable_cap_perc,
145 Opt_checkpoint_enable,
146 Opt_checkpoint_merge,
147 Opt_nocheckpoint_merge,
148 Opt_compress_algorithm,
149 Opt_compress_log_size,
150 Opt_compress_extension,
160 static match_table_t f2fs_tokens = {
161 {Opt_gc_background, "background_gc=%s"},
162 {Opt_disable_roll_forward, "disable_roll_forward"},
163 {Opt_norecovery, "norecovery"},
164 {Opt_discard, "discard"},
165 {Opt_nodiscard, "nodiscard"},
166 {Opt_noheap, "no_heap"},
168 {Opt_user_xattr, "user_xattr"},
169 {Opt_nouser_xattr, "nouser_xattr"},
171 {Opt_noacl, "noacl"},
172 {Opt_active_logs, "active_logs=%u"},
173 {Opt_disable_ext_identify, "disable_ext_identify"},
174 {Opt_inline_xattr, "inline_xattr"},
175 {Opt_noinline_xattr, "noinline_xattr"},
176 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
177 {Opt_inline_data, "inline_data"},
178 {Opt_inline_dentry, "inline_dentry"},
179 {Opt_noinline_dentry, "noinline_dentry"},
180 {Opt_flush_merge, "flush_merge"},
181 {Opt_noflush_merge, "noflush_merge"},
182 {Opt_nobarrier, "nobarrier"},
183 {Opt_fastboot, "fastboot"},
184 {Opt_extent_cache, "extent_cache"},
185 {Opt_noextent_cache, "noextent_cache"},
186 {Opt_noinline_data, "noinline_data"},
187 {Opt_data_flush, "data_flush"},
188 {Opt_reserve_root, "reserve_root=%u"},
189 {Opt_resgid, "resgid=%u"},
190 {Opt_resuid, "resuid=%u"},
191 {Opt_mode, "mode=%s"},
192 {Opt_io_size_bits, "io_bits=%u"},
193 {Opt_fault_injection, "fault_injection=%u"},
194 {Opt_fault_type, "fault_type=%u"},
195 {Opt_lazytime, "lazytime"},
196 {Opt_nolazytime, "nolazytime"},
197 {Opt_quota, "quota"},
198 {Opt_noquota, "noquota"},
199 {Opt_usrquota, "usrquota"},
200 {Opt_grpquota, "grpquota"},
201 {Opt_prjquota, "prjquota"},
202 {Opt_usrjquota, "usrjquota=%s"},
203 {Opt_grpjquota, "grpjquota=%s"},
204 {Opt_prjjquota, "prjjquota=%s"},
205 {Opt_offusrjquota, "usrjquota="},
206 {Opt_offgrpjquota, "grpjquota="},
207 {Opt_offprjjquota, "prjjquota="},
208 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
209 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
210 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
211 {Opt_whint, "whint_mode=%s"},
212 {Opt_alloc, "alloc_mode=%s"},
213 {Opt_fsync, "fsync_mode=%s"},
214 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
215 {Opt_test_dummy_encryption, "test_dummy_encryption"},
216 {Opt_inlinecrypt, "inlinecrypt"},
217 {Opt_checkpoint_disable, "checkpoint=disable"},
218 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
219 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
220 {Opt_checkpoint_enable, "checkpoint=enable"},
221 {Opt_checkpoint_merge, "checkpoint_merge"},
222 {Opt_nocheckpoint_merge, "nocheckpoint_merge"},
223 {Opt_compress_algorithm, "compress_algorithm=%s"},
224 {Opt_compress_log_size, "compress_log_size=%u"},
225 {Opt_compress_extension, "compress_extension=%s"},
226 {Opt_compress_chksum, "compress_chksum"},
227 {Opt_compress_mode, "compress_mode=%s"},
228 {Opt_compress_cache, "compress_cache"},
230 {Opt_gc_merge, "gc_merge"},
231 {Opt_nogc_merge, "nogc_merge"},
235 void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...)
237 struct va_format vaf;
243 level = printk_get_level(fmt);
244 vaf.fmt = printk_skip_level(fmt);
246 printk("%c%cF2FS-fs (%s): %pV\n",
247 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
252 #ifdef CONFIG_UNICODE
253 static const struct f2fs_sb_encodings {
257 } f2fs_sb_encoding_map[] = {
258 {F2FS_ENC_UTF8_12_1, "utf8", "12.1.0"},
261 static int f2fs_sb_read_encoding(const struct f2fs_super_block *sb,
262 const struct f2fs_sb_encodings **encoding,
265 __u16 magic = le16_to_cpu(sb->s_encoding);
268 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
269 if (magic == f2fs_sb_encoding_map[i].magic)
272 if (i >= ARRAY_SIZE(f2fs_sb_encoding_map))
275 *encoding = &f2fs_sb_encoding_map[i];
276 *flags = le16_to_cpu(sb->s_encoding_flags);
282 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
284 block_t limit = min((sbi->user_block_count << 1) / 1000,
285 sbi->user_block_count - sbi->reserved_blocks);
288 if (test_opt(sbi, RESERVE_ROOT) &&
289 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
290 F2FS_OPTION(sbi).root_reserved_blocks = limit;
291 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
292 F2FS_OPTION(sbi).root_reserved_blocks);
294 if (!test_opt(sbi, RESERVE_ROOT) &&
295 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
296 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
297 !gid_eq(F2FS_OPTION(sbi).s_resgid,
298 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
299 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
300 from_kuid_munged(&init_user_ns,
301 F2FS_OPTION(sbi).s_resuid),
302 from_kgid_munged(&init_user_ns,
303 F2FS_OPTION(sbi).s_resgid));
306 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
308 if (!F2FS_OPTION(sbi).unusable_cap_perc)
311 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
312 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
314 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
315 F2FS_OPTION(sbi).unusable_cap_perc;
317 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
318 F2FS_OPTION(sbi).unusable_cap,
319 F2FS_OPTION(sbi).unusable_cap_perc);
322 static void init_once(void *foo)
324 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
326 inode_init_once(&fi->vfs_inode);
330 static const char * const quotatypes[] = INITQFNAMES;
331 #define QTYPE2NAME(t) (quotatypes[t])
332 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
335 struct f2fs_sb_info *sbi = F2FS_SB(sb);
339 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
340 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
343 if (f2fs_sb_has_quota_ino(sbi)) {
344 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
348 qname = match_strdup(args);
350 f2fs_err(sbi, "Not enough memory for storing quotafile name");
353 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
354 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
357 f2fs_err(sbi, "%s quota file already specified",
361 if (strchr(qname, '/')) {
362 f2fs_err(sbi, "quotafile must be on filesystem root");
365 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
373 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
375 struct f2fs_sb_info *sbi = F2FS_SB(sb);
377 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
378 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
381 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
382 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
386 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
389 * We do the test below only for project quotas. 'usrquota' and
390 * 'grpquota' mount options are allowed even without quota feature
391 * to support legacy quotas in quota files.
393 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
394 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
397 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
398 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
399 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
400 if (test_opt(sbi, USRQUOTA) &&
401 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
402 clear_opt(sbi, USRQUOTA);
404 if (test_opt(sbi, GRPQUOTA) &&
405 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
406 clear_opt(sbi, GRPQUOTA);
408 if (test_opt(sbi, PRJQUOTA) &&
409 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
410 clear_opt(sbi, PRJQUOTA);
412 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
413 test_opt(sbi, PRJQUOTA)) {
414 f2fs_err(sbi, "old and new quota format mixing");
418 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
419 f2fs_err(sbi, "journaled quota format not specified");
424 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
425 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
426 F2FS_OPTION(sbi).s_jquota_fmt = 0;
432 static int f2fs_set_test_dummy_encryption(struct super_block *sb,
434 const substring_t *arg,
437 struct f2fs_sb_info *sbi = F2FS_SB(sb);
438 #ifdef CONFIG_FS_ENCRYPTION
441 if (!f2fs_sb_has_encrypt(sbi)) {
442 f2fs_err(sbi, "Encrypt feature is off");
447 * This mount option is just for testing, and it's not worthwhile to
448 * implement the extra complexity (e.g. RCU protection) that would be
449 * needed to allow it to be set or changed during remount. We do allow
450 * it to be specified during remount, but only if there is no change.
452 if (is_remount && !F2FS_OPTION(sbi).dummy_enc_policy.policy) {
453 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
456 err = fscrypt_set_test_dummy_encryption(
457 sb, arg->from, &F2FS_OPTION(sbi).dummy_enc_policy);
461 "Can't change test_dummy_encryption on remount");
462 else if (err == -EINVAL)
463 f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
466 f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
470 f2fs_warn(sbi, "Test dummy encryption mode enabled");
472 f2fs_warn(sbi, "Test dummy encryption mount option ignored");
477 #ifdef CONFIG_F2FS_FS_COMPRESSION
478 #ifdef CONFIG_F2FS_FS_LZ4
479 static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
481 #ifdef CONFIG_F2FS_FS_LZ4HC
485 if (strlen(str) == 3) {
486 F2FS_OPTION(sbi).compress_level = 0;
490 #ifdef CONFIG_F2FS_FS_LZ4HC
494 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
497 if (kstrtouint(str + 1, 10, &level))
500 if (level < LZ4HC_MIN_CLEVEL || level > LZ4HC_MAX_CLEVEL) {
501 f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
505 F2FS_OPTION(sbi).compress_level = level;
508 f2fs_info(sbi, "kernel doesn't support lz4hc compression");
514 #ifdef CONFIG_F2FS_FS_ZSTD
515 static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
520 if (strlen(str) == len) {
521 F2FS_OPTION(sbi).compress_level = 0;
528 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
531 if (kstrtouint(str + 1, 10, &level))
534 if (!level || level > ZSTD_maxCLevel()) {
535 f2fs_info(sbi, "invalid zstd compress level: %d", level);
539 F2FS_OPTION(sbi).compress_level = level;
545 static int parse_options(struct super_block *sb, char *options, bool is_remount)
547 struct f2fs_sb_info *sbi = F2FS_SB(sb);
548 substring_t args[MAX_OPT_ARGS];
549 #ifdef CONFIG_F2FS_FS_COMPRESSION
550 unsigned char (*ext)[F2FS_EXTENSION_LEN];
562 while ((p = strsep(&options, ",")) != NULL) {
568 * Initialize args struct so we know whether arg was
569 * found; some options take optional arguments.
571 args[0].to = args[0].from = NULL;
572 token = match_token(p, f2fs_tokens, args);
575 case Opt_gc_background:
576 name = match_strdup(&args[0]);
580 if (!strcmp(name, "on")) {
581 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
582 } else if (!strcmp(name, "off")) {
583 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
584 } else if (!strcmp(name, "sync")) {
585 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
592 case Opt_disable_roll_forward:
593 set_opt(sbi, DISABLE_ROLL_FORWARD);
596 /* this option mounts f2fs with ro */
597 set_opt(sbi, NORECOVERY);
598 if (!f2fs_readonly(sb))
602 set_opt(sbi, DISCARD);
605 if (f2fs_sb_has_blkzoned(sbi)) {
606 f2fs_warn(sbi, "discard is required for zoned block devices");
609 clear_opt(sbi, DISCARD);
612 set_opt(sbi, NOHEAP);
615 clear_opt(sbi, NOHEAP);
617 #ifdef CONFIG_F2FS_FS_XATTR
619 set_opt(sbi, XATTR_USER);
621 case Opt_nouser_xattr:
622 clear_opt(sbi, XATTR_USER);
624 case Opt_inline_xattr:
625 set_opt(sbi, INLINE_XATTR);
627 case Opt_noinline_xattr:
628 clear_opt(sbi, INLINE_XATTR);
630 case Opt_inline_xattr_size:
631 if (args->from && match_int(args, &arg))
633 set_opt(sbi, INLINE_XATTR_SIZE);
634 F2FS_OPTION(sbi).inline_xattr_size = arg;
638 f2fs_info(sbi, "user_xattr options not supported");
640 case Opt_nouser_xattr:
641 f2fs_info(sbi, "nouser_xattr options not supported");
643 case Opt_inline_xattr:
644 f2fs_info(sbi, "inline_xattr options not supported");
646 case Opt_noinline_xattr:
647 f2fs_info(sbi, "noinline_xattr options not supported");
650 #ifdef CONFIG_F2FS_FS_POSIX_ACL
652 set_opt(sbi, POSIX_ACL);
655 clear_opt(sbi, POSIX_ACL);
659 f2fs_info(sbi, "acl options not supported");
662 f2fs_info(sbi, "noacl options not supported");
665 case Opt_active_logs:
666 if (args->from && match_int(args, &arg))
668 if (arg != 2 && arg != 4 &&
669 arg != NR_CURSEG_PERSIST_TYPE)
671 F2FS_OPTION(sbi).active_logs = arg;
673 case Opt_disable_ext_identify:
674 set_opt(sbi, DISABLE_EXT_IDENTIFY);
676 case Opt_inline_data:
677 set_opt(sbi, INLINE_DATA);
679 case Opt_inline_dentry:
680 set_opt(sbi, INLINE_DENTRY);
682 case Opt_noinline_dentry:
683 clear_opt(sbi, INLINE_DENTRY);
685 case Opt_flush_merge:
686 set_opt(sbi, FLUSH_MERGE);
688 case Opt_noflush_merge:
689 clear_opt(sbi, FLUSH_MERGE);
692 set_opt(sbi, NOBARRIER);
695 set_opt(sbi, FASTBOOT);
697 case Opt_extent_cache:
698 set_opt(sbi, EXTENT_CACHE);
700 case Opt_noextent_cache:
701 clear_opt(sbi, EXTENT_CACHE);
703 case Opt_noinline_data:
704 clear_opt(sbi, INLINE_DATA);
707 set_opt(sbi, DATA_FLUSH);
709 case Opt_reserve_root:
710 if (args->from && match_int(args, &arg))
712 if (test_opt(sbi, RESERVE_ROOT)) {
713 f2fs_info(sbi, "Preserve previous reserve_root=%u",
714 F2FS_OPTION(sbi).root_reserved_blocks);
716 F2FS_OPTION(sbi).root_reserved_blocks = arg;
717 set_opt(sbi, RESERVE_ROOT);
721 if (args->from && match_int(args, &arg))
723 uid = make_kuid(current_user_ns(), arg);
724 if (!uid_valid(uid)) {
725 f2fs_err(sbi, "Invalid uid value %d", arg);
728 F2FS_OPTION(sbi).s_resuid = uid;
731 if (args->from && match_int(args, &arg))
733 gid = make_kgid(current_user_ns(), arg);
734 if (!gid_valid(gid)) {
735 f2fs_err(sbi, "Invalid gid value %d", arg);
738 F2FS_OPTION(sbi).s_resgid = gid;
741 name = match_strdup(&args[0]);
745 if (!strcmp(name, "adaptive")) {
746 if (f2fs_sb_has_blkzoned(sbi)) {
747 f2fs_warn(sbi, "adaptive mode is not allowed with zoned block device feature");
751 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
752 } else if (!strcmp(name, "lfs")) {
753 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
760 case Opt_io_size_bits:
761 if (args->from && match_int(args, &arg))
763 if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_VECS)) {
764 f2fs_warn(sbi, "Not support %d, larger than %d",
765 1 << arg, BIO_MAX_VECS);
768 F2FS_OPTION(sbi).write_io_size_bits = arg;
770 #ifdef CONFIG_F2FS_FAULT_INJECTION
771 case Opt_fault_injection:
772 if (args->from && match_int(args, &arg))
774 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
775 set_opt(sbi, FAULT_INJECTION);
779 if (args->from && match_int(args, &arg))
781 f2fs_build_fault_attr(sbi, 0, arg);
782 set_opt(sbi, FAULT_INJECTION);
785 case Opt_fault_injection:
786 f2fs_info(sbi, "fault_injection options not supported");
790 f2fs_info(sbi, "fault_type options not supported");
794 sb->s_flags |= SB_LAZYTIME;
797 sb->s_flags &= ~SB_LAZYTIME;
802 set_opt(sbi, USRQUOTA);
805 set_opt(sbi, GRPQUOTA);
808 set_opt(sbi, PRJQUOTA);
811 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
816 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
821 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
825 case Opt_offusrjquota:
826 ret = f2fs_clear_qf_name(sb, USRQUOTA);
830 case Opt_offgrpjquota:
831 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
835 case Opt_offprjjquota:
836 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
840 case Opt_jqfmt_vfsold:
841 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
843 case Opt_jqfmt_vfsv0:
844 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
846 case Opt_jqfmt_vfsv1:
847 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
850 clear_opt(sbi, QUOTA);
851 clear_opt(sbi, USRQUOTA);
852 clear_opt(sbi, GRPQUOTA);
853 clear_opt(sbi, PRJQUOTA);
863 case Opt_offusrjquota:
864 case Opt_offgrpjquota:
865 case Opt_offprjjquota:
866 case Opt_jqfmt_vfsold:
867 case Opt_jqfmt_vfsv0:
868 case Opt_jqfmt_vfsv1:
870 f2fs_info(sbi, "quota operations not supported");
874 name = match_strdup(&args[0]);
877 if (!strcmp(name, "user-based")) {
878 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
879 } else if (!strcmp(name, "off")) {
880 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
881 } else if (!strcmp(name, "fs-based")) {
882 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
890 name = match_strdup(&args[0]);
894 if (!strcmp(name, "default")) {
895 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
896 } else if (!strcmp(name, "reuse")) {
897 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
905 name = match_strdup(&args[0]);
908 if (!strcmp(name, "posix")) {
909 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
910 } else if (!strcmp(name, "strict")) {
911 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
912 } else if (!strcmp(name, "nobarrier")) {
913 F2FS_OPTION(sbi).fsync_mode =
914 FSYNC_MODE_NOBARRIER;
921 case Opt_test_dummy_encryption:
922 ret = f2fs_set_test_dummy_encryption(sb, p, &args[0],
927 case Opt_inlinecrypt:
928 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
929 sb->s_flags |= SB_INLINECRYPT;
931 f2fs_info(sbi, "inline encryption not supported");
934 case Opt_checkpoint_disable_cap_perc:
935 if (args->from && match_int(args, &arg))
937 if (arg < 0 || arg > 100)
939 F2FS_OPTION(sbi).unusable_cap_perc = arg;
940 set_opt(sbi, DISABLE_CHECKPOINT);
942 case Opt_checkpoint_disable_cap:
943 if (args->from && match_int(args, &arg))
945 F2FS_OPTION(sbi).unusable_cap = arg;
946 set_opt(sbi, DISABLE_CHECKPOINT);
948 case Opt_checkpoint_disable:
949 set_opt(sbi, DISABLE_CHECKPOINT);
951 case Opt_checkpoint_enable:
952 clear_opt(sbi, DISABLE_CHECKPOINT);
954 case Opt_checkpoint_merge:
955 set_opt(sbi, MERGE_CHECKPOINT);
957 case Opt_nocheckpoint_merge:
958 clear_opt(sbi, MERGE_CHECKPOINT);
960 #ifdef CONFIG_F2FS_FS_COMPRESSION
961 case Opt_compress_algorithm:
962 if (!f2fs_sb_has_compression(sbi)) {
963 f2fs_info(sbi, "Image doesn't support compression");
966 name = match_strdup(&args[0]);
969 if (!strcmp(name, "lzo")) {
970 #ifdef CONFIG_F2FS_FS_LZO
971 F2FS_OPTION(sbi).compress_level = 0;
972 F2FS_OPTION(sbi).compress_algorithm =
975 f2fs_info(sbi, "kernel doesn't support lzo compression");
977 } else if (!strncmp(name, "lz4", 3)) {
978 #ifdef CONFIG_F2FS_FS_LZ4
979 ret = f2fs_set_lz4hc_level(sbi, name);
984 F2FS_OPTION(sbi).compress_algorithm =
987 f2fs_info(sbi, "kernel doesn't support lz4 compression");
989 } else if (!strncmp(name, "zstd", 4)) {
990 #ifdef CONFIG_F2FS_FS_ZSTD
991 ret = f2fs_set_zstd_level(sbi, name);
996 F2FS_OPTION(sbi).compress_algorithm =
999 f2fs_info(sbi, "kernel doesn't support zstd compression");
1001 } else if (!strcmp(name, "lzo-rle")) {
1002 #ifdef CONFIG_F2FS_FS_LZORLE
1003 F2FS_OPTION(sbi).compress_level = 0;
1004 F2FS_OPTION(sbi).compress_algorithm =
1007 f2fs_info(sbi, "kernel doesn't support lzorle compression");
1015 case Opt_compress_log_size:
1016 if (!f2fs_sb_has_compression(sbi)) {
1017 f2fs_info(sbi, "Image doesn't support compression");
1020 if (args->from && match_int(args, &arg))
1022 if (arg < MIN_COMPRESS_LOG_SIZE ||
1023 arg > MAX_COMPRESS_LOG_SIZE) {
1025 "Compress cluster log size is out of range");
1028 F2FS_OPTION(sbi).compress_log_size = arg;
1030 case Opt_compress_extension:
1031 if (!f2fs_sb_has_compression(sbi)) {
1032 f2fs_info(sbi, "Image doesn't support compression");
1035 name = match_strdup(&args[0]);
1039 ext = F2FS_OPTION(sbi).extensions;
1040 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1042 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1043 ext_cnt >= COMPRESS_EXT_NUM) {
1045 "invalid extension length/number");
1050 strcpy(ext[ext_cnt], name);
1051 F2FS_OPTION(sbi).compress_ext_cnt++;
1054 case Opt_compress_chksum:
1055 F2FS_OPTION(sbi).compress_chksum = true;
1057 case Opt_compress_mode:
1058 name = match_strdup(&args[0]);
1061 if (!strcmp(name, "fs")) {
1062 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1063 } else if (!strcmp(name, "user")) {
1064 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1071 case Opt_compress_cache:
1072 set_opt(sbi, COMPRESS_CACHE);
1075 case Opt_compress_algorithm:
1076 case Opt_compress_log_size:
1077 case Opt_compress_extension:
1078 case Opt_compress_chksum:
1079 case Opt_compress_mode:
1080 case Opt_compress_cache:
1081 f2fs_info(sbi, "compression options not supported");
1088 set_opt(sbi, GC_MERGE);
1090 case Opt_nogc_merge:
1091 clear_opt(sbi, GC_MERGE);
1094 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1101 if (f2fs_check_quota_options(sbi))
1104 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
1105 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1108 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
1109 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
1113 #ifndef CONFIG_UNICODE
1114 if (f2fs_sb_has_casefold(sbi)) {
1116 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1121 * The BLKZONED feature indicates that the drive was formatted with
1122 * zone alignment optimization. This is optional for host-aware
1123 * devices, but mandatory for host-managed zoned block devices.
1125 #ifndef CONFIG_BLK_DEV_ZONED
1126 if (f2fs_sb_has_blkzoned(sbi)) {
1127 f2fs_err(sbi, "Zoned block device support is not enabled");
1132 if (F2FS_IO_SIZE_BITS(sbi) && !f2fs_lfs_mode(sbi)) {
1133 f2fs_err(sbi, "Should set mode=lfs with %uKB-sized IO",
1134 F2FS_IO_SIZE_KB(sbi));
1138 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
1139 int min_size, max_size;
1141 if (!f2fs_sb_has_extra_attr(sbi) ||
1142 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
1143 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1146 if (!test_opt(sbi, INLINE_XATTR)) {
1147 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1151 min_size = sizeof(struct f2fs_xattr_header) / sizeof(__le32);
1152 max_size = MAX_INLINE_XATTR_SIZE;
1154 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1155 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
1156 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1157 min_size, max_size);
1162 if (test_opt(sbi, DISABLE_CHECKPOINT) && f2fs_lfs_mode(sbi)) {
1163 f2fs_err(sbi, "LFS not compatible with checkpoint=disable");
1167 /* Not pass down write hints if the number of active logs is lesser
1168 * than NR_CURSEG_PERSIST_TYPE.
1170 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
1171 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1173 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1174 f2fs_err(sbi, "Allow to mount readonly mode only");
1180 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1182 struct f2fs_inode_info *fi;
1184 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
1188 init_once((void *) fi);
1190 /* Initialize f2fs-specific inode info */
1191 atomic_set(&fi->dirty_pages, 0);
1192 atomic_set(&fi->i_compr_blocks, 0);
1193 init_rwsem(&fi->i_sem);
1194 spin_lock_init(&fi->i_size_lock);
1195 INIT_LIST_HEAD(&fi->dirty_list);
1196 INIT_LIST_HEAD(&fi->gdirty_list);
1197 INIT_LIST_HEAD(&fi->inmem_ilist);
1198 INIT_LIST_HEAD(&fi->inmem_pages);
1199 mutex_init(&fi->inmem_lock);
1200 init_rwsem(&fi->i_gc_rwsem[READ]);
1201 init_rwsem(&fi->i_gc_rwsem[WRITE]);
1202 init_rwsem(&fi->i_mmap_sem);
1203 init_rwsem(&fi->i_xattr_sem);
1205 /* Will be used by directory only */
1206 fi->i_dir_level = F2FS_SB(sb)->dir_level;
1208 return &fi->vfs_inode;
1211 static int f2fs_drop_inode(struct inode *inode)
1213 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1217 * during filesystem shutdown, if checkpoint is disabled,
1218 * drop useless meta/node dirty pages.
1220 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1221 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1222 inode->i_ino == F2FS_META_INO(sbi)) {
1223 trace_f2fs_drop_inode(inode, 1);
1229 * This is to avoid a deadlock condition like below.
1230 * writeback_single_inode(inode)
1231 * - f2fs_write_data_page
1232 * - f2fs_gc -> iput -> evict
1233 * - inode_wait_for_writeback(inode)
1235 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
1236 if (!inode->i_nlink && !is_bad_inode(inode)) {
1237 /* to avoid evict_inode call simultaneously */
1238 atomic_inc(&inode->i_count);
1239 spin_unlock(&inode->i_lock);
1241 /* some remained atomic pages should discarded */
1242 if (f2fs_is_atomic_file(inode))
1243 f2fs_drop_inmem_pages(inode);
1245 /* should remain fi->extent_tree for writepage */
1246 f2fs_destroy_extent_node(inode);
1248 sb_start_intwrite(inode->i_sb);
1249 f2fs_i_size_write(inode, 0);
1251 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1252 inode, NULL, 0, DATA);
1253 truncate_inode_pages_final(inode->i_mapping);
1255 if (F2FS_HAS_BLOCKS(inode))
1256 f2fs_truncate(inode);
1258 sb_end_intwrite(inode->i_sb);
1260 spin_lock(&inode->i_lock);
1261 atomic_dec(&inode->i_count);
1263 trace_f2fs_drop_inode(inode, 0);
1266 ret = generic_drop_inode(inode);
1268 ret = fscrypt_drop_inode(inode);
1269 trace_f2fs_drop_inode(inode, ret);
1273 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1275 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1278 spin_lock(&sbi->inode_lock[DIRTY_META]);
1279 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1282 set_inode_flag(inode, FI_DIRTY_INODE);
1283 stat_inc_dirty_inode(sbi, DIRTY_META);
1285 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1286 list_add_tail(&F2FS_I(inode)->gdirty_list,
1287 &sbi->inode_list[DIRTY_META]);
1288 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1290 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1294 void f2fs_inode_synced(struct inode *inode)
1296 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1298 spin_lock(&sbi->inode_lock[DIRTY_META]);
1299 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1300 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1303 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1304 list_del_init(&F2FS_I(inode)->gdirty_list);
1305 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1307 clear_inode_flag(inode, FI_DIRTY_INODE);
1308 clear_inode_flag(inode, FI_AUTO_RECOVER);
1309 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1310 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1314 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1316 * We should call set_dirty_inode to write the dirty inode through write_inode.
1318 static void f2fs_dirty_inode(struct inode *inode, int flags)
1320 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1322 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1323 inode->i_ino == F2FS_META_INO(sbi))
1326 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1327 clear_inode_flag(inode, FI_AUTO_RECOVER);
1329 f2fs_inode_dirtied(inode, false);
1332 static void f2fs_free_inode(struct inode *inode)
1334 fscrypt_free_inode(inode);
1335 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1338 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1340 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1341 percpu_counter_destroy(&sbi->total_valid_inode_count);
1344 static void destroy_device_list(struct f2fs_sb_info *sbi)
1348 for (i = 0; i < sbi->s_ndevs; i++) {
1349 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
1350 #ifdef CONFIG_BLK_DEV_ZONED
1351 kvfree(FDEV(i).blkz_seq);
1352 kfree(FDEV(i).zone_capacity_blocks);
1358 static void f2fs_put_super(struct super_block *sb)
1360 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1364 /* unregister procfs/sysfs entries in advance to avoid race case */
1365 f2fs_unregister_sysfs(sbi);
1367 f2fs_quota_off_umount(sb);
1369 /* prevent remaining shrinker jobs */
1370 mutex_lock(&sbi->umount_mutex);
1373 * flush all issued checkpoints and stop checkpoint issue thread.
1374 * after then, all checkpoints should be done by each process context.
1376 f2fs_stop_ckpt_thread(sbi);
1379 * We don't need to do checkpoint when superblock is clean.
1380 * But, the previous checkpoint was not done by umount, it needs to do
1381 * clean checkpoint again.
1383 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1384 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1385 struct cp_control cpc = {
1386 .reason = CP_UMOUNT,
1388 f2fs_write_checkpoint(sbi, &cpc);
1391 /* be sure to wait for any on-going discard commands */
1392 dropped = f2fs_issue_discard_timeout(sbi);
1394 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1395 !sbi->discard_blks && !dropped) {
1396 struct cp_control cpc = {
1397 .reason = CP_UMOUNT | CP_TRIMMED,
1399 f2fs_write_checkpoint(sbi, &cpc);
1403 * normally superblock is clean, so we need to release this.
1404 * In addition, EIO will skip do checkpoint, we need this as well.
1406 f2fs_release_ino_entry(sbi, true);
1408 f2fs_leave_shrinker(sbi);
1409 mutex_unlock(&sbi->umount_mutex);
1411 /* our cp_error case, we can wait for any writeback page */
1412 f2fs_flush_merged_writes(sbi);
1414 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1416 f2fs_bug_on(sbi, sbi->fsync_node_num);
1418 f2fs_destroy_compress_inode(sbi);
1420 iput(sbi->node_inode);
1421 sbi->node_inode = NULL;
1423 iput(sbi->meta_inode);
1424 sbi->meta_inode = NULL;
1427 * iput() can update stat information, if f2fs_write_checkpoint()
1428 * above failed with error.
1430 f2fs_destroy_stats(sbi);
1432 /* destroy f2fs internal modules */
1433 f2fs_destroy_node_manager(sbi);
1434 f2fs_destroy_segment_manager(sbi);
1436 f2fs_destroy_post_read_wq(sbi);
1440 sb->s_fs_info = NULL;
1441 if (sbi->s_chksum_driver)
1442 crypto_free_shash(sbi->s_chksum_driver);
1443 kfree(sbi->raw_super);
1445 destroy_device_list(sbi);
1446 f2fs_destroy_page_array_cache(sbi);
1447 f2fs_destroy_xattr_caches(sbi);
1448 mempool_destroy(sbi->write_io_dummy);
1450 for (i = 0; i < MAXQUOTAS; i++)
1451 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1453 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1454 destroy_percpu_info(sbi);
1455 for (i = 0; i < NR_PAGE_TYPE; i++)
1456 kvfree(sbi->write_io[i]);
1457 #ifdef CONFIG_UNICODE
1458 utf8_unload(sb->s_encoding);
1463 int f2fs_sync_fs(struct super_block *sb, int sync)
1465 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1468 if (unlikely(f2fs_cp_error(sbi)))
1470 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1473 trace_f2fs_sync_fs(sb, sync);
1475 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1479 err = f2fs_issue_checkpoint(sbi);
1484 static int f2fs_freeze(struct super_block *sb)
1486 if (f2fs_readonly(sb))
1489 /* IO error happened before */
1490 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1493 /* must be clean, since sync_filesystem() was already called */
1494 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1497 /* ensure no checkpoint required */
1498 if (!llist_empty(&F2FS_SB(sb)->cprc_info.issue_list))
1503 static int f2fs_unfreeze(struct super_block *sb)
1509 static int f2fs_statfs_project(struct super_block *sb,
1510 kprojid_t projid, struct kstatfs *buf)
1513 struct dquot *dquot;
1517 qid = make_kqid_projid(projid);
1518 dquot = dqget(sb, qid);
1520 return PTR_ERR(dquot);
1521 spin_lock(&dquot->dq_dqb_lock);
1523 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1524 dquot->dq_dqb.dqb_bhardlimit);
1526 limit >>= sb->s_blocksize_bits;
1528 if (limit && buf->f_blocks > limit) {
1529 curblock = (dquot->dq_dqb.dqb_curspace +
1530 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1531 buf->f_blocks = limit;
1532 buf->f_bfree = buf->f_bavail =
1533 (buf->f_blocks > curblock) ?
1534 (buf->f_blocks - curblock) : 0;
1537 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1538 dquot->dq_dqb.dqb_ihardlimit);
1540 if (limit && buf->f_files > limit) {
1541 buf->f_files = limit;
1543 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1544 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1547 spin_unlock(&dquot->dq_dqb_lock);
1553 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1555 struct super_block *sb = dentry->d_sb;
1556 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1557 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1558 block_t total_count, user_block_count, start_count;
1559 u64 avail_node_count;
1561 total_count = le64_to_cpu(sbi->raw_super->block_count);
1562 user_block_count = sbi->user_block_count;
1563 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1564 buf->f_type = F2FS_SUPER_MAGIC;
1565 buf->f_bsize = sbi->blocksize;
1567 buf->f_blocks = total_count - start_count;
1568 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1569 sbi->current_reserved_blocks;
1571 spin_lock(&sbi->stat_lock);
1572 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1575 buf->f_bfree -= sbi->unusable_block_count;
1576 spin_unlock(&sbi->stat_lock);
1578 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1579 buf->f_bavail = buf->f_bfree -
1580 F2FS_OPTION(sbi).root_reserved_blocks;
1584 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1586 if (avail_node_count > user_block_count) {
1587 buf->f_files = user_block_count;
1588 buf->f_ffree = buf->f_bavail;
1590 buf->f_files = avail_node_count;
1591 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1595 buf->f_namelen = F2FS_NAME_LEN;
1596 buf->f_fsid = u64_to_fsid(id);
1599 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1600 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1601 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1607 static inline void f2fs_show_quota_options(struct seq_file *seq,
1608 struct super_block *sb)
1611 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1613 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1616 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1627 seq_printf(seq, ",jqfmt=%s", fmtname);
1630 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1631 seq_show_option(seq, "usrjquota",
1632 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1634 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1635 seq_show_option(seq, "grpjquota",
1636 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1638 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1639 seq_show_option(seq, "prjjquota",
1640 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1644 #ifdef CONFIG_F2FS_FS_COMPRESSION
1645 static inline void f2fs_show_compress_options(struct seq_file *seq,
1646 struct super_block *sb)
1648 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1652 if (!f2fs_sb_has_compression(sbi))
1655 switch (F2FS_OPTION(sbi).compress_algorithm) {
1665 case COMPRESS_LZORLE:
1666 algtype = "lzo-rle";
1669 seq_printf(seq, ",compress_algorithm=%s", algtype);
1671 if (F2FS_OPTION(sbi).compress_level)
1672 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1674 seq_printf(seq, ",compress_log_size=%u",
1675 F2FS_OPTION(sbi).compress_log_size);
1677 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1678 seq_printf(seq, ",compress_extension=%s",
1679 F2FS_OPTION(sbi).extensions[i]);
1682 if (F2FS_OPTION(sbi).compress_chksum)
1683 seq_puts(seq, ",compress_chksum");
1685 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1686 seq_printf(seq, ",compress_mode=%s", "fs");
1687 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1688 seq_printf(seq, ",compress_mode=%s", "user");
1690 if (test_opt(sbi, COMPRESS_CACHE))
1691 seq_puts(seq, ",compress_cache");
1695 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1697 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1699 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1700 seq_printf(seq, ",background_gc=%s", "sync");
1701 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1702 seq_printf(seq, ",background_gc=%s", "on");
1703 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
1704 seq_printf(seq, ",background_gc=%s", "off");
1706 if (test_opt(sbi, GC_MERGE))
1707 seq_puts(seq, ",gc_merge");
1709 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1710 seq_puts(seq, ",disable_roll_forward");
1711 if (test_opt(sbi, NORECOVERY))
1712 seq_puts(seq, ",norecovery");
1713 if (test_opt(sbi, DISCARD))
1714 seq_puts(seq, ",discard");
1716 seq_puts(seq, ",nodiscard");
1717 if (test_opt(sbi, NOHEAP))
1718 seq_puts(seq, ",no_heap");
1720 seq_puts(seq, ",heap");
1721 #ifdef CONFIG_F2FS_FS_XATTR
1722 if (test_opt(sbi, XATTR_USER))
1723 seq_puts(seq, ",user_xattr");
1725 seq_puts(seq, ",nouser_xattr");
1726 if (test_opt(sbi, INLINE_XATTR))
1727 seq_puts(seq, ",inline_xattr");
1729 seq_puts(seq, ",noinline_xattr");
1730 if (test_opt(sbi, INLINE_XATTR_SIZE))
1731 seq_printf(seq, ",inline_xattr_size=%u",
1732 F2FS_OPTION(sbi).inline_xattr_size);
1734 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1735 if (test_opt(sbi, POSIX_ACL))
1736 seq_puts(seq, ",acl");
1738 seq_puts(seq, ",noacl");
1740 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1741 seq_puts(seq, ",disable_ext_identify");
1742 if (test_opt(sbi, INLINE_DATA))
1743 seq_puts(seq, ",inline_data");
1745 seq_puts(seq, ",noinline_data");
1746 if (test_opt(sbi, INLINE_DENTRY))
1747 seq_puts(seq, ",inline_dentry");
1749 seq_puts(seq, ",noinline_dentry");
1750 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
1751 seq_puts(seq, ",flush_merge");
1752 if (test_opt(sbi, NOBARRIER))
1753 seq_puts(seq, ",nobarrier");
1754 if (test_opt(sbi, FASTBOOT))
1755 seq_puts(seq, ",fastboot");
1756 if (test_opt(sbi, EXTENT_CACHE))
1757 seq_puts(seq, ",extent_cache");
1759 seq_puts(seq, ",noextent_cache");
1760 if (test_opt(sbi, DATA_FLUSH))
1761 seq_puts(seq, ",data_flush");
1763 seq_puts(seq, ",mode=");
1764 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
1765 seq_puts(seq, "adaptive");
1766 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
1767 seq_puts(seq, "lfs");
1768 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
1769 if (test_opt(sbi, RESERVE_ROOT))
1770 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1771 F2FS_OPTION(sbi).root_reserved_blocks,
1772 from_kuid_munged(&init_user_ns,
1773 F2FS_OPTION(sbi).s_resuid),
1774 from_kgid_munged(&init_user_ns,
1775 F2FS_OPTION(sbi).s_resgid));
1776 if (F2FS_IO_SIZE_BITS(sbi))
1777 seq_printf(seq, ",io_bits=%u",
1778 F2FS_OPTION(sbi).write_io_size_bits);
1779 #ifdef CONFIG_F2FS_FAULT_INJECTION
1780 if (test_opt(sbi, FAULT_INJECTION)) {
1781 seq_printf(seq, ",fault_injection=%u",
1782 F2FS_OPTION(sbi).fault_info.inject_rate);
1783 seq_printf(seq, ",fault_type=%u",
1784 F2FS_OPTION(sbi).fault_info.inject_type);
1788 if (test_opt(sbi, QUOTA))
1789 seq_puts(seq, ",quota");
1790 if (test_opt(sbi, USRQUOTA))
1791 seq_puts(seq, ",usrquota");
1792 if (test_opt(sbi, GRPQUOTA))
1793 seq_puts(seq, ",grpquota");
1794 if (test_opt(sbi, PRJQUOTA))
1795 seq_puts(seq, ",prjquota");
1797 f2fs_show_quota_options(seq, sbi->sb);
1798 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
1799 seq_printf(seq, ",whint_mode=%s", "user-based");
1800 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
1801 seq_printf(seq, ",whint_mode=%s", "fs-based");
1803 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
1805 if (sbi->sb->s_flags & SB_INLINECRYPT)
1806 seq_puts(seq, ",inlinecrypt");
1808 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
1809 seq_printf(seq, ",alloc_mode=%s", "default");
1810 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
1811 seq_printf(seq, ",alloc_mode=%s", "reuse");
1813 if (test_opt(sbi, DISABLE_CHECKPOINT))
1814 seq_printf(seq, ",checkpoint=disable:%u",
1815 F2FS_OPTION(sbi).unusable_cap);
1816 if (test_opt(sbi, MERGE_CHECKPOINT))
1817 seq_puts(seq, ",checkpoint_merge");
1819 seq_puts(seq, ",nocheckpoint_merge");
1820 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
1821 seq_printf(seq, ",fsync_mode=%s", "posix");
1822 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
1823 seq_printf(seq, ",fsync_mode=%s", "strict");
1824 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1825 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
1827 #ifdef CONFIG_F2FS_FS_COMPRESSION
1828 f2fs_show_compress_options(seq, sbi->sb);
1831 if (test_opt(sbi, ATGC))
1832 seq_puts(seq, ",atgc");
1836 static void default_options(struct f2fs_sb_info *sbi)
1838 /* init some FS parameters */
1839 if (f2fs_sb_has_readonly(sbi))
1840 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
1842 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
1844 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1845 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1846 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1847 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1848 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1849 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
1850 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
1851 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
1852 F2FS_OPTION(sbi).compress_ext_cnt = 0;
1853 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1854 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
1856 sbi->sb->s_flags &= ~SB_INLINECRYPT;
1858 set_opt(sbi, INLINE_XATTR);
1859 set_opt(sbi, INLINE_DATA);
1860 set_opt(sbi, INLINE_DENTRY);
1861 set_opt(sbi, EXTENT_CACHE);
1862 set_opt(sbi, NOHEAP);
1863 clear_opt(sbi, DISABLE_CHECKPOINT);
1864 set_opt(sbi, MERGE_CHECKPOINT);
1865 F2FS_OPTION(sbi).unusable_cap = 0;
1866 sbi->sb->s_flags |= SB_LAZYTIME;
1867 set_opt(sbi, FLUSH_MERGE);
1868 set_opt(sbi, DISCARD);
1869 if (f2fs_sb_has_blkzoned(sbi))
1870 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
1872 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
1874 #ifdef CONFIG_F2FS_FS_XATTR
1875 set_opt(sbi, XATTR_USER);
1877 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1878 set_opt(sbi, POSIX_ACL);
1881 f2fs_build_fault_attr(sbi, 0, 0);
1885 static int f2fs_enable_quotas(struct super_block *sb);
1888 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
1890 unsigned int s_flags = sbi->sb->s_flags;
1891 struct cp_control cpc;
1896 if (s_flags & SB_RDONLY) {
1897 f2fs_err(sbi, "checkpoint=disable on readonly fs");
1900 sbi->sb->s_flags |= SB_ACTIVE;
1902 f2fs_update_time(sbi, DISABLE_TIME);
1904 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
1905 down_write(&sbi->gc_lock);
1906 err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
1907 if (err == -ENODATA) {
1911 if (err && err != -EAGAIN)
1915 ret = sync_filesystem(sbi->sb);
1917 err = ret ? ret : err;
1921 unusable = f2fs_get_unusable_blocks(sbi);
1922 if (f2fs_disable_cp_again(sbi, unusable)) {
1927 down_write(&sbi->gc_lock);
1928 cpc.reason = CP_PAUSE;
1929 set_sbi_flag(sbi, SBI_CP_DISABLED);
1930 err = f2fs_write_checkpoint(sbi, &cpc);
1934 spin_lock(&sbi->stat_lock);
1935 sbi->unusable_block_count = unusable;
1936 spin_unlock(&sbi->stat_lock);
1939 up_write(&sbi->gc_lock);
1941 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
1945 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
1947 /* we should flush all the data to keep data consistency */
1948 sync_inodes_sb(sbi->sb);
1950 down_write(&sbi->gc_lock);
1951 f2fs_dirty_to_prefree(sbi);
1953 clear_sbi_flag(sbi, SBI_CP_DISABLED);
1954 set_sbi_flag(sbi, SBI_IS_DIRTY);
1955 up_write(&sbi->gc_lock);
1957 f2fs_sync_fs(sbi->sb, 1);
1960 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1962 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1963 struct f2fs_mount_info org_mount_opt;
1964 unsigned long old_sb_flags;
1966 bool need_restart_gc = false, need_stop_gc = false;
1967 bool need_restart_ckpt = false, need_stop_ckpt = false;
1968 bool need_restart_flush = false, need_stop_flush = false;
1969 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
1970 bool disable_checkpoint = test_opt(sbi, DISABLE_CHECKPOINT);
1971 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
1972 bool no_atgc = !test_opt(sbi, ATGC);
1973 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
1974 bool checkpoint_changed;
1980 * Save the old mount options in case we
1981 * need to restore them.
1983 org_mount_opt = sbi->mount_opt;
1984 old_sb_flags = sb->s_flags;
1987 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
1988 for (i = 0; i < MAXQUOTAS; i++) {
1989 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1990 org_mount_opt.s_qf_names[i] =
1991 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1993 if (!org_mount_opt.s_qf_names[i]) {
1994 for (j = 0; j < i; j++)
1995 kfree(org_mount_opt.s_qf_names[j]);
1999 org_mount_opt.s_qf_names[i] = NULL;
2004 /* recover superblocks we couldn't write due to previous RO mount */
2005 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
2006 err = f2fs_commit_super(sbi, false);
2007 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2010 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2013 default_options(sbi);
2015 /* parse mount options */
2016 err = parse_options(sb, data, true);
2019 checkpoint_changed =
2020 disable_checkpoint != test_opt(sbi, DISABLE_CHECKPOINT);
2023 * Previous and new state of filesystem is RO,
2024 * so skip checking GC and FLUSH_MERGE conditions.
2026 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
2029 if (f2fs_sb_has_readonly(sbi) && !(*flags & SB_RDONLY)) {
2035 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
2036 err = dquot_suspend(sb, -1);
2039 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
2040 /* dquot_resume needs RW */
2041 sb->s_flags &= ~SB_RDONLY;
2042 if (sb_any_quota_suspended(sb)) {
2043 dquot_resume(sb, -1);
2044 } else if (f2fs_sb_has_quota_ino(sbi)) {
2045 err = f2fs_enable_quotas(sb);
2051 /* disallow enable atgc dynamically */
2052 if (no_atgc == !!test_opt(sbi, ATGC)) {
2054 f2fs_warn(sbi, "switch atgc option is not allowed");
2058 /* disallow enable/disable extent_cache dynamically */
2059 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
2061 f2fs_warn(sbi, "switch extent_cache option is not allowed");
2065 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
2067 f2fs_warn(sbi, "switch io_bits option is not allowed");
2071 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2073 f2fs_warn(sbi, "switch compress_cache option is not allowed");
2077 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2079 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
2084 * We stop the GC thread if FS is mounted as RO
2085 * or if background_gc = off is passed in mount
2086 * option. Also sync the filesystem.
2088 if ((*flags & SB_RDONLY) ||
2089 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2090 !test_opt(sbi, GC_MERGE))) {
2091 if (sbi->gc_thread) {
2092 f2fs_stop_gc_thread(sbi);
2093 need_restart_gc = true;
2095 } else if (!sbi->gc_thread) {
2096 err = f2fs_start_gc_thread(sbi);
2099 need_stop_gc = true;
2102 if (*flags & SB_RDONLY ||
2103 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
2106 set_sbi_flag(sbi, SBI_IS_DIRTY);
2107 set_sbi_flag(sbi, SBI_IS_CLOSE);
2108 f2fs_sync_fs(sb, 1);
2109 clear_sbi_flag(sbi, SBI_IS_CLOSE);
2112 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2113 !test_opt(sbi, MERGE_CHECKPOINT)) {
2114 f2fs_stop_ckpt_thread(sbi);
2115 need_restart_ckpt = true;
2117 err = f2fs_start_ckpt_thread(sbi);
2120 "Failed to start F2FS issue_checkpoint_thread (%d)",
2124 need_stop_ckpt = true;
2128 * We stop issue flush thread if FS is mounted as RO
2129 * or if flush_merge is not passed in mount option.
2131 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2132 clear_opt(sbi, FLUSH_MERGE);
2133 f2fs_destroy_flush_cmd_control(sbi, false);
2134 need_restart_flush = true;
2136 err = f2fs_create_flush_cmd_control(sbi);
2139 need_stop_flush = true;
2142 if (checkpoint_changed) {
2143 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2144 err = f2fs_disable_checkpoint(sbi);
2148 f2fs_enable_checkpoint(sbi);
2154 /* Release old quota file names */
2155 for (i = 0; i < MAXQUOTAS; i++)
2156 kfree(org_mount_opt.s_qf_names[i]);
2158 /* Update the POSIXACL Flag */
2159 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2160 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2162 limit_reserve_root(sbi);
2163 adjust_unusable_cap_perc(sbi);
2164 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
2167 if (need_restart_flush) {
2168 if (f2fs_create_flush_cmd_control(sbi))
2169 f2fs_warn(sbi, "background flush thread has stopped");
2170 } else if (need_stop_flush) {
2171 clear_opt(sbi, FLUSH_MERGE);
2172 f2fs_destroy_flush_cmd_control(sbi, false);
2175 if (need_restart_ckpt) {
2176 if (f2fs_start_ckpt_thread(sbi))
2177 f2fs_warn(sbi, "background ckpt thread has stopped");
2178 } else if (need_stop_ckpt) {
2179 f2fs_stop_ckpt_thread(sbi);
2182 if (need_restart_gc) {
2183 if (f2fs_start_gc_thread(sbi))
2184 f2fs_warn(sbi, "background gc thread has stopped");
2185 } else if (need_stop_gc) {
2186 f2fs_stop_gc_thread(sbi);
2190 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
2191 for (i = 0; i < MAXQUOTAS; i++) {
2192 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2193 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
2196 sbi->mount_opt = org_mount_opt;
2197 sb->s_flags = old_sb_flags;
2202 /* Read data from quotafile */
2203 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2204 size_t len, loff_t off)
2206 struct inode *inode = sb_dqopt(sb)->files[type];
2207 struct address_space *mapping = inode->i_mapping;
2208 block_t blkidx = F2FS_BYTES_TO_BLK(off);
2209 int offset = off & (sb->s_blocksize - 1);
2212 loff_t i_size = i_size_read(inode);
2219 if (off + len > i_size)
2222 while (toread > 0) {
2223 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2225 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
2227 if (PTR_ERR(page) == -ENOMEM) {
2228 congestion_wait(BLK_RW_ASYNC,
2229 DEFAULT_IO_TIMEOUT);
2232 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2233 return PTR_ERR(page);
2238 if (unlikely(page->mapping != mapping)) {
2239 f2fs_put_page(page, 1);
2242 if (unlikely(!PageUptodate(page))) {
2243 f2fs_put_page(page, 1);
2244 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2248 kaddr = kmap_atomic(page);
2249 memcpy(data, kaddr + offset, tocopy);
2250 kunmap_atomic(kaddr);
2251 f2fs_put_page(page, 1);
2261 /* Write to quotafile */
2262 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2263 const char *data, size_t len, loff_t off)
2265 struct inode *inode = sb_dqopt(sb)->files[type];
2266 struct address_space *mapping = inode->i_mapping;
2267 const struct address_space_operations *a_ops = mapping->a_ops;
2268 int offset = off & (sb->s_blocksize - 1);
2269 size_t towrite = len;
2271 void *fsdata = NULL;
2276 while (towrite > 0) {
2277 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2280 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
2282 if (unlikely(err)) {
2283 if (err == -ENOMEM) {
2284 congestion_wait(BLK_RW_ASYNC,
2285 DEFAULT_IO_TIMEOUT);
2288 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2292 kaddr = kmap_atomic(page);
2293 memcpy(kaddr + offset, data, tocopy);
2294 kunmap_atomic(kaddr);
2295 flush_dcache_page(page);
2297 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2308 inode->i_mtime = inode->i_ctime = current_time(inode);
2309 f2fs_mark_inode_dirty_sync(inode, false);
2310 return len - towrite;
2313 static struct dquot **f2fs_get_dquots(struct inode *inode)
2315 return F2FS_I(inode)->i_dquot;
2318 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2320 return &F2FS_I(inode)->i_reserved_quota;
2323 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2325 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2326 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2330 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2331 F2FS_OPTION(sbi).s_jquota_fmt, type);
2334 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2339 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2340 err = f2fs_enable_quotas(sbi->sb);
2342 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2348 for (i = 0; i < MAXQUOTAS; i++) {
2349 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2350 err = f2fs_quota_on_mount(sbi, i);
2355 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2362 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2365 struct inode *qf_inode;
2366 unsigned long qf_inum;
2369 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2371 qf_inum = f2fs_qf_ino(sb, type);
2375 qf_inode = f2fs_iget(sb, qf_inum);
2376 if (IS_ERR(qf_inode)) {
2377 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2378 return PTR_ERR(qf_inode);
2381 /* Don't account quota for quota files to avoid recursion */
2382 qf_inode->i_flags |= S_NOQUOTA;
2383 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2388 static int f2fs_enable_quotas(struct super_block *sb)
2390 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2392 unsigned long qf_inum;
2393 bool quota_mopt[MAXQUOTAS] = {
2394 test_opt(sbi, USRQUOTA),
2395 test_opt(sbi, GRPQUOTA),
2396 test_opt(sbi, PRJQUOTA),
2399 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2400 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2404 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2406 for (type = 0; type < MAXQUOTAS; type++) {
2407 qf_inum = f2fs_qf_ino(sb, type);
2409 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2410 DQUOT_USAGE_ENABLED |
2411 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2413 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2415 for (type--; type >= 0; type--)
2416 dquot_quota_off(sb, type);
2417 set_sbi_flag(F2FS_SB(sb),
2418 SBI_QUOTA_NEED_REPAIR);
2426 int f2fs_quota_sync(struct super_block *sb, int type)
2428 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2429 struct quota_info *dqopt = sb_dqopt(sb);
2436 * down_read(quota_sem)
2437 * dquot_writeback_dquots()
2440 * down_read(quota_sem)
2444 down_read(&sbi->quota_sem);
2445 ret = dquot_writeback_dquots(sb, type);
2450 * Now when everything is written we can discard the pagecache so
2451 * that userspace sees the changes.
2453 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2454 struct address_space *mapping;
2456 if (type != -1 && cnt != type)
2458 if (!sb_has_quota_active(sb, cnt))
2461 mapping = dqopt->files[cnt]->i_mapping;
2463 ret = filemap_fdatawrite(mapping);
2467 /* if we are using journalled quota */
2468 if (is_journalled_quota(sbi))
2471 ret = filemap_fdatawait(mapping);
2473 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2475 inode_lock(dqopt->files[cnt]);
2476 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2477 inode_unlock(dqopt->files[cnt]);
2481 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2482 up_read(&sbi->quota_sem);
2483 f2fs_unlock_op(sbi);
2487 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2488 const struct path *path)
2490 struct inode *inode;
2493 /* if quota sysfile exists, deny enabling quota with specific file */
2494 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2495 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2499 err = f2fs_quota_sync(sb, type);
2503 err = dquot_quota_on(sb, type, format_id, path);
2507 inode = d_inode(path->dentry);
2510 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
2511 f2fs_set_inode_flags(inode);
2512 inode_unlock(inode);
2513 f2fs_mark_inode_dirty_sync(inode, false);
2518 static int __f2fs_quota_off(struct super_block *sb, int type)
2520 struct inode *inode = sb_dqopt(sb)->files[type];
2523 if (!inode || !igrab(inode))
2524 return dquot_quota_off(sb, type);
2526 err = f2fs_quota_sync(sb, type);
2530 err = dquot_quota_off(sb, type);
2531 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
2535 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
2536 f2fs_set_inode_flags(inode);
2537 inode_unlock(inode);
2538 f2fs_mark_inode_dirty_sync(inode, false);
2544 static int f2fs_quota_off(struct super_block *sb, int type)
2546 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2549 err = __f2fs_quota_off(sb, type);
2552 * quotactl can shutdown journalled quota, result in inconsistence
2553 * between quota record and fs data by following updates, tag the
2554 * flag to let fsck be aware of it.
2556 if (is_journalled_quota(sbi))
2557 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2561 void f2fs_quota_off_umount(struct super_block *sb)
2566 for (type = 0; type < MAXQUOTAS; type++) {
2567 err = __f2fs_quota_off(sb, type);
2569 int ret = dquot_quota_off(sb, type);
2571 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
2573 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2577 * In case of checkpoint=disable, we must flush quota blocks.
2578 * This can cause NULL exception for node_inode in end_io, since
2579 * put_super already dropped it.
2581 sync_filesystem(sb);
2584 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
2586 struct quota_info *dqopt = sb_dqopt(sb);
2589 for (type = 0; type < MAXQUOTAS; type++) {
2590 if (!dqopt->files[type])
2592 f2fs_inode_synced(dqopt->files[type]);
2596 static int f2fs_dquot_commit(struct dquot *dquot)
2598 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2601 down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
2602 ret = dquot_commit(dquot);
2604 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2605 up_read(&sbi->quota_sem);
2609 static int f2fs_dquot_acquire(struct dquot *dquot)
2611 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2614 down_read(&sbi->quota_sem);
2615 ret = dquot_acquire(dquot);
2617 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2618 up_read(&sbi->quota_sem);
2622 static int f2fs_dquot_release(struct dquot *dquot)
2624 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2625 int ret = dquot_release(dquot);
2628 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2632 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
2634 struct super_block *sb = dquot->dq_sb;
2635 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2636 int ret = dquot_mark_dquot_dirty(dquot);
2638 /* if we are using journalled quota */
2639 if (is_journalled_quota(sbi))
2640 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
2645 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
2647 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2648 int ret = dquot_commit_info(sb, type);
2651 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2655 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
2657 *projid = F2FS_I(inode)->i_projid;
2661 static const struct dquot_operations f2fs_quota_operations = {
2662 .get_reserved_space = f2fs_get_reserved_space,
2663 .write_dquot = f2fs_dquot_commit,
2664 .acquire_dquot = f2fs_dquot_acquire,
2665 .release_dquot = f2fs_dquot_release,
2666 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
2667 .write_info = f2fs_dquot_commit_info,
2668 .alloc_dquot = dquot_alloc,
2669 .destroy_dquot = dquot_destroy,
2670 .get_projid = f2fs_get_projid,
2671 .get_next_id = dquot_get_next_id,
2674 static const struct quotactl_ops f2fs_quotactl_ops = {
2675 .quota_on = f2fs_quota_on,
2676 .quota_off = f2fs_quota_off,
2677 .quota_sync = f2fs_quota_sync,
2678 .get_state = dquot_get_state,
2679 .set_info = dquot_set_dqinfo,
2680 .get_dqblk = dquot_get_dqblk,
2681 .set_dqblk = dquot_set_dqblk,
2682 .get_nextdqblk = dquot_get_next_dqblk,
2685 int f2fs_quota_sync(struct super_block *sb, int type)
2690 void f2fs_quota_off_umount(struct super_block *sb)
2695 static const struct super_operations f2fs_sops = {
2696 .alloc_inode = f2fs_alloc_inode,
2697 .free_inode = f2fs_free_inode,
2698 .drop_inode = f2fs_drop_inode,
2699 .write_inode = f2fs_write_inode,
2700 .dirty_inode = f2fs_dirty_inode,
2701 .show_options = f2fs_show_options,
2703 .quota_read = f2fs_quota_read,
2704 .quota_write = f2fs_quota_write,
2705 .get_dquots = f2fs_get_dquots,
2707 .evict_inode = f2fs_evict_inode,
2708 .put_super = f2fs_put_super,
2709 .sync_fs = f2fs_sync_fs,
2710 .freeze_fs = f2fs_freeze,
2711 .unfreeze_fs = f2fs_unfreeze,
2712 .statfs = f2fs_statfs,
2713 .remount_fs = f2fs_remount,
2716 #ifdef CONFIG_FS_ENCRYPTION
2717 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
2719 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2720 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2724 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
2727 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2730 * Encrypting the root directory is not allowed because fsck
2731 * expects lost+found directory to exist and remain unencrypted
2732 * if LOST_FOUND feature is enabled.
2735 if (f2fs_sb_has_lost_found(sbi) &&
2736 inode->i_ino == F2FS_ROOT_INO(sbi))
2739 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2740 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2741 ctx, len, fs_data, XATTR_CREATE);
2744 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
2746 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
2749 static bool f2fs_has_stable_inodes(struct super_block *sb)
2754 static void f2fs_get_ino_and_lblk_bits(struct super_block *sb,
2755 int *ino_bits_ret, int *lblk_bits_ret)
2757 *ino_bits_ret = 8 * sizeof(nid_t);
2758 *lblk_bits_ret = 8 * sizeof(block_t);
2761 static int f2fs_get_num_devices(struct super_block *sb)
2763 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2765 if (f2fs_is_multi_device(sbi))
2766 return sbi->s_ndevs;
2770 static void f2fs_get_devices(struct super_block *sb,
2771 struct request_queue **devs)
2773 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2776 for (i = 0; i < sbi->s_ndevs; i++)
2777 devs[i] = bdev_get_queue(FDEV(i).bdev);
2780 static const struct fscrypt_operations f2fs_cryptops = {
2781 .key_prefix = "f2fs:",
2782 .get_context = f2fs_get_context,
2783 .set_context = f2fs_set_context,
2784 .get_dummy_policy = f2fs_get_dummy_policy,
2785 .empty_dir = f2fs_empty_dir,
2786 .max_namelen = F2FS_NAME_LEN,
2787 .has_stable_inodes = f2fs_has_stable_inodes,
2788 .get_ino_and_lblk_bits = f2fs_get_ino_and_lblk_bits,
2789 .get_num_devices = f2fs_get_num_devices,
2790 .get_devices = f2fs_get_devices,
2794 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2795 u64 ino, u32 generation)
2797 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2798 struct inode *inode;
2800 if (f2fs_check_nid_range(sbi, ino))
2801 return ERR_PTR(-ESTALE);
2804 * f2fs_iget isn't quite right if the inode is currently unallocated!
2805 * However f2fs_iget currently does appropriate checks to handle stale
2806 * inodes so everything is OK.
2808 inode = f2fs_iget(sb, ino);
2810 return ERR_CAST(inode);
2811 if (unlikely(generation && inode->i_generation != generation)) {
2812 /* we didn't find the right inode.. */
2814 return ERR_PTR(-ESTALE);
2819 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2820 int fh_len, int fh_type)
2822 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2823 f2fs_nfs_get_inode);
2826 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2827 int fh_len, int fh_type)
2829 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2830 f2fs_nfs_get_inode);
2833 static const struct export_operations f2fs_export_ops = {
2834 .fh_to_dentry = f2fs_fh_to_dentry,
2835 .fh_to_parent = f2fs_fh_to_parent,
2836 .get_parent = f2fs_get_parent,
2839 loff_t max_file_blocks(struct inode *inode)
2845 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
2846 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
2847 * space in inode.i_addr, it will be more safe to reassign
2851 if (inode && f2fs_compressed_file(inode))
2852 leaf_count = ADDRS_PER_BLOCK(inode);
2854 leaf_count = DEF_ADDRS_PER_BLOCK;
2856 /* two direct node blocks */
2857 result += (leaf_count * 2);
2859 /* two indirect node blocks */
2860 leaf_count *= NIDS_PER_BLOCK;
2861 result += (leaf_count * 2);
2863 /* one double indirect node block */
2864 leaf_count *= NIDS_PER_BLOCK;
2865 result += leaf_count;
2870 static int __f2fs_commit_super(struct buffer_head *bh,
2871 struct f2fs_super_block *super)
2875 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
2876 set_buffer_dirty(bh);
2879 /* it's rare case, we can do fua all the time */
2880 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
2883 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
2884 struct buffer_head *bh)
2886 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2887 (bh->b_data + F2FS_SUPER_OFFSET);
2888 struct super_block *sb = sbi->sb;
2889 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2890 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2891 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2892 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2893 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2894 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2895 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2896 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2897 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2898 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2899 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2900 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2901 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2902 u64 main_end_blkaddr = main_blkaddr +
2903 (segment_count_main << log_blocks_per_seg);
2904 u64 seg_end_blkaddr = segment0_blkaddr +
2905 (segment_count << log_blocks_per_seg);
2907 if (segment0_blkaddr != cp_blkaddr) {
2908 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2909 segment0_blkaddr, cp_blkaddr);
2913 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2915 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2916 cp_blkaddr, sit_blkaddr,
2917 segment_count_ckpt << log_blocks_per_seg);
2921 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2923 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2924 sit_blkaddr, nat_blkaddr,
2925 segment_count_sit << log_blocks_per_seg);
2929 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2931 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2932 nat_blkaddr, ssa_blkaddr,
2933 segment_count_nat << log_blocks_per_seg);
2937 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2939 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2940 ssa_blkaddr, main_blkaddr,
2941 segment_count_ssa << log_blocks_per_seg);
2945 if (main_end_blkaddr > seg_end_blkaddr) {
2946 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
2947 main_blkaddr, seg_end_blkaddr,
2948 segment_count_main << log_blocks_per_seg);
2950 } else if (main_end_blkaddr < seg_end_blkaddr) {
2954 /* fix in-memory information all the time */
2955 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2956 segment0_blkaddr) >> log_blocks_per_seg);
2958 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
2959 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2962 err = __f2fs_commit_super(bh, NULL);
2963 res = err ? "failed" : "done";
2965 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
2966 res, main_blkaddr, seg_end_blkaddr,
2967 segment_count_main << log_blocks_per_seg);
2974 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2975 struct buffer_head *bh)
2977 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
2978 block_t total_sections, blocks_per_seg;
2979 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2980 (bh->b_data + F2FS_SUPER_OFFSET);
2981 size_t crc_offset = 0;
2984 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
2985 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
2986 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2990 /* Check checksum_offset and crc in superblock */
2991 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
2992 crc_offset = le32_to_cpu(raw_super->checksum_offset);
2994 offsetof(struct f2fs_super_block, crc)) {
2995 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
2997 return -EFSCORRUPTED;
2999 crc = le32_to_cpu(raw_super->crc);
3000 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
3001 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
3002 return -EFSCORRUPTED;
3006 /* Currently, support only 4KB block size */
3007 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3008 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3009 le32_to_cpu(raw_super->log_blocksize),
3011 return -EFSCORRUPTED;
3014 /* check log blocks per segment */
3015 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
3016 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
3017 le32_to_cpu(raw_super->log_blocks_per_seg));
3018 return -EFSCORRUPTED;
3021 /* Currently, support 512/1024/2048/4096 bytes sector size */
3022 if (le32_to_cpu(raw_super->log_sectorsize) >
3023 F2FS_MAX_LOG_SECTOR_SIZE ||
3024 le32_to_cpu(raw_super->log_sectorsize) <
3025 F2FS_MIN_LOG_SECTOR_SIZE) {
3026 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3027 le32_to_cpu(raw_super->log_sectorsize));
3028 return -EFSCORRUPTED;
3030 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3031 le32_to_cpu(raw_super->log_sectorsize) !=
3032 F2FS_MAX_LOG_SECTOR_SIZE) {
3033 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3034 le32_to_cpu(raw_super->log_sectors_per_block),
3035 le32_to_cpu(raw_super->log_sectorsize));
3036 return -EFSCORRUPTED;
3039 segment_count = le32_to_cpu(raw_super->segment_count);
3040 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3041 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3042 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3043 total_sections = le32_to_cpu(raw_super->section_count);
3045 /* blocks_per_seg should be 512, given the above check */
3046 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
3048 if (segment_count > F2FS_MAX_SEGMENT ||
3049 segment_count < F2FS_MIN_SEGMENTS) {
3050 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
3051 return -EFSCORRUPTED;
3054 if (total_sections > segment_count_main || total_sections < 1 ||
3055 segs_per_sec > segment_count || !segs_per_sec) {
3056 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3057 segment_count, total_sections, segs_per_sec);
3058 return -EFSCORRUPTED;
3061 if (segment_count_main != total_sections * segs_per_sec) {
3062 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3063 segment_count_main, total_sections, segs_per_sec);
3064 return -EFSCORRUPTED;
3067 if ((segment_count / segs_per_sec) < total_sections) {
3068 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3069 segment_count, segs_per_sec, total_sections);
3070 return -EFSCORRUPTED;
3073 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
3074 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3075 segment_count, le64_to_cpu(raw_super->block_count));
3076 return -EFSCORRUPTED;
3079 if (RDEV(0).path[0]) {
3080 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3083 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3084 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3087 if (segment_count != dev_seg_count) {
3088 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3089 segment_count, dev_seg_count);
3090 return -EFSCORRUPTED;
3093 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3094 !bdev_is_zoned(sbi->sb->s_bdev)) {
3095 f2fs_info(sbi, "Zoned block device path is missing");
3096 return -EFSCORRUPTED;
3100 if (secs_per_zone > total_sections || !secs_per_zone) {
3101 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3102 secs_per_zone, total_sections);
3103 return -EFSCORRUPTED;
3105 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3106 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3107 (le32_to_cpu(raw_super->extension_count) +
3108 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
3109 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3110 le32_to_cpu(raw_super->extension_count),
3111 raw_super->hot_ext_count,
3112 F2FS_MAX_EXTENSION);
3113 return -EFSCORRUPTED;
3116 if (le32_to_cpu(raw_super->cp_payload) >
3117 (blocks_per_seg - F2FS_CP_PACKS)) {
3118 f2fs_info(sbi, "Insane cp_payload (%u > %u)",
3119 le32_to_cpu(raw_super->cp_payload),
3120 blocks_per_seg - F2FS_CP_PACKS);
3121 return -EFSCORRUPTED;
3124 /* check reserved ino info */
3125 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3126 le32_to_cpu(raw_super->meta_ino) != 2 ||
3127 le32_to_cpu(raw_super->root_ino) != 3) {
3128 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3129 le32_to_cpu(raw_super->node_ino),
3130 le32_to_cpu(raw_super->meta_ino),
3131 le32_to_cpu(raw_super->root_ino));
3132 return -EFSCORRUPTED;
3135 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
3136 if (sanity_check_area_boundary(sbi, bh))
3137 return -EFSCORRUPTED;
3142 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
3144 unsigned int total, fsmeta;
3145 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3146 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3147 unsigned int ovp_segments, reserved_segments;
3148 unsigned int main_segs, blocks_per_seg;
3149 unsigned int sit_segs, nat_segs;
3150 unsigned int sit_bitmap_size, nat_bitmap_size;
3151 unsigned int log_blocks_per_seg;
3152 unsigned int segment_count_main;
3153 unsigned int cp_pack_start_sum, cp_payload;
3154 block_t user_block_count, valid_user_blocks;
3155 block_t avail_node_count, valid_node_count;
3158 total = le32_to_cpu(raw_super->segment_count);
3159 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
3160 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3162 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3164 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3165 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3167 if (unlikely(fsmeta >= total))
3170 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3171 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3173 if (!f2fs_sb_has_readonly(sbi) &&
3174 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
3175 ovp_segments == 0 || reserved_segments == 0)) {
3176 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
3179 user_block_count = le64_to_cpu(ckpt->user_block_count);
3180 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3181 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
3182 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3183 if (!user_block_count || user_block_count >=
3184 segment_count_main << log_blocks_per_seg) {
3185 f2fs_err(sbi, "Wrong user_block_count: %u",
3190 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3191 if (valid_user_blocks > user_block_count) {
3192 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3193 valid_user_blocks, user_block_count);
3197 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
3198 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
3199 if (valid_node_count > avail_node_count) {
3200 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3201 valid_node_count, avail_node_count);
3205 main_segs = le32_to_cpu(raw_super->segment_count_main);
3206 blocks_per_seg = sbi->blocks_per_seg;
3208 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3209 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3210 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3213 if (f2fs_sb_has_readonly(sbi))
3216 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3217 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3218 le32_to_cpu(ckpt->cur_node_segno[j])) {
3219 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3221 le32_to_cpu(ckpt->cur_node_segno[i]));
3227 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3228 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3229 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3232 if (f2fs_sb_has_readonly(sbi))
3235 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3236 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3237 le32_to_cpu(ckpt->cur_data_segno[j])) {
3238 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3240 le32_to_cpu(ckpt->cur_data_segno[i]));
3245 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3246 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3247 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3248 le32_to_cpu(ckpt->cur_data_segno[j])) {
3249 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3251 le32_to_cpu(ckpt->cur_node_segno[i]));
3257 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3258 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3260 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3261 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3262 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3263 sit_bitmap_size, nat_bitmap_size);
3267 cp_pack_start_sum = __start_sum_addr(sbi);
3268 cp_payload = __cp_payload(sbi);
3269 if (cp_pack_start_sum < cp_payload + 1 ||
3270 cp_pack_start_sum > blocks_per_seg - 1 -
3271 NR_CURSEG_PERSIST_TYPE) {
3272 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3277 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3278 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3279 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3280 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3281 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3282 le32_to_cpu(ckpt->checksum_offset));
3286 if (unlikely(f2fs_cp_error(sbi))) {
3287 f2fs_err(sbi, "A bug case: need to run fsck");
3293 static void init_sb_info(struct f2fs_sb_info *sbi)
3295 struct f2fs_super_block *raw_super = sbi->raw_super;
3298 sbi->log_sectors_per_block =
3299 le32_to_cpu(raw_super->log_sectors_per_block);
3300 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3301 sbi->blocksize = 1 << sbi->log_blocksize;
3302 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3303 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
3304 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3305 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3306 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3307 sbi->total_node_count =
3308 (le32_to_cpu(raw_super->segment_count_nat) / 2)
3309 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
3310 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3311 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3312 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3313 sbi->cur_victim_sec = NULL_SECNO;
3314 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3315 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3316 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3317 sbi->migration_granularity = sbi->segs_per_sec;
3319 sbi->dir_level = DEF_DIR_LEVEL;
3320 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3321 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3322 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3323 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3324 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3325 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3326 DEF_UMOUNT_DISCARD_TIMEOUT;
3327 clear_sbi_flag(sbi, SBI_NEED_FSCK);
3329 for (i = 0; i < NR_COUNT_TYPE; i++)
3330 atomic_set(&sbi->nr_pages[i], 0);
3332 for (i = 0; i < META; i++)
3333 atomic_set(&sbi->wb_sync_req[i], 0);
3335 INIT_LIST_HEAD(&sbi->s_list);
3336 mutex_init(&sbi->umount_mutex);
3337 init_rwsem(&sbi->io_order_lock);
3338 spin_lock_init(&sbi->cp_lock);
3340 sbi->dirty_device = 0;
3341 spin_lock_init(&sbi->dev_lock);
3343 init_rwsem(&sbi->sb_lock);
3344 init_rwsem(&sbi->pin_sem);
3347 static int init_percpu_info(struct f2fs_sb_info *sbi)
3351 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3355 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3358 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3363 #ifdef CONFIG_BLK_DEV_ZONED
3365 struct f2fs_report_zones_args {
3366 struct f2fs_dev_info *dev;
3367 bool zone_cap_mismatch;
3370 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3373 struct f2fs_report_zones_args *rz_args = data;
3375 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3378 set_bit(idx, rz_args->dev->blkz_seq);
3379 rz_args->dev->zone_capacity_blocks[idx] = zone->capacity >>
3380 F2FS_LOG_SECTORS_PER_BLOCK;
3381 if (zone->len != zone->capacity && !rz_args->zone_cap_mismatch)
3382 rz_args->zone_cap_mismatch = true;
3387 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
3389 struct block_device *bdev = FDEV(devi).bdev;
3390 sector_t nr_sectors = bdev_nr_sectors(bdev);
3391 struct f2fs_report_zones_args rep_zone_arg;
3394 if (!f2fs_sb_has_blkzoned(sbi))
3397 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
3398 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
3400 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
3401 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
3402 __ilog2_u32(sbi->blocks_per_blkz))
3404 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
3405 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
3406 sbi->log_blocks_per_blkz;
3407 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
3408 FDEV(devi).nr_blkz++;
3410 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
3411 BITS_TO_LONGS(FDEV(devi).nr_blkz)
3412 * sizeof(unsigned long),
3414 if (!FDEV(devi).blkz_seq)
3417 /* Get block zones type and zone-capacity */
3418 FDEV(devi).zone_capacity_blocks = f2fs_kzalloc(sbi,
3419 FDEV(devi).nr_blkz * sizeof(block_t),
3421 if (!FDEV(devi).zone_capacity_blocks)
3424 rep_zone_arg.dev = &FDEV(devi);
3425 rep_zone_arg.zone_cap_mismatch = false;
3427 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3432 if (!rep_zone_arg.zone_cap_mismatch) {
3433 kfree(FDEV(devi).zone_capacity_blocks);
3434 FDEV(devi).zone_capacity_blocks = NULL;
3442 * Read f2fs raw super block.
3443 * Because we have two copies of super block, so read both of them
3444 * to get the first valid one. If any one of them is broken, we pass
3445 * them recovery flag back to the caller.
3447 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3448 struct f2fs_super_block **raw_super,
3449 int *valid_super_block, int *recovery)
3451 struct super_block *sb = sbi->sb;
3453 struct buffer_head *bh;
3454 struct f2fs_super_block *super;
3457 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3461 for (block = 0; block < 2; block++) {
3462 bh = sb_bread(sb, block);
3464 f2fs_err(sbi, "Unable to read %dth superblock",
3471 /* sanity checking of raw super */
3472 err = sanity_check_raw_super(sbi, bh);
3474 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3482 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3484 *valid_super_block = block;
3490 /* No valid superblock */
3499 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3501 struct buffer_head *bh;
3505 if ((recover && f2fs_readonly(sbi->sb)) ||
3506 bdev_read_only(sbi->sb->s_bdev)) {
3507 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3511 /* we should update superblock crc here */
3512 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
3513 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3514 offsetof(struct f2fs_super_block, crc));
3515 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3518 /* write back-up superblock first */
3519 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
3522 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3525 /* if we are in recovery path, skip writing valid superblock */
3529 /* write current valid superblock */
3530 bh = sb_bread(sbi->sb, sbi->valid_super_block);
3533 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3538 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
3540 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3541 unsigned int max_devices = MAX_DEVICES;
3544 /* Initialize single device information */
3545 if (!RDEV(0).path[0]) {
3546 if (!bdev_is_zoned(sbi->sb->s_bdev))
3552 * Initialize multiple devices information, or single
3553 * zoned block device information.
3555 sbi->devs = f2fs_kzalloc(sbi,
3556 array_size(max_devices,
3557 sizeof(struct f2fs_dev_info)),
3562 for (i = 0; i < max_devices; i++) {
3564 if (i > 0 && !RDEV(i).path[0])
3567 if (max_devices == 1) {
3568 /* Single zoned block device mount */
3570 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
3571 sbi->sb->s_mode, sbi->sb->s_type);
3573 /* Multi-device mount */
3574 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
3575 FDEV(i).total_segments =
3576 le32_to_cpu(RDEV(i).total_segments);
3578 FDEV(i).start_blk = 0;
3579 FDEV(i).end_blk = FDEV(i).start_blk +
3580 (FDEV(i).total_segments <<
3581 sbi->log_blocks_per_seg) - 1 +
3582 le32_to_cpu(raw_super->segment0_blkaddr);
3584 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
3585 FDEV(i).end_blk = FDEV(i).start_blk +
3586 (FDEV(i).total_segments <<
3587 sbi->log_blocks_per_seg) - 1;
3589 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3590 sbi->sb->s_mode, sbi->sb->s_type);
3592 if (IS_ERR(FDEV(i).bdev))
3593 return PTR_ERR(FDEV(i).bdev);
3595 /* to release errored devices */
3596 sbi->s_ndevs = i + 1;
3598 #ifdef CONFIG_BLK_DEV_ZONED
3599 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
3600 !f2fs_sb_has_blkzoned(sbi)) {
3601 f2fs_err(sbi, "Zoned block device feature not enabled");
3604 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
3605 if (init_blkz_info(sbi, i)) {
3606 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
3609 if (max_devices == 1)
3611 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
3613 FDEV(i).total_segments,
3614 FDEV(i).start_blk, FDEV(i).end_blk,
3615 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
3616 "Host-aware" : "Host-managed");
3620 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
3622 FDEV(i).total_segments,
3623 FDEV(i).start_blk, FDEV(i).end_blk);
3626 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
3630 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
3632 #ifdef CONFIG_UNICODE
3633 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
3634 const struct f2fs_sb_encodings *encoding_info;
3635 struct unicode_map *encoding;
3636 __u16 encoding_flags;
3638 if (f2fs_sb_read_encoding(sbi->raw_super, &encoding_info,
3641 "Encoding requested by superblock is unknown");
3645 encoding = utf8_load(encoding_info->version);
3646 if (IS_ERR(encoding)) {
3648 "can't mount with superblock charset: %s-%s "
3649 "not supported by the kernel. flags: 0x%x.",
3650 encoding_info->name, encoding_info->version,
3652 return PTR_ERR(encoding);
3654 f2fs_info(sbi, "Using encoding defined by superblock: "
3655 "%s-%s with flags 0x%hx", encoding_info->name,
3656 encoding_info->version?:"\b", encoding_flags);
3658 sbi->sb->s_encoding = encoding;
3659 sbi->sb->s_encoding_flags = encoding_flags;
3662 if (f2fs_sb_has_casefold(sbi)) {
3663 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
3670 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
3672 struct f2fs_sm_info *sm_i = SM_I(sbi);
3674 /* adjust parameters according to the volume size */
3675 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
3676 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
3677 sm_i->dcc_info->discard_granularity = 1;
3678 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
3681 sbi->readdir_ra = 1;
3684 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
3686 struct f2fs_sb_info *sbi;
3687 struct f2fs_super_block *raw_super;
3690 bool skip_recovery = false, need_fsck = false;
3691 char *options = NULL;
3692 int recovery, i, valid_super_block;
3693 struct curseg_info *seg_i;
3699 valid_super_block = -1;
3702 /* allocate memory for f2fs-specific super block info */
3703 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
3709 /* Load the checksum driver */
3710 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
3711 if (IS_ERR(sbi->s_chksum_driver)) {
3712 f2fs_err(sbi, "Cannot load crc32 driver.");
3713 err = PTR_ERR(sbi->s_chksum_driver);
3714 sbi->s_chksum_driver = NULL;
3718 /* set a block size */
3719 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
3720 f2fs_err(sbi, "unable to set blocksize");
3724 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
3729 sb->s_fs_info = sbi;
3730 sbi->raw_super = raw_super;
3732 /* precompute checksum seed for metadata */
3733 if (f2fs_sb_has_inode_chksum(sbi))
3734 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
3735 sizeof(raw_super->uuid));
3737 default_options(sbi);
3738 /* parse mount options */
3739 options = kstrdup((const char *)data, GFP_KERNEL);
3740 if (data && !options) {
3745 err = parse_options(sb, options, false);
3749 sb->s_maxbytes = max_file_blocks(NULL) <<
3750 le32_to_cpu(raw_super->log_blocksize);
3751 sb->s_max_links = F2FS_LINK_MAX;
3753 err = f2fs_setup_casefold(sbi);
3758 sb->dq_op = &f2fs_quota_operations;
3759 sb->s_qcop = &f2fs_quotactl_ops;
3760 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
3762 if (f2fs_sb_has_quota_ino(sbi)) {
3763 for (i = 0; i < MAXQUOTAS; i++) {
3764 if (f2fs_qf_ino(sbi->sb, i))
3765 sbi->nquota_files++;
3770 sb->s_op = &f2fs_sops;
3771 #ifdef CONFIG_FS_ENCRYPTION
3772 sb->s_cop = &f2fs_cryptops;
3774 #ifdef CONFIG_FS_VERITY
3775 sb->s_vop = &f2fs_verityops;
3777 sb->s_xattr = f2fs_xattr_handlers;
3778 sb->s_export_op = &f2fs_export_ops;
3779 sb->s_magic = F2FS_SUPER_MAGIC;
3780 sb->s_time_gran = 1;
3781 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
3782 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
3783 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
3784 sb->s_iflags |= SB_I_CGROUPWB;
3786 /* init f2fs-specific super block info */
3787 sbi->valid_super_block = valid_super_block;
3788 init_rwsem(&sbi->gc_lock);
3789 mutex_init(&sbi->writepages);
3790 init_rwsem(&sbi->cp_global_sem);
3791 init_rwsem(&sbi->node_write);
3792 init_rwsem(&sbi->node_change);
3794 /* disallow all the data/node/meta page writes */
3795 set_sbi_flag(sbi, SBI_POR_DOING);
3796 spin_lock_init(&sbi->stat_lock);
3798 /* init iostat info */
3799 spin_lock_init(&sbi->iostat_lock);
3800 sbi->iostat_enable = false;
3801 sbi->iostat_period_ms = DEFAULT_IOSTAT_PERIOD_MS;
3803 for (i = 0; i < NR_PAGE_TYPE; i++) {
3804 int n = (i == META) ? 1 : NR_TEMP_TYPE;
3810 sizeof(struct f2fs_bio_info)),
3812 if (!sbi->write_io[i]) {
3817 for (j = HOT; j < n; j++) {
3818 init_rwsem(&sbi->write_io[i][j].io_rwsem);
3819 sbi->write_io[i][j].sbi = sbi;
3820 sbi->write_io[i][j].bio = NULL;
3821 spin_lock_init(&sbi->write_io[i][j].io_lock);
3822 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
3823 INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
3824 init_rwsem(&sbi->write_io[i][j].bio_list_lock);
3828 init_rwsem(&sbi->cp_rwsem);
3829 init_rwsem(&sbi->quota_sem);
3830 init_waitqueue_head(&sbi->cp_wait);
3833 err = init_percpu_info(sbi);
3837 if (F2FS_IO_ALIGNED(sbi)) {
3838 sbi->write_io_dummy =
3839 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
3840 if (!sbi->write_io_dummy) {
3846 /* init per sbi slab cache */
3847 err = f2fs_init_xattr_caches(sbi);
3850 err = f2fs_init_page_array_cache(sbi);
3852 goto free_xattr_cache;
3854 /* get an inode for meta space */
3855 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
3856 if (IS_ERR(sbi->meta_inode)) {
3857 f2fs_err(sbi, "Failed to read F2FS meta data inode");
3858 err = PTR_ERR(sbi->meta_inode);
3859 goto free_page_array_cache;
3862 err = f2fs_get_valid_checkpoint(sbi);
3864 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
3865 goto free_meta_inode;
3868 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
3869 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3870 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
3871 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3872 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
3875 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
3876 set_sbi_flag(sbi, SBI_NEED_FSCK);
3878 /* Initialize device list */
3879 err = f2fs_scan_devices(sbi);
3881 f2fs_err(sbi, "Failed to find devices");
3885 err = f2fs_init_post_read_wq(sbi);
3887 f2fs_err(sbi, "Failed to initialize post read workqueue");
3891 sbi->total_valid_node_count =
3892 le32_to_cpu(sbi->ckpt->valid_node_count);
3893 percpu_counter_set(&sbi->total_valid_inode_count,
3894 le32_to_cpu(sbi->ckpt->valid_inode_count));
3895 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
3896 sbi->total_valid_block_count =
3897 le64_to_cpu(sbi->ckpt->valid_block_count);
3898 sbi->last_valid_block_count = sbi->total_valid_block_count;
3899 sbi->reserved_blocks = 0;
3900 sbi->current_reserved_blocks = 0;
3901 limit_reserve_root(sbi);
3902 adjust_unusable_cap_perc(sbi);
3904 for (i = 0; i < NR_INODE_TYPE; i++) {
3905 INIT_LIST_HEAD(&sbi->inode_list[i]);
3906 spin_lock_init(&sbi->inode_lock[i]);
3908 mutex_init(&sbi->flush_lock);
3910 f2fs_init_extent_cache_info(sbi);
3912 f2fs_init_ino_entry_info(sbi);
3914 f2fs_init_fsync_node_info(sbi);
3916 /* setup checkpoint request control and start checkpoint issue thread */
3917 f2fs_init_ckpt_req_control(sbi);
3918 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
3919 test_opt(sbi, MERGE_CHECKPOINT)) {
3920 err = f2fs_start_ckpt_thread(sbi);
3923 "Failed to start F2FS issue_checkpoint_thread (%d)",
3925 goto stop_ckpt_thread;
3929 /* setup f2fs internal modules */
3930 err = f2fs_build_segment_manager(sbi);
3932 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
3936 err = f2fs_build_node_manager(sbi);
3938 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
3943 /* For write statistics */
3944 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
3946 /* Read accumulated write IO statistics if exists */
3947 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3948 if (__exist_node_summaries(sbi))
3949 sbi->kbytes_written =
3950 le64_to_cpu(seg_i->journal->info.kbytes_written);
3952 f2fs_build_gc_manager(sbi);
3954 err = f2fs_build_stats(sbi);
3958 /* get an inode for node space */
3959 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3960 if (IS_ERR(sbi->node_inode)) {
3961 f2fs_err(sbi, "Failed to read node inode");
3962 err = PTR_ERR(sbi->node_inode);
3966 /* read root inode and dentry */
3967 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3969 f2fs_err(sbi, "Failed to read root inode");
3970 err = PTR_ERR(root);
3971 goto free_node_inode;
3973 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3974 !root->i_size || !root->i_nlink) {
3977 goto free_node_inode;
3980 sb->s_root = d_make_root(root); /* allocate root dentry */
3983 goto free_node_inode;
3986 err = f2fs_init_compress_inode(sbi);
3988 goto free_root_inode;
3990 err = f2fs_register_sysfs(sbi);
3992 goto free_compress_inode;
3995 /* Enable quota usage during mount */
3996 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
3997 err = f2fs_enable_quotas(sb);
3999 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
4002 /* if there are any orphan inodes, free them */
4003 err = f2fs_recover_orphan_inodes(sbi);
4007 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
4008 goto reset_checkpoint;
4010 /* recover fsynced data */
4011 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
4012 !test_opt(sbi, NORECOVERY)) {
4014 * mount should be failed, when device has readonly mode, and
4015 * previous checkpoint was not done by clean system shutdown.
4017 if (f2fs_hw_is_readonly(sbi)) {
4018 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4019 err = f2fs_recover_fsync_data(sbi, true);
4022 f2fs_err(sbi, "Need to recover fsync data, but "
4023 "write access unavailable, please try "
4024 "mount w/ disable_roll_forward or norecovery");
4029 f2fs_info(sbi, "write access unavailable, skipping recovery");
4030 goto reset_checkpoint;
4034 set_sbi_flag(sbi, SBI_NEED_FSCK);
4037 goto reset_checkpoint;
4039 err = f2fs_recover_fsync_data(sbi, false);
4042 skip_recovery = true;
4044 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4049 err = f2fs_recover_fsync_data(sbi, true);
4051 if (!f2fs_readonly(sb) && err > 0) {
4053 f2fs_err(sbi, "Need to recover fsync data");
4059 * If the f2fs is not readonly and fsync data recovery succeeds,
4060 * check zoned block devices' write pointer consistency.
4062 if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
4063 err = f2fs_check_write_pointer(sbi);
4069 f2fs_init_inmem_curseg(sbi);
4071 /* f2fs_recover_fsync_data() cleared this already */
4072 clear_sbi_flag(sbi, SBI_POR_DOING);
4074 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4075 err = f2fs_disable_checkpoint(sbi);
4077 goto sync_free_meta;
4078 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4079 f2fs_enable_checkpoint(sbi);
4083 * If filesystem is not mounted as read-only then
4084 * do start the gc_thread.
4086 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4087 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
4088 /* After POR, we can run background GC thread.*/
4089 err = f2fs_start_gc_thread(sbi);
4091 goto sync_free_meta;
4095 /* recover broken superblock */
4097 err = f2fs_commit_super(sbi, true);
4098 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4099 sbi->valid_super_block ? 1 : 2, err);
4102 f2fs_join_shrinker(sbi);
4104 f2fs_tuning_parameters(sbi);
4106 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4107 cur_cp_version(F2FS_CKPT(sbi)));
4108 f2fs_update_time(sbi, CP_TIME);
4109 f2fs_update_time(sbi, REQ_TIME);
4110 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4114 /* safe to flush all the data */
4115 sync_filesystem(sbi->sb);
4120 f2fs_truncate_quota_inode_pages(sb);
4121 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
4122 f2fs_quota_off_umount(sbi->sb);
4125 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4126 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4127 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4128 * falls into an infinite loop in f2fs_sync_meta_pages().
4130 truncate_inode_pages_final(META_MAPPING(sbi));
4131 /* evict some inodes being cached by GC */
4133 f2fs_unregister_sysfs(sbi);
4134 free_compress_inode:
4135 f2fs_destroy_compress_inode(sbi);
4140 f2fs_release_ino_entry(sbi, true);
4141 truncate_inode_pages_final(NODE_MAPPING(sbi));
4142 iput(sbi->node_inode);
4143 sbi->node_inode = NULL;
4145 f2fs_destroy_stats(sbi);
4147 f2fs_destroy_node_manager(sbi);
4149 f2fs_destroy_segment_manager(sbi);
4150 f2fs_destroy_post_read_wq(sbi);
4152 f2fs_stop_ckpt_thread(sbi);
4154 destroy_device_list(sbi);
4157 make_bad_inode(sbi->meta_inode);
4158 iput(sbi->meta_inode);
4159 sbi->meta_inode = NULL;
4160 free_page_array_cache:
4161 f2fs_destroy_page_array_cache(sbi);
4163 f2fs_destroy_xattr_caches(sbi);
4165 mempool_destroy(sbi->write_io_dummy);
4167 destroy_percpu_info(sbi);
4169 for (i = 0; i < NR_PAGE_TYPE; i++)
4170 kvfree(sbi->write_io[i]);
4172 #ifdef CONFIG_UNICODE
4173 utf8_unload(sb->s_encoding);
4174 sb->s_encoding = NULL;
4178 for (i = 0; i < MAXQUOTAS; i++)
4179 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4181 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
4186 if (sbi->s_chksum_driver)
4187 crypto_free_shash(sbi->s_chksum_driver);
4190 /* give only one another chance */
4191 if (retry_cnt > 0 && skip_recovery) {
4193 shrink_dcache_sb(sb);
4199 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
4200 const char *dev_name, void *data)
4202 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
4205 static void kill_f2fs_super(struct super_block *sb)
4208 struct f2fs_sb_info *sbi = F2FS_SB(sb);
4210 set_sbi_flag(sbi, SBI_IS_CLOSE);
4211 f2fs_stop_gc_thread(sbi);
4212 f2fs_stop_discard_thread(sbi);
4214 #ifdef CONFIG_F2FS_FS_COMPRESSION
4216 * latter evict_inode() can bypass checking and invalidating
4217 * compress inode cache.
4219 if (test_opt(sbi, COMPRESS_CACHE))
4220 truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
4223 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
4224 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4225 struct cp_control cpc = {
4226 .reason = CP_UMOUNT,
4228 f2fs_write_checkpoint(sbi, &cpc);
4231 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
4232 sb->s_flags &= ~SB_RDONLY;
4234 kill_block_super(sb);
4237 static struct file_system_type f2fs_fs_type = {
4238 .owner = THIS_MODULE,
4240 .mount = f2fs_mount,
4241 .kill_sb = kill_f2fs_super,
4242 .fs_flags = FS_REQUIRES_DEV,
4244 MODULE_ALIAS_FS("f2fs");
4246 static int __init init_inodecache(void)
4248 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
4249 sizeof(struct f2fs_inode_info), 0,
4250 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
4251 if (!f2fs_inode_cachep)
4256 static void destroy_inodecache(void)
4259 * Make sure all delayed rcu free inodes are flushed before we
4263 kmem_cache_destroy(f2fs_inode_cachep);
4266 static int __init init_f2fs_fs(void)
4270 if (PAGE_SIZE != F2FS_BLKSIZE) {
4271 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
4272 PAGE_SIZE, F2FS_BLKSIZE);
4276 err = init_inodecache();
4279 err = f2fs_create_node_manager_caches();
4281 goto free_inodecache;
4282 err = f2fs_create_segment_manager_caches();
4284 goto free_node_manager_caches;
4285 err = f2fs_create_checkpoint_caches();
4287 goto free_segment_manager_caches;
4288 err = f2fs_create_recovery_cache();
4290 goto free_checkpoint_caches;
4291 err = f2fs_create_extent_cache();
4293 goto free_recovery_cache;
4294 err = f2fs_create_garbage_collection_cache();
4296 goto free_extent_cache;
4297 err = f2fs_init_sysfs();
4299 goto free_garbage_collection_cache;
4300 err = register_shrinker(&f2fs_shrinker_info);
4303 err = register_filesystem(&f2fs_fs_type);
4306 f2fs_create_root_stats();
4307 err = f2fs_init_post_read_processing();
4309 goto free_root_stats;
4310 err = f2fs_init_bio_entry_cache();
4312 goto free_post_read;
4313 err = f2fs_init_bioset();
4315 goto free_bio_enrty_cache;
4316 err = f2fs_init_compress_mempool();
4319 err = f2fs_init_compress_cache();
4321 goto free_compress_mempool;
4323 free_compress_mempool:
4324 f2fs_destroy_compress_mempool();
4326 f2fs_destroy_bioset();
4327 free_bio_enrty_cache:
4328 f2fs_destroy_bio_entry_cache();
4330 f2fs_destroy_post_read_processing();
4332 f2fs_destroy_root_stats();
4333 unregister_filesystem(&f2fs_fs_type);
4335 unregister_shrinker(&f2fs_shrinker_info);
4338 free_garbage_collection_cache:
4339 f2fs_destroy_garbage_collection_cache();
4341 f2fs_destroy_extent_cache();
4342 free_recovery_cache:
4343 f2fs_destroy_recovery_cache();
4344 free_checkpoint_caches:
4345 f2fs_destroy_checkpoint_caches();
4346 free_segment_manager_caches:
4347 f2fs_destroy_segment_manager_caches();
4348 free_node_manager_caches:
4349 f2fs_destroy_node_manager_caches();
4351 destroy_inodecache();
4356 static void __exit exit_f2fs_fs(void)
4358 f2fs_destroy_compress_cache();
4359 f2fs_destroy_compress_mempool();
4360 f2fs_destroy_bioset();
4361 f2fs_destroy_bio_entry_cache();
4362 f2fs_destroy_post_read_processing();
4363 f2fs_destroy_root_stats();
4364 unregister_filesystem(&f2fs_fs_type);
4365 unregister_shrinker(&f2fs_shrinker_info);
4367 f2fs_destroy_garbage_collection_cache();
4368 f2fs_destroy_extent_cache();
4369 f2fs_destroy_recovery_cache();
4370 f2fs_destroy_checkpoint_caches();
4371 f2fs_destroy_segment_manager_caches();
4372 f2fs_destroy_node_manager_caches();
4373 destroy_inodecache();
4376 module_init(init_f2fs_fs)
4377 module_exit(exit_f2fs_fs)
4379 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
4380 MODULE_DESCRIPTION("Flash Friendly File System");
4381 MODULE_LICENSE("GPL");
4382 MODULE_SOFTDEP("pre: crc32");