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