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