blob: 5a252d78d1787e6adddeede3bec53038a869e12e [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
Sheng Yong770611e2018-02-06 12:31:17 +080093 if (f2fs_sb_has_encrypt(sb))
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -070094 len += snprintf(buf, PAGE_SIZE - len, "%s",
95 "encryption");
Sheng Yong770611e2018-02-06 12:31:17 +080096 if (f2fs_sb_has_blkzoned(sb))
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -070097 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
98 len ? ", " : "", "blkzoned");
99 if (f2fs_sb_has_extra_attr(sb))
100 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
101 len ? ", " : "", "extra_attr");
102 if (f2fs_sb_has_project_quota(sb))
103 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
104 len ? ", " : "", "projquota");
105 if (f2fs_sb_has_inode_chksum(sb))
106 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
107 len ? ", " : "", "inode_checksum");
Chao Yu50ffaa92017-09-06 21:59:50 +0800108 if (f2fs_sb_has_flexible_inline_xattr(sb))
109 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
110 len ? ", " : "", "flexible_inline_xattr");
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700111 if (f2fs_sb_has_quota_ino(sb))
112 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
113 len ? ", " : "", "quota_ino");
Chao Yu71f8f042018-01-25 14:54:42 +0800114 if (f2fs_sb_has_inode_crtime(sb))
115 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
116 len ? ", " : "", "inode_crtime");
Sheng Yong9b880fe2018-03-15 18:51:41 +0800117 if (f2fs_sb_has_lost_found(sb))
118 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
119 len ? ", " : "", "lost_found");
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700120 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
121 return len;
122}
123
Yunlong Song68ab6f82017-10-27 20:45:05 +0800124static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
125 struct f2fs_sb_info *sbi, char *buf)
126{
127 return snprintf(buf, PAGE_SIZE, "%u\n", sbi->current_reserved_blocks);
128}
129
Chao Yu57fb30c2017-06-14 17:39:47 +0800130static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
131 struct f2fs_sb_info *sbi, char *buf)
132{
133 unsigned char *ptr = NULL;
134 unsigned int *ui;
135
136 ptr = __struct_ptr(sbi, a->struct_type);
137 if (!ptr)
138 return -EINVAL;
139
Chao Yu1e72cb22018-02-26 22:04:13 +0800140 if (!strcmp(a->attr.name, "extension_list")) {
141 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
142 sbi->raw_super->extension_list;
Chao Yuac734c42018-02-28 17:07:27 +0800143 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
144 int hot_count = sbi->raw_super->hot_ext_count;
Chao Yu1e72cb22018-02-26 22:04:13 +0800145 int len = 0, i;
146
Chao Yuac734c42018-02-28 17:07:27 +0800147 len += snprintf(buf + len, PAGE_SIZE - len,
Colin Ian King0aca14f2018-04-30 16:27:44 +0100148 "cold file extension:\n");
Chao Yuac734c42018-02-28 17:07:27 +0800149 for (i = 0; i < cold_count; i++)
150 len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
151 extlist[i]);
152
153 len += snprintf(buf + len, PAGE_SIZE - len,
Colin Ian King0aca14f2018-04-30 16:27:44 +0100154 "hot file extension:\n");
Chao Yuac734c42018-02-28 17:07:27 +0800155 for (i = cold_count; i < cold_count + hot_count; i++)
Chao Yu1e72cb22018-02-26 22:04:13 +0800156 len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
157 extlist[i]);
158 return len;
159 }
160
Chao Yu57fb30c2017-06-14 17:39:47 +0800161 ui = (unsigned int *)(ptr + a->offset);
162
163 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
164}
165
Chao Yu6b4d6a82018-05-30 00:20:41 +0800166static ssize_t __sbi_store(struct f2fs_attr *a,
Chao Yu57fb30c2017-06-14 17:39:47 +0800167 struct f2fs_sb_info *sbi,
168 const char *buf, size_t count)
169{
170 unsigned char *ptr;
171 unsigned long t;
172 unsigned int *ui;
173 ssize_t ret;
174
175 ptr = __struct_ptr(sbi, a->struct_type);
176 if (!ptr)
177 return -EINVAL;
178
Chao Yu1e72cb22018-02-26 22:04:13 +0800179 if (!strcmp(a->attr.name, "extension_list")) {
180 const char *name = strim((char *)buf);
Chao Yuac734c42018-02-28 17:07:27 +0800181 bool set = true, hot;
Chao Yu1e72cb22018-02-26 22:04:13 +0800182
Chao Yuac734c42018-02-28 17:07:27 +0800183 if (!strncmp(name, "[h]", 3))
184 hot = true;
185 else if (!strncmp(name, "[c]", 3))
186 hot = false;
187 else
188 return -EINVAL;
189
190 name += 3;
191
192 if (*name == '!') {
Chao Yu1e72cb22018-02-26 22:04:13 +0800193 name++;
194 set = false;
195 }
196
197 if (strlen(name) >= F2FS_EXTENSION_LEN)
198 return -EINVAL;
199
200 down_write(&sbi->sb_lock);
201
Chao Yu6b4d6a82018-05-30 00:20:41 +0800202 ret = f2fs_update_extension_list(sbi, name, hot, set);
Chao Yu1e72cb22018-02-26 22:04:13 +0800203 if (ret)
204 goto out;
205
206 ret = f2fs_commit_super(sbi, false);
207 if (ret)
Chao Yu6b4d6a82018-05-30 00:20:41 +0800208 f2fs_update_extension_list(sbi, name, hot, !set);
Chao Yu1e72cb22018-02-26 22:04:13 +0800209out:
210 up_write(&sbi->sb_lock);
211 return ret ? ret : count;
212 }
213
Chao Yu57fb30c2017-06-14 17:39:47 +0800214 ui = (unsigned int *)(ptr + a->offset);
215
216 ret = kstrtoul(skip_spaces(buf), 0, &t);
217 if (ret < 0)
218 return ret;
219#ifdef CONFIG_F2FS_FAULT_INJECTION
220 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
221 return -EINVAL;
222#endif
Chao Yu026bd9d2017-06-26 16:24:41 +0800223 if (a->struct_type == RESERVED_BLOCKS) {
224 spin_lock(&sbi->stat_lock);
Jaegeuk Kim5fee5402017-12-27 15:05:52 -0800225 if (t > (unsigned long)(sbi->user_block_count -
Chao Yue9a50e62018-03-08 14:22:56 +0800226 F2FS_OPTION(sbi).root_reserved_blocks)) {
Chao Yu026bd9d2017-06-26 16:24:41 +0800227 spin_unlock(&sbi->stat_lock);
228 return -EINVAL;
229 }
230 *ui = t;
Yunlong Song68ab6f82017-10-27 20:45:05 +0800231 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
232 sbi->user_block_count - valid_user_blocks(sbi));
Chao Yu026bd9d2017-06-26 16:24:41 +0800233 spin_unlock(&sbi->stat_lock);
234 return count;
235 }
Chao Yuefe24da2017-08-07 23:09:56 +0800236
237 if (!strcmp(a->attr.name, "discard_granularity")) {
Chao Yuefe24da2017-08-07 23:09:56 +0800238 if (t == 0 || t > MAX_PLIST_NUM)
239 return -EINVAL;
240 if (t == *ui)
241 return count;
Chao Yud1c363b2017-09-12 14:25:35 +0800242 *ui = t;
Chao Yuefe24da2017-08-07 23:09:56 +0800243 return count;
244 }
245
Chao Yu0357a0f2018-04-09 10:25:23 +0800246 if (!strcmp(a->attr.name, "trim_sections"))
247 return -EINVAL;
248
Jaegeuk Kim0390d832018-05-07 14:22:40 -0700249 if (!strcmp(a->attr.name, "gc_urgent")) {
250 if (t >= 1) {
251 sbi->gc_mode = GC_URGENT;
252 if (sbi->gc_thread) {
Sheng Yongbb763b52018-08-05 12:45:35 +0800253 sbi->gc_thread->gc_wake = 1;
Jaegeuk Kim0390d832018-05-07 14:22:40 -0700254 wake_up_interruptible_all(
255 &sbi->gc_thread->gc_wait_queue_head);
256 wake_up_discard_thread(sbi, true);
257 }
258 } else {
259 sbi->gc_mode = GC_NORMAL;
260 }
261 return count;
262 }
263 if (!strcmp(a->attr.name, "gc_idle")) {
264 if (t == GC_IDLE_CB)
265 sbi->gc_mode = GC_IDLE_CB;
266 else if (t == GC_IDLE_GREEDY)
267 sbi->gc_mode = GC_IDLE_GREEDY;
268 else
269 sbi->gc_mode = GC_NORMAL;
270 return count;
271 }
272
Chao Yu57fb30c2017-06-14 17:39:47 +0800273 *ui = t;
Chao Yuc0fe4882017-08-02 23:21:48 +0800274
275 if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
276 f2fs_reset_iostat(sbi);
Chao Yu57fb30c2017-06-14 17:39:47 +0800277 return count;
278}
279
Chao Yub9921f02018-05-28 16:57:32 +0800280static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
281 struct f2fs_sb_info *sbi,
282 const char *buf, size_t count)
283{
284 ssize_t ret;
285 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
286 a->struct_type == GC_THREAD);
287
Jaegeuk Kim1d1f79d2018-07-15 09:58:08 +0900288 if (gc_entry) {
289 if (!down_read_trylock(&sbi->sb->s_umount))
290 return -EAGAIN;
291 }
Chao Yu6b4d6a82018-05-30 00:20:41 +0800292 ret = __sbi_store(a, sbi, buf, count);
Chao Yub9921f02018-05-28 16:57:32 +0800293 if (gc_entry)
294 up_read(&sbi->sb->s_umount);
295
296 return ret;
297}
298
Chao Yu57fb30c2017-06-14 17:39:47 +0800299static ssize_t f2fs_attr_show(struct kobject *kobj,
300 struct attribute *attr, char *buf)
301{
302 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
303 s_kobj);
304 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
305
306 return a->show ? a->show(a, sbi, buf) : 0;
307}
308
309static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
310 const char *buf, size_t len)
311{
312 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
313 s_kobj);
314 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
315
316 return a->store ? a->store(a, sbi, buf, len) : 0;
317}
318
319static void f2fs_sb_release(struct kobject *kobj)
320{
321 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
322 s_kobj);
323 complete(&sbi->s_kobj_unregister);
324}
325
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700326enum feat_id {
327 FEAT_CRYPTO = 0,
328 FEAT_BLKZONED,
329 FEAT_ATOMIC_WRITE,
330 FEAT_EXTRA_ATTR,
331 FEAT_PROJECT_QUOTA,
332 FEAT_INODE_CHECKSUM,
Chao Yu50ffaa92017-09-06 21:59:50 +0800333 FEAT_FLEXIBLE_INLINE_XATTR,
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700334 FEAT_QUOTA_INO,
Chao Yu71f8f042018-01-25 14:54:42 +0800335 FEAT_INODE_CRTIME,
Sheng Yong9b880fe2018-03-15 18:51:41 +0800336 FEAT_LOST_FOUND,
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700337};
338
339static ssize_t f2fs_feature_show(struct f2fs_attr *a,
340 struct f2fs_sb_info *sbi, char *buf)
341{
342 switch (a->id) {
343 case FEAT_CRYPTO:
344 case FEAT_BLKZONED:
345 case FEAT_ATOMIC_WRITE:
346 case FEAT_EXTRA_ATTR:
347 case FEAT_PROJECT_QUOTA:
348 case FEAT_INODE_CHECKSUM:
Chao Yu50ffaa92017-09-06 21:59:50 +0800349 case FEAT_FLEXIBLE_INLINE_XATTR:
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700350 case FEAT_QUOTA_INO:
Chao Yu71f8f042018-01-25 14:54:42 +0800351 case FEAT_INODE_CRTIME:
Sheng Yong9b880fe2018-03-15 18:51:41 +0800352 case FEAT_LOST_FOUND:
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700353 return snprintf(buf, PAGE_SIZE, "supported\n");
354 }
355 return 0;
356}
357
Chao Yu57fb30c2017-06-14 17:39:47 +0800358#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
359static struct f2fs_attr f2fs_attr_##_name = { \
360 .attr = {.name = __stringify(_name), .mode = _mode }, \
361 .show = _show, \
362 .store = _store, \
363 .struct_type = _struct_type, \
364 .offset = _offset \
365}
366
367#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
368 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
369 f2fs_sbi_show, f2fs_sbi_store, \
370 offsetof(struct struct_name, elname))
371
372#define F2FS_GENERAL_RO_ATTR(name) \
373static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
374
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700375#define F2FS_FEATURE_RO_ATTR(_name, _id) \
376static struct f2fs_attr f2fs_attr_##_name = { \
377 .attr = {.name = __stringify(_name), .mode = 0444 }, \
378 .show = f2fs_feature_show, \
379 .id = _id, \
380}
381
Jaegeuk Kimbd2ffc22017-08-06 22:09:00 -0700382F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
383 urgent_sleep_time);
Chao Yu57fb30c2017-06-14 17:39:47 +0800384F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
385F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
386F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
Jaegeuk Kim0390d832018-05-07 14:22:40 -0700387F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
388F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
Chao Yu57fb30c2017-06-14 17:39:47 +0800389F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
390F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
Chao Yuefe24da2017-08-07 23:09:56 +0800391F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
Chao Yu026bd9d2017-06-26 16:24:41 +0800392F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
Chao Yu57fb30c2017-06-14 17:39:47 +0800393F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
394F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
395F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
396F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
Jaegeuk Kim6d598452018-08-09 17:53:34 -0700397F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
Chao Yu57fb30c2017-06-14 17:39:47 +0800398F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
Chao Yub9f73872017-10-28 16:52:33 +0800399F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
Chao Yu57fb30c2017-06-14 17:39:47 +0800400F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
401F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
402F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
403F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
404F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
405F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
406F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
Sahitya Tummalafeb1b252018-09-19 14:18:47 +0530407F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
408 interval_time[DISCARD_TIME]);
409F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
Chao Yuc0fe4882017-08-02 23:21:48 +0800410F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
Sheng Yong1ed75332017-11-22 18:23:38 +0800411F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -0800412F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
Chao Yu1e72cb22018-02-26 22:04:13 +0800413F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
Chao Yu57fb30c2017-06-14 17:39:47 +0800414#ifdef CONFIG_F2FS_FAULT_INJECTION
415F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
416F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
417#endif
Jaegeuk Kimaf1cc1e2017-10-24 09:46:54 +0200418F2FS_GENERAL_RO_ATTR(dirty_segments);
Chao Yu57fb30c2017-06-14 17:39:47 +0800419F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700420F2FS_GENERAL_RO_ATTR(features);
Yunlong Song68ab6f82017-10-27 20:45:05 +0800421F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700422
423#ifdef CONFIG_F2FS_FS_ENCRYPTION
424F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
425#endif
426#ifdef CONFIG_BLK_DEV_ZONED
427F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
428#endif
429F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
430F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
431F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
432F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
Chao Yu50ffaa92017-09-06 21:59:50 +0800433F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700434F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
Chao Yu71f8f042018-01-25 14:54:42 +0800435F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
Sheng Yong9b880fe2018-03-15 18:51:41 +0800436F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
Chao Yu57fb30c2017-06-14 17:39:47 +0800437
438#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
439static struct attribute *f2fs_attrs[] = {
Jaegeuk Kimbd2ffc22017-08-06 22:09:00 -0700440 ATTR_LIST(gc_urgent_sleep_time),
Chao Yu57fb30c2017-06-14 17:39:47 +0800441 ATTR_LIST(gc_min_sleep_time),
442 ATTR_LIST(gc_max_sleep_time),
443 ATTR_LIST(gc_no_gc_sleep_time),
444 ATTR_LIST(gc_idle),
Jaegeuk Kimbd2ffc22017-08-06 22:09:00 -0700445 ATTR_LIST(gc_urgent),
Chao Yu57fb30c2017-06-14 17:39:47 +0800446 ATTR_LIST(reclaim_segments),
447 ATTR_LIST(max_small_discards),
Chao Yuefe24da2017-08-07 23:09:56 +0800448 ATTR_LIST(discard_granularity),
Chao Yu57fb30c2017-06-14 17:39:47 +0800449 ATTR_LIST(batched_trim_sections),
450 ATTR_LIST(ipu_policy),
451 ATTR_LIST(min_ipu_util),
452 ATTR_LIST(min_fsync_blocks),
Jaegeuk Kim6d598452018-08-09 17:53:34 -0700453 ATTR_LIST(min_seq_blocks),
Chao Yu57fb30c2017-06-14 17:39:47 +0800454 ATTR_LIST(min_hot_blocks),
Chao Yub9f73872017-10-28 16:52:33 +0800455 ATTR_LIST(min_ssr_sections),
Chao Yu57fb30c2017-06-14 17:39:47 +0800456 ATTR_LIST(max_victim_search),
457 ATTR_LIST(dir_level),
458 ATTR_LIST(ram_thresh),
459 ATTR_LIST(ra_nid_pages),
460 ATTR_LIST(dirty_nats_ratio),
461 ATTR_LIST(cp_interval),
462 ATTR_LIST(idle_interval),
Sahitya Tummalafeb1b252018-09-19 14:18:47 +0530463 ATTR_LIST(discard_idle_interval),
464 ATTR_LIST(gc_idle_interval),
Chao Yuc0fe4882017-08-02 23:21:48 +0800465 ATTR_LIST(iostat_enable),
Sheng Yong1ed75332017-11-22 18:23:38 +0800466 ATTR_LIST(readdir_ra),
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -0800467 ATTR_LIST(gc_pin_file_thresh),
Chao Yu1e72cb22018-02-26 22:04:13 +0800468 ATTR_LIST(extension_list),
Chao Yu57fb30c2017-06-14 17:39:47 +0800469#ifdef CONFIG_F2FS_FAULT_INJECTION
470 ATTR_LIST(inject_rate),
471 ATTR_LIST(inject_type),
472#endif
Jaegeuk Kimaf1cc1e2017-10-24 09:46:54 +0200473 ATTR_LIST(dirty_segments),
Chao Yu57fb30c2017-06-14 17:39:47 +0800474 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700475 ATTR_LIST(features),
Chao Yu026bd9d2017-06-26 16:24:41 +0800476 ATTR_LIST(reserved_blocks),
Yunlong Song68ab6f82017-10-27 20:45:05 +0800477 ATTR_LIST(current_reserved_blocks),
Chao Yu57fb30c2017-06-14 17:39:47 +0800478 NULL,
479};
480
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700481static struct attribute *f2fs_feat_attrs[] = {
482#ifdef CONFIG_F2FS_FS_ENCRYPTION
483 ATTR_LIST(encryption),
484#endif
485#ifdef CONFIG_BLK_DEV_ZONED
486 ATTR_LIST(block_zoned),
487#endif
488 ATTR_LIST(atomic_write),
489 ATTR_LIST(extra_attr),
490 ATTR_LIST(project_quota),
491 ATTR_LIST(inode_checksum),
Chao Yu50ffaa92017-09-06 21:59:50 +0800492 ATTR_LIST(flexible_inline_xattr),
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700493 ATTR_LIST(quota_ino),
Chao Yu71f8f042018-01-25 14:54:42 +0800494 ATTR_LIST(inode_crtime),
Sheng Yong9b880fe2018-03-15 18:51:41 +0800495 ATTR_LIST(lost_found),
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700496 NULL,
497};
498
Chao Yu57fb30c2017-06-14 17:39:47 +0800499static const struct sysfs_ops f2fs_attr_ops = {
500 .show = f2fs_attr_show,
501 .store = f2fs_attr_store,
502};
503
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700504static struct kobj_type f2fs_sb_ktype = {
Chao Yu57fb30c2017-06-14 17:39:47 +0800505 .default_attrs = f2fs_attrs,
506 .sysfs_ops = &f2fs_attr_ops,
507 .release = f2fs_sb_release,
508};
509
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700510static struct kobj_type f2fs_ktype = {
511 .sysfs_ops = &f2fs_attr_ops,
512};
513
514static struct kset f2fs_kset = {
515 .kobj = {.ktype = &f2fs_ktype},
516};
517
518static struct kobj_type f2fs_feat_ktype = {
519 .default_attrs = f2fs_feat_attrs,
520 .sysfs_ops = &f2fs_attr_ops,
521};
522
523static struct kobject f2fs_feat = {
524 .kset = &f2fs_kset,
525};
526
Randy Dunlapbc1ad772018-07-06 20:50:57 -0700527static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
528 void *offset)
Chao Yu57fb30c2017-06-14 17:39:47 +0800529{
530 struct super_block *sb = seq->private;
531 struct f2fs_sb_info *sbi = F2FS_SB(sb);
532 unsigned int total_segs =
533 le32_to_cpu(sbi->raw_super->segment_count_main);
534 int i;
535
536 seq_puts(seq, "format: segment_type|valid_blocks\n"
537 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
538
539 for (i = 0; i < total_segs; i++) {
540 struct seg_entry *se = get_seg_entry(sbi, i);
541
542 if ((i % 10) == 0)
543 seq_printf(seq, "%-10d", i);
544 seq_printf(seq, "%d|%-3u", se->type,
545 get_valid_blocks(sbi, i, false));
546 if ((i % 10) == 9 || i == (total_segs - 1))
547 seq_putc(seq, '\n');
548 else
549 seq_putc(seq, ' ');
550 }
551
552 return 0;
553}
554
Randy Dunlapbc1ad772018-07-06 20:50:57 -0700555static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
556 void *offset)
Chao Yu57fb30c2017-06-14 17:39:47 +0800557{
558 struct super_block *sb = seq->private;
559 struct f2fs_sb_info *sbi = F2FS_SB(sb);
560 unsigned int total_segs =
561 le32_to_cpu(sbi->raw_super->segment_count_main);
562 int i, j;
563
564 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
565 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
566
567 for (i = 0; i < total_segs; i++) {
568 struct seg_entry *se = get_seg_entry(sbi, i);
569
570 seq_printf(seq, "%-10d", i);
571 seq_printf(seq, "%d|%-3u|", se->type,
572 get_valid_blocks(sbi, i, false));
573 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
574 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
575 seq_putc(seq, '\n');
576 }
577 return 0;
578}
579
Randy Dunlapbc1ad772018-07-06 20:50:57 -0700580static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
581 void *offset)
Chao Yuc0fe4882017-08-02 23:21:48 +0800582{
583 struct super_block *sb = seq->private;
584 struct f2fs_sb_info *sbi = F2FS_SB(sb);
585 time64_t now = ktime_get_real_seconds();
586
587 if (!sbi->iostat_enable)
588 return 0;
589
590 seq_printf(seq, "time: %-16llu\n", now);
591
592 /* print app IOs */
593 seq_printf(seq, "app buffered: %-16llu\n",
594 sbi->write_iostat[APP_BUFFERED_IO]);
595 seq_printf(seq, "app direct: %-16llu\n",
596 sbi->write_iostat[APP_DIRECT_IO]);
597 seq_printf(seq, "app mapped: %-16llu\n",
598 sbi->write_iostat[APP_MAPPED_IO]);
599
600 /* print fs IOs */
601 seq_printf(seq, "fs data: %-16llu\n",
602 sbi->write_iostat[FS_DATA_IO]);
603 seq_printf(seq, "fs node: %-16llu\n",
604 sbi->write_iostat[FS_NODE_IO]);
605 seq_printf(seq, "fs meta: %-16llu\n",
606 sbi->write_iostat[FS_META_IO]);
607 seq_printf(seq, "fs gc data: %-16llu\n",
608 sbi->write_iostat[FS_GC_DATA_IO]);
609 seq_printf(seq, "fs gc node: %-16llu\n",
610 sbi->write_iostat[FS_GC_NODE_IO]);
611 seq_printf(seq, "fs cp data: %-16llu\n",
612 sbi->write_iostat[FS_CP_DATA_IO]);
613 seq_printf(seq, "fs cp node: %-16llu\n",
614 sbi->write_iostat[FS_CP_NODE_IO]);
615 seq_printf(seq, "fs cp meta: %-16llu\n",
616 sbi->write_iostat[FS_CP_META_IO]);
617 seq_printf(seq, "fs discard: %-16llu\n",
618 sbi->write_iostat[FS_DISCARD]);
619
620 return 0;
621}
622
Yunlong Song13734782018-07-23 22:10:22 +0800623static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
624 void *offset)
625{
626 struct super_block *sb = seq->private;
627 struct f2fs_sb_info *sbi = F2FS_SB(sb);
628 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
629 int i;
630
631 seq_puts(seq, "format: victim_secmap bitmaps\n");
632
633 for (i = 0; i < MAIN_SECS(sbi); i++) {
634 if ((i % 10) == 0)
635 seq_printf(seq, "%-10d", i);
636 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
637 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
638 seq_putc(seq, '\n');
639 else
640 seq_putc(seq, ' ');
641 }
642 return 0;
643}
644
Chao Yu57fb30c2017-06-14 17:39:47 +0800645#define F2FS_PROC_FILE_DEF(_name) \
646static int _name##_open_fs(struct inode *inode, struct file *file) \
647{ \
648 return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
649} \
650 \
651static const struct file_operations f2fs_seq_##_name##_fops = { \
652 .open = _name##_open_fs, \
653 .read = seq_read, \
654 .llseek = seq_lseek, \
655 .release = single_release, \
656};
657
658F2FS_PROC_FILE_DEF(segment_info);
659F2FS_PROC_FILE_DEF(segment_bits);
Chao Yuc0fe4882017-08-02 23:21:48 +0800660F2FS_PROC_FILE_DEF(iostat_info);
Yunlong Song13734782018-07-23 22:10:22 +0800661F2FS_PROC_FILE_DEF(victim_bits);
Chao Yu57fb30c2017-06-14 17:39:47 +0800662
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700663int __init f2fs_init_sysfs(void)
Chao Yu57fb30c2017-06-14 17:39:47 +0800664{
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700665 int ret;
Chao Yu57fb30c2017-06-14 17:39:47 +0800666
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700667 kobject_set_name(&f2fs_kset.kobj, "f2fs");
668 f2fs_kset.kobj.parent = fs_kobj;
669 ret = kset_register(&f2fs_kset);
670 if (ret)
671 return ret;
672
673 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
674 NULL, "features");
675 if (ret)
676 kset_unregister(&f2fs_kset);
677 else
678 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
679 return ret;
Chao Yu57fb30c2017-06-14 17:39:47 +0800680}
681
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700682void f2fs_exit_sysfs(void)
Chao Yu57fb30c2017-06-14 17:39:47 +0800683{
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700684 kobject_put(&f2fs_feat);
685 kset_unregister(&f2fs_kset);
Chao Yu57fb30c2017-06-14 17:39:47 +0800686 remove_proc_entry("fs/f2fs", NULL);
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700687 f2fs_proc_root = NULL;
Chao Yu57fb30c2017-06-14 17:39:47 +0800688}
689
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700690int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
Chao Yu57fb30c2017-06-14 17:39:47 +0800691{
692 struct super_block *sb = sbi->sb;
693 int err;
694
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700695 sbi->s_kobj.kset = &f2fs_kset;
696 init_completion(&sbi->s_kobj_unregister);
697 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
698 "%s", sb->s_id);
699 if (err)
700 return err;
701
Chao Yu57fb30c2017-06-14 17:39:47 +0800702 if (f2fs_proc_root)
703 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
704
705 if (sbi->s_proc) {
706 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
707 &f2fs_seq_segment_info_fops, sb);
708 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
709 &f2fs_seq_segment_bits_fops, sb);
Chao Yuc0fe4882017-08-02 23:21:48 +0800710 proc_create_data("iostat_info", S_IRUGO, sbi->s_proc,
711 &f2fs_seq_iostat_info_fops, sb);
Yunlong Song13734782018-07-23 22:10:22 +0800712 proc_create_data("victim_bits", S_IRUGO, sbi->s_proc,
713 &f2fs_seq_victim_bits_fops, sb);
Chao Yu57fb30c2017-06-14 17:39:47 +0800714 }
Chao Yu57fb30c2017-06-14 17:39:47 +0800715 return 0;
Chao Yu57fb30c2017-06-14 17:39:47 +0800716}
717
Jaegeuk Kim883d5532017-07-26 11:24:13 -0700718void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
Chao Yu57fb30c2017-06-14 17:39:47 +0800719{
Chao Yu57fb30c2017-06-14 17:39:47 +0800720 if (sbi->s_proc) {
Chao Yuc0fe4882017-08-02 23:21:48 +0800721 remove_proc_entry("iostat_info", sbi->s_proc);
Chao Yu57fb30c2017-06-14 17:39:47 +0800722 remove_proc_entry("segment_info", sbi->s_proc);
723 remove_proc_entry("segment_bits", sbi->s_proc);
Yunlong Song13734782018-07-23 22:10:22 +0800724 remove_proc_entry("victim_bits", sbi->s_proc);
Chao Yu57fb30c2017-06-14 17:39:47 +0800725 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
726 }
Jaegeuk Kim3b3d1b82017-07-21 17:14:09 -0700727 kobject_del(&sbi->s_kobj);
Chao Yu57fb30c2017-06-14 17:39:47 +0800728}