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