4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 * Copyright (c) 2017 Chao Yu <chao@kernel.org>
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.
12 #include <linux/proc_fs.h>
13 #include <linux/f2fs_fs.h>
14 #include <linux/seq_file.h>
20 static struct proc_dir_entry
*f2fs_proc_root
;
21 static struct kset
*f2fs_kset
;
23 /* Sysfs support for f2fs */
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 */
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);
46 static unsigned char *__struct_ptr(struct f2fs_sb_info
*sbi
, int struct_type
)
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
);
56 else if (struct_type
== F2FS_SBI
|| struct_type
== RESERVED_BLOCKS
)
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
;
66 static ssize_t
lifetime_write_kbytes_show(struct f2fs_attr
*a
,
67 struct f2fs_sb_info
*sbi
, char *buf
)
69 struct super_block
*sb
= sbi
->sb
;
71 if (!sb
->s_bdev
->bd_part
)
72 return snprintf(buf
, PAGE_SIZE
, "0\n");
74 return snprintf(buf
, PAGE_SIZE
, "%llu\n",
75 (unsigned long long)(sbi
->kbytes_written
+
76 BD_PART_WRITTEN(sbi
)));
79 static ssize_t
f2fs_sbi_show(struct f2fs_attr
*a
,
80 struct f2fs_sb_info
*sbi
, char *buf
)
82 unsigned char *ptr
= NULL
;
85 ptr
= __struct_ptr(sbi
, a
->struct_type
);
89 ui
= (unsigned int *)(ptr
+ a
->offset
);
91 return snprintf(buf
, PAGE_SIZE
, "%u\n", *ui
);
94 static ssize_t
f2fs_sbi_store(struct f2fs_attr
*a
,
95 struct f2fs_sb_info
*sbi
,
96 const char *buf
, size_t count
)
103 ptr
= __struct_ptr(sbi
, a
->struct_type
);
107 ui
= (unsigned int *)(ptr
+ a
->offset
);
109 ret
= kstrtoul(skip_spaces(buf
), 0, &t
);
112 #ifdef CONFIG_F2FS_FAULT_INJECTION
113 if (a
->struct_type
== FAULT_INFO_TYPE
&& t
>= (1 << FAULT_MAX
))
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
);
124 spin_unlock(&sbi
->stat_lock
);
131 static ssize_t
f2fs_attr_show(struct kobject
*kobj
,
132 struct attribute
*attr
, char *buf
)
134 struct f2fs_sb_info
*sbi
= container_of(kobj
, struct f2fs_sb_info
,
136 struct f2fs_attr
*a
= container_of(attr
, struct f2fs_attr
, attr
);
138 return a
->show
? a
->show(a
, sbi
, buf
) : 0;
141 static ssize_t
f2fs_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
142 const char *buf
, size_t len
)
144 struct f2fs_sb_info
*sbi
= container_of(kobj
, struct f2fs_sb_info
,
146 struct f2fs_attr
*a
= container_of(attr
, struct f2fs_attr
, attr
);
148 return a
->store
? a
->store(a
, sbi
, buf
, len
) : 0;
151 static void f2fs_sb_release(struct kobject
*kobj
)
153 struct f2fs_sb_info
*sbi
= container_of(kobj
, struct f2fs_sb_info
,
155 complete(&sbi
->s_kobj_unregister
);
158 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
159 static struct f2fs_attr f2fs_attr_##_name = { \
160 .attr = {.name = __stringify(_name), .mode = _mode }, \
163 .struct_type = _struct_type, \
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))
172 #define F2FS_GENERAL_RO_ATTR(name) \
173 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
175 F2FS_RW_ATTR(GC_THREAD
, f2fs_gc_kthread
, gc_min_sleep_time
, min_sleep_time
);
176 F2FS_RW_ATTR(GC_THREAD
, f2fs_gc_kthread
, gc_max_sleep_time
, max_sleep_time
);
177 F2FS_RW_ATTR(GC_THREAD
, f2fs_gc_kthread
, gc_no_gc_sleep_time
, no_gc_sleep_time
);
178 F2FS_RW_ATTR(GC_THREAD
, f2fs_gc_kthread
, gc_idle
, gc_idle
);
179 F2FS_RW_ATTR(SM_INFO
, f2fs_sm_info
, reclaim_segments
, rec_prefree_segments
);
180 F2FS_RW_ATTR(DCC_INFO
, discard_cmd_control
, max_small_discards
, max_discards
);
181 F2FS_RW_ATTR(RESERVED_BLOCKS
, f2fs_sb_info
, reserved_blocks
, reserved_blocks
);
182 F2FS_RW_ATTR(SM_INFO
, f2fs_sm_info
, batched_trim_sections
, trim_sections
);
183 F2FS_RW_ATTR(SM_INFO
, f2fs_sm_info
, ipu_policy
, ipu_policy
);
184 F2FS_RW_ATTR(SM_INFO
, f2fs_sm_info
, min_ipu_util
, min_ipu_util
);
185 F2FS_RW_ATTR(SM_INFO
, f2fs_sm_info
, min_fsync_blocks
, min_fsync_blocks
);
186 F2FS_RW_ATTR(SM_INFO
, f2fs_sm_info
, min_hot_blocks
, min_hot_blocks
);
187 F2FS_RW_ATTR(NM_INFO
, f2fs_nm_info
, ram_thresh
, ram_thresh
);
188 F2FS_RW_ATTR(NM_INFO
, f2fs_nm_info
, ra_nid_pages
, ra_nid_pages
);
189 F2FS_RW_ATTR(NM_INFO
, f2fs_nm_info
, dirty_nats_ratio
, dirty_nats_ratio
);
190 F2FS_RW_ATTR(F2FS_SBI
, f2fs_sb_info
, max_victim_search
, max_victim_search
);
191 F2FS_RW_ATTR(F2FS_SBI
, f2fs_sb_info
, dir_level
, dir_level
);
192 F2FS_RW_ATTR(F2FS_SBI
, f2fs_sb_info
, cp_interval
, interval_time
[CP_TIME
]);
193 F2FS_RW_ATTR(F2FS_SBI
, f2fs_sb_info
, idle_interval
, interval_time
[REQ_TIME
]);
194 #ifdef CONFIG_F2FS_FAULT_INJECTION
195 F2FS_RW_ATTR(FAULT_INFO_RATE
, f2fs_fault_info
, inject_rate
, inject_rate
);
196 F2FS_RW_ATTR(FAULT_INFO_TYPE
, f2fs_fault_info
, inject_type
, inject_type
);
198 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes
);
200 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
201 static 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
),
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
),
224 ATTR_LIST(lifetime_write_kbytes
),
225 ATTR_LIST(reserved_blocks
),
229 static const struct sysfs_ops f2fs_attr_ops
= {
230 .show
= f2fs_attr_show
,
231 .store
= f2fs_attr_store
,
234 static struct kobj_type f2fs_ktype
= {
235 .default_attrs
= f2fs_attrs
,
236 .sysfs_ops
= &f2fs_attr_ops
,
237 .release
= f2fs_sb_release
,
240 static int segment_info_seq_show(struct seq_file
*seq
, void *offset
)
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
);
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");
251 for (i
= 0; i
< total_segs
; i
++) {
252 struct seg_entry
*se
= get_seg_entry(sbi
, i
);
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))
267 static int segment_bits_seq_show(struct seq_file
*seq
, void *offset
)
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
);
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");
278 for (i
= 0; i
< total_segs
; i
++) {
279 struct seg_entry
*se
= get_seg_entry(sbi
, i
);
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
]);
291 #define F2FS_PROC_FILE_DEF(_name) \
292 static int _name##_open_fs(struct inode *inode, struct file *file) \
294 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
297 static const struct file_operations f2fs_seq_##_name##_fops = { \
298 .open = _name##_open_fs, \
300 .llseek = seq_lseek, \
301 .release = single_release, \
304 F2FS_PROC_FILE_DEF(segment_info
);
305 F2FS_PROC_FILE_DEF(segment_bits
);
307 int __init
f2fs_register_sysfs(void)
309 f2fs_proc_root
= proc_mkdir("fs/f2fs", NULL
);
311 f2fs_kset
= kset_create_and_add("f2fs", NULL
, fs_kobj
);
317 void f2fs_unregister_sysfs(void)
319 kset_unregister(f2fs_kset
);
320 remove_proc_entry("fs/f2fs", NULL
);
323 int f2fs_init_sysfs(struct f2fs_sb_info
*sbi
)
325 struct super_block
*sb
= sbi
->sb
;
329 sbi
->s_proc
= proc_mkdir(sb
->s_id
, f2fs_proc_root
);
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
);
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
,
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
);
354 void f2fs_exit_sysfs(struct f2fs_sb_info
*sbi
)
356 kobject_del(&sbi
->s_kobj
);
357 kobject_put(&sbi
->s_kobj
);
358 wait_for_completion(&sbi
->s_kobj_unregister
);
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
);