]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/f2fs/sysfs.c
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-kernel.git] / fs / f2fs / sysfs.c
CommitLineData
8ceffcb2
CY
1/*
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>
299aa41a 14#include <linux/seq_file.h>
8ceffcb2
CY
15
16#include "f2fs.h"
17#include "segment.h"
18#include "gc.h"
19
20static struct proc_dir_entry *f2fs_proc_root;
21static struct kset *f2fs_kset;
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
daeb433e 34 RESERVED_BLOCKS,
8ceffcb2
CY
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};
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);
daeb433e 56 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
8ceffcb2
CY
57 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
79static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
80 struct f2fs_sb_info *sbi, char *buf)
81{
82 unsigned char *ptr = NULL;
83 unsigned int *ui;
84
85 ptr = __struct_ptr(sbi, a->struct_type);
86 if (!ptr)
87 return -EINVAL;
88
89 ui = (unsigned int *)(ptr + a->offset);
90
91 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
92}
93
94static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
95 struct f2fs_sb_info *sbi,
96 const char *buf, size_t count)
97{
98 unsigned char *ptr;
99 unsigned long t;
100 unsigned int *ui;
101 ssize_t ret;
102
103 ptr = __struct_ptr(sbi, a->struct_type);
104 if (!ptr)
105 return -EINVAL;
106
107 ui = (unsigned int *)(ptr + a->offset);
108
109 ret = kstrtoul(skip_spaces(buf), 0, &t);
110 if (ret < 0)
111 return ret;
112#ifdef CONFIG_F2FS_FAULT_INJECTION
113 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
114 return -EINVAL;
115#endif
daeb433e
CY
116 if (a->struct_type == RESERVED_BLOCKS) {
117 spin_lock(&sbi->stat_lock);
118 if ((unsigned long)sbi->total_valid_block_count + t >
119 (unsigned long)sbi->user_block_count) {
120 spin_unlock(&sbi->stat_lock);
121 return -EINVAL;
122 }
123 *ui = t;
124 spin_unlock(&sbi->stat_lock);
125 return count;
126 }
8ceffcb2
CY
127 *ui = t;
128 return count;
129}
130
131static ssize_t f2fs_attr_show(struct kobject *kobj,
132 struct attribute *attr, char *buf)
133{
134 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
135 s_kobj);
136 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
137
138 return a->show ? a->show(a, sbi, buf) : 0;
139}
140
141static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
142 const char *buf, size_t len)
143{
144 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
145 s_kobj);
146 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
147
148 return a->store ? a->store(a, sbi, buf, len) : 0;
149}
150
151static void f2fs_sb_release(struct kobject *kobj)
152{
153 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
154 s_kobj);
155 complete(&sbi->s_kobj_unregister);
156}
157
158#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
159static struct f2fs_attr f2fs_attr_##_name = { \
160 .attr = {.name = __stringify(_name), .mode = _mode }, \
161 .show = _show, \
162 .store = _store, \
163 .struct_type = _struct_type, \
164 .offset = _offset \
165}
166
167#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
168 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
169 f2fs_sbi_show, f2fs_sbi_store, \
170 offsetof(struct struct_name, elname))
171
172#define F2FS_GENERAL_RO_ATTR(name) \
173static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
174
175F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
176F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
177F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
178F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
179F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
180F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
daeb433e 181F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
8ceffcb2
CY
182F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
183F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
184F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
185F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
186F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
187F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
188F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
189F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
190F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
191F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
192F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
193F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
194#ifdef CONFIG_F2FS_FAULT_INJECTION
195F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
196F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
197#endif
198F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
199
200#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
201static struct attribute *f2fs_attrs[] = {
202 ATTR_LIST(gc_min_sleep_time),
203 ATTR_LIST(gc_max_sleep_time),
204 ATTR_LIST(gc_no_gc_sleep_time),
205 ATTR_LIST(gc_idle),
206 ATTR_LIST(reclaim_segments),
207 ATTR_LIST(max_small_discards),
208 ATTR_LIST(batched_trim_sections),
209 ATTR_LIST(ipu_policy),
210 ATTR_LIST(min_ipu_util),
211 ATTR_LIST(min_fsync_blocks),
212 ATTR_LIST(min_hot_blocks),
213 ATTR_LIST(max_victim_search),
214 ATTR_LIST(dir_level),
215 ATTR_LIST(ram_thresh),
216 ATTR_LIST(ra_nid_pages),
217 ATTR_LIST(dirty_nats_ratio),
218 ATTR_LIST(cp_interval),
219 ATTR_LIST(idle_interval),
220#ifdef CONFIG_F2FS_FAULT_INJECTION
221 ATTR_LIST(inject_rate),
222 ATTR_LIST(inject_type),
223#endif
224 ATTR_LIST(lifetime_write_kbytes),
daeb433e 225 ATTR_LIST(reserved_blocks),
8ceffcb2
CY
226 NULL,
227};
228
229static const struct sysfs_ops f2fs_attr_ops = {
230 .show = f2fs_attr_show,
231 .store = f2fs_attr_store,
232};
233
234static struct kobj_type f2fs_ktype = {
235 .default_attrs = f2fs_attrs,
236 .sysfs_ops = &f2fs_attr_ops,
237 .release = f2fs_sb_release,
238};
239
240static int segment_info_seq_show(struct seq_file *seq, void *offset)
241{
242 struct super_block *sb = seq->private;
243 struct f2fs_sb_info *sbi = F2FS_SB(sb);
244 unsigned int total_segs =
245 le32_to_cpu(sbi->raw_super->segment_count_main);
246 int i;
247
248 seq_puts(seq, "format: segment_type|valid_blocks\n"
249 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
250
251 for (i = 0; i < total_segs; i++) {
252 struct seg_entry *se = get_seg_entry(sbi, i);
253
254 if ((i % 10) == 0)
255 seq_printf(seq, "%-10d", i);
256 seq_printf(seq, "%d|%-3u", se->type,
257 get_valid_blocks(sbi, i, false));
258 if ((i % 10) == 9 || i == (total_segs - 1))
259 seq_putc(seq, '\n');
260 else
261 seq_putc(seq, ' ');
262 }
263
264 return 0;
265}
266
267static int segment_bits_seq_show(struct seq_file *seq, void *offset)
268{
269 struct super_block *sb = seq->private;
270 struct f2fs_sb_info *sbi = F2FS_SB(sb);
271 unsigned int total_segs =
272 le32_to_cpu(sbi->raw_super->segment_count_main);
273 int i, j;
274
275 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
276 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
277
278 for (i = 0; i < total_segs; i++) {
279 struct seg_entry *se = get_seg_entry(sbi, i);
280
281 seq_printf(seq, "%-10d", i);
282 seq_printf(seq, "%d|%-3u|", se->type,
283 get_valid_blocks(sbi, i, false));
284 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
285 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
286 seq_putc(seq, '\n');
287 }
288 return 0;
289}
290
291#define F2FS_PROC_FILE_DEF(_name) \
292static int _name##_open_fs(struct inode *inode, struct file *file) \
293{ \
294 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
295} \
296 \
297static const struct file_operations f2fs_seq_##_name##_fops = { \
298 .open = _name##_open_fs, \
299 .read = seq_read, \
300 .llseek = seq_lseek, \
301 .release = single_release, \
302};
303
304F2FS_PROC_FILE_DEF(segment_info);
305F2FS_PROC_FILE_DEF(segment_bits);
306
307int __init f2fs_register_sysfs(void)
308{
309 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
310
311 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
312 if (!f2fs_kset)
313 return -ENOMEM;
314 return 0;
315}
316
317void f2fs_unregister_sysfs(void)
318{
319 kset_unregister(f2fs_kset);
320 remove_proc_entry("fs/f2fs", NULL);
321}
322
323int f2fs_init_sysfs(struct f2fs_sb_info *sbi)
324{
325 struct super_block *sb = sbi->sb;
326 int err;
327
328 if (f2fs_proc_root)
329 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
330
331 if (sbi->s_proc) {
332 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
333 &f2fs_seq_segment_info_fops, sb);
334 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
335 &f2fs_seq_segment_bits_fops, sb);
336 }
337
338 sbi->s_kobj.kset = f2fs_kset;
339 init_completion(&sbi->s_kobj_unregister);
340 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
341 "%s", sb->s_id);
342 if (err)
343 goto err_out;
344 return 0;
345err_out:
346 if (sbi->s_proc) {
347 remove_proc_entry("segment_info", sbi->s_proc);
348 remove_proc_entry("segment_bits", sbi->s_proc);
349 remove_proc_entry(sb->s_id, f2fs_proc_root);
350 }
351 return err;
352}
353
354void f2fs_exit_sysfs(struct f2fs_sb_info *sbi)
355{
356 kobject_del(&sbi->s_kobj);
357 kobject_put(&sbi->s_kobj);
358 wait_for_completion(&sbi->s_kobj_unregister);
359
360 if (sbi->s_proc) {
361 remove_proc_entry("segment_info", sbi->s_proc);
362 remove_proc_entry("segment_bits", sbi->s_proc);
363 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
364 }
365}