blob: d7b47662a0aa054ae552f9a0a69a9ae94804eb42 [file] [log] [blame]
Chao Yu6d1a8322018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Chao Yu57fb30c2017-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 Yu57fb30c2017-06-14 17:39:47 +08008 */
Randy Dunlapbc1ad772018-07-06 20:50:57 -07009#include <linux/compiler.h>
Chao Yu57fb30c2017-06-14 17:39:47 +080010#include <linux/proc_fs.h>
11#include <linux/f2fs_fs.h>
Jaegeuk Kimdf726842017-07-13 17:45:21 -070012#include <linux/seq_file.h>
Chao Yu57fb30c2017-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 Yu57fb30c2017-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 Song68ab6f82017-10-27 20:45:05 +080031 RESERVED_BLOCKS, /* struct f2fs_sb_info */
Chao Yu57fb30c2017-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 Kim3b3d1b82017-07-21 17:14:09 -070041 int id;
Chao Yu57fb30c2017-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 Yu026bd9d2017-06-26 16:24:41 +080054 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
Chao Yu57fb30c2017-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 Yue9a50e62018-03-08 14:22:56 +080059 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
Chao Yu57fb30c2017-06-14 17:39:47 +080060#endif
61 return NULL;
62}
63
Jaegeuk Kimaf1cc1e2017-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 Yu57fb30c2017-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 Kim3b3d1b82017-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 Yu5b11d2b2018-10-24 18:34:26 +080093 if (f2fs_sb_has_encrypt(sbi))
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -070094 len += snprintf(buf, PAGE_SIZE - len, "%s",
95 "encryption");
Chao Yu5b11d2b2018-10-24 18:34:26 +080096 if (f2fs_sb_has_blkzoned(sbi))
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -070097 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
98 len ? ", " : "", "blkzoned");
Chao Yu5b11d2b2018-10-24 18:34:26 +080099 if (f2fs_sb_has_extra_attr(sbi))
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700100 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
101 len ? ", " : "", "extra_attr");
Chao Yu5b11d2b2018-10-24 18:34:26 +0800102 if (f2fs_sb_has_project_quota(sbi))
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700103 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
104 len ? ", " : "", "projquota");
Chao Yu5b11d2b2018-10-24 18:34:26 +0800105 if (f2fs_sb_has_inode_chksum(sbi))
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700106 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
107 len ? ", " : "", "inode_checksum");
Chao Yu5b11d2b2018-10-24 18:34:26 +0800108 if (f2fs_sb_has_flexible_inline_xattr(sbi))
Chao Yu50ffaa92017-09-06 21:59:50 +0800109 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
110 len ? ", " : "", "flexible_inline_xattr");
Chao Yu5b11d2b2018-10-24 18:34:26 +0800111 if (f2fs_sb_has_quota_ino(sbi))
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700112 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
113 len ? ", " : "", "quota_ino");
Chao Yu5b11d2b2018-10-24 18:34:26 +0800114 if (f2fs_sb_has_inode_crtime(sbi))
Chao Yu71f8f042018-01-25 14:54:42 +0800115 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
116 len ? ", " : "", "inode_crtime");
Chao Yu5b11d2b2018-10-24 18:34:26 +0800117 if (f2fs_sb_has_lost_found(sbi))
Sheng Yong9b880fe2018-03-15 18:51:41 +0800118 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
119 len ? ", " : "", "lost_found");
Chao Yu5b11d2b2018-10-24 18:34:26 +0800120 if (f2fs_sb_has_sb_chksum(sbi))
Junling Zheng0570b3f2018-09-28 20:25:56 +0800121 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
122 len ? ", " : "", "sb_checksum");
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700123 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
124 return len;
125}
126
Yunlong Song68ab6f82017-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 Yu57fb30c2017-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 Yu1e72cb22018-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 Yuac734c42018-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 Yu1e72cb22018-02-26 22:04:13 +0800148 int len = 0, i;
149
Chao Yuac734c42018-02-28 17:07:27 +0800150 len += snprintf(buf + len, PAGE_SIZE - len,
Colin Ian King0aca14f2018-04-30 16:27:44 +0100151 "cold file extension:\n");
Chao Yuac734c42018-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 King0aca14f2018-04-30 16:27:44 +0100157 "hot file extension:\n");
Chao Yuac734c42018-02-28 17:07:27 +0800158 for (i = cold_count; i < cold_count + hot_count; i++)
Chao Yu1e72cb22018-02-26 22:04:13 +0800159 len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
160 extlist[i]);
161 return len;
162 }
163
Chao Yu57fb30c2017-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 Yu6b4d6a82018-05-30 00:20:41 +0800169static ssize_t __sbi_store(struct f2fs_attr *a,
Chao Yu57fb30c2017-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 Yu1e72cb22018-02-26 22:04:13 +0800182 if (!strcmp(a->attr.name, "extension_list")) {
183 const char *name = strim((char *)buf);
Chao Yuac734c42018-02-28 17:07:27 +0800184 bool set = true, hot;
Chao Yu1e72cb22018-02-26 22:04:13 +0800185
Chao Yuac734c42018-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 Yu1e72cb22018-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 Yu6b4d6a82018-05-30 00:20:41 +0800205 ret = f2fs_update_extension_list(sbi, name, hot, set);
Chao Yu1e72cb22018-02-26 22:04:13 +0800206 if (ret)
207 goto out;
208
209 ret = f2fs_commit_super(sbi, false);
210 if (ret)
Chao Yu6b4d6a82018-05-30 00:20:41 +0800211 f2fs_update_extension_list(sbi, name, hot, !set);
Chao Yu1e72cb22018-02-26 22:04:13 +0800212out:
213 up_write(&sbi->sb_lock);
214 return ret ? ret : count;
215 }
216
Chao Yu57fb30c2017-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;
Chao Yu00887062019-01-04 17:39:53 +0800225 if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
226 return -EINVAL;
Chao Yu57fb30c2017-06-14 17:39:47 +0800227#endif
Chao Yu026bd9d2017-06-26 16:24:41 +0800228 if (a->struct_type == RESERVED_BLOCKS) {
229 spin_lock(&sbi->stat_lock);
Jaegeuk Kim5fee5402017-12-27 15:05:52 -0800230 if (t > (unsigned long)(sbi->user_block_count -
Chao Yue9a50e62018-03-08 14:22:56 +0800231 F2FS_OPTION(sbi).root_reserved_blocks)) {
Chao Yu026bd9d2017-06-26 16:24:41 +0800232 spin_unlock(&sbi->stat_lock);
233 return -EINVAL;
234 }
235 *ui = t;
Yunlong Song68ab6f82017-10-27 20:45:05 +0800236 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
237 sbi->user_block_count - valid_user_blocks(sbi));
Chao Yu026bd9d2017-06-26 16:24:41 +0800238 spin_unlock(&sbi->stat_lock);
239 return count;
240 }
Chao Yuefe24da2017-08-07 23:09:56 +0800241
242 if (!strcmp(a->attr.name, "discard_granularity")) {
Chao Yuefe24da2017-08-07 23:09:56 +0800243 if (t == 0 || t > MAX_PLIST_NUM)
244 return -EINVAL;
245 if (t == *ui)
246 return count;
Chao Yud1c363b2017-09-12 14:25:35 +0800247 *ui = t;
Chao Yuefe24da2017-08-07 23:09:56 +0800248 return count;
249 }
250
Chao Yubc83a660b2018-10-25 16:19:28 +0800251 if (!strcmp(a->attr.name, "migration_granularity")) {
252 if (t == 0 || t > sbi->segs_per_sec)
253 return -EINVAL;
254 }
255
Chao Yu0357a0f2018-04-09 10:25:23 +0800256 if (!strcmp(a->attr.name, "trim_sections"))
257 return -EINVAL;
258
Jaegeuk Kim0390d832018-05-07 14:22:40 -0700259 if (!strcmp(a->attr.name, "gc_urgent")) {
260 if (t >= 1) {
261 sbi->gc_mode = GC_URGENT;
262 if (sbi->gc_thread) {
Sheng Yongbb763b52018-08-05 12:45:35 +0800263 sbi->gc_thread->gc_wake = 1;
Jaegeuk Kim0390d832018-05-07 14:22:40 -0700264 wake_up_interruptible_all(
265 &sbi->gc_thread->gc_wait_queue_head);
266 wake_up_discard_thread(sbi, true);
267 }
268 } else {
269 sbi->gc_mode = GC_NORMAL;
270 }
271 return count;
272 }
273 if (!strcmp(a->attr.name, "gc_idle")) {
274 if (t == GC_IDLE_CB)
275 sbi->gc_mode = GC_IDLE_CB;
276 else if (t == GC_IDLE_GREEDY)
277 sbi->gc_mode = GC_IDLE_GREEDY;
278 else
279 sbi->gc_mode = GC_NORMAL;
280 return count;
281 }
282
Chao Yuc0fe4882017-08-02 23:21:48 +0800283
Sheng Yong13adc6f2019-01-15 20:02:15 +0000284 if (!strcmp(a->attr.name, "iostat_enable")) {
285 sbi->iostat_enable = !!t;
286 if (!sbi->iostat_enable)
287 f2fs_reset_iostat(sbi);
288 return count;
289 }
290
291 *ui = (unsigned int)t;
292
Chao Yu57fb30c2017-06-14 17:39:47 +0800293 return count;
294}
295
Chao Yub9921f02018-05-28 16:57:32 +0800296static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
297 struct f2fs_sb_info *sbi,
298 const char *buf, size_t count)
299{
300 ssize_t ret;
301 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
302 a->struct_type == GC_THREAD);
303
Jaegeuk Kim1d1f79d2018-07-15 09:58:08 +0900304 if (gc_entry) {
305 if (!down_read_trylock(&sbi->sb->s_umount))
306 return -EAGAIN;
307 }
Chao Yu6b4d6a82018-05-30 00:20:41 +0800308 ret = __sbi_store(a, sbi, buf, count);
Chao Yub9921f02018-05-28 16:57:32 +0800309 if (gc_entry)
310 up_read(&sbi->sb->s_umount);
311
312 return ret;
313}
314
Chao Yu57fb30c2017-06-14 17:39:47 +0800315static ssize_t f2fs_attr_show(struct kobject *kobj,
316 struct attribute *attr, char *buf)
317{
318 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
319 s_kobj);
320 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
321
322 return a->show ? a->show(a, sbi, buf) : 0;
323}
324
325static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
326 const char *buf, size_t len)
327{
328 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
329 s_kobj);
330 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
331
332 return a->store ? a->store(a, sbi, buf, len) : 0;
333}
334
335static void f2fs_sb_release(struct kobject *kobj)
336{
337 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
338 s_kobj);
339 complete(&sbi->s_kobj_unregister);
340}
341
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700342enum feat_id {
343 FEAT_CRYPTO = 0,
344 FEAT_BLKZONED,
345 FEAT_ATOMIC_WRITE,
346 FEAT_EXTRA_ATTR,
347 FEAT_PROJECT_QUOTA,
348 FEAT_INODE_CHECKSUM,
Chao Yu50ffaa92017-09-06 21:59:50 +0800349 FEAT_FLEXIBLE_INLINE_XATTR,
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700350 FEAT_QUOTA_INO,
Chao Yu71f8f042018-01-25 14:54:42 +0800351 FEAT_INODE_CRTIME,
Sheng Yong9b880fe2018-03-15 18:51:41 +0800352 FEAT_LOST_FOUND,
Junling Zheng0570b3f2018-09-28 20:25:56 +0800353 FEAT_SB_CHECKSUM,
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700354};
355
356static ssize_t f2fs_feature_show(struct f2fs_attr *a,
357 struct f2fs_sb_info *sbi, char *buf)
358{
359 switch (a->id) {
360 case FEAT_CRYPTO:
361 case FEAT_BLKZONED:
362 case FEAT_ATOMIC_WRITE:
363 case FEAT_EXTRA_ATTR:
364 case FEAT_PROJECT_QUOTA:
365 case FEAT_INODE_CHECKSUM:
Chao Yu50ffaa92017-09-06 21:59:50 +0800366 case FEAT_FLEXIBLE_INLINE_XATTR:
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700367 case FEAT_QUOTA_INO:
Chao Yu71f8f042018-01-25 14:54:42 +0800368 case FEAT_INODE_CRTIME:
Sheng Yong9b880fe2018-03-15 18:51:41 +0800369 case FEAT_LOST_FOUND:
Junling Zheng0570b3f2018-09-28 20:25:56 +0800370 case FEAT_SB_CHECKSUM:
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700371 return snprintf(buf, PAGE_SIZE, "supported\n");
372 }
373 return 0;
374}
375
Chao Yu57fb30c2017-06-14 17:39:47 +0800376#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
377static struct f2fs_attr f2fs_attr_##_name = { \
378 .attr = {.name = __stringify(_name), .mode = _mode }, \
379 .show = _show, \
380 .store = _store, \
381 .struct_type = _struct_type, \
382 .offset = _offset \
383}
384
385#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
386 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
387 f2fs_sbi_show, f2fs_sbi_store, \
388 offsetof(struct struct_name, elname))
389
390#define F2FS_GENERAL_RO_ATTR(name) \
391static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
392
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700393#define F2FS_FEATURE_RO_ATTR(_name, _id) \
394static struct f2fs_attr f2fs_attr_##_name = { \
395 .attr = {.name = __stringify(_name), .mode = 0444 }, \
396 .show = f2fs_feature_show, \
397 .id = _id, \
398}
399
Jaegeuk Kimbd2ffc22017-08-06 22:09:00 -0700400F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
401 urgent_sleep_time);
Chao Yu57fb30c2017-06-14 17:39:47 +0800402F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
403F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
404F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
Jaegeuk Kim0390d832018-05-07 14:22:40 -0700405F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
406F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
Chao Yu57fb30c2017-06-14 17:39:47 +0800407F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
408F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
Chao Yuefe24da2017-08-07 23:09:56 +0800409F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
Chao Yu026bd9d2017-06-26 16:24:41 +0800410F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
Chao Yu57fb30c2017-06-14 17:39:47 +0800411F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
412F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
413F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
414F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
Jaegeuk Kim6d598452018-08-09 17:53:34 -0700415F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
Chao Yu57fb30c2017-06-14 17:39:47 +0800416F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
Chao Yub9f73872017-10-28 16:52:33 +0800417F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
Chao Yu57fb30c2017-06-14 17:39:47 +0800418F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
419F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
420F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
421F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
Chao Yubc83a660b2018-10-25 16:19:28 +0800422F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
Chao Yu57fb30c2017-06-14 17:39:47 +0800423F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
424F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
425F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
Sahitya Tummalafeb1b252018-09-19 14:18:47 +0530426F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
427 interval_time[DISCARD_TIME]);
428F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
Jaegeuk Kimb842f2d2019-01-14 10:42:11 -0800429F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
430 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
Chao Yuc0fe4882017-08-02 23:21:48 +0800431F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
Sheng Yong1ed75332017-11-22 18:23:38 +0800432F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -0800433F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
Chao Yu1e72cb22018-02-26 22:04:13 +0800434F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
Chao Yu57fb30c2017-06-14 17:39:47 +0800435#ifdef CONFIG_F2FS_FAULT_INJECTION
436F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
437F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
438#endif
Jaegeuk Kimaf1cc1e2017-10-24 09:46:54 +0200439F2FS_GENERAL_RO_ATTR(dirty_segments);
Chao Yu57fb30c2017-06-14 17:39:47 +0800440F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700441F2FS_GENERAL_RO_ATTR(features);
Yunlong Song68ab6f82017-10-27 20:45:05 +0800442F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700443
444#ifdef CONFIG_F2FS_FS_ENCRYPTION
445F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
446#endif
447#ifdef CONFIG_BLK_DEV_ZONED
448F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
449#endif
450F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
451F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
452F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
453F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
Chao Yu50ffaa92017-09-06 21:59:50 +0800454F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700455F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
Chao Yu71f8f042018-01-25 14:54:42 +0800456F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
Sheng Yong9b880fe2018-03-15 18:51:41 +0800457F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
Junling Zheng0570b3f2018-09-28 20:25:56 +0800458F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
Chao Yu57fb30c2017-06-14 17:39:47 +0800459
460#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
461static struct attribute *f2fs_attrs[] = {
Jaegeuk Kimbd2ffc22017-08-06 22:09:00 -0700462 ATTR_LIST(gc_urgent_sleep_time),
Chao Yu57fb30c2017-06-14 17:39:47 +0800463 ATTR_LIST(gc_min_sleep_time),
464 ATTR_LIST(gc_max_sleep_time),
465 ATTR_LIST(gc_no_gc_sleep_time),
466 ATTR_LIST(gc_idle),
Jaegeuk Kimbd2ffc22017-08-06 22:09:00 -0700467 ATTR_LIST(gc_urgent),
Chao Yu57fb30c2017-06-14 17:39:47 +0800468 ATTR_LIST(reclaim_segments),
469 ATTR_LIST(max_small_discards),
Chao Yuefe24da2017-08-07 23:09:56 +0800470 ATTR_LIST(discard_granularity),
Chao Yu57fb30c2017-06-14 17:39:47 +0800471 ATTR_LIST(batched_trim_sections),
472 ATTR_LIST(ipu_policy),
473 ATTR_LIST(min_ipu_util),
474 ATTR_LIST(min_fsync_blocks),
Jaegeuk Kim6d598452018-08-09 17:53:34 -0700475 ATTR_LIST(min_seq_blocks),
Chao Yu57fb30c2017-06-14 17:39:47 +0800476 ATTR_LIST(min_hot_blocks),
Chao Yub9f73872017-10-28 16:52:33 +0800477 ATTR_LIST(min_ssr_sections),
Chao Yu57fb30c2017-06-14 17:39:47 +0800478 ATTR_LIST(max_victim_search),
Chao Yubc83a660b2018-10-25 16:19:28 +0800479 ATTR_LIST(migration_granularity),
Chao Yu57fb30c2017-06-14 17:39:47 +0800480 ATTR_LIST(dir_level),
481 ATTR_LIST(ram_thresh),
482 ATTR_LIST(ra_nid_pages),
483 ATTR_LIST(dirty_nats_ratio),
484 ATTR_LIST(cp_interval),
485 ATTR_LIST(idle_interval),
Sahitya Tummalafeb1b252018-09-19 14:18:47 +0530486 ATTR_LIST(discard_idle_interval),
487 ATTR_LIST(gc_idle_interval),
Jaegeuk Kimb842f2d2019-01-14 10:42:11 -0800488 ATTR_LIST(umount_discard_timeout),
Chao Yuc0fe4882017-08-02 23:21:48 +0800489 ATTR_LIST(iostat_enable),
Sheng Yong1ed75332017-11-22 18:23:38 +0800490 ATTR_LIST(readdir_ra),
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -0800491 ATTR_LIST(gc_pin_file_thresh),
Chao Yu1e72cb22018-02-26 22:04:13 +0800492 ATTR_LIST(extension_list),
Chao Yu57fb30c2017-06-14 17:39:47 +0800493#ifdef CONFIG_F2FS_FAULT_INJECTION
494 ATTR_LIST(inject_rate),
495 ATTR_LIST(inject_type),
496#endif
Jaegeuk Kimaf1cc1e2017-10-24 09:46:54 +0200497 ATTR_LIST(dirty_segments),
Chao Yu57fb30c2017-06-14 17:39:47 +0800498 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700499 ATTR_LIST(features),
Chao Yu026bd9d2017-06-26 16:24:41 +0800500 ATTR_LIST(reserved_blocks),
Yunlong Song68ab6f82017-10-27 20:45:05 +0800501 ATTR_LIST(current_reserved_blocks),
Chao Yu57fb30c2017-06-14 17:39:47 +0800502 NULL,
503};
504
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700505static struct attribute *f2fs_feat_attrs[] = {
506#ifdef CONFIG_F2FS_FS_ENCRYPTION
507 ATTR_LIST(encryption),
508#endif
509#ifdef CONFIG_BLK_DEV_ZONED
510 ATTR_LIST(block_zoned),
511#endif
512 ATTR_LIST(atomic_write),
513 ATTR_LIST(extra_attr),
514 ATTR_LIST(project_quota),
515 ATTR_LIST(inode_checksum),
Chao Yu50ffaa92017-09-06 21:59:50 +0800516 ATTR_LIST(flexible_inline_xattr),
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700517 ATTR_LIST(quota_ino),
Chao Yu71f8f042018-01-25 14:54:42 +0800518 ATTR_LIST(inode_crtime),
Sheng Yong9b880fe2018-03-15 18:51:41 +0800519 ATTR_LIST(lost_found),
Junling Zheng0570b3f2018-09-28 20:25:56 +0800520 ATTR_LIST(sb_checksum),
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700521 NULL,
522};
523
Chao Yu57fb30c2017-06-14 17:39:47 +0800524static const struct sysfs_ops f2fs_attr_ops = {
525 .show = f2fs_attr_show,
526 .store = f2fs_attr_store,
527};
528
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700529static struct kobj_type f2fs_sb_ktype = {
Chao Yu57fb30c2017-06-14 17:39:47 +0800530 .default_attrs = f2fs_attrs,
531 .sysfs_ops = &f2fs_attr_ops,
532 .release = f2fs_sb_release,
533};
534
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700535static struct kobj_type f2fs_ktype = {
536 .sysfs_ops = &f2fs_attr_ops,
537};
538
539static struct kset f2fs_kset = {
540 .kobj = {.ktype = &f2fs_ktype},
541};
542
543static struct kobj_type f2fs_feat_ktype = {
544 .default_attrs = f2fs_feat_attrs,
545 .sysfs_ops = &f2fs_attr_ops,
546};
547
548static struct kobject f2fs_feat = {
549 .kset = &f2fs_kset,
550};
551
Randy Dunlapbc1ad772018-07-06 20:50:57 -0700552static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
553 void *offset)
Chao Yu57fb30c2017-06-14 17:39:47 +0800554{
555 struct super_block *sb = seq->private;
556 struct f2fs_sb_info *sbi = F2FS_SB(sb);
557 unsigned int total_segs =
558 le32_to_cpu(sbi->raw_super->segment_count_main);
559 int i;
560
561 seq_puts(seq, "format: segment_type|valid_blocks\n"
562 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
563
564 for (i = 0; i < total_segs; i++) {
565 struct seg_entry *se = get_seg_entry(sbi, i);
566
567 if ((i % 10) == 0)
568 seq_printf(seq, "%-10d", i);
569 seq_printf(seq, "%d|%-3u", se->type,
570 get_valid_blocks(sbi, i, false));
571 if ((i % 10) == 9 || i == (total_segs - 1))
572 seq_putc(seq, '\n');
573 else
574 seq_putc(seq, ' ');
575 }
576
577 return 0;
578}
579
Randy Dunlapbc1ad772018-07-06 20:50:57 -0700580static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
581 void *offset)
Chao Yu57fb30c2017-06-14 17:39:47 +0800582{
583 struct super_block *sb = seq->private;
584 struct f2fs_sb_info *sbi = F2FS_SB(sb);
585 unsigned int total_segs =
586 le32_to_cpu(sbi->raw_super->segment_count_main);
587 int i, j;
588
589 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
590 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
591
592 for (i = 0; i < total_segs; i++) {
593 struct seg_entry *se = get_seg_entry(sbi, i);
594
595 seq_printf(seq, "%-10d", i);
596 seq_printf(seq, "%d|%-3u|", se->type,
597 get_valid_blocks(sbi, i, false));
598 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
599 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
600 seq_putc(seq, '\n');
601 }
602 return 0;
603}
604
Randy Dunlapbc1ad772018-07-06 20:50:57 -0700605static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
606 void *offset)
Chao Yuc0fe4882017-08-02 23:21:48 +0800607{
608 struct super_block *sb = seq->private;
609 struct f2fs_sb_info *sbi = F2FS_SB(sb);
610 time64_t now = ktime_get_real_seconds();
611
612 if (!sbi->iostat_enable)
613 return 0;
614
615 seq_printf(seq, "time: %-16llu\n", now);
616
617 /* print app IOs */
618 seq_printf(seq, "app buffered: %-16llu\n",
619 sbi->write_iostat[APP_BUFFERED_IO]);
620 seq_printf(seq, "app direct: %-16llu\n",
621 sbi->write_iostat[APP_DIRECT_IO]);
622 seq_printf(seq, "app mapped: %-16llu\n",
623 sbi->write_iostat[APP_MAPPED_IO]);
624
625 /* print fs IOs */
626 seq_printf(seq, "fs data: %-16llu\n",
627 sbi->write_iostat[FS_DATA_IO]);
628 seq_printf(seq, "fs node: %-16llu\n",
629 sbi->write_iostat[FS_NODE_IO]);
630 seq_printf(seq, "fs meta: %-16llu\n",
631 sbi->write_iostat[FS_META_IO]);
632 seq_printf(seq, "fs gc data: %-16llu\n",
633 sbi->write_iostat[FS_GC_DATA_IO]);
634 seq_printf(seq, "fs gc node: %-16llu\n",
635 sbi->write_iostat[FS_GC_NODE_IO]);
636 seq_printf(seq, "fs cp data: %-16llu\n",
637 sbi->write_iostat[FS_CP_DATA_IO]);
638 seq_printf(seq, "fs cp node: %-16llu\n",
639 sbi->write_iostat[FS_CP_NODE_IO]);
640 seq_printf(seq, "fs cp meta: %-16llu\n",
641 sbi->write_iostat[FS_CP_META_IO]);
642 seq_printf(seq, "fs discard: %-16llu\n",
643 sbi->write_iostat[FS_DISCARD]);
644
645 return 0;
646}
647
Yunlong Song13734782018-07-23 22:10:22 +0800648static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
649 void *offset)
650{
651 struct super_block *sb = seq->private;
652 struct f2fs_sb_info *sbi = F2FS_SB(sb);
653 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
654 int i;
655
656 seq_puts(seq, "format: victim_secmap bitmaps\n");
657
658 for (i = 0; i < MAIN_SECS(sbi); i++) {
659 if ((i % 10) == 0)
660 seq_printf(seq, "%-10d", i);
661 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
662 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
663 seq_putc(seq, '\n');
664 else
665 seq_putc(seq, ' ');
666 }
667 return 0;
668}
669
Chao Yu57fb30c2017-06-14 17:39:47 +0800670#define F2FS_PROC_FILE_DEF(_name) \
671static int _name##_open_fs(struct inode *inode, struct file *file) \
672{ \
673 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
674} \
675 \
676static const struct file_operations f2fs_seq_##_name##_fops = { \
677 .open = _name##_open_fs, \
678 .read = seq_read, \
679 .llseek = seq_lseek, \
680 .release = single_release, \
681};
682
683F2FS_PROC_FILE_DEF(segment_info);
684F2FS_PROC_FILE_DEF(segment_bits);
Chao Yuc0fe4882017-08-02 23:21:48 +0800685F2FS_PROC_FILE_DEF(iostat_info);
Yunlong Song13734782018-07-23 22:10:22 +0800686F2FS_PROC_FILE_DEF(victim_bits);
Chao Yu57fb30c2017-06-14 17:39:47 +0800687
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700688int __init f2fs_init_sysfs(void)
Chao Yu57fb30c2017-06-14 17:39:47 +0800689{
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700690 int ret;
Chao Yu57fb30c2017-06-14 17:39:47 +0800691
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700692 kobject_set_name(&f2fs_kset.kobj, "f2fs");
693 f2fs_kset.kobj.parent = fs_kobj;
694 ret = kset_register(&f2fs_kset);
695 if (ret)
696 return ret;
697
698 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
699 NULL, "features");
700 if (ret)
701 kset_unregister(&f2fs_kset);
702 else
703 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
704 return ret;
Chao Yu57fb30c2017-06-14 17:39:47 +0800705}
706
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700707void f2fs_exit_sysfs(void)
Chao Yu57fb30c2017-06-14 17:39:47 +0800708{
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700709 kobject_put(&f2fs_feat);
710 kset_unregister(&f2fs_kset);
Chao Yu57fb30c2017-06-14 17:39:47 +0800711 remove_proc_entry("fs/f2fs", NULL);
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700712 f2fs_proc_root = NULL;
Chao Yu57fb30c2017-06-14 17:39:47 +0800713}
714
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700715int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
Chao Yu57fb30c2017-06-14 17:39:47 +0800716{
717 struct super_block *sb = sbi->sb;
718 int err;
719
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700720 sbi->s_kobj.kset = &f2fs_kset;
721 init_completion(&sbi->s_kobj_unregister);
722 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
723 "%s", sb->s_id);
724 if (err)
725 return err;
726
Chao Yu57fb30c2017-06-14 17:39:47 +0800727 if (f2fs_proc_root)
728 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
729
730 if (sbi->s_proc) {
731 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
732 &f2fs_seq_segment_info_fops, sb);
733 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
734 &f2fs_seq_segment_bits_fops, sb);
Chao Yuc0fe4882017-08-02 23:21:48 +0800735 proc_create_data("iostat_info", S_IRUGO, sbi->s_proc,
736 &f2fs_seq_iostat_info_fops, sb);
Yunlong Song13734782018-07-23 22:10:22 +0800737 proc_create_data("victim_bits", S_IRUGO, sbi->s_proc,
738 &f2fs_seq_victim_bits_fops, sb);
Chao Yu57fb30c2017-06-14 17:39:47 +0800739 }
Chao Yu57fb30c2017-06-14 17:39:47 +0800740 return 0;
Chao Yu57fb30c2017-06-14 17:39:47 +0800741}
742
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700743void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
Chao Yu57fb30c2017-06-14 17:39:47 +0800744{
Chao Yu57fb30c2017-06-14 17:39:47 +0800745 if (sbi->s_proc) {
Chao Yuc0fe4882017-08-02 23:21:48 +0800746 remove_proc_entry("iostat_info", sbi->s_proc);
Chao Yu57fb30c2017-06-14 17:39:47 +0800747 remove_proc_entry("segment_info", sbi->s_proc);
748 remove_proc_entry("segment_bits", sbi->s_proc);
Yunlong Song13734782018-07-23 22:10:22 +0800749 remove_proc_entry("victim_bits", sbi->s_proc);
Chao Yu57fb30c2017-06-14 17:39:47 +0800750 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
751 }
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700752 kobject_del(&sbi->s_kobj);
Chao Yu57fb30c2017-06-14 17:39:47 +0800753}