blob: 1e31d0c5b6ab5fb78012251086fe134ced40512c [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 Yu8ceffcb2017-06-14 17:39:47 +0800155 *ui = t;
156 return count;
157}
158
159static ssize_t f2fs_attr_show(struct kobject *kobj,
160 struct attribute *attr, char *buf)
161{
162 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
163 s_kobj);
164 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
165
166 return a->show ? a->show(a, sbi, buf) : 0;
167}
168
169static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
170 const char *buf, size_t len)
171{
172 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
173 s_kobj);
174 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
175
176 return a->store ? a->store(a, sbi, buf, len) : 0;
177}
178
179static void f2fs_sb_release(struct kobject *kobj)
180{
181 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
182 s_kobj);
183 complete(&sbi->s_kobj_unregister);
184}
185
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700186enum feat_id {
187 FEAT_CRYPTO = 0,
188 FEAT_BLKZONED,
189 FEAT_ATOMIC_WRITE,
190 FEAT_EXTRA_ATTR,
191 FEAT_PROJECT_QUOTA,
192 FEAT_INODE_CHECKSUM,
193};
194
195static ssize_t f2fs_feature_show(struct f2fs_attr *a,
196 struct f2fs_sb_info *sbi, char *buf)
197{
198 switch (a->id) {
199 case FEAT_CRYPTO:
200 case FEAT_BLKZONED:
201 case FEAT_ATOMIC_WRITE:
202 case FEAT_EXTRA_ATTR:
203 case FEAT_PROJECT_QUOTA:
204 case FEAT_INODE_CHECKSUM:
205 return snprintf(buf, PAGE_SIZE, "supported\n");
206 }
207 return 0;
208}
209
Chao Yu8ceffcb2017-06-14 17:39:47 +0800210#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
211static struct f2fs_attr f2fs_attr_##_name = { \
212 .attr = {.name = __stringify(_name), .mode = _mode }, \
213 .show = _show, \
214 .store = _store, \
215 .struct_type = _struct_type, \
216 .offset = _offset \
217}
218
219#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
220 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
221 f2fs_sbi_show, f2fs_sbi_store, \
222 offsetof(struct struct_name, elname))
223
224#define F2FS_GENERAL_RO_ATTR(name) \
225static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
226
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700227#define F2FS_FEATURE_RO_ATTR(_name, _id) \
228static struct f2fs_attr f2fs_attr_##_name = { \
229 .attr = {.name = __stringify(_name), .mode = 0444 }, \
230 .show = f2fs_feature_show, \
231 .id = _id, \
232}
233
Chao Yu8ceffcb2017-06-14 17:39:47 +0800234F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
235F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
236F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
237F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
238F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
239F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
Chao Yudaeb4332017-06-26 16:24:41 +0800240F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800241F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
242F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
243F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
244F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
245F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
246F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
247F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
248F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
249F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
250F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
251F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
252F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
253#ifdef CONFIG_F2FS_FAULT_INJECTION
254F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
255F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
256#endif
257F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700258F2FS_GENERAL_RO_ATTR(features);
259
260#ifdef CONFIG_F2FS_FS_ENCRYPTION
261F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
262#endif
263#ifdef CONFIG_BLK_DEV_ZONED
264F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
265#endif
266F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
267F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
268F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
269F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800270
271#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
272static struct attribute *f2fs_attrs[] = {
273 ATTR_LIST(gc_min_sleep_time),
274 ATTR_LIST(gc_max_sleep_time),
275 ATTR_LIST(gc_no_gc_sleep_time),
276 ATTR_LIST(gc_idle),
277 ATTR_LIST(reclaim_segments),
278 ATTR_LIST(max_small_discards),
279 ATTR_LIST(batched_trim_sections),
280 ATTR_LIST(ipu_policy),
281 ATTR_LIST(min_ipu_util),
282 ATTR_LIST(min_fsync_blocks),
283 ATTR_LIST(min_hot_blocks),
284 ATTR_LIST(max_victim_search),
285 ATTR_LIST(dir_level),
286 ATTR_LIST(ram_thresh),
287 ATTR_LIST(ra_nid_pages),
288 ATTR_LIST(dirty_nats_ratio),
289 ATTR_LIST(cp_interval),
290 ATTR_LIST(idle_interval),
291#ifdef CONFIG_F2FS_FAULT_INJECTION
292 ATTR_LIST(inject_rate),
293 ATTR_LIST(inject_type),
294#endif
295 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700296 ATTR_LIST(features),
Chao Yudaeb4332017-06-26 16:24:41 +0800297 ATTR_LIST(reserved_blocks),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800298 NULL,
299};
300
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700301static struct attribute *f2fs_feat_attrs[] = {
302#ifdef CONFIG_F2FS_FS_ENCRYPTION
303 ATTR_LIST(encryption),
304#endif
305#ifdef CONFIG_BLK_DEV_ZONED
306 ATTR_LIST(block_zoned),
307#endif
308 ATTR_LIST(atomic_write),
309 ATTR_LIST(extra_attr),
310 ATTR_LIST(project_quota),
311 ATTR_LIST(inode_checksum),
312 NULL,
313};
314
Chao Yu8ceffcb2017-06-14 17:39:47 +0800315static const struct sysfs_ops f2fs_attr_ops = {
316 .show = f2fs_attr_show,
317 .store = f2fs_attr_store,
318};
319
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700320static struct kobj_type f2fs_sb_ktype = {
Chao Yu8ceffcb2017-06-14 17:39:47 +0800321 .default_attrs = f2fs_attrs,
322 .sysfs_ops = &f2fs_attr_ops,
323 .release = f2fs_sb_release,
324};
325
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700326static struct kobj_type f2fs_ktype = {
327 .sysfs_ops = &f2fs_attr_ops,
328};
329
330static struct kset f2fs_kset = {
331 .kobj = {.ktype = &f2fs_ktype},
332};
333
334static struct kobj_type f2fs_feat_ktype = {
335 .default_attrs = f2fs_feat_attrs,
336 .sysfs_ops = &f2fs_attr_ops,
337};
338
339static struct kobject f2fs_feat = {
340 .kset = &f2fs_kset,
341};
342
Chao Yu8ceffcb2017-06-14 17:39:47 +0800343static int segment_info_seq_show(struct seq_file *seq, void *offset)
344{
345 struct super_block *sb = seq->private;
346 struct f2fs_sb_info *sbi = F2FS_SB(sb);
347 unsigned int total_segs =
348 le32_to_cpu(sbi->raw_super->segment_count_main);
349 int i;
350
351 seq_puts(seq, "format: segment_type|valid_blocks\n"
352 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
353
354 for (i = 0; i < total_segs; i++) {
355 struct seg_entry *se = get_seg_entry(sbi, i);
356
357 if ((i % 10) == 0)
358 seq_printf(seq, "%-10d", i);
359 seq_printf(seq, "%d|%-3u", se->type,
360 get_valid_blocks(sbi, i, false));
361 if ((i % 10) == 9 || i == (total_segs - 1))
362 seq_putc(seq, '\n');
363 else
364 seq_putc(seq, ' ');
365 }
366
367 return 0;
368}
369
370static int segment_bits_seq_show(struct seq_file *seq, void *offset)
371{
372 struct super_block *sb = seq->private;
373 struct f2fs_sb_info *sbi = F2FS_SB(sb);
374 unsigned int total_segs =
375 le32_to_cpu(sbi->raw_super->segment_count_main);
376 int i, j;
377
378 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
379 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
380
381 for (i = 0; i < total_segs; i++) {
382 struct seg_entry *se = get_seg_entry(sbi, i);
383
384 seq_printf(seq, "%-10d", i);
385 seq_printf(seq, "%d|%-3u|", se->type,
386 get_valid_blocks(sbi, i, false));
387 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
388 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
389 seq_putc(seq, '\n');
390 }
391 return 0;
392}
393
394#define F2FS_PROC_FILE_DEF(_name) \
395static int _name##_open_fs(struct inode *inode, struct file *file) \
396{ \
397 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
398} \
399 \
400static const struct file_operations f2fs_seq_##_name##_fops = { \
401 .open = _name##_open_fs, \
402 .read = seq_read, \
403 .llseek = seq_lseek, \
404 .release = single_release, \
405};
406
407F2FS_PROC_FILE_DEF(segment_info);
408F2FS_PROC_FILE_DEF(segment_bits);
409
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700410int __init f2fs_init_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800411{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700412 int ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800413
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700414 kobject_set_name(&f2fs_kset.kobj, "f2fs");
415 f2fs_kset.kobj.parent = fs_kobj;
416 ret = kset_register(&f2fs_kset);
417 if (ret)
418 return ret;
419
420 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
421 NULL, "features");
422 if (ret)
423 kset_unregister(&f2fs_kset);
424 else
425 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
426 return ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800427}
428
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700429void f2fs_exit_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800430{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700431 kobject_put(&f2fs_feat);
432 kset_unregister(&f2fs_kset);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800433 remove_proc_entry("fs/f2fs", NULL);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700434 f2fs_proc_root = NULL;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800435}
436
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700437int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800438{
439 struct super_block *sb = sbi->sb;
440 int err;
441
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700442 sbi->s_kobj.kset = &f2fs_kset;
443 init_completion(&sbi->s_kobj_unregister);
444 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
445 "%s", sb->s_id);
446 if (err)
447 return err;
448
Chao Yu8ceffcb2017-06-14 17:39:47 +0800449 if (f2fs_proc_root)
450 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
451
452 if (sbi->s_proc) {
453 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
454 &f2fs_seq_segment_info_fops, sb);
455 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
456 &f2fs_seq_segment_bits_fops, sb);
457 }
Chao Yu8ceffcb2017-06-14 17:39:47 +0800458 return 0;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800459}
460
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700461void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800462{
Chao Yu8ceffcb2017-06-14 17:39:47 +0800463 if (sbi->s_proc) {
464 remove_proc_entry("segment_info", sbi->s_proc);
465 remove_proc_entry("segment_bits", sbi->s_proc);
466 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
467 }
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700468 kobject_del(&sbi->s_kobj);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800469}