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
issued_discard_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)152 static ssize_t issued_discard_show(struct f2fs_attr *a,
153 struct f2fs_sb_info *sbi, char *buf)
154 {
155 if (!SM_I(sbi)->dcc_info)
156 return -EINVAL;
157 return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
158 &SM_I(sbi)->dcc_info->issued_discard));
159 }
160
queued_discard_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)161 static ssize_t queued_discard_show(struct f2fs_attr *a,
162 struct f2fs_sb_info *sbi, char *buf)
163 {
164 if (!SM_I(sbi)->dcc_info)
165 return -EINVAL;
166 return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
167 &SM_I(sbi)->dcc_info->queued_discard));
168 }
169
undiscard_blks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)170 static ssize_t undiscard_blks_show(struct f2fs_attr *a,
171 struct f2fs_sb_info *sbi, char *buf)
172 {
173 if (!SM_I(sbi)->dcc_info)
174 return -EINVAL;
175 return sysfs_emit(buf, "%u\n",
176 SM_I(sbi)->dcc_info->undiscard_blks);
177 }
178
atgc_enabled_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)179 static ssize_t atgc_enabled_show(struct f2fs_attr *a,
180 struct f2fs_sb_info *sbi, char *buf)
181 {
182 return sysfs_emit(buf, "%d\n", sbi->am.atgc_enabled ? 1 : 0);
183 }
184
gc_mode_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)185 static ssize_t gc_mode_show(struct f2fs_attr *a,
186 struct f2fs_sb_info *sbi, char *buf)
187 {
188 return sysfs_emit(buf, "%s\n", gc_mode_names[sbi->gc_mode]);
189 }
190
features_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)191 static ssize_t features_show(struct f2fs_attr *a,
192 struct f2fs_sb_info *sbi, char *buf)
193 {
194 int len = 0;
195
196 if (f2fs_sb_has_encrypt(sbi))
197 len += sysfs_emit_at(buf, len, "%s",
198 "encryption");
199 if (f2fs_sb_has_blkzoned(sbi))
200 len += sysfs_emit_at(buf, len, "%s%s",
201 len ? ", " : "", "blkzoned");
202 if (f2fs_sb_has_extra_attr(sbi))
203 len += sysfs_emit_at(buf, len, "%s%s",
204 len ? ", " : "", "extra_attr");
205 if (f2fs_sb_has_project_quota(sbi))
206 len += sysfs_emit_at(buf, len, "%s%s",
207 len ? ", " : "", "projquota");
208 if (f2fs_sb_has_inode_chksum(sbi))
209 len += sysfs_emit_at(buf, len, "%s%s",
210 len ? ", " : "", "inode_checksum");
211 if (f2fs_sb_has_flexible_inline_xattr(sbi))
212 len += sysfs_emit_at(buf, len, "%s%s",
213 len ? ", " : "", "flexible_inline_xattr");
214 if (f2fs_sb_has_quota_ino(sbi))
215 len += sysfs_emit_at(buf, len, "%s%s",
216 len ? ", " : "", "quota_ino");
217 if (f2fs_sb_has_inode_crtime(sbi))
218 len += sysfs_emit_at(buf, len, "%s%s",
219 len ? ", " : "", "inode_crtime");
220 if (f2fs_sb_has_lost_found(sbi))
221 len += sysfs_emit_at(buf, len, "%s%s",
222 len ? ", " : "", "lost_found");
223 if (f2fs_sb_has_verity(sbi))
224 len += sysfs_emit_at(buf, len, "%s%s",
225 len ? ", " : "", "verity");
226 if (f2fs_sb_has_sb_chksum(sbi))
227 len += sysfs_emit_at(buf, len, "%s%s",
228 len ? ", " : "", "sb_checksum");
229 if (f2fs_sb_has_casefold(sbi))
230 len += sysfs_emit_at(buf, len, "%s%s",
231 len ? ", " : "", "casefold");
232 if (f2fs_sb_has_readonly(sbi))
233 len += sysfs_emit_at(buf, len, "%s%s",
234 len ? ", " : "", "readonly");
235 if (f2fs_sb_has_compression(sbi))
236 len += sysfs_emit_at(buf, len, "%s%s",
237 len ? ", " : "", "compression");
238 len += sysfs_emit_at(buf, len, "%s%s",
239 len ? ", " : "", "pin_file");
240 len += sysfs_emit_at(buf, len, "\n");
241 return len;
242 }
243
current_reserved_blocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)244 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
245 struct f2fs_sb_info *sbi, char *buf)
246 {
247 return sysfs_emit(buf, "%u\n", sbi->current_reserved_blocks);
248 }
249
unusable_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)250 static ssize_t unusable_show(struct f2fs_attr *a,
251 struct f2fs_sb_info *sbi, char *buf)
252 {
253 block_t unusable;
254
255 if (test_opt(sbi, DISABLE_CHECKPOINT))
256 unusable = sbi->unusable_block_count;
257 else
258 unusable = f2fs_get_unusable_blocks(sbi);
259 return sysfs_emit(buf, "%llu\n", (unsigned long long)unusable);
260 }
261
encoding_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)262 static ssize_t encoding_show(struct f2fs_attr *a,
263 struct f2fs_sb_info *sbi, char *buf)
264 {
265 #if IS_ENABLED(CONFIG_UNICODE)
266 struct super_block *sb = sbi->sb;
267
268 if (f2fs_sb_has_casefold(sbi))
269 return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
270 (sb->s_encoding->version >> 16) & 0xff,
271 (sb->s_encoding->version >> 8) & 0xff,
272 sb->s_encoding->version & 0xff);
273 #endif
274 return sysfs_emit(buf, "(none)\n");
275 }
276
encoding_flags_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)277 static ssize_t encoding_flags_show(struct f2fs_attr *a,
278 struct f2fs_sb_info *sbi, char *buf)
279 {
280 return sysfs_emit(buf, "%x\n",
281 le16_to_cpu(F2FS_RAW_SUPER(sbi)->s_encoding_flags));
282 }
283
effective_lookup_mode_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)284 static ssize_t effective_lookup_mode_show(struct f2fs_attr *a,
285 struct f2fs_sb_info *sbi, char *buf)
286 {
287 switch (F2FS_OPTION(sbi).lookup_mode) {
288 case LOOKUP_PERF:
289 return sysfs_emit(buf, "perf\n");
290 case LOOKUP_COMPAT:
291 return sysfs_emit(buf, "compat\n");
292 case LOOKUP_AUTO:
293 if (sb_no_casefold_compat_fallback(sbi->sb))
294 return sysfs_emit(buf, "auto:perf\n");
295 return sysfs_emit(buf, "auto:compat\n");
296 }
297 return 0;
298 }
299
mounted_time_sec_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)300 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
301 struct f2fs_sb_info *sbi, char *buf)
302 {
303 return sysfs_emit(buf, "%llu\n", SIT_I(sbi)->mounted_time);
304 }
305
306 #ifdef CONFIG_F2FS_STAT_FS
moved_blocks_foreground_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)307 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
308 struct f2fs_sb_info *sbi, char *buf)
309 {
310 struct f2fs_stat_info *si = F2FS_STAT(sbi);
311
312 return sysfs_emit(buf, "%llu\n",
313 (unsigned long long)(si->tot_blks -
314 (si->bg_data_blks + si->bg_node_blks)));
315 }
316
moved_blocks_background_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)317 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
318 struct f2fs_sb_info *sbi, char *buf)
319 {
320 struct f2fs_stat_info *si = F2FS_STAT(sbi);
321
322 return sysfs_emit(buf, "%llu\n",
323 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
324 }
325
avg_vblocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)326 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
327 struct f2fs_sb_info *sbi, char *buf)
328 {
329 struct f2fs_stat_info *si = F2FS_STAT(sbi);
330
331 si->dirty_count = dirty_segments(sbi);
332 f2fs_update_sit_info(sbi);
333 return sysfs_emit(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
334 }
335 #endif
336
main_blkaddr_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)337 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
338 struct f2fs_sb_info *sbi, char *buf)
339 {
340 return sysfs_emit(buf, "%llu\n",
341 (unsigned long long)MAIN_BLKADDR(sbi));
342 }
343
f2fs_sbi_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)344 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
345 struct f2fs_sb_info *sbi, char *buf)
346 {
347 unsigned char *ptr = NULL;
348 unsigned int *ui;
349
350 ptr = __struct_ptr(sbi, a->struct_type);
351 if (!ptr)
352 return -EINVAL;
353
354 if (!strcmp(a->attr.name, "extension_list")) {
355 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
356 sbi->raw_super->extension_list;
357 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
358 int hot_count = sbi->raw_super->hot_ext_count;
359 int len = 0, i;
360
361 len += sysfs_emit_at(buf, len, "cold file extension:\n");
362 for (i = 0; i < cold_count; i++)
363 len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
364
365 len += sysfs_emit_at(buf, len, "hot file extension:\n");
366 for (i = cold_count; i < cold_count + hot_count; i++)
367 len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
368
369 return len;
370 }
371
372 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
373 struct ckpt_req_control *cprc = &sbi->cprc_info;
374 int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
375 int level = IOPRIO_PRIO_LEVEL(cprc->ckpt_thread_ioprio);
376
377 if (class != IOPRIO_CLASS_RT && class != IOPRIO_CLASS_BE)
378 return -EINVAL;
379
380 return sysfs_emit(buf, "%s,%d\n",
381 class == IOPRIO_CLASS_RT ? "rt" : "be", level);
382 }
383
384 #ifdef CONFIG_F2FS_FS_COMPRESSION
385 if (!strcmp(a->attr.name, "compr_written_block"))
386 return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
387
388 if (!strcmp(a->attr.name, "compr_saved_block"))
389 return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
390
391 if (!strcmp(a->attr.name, "compr_new_inode"))
392 return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
393 #endif
394
395 if (!strcmp(a->attr.name, "gc_segment_mode"))
396 return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
397
398 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
399 return sysfs_emit(buf, "%u\n",
400 sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
401 }
402
403 if (!strcmp(a->attr.name, "current_atomic_write")) {
404 s64 current_write = atomic64_read(&sbi->current_atomic_write);
405
406 return sysfs_emit(buf, "%lld\n", current_write);
407 }
408
409 if (!strcmp(a->attr.name, "peak_atomic_write"))
410 return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write);
411
412 if (!strcmp(a->attr.name, "committed_atomic_block"))
413 return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block);
414
415 if (!strcmp(a->attr.name, "revoked_atomic_block"))
416 return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block);
417
418 #ifdef CONFIG_F2FS_STAT_FS
419 if (!strcmp(a->attr.name, "cp_foreground_calls"))
420 return sysfs_emit(buf, "%d\n",
421 atomic_read(&sbi->cp_call_count[TOTAL_CALL]) -
422 atomic_read(&sbi->cp_call_count[BACKGROUND]));
423 if (!strcmp(a->attr.name, "cp_background_calls"))
424 return sysfs_emit(buf, "%d\n",
425 atomic_read(&sbi->cp_call_count[BACKGROUND]));
426 #endif
427
428 ui = (unsigned int *)(ptr + a->offset);
429
430 return sysfs_emit(buf, "%u\n", *ui);
431 }
432
__sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)433 static ssize_t __sbi_store(struct f2fs_attr *a,
434 struct f2fs_sb_info *sbi,
435 const char *buf, size_t count)
436 {
437 unsigned char *ptr;
438 unsigned long t;
439 unsigned int *ui;
440 ssize_t ret;
441
442 ptr = __struct_ptr(sbi, a->struct_type);
443 if (!ptr)
444 return -EINVAL;
445
446 if (!strcmp(a->attr.name, "extension_list")) {
447 const char *name = strim((char *)buf);
448 bool set = true, hot;
449
450 if (!strncmp(name, "[h]", 3))
451 hot = true;
452 else if (!strncmp(name, "[c]", 3))
453 hot = false;
454 else
455 return -EINVAL;
456
457 name += 3;
458
459 if (*name == '!') {
460 name++;
461 set = false;
462 }
463
464 if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
465 return -EINVAL;
466
467 f2fs_down_write(&sbi->sb_lock);
468
469 ret = f2fs_update_extension_list(sbi, name, hot, set);
470 if (ret)
471 goto out;
472
473 ret = f2fs_commit_super(sbi, false);
474 if (ret)
475 f2fs_update_extension_list(sbi, name, hot, !set);
476 out:
477 f2fs_up_write(&sbi->sb_lock);
478 return ret ? ret : count;
479 }
480
481 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
482 const char *name = strim((char *)buf);
483 struct ckpt_req_control *cprc = &sbi->cprc_info;
484 int class;
485 long level;
486 int ret;
487
488 if (!strncmp(name, "rt,", 3))
489 class = IOPRIO_CLASS_RT;
490 else if (!strncmp(name, "be,", 3))
491 class = IOPRIO_CLASS_BE;
492 else
493 return -EINVAL;
494
495 name += 3;
496 ret = kstrtol(name, 10, &level);
497 if (ret)
498 return ret;
499 if (level >= IOPRIO_NR_LEVELS || level < 0)
500 return -EINVAL;
501
502 cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, level);
503 if (test_opt(sbi, MERGE_CHECKPOINT)) {
504 ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
505 cprc->ckpt_thread_ioprio);
506 if (ret)
507 return ret;
508 }
509
510 return count;
511 }
512
513 ui = (unsigned int *)(ptr + a->offset);
514
515 ret = kstrtoul(skip_spaces(buf), 0, &t);
516 if (ret < 0)
517 return ret;
518 #ifdef CONFIG_F2FS_FAULT_INJECTION
519 if (a->struct_type == FAULT_INFO_TYPE) {
520 if (f2fs_build_fault_attr(sbi, 0, t, FAULT_TYPE))
521 return -EINVAL;
522 return count;
523 }
524 if (a->struct_type == FAULT_INFO_RATE) {
525 if (f2fs_build_fault_attr(sbi, t, 0, FAULT_RATE))
526 return -EINVAL;
527 return count;
528 }
529 #endif
530 if (a->struct_type == RESERVED_BLOCKS) {
531 spin_lock(&sbi->stat_lock);
532 if (t > (unsigned long)(sbi->user_block_count -
533 F2FS_OPTION(sbi).root_reserved_blocks)) {
534 spin_unlock(&sbi->stat_lock);
535 return -EINVAL;
536 }
537 *ui = t;
538 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
539 sbi->user_block_count - valid_user_blocks(sbi));
540 spin_unlock(&sbi->stat_lock);
541 return count;
542 }
543
544 if (!strcmp(a->attr.name, "discard_io_aware_gran")) {
545 if (t > MAX_PLIST_NUM)
546 return -EINVAL;
547 if (!f2fs_block_unit_discard(sbi))
548 return -EINVAL;
549 if (t == *ui)
550 return count;
551 *ui = t;
552 return count;
553 }
554
555 if (!strcmp(a->attr.name, "discard_granularity")) {
556 if (t == 0 || t > MAX_PLIST_NUM)
557 return -EINVAL;
558 if (!f2fs_block_unit_discard(sbi))
559 return -EINVAL;
560 if (t == *ui)
561 return count;
562 *ui = t;
563 return count;
564 }
565
566 if (!strcmp(a->attr.name, "max_ordered_discard")) {
567 if (t == 0 || t > MAX_PLIST_NUM)
568 return -EINVAL;
569 if (!f2fs_block_unit_discard(sbi))
570 return -EINVAL;
571 *ui = t;
572 return count;
573 }
574
575 if (!strcmp(a->attr.name, "discard_urgent_util")) {
576 if (t > 100)
577 return -EINVAL;
578 *ui = t;
579 return count;
580 }
581
582 if (!strcmp(a->attr.name, "discard_io_aware")) {
583 if (t >= DPOLICY_IO_AWARE_MAX)
584 return -EINVAL;
585 *ui = t;
586 return count;
587 }
588
589 if (!strcmp(a->attr.name, "migration_granularity")) {
590 if (t == 0 || t > SEGS_PER_SEC(sbi))
591 return -EINVAL;
592 }
593
594 if (!strcmp(a->attr.name, "migration_window_granularity")) {
595 if (t == 0 || t > SEGS_PER_SEC(sbi))
596 return -EINVAL;
597 }
598
599 if (!strcmp(a->attr.name, "gc_urgent")) {
600 if (t == 0) {
601 sbi->gc_mode = GC_NORMAL;
602 } else if (t == 1) {
603 sbi->gc_mode = GC_URGENT_HIGH;
604 if (sbi->gc_thread) {
605 sbi->gc_thread->gc_wake = true;
606 wake_up_interruptible_all(
607 &sbi->gc_thread->gc_wait_queue_head);
608 wake_up_discard_thread(sbi, true);
609 }
610 } else if (t == 2) {
611 sbi->gc_mode = GC_URGENT_LOW;
612 } else if (t == 3) {
613 sbi->gc_mode = GC_URGENT_MID;
614 if (sbi->gc_thread) {
615 sbi->gc_thread->gc_wake = true;
616 wake_up_interruptible_all(
617 &sbi->gc_thread->gc_wait_queue_head);
618 }
619 } else {
620 return -EINVAL;
621 }
622 return count;
623 }
624 if (!strcmp(a->attr.name, "gc_idle")) {
625 if (t == GC_IDLE_CB) {
626 sbi->gc_mode = GC_IDLE_CB;
627 } else if (t == GC_IDLE_GREEDY) {
628 sbi->gc_mode = GC_IDLE_GREEDY;
629 } else if (t == GC_IDLE_AT) {
630 if (!sbi->am.atgc_enabled)
631 return -EINVAL;
632 sbi->gc_mode = GC_IDLE_AT;
633 } else {
634 sbi->gc_mode = GC_NORMAL;
635 }
636 return count;
637 }
638
639 if (!strcmp(a->attr.name, "gc_remaining_trials")) {
640 spin_lock(&sbi->gc_remaining_trials_lock);
641 sbi->gc_remaining_trials = t;
642 spin_unlock(&sbi->gc_remaining_trials_lock);
643
644 return count;
645 }
646
647 if (!strcmp(a->attr.name, "gc_no_zoned_gc_percent")) {
648 if (t > 100)
649 return -EINVAL;
650 *ui = (unsigned int)t;
651 return count;
652 }
653
654 if (!strcmp(a->attr.name, "gc_boost_zoned_gc_percent")) {
655 if (t > 100)
656 return -EINVAL;
657 *ui = (unsigned int)t;
658 return count;
659 }
660
661 if (!strcmp(a->attr.name, "gc_valid_thresh_ratio")) {
662 if (t > 100)
663 return -EINVAL;
664 *ui = (unsigned int)t;
665 return count;
666 }
667
668 #ifdef CONFIG_F2FS_IOSTAT
669 if (!strcmp(a->attr.name, "iostat_enable")) {
670 sbi->iostat_enable = !!t;
671 if (!sbi->iostat_enable)
672 f2fs_reset_iostat(sbi);
673 return count;
674 }
675
676 if (!strcmp(a->attr.name, "iostat_period_ms")) {
677 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
678 return -EINVAL;
679 spin_lock_irq(&sbi->iostat_lock);
680 sbi->iostat_period_ms = (unsigned int)t;
681 spin_unlock_irq(&sbi->iostat_lock);
682 return count;
683 }
684 #endif
685
686 #ifdef CONFIG_BLK_DEV_ZONED
687 if (!strcmp(a->attr.name, "blkzone_alloc_policy")) {
688 if (t < BLKZONE_ALLOC_PRIOR_SEQ || t > BLKZONE_ALLOC_PRIOR_CONV)
689 return -EINVAL;
690 sbi->blkzone_alloc_policy = t;
691 return count;
692 }
693 #endif
694
695 #ifdef CONFIG_F2FS_FS_COMPRESSION
696 if (!strcmp(a->attr.name, "compr_written_block") ||
697 !strcmp(a->attr.name, "compr_saved_block")) {
698 if (t != 0)
699 return -EINVAL;
700 sbi->compr_written_block = 0;
701 sbi->compr_saved_block = 0;
702 return count;
703 }
704
705 if (!strcmp(a->attr.name, "compr_new_inode")) {
706 if (t != 0)
707 return -EINVAL;
708 sbi->compr_new_inode = 0;
709 return count;
710 }
711
712 if (!strcmp(a->attr.name, "compress_percent")) {
713 if (t == 0 || t > 100)
714 return -EINVAL;
715 *ui = t;
716 return count;
717 }
718
719 if (!strcmp(a->attr.name, "compress_watermark")) {
720 if (t == 0 || t > 100)
721 return -EINVAL;
722 *ui = t;
723 return count;
724 }
725 #endif
726
727 if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
728 if (t > 100)
729 return -EINVAL;
730 sbi->am.candidate_ratio = t;
731 return count;
732 }
733
734 if (!strcmp(a->attr.name, "atgc_age_weight")) {
735 if (t > 100)
736 return -EINVAL;
737 sbi->am.age_weight = t;
738 return count;
739 }
740
741 if (!strcmp(a->attr.name, "gc_segment_mode")) {
742 if (t < MAX_GC_MODE)
743 sbi->gc_segment_mode = t;
744 else
745 return -EINVAL;
746 return count;
747 }
748
749 if (!strcmp(a->attr.name, "gc_pin_file_threshold")) {
750 if (t > MAX_GC_FAILED_PINNED_FILES)
751 return -EINVAL;
752 sbi->gc_pin_file_threshold = t;
753 return count;
754 }
755
756 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
757 if (t != 0)
758 return -EINVAL;
759 sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
760 return count;
761 }
762
763 if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
764 if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
765 sbi->seq_file_ra_mul = t;
766 else
767 return -EINVAL;
768 return count;
769 }
770
771 if (!strcmp(a->attr.name, "max_fragment_chunk")) {
772 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
773 sbi->max_fragment_chunk = t;
774 else
775 return -EINVAL;
776 return count;
777 }
778
779 if (!strcmp(a->attr.name, "max_fragment_hole")) {
780 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
781 sbi->max_fragment_hole = t;
782 else
783 return -EINVAL;
784 return count;
785 }
786
787 if (!strcmp(a->attr.name, "peak_atomic_write")) {
788 if (t != 0)
789 return -EINVAL;
790 sbi->peak_atomic_write = 0;
791 return count;
792 }
793
794 if (!strcmp(a->attr.name, "committed_atomic_block")) {
795 if (t != 0)
796 return -EINVAL;
797 sbi->committed_atomic_block = 0;
798 return count;
799 }
800
801 if (!strcmp(a->attr.name, "revoked_atomic_block")) {
802 if (t != 0)
803 return -EINVAL;
804 sbi->revoked_atomic_block = 0;
805 return count;
806 }
807
808 if (!strcmp(a->attr.name, "readdir_ra")) {
809 sbi->readdir_ra = !!t;
810 return count;
811 }
812
813 if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
814 if (t == 0 || t >= sbi->warm_data_age_threshold)
815 return -EINVAL;
816 if (t == *ui)
817 return count;
818 *ui = (unsigned int)t;
819 return count;
820 }
821
822 if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
823 if (t <= sbi->hot_data_age_threshold)
824 return -EINVAL;
825 if (t == *ui)
826 return count;
827 *ui = (unsigned int)t;
828 return count;
829 }
830
831 if (!strcmp(a->attr.name, "last_age_weight")) {
832 if (t > 100)
833 return -EINVAL;
834 if (t == *ui)
835 return count;
836 *ui = (unsigned int)t;
837 return count;
838 }
839
840 if (!strcmp(a->attr.name, "max_read_extent_count")) {
841 if (t > UINT_MAX)
842 return -EINVAL;
843 *ui = (unsigned int)t;
844 return count;
845 }
846
847 if (!strcmp(a->attr.name, "ipu_policy")) {
848 if (t >= BIT(F2FS_IPU_MAX))
849 return -EINVAL;
850 /* allow F2FS_IPU_NOCACHE only for IPU in the pinned file */
851 if (f2fs_lfs_mode(sbi) && (t & ~BIT(F2FS_IPU_NOCACHE)))
852 return -EINVAL;
853 SM_I(sbi)->ipu_policy = (unsigned int)t;
854 return count;
855 }
856
857 if (!strcmp(a->attr.name, "dir_level")) {
858 if (t > MAX_DIR_HASH_DEPTH)
859 return -EINVAL;
860 sbi->dir_level = t;
861 return count;
862 }
863
864 if (!strcmp(a->attr.name, "reserved_pin_section")) {
865 if (t > GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))
866 return -EINVAL;
867 *ui = (unsigned int)t;
868 return count;
869 }
870
871 if (!strcmp(a->attr.name, "gc_boost_gc_multiple")) {
872 if (t < 1 || t > SEGS_PER_SEC(sbi))
873 return -EINVAL;
874 sbi->gc_thread->boost_gc_multiple = (unsigned int)t;
875 return count;
876 }
877
878 if (!strcmp(a->attr.name, "gc_boost_gc_greedy")) {
879 if (t > GC_GREEDY)
880 return -EINVAL;
881 sbi->gc_thread->boost_gc_greedy = (unsigned int)t;
882 return count;
883 }
884
885 if (!strcmp(a->attr.name, "bggc_io_aware")) {
886 if (t < AWARE_ALL_IO || t > AWARE_NONE)
887 return -EINVAL;
888 sbi->bggc_io_aware = t;
889 return count;
890 }
891
892 if (!strcmp(a->attr.name, "allocate_section_hint")) {
893 if (t < 0 || t > MAIN_SECS(sbi))
894 return -EINVAL;
895 sbi->allocate_section_hint = t;
896 return count;
897 }
898
899 if (!strcmp(a->attr.name, "allocate_section_policy")) {
900 if (t < ALLOCATE_FORWARD_NOHINT || t > ALLOCATE_FORWARD_FROM_HINT)
901 return -EINVAL;
902 sbi->allocate_section_policy = t;
903 return count;
904 }
905
906 *ui = (unsigned int)t;
907
908 return count;
909 }
910
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)911 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
912 struct f2fs_sb_info *sbi,
913 const char *buf, size_t count)
914 {
915 ssize_t ret;
916 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
917 a->struct_type == GC_THREAD);
918
919 if (gc_entry) {
920 if (!down_read_trylock(&sbi->sb->s_umount))
921 return -EAGAIN;
922 }
923 ret = __sbi_store(a, sbi, buf, count);
924 if (gc_entry)
925 up_read(&sbi->sb->s_umount);
926
927 return ret;
928 }
929
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)930 static ssize_t f2fs_attr_show(struct kobject *kobj,
931 struct attribute *attr, char *buf)
932 {
933 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
934 s_kobj);
935 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
936
937 return a->show ? a->show(a, sbi, buf) : 0;
938 }
939
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)940 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
941 const char *buf, size_t len)
942 {
943 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
944 s_kobj);
945 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
946
947 return a->store ? a->store(a, sbi, buf, len) : 0;
948 }
949
f2fs_sb_release(struct kobject * kobj)950 static void f2fs_sb_release(struct kobject *kobj)
951 {
952 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
953 s_kobj);
954 complete(&sbi->s_kobj_unregister);
955 }
956
f2fs_base_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)957 static ssize_t f2fs_base_attr_show(struct kobject *kobj,
958 struct attribute *attr, char *buf)
959 {
960 struct f2fs_base_attr *a = container_of(attr,
961 struct f2fs_base_attr, attr);
962
963 return a->show ? a->show(a, buf) : 0;
964 }
965
f2fs_base_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)966 static ssize_t f2fs_base_attr_store(struct kobject *kobj,
967 struct attribute *attr,
968 const char *buf, size_t len)
969 {
970 struct f2fs_base_attr *a = container_of(attr,
971 struct f2fs_base_attr, attr);
972
973 return a->store ? a->store(a, buf, len) : 0;
974 }
975
976 /*
977 * Note that there are three feature list entries:
978 * 1) /sys/fs/f2fs/features
979 * : shows runtime features supported by in-kernel f2fs along with Kconfig.
980 * - ref. F2FS_FEATURE_RO_ATTR()
981 *
982 * 2) /sys/fs/f2fs/$s_id/features <deprecated>
983 * : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
984 * won't add new feature anymore, and thus, users should check entries in 3)
985 * instead of this 2).
986 *
987 * 3) /sys/fs/f2fs/$s_id/feature_list
988 * : shows on-disk features enabled by mkfs.f2fs per instance, which follows
989 * sysfs entry rule where each entry should expose single value.
990 * This list covers old feature list provided by 2) and beyond. Therefore,
991 * please add new on-disk feature in this list only.
992 * - ref. F2FS_SB_FEATURE_RO_ATTR()
993 */
f2fs_feature_show(struct f2fs_base_attr * a,char * buf)994 static ssize_t f2fs_feature_show(struct f2fs_base_attr *a, char *buf)
995 {
996 return sysfs_emit(buf, "supported\n");
997 }
998
999 #define F2FS_FEATURE_RO_ATTR(_name) \
1000 static struct f2fs_base_attr f2fs_base_attr_##_name = { \
1001 .attr = {.name = __stringify(_name), .mode = 0444 }, \
1002 .show = f2fs_feature_show, \
1003 }
1004
f2fs_tune_show(struct f2fs_base_attr * a,char * buf)1005 static ssize_t f2fs_tune_show(struct f2fs_base_attr *a, char *buf)
1006 {
1007 unsigned int res = 0;
1008
1009 if (!strcmp(a->attr.name, "reclaim_caches_kb"))
1010 res = f2fs_donate_files();
1011
1012 return sysfs_emit(buf, "%u\n", res);
1013 }
1014
f2fs_tune_store(struct f2fs_base_attr * a,const char * buf,size_t count)1015 static ssize_t f2fs_tune_store(struct f2fs_base_attr *a,
1016 const char *buf, size_t count)
1017 {
1018 unsigned long t;
1019 int ret;
1020
1021 ret = kstrtoul(skip_spaces(buf), 0, &t);
1022 if (ret)
1023 return ret;
1024
1025 if (!strcmp(a->attr.name, "reclaim_caches_kb"))
1026 f2fs_reclaim_caches(t);
1027
1028 return count;
1029 }
1030
1031 #define F2FS_TUNE_RW_ATTR(_name) \
1032 static struct f2fs_base_attr f2fs_base_attr_##_name = { \
1033 .attr = {.name = __stringify(_name), .mode = 0644 }, \
1034 .show = f2fs_tune_show, \
1035 .store = f2fs_tune_store, \
1036 }
1037
f2fs_sb_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)1038 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
1039 struct f2fs_sb_info *sbi, char *buf)
1040 {
1041 if (F2FS_HAS_FEATURE(sbi, a->id))
1042 return sysfs_emit(buf, "supported\n");
1043 return sysfs_emit(buf, "unsupported\n");
1044 }
1045
1046 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat) \
1047 static struct f2fs_attr f2fs_attr_sb_##_name = { \
1048 .attr = {.name = __stringify(_name), .mode = 0444 }, \
1049 .show = f2fs_sb_feature_show, \
1050 .id = F2FS_FEATURE_##_feat, \
1051 }
1052
1053 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
1054 static struct f2fs_attr f2fs_attr_##_name = { \
1055 .attr = {.name = __stringify(_name), .mode = _mode }, \
1056 .show = _show, \
1057 .store = _store, \
1058 .struct_type = _struct_type, \
1059 .offset = _offset \
1060 }
1061
1062 #define F2FS_RO_ATTR(struct_type, struct_name, name, elname) \
1063 F2FS_ATTR_OFFSET(struct_type, name, 0444, \
1064 f2fs_sbi_show, NULL, \
1065 offsetof(struct struct_name, elname))
1066
1067 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
1068 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
1069 f2fs_sbi_show, f2fs_sbi_store, \
1070 offsetof(struct struct_name, elname))
1071
1072 #define F2FS_GENERAL_RO_ATTR(name) \
1073 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
1074
1075 #ifdef CONFIG_F2FS_STAT_FS
1076 #define STAT_INFO_RO_ATTR(name, elname) \
1077 F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname)
1078 #endif
1079
1080 #define GC_THREAD_RW_ATTR(name, elname) \
1081 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname)
1082
1083 #define SM_INFO_RW_ATTR(name, elname) \
1084 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname)
1085
1086 #define SM_INFO_GENERAL_RW_ATTR(elname) \
1087 SM_INFO_RW_ATTR(elname, elname)
1088
1089 #define DCC_INFO_RW_ATTR(name, elname) \
1090 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname)
1091
1092 #define DCC_INFO_GENERAL_RW_ATTR(elname) \
1093 DCC_INFO_RW_ATTR(elname, elname)
1094
1095 #define NM_INFO_RW_ATTR(name, elname) \
1096 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname)
1097
1098 #define NM_INFO_GENERAL_RW_ATTR(elname) \
1099 NM_INFO_RW_ATTR(elname, elname)
1100
1101 #define F2FS_SBI_RW_ATTR(name, elname) \
1102 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname)
1103
1104 #define F2FS_SBI_GENERAL_RW_ATTR(elname) \
1105 F2FS_SBI_RW_ATTR(elname, elname)
1106
1107 #define F2FS_SBI_GENERAL_RO_ATTR(elname) \
1108 F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname)
1109
1110 #ifdef CONFIG_F2FS_FAULT_INJECTION
1111 #define FAULT_INFO_GENERAL_RW_ATTR(type, elname) \
1112 F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname)
1113 #endif
1114
1115 #define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname) \
1116 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname)
1117
1118 #define CPRC_INFO_GENERAL_RW_ATTR(elname) \
1119 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname)
1120
1121 #define ATGC_INFO_RW_ATTR(name, elname) \
1122 F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
1123
1124 /* GC_THREAD ATTR */
1125 GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
1126 GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
1127 GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time);
1128 GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
1129 GC_THREAD_RW_ATTR(gc_no_zoned_gc_percent, no_zoned_gc_percent);
1130 GC_THREAD_RW_ATTR(gc_boost_zoned_gc_percent, boost_zoned_gc_percent);
1131 GC_THREAD_RW_ATTR(gc_valid_thresh_ratio, valid_thresh_ratio);
1132 GC_THREAD_RW_ATTR(gc_boost_gc_multiple, boost_gc_multiple);
1133 GC_THREAD_RW_ATTR(gc_boost_gc_greedy, boost_gc_greedy);
1134
1135 /* SM_INFO ATTR */
1136 SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments);
1137 SM_INFO_GENERAL_RW_ATTR(ipu_policy);
1138 SM_INFO_GENERAL_RW_ATTR(min_ipu_util);
1139 SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks);
1140 SM_INFO_GENERAL_RW_ATTR(min_seq_blocks);
1141 SM_INFO_GENERAL_RW_ATTR(min_hot_blocks);
1142 SM_INFO_GENERAL_RW_ATTR(min_ssr_sections);
1143 SM_INFO_GENERAL_RW_ATTR(reserved_segments);
1144
1145 /* DCC_INFO ATTR */
1146 DCC_INFO_RW_ATTR(max_small_discards, max_discards);
1147 DCC_INFO_GENERAL_RW_ATTR(max_discard_request);
1148 DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time);
1149 DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
1150 DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
1151 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
1152 DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
1153 DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
1154 DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
1155 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware);
1156
1157 /* NM_INFO ATTR */
1158 NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
1159 NM_INFO_GENERAL_RW_ATTR(ram_thresh);
1160 NM_INFO_GENERAL_RW_ATTR(ra_nid_pages);
1161 NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio);
1162
1163 /* F2FS_SBI ATTR */
1164 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
1165 F2FS_SBI_RW_ATTR(gc_idle, gc_mode);
1166 F2FS_SBI_RW_ATTR(gc_urgent, gc_mode);
1167 F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]);
1168 F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]);
1169 F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]);
1170 F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]);
1171 F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
1172 F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold);
1173 F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs);
1174 F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
1175 F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
1176 F2FS_SBI_GENERAL_RW_ATTR(migration_window_granularity);
1177 F2FS_SBI_GENERAL_RW_ATTR(dir_level);
1178 F2FS_SBI_GENERAL_RW_ATTR(allocate_section_hint);
1179 F2FS_SBI_GENERAL_RW_ATTR(allocate_section_policy);
1180 #ifdef CONFIG_F2FS_IOSTAT
1181 F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
1182 F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
1183 #endif
1184 F2FS_SBI_GENERAL_RW_ATTR(readdir_ra);
1185 F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes);
1186 F2FS_SBI_GENERAL_RW_ATTR(data_io_flag);
1187 F2FS_SBI_GENERAL_RW_ATTR(node_io_flag);
1188 F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials);
1189 F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul);
1190 F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode);
1191 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk);
1192 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole);
1193 #ifdef CONFIG_F2FS_FS_COMPRESSION
1194 F2FS_SBI_GENERAL_RW_ATTR(compr_written_block);
1195 F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block);
1196 F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode);
1197 F2FS_SBI_GENERAL_RW_ATTR(compress_percent);
1198 F2FS_SBI_GENERAL_RW_ATTR(compress_watermark);
1199 #endif
1200 /* atomic write */
1201 F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write);
1202 F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write);
1203 F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block);
1204 F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block);
1205 /* block age extent cache */
1206 F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold);
1207 F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold);
1208 F2FS_SBI_GENERAL_RW_ATTR(last_age_weight);
1209 /* read extent cache */
1210 F2FS_SBI_GENERAL_RW_ATTR(max_read_extent_count);
1211 #ifdef CONFIG_BLK_DEV_ZONED
1212 F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec);
1213 F2FS_SBI_GENERAL_RW_ATTR(blkzone_alloc_policy);
1214 #endif
1215 F2FS_SBI_GENERAL_RW_ATTR(carve_out);
1216 F2FS_SBI_GENERAL_RW_ATTR(reserved_pin_section);
1217 F2FS_SBI_GENERAL_RW_ATTR(bggc_io_aware);
1218
1219 /* STAT_INFO ATTR */
1220 #ifdef CONFIG_F2FS_STAT_FS
1221 STAT_INFO_RO_ATTR(cp_foreground_calls, cp_call_count[FOREGROUND]);
1222 STAT_INFO_RO_ATTR(cp_background_calls, cp_call_count[BACKGROUND]);
1223 STAT_INFO_RO_ATTR(gc_foreground_calls, gc_call_count[FOREGROUND]);
1224 STAT_INFO_RO_ATTR(gc_background_calls, gc_call_count[BACKGROUND]);
1225 #endif
1226
1227 /* FAULT_INFO ATTR */
1228 #ifdef CONFIG_F2FS_FAULT_INJECTION
1229 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate);
1230 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type);
1231 #endif
1232
1233 /* RESERVED_BLOCKS ATTR */
1234 RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks);
1235
1236 /* CPRC_INFO ATTR */
1237 CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio);
1238
1239 /* ATGC_INFO ATTR */
1240 ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio);
1241 ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
1242 ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
1243 ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
1244
1245 F2FS_GENERAL_RO_ATTR(dirty_segments);
1246 F2FS_GENERAL_RO_ATTR(free_segments);
1247 F2FS_GENERAL_RO_ATTR(ovp_segments);
1248 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
1249 F2FS_GENERAL_RO_ATTR(features);
1250 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
1251 F2FS_GENERAL_RO_ATTR(unusable);
1252 F2FS_GENERAL_RO_ATTR(encoding);
1253 F2FS_GENERAL_RO_ATTR(encoding_flags);
1254 F2FS_GENERAL_RO_ATTR(effective_lookup_mode);
1255 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
1256 F2FS_GENERAL_RO_ATTR(main_blkaddr);
1257 F2FS_GENERAL_RO_ATTR(pending_discard);
1258 F2FS_GENERAL_RO_ATTR(atgc_enabled);
1259 F2FS_GENERAL_RO_ATTR(gc_mode);
1260 #ifdef CONFIG_F2FS_STAT_FS
1261 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
1262 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
1263 F2FS_GENERAL_RO_ATTR(avg_vblocks);
1264 #endif
1265
1266 #ifdef CONFIG_FS_ENCRYPTION
1267 F2FS_FEATURE_RO_ATTR(encryption);
1268 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
1269 #if IS_ENABLED(CONFIG_UNICODE)
1270 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
1271 #endif
1272 #endif /* CONFIG_FS_ENCRYPTION */
1273 #ifdef CONFIG_BLK_DEV_ZONED
1274 F2FS_FEATURE_RO_ATTR(block_zoned);
1275 #endif
1276 F2FS_FEATURE_RO_ATTR(atomic_write);
1277 F2FS_FEATURE_RO_ATTR(extra_attr);
1278 F2FS_FEATURE_RO_ATTR(project_quota);
1279 F2FS_FEATURE_RO_ATTR(inode_checksum);
1280 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
1281 F2FS_FEATURE_RO_ATTR(quota_ino);
1282 F2FS_FEATURE_RO_ATTR(inode_crtime);
1283 F2FS_FEATURE_RO_ATTR(lost_found);
1284 #ifdef CONFIG_FS_VERITY
1285 F2FS_FEATURE_RO_ATTR(verity);
1286 #endif
1287 F2FS_FEATURE_RO_ATTR(sb_checksum);
1288 #if IS_ENABLED(CONFIG_UNICODE)
1289 F2FS_FEATURE_RO_ATTR(casefold);
1290 #endif
1291 F2FS_FEATURE_RO_ATTR(readonly);
1292 #ifdef CONFIG_F2FS_FS_COMPRESSION
1293 F2FS_FEATURE_RO_ATTR(compression);
1294 #endif
1295 F2FS_FEATURE_RO_ATTR(pin_file);
1296 #ifdef CONFIG_UNICODE
1297 F2FS_FEATURE_RO_ATTR(linear_lookup);
1298 #endif
1299
1300 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
1301 static struct attribute *f2fs_attrs[] = {
1302 ATTR_LIST(gc_urgent_sleep_time),
1303 ATTR_LIST(gc_min_sleep_time),
1304 ATTR_LIST(gc_max_sleep_time),
1305 ATTR_LIST(gc_no_gc_sleep_time),
1306 ATTR_LIST(gc_no_zoned_gc_percent),
1307 ATTR_LIST(gc_boost_zoned_gc_percent),
1308 ATTR_LIST(gc_valid_thresh_ratio),
1309 ATTR_LIST(gc_boost_gc_multiple),
1310 ATTR_LIST(gc_boost_gc_greedy),
1311 ATTR_LIST(gc_idle),
1312 ATTR_LIST(gc_urgent),
1313 ATTR_LIST(reclaim_segments),
1314 ATTR_LIST(main_blkaddr),
1315 ATTR_LIST(max_small_discards),
1316 ATTR_LIST(max_discard_request),
1317 ATTR_LIST(min_discard_issue_time),
1318 ATTR_LIST(mid_discard_issue_time),
1319 ATTR_LIST(max_discard_issue_time),
1320 ATTR_LIST(discard_io_aware_gran),
1321 ATTR_LIST(discard_urgent_util),
1322 ATTR_LIST(discard_granularity),
1323 ATTR_LIST(max_ordered_discard),
1324 ATTR_LIST(discard_io_aware),
1325 ATTR_LIST(pending_discard),
1326 ATTR_LIST(gc_mode),
1327 ATTR_LIST(ipu_policy),
1328 ATTR_LIST(min_ipu_util),
1329 ATTR_LIST(min_fsync_blocks),
1330 ATTR_LIST(min_seq_blocks),
1331 ATTR_LIST(min_hot_blocks),
1332 ATTR_LIST(min_ssr_sections),
1333 ATTR_LIST(reserved_segments),
1334 ATTR_LIST(max_victim_search),
1335 ATTR_LIST(migration_granularity),
1336 ATTR_LIST(migration_window_granularity),
1337 ATTR_LIST(dir_level),
1338 ATTR_LIST(ram_thresh),
1339 ATTR_LIST(ra_nid_pages),
1340 ATTR_LIST(dirty_nats_ratio),
1341 ATTR_LIST(max_roll_forward_node_blocks),
1342 ATTR_LIST(cp_interval),
1343 ATTR_LIST(idle_interval),
1344 ATTR_LIST(discard_idle_interval),
1345 ATTR_LIST(gc_idle_interval),
1346 ATTR_LIST(umount_discard_timeout),
1347 ATTR_LIST(bggc_io_aware),
1348 #ifdef CONFIG_F2FS_IOSTAT
1349 ATTR_LIST(iostat_enable),
1350 ATTR_LIST(iostat_period_ms),
1351 #endif
1352 ATTR_LIST(readdir_ra),
1353 ATTR_LIST(max_io_bytes),
1354 ATTR_LIST(gc_pin_file_thresh),
1355 ATTR_LIST(extension_list),
1356 #ifdef CONFIG_F2FS_FAULT_INJECTION
1357 ATTR_LIST(inject_rate),
1358 ATTR_LIST(inject_type),
1359 #endif
1360 ATTR_LIST(data_io_flag),
1361 ATTR_LIST(node_io_flag),
1362 ATTR_LIST(gc_remaining_trials),
1363 ATTR_LIST(ckpt_thread_ioprio),
1364 ATTR_LIST(dirty_segments),
1365 ATTR_LIST(free_segments),
1366 ATTR_LIST(ovp_segments),
1367 ATTR_LIST(unusable),
1368 ATTR_LIST(lifetime_write_kbytes),
1369 ATTR_LIST(features),
1370 ATTR_LIST(reserved_blocks),
1371 ATTR_LIST(current_reserved_blocks),
1372 ATTR_LIST(encoding),
1373 ATTR_LIST(encoding_flags),
1374 ATTR_LIST(effective_lookup_mode),
1375 ATTR_LIST(mounted_time_sec),
1376 #ifdef CONFIG_F2FS_STAT_FS
1377 ATTR_LIST(cp_foreground_calls),
1378 ATTR_LIST(cp_background_calls),
1379 ATTR_LIST(gc_foreground_calls),
1380 ATTR_LIST(gc_background_calls),
1381 ATTR_LIST(moved_blocks_foreground),
1382 ATTR_LIST(moved_blocks_background),
1383 ATTR_LIST(avg_vblocks),
1384 #endif
1385 #ifdef CONFIG_BLK_DEV_ZONED
1386 ATTR_LIST(unusable_blocks_per_sec),
1387 ATTR_LIST(blkzone_alloc_policy),
1388 #endif
1389 #ifdef CONFIG_F2FS_FS_COMPRESSION
1390 ATTR_LIST(compr_written_block),
1391 ATTR_LIST(compr_saved_block),
1392 ATTR_LIST(compr_new_inode),
1393 ATTR_LIST(compress_percent),
1394 ATTR_LIST(compress_watermark),
1395 #endif
1396 /* For ATGC */
1397 ATTR_LIST(atgc_candidate_ratio),
1398 ATTR_LIST(atgc_candidate_count),
1399 ATTR_LIST(atgc_age_weight),
1400 ATTR_LIST(atgc_age_threshold),
1401 ATTR_LIST(atgc_enabled),
1402 ATTR_LIST(seq_file_ra_mul),
1403 ATTR_LIST(gc_segment_mode),
1404 ATTR_LIST(gc_reclaimed_segments),
1405 ATTR_LIST(max_fragment_chunk),
1406 ATTR_LIST(max_fragment_hole),
1407 ATTR_LIST(current_atomic_write),
1408 ATTR_LIST(peak_atomic_write),
1409 ATTR_LIST(committed_atomic_block),
1410 ATTR_LIST(revoked_atomic_block),
1411 ATTR_LIST(hot_data_age_threshold),
1412 ATTR_LIST(warm_data_age_threshold),
1413 ATTR_LIST(last_age_weight),
1414 ATTR_LIST(max_read_extent_count),
1415 ATTR_LIST(carve_out),
1416 ATTR_LIST(reserved_pin_section),
1417 ATTR_LIST(allocate_section_hint),
1418 ATTR_LIST(allocate_section_policy),
1419 NULL,
1420 };
1421 ATTRIBUTE_GROUPS(f2fs);
1422
1423 #define BASE_ATTR_LIST(name) (&f2fs_base_attr_##name.attr)
1424 static struct attribute *f2fs_feat_attrs[] = {
1425 #ifdef CONFIG_FS_ENCRYPTION
1426 BASE_ATTR_LIST(encryption),
1427 BASE_ATTR_LIST(test_dummy_encryption_v2),
1428 #if IS_ENABLED(CONFIG_UNICODE)
1429 BASE_ATTR_LIST(encrypted_casefold),
1430 #endif
1431 #endif /* CONFIG_FS_ENCRYPTION */
1432 #ifdef CONFIG_BLK_DEV_ZONED
1433 BASE_ATTR_LIST(block_zoned),
1434 #endif
1435 BASE_ATTR_LIST(atomic_write),
1436 BASE_ATTR_LIST(extra_attr),
1437 BASE_ATTR_LIST(project_quota),
1438 BASE_ATTR_LIST(inode_checksum),
1439 BASE_ATTR_LIST(flexible_inline_xattr),
1440 BASE_ATTR_LIST(quota_ino),
1441 BASE_ATTR_LIST(inode_crtime),
1442 BASE_ATTR_LIST(lost_found),
1443 #ifdef CONFIG_FS_VERITY
1444 BASE_ATTR_LIST(verity),
1445 #endif
1446 BASE_ATTR_LIST(sb_checksum),
1447 #if IS_ENABLED(CONFIG_UNICODE)
1448 BASE_ATTR_LIST(casefold),
1449 #endif
1450 BASE_ATTR_LIST(readonly),
1451 #ifdef CONFIG_F2FS_FS_COMPRESSION
1452 BASE_ATTR_LIST(compression),
1453 #endif
1454 BASE_ATTR_LIST(pin_file),
1455 #ifdef CONFIG_UNICODE
1456 BASE_ATTR_LIST(linear_lookup),
1457 #endif
1458 NULL,
1459 };
1460 ATTRIBUTE_GROUPS(f2fs_feat);
1461
1462 F2FS_GENERAL_RO_ATTR(sb_status);
1463 F2FS_GENERAL_RO_ATTR(cp_status);
1464 F2FS_GENERAL_RO_ATTR(issued_discard);
1465 F2FS_GENERAL_RO_ATTR(queued_discard);
1466 F2FS_GENERAL_RO_ATTR(undiscard_blks);
1467
1468 static struct attribute *f2fs_stat_attrs[] = {
1469 ATTR_LIST(sb_status),
1470 ATTR_LIST(cp_status),
1471 ATTR_LIST(issued_discard),
1472 ATTR_LIST(queued_discard),
1473 ATTR_LIST(undiscard_blks),
1474 NULL,
1475 };
1476 ATTRIBUTE_GROUPS(f2fs_stat);
1477
1478 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1479 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1480 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1481 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1482 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1483 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1484 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1485 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1486 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1487 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1488 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1489 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1490 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1491 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1492 F2FS_SB_FEATURE_RO_ATTR(device_alias, DEVICE_ALIAS);
1493
1494 static struct attribute *f2fs_sb_feat_attrs[] = {
1495 ATTR_LIST(sb_encryption),
1496 ATTR_LIST(sb_block_zoned),
1497 ATTR_LIST(sb_extra_attr),
1498 ATTR_LIST(sb_project_quota),
1499 ATTR_LIST(sb_inode_checksum),
1500 ATTR_LIST(sb_flexible_inline_xattr),
1501 ATTR_LIST(sb_quota_ino),
1502 ATTR_LIST(sb_inode_crtime),
1503 ATTR_LIST(sb_lost_found),
1504 ATTR_LIST(sb_verity),
1505 ATTR_LIST(sb_sb_checksum),
1506 ATTR_LIST(sb_casefold),
1507 ATTR_LIST(sb_compression),
1508 ATTR_LIST(sb_readonly),
1509 ATTR_LIST(sb_device_alias),
1510 NULL,
1511 };
1512 ATTRIBUTE_GROUPS(f2fs_sb_feat);
1513
1514 F2FS_TUNE_RW_ATTR(reclaim_caches_kb);
1515
1516 static struct attribute *f2fs_tune_attrs[] = {
1517 BASE_ATTR_LIST(reclaim_caches_kb),
1518 NULL,
1519 };
1520 ATTRIBUTE_GROUPS(f2fs_tune);
1521
1522 static const struct sysfs_ops f2fs_attr_ops = {
1523 .show = f2fs_attr_show,
1524 .store = f2fs_attr_store,
1525 };
1526
1527 static const struct kobj_type f2fs_sb_ktype = {
1528 .default_groups = f2fs_groups,
1529 .sysfs_ops = &f2fs_attr_ops,
1530 .release = f2fs_sb_release,
1531 };
1532
1533 static const struct kobj_type f2fs_ktype = {
1534 .sysfs_ops = &f2fs_attr_ops,
1535 };
1536
1537 static struct kset f2fs_kset = {
1538 .kobj = {.ktype = &f2fs_ktype},
1539 };
1540
1541 static const struct sysfs_ops f2fs_feat_attr_ops = {
1542 .show = f2fs_base_attr_show,
1543 .store = f2fs_base_attr_store,
1544 };
1545
1546 static const struct kobj_type f2fs_feat_ktype = {
1547 .default_groups = f2fs_feat_groups,
1548 .sysfs_ops = &f2fs_feat_attr_ops,
1549 };
1550
1551 static struct kobject f2fs_feat = {
1552 .kset = &f2fs_kset,
1553 };
1554
1555 static const struct sysfs_ops f2fs_tune_attr_ops = {
1556 .show = f2fs_base_attr_show,
1557 .store = f2fs_base_attr_store,
1558 };
1559
1560 static const struct kobj_type f2fs_tune_ktype = {
1561 .default_groups = f2fs_tune_groups,
1562 .sysfs_ops = &f2fs_tune_attr_ops,
1563 };
1564
1565 static struct kobject f2fs_tune = {
1566 .kset = &f2fs_kset,
1567 };
1568
f2fs_stat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1569 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1570 struct attribute *attr, char *buf)
1571 {
1572 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1573 s_stat_kobj);
1574 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1575
1576 return a->show ? a->show(a, sbi, buf) : 0;
1577 }
1578
f2fs_stat_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)1579 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1580 const char *buf, size_t len)
1581 {
1582 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1583 s_stat_kobj);
1584 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1585
1586 return a->store ? a->store(a, sbi, buf, len) : 0;
1587 }
1588
f2fs_stat_kobj_release(struct kobject * kobj)1589 static void f2fs_stat_kobj_release(struct kobject *kobj)
1590 {
1591 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1592 s_stat_kobj);
1593 complete(&sbi->s_stat_kobj_unregister);
1594 }
1595
1596 static const struct sysfs_ops f2fs_stat_attr_ops = {
1597 .show = f2fs_stat_attr_show,
1598 .store = f2fs_stat_attr_store,
1599 };
1600
1601 static const struct kobj_type f2fs_stat_ktype = {
1602 .default_groups = f2fs_stat_groups,
1603 .sysfs_ops = &f2fs_stat_attr_ops,
1604 .release = f2fs_stat_kobj_release,
1605 };
1606
f2fs_sb_feat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1607 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1608 struct attribute *attr, char *buf)
1609 {
1610 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1611 s_feature_list_kobj);
1612 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1613
1614 return a->show ? a->show(a, sbi, buf) : 0;
1615 }
1616
f2fs_feature_list_kobj_release(struct kobject * kobj)1617 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1618 {
1619 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1620 s_feature_list_kobj);
1621 complete(&sbi->s_feature_list_kobj_unregister);
1622 }
1623
1624 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1625 .show = f2fs_sb_feat_attr_show,
1626 };
1627
1628 static const struct kobj_type f2fs_feature_list_ktype = {
1629 .default_groups = f2fs_sb_feat_groups,
1630 .sysfs_ops = &f2fs_feature_list_attr_ops,
1631 .release = f2fs_feature_list_kobj_release,
1632 };
1633
segment_info_seq_show(struct seq_file * seq,void * offset)1634 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1635 void *offset)
1636 {
1637 struct super_block *sb = seq->private;
1638 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1639 unsigned int total_segs =
1640 le32_to_cpu(sbi->raw_super->segment_count_main);
1641 int i;
1642
1643 seq_puts(seq, "format: segment_type|valid_blocks\n"
1644 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1645
1646 for (i = 0; i < total_segs; i++) {
1647 struct seg_entry *se = get_seg_entry(sbi, i);
1648
1649 if ((i % 10) == 0)
1650 seq_printf(seq, "%-10d", i);
1651 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1652 if ((i % 10) == 9 || i == (total_segs - 1))
1653 seq_putc(seq, '\n');
1654 else
1655 seq_putc(seq, ' ');
1656 }
1657
1658 return 0;
1659 }
1660
segment_bits_seq_show(struct seq_file * seq,void * offset)1661 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1662 void *offset)
1663 {
1664 struct super_block *sb = seq->private;
1665 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1666 unsigned int total_segs =
1667 le32_to_cpu(sbi->raw_super->segment_count_main);
1668 int i, j;
1669
1670 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps|mtime\n"
1671 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1672
1673 for (i = 0; i < total_segs; i++) {
1674 struct seg_entry *se = get_seg_entry(sbi, i);
1675
1676 seq_printf(seq, "%-10d", i);
1677 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1678 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1679 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1680 seq_printf(seq, "| %llx", se->mtime);
1681 seq_putc(seq, '\n');
1682 }
1683 return 0;
1684 }
1685
victim_bits_seq_show(struct seq_file * seq,void * offset)1686 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1687 void *offset)
1688 {
1689 struct super_block *sb = seq->private;
1690 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1691 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1692 int i;
1693
1694 seq_puts(seq, "format: victim_secmap bitmaps\n");
1695
1696 for (i = 0; i < MAIN_SECS(sbi); i++) {
1697 if ((i % 10) == 0)
1698 seq_printf(seq, "%-10d", i);
1699 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1700 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1701 seq_putc(seq, '\n');
1702 else
1703 seq_putc(seq, ' ');
1704 }
1705 return 0;
1706 }
1707
discard_plist_seq_show(struct seq_file * seq,void * offset)1708 static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
1709 void *offset)
1710 {
1711 struct super_block *sb = seq->private;
1712 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1713 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1714 int i, count;
1715
1716 seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
1717 if (!f2fs_realtime_discard_enable(sbi))
1718 return 0;
1719
1720 if (dcc) {
1721 mutex_lock(&dcc->cmd_lock);
1722 for (i = 0; i < MAX_PLIST_NUM; i++) {
1723 struct list_head *pend_list;
1724 struct discard_cmd *dc, *tmp;
1725
1726 if (i % 8 == 0)
1727 seq_printf(seq, " %-3d", i);
1728 count = 0;
1729 pend_list = &dcc->pend_list[i];
1730 list_for_each_entry_safe(dc, tmp, pend_list, list)
1731 count++;
1732 if (count)
1733 seq_printf(seq, " %7d", count);
1734 else
1735 seq_puts(seq, " .");
1736 if (i % 8 == 7)
1737 seq_putc(seq, '\n');
1738 }
1739 seq_putc(seq, '\n');
1740 mutex_unlock(&dcc->cmd_lock);
1741 }
1742
1743 return 0;
1744 }
1745
disk_map_seq_show(struct seq_file * seq,void * offset)1746 static int __maybe_unused disk_map_seq_show(struct seq_file *seq,
1747 void *offset)
1748 {
1749 struct super_block *sb = seq->private;
1750 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1751 int i;
1752
1753 seq_printf(seq, "Address Layout : %5luB Block address (# of Segments)\n",
1754 F2FS_BLKSIZE);
1755 seq_printf(seq, " SB : %12s\n", "0/1024B");
1756 seq_printf(seq, " seg0_blkaddr : 0x%010x\n", SEG0_BLKADDR(sbi));
1757 seq_printf(seq, " Checkpoint : 0x%010x (%10d)\n",
1758 le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2);
1759 seq_printf(seq, " SIT : 0x%010x (%10d)\n",
1760 SIT_I(sbi)->sit_base_addr,
1761 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit));
1762 seq_printf(seq, " NAT : 0x%010x (%10d)\n",
1763 NM_I(sbi)->nat_blkaddr,
1764 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat));
1765 seq_printf(seq, " SSA : 0x%010x (%10d)\n",
1766 SM_I(sbi)->ssa_blkaddr,
1767 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
1768 seq_printf(seq, " Main : 0x%010x (%10d)\n",
1769 SM_I(sbi)->main_blkaddr,
1770 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main));
1771 seq_printf(seq, " Block size : %12lu KB\n", F2FS_BLKSIZE >> 10);
1772 seq_printf(seq, " Segment size : %12d MB\n",
1773 (BLKS_PER_SEG(sbi) << (F2FS_BLKSIZE_BITS - 10)) >> 10);
1774 seq_printf(seq, " Segs/Sections : %12d\n",
1775 SEGS_PER_SEC(sbi));
1776 seq_printf(seq, " Section size : %12d MB\n",
1777 (BLKS_PER_SEC(sbi) << (F2FS_BLKSIZE_BITS - 10)) >> 10);
1778 seq_printf(seq, " # of Sections : %12d\n",
1779 le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count));
1780
1781 if (!f2fs_is_multi_device(sbi))
1782 return 0;
1783
1784 seq_puts(seq, "\nDisk Map for multi devices:\n");
1785 for (i = 0; i < sbi->s_ndevs; i++)
1786 seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n",
1787 i, bdev_is_zoned(FDEV(i).bdev),
1788 FDEV(i).start_blk, FDEV(i).end_blk,
1789 FDEV(i).path);
1790 return 0;
1791 }
1792
donation_list_seq_show(struct seq_file * seq,void * offset)1793 static int __maybe_unused donation_list_seq_show(struct seq_file *seq,
1794 void *offset)
1795 {
1796 struct super_block *sb = seq->private;
1797 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1798 struct inode *inode;
1799 struct f2fs_inode_info *fi;
1800 struct dentry *dentry;
1801 char *buf, *path;
1802 int i;
1803
1804 buf = f2fs_getname(sbi);
1805 if (!buf)
1806 return 0;
1807
1808 seq_printf(seq, "Donation List\n");
1809 seq_printf(seq, " # of files : %u\n", sbi->donate_files);
1810 seq_printf(seq, " %-50s %10s %20s %20s %22s\n",
1811 "File path", "Status", "Donation offset (kb)",
1812 "Donation size (kb)", "File cached size (kb)");
1813 seq_printf(seq, "---\n");
1814
1815 for (i = 0; i < sbi->donate_files; i++) {
1816 spin_lock(&sbi->inode_lock[DONATE_INODE]);
1817 if (list_empty(&sbi->inode_list[DONATE_INODE])) {
1818 spin_unlock(&sbi->inode_lock[DONATE_INODE]);
1819 break;
1820 }
1821 fi = list_first_entry(&sbi->inode_list[DONATE_INODE],
1822 struct f2fs_inode_info, gdonate_list);
1823 list_move_tail(&fi->gdonate_list, &sbi->inode_list[DONATE_INODE]);
1824 inode = igrab(&fi->vfs_inode);
1825 spin_unlock(&sbi->inode_lock[DONATE_INODE]);
1826
1827 if (!inode)
1828 continue;
1829
1830 inode_lock_shared(inode);
1831
1832 dentry = d_find_alias(inode);
1833 if (!dentry) {
1834 path = NULL;
1835 } else {
1836 path = dentry_path_raw(dentry, buf, PATH_MAX);
1837 if (IS_ERR(path))
1838 goto next;
1839 }
1840 seq_printf(seq, " %-50s %10s %20llu %20llu %22llu\n",
1841 path ? path : "<unlinked>",
1842 is_inode_flag_set(inode, FI_DONATE_FINISHED) ?
1843 "Evicted" : "Donated",
1844 (loff_t)fi->donate_start << (PAGE_SHIFT - 10),
1845 (loff_t)(fi->donate_end + 1) << (PAGE_SHIFT - 10),
1846 (loff_t)inode->i_mapping->nrpages << (PAGE_SHIFT - 10));
1847 next:
1848 dput(dentry);
1849 inode_unlock_shared(inode);
1850 iput(inode);
1851 }
1852 f2fs_putname(buf);
1853 return 0;
1854 }
1855
1856 #ifdef CONFIG_F2FS_FAULT_INJECTION
inject_stats_seq_show(struct seq_file * seq,void * offset)1857 static int __maybe_unused inject_stats_seq_show(struct seq_file *seq,
1858 void *offset)
1859 {
1860 struct super_block *sb = seq->private;
1861 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1862 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1863 int i;
1864
1865 seq_puts(seq, "fault_type injected_count\n");
1866
1867 for (i = 0; i < FAULT_MAX; i++)
1868 seq_printf(seq, "%-24s%-10u\n", f2fs_fault_name[i],
1869 ffi->inject_count[i]);
1870 return 0;
1871 }
1872 #endif
1873
f2fs_init_sysfs(void)1874 int __init f2fs_init_sysfs(void)
1875 {
1876 int ret;
1877
1878 kobject_set_name(&f2fs_kset.kobj, "f2fs");
1879 f2fs_kset.kobj.parent = fs_kobj;
1880 ret = kset_register(&f2fs_kset);
1881 if (ret)
1882 return ret;
1883
1884 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1885 NULL, "features");
1886 if (ret)
1887 goto put_kobject;
1888
1889 ret = kobject_init_and_add(&f2fs_tune, &f2fs_tune_ktype,
1890 NULL, "tuning");
1891 if (ret)
1892 goto put_kobject;
1893
1894 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1895 if (!f2fs_proc_root) {
1896 ret = -ENOMEM;
1897 goto put_kobject;
1898 }
1899
1900 return 0;
1901
1902 put_kobject:
1903 kobject_put(&f2fs_tune);
1904 kobject_put(&f2fs_feat);
1905 kset_unregister(&f2fs_kset);
1906 return ret;
1907 }
1908
f2fs_exit_sysfs(void)1909 void f2fs_exit_sysfs(void)
1910 {
1911 kobject_put(&f2fs_tune);
1912 kobject_put(&f2fs_feat);
1913 kset_unregister(&f2fs_kset);
1914 remove_proc_entry("fs/f2fs", NULL);
1915 f2fs_proc_root = NULL;
1916 }
1917
f2fs_register_sysfs(struct f2fs_sb_info * sbi)1918 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1919 {
1920 struct super_block *sb = sbi->sb;
1921 int err;
1922
1923 sbi->s_kobj.kset = &f2fs_kset;
1924 init_completion(&sbi->s_kobj_unregister);
1925 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1926 "%s", sb->s_id);
1927 if (err)
1928 goto put_sb_kobj;
1929
1930 sbi->s_stat_kobj.kset = &f2fs_kset;
1931 init_completion(&sbi->s_stat_kobj_unregister);
1932 err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1933 &sbi->s_kobj, "stat");
1934 if (err)
1935 goto put_stat_kobj;
1936
1937 sbi->s_feature_list_kobj.kset = &f2fs_kset;
1938 init_completion(&sbi->s_feature_list_kobj_unregister);
1939 err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1940 &f2fs_feature_list_ktype,
1941 &sbi->s_kobj, "feature_list");
1942 if (err)
1943 goto put_feature_list_kobj;
1944
1945 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1946 if (!sbi->s_proc) {
1947 err = -ENOMEM;
1948 goto put_feature_list_kobj;
1949 }
1950
1951 proc_create_single_data("segment_info", 0444, sbi->s_proc,
1952 segment_info_seq_show, sb);
1953 proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1954 segment_bits_seq_show, sb);
1955 #ifdef CONFIG_F2FS_IOSTAT
1956 proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1957 iostat_info_seq_show, sb);
1958 #endif
1959 proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1960 victim_bits_seq_show, sb);
1961 proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
1962 discard_plist_seq_show, sb);
1963 proc_create_single_data("disk_map", 0444, sbi->s_proc,
1964 disk_map_seq_show, sb);
1965 proc_create_single_data("donation_list", 0444, sbi->s_proc,
1966 donation_list_seq_show, sb);
1967 #ifdef CONFIG_F2FS_FAULT_INJECTION
1968 proc_create_single_data("inject_stats", 0444, sbi->s_proc,
1969 inject_stats_seq_show, sb);
1970 #endif
1971 return 0;
1972 put_feature_list_kobj:
1973 kobject_put(&sbi->s_feature_list_kobj);
1974 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1975 put_stat_kobj:
1976 kobject_put(&sbi->s_stat_kobj);
1977 wait_for_completion(&sbi->s_stat_kobj_unregister);
1978 put_sb_kobj:
1979 kobject_put(&sbi->s_kobj);
1980 wait_for_completion(&sbi->s_kobj_unregister);
1981 return err;
1982 }
1983
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)1984 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1985 {
1986 remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
1987
1988 kobject_put(&sbi->s_stat_kobj);
1989 wait_for_completion(&sbi->s_stat_kobj_unregister);
1990 kobject_put(&sbi->s_feature_list_kobj);
1991 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1992
1993 kobject_put(&sbi->s_kobj);
1994 wait_for_completion(&sbi->s_kobj_unregister);
1995 }
1996