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 #include <linux/ioprio.h>
15 #include <linux/sysfs.h>
16
17 #include "f2fs.h"
18 #include "segment.h"
19 #include "gc.h"
20 #include <trace/events/f2fs.h>
21
22 static struct proc_dir_entry *f2fs_proc_root;
23
24 /* Sysfs support for f2fs */
25 enum {
26 GC_THREAD, /* struct f2fs_gc_thread */
27 SM_INFO, /* struct f2fs_sm_info */
28 DCC_INFO, /* struct discard_cmd_control */
29 NM_INFO, /* struct f2fs_nm_info */
30 F2FS_SBI, /* struct f2fs_sb_info */
31 #ifdef CONFIG_F2FS_STAT_FS
32 STAT_INFO, /* struct f2fs_stat_info */
33 #endif
34 #ifdef CONFIG_F2FS_FAULT_INJECTION
35 FAULT_INFO_RATE, /* struct f2fs_fault_info */
36 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
37 #endif
38 RESERVED_BLOCKS, /* struct f2fs_sb_info */
39 CPRC_INFO, /* struct ckpt_req_control */
40 ATGC_INFO, /* struct atgc_management */
41 };
42
43 struct f2fs_attr {
44 struct attribute attr;
45 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
46 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
47 const char *, size_t);
48 int struct_type;
49 int offset;
50 int id;
51 };
52
53 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
54 struct f2fs_sb_info *sbi, char *buf);
55
__struct_ptr(struct f2fs_sb_info * sbi,int struct_type)56 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
57 {
58 if (struct_type == GC_THREAD)
59 return (unsigned char *)sbi->gc_thread;
60 else if (struct_type == SM_INFO)
61 return (unsigned char *)SM_I(sbi);
62 else if (struct_type == DCC_INFO)
63 return (unsigned char *)SM_I(sbi)->dcc_info;
64 else if (struct_type == NM_INFO)
65 return (unsigned char *)NM_I(sbi);
66 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
67 return (unsigned char *)sbi;
68 #ifdef CONFIG_F2FS_FAULT_INJECTION
69 else if (struct_type == FAULT_INFO_RATE ||
70 struct_type == FAULT_INFO_TYPE)
71 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
72 #endif
73 #ifdef CONFIG_F2FS_STAT_FS
74 else if (struct_type == STAT_INFO)
75 return (unsigned char *)F2FS_STAT(sbi);
76 #endif
77 else if (struct_type == CPRC_INFO)
78 return (unsigned char *)&sbi->cprc_info;
79 else if (struct_type == ATGC_INFO)
80 return (unsigned char *)&sbi->am;
81 return NULL;
82 }
83
dirty_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)84 static ssize_t dirty_segments_show(struct f2fs_attr *a,
85 struct f2fs_sb_info *sbi, char *buf)
86 {
87 return sprintf(buf, "%llu\n",
88 (unsigned long long)(dirty_segments(sbi)));
89 }
90
free_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)91 static ssize_t free_segments_show(struct f2fs_attr *a,
92 struct f2fs_sb_info *sbi, char *buf)
93 {
94 return sprintf(buf, "%llu\n",
95 (unsigned long long)(free_segments(sbi)));
96 }
97
ovp_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)98 static ssize_t ovp_segments_show(struct f2fs_attr *a,
99 struct f2fs_sb_info *sbi, char *buf)
100 {
101 return sprintf(buf, "%llu\n",
102 (unsigned long long)(overprovision_segments(sbi)));
103 }
104
lifetime_write_kbytes_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)105 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
106 struct f2fs_sb_info *sbi, char *buf)
107 {
108 return sprintf(buf, "%llu\n",
109 (unsigned long long)(sbi->kbytes_written +
110 ((f2fs_get_sectors_written(sbi) -
111 sbi->sectors_written_start) >> 1)));
112 }
113
sb_status_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)114 static ssize_t sb_status_show(struct f2fs_attr *a,
115 struct f2fs_sb_info *sbi, char *buf)
116 {
117 return sprintf(buf, "%lx\n", sbi->s_flag);
118 }
119
pending_discard_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)120 static ssize_t pending_discard_show(struct f2fs_attr *a,
121 struct f2fs_sb_info *sbi, char *buf)
122 {
123 if (!SM_I(sbi)->dcc_info)
124 return -EINVAL;
125 return sprintf(buf, "%llu\n", (unsigned long long)atomic_read(
126 &SM_I(sbi)->dcc_info->discard_cmd_cnt));
127 }
128
features_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)129 static ssize_t features_show(struct f2fs_attr *a,
130 struct f2fs_sb_info *sbi, char *buf)
131 {
132 int len = 0;
133
134 if (f2fs_sb_has_encrypt(sbi))
135 len += scnprintf(buf, PAGE_SIZE - len, "%s",
136 "encryption");
137 if (f2fs_sb_has_blkzoned(sbi))
138 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
139 len ? ", " : "", "blkzoned");
140 if (f2fs_sb_has_extra_attr(sbi))
141 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
142 len ? ", " : "", "extra_attr");
143 if (f2fs_sb_has_project_quota(sbi))
144 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
145 len ? ", " : "", "projquota");
146 if (f2fs_sb_has_inode_chksum(sbi))
147 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
148 len ? ", " : "", "inode_checksum");
149 if (f2fs_sb_has_flexible_inline_xattr(sbi))
150 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
151 len ? ", " : "", "flexible_inline_xattr");
152 if (f2fs_sb_has_quota_ino(sbi))
153 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
154 len ? ", " : "", "quota_ino");
155 if (f2fs_sb_has_inode_crtime(sbi))
156 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
157 len ? ", " : "", "inode_crtime");
158 if (f2fs_sb_has_lost_found(sbi))
159 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
160 len ? ", " : "", "lost_found");
161 if (f2fs_sb_has_verity(sbi))
162 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
163 len ? ", " : "", "verity");
164 if (f2fs_sb_has_sb_chksum(sbi))
165 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
166 len ? ", " : "", "sb_checksum");
167 if (f2fs_sb_has_casefold(sbi))
168 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
169 len ? ", " : "", "casefold");
170 if (f2fs_sb_has_readonly(sbi))
171 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
172 len ? ", " : "", "readonly");
173 if (f2fs_sb_has_compression(sbi))
174 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
175 len ? ", " : "", "compression");
176 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
177 len ? ", " : "", "pin_file");
178 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
179 return len;
180 }
181
current_reserved_blocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)182 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
183 struct f2fs_sb_info *sbi, char *buf)
184 {
185 return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
186 }
187
unusable_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)188 static ssize_t unusable_show(struct f2fs_attr *a,
189 struct f2fs_sb_info *sbi, char *buf)
190 {
191 block_t unusable;
192
193 if (test_opt(sbi, DISABLE_CHECKPOINT))
194 unusable = sbi->unusable_block_count;
195 else
196 unusable = f2fs_get_unusable_blocks(sbi);
197 return sprintf(buf, "%llu\n", (unsigned long long)unusable);
198 }
199
encoding_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)200 static ssize_t encoding_show(struct f2fs_attr *a,
201 struct f2fs_sb_info *sbi, char *buf)
202 {
203 #ifdef CONFIG_UNICODE
204 struct super_block *sb = sbi->sb;
205
206 if (f2fs_sb_has_casefold(sbi))
207 return snprintf(buf, PAGE_SIZE, "%s (%d.%d.%d)\n",
208 sb->s_encoding->charset,
209 (sb->s_encoding->version >> 16) & 0xff,
210 (sb->s_encoding->version >> 8) & 0xff,
211 sb->s_encoding->version & 0xff);
212 #endif
213 return sprintf(buf, "(none)");
214 }
215
mounted_time_sec_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)216 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
217 struct f2fs_sb_info *sbi, char *buf)
218 {
219 return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
220 }
221
222 #ifdef CONFIG_F2FS_STAT_FS
moved_blocks_foreground_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)223 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
224 struct f2fs_sb_info *sbi, char *buf)
225 {
226 struct f2fs_stat_info *si = F2FS_STAT(sbi);
227
228 return sprintf(buf, "%llu\n",
229 (unsigned long long)(si->tot_blks -
230 (si->bg_data_blks + si->bg_node_blks)));
231 }
232
moved_blocks_background_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)233 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
234 struct f2fs_sb_info *sbi, char *buf)
235 {
236 struct f2fs_stat_info *si = F2FS_STAT(sbi);
237
238 return sprintf(buf, "%llu\n",
239 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
240 }
241
avg_vblocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)242 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
243 struct f2fs_sb_info *sbi, char *buf)
244 {
245 struct f2fs_stat_info *si = F2FS_STAT(sbi);
246
247 si->dirty_count = dirty_segments(sbi);
248 f2fs_update_sit_info(sbi);
249 return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
250 }
251 #endif
252
main_blkaddr_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)253 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
254 struct f2fs_sb_info *sbi, char *buf)
255 {
256 return snprintf(buf, PAGE_SIZE, "%llu\n",
257 (unsigned long long)MAIN_BLKADDR(sbi));
258 }
259
f2fs_sbi_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)260 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
261 struct f2fs_sb_info *sbi, char *buf)
262 {
263 unsigned char *ptr = NULL;
264 unsigned int *ui;
265
266 ptr = __struct_ptr(sbi, a->struct_type);
267 if (!ptr)
268 return -EINVAL;
269
270 if (!strcmp(a->attr.name, "extension_list")) {
271 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
272 sbi->raw_super->extension_list;
273 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
274 int hot_count = sbi->raw_super->hot_ext_count;
275 int len = 0, i;
276
277 len += scnprintf(buf + len, PAGE_SIZE - len,
278 "cold file extension:\n");
279 for (i = 0; i < cold_count; i++)
280 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
281 extlist[i]);
282
283 len += scnprintf(buf + len, PAGE_SIZE - len,
284 "hot file extension:\n");
285 for (i = cold_count; i < cold_count + hot_count; i++)
286 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
287 extlist[i]);
288 return len;
289 }
290
291 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
292 struct ckpt_req_control *cprc = &sbi->cprc_info;
293 int len = 0;
294 int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
295 int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
296
297 if (class == IOPRIO_CLASS_RT)
298 len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
299 else if (class == IOPRIO_CLASS_BE)
300 len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
301 else
302 return -EINVAL;
303
304 len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
305 return len;
306 }
307
308 #ifdef CONFIG_F2FS_FS_COMPRESSION
309 if (!strcmp(a->attr.name, "compr_written_block"))
310 return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
311
312 if (!strcmp(a->attr.name, "compr_saved_block"))
313 return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
314
315 if (!strcmp(a->attr.name, "compr_new_inode"))
316 return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
317 #endif
318
319 if (!strcmp(a->attr.name, "gc_segment_mode"))
320 return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
321
322 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
323 return sysfs_emit(buf, "%u\n",
324 sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
325 }
326
327 ui = (unsigned int *)(ptr + a->offset);
328
329 return sprintf(buf, "%u\n", *ui);
330 }
331
__sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)332 static ssize_t __sbi_store(struct f2fs_attr *a,
333 struct f2fs_sb_info *sbi,
334 const char *buf, size_t count)
335 {
336 unsigned char *ptr;
337 unsigned long t;
338 unsigned int *ui;
339 ssize_t ret;
340
341 ptr = __struct_ptr(sbi, a->struct_type);
342 if (!ptr)
343 return -EINVAL;
344
345 if (!strcmp(a->attr.name, "extension_list")) {
346 const char *name = strim((char *)buf);
347 bool set = true, hot;
348
349 if (!strncmp(name, "[h]", 3))
350 hot = true;
351 else if (!strncmp(name, "[c]", 3))
352 hot = false;
353 else
354 return -EINVAL;
355
356 name += 3;
357
358 if (*name == '!') {
359 name++;
360 set = false;
361 }
362
363 if (strlen(name) >= F2FS_EXTENSION_LEN)
364 return -EINVAL;
365
366 f2fs_down_write(&sbi->sb_lock);
367
368 ret = f2fs_update_extension_list(sbi, name, hot, set);
369 if (ret)
370 goto out;
371
372 ret = f2fs_commit_super(sbi, false);
373 if (ret)
374 f2fs_update_extension_list(sbi, name, hot, !set);
375 out:
376 f2fs_up_write(&sbi->sb_lock);
377 return ret ? ret : count;
378 }
379
380 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
381 const char *name = strim((char *)buf);
382 struct ckpt_req_control *cprc = &sbi->cprc_info;
383 int class;
384 long data;
385 int ret;
386
387 if (!strncmp(name, "rt,", 3))
388 class = IOPRIO_CLASS_RT;
389 else if (!strncmp(name, "be,", 3))
390 class = IOPRIO_CLASS_BE;
391 else
392 return -EINVAL;
393
394 name += 3;
395 ret = kstrtol(name, 10, &data);
396 if (ret)
397 return ret;
398 if (data >= IOPRIO_BE_NR || data < 0)
399 return -EINVAL;
400
401 cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
402 if (test_opt(sbi, MERGE_CHECKPOINT)) {
403 ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
404 cprc->ckpt_thread_ioprio);
405 if (ret)
406 return ret;
407 }
408
409 return count;
410 }
411
412 ui = (unsigned int *)(ptr + a->offset);
413
414 ret = kstrtoul(skip_spaces(buf), 0, &t);
415 if (ret < 0)
416 return ret;
417 #ifdef CONFIG_F2FS_FAULT_INJECTION
418 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
419 return -EINVAL;
420 if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
421 return -EINVAL;
422 #endif
423 if (a->struct_type == RESERVED_BLOCKS) {
424 spin_lock(&sbi->stat_lock);
425 if (t > (unsigned long)(sbi->user_block_count -
426 F2FS_OPTION(sbi).root_reserved_blocks -
427 sbi->blocks_per_seg *
428 SM_I(sbi)->additional_reserved_segments)) {
429 spin_unlock(&sbi->stat_lock);
430 return -EINVAL;
431 }
432 *ui = t;
433 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
434 sbi->user_block_count - valid_user_blocks(sbi));
435 spin_unlock(&sbi->stat_lock);
436 return count;
437 }
438
439 if (!strcmp(a->attr.name, "discard_granularity")) {
440 if (t == 0 || t > MAX_PLIST_NUM)
441 return -EINVAL;
442 if (t == *ui)
443 return count;
444 *ui = t;
445 return count;
446 }
447
448 if (!strcmp(a->attr.name, "migration_granularity")) {
449 if (t == 0 || t > sbi->segs_per_sec)
450 return -EINVAL;
451 }
452
453 if (!strcmp(a->attr.name, "trim_sections"))
454 return -EINVAL;
455
456 if (!strcmp(a->attr.name, "gc_urgent")) {
457 if (t == 0) {
458 sbi->gc_mode = GC_NORMAL;
459 } else if (t == 1) {
460 sbi->gc_mode = GC_URGENT_HIGH;
461 if (sbi->gc_thread) {
462 sbi->gc_thread->gc_wake = 1;
463 wake_up_interruptible_all(
464 &sbi->gc_thread->gc_wait_queue_head);
465 wake_up_discard_thread(sbi, true);
466 }
467 } else if (t == 2) {
468 sbi->gc_mode = GC_URGENT_LOW;
469 } else if (t == 3) {
470 sbi->gc_mode = GC_URGENT_MID;
471 if (sbi->gc_thread) {
472 sbi->gc_thread->gc_wake = 1;
473 wake_up_interruptible_all(
474 &sbi->gc_thread->gc_wait_queue_head);
475 }
476 } else {
477 return -EINVAL;
478 }
479 return count;
480 }
481 if (!strcmp(a->attr.name, "gc_idle")) {
482 if (t == GC_IDLE_CB) {
483 sbi->gc_mode = GC_IDLE_CB;
484 } else if (t == GC_IDLE_GREEDY) {
485 sbi->gc_mode = GC_IDLE_GREEDY;
486 } else if (t == GC_IDLE_AT) {
487 if (!sbi->am.atgc_enabled)
488 return -EINVAL;
489 sbi->gc_mode = GC_IDLE_AT;
490 } else {
491 sbi->gc_mode = GC_NORMAL;
492 }
493 return count;
494 }
495
496 if (!strcmp(a->attr.name, "iostat_enable")) {
497 sbi->iostat_enable = !!t;
498 if (!sbi->iostat_enable)
499 f2fs_reset_iostat(sbi);
500 return count;
501 }
502
503 if (!strcmp(a->attr.name, "iostat_period_ms")) {
504 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
505 return -EINVAL;
506 spin_lock_irq(&sbi->iostat_lock);
507 sbi->iostat_period_ms = (unsigned int)t;
508 spin_unlock_irq(&sbi->iostat_lock);
509 return count;
510 }
511
512 #ifdef CONFIG_F2FS_FS_COMPRESSION
513 if (!strcmp(a->attr.name, "compr_written_block") ||
514 !strcmp(a->attr.name, "compr_saved_block")) {
515 if (t != 0)
516 return -EINVAL;
517 sbi->compr_written_block = 0;
518 sbi->compr_saved_block = 0;
519 return count;
520 }
521
522 if (!strcmp(a->attr.name, "compr_new_inode")) {
523 if (t != 0)
524 return -EINVAL;
525 sbi->compr_new_inode = 0;
526 return count;
527 }
528 #endif
529
530 if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
531 if (t > 100)
532 return -EINVAL;
533 sbi->am.candidate_ratio = t;
534 return count;
535 }
536
537 if (!strcmp(a->attr.name, "atgc_age_weight")) {
538 if (t > 100)
539 return -EINVAL;
540 sbi->am.age_weight = t;
541 return count;
542 }
543
544 if (!strcmp(a->attr.name, "gc_segment_mode")) {
545 if (t < MAX_GC_MODE)
546 sbi->gc_segment_mode = t;
547 else
548 return -EINVAL;
549 return count;
550 }
551
552 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
553 if (t != 0)
554 return -EINVAL;
555 sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
556 return count;
557 }
558
559 if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
560 if (t == 0 || t >= sbi->warm_data_age_threshold)
561 return -EINVAL;
562 if (t == *ui)
563 return count;
564 *ui = (unsigned int)t;
565 return count;
566 }
567
568 if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
569 if (t == 0 || t <= sbi->hot_data_age_threshold)
570 return -EINVAL;
571 if (t == *ui)
572 return count;
573 *ui = (unsigned int)t;
574 return count;
575 }
576
577 if (!strcmp(a->attr.name, "last_age_weight")) {
578 if (t > 100)
579 return -EINVAL;
580 if (t == *ui)
581 return count;
582 *ui = (unsigned int)t;
583 return count;
584 }
585
586 *ui = (unsigned int)t;
587
588 return count;
589 }
590
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)591 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
592 struct f2fs_sb_info *sbi,
593 const char *buf, size_t count)
594 {
595 ssize_t ret;
596 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
597 a->struct_type == GC_THREAD);
598
599 if (gc_entry) {
600 if (!down_read_trylock(&sbi->sb->s_umount))
601 return -EAGAIN;
602 }
603 ret = __sbi_store(a, sbi, buf, count);
604 if (gc_entry)
605 up_read(&sbi->sb->s_umount);
606
607 return ret;
608 }
609
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)610 static ssize_t f2fs_attr_show(struct kobject *kobj,
611 struct attribute *attr, char *buf)
612 {
613 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
614 s_kobj);
615 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
616
617 return a->show ? a->show(a, sbi, buf) : 0;
618 }
619
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)620 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
621 const char *buf, size_t len)
622 {
623 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
624 s_kobj);
625 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
626
627 return a->store ? a->store(a, sbi, buf, len) : 0;
628 }
629
f2fs_sb_release(struct kobject * kobj)630 static void f2fs_sb_release(struct kobject *kobj)
631 {
632 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
633 s_kobj);
634 complete(&sbi->s_kobj_unregister);
635 }
636
637 /*
638 * Note that there are three feature list entries:
639 * 1) /sys/fs/f2fs/features
640 * : shows runtime features supported by in-kernel f2fs along with Kconfig.
641 * - ref. F2FS_FEATURE_RO_ATTR()
642 *
643 * 2) /sys/fs/f2fs/$s_id/features <deprecated>
644 * : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
645 * won't add new feature anymore, and thus, users should check entries in 3)
646 * instead of this 2).
647 *
648 * 3) /sys/fs/f2fs/$s_id/feature_list
649 * : shows on-disk features enabled by mkfs.f2fs per instance, which follows
650 * sysfs entry rule where each entry should expose single value.
651 * This list covers old feature list provided by 2) and beyond. Therefore,
652 * please add new on-disk feature in this list only.
653 * - ref. F2FS_SB_FEATURE_RO_ATTR()
654 */
f2fs_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)655 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
656 struct f2fs_sb_info *sbi, char *buf)
657 {
658 return sprintf(buf, "supported\n");
659 }
660
661 #define F2FS_FEATURE_RO_ATTR(_name) \
662 static struct f2fs_attr f2fs_attr_##_name = { \
663 .attr = {.name = __stringify(_name), .mode = 0444 }, \
664 .show = f2fs_feature_show, \
665 }
666
f2fs_sb_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)667 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
668 struct f2fs_sb_info *sbi, char *buf)
669 {
670 if (F2FS_HAS_FEATURE(sbi, a->id))
671 return sprintf(buf, "supported\n");
672 return sprintf(buf, "unsupported\n");
673 }
674
675 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat) \
676 static struct f2fs_attr f2fs_attr_sb_##_name = { \
677 .attr = {.name = __stringify(_name), .mode = 0444 }, \
678 .show = f2fs_sb_feature_show, \
679 .id = F2FS_FEATURE_##_feat, \
680 }
681
682 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
683 static struct f2fs_attr f2fs_attr_##_name = { \
684 .attr = {.name = __stringify(_name), .mode = _mode }, \
685 .show = _show, \
686 .store = _store, \
687 .struct_type = _struct_type, \
688 .offset = _offset \
689 }
690
691 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
692 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
693 f2fs_sbi_show, f2fs_sbi_store, \
694 offsetof(struct struct_name, elname))
695
696 #define F2FS_GENERAL_RO_ATTR(name) \
697 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
698
699 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname) \
700 static struct f2fs_attr f2fs_attr_##_name = { \
701 .attr = {.name = __stringify(_name), .mode = 0444 }, \
702 .show = f2fs_sbi_show, \
703 .struct_type = _struct_type, \
704 .offset = offsetof(struct _struct_name, _elname), \
705 }
706
707 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
708 urgent_sleep_time);
709 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
710 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
711 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
712 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
713 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
714 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
715 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
716 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
717 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
718 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
719 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
720 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
721 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
722 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
723 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
724 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
725 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
726 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
727 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
728 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
729 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
730 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
731 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
732 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
733 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
734 interval_time[DISCARD_TIME]);
735 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
736 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
737 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
738 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
739 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
740 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
741 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
742 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
743 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
744 #ifdef CONFIG_F2FS_FAULT_INJECTION
745 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
746 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
747 #endif
748 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
749 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
750 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
751 F2FS_GENERAL_RO_ATTR(dirty_segments);
752 F2FS_GENERAL_RO_ATTR(free_segments);
753 F2FS_GENERAL_RO_ATTR(ovp_segments);
754 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
755 F2FS_GENERAL_RO_ATTR(features);
756 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
757 F2FS_GENERAL_RO_ATTR(unusable);
758 F2FS_GENERAL_RO_ATTR(encoding);
759 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
760 F2FS_GENERAL_RO_ATTR(main_blkaddr);
761 F2FS_GENERAL_RO_ATTR(pending_discard);
762 #ifdef CONFIG_F2FS_STAT_FS
763 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
764 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
765 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
766 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
767 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
768 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
769 F2FS_GENERAL_RO_ATTR(avg_vblocks);
770 #endif
771
772 #ifdef CONFIG_FS_ENCRYPTION
773 F2FS_FEATURE_RO_ATTR(encryption);
774 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
775 #ifdef CONFIG_UNICODE
776 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
777 #endif
778 #endif /* CONFIG_FS_ENCRYPTION */
779 #ifdef CONFIG_BLK_DEV_ZONED
780 F2FS_FEATURE_RO_ATTR(block_zoned);
781 #endif
782 F2FS_FEATURE_RO_ATTR(atomic_write);
783 F2FS_FEATURE_RO_ATTR(extra_attr);
784 F2FS_FEATURE_RO_ATTR(project_quota);
785 F2FS_FEATURE_RO_ATTR(inode_checksum);
786 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
787 F2FS_FEATURE_RO_ATTR(quota_ino);
788 F2FS_FEATURE_RO_ATTR(inode_crtime);
789 F2FS_FEATURE_RO_ATTR(lost_found);
790 #ifdef CONFIG_FS_VERITY
791 F2FS_FEATURE_RO_ATTR(verity);
792 #endif
793 F2FS_FEATURE_RO_ATTR(sb_checksum);
794 #ifdef CONFIG_UNICODE
795 F2FS_FEATURE_RO_ATTR(casefold);
796 #endif
797 F2FS_FEATURE_RO_ATTR(readonly);
798 #ifdef CONFIG_F2FS_FS_COMPRESSION
799 F2FS_FEATURE_RO_ATTR(compression);
800 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_written_block, compr_written_block);
801 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_saved_block, compr_saved_block);
802 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_new_inode, compr_new_inode);
803 #endif
804 F2FS_FEATURE_RO_ATTR(pin_file);
805
806 /* For ATGC */
807 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_ratio, candidate_ratio);
808 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_count, max_candidate_count);
809 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_weight, age_weight);
810 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
811
812 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
813 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
814
815 /* For block age extent cache */
816 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, hot_data_age_threshold, hot_data_age_threshold);
817 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, warm_data_age_threshold, warm_data_age_threshold);
818 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, last_age_weight, last_age_weight);
819
820 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
821 static struct attribute *f2fs_attrs[] = {
822 ATTR_LIST(gc_urgent_sleep_time),
823 ATTR_LIST(gc_min_sleep_time),
824 ATTR_LIST(gc_max_sleep_time),
825 ATTR_LIST(gc_no_gc_sleep_time),
826 ATTR_LIST(gc_idle),
827 ATTR_LIST(gc_urgent),
828 ATTR_LIST(reclaim_segments),
829 ATTR_LIST(main_blkaddr),
830 ATTR_LIST(max_small_discards),
831 ATTR_LIST(discard_granularity),
832 ATTR_LIST(pending_discard),
833 ATTR_LIST(batched_trim_sections),
834 ATTR_LIST(ipu_policy),
835 ATTR_LIST(min_ipu_util),
836 ATTR_LIST(min_fsync_blocks),
837 ATTR_LIST(min_seq_blocks),
838 ATTR_LIST(min_hot_blocks),
839 ATTR_LIST(min_ssr_sections),
840 ATTR_LIST(max_victim_search),
841 ATTR_LIST(migration_granularity),
842 ATTR_LIST(dir_level),
843 ATTR_LIST(ram_thresh),
844 ATTR_LIST(ra_nid_pages),
845 ATTR_LIST(dirty_nats_ratio),
846 ATTR_LIST(cp_interval),
847 ATTR_LIST(idle_interval),
848 ATTR_LIST(discard_idle_interval),
849 ATTR_LIST(gc_idle_interval),
850 ATTR_LIST(umount_discard_timeout),
851 ATTR_LIST(iostat_enable),
852 ATTR_LIST(iostat_period_ms),
853 ATTR_LIST(readdir_ra),
854 ATTR_LIST(max_io_bytes),
855 ATTR_LIST(gc_pin_file_thresh),
856 ATTR_LIST(extension_list),
857 #ifdef CONFIG_F2FS_FAULT_INJECTION
858 ATTR_LIST(inject_rate),
859 ATTR_LIST(inject_type),
860 #endif
861 ATTR_LIST(data_io_flag),
862 ATTR_LIST(node_io_flag),
863 ATTR_LIST(ckpt_thread_ioprio),
864 ATTR_LIST(dirty_segments),
865 ATTR_LIST(free_segments),
866 ATTR_LIST(ovp_segments),
867 ATTR_LIST(unusable),
868 ATTR_LIST(lifetime_write_kbytes),
869 ATTR_LIST(features),
870 ATTR_LIST(reserved_blocks),
871 ATTR_LIST(current_reserved_blocks),
872 ATTR_LIST(encoding),
873 ATTR_LIST(mounted_time_sec),
874 #ifdef CONFIG_F2FS_STAT_FS
875 ATTR_LIST(cp_foreground_calls),
876 ATTR_LIST(cp_background_calls),
877 ATTR_LIST(gc_foreground_calls),
878 ATTR_LIST(gc_background_calls),
879 ATTR_LIST(moved_blocks_foreground),
880 ATTR_LIST(moved_blocks_background),
881 ATTR_LIST(avg_vblocks),
882 #endif
883 #ifdef CONFIG_F2FS_FS_COMPRESSION
884 ATTR_LIST(compr_written_block),
885 ATTR_LIST(compr_saved_block),
886 ATTR_LIST(compr_new_inode),
887 #endif
888 /* For ATGC */
889 ATTR_LIST(atgc_candidate_ratio),
890 ATTR_LIST(atgc_candidate_count),
891 ATTR_LIST(atgc_age_weight),
892 ATTR_LIST(atgc_age_threshold),
893 ATTR_LIST(gc_segment_mode),
894 ATTR_LIST(gc_reclaimed_segments),
895 ATTR_LIST(hot_data_age_threshold),
896 ATTR_LIST(warm_data_age_threshold),
897 ATTR_LIST(last_age_weight),
898 NULL,
899 };
900 ATTRIBUTE_GROUPS(f2fs);
901
902 static struct attribute *f2fs_feat_attrs[] = {
903 #ifdef CONFIG_FS_ENCRYPTION
904 ATTR_LIST(encryption),
905 ATTR_LIST(test_dummy_encryption_v2),
906 #ifdef CONFIG_UNICODE
907 ATTR_LIST(encrypted_casefold),
908 #endif
909 #endif /* CONFIG_FS_ENCRYPTION */
910 #ifdef CONFIG_BLK_DEV_ZONED
911 ATTR_LIST(block_zoned),
912 #endif
913 ATTR_LIST(atomic_write),
914 ATTR_LIST(extra_attr),
915 ATTR_LIST(project_quota),
916 ATTR_LIST(inode_checksum),
917 ATTR_LIST(flexible_inline_xattr),
918 ATTR_LIST(quota_ino),
919 ATTR_LIST(inode_crtime),
920 ATTR_LIST(lost_found),
921 #ifdef CONFIG_FS_VERITY
922 ATTR_LIST(verity),
923 #endif
924 ATTR_LIST(sb_checksum),
925 #ifdef CONFIG_UNICODE
926 ATTR_LIST(casefold),
927 #endif
928 ATTR_LIST(readonly),
929 #ifdef CONFIG_F2FS_FS_COMPRESSION
930 ATTR_LIST(compression),
931 #endif
932 ATTR_LIST(pin_file),
933 NULL,
934 };
935 ATTRIBUTE_GROUPS(f2fs_feat);
936
937 F2FS_GENERAL_RO_ATTR(sb_status);
938 static struct attribute *f2fs_stat_attrs[] = {
939 ATTR_LIST(sb_status),
940 NULL,
941 };
942 ATTRIBUTE_GROUPS(f2fs_stat);
943
944 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
945 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
946 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
947 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
948 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
949 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
950 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
951 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
952 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
953 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
954 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
955 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
956 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
957 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
958
959 static struct attribute *f2fs_sb_feat_attrs[] = {
960 ATTR_LIST(sb_encryption),
961 ATTR_LIST(sb_block_zoned),
962 ATTR_LIST(sb_extra_attr),
963 ATTR_LIST(sb_project_quota),
964 ATTR_LIST(sb_inode_checksum),
965 ATTR_LIST(sb_flexible_inline_xattr),
966 ATTR_LIST(sb_quota_ino),
967 ATTR_LIST(sb_inode_crtime),
968 ATTR_LIST(sb_lost_found),
969 ATTR_LIST(sb_verity),
970 ATTR_LIST(sb_sb_checksum),
971 ATTR_LIST(sb_casefold),
972 ATTR_LIST(sb_compression),
973 ATTR_LIST(sb_readonly),
974 NULL,
975 };
976 ATTRIBUTE_GROUPS(f2fs_sb_feat);
977
978 static const struct sysfs_ops f2fs_attr_ops = {
979 .show = f2fs_attr_show,
980 .store = f2fs_attr_store,
981 };
982
983 static struct kobj_type f2fs_sb_ktype = {
984 .default_groups = f2fs_groups,
985 .sysfs_ops = &f2fs_attr_ops,
986 .release = f2fs_sb_release,
987 };
988
989 static struct kobj_type f2fs_ktype = {
990 .sysfs_ops = &f2fs_attr_ops,
991 };
992
993 static struct kset f2fs_kset = {
994 .kobj = {.ktype = &f2fs_ktype},
995 };
996
997 static struct kobj_type f2fs_feat_ktype = {
998 .default_groups = f2fs_feat_groups,
999 .sysfs_ops = &f2fs_attr_ops,
1000 };
1001
1002 static struct kobject f2fs_feat = {
1003 .kset = &f2fs_kset,
1004 };
1005
f2fs_stat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1006 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1007 struct attribute *attr, char *buf)
1008 {
1009 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1010 s_stat_kobj);
1011 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1012
1013 return a->show ? a->show(a, sbi, buf) : 0;
1014 }
1015
f2fs_stat_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)1016 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1017 const char *buf, size_t len)
1018 {
1019 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1020 s_stat_kobj);
1021 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1022
1023 return a->store ? a->store(a, sbi, buf, len) : 0;
1024 }
1025
f2fs_stat_kobj_release(struct kobject * kobj)1026 static void f2fs_stat_kobj_release(struct kobject *kobj)
1027 {
1028 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1029 s_stat_kobj);
1030 complete(&sbi->s_stat_kobj_unregister);
1031 }
1032
1033 static const struct sysfs_ops f2fs_stat_attr_ops = {
1034 .show = f2fs_stat_attr_show,
1035 .store = f2fs_stat_attr_store,
1036 };
1037
1038 static struct kobj_type f2fs_stat_ktype = {
1039 .default_groups = f2fs_stat_groups,
1040 .sysfs_ops = &f2fs_stat_attr_ops,
1041 .release = f2fs_stat_kobj_release,
1042 };
1043
f2fs_sb_feat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1044 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1045 struct attribute *attr, char *buf)
1046 {
1047 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1048 s_feature_list_kobj);
1049 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1050
1051 return a->show ? a->show(a, sbi, buf) : 0;
1052 }
1053
f2fs_feature_list_kobj_release(struct kobject * kobj)1054 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1055 {
1056 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1057 s_feature_list_kobj);
1058 complete(&sbi->s_feature_list_kobj_unregister);
1059 }
1060
1061 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1062 .show = f2fs_sb_feat_attr_show,
1063 };
1064
1065 static struct kobj_type f2fs_feature_list_ktype = {
1066 .default_groups = f2fs_sb_feat_groups,
1067 .sysfs_ops = &f2fs_feature_list_attr_ops,
1068 .release = f2fs_feature_list_kobj_release,
1069 };
1070
segment_info_seq_show(struct seq_file * seq,void * offset)1071 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1072 void *offset)
1073 {
1074 struct super_block *sb = seq->private;
1075 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1076 unsigned int total_segs =
1077 le32_to_cpu(sbi->raw_super->segment_count_main);
1078 int i;
1079
1080 seq_puts(seq, "format: segment_type|valid_blocks\n"
1081 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1082
1083 for (i = 0; i < total_segs; i++) {
1084 struct seg_entry *se = get_seg_entry(sbi, i);
1085
1086 if ((i % 10) == 0)
1087 seq_printf(seq, "%-10d", i);
1088 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1089 if ((i % 10) == 9 || i == (total_segs - 1))
1090 seq_putc(seq, '\n');
1091 else
1092 seq_putc(seq, ' ');
1093 }
1094
1095 return 0;
1096 }
1097
segment_bits_seq_show(struct seq_file * seq,void * offset)1098 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1099 void *offset)
1100 {
1101 struct super_block *sb = seq->private;
1102 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1103 unsigned int total_segs =
1104 le32_to_cpu(sbi->raw_super->segment_count_main);
1105 int i, j;
1106
1107 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1108 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1109
1110 for (i = 0; i < total_segs; i++) {
1111 struct seg_entry *se = get_seg_entry(sbi, i);
1112
1113 seq_printf(seq, "%-10d", i);
1114 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1115 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1116 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1117 seq_putc(seq, '\n');
1118 }
1119 return 0;
1120 }
1121
f2fs_record_iostat(struct f2fs_sb_info * sbi)1122 void f2fs_record_iostat(struct f2fs_sb_info *sbi)
1123 {
1124 unsigned long long iostat_diff[NR_IO_TYPE];
1125 int i;
1126
1127 if (time_is_after_jiffies(sbi->iostat_next_period))
1128 return;
1129
1130 /* Need double check under the lock */
1131 spin_lock(&sbi->iostat_lock);
1132 if (time_is_after_jiffies(sbi->iostat_next_period)) {
1133 spin_unlock(&sbi->iostat_lock);
1134 return;
1135 }
1136 sbi->iostat_next_period = jiffies +
1137 msecs_to_jiffies(sbi->iostat_period_ms);
1138
1139 for (i = 0; i < NR_IO_TYPE; i++) {
1140 iostat_diff[i] = sbi->rw_iostat[i] -
1141 sbi->prev_rw_iostat[i];
1142 sbi->prev_rw_iostat[i] = sbi->rw_iostat[i];
1143 }
1144 spin_unlock(&sbi->iostat_lock);
1145
1146 trace_f2fs_iostat(sbi, iostat_diff);
1147 }
1148
iostat_info_seq_show(struct seq_file * seq,void * offset)1149 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
1150 void *offset)
1151 {
1152 struct super_block *sb = seq->private;
1153 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1154 time64_t now = ktime_get_real_seconds();
1155
1156 if (!sbi->iostat_enable)
1157 return 0;
1158
1159 seq_printf(seq, "time: %-16llu\n", now);
1160
1161 /* print app write IOs */
1162 seq_puts(seq, "[WRITE]\n");
1163 seq_printf(seq, "app buffered: %-16llu\n",
1164 sbi->rw_iostat[APP_BUFFERED_IO]);
1165 seq_printf(seq, "app direct: %-16llu\n",
1166 sbi->rw_iostat[APP_DIRECT_IO]);
1167 seq_printf(seq, "app mapped: %-16llu\n",
1168 sbi->rw_iostat[APP_MAPPED_IO]);
1169
1170 /* print fs write IOs */
1171 seq_printf(seq, "fs data: %-16llu\n",
1172 sbi->rw_iostat[FS_DATA_IO]);
1173 seq_printf(seq, "fs node: %-16llu\n",
1174 sbi->rw_iostat[FS_NODE_IO]);
1175 seq_printf(seq, "fs meta: %-16llu\n",
1176 sbi->rw_iostat[FS_META_IO]);
1177 seq_printf(seq, "fs gc data: %-16llu\n",
1178 sbi->rw_iostat[FS_GC_DATA_IO]);
1179 seq_printf(seq, "fs gc node: %-16llu\n",
1180 sbi->rw_iostat[FS_GC_NODE_IO]);
1181 seq_printf(seq, "fs cp data: %-16llu\n",
1182 sbi->rw_iostat[FS_CP_DATA_IO]);
1183 seq_printf(seq, "fs cp node: %-16llu\n",
1184 sbi->rw_iostat[FS_CP_NODE_IO]);
1185 seq_printf(seq, "fs cp meta: %-16llu\n",
1186 sbi->rw_iostat[FS_CP_META_IO]);
1187
1188 /* print app read IOs */
1189 seq_puts(seq, "[READ]\n");
1190 seq_printf(seq, "app buffered: %-16llu\n",
1191 sbi->rw_iostat[APP_BUFFERED_READ_IO]);
1192 seq_printf(seq, "app direct: %-16llu\n",
1193 sbi->rw_iostat[APP_DIRECT_READ_IO]);
1194 seq_printf(seq, "app mapped: %-16llu\n",
1195 sbi->rw_iostat[APP_MAPPED_READ_IO]);
1196
1197 /* print fs read IOs */
1198 seq_printf(seq, "fs data: %-16llu\n",
1199 sbi->rw_iostat[FS_DATA_READ_IO]);
1200 seq_printf(seq, "fs gc data: %-16llu\n",
1201 sbi->rw_iostat[FS_GDATA_READ_IO]);
1202 seq_printf(seq, "fs compr_data: %-16llu\n",
1203 sbi->rw_iostat[FS_CDATA_READ_IO]);
1204 seq_printf(seq, "fs node: %-16llu\n",
1205 sbi->rw_iostat[FS_NODE_READ_IO]);
1206 seq_printf(seq, "fs meta: %-16llu\n",
1207 sbi->rw_iostat[FS_META_READ_IO]);
1208
1209 /* print other IOs */
1210 seq_puts(seq, "[OTHER]\n");
1211 seq_printf(seq, "fs discard: %-16llu\n",
1212 sbi->rw_iostat[FS_DISCARD]);
1213
1214 return 0;
1215 }
1216
victim_bits_seq_show(struct seq_file * seq,void * offset)1217 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1218 void *offset)
1219 {
1220 struct super_block *sb = seq->private;
1221 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1222 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1223 int i;
1224
1225 seq_puts(seq, "format: victim_secmap bitmaps\n");
1226
1227 for (i = 0; i < MAIN_SECS(sbi); i++) {
1228 if ((i % 10) == 0)
1229 seq_printf(seq, "%-10d", i);
1230 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1231 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1232 seq_putc(seq, '\n');
1233 else
1234 seq_putc(seq, ' ');
1235 }
1236 return 0;
1237 }
1238
f2fs_init_sysfs(void)1239 int __init f2fs_init_sysfs(void)
1240 {
1241 int ret;
1242
1243 kobject_set_name(&f2fs_kset.kobj, "f2fs");
1244 f2fs_kset.kobj.parent = fs_kobj;
1245 ret = kset_register(&f2fs_kset);
1246 if (ret)
1247 return ret;
1248
1249 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1250 NULL, "features");
1251 if (ret) {
1252 kobject_put(&f2fs_feat);
1253 kset_unregister(&f2fs_kset);
1254 } else {
1255 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1256 }
1257 return ret;
1258 }
1259
f2fs_exit_sysfs(void)1260 void f2fs_exit_sysfs(void)
1261 {
1262 kobject_put(&f2fs_feat);
1263 kset_unregister(&f2fs_kset);
1264 remove_proc_entry("fs/f2fs", NULL);
1265 f2fs_proc_root = NULL;
1266 }
1267
f2fs_register_sysfs(struct f2fs_sb_info * sbi)1268 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1269 {
1270 struct super_block *sb = sbi->sb;
1271 int err;
1272
1273 sbi->s_kobj.kset = &f2fs_kset;
1274 init_completion(&sbi->s_kobj_unregister);
1275 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1276 "%s", sb->s_id);
1277 if (err)
1278 goto put_sb_kobj;
1279
1280 sbi->s_stat_kobj.kset = &f2fs_kset;
1281 init_completion(&sbi->s_stat_kobj_unregister);
1282 err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1283 &sbi->s_kobj, "stat");
1284 if (err)
1285 goto put_stat_kobj;
1286
1287 sbi->s_feature_list_kobj.kset = &f2fs_kset;
1288 init_completion(&sbi->s_feature_list_kobj_unregister);
1289 err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1290 &f2fs_feature_list_ktype,
1291 &sbi->s_kobj, "feature_list");
1292 if (err)
1293 goto put_feature_list_kobj;
1294
1295 if (f2fs_proc_root)
1296 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1297
1298 if (sbi->s_proc) {
1299 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
1300 segment_info_seq_show, sb);
1301 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
1302 segment_bits_seq_show, sb);
1303 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
1304 iostat_info_seq_show, sb);
1305 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
1306 victim_bits_seq_show, sb);
1307 }
1308 return 0;
1309 put_feature_list_kobj:
1310 kobject_put(&sbi->s_feature_list_kobj);
1311 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1312 put_stat_kobj:
1313 kobject_put(&sbi->s_stat_kobj);
1314 wait_for_completion(&sbi->s_stat_kobj_unregister);
1315 put_sb_kobj:
1316 kobject_put(&sbi->s_kobj);
1317 wait_for_completion(&sbi->s_kobj_unregister);
1318 return err;
1319 }
1320
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)1321 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1322 {
1323 if (sbi->s_proc) {
1324 remove_proc_entry("iostat_info", sbi->s_proc);
1325 remove_proc_entry("segment_info", sbi->s_proc);
1326 remove_proc_entry("segment_bits", sbi->s_proc);
1327 remove_proc_entry("victim_bits", sbi->s_proc);
1328 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1329 }
1330
1331 kobject_del(&sbi->s_stat_kobj);
1332 kobject_put(&sbi->s_stat_kobj);
1333 wait_for_completion(&sbi->s_stat_kobj_unregister);
1334 kobject_del(&sbi->s_feature_list_kobj);
1335 kobject_put(&sbi->s_feature_list_kobj);
1336 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1337
1338 kobject_del(&sbi->s_kobj);
1339 kobject_put(&sbi->s_kobj);
1340 wait_for_completion(&sbi->s_kobj_unregister);
1341 }
1342