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