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