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