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