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>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/f2fs.h>
38 static struct kmem_cache *f2fs_inode_cachep;
40 #ifdef CONFIG_F2FS_FAULT_INJECTION
42 const char *f2fs_fault_name[FAULT_MAX] = {
43 [FAULT_KMALLOC] = "kmalloc",
44 [FAULT_KVMALLOC] = "kvmalloc",
45 [FAULT_PAGE_ALLOC] = "page alloc",
46 [FAULT_PAGE_GET] = "page get",
47 [FAULT_ALLOC_BIO] = "alloc bio",
48 [FAULT_ALLOC_NID] = "alloc nid",
49 [FAULT_ORPHAN] = "orphan",
50 [FAULT_BLOCK] = "no more block",
51 [FAULT_DIR_DEPTH] = "too big dir depth",
52 [FAULT_EVICT_INODE] = "evict_inode fail",
53 [FAULT_TRUNCATE] = "truncate fail",
54 [FAULT_READ_IO] = "read IO error",
55 [FAULT_CHECKPOINT] = "checkpoint error",
56 [FAULT_DISCARD] = "discard error",
57 [FAULT_WRITE_IO] = "write IO error",
60 void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
63 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
66 atomic_set(&ffi->inject_ops, 0);
67 ffi->inject_rate = rate;
71 ffi->inject_type = type;
74 memset(ffi, 0, sizeof(struct f2fs_fault_info));
78 /* f2fs-wide shrinker description */
79 static struct shrinker f2fs_shrinker_info = {
80 .scan_objects = f2fs_shrink_scan,
81 .count_objects = f2fs_shrink_count,
82 .seeks = DEFAULT_SEEKS,
87 Opt_disable_roll_forward,
98 Opt_disable_ext_identify,
101 Opt_inline_xattr_size,
139 Opt_test_dummy_encryption,
140 Opt_checkpoint_disable,
141 Opt_checkpoint_disable_cap,
142 Opt_checkpoint_disable_cap_perc,
143 Opt_checkpoint_enable,
147 static match_table_t f2fs_tokens = {
148 {Opt_gc_background, "background_gc=%s"},
149 {Opt_disable_roll_forward, "disable_roll_forward"},
150 {Opt_norecovery, "norecovery"},
151 {Opt_discard, "discard"},
152 {Opt_nodiscard, "nodiscard"},
153 {Opt_noheap, "no_heap"},
155 {Opt_user_xattr, "user_xattr"},
156 {Opt_nouser_xattr, "nouser_xattr"},
158 {Opt_noacl, "noacl"},
159 {Opt_active_logs, "active_logs=%u"},
160 {Opt_disable_ext_identify, "disable_ext_identify"},
161 {Opt_inline_xattr, "inline_xattr"},
162 {Opt_noinline_xattr, "noinline_xattr"},
163 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
164 {Opt_inline_data, "inline_data"},
165 {Opt_inline_dentry, "inline_dentry"},
166 {Opt_noinline_dentry, "noinline_dentry"},
167 {Opt_flush_merge, "flush_merge"},
168 {Opt_noflush_merge, "noflush_merge"},
169 {Opt_nobarrier, "nobarrier"},
170 {Opt_fastboot, "fastboot"},
171 {Opt_extent_cache, "extent_cache"},
172 {Opt_noextent_cache, "noextent_cache"},
173 {Opt_noinline_data, "noinline_data"},
174 {Opt_data_flush, "data_flush"},
175 {Opt_reserve_root, "reserve_root=%u"},
176 {Opt_resgid, "resgid=%u"},
177 {Opt_resuid, "resuid=%u"},
178 {Opt_mode, "mode=%s"},
179 {Opt_io_size_bits, "io_bits=%u"},
180 {Opt_fault_injection, "fault_injection=%u"},
181 {Opt_fault_type, "fault_type=%u"},
182 {Opt_lazytime, "lazytime"},
183 {Opt_nolazytime, "nolazytime"},
184 {Opt_quota, "quota"},
185 {Opt_noquota, "noquota"},
186 {Opt_usrquota, "usrquota"},
187 {Opt_grpquota, "grpquota"},
188 {Opt_prjquota, "prjquota"},
189 {Opt_usrjquota, "usrjquota=%s"},
190 {Opt_grpjquota, "grpjquota=%s"},
191 {Opt_prjjquota, "prjjquota=%s"},
192 {Opt_offusrjquota, "usrjquota="},
193 {Opt_offgrpjquota, "grpjquota="},
194 {Opt_offprjjquota, "prjjquota="},
195 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
196 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
197 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
198 {Opt_whint, "whint_mode=%s"},
199 {Opt_alloc, "alloc_mode=%s"},
200 {Opt_fsync, "fsync_mode=%s"},
201 {Opt_test_dummy_encryption, "test_dummy_encryption"},
202 {Opt_checkpoint_disable, "checkpoint=disable"},
203 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
204 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
205 {Opt_checkpoint_enable, "checkpoint=enable"},
209 void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...)
211 struct va_format vaf;
217 level = printk_get_level(fmt);
218 vaf.fmt = printk_skip_level(fmt);
220 printk("%c%cF2FS-fs (%s): %pV\n",
221 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
226 #ifdef CONFIG_UNICODE
227 static const struct f2fs_sb_encodings {
231 } f2fs_sb_encoding_map[] = {
232 {F2FS_ENC_UTF8_12_1, "utf8", "12.1.0"},
235 static int f2fs_sb_read_encoding(const struct f2fs_super_block *sb,
236 const struct f2fs_sb_encodings **encoding,
239 __u16 magic = le16_to_cpu(sb->s_encoding);
242 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
243 if (magic == f2fs_sb_encoding_map[i].magic)
246 if (i >= ARRAY_SIZE(f2fs_sb_encoding_map))
249 *encoding = &f2fs_sb_encoding_map[i];
250 *flags = le16_to_cpu(sb->s_encoding_flags);
256 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
258 block_t limit = min((sbi->user_block_count >> 3),
259 sbi->user_block_count - sbi->reserved_blocks);
262 if (test_opt(sbi, RESERVE_ROOT) &&
263 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
264 F2FS_OPTION(sbi).root_reserved_blocks = limit;
265 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
266 F2FS_OPTION(sbi).root_reserved_blocks);
268 if (!test_opt(sbi, RESERVE_ROOT) &&
269 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
270 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
271 !gid_eq(F2FS_OPTION(sbi).s_resgid,
272 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
273 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
274 from_kuid_munged(&init_user_ns,
275 F2FS_OPTION(sbi).s_resuid),
276 from_kgid_munged(&init_user_ns,
277 F2FS_OPTION(sbi).s_resgid));
280 static inline int adjust_reserved_segment(struct f2fs_sb_info *sbi)
282 unsigned int sec_blks = sbi->blocks_per_seg * sbi->segs_per_sec;
283 unsigned int avg_vblocks;
284 unsigned int wanted_reserved_segments;
285 block_t avail_user_block_count;
287 if (!F2FS_IO_ALIGNED(sbi))
290 /* average valid block count in section in worst case */
291 avg_vblocks = sec_blks / F2FS_IO_SIZE(sbi);
294 * we need enough free space when migrating one section in worst case
296 wanted_reserved_segments = (F2FS_IO_SIZE(sbi) / avg_vblocks) *
297 reserved_segments(sbi);
298 wanted_reserved_segments -= reserved_segments(sbi);
300 avail_user_block_count = sbi->user_block_count -
301 sbi->current_reserved_blocks -
302 F2FS_OPTION(sbi).root_reserved_blocks;
304 if (wanted_reserved_segments * sbi->blocks_per_seg >
305 avail_user_block_count) {
306 f2fs_err(sbi, "IO align feature can't grab additional reserved segment: %u, available segments: %u",
307 wanted_reserved_segments,
308 avail_user_block_count >> sbi->log_blocks_per_seg);
312 SM_I(sbi)->additional_reserved_segments = wanted_reserved_segments;
314 f2fs_info(sbi, "IO align feature needs additional reserved segment: %u",
315 wanted_reserved_segments);
320 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
322 if (!F2FS_OPTION(sbi).unusable_cap_perc)
325 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
326 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
328 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
329 F2FS_OPTION(sbi).unusable_cap_perc;
331 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
332 F2FS_OPTION(sbi).unusable_cap,
333 F2FS_OPTION(sbi).unusable_cap_perc);
336 static void init_once(void *foo)
338 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
340 inode_init_once(&fi->vfs_inode);
344 static const char * const quotatypes[] = INITQFNAMES;
345 #define QTYPE2NAME(t) (quotatypes[t])
346 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
349 struct f2fs_sb_info *sbi = F2FS_SB(sb);
353 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
354 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
357 if (f2fs_sb_has_quota_ino(sbi)) {
358 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
362 qname = match_strdup(args);
364 f2fs_err(sbi, "Not enough memory for storing quotafile name");
367 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
368 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
371 f2fs_err(sbi, "%s quota file already specified",
375 if (strchr(qname, '/')) {
376 f2fs_err(sbi, "quotafile must be on filesystem root");
379 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
387 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
389 struct f2fs_sb_info *sbi = F2FS_SB(sb);
391 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
392 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
395 kvfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
396 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
400 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
403 * We do the test below only for project quotas. 'usrquota' and
404 * 'grpquota' mount options are allowed even without quota feature
405 * to support legacy quotas in quota files.
407 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
408 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
411 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
412 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
413 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
414 if (test_opt(sbi, USRQUOTA) &&
415 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
416 clear_opt(sbi, USRQUOTA);
418 if (test_opt(sbi, GRPQUOTA) &&
419 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
420 clear_opt(sbi, GRPQUOTA);
422 if (test_opt(sbi, PRJQUOTA) &&
423 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
424 clear_opt(sbi, PRJQUOTA);
426 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
427 test_opt(sbi, PRJQUOTA)) {
428 f2fs_err(sbi, "old and new quota format mixing");
432 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
433 f2fs_err(sbi, "journaled quota format not specified");
438 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
439 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
440 F2FS_OPTION(sbi).s_jquota_fmt = 0;
446 static int parse_options(struct super_block *sb, char *options)
448 struct f2fs_sb_info *sbi = F2FS_SB(sb);
449 substring_t args[MAX_OPT_ARGS];
461 while ((p = strsep(&options, ",")) != NULL) {
466 * Initialize args struct so we know whether arg was
467 * found; some options take optional arguments.
469 args[0].to = args[0].from = NULL;
470 token = match_token(p, f2fs_tokens, args);
473 case Opt_gc_background:
474 name = match_strdup(&args[0]);
478 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
480 clear_opt(sbi, FORCE_FG_GC);
481 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
482 clear_opt(sbi, BG_GC);
483 clear_opt(sbi, FORCE_FG_GC);
484 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
486 set_opt(sbi, FORCE_FG_GC);
493 case Opt_disable_roll_forward:
494 set_opt(sbi, DISABLE_ROLL_FORWARD);
497 /* this option mounts f2fs with ro */
498 set_opt(sbi, NORECOVERY);
499 if (!f2fs_readonly(sb))
503 set_opt(sbi, DISCARD);
506 if (f2fs_sb_has_blkzoned(sbi)) {
507 f2fs_warn(sbi, "discard is required for zoned block devices");
510 clear_opt(sbi, DISCARD);
513 set_opt(sbi, NOHEAP);
516 clear_opt(sbi, NOHEAP);
518 #ifdef CONFIG_F2FS_FS_XATTR
520 set_opt(sbi, XATTR_USER);
522 case Opt_nouser_xattr:
523 clear_opt(sbi, XATTR_USER);
525 case Opt_inline_xattr:
526 set_opt(sbi, INLINE_XATTR);
528 case Opt_noinline_xattr:
529 clear_opt(sbi, INLINE_XATTR);
531 case Opt_inline_xattr_size:
532 if (args->from && match_int(args, &arg))
534 set_opt(sbi, INLINE_XATTR_SIZE);
535 F2FS_OPTION(sbi).inline_xattr_size = arg;
539 f2fs_info(sbi, "user_xattr options not supported");
541 case Opt_nouser_xattr:
542 f2fs_info(sbi, "nouser_xattr options not supported");
544 case Opt_inline_xattr:
545 f2fs_info(sbi, "inline_xattr options not supported");
547 case Opt_noinline_xattr:
548 f2fs_info(sbi, "noinline_xattr options not supported");
551 #ifdef CONFIG_F2FS_FS_POSIX_ACL
553 set_opt(sbi, POSIX_ACL);
556 clear_opt(sbi, POSIX_ACL);
560 f2fs_info(sbi, "acl options not supported");
563 f2fs_info(sbi, "noacl options not supported");
566 case Opt_active_logs:
567 if (args->from && match_int(args, &arg))
569 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
571 F2FS_OPTION(sbi).active_logs = arg;
573 case Opt_disable_ext_identify:
574 set_opt(sbi, DISABLE_EXT_IDENTIFY);
576 case Opt_inline_data:
577 set_opt(sbi, INLINE_DATA);
579 case Opt_inline_dentry:
580 set_opt(sbi, INLINE_DENTRY);
582 case Opt_noinline_dentry:
583 clear_opt(sbi, INLINE_DENTRY);
585 case Opt_flush_merge:
586 set_opt(sbi, FLUSH_MERGE);
588 case Opt_noflush_merge:
589 clear_opt(sbi, FLUSH_MERGE);
592 set_opt(sbi, NOBARRIER);
595 set_opt(sbi, FASTBOOT);
597 case Opt_extent_cache:
598 set_opt(sbi, EXTENT_CACHE);
600 case Opt_noextent_cache:
601 clear_opt(sbi, EXTENT_CACHE);
603 case Opt_noinline_data:
604 clear_opt(sbi, INLINE_DATA);
607 set_opt(sbi, DATA_FLUSH);
609 case Opt_reserve_root:
610 if (args->from && match_int(args, &arg))
612 if (test_opt(sbi, RESERVE_ROOT)) {
613 f2fs_info(sbi, "Preserve previous reserve_root=%u",
614 F2FS_OPTION(sbi).root_reserved_blocks);
616 F2FS_OPTION(sbi).root_reserved_blocks = arg;
617 set_opt(sbi, RESERVE_ROOT);
621 if (args->from && match_int(args, &arg))
623 uid = make_kuid(current_user_ns(), arg);
624 if (!uid_valid(uid)) {
625 f2fs_err(sbi, "Invalid uid value %d", arg);
628 F2FS_OPTION(sbi).s_resuid = uid;
631 if (args->from && match_int(args, &arg))
633 gid = make_kgid(current_user_ns(), arg);
634 if (!gid_valid(gid)) {
635 f2fs_err(sbi, "Invalid gid value %d", arg);
638 F2FS_OPTION(sbi).s_resgid = gid;
641 name = match_strdup(&args[0]);
645 if (strlen(name) == 8 &&
646 !strncmp(name, "adaptive", 8)) {
647 if (f2fs_sb_has_blkzoned(sbi)) {
648 f2fs_warn(sbi, "adaptive mode is not allowed with zoned block device feature");
652 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
653 } else if (strlen(name) == 3 &&
654 !strncmp(name, "lfs", 3)) {
655 set_opt_mode(sbi, F2FS_MOUNT_LFS);
662 case Opt_io_size_bits:
663 if (args->from && match_int(args, &arg))
665 if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_PAGES)) {
666 f2fs_warn(sbi, "Not support %d, larger than %d",
667 1 << arg, BIO_MAX_PAGES);
670 F2FS_OPTION(sbi).write_io_size_bits = arg;
672 #ifdef CONFIG_F2FS_FAULT_INJECTION
673 case Opt_fault_injection:
674 if (args->from && match_int(args, &arg))
676 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
677 set_opt(sbi, FAULT_INJECTION);
681 if (args->from && match_int(args, &arg))
683 f2fs_build_fault_attr(sbi, 0, arg);
684 set_opt(sbi, FAULT_INJECTION);
687 case Opt_fault_injection:
688 f2fs_info(sbi, "fault_injection options not supported");
692 f2fs_info(sbi, "fault_type options not supported");
696 sb->s_flags |= SB_LAZYTIME;
699 sb->s_flags &= ~SB_LAZYTIME;
704 set_opt(sbi, USRQUOTA);
707 set_opt(sbi, GRPQUOTA);
710 set_opt(sbi, PRJQUOTA);
713 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
718 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
723 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
727 case Opt_offusrjquota:
728 ret = f2fs_clear_qf_name(sb, USRQUOTA);
732 case Opt_offgrpjquota:
733 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
737 case Opt_offprjjquota:
738 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
742 case Opt_jqfmt_vfsold:
743 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
745 case Opt_jqfmt_vfsv0:
746 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
748 case Opt_jqfmt_vfsv1:
749 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
752 clear_opt(sbi, QUOTA);
753 clear_opt(sbi, USRQUOTA);
754 clear_opt(sbi, GRPQUOTA);
755 clear_opt(sbi, PRJQUOTA);
765 case Opt_offusrjquota:
766 case Opt_offgrpjquota:
767 case Opt_offprjjquota:
768 case Opt_jqfmt_vfsold:
769 case Opt_jqfmt_vfsv0:
770 case Opt_jqfmt_vfsv1:
772 f2fs_info(sbi, "quota operations not supported");
776 name = match_strdup(&args[0]);
779 if (strlen(name) == 10 &&
780 !strncmp(name, "user-based", 10)) {
781 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
782 } else if (strlen(name) == 3 &&
783 !strncmp(name, "off", 3)) {
784 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
785 } else if (strlen(name) == 8 &&
786 !strncmp(name, "fs-based", 8)) {
787 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
795 name = match_strdup(&args[0]);
799 if (strlen(name) == 7 &&
800 !strncmp(name, "default", 7)) {
801 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
802 } else if (strlen(name) == 5 &&
803 !strncmp(name, "reuse", 5)) {
804 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
812 name = match_strdup(&args[0]);
815 if (strlen(name) == 5 &&
816 !strncmp(name, "posix", 5)) {
817 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
818 } else if (strlen(name) == 6 &&
819 !strncmp(name, "strict", 6)) {
820 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
821 } else if (strlen(name) == 9 &&
822 !strncmp(name, "nobarrier", 9)) {
823 F2FS_OPTION(sbi).fsync_mode =
824 FSYNC_MODE_NOBARRIER;
831 case Opt_test_dummy_encryption:
832 #ifdef CONFIG_FS_ENCRYPTION
833 if (!f2fs_sb_has_encrypt(sbi)) {
834 f2fs_err(sbi, "Encrypt feature is off");
838 F2FS_OPTION(sbi).test_dummy_encryption = true;
839 f2fs_info(sbi, "Test dummy encryption mode enabled");
841 f2fs_info(sbi, "Test dummy encryption mount option ignored");
844 case Opt_checkpoint_disable_cap_perc:
845 if (args->from && match_int(args, &arg))
847 if (arg < 0 || arg > 100)
849 F2FS_OPTION(sbi).unusable_cap_perc = arg;
850 set_opt(sbi, DISABLE_CHECKPOINT);
852 case Opt_checkpoint_disable_cap:
853 if (args->from && match_int(args, &arg))
855 F2FS_OPTION(sbi).unusable_cap = arg;
856 set_opt(sbi, DISABLE_CHECKPOINT);
858 case Opt_checkpoint_disable:
859 set_opt(sbi, DISABLE_CHECKPOINT);
861 case Opt_checkpoint_enable:
862 clear_opt(sbi, DISABLE_CHECKPOINT);
865 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
871 if (f2fs_check_quota_options(sbi))
874 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
875 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
878 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
879 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
883 #ifndef CONFIG_UNICODE
884 if (f2fs_sb_has_casefold(sbi)) {
886 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
891 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
892 f2fs_err(sbi, "Should set mode=lfs with %uKB-sized IO",
893 F2FS_IO_SIZE_KB(sbi));
897 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
898 int min_size, max_size;
900 if (!f2fs_sb_has_extra_attr(sbi) ||
901 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
902 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
905 if (!test_opt(sbi, INLINE_XATTR)) {
906 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
910 min_size = sizeof(struct f2fs_xattr_header) / sizeof(__le32);
911 max_size = MAX_INLINE_XATTR_SIZE;
913 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
914 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
915 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
921 if (test_opt(sbi, DISABLE_CHECKPOINT) && test_opt(sbi, LFS)) {
922 f2fs_err(sbi, "LFS not compatible with checkpoint=disable\n");
926 /* Not pass down write hints if the number of active logs is lesser
927 * than NR_CURSEG_TYPE.
929 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
930 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
934 static struct inode *f2fs_alloc_inode(struct super_block *sb)
936 struct f2fs_inode_info *fi;
938 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
942 init_once((void *) fi);
944 /* Initialize f2fs-specific inode info */
945 atomic_set(&fi->dirty_pages, 0);
946 init_rwsem(&fi->i_sem);
947 INIT_LIST_HEAD(&fi->dirty_list);
948 INIT_LIST_HEAD(&fi->gdirty_list);
949 INIT_LIST_HEAD(&fi->inmem_ilist);
950 INIT_LIST_HEAD(&fi->inmem_pages);
951 mutex_init(&fi->inmem_lock);
952 init_rwsem(&fi->i_gc_rwsem[READ]);
953 init_rwsem(&fi->i_gc_rwsem[WRITE]);
954 init_rwsem(&fi->i_mmap_sem);
955 init_rwsem(&fi->i_xattr_sem);
957 /* Will be used by directory only */
958 fi->i_dir_level = F2FS_SB(sb)->dir_level;
960 return &fi->vfs_inode;
963 static int f2fs_drop_inode(struct inode *inode)
965 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
969 * during filesystem shutdown, if checkpoint is disabled,
970 * drop useless meta/node dirty pages.
972 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
973 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
974 inode->i_ino == F2FS_META_INO(sbi)) {
975 trace_f2fs_drop_inode(inode, 1);
981 * This is to avoid a deadlock condition like below.
982 * writeback_single_inode(inode)
983 * - f2fs_write_data_page
984 * - f2fs_gc -> iput -> evict
985 * - inode_wait_for_writeback(inode)
987 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
988 if (!inode->i_nlink && !is_bad_inode(inode)) {
989 /* to avoid evict_inode call simultaneously */
990 atomic_inc(&inode->i_count);
991 spin_unlock(&inode->i_lock);
993 /* some remained atomic pages should discarded */
994 if (f2fs_is_atomic_file(inode))
995 f2fs_drop_inmem_pages(inode);
997 /* should remain fi->extent_tree for writepage */
998 f2fs_destroy_extent_node(inode);
1000 sb_start_intwrite(inode->i_sb);
1001 f2fs_i_size_write(inode, 0);
1003 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1004 inode, NULL, 0, DATA);
1005 truncate_inode_pages_final(inode->i_mapping);
1007 if (F2FS_HAS_BLOCKS(inode))
1008 f2fs_truncate(inode);
1010 sb_end_intwrite(inode->i_sb);
1012 spin_lock(&inode->i_lock);
1013 atomic_dec(&inode->i_count);
1015 trace_f2fs_drop_inode(inode, 0);
1018 ret = generic_drop_inode(inode);
1020 ret = fscrypt_drop_inode(inode);
1021 trace_f2fs_drop_inode(inode, ret);
1025 int f2fs_inode_dirtied(struct inode *inode, bool sync)
1027 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1030 spin_lock(&sbi->inode_lock[DIRTY_META]);
1031 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1034 set_inode_flag(inode, FI_DIRTY_INODE);
1035 stat_inc_dirty_inode(sbi, DIRTY_META);
1037 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1038 list_add_tail(&F2FS_I(inode)->gdirty_list,
1039 &sbi->inode_list[DIRTY_META]);
1040 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1042 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1046 void f2fs_inode_synced(struct inode *inode)
1048 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1050 spin_lock(&sbi->inode_lock[DIRTY_META]);
1051 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1052 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1055 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1056 list_del_init(&F2FS_I(inode)->gdirty_list);
1057 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1059 clear_inode_flag(inode, FI_DIRTY_INODE);
1060 clear_inode_flag(inode, FI_AUTO_RECOVER);
1061 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1062 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1066 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1068 * We should call set_dirty_inode to write the dirty inode through write_inode.
1070 static void f2fs_dirty_inode(struct inode *inode, int flags)
1072 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1074 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1075 inode->i_ino == F2FS_META_INO(sbi))
1078 if (flags == I_DIRTY_TIME)
1081 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1082 clear_inode_flag(inode, FI_AUTO_RECOVER);
1084 f2fs_inode_dirtied(inode, false);
1087 static void f2fs_free_inode(struct inode *inode)
1089 fscrypt_free_inode(inode);
1090 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1093 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1095 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1096 percpu_counter_destroy(&sbi->total_valid_inode_count);
1099 static void destroy_device_list(struct f2fs_sb_info *sbi)
1103 for (i = 0; i < sbi->s_ndevs; i++) {
1104 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
1105 #ifdef CONFIG_BLK_DEV_ZONED
1106 kvfree(FDEV(i).blkz_seq);
1112 static void f2fs_put_super(struct super_block *sb)
1114 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1118 /* unregister procfs/sysfs entries in advance to avoid race case */
1119 f2fs_unregister_sysfs(sbi);
1121 f2fs_quota_off_umount(sb);
1123 /* prevent remaining shrinker jobs */
1124 mutex_lock(&sbi->umount_mutex);
1127 * We don't need to do checkpoint when superblock is clean.
1128 * But, the previous checkpoint was not done by umount, it needs to do
1129 * clean checkpoint again.
1131 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1132 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1133 struct cp_control cpc = {
1134 .reason = CP_UMOUNT,
1136 f2fs_write_checkpoint(sbi, &cpc);
1139 /* be sure to wait for any on-going discard commands */
1140 dropped = f2fs_issue_discard_timeout(sbi);
1142 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1143 !sbi->discard_blks && !dropped) {
1144 struct cp_control cpc = {
1145 .reason = CP_UMOUNT | CP_TRIMMED,
1147 f2fs_write_checkpoint(sbi, &cpc);
1151 * normally superblock is clean, so we need to release this.
1152 * In addition, EIO will skip do checkpoint, we need this as well.
1154 f2fs_release_ino_entry(sbi, true);
1156 f2fs_leave_shrinker(sbi);
1157 mutex_unlock(&sbi->umount_mutex);
1159 /* our cp_error case, we can wait for any writeback page */
1160 f2fs_flush_merged_writes(sbi);
1162 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1164 f2fs_bug_on(sbi, sbi->fsync_node_num);
1166 iput(sbi->node_inode);
1167 sbi->node_inode = NULL;
1169 iput(sbi->meta_inode);
1170 sbi->meta_inode = NULL;
1173 * iput() can update stat information, if f2fs_write_checkpoint()
1174 * above failed with error.
1176 f2fs_destroy_stats(sbi);
1178 /* destroy f2fs internal modules */
1179 f2fs_destroy_node_manager(sbi);
1180 f2fs_destroy_segment_manager(sbi);
1184 sb->s_fs_info = NULL;
1185 if (sbi->s_chksum_driver)
1186 crypto_free_shash(sbi->s_chksum_driver);
1187 kvfree(sbi->raw_super);
1189 destroy_device_list(sbi);
1190 mempool_destroy(sbi->write_io_dummy);
1192 for (i = 0; i < MAXQUOTAS; i++)
1193 kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
1195 destroy_percpu_info(sbi);
1196 for (i = 0; i < NR_PAGE_TYPE; i++)
1197 kvfree(sbi->write_io[i]);
1198 #ifdef CONFIG_UNICODE
1199 utf8_unload(sbi->s_encoding);
1204 int f2fs_sync_fs(struct super_block *sb, int sync)
1206 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1209 if (unlikely(f2fs_cp_error(sbi)))
1211 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1214 trace_f2fs_sync_fs(sb, sync);
1216 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1220 struct cp_control cpc;
1222 cpc.reason = __get_cp_reason(sbi);
1224 mutex_lock(&sbi->gc_mutex);
1225 err = f2fs_write_checkpoint(sbi, &cpc);
1226 mutex_unlock(&sbi->gc_mutex);
1228 f2fs_trace_ios(NULL, 1);
1233 static int f2fs_freeze(struct super_block *sb)
1235 if (f2fs_readonly(sb))
1238 /* IO error happened before */
1239 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1242 /* must be clean, since sync_filesystem() was already called */
1243 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1248 static int f2fs_unfreeze(struct super_block *sb)
1254 static int f2fs_statfs_project(struct super_block *sb,
1255 kprojid_t projid, struct kstatfs *buf)
1258 struct dquot *dquot;
1262 qid = make_kqid_projid(projid);
1263 dquot = dqget(sb, qid);
1265 return PTR_ERR(dquot);
1266 spin_lock(&dquot->dq_dqb_lock);
1268 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1269 dquot->dq_dqb.dqb_bhardlimit);
1271 limit >>= sb->s_blocksize_bits;
1273 if (limit && buf->f_blocks > limit) {
1274 curblock = (dquot->dq_dqb.dqb_curspace +
1275 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1276 buf->f_blocks = limit;
1277 buf->f_bfree = buf->f_bavail =
1278 (buf->f_blocks > curblock) ?
1279 (buf->f_blocks - curblock) : 0;
1282 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1283 dquot->dq_dqb.dqb_ihardlimit);
1285 if (limit && buf->f_files > limit) {
1286 buf->f_files = limit;
1288 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1289 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1292 spin_unlock(&dquot->dq_dqb_lock);
1298 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1300 struct super_block *sb = dentry->d_sb;
1301 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1302 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1303 block_t total_count, user_block_count, start_count;
1304 u64 avail_node_count;
1306 total_count = le64_to_cpu(sbi->raw_super->block_count);
1307 user_block_count = sbi->user_block_count;
1308 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1309 buf->f_type = F2FS_SUPER_MAGIC;
1310 buf->f_bsize = sbi->blocksize;
1312 buf->f_blocks = total_count - start_count;
1313 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1314 sbi->current_reserved_blocks;
1316 spin_lock(&sbi->stat_lock);
1317 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1320 buf->f_bfree -= sbi->unusable_block_count;
1321 spin_unlock(&sbi->stat_lock);
1323 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1324 buf->f_bavail = buf->f_bfree -
1325 F2FS_OPTION(sbi).root_reserved_blocks;
1329 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1331 if (avail_node_count > user_block_count) {
1332 buf->f_files = user_block_count;
1333 buf->f_ffree = buf->f_bavail;
1335 buf->f_files = avail_node_count;
1336 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1340 buf->f_namelen = F2FS_NAME_LEN;
1341 buf->f_fsid.val[0] = (u32)id;
1342 buf->f_fsid.val[1] = (u32)(id >> 32);
1345 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1346 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1347 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1353 static inline void f2fs_show_quota_options(struct seq_file *seq,
1354 struct super_block *sb)
1357 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1359 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1362 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1373 seq_printf(seq, ",jqfmt=%s", fmtname);
1376 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1377 seq_show_option(seq, "usrjquota",
1378 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1380 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1381 seq_show_option(seq, "grpjquota",
1382 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1384 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1385 seq_show_option(seq, "prjjquota",
1386 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1390 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1392 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1394 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1395 if (test_opt(sbi, FORCE_FG_GC))
1396 seq_printf(seq, ",background_gc=%s", "sync");
1398 seq_printf(seq, ",background_gc=%s", "on");
1400 seq_printf(seq, ",background_gc=%s", "off");
1402 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1403 seq_puts(seq, ",disable_roll_forward");
1404 if (test_opt(sbi, NORECOVERY))
1405 seq_puts(seq, ",norecovery");
1406 if (test_opt(sbi, DISCARD))
1407 seq_puts(seq, ",discard");
1409 seq_puts(seq, ",nodiscard");
1410 if (test_opt(sbi, NOHEAP))
1411 seq_puts(seq, ",no_heap");
1413 seq_puts(seq, ",heap");
1414 #ifdef CONFIG_F2FS_FS_XATTR
1415 if (test_opt(sbi, XATTR_USER))
1416 seq_puts(seq, ",user_xattr");
1418 seq_puts(seq, ",nouser_xattr");
1419 if (test_opt(sbi, INLINE_XATTR))
1420 seq_puts(seq, ",inline_xattr");
1422 seq_puts(seq, ",noinline_xattr");
1423 if (test_opt(sbi, INLINE_XATTR_SIZE))
1424 seq_printf(seq, ",inline_xattr_size=%u",
1425 F2FS_OPTION(sbi).inline_xattr_size);
1427 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1428 if (test_opt(sbi, POSIX_ACL))
1429 seq_puts(seq, ",acl");
1431 seq_puts(seq, ",noacl");
1433 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1434 seq_puts(seq, ",disable_ext_identify");
1435 if (test_opt(sbi, INLINE_DATA))
1436 seq_puts(seq, ",inline_data");
1438 seq_puts(seq, ",noinline_data");
1439 if (test_opt(sbi, INLINE_DENTRY))
1440 seq_puts(seq, ",inline_dentry");
1442 seq_puts(seq, ",noinline_dentry");
1443 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
1444 seq_puts(seq, ",flush_merge");
1445 if (test_opt(sbi, NOBARRIER))
1446 seq_puts(seq, ",nobarrier");
1447 if (test_opt(sbi, FASTBOOT))
1448 seq_puts(seq, ",fastboot");
1449 if (test_opt(sbi, EXTENT_CACHE))
1450 seq_puts(seq, ",extent_cache");
1452 seq_puts(seq, ",noextent_cache");
1453 if (test_opt(sbi, DATA_FLUSH))
1454 seq_puts(seq, ",data_flush");
1456 seq_puts(seq, ",mode=");
1457 if (test_opt(sbi, ADAPTIVE))
1458 seq_puts(seq, "adaptive");
1459 else if (test_opt(sbi, LFS))
1460 seq_puts(seq, "lfs");
1461 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
1462 if (test_opt(sbi, RESERVE_ROOT))
1463 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1464 F2FS_OPTION(sbi).root_reserved_blocks,
1465 from_kuid_munged(&init_user_ns,
1466 F2FS_OPTION(sbi).s_resuid),
1467 from_kgid_munged(&init_user_ns,
1468 F2FS_OPTION(sbi).s_resgid));
1469 if (F2FS_IO_SIZE_BITS(sbi))
1470 seq_printf(seq, ",io_bits=%u",
1471 F2FS_OPTION(sbi).write_io_size_bits);
1472 #ifdef CONFIG_F2FS_FAULT_INJECTION
1473 if (test_opt(sbi, FAULT_INJECTION)) {
1474 seq_printf(seq, ",fault_injection=%u",
1475 F2FS_OPTION(sbi).fault_info.inject_rate);
1476 seq_printf(seq, ",fault_type=%u",
1477 F2FS_OPTION(sbi).fault_info.inject_type);
1481 if (test_opt(sbi, QUOTA))
1482 seq_puts(seq, ",quota");
1483 if (test_opt(sbi, USRQUOTA))
1484 seq_puts(seq, ",usrquota");
1485 if (test_opt(sbi, GRPQUOTA))
1486 seq_puts(seq, ",grpquota");
1487 if (test_opt(sbi, PRJQUOTA))
1488 seq_puts(seq, ",prjquota");
1490 f2fs_show_quota_options(seq, sbi->sb);
1491 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
1492 seq_printf(seq, ",whint_mode=%s", "user-based");
1493 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
1494 seq_printf(seq, ",whint_mode=%s", "fs-based");
1495 #ifdef CONFIG_FS_ENCRYPTION
1496 if (F2FS_OPTION(sbi).test_dummy_encryption)
1497 seq_puts(seq, ",test_dummy_encryption");
1500 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
1501 seq_printf(seq, ",alloc_mode=%s", "default");
1502 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
1503 seq_printf(seq, ",alloc_mode=%s", "reuse");
1505 if (test_opt(sbi, DISABLE_CHECKPOINT))
1506 seq_printf(seq, ",checkpoint=disable:%u",
1507 F2FS_OPTION(sbi).unusable_cap);
1508 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
1509 seq_printf(seq, ",fsync_mode=%s", "posix");
1510 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
1511 seq_printf(seq, ",fsync_mode=%s", "strict");
1512 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1513 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
1517 static void default_options(struct f2fs_sb_info *sbi)
1519 /* init some FS parameters */
1520 F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
1521 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1522 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1523 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1524 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1525 F2FS_OPTION(sbi).test_dummy_encryption = false;
1526 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1527 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
1529 set_opt(sbi, BG_GC);
1530 set_opt(sbi, INLINE_XATTR);
1531 set_opt(sbi, INLINE_DATA);
1532 set_opt(sbi, INLINE_DENTRY);
1533 set_opt(sbi, EXTENT_CACHE);
1534 set_opt(sbi, NOHEAP);
1535 clear_opt(sbi, DISABLE_CHECKPOINT);
1536 F2FS_OPTION(sbi).unusable_cap = 0;
1537 sbi->sb->s_flags |= SB_LAZYTIME;
1538 set_opt(sbi, FLUSH_MERGE);
1539 set_opt(sbi, DISCARD);
1540 if (f2fs_sb_has_blkzoned(sbi))
1541 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1543 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
1545 #ifdef CONFIG_F2FS_FS_XATTR
1546 set_opt(sbi, XATTR_USER);
1548 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1549 set_opt(sbi, POSIX_ACL);
1552 f2fs_build_fault_attr(sbi, 0, 0);
1556 static int f2fs_enable_quotas(struct super_block *sb);
1559 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
1561 unsigned int s_flags = sbi->sb->s_flags;
1562 struct cp_control cpc;
1567 if (s_flags & SB_RDONLY) {
1568 f2fs_err(sbi, "checkpoint=disable on readonly fs");
1571 sbi->sb->s_flags |= SB_ACTIVE;
1573 f2fs_update_time(sbi, DISABLE_TIME);
1575 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
1576 mutex_lock(&sbi->gc_mutex);
1577 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1578 if (err == -ENODATA) {
1582 if (err && err != -EAGAIN)
1586 ret = sync_filesystem(sbi->sb);
1588 err = ret ? ret: err;
1592 unusable = f2fs_get_unusable_blocks(sbi);
1593 if (f2fs_disable_cp_again(sbi, unusable)) {
1598 mutex_lock(&sbi->gc_mutex);
1599 cpc.reason = CP_PAUSE;
1600 set_sbi_flag(sbi, SBI_CP_DISABLED);
1601 err = f2fs_write_checkpoint(sbi, &cpc);
1605 spin_lock(&sbi->stat_lock);
1606 sbi->unusable_block_count = unusable;
1607 spin_unlock(&sbi->stat_lock);
1610 mutex_unlock(&sbi->gc_mutex);
1612 sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1616 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
1618 mutex_lock(&sbi->gc_mutex);
1619 f2fs_dirty_to_prefree(sbi);
1621 clear_sbi_flag(sbi, SBI_CP_DISABLED);
1622 set_sbi_flag(sbi, SBI_IS_DIRTY);
1623 mutex_unlock(&sbi->gc_mutex);
1625 f2fs_sync_fs(sbi->sb, 1);
1628 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1630 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1631 struct f2fs_mount_info org_mount_opt;
1632 unsigned long old_sb_flags;
1634 bool need_restart_gc = false;
1635 bool need_stop_gc = false;
1636 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
1637 bool disable_checkpoint = test_opt(sbi, DISABLE_CHECKPOINT);
1638 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
1639 bool checkpoint_changed;
1645 * Save the old mount options in case we
1646 * need to restore them.
1648 org_mount_opt = sbi->mount_opt;
1649 old_sb_flags = sb->s_flags;
1652 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
1653 for (i = 0; i < MAXQUOTAS; i++) {
1654 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1655 org_mount_opt.s_qf_names[i] =
1656 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1658 if (!org_mount_opt.s_qf_names[i]) {
1659 for (j = 0; j < i; j++)
1660 kvfree(org_mount_opt.s_qf_names[j]);
1664 org_mount_opt.s_qf_names[i] = NULL;
1669 /* recover superblocks we couldn't write due to previous RO mount */
1670 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
1671 err = f2fs_commit_super(sbi, false);
1672 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
1675 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1678 default_options(sbi);
1680 /* parse mount options */
1681 err = parse_options(sb, data);
1684 checkpoint_changed =
1685 disable_checkpoint != test_opt(sbi, DISABLE_CHECKPOINT);
1688 * Previous and new state of filesystem is RO,
1689 * so skip checking GC and FLUSH_MERGE conditions.
1691 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
1695 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
1696 err = dquot_suspend(sb, -1);
1699 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
1700 /* dquot_resume needs RW */
1701 sb->s_flags &= ~SB_RDONLY;
1702 if (sb_any_quota_suspended(sb)) {
1703 dquot_resume(sb, -1);
1704 } else if (f2fs_sb_has_quota_ino(sbi)) {
1705 err = f2fs_enable_quotas(sb);
1711 /* disallow enable/disable extent_cache dynamically */
1712 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1714 f2fs_warn(sbi, "switch extent_cache option is not allowed");
1718 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
1720 f2fs_warn(sbi, "switch io_bits option is not allowed");
1724 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
1726 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
1731 * We stop the GC thread if FS is mounted as RO
1732 * or if background_gc = off is passed in mount
1733 * option. Also sync the filesystem.
1735 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
1736 if (sbi->gc_thread) {
1737 f2fs_stop_gc_thread(sbi);
1738 need_restart_gc = true;
1740 } else if (!sbi->gc_thread) {
1741 err = f2fs_start_gc_thread(sbi);
1744 need_stop_gc = true;
1747 if (*flags & SB_RDONLY ||
1748 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
1749 writeback_inodes_sb(sb, WB_REASON_SYNC);
1752 set_sbi_flag(sbi, SBI_IS_DIRTY);
1753 set_sbi_flag(sbi, SBI_IS_CLOSE);
1754 f2fs_sync_fs(sb, 1);
1755 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1758 if (checkpoint_changed) {
1759 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
1760 err = f2fs_disable_checkpoint(sbi);
1764 f2fs_enable_checkpoint(sbi);
1769 * We stop issue flush thread if FS is mounted as RO
1770 * or if flush_merge is not passed in mount option.
1772 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
1773 clear_opt(sbi, FLUSH_MERGE);
1774 f2fs_destroy_flush_cmd_control(sbi, false);
1776 err = f2fs_create_flush_cmd_control(sbi);
1782 /* Release old quota file names */
1783 for (i = 0; i < MAXQUOTAS; i++)
1784 kvfree(org_mount_opt.s_qf_names[i]);
1786 /* Update the POSIXACL Flag */
1787 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1788 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
1790 limit_reserve_root(sbi);
1791 adjust_unusable_cap_perc(sbi);
1792 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
1795 if (need_restart_gc) {
1796 if (f2fs_start_gc_thread(sbi))
1797 f2fs_warn(sbi, "background gc thread has stopped");
1798 } else if (need_stop_gc) {
1799 f2fs_stop_gc_thread(sbi);
1803 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
1804 for (i = 0; i < MAXQUOTAS; i++) {
1805 kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
1806 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
1809 sbi->mount_opt = org_mount_opt;
1810 sb->s_flags = old_sb_flags;
1815 /* Read data from quotafile */
1816 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1817 size_t len, loff_t off)
1819 struct inode *inode = sb_dqopt(sb)->files[type];
1820 struct address_space *mapping = inode->i_mapping;
1821 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1822 int offset = off & (sb->s_blocksize - 1);
1825 loff_t i_size = i_size_read(inode);
1831 if (off + len > i_size)
1834 while (toread > 0) {
1835 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1837 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
1839 if (PTR_ERR(page) == -ENOMEM) {
1840 congestion_wait(BLK_RW_ASYNC, HZ/50);
1843 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1844 return PTR_ERR(page);
1849 if (unlikely(page->mapping != mapping)) {
1850 f2fs_put_page(page, 1);
1853 if (unlikely(!PageUptodate(page))) {
1854 f2fs_put_page(page, 1);
1855 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1859 memcpy_from_page(data, page, offset, tocopy);
1860 f2fs_put_page(page, 1);
1870 /* Write to quotafile */
1871 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1872 const char *data, size_t len, loff_t off)
1874 struct inode *inode = sb_dqopt(sb)->files[type];
1875 struct address_space *mapping = inode->i_mapping;
1876 const struct address_space_operations *a_ops = mapping->a_ops;
1877 int offset = off & (sb->s_blocksize - 1);
1878 size_t towrite = len;
1880 void *fsdata = NULL;
1884 while (towrite > 0) {
1885 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1888 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1890 if (unlikely(err)) {
1891 if (err == -ENOMEM) {
1892 congestion_wait(BLK_RW_ASYNC, HZ/50);
1895 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1899 memcpy_to_page(page, offset, data, tocopy);
1901 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1912 inode->i_mtime = inode->i_ctime = current_time(inode);
1913 f2fs_mark_inode_dirty_sync(inode, false);
1914 return len - towrite;
1917 static struct dquot **f2fs_get_dquots(struct inode *inode)
1919 return F2FS_I(inode)->i_dquot;
1922 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1924 return &F2FS_I(inode)->i_reserved_quota;
1927 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1929 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
1930 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
1934 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
1935 F2FS_OPTION(sbi).s_jquota_fmt, type);
1938 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
1943 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
1944 err = f2fs_enable_quotas(sbi->sb);
1946 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
1952 for (i = 0; i < MAXQUOTAS; i++) {
1953 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1954 err = f2fs_quota_on_mount(sbi, i);
1959 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
1966 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1969 struct inode *qf_inode;
1970 unsigned long qf_inum;
1973 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
1975 qf_inum = f2fs_qf_ino(sb, type);
1979 qf_inode = f2fs_iget(sb, qf_inum);
1980 if (IS_ERR(qf_inode)) {
1981 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
1982 return PTR_ERR(qf_inode);
1985 /* Don't account quota for quota files to avoid recursion */
1986 qf_inode->i_flags |= S_NOQUOTA;
1987 err = dquot_enable(qf_inode, type, format_id, flags);
1992 static int f2fs_enable_quotas(struct super_block *sb)
1994 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1996 unsigned long qf_inum;
1997 bool quota_mopt[MAXQUOTAS] = {
1998 test_opt(sbi, USRQUOTA),
1999 test_opt(sbi, GRPQUOTA),
2000 test_opt(sbi, PRJQUOTA),
2003 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2004 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2008 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2010 for (type = 0; type < MAXQUOTAS; type++) {
2011 qf_inum = f2fs_qf_ino(sb, type);
2013 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2014 DQUOT_USAGE_ENABLED |
2015 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2017 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2019 for (type--; type >= 0; type--)
2020 dquot_quota_off(sb, type);
2021 set_sbi_flag(F2FS_SB(sb),
2022 SBI_QUOTA_NEED_REPAIR);
2030 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2032 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2033 struct address_space *mapping = dqopt->files[type]->i_mapping;
2036 ret = dquot_writeback_dquots(sbi->sb, type);
2040 ret = filemap_fdatawrite(mapping);
2044 /* if we are using journalled quota */
2045 if (is_journalled_quota(sbi))
2048 ret = filemap_fdatawait(mapping);
2050 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2053 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2057 int f2fs_quota_sync(struct super_block *sb, int type)
2059 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2060 struct quota_info *dqopt = sb_dqopt(sb);
2065 * Now when everything is written we can discard the pagecache so
2066 * that userspace sees the changes.
2068 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2070 if (type != -1 && cnt != type)
2073 if (!sb_has_quota_active(sb, cnt))
2076 if (!f2fs_sb_has_quota_ino(sbi))
2077 inode_lock(dqopt->files[cnt]);
2082 * down_read(quota_sem)
2083 * dquot_writeback_dquots()
2086 * down_read(quota_sem)
2089 down_read(&sbi->quota_sem);
2091 ret = f2fs_quota_sync_file(sbi, cnt);
2093 up_read(&sbi->quota_sem);
2094 f2fs_unlock_op(sbi);
2096 if (!f2fs_sb_has_quota_ino(sbi))
2097 inode_unlock(dqopt->files[cnt]);
2105 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2106 const struct path *path)
2108 struct inode *inode;
2111 /* if quota sysfile exists, deny enabling quota with specific file */
2112 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2113 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2117 err = f2fs_quota_sync(sb, type);
2121 err = dquot_quota_on(sb, type, format_id, path);
2125 inode = d_inode(path->dentry);
2128 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
2129 f2fs_set_inode_flags(inode);
2130 inode_unlock(inode);
2131 f2fs_mark_inode_dirty_sync(inode, false);
2136 static int __f2fs_quota_off(struct super_block *sb, int type)
2138 struct inode *inode = sb_dqopt(sb)->files[type];
2141 if (!inode || !igrab(inode))
2142 return dquot_quota_off(sb, type);
2144 err = f2fs_quota_sync(sb, type);
2148 err = dquot_quota_off(sb, type);
2149 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
2153 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
2154 f2fs_set_inode_flags(inode);
2155 inode_unlock(inode);
2156 f2fs_mark_inode_dirty_sync(inode, false);
2162 static int f2fs_quota_off(struct super_block *sb, int type)
2164 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2167 err = __f2fs_quota_off(sb, type);
2170 * quotactl can shutdown journalled quota, result in inconsistence
2171 * between quota record and fs data by following updates, tag the
2172 * flag to let fsck be aware of it.
2174 if (is_journalled_quota(sbi))
2175 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2179 void f2fs_quota_off_umount(struct super_block *sb)
2184 for (type = 0; type < MAXQUOTAS; type++) {
2185 err = __f2fs_quota_off(sb, type);
2187 int ret = dquot_quota_off(sb, type);
2189 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
2191 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2195 * In case of checkpoint=disable, we must flush quota blocks.
2196 * This can cause NULL exception for node_inode in end_io, since
2197 * put_super already dropped it.
2199 sync_filesystem(sb);
2202 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
2204 struct quota_info *dqopt = sb_dqopt(sb);
2207 for (type = 0; type < MAXQUOTAS; type++) {
2208 if (!dqopt->files[type])
2210 f2fs_inode_synced(dqopt->files[type]);
2214 static int f2fs_dquot_commit(struct dquot *dquot)
2216 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2219 down_read(&sbi->quota_sem);
2220 ret = dquot_commit(dquot);
2222 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2223 up_read(&sbi->quota_sem);
2227 static int f2fs_dquot_acquire(struct dquot *dquot)
2229 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2232 down_read(&sbi->quota_sem);
2233 ret = dquot_acquire(dquot);
2235 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2236 up_read(&sbi->quota_sem);
2240 static int f2fs_dquot_release(struct dquot *dquot)
2242 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2245 down_read(&sbi->quota_sem);
2246 ret = dquot_release(dquot);
2248 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2249 up_read(&sbi->quota_sem);
2253 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
2255 struct super_block *sb = dquot->dq_sb;
2256 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2259 down_read(&sbi->quota_sem);
2260 ret = dquot_mark_dquot_dirty(dquot);
2262 /* if we are using journalled quota */
2263 if (is_journalled_quota(sbi))
2264 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
2266 up_read(&sbi->quota_sem);
2270 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
2272 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2275 down_read(&sbi->quota_sem);
2276 ret = dquot_commit_info(sb, type);
2278 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2279 up_read(&sbi->quota_sem);
2283 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
2285 *projid = F2FS_I(inode)->i_projid;
2289 static const struct dquot_operations f2fs_quota_operations = {
2290 .get_reserved_space = f2fs_get_reserved_space,
2291 .write_dquot = f2fs_dquot_commit,
2292 .acquire_dquot = f2fs_dquot_acquire,
2293 .release_dquot = f2fs_dquot_release,
2294 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
2295 .write_info = f2fs_dquot_commit_info,
2296 .alloc_dquot = dquot_alloc,
2297 .destroy_dquot = dquot_destroy,
2298 .get_projid = f2fs_get_projid,
2299 .get_next_id = dquot_get_next_id,
2302 static const struct quotactl_ops f2fs_quotactl_ops = {
2303 .quota_on = f2fs_quota_on,
2304 .quota_off = f2fs_quota_off,
2305 .quota_sync = f2fs_quota_sync,
2306 .get_state = dquot_get_state,
2307 .set_info = dquot_set_dqinfo,
2308 .get_dqblk = dquot_get_dqblk,
2309 .set_dqblk = dquot_set_dqblk,
2310 .get_nextdqblk = dquot_get_next_dqblk,
2313 int f2fs_quota_sync(struct super_block *sb, int type)
2318 void f2fs_quota_off_umount(struct super_block *sb)
2323 static const struct super_operations f2fs_sops = {
2324 .alloc_inode = f2fs_alloc_inode,
2325 .free_inode = f2fs_free_inode,
2326 .drop_inode = f2fs_drop_inode,
2327 .write_inode = f2fs_write_inode,
2328 .dirty_inode = f2fs_dirty_inode,
2329 .show_options = f2fs_show_options,
2331 .quota_read = f2fs_quota_read,
2332 .quota_write = f2fs_quota_write,
2333 .get_dquots = f2fs_get_dquots,
2335 .evict_inode = f2fs_evict_inode,
2336 .put_super = f2fs_put_super,
2337 .sync_fs = f2fs_sync_fs,
2338 .freeze_fs = f2fs_freeze,
2339 .unfreeze_fs = f2fs_unfreeze,
2340 .statfs = f2fs_statfs,
2341 .remount_fs = f2fs_remount,
2344 #ifdef CONFIG_FS_ENCRYPTION
2345 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
2347 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2348 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2352 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
2355 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2358 * Encrypting the root directory is not allowed because fsck
2359 * expects lost+found directory to exist and remain unencrypted
2360 * if LOST_FOUND feature is enabled.
2363 if (f2fs_sb_has_lost_found(sbi) &&
2364 inode->i_ino == F2FS_ROOT_INO(sbi))
2367 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2368 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2369 ctx, len, fs_data, XATTR_CREATE);
2372 static bool f2fs_dummy_context(struct inode *inode)
2374 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
2377 static const struct fscrypt_operations f2fs_cryptops = {
2378 .key_prefix = "f2fs:",
2379 .get_context = f2fs_get_context,
2380 .set_context = f2fs_set_context,
2381 .dummy_context = f2fs_dummy_context,
2382 .empty_dir = f2fs_empty_dir,
2383 .max_namelen = F2FS_NAME_LEN,
2387 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2388 u64 ino, u32 generation)
2390 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2391 struct inode *inode;
2393 if (f2fs_check_nid_range(sbi, ino))
2394 return ERR_PTR(-ESTALE);
2397 * f2fs_iget isn't quite right if the inode is currently unallocated!
2398 * However f2fs_iget currently does appropriate checks to handle stale
2399 * inodes so everything is OK.
2401 inode = f2fs_iget(sb, ino);
2403 return ERR_CAST(inode);
2404 if (unlikely(generation && inode->i_generation != generation)) {
2405 /* we didn't find the right inode.. */
2407 return ERR_PTR(-ESTALE);
2412 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2413 int fh_len, int fh_type)
2415 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2416 f2fs_nfs_get_inode);
2419 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2420 int fh_len, int fh_type)
2422 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2423 f2fs_nfs_get_inode);
2426 static const struct export_operations f2fs_export_ops = {
2427 .fh_to_dentry = f2fs_fh_to_dentry,
2428 .fh_to_parent = f2fs_fh_to_parent,
2429 .get_parent = f2fs_get_parent,
2432 static loff_t max_file_blocks(void)
2435 loff_t leaf_count = DEF_ADDRS_PER_BLOCK;
2438 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
2439 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
2440 * space in inode.i_addr, it will be more safe to reassign
2444 /* two direct node blocks */
2445 result += (leaf_count * 2);
2447 /* two indirect node blocks */
2448 leaf_count *= NIDS_PER_BLOCK;
2449 result += (leaf_count * 2);
2451 /* one double indirect node block */
2452 leaf_count *= NIDS_PER_BLOCK;
2453 result += leaf_count;
2458 static int __f2fs_commit_super(struct buffer_head *bh,
2459 struct f2fs_super_block *super)
2463 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
2464 set_buffer_dirty(bh);
2467 /* it's rare case, we can do fua all the time */
2468 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
2471 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
2472 struct buffer_head *bh)
2474 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2475 (bh->b_data + F2FS_SUPER_OFFSET);
2476 struct super_block *sb = sbi->sb;
2477 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2478 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2479 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2480 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2481 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2482 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2483 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2484 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2485 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2486 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2487 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2488 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2489 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2490 u64 main_end_blkaddr = main_blkaddr +
2491 (segment_count_main << log_blocks_per_seg);
2492 u64 seg_end_blkaddr = segment0_blkaddr +
2493 (segment_count << log_blocks_per_seg);
2495 if (segment0_blkaddr != cp_blkaddr) {
2496 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2497 segment0_blkaddr, cp_blkaddr);
2501 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2503 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2504 cp_blkaddr, sit_blkaddr,
2505 segment_count_ckpt << log_blocks_per_seg);
2509 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2511 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2512 sit_blkaddr, nat_blkaddr,
2513 segment_count_sit << log_blocks_per_seg);
2517 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2519 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2520 nat_blkaddr, ssa_blkaddr,
2521 segment_count_nat << log_blocks_per_seg);
2525 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2527 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2528 ssa_blkaddr, main_blkaddr,
2529 segment_count_ssa << log_blocks_per_seg);
2533 if (main_end_blkaddr > seg_end_blkaddr) {
2534 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
2537 (segment_count << log_blocks_per_seg),
2538 segment_count_main << log_blocks_per_seg);
2540 } else if (main_end_blkaddr < seg_end_blkaddr) {
2544 /* fix in-memory information all the time */
2545 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2546 segment0_blkaddr) >> log_blocks_per_seg);
2548 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
2549 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2552 err = __f2fs_commit_super(bh, NULL);
2553 res = err ? "failed" : "done";
2555 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%u) block(%u)",
2558 (segment_count << log_blocks_per_seg),
2559 segment_count_main << log_blocks_per_seg);
2566 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2567 struct buffer_head *bh)
2569 block_t segment_count, segs_per_sec, secs_per_zone;
2570 block_t total_sections, blocks_per_seg;
2571 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2572 (bh->b_data + F2FS_SUPER_OFFSET);
2573 size_t crc_offset = 0;
2576 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
2577 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
2578 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2582 /* Check checksum_offset and crc in superblock */
2583 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
2584 crc_offset = le32_to_cpu(raw_super->checksum_offset);
2586 offsetof(struct f2fs_super_block, crc)) {
2587 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
2589 return -EFSCORRUPTED;
2591 crc = le32_to_cpu(raw_super->crc);
2592 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
2593 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
2594 return -EFSCORRUPTED;
2598 /* Currently, support only 4KB page cache size */
2599 if (F2FS_BLKSIZE != PAGE_SIZE) {
2600 f2fs_info(sbi, "Invalid page_cache_size (%lu), supports only 4KB",
2602 return -EFSCORRUPTED;
2605 /* Currently, support only 4KB block size */
2606 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
2607 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
2608 le32_to_cpu(raw_super->log_blocksize),
2610 return -EFSCORRUPTED;
2613 /* check log blocks per segment */
2614 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2615 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
2616 le32_to_cpu(raw_super->log_blocks_per_seg));
2617 return -EFSCORRUPTED;
2620 /* Currently, support 512/1024/2048/4096 bytes sector size */
2621 if (le32_to_cpu(raw_super->log_sectorsize) >
2622 F2FS_MAX_LOG_SECTOR_SIZE ||
2623 le32_to_cpu(raw_super->log_sectorsize) <
2624 F2FS_MIN_LOG_SECTOR_SIZE) {
2625 f2fs_info(sbi, "Invalid log sectorsize (%u)",
2626 le32_to_cpu(raw_super->log_sectorsize));
2627 return -EFSCORRUPTED;
2629 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2630 le32_to_cpu(raw_super->log_sectorsize) !=
2631 F2FS_MAX_LOG_SECTOR_SIZE) {
2632 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
2633 le32_to_cpu(raw_super->log_sectors_per_block),
2634 le32_to_cpu(raw_super->log_sectorsize));
2635 return -EFSCORRUPTED;
2638 segment_count = le32_to_cpu(raw_super->segment_count);
2639 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2640 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2641 total_sections = le32_to_cpu(raw_super->section_count);
2643 /* blocks_per_seg should be 512, given the above check */
2644 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2646 if (segment_count > F2FS_MAX_SEGMENT ||
2647 segment_count < F2FS_MIN_SEGMENTS) {
2648 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
2649 return -EFSCORRUPTED;
2652 if (total_sections > segment_count ||
2653 total_sections < F2FS_MIN_SEGMENTS ||
2654 segs_per_sec > segment_count || !segs_per_sec) {
2655 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
2656 segment_count, total_sections, segs_per_sec);
2657 return -EFSCORRUPTED;
2660 if ((segment_count / segs_per_sec) < total_sections) {
2661 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
2662 segment_count, segs_per_sec, total_sections);
2663 return -EFSCORRUPTED;
2666 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
2667 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
2668 segment_count, le64_to_cpu(raw_super->block_count));
2669 return -EFSCORRUPTED;
2672 if (secs_per_zone > total_sections || !secs_per_zone) {
2673 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
2674 secs_per_zone, total_sections);
2675 return -EFSCORRUPTED;
2677 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
2678 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
2679 (le32_to_cpu(raw_super->extension_count) +
2680 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
2681 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
2682 le32_to_cpu(raw_super->extension_count),
2683 raw_super->hot_ext_count,
2684 F2FS_MAX_EXTENSION);
2685 return -EFSCORRUPTED;
2688 if (le32_to_cpu(raw_super->cp_payload) >
2689 (blocks_per_seg - F2FS_CP_PACKS)) {
2690 f2fs_info(sbi, "Insane cp_payload (%u > %u)",
2691 le32_to_cpu(raw_super->cp_payload),
2692 blocks_per_seg - F2FS_CP_PACKS);
2693 return -EFSCORRUPTED;
2696 /* check reserved ino info */
2697 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2698 le32_to_cpu(raw_super->meta_ino) != 2 ||
2699 le32_to_cpu(raw_super->root_ino) != 3) {
2700 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2701 le32_to_cpu(raw_super->node_ino),
2702 le32_to_cpu(raw_super->meta_ino),
2703 le32_to_cpu(raw_super->root_ino));
2704 return -EFSCORRUPTED;
2707 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
2708 if (sanity_check_area_boundary(sbi, bh))
2709 return -EFSCORRUPTED;
2714 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
2716 unsigned int total, fsmeta;
2717 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2718 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2719 unsigned int ovp_segments, reserved_segments;
2720 unsigned int main_segs, blocks_per_seg;
2721 unsigned int sit_segs, nat_segs;
2722 unsigned int sit_bitmap_size, nat_bitmap_size;
2723 unsigned int log_blocks_per_seg;
2724 unsigned int segment_count_main;
2725 unsigned int cp_pack_start_sum, cp_payload;
2726 block_t user_block_count, valid_user_blocks;
2727 block_t avail_node_count, valid_node_count;
2730 total = le32_to_cpu(raw_super->segment_count);
2731 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2732 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
2734 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
2736 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2737 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2739 if (unlikely(fsmeta >= total))
2742 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2743 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2745 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2746 ovp_segments == 0 || reserved_segments == 0)) {
2747 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
2751 user_block_count = le64_to_cpu(ckpt->user_block_count);
2752 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2753 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2754 if (!user_block_count || user_block_count >=
2755 segment_count_main << log_blocks_per_seg) {
2756 f2fs_err(sbi, "Wrong user_block_count: %u",
2761 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
2762 if (valid_user_blocks > user_block_count) {
2763 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
2764 valid_user_blocks, user_block_count);
2768 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
2769 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
2770 if (valid_node_count > avail_node_count) {
2771 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
2772 valid_node_count, avail_node_count);
2776 main_segs = le32_to_cpu(raw_super->segment_count_main);
2777 blocks_per_seg = sbi->blocks_per_seg;
2779 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2780 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2781 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2783 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
2784 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2785 le32_to_cpu(ckpt->cur_node_segno[j])) {
2786 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
2788 le32_to_cpu(ckpt->cur_node_segno[i]));
2793 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2794 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2795 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2797 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
2798 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
2799 le32_to_cpu(ckpt->cur_data_segno[j])) {
2800 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
2802 le32_to_cpu(ckpt->cur_data_segno[i]));
2807 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2808 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
2809 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2810 le32_to_cpu(ckpt->cur_data_segno[j])) {
2811 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
2813 le32_to_cpu(ckpt->cur_node_segno[i]));
2819 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
2820 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
2822 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
2823 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
2824 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
2825 sit_bitmap_size, nat_bitmap_size);
2829 cp_pack_start_sum = __start_sum_addr(sbi);
2830 cp_payload = __cp_payload(sbi);
2831 if (cp_pack_start_sum < cp_payload + 1 ||
2832 cp_pack_start_sum > blocks_per_seg - 1 -
2834 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
2839 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
2840 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
2841 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
2842 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
2843 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
2844 le32_to_cpu(ckpt->checksum_offset));
2848 if (unlikely(f2fs_cp_error(sbi))) {
2849 f2fs_err(sbi, "A bug case: need to run fsck");
2855 static void init_sb_info(struct f2fs_sb_info *sbi)
2857 struct f2fs_super_block *raw_super = sbi->raw_super;
2860 sbi->log_sectors_per_block =
2861 le32_to_cpu(raw_super->log_sectors_per_block);
2862 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2863 sbi->blocksize = 1 << sbi->log_blocksize;
2864 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2865 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2866 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2867 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2868 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2869 sbi->total_node_count =
2870 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2871 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2872 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2873 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2874 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
2875 sbi->cur_victim_sec = NULL_SECNO;
2876 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
2877 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
2878 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
2879 sbi->migration_granularity = sbi->segs_per_sec;
2881 sbi->dir_level = DEF_DIR_LEVEL;
2882 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
2883 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
2884 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
2885 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
2886 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
2887 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
2888 DEF_UMOUNT_DISCARD_TIMEOUT;
2889 clear_sbi_flag(sbi, SBI_NEED_FSCK);
2891 for (i = 0; i < NR_COUNT_TYPE; i++)
2892 atomic_set(&sbi->nr_pages[i], 0);
2894 for (i = 0; i < META; i++)
2895 atomic_set(&sbi->wb_sync_req[i], 0);
2897 INIT_LIST_HEAD(&sbi->s_list);
2898 mutex_init(&sbi->umount_mutex);
2899 init_rwsem(&sbi->io_order_lock);
2900 spin_lock_init(&sbi->cp_lock);
2902 sbi->dirty_device = 0;
2903 spin_lock_init(&sbi->dev_lock);
2905 init_rwsem(&sbi->sb_lock);
2908 static int init_percpu_info(struct f2fs_sb_info *sbi)
2912 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2916 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
2919 percpu_counter_destroy(&sbi->alloc_valid_block_count);
2924 #ifdef CONFIG_BLK_DEV_ZONED
2925 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
2927 struct block_device *bdev = FDEV(devi).bdev;
2928 sector_t nr_sectors = bdev->bd_part->nr_sects;
2929 sector_t sector = 0;
2930 struct blk_zone *zones;
2931 unsigned int i, nr_zones;
2935 if (!f2fs_sb_has_blkzoned(sbi))
2938 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
2939 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
2941 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
2942 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2943 __ilog2_u32(sbi->blocks_per_blkz))
2945 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
2946 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2947 sbi->log_blocks_per_blkz;
2948 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
2949 FDEV(devi).nr_blkz++;
2951 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
2952 BITS_TO_LONGS(FDEV(devi).nr_blkz)
2953 * sizeof(unsigned long),
2955 if (!FDEV(devi).blkz_seq)
2958 #define F2FS_REPORT_NR_ZONES 4096
2960 zones = f2fs_kzalloc(sbi,
2961 array_size(F2FS_REPORT_NR_ZONES,
2962 sizeof(struct blk_zone)),
2967 /* Get block zones type */
2968 while (zones && sector < nr_sectors) {
2970 nr_zones = F2FS_REPORT_NR_ZONES;
2971 err = blkdev_report_zones(bdev, sector, zones, &nr_zones);
2979 for (i = 0; i < nr_zones; i++) {
2980 if (zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL)
2981 set_bit(n, FDEV(devi).blkz_seq);
2982 sector += zones[i].len;
2994 * Read f2fs raw super block.
2995 * Because we have two copies of super block, so read both of them
2996 * to get the first valid one. If any one of them is broken, we pass
2997 * them recovery flag back to the caller.
2999 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3000 struct f2fs_super_block **raw_super,
3001 int *valid_super_block, int *recovery)
3003 struct super_block *sb = sbi->sb;
3005 struct buffer_head *bh;
3006 struct f2fs_super_block *super;
3009 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3013 for (block = 0; block < 2; block++) {
3014 bh = sb_bread(sb, block);
3016 f2fs_err(sbi, "Unable to read %dth superblock",
3022 /* sanity checking of raw super */
3023 err = sanity_check_raw_super(sbi, bh);
3025 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3032 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3034 *valid_super_block = block;
3040 /* Fail to read any one of the superblocks*/
3044 /* No valid superblock */
3053 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3055 struct buffer_head *bh;
3059 if ((recover && f2fs_readonly(sbi->sb)) ||
3060 bdev_read_only(sbi->sb->s_bdev)) {
3061 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3065 /* we should update superblock crc here */
3066 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
3067 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3068 offsetof(struct f2fs_super_block, crc));
3069 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3072 /* write back-up superblock first */
3073 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
3076 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3079 /* if we are in recovery path, skip writing valid superblock */
3083 /* write current valid superblock */
3084 bh = sb_bread(sbi->sb, sbi->valid_super_block);
3087 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3092 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
3094 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3095 unsigned int max_devices = MAX_DEVICES;
3098 /* Initialize single device information */
3099 if (!RDEV(0).path[0]) {
3100 if (!bdev_is_zoned(sbi->sb->s_bdev))
3106 * Initialize multiple devices information, or single
3107 * zoned block device information.
3109 sbi->devs = f2fs_kzalloc(sbi,
3110 array_size(max_devices,
3111 sizeof(struct f2fs_dev_info)),
3116 for (i = 0; i < max_devices; i++) {
3118 if (i > 0 && !RDEV(i).path[0])
3121 if (max_devices == 1) {
3122 /* Single zoned block device mount */
3124 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
3125 sbi->sb->s_mode, sbi->sb->s_type);
3127 /* Multi-device mount */
3128 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
3129 FDEV(i).total_segments =
3130 le32_to_cpu(RDEV(i).total_segments);
3132 FDEV(i).start_blk = 0;
3133 FDEV(i).end_blk = FDEV(i).start_blk +
3134 (FDEV(i).total_segments <<
3135 sbi->log_blocks_per_seg) - 1 +
3136 le32_to_cpu(raw_super->segment0_blkaddr);
3138 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
3139 FDEV(i).end_blk = FDEV(i).start_blk +
3140 (FDEV(i).total_segments <<
3141 sbi->log_blocks_per_seg) - 1;
3143 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3144 sbi->sb->s_mode, sbi->sb->s_type);
3146 if (IS_ERR(FDEV(i).bdev))
3147 return PTR_ERR(FDEV(i).bdev);
3149 /* to release errored devices */
3150 sbi->s_ndevs = i + 1;
3152 #ifdef CONFIG_BLK_DEV_ZONED
3153 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
3154 !f2fs_sb_has_blkzoned(sbi)) {
3155 f2fs_err(sbi, "Zoned block device feature not enabled\n");
3158 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
3159 if (init_blkz_info(sbi, i)) {
3160 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
3163 if (max_devices == 1)
3165 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
3167 FDEV(i).total_segments,
3168 FDEV(i).start_blk, FDEV(i).end_blk,
3169 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
3170 "Host-aware" : "Host-managed");
3174 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
3176 FDEV(i).total_segments,
3177 FDEV(i).start_blk, FDEV(i).end_blk);
3180 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
3184 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
3186 #ifdef CONFIG_UNICODE
3187 if (f2fs_sb_has_casefold(sbi) && !sbi->s_encoding) {
3188 const struct f2fs_sb_encodings *encoding_info;
3189 struct unicode_map *encoding;
3190 __u16 encoding_flags;
3192 if (f2fs_sb_has_encrypt(sbi)) {
3194 "Can't mount with encoding and encryption");
3198 if (f2fs_sb_read_encoding(sbi->raw_super, &encoding_info,
3201 "Encoding requested by superblock is unknown");
3205 encoding = utf8_load(encoding_info->version);
3206 if (IS_ERR(encoding)) {
3208 "can't mount with superblock charset: %s-%s "
3209 "not supported by the kernel. flags: 0x%x.",
3210 encoding_info->name, encoding_info->version,
3212 return PTR_ERR(encoding);
3214 f2fs_info(sbi, "Using encoding defined by superblock: "
3215 "%s-%s with flags 0x%hx", encoding_info->name,
3216 encoding_info->version?:"\b", encoding_flags);
3218 sbi->s_encoding = encoding;
3219 sbi->s_encoding_flags = encoding_flags;
3220 sbi->sb->s_d_op = &f2fs_dentry_ops;
3223 if (f2fs_sb_has_casefold(sbi)) {
3224 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
3231 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
3233 struct f2fs_sm_info *sm_i = SM_I(sbi);
3235 /* adjust parameters according to the volume size */
3236 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
3237 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
3238 sm_i->dcc_info->discard_granularity = 1;
3239 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
3242 sbi->readdir_ra = 1;
3245 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
3247 struct f2fs_sb_info *sbi;
3248 struct f2fs_super_block *raw_super;
3251 bool skip_recovery = false, need_fsck = false;
3252 char *options = NULL;
3253 int recovery, i, valid_super_block;
3254 struct curseg_info *seg_i;
3260 valid_super_block = -1;
3263 /* allocate memory for f2fs-specific super block info */
3264 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
3270 /* Load the checksum driver */
3271 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
3272 if (IS_ERR(sbi->s_chksum_driver)) {
3273 f2fs_err(sbi, "Cannot load crc32 driver.");
3274 err = PTR_ERR(sbi->s_chksum_driver);
3275 sbi->s_chksum_driver = NULL;
3279 /* set a block size */
3280 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
3281 f2fs_err(sbi, "unable to set blocksize");
3285 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
3290 sb->s_fs_info = sbi;
3291 sbi->raw_super = raw_super;
3293 /* precompute checksum seed for metadata */
3294 if (f2fs_sb_has_inode_chksum(sbi))
3295 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
3296 sizeof(raw_super->uuid));
3299 * The BLKZONED feature indicates that the drive was formatted with
3300 * zone alignment optimization. This is optional for host-aware
3301 * devices, but mandatory for host-managed zoned block devices.
3303 #ifndef CONFIG_BLK_DEV_ZONED
3304 if (f2fs_sb_has_blkzoned(sbi)) {
3305 f2fs_err(sbi, "Zoned block device support is not enabled");
3310 default_options(sbi);
3311 /* parse mount options */
3312 options = kstrdup((const char *)data, GFP_KERNEL);
3313 if (data && !options) {
3318 err = parse_options(sb, options);
3322 sbi->max_file_blocks = max_file_blocks();
3323 sb->s_maxbytes = sbi->max_file_blocks <<
3324 le32_to_cpu(raw_super->log_blocksize);
3325 sb->s_max_links = F2FS_LINK_MAX;
3327 err = f2fs_setup_casefold(sbi);
3332 sb->dq_op = &f2fs_quota_operations;
3333 sb->s_qcop = &f2fs_quotactl_ops;
3334 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
3336 if (f2fs_sb_has_quota_ino(sbi)) {
3337 for (i = 0; i < MAXQUOTAS; i++) {
3338 if (f2fs_qf_ino(sbi->sb, i))
3339 sbi->nquota_files++;
3344 sb->s_op = &f2fs_sops;
3345 #ifdef CONFIG_FS_ENCRYPTION
3346 sb->s_cop = &f2fs_cryptops;
3348 #ifdef CONFIG_FS_VERITY
3349 sb->s_vop = &f2fs_verityops;
3351 sb->s_xattr = f2fs_xattr_handlers;
3352 sb->s_export_op = &f2fs_export_ops;
3353 sb->s_magic = F2FS_SUPER_MAGIC;
3354 sb->s_time_gran = 1;
3355 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
3356 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
3357 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
3358 sb->s_iflags |= SB_I_CGROUPWB;
3360 /* init f2fs-specific super block info */
3361 sbi->valid_super_block = valid_super_block;
3362 mutex_init(&sbi->gc_mutex);
3363 mutex_init(&sbi->writepages);
3364 mutex_init(&sbi->cp_mutex);
3365 mutex_init(&sbi->resize_mutex);
3366 init_rwsem(&sbi->node_write);
3367 init_rwsem(&sbi->node_change);
3369 /* disallow all the data/node/meta page writes */
3370 set_sbi_flag(sbi, SBI_POR_DOING);
3371 spin_lock_init(&sbi->stat_lock);
3373 /* init iostat info */
3374 spin_lock_init(&sbi->iostat_lock);
3375 sbi->iostat_enable = false;
3377 for (i = 0; i < NR_PAGE_TYPE; i++) {
3378 int n = (i == META) ? 1: NR_TEMP_TYPE;
3384 sizeof(struct f2fs_bio_info)),
3386 if (!sbi->write_io[i]) {
3391 for (j = HOT; j < n; j++) {
3392 init_rwsem(&sbi->write_io[i][j].io_rwsem);
3393 sbi->write_io[i][j].sbi = sbi;
3394 sbi->write_io[i][j].bio = NULL;
3395 spin_lock_init(&sbi->write_io[i][j].io_lock);
3396 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
3400 init_rwsem(&sbi->cp_rwsem);
3401 init_rwsem(&sbi->quota_sem);
3402 init_waitqueue_head(&sbi->cp_wait);
3405 err = init_percpu_info(sbi);
3409 if (F2FS_IO_ALIGNED(sbi)) {
3410 sbi->write_io_dummy =
3411 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
3412 if (!sbi->write_io_dummy) {
3418 /* get an inode for meta space */
3419 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
3420 if (IS_ERR(sbi->meta_inode)) {
3421 f2fs_err(sbi, "Failed to read F2FS meta data inode");
3422 err = PTR_ERR(sbi->meta_inode);
3426 err = f2fs_get_valid_checkpoint(sbi);
3428 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
3429 goto free_meta_inode;
3432 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
3433 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3434 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
3435 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3436 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
3439 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
3440 set_sbi_flag(sbi, SBI_NEED_FSCK);
3442 /* Initialize device list */
3443 err = f2fs_scan_devices(sbi);
3445 f2fs_err(sbi, "Failed to find devices");
3449 sbi->total_valid_node_count =
3450 le32_to_cpu(sbi->ckpt->valid_node_count);
3451 percpu_counter_set(&sbi->total_valid_inode_count,
3452 le32_to_cpu(sbi->ckpt->valid_inode_count));
3453 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
3454 sbi->total_valid_block_count =
3455 le64_to_cpu(sbi->ckpt->valid_block_count);
3456 sbi->last_valid_block_count = sbi->total_valid_block_count;
3457 sbi->reserved_blocks = 0;
3458 sbi->current_reserved_blocks = 0;
3459 limit_reserve_root(sbi);
3460 adjust_unusable_cap_perc(sbi);
3462 for (i = 0; i < NR_INODE_TYPE; i++) {
3463 INIT_LIST_HEAD(&sbi->inode_list[i]);
3464 spin_lock_init(&sbi->inode_lock[i]);
3466 mutex_init(&sbi->flush_lock);
3468 f2fs_init_extent_cache_info(sbi);
3470 f2fs_init_ino_entry_info(sbi);
3472 f2fs_init_fsync_node_info(sbi);
3474 /* setup f2fs internal modules */
3475 err = f2fs_build_segment_manager(sbi);
3477 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
3481 err = f2fs_build_node_manager(sbi);
3483 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
3488 err = adjust_reserved_segment(sbi);
3492 /* For write statistics */
3493 if (sb->s_bdev->bd_part)
3494 sbi->sectors_written_start =
3495 (u64)part_stat_read(sb->s_bdev->bd_part,
3496 sectors[STAT_WRITE]);
3498 /* Read accumulated write IO statistics if exists */
3499 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3500 if (__exist_node_summaries(sbi))
3501 sbi->kbytes_written =
3502 le64_to_cpu(seg_i->journal->info.kbytes_written);
3504 f2fs_build_gc_manager(sbi);
3506 err = f2fs_build_stats(sbi);
3510 /* get an inode for node space */
3511 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3512 if (IS_ERR(sbi->node_inode)) {
3513 f2fs_err(sbi, "Failed to read node inode");
3514 err = PTR_ERR(sbi->node_inode);
3518 /* read root inode and dentry */
3519 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3521 f2fs_err(sbi, "Failed to read root inode");
3522 err = PTR_ERR(root);
3523 goto free_node_inode;
3525 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3526 !root->i_size || !root->i_nlink) {
3529 goto free_node_inode;
3532 sb->s_root = d_make_root(root); /* allocate root dentry */
3535 goto free_node_inode;
3538 err = f2fs_register_sysfs(sbi);
3540 goto free_root_inode;
3543 /* Enable quota usage during mount */
3544 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
3545 err = f2fs_enable_quotas(sb);
3547 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
3550 /* if there are nt orphan nodes free them */
3551 err = f2fs_recover_orphan_inodes(sbi);
3555 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
3556 goto reset_checkpoint;
3558 /* recover fsynced data */
3559 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
3560 !test_opt(sbi, NORECOVERY)) {
3562 * mount should be failed, when device has readonly mode, and
3563 * previous checkpoint was not done by clean system shutdown.
3565 if (f2fs_hw_is_readonly(sbi)) {
3566 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3568 f2fs_err(sbi, "Need to recover fsync data, but write access unavailable");
3571 f2fs_info(sbi, "write access unavailable, skipping recovery");
3572 goto reset_checkpoint;
3576 set_sbi_flag(sbi, SBI_NEED_FSCK);
3579 goto reset_checkpoint;
3581 err = f2fs_recover_fsync_data(sbi, false);
3584 skip_recovery = true;
3586 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
3591 err = f2fs_recover_fsync_data(sbi, true);
3593 if (!f2fs_readonly(sb) && err > 0) {
3595 f2fs_err(sbi, "Need to recover fsync data");
3600 /* f2fs_recover_fsync_data() cleared this already */
3601 clear_sbi_flag(sbi, SBI_POR_DOING);
3603 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
3604 err = f2fs_disable_checkpoint(sbi);
3606 goto sync_free_meta;
3607 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
3608 f2fs_enable_checkpoint(sbi);
3612 * If filesystem is not mounted as read-only then
3613 * do start the gc_thread.
3615 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
3616 /* After POR, we can run background GC thread.*/
3617 err = f2fs_start_gc_thread(sbi);
3619 goto sync_free_meta;
3623 /* recover broken superblock */
3625 err = f2fs_commit_super(sbi, true);
3626 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
3627 sbi->valid_super_block ? 1 : 2, err);
3630 f2fs_join_shrinker(sbi);
3632 f2fs_tuning_parameters(sbi);
3634 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
3635 cur_cp_version(F2FS_CKPT(sbi)));
3636 f2fs_update_time(sbi, CP_TIME);
3637 f2fs_update_time(sbi, REQ_TIME);
3638 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3642 /* safe to flush all the data */
3643 sync_filesystem(sbi->sb);
3648 f2fs_truncate_quota_inode_pages(sb);
3649 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
3650 f2fs_quota_off_umount(sbi->sb);
3653 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
3654 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
3655 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
3656 * falls into an infinite loop in f2fs_sync_meta_pages().
3658 truncate_inode_pages_final(META_MAPPING(sbi));
3659 /* evict some inodes being cached by GC */
3661 f2fs_unregister_sysfs(sbi);
3666 f2fs_release_ino_entry(sbi, true);
3667 truncate_inode_pages_final(NODE_MAPPING(sbi));
3668 iput(sbi->node_inode);
3669 sbi->node_inode = NULL;
3671 f2fs_destroy_stats(sbi);
3673 f2fs_destroy_node_manager(sbi);
3675 f2fs_destroy_segment_manager(sbi);
3677 destroy_device_list(sbi);
3680 make_bad_inode(sbi->meta_inode);
3681 iput(sbi->meta_inode);
3682 sbi->meta_inode = NULL;
3684 mempool_destroy(sbi->write_io_dummy);
3686 destroy_percpu_info(sbi);
3688 for (i = 0; i < NR_PAGE_TYPE; i++)
3689 kvfree(sbi->write_io[i]);
3691 #ifdef CONFIG_UNICODE
3692 utf8_unload(sbi->s_encoding);
3696 for (i = 0; i < MAXQUOTAS; i++)
3697 kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
3703 if (sbi->s_chksum_driver)
3704 crypto_free_shash(sbi->s_chksum_driver);
3707 /* give only one another chance */
3708 if (retry_cnt > 0 && skip_recovery) {
3710 shrink_dcache_sb(sb);
3716 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
3717 const char *dev_name, void *data)
3719 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
3722 static void kill_f2fs_super(struct super_block *sb)
3725 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3727 set_sbi_flag(sbi, SBI_IS_CLOSE);
3728 f2fs_stop_gc_thread(sbi);
3729 f2fs_stop_discard_thread(sbi);
3731 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
3732 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3733 struct cp_control cpc = {
3734 .reason = CP_UMOUNT,
3736 f2fs_write_checkpoint(sbi, &cpc);
3739 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
3740 sb->s_flags &= ~SB_RDONLY;
3742 kill_block_super(sb);
3745 static struct file_system_type f2fs_fs_type = {
3746 .owner = THIS_MODULE,
3748 .mount = f2fs_mount,
3749 .kill_sb = kill_f2fs_super,
3750 .fs_flags = FS_REQUIRES_DEV,
3752 MODULE_ALIAS_FS("f2fs");
3754 static int __init init_inodecache(void)
3756 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
3757 sizeof(struct f2fs_inode_info), 0,
3758 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
3759 if (!f2fs_inode_cachep)
3764 static void destroy_inodecache(void)
3767 * Make sure all delayed rcu free inodes are flushed before we
3771 kmem_cache_destroy(f2fs_inode_cachep);
3774 static int __init init_f2fs_fs(void)
3778 if (PAGE_SIZE != F2FS_BLKSIZE) {
3779 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
3780 PAGE_SIZE, F2FS_BLKSIZE);
3784 f2fs_build_trace_ios();
3786 err = init_inodecache();
3789 err = f2fs_create_node_manager_caches();
3791 goto free_inodecache;
3792 err = f2fs_create_segment_manager_caches();
3794 goto free_node_manager_caches;
3795 err = f2fs_create_checkpoint_caches();
3797 goto free_segment_manager_caches;
3798 err = f2fs_create_extent_cache();
3800 goto free_checkpoint_caches;
3801 err = f2fs_init_sysfs();
3803 goto free_extent_cache;
3804 err = register_shrinker(&f2fs_shrinker_info);
3807 err = register_filesystem(&f2fs_fs_type);
3810 f2fs_create_root_stats();
3811 err = f2fs_init_post_read_processing();
3813 goto free_root_stats;
3817 f2fs_destroy_root_stats();
3818 unregister_filesystem(&f2fs_fs_type);
3820 unregister_shrinker(&f2fs_shrinker_info);
3824 f2fs_destroy_extent_cache();
3825 free_checkpoint_caches:
3826 f2fs_destroy_checkpoint_caches();
3827 free_segment_manager_caches:
3828 f2fs_destroy_segment_manager_caches();
3829 free_node_manager_caches:
3830 f2fs_destroy_node_manager_caches();
3832 destroy_inodecache();
3837 static void __exit exit_f2fs_fs(void)
3839 f2fs_destroy_post_read_processing();
3840 f2fs_destroy_root_stats();
3841 unregister_filesystem(&f2fs_fs_type);
3842 unregister_shrinker(&f2fs_shrinker_info);
3844 f2fs_destroy_extent_cache();
3845 f2fs_destroy_checkpoint_caches();
3846 f2fs_destroy_segment_manager_caches();
3847 f2fs_destroy_node_manager_caches();
3848 destroy_inodecache();
3849 f2fs_destroy_trace_ios();
3852 module_init(init_f2fs_fs)
3853 module_exit(exit_f2fs_fs)
3855 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3856 MODULE_DESCRIPTION("Flash Friendly File System");
3857 MODULE_LICENSE("GPL");
3858 MODULE_SOFTDEP("pre: crc32");