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