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 {
470 return -EINVAL;
471 }
472 return count;
473 }
474 if (!strcmp(a->attr.name, "gc_idle")) {
475 if (t == GC_IDLE_CB) {
476 sbi->gc_mode = GC_IDLE_CB;
477 } else if (t == GC_IDLE_GREEDY) {
478 sbi->gc_mode = GC_IDLE_GREEDY;
479 } else if (t == GC_IDLE_AT) {
480 if (!sbi->am.atgc_enabled)
481 return -EINVAL;
482 sbi->gc_mode = GC_IDLE_AT;
483 } else {
484 sbi->gc_mode = GC_NORMAL;
485 }
486 return count;
487 }
488
489 if (!strcmp(a->attr.name, "iostat_enable")) {
490 sbi->iostat_enable = !!t;
491 if (!sbi->iostat_enable)
492 f2fs_reset_iostat(sbi);
493 return count;
494 }
495
496 if (!strcmp(a->attr.name, "iostat_period_ms")) {
497 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
498 return -EINVAL;
499 spin_lock(&sbi->iostat_lock);
500 sbi->iostat_period_ms = (unsigned int)t;
501 spin_unlock(&sbi->iostat_lock);
502 return count;
503 }
504
505 #ifdef CONFIG_F2FS_FS_COMPRESSION
506 if (!strcmp(a->attr.name, "compr_written_block") ||
507 !strcmp(a->attr.name, "compr_saved_block")) {
508 if (t != 0)
509 return -EINVAL;
510 sbi->compr_written_block = 0;
511 sbi->compr_saved_block = 0;
512 return count;
513 }
514
515 if (!strcmp(a->attr.name, "compr_new_inode")) {
516 if (t != 0)
517 return -EINVAL;
518 sbi->compr_new_inode = 0;
519 return count;
520 }
521 #endif
522
523 if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
524 if (t > 100)
525 return -EINVAL;
526 sbi->am.candidate_ratio = t;
527 return count;
528 }
529
530 if (!strcmp(a->attr.name, "atgc_age_weight")) {
531 if (t > 100)
532 return -EINVAL;
533 sbi->am.age_weight = t;
534 return count;
535 }
536
537 if (!strcmp(a->attr.name, "gc_segment_mode")) {
538 if (t < MAX_GC_MODE)
539 sbi->gc_segment_mode = t;
540 else
541 return -EINVAL;
542 return count;
543 }
544
545 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
546 if (t != 0)
547 return -EINVAL;
548 sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
549 return count;
550 }
551
552 if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
553 if (t == 0 || t >= sbi->warm_data_age_threshold)
554 return -EINVAL;
555 if (t == *ui)
556 return count;
557 *ui = (unsigned int)t;
558 return count;
559 }
560
561 if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
562 if (t == 0 || t <= sbi->hot_data_age_threshold)
563 return -EINVAL;
564 if (t == *ui)
565 return count;
566 *ui = (unsigned int)t;
567 return count;
568 }
569
570 if (!strcmp(a->attr.name, "last_age_weight")) {
571 if (t > 100)
572 return -EINVAL;
573 if (t == *ui)
574 return count;
575 *ui = (unsigned int)t;
576 return count;
577 }
578
579 *ui = (unsigned int)t;
580
581 return count;
582 }
583
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)584 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
585 struct f2fs_sb_info *sbi,
586 const char *buf, size_t count)
587 {
588 ssize_t ret;
589 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
590 a->struct_type == GC_THREAD);
591
592 if (gc_entry) {
593 if (!down_read_trylock(&sbi->sb->s_umount))
594 return -EAGAIN;
595 }
596 ret = __sbi_store(a, sbi, buf, count);
597 if (gc_entry)
598 up_read(&sbi->sb->s_umount);
599
600 return ret;
601 }
602
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)603 static ssize_t f2fs_attr_show(struct kobject *kobj,
604 struct attribute *attr, char *buf)
605 {
606 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
607 s_kobj);
608 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
609
610 return a->show ? a->show(a, sbi, buf) : 0;
611 }
612
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)613 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
614 const char *buf, size_t len)
615 {
616 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
617 s_kobj);
618 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
619
620 return a->store ? a->store(a, sbi, buf, len) : 0;
621 }
622
f2fs_sb_release(struct kobject * kobj)623 static void f2fs_sb_release(struct kobject *kobj)
624 {
625 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
626 s_kobj);
627 complete(&sbi->s_kobj_unregister);
628 }
629
630 /*
631 * Note that there are three feature list entries:
632 * 1) /sys/fs/f2fs/features
633 * : shows runtime features supported by in-kernel f2fs along with Kconfig.
634 * - ref. F2FS_FEATURE_RO_ATTR()
635 *
636 * 2) /sys/fs/f2fs/$s_id/features <deprecated>
637 * : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
638 * won't add new feature anymore, and thus, users should check entries in 3)
639 * instead of this 2).
640 *
641 * 3) /sys/fs/f2fs/$s_id/feature_list
642 * : shows on-disk features enabled by mkfs.f2fs per instance, which follows
643 * sysfs entry rule where each entry should expose single value.
644 * This list covers old feature list provided by 2) and beyond. Therefore,
645 * please add new on-disk feature in this list only.
646 * - ref. F2FS_SB_FEATURE_RO_ATTR()
647 */
f2fs_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)648 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
649 struct f2fs_sb_info *sbi, char *buf)
650 {
651 return sprintf(buf, "supported\n");
652 }
653
654 #define F2FS_FEATURE_RO_ATTR(_name) \
655 static struct f2fs_attr f2fs_attr_##_name = { \
656 .attr = {.name = __stringify(_name), .mode = 0444 }, \
657 .show = f2fs_feature_show, \
658 }
659
f2fs_sb_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)660 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
661 struct f2fs_sb_info *sbi, char *buf)
662 {
663 if (F2FS_HAS_FEATURE(sbi, a->id))
664 return sprintf(buf, "supported\n");
665 return sprintf(buf, "unsupported\n");
666 }
667
668 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat) \
669 static struct f2fs_attr f2fs_attr_sb_##_name = { \
670 .attr = {.name = __stringify(_name), .mode = 0444 }, \
671 .show = f2fs_sb_feature_show, \
672 .id = F2FS_FEATURE_##_feat, \
673 }
674
675 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
676 static struct f2fs_attr f2fs_attr_##_name = { \
677 .attr = {.name = __stringify(_name), .mode = _mode }, \
678 .show = _show, \
679 .store = _store, \
680 .struct_type = _struct_type, \
681 .offset = _offset \
682 }
683
684 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
685 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
686 f2fs_sbi_show, f2fs_sbi_store, \
687 offsetof(struct struct_name, elname))
688
689 #define F2FS_GENERAL_RO_ATTR(name) \
690 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
691
692 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname) \
693 static struct f2fs_attr f2fs_attr_##_name = { \
694 .attr = {.name = __stringify(_name), .mode = 0444 }, \
695 .show = f2fs_sbi_show, \
696 .struct_type = _struct_type, \
697 .offset = offsetof(struct _struct_name, _elname), \
698 }
699
700 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
701 urgent_sleep_time);
702 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
703 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
704 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
705 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
706 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
707 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
708 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
709 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
710 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
711 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
712 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
713 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
714 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
715 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
716 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
717 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
718 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
719 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
720 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
721 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
722 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
723 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
724 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
725 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
726 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
727 interval_time[DISCARD_TIME]);
728 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
729 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
730 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
731 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
732 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
733 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
734 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
735 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
736 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
737 #ifdef CONFIG_F2FS_FAULT_INJECTION
738 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
739 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
740 #endif
741 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
742 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
743 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
744 F2FS_GENERAL_RO_ATTR(dirty_segments);
745 F2FS_GENERAL_RO_ATTR(free_segments);
746 F2FS_GENERAL_RO_ATTR(ovp_segments);
747 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
748 F2FS_GENERAL_RO_ATTR(features);
749 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
750 F2FS_GENERAL_RO_ATTR(unusable);
751 F2FS_GENERAL_RO_ATTR(encoding);
752 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
753 F2FS_GENERAL_RO_ATTR(main_blkaddr);
754 F2FS_GENERAL_RO_ATTR(pending_discard);
755 #ifdef CONFIG_F2FS_STAT_FS
756 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
757 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
758 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
759 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
760 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
761 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
762 F2FS_GENERAL_RO_ATTR(avg_vblocks);
763 #endif
764
765 #ifdef CONFIG_FS_ENCRYPTION
766 F2FS_FEATURE_RO_ATTR(encryption);
767 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
768 #ifdef CONFIG_UNICODE
769 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
770 #endif
771 #endif /* CONFIG_FS_ENCRYPTION */
772 #ifdef CONFIG_BLK_DEV_ZONED
773 F2FS_FEATURE_RO_ATTR(block_zoned);
774 #endif
775 F2FS_FEATURE_RO_ATTR(atomic_write);
776 F2FS_FEATURE_RO_ATTR(extra_attr);
777 F2FS_FEATURE_RO_ATTR(project_quota);
778 F2FS_FEATURE_RO_ATTR(inode_checksum);
779 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
780 F2FS_FEATURE_RO_ATTR(quota_ino);
781 F2FS_FEATURE_RO_ATTR(inode_crtime);
782 F2FS_FEATURE_RO_ATTR(lost_found);
783 #ifdef CONFIG_FS_VERITY
784 F2FS_FEATURE_RO_ATTR(verity);
785 #endif
786 F2FS_FEATURE_RO_ATTR(sb_checksum);
787 #ifdef CONFIG_UNICODE
788 F2FS_FEATURE_RO_ATTR(casefold);
789 #endif
790 F2FS_FEATURE_RO_ATTR(readonly);
791 #ifdef CONFIG_F2FS_FS_COMPRESSION
792 F2FS_FEATURE_RO_ATTR(compression);
793 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_written_block, compr_written_block);
794 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_saved_block, compr_saved_block);
795 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_new_inode, compr_new_inode);
796 #endif
797 F2FS_FEATURE_RO_ATTR(pin_file);
798
799 /* For ATGC */
800 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_ratio, candidate_ratio);
801 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_count, max_candidate_count);
802 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_weight, age_weight);
803 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
804
805 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
806 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
807
808 /* For block age extent cache */
809 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, hot_data_age_threshold, hot_data_age_threshold);
810 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, warm_data_age_threshold, warm_data_age_threshold);
811 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, last_age_weight, last_age_weight);
812
813 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
814 static struct attribute *f2fs_attrs[] = {
815 ATTR_LIST(gc_urgent_sleep_time),
816 ATTR_LIST(gc_min_sleep_time),
817 ATTR_LIST(gc_max_sleep_time),
818 ATTR_LIST(gc_no_gc_sleep_time),
819 ATTR_LIST(gc_idle),
820 ATTR_LIST(gc_urgent),
821 ATTR_LIST(reclaim_segments),
822 ATTR_LIST(main_blkaddr),
823 ATTR_LIST(max_small_discards),
824 ATTR_LIST(discard_granularity),
825 ATTR_LIST(pending_discard),
826 ATTR_LIST(batched_trim_sections),
827 ATTR_LIST(ipu_policy),
828 ATTR_LIST(min_ipu_util),
829 ATTR_LIST(min_fsync_blocks),
830 ATTR_LIST(min_seq_blocks),
831 ATTR_LIST(min_hot_blocks),
832 ATTR_LIST(min_ssr_sections),
833 ATTR_LIST(max_victim_search),
834 ATTR_LIST(migration_granularity),
835 ATTR_LIST(dir_level),
836 ATTR_LIST(ram_thresh),
837 ATTR_LIST(ra_nid_pages),
838 ATTR_LIST(dirty_nats_ratio),
839 ATTR_LIST(cp_interval),
840 ATTR_LIST(idle_interval),
841 ATTR_LIST(discard_idle_interval),
842 ATTR_LIST(gc_idle_interval),
843 ATTR_LIST(umount_discard_timeout),
844 ATTR_LIST(iostat_enable),
845 ATTR_LIST(iostat_period_ms),
846 ATTR_LIST(readdir_ra),
847 ATTR_LIST(max_io_bytes),
848 ATTR_LIST(gc_pin_file_thresh),
849 ATTR_LIST(extension_list),
850 #ifdef CONFIG_F2FS_FAULT_INJECTION
851 ATTR_LIST(inject_rate),
852 ATTR_LIST(inject_type),
853 #endif
854 ATTR_LIST(data_io_flag),
855 ATTR_LIST(node_io_flag),
856 ATTR_LIST(ckpt_thread_ioprio),
857 ATTR_LIST(dirty_segments),
858 ATTR_LIST(free_segments),
859 ATTR_LIST(ovp_segments),
860 ATTR_LIST(unusable),
861 ATTR_LIST(lifetime_write_kbytes),
862 ATTR_LIST(features),
863 ATTR_LIST(reserved_blocks),
864 ATTR_LIST(current_reserved_blocks),
865 ATTR_LIST(encoding),
866 ATTR_LIST(mounted_time_sec),
867 #ifdef CONFIG_F2FS_STAT_FS
868 ATTR_LIST(cp_foreground_calls),
869 ATTR_LIST(cp_background_calls),
870 ATTR_LIST(gc_foreground_calls),
871 ATTR_LIST(gc_background_calls),
872 ATTR_LIST(moved_blocks_foreground),
873 ATTR_LIST(moved_blocks_background),
874 ATTR_LIST(avg_vblocks),
875 #endif
876 #ifdef CONFIG_F2FS_FS_COMPRESSION
877 ATTR_LIST(compr_written_block),
878 ATTR_LIST(compr_saved_block),
879 ATTR_LIST(compr_new_inode),
880 #endif
881 /* For ATGC */
882 ATTR_LIST(atgc_candidate_ratio),
883 ATTR_LIST(atgc_candidate_count),
884 ATTR_LIST(atgc_age_weight),
885 ATTR_LIST(atgc_age_threshold),
886 ATTR_LIST(gc_segment_mode),
887 ATTR_LIST(gc_reclaimed_segments),
888 ATTR_LIST(hot_data_age_threshold),
889 ATTR_LIST(warm_data_age_threshold),
890 ATTR_LIST(last_age_weight),
891 NULL,
892 };
893 ATTRIBUTE_GROUPS(f2fs);
894
895 static struct attribute *f2fs_feat_attrs[] = {
896 #ifdef CONFIG_FS_ENCRYPTION
897 ATTR_LIST(encryption),
898 ATTR_LIST(test_dummy_encryption_v2),
899 #ifdef CONFIG_UNICODE
900 ATTR_LIST(encrypted_casefold),
901 #endif
902 #endif /* CONFIG_FS_ENCRYPTION */
903 #ifdef CONFIG_BLK_DEV_ZONED
904 ATTR_LIST(block_zoned),
905 #endif
906 ATTR_LIST(atomic_write),
907 ATTR_LIST(extra_attr),
908 ATTR_LIST(project_quota),
909 ATTR_LIST(inode_checksum),
910 ATTR_LIST(flexible_inline_xattr),
911 ATTR_LIST(quota_ino),
912 ATTR_LIST(inode_crtime),
913 ATTR_LIST(lost_found),
914 #ifdef CONFIG_FS_VERITY
915 ATTR_LIST(verity),
916 #endif
917 ATTR_LIST(sb_checksum),
918 #ifdef CONFIG_UNICODE
919 ATTR_LIST(casefold),
920 #endif
921 ATTR_LIST(readonly),
922 #ifdef CONFIG_F2FS_FS_COMPRESSION
923 ATTR_LIST(compression),
924 #endif
925 ATTR_LIST(pin_file),
926 NULL,
927 };
928 ATTRIBUTE_GROUPS(f2fs_feat);
929
930 F2FS_GENERAL_RO_ATTR(sb_status);
931 static struct attribute *f2fs_stat_attrs[] = {
932 ATTR_LIST(sb_status),
933 NULL,
934 };
935 ATTRIBUTE_GROUPS(f2fs_stat);
936
937 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
938 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
939 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
940 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
941 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
942 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
943 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
944 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
945 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
946 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
947 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
948 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
949 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
950 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
951
952 static struct attribute *f2fs_sb_feat_attrs[] = {
953 ATTR_LIST(sb_encryption),
954 ATTR_LIST(sb_block_zoned),
955 ATTR_LIST(sb_extra_attr),
956 ATTR_LIST(sb_project_quota),
957 ATTR_LIST(sb_inode_checksum),
958 ATTR_LIST(sb_flexible_inline_xattr),
959 ATTR_LIST(sb_quota_ino),
960 ATTR_LIST(sb_inode_crtime),
961 ATTR_LIST(sb_lost_found),
962 ATTR_LIST(sb_verity),
963 ATTR_LIST(sb_sb_checksum),
964 ATTR_LIST(sb_casefold),
965 ATTR_LIST(sb_compression),
966 ATTR_LIST(sb_readonly),
967 NULL,
968 };
969 ATTRIBUTE_GROUPS(f2fs_sb_feat);
970
971 static const struct sysfs_ops f2fs_attr_ops = {
972 .show = f2fs_attr_show,
973 .store = f2fs_attr_store,
974 };
975
976 static struct kobj_type f2fs_sb_ktype = {
977 .default_groups = f2fs_groups,
978 .sysfs_ops = &f2fs_attr_ops,
979 .release = f2fs_sb_release,
980 };
981
982 static struct kobj_type f2fs_ktype = {
983 .sysfs_ops = &f2fs_attr_ops,
984 };
985
986 static struct kset f2fs_kset = {
987 .kobj = {.ktype = &f2fs_ktype},
988 };
989
990 static struct kobj_type f2fs_feat_ktype = {
991 .default_groups = f2fs_feat_groups,
992 .sysfs_ops = &f2fs_attr_ops,
993 };
994
995 static struct kobject f2fs_feat = {
996 .kset = &f2fs_kset,
997 };
998
f2fs_stat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)999 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1000 struct attribute *attr, char *buf)
1001 {
1002 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1003 s_stat_kobj);
1004 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1005
1006 return a->show ? a->show(a, sbi, buf) : 0;
1007 }
1008
f2fs_stat_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)1009 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1010 const char *buf, size_t len)
1011 {
1012 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1013 s_stat_kobj);
1014 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1015
1016 return a->store ? a->store(a, sbi, buf, len) : 0;
1017 }
1018
f2fs_stat_kobj_release(struct kobject * kobj)1019 static void f2fs_stat_kobj_release(struct kobject *kobj)
1020 {
1021 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1022 s_stat_kobj);
1023 complete(&sbi->s_stat_kobj_unregister);
1024 }
1025
1026 static const struct sysfs_ops f2fs_stat_attr_ops = {
1027 .show = f2fs_stat_attr_show,
1028 .store = f2fs_stat_attr_store,
1029 };
1030
1031 static struct kobj_type f2fs_stat_ktype = {
1032 .default_groups = f2fs_stat_groups,
1033 .sysfs_ops = &f2fs_stat_attr_ops,
1034 .release = f2fs_stat_kobj_release,
1035 };
1036
f2fs_sb_feat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1037 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1038 struct attribute *attr, char *buf)
1039 {
1040 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1041 s_feature_list_kobj);
1042 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1043
1044 return a->show ? a->show(a, sbi, buf) : 0;
1045 }
1046
f2fs_feature_list_kobj_release(struct kobject * kobj)1047 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1048 {
1049 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1050 s_feature_list_kobj);
1051 complete(&sbi->s_feature_list_kobj_unregister);
1052 }
1053
1054 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1055 .show = f2fs_sb_feat_attr_show,
1056 };
1057
1058 static struct kobj_type f2fs_feature_list_ktype = {
1059 .default_groups = f2fs_sb_feat_groups,
1060 .sysfs_ops = &f2fs_feature_list_attr_ops,
1061 .release = f2fs_feature_list_kobj_release,
1062 };
1063
segment_info_seq_show(struct seq_file * seq,void * offset)1064 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1065 void *offset)
1066 {
1067 struct super_block *sb = seq->private;
1068 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1069 unsigned int total_segs =
1070 le32_to_cpu(sbi->raw_super->segment_count_main);
1071 int i;
1072
1073 seq_puts(seq, "format: segment_type|valid_blocks\n"
1074 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1075
1076 for (i = 0; i < total_segs; i++) {
1077 struct seg_entry *se = get_seg_entry(sbi, i);
1078
1079 if ((i % 10) == 0)
1080 seq_printf(seq, "%-10d", i);
1081 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1082 if ((i % 10) == 9 || i == (total_segs - 1))
1083 seq_putc(seq, '\n');
1084 else
1085 seq_putc(seq, ' ');
1086 }
1087
1088 return 0;
1089 }
1090
segment_bits_seq_show(struct seq_file * seq,void * offset)1091 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1092 void *offset)
1093 {
1094 struct super_block *sb = seq->private;
1095 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1096 unsigned int total_segs =
1097 le32_to_cpu(sbi->raw_super->segment_count_main);
1098 int i, j;
1099
1100 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1101 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1102
1103 for (i = 0; i < total_segs; i++) {
1104 struct seg_entry *se = get_seg_entry(sbi, i);
1105
1106 seq_printf(seq, "%-10d", i);
1107 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1108 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1109 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1110 seq_putc(seq, '\n');
1111 }
1112 return 0;
1113 }
1114
f2fs_record_iostat(struct f2fs_sb_info * sbi)1115 void f2fs_record_iostat(struct f2fs_sb_info *sbi)
1116 {
1117 unsigned long long iostat_diff[NR_IO_TYPE];
1118 int i;
1119
1120 if (time_is_after_jiffies(sbi->iostat_next_period))
1121 return;
1122
1123 /* Need double check under the lock */
1124 spin_lock(&sbi->iostat_lock);
1125 if (time_is_after_jiffies(sbi->iostat_next_period)) {
1126 spin_unlock(&sbi->iostat_lock);
1127 return;
1128 }
1129 sbi->iostat_next_period = jiffies +
1130 msecs_to_jiffies(sbi->iostat_period_ms);
1131
1132 for (i = 0; i < NR_IO_TYPE; i++) {
1133 iostat_diff[i] = sbi->rw_iostat[i] -
1134 sbi->prev_rw_iostat[i];
1135 sbi->prev_rw_iostat[i] = sbi->rw_iostat[i];
1136 }
1137 spin_unlock(&sbi->iostat_lock);
1138
1139 trace_f2fs_iostat(sbi, iostat_diff);
1140 }
1141
iostat_info_seq_show(struct seq_file * seq,void * offset)1142 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
1143 void *offset)
1144 {
1145 struct super_block *sb = seq->private;
1146 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1147 time64_t now = ktime_get_real_seconds();
1148
1149 if (!sbi->iostat_enable)
1150 return 0;
1151
1152 seq_printf(seq, "time: %-16llu\n", now);
1153
1154 /* print app write IOs */
1155 seq_puts(seq, "[WRITE]\n");
1156 seq_printf(seq, "app buffered: %-16llu\n",
1157 sbi->rw_iostat[APP_BUFFERED_IO]);
1158 seq_printf(seq, "app direct: %-16llu\n",
1159 sbi->rw_iostat[APP_DIRECT_IO]);
1160 seq_printf(seq, "app mapped: %-16llu\n",
1161 sbi->rw_iostat[APP_MAPPED_IO]);
1162
1163 /* print fs write IOs */
1164 seq_printf(seq, "fs data: %-16llu\n",
1165 sbi->rw_iostat[FS_DATA_IO]);
1166 seq_printf(seq, "fs node: %-16llu\n",
1167 sbi->rw_iostat[FS_NODE_IO]);
1168 seq_printf(seq, "fs meta: %-16llu\n",
1169 sbi->rw_iostat[FS_META_IO]);
1170 seq_printf(seq, "fs gc data: %-16llu\n",
1171 sbi->rw_iostat[FS_GC_DATA_IO]);
1172 seq_printf(seq, "fs gc node: %-16llu\n",
1173 sbi->rw_iostat[FS_GC_NODE_IO]);
1174 seq_printf(seq, "fs cp data: %-16llu\n",
1175 sbi->rw_iostat[FS_CP_DATA_IO]);
1176 seq_printf(seq, "fs cp node: %-16llu\n",
1177 sbi->rw_iostat[FS_CP_NODE_IO]);
1178 seq_printf(seq, "fs cp meta: %-16llu\n",
1179 sbi->rw_iostat[FS_CP_META_IO]);
1180
1181 /* print app read IOs */
1182 seq_puts(seq, "[READ]\n");
1183 seq_printf(seq, "app buffered: %-16llu\n",
1184 sbi->rw_iostat[APP_BUFFERED_READ_IO]);
1185 seq_printf(seq, "app direct: %-16llu\n",
1186 sbi->rw_iostat[APP_DIRECT_READ_IO]);
1187 seq_printf(seq, "app mapped: %-16llu\n",
1188 sbi->rw_iostat[APP_MAPPED_READ_IO]);
1189
1190 /* print fs read IOs */
1191 seq_printf(seq, "fs data: %-16llu\n",
1192 sbi->rw_iostat[FS_DATA_READ_IO]);
1193 seq_printf(seq, "fs gc data: %-16llu\n",
1194 sbi->rw_iostat[FS_GDATA_READ_IO]);
1195 seq_printf(seq, "fs compr_data: %-16llu\n",
1196 sbi->rw_iostat[FS_CDATA_READ_IO]);
1197 seq_printf(seq, "fs node: %-16llu\n",
1198 sbi->rw_iostat[FS_NODE_READ_IO]);
1199 seq_printf(seq, "fs meta: %-16llu\n",
1200 sbi->rw_iostat[FS_META_READ_IO]);
1201
1202 /* print other IOs */
1203 seq_puts(seq, "[OTHER]\n");
1204 seq_printf(seq, "fs discard: %-16llu\n",
1205 sbi->rw_iostat[FS_DISCARD]);
1206
1207 return 0;
1208 }
1209
victim_bits_seq_show(struct seq_file * seq,void * offset)1210 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1211 void *offset)
1212 {
1213 struct super_block *sb = seq->private;
1214 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1215 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1216 int i;
1217
1218 seq_puts(seq, "format: victim_secmap bitmaps\n");
1219
1220 for (i = 0; i < MAIN_SECS(sbi); i++) {
1221 if ((i % 10) == 0)
1222 seq_printf(seq, "%-10d", i);
1223 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1224 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1225 seq_putc(seq, '\n');
1226 else
1227 seq_putc(seq, ' ');
1228 }
1229 return 0;
1230 }
1231
f2fs_init_sysfs(void)1232 int __init f2fs_init_sysfs(void)
1233 {
1234 int ret;
1235
1236 kobject_set_name(&f2fs_kset.kobj, "f2fs");
1237 f2fs_kset.kobj.parent = fs_kobj;
1238 ret = kset_register(&f2fs_kset);
1239 if (ret)
1240 return ret;
1241
1242 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1243 NULL, "features");
1244 if (ret) {
1245 kobject_put(&f2fs_feat);
1246 kset_unregister(&f2fs_kset);
1247 } else {
1248 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1249 }
1250 return ret;
1251 }
1252
f2fs_exit_sysfs(void)1253 void f2fs_exit_sysfs(void)
1254 {
1255 kobject_put(&f2fs_feat);
1256 kset_unregister(&f2fs_kset);
1257 remove_proc_entry("fs/f2fs", NULL);
1258 f2fs_proc_root = NULL;
1259 }
1260
f2fs_register_sysfs(struct f2fs_sb_info * sbi)1261 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1262 {
1263 struct super_block *sb = sbi->sb;
1264 int err;
1265
1266 sbi->s_kobj.kset = &f2fs_kset;
1267 init_completion(&sbi->s_kobj_unregister);
1268 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1269 "%s", sb->s_id);
1270 if (err)
1271 goto put_sb_kobj;
1272
1273 sbi->s_stat_kobj.kset = &f2fs_kset;
1274 init_completion(&sbi->s_stat_kobj_unregister);
1275 err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1276 &sbi->s_kobj, "stat");
1277 if (err)
1278 goto put_stat_kobj;
1279
1280 sbi->s_feature_list_kobj.kset = &f2fs_kset;
1281 init_completion(&sbi->s_feature_list_kobj_unregister);
1282 err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1283 &f2fs_feature_list_ktype,
1284 &sbi->s_kobj, "feature_list");
1285 if (err)
1286 goto put_feature_list_kobj;
1287
1288 if (f2fs_proc_root)
1289 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1290
1291 if (sbi->s_proc) {
1292 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
1293 segment_info_seq_show, sb);
1294 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
1295 segment_bits_seq_show, sb);
1296 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
1297 iostat_info_seq_show, sb);
1298 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
1299 victim_bits_seq_show, sb);
1300 }
1301 return 0;
1302 put_feature_list_kobj:
1303 kobject_put(&sbi->s_feature_list_kobj);
1304 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1305 put_stat_kobj:
1306 kobject_put(&sbi->s_stat_kobj);
1307 wait_for_completion(&sbi->s_stat_kobj_unregister);
1308 put_sb_kobj:
1309 kobject_put(&sbi->s_kobj);
1310 wait_for_completion(&sbi->s_kobj_unregister);
1311 return err;
1312 }
1313
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)1314 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1315 {
1316 if (sbi->s_proc) {
1317 remove_proc_entry("iostat_info", sbi->s_proc);
1318 remove_proc_entry("segment_info", sbi->s_proc);
1319 remove_proc_entry("segment_bits", sbi->s_proc);
1320 remove_proc_entry("victim_bits", sbi->s_proc);
1321 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1322 }
1323
1324 kobject_del(&sbi->s_stat_kobj);
1325 kobject_put(&sbi->s_stat_kobj);
1326 wait_for_completion(&sbi->s_stat_kobj_unregister);
1327 kobject_del(&sbi->s_feature_list_kobj);
1328 kobject_put(&sbi->s_feature_list_kobj);
1329 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1330
1331 kobject_del(&sbi->s_kobj);
1332 kobject_put(&sbi->s_kobj);
1333 wait_for_completion(&sbi->s_kobj_unregister);
1334 }
1335