blob: 240e4881279e4f5a0773391dbf53fd95bf109a8c [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Chao Yu8ceffcb2017-06-14 17:39:47 +08002/*
3 * f2fs sysfs interface
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 * Copyright (c) 2017 Chao Yu <chao@kernel.org>
Chao Yu8ceffcb2017-06-14 17:39:47 +08008 */
Randy Dunlapcb15d1e2018-07-06 20:50:57 -07009#include <linux/compiler.h>
Chao Yu8ceffcb2017-06-14 17:39:47 +080010#include <linux/proc_fs.h>
11#include <linux/f2fs_fs.h>
Jaegeuk Kim299aa412017-07-13 17:45:21 -070012#include <linux/seq_file.h>
Chao Yu8ceffcb2017-06-14 17:39:47 +080013
14#include "f2fs.h"
15#include "segment.h"
16#include "gc.h"
17
18static struct proc_dir_entry *f2fs_proc_root;
Chao Yu8ceffcb2017-06-14 17:39:47 +080019
20/* Sysfs support for f2fs */
21enum {
22 GC_THREAD, /* struct f2fs_gc_thread */
23 SM_INFO, /* struct f2fs_sm_info */
24 DCC_INFO, /* struct discard_cmd_control */
25 NM_INFO, /* struct f2fs_nm_info */
26 F2FS_SBI, /* struct f2fs_sb_info */
27#ifdef CONFIG_F2FS_FAULT_INJECTION
28 FAULT_INFO_RATE, /* struct f2fs_fault_info */
29 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
30#endif
Yunlong Song80d42142017-10-27 20:45:05 +080031 RESERVED_BLOCKS, /* struct f2fs_sb_info */
Chao Yu8ceffcb2017-06-14 17:39:47 +080032};
33
34struct f2fs_attr {
35 struct attribute attr;
36 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
37 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
38 const char *, size_t);
39 int struct_type;
40 int offset;
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -070041 int id;
Chao Yu8ceffcb2017-06-14 17:39:47 +080042};
43
44static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
45{
46 if (struct_type == GC_THREAD)
47 return (unsigned char *)sbi->gc_thread;
48 else if (struct_type == SM_INFO)
49 return (unsigned char *)SM_I(sbi);
50 else if (struct_type == DCC_INFO)
51 return (unsigned char *)SM_I(sbi)->dcc_info;
52 else if (struct_type == NM_INFO)
53 return (unsigned char *)NM_I(sbi);
Chao Yudaeb4332017-06-26 16:24:41 +080054 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
Chao Yu8ceffcb2017-06-14 17:39:47 +080055 return (unsigned char *)sbi;
56#ifdef CONFIG_F2FS_FAULT_INJECTION
57 else if (struct_type == FAULT_INFO_RATE ||
58 struct_type == FAULT_INFO_TYPE)
Chao Yu63189b72018-03-08 14:22:56 +080059 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
Chao Yu8ceffcb2017-06-14 17:39:47 +080060#endif
61 return NULL;
62}
63
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +020064static ssize_t dirty_segments_show(struct f2fs_attr *a,
65 struct f2fs_sb_info *sbi, char *buf)
66{
67 return snprintf(buf, PAGE_SIZE, "%llu\n",
68 (unsigned long long)(dirty_segments(sbi)));
69}
70
Chao Yu8ceffcb2017-06-14 17:39:47 +080071static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
72 struct f2fs_sb_info *sbi, char *buf)
73{
74 struct super_block *sb = sbi->sb;
75
76 if (!sb->s_bdev->bd_part)
77 return snprintf(buf, PAGE_SIZE, "0\n");
78
79 return snprintf(buf, PAGE_SIZE, "%llu\n",
80 (unsigned long long)(sbi->kbytes_written +
81 BD_PART_WRITTEN(sbi)));
82}
83
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -070084static ssize_t features_show(struct f2fs_attr *a,
85 struct f2fs_sb_info *sbi, char *buf)
86{
87 struct super_block *sb = sbi->sb;
88 int len = 0;
89
90 if (!sb->s_bdev->bd_part)
91 return snprintf(buf, PAGE_SIZE, "0\n");
92
Chao Yu7beb01f2018-10-24 18:34:26 +080093 if (f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -070094 len += snprintf(buf, PAGE_SIZE - len, "%s",
95 "encryption");
Chao Yu7beb01f2018-10-24 18:34:26 +080096 if (f2fs_sb_has_blkzoned(sbi))
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -070097 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
98 len ? ", " : "", "blkzoned");
Chao Yu7beb01f2018-10-24 18:34:26 +080099 if (f2fs_sb_has_extra_attr(sbi))
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700100 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
101 len ? ", " : "", "extra_attr");
Chao Yu7beb01f2018-10-24 18:34:26 +0800102 if (f2fs_sb_has_project_quota(sbi))
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700103 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
104 len ? ", " : "", "projquota");
Chao Yu7beb01f2018-10-24 18:34:26 +0800105 if (f2fs_sb_has_inode_chksum(sbi))
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700106 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
107 len ? ", " : "", "inode_checksum");
Chao Yu7beb01f2018-10-24 18:34:26 +0800108 if (f2fs_sb_has_flexible_inline_xattr(sbi))
Chao Yu6afc6622017-09-06 21:59:50 +0800109 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
110 len ? ", " : "", "flexible_inline_xattr");
Chao Yu7beb01f2018-10-24 18:34:26 +0800111 if (f2fs_sb_has_quota_ino(sbi))
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700112 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
113 len ? ", " : "", "quota_ino");
Chao Yu7beb01f2018-10-24 18:34:26 +0800114 if (f2fs_sb_has_inode_crtime(sbi))
Chao Yu1c1d35d2018-01-25 14:54:42 +0800115 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
116 len ? ", " : "", "inode_crtime");
Chao Yu7beb01f2018-10-24 18:34:26 +0800117 if (f2fs_sb_has_lost_found(sbi))
Sheng Yongb7c409d2018-03-15 18:51:41 +0800118 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
119 len ? ", " : "", "lost_found");
Chao Yu7beb01f2018-10-24 18:34:26 +0800120 if (f2fs_sb_has_sb_chksum(sbi))
Junling Zhengd440c522018-09-28 20:25:56 +0800121 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
122 len ? ", " : "", "sb_checksum");
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700123 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
124 return len;
125}
126
Yunlong Song80d42142017-10-27 20:45:05 +0800127static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
128 struct f2fs_sb_info *sbi, char *buf)
129{
130 return snprintf(buf, PAGE_SIZE, "%u\n", sbi->current_reserved_blocks);
131}
132
Chao Yu8ceffcb2017-06-14 17:39:47 +0800133static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
134 struct f2fs_sb_info *sbi, char *buf)
135{
136 unsigned char *ptr = NULL;
137 unsigned int *ui;
138
139 ptr = __struct_ptr(sbi, a->struct_type);
140 if (!ptr)
141 return -EINVAL;
142
Chao Yu846ae672018-02-26 22:04:13 +0800143 if (!strcmp(a->attr.name, "extension_list")) {
144 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
145 sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800146 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
147 int hot_count = sbi->raw_super->hot_ext_count;
Chao Yu846ae672018-02-26 22:04:13 +0800148 int len = 0, i;
149
Chao Yub6a06cb2018-02-28 17:07:27 +0800150 len += snprintf(buf + len, PAGE_SIZE - len,
Colin Ian King45800382018-04-30 16:27:44 +0100151 "cold file extension:\n");
Chao Yub6a06cb2018-02-28 17:07:27 +0800152 for (i = 0; i < cold_count; i++)
153 len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
154 extlist[i]);
155
156 len += snprintf(buf + len, PAGE_SIZE - len,
Colin Ian King45800382018-04-30 16:27:44 +0100157 "hot file extension:\n");
Chao Yub6a06cb2018-02-28 17:07:27 +0800158 for (i = cold_count; i < cold_count + hot_count; i++)
Chao Yu846ae672018-02-26 22:04:13 +0800159 len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
160 extlist[i]);
161 return len;
162 }
163
Chao Yu8ceffcb2017-06-14 17:39:47 +0800164 ui = (unsigned int *)(ptr + a->offset);
165
166 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
167}
168
Chao Yu4d57b862018-05-30 00:20:41 +0800169static ssize_t __sbi_store(struct f2fs_attr *a,
Chao Yu8ceffcb2017-06-14 17:39:47 +0800170 struct f2fs_sb_info *sbi,
171 const char *buf, size_t count)
172{
173 unsigned char *ptr;
174 unsigned long t;
175 unsigned int *ui;
176 ssize_t ret;
177
178 ptr = __struct_ptr(sbi, a->struct_type);
179 if (!ptr)
180 return -EINVAL;
181
Chao Yu846ae672018-02-26 22:04:13 +0800182 if (!strcmp(a->attr.name, "extension_list")) {
183 const char *name = strim((char *)buf);
Chao Yub6a06cb2018-02-28 17:07:27 +0800184 bool set = true, hot;
Chao Yu846ae672018-02-26 22:04:13 +0800185
Chao Yub6a06cb2018-02-28 17:07:27 +0800186 if (!strncmp(name, "[h]", 3))
187 hot = true;
188 else if (!strncmp(name, "[c]", 3))
189 hot = false;
190 else
191 return -EINVAL;
192
193 name += 3;
194
195 if (*name == '!') {
Chao Yu846ae672018-02-26 22:04:13 +0800196 name++;
197 set = false;
198 }
199
200 if (strlen(name) >= F2FS_EXTENSION_LEN)
201 return -EINVAL;
202
203 down_write(&sbi->sb_lock);
204
Chao Yu4d57b862018-05-30 00:20:41 +0800205 ret = f2fs_update_extension_list(sbi, name, hot, set);
Chao Yu846ae672018-02-26 22:04:13 +0800206 if (ret)
207 goto out;
208
209 ret = f2fs_commit_super(sbi, false);
210 if (ret)
Chao Yu4d57b862018-05-30 00:20:41 +0800211 f2fs_update_extension_list(sbi, name, hot, !set);
Chao Yu846ae672018-02-26 22:04:13 +0800212out:
213 up_write(&sbi->sb_lock);
214 return ret ? ret : count;
215 }
216
Chao Yu8ceffcb2017-06-14 17:39:47 +0800217 ui = (unsigned int *)(ptr + a->offset);
218
219 ret = kstrtoul(skip_spaces(buf), 0, &t);
220 if (ret < 0)
221 return ret;
222#ifdef CONFIG_F2FS_FAULT_INJECTION
223 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
224 return -EINVAL;
225#endif
Chao Yudaeb4332017-06-26 16:24:41 +0800226 if (a->struct_type == RESERVED_BLOCKS) {
227 spin_lock(&sbi->stat_lock);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800228 if (t > (unsigned long)(sbi->user_block_count -
Chao Yu63189b72018-03-08 14:22:56 +0800229 F2FS_OPTION(sbi).root_reserved_blocks)) {
Chao Yudaeb4332017-06-26 16:24:41 +0800230 spin_unlock(&sbi->stat_lock);
231 return -EINVAL;
232 }
233 *ui = t;
Yunlong Song80d42142017-10-27 20:45:05 +0800234 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
235 sbi->user_block_count - valid_user_blocks(sbi));
Chao Yudaeb4332017-06-26 16:24:41 +0800236 spin_unlock(&sbi->stat_lock);
237 return count;
238 }
Chao Yu969d1b12017-08-07 23:09:56 +0800239
240 if (!strcmp(a->attr.name, "discard_granularity")) {
Chao Yu969d1b12017-08-07 23:09:56 +0800241 if (t == 0 || t > MAX_PLIST_NUM)
242 return -EINVAL;
243 if (t == *ui)
244 return count;
Chao Yu80647e52017-09-12 14:25:35 +0800245 *ui = t;
Chao Yu969d1b12017-08-07 23:09:56 +0800246 return count;
247 }
248
Chao Yu377224c2018-04-09 10:25:23 +0800249 if (!strcmp(a->attr.name, "trim_sections"))
250 return -EINVAL;
251
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700252 if (!strcmp(a->attr.name, "gc_urgent")) {
253 if (t >= 1) {
254 sbi->gc_mode = GC_URGENT;
255 if (sbi->gc_thread) {
Sheng Yonga690eff2018-08-05 12:45:35 +0800256 sbi->gc_thread->gc_wake = 1;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700257 wake_up_interruptible_all(
258 &sbi->gc_thread->gc_wait_queue_head);
259 wake_up_discard_thread(sbi, true);
260 }
261 } else {
262 sbi->gc_mode = GC_NORMAL;
263 }
264 return count;
265 }
266 if (!strcmp(a->attr.name, "gc_idle")) {
267 if (t == GC_IDLE_CB)
268 sbi->gc_mode = GC_IDLE_CB;
269 else if (t == GC_IDLE_GREEDY)
270 sbi->gc_mode = GC_IDLE_GREEDY;
271 else
272 sbi->gc_mode = GC_NORMAL;
273 return count;
274 }
275
Chao Yu8ceffcb2017-06-14 17:39:47 +0800276 *ui = t;
Chao Yub0af6d42017-08-02 23:21:48 +0800277
278 if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
279 f2fs_reset_iostat(sbi);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800280 return count;
281}
282
Chao Yu250dbf52018-05-28 16:57:32 +0800283static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
284 struct f2fs_sb_info *sbi,
285 const char *buf, size_t count)
286{
287 ssize_t ret;
288 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
289 a->struct_type == GC_THREAD);
290
Jaegeuk Kima1933c02018-07-15 09:58:08 +0900291 if (gc_entry) {
292 if (!down_read_trylock(&sbi->sb->s_umount))
293 return -EAGAIN;
294 }
Chao Yu4d57b862018-05-30 00:20:41 +0800295 ret = __sbi_store(a, sbi, buf, count);
Chao Yu250dbf52018-05-28 16:57:32 +0800296 if (gc_entry)
297 up_read(&sbi->sb->s_umount);
298
299 return ret;
300}
301
Chao Yu8ceffcb2017-06-14 17:39:47 +0800302static ssize_t f2fs_attr_show(struct kobject *kobj,
303 struct attribute *attr, char *buf)
304{
305 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
306 s_kobj);
307 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
308
309 return a->show ? a->show(a, sbi, buf) : 0;
310}
311
312static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
313 const char *buf, size_t len)
314{
315 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
316 s_kobj);
317 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
318
319 return a->store ? a->store(a, sbi, buf, len) : 0;
320}
321
322static void f2fs_sb_release(struct kobject *kobj)
323{
324 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
325 s_kobj);
326 complete(&sbi->s_kobj_unregister);
327}
328
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700329enum feat_id {
330 FEAT_CRYPTO = 0,
331 FEAT_BLKZONED,
332 FEAT_ATOMIC_WRITE,
333 FEAT_EXTRA_ATTR,
334 FEAT_PROJECT_QUOTA,
335 FEAT_INODE_CHECKSUM,
Chao Yu6afc6622017-09-06 21:59:50 +0800336 FEAT_FLEXIBLE_INLINE_XATTR,
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700337 FEAT_QUOTA_INO,
Chao Yu1c1d35d2018-01-25 14:54:42 +0800338 FEAT_INODE_CRTIME,
Sheng Yongb7c409d2018-03-15 18:51:41 +0800339 FEAT_LOST_FOUND,
Junling Zhengd440c522018-09-28 20:25:56 +0800340 FEAT_SB_CHECKSUM,
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700341};
342
343static ssize_t f2fs_feature_show(struct f2fs_attr *a,
344 struct f2fs_sb_info *sbi, char *buf)
345{
346 switch (a->id) {
347 case FEAT_CRYPTO:
348 case FEAT_BLKZONED:
349 case FEAT_ATOMIC_WRITE:
350 case FEAT_EXTRA_ATTR:
351 case FEAT_PROJECT_QUOTA:
352 case FEAT_INODE_CHECKSUM:
Chao Yu6afc6622017-09-06 21:59:50 +0800353 case FEAT_FLEXIBLE_INLINE_XATTR:
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700354 case FEAT_QUOTA_INO:
Chao Yu1c1d35d2018-01-25 14:54:42 +0800355 case FEAT_INODE_CRTIME:
Sheng Yongb7c409d2018-03-15 18:51:41 +0800356 case FEAT_LOST_FOUND:
Junling Zhengd440c522018-09-28 20:25:56 +0800357 case FEAT_SB_CHECKSUM:
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700358 return snprintf(buf, PAGE_SIZE, "supported\n");
359 }
360 return 0;
361}
362
Chao Yu8ceffcb2017-06-14 17:39:47 +0800363#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
364static struct f2fs_attr f2fs_attr_##_name = { \
365 .attr = {.name = __stringify(_name), .mode = _mode }, \
366 .show = _show, \
367 .store = _store, \
368 .struct_type = _struct_type, \
369 .offset = _offset \
370}
371
372#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
373 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
374 f2fs_sbi_show, f2fs_sbi_store, \
375 offsetof(struct struct_name, elname))
376
377#define F2FS_GENERAL_RO_ATTR(name) \
378static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
379
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700380#define F2FS_FEATURE_RO_ATTR(_name, _id) \
381static struct f2fs_attr f2fs_attr_##_name = { \
382 .attr = {.name = __stringify(_name), .mode = 0444 }, \
383 .show = f2fs_feature_show, \
384 .id = _id, \
385}
386
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700387F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
388 urgent_sleep_time);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800389F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
390F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
391F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700392F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
393F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800394F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
395F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
Chao Yu969d1b12017-08-07 23:09:56 +0800396F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
Chao Yudaeb4332017-06-26 16:24:41 +0800397F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800398F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
399F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
400F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
401F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
Jaegeuk Kim853137c2018-08-09 17:53:34 -0700402F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800403F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
Chao Yua2a12b62017-10-28 16:52:33 +0800404F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800405F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
406F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
407F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
408F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
409F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
410F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
411F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +0530412F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
413 interval_time[DISCARD_TIME]);
414F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
Chao Yub0af6d42017-08-02 23:21:48 +0800415F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
Sheng Yongf6df8f22017-11-22 18:23:38 +0800416F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -0800417F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
Chao Yu846ae672018-02-26 22:04:13 +0800418F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800419#ifdef CONFIG_F2FS_FAULT_INJECTION
420F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
421F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
422#endif
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +0200423F2FS_GENERAL_RO_ATTR(dirty_segments);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800424F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700425F2FS_GENERAL_RO_ATTR(features);
Yunlong Song80d42142017-10-27 20:45:05 +0800426F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700427
428#ifdef CONFIG_F2FS_FS_ENCRYPTION
429F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
430#endif
431#ifdef CONFIG_BLK_DEV_ZONED
432F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
433#endif
434F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
435F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
436F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
437F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
Chao Yu6afc6622017-09-06 21:59:50 +0800438F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700439F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800440F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
Sheng Yongb7c409d2018-03-15 18:51:41 +0800441F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
Junling Zhengd440c522018-09-28 20:25:56 +0800442F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800443
444#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
445static struct attribute *f2fs_attrs[] = {
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700446 ATTR_LIST(gc_urgent_sleep_time),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800447 ATTR_LIST(gc_min_sleep_time),
448 ATTR_LIST(gc_max_sleep_time),
449 ATTR_LIST(gc_no_gc_sleep_time),
450 ATTR_LIST(gc_idle),
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700451 ATTR_LIST(gc_urgent),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800452 ATTR_LIST(reclaim_segments),
453 ATTR_LIST(max_small_discards),
Chao Yu969d1b12017-08-07 23:09:56 +0800454 ATTR_LIST(discard_granularity),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800455 ATTR_LIST(batched_trim_sections),
456 ATTR_LIST(ipu_policy),
457 ATTR_LIST(min_ipu_util),
458 ATTR_LIST(min_fsync_blocks),
Jaegeuk Kim853137c2018-08-09 17:53:34 -0700459 ATTR_LIST(min_seq_blocks),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800460 ATTR_LIST(min_hot_blocks),
Chao Yua2a12b62017-10-28 16:52:33 +0800461 ATTR_LIST(min_ssr_sections),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800462 ATTR_LIST(max_victim_search),
463 ATTR_LIST(dir_level),
464 ATTR_LIST(ram_thresh),
465 ATTR_LIST(ra_nid_pages),
466 ATTR_LIST(dirty_nats_ratio),
467 ATTR_LIST(cp_interval),
468 ATTR_LIST(idle_interval),
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +0530469 ATTR_LIST(discard_idle_interval),
470 ATTR_LIST(gc_idle_interval),
Chao Yub0af6d42017-08-02 23:21:48 +0800471 ATTR_LIST(iostat_enable),
Sheng Yongf6df8f22017-11-22 18:23:38 +0800472 ATTR_LIST(readdir_ra),
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -0800473 ATTR_LIST(gc_pin_file_thresh),
Chao Yu846ae672018-02-26 22:04:13 +0800474 ATTR_LIST(extension_list),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800475#ifdef CONFIG_F2FS_FAULT_INJECTION
476 ATTR_LIST(inject_rate),
477 ATTR_LIST(inject_type),
478#endif
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +0200479 ATTR_LIST(dirty_segments),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800480 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700481 ATTR_LIST(features),
Chao Yudaeb4332017-06-26 16:24:41 +0800482 ATTR_LIST(reserved_blocks),
Yunlong Song80d42142017-10-27 20:45:05 +0800483 ATTR_LIST(current_reserved_blocks),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800484 NULL,
485};
486
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700487static struct attribute *f2fs_feat_attrs[] = {
488#ifdef CONFIG_F2FS_FS_ENCRYPTION
489 ATTR_LIST(encryption),
490#endif
491#ifdef CONFIG_BLK_DEV_ZONED
492 ATTR_LIST(block_zoned),
493#endif
494 ATTR_LIST(atomic_write),
495 ATTR_LIST(extra_attr),
496 ATTR_LIST(project_quota),
497 ATTR_LIST(inode_checksum),
Chao Yu6afc6622017-09-06 21:59:50 +0800498 ATTR_LIST(flexible_inline_xattr),
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700499 ATTR_LIST(quota_ino),
Chao Yu1c1d35d2018-01-25 14:54:42 +0800500 ATTR_LIST(inode_crtime),
Sheng Yongb7c409d2018-03-15 18:51:41 +0800501 ATTR_LIST(lost_found),
Junling Zhengd440c522018-09-28 20:25:56 +0800502 ATTR_LIST(sb_checksum),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700503 NULL,
504};
505
Chao Yu8ceffcb2017-06-14 17:39:47 +0800506static const struct sysfs_ops f2fs_attr_ops = {
507 .show = f2fs_attr_show,
508 .store = f2fs_attr_store,
509};
510
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700511static struct kobj_type f2fs_sb_ktype = {
Chao Yu8ceffcb2017-06-14 17:39:47 +0800512 .default_attrs = f2fs_attrs,
513 .sysfs_ops = &f2fs_attr_ops,
514 .release = f2fs_sb_release,
515};
516
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700517static struct kobj_type f2fs_ktype = {
518 .sysfs_ops = &f2fs_attr_ops,
519};
520
521static struct kset f2fs_kset = {
522 .kobj = {.ktype = &f2fs_ktype},
523};
524
525static struct kobj_type f2fs_feat_ktype = {
526 .default_attrs = f2fs_feat_attrs,
527 .sysfs_ops = &f2fs_attr_ops,
528};
529
530static struct kobject f2fs_feat = {
531 .kset = &f2fs_kset,
532};
533
Randy Dunlapcb15d1e2018-07-06 20:50:57 -0700534static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
535 void *offset)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800536{
537 struct super_block *sb = seq->private;
538 struct f2fs_sb_info *sbi = F2FS_SB(sb);
539 unsigned int total_segs =
540 le32_to_cpu(sbi->raw_super->segment_count_main);
541 int i;
542
543 seq_puts(seq, "format: segment_type|valid_blocks\n"
544 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
545
546 for (i = 0; i < total_segs; i++) {
547 struct seg_entry *se = get_seg_entry(sbi, i);
548
549 if ((i % 10) == 0)
550 seq_printf(seq, "%-10d", i);
551 seq_printf(seq, "%d|%-3u", se->type,
552 get_valid_blocks(sbi, i, false));
553 if ((i % 10) == 9 || i == (total_segs - 1))
554 seq_putc(seq, '\n');
555 else
556 seq_putc(seq, ' ');
557 }
558
559 return 0;
560}
561
Randy Dunlapcb15d1e2018-07-06 20:50:57 -0700562static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
563 void *offset)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800564{
565 struct super_block *sb = seq->private;
566 struct f2fs_sb_info *sbi = F2FS_SB(sb);
567 unsigned int total_segs =
568 le32_to_cpu(sbi->raw_super->segment_count_main);
569 int i, j;
570
571 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
572 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
573
574 for (i = 0; i < total_segs; i++) {
575 struct seg_entry *se = get_seg_entry(sbi, i);
576
577 seq_printf(seq, "%-10d", i);
578 seq_printf(seq, "%d|%-3u|", se->type,
579 get_valid_blocks(sbi, i, false));
580 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
581 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
582 seq_putc(seq, '\n');
583 }
584 return 0;
585}
586
Randy Dunlapcb15d1e2018-07-06 20:50:57 -0700587static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
588 void *offset)
Chao Yub0af6d42017-08-02 23:21:48 +0800589{
590 struct super_block *sb = seq->private;
591 struct f2fs_sb_info *sbi = F2FS_SB(sb);
592 time64_t now = ktime_get_real_seconds();
593
594 if (!sbi->iostat_enable)
595 return 0;
596
597 seq_printf(seq, "time: %-16llu\n", now);
598
599 /* print app IOs */
600 seq_printf(seq, "app buffered: %-16llu\n",
601 sbi->write_iostat[APP_BUFFERED_IO]);
602 seq_printf(seq, "app direct: %-16llu\n",
603 sbi->write_iostat[APP_DIRECT_IO]);
604 seq_printf(seq, "app mapped: %-16llu\n",
605 sbi->write_iostat[APP_MAPPED_IO]);
606
607 /* print fs IOs */
608 seq_printf(seq, "fs data: %-16llu\n",
609 sbi->write_iostat[FS_DATA_IO]);
610 seq_printf(seq, "fs node: %-16llu\n",
611 sbi->write_iostat[FS_NODE_IO]);
612 seq_printf(seq, "fs meta: %-16llu\n",
613 sbi->write_iostat[FS_META_IO]);
614 seq_printf(seq, "fs gc data: %-16llu\n",
615 sbi->write_iostat[FS_GC_DATA_IO]);
616 seq_printf(seq, "fs gc node: %-16llu\n",
617 sbi->write_iostat[FS_GC_NODE_IO]);
618 seq_printf(seq, "fs cp data: %-16llu\n",
619 sbi->write_iostat[FS_CP_DATA_IO]);
620 seq_printf(seq, "fs cp node: %-16llu\n",
621 sbi->write_iostat[FS_CP_NODE_IO]);
622 seq_printf(seq, "fs cp meta: %-16llu\n",
623 sbi->write_iostat[FS_CP_META_IO]);
624 seq_printf(seq, "fs discard: %-16llu\n",
625 sbi->write_iostat[FS_DISCARD]);
626
627 return 0;
628}
629
Yunlong Song970e3482018-07-23 22:10:22 +0800630static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
631 void *offset)
632{
633 struct super_block *sb = seq->private;
634 struct f2fs_sb_info *sbi = F2FS_SB(sb);
635 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
636 int i;
637
638 seq_puts(seq, "format: victim_secmap bitmaps\n");
639
640 for (i = 0; i < MAIN_SECS(sbi); i++) {
641 if ((i % 10) == 0)
642 seq_printf(seq, "%-10d", i);
643 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
644 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
645 seq_putc(seq, '\n');
646 else
647 seq_putc(seq, ' ');
648 }
649 return 0;
650}
651
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700652int __init f2fs_init_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800653{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700654 int ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800655
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700656 kobject_set_name(&f2fs_kset.kobj, "f2fs");
657 f2fs_kset.kobj.parent = fs_kobj;
658 ret = kset_register(&f2fs_kset);
659 if (ret)
660 return ret;
661
662 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
663 NULL, "features");
664 if (ret)
665 kset_unregister(&f2fs_kset);
666 else
667 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
668 return ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800669}
670
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700671void f2fs_exit_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800672{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700673 kobject_put(&f2fs_feat);
674 kset_unregister(&f2fs_kset);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800675 remove_proc_entry("fs/f2fs", NULL);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700676 f2fs_proc_root = NULL;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800677}
678
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700679int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800680{
681 struct super_block *sb = sbi->sb;
682 int err;
683
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700684 sbi->s_kobj.kset = &f2fs_kset;
685 init_completion(&sbi->s_kobj_unregister);
686 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
687 "%s", sb->s_id);
688 if (err)
689 return err;
690
Chao Yu8ceffcb2017-06-14 17:39:47 +0800691 if (f2fs_proc_root)
692 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
693
694 if (sbi->s_proc) {
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200695 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
696 segment_info_seq_show, sb);
697 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
698 segment_bits_seq_show, sb);
699 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
700 iostat_info_seq_show, sb);
Yunlong Song970e3482018-07-23 22:10:22 +0800701 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
702 victim_bits_seq_show, sb);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800703 }
Chao Yu8ceffcb2017-06-14 17:39:47 +0800704 return 0;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800705}
706
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700707void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +0800708{
Chao Yu8ceffcb2017-06-14 17:39:47 +0800709 if (sbi->s_proc) {
Chao Yub0af6d42017-08-02 23:21:48 +0800710 remove_proc_entry("iostat_info", sbi->s_proc);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800711 remove_proc_entry("segment_info", sbi->s_proc);
712 remove_proc_entry("segment_bits", sbi->s_proc);
Yunlong Song970e3482018-07-23 22:10:22 +0800713 remove_proc_entry("victim_bits", sbi->s_proc);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800714 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
715 }
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700716 kobject_del(&sbi->s_kobj);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800717}