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