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>
19 static struct proc_dir_entry
*f2fs_proc_root
;
20 static struct kset
*f2fs_kset
;
22 /* Sysfs support for f2fs */
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 */
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);
45 static unsigned char *__struct_ptr(struct f2fs_sb_info
*sbi
, int struct_type
)
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
;
65 static ssize_t
lifetime_write_kbytes_show(struct f2fs_attr
*a
,
66 struct f2fs_sb_info
*sbi
, char *buf
)
68 struct super_block
*sb
= sbi
->sb
;
70 if (!sb
->s_bdev
->bd_part
)
71 return snprintf(buf
, PAGE_SIZE
, "0\n");
73 return snprintf(buf
, PAGE_SIZE
, "%llu\n",
74 (unsigned long long)(sbi
->kbytes_written
+
75 BD_PART_WRITTEN(sbi
)));
78 static ssize_t
f2fs_sbi_show(struct f2fs_attr
*a
,
79 struct f2fs_sb_info
*sbi
, char *buf
)
81 unsigned char *ptr
= NULL
;
84 ptr
= __struct_ptr(sbi
, a
->struct_type
);
88 ui
= (unsigned int *)(ptr
+ a
->offset
);
90 return snprintf(buf
, PAGE_SIZE
, "%u\n", *ui
);
93 static ssize_t
f2fs_sbi_store(struct f2fs_attr
*a
,
94 struct f2fs_sb_info
*sbi
,
95 const char *buf
, size_t count
)
102 ptr
= __struct_ptr(sbi
, a
->struct_type
);
106 ui
= (unsigned int *)(ptr
+ a
->offset
);
108 ret
= kstrtoul(skip_spaces(buf
), 0, &t
);
111 #ifdef CONFIG_F2FS_FAULT_INJECTION
112 if (a
->struct_type
== FAULT_INFO_TYPE
&& t
>= (1 << FAULT_MAX
))
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
);
123 spin_unlock(&sbi
->stat_lock
);
130 static ssize_t
f2fs_attr_show(struct kobject
*kobj
,
131 struct attribute
*attr
, char *buf
)
133 struct f2fs_sb_info
*sbi
= container_of(kobj
, struct f2fs_sb_info
,
135 struct f2fs_attr
*a
= container_of(attr
, struct f2fs_attr
, attr
);
137 return a
->show
? a
->show(a
, sbi
, buf
) : 0;
140 static ssize_t
f2fs_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
141 const char *buf
, size_t len
)
143 struct f2fs_sb_info
*sbi
= container_of(kobj
, struct f2fs_sb_info
,
145 struct f2fs_attr
*a
= container_of(attr
, struct f2fs_attr
, attr
);
147 return a
->store
? a
->store(a
, sbi
, buf
, len
) : 0;
150 static void f2fs_sb_release(struct kobject
*kobj
)
152 struct f2fs_sb_info
*sbi
= container_of(kobj
, struct f2fs_sb_info
,
154 complete(&sbi
->s_kobj_unregister
);
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 }, \
162 .struct_type = _struct_type, \
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))
171 #define F2FS_GENERAL_RO_ATTR(name) \
172 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
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
);
197 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes
);
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
),
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
),
223 ATTR_LIST(lifetime_write_kbytes
),
224 ATTR_LIST(reserved_blocks
),
228 static const struct sysfs_ops f2fs_attr_ops
= {
229 .show
= f2fs_attr_show
,
230 .store
= f2fs_attr_store
,
233 static struct kobj_type f2fs_ktype
= {
234 .default_attrs
= f2fs_attrs
,
235 .sysfs_ops
= &f2fs_attr_ops
,
236 .release
= f2fs_sb_release
,
239 static int segment_info_seq_show(struct seq_file
*seq
, void *offset
)
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
);
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");
250 for (i
= 0; i
< total_segs
; i
++) {
251 struct seg_entry
*se
= get_seg_entry(sbi
, i
);
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))
266 static int segment_bits_seq_show(struct seq_file
*seq
, void *offset
)
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
);
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");
277 for (i
= 0; i
< total_segs
; i
++) {
278 struct seg_entry
*se
= get_seg_entry(sbi
, i
);
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
]);
290 #define F2FS_PROC_FILE_DEF(_name) \
291 static int _name##_open_fs(struct inode *inode, struct file *file) \
293 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
296 static const struct file_operations f2fs_seq_##_name##_fops = { \
297 .open = _name##_open_fs, \
299 .llseek = seq_lseek, \
300 .release = single_release, \
303 F2FS_PROC_FILE_DEF(segment_info
);
304 F2FS_PROC_FILE_DEF(segment_bits
);
306 int __init
f2fs_register_sysfs(void)
308 f2fs_proc_root
= proc_mkdir("fs/f2fs", NULL
);
310 f2fs_kset
= kset_create_and_add("f2fs", NULL
, fs_kobj
);
316 void f2fs_unregister_sysfs(void)
318 kset_unregister(f2fs_kset
);
319 remove_proc_entry("fs/f2fs", NULL
);
322 int f2fs_init_sysfs(struct f2fs_sb_info
*sbi
)
324 struct super_block
*sb
= sbi
->sb
;
328 sbi
->s_proc
= proc_mkdir(sb
->s_id
, f2fs_proc_root
);
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
);
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
,
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
);
353 void f2fs_exit_sysfs(struct f2fs_sb_info
*sbi
)
355 kobject_del(&sbi
->s_kobj
);
356 kobject_put(&sbi
->s_kobj
);
357 wait_for_completion(&sbi
->s_kobj_unregister
);
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
);