blob: 89f25efffd4361c0a427746ed1d1fcb2097eb5b1 [file] [log] [blame]
Chao Yu8ceffcb2017-06-14 17:39:47 +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/proc_fs.h>
13#include <linux/f2fs_fs.h>
Jaegeuk Kim299aa412017-07-13 17:45:21 -070014#include <linux/seq_file.h>
Chao Yu8ceffcb2017-06-14 17:39:47 +080015
16#include "f2fs.h"
17#include "segment.h"
18#include "gc.h"
19
20static struct proc_dir_entry *f2fs_proc_root;
Chao Yu8ceffcb2017-06-14 17:39:47 +080021
22/* Sysfs support for f2fs */
23enum {
24 GC_THREAD, /* struct f2fs_gc_thread */
25 SM_INFO, /* struct f2fs_sm_info */
26 DCC_INFO, /* struct discard_cmd_control */
27 NM_INFO, /* struct f2fs_nm_info */
28 F2FS_SBI, /* struct f2fs_sb_info */
29#ifdef CONFIG_F2FS_FAULT_INJECTION
30 FAULT_INFO_RATE, /* struct f2fs_fault_info */
31 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
32#endif
Chao Yudaeb4332017-06-26 16:24:41 +080033 RESERVED_BLOCKS,
Chao Yu8ceffcb2017-06-14 17:39:47 +080034};
35
36struct f2fs_attr {
37 struct attribute attr;
38 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
39 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
40 const char *, size_t);
41 int struct_type;
42 int offset;
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -070043 int id;
Chao Yu8ceffcb2017-06-14 17:39:47 +080044};
45
46static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
47{
48 if (struct_type == GC_THREAD)
49 return (unsigned char *)sbi->gc_thread;
50 else if (struct_type == SM_INFO)
51 return (unsigned char *)SM_I(sbi);
52 else if (struct_type == DCC_INFO)
53 return (unsigned char *)SM_I(sbi)->dcc_info;
54 else if (struct_type == NM_INFO)
55 return (unsigned char *)NM_I(sbi);
Chao Yudaeb4332017-06-26 16:24:41 +080056 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
Chao Yu8ceffcb2017-06-14 17:39:47 +080057 return (unsigned char *)sbi;
58#ifdef CONFIG_F2FS_FAULT_INJECTION
59 else if (struct_type == FAULT_INFO_RATE ||
60 struct_type == FAULT_INFO_TYPE)
61 return (unsigned char *)&sbi->fault_info;
62#endif
63 return NULL;
64}
65
66static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
67 struct f2fs_sb_info *sbi, char *buf)
68{
69 struct super_block *sb = sbi->sb;
70
71 if (!sb->s_bdev->bd_part)
72 return snprintf(buf, PAGE_SIZE, "0\n");
73
74 return snprintf(buf, PAGE_SIZE, "%llu\n",
75 (unsigned long long)(sbi->kbytes_written +
76 BD_PART_WRITTEN(sbi)));
77}
78
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -070079static ssize_t features_show(struct f2fs_attr *a,
80 struct f2fs_sb_info *sbi, char *buf)
81{
82 struct super_block *sb = sbi->sb;
83 int len = 0;
84
85 if (!sb->s_bdev->bd_part)
86 return snprintf(buf, PAGE_SIZE, "0\n");
87
88 if (f2fs_sb_has_crypto(sb))
89 len += snprintf(buf, PAGE_SIZE - len, "%s",
90 "encryption");
91 if (f2fs_sb_mounted_blkzoned(sb))
92 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
93 len ? ", " : "", "blkzoned");
94 if (f2fs_sb_has_extra_attr(sb))
95 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
96 len ? ", " : "", "extra_attr");
97 if (f2fs_sb_has_project_quota(sb))
98 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
99 len ? ", " : "", "projquota");
100 if (f2fs_sb_has_inode_chksum(sb))
101 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
102 len ? ", " : "", "inode_checksum");
103 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
104 return len;
105}
106
Chao Yu8ceffcb2017-06-14 17:39:47 +0800107static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
108 struct f2fs_sb_info *sbi, char *buf)
109{
110 unsigned char *ptr = NULL;
111 unsigned int *ui;
112
113 ptr = __struct_ptr(sbi, a->struct_type);
114 if (!ptr)
115 return -EINVAL;
116
117 ui = (unsigned int *)(ptr + a->offset);
118
119 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
120}
121
122static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
123 struct f2fs_sb_info *sbi,
124 const char *buf, size_t count)
125{
126 unsigned char *ptr;
127 unsigned long t;
128 unsigned int *ui;
129 ssize_t ret;
130
131 ptr = __struct_ptr(sbi, a->struct_type);
132 if (!ptr)
133 return -EINVAL;
134
135 ui = (unsigned int *)(ptr + a->offset);
136
137 ret = kstrtoul(skip_spaces(buf), 0, &t);
138 if (ret < 0)
139 return ret;
140#ifdef CONFIG_F2FS_FAULT_INJECTION
141 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
142 return -EINVAL;
143#endif
Chao Yudaeb4332017-06-26 16:24:41 +0800144 if (a->struct_type == RESERVED_BLOCKS) {
145 spin_lock(&sbi->stat_lock);
146 if ((unsigned long)sbi->total_valid_block_count + t >
147 (unsigned long)sbi->user_block_count) {
148 spin_unlock(&sbi->stat_lock);
149 return -EINVAL;
150 }
151 *ui = t;
152 spin_unlock(&sbi->stat_lock);
153 return count;
154 }
Chao Yu969d1b12017-08-07 23:09:56 +0800155
156 if (!strcmp(a->attr.name, "discard_granularity")) {
Chao Yu969d1b12017-08-07 23:09:56 +0800157 if (t == 0 || t > MAX_PLIST_NUM)
158 return -EINVAL;
159 if (t == *ui)
160 return count;
Chao Yu80647e52017-09-12 14:25:35 +0800161 *ui = t;
Chao Yu969d1b12017-08-07 23:09:56 +0800162 return count;
163 }
164
Chao Yu8ceffcb2017-06-14 17:39:47 +0800165 *ui = t;
Chao Yub0af6d42017-08-02 23:21:48 +0800166
167 if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
168 f2fs_reset_iostat(sbi);
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700169 if (!strcmp(a->attr.name, "gc_urgent") && t == 1 && sbi->gc_thread) {
170 sbi->gc_thread->gc_wake = 1;
171 wake_up_interruptible_all(&sbi->gc_thread->gc_wait_queue_head);
Jaegeuk Kim01983c72017-08-22 21:15:43 -0700172 wake_up_discard_thread(sbi, true);
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700173 }
Chao Yub0af6d42017-08-02 23:21:48 +0800174
Chao Yu8ceffcb2017-06-14 17:39:47 +0800175 return count;
176}
177
178static ssize_t f2fs_attr_show(struct kobject *kobj,
179 struct attribute *attr, char *buf)
180{
181 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
182 s_kobj);
183 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
184
185 return a->show ? a->show(a, sbi, buf) : 0;
186}
187
188static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
189 const char *buf, size_t len)
190{
191 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
192 s_kobj);
193 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
194
195 return a->store ? a->store(a, sbi, buf, len) : 0;
196}
197
198static void f2fs_sb_release(struct kobject *kobj)
199{
200 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
201 s_kobj);
202 complete(&sbi->s_kobj_unregister);
203}
204
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700205enum feat_id {
206 FEAT_CRYPTO = 0,
207 FEAT_BLKZONED,
208 FEAT_ATOMIC_WRITE,
209 FEAT_EXTRA_ATTR,
210 FEAT_PROJECT_QUOTA,
211 FEAT_INODE_CHECKSUM,
212};
213
214static ssize_t f2fs_feature_show(struct f2fs_attr *a,
215 struct f2fs_sb_info *sbi, char *buf)
216{
217 switch (a->id) {
218 case FEAT_CRYPTO:
219 case FEAT_BLKZONED:
220 case FEAT_ATOMIC_WRITE:
221 case FEAT_EXTRA_ATTR:
222 case FEAT_PROJECT_QUOTA:
223 case FEAT_INODE_CHECKSUM:
224 return snprintf(buf, PAGE_SIZE, "supported\n");
225 }
226 return 0;
227}
228
Chao Yu8ceffcb2017-06-14 17:39:47 +0800229#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
230static struct f2fs_attr f2fs_attr_##_name = { \
231 .attr = {.name = __stringify(_name), .mode = _mode }, \
232 .show = _show, \
233 .store = _store, \
234 .struct_type = _struct_type, \
235 .offset = _offset \
236}
237
238#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
239 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
240 f2fs_sbi_show, f2fs_sbi_store, \
241 offsetof(struct struct_name, elname))
242
243#define F2FS_GENERAL_RO_ATTR(name) \
244static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
245
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700246#define F2FS_FEATURE_RO_ATTR(_name, _id) \
247static struct f2fs_attr f2fs_attr_##_name = { \
248 .attr = {.name = __stringify(_name), .mode = 0444 }, \
249 .show = f2fs_feature_show, \
250 .id = _id, \
251}
252
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700253F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
254 urgent_sleep_time);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800255F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
256F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
257F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
258F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700259F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent, gc_urgent);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800260F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
261F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
Chao Yu969d1b12017-08-07 23:09:56 +0800262F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
Chao Yudaeb4332017-06-26 16:24:41 +0800263F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800264F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
265F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
266F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
267F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
268F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
269F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
270F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
271F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
272F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
273F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
274F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
275F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
Chao Yub0af6d42017-08-02 23:21:48 +0800276F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800277#ifdef CONFIG_F2FS_FAULT_INJECTION
278F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
279F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
280#endif
281F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700282F2FS_GENERAL_RO_ATTR(features);
283
284#ifdef CONFIG_F2FS_FS_ENCRYPTION
285F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
286#endif
287#ifdef CONFIG_BLK_DEV_ZONED
288F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
289#endif
290F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
291F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
292F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
293F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800294
295#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
296static struct attribute *f2fs_attrs[] = {
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700297 ATTR_LIST(gc_urgent_sleep_time),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800298 ATTR_LIST(gc_min_sleep_time),
299 ATTR_LIST(gc_max_sleep_time),
300 ATTR_LIST(gc_no_gc_sleep_time),
301 ATTR_LIST(gc_idle),
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700302 ATTR_LIST(gc_urgent),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800303 ATTR_LIST(reclaim_segments),
304 ATTR_LIST(max_small_discards),
Chao Yu969d1b12017-08-07 23:09:56 +0800305 ATTR_LIST(discard_granularity),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800306 ATTR_LIST(batched_trim_sections),
307 ATTR_LIST(ipu_policy),
308 ATTR_LIST(min_ipu_util),
309 ATTR_LIST(min_fsync_blocks),
310 ATTR_LIST(min_hot_blocks),
311 ATTR_LIST(max_victim_search),
312 ATTR_LIST(dir_level),
313 ATTR_LIST(ram_thresh),
314 ATTR_LIST(ra_nid_pages),
315 ATTR_LIST(dirty_nats_ratio),
316 ATTR_LIST(cp_interval),
317 ATTR_LIST(idle_interval),
Chao Yub0af6d42017-08-02 23:21:48 +0800318 ATTR_LIST(iostat_enable),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800319#ifdef CONFIG_F2FS_FAULT_INJECTION
320 ATTR_LIST(inject_rate),
321 ATTR_LIST(inject_type),
322#endif
323 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700324 ATTR_LIST(features),
Chao Yudaeb4332017-06-26 16:24:41 +0800325 ATTR_LIST(reserved_blocks),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800326 NULL,
327};
328
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700329static struct attribute *f2fs_feat_attrs[] = {
330#ifdef CONFIG_F2FS_FS_ENCRYPTION
331 ATTR_LIST(encryption),
332#endif
333#ifdef CONFIG_BLK_DEV_ZONED
334 ATTR_LIST(block_zoned),
335#endif
336 ATTR_LIST(atomic_write),
337 ATTR_LIST(extra_attr),
338 ATTR_LIST(project_quota),
339 ATTR_LIST(inode_checksum),
340 NULL,
341};
342
Chao Yu8ceffcb2017-06-14 17:39:47 +0800343static const struct sysfs_ops f2fs_attr_ops = {
344 .show = f2fs_attr_show,
345 .store = f2fs_attr_store,
346};
347
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700348static struct kobj_type f2fs_sb_ktype = {
Chao Yu8ceffcb2017-06-14 17:39:47 +0800349 .default_attrs = f2fs_attrs,
350 .sysfs_ops = &f2fs_attr_ops,
351 .release = f2fs_sb_release,
352};
353
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700354static struct kobj_type f2fs_ktype = {
355 .sysfs_ops = &f2fs_attr_ops,
356};
357
358static struct kset f2fs_kset = {
359 .kobj = {.ktype = &f2fs_ktype},
360};
361
362static struct kobj_type f2fs_feat_ktype = {
363 .default_attrs = f2fs_feat_attrs,
364 .sysfs_ops = &f2fs_attr_ops,
365};
366
367static struct kobject f2fs_feat = {
368 .kset = &f2fs_kset,
369};
370
Chao Yu8ceffcb2017-06-14 17:39:47 +0800371static int segment_info_seq_show(struct seq_file *seq, void *offset)
372{
373 struct super_block *sb = seq->private;
374 struct f2fs_sb_info *sbi = F2FS_SB(sb);
375 unsigned int total_segs =
376 le32_to_cpu(sbi->raw_super->segment_count_main);
377 int i;
378
379 seq_puts(seq, "format: segment_type|valid_blocks\n"
380 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
381
382 for (i = 0; i < total_segs; i++) {
383 struct seg_entry *se = get_seg_entry(sbi, i);
384
385 if ((i % 10) == 0)
386 seq_printf(seq, "%-10d", i);
387 seq_printf(seq, "%d|%-3u", se->type,
388 get_valid_blocks(sbi, i, false));
389 if ((i % 10) == 9 || i == (total_segs - 1))
390 seq_putc(seq, '\n');
391 else
392 seq_putc(seq, ' ');
393 }
394
395 return 0;
396}
397
398static int segment_bits_seq_show(struct seq_file *seq, void *offset)
399{
400 struct super_block *sb = seq->private;
401 struct f2fs_sb_info *sbi = F2FS_SB(sb);
402 unsigned int total_segs =
403 le32_to_cpu(sbi->raw_super->segment_count_main);
404 int i, j;
405
406 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
407 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
408
409 for (i = 0; i < total_segs; i++) {
410 struct seg_entry *se = get_seg_entry(sbi, i);
411
412 seq_printf(seq, "%-10d", i);
413 seq_printf(seq, "%d|%-3u|", se->type,
414 get_valid_blocks(sbi, i, false));
415 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
416 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
417 seq_putc(seq, '\n');
418 }
419 return 0;
420}
421
Chao Yub0af6d42017-08-02 23:21:48 +0800422static int iostat_info_seq_show(struct seq_file *seq, void *offset)
423{
424 struct super_block *sb = seq->private;
425 struct f2fs_sb_info *sbi = F2FS_SB(sb);
426 time64_t now = ktime_get_real_seconds();
427
428 if (!sbi->iostat_enable)
429 return 0;
430
431 seq_printf(seq, "time: %-16llu\n", now);
432
433 /* print app IOs */
434 seq_printf(seq, "app buffered: %-16llu\n",
435 sbi->write_iostat[APP_BUFFERED_IO]);
436 seq_printf(seq, "app direct: %-16llu\n",
437 sbi->write_iostat[APP_DIRECT_IO]);
438 seq_printf(seq, "app mapped: %-16llu\n",
439 sbi->write_iostat[APP_MAPPED_IO]);
440
441 /* print fs IOs */
442 seq_printf(seq, "fs data: %-16llu\n",
443 sbi->write_iostat[FS_DATA_IO]);
444 seq_printf(seq, "fs node: %-16llu\n",
445 sbi->write_iostat[FS_NODE_IO]);
446 seq_printf(seq, "fs meta: %-16llu\n",
447 sbi->write_iostat[FS_META_IO]);
448 seq_printf(seq, "fs gc data: %-16llu\n",
449 sbi->write_iostat[FS_GC_DATA_IO]);
450 seq_printf(seq, "fs gc node: %-16llu\n",
451 sbi->write_iostat[FS_GC_NODE_IO]);
452 seq_printf(seq, "fs cp data: %-16llu\n",
453 sbi->write_iostat[FS_CP_DATA_IO]);
454 seq_printf(seq, "fs cp node: %-16llu\n",
455 sbi->write_iostat[FS_CP_NODE_IO]);
456 seq_printf(seq, "fs cp meta: %-16llu\n",
457 sbi->write_iostat[FS_CP_META_IO]);
458 seq_printf(seq, "fs discard: %-16llu\n",
459 sbi->write_iostat[FS_DISCARD]);
460
461 return 0;
462}
463
Chao Yu8ceffcb2017-06-14 17:39:47 +0800464#define F2FS_PROC_FILE_DEF(_name) \
465static int _name##_open_fs(struct inode *inode, struct file *file) \
466{ \
467 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
468} \
469 \
470static const struct file_operations f2fs_seq_##_name##_fops = { \
471 .open = _name##_open_fs, \
472 .read = seq_read, \
473 .llseek = seq_lseek, \
474 .release = single_release, \
475};
476
477F2FS_PROC_FILE_DEF(segment_info);
478F2FS_PROC_FILE_DEF(segment_bits);
Chao Yub0af6d42017-08-02 23:21:48 +0800479F2FS_PROC_FILE_DEF(iostat_info);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800480
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700481int __init f2fs_init_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800482{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700483 int ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800484
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700485 kobject_set_name(&f2fs_kset.kobj, "f2fs");
486 f2fs_kset.kobj.parent = fs_kobj;
487 ret = kset_register(&f2fs_kset);
488 if (ret)
489 return ret;
490
491 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
492 NULL, "features");
493 if (ret)
494 kset_unregister(&f2fs_kset);
495 else
496 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
497 return ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800498}
499
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700500void f2fs_exit_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800501{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700502 kobject_put(&f2fs_feat);
503 kset_unregister(&f2fs_kset);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800504 remove_proc_entry("fs/f2fs", NULL);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700505 f2fs_proc_root = NULL;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800506}
507
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700508int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800509{
510 struct super_block *sb = sbi->sb;
511 int err;
512
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700513 sbi->s_kobj.kset = &f2fs_kset;
514 init_completion(&sbi->s_kobj_unregister);
515 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
516 "%s", sb->s_id);
517 if (err)
518 return err;
519
Chao Yu8ceffcb2017-06-14 17:39:47 +0800520 if (f2fs_proc_root)
521 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
522
523 if (sbi->s_proc) {
524 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
525 &f2fs_seq_segment_info_fops, sb);
526 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
527 &f2fs_seq_segment_bits_fops, sb);
Chao Yub0af6d42017-08-02 23:21:48 +0800528 proc_create_data("iostat_info", S_IRUGO, sbi->s_proc,
529 &f2fs_seq_iostat_info_fops, sb);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800530 }
Chao Yu8ceffcb2017-06-14 17:39:47 +0800531 return 0;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800532}
533
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700534void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800535{
Chao Yu8ceffcb2017-06-14 17:39:47 +0800536 if (sbi->s_proc) {
Chao Yub0af6d42017-08-02 23:21:48 +0800537 remove_proc_entry("iostat_info", sbi->s_proc);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800538 remove_proc_entry("segment_info", sbi->s_proc);
539 remove_proc_entry("segment_bits", sbi->s_proc);
540 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
541 }
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700542 kobject_del(&sbi->s_kobj);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800543}