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