xref: /openbmc/linux/fs/f2fs/sysfs.c (revision 44900c3e)
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 	ui = (unsigned int *)(ptr + a->offset);
360 
361 	return sysfs_emit(buf, "%u\n", *ui);
362 }
363 
364 static ssize_t __sbi_store(struct f2fs_attr *a,
365 			struct f2fs_sb_info *sbi,
366 			const char *buf, size_t count)
367 {
368 	unsigned char *ptr;
369 	unsigned long t;
370 	unsigned int *ui;
371 	ssize_t ret;
372 
373 	ptr = __struct_ptr(sbi, a->struct_type);
374 	if (!ptr)
375 		return -EINVAL;
376 
377 	if (!strcmp(a->attr.name, "extension_list")) {
378 		const char *name = strim((char *)buf);
379 		bool set = true, hot;
380 
381 		if (!strncmp(name, "[h]", 3))
382 			hot = true;
383 		else if (!strncmp(name, "[c]", 3))
384 			hot = false;
385 		else
386 			return -EINVAL;
387 
388 		name += 3;
389 
390 		if (*name == '!') {
391 			name++;
392 			set = false;
393 		}
394 
395 		if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
396 			return -EINVAL;
397 
398 		f2fs_down_write(&sbi->sb_lock);
399 
400 		ret = f2fs_update_extension_list(sbi, name, hot, set);
401 		if (ret)
402 			goto out;
403 
404 		ret = f2fs_commit_super(sbi, false);
405 		if (ret)
406 			f2fs_update_extension_list(sbi, name, hot, !set);
407 out:
408 		f2fs_up_write(&sbi->sb_lock);
409 		return ret ? ret : count;
410 	}
411 
412 	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
413 		const char *name = strim((char *)buf);
414 		struct ckpt_req_control *cprc = &sbi->cprc_info;
415 		int class;
416 		long data;
417 		int ret;
418 
419 		if (!strncmp(name, "rt,", 3))
420 			class = IOPRIO_CLASS_RT;
421 		else if (!strncmp(name, "be,", 3))
422 			class = IOPRIO_CLASS_BE;
423 		else
424 			return -EINVAL;
425 
426 		name += 3;
427 		ret = kstrtol(name, 10, &data);
428 		if (ret)
429 			return ret;
430 		if (data >= IOPRIO_NR_LEVELS || data < 0)
431 			return -EINVAL;
432 
433 		cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
434 		if (test_opt(sbi, MERGE_CHECKPOINT)) {
435 			ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
436 					cprc->ckpt_thread_ioprio);
437 			if (ret)
438 				return ret;
439 		}
440 
441 		return count;
442 	}
443 
444 	ui = (unsigned int *)(ptr + a->offset);
445 
446 	ret = kstrtoul(skip_spaces(buf), 0, &t);
447 	if (ret < 0)
448 		return ret;
449 #ifdef CONFIG_F2FS_FAULT_INJECTION
450 	if (a->struct_type == FAULT_INFO_TYPE && t >= BIT(FAULT_MAX))
451 		return -EINVAL;
452 	if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
453 		return -EINVAL;
454 #endif
455 	if (a->struct_type == RESERVED_BLOCKS) {
456 		spin_lock(&sbi->stat_lock);
457 		if (t > (unsigned long)(sbi->user_block_count -
458 				F2FS_OPTION(sbi).root_reserved_blocks -
459 				sbi->blocks_per_seg *
460 				SM_I(sbi)->additional_reserved_segments)) {
461 			spin_unlock(&sbi->stat_lock);
462 			return -EINVAL;
463 		}
464 		*ui = t;
465 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
466 				sbi->user_block_count - valid_user_blocks(sbi));
467 		spin_unlock(&sbi->stat_lock);
468 		return count;
469 	}
470 
471 	if (!strcmp(a->attr.name, "discard_io_aware_gran")) {
472 		if (t > MAX_PLIST_NUM)
473 			return -EINVAL;
474 		if (!f2fs_block_unit_discard(sbi))
475 			return -EINVAL;
476 		if (t == *ui)
477 			return count;
478 		*ui = t;
479 		return count;
480 	}
481 
482 	if (!strcmp(a->attr.name, "discard_granularity")) {
483 		if (t == 0 || t > MAX_PLIST_NUM)
484 			return -EINVAL;
485 		if (!f2fs_block_unit_discard(sbi))
486 			return -EINVAL;
487 		if (t == *ui)
488 			return count;
489 		*ui = t;
490 		return count;
491 	}
492 
493 	if (!strcmp(a->attr.name, "max_ordered_discard")) {
494 		if (t == 0 || t > MAX_PLIST_NUM)
495 			return -EINVAL;
496 		if (!f2fs_block_unit_discard(sbi))
497 			return -EINVAL;
498 		*ui = t;
499 		return count;
500 	}
501 
502 	if (!strcmp(a->attr.name, "discard_urgent_util")) {
503 		if (t > 100)
504 			return -EINVAL;
505 		*ui = t;
506 		return count;
507 	}
508 
509 	if (!strcmp(a->attr.name, "migration_granularity")) {
510 		if (t == 0 || t > sbi->segs_per_sec)
511 			return -EINVAL;
512 	}
513 
514 	if (!strcmp(a->attr.name, "gc_urgent")) {
515 		if (t == 0) {
516 			sbi->gc_mode = GC_NORMAL;
517 		} else if (t == 1) {
518 			sbi->gc_mode = GC_URGENT_HIGH;
519 			if (sbi->gc_thread) {
520 				sbi->gc_thread->gc_wake = true;
521 				wake_up_interruptible_all(
522 					&sbi->gc_thread->gc_wait_queue_head);
523 				wake_up_discard_thread(sbi, true);
524 			}
525 		} else if (t == 2) {
526 			sbi->gc_mode = GC_URGENT_LOW;
527 		} else if (t == 3) {
528 			sbi->gc_mode = GC_URGENT_MID;
529 			if (sbi->gc_thread) {
530 				sbi->gc_thread->gc_wake = true;
531 				wake_up_interruptible_all(
532 					&sbi->gc_thread->gc_wait_queue_head);
533 			}
534 		} else {
535 			return -EINVAL;
536 		}
537 		return count;
538 	}
539 	if (!strcmp(a->attr.name, "gc_idle")) {
540 		if (t == GC_IDLE_CB) {
541 			sbi->gc_mode = GC_IDLE_CB;
542 		} else if (t == GC_IDLE_GREEDY) {
543 			sbi->gc_mode = GC_IDLE_GREEDY;
544 		} else if (t == GC_IDLE_AT) {
545 			if (!sbi->am.atgc_enabled)
546 				return -EINVAL;
547 			sbi->gc_mode = GC_IDLE_AT;
548 		} else {
549 			sbi->gc_mode = GC_NORMAL;
550 		}
551 		return count;
552 	}
553 
554 	if (!strcmp(a->attr.name, "gc_remaining_trials")) {
555 		spin_lock(&sbi->gc_remaining_trials_lock);
556 		sbi->gc_remaining_trials = t;
557 		spin_unlock(&sbi->gc_remaining_trials_lock);
558 
559 		return count;
560 	}
561 
562 #ifdef CONFIG_F2FS_IOSTAT
563 	if (!strcmp(a->attr.name, "iostat_enable")) {
564 		sbi->iostat_enable = !!t;
565 		if (!sbi->iostat_enable)
566 			f2fs_reset_iostat(sbi);
567 		return count;
568 	}
569 
570 	if (!strcmp(a->attr.name, "iostat_period_ms")) {
571 		if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
572 			return -EINVAL;
573 		spin_lock_irq(&sbi->iostat_lock);
574 		sbi->iostat_period_ms = (unsigned int)t;
575 		spin_unlock_irq(&sbi->iostat_lock);
576 		return count;
577 	}
578 #endif
579 
580 #ifdef CONFIG_F2FS_FS_COMPRESSION
581 	if (!strcmp(a->attr.name, "compr_written_block") ||
582 		!strcmp(a->attr.name, "compr_saved_block")) {
583 		if (t != 0)
584 			return -EINVAL;
585 		sbi->compr_written_block = 0;
586 		sbi->compr_saved_block = 0;
587 		return count;
588 	}
589 
590 	if (!strcmp(a->attr.name, "compr_new_inode")) {
591 		if (t != 0)
592 			return -EINVAL;
593 		sbi->compr_new_inode = 0;
594 		return count;
595 	}
596 
597 	if (!strcmp(a->attr.name, "compress_percent")) {
598 		if (t == 0 || t > 100)
599 			return -EINVAL;
600 		*ui = t;
601 		return count;
602 	}
603 
604 	if (!strcmp(a->attr.name, "compress_watermark")) {
605 		if (t == 0 || t > 100)
606 			return -EINVAL;
607 		*ui = t;
608 		return count;
609 	}
610 #endif
611 
612 	if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
613 		if (t > 100)
614 			return -EINVAL;
615 		sbi->am.candidate_ratio = t;
616 		return count;
617 	}
618 
619 	if (!strcmp(a->attr.name, "atgc_age_weight")) {
620 		if (t > 100)
621 			return -EINVAL;
622 		sbi->am.age_weight = t;
623 		return count;
624 	}
625 
626 	if (!strcmp(a->attr.name, "gc_segment_mode")) {
627 		if (t < MAX_GC_MODE)
628 			sbi->gc_segment_mode = t;
629 		else
630 			return -EINVAL;
631 		return count;
632 	}
633 
634 	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
635 		if (t != 0)
636 			return -EINVAL;
637 		sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
638 		return count;
639 	}
640 
641 	if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
642 		if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
643 			sbi->seq_file_ra_mul = t;
644 		else
645 			return -EINVAL;
646 		return count;
647 	}
648 
649 	if (!strcmp(a->attr.name, "max_fragment_chunk")) {
650 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
651 			sbi->max_fragment_chunk = t;
652 		else
653 			return -EINVAL;
654 		return count;
655 	}
656 
657 	if (!strcmp(a->attr.name, "max_fragment_hole")) {
658 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
659 			sbi->max_fragment_hole = t;
660 		else
661 			return -EINVAL;
662 		return count;
663 	}
664 
665 	if (!strcmp(a->attr.name, "peak_atomic_write")) {
666 		if (t != 0)
667 			return -EINVAL;
668 		sbi->peak_atomic_write = 0;
669 		return count;
670 	}
671 
672 	if (!strcmp(a->attr.name, "committed_atomic_block")) {
673 		if (t != 0)
674 			return -EINVAL;
675 		sbi->committed_atomic_block = 0;
676 		return count;
677 	}
678 
679 	if (!strcmp(a->attr.name, "revoked_atomic_block")) {
680 		if (t != 0)
681 			return -EINVAL;
682 		sbi->revoked_atomic_block = 0;
683 		return count;
684 	}
685 
686 	if (!strcmp(a->attr.name, "readdir_ra")) {
687 		sbi->readdir_ra = !!t;
688 		return count;
689 	}
690 
691 	if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
692 		if (t == 0 || t >= sbi->warm_data_age_threshold)
693 			return -EINVAL;
694 		if (t == *ui)
695 			return count;
696 		*ui = (unsigned int)t;
697 		return count;
698 	}
699 
700 	if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
701 		if (t <= sbi->hot_data_age_threshold)
702 			return -EINVAL;
703 		if (t == *ui)
704 			return count;
705 		*ui = (unsigned int)t;
706 		return count;
707 	}
708 
709 	if (!strcmp(a->attr.name, "last_age_weight")) {
710 		if (t > 100)
711 			return -EINVAL;
712 		if (t == *ui)
713 			return count;
714 		*ui = (unsigned int)t;
715 		return count;
716 	}
717 
718 	if (!strcmp(a->attr.name, "ipu_policy")) {
719 		if (t >= BIT(F2FS_IPU_MAX))
720 			return -EINVAL;
721 		if (t && f2fs_lfs_mode(sbi))
722 			return -EINVAL;
723 		SM_I(sbi)->ipu_policy = (unsigned int)t;
724 		return count;
725 	}
726 
727 	*ui = (unsigned int)t;
728 
729 	return count;
730 }
731 
732 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
733 			struct f2fs_sb_info *sbi,
734 			const char *buf, size_t count)
735 {
736 	ssize_t ret;
737 	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
738 					a->struct_type == GC_THREAD);
739 
740 	if (gc_entry) {
741 		if (!down_read_trylock(&sbi->sb->s_umount))
742 			return -EAGAIN;
743 	}
744 	ret = __sbi_store(a, sbi, buf, count);
745 	if (gc_entry)
746 		up_read(&sbi->sb->s_umount);
747 
748 	return ret;
749 }
750 
751 static ssize_t f2fs_attr_show(struct kobject *kobj,
752 				struct attribute *attr, char *buf)
753 {
754 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
755 								s_kobj);
756 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
757 
758 	return a->show ? a->show(a, sbi, buf) : 0;
759 }
760 
761 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
762 						const char *buf, size_t len)
763 {
764 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
765 									s_kobj);
766 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
767 
768 	return a->store ? a->store(a, sbi, buf, len) : 0;
769 }
770 
771 static void f2fs_sb_release(struct kobject *kobj)
772 {
773 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
774 								s_kobj);
775 	complete(&sbi->s_kobj_unregister);
776 }
777 
778 /*
779  * Note that there are three feature list entries:
780  * 1) /sys/fs/f2fs/features
781  *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
782  *     - ref. F2FS_FEATURE_RO_ATTR()
783  *
784  * 2) /sys/fs/f2fs/$s_id/features <deprecated>
785  *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
786  *     won't add new feature anymore, and thus, users should check entries in 3)
787  *     instead of this 2).
788  *
789  * 3) /sys/fs/f2fs/$s_id/feature_list
790  *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
791  *     sysfs entry rule where each entry should expose single value.
792  *     This list covers old feature list provided by 2) and beyond. Therefore,
793  *     please add new on-disk feature in this list only.
794  *     - ref. F2FS_SB_FEATURE_RO_ATTR()
795  */
796 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
797 		struct f2fs_sb_info *sbi, char *buf)
798 {
799 	return sysfs_emit(buf, "supported\n");
800 }
801 
802 #define F2FS_FEATURE_RO_ATTR(_name)				\
803 static struct f2fs_attr f2fs_attr_##_name = {			\
804 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
805 	.show	= f2fs_feature_show,				\
806 }
807 
808 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
809 		struct f2fs_sb_info *sbi, char *buf)
810 {
811 	if (F2FS_HAS_FEATURE(sbi, a->id))
812 		return sysfs_emit(buf, "supported\n");
813 	return sysfs_emit(buf, "unsupported\n");
814 }
815 
816 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
817 static struct f2fs_attr f2fs_attr_sb_##_name = {		\
818 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
819 	.show	= f2fs_sb_feature_show,				\
820 	.id	= F2FS_FEATURE_##_feat,				\
821 }
822 
823 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
824 static struct f2fs_attr f2fs_attr_##_name = {			\
825 	.attr = {.name = __stringify(_name), .mode = _mode },	\
826 	.show	= _show,					\
827 	.store	= _store,					\
828 	.struct_type = _struct_type,				\
829 	.offset = _offset					\
830 }
831 
832 #define F2FS_RO_ATTR(struct_type, struct_name, name, elname)	\
833 	F2FS_ATTR_OFFSET(struct_type, name, 0444,		\
834 		f2fs_sbi_show, NULL,				\
835 		offsetof(struct struct_name, elname))
836 
837 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
838 	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
839 		f2fs_sbi_show, f2fs_sbi_store,			\
840 		offsetof(struct struct_name, elname))
841 
842 #define F2FS_GENERAL_RO_ATTR(name) \
843 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
844 
845 #ifdef CONFIG_F2FS_STAT_FS
846 #define STAT_INFO_RO_ATTR(name, elname)				\
847 	F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname)
848 #endif
849 
850 #define GC_THREAD_RW_ATTR(name, elname)				\
851 	F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname)
852 
853 #define SM_INFO_RW_ATTR(name, elname)				\
854 	F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname)
855 
856 #define SM_INFO_GENERAL_RW_ATTR(elname)				\
857 	SM_INFO_RW_ATTR(elname, elname)
858 
859 #define DCC_INFO_RW_ATTR(name, elname)				\
860 	F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname)
861 
862 #define DCC_INFO_GENERAL_RW_ATTR(elname)			\
863 	DCC_INFO_RW_ATTR(elname, elname)
864 
865 #define NM_INFO_RW_ATTR(name, elname)				\
866 	F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname)
867 
868 #define NM_INFO_GENERAL_RW_ATTR(elname)				\
869 	NM_INFO_RW_ATTR(elname, elname)
870 
871 #define F2FS_SBI_RW_ATTR(name, elname)				\
872 	F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname)
873 
874 #define F2FS_SBI_GENERAL_RW_ATTR(elname)			\
875 	F2FS_SBI_RW_ATTR(elname, elname)
876 
877 #define F2FS_SBI_GENERAL_RO_ATTR(elname)			\
878 	F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname)
879 
880 #ifdef CONFIG_F2FS_FAULT_INJECTION
881 #define FAULT_INFO_GENERAL_RW_ATTR(type, elname)		\
882 	F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname)
883 #endif
884 
885 #define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname)			\
886 	F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname)
887 
888 #define CPRC_INFO_GENERAL_RW_ATTR(elname)			\
889 	F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname)
890 
891 #define ATGC_INFO_RW_ATTR(name, elname)				\
892 	F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
893 
894 /* GC_THREAD ATTR */
895 GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
896 GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
897 GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time);
898 GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
899 
900 /* SM_INFO ATTR */
901 SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments);
902 SM_INFO_GENERAL_RW_ATTR(ipu_policy);
903 SM_INFO_GENERAL_RW_ATTR(min_ipu_util);
904 SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks);
905 SM_INFO_GENERAL_RW_ATTR(min_seq_blocks);
906 SM_INFO_GENERAL_RW_ATTR(min_hot_blocks);
907 SM_INFO_GENERAL_RW_ATTR(min_ssr_sections);
908 
909 /* DCC_INFO ATTR */
910 DCC_INFO_RW_ATTR(max_small_discards, max_discards);
911 DCC_INFO_GENERAL_RW_ATTR(max_discard_request);
912 DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time);
913 DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
914 DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
915 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
916 DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
917 DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
918 DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
919 
920 /* NM_INFO ATTR */
921 NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
922 NM_INFO_GENERAL_RW_ATTR(ram_thresh);
923 NM_INFO_GENERAL_RW_ATTR(ra_nid_pages);
924 NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio);
925 
926 /* F2FS_SBI ATTR */
927 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
928 F2FS_SBI_RW_ATTR(gc_idle, gc_mode);
929 F2FS_SBI_RW_ATTR(gc_urgent, gc_mode);
930 F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]);
931 F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]);
932 F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]);
933 F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]);
934 F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
935 F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold);
936 F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs);
937 F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
938 F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
939 F2FS_SBI_GENERAL_RW_ATTR(dir_level);
940 #ifdef CONFIG_F2FS_IOSTAT
941 F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
942 F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
943 #endif
944 F2FS_SBI_GENERAL_RW_ATTR(readdir_ra);
945 F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes);
946 F2FS_SBI_GENERAL_RW_ATTR(data_io_flag);
947 F2FS_SBI_GENERAL_RW_ATTR(node_io_flag);
948 F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials);
949 F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul);
950 F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode);
951 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk);
952 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole);
953 #ifdef CONFIG_F2FS_FS_COMPRESSION
954 F2FS_SBI_GENERAL_RW_ATTR(compr_written_block);
955 F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block);
956 F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode);
957 F2FS_SBI_GENERAL_RW_ATTR(compress_percent);
958 F2FS_SBI_GENERAL_RW_ATTR(compress_watermark);
959 #endif
960 /* atomic write */
961 F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write);
962 F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write);
963 F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block);
964 F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block);
965 /* block age extent cache */
966 F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold);
967 F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold);
968 F2FS_SBI_GENERAL_RW_ATTR(last_age_weight);
969 #ifdef CONFIG_BLK_DEV_ZONED
970 F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec);
971 #endif
972 
973 /* STAT_INFO ATTR */
974 #ifdef CONFIG_F2FS_STAT_FS
975 STAT_INFO_RO_ATTR(cp_foreground_calls, cp_count);
976 STAT_INFO_RO_ATTR(cp_background_calls, bg_cp_count);
977 STAT_INFO_RO_ATTR(gc_foreground_calls, call_count);
978 STAT_INFO_RO_ATTR(gc_background_calls, bg_gc);
979 #endif
980 
981 /* FAULT_INFO ATTR */
982 #ifdef CONFIG_F2FS_FAULT_INJECTION
983 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate);
984 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type);
985 #endif
986 
987 /* RESERVED_BLOCKS ATTR */
988 RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks);
989 
990 /* CPRC_INFO ATTR */
991 CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio);
992 
993 /* ATGC_INFO ATTR */
994 ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio);
995 ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
996 ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
997 ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
998 
999 F2FS_GENERAL_RO_ATTR(dirty_segments);
1000 F2FS_GENERAL_RO_ATTR(free_segments);
1001 F2FS_GENERAL_RO_ATTR(ovp_segments);
1002 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
1003 F2FS_GENERAL_RO_ATTR(features);
1004 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
1005 F2FS_GENERAL_RO_ATTR(unusable);
1006 F2FS_GENERAL_RO_ATTR(encoding);
1007 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
1008 F2FS_GENERAL_RO_ATTR(main_blkaddr);
1009 F2FS_GENERAL_RO_ATTR(pending_discard);
1010 F2FS_GENERAL_RO_ATTR(gc_mode);
1011 #ifdef CONFIG_F2FS_STAT_FS
1012 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
1013 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
1014 F2FS_GENERAL_RO_ATTR(avg_vblocks);
1015 #endif
1016 
1017 #ifdef CONFIG_FS_ENCRYPTION
1018 F2FS_FEATURE_RO_ATTR(encryption);
1019 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
1020 #if IS_ENABLED(CONFIG_UNICODE)
1021 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
1022 #endif
1023 #endif /* CONFIG_FS_ENCRYPTION */
1024 #ifdef CONFIG_BLK_DEV_ZONED
1025 F2FS_FEATURE_RO_ATTR(block_zoned);
1026 #endif
1027 F2FS_FEATURE_RO_ATTR(atomic_write);
1028 F2FS_FEATURE_RO_ATTR(extra_attr);
1029 F2FS_FEATURE_RO_ATTR(project_quota);
1030 F2FS_FEATURE_RO_ATTR(inode_checksum);
1031 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
1032 F2FS_FEATURE_RO_ATTR(quota_ino);
1033 F2FS_FEATURE_RO_ATTR(inode_crtime);
1034 F2FS_FEATURE_RO_ATTR(lost_found);
1035 #ifdef CONFIG_FS_VERITY
1036 F2FS_FEATURE_RO_ATTR(verity);
1037 #endif
1038 F2FS_FEATURE_RO_ATTR(sb_checksum);
1039 #if IS_ENABLED(CONFIG_UNICODE)
1040 F2FS_FEATURE_RO_ATTR(casefold);
1041 #endif
1042 F2FS_FEATURE_RO_ATTR(readonly);
1043 #ifdef CONFIG_F2FS_FS_COMPRESSION
1044 F2FS_FEATURE_RO_ATTR(compression);
1045 #endif
1046 F2FS_FEATURE_RO_ATTR(pin_file);
1047 
1048 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
1049 static struct attribute *f2fs_attrs[] = {
1050 	ATTR_LIST(gc_urgent_sleep_time),
1051 	ATTR_LIST(gc_min_sleep_time),
1052 	ATTR_LIST(gc_max_sleep_time),
1053 	ATTR_LIST(gc_no_gc_sleep_time),
1054 	ATTR_LIST(gc_idle),
1055 	ATTR_LIST(gc_urgent),
1056 	ATTR_LIST(reclaim_segments),
1057 	ATTR_LIST(main_blkaddr),
1058 	ATTR_LIST(max_small_discards),
1059 	ATTR_LIST(max_discard_request),
1060 	ATTR_LIST(min_discard_issue_time),
1061 	ATTR_LIST(mid_discard_issue_time),
1062 	ATTR_LIST(max_discard_issue_time),
1063 	ATTR_LIST(discard_io_aware_gran),
1064 	ATTR_LIST(discard_urgent_util),
1065 	ATTR_LIST(discard_granularity),
1066 	ATTR_LIST(max_ordered_discard),
1067 	ATTR_LIST(pending_discard),
1068 	ATTR_LIST(gc_mode),
1069 	ATTR_LIST(ipu_policy),
1070 	ATTR_LIST(min_ipu_util),
1071 	ATTR_LIST(min_fsync_blocks),
1072 	ATTR_LIST(min_seq_blocks),
1073 	ATTR_LIST(min_hot_blocks),
1074 	ATTR_LIST(min_ssr_sections),
1075 	ATTR_LIST(max_victim_search),
1076 	ATTR_LIST(migration_granularity),
1077 	ATTR_LIST(dir_level),
1078 	ATTR_LIST(ram_thresh),
1079 	ATTR_LIST(ra_nid_pages),
1080 	ATTR_LIST(dirty_nats_ratio),
1081 	ATTR_LIST(max_roll_forward_node_blocks),
1082 	ATTR_LIST(cp_interval),
1083 	ATTR_LIST(idle_interval),
1084 	ATTR_LIST(discard_idle_interval),
1085 	ATTR_LIST(gc_idle_interval),
1086 	ATTR_LIST(umount_discard_timeout),
1087 #ifdef CONFIG_F2FS_IOSTAT
1088 	ATTR_LIST(iostat_enable),
1089 	ATTR_LIST(iostat_period_ms),
1090 #endif
1091 	ATTR_LIST(readdir_ra),
1092 	ATTR_LIST(max_io_bytes),
1093 	ATTR_LIST(gc_pin_file_thresh),
1094 	ATTR_LIST(extension_list),
1095 #ifdef CONFIG_F2FS_FAULT_INJECTION
1096 	ATTR_LIST(inject_rate),
1097 	ATTR_LIST(inject_type),
1098 #endif
1099 	ATTR_LIST(data_io_flag),
1100 	ATTR_LIST(node_io_flag),
1101 	ATTR_LIST(gc_remaining_trials),
1102 	ATTR_LIST(ckpt_thread_ioprio),
1103 	ATTR_LIST(dirty_segments),
1104 	ATTR_LIST(free_segments),
1105 	ATTR_LIST(ovp_segments),
1106 	ATTR_LIST(unusable),
1107 	ATTR_LIST(lifetime_write_kbytes),
1108 	ATTR_LIST(features),
1109 	ATTR_LIST(reserved_blocks),
1110 	ATTR_LIST(current_reserved_blocks),
1111 	ATTR_LIST(encoding),
1112 	ATTR_LIST(mounted_time_sec),
1113 #ifdef CONFIG_F2FS_STAT_FS
1114 	ATTR_LIST(cp_foreground_calls),
1115 	ATTR_LIST(cp_background_calls),
1116 	ATTR_LIST(gc_foreground_calls),
1117 	ATTR_LIST(gc_background_calls),
1118 	ATTR_LIST(moved_blocks_foreground),
1119 	ATTR_LIST(moved_blocks_background),
1120 	ATTR_LIST(avg_vblocks),
1121 #endif
1122 #ifdef CONFIG_BLK_DEV_ZONED
1123 	ATTR_LIST(unusable_blocks_per_sec),
1124 #endif
1125 #ifdef CONFIG_F2FS_FS_COMPRESSION
1126 	ATTR_LIST(compr_written_block),
1127 	ATTR_LIST(compr_saved_block),
1128 	ATTR_LIST(compr_new_inode),
1129 	ATTR_LIST(compress_percent),
1130 	ATTR_LIST(compress_watermark),
1131 #endif
1132 	/* For ATGC */
1133 	ATTR_LIST(atgc_candidate_ratio),
1134 	ATTR_LIST(atgc_candidate_count),
1135 	ATTR_LIST(atgc_age_weight),
1136 	ATTR_LIST(atgc_age_threshold),
1137 	ATTR_LIST(seq_file_ra_mul),
1138 	ATTR_LIST(gc_segment_mode),
1139 	ATTR_LIST(gc_reclaimed_segments),
1140 	ATTR_LIST(max_fragment_chunk),
1141 	ATTR_LIST(max_fragment_hole),
1142 	ATTR_LIST(current_atomic_write),
1143 	ATTR_LIST(peak_atomic_write),
1144 	ATTR_LIST(committed_atomic_block),
1145 	ATTR_LIST(revoked_atomic_block),
1146 	ATTR_LIST(hot_data_age_threshold),
1147 	ATTR_LIST(warm_data_age_threshold),
1148 	ATTR_LIST(last_age_weight),
1149 	NULL,
1150 };
1151 ATTRIBUTE_GROUPS(f2fs);
1152 
1153 static struct attribute *f2fs_feat_attrs[] = {
1154 #ifdef CONFIG_FS_ENCRYPTION
1155 	ATTR_LIST(encryption),
1156 	ATTR_LIST(test_dummy_encryption_v2),
1157 #if IS_ENABLED(CONFIG_UNICODE)
1158 	ATTR_LIST(encrypted_casefold),
1159 #endif
1160 #endif /* CONFIG_FS_ENCRYPTION */
1161 #ifdef CONFIG_BLK_DEV_ZONED
1162 	ATTR_LIST(block_zoned),
1163 #endif
1164 	ATTR_LIST(atomic_write),
1165 	ATTR_LIST(extra_attr),
1166 	ATTR_LIST(project_quota),
1167 	ATTR_LIST(inode_checksum),
1168 	ATTR_LIST(flexible_inline_xattr),
1169 	ATTR_LIST(quota_ino),
1170 	ATTR_LIST(inode_crtime),
1171 	ATTR_LIST(lost_found),
1172 #ifdef CONFIG_FS_VERITY
1173 	ATTR_LIST(verity),
1174 #endif
1175 	ATTR_LIST(sb_checksum),
1176 #if IS_ENABLED(CONFIG_UNICODE)
1177 	ATTR_LIST(casefold),
1178 #endif
1179 	ATTR_LIST(readonly),
1180 #ifdef CONFIG_F2FS_FS_COMPRESSION
1181 	ATTR_LIST(compression),
1182 #endif
1183 	ATTR_LIST(pin_file),
1184 	NULL,
1185 };
1186 ATTRIBUTE_GROUPS(f2fs_feat);
1187 
1188 F2FS_GENERAL_RO_ATTR(sb_status);
1189 F2FS_GENERAL_RO_ATTR(cp_status);
1190 static struct attribute *f2fs_stat_attrs[] = {
1191 	ATTR_LIST(sb_status),
1192 	ATTR_LIST(cp_status),
1193 	NULL,
1194 };
1195 ATTRIBUTE_GROUPS(f2fs_stat);
1196 
1197 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1198 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1199 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1200 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1201 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1202 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1203 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1204 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1205 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1206 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1207 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1208 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1209 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1210 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1211 
1212 static struct attribute *f2fs_sb_feat_attrs[] = {
1213 	ATTR_LIST(sb_encryption),
1214 	ATTR_LIST(sb_block_zoned),
1215 	ATTR_LIST(sb_extra_attr),
1216 	ATTR_LIST(sb_project_quota),
1217 	ATTR_LIST(sb_inode_checksum),
1218 	ATTR_LIST(sb_flexible_inline_xattr),
1219 	ATTR_LIST(sb_quota_ino),
1220 	ATTR_LIST(sb_inode_crtime),
1221 	ATTR_LIST(sb_lost_found),
1222 	ATTR_LIST(sb_verity),
1223 	ATTR_LIST(sb_sb_checksum),
1224 	ATTR_LIST(sb_casefold),
1225 	ATTR_LIST(sb_compression),
1226 	ATTR_LIST(sb_readonly),
1227 	NULL,
1228 };
1229 ATTRIBUTE_GROUPS(f2fs_sb_feat);
1230 
1231 static const struct sysfs_ops f2fs_attr_ops = {
1232 	.show	= f2fs_attr_show,
1233 	.store	= f2fs_attr_store,
1234 };
1235 
1236 static const struct kobj_type f2fs_sb_ktype = {
1237 	.default_groups = f2fs_groups,
1238 	.sysfs_ops	= &f2fs_attr_ops,
1239 	.release	= f2fs_sb_release,
1240 };
1241 
1242 static const struct kobj_type f2fs_ktype = {
1243 	.sysfs_ops	= &f2fs_attr_ops,
1244 };
1245 
1246 static struct kset f2fs_kset = {
1247 	.kobj	= {.ktype = &f2fs_ktype},
1248 };
1249 
1250 static const struct kobj_type f2fs_feat_ktype = {
1251 	.default_groups = f2fs_feat_groups,
1252 	.sysfs_ops	= &f2fs_attr_ops,
1253 };
1254 
1255 static struct kobject f2fs_feat = {
1256 	.kset	= &f2fs_kset,
1257 };
1258 
1259 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1260 				struct attribute *attr, char *buf)
1261 {
1262 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1263 								s_stat_kobj);
1264 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1265 
1266 	return a->show ? a->show(a, sbi, buf) : 0;
1267 }
1268 
1269 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1270 						const char *buf, size_t len)
1271 {
1272 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1273 								s_stat_kobj);
1274 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1275 
1276 	return a->store ? a->store(a, sbi, buf, len) : 0;
1277 }
1278 
1279 static void f2fs_stat_kobj_release(struct kobject *kobj)
1280 {
1281 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1282 								s_stat_kobj);
1283 	complete(&sbi->s_stat_kobj_unregister);
1284 }
1285 
1286 static const struct sysfs_ops f2fs_stat_attr_ops = {
1287 	.show	= f2fs_stat_attr_show,
1288 	.store	= f2fs_stat_attr_store,
1289 };
1290 
1291 static const struct kobj_type f2fs_stat_ktype = {
1292 	.default_groups = f2fs_stat_groups,
1293 	.sysfs_ops	= &f2fs_stat_attr_ops,
1294 	.release	= f2fs_stat_kobj_release,
1295 };
1296 
1297 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1298 				struct attribute *attr, char *buf)
1299 {
1300 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1301 							s_feature_list_kobj);
1302 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1303 
1304 	return a->show ? a->show(a, sbi, buf) : 0;
1305 }
1306 
1307 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1308 {
1309 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1310 							s_feature_list_kobj);
1311 	complete(&sbi->s_feature_list_kobj_unregister);
1312 }
1313 
1314 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1315 	.show	= f2fs_sb_feat_attr_show,
1316 };
1317 
1318 static const struct kobj_type f2fs_feature_list_ktype = {
1319 	.default_groups = f2fs_sb_feat_groups,
1320 	.sysfs_ops	= &f2fs_feature_list_attr_ops,
1321 	.release	= f2fs_feature_list_kobj_release,
1322 };
1323 
1324 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1325 						void *offset)
1326 {
1327 	struct super_block *sb = seq->private;
1328 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1329 	unsigned int total_segs =
1330 			le32_to_cpu(sbi->raw_super->segment_count_main);
1331 	int i;
1332 
1333 	seq_puts(seq, "format: segment_type|valid_blocks\n"
1334 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1335 
1336 	for (i = 0; i < total_segs; i++) {
1337 		struct seg_entry *se = get_seg_entry(sbi, i);
1338 
1339 		if ((i % 10) == 0)
1340 			seq_printf(seq, "%-10d", i);
1341 		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1342 		if ((i % 10) == 9 || i == (total_segs - 1))
1343 			seq_putc(seq, '\n');
1344 		else
1345 			seq_putc(seq, ' ');
1346 	}
1347 
1348 	return 0;
1349 }
1350 
1351 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1352 						void *offset)
1353 {
1354 	struct super_block *sb = seq->private;
1355 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1356 	unsigned int total_segs =
1357 			le32_to_cpu(sbi->raw_super->segment_count_main);
1358 	int i, j;
1359 
1360 	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1361 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1362 
1363 	for (i = 0; i < total_segs; i++) {
1364 		struct seg_entry *se = get_seg_entry(sbi, i);
1365 
1366 		seq_printf(seq, "%-10d", i);
1367 		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1368 		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1369 			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1370 		seq_putc(seq, '\n');
1371 	}
1372 	return 0;
1373 }
1374 
1375 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1376 						void *offset)
1377 {
1378 	struct super_block *sb = seq->private;
1379 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1380 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1381 	int i;
1382 
1383 	seq_puts(seq, "format: victim_secmap bitmaps\n");
1384 
1385 	for (i = 0; i < MAIN_SECS(sbi); i++) {
1386 		if ((i % 10) == 0)
1387 			seq_printf(seq, "%-10d", i);
1388 		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1389 		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1390 			seq_putc(seq, '\n');
1391 		else
1392 			seq_putc(seq, ' ');
1393 	}
1394 	return 0;
1395 }
1396 
1397 static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
1398 						void *offset)
1399 {
1400 	struct super_block *sb = seq->private;
1401 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1402 	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1403 	int i, count;
1404 
1405 	seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
1406 	if (!f2fs_realtime_discard_enable(sbi))
1407 		return 0;
1408 
1409 	if (dcc) {
1410 		mutex_lock(&dcc->cmd_lock);
1411 		for (i = 0; i < MAX_PLIST_NUM; i++) {
1412 			struct list_head *pend_list;
1413 			struct discard_cmd *dc, *tmp;
1414 
1415 			if (i % 8 == 0)
1416 				seq_printf(seq, "  %-3d", i);
1417 			count = 0;
1418 			pend_list = &dcc->pend_list[i];
1419 			list_for_each_entry_safe(dc, tmp, pend_list, list)
1420 				count++;
1421 			if (count)
1422 				seq_printf(seq, " %7d", count);
1423 			else
1424 				seq_puts(seq, "       .");
1425 			if (i % 8 == 7)
1426 				seq_putc(seq, '\n');
1427 		}
1428 		seq_putc(seq, '\n');
1429 		mutex_unlock(&dcc->cmd_lock);
1430 	}
1431 
1432 	return 0;
1433 }
1434 
1435 int __init f2fs_init_sysfs(void)
1436 {
1437 	int ret;
1438 
1439 	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1440 	f2fs_kset.kobj.parent = fs_kobj;
1441 	ret = kset_register(&f2fs_kset);
1442 	if (ret)
1443 		return ret;
1444 
1445 	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1446 				   NULL, "features");
1447 	if (ret)
1448 		goto put_kobject;
1449 
1450 	f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1451 	if (!f2fs_proc_root) {
1452 		ret = -ENOMEM;
1453 		goto put_kobject;
1454 	}
1455 
1456 	return 0;
1457 put_kobject:
1458 	kobject_put(&f2fs_feat);
1459 	kset_unregister(&f2fs_kset);
1460 	return ret;
1461 }
1462 
1463 void f2fs_exit_sysfs(void)
1464 {
1465 	kobject_put(&f2fs_feat);
1466 	kset_unregister(&f2fs_kset);
1467 	remove_proc_entry("fs/f2fs", NULL);
1468 	f2fs_proc_root = NULL;
1469 }
1470 
1471 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1472 {
1473 	struct super_block *sb = sbi->sb;
1474 	int err;
1475 
1476 	sbi->s_kobj.kset = &f2fs_kset;
1477 	init_completion(&sbi->s_kobj_unregister);
1478 	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1479 				"%s", sb->s_id);
1480 	if (err)
1481 		goto put_sb_kobj;
1482 
1483 	sbi->s_stat_kobj.kset = &f2fs_kset;
1484 	init_completion(&sbi->s_stat_kobj_unregister);
1485 	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1486 						&sbi->s_kobj, "stat");
1487 	if (err)
1488 		goto put_stat_kobj;
1489 
1490 	sbi->s_feature_list_kobj.kset = &f2fs_kset;
1491 	init_completion(&sbi->s_feature_list_kobj_unregister);
1492 	err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1493 					&f2fs_feature_list_ktype,
1494 					&sbi->s_kobj, "feature_list");
1495 	if (err)
1496 		goto put_feature_list_kobj;
1497 
1498 	sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1499 	if (!sbi->s_proc) {
1500 		err = -ENOMEM;
1501 		goto put_feature_list_kobj;
1502 	}
1503 
1504 	proc_create_single_data("segment_info", 0444, sbi->s_proc,
1505 				segment_info_seq_show, sb);
1506 	proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1507 				segment_bits_seq_show, sb);
1508 #ifdef CONFIG_F2FS_IOSTAT
1509 	proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1510 				iostat_info_seq_show, sb);
1511 #endif
1512 	proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1513 				victim_bits_seq_show, sb);
1514 	proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
1515 				discard_plist_seq_show, sb);
1516 	return 0;
1517 put_feature_list_kobj:
1518 	kobject_put(&sbi->s_feature_list_kobj);
1519 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1520 put_stat_kobj:
1521 	kobject_put(&sbi->s_stat_kobj);
1522 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1523 put_sb_kobj:
1524 	kobject_put(&sbi->s_kobj);
1525 	wait_for_completion(&sbi->s_kobj_unregister);
1526 	return err;
1527 }
1528 
1529 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1530 {
1531 	remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
1532 
1533 	kobject_put(&sbi->s_stat_kobj);
1534 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1535 	kobject_put(&sbi->s_feature_list_kobj);
1536 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1537 
1538 	kobject_put(&sbi->s_kobj);
1539 	wait_for_completion(&sbi->s_kobj_unregister);
1540 }
1541