blob: 594023260f2c4e685bd3e7a0635c9239b799a766 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * f2fs sysfs interface
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 * Copyright (c) 2017 Chao Yu <chao@kernel.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/compiler.h>
13#include <linux/proc_fs.h>
14#include <linux/f2fs_fs.h>
15#include <linux/seq_file.h>
16
17#include "f2fs.h"
18#include "segment.h"
19#include "gc.h"
20
21static struct proc_dir_entry *f2fs_proc_root;
22
23/* Sysfs support for f2fs */
24enum {
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_FAULT_INJECTION
31 FAULT_INFO_RATE, /* struct f2fs_fault_info */
32 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
33#endif
34 RESERVED_BLOCKS, /* struct f2fs_sb_info */
35};
36
37struct f2fs_attr {
38 struct attribute attr;
39 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
40 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
41 const char *, size_t);
42 int struct_type;
43 int offset;
44 int id;
45};
46
47static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
48{
49 if (struct_type == GC_THREAD)
50 return (unsigned char *)sbi->gc_thread;
51 else if (struct_type == SM_INFO)
52 return (unsigned char *)SM_I(sbi);
53 else if (struct_type == DCC_INFO)
54 return (unsigned char *)SM_I(sbi)->dcc_info;
55 else if (struct_type == NM_INFO)
56 return (unsigned char *)NM_I(sbi);
57 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
58 return (unsigned char *)sbi;
59#ifdef CONFIG_F2FS_FAULT_INJECTION
60 else if (struct_type == FAULT_INFO_RATE ||
61 struct_type == FAULT_INFO_TYPE)
62 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
63#endif
64 return NULL;
65}
66
67static ssize_t dirty_segments_show(struct f2fs_attr *a,
68 struct f2fs_sb_info *sbi, char *buf)
69{
70 return snprintf(buf, PAGE_SIZE, "%llu\n",
71 (unsigned long long)(dirty_segments(sbi)));
72}
73
74static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
75 struct f2fs_sb_info *sbi, char *buf)
76{
77 struct super_block *sb = sbi->sb;
78
79 if (!sb->s_bdev->bd_part)
80 return snprintf(buf, PAGE_SIZE, "0\n");
81
82 return snprintf(buf, PAGE_SIZE, "%llu\n",
83 (unsigned long long)(sbi->kbytes_written +
84 BD_PART_WRITTEN(sbi)));
85}
86
87static ssize_t features_show(struct f2fs_attr *a,
88 struct f2fs_sb_info *sbi, char *buf)
89{
90 struct super_block *sb = sbi->sb;
91 int len = 0;
92
93 if (!sb->s_bdev->bd_part)
94 return snprintf(buf, PAGE_SIZE, "0\n");
95
96 if (f2fs_sb_has_encrypt(sb))
97 len += snprintf(buf, PAGE_SIZE - len, "%s",
98 "encryption");
99 if (f2fs_sb_has_blkzoned(sb))
100 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
101 len ? ", " : "", "blkzoned");
102 if (f2fs_sb_has_extra_attr(sb))
103 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
104 len ? ", " : "", "extra_attr");
105 if (f2fs_sb_has_project_quota(sb))
106 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
107 len ? ", " : "", "projquota");
108 if (f2fs_sb_has_inode_chksum(sb))
109 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
110 len ? ", " : "", "inode_checksum");
111 if (f2fs_sb_has_flexible_inline_xattr(sb))
112 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
113 len ? ", " : "", "flexible_inline_xattr");
114 if (f2fs_sb_has_quota_ino(sb))
115 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
116 len ? ", " : "", "quota_ino");
117 if (f2fs_sb_has_inode_crtime(sb))
118 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
119 len ? ", " : "", "inode_crtime");
120 if (f2fs_sb_has_lost_found(sb))
121 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
122 len ? ", " : "", "lost_found");
123 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
124 return len;
125}
126
127static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
128 struct f2fs_sb_info *sbi, char *buf)
129{
130 return snprintf(buf, PAGE_SIZE, "%u\n", sbi->current_reserved_blocks);
131}
132
133static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
134 struct f2fs_sb_info *sbi, char *buf)
135{
136 unsigned char *ptr = NULL;
137 unsigned int *ui;
138
139 ptr = __struct_ptr(sbi, a->struct_type);
140 if (!ptr)
141 return -EINVAL;
142
143 if (!strcmp(a->attr.name, "extension_list")) {
144 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
145 sbi->raw_super->extension_list;
146 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
147 int hot_count = sbi->raw_super->hot_ext_count;
148 int len = 0, i;
149
150 len += snprintf(buf + len, PAGE_SIZE - len,
151 "cold file extenstion:\n");
152 for (i = 0; i < cold_count; i++)
153 len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
154 extlist[i]);
155
156 len += snprintf(buf + len, PAGE_SIZE - len,
157 "hot file extenstion:\n");
158 for (i = cold_count; i < cold_count + hot_count; i++)
159 len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
160 extlist[i]);
161 return len;
162 }
163
164 ui = (unsigned int *)(ptr + a->offset);
165
166 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
167}
168
169static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
170 struct f2fs_sb_info *sbi,
171 const char *buf, size_t count)
172{
173 unsigned char *ptr;
174 unsigned long t;
175 unsigned int *ui;
176 ssize_t ret;
177
178 ptr = __struct_ptr(sbi, a->struct_type);
179 if (!ptr)
180 return -EINVAL;
181
182 if (!strcmp(a->attr.name, "extension_list")) {
183 const char *name = strim((char *)buf);
184 bool set = true, hot;
185
186 if (!strncmp(name, "[h]", 3))
187 hot = true;
188 else if (!strncmp(name, "[c]", 3))
189 hot = false;
190 else
191 return -EINVAL;
192
193 name += 3;
194
195 if (*name == '!') {
196 name++;
197 set = false;
198 }
199
200 if (strlen(name) >= F2FS_EXTENSION_LEN)
201 return -EINVAL;
202
203 down_write(&sbi->sb_lock);
204
205 ret = update_extension_list(sbi, name, hot, set);
206 if (ret)
207 goto out;
208
209 ret = f2fs_commit_super(sbi, false);
210 if (ret)
211 update_extension_list(sbi, name, hot, !set);
212out:
213 up_write(&sbi->sb_lock);
214 return ret ? ret : count;
215 }
216
217 ui = (unsigned int *)(ptr + a->offset);
218
219 ret = kstrtoul(skip_spaces(buf), 0, &t);
220 if (ret < 0)
221 return ret;
222#ifdef CONFIG_F2FS_FAULT_INJECTION
223 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
224 return -EINVAL;
225#endif
226 if (a->struct_type == RESERVED_BLOCKS) {
227 spin_lock(&sbi->stat_lock);
228 if (t > (unsigned long)(sbi->user_block_count -
229 F2FS_OPTION(sbi).root_reserved_blocks)) {
230 spin_unlock(&sbi->stat_lock);
231 return -EINVAL;
232 }
233 *ui = t;
234 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
235 sbi->user_block_count - valid_user_blocks(sbi));
236 spin_unlock(&sbi->stat_lock);
237 return count;
238 }
239
240 if (!strcmp(a->attr.name, "discard_granularity")) {
241 if (t == 0 || t > MAX_PLIST_NUM)
242 return -EINVAL;
243 if (t == *ui)
244 return count;
245 *ui = t;
246 return count;
247 }
248
249 if (!strcmp(a->attr.name, "trim_sections"))
250 return -EINVAL;
251
252 *ui = t;
253
254 if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
255 f2fs_reset_iostat(sbi);
256 if (!strcmp(a->attr.name, "gc_urgent") && t == 1 && sbi->gc_thread) {
257 sbi->gc_thread->gc_wake = 1;
258 wake_up_interruptible_all(&sbi->gc_thread->gc_wait_queue_head);
259 wake_up_discard_thread(sbi, true);
260 }
261
262 return count;
263}
264
265static ssize_t f2fs_attr_show(struct kobject *kobj,
266 struct attribute *attr, char *buf)
267{
268 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
269 s_kobj);
270 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
271
272 return a->show ? a->show(a, sbi, buf) : 0;
273}
274
275static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
276 const char *buf, size_t len)
277{
278 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
279 s_kobj);
280 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
281
282 return a->store ? a->store(a, sbi, buf, len) : 0;
283}
284
285static void f2fs_sb_release(struct kobject *kobj)
286{
287 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
288 s_kobj);
289 complete(&sbi->s_kobj_unregister);
290}
291
292enum feat_id {
293 FEAT_CRYPTO = 0,
294 FEAT_BLKZONED,
295 FEAT_ATOMIC_WRITE,
296 FEAT_EXTRA_ATTR,
297 FEAT_PROJECT_QUOTA,
298 FEAT_INODE_CHECKSUM,
299 FEAT_FLEXIBLE_INLINE_XATTR,
300 FEAT_QUOTA_INO,
301 FEAT_INODE_CRTIME,
302 FEAT_LOST_FOUND,
303};
304
305static ssize_t f2fs_feature_show(struct f2fs_attr *a,
306 struct f2fs_sb_info *sbi, char *buf)
307{
308 switch (a->id) {
309 case FEAT_CRYPTO:
310 case FEAT_BLKZONED:
311 case FEAT_ATOMIC_WRITE:
312 case FEAT_EXTRA_ATTR:
313 case FEAT_PROJECT_QUOTA:
314 case FEAT_INODE_CHECKSUM:
315 case FEAT_FLEXIBLE_INLINE_XATTR:
316 case FEAT_QUOTA_INO:
317 case FEAT_INODE_CRTIME:
318 case FEAT_LOST_FOUND:
319 return snprintf(buf, PAGE_SIZE, "supported\n");
320 }
321 return 0;
322}
323
324#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
325static struct f2fs_attr f2fs_attr_##_name = { \
326 .attr = {.name = __stringify(_name), .mode = _mode }, \
327 .show = _show, \
328 .store = _store, \
329 .struct_type = _struct_type, \
330 .offset = _offset \
331}
332
333#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
334 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
335 f2fs_sbi_show, f2fs_sbi_store, \
336 offsetof(struct struct_name, elname))
337
338#define F2FS_GENERAL_RO_ATTR(name) \
339static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
340
341#define F2FS_FEATURE_RO_ATTR(_name, _id) \
342static struct f2fs_attr f2fs_attr_##_name = { \
343 .attr = {.name = __stringify(_name), .mode = 0444 }, \
344 .show = f2fs_feature_show, \
345 .id = _id, \
346}
347
348F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
349 urgent_sleep_time);
350F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
351F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
352F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
353F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
354F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent, gc_urgent);
355F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
356F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
357F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
358F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
359F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
360F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
361F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
362F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
363F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
364F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
365F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
366F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
367F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
368F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
369F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
370F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
371F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
372F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
373F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
374F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
375F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
376#ifdef CONFIG_F2FS_FAULT_INJECTION
377F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
378F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
379#endif
380F2FS_GENERAL_RO_ATTR(dirty_segments);
381F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
382F2FS_GENERAL_RO_ATTR(features);
383F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
384
385#ifdef CONFIG_F2FS_FS_ENCRYPTION
386F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
387#endif
388#ifdef CONFIG_BLK_DEV_ZONED
389F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
390#endif
391F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
392F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
393F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
394F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
395F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
396F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
397F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
398F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
399
400#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
401static struct attribute *f2fs_attrs[] = {
402 ATTR_LIST(gc_urgent_sleep_time),
403 ATTR_LIST(gc_min_sleep_time),
404 ATTR_LIST(gc_max_sleep_time),
405 ATTR_LIST(gc_no_gc_sleep_time),
406 ATTR_LIST(gc_idle),
407 ATTR_LIST(gc_urgent),
408 ATTR_LIST(reclaim_segments),
409 ATTR_LIST(max_small_discards),
410 ATTR_LIST(discard_granularity),
411 ATTR_LIST(batched_trim_sections),
412 ATTR_LIST(ipu_policy),
413 ATTR_LIST(min_ipu_util),
414 ATTR_LIST(min_fsync_blocks),
415 ATTR_LIST(min_hot_blocks),
416 ATTR_LIST(min_ssr_sections),
417 ATTR_LIST(max_victim_search),
418 ATTR_LIST(dir_level),
419 ATTR_LIST(ram_thresh),
420 ATTR_LIST(ra_nid_pages),
421 ATTR_LIST(dirty_nats_ratio),
422 ATTR_LIST(cp_interval),
423 ATTR_LIST(idle_interval),
424 ATTR_LIST(iostat_enable),
425 ATTR_LIST(readdir_ra),
426 ATTR_LIST(gc_pin_file_thresh),
427 ATTR_LIST(extension_list),
428#ifdef CONFIG_F2FS_FAULT_INJECTION
429 ATTR_LIST(inject_rate),
430 ATTR_LIST(inject_type),
431#endif
432 ATTR_LIST(dirty_segments),
433 ATTR_LIST(lifetime_write_kbytes),
434 ATTR_LIST(features),
435 ATTR_LIST(reserved_blocks),
436 ATTR_LIST(current_reserved_blocks),
437 NULL,
438};
439
440static struct attribute *f2fs_feat_attrs[] = {
441#ifdef CONFIG_F2FS_FS_ENCRYPTION
442 ATTR_LIST(encryption),
443#endif
444#ifdef CONFIG_BLK_DEV_ZONED
445 ATTR_LIST(block_zoned),
446#endif
447 ATTR_LIST(atomic_write),
448 ATTR_LIST(extra_attr),
449 ATTR_LIST(project_quota),
450 ATTR_LIST(inode_checksum),
451 ATTR_LIST(flexible_inline_xattr),
452 ATTR_LIST(quota_ino),
453 ATTR_LIST(inode_crtime),
454 ATTR_LIST(lost_found),
455 NULL,
456};
457
458static const struct sysfs_ops f2fs_attr_ops = {
459 .show = f2fs_attr_show,
460 .store = f2fs_attr_store,
461};
462
463static struct kobj_type f2fs_sb_ktype = {
464 .default_attrs = f2fs_attrs,
465 .sysfs_ops = &f2fs_attr_ops,
466 .release = f2fs_sb_release,
467};
468
469static struct kobj_type f2fs_ktype = {
470 .sysfs_ops = &f2fs_attr_ops,
471};
472
473static struct kset f2fs_kset = {
474 .kobj = {.ktype = &f2fs_ktype},
475};
476
477static struct kobj_type f2fs_feat_ktype = {
478 .default_attrs = f2fs_feat_attrs,
479 .sysfs_ops = &f2fs_attr_ops,
480};
481
482static struct kobject f2fs_feat = {
483 .kset = &f2fs_kset,
484};
485
486static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
487 void *offset)
488{
489 struct super_block *sb = seq->private;
490 struct f2fs_sb_info *sbi = F2FS_SB(sb);
491 unsigned int total_segs =
492 le32_to_cpu(sbi->raw_super->segment_count_main);
493 int i;
494
495 seq_puts(seq, "format: segment_type|valid_blocks\n"
496 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
497
498 for (i = 0; i < total_segs; i++) {
499 struct seg_entry *se = get_seg_entry(sbi, i);
500
501 if ((i % 10) == 0)
502 seq_printf(seq, "%-10d", i);
503 seq_printf(seq, "%d|%-3u", se->type,
504 get_valid_blocks(sbi, i, false));
505 if ((i % 10) == 9 || i == (total_segs - 1))
506 seq_putc(seq, '\n');
507 else
508 seq_putc(seq, ' ');
509 }
510
511 return 0;
512}
513
514static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
515 void *offset)
516{
517 struct super_block *sb = seq->private;
518 struct f2fs_sb_info *sbi = F2FS_SB(sb);
519 unsigned int total_segs =
520 le32_to_cpu(sbi->raw_super->segment_count_main);
521 int i, j;
522
523 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
524 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
525
526 for (i = 0; i < total_segs; i++) {
527 struct seg_entry *se = get_seg_entry(sbi, i);
528
529 seq_printf(seq, "%-10d", i);
530 seq_printf(seq, "%d|%-3u|", se->type,
531 get_valid_blocks(sbi, i, false));
532 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
533 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
534 seq_putc(seq, '\n');
535 }
536 return 0;
537}
538
539static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
540 void *offset)
541{
542 struct super_block *sb = seq->private;
543 struct f2fs_sb_info *sbi = F2FS_SB(sb);
544 time64_t now = ktime_get_real_seconds();
545
546 if (!sbi->iostat_enable)
547 return 0;
548
549 seq_printf(seq, "time: %-16llu\n", now);
550
551 /* print app IOs */
552 seq_printf(seq, "app buffered: %-16llu\n",
553 sbi->write_iostat[APP_BUFFERED_IO]);
554 seq_printf(seq, "app direct: %-16llu\n",
555 sbi->write_iostat[APP_DIRECT_IO]);
556 seq_printf(seq, "app mapped: %-16llu\n",
557 sbi->write_iostat[APP_MAPPED_IO]);
558
559 /* print fs IOs */
560 seq_printf(seq, "fs data: %-16llu\n",
561 sbi->write_iostat[FS_DATA_IO]);
562 seq_printf(seq, "fs node: %-16llu\n",
563 sbi->write_iostat[FS_NODE_IO]);
564 seq_printf(seq, "fs meta: %-16llu\n",
565 sbi->write_iostat[FS_META_IO]);
566 seq_printf(seq, "fs gc data: %-16llu\n",
567 sbi->write_iostat[FS_GC_DATA_IO]);
568 seq_printf(seq, "fs gc node: %-16llu\n",
569 sbi->write_iostat[FS_GC_NODE_IO]);
570 seq_printf(seq, "fs cp data: %-16llu\n",
571 sbi->write_iostat[FS_CP_DATA_IO]);
572 seq_printf(seq, "fs cp node: %-16llu\n",
573 sbi->write_iostat[FS_CP_NODE_IO]);
574 seq_printf(seq, "fs cp meta: %-16llu\n",
575 sbi->write_iostat[FS_CP_META_IO]);
576 seq_printf(seq, "fs discard: %-16llu\n",
577 sbi->write_iostat[FS_DISCARD]);
578
579 return 0;
580}
581
582#define F2FS_PROC_FILE_DEF(_name) \
583static int _name##_open_fs(struct inode *inode, struct file *file) \
584{ \
585 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
586} \
587 \
588static const struct file_operations f2fs_seq_##_name##_fops = { \
589 .open = _name##_open_fs, \
590 .read = seq_read, \
591 .llseek = seq_lseek, \
592 .release = single_release, \
593};
594
595F2FS_PROC_FILE_DEF(segment_info);
596F2FS_PROC_FILE_DEF(segment_bits);
597F2FS_PROC_FILE_DEF(iostat_info);
598
599int __init f2fs_init_sysfs(void)
600{
601 int ret;
602
603 kobject_set_name(&f2fs_kset.kobj, "f2fs");
604 f2fs_kset.kobj.parent = fs_kobj;
605 ret = kset_register(&f2fs_kset);
606 if (ret)
607 return ret;
608
609 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
610 NULL, "features");
611 if (ret) {
612 kobject_put(&f2fs_feat);
613 kset_unregister(&f2fs_kset);
614 } else {
615 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
616 }
617 return ret;
618}
619
620void f2fs_exit_sysfs(void)
621{
622 kobject_put(&f2fs_feat);
623 kset_unregister(&f2fs_kset);
624 remove_proc_entry("fs/f2fs", NULL);
625 f2fs_proc_root = NULL;
626}
627
628int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
629{
630 struct super_block *sb = sbi->sb;
631 int err;
632
633 sbi->s_kobj.kset = &f2fs_kset;
634 init_completion(&sbi->s_kobj_unregister);
635 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
636 "%s", sb->s_id);
637 if (err) {
638 kobject_put(&sbi->s_kobj);
639 wait_for_completion(&sbi->s_kobj_unregister);
640 return err;
641 }
642
643 if (f2fs_proc_root)
644 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
645
646 if (sbi->s_proc) {
647 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
648 &f2fs_seq_segment_info_fops, sb);
649 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
650 &f2fs_seq_segment_bits_fops, sb);
651 proc_create_data("iostat_info", S_IRUGO, sbi->s_proc,
652 &f2fs_seq_iostat_info_fops, sb);
653 }
654 return 0;
655}
656
657void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
658{
659 if (sbi->s_proc) {
660 remove_proc_entry("iostat_info", sbi->s_proc);
661 remove_proc_entry("segment_info", sbi->s_proc);
662 remove_proc_entry("segment_bits", sbi->s_proc);
663 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
664 }
665 kobject_del(&sbi->s_kobj);
666 kobject_put(&sbi->s_kobj);
667}