xref: /linux/fs/f2fs/sysfs.c (revision 995832b2cebe6969d1b42635db698803ee31294d)
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 F2FS_FEATURE_RO_ATTR(fserror);
1403 
1404 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
1405 static struct attribute *f2fs_attrs[] = {
1406 	ATTR_LIST(gc_urgent_sleep_time),
1407 	ATTR_LIST(gc_min_sleep_time),
1408 	ATTR_LIST(gc_max_sleep_time),
1409 	ATTR_LIST(gc_no_gc_sleep_time),
1410 	ATTR_LIST(gc_no_zoned_gc_percent),
1411 	ATTR_LIST(gc_boost_zoned_gc_percent),
1412 	ATTR_LIST(gc_valid_thresh_ratio),
1413 	ATTR_LIST(gc_boost_gc_multiple),
1414 	ATTR_LIST(gc_boost_gc_greedy),
1415 	ATTR_LIST(gc_idle),
1416 	ATTR_LIST(gc_urgent),
1417 	ATTR_LIST(reclaim_segments),
1418 	ATTR_LIST(main_blkaddr),
1419 	ATTR_LIST(max_small_discards),
1420 	ATTR_LIST(max_discard_request),
1421 	ATTR_LIST(min_discard_issue_time),
1422 	ATTR_LIST(mid_discard_issue_time),
1423 	ATTR_LIST(max_discard_issue_time),
1424 	ATTR_LIST(discard_io_aware_gran),
1425 	ATTR_LIST(discard_urgent_util),
1426 	ATTR_LIST(discard_granularity),
1427 	ATTR_LIST(max_ordered_discard),
1428 	ATTR_LIST(discard_io_aware),
1429 	ATTR_LIST(pending_discard),
1430 	ATTR_LIST(gc_mode),
1431 	ATTR_LIST(ipu_policy),
1432 	ATTR_LIST(min_ipu_util),
1433 	ATTR_LIST(min_fsync_blocks),
1434 	ATTR_LIST(min_seq_blocks),
1435 	ATTR_LIST(min_hot_blocks),
1436 	ATTR_LIST(min_ssr_sections),
1437 	ATTR_LIST(reserved_segments),
1438 	ATTR_LIST(max_victim_search),
1439 	ATTR_LIST(migration_granularity),
1440 	ATTR_LIST(migration_window_granularity),
1441 	ATTR_LIST(dir_level),
1442 	ATTR_LIST(ram_thresh),
1443 	ATTR_LIST(ra_nid_pages),
1444 	ATTR_LIST(dirty_nats_ratio),
1445 	ATTR_LIST(max_roll_forward_node_blocks),
1446 	ATTR_LIST(cp_interval),
1447 	ATTR_LIST(idle_interval),
1448 	ATTR_LIST(discard_idle_interval),
1449 	ATTR_LIST(gc_idle_interval),
1450 	ATTR_LIST(umount_discard_timeout),
1451 	ATTR_LIST(bggc_io_aware),
1452 #ifdef CONFIG_F2FS_IOSTAT
1453 	ATTR_LIST(iostat_enable),
1454 	ATTR_LIST(iostat_period_ms),
1455 #endif
1456 	ATTR_LIST(readdir_ra),
1457 	ATTR_LIST(max_io_bytes),
1458 	ATTR_LIST(gc_pin_file_thresh),
1459 	ATTR_LIST(extension_list),
1460 #ifdef CONFIG_F2FS_FAULT_INJECTION
1461 	ATTR_LIST(inject_rate),
1462 	ATTR_LIST(inject_type),
1463 	ATTR_LIST(inject_lock_timeout),
1464 #endif
1465 	ATTR_LIST(data_io_flag),
1466 	ATTR_LIST(node_io_flag),
1467 	ATTR_LIST(gc_remaining_trials),
1468 	ATTR_LIST(ckpt_thread_ioprio),
1469 	ATTR_LIST(dirty_segments),
1470 	ATTR_LIST(free_segments),
1471 	ATTR_LIST(ovp_segments),
1472 	ATTR_LIST(unusable),
1473 	ATTR_LIST(lifetime_write_kbytes),
1474 	ATTR_LIST(features),
1475 	ATTR_LIST(reserved_blocks),
1476 	ATTR_LIST(current_reserved_blocks),
1477 	ATTR_LIST(encoding),
1478 	ATTR_LIST(encoding_flags),
1479 	ATTR_LIST(effective_lookup_mode),
1480 	ATTR_LIST(mounted_time_sec),
1481 #ifdef CONFIG_F2FS_STAT_FS
1482 	ATTR_LIST(cp_foreground_calls),
1483 	ATTR_LIST(cp_background_calls),
1484 	ATTR_LIST(gc_foreground_calls),
1485 	ATTR_LIST(gc_background_calls),
1486 	ATTR_LIST(moved_blocks_foreground),
1487 	ATTR_LIST(moved_blocks_background),
1488 	ATTR_LIST(avg_vblocks),
1489 	ATTR_LIST(defrag_blocks),
1490 #endif
1491 #ifdef CONFIG_BLK_DEV_ZONED
1492 	ATTR_LIST(unusable_blocks_per_sec),
1493 	ATTR_LIST(max_open_zones),
1494 	ATTR_LIST(blkzone_alloc_policy),
1495 #endif
1496 #ifdef CONFIG_F2FS_FS_COMPRESSION
1497 	ATTR_LIST(compr_written_block),
1498 	ATTR_LIST(compr_saved_block),
1499 	ATTR_LIST(compr_new_inode),
1500 	ATTR_LIST(compress_percent),
1501 	ATTR_LIST(compress_watermark),
1502 #endif
1503 	/* For ATGC */
1504 	ATTR_LIST(atgc_candidate_ratio),
1505 	ATTR_LIST(atgc_candidate_count),
1506 	ATTR_LIST(atgc_age_weight),
1507 	ATTR_LIST(atgc_age_threshold),
1508 	ATTR_LIST(atgc_enabled),
1509 	ATTR_LIST(seq_file_ra_mul),
1510 	ATTR_LIST(gc_segment_mode),
1511 	ATTR_LIST(gc_reclaimed_segments),
1512 	ATTR_LIST(max_fragment_chunk),
1513 	ATTR_LIST(max_fragment_hole),
1514 	ATTR_LIST(current_atomic_write),
1515 	ATTR_LIST(peak_atomic_write),
1516 	ATTR_LIST(committed_atomic_block),
1517 	ATTR_LIST(revoked_atomic_block),
1518 	ATTR_LIST(hot_data_age_threshold),
1519 	ATTR_LIST(warm_data_age_threshold),
1520 	ATTR_LIST(last_age_weight),
1521 	ATTR_LIST(max_read_extent_count),
1522 	ATTR_LIST(carve_out),
1523 	ATTR_LIST(reserved_pin_section),
1524 	ATTR_LIST(allocate_section_hint),
1525 	ATTR_LIST(allocate_section_policy),
1526 	ATTR_LIST(max_lock_elapsed_time),
1527 	ATTR_LIST(lock_duration_priority),
1528 	ATTR_LIST(adjust_lock_priority),
1529 	ATTR_LIST(critical_task_priority),
1530 	NULL,
1531 };
1532 ATTRIBUTE_GROUPS(f2fs);
1533 
1534 #define BASE_ATTR_LIST(name) (&f2fs_base_attr_##name.attr)
1535 static struct attribute *f2fs_feat_attrs[] = {
1536 #ifdef CONFIG_FS_ENCRYPTION
1537 	BASE_ATTR_LIST(encryption),
1538 	BASE_ATTR_LIST(test_dummy_encryption_v2),
1539 #if IS_ENABLED(CONFIG_UNICODE)
1540 	BASE_ATTR_LIST(encrypted_casefold),
1541 #endif
1542 #endif /* CONFIG_FS_ENCRYPTION */
1543 #ifdef CONFIG_BLK_DEV_ZONED
1544 	BASE_ATTR_LIST(block_zoned),
1545 #endif
1546 	BASE_ATTR_LIST(atomic_write),
1547 	BASE_ATTR_LIST(extra_attr),
1548 	BASE_ATTR_LIST(project_quota),
1549 	BASE_ATTR_LIST(inode_checksum),
1550 	BASE_ATTR_LIST(flexible_inline_xattr),
1551 	BASE_ATTR_LIST(quota_ino),
1552 	BASE_ATTR_LIST(inode_crtime),
1553 	BASE_ATTR_LIST(lost_found),
1554 #ifdef CONFIG_FS_VERITY
1555 	BASE_ATTR_LIST(verity),
1556 #endif
1557 	BASE_ATTR_LIST(sb_checksum),
1558 #if IS_ENABLED(CONFIG_UNICODE)
1559 	BASE_ATTR_LIST(casefold),
1560 #endif
1561 	BASE_ATTR_LIST(readonly),
1562 #ifdef CONFIG_F2FS_FS_COMPRESSION
1563 	BASE_ATTR_LIST(compression),
1564 #endif
1565 	BASE_ATTR_LIST(pin_file),
1566 #ifdef CONFIG_UNICODE
1567 	BASE_ATTR_LIST(linear_lookup),
1568 #endif
1569 	BASE_ATTR_LIST(packed_ssa),
1570 	BASE_ATTR_LIST(fserror),
1571 	NULL,
1572 };
1573 ATTRIBUTE_GROUPS(f2fs_feat);
1574 
1575 F2FS_GENERAL_RO_ATTR(sb_status);
1576 F2FS_GENERAL_RO_ATTR(cp_status);
1577 F2FS_GENERAL_RO_ATTR(issued_discard);
1578 F2FS_GENERAL_RO_ATTR(queued_discard);
1579 F2FS_GENERAL_RO_ATTR(undiscard_blks);
1580 
1581 static struct attribute *f2fs_stat_attrs[] = {
1582 	ATTR_LIST(sb_status),
1583 	ATTR_LIST(cp_status),
1584 	ATTR_LIST(issued_discard),
1585 	ATTR_LIST(queued_discard),
1586 	ATTR_LIST(undiscard_blks),
1587 	NULL,
1588 };
1589 ATTRIBUTE_GROUPS(f2fs_stat);
1590 
1591 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1592 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1593 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1594 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1595 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1596 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1597 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1598 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1599 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1600 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1601 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1602 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1603 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1604 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1605 F2FS_SB_FEATURE_RO_ATTR(device_alias, DEVICE_ALIAS);
1606 F2FS_SB_FEATURE_RO_ATTR(packed_ssa, PACKED_SSA);
1607 
1608 static struct attribute *f2fs_sb_feat_attrs[] = {
1609 	ATTR_LIST(sb_encryption),
1610 	ATTR_LIST(sb_block_zoned),
1611 	ATTR_LIST(sb_extra_attr),
1612 	ATTR_LIST(sb_project_quota),
1613 	ATTR_LIST(sb_inode_checksum),
1614 	ATTR_LIST(sb_flexible_inline_xattr),
1615 	ATTR_LIST(sb_quota_ino),
1616 	ATTR_LIST(sb_inode_crtime),
1617 	ATTR_LIST(sb_lost_found),
1618 	ATTR_LIST(sb_verity),
1619 	ATTR_LIST(sb_sb_checksum),
1620 	ATTR_LIST(sb_casefold),
1621 	ATTR_LIST(sb_compression),
1622 	ATTR_LIST(sb_readonly),
1623 	ATTR_LIST(sb_device_alias),
1624 	ATTR_LIST(sb_packed_ssa),
1625 	NULL,
1626 };
1627 ATTRIBUTE_GROUPS(f2fs_sb_feat);
1628 
1629 F2FS_TUNE_RW_ATTR(reclaim_caches_kb);
1630 
1631 static struct attribute *f2fs_tune_attrs[] = {
1632 	BASE_ATTR_LIST(reclaim_caches_kb),
1633 	NULL,
1634 };
1635 ATTRIBUTE_GROUPS(f2fs_tune);
1636 
1637 static const struct sysfs_ops f2fs_attr_ops = {
1638 	.show	= f2fs_attr_show,
1639 	.store	= f2fs_attr_store,
1640 };
1641 
1642 static const struct kobj_type f2fs_sb_ktype = {
1643 	.default_groups = f2fs_groups,
1644 	.sysfs_ops	= &f2fs_attr_ops,
1645 	.release	= f2fs_sb_release,
1646 };
1647 
1648 static const struct kobj_type f2fs_ktype = {
1649 	.sysfs_ops	= &f2fs_attr_ops,
1650 };
1651 
1652 static struct kset f2fs_kset = {
1653 	.kobj	= {.ktype = &f2fs_ktype},
1654 };
1655 
1656 static const struct sysfs_ops f2fs_feat_attr_ops = {
1657 	.show	= f2fs_base_attr_show,
1658 	.store	= f2fs_base_attr_store,
1659 };
1660 
1661 static const struct kobj_type f2fs_feat_ktype = {
1662 	.default_groups = f2fs_feat_groups,
1663 	.sysfs_ops	= &f2fs_feat_attr_ops,
1664 };
1665 
1666 static struct kobject f2fs_feat = {
1667 	.kset	= &f2fs_kset,
1668 };
1669 
1670 static const struct sysfs_ops f2fs_tune_attr_ops = {
1671 	.show	= f2fs_base_attr_show,
1672 	.store	= f2fs_base_attr_store,
1673 };
1674 
1675 static const struct kobj_type f2fs_tune_ktype = {
1676 	.default_groups = f2fs_tune_groups,
1677 	.sysfs_ops	= &f2fs_tune_attr_ops,
1678 };
1679 
1680 static struct kobject f2fs_tune = {
1681 	.kset	= &f2fs_kset,
1682 };
1683 
1684 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1685 				struct attribute *attr, char *buf)
1686 {
1687 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1688 								s_stat_kobj);
1689 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1690 
1691 	return a->show ? a->show(a, sbi, buf) : 0;
1692 }
1693 
1694 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1695 						const char *buf, size_t len)
1696 {
1697 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1698 								s_stat_kobj);
1699 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1700 
1701 	return a->store ? a->store(a, sbi, buf, len) : 0;
1702 }
1703 
1704 static void f2fs_stat_kobj_release(struct kobject *kobj)
1705 {
1706 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1707 								s_stat_kobj);
1708 	complete(&sbi->s_stat_kobj_unregister);
1709 }
1710 
1711 static const struct sysfs_ops f2fs_stat_attr_ops = {
1712 	.show	= f2fs_stat_attr_show,
1713 	.store	= f2fs_stat_attr_store,
1714 };
1715 
1716 static const struct kobj_type f2fs_stat_ktype = {
1717 	.default_groups = f2fs_stat_groups,
1718 	.sysfs_ops	= &f2fs_stat_attr_ops,
1719 	.release	= f2fs_stat_kobj_release,
1720 };
1721 
1722 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1723 				struct attribute *attr, char *buf)
1724 {
1725 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1726 							s_feature_list_kobj);
1727 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1728 
1729 	return a->show ? a->show(a, sbi, buf) : 0;
1730 }
1731 
1732 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1733 {
1734 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1735 							s_feature_list_kobj);
1736 	complete(&sbi->s_feature_list_kobj_unregister);
1737 }
1738 
1739 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1740 	.show	= f2fs_sb_feat_attr_show,
1741 };
1742 
1743 static const struct kobj_type f2fs_feature_list_ktype = {
1744 	.default_groups = f2fs_sb_feat_groups,
1745 	.sysfs_ops	= &f2fs_feature_list_attr_ops,
1746 	.release	= f2fs_feature_list_kobj_release,
1747 };
1748 
1749 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1750 						void *offset)
1751 {
1752 	struct super_block *sb = seq->private;
1753 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1754 	unsigned int total_segs =
1755 			le32_to_cpu(sbi->raw_super->segment_count_main);
1756 	int i;
1757 
1758 	seq_puts(seq, "format: segment_type|valid_blocks\n"
1759 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1760 
1761 	for (i = 0; i < total_segs; i++) {
1762 		struct seg_entry *se = get_seg_entry(sbi, i);
1763 
1764 		if ((i % 10) == 0)
1765 			seq_printf(seq, "%-10d", i);
1766 		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1767 		if ((i % 10) == 9 || i == (total_segs - 1))
1768 			seq_putc(seq, '\n');
1769 		else
1770 			seq_putc(seq, ' ');
1771 	}
1772 
1773 	return 0;
1774 }
1775 
1776 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1777 						void *offset)
1778 {
1779 	struct super_block *sb = seq->private;
1780 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1781 	unsigned int total_segs =
1782 			le32_to_cpu(sbi->raw_super->segment_count_main);
1783 	int i, j;
1784 
1785 	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps|mtime\n"
1786 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1787 
1788 	for (i = 0; i < total_segs; i++) {
1789 		struct seg_entry *se = get_seg_entry(sbi, i);
1790 
1791 		seq_printf(seq, "%-10d", i);
1792 		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1793 		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1794 			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1795 		seq_printf(seq, "| %llx", se->mtime);
1796 		seq_putc(seq, '\n');
1797 	}
1798 	return 0;
1799 }
1800 
1801 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1802 						void *offset)
1803 {
1804 	struct super_block *sb = seq->private;
1805 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1806 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1807 	int i;
1808 
1809 	seq_puts(seq, "format: victim_secmap bitmaps\n");
1810 
1811 	for (i = 0; i < MAIN_SECS(sbi); i++) {
1812 		if ((i % 10) == 0)
1813 			seq_printf(seq, "%-10d", i);
1814 		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1815 		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1816 			seq_putc(seq, '\n');
1817 		else
1818 			seq_putc(seq, ' ');
1819 	}
1820 	return 0;
1821 }
1822 
1823 static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
1824 						void *offset)
1825 {
1826 	struct super_block *sb = seq->private;
1827 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1828 	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1829 	int i, count;
1830 
1831 	seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
1832 	if (!f2fs_realtime_discard_enable(sbi))
1833 		return 0;
1834 
1835 	if (dcc) {
1836 		mutex_lock(&dcc->cmd_lock);
1837 		for (i = 0; i < MAX_PLIST_NUM; i++) {
1838 			struct list_head *pend_list;
1839 			struct discard_cmd *dc, *tmp;
1840 
1841 			if (i % 8 == 0)
1842 				seq_printf(seq, "  %-3d", i);
1843 			count = 0;
1844 			pend_list = &dcc->pend_list[i];
1845 			list_for_each_entry_safe(dc, tmp, pend_list, list)
1846 				count++;
1847 			if (count)
1848 				seq_printf(seq, " %7d", count);
1849 			else
1850 				seq_puts(seq, "       .");
1851 			if (i % 8 == 7)
1852 				seq_putc(seq, '\n');
1853 		}
1854 		seq_putc(seq, '\n');
1855 		mutex_unlock(&dcc->cmd_lock);
1856 	}
1857 
1858 	return 0;
1859 }
1860 
1861 static int __maybe_unused disk_map_seq_show(struct seq_file *seq,
1862 						void *offset)
1863 {
1864 	struct super_block *sb = seq->private;
1865 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1866 	int i;
1867 
1868 	seq_printf(seq, "Address Layout   : %5luB Block address (# of Segments)\n",
1869 					F2FS_BLKSIZE);
1870 	seq_printf(seq, " SB            : %12s\n", "0/1024B");
1871 	seq_printf(seq, " seg0_blkaddr  : 0x%010x\n", SEG0_BLKADDR(sbi));
1872 	seq_printf(seq, " Checkpoint    : 0x%010x (%10d)\n",
1873 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2);
1874 	seq_printf(seq, " SIT           : 0x%010x (%10d)\n",
1875 			SIT_I(sbi)->sit_base_addr,
1876 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit));
1877 	seq_printf(seq, " NAT           : 0x%010x (%10d)\n",
1878 			NM_I(sbi)->nat_blkaddr,
1879 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat));
1880 	seq_printf(seq, " SSA           : 0x%010x (%10d)\n",
1881 			SM_I(sbi)->ssa_blkaddr,
1882 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
1883 	seq_printf(seq, " Main          : 0x%010x (%10d)\n",
1884 			SM_I(sbi)->main_blkaddr,
1885 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main));
1886 	seq_printf(seq, " Block size    : %12lu KB\n", F2FS_BLKSIZE >> 10);
1887 	seq_printf(seq, " Segment size  : %12d MB\n",
1888 			(BLKS_PER_SEG(sbi) << (F2FS_BLKSIZE_BITS - 10)) >> 10);
1889 	seq_printf(seq, " Segs/Sections : %12d\n",
1890 			SEGS_PER_SEC(sbi));
1891 	seq_printf(seq, " Section size  : %12d MB\n",
1892 			(BLKS_PER_SEC(sbi) << (F2FS_BLKSIZE_BITS - 10)) >> 10);
1893 	seq_printf(seq, " # of Sections : %12d\n",
1894 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count));
1895 
1896 	if (!f2fs_is_multi_device(sbi))
1897 		return 0;
1898 
1899 	seq_puts(seq, "\nDisk Map for multi devices:\n");
1900 	for (i = 0; i < sbi->s_ndevs; i++)
1901 		seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n",
1902 			i, bdev_is_zoned(FDEV(i).bdev),
1903 			FDEV(i).start_blk, FDEV(i).end_blk,
1904 			FDEV(i).path);
1905 	return 0;
1906 }
1907 
1908 static int __maybe_unused donation_list_seq_show(struct seq_file *seq,
1909 						void *offset)
1910 {
1911 	struct super_block *sb = seq->private;
1912 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1913 	struct inode *inode;
1914 	struct f2fs_inode_info *fi;
1915 	struct dentry *dentry;
1916 	char *buf, *path;
1917 	int i;
1918 
1919 	buf = f2fs_getname(sbi);
1920 	if (!buf)
1921 		return 0;
1922 
1923 	seq_printf(seq, "Donation List\n");
1924 	seq_printf(seq, " # of files  : %u\n", sbi->donate_files);
1925 	seq_printf(seq, " %-50s %10s %20s %20s %22s\n",
1926 			"File path", "Status", "Donation offset (kb)",
1927 			"Donation size (kb)", "File cached size (kb)");
1928 	seq_printf(seq, "---\n");
1929 
1930 	for (i = 0; i < sbi->donate_files; i++) {
1931 		spin_lock(&sbi->inode_lock[DONATE_INODE]);
1932 		if (list_empty(&sbi->inode_list[DONATE_INODE])) {
1933 			spin_unlock(&sbi->inode_lock[DONATE_INODE]);
1934 			break;
1935 		}
1936 		fi = list_first_entry(&sbi->inode_list[DONATE_INODE],
1937 					struct f2fs_inode_info, gdonate_list);
1938 		list_move_tail(&fi->gdonate_list, &sbi->inode_list[DONATE_INODE]);
1939 		inode = igrab(&fi->vfs_inode);
1940 		spin_unlock(&sbi->inode_lock[DONATE_INODE]);
1941 
1942 		if (!inode)
1943 			continue;
1944 
1945 		inode_lock_shared(inode);
1946 
1947 		dentry = d_find_alias(inode);
1948 		if (!dentry) {
1949 			path = NULL;
1950 		} else {
1951 			path = dentry_path_raw(dentry, buf, PATH_MAX);
1952 			if (IS_ERR(path))
1953 				goto next;
1954 		}
1955 		seq_printf(seq, " %-50s %10s %20llu %20llu %22llu\n",
1956 				path ? path : "<unlinked>",
1957 				is_inode_flag_set(inode, FI_DONATE_FINISHED) ?
1958 				"Evicted" : "Donated",
1959 				(loff_t)fi->donate_start << (PAGE_SHIFT - 10),
1960 				(loff_t)(fi->donate_end + 1) << (PAGE_SHIFT - 10),
1961 				(loff_t)inode->i_mapping->nrpages << (PAGE_SHIFT - 10));
1962 next:
1963 		dput(dentry);
1964 		inode_unlock_shared(inode);
1965 		iput(inode);
1966 	}
1967 	f2fs_putname(buf);
1968 	return 0;
1969 }
1970 
1971 #ifdef CONFIG_F2FS_FAULT_INJECTION
1972 static int __maybe_unused inject_stats_seq_show(struct seq_file *seq,
1973 						void *offset)
1974 {
1975 	struct super_block *sb = seq->private;
1976 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1977 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1978 	int i;
1979 
1980 	seq_puts(seq, "fault_type		injected_count\n");
1981 
1982 	for (i = 0; i < FAULT_MAX; i++)
1983 		seq_printf(seq, "%-24s%-10u\n", f2fs_fault_name[i],
1984 						ffi->inject_count[i]);
1985 	return 0;
1986 }
1987 #endif
1988 
1989 int __init f2fs_init_sysfs(void)
1990 {
1991 	int ret;
1992 
1993 	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1994 	f2fs_kset.kobj.parent = fs_kobj;
1995 	ret = kset_register(&f2fs_kset);
1996 	if (ret)
1997 		return ret;
1998 
1999 	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
2000 				   NULL, "features");
2001 	if (ret)
2002 		goto unregister_kset;
2003 
2004 	ret = kobject_init_and_add(&f2fs_tune, &f2fs_tune_ktype,
2005 				   NULL, "tuning");
2006 	if (ret)
2007 		goto put_feat;
2008 
2009 	f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
2010 	if (!f2fs_proc_root) {
2011 		ret = -ENOMEM;
2012 		goto put_tune;
2013 	}
2014 
2015 	return 0;
2016 
2017 put_tune:
2018 	kobject_put(&f2fs_tune);
2019 put_feat:
2020 	kobject_put(&f2fs_feat);
2021 unregister_kset:
2022 	kset_unregister(&f2fs_kset);
2023 	return ret;
2024 }
2025 
2026 void f2fs_exit_sysfs(void)
2027 {
2028 	kobject_put(&f2fs_tune);
2029 	kobject_put(&f2fs_feat);
2030 	kset_unregister(&f2fs_kset);
2031 	remove_proc_entry("fs/f2fs", NULL);
2032 	f2fs_proc_root = NULL;
2033 }
2034 
2035 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
2036 {
2037 	struct super_block *sb = sbi->sb;
2038 	int err;
2039 
2040 	sbi->s_kobj.kset = &f2fs_kset;
2041 	init_completion(&sbi->s_kobj_unregister);
2042 	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
2043 				"%s", sb->s_id);
2044 	if (err)
2045 		goto put_sb_kobj;
2046 
2047 	sbi->s_stat_kobj.kset = &f2fs_kset;
2048 	init_completion(&sbi->s_stat_kobj_unregister);
2049 	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
2050 						&sbi->s_kobj, "stat");
2051 	if (err)
2052 		goto put_stat_kobj;
2053 
2054 	sbi->s_feature_list_kobj.kset = &f2fs_kset;
2055 	init_completion(&sbi->s_feature_list_kobj_unregister);
2056 	err = kobject_init_and_add(&sbi->s_feature_list_kobj,
2057 					&f2fs_feature_list_ktype,
2058 					&sbi->s_kobj, "feature_list");
2059 	if (err)
2060 		goto put_feature_list_kobj;
2061 
2062 	sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
2063 	if (!sbi->s_proc) {
2064 		err = -ENOMEM;
2065 		goto put_feature_list_kobj;
2066 	}
2067 
2068 	proc_create_single_data("segment_info", 0444, sbi->s_proc,
2069 				segment_info_seq_show, sb);
2070 	proc_create_single_data("segment_bits", 0444, sbi->s_proc,
2071 				segment_bits_seq_show, sb);
2072 #ifdef CONFIG_F2FS_IOSTAT
2073 	proc_create_single_data("iostat_info", 0444, sbi->s_proc,
2074 				iostat_info_seq_show, sb);
2075 #endif
2076 	proc_create_single_data("victim_bits", 0444, sbi->s_proc,
2077 				victim_bits_seq_show, sb);
2078 	proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
2079 				discard_plist_seq_show, sb);
2080 	proc_create_single_data("disk_map", 0444, sbi->s_proc,
2081 				disk_map_seq_show, sb);
2082 	proc_create_single_data("donation_list", 0444, sbi->s_proc,
2083 				donation_list_seq_show, sb);
2084 #ifdef CONFIG_F2FS_FAULT_INJECTION
2085 	proc_create_single_data("inject_stats", 0444, sbi->s_proc,
2086 				inject_stats_seq_show, sb);
2087 #endif
2088 	return 0;
2089 put_feature_list_kobj:
2090 	kobject_put(&sbi->s_feature_list_kobj);
2091 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
2092 put_stat_kobj:
2093 	kobject_put(&sbi->s_stat_kobj);
2094 	wait_for_completion(&sbi->s_stat_kobj_unregister);
2095 put_sb_kobj:
2096 	kobject_put(&sbi->s_kobj);
2097 	wait_for_completion(&sbi->s_kobj_unregister);
2098 	return err;
2099 }
2100 
2101 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
2102 {
2103 	remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
2104 
2105 	kobject_put(&sbi->s_stat_kobj);
2106 	wait_for_completion(&sbi->s_stat_kobj_unregister);
2107 	kobject_put(&sbi->s_feature_list_kobj);
2108 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
2109 
2110 	kobject_put(&sbi->s_kobj);
2111 	wait_for_completion(&sbi->s_kobj_unregister);
2112 }
2113