xref: /openbmc/linux/fs/f2fs/sysfs.c (revision 8938c48f)
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 
16 #include "f2fs.h"
17 #include "segment.h"
18 #include "gc.h"
19 #include <trace/events/f2fs.h>
20 
21 static struct proc_dir_entry *f2fs_proc_root;
22 
23 /* Sysfs support for f2fs */
24 enum {
25 	GC_THREAD,	/* struct f2fs_gc_thread */
26 	SM_INFO,	/* struct f2fs_sm_info */
27 	DCC_INFO,	/* struct discard_cmd_control */
28 	NM_INFO,	/* struct f2fs_nm_info */
29 	F2FS_SBI,	/* struct f2fs_sb_info */
30 #ifdef CONFIG_F2FS_STAT_FS
31 	STAT_INFO,	/* struct f2fs_stat_info */
32 #endif
33 #ifdef CONFIG_F2FS_FAULT_INJECTION
34 	FAULT_INFO_RATE,	/* struct f2fs_fault_info */
35 	FAULT_INFO_TYPE,	/* struct f2fs_fault_info */
36 #endif
37 	RESERVED_BLOCKS,	/* struct f2fs_sb_info */
38 	CPRC_INFO,	/* struct ckpt_req_control */
39 };
40 
41 struct f2fs_attr {
42 	struct attribute attr;
43 	ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
44 	ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
45 			 const char *, size_t);
46 	int struct_type;
47 	int offset;
48 	int id;
49 };
50 
51 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
52 			     struct f2fs_sb_info *sbi, char *buf);
53 
54 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
55 {
56 	if (struct_type == GC_THREAD)
57 		return (unsigned char *)sbi->gc_thread;
58 	else if (struct_type == SM_INFO)
59 		return (unsigned char *)SM_I(sbi);
60 	else if (struct_type == DCC_INFO)
61 		return (unsigned char *)SM_I(sbi)->dcc_info;
62 	else if (struct_type == NM_INFO)
63 		return (unsigned char *)NM_I(sbi);
64 	else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
65 		return (unsigned char *)sbi;
66 #ifdef CONFIG_F2FS_FAULT_INJECTION
67 	else if (struct_type == FAULT_INFO_RATE ||
68 					struct_type == FAULT_INFO_TYPE)
69 		return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
70 #endif
71 #ifdef CONFIG_F2FS_STAT_FS
72 	else if (struct_type == STAT_INFO)
73 		return (unsigned char *)F2FS_STAT(sbi);
74 #endif
75 	else if (struct_type == CPRC_INFO)
76 		return (unsigned char *)&sbi->cprc_info;
77 	return NULL;
78 }
79 
80 static ssize_t dirty_segments_show(struct f2fs_attr *a,
81 		struct f2fs_sb_info *sbi, char *buf)
82 {
83 	return sprintf(buf, "%llu\n",
84 			(unsigned long long)(dirty_segments(sbi)));
85 }
86 
87 static ssize_t free_segments_show(struct f2fs_attr *a,
88 		struct f2fs_sb_info *sbi, char *buf)
89 {
90 	return sprintf(buf, "%llu\n",
91 			(unsigned long long)(free_segments(sbi)));
92 }
93 
94 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
95 		struct f2fs_sb_info *sbi, char *buf)
96 {
97 	return sprintf(buf, "%llu\n",
98 			(unsigned long long)(sbi->kbytes_written +
99 			((f2fs_get_sectors_written(sbi) -
100 				sbi->sectors_written_start) >> 1)));
101 }
102 
103 static ssize_t sb_status_show(struct f2fs_attr *a,
104 		struct f2fs_sb_info *sbi, char *buf)
105 {
106 	return sprintf(buf, "%lx\n", sbi->s_flag);
107 }
108 
109 static ssize_t features_show(struct f2fs_attr *a,
110 		struct f2fs_sb_info *sbi, char *buf)
111 {
112 	int len = 0;
113 
114 	if (f2fs_sb_has_encrypt(sbi))
115 		len += scnprintf(buf, PAGE_SIZE - len, "%s",
116 						"encryption");
117 	if (f2fs_sb_has_blkzoned(sbi))
118 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
119 				len ? ", " : "", "blkzoned");
120 	if (f2fs_sb_has_extra_attr(sbi))
121 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
122 				len ? ", " : "", "extra_attr");
123 	if (f2fs_sb_has_project_quota(sbi))
124 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
125 				len ? ", " : "", "projquota");
126 	if (f2fs_sb_has_inode_chksum(sbi))
127 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
128 				len ? ", " : "", "inode_checksum");
129 	if (f2fs_sb_has_flexible_inline_xattr(sbi))
130 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
131 				len ? ", " : "", "flexible_inline_xattr");
132 	if (f2fs_sb_has_quota_ino(sbi))
133 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
134 				len ? ", " : "", "quota_ino");
135 	if (f2fs_sb_has_inode_crtime(sbi))
136 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
137 				len ? ", " : "", "inode_crtime");
138 	if (f2fs_sb_has_lost_found(sbi))
139 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
140 				len ? ", " : "", "lost_found");
141 	if (f2fs_sb_has_verity(sbi))
142 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
143 				len ? ", " : "", "verity");
144 	if (f2fs_sb_has_sb_chksum(sbi))
145 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
146 				len ? ", " : "", "sb_checksum");
147 	if (f2fs_sb_has_casefold(sbi))
148 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
149 				len ? ", " : "", "casefold");
150 	if (f2fs_sb_has_compression(sbi))
151 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
152 				len ? ", " : "", "compression");
153 	len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
154 				len ? ", " : "", "pin_file");
155 	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
156 	return len;
157 }
158 
159 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
160 					struct f2fs_sb_info *sbi, char *buf)
161 {
162 	return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
163 }
164 
165 static ssize_t unusable_show(struct f2fs_attr *a,
166 		struct f2fs_sb_info *sbi, char *buf)
167 {
168 	block_t unusable;
169 
170 	if (test_opt(sbi, DISABLE_CHECKPOINT))
171 		unusable = sbi->unusable_block_count;
172 	else
173 		unusable = f2fs_get_unusable_blocks(sbi);
174 	return sprintf(buf, "%llu\n", (unsigned long long)unusable);
175 }
176 
177 static ssize_t encoding_show(struct f2fs_attr *a,
178 		struct f2fs_sb_info *sbi, char *buf)
179 {
180 #ifdef CONFIG_UNICODE
181 	struct super_block *sb = sbi->sb;
182 
183 	if (f2fs_sb_has_casefold(sbi))
184 		return snprintf(buf, PAGE_SIZE, "%s (%d.%d.%d)\n",
185 			sb->s_encoding->charset,
186 			(sb->s_encoding->version >> 16) & 0xff,
187 			(sb->s_encoding->version >> 8) & 0xff,
188 			sb->s_encoding->version & 0xff);
189 #endif
190 	return sprintf(buf, "(none)");
191 }
192 
193 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
194 		struct f2fs_sb_info *sbi, char *buf)
195 {
196 	return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
197 }
198 
199 #ifdef CONFIG_F2FS_STAT_FS
200 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
201 				struct f2fs_sb_info *sbi, char *buf)
202 {
203 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
204 
205 	return sprintf(buf, "%llu\n",
206 		(unsigned long long)(si->tot_blks -
207 			(si->bg_data_blks + si->bg_node_blks)));
208 }
209 
210 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
211 				struct f2fs_sb_info *sbi, char *buf)
212 {
213 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
214 
215 	return sprintf(buf, "%llu\n",
216 		(unsigned long long)(si->bg_data_blks + si->bg_node_blks));
217 }
218 
219 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
220 		struct f2fs_sb_info *sbi, char *buf)
221 {
222 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
223 
224 	si->dirty_count = dirty_segments(sbi);
225 	f2fs_update_sit_info(sbi);
226 	return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
227 }
228 #endif
229 
230 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
231 				struct f2fs_sb_info *sbi, char *buf)
232 {
233 	return snprintf(buf, PAGE_SIZE, "%llu\n",
234 			(unsigned long long)MAIN_BLKADDR(sbi));
235 }
236 
237 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
238 			struct f2fs_sb_info *sbi, char *buf)
239 {
240 	unsigned char *ptr = NULL;
241 	unsigned int *ui;
242 
243 	ptr = __struct_ptr(sbi, a->struct_type);
244 	if (!ptr)
245 		return -EINVAL;
246 
247 	if (!strcmp(a->attr.name, "extension_list")) {
248 		__u8 (*extlist)[F2FS_EXTENSION_LEN] =
249 					sbi->raw_super->extension_list;
250 		int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
251 		int hot_count = sbi->raw_super->hot_ext_count;
252 		int len = 0, i;
253 
254 		len += scnprintf(buf + len, PAGE_SIZE - len,
255 						"cold file extension:\n");
256 		for (i = 0; i < cold_count; i++)
257 			len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
258 								extlist[i]);
259 
260 		len += scnprintf(buf + len, PAGE_SIZE - len,
261 						"hot file extension:\n");
262 		for (i = cold_count; i < cold_count + hot_count; i++)
263 			len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
264 								extlist[i]);
265 		return len;
266 	}
267 
268 	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
269 		struct ckpt_req_control *cprc = &sbi->cprc_info;
270 		int len = 0;
271 		int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
272 		int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
273 
274 		if (class == IOPRIO_CLASS_RT)
275 			len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
276 		else if (class == IOPRIO_CLASS_BE)
277 			len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
278 		else
279 			return -EINVAL;
280 
281 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
282 		return len;
283 	}
284 
285 	ui = (unsigned int *)(ptr + a->offset);
286 
287 	return sprintf(buf, "%u\n", *ui);
288 }
289 
290 static ssize_t __sbi_store(struct f2fs_attr *a,
291 			struct f2fs_sb_info *sbi,
292 			const char *buf, size_t count)
293 {
294 	unsigned char *ptr;
295 	unsigned long t;
296 	unsigned int *ui;
297 	ssize_t ret;
298 
299 	ptr = __struct_ptr(sbi, a->struct_type);
300 	if (!ptr)
301 		return -EINVAL;
302 
303 	if (!strcmp(a->attr.name, "extension_list")) {
304 		const char *name = strim((char *)buf);
305 		bool set = true, hot;
306 
307 		if (!strncmp(name, "[h]", 3))
308 			hot = true;
309 		else if (!strncmp(name, "[c]", 3))
310 			hot = false;
311 		else
312 			return -EINVAL;
313 
314 		name += 3;
315 
316 		if (*name == '!') {
317 			name++;
318 			set = false;
319 		}
320 
321 		if (strlen(name) >= F2FS_EXTENSION_LEN)
322 			return -EINVAL;
323 
324 		down_write(&sbi->sb_lock);
325 
326 		ret = f2fs_update_extension_list(sbi, name, hot, set);
327 		if (ret)
328 			goto out;
329 
330 		ret = f2fs_commit_super(sbi, false);
331 		if (ret)
332 			f2fs_update_extension_list(sbi, name, hot, !set);
333 out:
334 		up_write(&sbi->sb_lock);
335 		return ret ? ret : count;
336 	}
337 
338 	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
339 		const char *name = strim((char *)buf);
340 		struct ckpt_req_control *cprc = &sbi->cprc_info;
341 		int class;
342 		long data;
343 		int ret;
344 
345 		if (!strncmp(name, "rt,", 3))
346 			class = IOPRIO_CLASS_RT;
347 		else if (!strncmp(name, "be,", 3))
348 			class = IOPRIO_CLASS_BE;
349 		else
350 			return -EINVAL;
351 
352 		name += 3;
353 		ret = kstrtol(name, 10, &data);
354 		if (ret)
355 			return ret;
356 		if (data >= IOPRIO_BE_NR || data < 0)
357 			return -EINVAL;
358 
359 		cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
360 		if (test_opt(sbi, MERGE_CHECKPOINT)) {
361 			ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
362 					cprc->ckpt_thread_ioprio);
363 			if (ret)
364 				return ret;
365 		}
366 
367 		return count;
368 	}
369 
370 	ui = (unsigned int *)(ptr + a->offset);
371 
372 	ret = kstrtoul(skip_spaces(buf), 0, &t);
373 	if (ret < 0)
374 		return ret;
375 #ifdef CONFIG_F2FS_FAULT_INJECTION
376 	if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
377 		return -EINVAL;
378 	if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
379 		return -EINVAL;
380 #endif
381 	if (a->struct_type == RESERVED_BLOCKS) {
382 		spin_lock(&sbi->stat_lock);
383 		if (t > (unsigned long)(sbi->user_block_count -
384 				F2FS_OPTION(sbi).root_reserved_blocks)) {
385 			spin_unlock(&sbi->stat_lock);
386 			return -EINVAL;
387 		}
388 		*ui = t;
389 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
390 				sbi->user_block_count - valid_user_blocks(sbi));
391 		spin_unlock(&sbi->stat_lock);
392 		return count;
393 	}
394 
395 	if (!strcmp(a->attr.name, "discard_granularity")) {
396 		if (t == 0 || t > MAX_PLIST_NUM)
397 			return -EINVAL;
398 		if (t == *ui)
399 			return count;
400 		*ui = t;
401 		return count;
402 	}
403 
404 	if (!strcmp(a->attr.name, "migration_granularity")) {
405 		if (t == 0 || t > sbi->segs_per_sec)
406 			return -EINVAL;
407 	}
408 
409 	if (!strcmp(a->attr.name, "trim_sections"))
410 		return -EINVAL;
411 
412 	if (!strcmp(a->attr.name, "gc_urgent")) {
413 		if (t == 0) {
414 			sbi->gc_mode = GC_NORMAL;
415 		} else if (t == 1) {
416 			sbi->gc_mode = GC_URGENT_HIGH;
417 			if (sbi->gc_thread) {
418 				sbi->gc_thread->gc_wake = 1;
419 				wake_up_interruptible_all(
420 					&sbi->gc_thread->gc_wait_queue_head);
421 				wake_up_discard_thread(sbi, true);
422 			}
423 		} else if (t == 2) {
424 			sbi->gc_mode = GC_URGENT_LOW;
425 		} else {
426 			return -EINVAL;
427 		}
428 		return count;
429 	}
430 	if (!strcmp(a->attr.name, "gc_idle")) {
431 		if (t == GC_IDLE_CB) {
432 			sbi->gc_mode = GC_IDLE_CB;
433 		} else if (t == GC_IDLE_GREEDY) {
434 			sbi->gc_mode = GC_IDLE_GREEDY;
435 		} else if (t == GC_IDLE_AT) {
436 			if (!sbi->am.atgc_enabled)
437 				return -EINVAL;
438 			sbi->gc_mode = GC_AT;
439 		} else {
440 			sbi->gc_mode = GC_NORMAL;
441 		}
442 		return count;
443 	}
444 
445 	if (!strcmp(a->attr.name, "iostat_enable")) {
446 		sbi->iostat_enable = !!t;
447 		if (!sbi->iostat_enable)
448 			f2fs_reset_iostat(sbi);
449 		return count;
450 	}
451 
452 	if (!strcmp(a->attr.name, "iostat_period_ms")) {
453 		if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
454 			return -EINVAL;
455 		spin_lock(&sbi->iostat_lock);
456 		sbi->iostat_period_ms = (unsigned int)t;
457 		spin_unlock(&sbi->iostat_lock);
458 		return count;
459 	}
460 
461 	*ui = (unsigned int)t;
462 
463 	return count;
464 }
465 
466 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
467 			struct f2fs_sb_info *sbi,
468 			const char *buf, size_t count)
469 {
470 	ssize_t ret;
471 	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
472 					a->struct_type == GC_THREAD);
473 
474 	if (gc_entry) {
475 		if (!down_read_trylock(&sbi->sb->s_umount))
476 			return -EAGAIN;
477 	}
478 	ret = __sbi_store(a, sbi, buf, count);
479 	if (gc_entry)
480 		up_read(&sbi->sb->s_umount);
481 
482 	return ret;
483 }
484 
485 static ssize_t f2fs_attr_show(struct kobject *kobj,
486 				struct attribute *attr, char *buf)
487 {
488 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
489 								s_kobj);
490 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
491 
492 	return a->show ? a->show(a, sbi, buf) : 0;
493 }
494 
495 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
496 						const char *buf, size_t len)
497 {
498 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
499 									s_kobj);
500 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
501 
502 	return a->store ? a->store(a, sbi, buf, len) : 0;
503 }
504 
505 static void f2fs_sb_release(struct kobject *kobj)
506 {
507 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
508 								s_kobj);
509 	complete(&sbi->s_kobj_unregister);
510 }
511 
512 enum feat_id {
513 	FEAT_CRYPTO = 0,
514 	FEAT_BLKZONED,
515 	FEAT_ATOMIC_WRITE,
516 	FEAT_EXTRA_ATTR,
517 	FEAT_PROJECT_QUOTA,
518 	FEAT_INODE_CHECKSUM,
519 	FEAT_FLEXIBLE_INLINE_XATTR,
520 	FEAT_QUOTA_INO,
521 	FEAT_INODE_CRTIME,
522 	FEAT_LOST_FOUND,
523 	FEAT_VERITY,
524 	FEAT_SB_CHECKSUM,
525 	FEAT_CASEFOLD,
526 	FEAT_COMPRESSION,
527 	FEAT_TEST_DUMMY_ENCRYPTION_V2,
528 };
529 
530 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
531 		struct f2fs_sb_info *sbi, char *buf)
532 {
533 	switch (a->id) {
534 	case FEAT_CRYPTO:
535 	case FEAT_BLKZONED:
536 	case FEAT_ATOMIC_WRITE:
537 	case FEAT_EXTRA_ATTR:
538 	case FEAT_PROJECT_QUOTA:
539 	case FEAT_INODE_CHECKSUM:
540 	case FEAT_FLEXIBLE_INLINE_XATTR:
541 	case FEAT_QUOTA_INO:
542 	case FEAT_INODE_CRTIME:
543 	case FEAT_LOST_FOUND:
544 	case FEAT_VERITY:
545 	case FEAT_SB_CHECKSUM:
546 	case FEAT_CASEFOLD:
547 	case FEAT_COMPRESSION:
548 	case FEAT_TEST_DUMMY_ENCRYPTION_V2:
549 		return sprintf(buf, "supported\n");
550 	}
551 	return 0;
552 }
553 
554 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
555 static struct f2fs_attr f2fs_attr_##_name = {			\
556 	.attr = {.name = __stringify(_name), .mode = _mode },	\
557 	.show	= _show,					\
558 	.store	= _store,					\
559 	.struct_type = _struct_type,				\
560 	.offset = _offset					\
561 }
562 
563 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
564 	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
565 		f2fs_sbi_show, f2fs_sbi_store,			\
566 		offsetof(struct struct_name, elname))
567 
568 #define F2FS_GENERAL_RO_ATTR(name) \
569 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
570 
571 #define F2FS_FEATURE_RO_ATTR(_name, _id)			\
572 static struct f2fs_attr f2fs_attr_##_name = {			\
573 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
574 	.show	= f2fs_feature_show,				\
575 	.id	= _id,						\
576 }
577 
578 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname)	\
579 static struct f2fs_attr f2fs_attr_##_name = {			\
580 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
581 	.show = f2fs_sbi_show,					\
582 	.struct_type = _struct_type,				\
583 	.offset = offsetof(struct _struct_name, _elname),       \
584 }
585 
586 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
587 							urgent_sleep_time);
588 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
589 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
590 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
591 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
592 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
593 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
594 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
595 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
596 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
597 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
598 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
599 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
600 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
601 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
602 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
603 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
604 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
605 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
606 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
607 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
608 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
609 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
610 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
611 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
612 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
613 					interval_time[DISCARD_TIME]);
614 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
615 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
616 		umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
617 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
618 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
619 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
620 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
621 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
622 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
623 #ifdef CONFIG_F2FS_FAULT_INJECTION
624 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
625 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
626 #endif
627 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
628 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
629 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
630 F2FS_GENERAL_RO_ATTR(dirty_segments);
631 F2FS_GENERAL_RO_ATTR(free_segments);
632 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
633 F2FS_GENERAL_RO_ATTR(features);
634 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
635 F2FS_GENERAL_RO_ATTR(unusable);
636 F2FS_GENERAL_RO_ATTR(encoding);
637 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
638 F2FS_GENERAL_RO_ATTR(main_blkaddr);
639 #ifdef CONFIG_F2FS_STAT_FS
640 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
641 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
642 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
643 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
644 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
645 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
646 F2FS_GENERAL_RO_ATTR(avg_vblocks);
647 #endif
648 
649 #ifdef CONFIG_FS_ENCRYPTION
650 F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
651 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2, FEAT_TEST_DUMMY_ENCRYPTION_V2);
652 #endif
653 #ifdef CONFIG_BLK_DEV_ZONED
654 F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
655 #endif
656 F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
657 F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
658 F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
659 F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
660 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
661 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
662 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
663 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
664 #ifdef CONFIG_FS_VERITY
665 F2FS_FEATURE_RO_ATTR(verity, FEAT_VERITY);
666 #endif
667 F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
668 F2FS_FEATURE_RO_ATTR(casefold, FEAT_CASEFOLD);
669 #ifdef CONFIG_F2FS_FS_COMPRESSION
670 F2FS_FEATURE_RO_ATTR(compression, FEAT_COMPRESSION);
671 #endif
672 
673 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
674 static struct attribute *f2fs_attrs[] = {
675 	ATTR_LIST(gc_urgent_sleep_time),
676 	ATTR_LIST(gc_min_sleep_time),
677 	ATTR_LIST(gc_max_sleep_time),
678 	ATTR_LIST(gc_no_gc_sleep_time),
679 	ATTR_LIST(gc_idle),
680 	ATTR_LIST(gc_urgent),
681 	ATTR_LIST(reclaim_segments),
682 	ATTR_LIST(main_blkaddr),
683 	ATTR_LIST(max_small_discards),
684 	ATTR_LIST(discard_granularity),
685 	ATTR_LIST(batched_trim_sections),
686 	ATTR_LIST(ipu_policy),
687 	ATTR_LIST(min_ipu_util),
688 	ATTR_LIST(min_fsync_blocks),
689 	ATTR_LIST(min_seq_blocks),
690 	ATTR_LIST(min_hot_blocks),
691 	ATTR_LIST(min_ssr_sections),
692 	ATTR_LIST(max_victim_search),
693 	ATTR_LIST(migration_granularity),
694 	ATTR_LIST(dir_level),
695 	ATTR_LIST(ram_thresh),
696 	ATTR_LIST(ra_nid_pages),
697 	ATTR_LIST(dirty_nats_ratio),
698 	ATTR_LIST(cp_interval),
699 	ATTR_LIST(idle_interval),
700 	ATTR_LIST(discard_idle_interval),
701 	ATTR_LIST(gc_idle_interval),
702 	ATTR_LIST(umount_discard_timeout),
703 	ATTR_LIST(iostat_enable),
704 	ATTR_LIST(iostat_period_ms),
705 	ATTR_LIST(readdir_ra),
706 	ATTR_LIST(max_io_bytes),
707 	ATTR_LIST(gc_pin_file_thresh),
708 	ATTR_LIST(extension_list),
709 #ifdef CONFIG_F2FS_FAULT_INJECTION
710 	ATTR_LIST(inject_rate),
711 	ATTR_LIST(inject_type),
712 #endif
713 	ATTR_LIST(data_io_flag),
714 	ATTR_LIST(node_io_flag),
715 	ATTR_LIST(ckpt_thread_ioprio),
716 	ATTR_LIST(dirty_segments),
717 	ATTR_LIST(free_segments),
718 	ATTR_LIST(unusable),
719 	ATTR_LIST(lifetime_write_kbytes),
720 	ATTR_LIST(features),
721 	ATTR_LIST(reserved_blocks),
722 	ATTR_LIST(current_reserved_blocks),
723 	ATTR_LIST(encoding),
724 	ATTR_LIST(mounted_time_sec),
725 #ifdef CONFIG_F2FS_STAT_FS
726 	ATTR_LIST(cp_foreground_calls),
727 	ATTR_LIST(cp_background_calls),
728 	ATTR_LIST(gc_foreground_calls),
729 	ATTR_LIST(gc_background_calls),
730 	ATTR_LIST(moved_blocks_foreground),
731 	ATTR_LIST(moved_blocks_background),
732 	ATTR_LIST(avg_vblocks),
733 #endif
734 	NULL,
735 };
736 ATTRIBUTE_GROUPS(f2fs);
737 
738 static struct attribute *f2fs_feat_attrs[] = {
739 #ifdef CONFIG_FS_ENCRYPTION
740 	ATTR_LIST(encryption),
741 	ATTR_LIST(test_dummy_encryption_v2),
742 #endif
743 #ifdef CONFIG_BLK_DEV_ZONED
744 	ATTR_LIST(block_zoned),
745 #endif
746 	ATTR_LIST(atomic_write),
747 	ATTR_LIST(extra_attr),
748 	ATTR_LIST(project_quota),
749 	ATTR_LIST(inode_checksum),
750 	ATTR_LIST(flexible_inline_xattr),
751 	ATTR_LIST(quota_ino),
752 	ATTR_LIST(inode_crtime),
753 	ATTR_LIST(lost_found),
754 #ifdef CONFIG_FS_VERITY
755 	ATTR_LIST(verity),
756 #endif
757 	ATTR_LIST(sb_checksum),
758 	ATTR_LIST(casefold),
759 #ifdef CONFIG_F2FS_FS_COMPRESSION
760 	ATTR_LIST(compression),
761 #endif
762 	NULL,
763 };
764 ATTRIBUTE_GROUPS(f2fs_feat);
765 
766 F2FS_GENERAL_RO_ATTR(sb_status);
767 static struct attribute *f2fs_stat_attrs[] = {
768 	ATTR_LIST(sb_status),
769 	NULL,
770 };
771 ATTRIBUTE_GROUPS(f2fs_stat);
772 
773 static const struct sysfs_ops f2fs_attr_ops = {
774 	.show	= f2fs_attr_show,
775 	.store	= f2fs_attr_store,
776 };
777 
778 static struct kobj_type f2fs_sb_ktype = {
779 	.default_groups = f2fs_groups,
780 	.sysfs_ops	= &f2fs_attr_ops,
781 	.release	= f2fs_sb_release,
782 };
783 
784 static struct kobj_type f2fs_ktype = {
785 	.sysfs_ops	= &f2fs_attr_ops,
786 };
787 
788 static struct kset f2fs_kset = {
789 	.kobj	= {.ktype = &f2fs_ktype},
790 };
791 
792 static struct kobj_type f2fs_feat_ktype = {
793 	.default_groups = f2fs_feat_groups,
794 	.sysfs_ops	= &f2fs_attr_ops,
795 };
796 
797 static struct kobject f2fs_feat = {
798 	.kset	= &f2fs_kset,
799 };
800 
801 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
802 				struct attribute *attr, char *buf)
803 {
804 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
805 								s_stat_kobj);
806 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
807 
808 	return a->show ? a->show(a, sbi, buf) : 0;
809 }
810 
811 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
812 						const char *buf, size_t len)
813 {
814 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
815 								s_stat_kobj);
816 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
817 
818 	return a->store ? a->store(a, sbi, buf, len) : 0;
819 }
820 
821 static void f2fs_stat_kobj_release(struct kobject *kobj)
822 {
823 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
824 								s_stat_kobj);
825 	complete(&sbi->s_stat_kobj_unregister);
826 }
827 
828 static const struct sysfs_ops f2fs_stat_attr_ops = {
829 	.show	= f2fs_stat_attr_show,
830 	.store	= f2fs_stat_attr_store,
831 };
832 
833 static struct kobj_type f2fs_stat_ktype = {
834 	.default_groups = f2fs_stat_groups,
835 	.sysfs_ops	= &f2fs_stat_attr_ops,
836 	.release	= f2fs_stat_kobj_release,
837 };
838 
839 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
840 						void *offset)
841 {
842 	struct super_block *sb = seq->private;
843 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
844 	unsigned int total_segs =
845 			le32_to_cpu(sbi->raw_super->segment_count_main);
846 	int i;
847 
848 	seq_puts(seq, "format: segment_type|valid_blocks\n"
849 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
850 
851 	for (i = 0; i < total_segs; i++) {
852 		struct seg_entry *se = get_seg_entry(sbi, i);
853 
854 		if ((i % 10) == 0)
855 			seq_printf(seq, "%-10d", i);
856 		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
857 		if ((i % 10) == 9 || i == (total_segs - 1))
858 			seq_putc(seq, '\n');
859 		else
860 			seq_putc(seq, ' ');
861 	}
862 
863 	return 0;
864 }
865 
866 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
867 						void *offset)
868 {
869 	struct super_block *sb = seq->private;
870 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
871 	unsigned int total_segs =
872 			le32_to_cpu(sbi->raw_super->segment_count_main);
873 	int i, j;
874 
875 	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
876 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
877 
878 	for (i = 0; i < total_segs; i++) {
879 		struct seg_entry *se = get_seg_entry(sbi, i);
880 
881 		seq_printf(seq, "%-10d", i);
882 		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
883 		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
884 			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
885 		seq_putc(seq, '\n');
886 	}
887 	return 0;
888 }
889 
890 void f2fs_record_iostat(struct f2fs_sb_info *sbi)
891 {
892 	unsigned long long iostat_diff[NR_IO_TYPE];
893 	int i;
894 
895 	if (time_is_after_jiffies(sbi->iostat_next_period))
896 		return;
897 
898 	/* Need double check under the lock */
899 	spin_lock(&sbi->iostat_lock);
900 	if (time_is_after_jiffies(sbi->iostat_next_period)) {
901 		spin_unlock(&sbi->iostat_lock);
902 		return;
903 	}
904 	sbi->iostat_next_period = jiffies +
905 				msecs_to_jiffies(sbi->iostat_period_ms);
906 
907 	for (i = 0; i < NR_IO_TYPE; i++) {
908 		iostat_diff[i] = sbi->rw_iostat[i] -
909 				sbi->prev_rw_iostat[i];
910 		sbi->prev_rw_iostat[i] = sbi->rw_iostat[i];
911 	}
912 	spin_unlock(&sbi->iostat_lock);
913 
914 	trace_f2fs_iostat(sbi, iostat_diff);
915 }
916 
917 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
918 					       void *offset)
919 {
920 	struct super_block *sb = seq->private;
921 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
922 	time64_t now = ktime_get_real_seconds();
923 
924 	if (!sbi->iostat_enable)
925 		return 0;
926 
927 	seq_printf(seq, "time:		%-16llu\n", now);
928 
929 	/* print app write IOs */
930 	seq_puts(seq, "[WRITE]\n");
931 	seq_printf(seq, "app buffered:	%-16llu\n",
932 				sbi->rw_iostat[APP_BUFFERED_IO]);
933 	seq_printf(seq, "app direct:	%-16llu\n",
934 				sbi->rw_iostat[APP_DIRECT_IO]);
935 	seq_printf(seq, "app mapped:	%-16llu\n",
936 				sbi->rw_iostat[APP_MAPPED_IO]);
937 
938 	/* print fs write IOs */
939 	seq_printf(seq, "fs data:	%-16llu\n",
940 				sbi->rw_iostat[FS_DATA_IO]);
941 	seq_printf(seq, "fs node:	%-16llu\n",
942 				sbi->rw_iostat[FS_NODE_IO]);
943 	seq_printf(seq, "fs meta:	%-16llu\n",
944 				sbi->rw_iostat[FS_META_IO]);
945 	seq_printf(seq, "fs gc data:	%-16llu\n",
946 				sbi->rw_iostat[FS_GC_DATA_IO]);
947 	seq_printf(seq, "fs gc node:	%-16llu\n",
948 				sbi->rw_iostat[FS_GC_NODE_IO]);
949 	seq_printf(seq, "fs cp data:	%-16llu\n",
950 				sbi->rw_iostat[FS_CP_DATA_IO]);
951 	seq_printf(seq, "fs cp node:	%-16llu\n",
952 				sbi->rw_iostat[FS_CP_NODE_IO]);
953 	seq_printf(seq, "fs cp meta:	%-16llu\n",
954 				sbi->rw_iostat[FS_CP_META_IO]);
955 
956 	/* print app read IOs */
957 	seq_puts(seq, "[READ]\n");
958 	seq_printf(seq, "app buffered:	%-16llu\n",
959 				sbi->rw_iostat[APP_BUFFERED_READ_IO]);
960 	seq_printf(seq, "app direct:	%-16llu\n",
961 				sbi->rw_iostat[APP_DIRECT_READ_IO]);
962 	seq_printf(seq, "app mapped:	%-16llu\n",
963 				sbi->rw_iostat[APP_MAPPED_READ_IO]);
964 
965 	/* print fs read IOs */
966 	seq_printf(seq, "fs data:	%-16llu\n",
967 				sbi->rw_iostat[FS_DATA_READ_IO]);
968 	seq_printf(seq, "fs gc data:	%-16llu\n",
969 				sbi->rw_iostat[FS_GDATA_READ_IO]);
970 	seq_printf(seq, "fs compr_data:	%-16llu\n",
971 				sbi->rw_iostat[FS_CDATA_READ_IO]);
972 	seq_printf(seq, "fs node:	%-16llu\n",
973 				sbi->rw_iostat[FS_NODE_READ_IO]);
974 	seq_printf(seq, "fs meta:	%-16llu\n",
975 				sbi->rw_iostat[FS_META_READ_IO]);
976 
977 	/* print other IOs */
978 	seq_puts(seq, "[OTHER]\n");
979 	seq_printf(seq, "fs discard:	%-16llu\n",
980 				sbi->rw_iostat[FS_DISCARD]);
981 
982 	return 0;
983 }
984 
985 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
986 						void *offset)
987 {
988 	struct super_block *sb = seq->private;
989 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
990 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
991 	int i;
992 
993 	seq_puts(seq, "format: victim_secmap bitmaps\n");
994 
995 	for (i = 0; i < MAIN_SECS(sbi); i++) {
996 		if ((i % 10) == 0)
997 			seq_printf(seq, "%-10d", i);
998 		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
999 		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1000 			seq_putc(seq, '\n');
1001 		else
1002 			seq_putc(seq, ' ');
1003 	}
1004 	return 0;
1005 }
1006 
1007 int __init f2fs_init_sysfs(void)
1008 {
1009 	int ret;
1010 
1011 	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1012 	f2fs_kset.kobj.parent = fs_kobj;
1013 	ret = kset_register(&f2fs_kset);
1014 	if (ret)
1015 		return ret;
1016 
1017 	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1018 				   NULL, "features");
1019 	if (ret) {
1020 		kobject_put(&f2fs_feat);
1021 		kset_unregister(&f2fs_kset);
1022 	} else {
1023 		f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1024 	}
1025 	return ret;
1026 }
1027 
1028 void f2fs_exit_sysfs(void)
1029 {
1030 	kobject_put(&f2fs_feat);
1031 	kset_unregister(&f2fs_kset);
1032 	remove_proc_entry("fs/f2fs", NULL);
1033 	f2fs_proc_root = NULL;
1034 }
1035 
1036 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1037 {
1038 	struct super_block *sb = sbi->sb;
1039 	int err;
1040 
1041 	sbi->s_kobj.kset = &f2fs_kset;
1042 	init_completion(&sbi->s_kobj_unregister);
1043 	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1044 				"%s", sb->s_id);
1045 	if (err)
1046 		goto put_sb_kobj;
1047 
1048 	sbi->s_stat_kobj.kset = &f2fs_kset;
1049 	init_completion(&sbi->s_stat_kobj_unregister);
1050 	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1051 						&sbi->s_kobj, "stat");
1052 	if (err)
1053 		goto put_stat_kobj;
1054 
1055 	if (f2fs_proc_root)
1056 		sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1057 
1058 	if (sbi->s_proc) {
1059 		proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
1060 				segment_info_seq_show, sb);
1061 		proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
1062 				segment_bits_seq_show, sb);
1063 		proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
1064 				iostat_info_seq_show, sb);
1065 		proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
1066 				victim_bits_seq_show, sb);
1067 	}
1068 	return 0;
1069 put_stat_kobj:
1070 	kobject_put(&sbi->s_stat_kobj);
1071 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1072 put_sb_kobj:
1073 	kobject_put(&sbi->s_kobj);
1074 	wait_for_completion(&sbi->s_kobj_unregister);
1075 	return err;
1076 }
1077 
1078 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1079 {
1080 	if (sbi->s_proc) {
1081 		remove_proc_entry("iostat_info", sbi->s_proc);
1082 		remove_proc_entry("segment_info", sbi->s_proc);
1083 		remove_proc_entry("segment_bits", sbi->s_proc);
1084 		remove_proc_entry("victim_bits", sbi->s_proc);
1085 		remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1086 	}
1087 
1088 	kobject_del(&sbi->s_stat_kobj);
1089 	kobject_put(&sbi->s_stat_kobj);
1090 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1091 
1092 	kobject_del(&sbi->s_kobj);
1093 	kobject_put(&sbi->s_kobj);
1094 	wait_for_completion(&sbi->s_kobj_unregister);
1095 }
1096