1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
8 */
9 #include <linux/compiler.h>
10 #include <linux/proc_fs.h>
11 #include <linux/f2fs_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/unicode.h>
14
15 #include "f2fs.h"
16 #include "segment.h"
17 #include "gc.h"
18 #include <trace/events/f2fs.h>
19
20 static struct proc_dir_entry *f2fs_proc_root;
21
22 /* Sysfs support for f2fs */
23 enum {
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_STAT_FS
30 STAT_INFO, /* struct f2fs_stat_info */
31 #endif
32 #ifdef CONFIG_F2FS_FAULT_INJECTION
33 FAULT_INFO_RATE, /* struct f2fs_fault_info */
34 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
35 #endif
36 RESERVED_BLOCKS, /* struct f2fs_sb_info */
37 };
38
39 struct f2fs_attr {
40 struct attribute attr;
41 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
42 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
43 const char *, size_t);
44 int struct_type;
45 int offset;
46 int id;
47 };
48
49 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
50 struct f2fs_sb_info *sbi, char *buf);
51
__struct_ptr(struct f2fs_sb_info * sbi,int struct_type)52 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
53 {
54 if (struct_type == GC_THREAD)
55 return (unsigned char *)sbi->gc_thread;
56 else if (struct_type == SM_INFO)
57 return (unsigned char *)SM_I(sbi);
58 else if (struct_type == DCC_INFO)
59 return (unsigned char *)SM_I(sbi)->dcc_info;
60 else if (struct_type == NM_INFO)
61 return (unsigned char *)NM_I(sbi);
62 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
63 return (unsigned char *)sbi;
64 #ifdef CONFIG_F2FS_FAULT_INJECTION
65 else if (struct_type == FAULT_INFO_RATE ||
66 struct_type == FAULT_INFO_TYPE)
67 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
68 #endif
69 #ifdef CONFIG_F2FS_STAT_FS
70 else if (struct_type == STAT_INFO)
71 return (unsigned char *)F2FS_STAT(sbi);
72 #endif
73 return NULL;
74 }
75
dirty_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)76 static ssize_t dirty_segments_show(struct f2fs_attr *a,
77 struct f2fs_sb_info *sbi, char *buf)
78 {
79 return sprintf(buf, "%llu\n",
80 (unsigned long long)(dirty_segments(sbi)));
81 }
82
free_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)83 static ssize_t free_segments_show(struct f2fs_attr *a,
84 struct f2fs_sb_info *sbi, char *buf)
85 {
86 return sprintf(buf, "%llu\n",
87 (unsigned long long)(free_segments(sbi)));
88 }
89
ovp_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)90 static ssize_t ovp_segments_show(struct f2fs_attr *a,
91 struct f2fs_sb_info *sbi, char *buf)
92 {
93 return sprintf(buf, "%llu\n",
94 (unsigned long long)(overprovision_segments(sbi)));
95 }
96
lifetime_write_kbytes_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)97 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
98 struct f2fs_sb_info *sbi, char *buf)
99 {
100 struct super_block *sb = sbi->sb;
101
102 if (!sb->s_bdev->bd_part)
103 return sprintf(buf, "0\n");
104
105 return sprintf(buf, "%llu\n",
106 (unsigned long long)(sbi->kbytes_written +
107 BD_PART_WRITTEN(sbi)));
108 }
109
features_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)110 static ssize_t features_show(struct f2fs_attr *a,
111 struct f2fs_sb_info *sbi, char *buf)
112 {
113 struct super_block *sb = sbi->sb;
114 int len = 0;
115
116 if (!sb->s_bdev->bd_part)
117 return sprintf(buf, "0\n");
118
119 if (f2fs_sb_has_encrypt(sbi))
120 len += scnprintf(buf, PAGE_SIZE - len, "%s",
121 "encryption");
122 if (f2fs_sb_has_blkzoned(sbi))
123 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
124 len ? ", " : "", "blkzoned");
125 if (f2fs_sb_has_extra_attr(sbi))
126 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
127 len ? ", " : "", "extra_attr");
128 if (f2fs_sb_has_project_quota(sbi))
129 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
130 len ? ", " : "", "projquota");
131 if (f2fs_sb_has_inode_chksum(sbi))
132 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
133 len ? ", " : "", "inode_checksum");
134 if (f2fs_sb_has_flexible_inline_xattr(sbi))
135 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
136 len ? ", " : "", "flexible_inline_xattr");
137 if (f2fs_sb_has_quota_ino(sbi))
138 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
139 len ? ", " : "", "quota_ino");
140 if (f2fs_sb_has_inode_crtime(sbi))
141 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
142 len ? ", " : "", "inode_crtime");
143 if (f2fs_sb_has_lost_found(sbi))
144 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
145 len ? ", " : "", "lost_found");
146 if (f2fs_sb_has_verity(sbi))
147 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
148 len ? ", " : "", "verity");
149 if (f2fs_sb_has_sb_chksum(sbi))
150 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
151 len ? ", " : "", "sb_checksum");
152 if (f2fs_sb_has_casefold(sbi))
153 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
154 len ? ", " : "", "casefold");
155 if (f2fs_sb_has_readonly(sbi))
156 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
157 len ? ", " : "", "readonly");
158 if (f2fs_sb_has_compression(sbi))
159 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
160 len ? ", " : "", "compression");
161 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
162 len ? ", " : "", "pin_file");
163 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
164 return len;
165 }
166
current_reserved_blocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)167 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
168 struct f2fs_sb_info *sbi, char *buf)
169 {
170 return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
171 }
172
unusable_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)173 static ssize_t unusable_show(struct f2fs_attr *a,
174 struct f2fs_sb_info *sbi, char *buf)
175 {
176 block_t unusable;
177
178 if (test_opt(sbi, DISABLE_CHECKPOINT))
179 unusable = sbi->unusable_block_count;
180 else
181 unusable = f2fs_get_unusable_blocks(sbi);
182 return sprintf(buf, "%llu\n", (unsigned long long)unusable);
183 }
184
encoding_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)185 static ssize_t encoding_show(struct f2fs_attr *a,
186 struct f2fs_sb_info *sbi, char *buf)
187 {
188 #ifdef CONFIG_UNICODE
189 struct super_block *sb = sbi->sb;
190
191 if (f2fs_sb_has_casefold(sbi))
192 return snprintf(buf, PAGE_SIZE, "%s (%d.%d.%d)\n",
193 sb->s_encoding->charset,
194 (sb->s_encoding->version >> 16) & 0xff,
195 (sb->s_encoding->version >> 8) & 0xff,
196 sb->s_encoding->version & 0xff);
197 #endif
198 return sprintf(buf, "(none)");
199 }
200
mounted_time_sec_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)201 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
202 struct f2fs_sb_info *sbi, char *buf)
203 {
204 return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
205 }
206
207 #ifdef CONFIG_F2FS_STAT_FS
moved_blocks_foreground_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)208 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
209 struct f2fs_sb_info *sbi, char *buf)
210 {
211 struct f2fs_stat_info *si = F2FS_STAT(sbi);
212
213 return sprintf(buf, "%llu\n",
214 (unsigned long long)(si->tot_blks -
215 (si->bg_data_blks + si->bg_node_blks)));
216 }
217
moved_blocks_background_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)218 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
219 struct f2fs_sb_info *sbi, char *buf)
220 {
221 struct f2fs_stat_info *si = F2FS_STAT(sbi);
222
223 return sprintf(buf, "%llu\n",
224 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
225 }
226
avg_vblocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)227 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
228 struct f2fs_sb_info *sbi, char *buf)
229 {
230 struct f2fs_stat_info *si = F2FS_STAT(sbi);
231
232 si->dirty_count = dirty_segments(sbi);
233 f2fs_update_sit_info(sbi);
234 return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
235 }
236 #endif
237
f2fs_sbi_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)238 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
239 struct f2fs_sb_info *sbi, char *buf)
240 {
241 unsigned char *ptr = NULL;
242 unsigned int *ui;
243
244 ptr = __struct_ptr(sbi, a->struct_type);
245 if (!ptr)
246 return -EINVAL;
247
248 if (!strcmp(a->attr.name, "extension_list")) {
249 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
250 sbi->raw_super->extension_list;
251 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
252 int hot_count = sbi->raw_super->hot_ext_count;
253 int len = 0, i;
254
255 len += scnprintf(buf + len, PAGE_SIZE - len,
256 "cold file extension:\n");
257 for (i = 0; i < cold_count; i++)
258 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
259 extlist[i]);
260
261 len += scnprintf(buf + len, PAGE_SIZE - len,
262 "hot file extension:\n");
263 for (i = cold_count; i < cold_count + hot_count; i++)
264 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
265 extlist[i]);
266 return len;
267 }
268
269 ui = (unsigned int *)(ptr + a->offset);
270
271 return sprintf(buf, "%u\n", *ui);
272 }
273
__sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)274 static ssize_t __sbi_store(struct f2fs_attr *a,
275 struct f2fs_sb_info *sbi,
276 const char *buf, size_t count)
277 {
278 unsigned char *ptr;
279 unsigned long t;
280 unsigned int *ui;
281 ssize_t ret;
282
283 ptr = __struct_ptr(sbi, a->struct_type);
284 if (!ptr)
285 return -EINVAL;
286
287 if (!strcmp(a->attr.name, "extension_list")) {
288 const char *name = strim((char *)buf);
289 bool set = true, hot;
290
291 if (!strncmp(name, "[h]", 3))
292 hot = true;
293 else if (!strncmp(name, "[c]", 3))
294 hot = false;
295 else
296 return -EINVAL;
297
298 name += 3;
299
300 if (*name == '!') {
301 name++;
302 set = false;
303 }
304
305 if (strlen(name) >= F2FS_EXTENSION_LEN)
306 return -EINVAL;
307
308 down_write(&sbi->sb_lock);
309
310 ret = f2fs_update_extension_list(sbi, name, hot, set);
311 if (ret)
312 goto out;
313
314 ret = f2fs_commit_super(sbi, false);
315 if (ret)
316 f2fs_update_extension_list(sbi, name, hot, !set);
317 out:
318 up_write(&sbi->sb_lock);
319 return ret ? ret : count;
320 }
321
322 ui = (unsigned int *)(ptr + a->offset);
323
324 ret = kstrtoul(skip_spaces(buf), 0, &t);
325 if (ret < 0)
326 return ret;
327 #ifdef CONFIG_F2FS_FAULT_INJECTION
328 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
329 return -EINVAL;
330 if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
331 return -EINVAL;
332 #endif
333 if (a->struct_type == RESERVED_BLOCKS) {
334 spin_lock(&sbi->stat_lock);
335 if (t > (unsigned long)(sbi->user_block_count -
336 F2FS_OPTION(sbi).root_reserved_blocks -
337 sbi->blocks_per_seg *
338 SM_I(sbi)->additional_reserved_segments)) {
339 spin_unlock(&sbi->stat_lock);
340 return -EINVAL;
341 }
342 *ui = t;
343 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
344 sbi->user_block_count - valid_user_blocks(sbi));
345 spin_unlock(&sbi->stat_lock);
346 return count;
347 }
348
349 if (!strcmp(a->attr.name, "discard_granularity")) {
350 if (t == 0 || t > MAX_PLIST_NUM)
351 return -EINVAL;
352 if (t == *ui)
353 return count;
354 *ui = t;
355 return count;
356 }
357
358 if (!strcmp(a->attr.name, "migration_granularity")) {
359 if (t == 0 || t > sbi->segs_per_sec)
360 return -EINVAL;
361 }
362
363 if (!strcmp(a->attr.name, "trim_sections"))
364 return -EINVAL;
365
366 if (!strcmp(a->attr.name, "gc_urgent")) {
367 if (t >= 1) {
368 sbi->gc_mode = GC_URGENT;
369 if (sbi->gc_thread) {
370 sbi->gc_thread->gc_wake = 1;
371 wake_up_interruptible_all(
372 &sbi->gc_thread->gc_wait_queue_head);
373 wake_up_discard_thread(sbi, true);
374 }
375 } else {
376 sbi->gc_mode = GC_NORMAL;
377 }
378 return count;
379 }
380 if (!strcmp(a->attr.name, "gc_idle")) {
381 if (t == GC_IDLE_CB)
382 sbi->gc_mode = GC_IDLE_CB;
383 else if (t == GC_IDLE_GREEDY)
384 sbi->gc_mode = GC_IDLE_GREEDY;
385 else
386 sbi->gc_mode = GC_NORMAL;
387 return count;
388 }
389
390 if (!strcmp(a->attr.name, "iostat_enable")) {
391 sbi->iostat_enable = !!t;
392 if (!sbi->iostat_enable)
393 f2fs_reset_iostat(sbi);
394 return count;
395 }
396
397 if (!strcmp(a->attr.name, "iostat_period_ms")) {
398 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
399 return -EINVAL;
400 spin_lock(&sbi->iostat_lock);
401 sbi->iostat_period_ms = (unsigned int)t;
402 spin_unlock(&sbi->iostat_lock);
403 return count;
404 }
405
406 *ui = (unsigned int)t;
407
408 return count;
409 }
410
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)411 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
412 struct f2fs_sb_info *sbi,
413 const char *buf, size_t count)
414 {
415 ssize_t ret;
416 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
417 a->struct_type == GC_THREAD);
418
419 if (gc_entry) {
420 if (!down_read_trylock(&sbi->sb->s_umount))
421 return -EAGAIN;
422 }
423 ret = __sbi_store(a, sbi, buf, count);
424 if (gc_entry)
425 up_read(&sbi->sb->s_umount);
426
427 return ret;
428 }
429
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)430 static ssize_t f2fs_attr_show(struct kobject *kobj,
431 struct attribute *attr, char *buf)
432 {
433 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
434 s_kobj);
435 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
436
437 return a->show ? a->show(a, sbi, buf) : 0;
438 }
439
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)440 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
441 const char *buf, size_t len)
442 {
443 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
444 s_kobj);
445 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
446
447 return a->store ? a->store(a, sbi, buf, len) : 0;
448 }
449
f2fs_sb_release(struct kobject * kobj)450 static void f2fs_sb_release(struct kobject *kobj)
451 {
452 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
453 s_kobj);
454 complete(&sbi->s_kobj_unregister);
455 }
456
457 enum feat_id {
458 FEAT_CRYPTO = 0,
459 FEAT_BLKZONED,
460 FEAT_ATOMIC_WRITE,
461 FEAT_EXTRA_ATTR,
462 FEAT_PROJECT_QUOTA,
463 FEAT_INODE_CHECKSUM,
464 FEAT_FLEXIBLE_INLINE_XATTR,
465 FEAT_QUOTA_INO,
466 FEAT_INODE_CRTIME,
467 FEAT_LOST_FOUND,
468 FEAT_VERITY,
469 FEAT_SB_CHECKSUM,
470 FEAT_CASEFOLD,
471 FEAT_COMPRESSION,
472 FEAT_RO,
473 FEAT_TEST_DUMMY_ENCRYPTION_V2,
474 FEAT_ENCRYPTED_CASEFOLD,
475 };
476
f2fs_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)477 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
478 struct f2fs_sb_info *sbi, char *buf)
479 {
480 switch (a->id) {
481 case FEAT_CRYPTO:
482 case FEAT_BLKZONED:
483 case FEAT_ATOMIC_WRITE:
484 case FEAT_EXTRA_ATTR:
485 case FEAT_PROJECT_QUOTA:
486 case FEAT_INODE_CHECKSUM:
487 case FEAT_FLEXIBLE_INLINE_XATTR:
488 case FEAT_QUOTA_INO:
489 case FEAT_INODE_CRTIME:
490 case FEAT_LOST_FOUND:
491 case FEAT_VERITY:
492 case FEAT_SB_CHECKSUM:
493 case FEAT_CASEFOLD:
494 case FEAT_COMPRESSION:
495 case FEAT_RO:
496 case FEAT_TEST_DUMMY_ENCRYPTION_V2:
497 case FEAT_ENCRYPTED_CASEFOLD:
498 return sprintf(buf, "supported\n");
499 }
500 return 0;
501 }
502
503 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
504 static struct f2fs_attr f2fs_attr_##_name = { \
505 .attr = {.name = __stringify(_name), .mode = _mode }, \
506 .show = _show, \
507 .store = _store, \
508 .struct_type = _struct_type, \
509 .offset = _offset \
510 }
511
512 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
513 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
514 f2fs_sbi_show, f2fs_sbi_store, \
515 offsetof(struct struct_name, elname))
516
517 #define F2FS_GENERAL_RO_ATTR(name) \
518 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
519
520 #define F2FS_FEATURE_RO_ATTR(_name, _id) \
521 static struct f2fs_attr f2fs_attr_##_name = { \
522 .attr = {.name = __stringify(_name), .mode = 0444 }, \
523 .show = f2fs_feature_show, \
524 .id = _id, \
525 }
526
527 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname) \
528 static struct f2fs_attr f2fs_attr_##_name = { \
529 .attr = {.name = __stringify(_name), .mode = 0444 }, \
530 .show = f2fs_sbi_show, \
531 .struct_type = _struct_type, \
532 .offset = offsetof(struct _struct_name, _elname), \
533 }
534
535 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
536 urgent_sleep_time);
537 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
538 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
539 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
540 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
541 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
542 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
543 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, main_blkaddr, main_blkaddr);
544 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
545 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
546 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
547 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
548 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
549 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
550 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
551 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
552 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
553 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
554 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
555 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
556 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
557 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
558 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
559 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
560 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
561 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
562 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
563 interval_time[DISCARD_TIME]);
564 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
565 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
566 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
567 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
568 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
569 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
570 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
571 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
572 #ifdef CONFIG_F2FS_FAULT_INJECTION
573 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
574 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
575 #endif
576 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
577 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
578 F2FS_GENERAL_RO_ATTR(dirty_segments);
579 F2FS_GENERAL_RO_ATTR(free_segments);
580 F2FS_GENERAL_RO_ATTR(ovp_segments);
581 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
582 F2FS_GENERAL_RO_ATTR(features);
583 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
584 F2FS_GENERAL_RO_ATTR(unusable);
585 F2FS_GENERAL_RO_ATTR(encoding);
586 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
587 #ifdef CONFIG_F2FS_STAT_FS
588 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
589 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
590 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
591 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
592 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
593 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
594 F2FS_GENERAL_RO_ATTR(avg_vblocks);
595 #endif
596
597 #ifdef CONFIG_FS_ENCRYPTION
598 F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
599 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2, FEAT_TEST_DUMMY_ENCRYPTION_V2);
600 #ifdef CONFIG_UNICODE
601 F2FS_FEATURE_RO_ATTR(encrypted_casefold, FEAT_ENCRYPTED_CASEFOLD);
602 #endif
603 #endif /* CONFIG_FS_ENCRYPTION */
604 #ifdef CONFIG_BLK_DEV_ZONED
605 F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
606 #endif
607 F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
608 F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
609 F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
610 F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
611 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
612 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
613 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
614 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
615 #ifdef CONFIG_FS_VERITY
616 F2FS_FEATURE_RO_ATTR(verity, FEAT_VERITY);
617 #endif
618 F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
619 #ifdef CONFIG_UNICODE
620 F2FS_FEATURE_RO_ATTR(casefold, FEAT_CASEFOLD);
621 #endif
622 F2FS_FEATURE_RO_ATTR(readonly, FEAT_RO);
623 #ifdef CONFIG_F2FS_FS_COMPRESSION
624 F2FS_FEATURE_RO_ATTR(compression, FEAT_COMPRESSION);
625 #endif
626
627 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
628 static struct attribute *f2fs_attrs[] = {
629 ATTR_LIST(gc_urgent_sleep_time),
630 ATTR_LIST(gc_min_sleep_time),
631 ATTR_LIST(gc_max_sleep_time),
632 ATTR_LIST(gc_no_gc_sleep_time),
633 ATTR_LIST(gc_idle),
634 ATTR_LIST(gc_urgent),
635 ATTR_LIST(reclaim_segments),
636 ATTR_LIST(main_blkaddr),
637 ATTR_LIST(max_small_discards),
638 ATTR_LIST(discard_granularity),
639 ATTR_LIST(batched_trim_sections),
640 ATTR_LIST(ipu_policy),
641 ATTR_LIST(min_ipu_util),
642 ATTR_LIST(min_fsync_blocks),
643 ATTR_LIST(min_seq_blocks),
644 ATTR_LIST(min_hot_blocks),
645 ATTR_LIST(min_ssr_sections),
646 ATTR_LIST(max_victim_search),
647 ATTR_LIST(migration_granularity),
648 ATTR_LIST(dir_level),
649 ATTR_LIST(ram_thresh),
650 ATTR_LIST(ra_nid_pages),
651 ATTR_LIST(dirty_nats_ratio),
652 ATTR_LIST(cp_interval),
653 ATTR_LIST(idle_interval),
654 ATTR_LIST(discard_idle_interval),
655 ATTR_LIST(gc_idle_interval),
656 ATTR_LIST(umount_discard_timeout),
657 ATTR_LIST(iostat_enable),
658 ATTR_LIST(iostat_period_ms),
659 ATTR_LIST(readdir_ra),
660 ATTR_LIST(gc_pin_file_thresh),
661 ATTR_LIST(extension_list),
662 #ifdef CONFIG_F2FS_FAULT_INJECTION
663 ATTR_LIST(inject_rate),
664 ATTR_LIST(inject_type),
665 #endif
666 ATTR_LIST(data_io_flag),
667 ATTR_LIST(node_io_flag),
668 ATTR_LIST(dirty_segments),
669 ATTR_LIST(free_segments),
670 ATTR_LIST(ovp_segments),
671 ATTR_LIST(unusable),
672 ATTR_LIST(lifetime_write_kbytes),
673 ATTR_LIST(features),
674 ATTR_LIST(reserved_blocks),
675 ATTR_LIST(current_reserved_blocks),
676 ATTR_LIST(encoding),
677 ATTR_LIST(mounted_time_sec),
678 #ifdef CONFIG_F2FS_STAT_FS
679 ATTR_LIST(cp_foreground_calls),
680 ATTR_LIST(cp_background_calls),
681 ATTR_LIST(gc_foreground_calls),
682 ATTR_LIST(gc_background_calls),
683 ATTR_LIST(moved_blocks_foreground),
684 ATTR_LIST(moved_blocks_background),
685 ATTR_LIST(avg_vblocks),
686 #endif
687 NULL,
688 };
689 ATTRIBUTE_GROUPS(f2fs);
690
691 static struct attribute *f2fs_feat_attrs[] = {
692 #ifdef CONFIG_FS_ENCRYPTION
693 ATTR_LIST(encryption),
694 ATTR_LIST(test_dummy_encryption_v2),
695 #ifdef CONFIG_UNICODE
696 ATTR_LIST(encrypted_casefold),
697 #endif
698 #endif /* CONFIG_FS_ENCRYPTION */
699 #ifdef CONFIG_BLK_DEV_ZONED
700 ATTR_LIST(block_zoned),
701 #endif
702 ATTR_LIST(atomic_write),
703 ATTR_LIST(extra_attr),
704 ATTR_LIST(project_quota),
705 ATTR_LIST(inode_checksum),
706 ATTR_LIST(flexible_inline_xattr),
707 ATTR_LIST(quota_ino),
708 ATTR_LIST(inode_crtime),
709 ATTR_LIST(lost_found),
710 #ifdef CONFIG_FS_VERITY
711 ATTR_LIST(verity),
712 #endif
713 ATTR_LIST(sb_checksum),
714 #ifdef CONFIG_UNICODE
715 ATTR_LIST(casefold),
716 #endif
717 ATTR_LIST(readonly),
718 #ifdef CONFIG_F2FS_FS_COMPRESSION
719 ATTR_LIST(compression),
720 #endif
721 NULL,
722 };
723 ATTRIBUTE_GROUPS(f2fs_feat);
724
725 static const struct sysfs_ops f2fs_attr_ops = {
726 .show = f2fs_attr_show,
727 .store = f2fs_attr_store,
728 };
729
730 static struct kobj_type f2fs_sb_ktype = {
731 .default_groups = f2fs_groups,
732 .sysfs_ops = &f2fs_attr_ops,
733 .release = f2fs_sb_release,
734 };
735
736 static struct kobj_type f2fs_ktype = {
737 .sysfs_ops = &f2fs_attr_ops,
738 };
739
740 static struct kset f2fs_kset = {
741 .kobj = {.ktype = &f2fs_ktype},
742 };
743
744 static struct kobj_type f2fs_feat_ktype = {
745 .default_groups = f2fs_feat_groups,
746 .sysfs_ops = &f2fs_attr_ops,
747 };
748
749 static struct kobject f2fs_feat = {
750 .kset = &f2fs_kset,
751 };
752
segment_info_seq_show(struct seq_file * seq,void * offset)753 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
754 void *offset)
755 {
756 struct super_block *sb = seq->private;
757 struct f2fs_sb_info *sbi = F2FS_SB(sb);
758 unsigned int total_segs =
759 le32_to_cpu(sbi->raw_super->segment_count_main);
760 int i;
761
762 seq_puts(seq, "format: segment_type|valid_blocks\n"
763 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
764
765 for (i = 0; i < total_segs; i++) {
766 struct seg_entry *se = get_seg_entry(sbi, i);
767
768 if ((i % 10) == 0)
769 seq_printf(seq, "%-10d", i);
770 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
771 if ((i % 10) == 9 || i == (total_segs - 1))
772 seq_putc(seq, '\n');
773 else
774 seq_putc(seq, ' ');
775 }
776
777 return 0;
778 }
779
segment_bits_seq_show(struct seq_file * seq,void * offset)780 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
781 void *offset)
782 {
783 struct super_block *sb = seq->private;
784 struct f2fs_sb_info *sbi = F2FS_SB(sb);
785 unsigned int total_segs =
786 le32_to_cpu(sbi->raw_super->segment_count_main);
787 int i, j;
788
789 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
790 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
791
792 for (i = 0; i < total_segs; i++) {
793 struct seg_entry *se = get_seg_entry(sbi, i);
794
795 seq_printf(seq, "%-10d", i);
796 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
797 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
798 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
799 seq_putc(seq, '\n');
800 }
801 return 0;
802 }
803
f2fs_record_iostat(struct f2fs_sb_info * sbi)804 void f2fs_record_iostat(struct f2fs_sb_info *sbi)
805 {
806 unsigned long long iostat_diff[NR_IO_TYPE];
807 int i;
808
809 if (time_is_after_jiffies(sbi->iostat_next_period))
810 return;
811
812 /* Need double check under the lock */
813 spin_lock(&sbi->iostat_lock);
814 if (time_is_after_jiffies(sbi->iostat_next_period)) {
815 spin_unlock(&sbi->iostat_lock);
816 return;
817 }
818 sbi->iostat_next_period = jiffies +
819 msecs_to_jiffies(sbi->iostat_period_ms);
820
821 for (i = 0; i < NR_IO_TYPE; i++) {
822 iostat_diff[i] = sbi->rw_iostat[i] -
823 sbi->prev_rw_iostat[i];
824 sbi->prev_rw_iostat[i] = sbi->rw_iostat[i];
825 }
826 spin_unlock(&sbi->iostat_lock);
827
828 trace_f2fs_iostat(sbi, iostat_diff);
829 }
830
iostat_info_seq_show(struct seq_file * seq,void * offset)831 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
832 void *offset)
833 {
834 struct super_block *sb = seq->private;
835 struct f2fs_sb_info *sbi = F2FS_SB(sb);
836 time64_t now = ktime_get_real_seconds();
837
838 if (!sbi->iostat_enable)
839 return 0;
840
841 seq_printf(seq, "time: %-16llu\n", now);
842
843 /* print app write IOs */
844 seq_puts(seq, "[WRITE]\n");
845 seq_printf(seq, "app buffered: %-16llu\n",
846 sbi->rw_iostat[APP_BUFFERED_IO]);
847 seq_printf(seq, "app direct: %-16llu\n",
848 sbi->rw_iostat[APP_DIRECT_IO]);
849 seq_printf(seq, "app mapped: %-16llu\n",
850 sbi->rw_iostat[APP_MAPPED_IO]);
851
852 /* print fs write IOs */
853 seq_printf(seq, "fs data: %-16llu\n",
854 sbi->rw_iostat[FS_DATA_IO]);
855 seq_printf(seq, "fs node: %-16llu\n",
856 sbi->rw_iostat[FS_NODE_IO]);
857 seq_printf(seq, "fs meta: %-16llu\n",
858 sbi->rw_iostat[FS_META_IO]);
859 seq_printf(seq, "fs gc data: %-16llu\n",
860 sbi->rw_iostat[FS_GC_DATA_IO]);
861 seq_printf(seq, "fs gc node: %-16llu\n",
862 sbi->rw_iostat[FS_GC_NODE_IO]);
863 seq_printf(seq, "fs cp data: %-16llu\n",
864 sbi->rw_iostat[FS_CP_DATA_IO]);
865 seq_printf(seq, "fs cp node: %-16llu\n",
866 sbi->rw_iostat[FS_CP_NODE_IO]);
867 seq_printf(seq, "fs cp meta: %-16llu\n",
868 sbi->rw_iostat[FS_CP_META_IO]);
869
870 /* print app read IOs */
871 seq_puts(seq, "[READ]\n");
872 seq_printf(seq, "app buffered: %-16llu\n",
873 sbi->rw_iostat[APP_BUFFERED_READ_IO]);
874 seq_printf(seq, "app direct: %-16llu\n",
875 sbi->rw_iostat[APP_DIRECT_READ_IO]);
876 seq_printf(seq, "app mapped: %-16llu\n",
877 sbi->rw_iostat[APP_MAPPED_READ_IO]);
878
879 /* print fs read IOs */
880 seq_printf(seq, "fs data: %-16llu\n",
881 sbi->rw_iostat[FS_DATA_READ_IO]);
882 seq_printf(seq, "fs gc data: %-16llu\n",
883 sbi->rw_iostat[FS_GDATA_READ_IO]);
884 seq_printf(seq, "fs compr_data: %-16llu\n",
885 sbi->rw_iostat[FS_CDATA_READ_IO]);
886 seq_printf(seq, "fs node: %-16llu\n",
887 sbi->rw_iostat[FS_NODE_READ_IO]);
888 seq_printf(seq, "fs meta: %-16llu\n",
889 sbi->rw_iostat[FS_META_READ_IO]);
890
891 /* print other IOs */
892 seq_puts(seq, "[OTHER]\n");
893 seq_printf(seq, "fs discard: %-16llu\n",
894 sbi->rw_iostat[FS_DISCARD]);
895
896 return 0;
897 }
898
victim_bits_seq_show(struct seq_file * seq,void * offset)899 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
900 void *offset)
901 {
902 struct super_block *sb = seq->private;
903 struct f2fs_sb_info *sbi = F2FS_SB(sb);
904 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
905 int i;
906
907 seq_puts(seq, "format: victim_secmap bitmaps\n");
908
909 for (i = 0; i < MAIN_SECS(sbi); i++) {
910 if ((i % 10) == 0)
911 seq_printf(seq, "%-10d", i);
912 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
913 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
914 seq_putc(seq, '\n');
915 else
916 seq_putc(seq, ' ');
917 }
918 return 0;
919 }
920
f2fs_init_sysfs(void)921 int __init f2fs_init_sysfs(void)
922 {
923 int ret;
924
925 kobject_set_name(&f2fs_kset.kobj, "f2fs");
926 f2fs_kset.kobj.parent = fs_kobj;
927 ret = kset_register(&f2fs_kset);
928 if (ret)
929 return ret;
930
931 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
932 NULL, "features");
933 if (ret) {
934 kobject_put(&f2fs_feat);
935 kset_unregister(&f2fs_kset);
936 } else {
937 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
938 }
939 return ret;
940 }
941
f2fs_exit_sysfs(void)942 void f2fs_exit_sysfs(void)
943 {
944 kobject_put(&f2fs_feat);
945 kset_unregister(&f2fs_kset);
946 remove_proc_entry("fs/f2fs", NULL);
947 f2fs_proc_root = NULL;
948 }
949
f2fs_register_sysfs(struct f2fs_sb_info * sbi)950 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
951 {
952 struct super_block *sb = sbi->sb;
953 int err;
954
955 sbi->s_kobj.kset = &f2fs_kset;
956 init_completion(&sbi->s_kobj_unregister);
957 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
958 "%s", sb->s_id);
959 if (err) {
960 kobject_put(&sbi->s_kobj);
961 wait_for_completion(&sbi->s_kobj_unregister);
962 return err;
963 }
964
965 if (f2fs_proc_root)
966 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
967
968 if (sbi->s_proc) {
969 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
970 segment_info_seq_show, sb);
971 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
972 segment_bits_seq_show, sb);
973 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
974 iostat_info_seq_show, sb);
975 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
976 victim_bits_seq_show, sb);
977 }
978 return 0;
979 }
980
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)981 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
982 {
983 if (sbi->s_proc) {
984 remove_proc_entry("iostat_info", sbi->s_proc);
985 remove_proc_entry("segment_info", sbi->s_proc);
986 remove_proc_entry("segment_bits", sbi->s_proc);
987 remove_proc_entry("victim_bits", sbi->s_proc);
988 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
989 }
990 kobject_del(&sbi->s_kobj);
991 kobject_put(&sbi->s_kobj);
992 wait_for_completion(&sbi->s_kobj_unregister);
993 }
994