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