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>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/f2fs.h>
39 static struct kmem_cache *f2fs_inode_cachep;
41 #ifdef CONFIG_F2FS_FAULT_INJECTION
43 const char *f2fs_fault_name[FAULT_MAX] = {
44 [FAULT_KMALLOC] = "kmalloc",
45 [FAULT_KVMALLOC] = "kvmalloc",
46 [FAULT_PAGE_ALLOC] = "page alloc",
47 [FAULT_PAGE_GET] = "page get",
48 [FAULT_ALLOC_BIO] = "alloc bio",
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_compress_algorithm,
147 Opt_compress_log_size,
148 Opt_compress_extension,
154 static match_table_t f2fs_tokens = {
155 {Opt_gc_background, "background_gc=%s"},
156 {Opt_disable_roll_forward, "disable_roll_forward"},
157 {Opt_norecovery, "norecovery"},
158 {Opt_discard, "discard"},
159 {Opt_nodiscard, "nodiscard"},
160 {Opt_noheap, "no_heap"},
162 {Opt_user_xattr, "user_xattr"},
163 {Opt_nouser_xattr, "nouser_xattr"},
165 {Opt_noacl, "noacl"},
166 {Opt_active_logs, "active_logs=%u"},
167 {Opt_disable_ext_identify, "disable_ext_identify"},
168 {Opt_inline_xattr, "inline_xattr"},
169 {Opt_noinline_xattr, "noinline_xattr"},
170 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
171 {Opt_inline_data, "inline_data"},
172 {Opt_inline_dentry, "inline_dentry"},
173 {Opt_noinline_dentry, "noinline_dentry"},
174 {Opt_flush_merge, "flush_merge"},
175 {Opt_noflush_merge, "noflush_merge"},
176 {Opt_nobarrier, "nobarrier"},
177 {Opt_fastboot, "fastboot"},
178 {Opt_extent_cache, "extent_cache"},
179 {Opt_noextent_cache, "noextent_cache"},
180 {Opt_noinline_data, "noinline_data"},
181 {Opt_data_flush, "data_flush"},
182 {Opt_reserve_root, "reserve_root=%u"},
183 {Opt_resgid, "resgid=%u"},
184 {Opt_resuid, "resuid=%u"},
185 {Opt_mode, "mode=%s"},
186 {Opt_io_size_bits, "io_bits=%u"},
187 {Opt_fault_injection, "fault_injection=%u"},
188 {Opt_fault_type, "fault_type=%u"},
189 {Opt_lazytime, "lazytime"},
190 {Opt_nolazytime, "nolazytime"},
191 {Opt_quota, "quota"},
192 {Opt_noquota, "noquota"},
193 {Opt_usrquota, "usrquota"},
194 {Opt_grpquota, "grpquota"},
195 {Opt_prjquota, "prjquota"},
196 {Opt_usrjquota, "usrjquota=%s"},
197 {Opt_grpjquota, "grpjquota=%s"},
198 {Opt_prjjquota, "prjjquota=%s"},
199 {Opt_offusrjquota, "usrjquota="},
200 {Opt_offgrpjquota, "grpjquota="},
201 {Opt_offprjjquota, "prjjquota="},
202 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
203 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
204 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
205 {Opt_whint, "whint_mode=%s"},
206 {Opt_alloc, "alloc_mode=%s"},
207 {Opt_fsync, "fsync_mode=%s"},
208 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
209 {Opt_test_dummy_encryption, "test_dummy_encryption"},
210 {Opt_inlinecrypt, "inlinecrypt"},
211 {Opt_checkpoint_disable, "checkpoint=disable"},
212 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
213 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
214 {Opt_checkpoint_enable, "checkpoint=enable"},
215 {Opt_compress_algorithm, "compress_algorithm=%s"},
216 {Opt_compress_log_size, "compress_log_size=%u"},
217 {Opt_compress_extension, "compress_extension=%s"},
218 {Opt_compress_chksum, "compress_chksum"},
223 void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...)
225 struct va_format vaf;
231 level = printk_get_level(fmt);
232 vaf.fmt = printk_skip_level(fmt);
234 printk("%c%cF2FS-fs (%s): %pV\n",
235 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
240 #ifdef CONFIG_UNICODE
241 static const struct f2fs_sb_encodings {
245 } f2fs_sb_encoding_map[] = {
246 {F2FS_ENC_UTF8_12_1, "utf8", "12.1.0"},
249 static int f2fs_sb_read_encoding(const struct f2fs_super_block *sb,
250 const struct f2fs_sb_encodings **encoding,
253 __u16 magic = le16_to_cpu(sb->s_encoding);
256 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
257 if (magic == f2fs_sb_encoding_map[i].magic)
260 if (i >= ARRAY_SIZE(f2fs_sb_encoding_map))
263 *encoding = &f2fs_sb_encoding_map[i];
264 *flags = le16_to_cpu(sb->s_encoding_flags);
270 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
272 block_t limit = min((sbi->user_block_count << 1) / 1000,
273 sbi->user_block_count - sbi->reserved_blocks);
276 if (test_opt(sbi, RESERVE_ROOT) &&
277 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
278 F2FS_OPTION(sbi).root_reserved_blocks = limit;
279 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
280 F2FS_OPTION(sbi).root_reserved_blocks);
282 if (!test_opt(sbi, RESERVE_ROOT) &&
283 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
284 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
285 !gid_eq(F2FS_OPTION(sbi).s_resgid,
286 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
287 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
288 from_kuid_munged(&init_user_ns,
289 F2FS_OPTION(sbi).s_resuid),
290 from_kgid_munged(&init_user_ns,
291 F2FS_OPTION(sbi).s_resgid));
294 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
296 if (!F2FS_OPTION(sbi).unusable_cap_perc)
299 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
300 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
302 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
303 F2FS_OPTION(sbi).unusable_cap_perc;
305 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
306 F2FS_OPTION(sbi).unusable_cap,
307 F2FS_OPTION(sbi).unusable_cap_perc);
310 static void init_once(void *foo)
312 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
314 inode_init_once(&fi->vfs_inode);
318 static const char * const quotatypes[] = INITQFNAMES;
319 #define QTYPE2NAME(t) (quotatypes[t])
320 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
323 struct f2fs_sb_info *sbi = F2FS_SB(sb);
327 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
328 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
331 if (f2fs_sb_has_quota_ino(sbi)) {
332 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
336 qname = match_strdup(args);
338 f2fs_err(sbi, "Not enough memory for storing quotafile name");
341 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
342 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
345 f2fs_err(sbi, "%s quota file already specified",
349 if (strchr(qname, '/')) {
350 f2fs_err(sbi, "quotafile must be on filesystem root");
353 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
361 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
363 struct f2fs_sb_info *sbi = F2FS_SB(sb);
365 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
366 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
369 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
370 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
374 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
377 * We do the test below only for project quotas. 'usrquota' and
378 * 'grpquota' mount options are allowed even without quota feature
379 * to support legacy quotas in quota files.
381 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
382 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
385 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
386 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
387 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
388 if (test_opt(sbi, USRQUOTA) &&
389 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
390 clear_opt(sbi, USRQUOTA);
392 if (test_opt(sbi, GRPQUOTA) &&
393 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
394 clear_opt(sbi, GRPQUOTA);
396 if (test_opt(sbi, PRJQUOTA) &&
397 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
398 clear_opt(sbi, PRJQUOTA);
400 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
401 test_opt(sbi, PRJQUOTA)) {
402 f2fs_err(sbi, "old and new quota format mixing");
406 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
407 f2fs_err(sbi, "journaled quota format not specified");
412 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
413 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
414 F2FS_OPTION(sbi).s_jquota_fmt = 0;
420 static int f2fs_set_test_dummy_encryption(struct super_block *sb,
422 const substring_t *arg,
425 struct f2fs_sb_info *sbi = F2FS_SB(sb);
426 #ifdef CONFIG_FS_ENCRYPTION
429 if (!f2fs_sb_has_encrypt(sbi)) {
430 f2fs_err(sbi, "Encrypt feature is off");
435 * This mount option is just for testing, and it's not worthwhile to
436 * implement the extra complexity (e.g. RCU protection) that would be
437 * needed to allow it to be set or changed during remount. We do allow
438 * it to be specified during remount, but only if there is no change.
440 if (is_remount && !F2FS_OPTION(sbi).dummy_enc_policy.policy) {
441 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
444 err = fscrypt_set_test_dummy_encryption(
445 sb, arg->from, &F2FS_OPTION(sbi).dummy_enc_policy);
449 "Can't change test_dummy_encryption on remount");
450 else if (err == -EINVAL)
451 f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
454 f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
458 f2fs_warn(sbi, "Test dummy encryption mode enabled");
460 f2fs_warn(sbi, "Test dummy encryption mount option ignored");
465 static int parse_options(struct super_block *sb, char *options, bool is_remount)
467 struct f2fs_sb_info *sbi = F2FS_SB(sb);
468 substring_t args[MAX_OPT_ARGS];
469 #ifdef CONFIG_F2FS_FS_COMPRESSION
470 unsigned char (*ext)[F2FS_EXTENSION_LEN];
482 while ((p = strsep(&options, ",")) != NULL) {
487 * Initialize args struct so we know whether arg was
488 * found; some options take optional arguments.
490 args[0].to = args[0].from = NULL;
491 token = match_token(p, f2fs_tokens, args);
494 case Opt_gc_background:
495 name = match_strdup(&args[0]);
499 if (!strcmp(name, "on")) {
500 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
501 } else if (!strcmp(name, "off")) {
502 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
503 } else if (!strcmp(name, "sync")) {
504 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
511 case Opt_disable_roll_forward:
512 set_opt(sbi, DISABLE_ROLL_FORWARD);
515 /* this option mounts f2fs with ro */
516 set_opt(sbi, NORECOVERY);
517 if (!f2fs_readonly(sb))
521 set_opt(sbi, DISCARD);
524 if (f2fs_sb_has_blkzoned(sbi)) {
525 f2fs_warn(sbi, "discard is required for zoned block devices");
528 clear_opt(sbi, DISCARD);
531 set_opt(sbi, NOHEAP);
534 clear_opt(sbi, NOHEAP);
536 #ifdef CONFIG_F2FS_FS_XATTR
538 set_opt(sbi, XATTR_USER);
540 case Opt_nouser_xattr:
541 clear_opt(sbi, XATTR_USER);
543 case Opt_inline_xattr:
544 set_opt(sbi, INLINE_XATTR);
546 case Opt_noinline_xattr:
547 clear_opt(sbi, INLINE_XATTR);
549 case Opt_inline_xattr_size:
550 if (args->from && match_int(args, &arg))
552 set_opt(sbi, INLINE_XATTR_SIZE);
553 F2FS_OPTION(sbi).inline_xattr_size = arg;
557 f2fs_info(sbi, "user_xattr options not supported");
559 case Opt_nouser_xattr:
560 f2fs_info(sbi, "nouser_xattr options not supported");
562 case Opt_inline_xattr:
563 f2fs_info(sbi, "inline_xattr options not supported");
565 case Opt_noinline_xattr:
566 f2fs_info(sbi, "noinline_xattr options not supported");
569 #ifdef CONFIG_F2FS_FS_POSIX_ACL
571 set_opt(sbi, POSIX_ACL);
574 clear_opt(sbi, POSIX_ACL);
578 f2fs_info(sbi, "acl options not supported");
581 f2fs_info(sbi, "noacl options not supported");
584 case Opt_active_logs:
585 if (args->from && match_int(args, &arg))
587 if (arg != 2 && arg != 4 &&
588 arg != NR_CURSEG_PERSIST_TYPE)
590 F2FS_OPTION(sbi).active_logs = arg;
592 case Opt_disable_ext_identify:
593 set_opt(sbi, DISABLE_EXT_IDENTIFY);
595 case Opt_inline_data:
596 set_opt(sbi, INLINE_DATA);
598 case Opt_inline_dentry:
599 set_opt(sbi, INLINE_DENTRY);
601 case Opt_noinline_dentry:
602 clear_opt(sbi, INLINE_DENTRY);
604 case Opt_flush_merge:
605 set_opt(sbi, FLUSH_MERGE);
607 case Opt_noflush_merge:
608 clear_opt(sbi, FLUSH_MERGE);
611 set_opt(sbi, NOBARRIER);
614 set_opt(sbi, FASTBOOT);
616 case Opt_extent_cache:
617 set_opt(sbi, EXTENT_CACHE);
619 case Opt_noextent_cache:
620 clear_opt(sbi, EXTENT_CACHE);
622 case Opt_noinline_data:
623 clear_opt(sbi, INLINE_DATA);
626 set_opt(sbi, DATA_FLUSH);
628 case Opt_reserve_root:
629 if (args->from && match_int(args, &arg))
631 if (test_opt(sbi, RESERVE_ROOT)) {
632 f2fs_info(sbi, "Preserve previous reserve_root=%u",
633 F2FS_OPTION(sbi).root_reserved_blocks);
635 F2FS_OPTION(sbi).root_reserved_blocks = arg;
636 set_opt(sbi, RESERVE_ROOT);
640 if (args->from && match_int(args, &arg))
642 uid = make_kuid(current_user_ns(), arg);
643 if (!uid_valid(uid)) {
644 f2fs_err(sbi, "Invalid uid value %d", arg);
647 F2FS_OPTION(sbi).s_resuid = uid;
650 if (args->from && match_int(args, &arg))
652 gid = make_kgid(current_user_ns(), arg);
653 if (!gid_valid(gid)) {
654 f2fs_err(sbi, "Invalid gid value %d", arg);
657 F2FS_OPTION(sbi).s_resgid = gid;
660 name = match_strdup(&args[0]);
664 if (!strcmp(name, "adaptive")) {
665 if (f2fs_sb_has_blkzoned(sbi)) {
666 f2fs_warn(sbi, "adaptive mode is not allowed with zoned block device feature");
670 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
671 } else if (!strcmp(name, "lfs")) {
672 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
679 case Opt_io_size_bits:
680 if (args->from && match_int(args, &arg))
682 if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_PAGES)) {
683 f2fs_warn(sbi, "Not support %d, larger than %d",
684 1 << arg, BIO_MAX_PAGES);
687 F2FS_OPTION(sbi).write_io_size_bits = arg;
689 #ifdef CONFIG_F2FS_FAULT_INJECTION
690 case Opt_fault_injection:
691 if (args->from && match_int(args, &arg))
693 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
694 set_opt(sbi, FAULT_INJECTION);
698 if (args->from && match_int(args, &arg))
700 f2fs_build_fault_attr(sbi, 0, arg);
701 set_opt(sbi, FAULT_INJECTION);
704 case Opt_fault_injection:
705 f2fs_info(sbi, "fault_injection options not supported");
709 f2fs_info(sbi, "fault_type options not supported");
713 sb->s_flags |= SB_LAZYTIME;
716 sb->s_flags &= ~SB_LAZYTIME;
721 set_opt(sbi, USRQUOTA);
724 set_opt(sbi, GRPQUOTA);
727 set_opt(sbi, PRJQUOTA);
730 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
735 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
740 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
744 case Opt_offusrjquota:
745 ret = f2fs_clear_qf_name(sb, USRQUOTA);
749 case Opt_offgrpjquota:
750 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
754 case Opt_offprjjquota:
755 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
759 case Opt_jqfmt_vfsold:
760 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
762 case Opt_jqfmt_vfsv0:
763 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
765 case Opt_jqfmt_vfsv1:
766 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
769 clear_opt(sbi, QUOTA);
770 clear_opt(sbi, USRQUOTA);
771 clear_opt(sbi, GRPQUOTA);
772 clear_opt(sbi, PRJQUOTA);
782 case Opt_offusrjquota:
783 case Opt_offgrpjquota:
784 case Opt_offprjjquota:
785 case Opt_jqfmt_vfsold:
786 case Opt_jqfmt_vfsv0:
787 case Opt_jqfmt_vfsv1:
789 f2fs_info(sbi, "quota operations not supported");
793 name = match_strdup(&args[0]);
796 if (!strcmp(name, "user-based")) {
797 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
798 } else if (!strcmp(name, "off")) {
799 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
800 } else if (!strcmp(name, "fs-based")) {
801 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
809 name = match_strdup(&args[0]);
813 if (!strcmp(name, "default")) {
814 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
815 } else if (!strcmp(name, "reuse")) {
816 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
824 name = match_strdup(&args[0]);
827 if (!strcmp(name, "posix")) {
828 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
829 } else if (!strcmp(name, "strict")) {
830 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
831 } else if (!strcmp(name, "nobarrier")) {
832 F2FS_OPTION(sbi).fsync_mode =
833 FSYNC_MODE_NOBARRIER;
840 case Opt_test_dummy_encryption:
841 ret = f2fs_set_test_dummy_encryption(sb, p, &args[0],
846 case Opt_inlinecrypt:
847 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
848 sb->s_flags |= SB_INLINECRYPT;
850 f2fs_info(sbi, "inline encryption not supported");
853 case Opt_checkpoint_disable_cap_perc:
854 if (args->from && match_int(args, &arg))
856 if (arg < 0 || arg > 100)
858 F2FS_OPTION(sbi).unusable_cap_perc = arg;
859 set_opt(sbi, DISABLE_CHECKPOINT);
861 case Opt_checkpoint_disable_cap:
862 if (args->from && match_int(args, &arg))
864 F2FS_OPTION(sbi).unusable_cap = arg;
865 set_opt(sbi, DISABLE_CHECKPOINT);
867 case Opt_checkpoint_disable:
868 set_opt(sbi, DISABLE_CHECKPOINT);
870 case Opt_checkpoint_enable:
871 clear_opt(sbi, DISABLE_CHECKPOINT);
873 #ifdef CONFIG_F2FS_FS_COMPRESSION
874 case Opt_compress_algorithm:
875 if (!f2fs_sb_has_compression(sbi)) {
876 f2fs_info(sbi, "Image doesn't support compression");
879 name = match_strdup(&args[0]);
882 if (!strcmp(name, "lzo")) {
883 F2FS_OPTION(sbi).compress_algorithm =
885 } else if (!strcmp(name, "lz4")) {
886 F2FS_OPTION(sbi).compress_algorithm =
888 } else if (!strcmp(name, "zstd")) {
889 F2FS_OPTION(sbi).compress_algorithm =
891 } else if (!strcmp(name, "lzo-rle")) {
892 F2FS_OPTION(sbi).compress_algorithm =
900 case Opt_compress_log_size:
901 if (!f2fs_sb_has_compression(sbi)) {
902 f2fs_info(sbi, "Image doesn't support compression");
905 if (args->from && match_int(args, &arg))
907 if (arg < MIN_COMPRESS_LOG_SIZE ||
908 arg > MAX_COMPRESS_LOG_SIZE) {
910 "Compress cluster log size is out of range");
913 F2FS_OPTION(sbi).compress_log_size = arg;
915 case Opt_compress_extension:
916 if (!f2fs_sb_has_compression(sbi)) {
917 f2fs_info(sbi, "Image doesn't support compression");
920 name = match_strdup(&args[0]);
924 ext = F2FS_OPTION(sbi).extensions;
925 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
927 if (strlen(name) >= F2FS_EXTENSION_LEN ||
928 ext_cnt >= COMPRESS_EXT_NUM) {
930 "invalid extension length/number");
935 strcpy(ext[ext_cnt], name);
936 F2FS_OPTION(sbi).compress_ext_cnt++;
939 case Opt_compress_chksum:
940 F2FS_OPTION(sbi).compress_chksum = true;
943 case Opt_compress_algorithm:
944 case Opt_compress_log_size:
945 case Opt_compress_extension:
946 case Opt_compress_chksum:
947 f2fs_info(sbi, "compression options not supported");
954 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
960 if (f2fs_check_quota_options(sbi))
963 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
964 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
967 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
968 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
972 #ifndef CONFIG_UNICODE
973 if (f2fs_sb_has_casefold(sbi)) {
975 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
980 * The BLKZONED feature indicates that the drive was formatted with
981 * zone alignment optimization. This is optional for host-aware
982 * devices, but mandatory for host-managed zoned block devices.
984 #ifndef CONFIG_BLK_DEV_ZONED
985 if (f2fs_sb_has_blkzoned(sbi)) {
986 f2fs_err(sbi, "Zoned block device support is not enabled");
991 if (F2FS_IO_SIZE_BITS(sbi) && !f2fs_lfs_mode(sbi)) {
992 f2fs_err(sbi, "Should set mode=lfs with %uKB-sized IO",
993 F2FS_IO_SIZE_KB(sbi));
997 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
998 int min_size, max_size;
1000 if (!f2fs_sb_has_extra_attr(sbi) ||
1001 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
1002 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
1005 if (!test_opt(sbi, INLINE_XATTR)) {
1006 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
1010 min_size = sizeof(struct f2fs_xattr_header) / sizeof(__le32);
1011 max_size = MAX_INLINE_XATTR_SIZE;
1013 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1014 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
1015 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1016 min_size, max_size);
1021 if (test_opt(sbi, DISABLE_CHECKPOINT) && f2fs_lfs_mode(sbi)) {
1022 f2fs_err(sbi, "LFS not compatible with checkpoint=disable\n");
1026 /* Not pass down write hints if the number of active logs is lesser
1027 * than NR_CURSEG_PERSIST_TYPE.
1029 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
1030 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1034 static struct inode *f2fs_alloc_inode(struct super_block *sb)
1036 struct f2fs_inode_info *fi;
1038 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
1042 init_once((void *) fi);
1044 /* Initialize f2fs-specific inode info */
1045 atomic_set(&fi->dirty_pages, 0);
1046 atomic_set(&fi->i_compr_blocks, 0);
1047 init_rwsem(&fi->i_sem);
1048 spin_lock_init(&fi->i_size_lock);
1049 INIT_LIST_HEAD(&fi->dirty_list);
1050 INIT_LIST_HEAD(&fi->gdirty_list);
1051 INIT_LIST_HEAD(&fi->inmem_ilist);
1052 INIT_LIST_HEAD(&fi->inmem_pages);
1053 mutex_init(&fi->inmem_lock);
1054 init_rwsem(&fi->i_gc_rwsem[READ]);
1055 init_rwsem(&fi->i_gc_rwsem[WRITE]);
1056 init_rwsem(&fi->i_mmap_sem);
1057 init_rwsem(&fi->i_xattr_sem);
1059 /* Will be used by directory only */
1060 fi->i_dir_level = F2FS_SB(sb)->dir_level;
1064 return &fi->vfs_inode;
1067 static int f2fs_drop_inode(struct inode *inode)
1069 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1073 * during filesystem shutdown, if checkpoint is disabled,
1074 * drop useless meta/node dirty pages.
1076 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1077 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1078 inode->i_ino == F2FS_META_INO(sbi)) {
1079 trace_f2fs_drop_inode(inode, 1);
1085 * This is to avoid a deadlock condition like below.
1086 * writeback_single_inode(inode)
1087 * - f2fs_write_data_page
1088 * - f2fs_gc -> iput -> evict
1089 * - inode_wait_for_writeback(inode)
1091 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
1092 if (!inode->i_nlink && !is_bad_inode(inode)) {
1093 /* to avoid evict_inode call simultaneously */
1094 atomic_inc(&inode->i_count);
1095 spin_unlock(&inode->i_lock);
1097 /* some remained atomic pages should discarded */
1098 if (f2fs_is_atomic_file(inode))
1099 f2fs_drop_inmem_pages(inode);
1101 /* should remain fi->extent_tree for writepage */
1102 f2fs_destroy_extent_node(inode);
1104 sb_start_intwrite(inode->i_sb);
1105 f2fs_i_size_write(inode, 0);
1107 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1108 inode, NULL, 0, DATA);
1109 truncate_inode_pages_final(inode->i_mapping);
1111 if (F2FS_HAS_BLOCKS(inode))
1112 f2fs_truncate(inode);
1114 sb_end_intwrite(inode->i_sb);
1116 spin_lock(&inode->i_lock);
1117 atomic_dec(&inode->i_count);
1119 trace_f2fs_drop_inode(inode, 0);
1122 ret = generic_drop_inode(inode);
1124 ret = fscrypt_drop_inode(inode);
1125 trace_f2fs_drop_inode(inode, ret);
1129 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1131 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1134 spin_lock(&sbi->inode_lock[DIRTY_META]);
1135 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1138 set_inode_flag(inode, FI_DIRTY_INODE);
1139 stat_inc_dirty_inode(sbi, DIRTY_META);
1141 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1142 list_add_tail(&F2FS_I(inode)->gdirty_list,
1143 &sbi->inode_list[DIRTY_META]);
1144 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1146 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1150 void f2fs_inode_synced(struct inode *inode)
1152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1154 spin_lock(&sbi->inode_lock[DIRTY_META]);
1155 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1156 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1159 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1160 list_del_init(&F2FS_I(inode)->gdirty_list);
1161 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1163 clear_inode_flag(inode, FI_DIRTY_INODE);
1164 clear_inode_flag(inode, FI_AUTO_RECOVER);
1165 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1166 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1170 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1172 * We should call set_dirty_inode to write the dirty inode through write_inode.
1174 static void f2fs_dirty_inode(struct inode *inode, int flags)
1176 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1178 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1179 inode->i_ino == F2FS_META_INO(sbi))
1182 if (flags == I_DIRTY_TIME)
1185 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1186 clear_inode_flag(inode, FI_AUTO_RECOVER);
1188 f2fs_inode_dirtied(inode, false);
1191 static void f2fs_free_inode(struct inode *inode)
1193 fscrypt_free_inode(inode);
1194 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1197 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1199 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1200 percpu_counter_destroy(&sbi->total_valid_inode_count);
1203 static void destroy_device_list(struct f2fs_sb_info *sbi)
1207 for (i = 0; i < sbi->s_ndevs; i++) {
1208 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
1209 #ifdef CONFIG_BLK_DEV_ZONED
1210 kvfree(FDEV(i).blkz_seq);
1211 kfree(FDEV(i).zone_capacity_blocks);
1217 static void f2fs_put_super(struct super_block *sb)
1219 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1223 /* unregister procfs/sysfs entries in advance to avoid race case */
1224 f2fs_unregister_sysfs(sbi);
1226 f2fs_quota_off_umount(sb);
1228 /* prevent remaining shrinker jobs */
1229 mutex_lock(&sbi->umount_mutex);
1232 * We don't need to do checkpoint when superblock is clean.
1233 * But, the previous checkpoint was not done by umount, it needs to do
1234 * clean checkpoint again.
1236 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1237 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1238 struct cp_control cpc = {
1239 .reason = CP_UMOUNT,
1241 f2fs_write_checkpoint(sbi, &cpc);
1244 /* be sure to wait for any on-going discard commands */
1245 dropped = f2fs_issue_discard_timeout(sbi);
1247 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1248 !sbi->discard_blks && !dropped) {
1249 struct cp_control cpc = {
1250 .reason = CP_UMOUNT | CP_TRIMMED,
1252 f2fs_write_checkpoint(sbi, &cpc);
1256 * normally superblock is clean, so we need to release this.
1257 * In addition, EIO will skip do checkpoint, we need this as well.
1259 f2fs_release_ino_entry(sbi, true);
1261 f2fs_leave_shrinker(sbi);
1262 mutex_unlock(&sbi->umount_mutex);
1264 /* our cp_error case, we can wait for any writeback page */
1265 f2fs_flush_merged_writes(sbi);
1267 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1269 f2fs_bug_on(sbi, sbi->fsync_node_num);
1271 iput(sbi->node_inode);
1272 sbi->node_inode = NULL;
1274 iput(sbi->meta_inode);
1275 sbi->meta_inode = NULL;
1278 * iput() can update stat information, if f2fs_write_checkpoint()
1279 * above failed with error.
1281 f2fs_destroy_stats(sbi);
1283 /* destroy f2fs internal modules */
1284 f2fs_destroy_node_manager(sbi);
1285 f2fs_destroy_segment_manager(sbi);
1287 f2fs_destroy_post_read_wq(sbi);
1291 sb->s_fs_info = NULL;
1292 if (sbi->s_chksum_driver)
1293 crypto_free_shash(sbi->s_chksum_driver);
1294 kfree(sbi->raw_super);
1296 destroy_device_list(sbi);
1297 f2fs_destroy_page_array_cache(sbi);
1298 f2fs_destroy_xattr_caches(sbi);
1299 mempool_destroy(sbi->write_io_dummy);
1301 for (i = 0; i < MAXQUOTAS; i++)
1302 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1304 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1305 destroy_percpu_info(sbi);
1306 for (i = 0; i < NR_PAGE_TYPE; i++)
1307 kvfree(sbi->write_io[i]);
1308 #ifdef CONFIG_UNICODE
1309 utf8_unload(sb->s_encoding);
1314 int f2fs_sync_fs(struct super_block *sb, int sync)
1316 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1319 if (unlikely(f2fs_cp_error(sbi)))
1321 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1324 trace_f2fs_sync_fs(sb, sync);
1326 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1330 struct cp_control cpc;
1332 cpc.reason = __get_cp_reason(sbi);
1334 down_write(&sbi->gc_lock);
1335 err = f2fs_write_checkpoint(sbi, &cpc);
1336 up_write(&sbi->gc_lock);
1338 f2fs_trace_ios(NULL, 1);
1343 static int f2fs_freeze(struct super_block *sb)
1345 if (f2fs_readonly(sb))
1348 /* IO error happened before */
1349 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1352 /* must be clean, since sync_filesystem() was already called */
1353 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1358 static int f2fs_unfreeze(struct super_block *sb)
1364 static int f2fs_statfs_project(struct super_block *sb,
1365 kprojid_t projid, struct kstatfs *buf)
1368 struct dquot *dquot;
1372 qid = make_kqid_projid(projid);
1373 dquot = dqget(sb, qid);
1375 return PTR_ERR(dquot);
1376 spin_lock(&dquot->dq_dqb_lock);
1378 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1379 dquot->dq_dqb.dqb_bhardlimit);
1381 limit >>= sb->s_blocksize_bits;
1383 if (limit && buf->f_blocks > limit) {
1384 curblock = (dquot->dq_dqb.dqb_curspace +
1385 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1386 buf->f_blocks = limit;
1387 buf->f_bfree = buf->f_bavail =
1388 (buf->f_blocks > curblock) ?
1389 (buf->f_blocks - curblock) : 0;
1392 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1393 dquot->dq_dqb.dqb_ihardlimit);
1395 if (limit && buf->f_files > limit) {
1396 buf->f_files = limit;
1398 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1399 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1402 spin_unlock(&dquot->dq_dqb_lock);
1408 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1410 struct super_block *sb = dentry->d_sb;
1411 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1412 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1413 block_t total_count, user_block_count, start_count;
1414 u64 avail_node_count;
1416 total_count = le64_to_cpu(sbi->raw_super->block_count);
1417 user_block_count = sbi->user_block_count;
1418 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1419 buf->f_type = F2FS_SUPER_MAGIC;
1420 buf->f_bsize = sbi->blocksize;
1422 buf->f_blocks = total_count - start_count;
1423 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1424 sbi->current_reserved_blocks;
1426 spin_lock(&sbi->stat_lock);
1427 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1430 buf->f_bfree -= sbi->unusable_block_count;
1431 spin_unlock(&sbi->stat_lock);
1433 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1434 buf->f_bavail = buf->f_bfree -
1435 F2FS_OPTION(sbi).root_reserved_blocks;
1439 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1441 if (avail_node_count > user_block_count) {
1442 buf->f_files = user_block_count;
1443 buf->f_ffree = buf->f_bavail;
1445 buf->f_files = avail_node_count;
1446 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1450 buf->f_namelen = F2FS_NAME_LEN;
1451 buf->f_fsid = u64_to_fsid(id);
1454 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1455 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1456 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1462 static inline void f2fs_show_quota_options(struct seq_file *seq,
1463 struct super_block *sb)
1466 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1468 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1471 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1482 seq_printf(seq, ",jqfmt=%s", fmtname);
1485 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1486 seq_show_option(seq, "usrjquota",
1487 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1489 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1490 seq_show_option(seq, "grpjquota",
1491 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1493 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1494 seq_show_option(seq, "prjjquota",
1495 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1499 static inline void f2fs_show_compress_options(struct seq_file *seq,
1500 struct super_block *sb)
1502 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1506 if (!f2fs_sb_has_compression(sbi))
1509 switch (F2FS_OPTION(sbi).compress_algorithm) {
1519 case COMPRESS_LZORLE:
1520 algtype = "lzo-rle";
1523 seq_printf(seq, ",compress_algorithm=%s", algtype);
1525 seq_printf(seq, ",compress_log_size=%u",
1526 F2FS_OPTION(sbi).compress_log_size);
1528 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1529 seq_printf(seq, ",compress_extension=%s",
1530 F2FS_OPTION(sbi).extensions[i]);
1533 if (F2FS_OPTION(sbi).compress_chksum)
1534 seq_puts(seq, ",compress_chksum");
1537 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1539 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1541 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1542 seq_printf(seq, ",background_gc=%s", "sync");
1543 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1544 seq_printf(seq, ",background_gc=%s", "on");
1545 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
1546 seq_printf(seq, ",background_gc=%s", "off");
1548 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1549 seq_puts(seq, ",disable_roll_forward");
1550 if (test_opt(sbi, NORECOVERY))
1551 seq_puts(seq, ",norecovery");
1552 if (test_opt(sbi, DISCARD))
1553 seq_puts(seq, ",discard");
1555 seq_puts(seq, ",nodiscard");
1556 if (test_opt(sbi, NOHEAP))
1557 seq_puts(seq, ",no_heap");
1559 seq_puts(seq, ",heap");
1560 #ifdef CONFIG_F2FS_FS_XATTR
1561 if (test_opt(sbi, XATTR_USER))
1562 seq_puts(seq, ",user_xattr");
1564 seq_puts(seq, ",nouser_xattr");
1565 if (test_opt(sbi, INLINE_XATTR))
1566 seq_puts(seq, ",inline_xattr");
1568 seq_puts(seq, ",noinline_xattr");
1569 if (test_opt(sbi, INLINE_XATTR_SIZE))
1570 seq_printf(seq, ",inline_xattr_size=%u",
1571 F2FS_OPTION(sbi).inline_xattr_size);
1573 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1574 if (test_opt(sbi, POSIX_ACL))
1575 seq_puts(seq, ",acl");
1577 seq_puts(seq, ",noacl");
1579 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1580 seq_puts(seq, ",disable_ext_identify");
1581 if (test_opt(sbi, INLINE_DATA))
1582 seq_puts(seq, ",inline_data");
1584 seq_puts(seq, ",noinline_data");
1585 if (test_opt(sbi, INLINE_DENTRY))
1586 seq_puts(seq, ",inline_dentry");
1588 seq_puts(seq, ",noinline_dentry");
1589 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
1590 seq_puts(seq, ",flush_merge");
1591 if (test_opt(sbi, NOBARRIER))
1592 seq_puts(seq, ",nobarrier");
1593 if (test_opt(sbi, FASTBOOT))
1594 seq_puts(seq, ",fastboot");
1595 if (test_opt(sbi, EXTENT_CACHE))
1596 seq_puts(seq, ",extent_cache");
1598 seq_puts(seq, ",noextent_cache");
1599 if (test_opt(sbi, DATA_FLUSH))
1600 seq_puts(seq, ",data_flush");
1602 seq_puts(seq, ",mode=");
1603 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
1604 seq_puts(seq, "adaptive");
1605 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
1606 seq_puts(seq, "lfs");
1607 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
1608 if (test_opt(sbi, RESERVE_ROOT))
1609 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1610 F2FS_OPTION(sbi).root_reserved_blocks,
1611 from_kuid_munged(&init_user_ns,
1612 F2FS_OPTION(sbi).s_resuid),
1613 from_kgid_munged(&init_user_ns,
1614 F2FS_OPTION(sbi).s_resgid));
1615 if (F2FS_IO_SIZE_BITS(sbi))
1616 seq_printf(seq, ",io_bits=%u",
1617 F2FS_OPTION(sbi).write_io_size_bits);
1618 #ifdef CONFIG_F2FS_FAULT_INJECTION
1619 if (test_opt(sbi, FAULT_INJECTION)) {
1620 seq_printf(seq, ",fault_injection=%u",
1621 F2FS_OPTION(sbi).fault_info.inject_rate);
1622 seq_printf(seq, ",fault_type=%u",
1623 F2FS_OPTION(sbi).fault_info.inject_type);
1627 if (test_opt(sbi, QUOTA))
1628 seq_puts(seq, ",quota");
1629 if (test_opt(sbi, USRQUOTA))
1630 seq_puts(seq, ",usrquota");
1631 if (test_opt(sbi, GRPQUOTA))
1632 seq_puts(seq, ",grpquota");
1633 if (test_opt(sbi, PRJQUOTA))
1634 seq_puts(seq, ",prjquota");
1636 f2fs_show_quota_options(seq, sbi->sb);
1637 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
1638 seq_printf(seq, ",whint_mode=%s", "user-based");
1639 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
1640 seq_printf(seq, ",whint_mode=%s", "fs-based");
1642 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
1644 if (sbi->sb->s_flags & SB_INLINECRYPT)
1645 seq_puts(seq, ",inlinecrypt");
1647 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
1648 seq_printf(seq, ",alloc_mode=%s", "default");
1649 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
1650 seq_printf(seq, ",alloc_mode=%s", "reuse");
1652 if (test_opt(sbi, DISABLE_CHECKPOINT))
1653 seq_printf(seq, ",checkpoint=disable:%u",
1654 F2FS_OPTION(sbi).unusable_cap);
1655 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
1656 seq_printf(seq, ",fsync_mode=%s", "posix");
1657 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
1658 seq_printf(seq, ",fsync_mode=%s", "strict");
1659 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1660 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
1662 #ifdef CONFIG_F2FS_FS_COMPRESSION
1663 f2fs_show_compress_options(seq, sbi->sb);
1666 if (test_opt(sbi, ATGC))
1667 seq_puts(seq, ",atgc");
1671 static void default_options(struct f2fs_sb_info *sbi)
1673 /* init some FS parameters */
1674 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
1675 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1676 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1677 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1678 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1679 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1680 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
1681 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
1682 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
1683 F2FS_OPTION(sbi).compress_ext_cnt = 0;
1684 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
1686 sbi->sb->s_flags &= ~SB_INLINECRYPT;
1688 set_opt(sbi, INLINE_XATTR);
1689 set_opt(sbi, INLINE_DATA);
1690 set_opt(sbi, INLINE_DENTRY);
1691 set_opt(sbi, EXTENT_CACHE);
1692 set_opt(sbi, NOHEAP);
1693 clear_opt(sbi, DISABLE_CHECKPOINT);
1694 F2FS_OPTION(sbi).unusable_cap = 0;
1695 sbi->sb->s_flags |= SB_LAZYTIME;
1696 set_opt(sbi, FLUSH_MERGE);
1697 set_opt(sbi, DISCARD);
1698 if (f2fs_sb_has_blkzoned(sbi))
1699 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
1701 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
1703 #ifdef CONFIG_F2FS_FS_XATTR
1704 set_opt(sbi, XATTR_USER);
1706 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1707 set_opt(sbi, POSIX_ACL);
1710 f2fs_build_fault_attr(sbi, 0, 0);
1714 static int f2fs_enable_quotas(struct super_block *sb);
1717 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
1719 unsigned int s_flags = sbi->sb->s_flags;
1720 struct cp_control cpc;
1725 if (s_flags & SB_RDONLY) {
1726 f2fs_err(sbi, "checkpoint=disable on readonly fs");
1729 sbi->sb->s_flags |= SB_ACTIVE;
1731 f2fs_update_time(sbi, DISABLE_TIME);
1733 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
1734 down_write(&sbi->gc_lock);
1735 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1736 if (err == -ENODATA) {
1740 if (err && err != -EAGAIN)
1744 ret = sync_filesystem(sbi->sb);
1746 err = ret ? ret: err;
1750 unusable = f2fs_get_unusable_blocks(sbi);
1751 if (f2fs_disable_cp_again(sbi, unusable)) {
1756 down_write(&sbi->gc_lock);
1757 cpc.reason = CP_PAUSE;
1758 set_sbi_flag(sbi, SBI_CP_DISABLED);
1759 err = f2fs_write_checkpoint(sbi, &cpc);
1763 spin_lock(&sbi->stat_lock);
1764 sbi->unusable_block_count = unusable;
1765 spin_unlock(&sbi->stat_lock);
1768 up_write(&sbi->gc_lock);
1770 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
1774 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
1776 down_write(&sbi->gc_lock);
1777 f2fs_dirty_to_prefree(sbi);
1779 clear_sbi_flag(sbi, SBI_CP_DISABLED);
1780 set_sbi_flag(sbi, SBI_IS_DIRTY);
1781 up_write(&sbi->gc_lock);
1783 f2fs_sync_fs(sbi->sb, 1);
1786 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1788 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1789 struct f2fs_mount_info org_mount_opt;
1790 unsigned long old_sb_flags;
1792 bool need_restart_gc = false;
1793 bool need_stop_gc = false;
1794 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
1795 bool disable_checkpoint = test_opt(sbi, DISABLE_CHECKPOINT);
1796 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
1797 bool no_atgc = !test_opt(sbi, ATGC);
1798 bool checkpoint_changed;
1804 * Save the old mount options in case we
1805 * need to restore them.
1807 org_mount_opt = sbi->mount_opt;
1808 old_sb_flags = sb->s_flags;
1811 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
1812 for (i = 0; i < MAXQUOTAS; i++) {
1813 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1814 org_mount_opt.s_qf_names[i] =
1815 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1817 if (!org_mount_opt.s_qf_names[i]) {
1818 for (j = 0; j < i; j++)
1819 kfree(org_mount_opt.s_qf_names[j]);
1823 org_mount_opt.s_qf_names[i] = NULL;
1828 /* recover superblocks we couldn't write due to previous RO mount */
1829 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
1830 err = f2fs_commit_super(sbi, false);
1831 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
1834 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1837 default_options(sbi);
1839 /* parse mount options */
1840 err = parse_options(sb, data, true);
1843 checkpoint_changed =
1844 disable_checkpoint != test_opt(sbi, DISABLE_CHECKPOINT);
1847 * Previous and new state of filesystem is RO,
1848 * so skip checking GC and FLUSH_MERGE conditions.
1850 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
1854 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
1855 err = dquot_suspend(sb, -1);
1858 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
1859 /* dquot_resume needs RW */
1860 sb->s_flags &= ~SB_RDONLY;
1861 if (sb_any_quota_suspended(sb)) {
1862 dquot_resume(sb, -1);
1863 } else if (f2fs_sb_has_quota_ino(sbi)) {
1864 err = f2fs_enable_quotas(sb);
1870 /* disallow enable atgc dynamically */
1871 if (no_atgc == !!test_opt(sbi, ATGC)) {
1873 f2fs_warn(sbi, "switch atgc option is not allowed");
1877 /* disallow enable/disable extent_cache dynamically */
1878 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1880 f2fs_warn(sbi, "switch extent_cache option is not allowed");
1884 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
1886 f2fs_warn(sbi, "switch io_bits option is not allowed");
1890 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
1892 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
1897 * We stop the GC thread if FS is mounted as RO
1898 * or if background_gc = off is passed in mount
1899 * option. Also sync the filesystem.
1901 if ((*flags & SB_RDONLY) ||
1902 F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF) {
1903 if (sbi->gc_thread) {
1904 f2fs_stop_gc_thread(sbi);
1905 need_restart_gc = true;
1907 } else if (!sbi->gc_thread) {
1908 err = f2fs_start_gc_thread(sbi);
1911 need_stop_gc = true;
1914 if (*flags & SB_RDONLY ||
1915 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
1918 set_sbi_flag(sbi, SBI_IS_DIRTY);
1919 set_sbi_flag(sbi, SBI_IS_CLOSE);
1920 f2fs_sync_fs(sb, 1);
1921 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1924 if (checkpoint_changed) {
1925 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
1926 err = f2fs_disable_checkpoint(sbi);
1930 f2fs_enable_checkpoint(sbi);
1935 * We stop issue flush thread if FS is mounted as RO
1936 * or if flush_merge is not passed in mount option.
1938 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
1939 clear_opt(sbi, FLUSH_MERGE);
1940 f2fs_destroy_flush_cmd_control(sbi, false);
1942 err = f2fs_create_flush_cmd_control(sbi);
1948 /* Release old quota file names */
1949 for (i = 0; i < MAXQUOTAS; i++)
1950 kfree(org_mount_opt.s_qf_names[i]);
1952 /* Update the POSIXACL Flag */
1953 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1954 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
1956 limit_reserve_root(sbi);
1957 adjust_unusable_cap_perc(sbi);
1958 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
1961 if (need_restart_gc) {
1962 if (f2fs_start_gc_thread(sbi))
1963 f2fs_warn(sbi, "background gc thread has stopped");
1964 } else if (need_stop_gc) {
1965 f2fs_stop_gc_thread(sbi);
1969 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
1970 for (i = 0; i < MAXQUOTAS; i++) {
1971 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1972 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
1975 sbi->mount_opt = org_mount_opt;
1976 sb->s_flags = old_sb_flags;
1981 /* Read data from quotafile */
1982 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1983 size_t len, loff_t off)
1985 struct inode *inode = sb_dqopt(sb)->files[type];
1986 struct address_space *mapping = inode->i_mapping;
1987 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1988 int offset = off & (sb->s_blocksize - 1);
1991 loff_t i_size = i_size_read(inode);
1998 if (off + len > i_size)
2001 while (toread > 0) {
2002 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2004 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
2006 if (PTR_ERR(page) == -ENOMEM) {
2007 congestion_wait(BLK_RW_ASYNC,
2008 DEFAULT_IO_TIMEOUT);
2011 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2012 return PTR_ERR(page);
2017 if (unlikely(page->mapping != mapping)) {
2018 f2fs_put_page(page, 1);
2021 if (unlikely(!PageUptodate(page))) {
2022 f2fs_put_page(page, 1);
2023 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2027 kaddr = kmap_atomic(page);
2028 memcpy(data, kaddr + offset, tocopy);
2029 kunmap_atomic(kaddr);
2030 f2fs_put_page(page, 1);
2040 /* Write to quotafile */
2041 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2042 const char *data, size_t len, loff_t off)
2044 struct inode *inode = sb_dqopt(sb)->files[type];
2045 struct address_space *mapping = inode->i_mapping;
2046 const struct address_space_operations *a_ops = mapping->a_ops;
2047 int offset = off & (sb->s_blocksize - 1);
2048 size_t towrite = len;
2050 void *fsdata = NULL;
2055 while (towrite > 0) {
2056 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2059 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
2061 if (unlikely(err)) {
2062 if (err == -ENOMEM) {
2063 congestion_wait(BLK_RW_ASYNC,
2064 DEFAULT_IO_TIMEOUT);
2067 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2071 kaddr = kmap_atomic(page);
2072 memcpy(kaddr + offset, data, tocopy);
2073 kunmap_atomic(kaddr);
2074 flush_dcache_page(page);
2076 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2087 inode->i_mtime = inode->i_ctime = current_time(inode);
2088 f2fs_mark_inode_dirty_sync(inode, false);
2089 return len - towrite;
2092 static struct dquot **f2fs_get_dquots(struct inode *inode)
2094 return F2FS_I(inode)->i_dquot;
2097 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2099 return &F2FS_I(inode)->i_reserved_quota;
2102 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2104 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2105 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2109 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2110 F2FS_OPTION(sbi).s_jquota_fmt, type);
2113 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2118 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2119 err = f2fs_enable_quotas(sbi->sb);
2121 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2127 for (i = 0; i < MAXQUOTAS; i++) {
2128 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2129 err = f2fs_quota_on_mount(sbi, i);
2134 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2141 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2144 struct inode *qf_inode;
2145 unsigned long qf_inum;
2148 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2150 qf_inum = f2fs_qf_ino(sb, type);
2154 qf_inode = f2fs_iget(sb, qf_inum);
2155 if (IS_ERR(qf_inode)) {
2156 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2157 return PTR_ERR(qf_inode);
2160 /* Don't account quota for quota files to avoid recursion */
2161 qf_inode->i_flags |= S_NOQUOTA;
2162 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2167 static int f2fs_enable_quotas(struct super_block *sb)
2169 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2171 unsigned long qf_inum;
2172 bool quota_mopt[MAXQUOTAS] = {
2173 test_opt(sbi, USRQUOTA),
2174 test_opt(sbi, GRPQUOTA),
2175 test_opt(sbi, PRJQUOTA),
2178 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2179 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2183 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2185 for (type = 0; type < MAXQUOTAS; type++) {
2186 qf_inum = f2fs_qf_ino(sb, type);
2188 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2189 DQUOT_USAGE_ENABLED |
2190 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2192 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2194 for (type--; type >= 0; type--)
2195 dquot_quota_off(sb, type);
2196 set_sbi_flag(F2FS_SB(sb),
2197 SBI_QUOTA_NEED_REPAIR);
2205 int f2fs_quota_sync(struct super_block *sb, int type)
2207 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2208 struct quota_info *dqopt = sb_dqopt(sb);
2215 * down_read(quota_sem)
2216 * dquot_writeback_dquots()
2219 * down_read(quota_sem)
2223 down_read(&sbi->quota_sem);
2224 ret = dquot_writeback_dquots(sb, type);
2229 * Now when everything is written we can discard the pagecache so
2230 * that userspace sees the changes.
2232 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2233 struct address_space *mapping;
2235 if (type != -1 && cnt != type)
2237 if (!sb_has_quota_active(sb, cnt))
2240 mapping = dqopt->files[cnt]->i_mapping;
2242 ret = filemap_fdatawrite(mapping);
2246 /* if we are using journalled quota */
2247 if (is_journalled_quota(sbi))
2250 ret = filemap_fdatawait(mapping);
2252 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2254 inode_lock(dqopt->files[cnt]);
2255 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2256 inode_unlock(dqopt->files[cnt]);
2260 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2261 up_read(&sbi->quota_sem);
2262 f2fs_unlock_op(sbi);
2266 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2267 const struct path *path)
2269 struct inode *inode;
2272 /* if quota sysfile exists, deny enabling quota with specific file */
2273 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2274 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2278 err = f2fs_quota_sync(sb, type);
2282 err = dquot_quota_on(sb, type, format_id, path);
2286 inode = d_inode(path->dentry);
2289 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
2290 f2fs_set_inode_flags(inode);
2291 inode_unlock(inode);
2292 f2fs_mark_inode_dirty_sync(inode, false);
2297 static int __f2fs_quota_off(struct super_block *sb, int type)
2299 struct inode *inode = sb_dqopt(sb)->files[type];
2302 if (!inode || !igrab(inode))
2303 return dquot_quota_off(sb, type);
2305 err = f2fs_quota_sync(sb, type);
2309 err = dquot_quota_off(sb, type);
2310 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
2314 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
2315 f2fs_set_inode_flags(inode);
2316 inode_unlock(inode);
2317 f2fs_mark_inode_dirty_sync(inode, false);
2323 static int f2fs_quota_off(struct super_block *sb, int type)
2325 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2328 err = __f2fs_quota_off(sb, type);
2331 * quotactl can shutdown journalled quota, result in inconsistence
2332 * between quota record and fs data by following updates, tag the
2333 * flag to let fsck be aware of it.
2335 if (is_journalled_quota(sbi))
2336 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2340 void f2fs_quota_off_umount(struct super_block *sb)
2345 for (type = 0; type < MAXQUOTAS; type++) {
2346 err = __f2fs_quota_off(sb, type);
2348 int ret = dquot_quota_off(sb, type);
2350 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
2352 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2356 * In case of checkpoint=disable, we must flush quota blocks.
2357 * This can cause NULL exception for node_inode in end_io, since
2358 * put_super already dropped it.
2360 sync_filesystem(sb);
2363 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
2365 struct quota_info *dqopt = sb_dqopt(sb);
2368 for (type = 0; type < MAXQUOTAS; type++) {
2369 if (!dqopt->files[type])
2371 f2fs_inode_synced(dqopt->files[type]);
2375 static int f2fs_dquot_commit(struct dquot *dquot)
2377 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2380 down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
2381 ret = dquot_commit(dquot);
2383 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2384 up_read(&sbi->quota_sem);
2388 static int f2fs_dquot_acquire(struct dquot *dquot)
2390 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2393 down_read(&sbi->quota_sem);
2394 ret = dquot_acquire(dquot);
2396 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2397 up_read(&sbi->quota_sem);
2401 static int f2fs_dquot_release(struct dquot *dquot)
2403 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2404 int ret = dquot_release(dquot);
2407 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2411 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
2413 struct super_block *sb = dquot->dq_sb;
2414 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2415 int ret = dquot_mark_dquot_dirty(dquot);
2417 /* if we are using journalled quota */
2418 if (is_journalled_quota(sbi))
2419 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
2424 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
2426 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2427 int ret = dquot_commit_info(sb, type);
2430 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2434 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
2436 *projid = F2FS_I(inode)->i_projid;
2440 static const struct dquot_operations f2fs_quota_operations = {
2441 .get_reserved_space = f2fs_get_reserved_space,
2442 .write_dquot = f2fs_dquot_commit,
2443 .acquire_dquot = f2fs_dquot_acquire,
2444 .release_dquot = f2fs_dquot_release,
2445 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
2446 .write_info = f2fs_dquot_commit_info,
2447 .alloc_dquot = dquot_alloc,
2448 .destroy_dquot = dquot_destroy,
2449 .get_projid = f2fs_get_projid,
2450 .get_next_id = dquot_get_next_id,
2453 static const struct quotactl_ops f2fs_quotactl_ops = {
2454 .quota_on = f2fs_quota_on,
2455 .quota_off = f2fs_quota_off,
2456 .quota_sync = f2fs_quota_sync,
2457 .get_state = dquot_get_state,
2458 .set_info = dquot_set_dqinfo,
2459 .get_dqblk = dquot_get_dqblk,
2460 .set_dqblk = dquot_set_dqblk,
2461 .get_nextdqblk = dquot_get_next_dqblk,
2464 int f2fs_quota_sync(struct super_block *sb, int type)
2469 void f2fs_quota_off_umount(struct super_block *sb)
2474 static const struct super_operations f2fs_sops = {
2475 .alloc_inode = f2fs_alloc_inode,
2476 .free_inode = f2fs_free_inode,
2477 .drop_inode = f2fs_drop_inode,
2478 .write_inode = f2fs_write_inode,
2479 .dirty_inode = f2fs_dirty_inode,
2480 .show_options = f2fs_show_options,
2482 .quota_read = f2fs_quota_read,
2483 .quota_write = f2fs_quota_write,
2484 .get_dquots = f2fs_get_dquots,
2486 .evict_inode = f2fs_evict_inode,
2487 .put_super = f2fs_put_super,
2488 .sync_fs = f2fs_sync_fs,
2489 .freeze_fs = f2fs_freeze,
2490 .unfreeze_fs = f2fs_unfreeze,
2491 .statfs = f2fs_statfs,
2492 .remount_fs = f2fs_remount,
2495 #ifdef CONFIG_FS_ENCRYPTION
2496 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
2498 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2499 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2503 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
2506 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2509 * Encrypting the root directory is not allowed because fsck
2510 * expects lost+found directory to exist and remain unencrypted
2511 * if LOST_FOUND feature is enabled.
2514 if (f2fs_sb_has_lost_found(sbi) &&
2515 inode->i_ino == F2FS_ROOT_INO(sbi))
2518 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2519 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2520 ctx, len, fs_data, XATTR_CREATE);
2523 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
2525 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
2528 static bool f2fs_has_stable_inodes(struct super_block *sb)
2533 static void f2fs_get_ino_and_lblk_bits(struct super_block *sb,
2534 int *ino_bits_ret, int *lblk_bits_ret)
2536 *ino_bits_ret = 8 * sizeof(nid_t);
2537 *lblk_bits_ret = 8 * sizeof(block_t);
2540 static int f2fs_get_num_devices(struct super_block *sb)
2542 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2544 if (f2fs_is_multi_device(sbi))
2545 return sbi->s_ndevs;
2549 static void f2fs_get_devices(struct super_block *sb,
2550 struct request_queue **devs)
2552 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2555 for (i = 0; i < sbi->s_ndevs; i++)
2556 devs[i] = bdev_get_queue(FDEV(i).bdev);
2559 static const struct fscrypt_operations f2fs_cryptops = {
2560 .key_prefix = "f2fs:",
2561 .get_context = f2fs_get_context,
2562 .set_context = f2fs_set_context,
2563 .get_dummy_policy = f2fs_get_dummy_policy,
2564 .empty_dir = f2fs_empty_dir,
2565 .max_namelen = F2FS_NAME_LEN,
2566 .has_stable_inodes = f2fs_has_stable_inodes,
2567 .get_ino_and_lblk_bits = f2fs_get_ino_and_lblk_bits,
2568 .get_num_devices = f2fs_get_num_devices,
2569 .get_devices = f2fs_get_devices,
2573 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2574 u64 ino, u32 generation)
2576 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2577 struct inode *inode;
2579 if (f2fs_check_nid_range(sbi, ino))
2580 return ERR_PTR(-ESTALE);
2583 * f2fs_iget isn't quite right if the inode is currently unallocated!
2584 * However f2fs_iget currently does appropriate checks to handle stale
2585 * inodes so everything is OK.
2587 inode = f2fs_iget(sb, ino);
2589 return ERR_CAST(inode);
2590 if (unlikely(generation && inode->i_generation != generation)) {
2591 /* we didn't find the right inode.. */
2593 return ERR_PTR(-ESTALE);
2598 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2599 int fh_len, int fh_type)
2601 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2602 f2fs_nfs_get_inode);
2605 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2606 int fh_len, int fh_type)
2608 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2609 f2fs_nfs_get_inode);
2612 static const struct export_operations f2fs_export_ops = {
2613 .fh_to_dentry = f2fs_fh_to_dentry,
2614 .fh_to_parent = f2fs_fh_to_parent,
2615 .get_parent = f2fs_get_parent,
2618 static loff_t max_file_blocks(void)
2621 loff_t leaf_count = DEF_ADDRS_PER_BLOCK;
2624 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
2625 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
2626 * space in inode.i_addr, it will be more safe to reassign
2630 /* two direct node blocks */
2631 result += (leaf_count * 2);
2633 /* two indirect node blocks */
2634 leaf_count *= NIDS_PER_BLOCK;
2635 result += (leaf_count * 2);
2637 /* one double indirect node block */
2638 leaf_count *= NIDS_PER_BLOCK;
2639 result += leaf_count;
2644 static int __f2fs_commit_super(struct buffer_head *bh,
2645 struct f2fs_super_block *super)
2649 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
2650 set_buffer_dirty(bh);
2653 /* it's rare case, we can do fua all the time */
2654 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
2657 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
2658 struct buffer_head *bh)
2660 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2661 (bh->b_data + F2FS_SUPER_OFFSET);
2662 struct super_block *sb = sbi->sb;
2663 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2664 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2665 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2666 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2667 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2668 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2669 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2670 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2671 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2672 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2673 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2674 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2675 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2676 u64 main_end_blkaddr = main_blkaddr +
2677 (segment_count_main << log_blocks_per_seg);
2678 u64 seg_end_blkaddr = segment0_blkaddr +
2679 (segment_count << log_blocks_per_seg);
2681 if (segment0_blkaddr != cp_blkaddr) {
2682 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2683 segment0_blkaddr, cp_blkaddr);
2687 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2689 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2690 cp_blkaddr, sit_blkaddr,
2691 segment_count_ckpt << log_blocks_per_seg);
2695 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2697 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2698 sit_blkaddr, nat_blkaddr,
2699 segment_count_sit << log_blocks_per_seg);
2703 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2705 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2706 nat_blkaddr, ssa_blkaddr,
2707 segment_count_nat << log_blocks_per_seg);
2711 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2713 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2714 ssa_blkaddr, main_blkaddr,
2715 segment_count_ssa << log_blocks_per_seg);
2719 if (main_end_blkaddr > seg_end_blkaddr) {
2720 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
2721 main_blkaddr, seg_end_blkaddr,
2722 segment_count_main << log_blocks_per_seg);
2724 } else if (main_end_blkaddr < seg_end_blkaddr) {
2728 /* fix in-memory information all the time */
2729 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2730 segment0_blkaddr) >> log_blocks_per_seg);
2732 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
2733 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2736 err = __f2fs_commit_super(bh, NULL);
2737 res = err ? "failed" : "done";
2739 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
2740 res, main_blkaddr, seg_end_blkaddr,
2741 segment_count_main << log_blocks_per_seg);
2748 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2749 struct buffer_head *bh)
2751 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
2752 block_t total_sections, blocks_per_seg;
2753 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2754 (bh->b_data + F2FS_SUPER_OFFSET);
2755 unsigned int blocksize;
2756 size_t crc_offset = 0;
2759 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
2760 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
2761 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2765 /* Check checksum_offset and crc in superblock */
2766 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
2767 crc_offset = le32_to_cpu(raw_super->checksum_offset);
2769 offsetof(struct f2fs_super_block, crc)) {
2770 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
2772 return -EFSCORRUPTED;
2774 crc = le32_to_cpu(raw_super->crc);
2775 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
2776 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
2777 return -EFSCORRUPTED;
2781 /* Currently, support only 4KB page cache size */
2782 if (F2FS_BLKSIZE != PAGE_SIZE) {
2783 f2fs_info(sbi, "Invalid page_cache_size (%lu), supports only 4KB",
2785 return -EFSCORRUPTED;
2788 /* Currently, support only 4KB block size */
2789 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
2790 if (blocksize != F2FS_BLKSIZE) {
2791 f2fs_info(sbi, "Invalid blocksize (%u), supports only 4KB",
2793 return -EFSCORRUPTED;
2796 /* check log blocks per segment */
2797 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2798 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
2799 le32_to_cpu(raw_super->log_blocks_per_seg));
2800 return -EFSCORRUPTED;
2803 /* Currently, support 512/1024/2048/4096 bytes sector size */
2804 if (le32_to_cpu(raw_super->log_sectorsize) >
2805 F2FS_MAX_LOG_SECTOR_SIZE ||
2806 le32_to_cpu(raw_super->log_sectorsize) <
2807 F2FS_MIN_LOG_SECTOR_SIZE) {
2808 f2fs_info(sbi, "Invalid log sectorsize (%u)",
2809 le32_to_cpu(raw_super->log_sectorsize));
2810 return -EFSCORRUPTED;
2812 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2813 le32_to_cpu(raw_super->log_sectorsize) !=
2814 F2FS_MAX_LOG_SECTOR_SIZE) {
2815 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
2816 le32_to_cpu(raw_super->log_sectors_per_block),
2817 le32_to_cpu(raw_super->log_sectorsize));
2818 return -EFSCORRUPTED;
2821 segment_count = le32_to_cpu(raw_super->segment_count);
2822 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2823 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2824 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2825 total_sections = le32_to_cpu(raw_super->section_count);
2827 /* blocks_per_seg should be 512, given the above check */
2828 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2830 if (segment_count > F2FS_MAX_SEGMENT ||
2831 segment_count < F2FS_MIN_SEGMENTS) {
2832 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
2833 return -EFSCORRUPTED;
2836 if (total_sections > segment_count_main || total_sections < 1 ||
2837 segs_per_sec > segment_count || !segs_per_sec) {
2838 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
2839 segment_count, total_sections, segs_per_sec);
2840 return -EFSCORRUPTED;
2843 if (segment_count_main != total_sections * segs_per_sec) {
2844 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
2845 segment_count_main, total_sections, segs_per_sec);
2846 return -EFSCORRUPTED;
2849 if ((segment_count / segs_per_sec) < total_sections) {
2850 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
2851 segment_count, segs_per_sec, total_sections);
2852 return -EFSCORRUPTED;
2855 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
2856 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
2857 segment_count, le64_to_cpu(raw_super->block_count));
2858 return -EFSCORRUPTED;
2861 if (RDEV(0).path[0]) {
2862 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
2865 while (i < MAX_DEVICES && RDEV(i).path[0]) {
2866 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
2869 if (segment_count != dev_seg_count) {
2870 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
2871 segment_count, dev_seg_count);
2872 return -EFSCORRUPTED;
2875 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
2876 !bdev_is_zoned(sbi->sb->s_bdev)) {
2877 f2fs_info(sbi, "Zoned block device path is missing");
2878 return -EFSCORRUPTED;
2882 if (secs_per_zone > total_sections || !secs_per_zone) {
2883 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
2884 secs_per_zone, total_sections);
2885 return -EFSCORRUPTED;
2887 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
2888 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
2889 (le32_to_cpu(raw_super->extension_count) +
2890 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
2891 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
2892 le32_to_cpu(raw_super->extension_count),
2893 raw_super->hot_ext_count,
2894 F2FS_MAX_EXTENSION);
2895 return -EFSCORRUPTED;
2898 if (le32_to_cpu(raw_super->cp_payload) >
2899 (blocks_per_seg - F2FS_CP_PACKS)) {
2900 f2fs_info(sbi, "Insane cp_payload (%u > %u)",
2901 le32_to_cpu(raw_super->cp_payload),
2902 blocks_per_seg - F2FS_CP_PACKS);
2903 return -EFSCORRUPTED;
2906 /* check reserved ino info */
2907 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2908 le32_to_cpu(raw_super->meta_ino) != 2 ||
2909 le32_to_cpu(raw_super->root_ino) != 3) {
2910 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2911 le32_to_cpu(raw_super->node_ino),
2912 le32_to_cpu(raw_super->meta_ino),
2913 le32_to_cpu(raw_super->root_ino));
2914 return -EFSCORRUPTED;
2917 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
2918 if (sanity_check_area_boundary(sbi, bh))
2919 return -EFSCORRUPTED;
2924 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
2926 unsigned int total, fsmeta;
2927 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2928 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2929 unsigned int ovp_segments, reserved_segments;
2930 unsigned int main_segs, blocks_per_seg;
2931 unsigned int sit_segs, nat_segs;
2932 unsigned int sit_bitmap_size, nat_bitmap_size;
2933 unsigned int log_blocks_per_seg;
2934 unsigned int segment_count_main;
2935 unsigned int cp_pack_start_sum, cp_payload;
2936 block_t user_block_count, valid_user_blocks;
2937 block_t avail_node_count, valid_node_count;
2940 total = le32_to_cpu(raw_super->segment_count);
2941 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2942 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
2944 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
2946 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2947 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2949 if (unlikely(fsmeta >= total))
2952 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2953 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2955 if (unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
2956 ovp_segments == 0 || reserved_segments == 0)) {
2957 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
2961 user_block_count = le64_to_cpu(ckpt->user_block_count);
2962 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2963 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2964 if (!user_block_count || user_block_count >=
2965 segment_count_main << log_blocks_per_seg) {
2966 f2fs_err(sbi, "Wrong user_block_count: %u",
2971 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
2972 if (valid_user_blocks > user_block_count) {
2973 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
2974 valid_user_blocks, user_block_count);
2978 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
2979 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
2980 if (valid_node_count > avail_node_count) {
2981 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
2982 valid_node_count, avail_node_count);
2986 main_segs = le32_to_cpu(raw_super->segment_count_main);
2987 blocks_per_seg = sbi->blocks_per_seg;
2989 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2990 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2991 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2993 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
2994 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2995 le32_to_cpu(ckpt->cur_node_segno[j])) {
2996 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
2998 le32_to_cpu(ckpt->cur_node_segno[i]));
3003 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3004 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3005 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3007 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3008 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3009 le32_to_cpu(ckpt->cur_data_segno[j])) {
3010 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3012 le32_to_cpu(ckpt->cur_data_segno[i]));
3017 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3018 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3019 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3020 le32_to_cpu(ckpt->cur_data_segno[j])) {
3021 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3023 le32_to_cpu(ckpt->cur_node_segno[i]));
3029 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3030 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3032 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3033 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3034 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3035 sit_bitmap_size, nat_bitmap_size);
3039 cp_pack_start_sum = __start_sum_addr(sbi);
3040 cp_payload = __cp_payload(sbi);
3041 if (cp_pack_start_sum < cp_payload + 1 ||
3042 cp_pack_start_sum > blocks_per_seg - 1 -
3043 NR_CURSEG_PERSIST_TYPE) {
3044 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3049 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3050 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3051 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3052 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3053 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3054 le32_to_cpu(ckpt->checksum_offset));
3058 if (unlikely(f2fs_cp_error(sbi))) {
3059 f2fs_err(sbi, "A bug case: need to run fsck");
3065 static void init_sb_info(struct f2fs_sb_info *sbi)
3067 struct f2fs_super_block *raw_super = sbi->raw_super;
3070 sbi->log_sectors_per_block =
3071 le32_to_cpu(raw_super->log_sectors_per_block);
3072 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3073 sbi->blocksize = 1 << sbi->log_blocksize;
3074 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3075 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
3076 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3077 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3078 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3079 sbi->total_node_count =
3080 (le32_to_cpu(raw_super->segment_count_nat) / 2)
3081 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
3082 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
3083 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
3084 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
3085 sbi->cur_victim_sec = NULL_SECNO;
3086 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3087 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3088 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3089 sbi->migration_granularity = sbi->segs_per_sec;
3091 sbi->dir_level = DEF_DIR_LEVEL;
3092 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3093 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3094 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3095 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3096 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3097 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3098 DEF_UMOUNT_DISCARD_TIMEOUT;
3099 clear_sbi_flag(sbi, SBI_NEED_FSCK);
3101 for (i = 0; i < NR_COUNT_TYPE; i++)
3102 atomic_set(&sbi->nr_pages[i], 0);
3104 for (i = 0; i < META; i++)
3105 atomic_set(&sbi->wb_sync_req[i], 0);
3107 INIT_LIST_HEAD(&sbi->s_list);
3108 mutex_init(&sbi->umount_mutex);
3109 init_rwsem(&sbi->io_order_lock);
3110 spin_lock_init(&sbi->cp_lock);
3112 sbi->dirty_device = 0;
3113 spin_lock_init(&sbi->dev_lock);
3115 init_rwsem(&sbi->sb_lock);
3116 init_rwsem(&sbi->pin_sem);
3119 static int init_percpu_info(struct f2fs_sb_info *sbi)
3123 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3127 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3130 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3135 #ifdef CONFIG_BLK_DEV_ZONED
3137 struct f2fs_report_zones_args {
3138 struct f2fs_dev_info *dev;
3139 bool zone_cap_mismatch;
3142 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3145 struct f2fs_report_zones_args *rz_args = data;
3147 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3150 set_bit(idx, rz_args->dev->blkz_seq);
3151 rz_args->dev->zone_capacity_blocks[idx] = zone->capacity >>
3152 F2FS_LOG_SECTORS_PER_BLOCK;
3153 if (zone->len != zone->capacity && !rz_args->zone_cap_mismatch)
3154 rz_args->zone_cap_mismatch = true;
3159 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
3161 struct block_device *bdev = FDEV(devi).bdev;
3162 sector_t nr_sectors = bdev->bd_part->nr_sects;
3163 struct f2fs_report_zones_args rep_zone_arg;
3166 if (!f2fs_sb_has_blkzoned(sbi))
3169 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
3170 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
3172 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
3173 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
3174 __ilog2_u32(sbi->blocks_per_blkz))
3176 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
3177 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
3178 sbi->log_blocks_per_blkz;
3179 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
3180 FDEV(devi).nr_blkz++;
3182 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
3183 BITS_TO_LONGS(FDEV(devi).nr_blkz)
3184 * sizeof(unsigned long),
3186 if (!FDEV(devi).blkz_seq)
3189 /* Get block zones type and zone-capacity */
3190 FDEV(devi).zone_capacity_blocks = f2fs_kzalloc(sbi,
3191 FDEV(devi).nr_blkz * sizeof(block_t),
3193 if (!FDEV(devi).zone_capacity_blocks)
3196 rep_zone_arg.dev = &FDEV(devi);
3197 rep_zone_arg.zone_cap_mismatch = false;
3199 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3204 if (!rep_zone_arg.zone_cap_mismatch) {
3205 kfree(FDEV(devi).zone_capacity_blocks);
3206 FDEV(devi).zone_capacity_blocks = NULL;
3214 * Read f2fs raw super block.
3215 * Because we have two copies of super block, so read both of them
3216 * to get the first valid one. If any one of them is broken, we pass
3217 * them recovery flag back to the caller.
3219 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3220 struct f2fs_super_block **raw_super,
3221 int *valid_super_block, int *recovery)
3223 struct super_block *sb = sbi->sb;
3225 struct buffer_head *bh;
3226 struct f2fs_super_block *super;
3229 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3233 for (block = 0; block < 2; block++) {
3234 bh = sb_bread(sb, block);
3236 f2fs_err(sbi, "Unable to read %dth superblock",
3243 /* sanity checking of raw super */
3244 err = sanity_check_raw_super(sbi, bh);
3246 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3254 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3256 *valid_super_block = block;
3262 /* No valid superblock */
3271 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3273 struct buffer_head *bh;
3277 if ((recover && f2fs_readonly(sbi->sb)) ||
3278 bdev_read_only(sbi->sb->s_bdev)) {
3279 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3283 /* we should update superblock crc here */
3284 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
3285 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3286 offsetof(struct f2fs_super_block, crc));
3287 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3290 /* write back-up superblock first */
3291 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
3294 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3297 /* if we are in recovery path, skip writing valid superblock */
3301 /* write current valid superblock */
3302 bh = sb_bread(sbi->sb, sbi->valid_super_block);
3305 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3310 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
3312 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3313 unsigned int max_devices = MAX_DEVICES;
3316 /* Initialize single device information */
3317 if (!RDEV(0).path[0]) {
3318 if (!bdev_is_zoned(sbi->sb->s_bdev))
3324 * Initialize multiple devices information, or single
3325 * zoned block device information.
3327 sbi->devs = f2fs_kzalloc(sbi,
3328 array_size(max_devices,
3329 sizeof(struct f2fs_dev_info)),
3334 for (i = 0; i < max_devices; i++) {
3336 if (i > 0 && !RDEV(i).path[0])
3339 if (max_devices == 1) {
3340 /* Single zoned block device mount */
3342 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
3343 sbi->sb->s_mode, sbi->sb->s_type);
3345 /* Multi-device mount */
3346 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
3347 FDEV(i).total_segments =
3348 le32_to_cpu(RDEV(i).total_segments);
3350 FDEV(i).start_blk = 0;
3351 FDEV(i).end_blk = FDEV(i).start_blk +
3352 (FDEV(i).total_segments <<
3353 sbi->log_blocks_per_seg) - 1 +
3354 le32_to_cpu(raw_super->segment0_blkaddr);
3356 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
3357 FDEV(i).end_blk = FDEV(i).start_blk +
3358 (FDEV(i).total_segments <<
3359 sbi->log_blocks_per_seg) - 1;
3361 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3362 sbi->sb->s_mode, sbi->sb->s_type);
3364 if (IS_ERR(FDEV(i).bdev))
3365 return PTR_ERR(FDEV(i).bdev);
3367 /* to release errored devices */
3368 sbi->s_ndevs = i + 1;
3370 #ifdef CONFIG_BLK_DEV_ZONED
3371 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
3372 !f2fs_sb_has_blkzoned(sbi)) {
3373 f2fs_err(sbi, "Zoned block device feature not enabled\n");
3376 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
3377 if (init_blkz_info(sbi, i)) {
3378 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
3381 if (max_devices == 1)
3383 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
3385 FDEV(i).total_segments,
3386 FDEV(i).start_blk, FDEV(i).end_blk,
3387 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
3388 "Host-aware" : "Host-managed");
3392 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
3394 FDEV(i).total_segments,
3395 FDEV(i).start_blk, FDEV(i).end_blk);
3398 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
3402 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
3404 #ifdef CONFIG_UNICODE
3405 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
3406 const struct f2fs_sb_encodings *encoding_info;
3407 struct unicode_map *encoding;
3408 __u16 encoding_flags;
3410 if (f2fs_sb_read_encoding(sbi->raw_super, &encoding_info,
3413 "Encoding requested by superblock is unknown");
3417 encoding = utf8_load(encoding_info->version);
3418 if (IS_ERR(encoding)) {
3420 "can't mount with superblock charset: %s-%s "
3421 "not supported by the kernel. flags: 0x%x.",
3422 encoding_info->name, encoding_info->version,
3424 return PTR_ERR(encoding);
3426 f2fs_info(sbi, "Using encoding defined by superblock: "
3427 "%s-%s with flags 0x%hx", encoding_info->name,
3428 encoding_info->version?:"\b", encoding_flags);
3430 sbi->sb->s_encoding = encoding;
3431 sbi->sb->s_encoding_flags = encoding_flags;
3434 if (f2fs_sb_has_casefold(sbi)) {
3435 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
3442 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
3444 struct f2fs_sm_info *sm_i = SM_I(sbi);
3446 /* adjust parameters according to the volume size */
3447 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
3448 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
3449 sm_i->dcc_info->discard_granularity = 1;
3450 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
3453 sbi->readdir_ra = 1;
3456 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
3458 struct f2fs_sb_info *sbi;
3459 struct f2fs_super_block *raw_super;
3462 bool skip_recovery = false, need_fsck = false;
3463 char *options = NULL;
3464 int recovery, i, valid_super_block;
3465 struct curseg_info *seg_i;
3471 valid_super_block = -1;
3474 /* allocate memory for f2fs-specific super block info */
3475 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
3481 /* Load the checksum driver */
3482 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
3483 if (IS_ERR(sbi->s_chksum_driver)) {
3484 f2fs_err(sbi, "Cannot load crc32 driver.");
3485 err = PTR_ERR(sbi->s_chksum_driver);
3486 sbi->s_chksum_driver = NULL;
3490 /* set a block size */
3491 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
3492 f2fs_err(sbi, "unable to set blocksize");
3496 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
3501 sb->s_fs_info = sbi;
3502 sbi->raw_super = raw_super;
3504 /* precompute checksum seed for metadata */
3505 if (f2fs_sb_has_inode_chksum(sbi))
3506 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
3507 sizeof(raw_super->uuid));
3509 default_options(sbi);
3510 /* parse mount options */
3511 options = kstrdup((const char *)data, GFP_KERNEL);
3512 if (data && !options) {
3517 err = parse_options(sb, options, false);
3521 sbi->max_file_blocks = max_file_blocks();
3522 sb->s_maxbytes = sbi->max_file_blocks <<
3523 le32_to_cpu(raw_super->log_blocksize);
3524 sb->s_max_links = F2FS_LINK_MAX;
3526 err = f2fs_setup_casefold(sbi);
3531 sb->dq_op = &f2fs_quota_operations;
3532 sb->s_qcop = &f2fs_quotactl_ops;
3533 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
3535 if (f2fs_sb_has_quota_ino(sbi)) {
3536 for (i = 0; i < MAXQUOTAS; i++) {
3537 if (f2fs_qf_ino(sbi->sb, i))
3538 sbi->nquota_files++;
3543 sb->s_op = &f2fs_sops;
3544 #ifdef CONFIG_FS_ENCRYPTION
3545 sb->s_cop = &f2fs_cryptops;
3547 #ifdef CONFIG_FS_VERITY
3548 sb->s_vop = &f2fs_verityops;
3550 sb->s_xattr = f2fs_xattr_handlers;
3551 sb->s_export_op = &f2fs_export_ops;
3552 sb->s_magic = F2FS_SUPER_MAGIC;
3553 sb->s_time_gran = 1;
3554 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
3555 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
3556 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
3557 sb->s_iflags |= SB_I_CGROUPWB;
3559 /* init f2fs-specific super block info */
3560 sbi->valid_super_block = valid_super_block;
3561 init_rwsem(&sbi->gc_lock);
3562 mutex_init(&sbi->writepages);
3563 init_rwsem(&sbi->cp_global_sem);
3564 init_rwsem(&sbi->node_write);
3565 init_rwsem(&sbi->node_change);
3567 /* disallow all the data/node/meta page writes */
3568 set_sbi_flag(sbi, SBI_POR_DOING);
3569 spin_lock_init(&sbi->stat_lock);
3571 /* init iostat info */
3572 spin_lock_init(&sbi->iostat_lock);
3573 sbi->iostat_enable = false;
3574 sbi->iostat_period_ms = DEFAULT_IOSTAT_PERIOD_MS;
3576 for (i = 0; i < NR_PAGE_TYPE; i++) {
3577 int n = (i == META) ? 1: NR_TEMP_TYPE;
3583 sizeof(struct f2fs_bio_info)),
3585 if (!sbi->write_io[i]) {
3590 for (j = HOT; j < n; j++) {
3591 init_rwsem(&sbi->write_io[i][j].io_rwsem);
3592 sbi->write_io[i][j].sbi = sbi;
3593 sbi->write_io[i][j].bio = NULL;
3594 spin_lock_init(&sbi->write_io[i][j].io_lock);
3595 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
3596 INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
3597 init_rwsem(&sbi->write_io[i][j].bio_list_lock);
3601 init_rwsem(&sbi->cp_rwsem);
3602 init_rwsem(&sbi->quota_sem);
3603 init_waitqueue_head(&sbi->cp_wait);
3606 err = init_percpu_info(sbi);
3610 if (F2FS_IO_ALIGNED(sbi)) {
3611 sbi->write_io_dummy =
3612 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
3613 if (!sbi->write_io_dummy) {
3619 /* init per sbi slab cache */
3620 err = f2fs_init_xattr_caches(sbi);
3623 err = f2fs_init_page_array_cache(sbi);
3625 goto free_xattr_cache;
3627 /* get an inode for meta space */
3628 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
3629 if (IS_ERR(sbi->meta_inode)) {
3630 f2fs_err(sbi, "Failed to read F2FS meta data inode");
3631 err = PTR_ERR(sbi->meta_inode);
3632 goto free_page_array_cache;
3635 err = f2fs_get_valid_checkpoint(sbi);
3637 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
3638 goto free_meta_inode;
3641 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
3642 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3643 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
3644 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3645 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
3648 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
3649 set_sbi_flag(sbi, SBI_NEED_FSCK);
3651 /* Initialize device list */
3652 err = f2fs_scan_devices(sbi);
3654 f2fs_err(sbi, "Failed to find devices");
3658 err = f2fs_init_post_read_wq(sbi);
3660 f2fs_err(sbi, "Failed to initialize post read workqueue");
3664 sbi->total_valid_node_count =
3665 le32_to_cpu(sbi->ckpt->valid_node_count);
3666 percpu_counter_set(&sbi->total_valid_inode_count,
3667 le32_to_cpu(sbi->ckpt->valid_inode_count));
3668 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
3669 sbi->total_valid_block_count =
3670 le64_to_cpu(sbi->ckpt->valid_block_count);
3671 sbi->last_valid_block_count = sbi->total_valid_block_count;
3672 sbi->reserved_blocks = 0;
3673 sbi->current_reserved_blocks = 0;
3674 limit_reserve_root(sbi);
3675 adjust_unusable_cap_perc(sbi);
3677 for (i = 0; i < NR_INODE_TYPE; i++) {
3678 INIT_LIST_HEAD(&sbi->inode_list[i]);
3679 spin_lock_init(&sbi->inode_lock[i]);
3681 mutex_init(&sbi->flush_lock);
3683 f2fs_init_extent_cache_info(sbi);
3685 f2fs_init_ino_entry_info(sbi);
3687 f2fs_init_fsync_node_info(sbi);
3689 /* setup f2fs internal modules */
3690 err = f2fs_build_segment_manager(sbi);
3692 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
3696 err = f2fs_build_node_manager(sbi);
3698 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
3703 /* For write statistics */
3704 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
3706 /* Read accumulated write IO statistics if exists */
3707 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3708 if (__exist_node_summaries(sbi))
3709 sbi->kbytes_written =
3710 le64_to_cpu(seg_i->journal->info.kbytes_written);
3712 f2fs_build_gc_manager(sbi);
3714 err = f2fs_build_stats(sbi);
3718 /* get an inode for node space */
3719 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3720 if (IS_ERR(sbi->node_inode)) {
3721 f2fs_err(sbi, "Failed to read node inode");
3722 err = PTR_ERR(sbi->node_inode);
3726 /* read root inode and dentry */
3727 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3729 f2fs_err(sbi, "Failed to read root inode");
3730 err = PTR_ERR(root);
3731 goto free_node_inode;
3733 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3734 !root->i_size || !root->i_nlink) {
3737 goto free_node_inode;
3740 sb->s_root = d_make_root(root); /* allocate root dentry */
3743 goto free_node_inode;
3746 err = f2fs_register_sysfs(sbi);
3748 goto free_root_inode;
3751 /* Enable quota usage during mount */
3752 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
3753 err = f2fs_enable_quotas(sb);
3755 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
3758 /* if there are any orphan inodes, free them */
3759 err = f2fs_recover_orphan_inodes(sbi);
3763 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
3764 goto reset_checkpoint;
3766 /* recover fsynced data */
3767 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
3768 !test_opt(sbi, NORECOVERY)) {
3770 * mount should be failed, when device has readonly mode, and
3771 * previous checkpoint was not done by clean system shutdown.
3773 if (f2fs_hw_is_readonly(sbi)) {
3774 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3776 f2fs_err(sbi, "Need to recover fsync data, but write access unavailable");
3779 f2fs_info(sbi, "write access unavailable, skipping recovery");
3780 goto reset_checkpoint;
3784 set_sbi_flag(sbi, SBI_NEED_FSCK);
3787 goto reset_checkpoint;
3789 err = f2fs_recover_fsync_data(sbi, false);
3792 skip_recovery = true;
3794 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
3799 err = f2fs_recover_fsync_data(sbi, true);
3801 if (!f2fs_readonly(sb) && err > 0) {
3803 f2fs_err(sbi, "Need to recover fsync data");
3809 * If the f2fs is not readonly and fsync data recovery succeeds,
3810 * check zoned block devices' write pointer consistency.
3812 if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
3813 err = f2fs_check_write_pointer(sbi);
3819 f2fs_init_inmem_curseg(sbi);
3821 /* f2fs_recover_fsync_data() cleared this already */
3822 clear_sbi_flag(sbi, SBI_POR_DOING);
3824 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
3825 err = f2fs_disable_checkpoint(sbi);
3827 goto sync_free_meta;
3828 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
3829 f2fs_enable_checkpoint(sbi);
3833 * If filesystem is not mounted as read-only then
3834 * do start the gc_thread.
3836 if (F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF && !f2fs_readonly(sb)) {
3837 /* After POR, we can run background GC thread.*/
3838 err = f2fs_start_gc_thread(sbi);
3840 goto sync_free_meta;
3844 /* recover broken superblock */
3846 err = f2fs_commit_super(sbi, true);
3847 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
3848 sbi->valid_super_block ? 1 : 2, err);
3851 f2fs_join_shrinker(sbi);
3853 f2fs_tuning_parameters(sbi);
3855 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
3856 cur_cp_version(F2FS_CKPT(sbi)));
3857 f2fs_update_time(sbi, CP_TIME);
3858 f2fs_update_time(sbi, REQ_TIME);
3859 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3863 /* safe to flush all the data */
3864 sync_filesystem(sbi->sb);
3869 f2fs_truncate_quota_inode_pages(sb);
3870 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
3871 f2fs_quota_off_umount(sbi->sb);
3874 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
3875 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
3876 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
3877 * falls into an infinite loop in f2fs_sync_meta_pages().
3879 truncate_inode_pages_final(META_MAPPING(sbi));
3880 /* evict some inodes being cached by GC */
3882 f2fs_unregister_sysfs(sbi);
3887 f2fs_release_ino_entry(sbi, true);
3888 truncate_inode_pages_final(NODE_MAPPING(sbi));
3889 iput(sbi->node_inode);
3890 sbi->node_inode = NULL;
3892 f2fs_destroy_stats(sbi);
3894 f2fs_destroy_node_manager(sbi);
3896 f2fs_destroy_segment_manager(sbi);
3897 f2fs_destroy_post_read_wq(sbi);
3899 destroy_device_list(sbi);
3902 make_bad_inode(sbi->meta_inode);
3903 iput(sbi->meta_inode);
3904 sbi->meta_inode = NULL;
3905 free_page_array_cache:
3906 f2fs_destroy_page_array_cache(sbi);
3908 f2fs_destroy_xattr_caches(sbi);
3910 mempool_destroy(sbi->write_io_dummy);
3912 destroy_percpu_info(sbi);
3914 for (i = 0; i < NR_PAGE_TYPE; i++)
3915 kvfree(sbi->write_io[i]);
3917 #ifdef CONFIG_UNICODE
3918 utf8_unload(sb->s_encoding);
3919 sb->s_encoding = NULL;
3923 for (i = 0; i < MAXQUOTAS; i++)
3924 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
3926 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
3931 if (sbi->s_chksum_driver)
3932 crypto_free_shash(sbi->s_chksum_driver);
3935 /* give only one another chance */
3936 if (retry_cnt > 0 && skip_recovery) {
3938 shrink_dcache_sb(sb);
3944 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
3945 const char *dev_name, void *data)
3947 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
3950 static void kill_f2fs_super(struct super_block *sb)
3953 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3955 set_sbi_flag(sbi, SBI_IS_CLOSE);
3956 f2fs_stop_gc_thread(sbi);
3957 f2fs_stop_discard_thread(sbi);
3959 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
3960 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3961 struct cp_control cpc = {
3962 .reason = CP_UMOUNT,
3964 f2fs_write_checkpoint(sbi, &cpc);
3967 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
3968 sb->s_flags &= ~SB_RDONLY;
3970 kill_block_super(sb);
3973 static struct file_system_type f2fs_fs_type = {
3974 .owner = THIS_MODULE,
3976 .mount = f2fs_mount,
3977 .kill_sb = kill_f2fs_super,
3978 .fs_flags = FS_REQUIRES_DEV,
3980 MODULE_ALIAS_FS("f2fs");
3982 static int __init init_inodecache(void)
3984 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
3985 sizeof(struct f2fs_inode_info), 0,
3986 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
3987 if (!f2fs_inode_cachep)
3992 static void destroy_inodecache(void)
3995 * Make sure all delayed rcu free inodes are flushed before we
3999 kmem_cache_destroy(f2fs_inode_cachep);
4002 static int __init init_f2fs_fs(void)
4006 if (PAGE_SIZE != F2FS_BLKSIZE) {
4007 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
4008 PAGE_SIZE, F2FS_BLKSIZE);
4012 f2fs_build_trace_ios();
4014 err = init_inodecache();
4017 err = f2fs_create_node_manager_caches();
4019 goto free_inodecache;
4020 err = f2fs_create_segment_manager_caches();
4022 goto free_node_manager_caches;
4023 err = f2fs_create_checkpoint_caches();
4025 goto free_segment_manager_caches;
4026 err = f2fs_create_extent_cache();
4028 goto free_checkpoint_caches;
4029 err = f2fs_create_garbage_collection_cache();
4031 goto free_extent_cache;
4032 err = f2fs_init_sysfs();
4034 goto free_garbage_collection_cache;
4035 err = register_shrinker(&f2fs_shrinker_info);
4038 err = register_filesystem(&f2fs_fs_type);
4041 f2fs_create_root_stats();
4042 err = f2fs_init_post_read_processing();
4044 goto free_root_stats;
4045 err = f2fs_init_bio_entry_cache();
4047 goto free_post_read;
4048 err = f2fs_init_bioset();
4050 goto free_bio_enrty_cache;
4051 err = f2fs_init_compress_mempool();
4054 err = f2fs_init_compress_cache();
4056 goto free_compress_mempool;
4058 free_compress_mempool:
4059 f2fs_destroy_compress_mempool();
4061 f2fs_destroy_bioset();
4062 free_bio_enrty_cache:
4063 f2fs_destroy_bio_entry_cache();
4065 f2fs_destroy_post_read_processing();
4067 f2fs_destroy_root_stats();
4068 unregister_filesystem(&f2fs_fs_type);
4070 unregister_shrinker(&f2fs_shrinker_info);
4073 free_garbage_collection_cache:
4074 f2fs_destroy_garbage_collection_cache();
4076 f2fs_destroy_extent_cache();
4077 free_checkpoint_caches:
4078 f2fs_destroy_checkpoint_caches();
4079 free_segment_manager_caches:
4080 f2fs_destroy_segment_manager_caches();
4081 free_node_manager_caches:
4082 f2fs_destroy_node_manager_caches();
4084 destroy_inodecache();
4089 static void __exit exit_f2fs_fs(void)
4091 f2fs_destroy_compress_cache();
4092 f2fs_destroy_compress_mempool();
4093 f2fs_destroy_bioset();
4094 f2fs_destroy_bio_entry_cache();
4095 f2fs_destroy_post_read_processing();
4096 f2fs_destroy_root_stats();
4097 unregister_filesystem(&f2fs_fs_type);
4098 unregister_shrinker(&f2fs_shrinker_info);
4100 f2fs_destroy_garbage_collection_cache();
4101 f2fs_destroy_extent_cache();
4102 f2fs_destroy_checkpoint_caches();
4103 f2fs_destroy_segment_manager_caches();
4104 f2fs_destroy_node_manager_caches();
4105 destroy_inodecache();
4106 f2fs_destroy_trace_ios();
4109 module_init(init_f2fs_fs)
4110 module_exit(exit_f2fs_fs)
4112 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
4113 MODULE_DESCRIPTION("Flash Friendly File System");
4114 MODULE_LICENSE("GPL");