1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/compiler.h>
10 #include <linux/proc_fs.h>
11 #include <linux/f2fs_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/unicode.h>
14 #include <linux/ioprio.h>
19 #include <trace/events/f2fs.h>
21 static struct proc_dir_entry *f2fs_proc_root;
23 /* Sysfs support for f2fs */
25 GC_THREAD, /* struct f2fs_gc_thread */
26 SM_INFO, /* struct f2fs_sm_info */
27 DCC_INFO, /* struct discard_cmd_control */
28 NM_INFO, /* struct f2fs_nm_info */
29 F2FS_SBI, /* struct f2fs_sb_info */
30 #ifdef CONFIG_F2FS_STAT_FS
31 STAT_INFO, /* struct f2fs_stat_info */
33 #ifdef CONFIG_F2FS_FAULT_INJECTION
34 FAULT_INFO_RATE, /* struct f2fs_fault_info */
35 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
37 RESERVED_BLOCKS, /* struct f2fs_sb_info */
38 CPRC_INFO, /* struct ckpt_req_control */
42 struct attribute attr;
43 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
44 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
45 const char *, size_t);
51 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
52 struct f2fs_sb_info *sbi, char *buf);
54 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
56 if (struct_type == GC_THREAD)
57 return (unsigned char *)sbi->gc_thread;
58 else if (struct_type == SM_INFO)
59 return (unsigned char *)SM_I(sbi);
60 else if (struct_type == DCC_INFO)
61 return (unsigned char *)SM_I(sbi)->dcc_info;
62 else if (struct_type == NM_INFO)
63 return (unsigned char *)NM_I(sbi);
64 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
65 return (unsigned char *)sbi;
66 #ifdef CONFIG_F2FS_FAULT_INJECTION
67 else if (struct_type == FAULT_INFO_RATE ||
68 struct_type == FAULT_INFO_TYPE)
69 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
71 #ifdef CONFIG_F2FS_STAT_FS
72 else if (struct_type == STAT_INFO)
73 return (unsigned char *)F2FS_STAT(sbi);
75 else if (struct_type == CPRC_INFO)
76 return (unsigned char *)&sbi->cprc_info;
80 static ssize_t dirty_segments_show(struct f2fs_attr *a,
81 struct f2fs_sb_info *sbi, char *buf)
83 return sprintf(buf, "%llu\n",
84 (unsigned long long)(dirty_segments(sbi)));
87 static ssize_t free_segments_show(struct f2fs_attr *a,
88 struct f2fs_sb_info *sbi, char *buf)
90 return sprintf(buf, "%llu\n",
91 (unsigned long long)(free_segments(sbi)));
94 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
95 struct f2fs_sb_info *sbi, char *buf)
97 return sprintf(buf, "%llu\n",
98 (unsigned long long)(sbi->kbytes_written +
99 ((f2fs_get_sectors_written(sbi) -
100 sbi->sectors_written_start) >> 1)));
103 static ssize_t sb_status_show(struct f2fs_attr *a,
104 struct f2fs_sb_info *sbi, char *buf)
106 return sprintf(buf, "%lx\n", sbi->s_flag);
109 static ssize_t features_show(struct f2fs_attr *a,
110 struct f2fs_sb_info *sbi, char *buf)
114 if (f2fs_sb_has_encrypt(sbi))
115 len += scnprintf(buf, PAGE_SIZE - len, "%s",
117 if (f2fs_sb_has_blkzoned(sbi))
118 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
119 len ? ", " : "", "blkzoned");
120 if (f2fs_sb_has_extra_attr(sbi))
121 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
122 len ? ", " : "", "extra_attr");
123 if (f2fs_sb_has_project_quota(sbi))
124 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
125 len ? ", " : "", "projquota");
126 if (f2fs_sb_has_inode_chksum(sbi))
127 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
128 len ? ", " : "", "inode_checksum");
129 if (f2fs_sb_has_flexible_inline_xattr(sbi))
130 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
131 len ? ", " : "", "flexible_inline_xattr");
132 if (f2fs_sb_has_quota_ino(sbi))
133 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
134 len ? ", " : "", "quota_ino");
135 if (f2fs_sb_has_inode_crtime(sbi))
136 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
137 len ? ", " : "", "inode_crtime");
138 if (f2fs_sb_has_lost_found(sbi))
139 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
140 len ? ", " : "", "lost_found");
141 if (f2fs_sb_has_verity(sbi))
142 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
143 len ? ", " : "", "verity");
144 if (f2fs_sb_has_sb_chksum(sbi))
145 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
146 len ? ", " : "", "sb_checksum");
147 if (f2fs_sb_has_casefold(sbi))
148 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
149 len ? ", " : "", "casefold");
150 if (f2fs_sb_has_compression(sbi))
151 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
152 len ? ", " : "", "compression");
153 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
154 len ? ", " : "", "pin_file");
155 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
159 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
160 struct f2fs_sb_info *sbi, char *buf)
162 return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
165 static ssize_t unusable_show(struct f2fs_attr *a,
166 struct f2fs_sb_info *sbi, char *buf)
170 if (test_opt(sbi, DISABLE_CHECKPOINT))
171 unusable = sbi->unusable_block_count;
173 unusable = f2fs_get_unusable_blocks(sbi);
174 return sprintf(buf, "%llu\n", (unsigned long long)unusable);
177 static ssize_t encoding_show(struct f2fs_attr *a,
178 struct f2fs_sb_info *sbi, char *buf)
180 #ifdef CONFIG_UNICODE
181 struct super_block *sb = sbi->sb;
183 if (f2fs_sb_has_casefold(sbi))
184 return snprintf(buf, PAGE_SIZE, "%s (%d.%d.%d)\n",
185 sb->s_encoding->charset,
186 (sb->s_encoding->version >> 16) & 0xff,
187 (sb->s_encoding->version >> 8) & 0xff,
188 sb->s_encoding->version & 0xff);
190 return sprintf(buf, "(none)");
193 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
194 struct f2fs_sb_info *sbi, char *buf)
196 return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
199 #ifdef CONFIG_F2FS_STAT_FS
200 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
201 struct f2fs_sb_info *sbi, char *buf)
203 struct f2fs_stat_info *si = F2FS_STAT(sbi);
205 return sprintf(buf, "%llu\n",
206 (unsigned long long)(si->tot_blks -
207 (si->bg_data_blks + si->bg_node_blks)));
210 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
211 struct f2fs_sb_info *sbi, char *buf)
213 struct f2fs_stat_info *si = F2FS_STAT(sbi);
215 return sprintf(buf, "%llu\n",
216 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
219 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
220 struct f2fs_sb_info *sbi, char *buf)
222 struct f2fs_stat_info *si = F2FS_STAT(sbi);
224 si->dirty_count = dirty_segments(sbi);
225 f2fs_update_sit_info(sbi);
226 return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
230 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
231 struct f2fs_sb_info *sbi, char *buf)
233 return snprintf(buf, PAGE_SIZE, "%llu\n",
234 (unsigned long long)MAIN_BLKADDR(sbi));
237 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
238 struct f2fs_sb_info *sbi, char *buf)
240 unsigned char *ptr = NULL;
243 ptr = __struct_ptr(sbi, a->struct_type);
247 if (!strcmp(a->attr.name, "extension_list")) {
248 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
249 sbi->raw_super->extension_list;
250 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
251 int hot_count = sbi->raw_super->hot_ext_count;
254 len += scnprintf(buf + len, PAGE_SIZE - len,
255 "cold file extension:\n");
256 for (i = 0; i < cold_count; i++)
257 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
260 len += scnprintf(buf + len, PAGE_SIZE - len,
261 "hot file extension:\n");
262 for (i = cold_count; i < cold_count + hot_count; i++)
263 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
268 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
269 struct ckpt_req_control *cprc = &sbi->cprc_info;
271 int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
272 int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
274 if (class == IOPRIO_CLASS_RT)
275 len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
276 else if (class == IOPRIO_CLASS_BE)
277 len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
281 len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
285 ui = (unsigned int *)(ptr + a->offset);
287 return sprintf(buf, "%u\n", *ui);
290 static ssize_t __sbi_store(struct f2fs_attr *a,
291 struct f2fs_sb_info *sbi,
292 const char *buf, size_t count)
299 ptr = __struct_ptr(sbi, a->struct_type);
303 if (!strcmp(a->attr.name, "extension_list")) {
304 const char *name = strim((char *)buf);
305 bool set = true, hot;
307 if (!strncmp(name, "[h]", 3))
309 else if (!strncmp(name, "[c]", 3))
321 if (strlen(name) >= F2FS_EXTENSION_LEN)
324 down_write(&sbi->sb_lock);
326 ret = f2fs_update_extension_list(sbi, name, hot, set);
330 ret = f2fs_commit_super(sbi, false);
332 f2fs_update_extension_list(sbi, name, hot, !set);
334 up_write(&sbi->sb_lock);
335 return ret ? ret : count;
338 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
339 const char *name = strim((char *)buf);
340 struct ckpt_req_control *cprc = &sbi->cprc_info;
345 if (!strncmp(name, "rt,", 3))
346 class = IOPRIO_CLASS_RT;
347 else if (!strncmp(name, "be,", 3))
348 class = IOPRIO_CLASS_BE;
353 ret = kstrtol(name, 10, &data);
356 if (data >= IOPRIO_BE_NR || data < 0)
359 cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
360 if (test_opt(sbi, MERGE_CHECKPOINT)) {
361 ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
362 cprc->ckpt_thread_ioprio);
370 ui = (unsigned int *)(ptr + a->offset);
372 ret = kstrtoul(skip_spaces(buf), 0, &t);
375 #ifdef CONFIG_F2FS_FAULT_INJECTION
376 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
378 if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
381 if (a->struct_type == RESERVED_BLOCKS) {
382 spin_lock(&sbi->stat_lock);
383 if (t > (unsigned long)(sbi->user_block_count -
384 F2FS_OPTION(sbi).root_reserved_blocks)) {
385 spin_unlock(&sbi->stat_lock);
389 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
390 sbi->user_block_count - valid_user_blocks(sbi));
391 spin_unlock(&sbi->stat_lock);
395 if (!strcmp(a->attr.name, "discard_granularity")) {
396 if (t == 0 || t > MAX_PLIST_NUM)
404 if (!strcmp(a->attr.name, "migration_granularity")) {
405 if (t == 0 || t > sbi->segs_per_sec)
409 if (!strcmp(a->attr.name, "trim_sections"))
412 if (!strcmp(a->attr.name, "gc_urgent")) {
414 sbi->gc_mode = GC_NORMAL;
416 sbi->gc_mode = GC_URGENT_HIGH;
417 if (sbi->gc_thread) {
418 sbi->gc_thread->gc_wake = 1;
419 wake_up_interruptible_all(
420 &sbi->gc_thread->gc_wait_queue_head);
421 wake_up_discard_thread(sbi, true);
424 sbi->gc_mode = GC_URGENT_LOW;
430 if (!strcmp(a->attr.name, "gc_idle")) {
431 if (t == GC_IDLE_CB) {
432 sbi->gc_mode = GC_IDLE_CB;
433 } else if (t == GC_IDLE_GREEDY) {
434 sbi->gc_mode = GC_IDLE_GREEDY;
435 } else if (t == GC_IDLE_AT) {
436 if (!sbi->am.atgc_enabled)
438 sbi->gc_mode = GC_AT;
440 sbi->gc_mode = GC_NORMAL;
445 if (!strcmp(a->attr.name, "iostat_enable")) {
446 sbi->iostat_enable = !!t;
447 if (!sbi->iostat_enable)
448 f2fs_reset_iostat(sbi);
452 if (!strcmp(a->attr.name, "iostat_period_ms")) {
453 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
455 spin_lock(&sbi->iostat_lock);
456 sbi->iostat_period_ms = (unsigned int)t;
457 spin_unlock(&sbi->iostat_lock);
461 *ui = (unsigned int)t;
466 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
467 struct f2fs_sb_info *sbi,
468 const char *buf, size_t count)
471 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
472 a->struct_type == GC_THREAD);
475 if (!down_read_trylock(&sbi->sb->s_umount))
478 ret = __sbi_store(a, sbi, buf, count);
480 up_read(&sbi->sb->s_umount);
485 static ssize_t f2fs_attr_show(struct kobject *kobj,
486 struct attribute *attr, char *buf)
488 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
490 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
492 return a->show ? a->show(a, sbi, buf) : 0;
495 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
496 const char *buf, size_t len)
498 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
500 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
502 return a->store ? a->store(a, sbi, buf, len) : 0;
505 static void f2fs_sb_release(struct kobject *kobj)
507 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
509 complete(&sbi->s_kobj_unregister);
519 FEAT_FLEXIBLE_INLINE_XATTR,
527 FEAT_TEST_DUMMY_ENCRYPTION_V2,
530 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
531 struct f2fs_sb_info *sbi, char *buf)
536 case FEAT_ATOMIC_WRITE:
537 case FEAT_EXTRA_ATTR:
538 case FEAT_PROJECT_QUOTA:
539 case FEAT_INODE_CHECKSUM:
540 case FEAT_FLEXIBLE_INLINE_XATTR:
542 case FEAT_INODE_CRTIME:
543 case FEAT_LOST_FOUND:
545 case FEAT_SB_CHECKSUM:
547 case FEAT_COMPRESSION:
548 case FEAT_TEST_DUMMY_ENCRYPTION_V2:
549 return sprintf(buf, "supported\n");
554 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
555 static struct f2fs_attr f2fs_attr_##_name = { \
556 .attr = {.name = __stringify(_name), .mode = _mode }, \
559 .struct_type = _struct_type, \
563 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
564 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
565 f2fs_sbi_show, f2fs_sbi_store, \
566 offsetof(struct struct_name, elname))
568 #define F2FS_GENERAL_RO_ATTR(name) \
569 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
571 #define F2FS_FEATURE_RO_ATTR(_name, _id) \
572 static struct f2fs_attr f2fs_attr_##_name = { \
573 .attr = {.name = __stringify(_name), .mode = 0444 }, \
574 .show = f2fs_feature_show, \
578 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname) \
579 static struct f2fs_attr f2fs_attr_##_name = { \
580 .attr = {.name = __stringify(_name), .mode = 0444 }, \
581 .show = f2fs_sbi_show, \
582 .struct_type = _struct_type, \
583 .offset = offsetof(struct _struct_name, _elname), \
586 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
588 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
589 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
590 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
591 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
592 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
593 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
594 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
595 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
596 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
597 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
598 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
599 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
600 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
601 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
602 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
603 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
604 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
605 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
606 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
607 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
608 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
609 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
610 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
611 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
612 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
613 interval_time[DISCARD_TIME]);
614 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
615 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
616 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
617 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
618 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
619 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
620 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
621 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
622 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
623 #ifdef CONFIG_F2FS_FAULT_INJECTION
624 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
625 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
627 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
628 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
629 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
630 F2FS_GENERAL_RO_ATTR(dirty_segments);
631 F2FS_GENERAL_RO_ATTR(free_segments);
632 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
633 F2FS_GENERAL_RO_ATTR(features);
634 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
635 F2FS_GENERAL_RO_ATTR(unusable);
636 F2FS_GENERAL_RO_ATTR(encoding);
637 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
638 F2FS_GENERAL_RO_ATTR(main_blkaddr);
639 #ifdef CONFIG_F2FS_STAT_FS
640 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
641 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
642 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
643 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
644 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
645 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
646 F2FS_GENERAL_RO_ATTR(avg_vblocks);
649 #ifdef CONFIG_FS_ENCRYPTION
650 F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
651 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2, FEAT_TEST_DUMMY_ENCRYPTION_V2);
653 #ifdef CONFIG_BLK_DEV_ZONED
654 F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
656 F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
657 F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
658 F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
659 F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
660 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
661 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
662 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
663 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
664 #ifdef CONFIG_FS_VERITY
665 F2FS_FEATURE_RO_ATTR(verity, FEAT_VERITY);
667 F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
668 F2FS_FEATURE_RO_ATTR(casefold, FEAT_CASEFOLD);
669 #ifdef CONFIG_F2FS_FS_COMPRESSION
670 F2FS_FEATURE_RO_ATTR(compression, FEAT_COMPRESSION);
673 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
674 static struct attribute *f2fs_attrs[] = {
675 ATTR_LIST(gc_urgent_sleep_time),
676 ATTR_LIST(gc_min_sleep_time),
677 ATTR_LIST(gc_max_sleep_time),
678 ATTR_LIST(gc_no_gc_sleep_time),
680 ATTR_LIST(gc_urgent),
681 ATTR_LIST(reclaim_segments),
682 ATTR_LIST(main_blkaddr),
683 ATTR_LIST(max_small_discards),
684 ATTR_LIST(discard_granularity),
685 ATTR_LIST(batched_trim_sections),
686 ATTR_LIST(ipu_policy),
687 ATTR_LIST(min_ipu_util),
688 ATTR_LIST(min_fsync_blocks),
689 ATTR_LIST(min_seq_blocks),
690 ATTR_LIST(min_hot_blocks),
691 ATTR_LIST(min_ssr_sections),
692 ATTR_LIST(max_victim_search),
693 ATTR_LIST(migration_granularity),
694 ATTR_LIST(dir_level),
695 ATTR_LIST(ram_thresh),
696 ATTR_LIST(ra_nid_pages),
697 ATTR_LIST(dirty_nats_ratio),
698 ATTR_LIST(cp_interval),
699 ATTR_LIST(idle_interval),
700 ATTR_LIST(discard_idle_interval),
701 ATTR_LIST(gc_idle_interval),
702 ATTR_LIST(umount_discard_timeout),
703 ATTR_LIST(iostat_enable),
704 ATTR_LIST(iostat_period_ms),
705 ATTR_LIST(readdir_ra),
706 ATTR_LIST(max_io_bytes),
707 ATTR_LIST(gc_pin_file_thresh),
708 ATTR_LIST(extension_list),
709 #ifdef CONFIG_F2FS_FAULT_INJECTION
710 ATTR_LIST(inject_rate),
711 ATTR_LIST(inject_type),
713 ATTR_LIST(data_io_flag),
714 ATTR_LIST(node_io_flag),
715 ATTR_LIST(ckpt_thread_ioprio),
716 ATTR_LIST(dirty_segments),
717 ATTR_LIST(free_segments),
719 ATTR_LIST(lifetime_write_kbytes),
721 ATTR_LIST(reserved_blocks),
722 ATTR_LIST(current_reserved_blocks),
724 ATTR_LIST(mounted_time_sec),
725 #ifdef CONFIG_F2FS_STAT_FS
726 ATTR_LIST(cp_foreground_calls),
727 ATTR_LIST(cp_background_calls),
728 ATTR_LIST(gc_foreground_calls),
729 ATTR_LIST(gc_background_calls),
730 ATTR_LIST(moved_blocks_foreground),
731 ATTR_LIST(moved_blocks_background),
732 ATTR_LIST(avg_vblocks),
736 ATTRIBUTE_GROUPS(f2fs);
738 static struct attribute *f2fs_feat_attrs[] = {
739 #ifdef CONFIG_FS_ENCRYPTION
740 ATTR_LIST(encryption),
741 ATTR_LIST(test_dummy_encryption_v2),
743 #ifdef CONFIG_BLK_DEV_ZONED
744 ATTR_LIST(block_zoned),
746 ATTR_LIST(atomic_write),
747 ATTR_LIST(extra_attr),
748 ATTR_LIST(project_quota),
749 ATTR_LIST(inode_checksum),
750 ATTR_LIST(flexible_inline_xattr),
751 ATTR_LIST(quota_ino),
752 ATTR_LIST(inode_crtime),
753 ATTR_LIST(lost_found),
754 #ifdef CONFIG_FS_VERITY
757 ATTR_LIST(sb_checksum),
759 #ifdef CONFIG_F2FS_FS_COMPRESSION
760 ATTR_LIST(compression),
764 ATTRIBUTE_GROUPS(f2fs_feat);
766 F2FS_GENERAL_RO_ATTR(sb_status);
767 static struct attribute *f2fs_stat_attrs[] = {
768 ATTR_LIST(sb_status),
771 ATTRIBUTE_GROUPS(f2fs_stat);
773 static const struct sysfs_ops f2fs_attr_ops = {
774 .show = f2fs_attr_show,
775 .store = f2fs_attr_store,
778 static struct kobj_type f2fs_sb_ktype = {
779 .default_groups = f2fs_groups,
780 .sysfs_ops = &f2fs_attr_ops,
781 .release = f2fs_sb_release,
784 static struct kobj_type f2fs_ktype = {
785 .sysfs_ops = &f2fs_attr_ops,
788 static struct kset f2fs_kset = {
789 .kobj = {.ktype = &f2fs_ktype},
792 static struct kobj_type f2fs_feat_ktype = {
793 .default_groups = f2fs_feat_groups,
794 .sysfs_ops = &f2fs_attr_ops,
797 static struct kobject f2fs_feat = {
801 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
802 struct attribute *attr, char *buf)
804 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
806 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
808 return a->show ? a->show(a, sbi, buf) : 0;
811 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
812 const char *buf, size_t len)
814 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
816 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
818 return a->store ? a->store(a, sbi, buf, len) : 0;
821 static void f2fs_stat_kobj_release(struct kobject *kobj)
823 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
825 complete(&sbi->s_stat_kobj_unregister);
828 static const struct sysfs_ops f2fs_stat_attr_ops = {
829 .show = f2fs_stat_attr_show,
830 .store = f2fs_stat_attr_store,
833 static struct kobj_type f2fs_stat_ktype = {
834 .default_groups = f2fs_stat_groups,
835 .sysfs_ops = &f2fs_stat_attr_ops,
836 .release = f2fs_stat_kobj_release,
839 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
842 struct super_block *sb = seq->private;
843 struct f2fs_sb_info *sbi = F2FS_SB(sb);
844 unsigned int total_segs =
845 le32_to_cpu(sbi->raw_super->segment_count_main);
848 seq_puts(seq, "format: segment_type|valid_blocks\n"
849 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
851 for (i = 0; i < total_segs; i++) {
852 struct seg_entry *se = get_seg_entry(sbi, i);
855 seq_printf(seq, "%-10d", i);
856 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
857 if ((i % 10) == 9 || i == (total_segs - 1))
866 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
869 struct super_block *sb = seq->private;
870 struct f2fs_sb_info *sbi = F2FS_SB(sb);
871 unsigned int total_segs =
872 le32_to_cpu(sbi->raw_super->segment_count_main);
875 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
876 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
878 for (i = 0; i < total_segs; i++) {
879 struct seg_entry *se = get_seg_entry(sbi, i);
881 seq_printf(seq, "%-10d", i);
882 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
883 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
884 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
890 void f2fs_record_iostat(struct f2fs_sb_info *sbi)
892 unsigned long long iostat_diff[NR_IO_TYPE];
895 if (time_is_after_jiffies(sbi->iostat_next_period))
898 /* Need double check under the lock */
899 spin_lock(&sbi->iostat_lock);
900 if (time_is_after_jiffies(sbi->iostat_next_period)) {
901 spin_unlock(&sbi->iostat_lock);
904 sbi->iostat_next_period = jiffies +
905 msecs_to_jiffies(sbi->iostat_period_ms);
907 for (i = 0; i < NR_IO_TYPE; i++) {
908 iostat_diff[i] = sbi->rw_iostat[i] -
909 sbi->prev_rw_iostat[i];
910 sbi->prev_rw_iostat[i] = sbi->rw_iostat[i];
912 spin_unlock(&sbi->iostat_lock);
914 trace_f2fs_iostat(sbi, iostat_diff);
917 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
920 struct super_block *sb = seq->private;
921 struct f2fs_sb_info *sbi = F2FS_SB(sb);
922 time64_t now = ktime_get_real_seconds();
924 if (!sbi->iostat_enable)
927 seq_printf(seq, "time: %-16llu\n", now);
929 /* print app write IOs */
930 seq_puts(seq, "[WRITE]\n");
931 seq_printf(seq, "app buffered: %-16llu\n",
932 sbi->rw_iostat[APP_BUFFERED_IO]);
933 seq_printf(seq, "app direct: %-16llu\n",
934 sbi->rw_iostat[APP_DIRECT_IO]);
935 seq_printf(seq, "app mapped: %-16llu\n",
936 sbi->rw_iostat[APP_MAPPED_IO]);
938 /* print fs write IOs */
939 seq_printf(seq, "fs data: %-16llu\n",
940 sbi->rw_iostat[FS_DATA_IO]);
941 seq_printf(seq, "fs node: %-16llu\n",
942 sbi->rw_iostat[FS_NODE_IO]);
943 seq_printf(seq, "fs meta: %-16llu\n",
944 sbi->rw_iostat[FS_META_IO]);
945 seq_printf(seq, "fs gc data: %-16llu\n",
946 sbi->rw_iostat[FS_GC_DATA_IO]);
947 seq_printf(seq, "fs gc node: %-16llu\n",
948 sbi->rw_iostat[FS_GC_NODE_IO]);
949 seq_printf(seq, "fs cp data: %-16llu\n",
950 sbi->rw_iostat[FS_CP_DATA_IO]);
951 seq_printf(seq, "fs cp node: %-16llu\n",
952 sbi->rw_iostat[FS_CP_NODE_IO]);
953 seq_printf(seq, "fs cp meta: %-16llu\n",
954 sbi->rw_iostat[FS_CP_META_IO]);
956 /* print app read IOs */
957 seq_puts(seq, "[READ]\n");
958 seq_printf(seq, "app buffered: %-16llu\n",
959 sbi->rw_iostat[APP_BUFFERED_READ_IO]);
960 seq_printf(seq, "app direct: %-16llu\n",
961 sbi->rw_iostat[APP_DIRECT_READ_IO]);
962 seq_printf(seq, "app mapped: %-16llu\n",
963 sbi->rw_iostat[APP_MAPPED_READ_IO]);
965 /* print fs read IOs */
966 seq_printf(seq, "fs data: %-16llu\n",
967 sbi->rw_iostat[FS_DATA_READ_IO]);
968 seq_printf(seq, "fs gc data: %-16llu\n",
969 sbi->rw_iostat[FS_GDATA_READ_IO]);
970 seq_printf(seq, "fs compr_data: %-16llu\n",
971 sbi->rw_iostat[FS_CDATA_READ_IO]);
972 seq_printf(seq, "fs node: %-16llu\n",
973 sbi->rw_iostat[FS_NODE_READ_IO]);
974 seq_printf(seq, "fs meta: %-16llu\n",
975 sbi->rw_iostat[FS_META_READ_IO]);
977 /* print other IOs */
978 seq_puts(seq, "[OTHER]\n");
979 seq_printf(seq, "fs discard: %-16llu\n",
980 sbi->rw_iostat[FS_DISCARD]);
985 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
988 struct super_block *sb = seq->private;
989 struct f2fs_sb_info *sbi = F2FS_SB(sb);
990 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
993 seq_puts(seq, "format: victim_secmap bitmaps\n");
995 for (i = 0; i < MAIN_SECS(sbi); i++) {
997 seq_printf(seq, "%-10d", i);
998 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
999 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1000 seq_putc(seq, '\n');
1007 int __init f2fs_init_sysfs(void)
1011 kobject_set_name(&f2fs_kset.kobj, "f2fs");
1012 f2fs_kset.kobj.parent = fs_kobj;
1013 ret = kset_register(&f2fs_kset);
1017 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1020 kobject_put(&f2fs_feat);
1021 kset_unregister(&f2fs_kset);
1023 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1028 void f2fs_exit_sysfs(void)
1030 kobject_put(&f2fs_feat);
1031 kset_unregister(&f2fs_kset);
1032 remove_proc_entry("fs/f2fs", NULL);
1033 f2fs_proc_root = NULL;
1036 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1038 struct super_block *sb = sbi->sb;
1041 sbi->s_kobj.kset = &f2fs_kset;
1042 init_completion(&sbi->s_kobj_unregister);
1043 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1048 sbi->s_stat_kobj.kset = &f2fs_kset;
1049 init_completion(&sbi->s_stat_kobj_unregister);
1050 err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1051 &sbi->s_kobj, "stat");
1056 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1059 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
1060 segment_info_seq_show, sb);
1061 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
1062 segment_bits_seq_show, sb);
1063 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
1064 iostat_info_seq_show, sb);
1065 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
1066 victim_bits_seq_show, sb);
1070 kobject_put(&sbi->s_stat_kobj);
1071 wait_for_completion(&sbi->s_stat_kobj_unregister);
1073 kobject_put(&sbi->s_kobj);
1074 wait_for_completion(&sbi->s_kobj_unregister);
1078 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1081 remove_proc_entry("iostat_info", sbi->s_proc);
1082 remove_proc_entry("segment_info", sbi->s_proc);
1083 remove_proc_entry("segment_bits", sbi->s_proc);
1084 remove_proc_entry("victim_bits", sbi->s_proc);
1085 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1088 kobject_del(&sbi->s_stat_kobj);
1089 kobject_put(&sbi->s_stat_kobj);
1090 wait_for_completion(&sbi->s_stat_kobj_unregister);
1092 kobject_del(&sbi->s_kobj);
1093 kobject_put(&sbi->s_kobj);
1094 wait_for_completion(&sbi->s_kobj_unregister);