xref: /linux/fs/f2fs/sysfs.c (revision 81d8e5e2132215d21f2cddffcd2b16d08c0389fa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * f2fs sysfs interface
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  * Copyright (c) 2017 Chao Yu <chao@kernel.org>
8  */
9 #include <linux/compiler.h>
10 #include <linux/proc_fs.h>
11 #include <linux/f2fs_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/unicode.h>
14 #include <linux/ioprio.h>
15 #include <linux/sysfs.h>
16 
17 #include "f2fs.h"
18 #include "segment.h"
19 #include "gc.h"
20 #include "iostat.h"
21 #include <trace/events/f2fs.h>
22 
23 static struct proc_dir_entry *f2fs_proc_root;
24 
25 /* Sysfs support for f2fs */
26 enum {
27 	GC_THREAD,	/* struct f2fs_gc_thread */
28 	SM_INFO,	/* struct f2fs_sm_info */
29 	DCC_INFO,	/* struct discard_cmd_control */
30 	NM_INFO,	/* struct f2fs_nm_info */
31 	F2FS_SBI,	/* struct f2fs_sb_info */
32 #ifdef CONFIG_F2FS_STAT_FS
33 	STAT_INFO,	/* struct f2fs_stat_info */
34 #endif
35 #ifdef CONFIG_F2FS_FAULT_INJECTION
36 	FAULT_INFO_RATE,	/* struct f2fs_fault_info */
37 	FAULT_INFO_TYPE,	/* struct f2fs_fault_info */
38 #endif
39 	RESERVED_BLOCKS,	/* struct f2fs_sb_info */
40 	CPRC_INFO,	/* struct ckpt_req_control */
41 	ATGC_INFO,	/* struct atgc_management */
42 };
43 
44 static const char *gc_mode_names[MAX_GC_MODE] = {
45 	"GC_NORMAL",
46 	"GC_IDLE_CB",
47 	"GC_IDLE_GREEDY",
48 	"GC_IDLE_AT",
49 	"GC_URGENT_HIGH",
50 	"GC_URGENT_LOW",
51 	"GC_URGENT_MID"
52 };
53 
54 struct f2fs_attr {
55 	struct attribute attr;
56 	ssize_t (*show)(struct f2fs_attr *a, struct f2fs_sb_info *sbi, char *buf);
57 	ssize_t (*store)(struct f2fs_attr *a, struct f2fs_sb_info *sbi,
58 			 const char *buf, size_t len);
59 	int struct_type;
60 	int offset;
61 	int id;
62 };
63 
64 struct f2fs_base_attr {
65 	struct attribute attr;
66 	ssize_t (*show)(struct f2fs_base_attr *a, char *buf);
67 	ssize_t (*store)(struct f2fs_base_attr *a, const char *buf, size_t len);
68 };
69 
70 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
71 			     struct f2fs_sb_info *sbi, char *buf);
72 
__struct_ptr(struct f2fs_sb_info * sbi,int struct_type)73 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
74 {
75 	if (struct_type == GC_THREAD)
76 		return (unsigned char *)sbi->gc_thread;
77 	else if (struct_type == SM_INFO)
78 		return (unsigned char *)SM_I(sbi);
79 	else if (struct_type == DCC_INFO)
80 		return (unsigned char *)SM_I(sbi)->dcc_info;
81 	else if (struct_type == NM_INFO)
82 		return (unsigned char *)NM_I(sbi);
83 	else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
84 		return (unsigned char *)sbi;
85 #ifdef CONFIG_F2FS_FAULT_INJECTION
86 	else if (struct_type == FAULT_INFO_RATE ||
87 					struct_type == FAULT_INFO_TYPE)
88 		return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
89 #endif
90 #ifdef CONFIG_F2FS_STAT_FS
91 	else if (struct_type == STAT_INFO)
92 		return (unsigned char *)F2FS_STAT(sbi);
93 #endif
94 	else if (struct_type == CPRC_INFO)
95 		return (unsigned char *)&sbi->cprc_info;
96 	else if (struct_type == ATGC_INFO)
97 		return (unsigned char *)&sbi->am;
98 	return NULL;
99 }
100 
dirty_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)101 static ssize_t dirty_segments_show(struct f2fs_attr *a,
102 		struct f2fs_sb_info *sbi, char *buf)
103 {
104 	return sysfs_emit(buf, "%llu\n",
105 			(unsigned long long)(dirty_segments(sbi)));
106 }
107 
free_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)108 static ssize_t free_segments_show(struct f2fs_attr *a,
109 		struct f2fs_sb_info *sbi, char *buf)
110 {
111 	return sysfs_emit(buf, "%llu\n",
112 			(unsigned long long)(free_segments(sbi)));
113 }
114 
ovp_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)115 static ssize_t ovp_segments_show(struct f2fs_attr *a,
116 		struct f2fs_sb_info *sbi, char *buf)
117 {
118 	return sysfs_emit(buf, "%llu\n",
119 			(unsigned long long)(overprovision_segments(sbi)));
120 }
121 
lifetime_write_kbytes_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)122 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
123 		struct f2fs_sb_info *sbi, char *buf)
124 {
125 	return sysfs_emit(buf, "%llu\n",
126 			(unsigned long long)(sbi->kbytes_written +
127 			((f2fs_get_sectors_written(sbi) -
128 				sbi->sectors_written_start) >> 1)));
129 }
130 
sb_status_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)131 static ssize_t sb_status_show(struct f2fs_attr *a,
132 		struct f2fs_sb_info *sbi, char *buf)
133 {
134 	return sysfs_emit(buf, "%lx\n", sbi->s_flag);
135 }
136 
cp_status_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)137 static ssize_t cp_status_show(struct f2fs_attr *a,
138 		struct f2fs_sb_info *sbi, char *buf)
139 {
140 	return sysfs_emit(buf, "%x\n", le32_to_cpu(F2FS_CKPT(sbi)->ckpt_flags));
141 }
142 
pending_discard_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)143 static ssize_t pending_discard_show(struct f2fs_attr *a,
144 		struct f2fs_sb_info *sbi, char *buf)
145 {
146 	if (!SM_I(sbi)->dcc_info)
147 		return -EINVAL;
148 	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
149 				&SM_I(sbi)->dcc_info->discard_cmd_cnt));
150 }
151 
issued_discard_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)152 static ssize_t issued_discard_show(struct f2fs_attr *a,
153 		struct f2fs_sb_info *sbi, char *buf)
154 {
155 	if (!SM_I(sbi)->dcc_info)
156 		return -EINVAL;
157 	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
158 				&SM_I(sbi)->dcc_info->issued_discard));
159 }
160 
queued_discard_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)161 static ssize_t queued_discard_show(struct f2fs_attr *a,
162 		struct f2fs_sb_info *sbi, char *buf)
163 {
164 	if (!SM_I(sbi)->dcc_info)
165 		return -EINVAL;
166 	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
167 				&SM_I(sbi)->dcc_info->queued_discard));
168 }
169 
undiscard_blks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)170 static ssize_t undiscard_blks_show(struct f2fs_attr *a,
171 		struct f2fs_sb_info *sbi, char *buf)
172 {
173 	if (!SM_I(sbi)->dcc_info)
174 		return -EINVAL;
175 	return sysfs_emit(buf, "%u\n",
176 				SM_I(sbi)->dcc_info->undiscard_blks);
177 }
178 
atgc_enabled_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)179 static ssize_t atgc_enabled_show(struct f2fs_attr *a,
180 		struct f2fs_sb_info *sbi, char *buf)
181 {
182 	return sysfs_emit(buf, "%d\n", sbi->am.atgc_enabled ? 1 : 0);
183 }
184 
gc_mode_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)185 static ssize_t gc_mode_show(struct f2fs_attr *a,
186 		struct f2fs_sb_info *sbi, char *buf)
187 {
188 	return sysfs_emit(buf, "%s\n", gc_mode_names[sbi->gc_mode]);
189 }
190 
features_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)191 static ssize_t features_show(struct f2fs_attr *a,
192 		struct f2fs_sb_info *sbi, char *buf)
193 {
194 	int len = 0;
195 
196 	if (f2fs_sb_has_encrypt(sbi))
197 		len += sysfs_emit_at(buf, len, "%s",
198 						"encryption");
199 	if (f2fs_sb_has_blkzoned(sbi))
200 		len += sysfs_emit_at(buf, len, "%s%s",
201 				len ? ", " : "", "blkzoned");
202 	if (f2fs_sb_has_extra_attr(sbi))
203 		len += sysfs_emit_at(buf, len, "%s%s",
204 				len ? ", " : "", "extra_attr");
205 	if (f2fs_sb_has_project_quota(sbi))
206 		len += sysfs_emit_at(buf, len, "%s%s",
207 				len ? ", " : "", "projquota");
208 	if (f2fs_sb_has_inode_chksum(sbi))
209 		len += sysfs_emit_at(buf, len, "%s%s",
210 				len ? ", " : "", "inode_checksum");
211 	if (f2fs_sb_has_flexible_inline_xattr(sbi))
212 		len += sysfs_emit_at(buf, len, "%s%s",
213 				len ? ", " : "", "flexible_inline_xattr");
214 	if (f2fs_sb_has_quota_ino(sbi))
215 		len += sysfs_emit_at(buf, len, "%s%s",
216 				len ? ", " : "", "quota_ino");
217 	if (f2fs_sb_has_inode_crtime(sbi))
218 		len += sysfs_emit_at(buf, len, "%s%s",
219 				len ? ", " : "", "inode_crtime");
220 	if (f2fs_sb_has_lost_found(sbi))
221 		len += sysfs_emit_at(buf, len, "%s%s",
222 				len ? ", " : "", "lost_found");
223 	if (f2fs_sb_has_verity(sbi))
224 		len += sysfs_emit_at(buf, len, "%s%s",
225 				len ? ", " : "", "verity");
226 	if (f2fs_sb_has_sb_chksum(sbi))
227 		len += sysfs_emit_at(buf, len, "%s%s",
228 				len ? ", " : "", "sb_checksum");
229 	if (f2fs_sb_has_casefold(sbi))
230 		len += sysfs_emit_at(buf, len, "%s%s",
231 				len ? ", " : "", "casefold");
232 	if (f2fs_sb_has_readonly(sbi))
233 		len += sysfs_emit_at(buf, len, "%s%s",
234 				len ? ", " : "", "readonly");
235 	if (f2fs_sb_has_compression(sbi))
236 		len += sysfs_emit_at(buf, len, "%s%s",
237 				len ? ", " : "", "compression");
238 	len += sysfs_emit_at(buf, len, "%s%s",
239 				len ? ", " : "", "pin_file");
240 	len += sysfs_emit_at(buf, len, "\n");
241 	return len;
242 }
243 
current_reserved_blocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)244 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
245 					struct f2fs_sb_info *sbi, char *buf)
246 {
247 	return sysfs_emit(buf, "%u\n", sbi->current_reserved_blocks);
248 }
249 
unusable_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)250 static ssize_t unusable_show(struct f2fs_attr *a,
251 		struct f2fs_sb_info *sbi, char *buf)
252 {
253 	block_t unusable;
254 
255 	if (test_opt(sbi, DISABLE_CHECKPOINT))
256 		unusable = sbi->unusable_block_count;
257 	else
258 		unusable = f2fs_get_unusable_blocks(sbi);
259 	return sysfs_emit(buf, "%llu\n", (unsigned long long)unusable);
260 }
261 
encoding_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)262 static ssize_t encoding_show(struct f2fs_attr *a,
263 		struct f2fs_sb_info *sbi, char *buf)
264 {
265 #if IS_ENABLED(CONFIG_UNICODE)
266 	struct super_block *sb = sbi->sb;
267 
268 	if (f2fs_sb_has_casefold(sbi))
269 		return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
270 			(sb->s_encoding->version >> 16) & 0xff,
271 			(sb->s_encoding->version >> 8) & 0xff,
272 			sb->s_encoding->version & 0xff);
273 #endif
274 	return sysfs_emit(buf, "(none)\n");
275 }
276 
mounted_time_sec_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)277 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
278 		struct f2fs_sb_info *sbi, char *buf)
279 {
280 	return sysfs_emit(buf, "%llu\n", SIT_I(sbi)->mounted_time);
281 }
282 
283 #ifdef CONFIG_F2FS_STAT_FS
moved_blocks_foreground_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)284 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
285 				struct f2fs_sb_info *sbi, char *buf)
286 {
287 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
288 
289 	return sysfs_emit(buf, "%llu\n",
290 		(unsigned long long)(si->tot_blks -
291 			(si->bg_data_blks + si->bg_node_blks)));
292 }
293 
moved_blocks_background_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)294 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
295 				struct f2fs_sb_info *sbi, char *buf)
296 {
297 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
298 
299 	return sysfs_emit(buf, "%llu\n",
300 		(unsigned long long)(si->bg_data_blks + si->bg_node_blks));
301 }
302 
avg_vblocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)303 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
304 		struct f2fs_sb_info *sbi, char *buf)
305 {
306 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
307 
308 	si->dirty_count = dirty_segments(sbi);
309 	f2fs_update_sit_info(sbi);
310 	return sysfs_emit(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
311 }
312 #endif
313 
main_blkaddr_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)314 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
315 				struct f2fs_sb_info *sbi, char *buf)
316 {
317 	return sysfs_emit(buf, "%llu\n",
318 			(unsigned long long)MAIN_BLKADDR(sbi));
319 }
320 
f2fs_sbi_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)321 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
322 			struct f2fs_sb_info *sbi, char *buf)
323 {
324 	unsigned char *ptr = NULL;
325 	unsigned int *ui;
326 
327 	ptr = __struct_ptr(sbi, a->struct_type);
328 	if (!ptr)
329 		return -EINVAL;
330 
331 	if (!strcmp(a->attr.name, "extension_list")) {
332 		__u8 (*extlist)[F2FS_EXTENSION_LEN] =
333 					sbi->raw_super->extension_list;
334 		int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
335 		int hot_count = sbi->raw_super->hot_ext_count;
336 		int len = 0, i;
337 
338 		len += sysfs_emit_at(buf, len, "cold file extension:\n");
339 		for (i = 0; i < cold_count; i++)
340 			len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
341 
342 		len += sysfs_emit_at(buf, len, "hot file extension:\n");
343 		for (i = cold_count; i < cold_count + hot_count; i++)
344 			len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
345 
346 		return len;
347 	}
348 
349 	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
350 		struct ckpt_req_control *cprc = &sbi->cprc_info;
351 		int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
352 		int level = IOPRIO_PRIO_LEVEL(cprc->ckpt_thread_ioprio);
353 
354 		if (class != IOPRIO_CLASS_RT && class != IOPRIO_CLASS_BE)
355 			return -EINVAL;
356 
357 		return sysfs_emit(buf, "%s,%d\n",
358 			class == IOPRIO_CLASS_RT ? "rt" : "be", level);
359 	}
360 
361 #ifdef CONFIG_F2FS_FS_COMPRESSION
362 	if (!strcmp(a->attr.name, "compr_written_block"))
363 		return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
364 
365 	if (!strcmp(a->attr.name, "compr_saved_block"))
366 		return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
367 
368 	if (!strcmp(a->attr.name, "compr_new_inode"))
369 		return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
370 #endif
371 
372 	if (!strcmp(a->attr.name, "gc_segment_mode"))
373 		return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
374 
375 	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
376 		return sysfs_emit(buf, "%u\n",
377 			sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
378 	}
379 
380 	if (!strcmp(a->attr.name, "current_atomic_write")) {
381 		s64 current_write = atomic64_read(&sbi->current_atomic_write);
382 
383 		return sysfs_emit(buf, "%lld\n", current_write);
384 	}
385 
386 	if (!strcmp(a->attr.name, "peak_atomic_write"))
387 		return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write);
388 
389 	if (!strcmp(a->attr.name, "committed_atomic_block"))
390 		return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block);
391 
392 	if (!strcmp(a->attr.name, "revoked_atomic_block"))
393 		return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block);
394 
395 #ifdef CONFIG_F2FS_STAT_FS
396 	if (!strcmp(a->attr.name, "cp_foreground_calls"))
397 		return sysfs_emit(buf, "%d\n",
398 				atomic_read(&sbi->cp_call_count[TOTAL_CALL]) -
399 				atomic_read(&sbi->cp_call_count[BACKGROUND]));
400 	if (!strcmp(a->attr.name, "cp_background_calls"))
401 		return sysfs_emit(buf, "%d\n",
402 				atomic_read(&sbi->cp_call_count[BACKGROUND]));
403 #endif
404 
405 	ui = (unsigned int *)(ptr + a->offset);
406 
407 	return sysfs_emit(buf, "%u\n", *ui);
408 }
409 
__sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)410 static ssize_t __sbi_store(struct f2fs_attr *a,
411 			struct f2fs_sb_info *sbi,
412 			const char *buf, size_t count)
413 {
414 	unsigned char *ptr;
415 	unsigned long t;
416 	unsigned int *ui;
417 	ssize_t ret;
418 
419 	ptr = __struct_ptr(sbi, a->struct_type);
420 	if (!ptr)
421 		return -EINVAL;
422 
423 	if (!strcmp(a->attr.name, "extension_list")) {
424 		const char *name = strim((char *)buf);
425 		bool set = true, hot;
426 
427 		if (!strncmp(name, "[h]", 3))
428 			hot = true;
429 		else if (!strncmp(name, "[c]", 3))
430 			hot = false;
431 		else
432 			return -EINVAL;
433 
434 		name += 3;
435 
436 		if (*name == '!') {
437 			name++;
438 			set = false;
439 		}
440 
441 		if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
442 			return -EINVAL;
443 
444 		f2fs_down_write(&sbi->sb_lock);
445 
446 		ret = f2fs_update_extension_list(sbi, name, hot, set);
447 		if (ret)
448 			goto out;
449 
450 		ret = f2fs_commit_super(sbi, false);
451 		if (ret)
452 			f2fs_update_extension_list(sbi, name, hot, !set);
453 out:
454 		f2fs_up_write(&sbi->sb_lock);
455 		return ret ? ret : count;
456 	}
457 
458 	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
459 		const char *name = strim((char *)buf);
460 		struct ckpt_req_control *cprc = &sbi->cprc_info;
461 		int class;
462 		long level;
463 		int ret;
464 
465 		if (!strncmp(name, "rt,", 3))
466 			class = IOPRIO_CLASS_RT;
467 		else if (!strncmp(name, "be,", 3))
468 			class = IOPRIO_CLASS_BE;
469 		else
470 			return -EINVAL;
471 
472 		name += 3;
473 		ret = kstrtol(name, 10, &level);
474 		if (ret)
475 			return ret;
476 		if (level >= IOPRIO_NR_LEVELS || level < 0)
477 			return -EINVAL;
478 
479 		cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, level);
480 		if (test_opt(sbi, MERGE_CHECKPOINT)) {
481 			ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
482 					cprc->ckpt_thread_ioprio);
483 			if (ret)
484 				return ret;
485 		}
486 
487 		return count;
488 	}
489 
490 	ui = (unsigned int *)(ptr + a->offset);
491 
492 	ret = kstrtoul(skip_spaces(buf), 0, &t);
493 	if (ret < 0)
494 		return ret;
495 #ifdef CONFIG_F2FS_FAULT_INJECTION
496 	if (a->struct_type == FAULT_INFO_TYPE) {
497 		if (f2fs_build_fault_attr(sbi, 0, t))
498 			return -EINVAL;
499 		return count;
500 	}
501 	if (a->struct_type == FAULT_INFO_RATE) {
502 		if (f2fs_build_fault_attr(sbi, t, 0))
503 			return -EINVAL;
504 		return count;
505 	}
506 #endif
507 	if (a->struct_type == RESERVED_BLOCKS) {
508 		spin_lock(&sbi->stat_lock);
509 		if (t > (unsigned long)(sbi->user_block_count -
510 				F2FS_OPTION(sbi).root_reserved_blocks)) {
511 			spin_unlock(&sbi->stat_lock);
512 			return -EINVAL;
513 		}
514 		*ui = t;
515 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
516 				sbi->user_block_count - valid_user_blocks(sbi));
517 		spin_unlock(&sbi->stat_lock);
518 		return count;
519 	}
520 
521 	if (!strcmp(a->attr.name, "discard_io_aware_gran")) {
522 		if (t > MAX_PLIST_NUM)
523 			return -EINVAL;
524 		if (!f2fs_block_unit_discard(sbi))
525 			return -EINVAL;
526 		if (t == *ui)
527 			return count;
528 		*ui = t;
529 		return count;
530 	}
531 
532 	if (!strcmp(a->attr.name, "discard_granularity")) {
533 		if (t == 0 || t > MAX_PLIST_NUM)
534 			return -EINVAL;
535 		if (!f2fs_block_unit_discard(sbi))
536 			return -EINVAL;
537 		if (t == *ui)
538 			return count;
539 		*ui = t;
540 		return count;
541 	}
542 
543 	if (!strcmp(a->attr.name, "max_ordered_discard")) {
544 		if (t == 0 || t > MAX_PLIST_NUM)
545 			return -EINVAL;
546 		if (!f2fs_block_unit_discard(sbi))
547 			return -EINVAL;
548 		*ui = t;
549 		return count;
550 	}
551 
552 	if (!strcmp(a->attr.name, "discard_urgent_util")) {
553 		if (t > 100)
554 			return -EINVAL;
555 		*ui = t;
556 		return count;
557 	}
558 
559 	if (!strcmp(a->attr.name, "discard_io_aware")) {
560 		if (t >= DPOLICY_IO_AWARE_MAX)
561 			return -EINVAL;
562 		*ui = t;
563 		return count;
564 	}
565 
566 	if (!strcmp(a->attr.name, "migration_granularity")) {
567 		if (t == 0 || t > SEGS_PER_SEC(sbi))
568 			return -EINVAL;
569 	}
570 
571 	if (!strcmp(a->attr.name, "migration_window_granularity")) {
572 		if (t == 0 || t > SEGS_PER_SEC(sbi))
573 			return -EINVAL;
574 	}
575 
576 	if (!strcmp(a->attr.name, "gc_urgent")) {
577 		if (t == 0) {
578 			sbi->gc_mode = GC_NORMAL;
579 		} else if (t == 1) {
580 			sbi->gc_mode = GC_URGENT_HIGH;
581 			if (sbi->gc_thread) {
582 				sbi->gc_thread->gc_wake = true;
583 				wake_up_interruptible_all(
584 					&sbi->gc_thread->gc_wait_queue_head);
585 				wake_up_discard_thread(sbi, true);
586 			}
587 		} else if (t == 2) {
588 			sbi->gc_mode = GC_URGENT_LOW;
589 		} else if (t == 3) {
590 			sbi->gc_mode = GC_URGENT_MID;
591 			if (sbi->gc_thread) {
592 				sbi->gc_thread->gc_wake = true;
593 				wake_up_interruptible_all(
594 					&sbi->gc_thread->gc_wait_queue_head);
595 			}
596 		} else {
597 			return -EINVAL;
598 		}
599 		return count;
600 	}
601 	if (!strcmp(a->attr.name, "gc_idle")) {
602 		if (t == GC_IDLE_CB) {
603 			sbi->gc_mode = GC_IDLE_CB;
604 		} else if (t == GC_IDLE_GREEDY) {
605 			sbi->gc_mode = GC_IDLE_GREEDY;
606 		} else if (t == GC_IDLE_AT) {
607 			if (!sbi->am.atgc_enabled)
608 				return -EINVAL;
609 			sbi->gc_mode = GC_IDLE_AT;
610 		} else {
611 			sbi->gc_mode = GC_NORMAL;
612 		}
613 		return count;
614 	}
615 
616 	if (!strcmp(a->attr.name, "gc_remaining_trials")) {
617 		spin_lock(&sbi->gc_remaining_trials_lock);
618 		sbi->gc_remaining_trials = t;
619 		spin_unlock(&sbi->gc_remaining_trials_lock);
620 
621 		return count;
622 	}
623 
624 #ifdef CONFIG_F2FS_IOSTAT
625 	if (!strcmp(a->attr.name, "iostat_enable")) {
626 		sbi->iostat_enable = !!t;
627 		if (!sbi->iostat_enable)
628 			f2fs_reset_iostat(sbi);
629 		return count;
630 	}
631 
632 	if (!strcmp(a->attr.name, "iostat_period_ms")) {
633 		if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
634 			return -EINVAL;
635 		spin_lock_irq(&sbi->iostat_lock);
636 		sbi->iostat_period_ms = (unsigned int)t;
637 		spin_unlock_irq(&sbi->iostat_lock);
638 		return count;
639 	}
640 #endif
641 
642 #ifdef CONFIG_BLK_DEV_ZONED
643 	if (!strcmp(a->attr.name, "blkzone_alloc_policy")) {
644 		if (t < BLKZONE_ALLOC_PRIOR_SEQ || t > BLKZONE_ALLOC_PRIOR_CONV)
645 			return -EINVAL;
646 		sbi->blkzone_alloc_policy = t;
647 		return count;
648 	}
649 #endif
650 
651 #ifdef CONFIG_F2FS_FS_COMPRESSION
652 	if (!strcmp(a->attr.name, "compr_written_block") ||
653 		!strcmp(a->attr.name, "compr_saved_block")) {
654 		if (t != 0)
655 			return -EINVAL;
656 		sbi->compr_written_block = 0;
657 		sbi->compr_saved_block = 0;
658 		return count;
659 	}
660 
661 	if (!strcmp(a->attr.name, "compr_new_inode")) {
662 		if (t != 0)
663 			return -EINVAL;
664 		sbi->compr_new_inode = 0;
665 		return count;
666 	}
667 
668 	if (!strcmp(a->attr.name, "compress_percent")) {
669 		if (t == 0 || t > 100)
670 			return -EINVAL;
671 		*ui = t;
672 		return count;
673 	}
674 
675 	if (!strcmp(a->attr.name, "compress_watermark")) {
676 		if (t == 0 || t > 100)
677 			return -EINVAL;
678 		*ui = t;
679 		return count;
680 	}
681 #endif
682 
683 	if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
684 		if (t > 100)
685 			return -EINVAL;
686 		sbi->am.candidate_ratio = t;
687 		return count;
688 	}
689 
690 	if (!strcmp(a->attr.name, "atgc_age_weight")) {
691 		if (t > 100)
692 			return -EINVAL;
693 		sbi->am.age_weight = t;
694 		return count;
695 	}
696 
697 	if (!strcmp(a->attr.name, "gc_segment_mode")) {
698 		if (t < MAX_GC_MODE)
699 			sbi->gc_segment_mode = t;
700 		else
701 			return -EINVAL;
702 		return count;
703 	}
704 
705 	if (!strcmp(a->attr.name, "gc_pin_file_threshold")) {
706 		if (t > MAX_GC_FAILED_PINNED_FILES)
707 			return -EINVAL;
708 		sbi->gc_pin_file_threshold = t;
709 		return count;
710 	}
711 
712 	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
713 		if (t != 0)
714 			return -EINVAL;
715 		sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
716 		return count;
717 	}
718 
719 	if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
720 		if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
721 			sbi->seq_file_ra_mul = t;
722 		else
723 			return -EINVAL;
724 		return count;
725 	}
726 
727 	if (!strcmp(a->attr.name, "max_fragment_chunk")) {
728 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
729 			sbi->max_fragment_chunk = t;
730 		else
731 			return -EINVAL;
732 		return count;
733 	}
734 
735 	if (!strcmp(a->attr.name, "max_fragment_hole")) {
736 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
737 			sbi->max_fragment_hole = t;
738 		else
739 			return -EINVAL;
740 		return count;
741 	}
742 
743 	if (!strcmp(a->attr.name, "peak_atomic_write")) {
744 		if (t != 0)
745 			return -EINVAL;
746 		sbi->peak_atomic_write = 0;
747 		return count;
748 	}
749 
750 	if (!strcmp(a->attr.name, "committed_atomic_block")) {
751 		if (t != 0)
752 			return -EINVAL;
753 		sbi->committed_atomic_block = 0;
754 		return count;
755 	}
756 
757 	if (!strcmp(a->attr.name, "revoked_atomic_block")) {
758 		if (t != 0)
759 			return -EINVAL;
760 		sbi->revoked_atomic_block = 0;
761 		return count;
762 	}
763 
764 	if (!strcmp(a->attr.name, "readdir_ra")) {
765 		sbi->readdir_ra = !!t;
766 		return count;
767 	}
768 
769 	if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
770 		if (t == 0 || t >= sbi->warm_data_age_threshold)
771 			return -EINVAL;
772 		if (t == *ui)
773 			return count;
774 		*ui = (unsigned int)t;
775 		return count;
776 	}
777 
778 	if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
779 		if (t <= sbi->hot_data_age_threshold)
780 			return -EINVAL;
781 		if (t == *ui)
782 			return count;
783 		*ui = (unsigned int)t;
784 		return count;
785 	}
786 
787 	if (!strcmp(a->attr.name, "last_age_weight")) {
788 		if (t > 100)
789 			return -EINVAL;
790 		if (t == *ui)
791 			return count;
792 		*ui = (unsigned int)t;
793 		return count;
794 	}
795 
796 	if (!strcmp(a->attr.name, "max_read_extent_count")) {
797 		if (t > UINT_MAX)
798 			return -EINVAL;
799 		*ui = (unsigned int)t;
800 		return count;
801 	}
802 
803 	if (!strcmp(a->attr.name, "ipu_policy")) {
804 		if (t >= BIT(F2FS_IPU_MAX))
805 			return -EINVAL;
806 		/* allow F2FS_IPU_NOCACHE only for IPU in the pinned file */
807 		if (f2fs_lfs_mode(sbi) && (t & ~BIT(F2FS_IPU_NOCACHE)))
808 			return -EINVAL;
809 		SM_I(sbi)->ipu_policy = (unsigned int)t;
810 		return count;
811 	}
812 
813 	if (!strcmp(a->attr.name, "dir_level")) {
814 		if (t > MAX_DIR_HASH_DEPTH)
815 			return -EINVAL;
816 		sbi->dir_level = t;
817 		return count;
818 	}
819 
820 	*ui = (unsigned int)t;
821 
822 	return count;
823 }
824 
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)825 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
826 			struct f2fs_sb_info *sbi,
827 			const char *buf, size_t count)
828 {
829 	ssize_t ret;
830 	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
831 					a->struct_type == GC_THREAD);
832 
833 	if (gc_entry) {
834 		if (!down_read_trylock(&sbi->sb->s_umount))
835 			return -EAGAIN;
836 	}
837 	ret = __sbi_store(a, sbi, buf, count);
838 	if (gc_entry)
839 		up_read(&sbi->sb->s_umount);
840 
841 	return ret;
842 }
843 
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)844 static ssize_t f2fs_attr_show(struct kobject *kobj,
845 				struct attribute *attr, char *buf)
846 {
847 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
848 								s_kobj);
849 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
850 
851 	return a->show ? a->show(a, sbi, buf) : 0;
852 }
853 
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)854 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
855 						const char *buf, size_t len)
856 {
857 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
858 									s_kobj);
859 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
860 
861 	return a->store ? a->store(a, sbi, buf, len) : 0;
862 }
863 
f2fs_sb_release(struct kobject * kobj)864 static void f2fs_sb_release(struct kobject *kobj)
865 {
866 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
867 								s_kobj);
868 	complete(&sbi->s_kobj_unregister);
869 }
870 
f2fs_base_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)871 static ssize_t f2fs_base_attr_show(struct kobject *kobj,
872 				struct attribute *attr, char *buf)
873 {
874 	struct f2fs_base_attr *a = container_of(attr,
875 				struct f2fs_base_attr, attr);
876 
877 	return a->show ? a->show(a, buf) : 0;
878 }
879 
f2fs_base_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)880 static ssize_t f2fs_base_attr_store(struct kobject *kobj,
881 				struct attribute *attr,
882 				const char *buf, size_t len)
883 {
884 	struct f2fs_base_attr *a = container_of(attr,
885 				struct f2fs_base_attr, attr);
886 
887 	return a->store ? a->store(a, buf, len) : 0;
888 }
889 
890 /*
891  * Note that there are three feature list entries:
892  * 1) /sys/fs/f2fs/features
893  *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
894  *     - ref. F2FS_FEATURE_RO_ATTR()
895  *
896  * 2) /sys/fs/f2fs/$s_id/features <deprecated>
897  *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
898  *     won't add new feature anymore, and thus, users should check entries in 3)
899  *     instead of this 2).
900  *
901  * 3) /sys/fs/f2fs/$s_id/feature_list
902  *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
903  *     sysfs entry rule where each entry should expose single value.
904  *     This list covers old feature list provided by 2) and beyond. Therefore,
905  *     please add new on-disk feature in this list only.
906  *     - ref. F2FS_SB_FEATURE_RO_ATTR()
907  */
f2fs_feature_show(struct f2fs_base_attr * a,char * buf)908 static ssize_t f2fs_feature_show(struct f2fs_base_attr *a, char *buf)
909 {
910 	return sysfs_emit(buf, "supported\n");
911 }
912 
913 #define F2FS_FEATURE_RO_ATTR(_name)				\
914 static struct f2fs_base_attr f2fs_base_attr_##_name = {		\
915 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
916 	.show	= f2fs_feature_show,				\
917 }
918 
f2fs_tune_show(struct f2fs_base_attr * a,char * buf)919 static ssize_t f2fs_tune_show(struct f2fs_base_attr *a, char *buf)
920 {
921 	unsigned int res = 0;
922 
923 	if (!strcmp(a->attr.name, "reclaim_caches_kb"))
924 		res = f2fs_donate_files();
925 
926 	return sysfs_emit(buf, "%u\n", res);
927 }
928 
f2fs_tune_store(struct f2fs_base_attr * a,const char * buf,size_t count)929 static ssize_t f2fs_tune_store(struct f2fs_base_attr *a,
930 			const char *buf, size_t count)
931 {
932 	unsigned long t;
933 	int ret;
934 
935 	ret = kstrtoul(skip_spaces(buf), 0, &t);
936 	if (ret)
937 		return ret;
938 
939 	if (!strcmp(a->attr.name, "reclaim_caches_kb"))
940 		f2fs_reclaim_caches(t);
941 
942 	return count;
943 }
944 
945 #define F2FS_TUNE_RW_ATTR(_name)				\
946 static struct f2fs_base_attr f2fs_base_attr_##_name = {		\
947 	.attr = {.name = __stringify(_name), .mode = 0644 },	\
948 	.show	= f2fs_tune_show,				\
949 	.store	= f2fs_tune_store,				\
950 }
951 
f2fs_sb_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)952 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
953 		struct f2fs_sb_info *sbi, char *buf)
954 {
955 	if (F2FS_HAS_FEATURE(sbi, a->id))
956 		return sysfs_emit(buf, "supported\n");
957 	return sysfs_emit(buf, "unsupported\n");
958 }
959 
960 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
961 static struct f2fs_attr f2fs_attr_sb_##_name = {		\
962 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
963 	.show	= f2fs_sb_feature_show,				\
964 	.id	= F2FS_FEATURE_##_feat,				\
965 }
966 
967 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
968 static struct f2fs_attr f2fs_attr_##_name = {			\
969 	.attr = {.name = __stringify(_name), .mode = _mode },	\
970 	.show	= _show,					\
971 	.store	= _store,					\
972 	.struct_type = _struct_type,				\
973 	.offset = _offset					\
974 }
975 
976 #define F2FS_RO_ATTR(struct_type, struct_name, name, elname)	\
977 	F2FS_ATTR_OFFSET(struct_type, name, 0444,		\
978 		f2fs_sbi_show, NULL,				\
979 		offsetof(struct struct_name, elname))
980 
981 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
982 	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
983 		f2fs_sbi_show, f2fs_sbi_store,			\
984 		offsetof(struct struct_name, elname))
985 
986 #define F2FS_GENERAL_RO_ATTR(name) \
987 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
988 
989 #ifdef CONFIG_F2FS_STAT_FS
990 #define STAT_INFO_RO_ATTR(name, elname)				\
991 	F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname)
992 #endif
993 
994 #define GC_THREAD_RW_ATTR(name, elname)				\
995 	F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname)
996 
997 #define SM_INFO_RW_ATTR(name, elname)				\
998 	F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname)
999 
1000 #define SM_INFO_GENERAL_RW_ATTR(elname)				\
1001 	SM_INFO_RW_ATTR(elname, elname)
1002 
1003 #define DCC_INFO_RW_ATTR(name, elname)				\
1004 	F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname)
1005 
1006 #define DCC_INFO_GENERAL_RW_ATTR(elname)			\
1007 	DCC_INFO_RW_ATTR(elname, elname)
1008 
1009 #define NM_INFO_RW_ATTR(name, elname)				\
1010 	F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname)
1011 
1012 #define NM_INFO_GENERAL_RW_ATTR(elname)				\
1013 	NM_INFO_RW_ATTR(elname, elname)
1014 
1015 #define F2FS_SBI_RW_ATTR(name, elname)				\
1016 	F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname)
1017 
1018 #define F2FS_SBI_GENERAL_RW_ATTR(elname)			\
1019 	F2FS_SBI_RW_ATTR(elname, elname)
1020 
1021 #define F2FS_SBI_GENERAL_RO_ATTR(elname)			\
1022 	F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname)
1023 
1024 #ifdef CONFIG_F2FS_FAULT_INJECTION
1025 #define FAULT_INFO_GENERAL_RW_ATTR(type, elname)		\
1026 	F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname)
1027 #endif
1028 
1029 #define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname)			\
1030 	F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname)
1031 
1032 #define CPRC_INFO_GENERAL_RW_ATTR(elname)			\
1033 	F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname)
1034 
1035 #define ATGC_INFO_RW_ATTR(name, elname)				\
1036 	F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
1037 
1038 /* GC_THREAD ATTR */
1039 GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
1040 GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
1041 GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time);
1042 GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
1043 GC_THREAD_RW_ATTR(gc_no_zoned_gc_percent, no_zoned_gc_percent);
1044 GC_THREAD_RW_ATTR(gc_boost_zoned_gc_percent, boost_zoned_gc_percent);
1045 GC_THREAD_RW_ATTR(gc_valid_thresh_ratio, valid_thresh_ratio);
1046 
1047 /* SM_INFO ATTR */
1048 SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments);
1049 SM_INFO_GENERAL_RW_ATTR(ipu_policy);
1050 SM_INFO_GENERAL_RW_ATTR(min_ipu_util);
1051 SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks);
1052 SM_INFO_GENERAL_RW_ATTR(min_seq_blocks);
1053 SM_INFO_GENERAL_RW_ATTR(min_hot_blocks);
1054 SM_INFO_GENERAL_RW_ATTR(min_ssr_sections);
1055 SM_INFO_GENERAL_RW_ATTR(reserved_segments);
1056 
1057 /* DCC_INFO ATTR */
1058 DCC_INFO_RW_ATTR(max_small_discards, max_discards);
1059 DCC_INFO_GENERAL_RW_ATTR(max_discard_request);
1060 DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time);
1061 DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
1062 DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
1063 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
1064 DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
1065 DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
1066 DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
1067 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware);
1068 
1069 /* NM_INFO ATTR */
1070 NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
1071 NM_INFO_GENERAL_RW_ATTR(ram_thresh);
1072 NM_INFO_GENERAL_RW_ATTR(ra_nid_pages);
1073 NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio);
1074 
1075 /* F2FS_SBI ATTR */
1076 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
1077 F2FS_SBI_RW_ATTR(gc_idle, gc_mode);
1078 F2FS_SBI_RW_ATTR(gc_urgent, gc_mode);
1079 F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]);
1080 F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]);
1081 F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]);
1082 F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]);
1083 F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
1084 F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold);
1085 F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs);
1086 F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
1087 F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
1088 F2FS_SBI_GENERAL_RW_ATTR(migration_window_granularity);
1089 F2FS_SBI_GENERAL_RW_ATTR(dir_level);
1090 #ifdef CONFIG_F2FS_IOSTAT
1091 F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
1092 F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
1093 #endif
1094 F2FS_SBI_GENERAL_RW_ATTR(readdir_ra);
1095 F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes);
1096 F2FS_SBI_GENERAL_RW_ATTR(data_io_flag);
1097 F2FS_SBI_GENERAL_RW_ATTR(node_io_flag);
1098 F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials);
1099 F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul);
1100 F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode);
1101 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk);
1102 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole);
1103 #ifdef CONFIG_F2FS_FS_COMPRESSION
1104 F2FS_SBI_GENERAL_RW_ATTR(compr_written_block);
1105 F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block);
1106 F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode);
1107 F2FS_SBI_GENERAL_RW_ATTR(compress_percent);
1108 F2FS_SBI_GENERAL_RW_ATTR(compress_watermark);
1109 #endif
1110 /* atomic write */
1111 F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write);
1112 F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write);
1113 F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block);
1114 F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block);
1115 /* block age extent cache */
1116 F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold);
1117 F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold);
1118 F2FS_SBI_GENERAL_RW_ATTR(last_age_weight);
1119 /* read extent cache */
1120 F2FS_SBI_GENERAL_RW_ATTR(max_read_extent_count);
1121 #ifdef CONFIG_BLK_DEV_ZONED
1122 F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec);
1123 F2FS_SBI_GENERAL_RW_ATTR(blkzone_alloc_policy);
1124 #endif
1125 F2FS_SBI_GENERAL_RW_ATTR(carve_out);
1126 
1127 /* STAT_INFO ATTR */
1128 #ifdef CONFIG_F2FS_STAT_FS
1129 STAT_INFO_RO_ATTR(cp_foreground_calls, cp_call_count[FOREGROUND]);
1130 STAT_INFO_RO_ATTR(cp_background_calls, cp_call_count[BACKGROUND]);
1131 STAT_INFO_RO_ATTR(gc_foreground_calls, gc_call_count[FOREGROUND]);
1132 STAT_INFO_RO_ATTR(gc_background_calls, gc_call_count[BACKGROUND]);
1133 #endif
1134 
1135 /* FAULT_INFO ATTR */
1136 #ifdef CONFIG_F2FS_FAULT_INJECTION
1137 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate);
1138 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type);
1139 #endif
1140 
1141 /* RESERVED_BLOCKS ATTR */
1142 RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks);
1143 
1144 /* CPRC_INFO ATTR */
1145 CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio);
1146 
1147 /* ATGC_INFO ATTR */
1148 ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio);
1149 ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
1150 ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
1151 ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
1152 
1153 F2FS_GENERAL_RO_ATTR(dirty_segments);
1154 F2FS_GENERAL_RO_ATTR(free_segments);
1155 F2FS_GENERAL_RO_ATTR(ovp_segments);
1156 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
1157 F2FS_GENERAL_RO_ATTR(features);
1158 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
1159 F2FS_GENERAL_RO_ATTR(unusable);
1160 F2FS_GENERAL_RO_ATTR(encoding);
1161 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
1162 F2FS_GENERAL_RO_ATTR(main_blkaddr);
1163 F2FS_GENERAL_RO_ATTR(pending_discard);
1164 F2FS_GENERAL_RO_ATTR(atgc_enabled);
1165 F2FS_GENERAL_RO_ATTR(gc_mode);
1166 #ifdef CONFIG_F2FS_STAT_FS
1167 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
1168 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
1169 F2FS_GENERAL_RO_ATTR(avg_vblocks);
1170 #endif
1171 
1172 #ifdef CONFIG_FS_ENCRYPTION
1173 F2FS_FEATURE_RO_ATTR(encryption);
1174 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
1175 #if IS_ENABLED(CONFIG_UNICODE)
1176 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
1177 #endif
1178 #endif /* CONFIG_FS_ENCRYPTION */
1179 #ifdef CONFIG_BLK_DEV_ZONED
1180 F2FS_FEATURE_RO_ATTR(block_zoned);
1181 #endif
1182 F2FS_FEATURE_RO_ATTR(atomic_write);
1183 F2FS_FEATURE_RO_ATTR(extra_attr);
1184 F2FS_FEATURE_RO_ATTR(project_quota);
1185 F2FS_FEATURE_RO_ATTR(inode_checksum);
1186 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
1187 F2FS_FEATURE_RO_ATTR(quota_ino);
1188 F2FS_FEATURE_RO_ATTR(inode_crtime);
1189 F2FS_FEATURE_RO_ATTR(lost_found);
1190 #ifdef CONFIG_FS_VERITY
1191 F2FS_FEATURE_RO_ATTR(verity);
1192 #endif
1193 F2FS_FEATURE_RO_ATTR(sb_checksum);
1194 #if IS_ENABLED(CONFIG_UNICODE)
1195 F2FS_FEATURE_RO_ATTR(casefold);
1196 #endif
1197 F2FS_FEATURE_RO_ATTR(readonly);
1198 #ifdef CONFIG_F2FS_FS_COMPRESSION
1199 F2FS_FEATURE_RO_ATTR(compression);
1200 #endif
1201 F2FS_FEATURE_RO_ATTR(pin_file);
1202 
1203 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
1204 static struct attribute *f2fs_attrs[] = {
1205 	ATTR_LIST(gc_urgent_sleep_time),
1206 	ATTR_LIST(gc_min_sleep_time),
1207 	ATTR_LIST(gc_max_sleep_time),
1208 	ATTR_LIST(gc_no_gc_sleep_time),
1209 	ATTR_LIST(gc_no_zoned_gc_percent),
1210 	ATTR_LIST(gc_boost_zoned_gc_percent),
1211 	ATTR_LIST(gc_valid_thresh_ratio),
1212 	ATTR_LIST(gc_idle),
1213 	ATTR_LIST(gc_urgent),
1214 	ATTR_LIST(reclaim_segments),
1215 	ATTR_LIST(main_blkaddr),
1216 	ATTR_LIST(max_small_discards),
1217 	ATTR_LIST(max_discard_request),
1218 	ATTR_LIST(min_discard_issue_time),
1219 	ATTR_LIST(mid_discard_issue_time),
1220 	ATTR_LIST(max_discard_issue_time),
1221 	ATTR_LIST(discard_io_aware_gran),
1222 	ATTR_LIST(discard_urgent_util),
1223 	ATTR_LIST(discard_granularity),
1224 	ATTR_LIST(max_ordered_discard),
1225 	ATTR_LIST(discard_io_aware),
1226 	ATTR_LIST(pending_discard),
1227 	ATTR_LIST(gc_mode),
1228 	ATTR_LIST(ipu_policy),
1229 	ATTR_LIST(min_ipu_util),
1230 	ATTR_LIST(min_fsync_blocks),
1231 	ATTR_LIST(min_seq_blocks),
1232 	ATTR_LIST(min_hot_blocks),
1233 	ATTR_LIST(min_ssr_sections),
1234 	ATTR_LIST(reserved_segments),
1235 	ATTR_LIST(max_victim_search),
1236 	ATTR_LIST(migration_granularity),
1237 	ATTR_LIST(migration_window_granularity),
1238 	ATTR_LIST(dir_level),
1239 	ATTR_LIST(ram_thresh),
1240 	ATTR_LIST(ra_nid_pages),
1241 	ATTR_LIST(dirty_nats_ratio),
1242 	ATTR_LIST(max_roll_forward_node_blocks),
1243 	ATTR_LIST(cp_interval),
1244 	ATTR_LIST(idle_interval),
1245 	ATTR_LIST(discard_idle_interval),
1246 	ATTR_LIST(gc_idle_interval),
1247 	ATTR_LIST(umount_discard_timeout),
1248 #ifdef CONFIG_F2FS_IOSTAT
1249 	ATTR_LIST(iostat_enable),
1250 	ATTR_LIST(iostat_period_ms),
1251 #endif
1252 	ATTR_LIST(readdir_ra),
1253 	ATTR_LIST(max_io_bytes),
1254 	ATTR_LIST(gc_pin_file_thresh),
1255 	ATTR_LIST(extension_list),
1256 #ifdef CONFIG_F2FS_FAULT_INJECTION
1257 	ATTR_LIST(inject_rate),
1258 	ATTR_LIST(inject_type),
1259 #endif
1260 	ATTR_LIST(data_io_flag),
1261 	ATTR_LIST(node_io_flag),
1262 	ATTR_LIST(gc_remaining_trials),
1263 	ATTR_LIST(ckpt_thread_ioprio),
1264 	ATTR_LIST(dirty_segments),
1265 	ATTR_LIST(free_segments),
1266 	ATTR_LIST(ovp_segments),
1267 	ATTR_LIST(unusable),
1268 	ATTR_LIST(lifetime_write_kbytes),
1269 	ATTR_LIST(features),
1270 	ATTR_LIST(reserved_blocks),
1271 	ATTR_LIST(current_reserved_blocks),
1272 	ATTR_LIST(encoding),
1273 	ATTR_LIST(mounted_time_sec),
1274 #ifdef CONFIG_F2FS_STAT_FS
1275 	ATTR_LIST(cp_foreground_calls),
1276 	ATTR_LIST(cp_background_calls),
1277 	ATTR_LIST(gc_foreground_calls),
1278 	ATTR_LIST(gc_background_calls),
1279 	ATTR_LIST(moved_blocks_foreground),
1280 	ATTR_LIST(moved_blocks_background),
1281 	ATTR_LIST(avg_vblocks),
1282 #endif
1283 #ifdef CONFIG_BLK_DEV_ZONED
1284 	ATTR_LIST(unusable_blocks_per_sec),
1285 	ATTR_LIST(blkzone_alloc_policy),
1286 #endif
1287 #ifdef CONFIG_F2FS_FS_COMPRESSION
1288 	ATTR_LIST(compr_written_block),
1289 	ATTR_LIST(compr_saved_block),
1290 	ATTR_LIST(compr_new_inode),
1291 	ATTR_LIST(compress_percent),
1292 	ATTR_LIST(compress_watermark),
1293 #endif
1294 	/* For ATGC */
1295 	ATTR_LIST(atgc_candidate_ratio),
1296 	ATTR_LIST(atgc_candidate_count),
1297 	ATTR_LIST(atgc_age_weight),
1298 	ATTR_LIST(atgc_age_threshold),
1299 	ATTR_LIST(atgc_enabled),
1300 	ATTR_LIST(seq_file_ra_mul),
1301 	ATTR_LIST(gc_segment_mode),
1302 	ATTR_LIST(gc_reclaimed_segments),
1303 	ATTR_LIST(max_fragment_chunk),
1304 	ATTR_LIST(max_fragment_hole),
1305 	ATTR_LIST(current_atomic_write),
1306 	ATTR_LIST(peak_atomic_write),
1307 	ATTR_LIST(committed_atomic_block),
1308 	ATTR_LIST(revoked_atomic_block),
1309 	ATTR_LIST(hot_data_age_threshold),
1310 	ATTR_LIST(warm_data_age_threshold),
1311 	ATTR_LIST(last_age_weight),
1312 	ATTR_LIST(max_read_extent_count),
1313 	ATTR_LIST(carve_out),
1314 	NULL,
1315 };
1316 ATTRIBUTE_GROUPS(f2fs);
1317 
1318 #define BASE_ATTR_LIST(name) (&f2fs_base_attr_##name.attr)
1319 static struct attribute *f2fs_feat_attrs[] = {
1320 #ifdef CONFIG_FS_ENCRYPTION
1321 	BASE_ATTR_LIST(encryption),
1322 	BASE_ATTR_LIST(test_dummy_encryption_v2),
1323 #if IS_ENABLED(CONFIG_UNICODE)
1324 	BASE_ATTR_LIST(encrypted_casefold),
1325 #endif
1326 #endif /* CONFIG_FS_ENCRYPTION */
1327 #ifdef CONFIG_BLK_DEV_ZONED
1328 	BASE_ATTR_LIST(block_zoned),
1329 #endif
1330 	BASE_ATTR_LIST(atomic_write),
1331 	BASE_ATTR_LIST(extra_attr),
1332 	BASE_ATTR_LIST(project_quota),
1333 	BASE_ATTR_LIST(inode_checksum),
1334 	BASE_ATTR_LIST(flexible_inline_xattr),
1335 	BASE_ATTR_LIST(quota_ino),
1336 	BASE_ATTR_LIST(inode_crtime),
1337 	BASE_ATTR_LIST(lost_found),
1338 #ifdef CONFIG_FS_VERITY
1339 	BASE_ATTR_LIST(verity),
1340 #endif
1341 	BASE_ATTR_LIST(sb_checksum),
1342 #if IS_ENABLED(CONFIG_UNICODE)
1343 	BASE_ATTR_LIST(casefold),
1344 #endif
1345 	BASE_ATTR_LIST(readonly),
1346 #ifdef CONFIG_F2FS_FS_COMPRESSION
1347 	BASE_ATTR_LIST(compression),
1348 #endif
1349 	BASE_ATTR_LIST(pin_file),
1350 	NULL,
1351 };
1352 ATTRIBUTE_GROUPS(f2fs_feat);
1353 
1354 F2FS_GENERAL_RO_ATTR(sb_status);
1355 F2FS_GENERAL_RO_ATTR(cp_status);
1356 F2FS_GENERAL_RO_ATTR(issued_discard);
1357 F2FS_GENERAL_RO_ATTR(queued_discard);
1358 F2FS_GENERAL_RO_ATTR(undiscard_blks);
1359 
1360 static struct attribute *f2fs_stat_attrs[] = {
1361 	ATTR_LIST(sb_status),
1362 	ATTR_LIST(cp_status),
1363 	ATTR_LIST(issued_discard),
1364 	ATTR_LIST(queued_discard),
1365 	ATTR_LIST(undiscard_blks),
1366 	NULL,
1367 };
1368 ATTRIBUTE_GROUPS(f2fs_stat);
1369 
1370 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1371 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1372 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1373 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1374 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1375 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1376 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1377 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1378 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1379 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1380 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1381 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1382 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1383 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1384 F2FS_SB_FEATURE_RO_ATTR(device_alias, DEVICE_ALIAS);
1385 
1386 static struct attribute *f2fs_sb_feat_attrs[] = {
1387 	ATTR_LIST(sb_encryption),
1388 	ATTR_LIST(sb_block_zoned),
1389 	ATTR_LIST(sb_extra_attr),
1390 	ATTR_LIST(sb_project_quota),
1391 	ATTR_LIST(sb_inode_checksum),
1392 	ATTR_LIST(sb_flexible_inline_xattr),
1393 	ATTR_LIST(sb_quota_ino),
1394 	ATTR_LIST(sb_inode_crtime),
1395 	ATTR_LIST(sb_lost_found),
1396 	ATTR_LIST(sb_verity),
1397 	ATTR_LIST(sb_sb_checksum),
1398 	ATTR_LIST(sb_casefold),
1399 	ATTR_LIST(sb_compression),
1400 	ATTR_LIST(sb_readonly),
1401 	ATTR_LIST(sb_device_alias),
1402 	NULL,
1403 };
1404 ATTRIBUTE_GROUPS(f2fs_sb_feat);
1405 
1406 F2FS_TUNE_RW_ATTR(reclaim_caches_kb);
1407 
1408 static struct attribute *f2fs_tune_attrs[] = {
1409 	BASE_ATTR_LIST(reclaim_caches_kb),
1410 	NULL,
1411 };
1412 ATTRIBUTE_GROUPS(f2fs_tune);
1413 
1414 static const struct sysfs_ops f2fs_attr_ops = {
1415 	.show	= f2fs_attr_show,
1416 	.store	= f2fs_attr_store,
1417 };
1418 
1419 static const struct kobj_type f2fs_sb_ktype = {
1420 	.default_groups = f2fs_groups,
1421 	.sysfs_ops	= &f2fs_attr_ops,
1422 	.release	= f2fs_sb_release,
1423 };
1424 
1425 static const struct kobj_type f2fs_ktype = {
1426 	.sysfs_ops	= &f2fs_attr_ops,
1427 };
1428 
1429 static struct kset f2fs_kset = {
1430 	.kobj	= {.ktype = &f2fs_ktype},
1431 };
1432 
1433 static const struct sysfs_ops f2fs_feat_attr_ops = {
1434 	.show	= f2fs_base_attr_show,
1435 	.store	= f2fs_base_attr_store,
1436 };
1437 
1438 static const struct kobj_type f2fs_feat_ktype = {
1439 	.default_groups = f2fs_feat_groups,
1440 	.sysfs_ops	= &f2fs_feat_attr_ops,
1441 };
1442 
1443 static struct kobject f2fs_feat = {
1444 	.kset	= &f2fs_kset,
1445 };
1446 
1447 static const struct sysfs_ops f2fs_tune_attr_ops = {
1448 	.show	= f2fs_base_attr_show,
1449 	.store	= f2fs_base_attr_store,
1450 };
1451 
1452 static const struct kobj_type f2fs_tune_ktype = {
1453 	.default_groups = f2fs_tune_groups,
1454 	.sysfs_ops	= &f2fs_tune_attr_ops,
1455 };
1456 
1457 static struct kobject f2fs_tune = {
1458 	.kset	= &f2fs_kset,
1459 };
1460 
f2fs_stat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1461 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1462 				struct attribute *attr, char *buf)
1463 {
1464 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1465 								s_stat_kobj);
1466 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1467 
1468 	return a->show ? a->show(a, sbi, buf) : 0;
1469 }
1470 
f2fs_stat_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)1471 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1472 						const char *buf, size_t len)
1473 {
1474 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1475 								s_stat_kobj);
1476 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1477 
1478 	return a->store ? a->store(a, sbi, buf, len) : 0;
1479 }
1480 
f2fs_stat_kobj_release(struct kobject * kobj)1481 static void f2fs_stat_kobj_release(struct kobject *kobj)
1482 {
1483 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1484 								s_stat_kobj);
1485 	complete(&sbi->s_stat_kobj_unregister);
1486 }
1487 
1488 static const struct sysfs_ops f2fs_stat_attr_ops = {
1489 	.show	= f2fs_stat_attr_show,
1490 	.store	= f2fs_stat_attr_store,
1491 };
1492 
1493 static const struct kobj_type f2fs_stat_ktype = {
1494 	.default_groups = f2fs_stat_groups,
1495 	.sysfs_ops	= &f2fs_stat_attr_ops,
1496 	.release	= f2fs_stat_kobj_release,
1497 };
1498 
f2fs_sb_feat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1499 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1500 				struct attribute *attr, char *buf)
1501 {
1502 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1503 							s_feature_list_kobj);
1504 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1505 
1506 	return a->show ? a->show(a, sbi, buf) : 0;
1507 }
1508 
f2fs_feature_list_kobj_release(struct kobject * kobj)1509 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1510 {
1511 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1512 							s_feature_list_kobj);
1513 	complete(&sbi->s_feature_list_kobj_unregister);
1514 }
1515 
1516 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1517 	.show	= f2fs_sb_feat_attr_show,
1518 };
1519 
1520 static const struct kobj_type f2fs_feature_list_ktype = {
1521 	.default_groups = f2fs_sb_feat_groups,
1522 	.sysfs_ops	= &f2fs_feature_list_attr_ops,
1523 	.release	= f2fs_feature_list_kobj_release,
1524 };
1525 
segment_info_seq_show(struct seq_file * seq,void * offset)1526 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1527 						void *offset)
1528 {
1529 	struct super_block *sb = seq->private;
1530 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1531 	unsigned int total_segs =
1532 			le32_to_cpu(sbi->raw_super->segment_count_main);
1533 	int i;
1534 
1535 	seq_puts(seq, "format: segment_type|valid_blocks\n"
1536 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1537 
1538 	for (i = 0; i < total_segs; i++) {
1539 		struct seg_entry *se = get_seg_entry(sbi, i);
1540 
1541 		if ((i % 10) == 0)
1542 			seq_printf(seq, "%-10d", i);
1543 		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1544 		if ((i % 10) == 9 || i == (total_segs - 1))
1545 			seq_putc(seq, '\n');
1546 		else
1547 			seq_putc(seq, ' ');
1548 	}
1549 
1550 	return 0;
1551 }
1552 
segment_bits_seq_show(struct seq_file * seq,void * offset)1553 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1554 						void *offset)
1555 {
1556 	struct super_block *sb = seq->private;
1557 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1558 	unsigned int total_segs =
1559 			le32_to_cpu(sbi->raw_super->segment_count_main);
1560 	int i, j;
1561 
1562 	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps|mtime\n"
1563 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1564 
1565 	for (i = 0; i < total_segs; i++) {
1566 		struct seg_entry *se = get_seg_entry(sbi, i);
1567 
1568 		seq_printf(seq, "%-10d", i);
1569 		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1570 		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1571 			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1572 		seq_printf(seq, "| %llx", se->mtime);
1573 		seq_putc(seq, '\n');
1574 	}
1575 	return 0;
1576 }
1577 
victim_bits_seq_show(struct seq_file * seq,void * offset)1578 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1579 						void *offset)
1580 {
1581 	struct super_block *sb = seq->private;
1582 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1583 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1584 	int i;
1585 
1586 	seq_puts(seq, "format: victim_secmap bitmaps\n");
1587 
1588 	for (i = 0; i < MAIN_SECS(sbi); i++) {
1589 		if ((i % 10) == 0)
1590 			seq_printf(seq, "%-10d", i);
1591 		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1592 		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1593 			seq_putc(seq, '\n');
1594 		else
1595 			seq_putc(seq, ' ');
1596 	}
1597 	return 0;
1598 }
1599 
discard_plist_seq_show(struct seq_file * seq,void * offset)1600 static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
1601 						void *offset)
1602 {
1603 	struct super_block *sb = seq->private;
1604 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1605 	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1606 	int i, count;
1607 
1608 	seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
1609 	if (!f2fs_realtime_discard_enable(sbi))
1610 		return 0;
1611 
1612 	if (dcc) {
1613 		mutex_lock(&dcc->cmd_lock);
1614 		for (i = 0; i < MAX_PLIST_NUM; i++) {
1615 			struct list_head *pend_list;
1616 			struct discard_cmd *dc, *tmp;
1617 
1618 			if (i % 8 == 0)
1619 				seq_printf(seq, "  %-3d", i);
1620 			count = 0;
1621 			pend_list = &dcc->pend_list[i];
1622 			list_for_each_entry_safe(dc, tmp, pend_list, list)
1623 				count++;
1624 			if (count)
1625 				seq_printf(seq, " %7d", count);
1626 			else
1627 				seq_puts(seq, "       .");
1628 			if (i % 8 == 7)
1629 				seq_putc(seq, '\n');
1630 		}
1631 		seq_putc(seq, '\n');
1632 		mutex_unlock(&dcc->cmd_lock);
1633 	}
1634 
1635 	return 0;
1636 }
1637 
disk_map_seq_show(struct seq_file * seq,void * offset)1638 static int __maybe_unused disk_map_seq_show(struct seq_file *seq,
1639 						void *offset)
1640 {
1641 	struct super_block *sb = seq->private;
1642 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1643 	int i;
1644 
1645 	seq_printf(seq, "Address Layout   : %5luB Block address (# of Segments)\n",
1646 					F2FS_BLKSIZE);
1647 	seq_printf(seq, " SB            : %12s\n", "0/1024B");
1648 	seq_printf(seq, " seg0_blkaddr  : 0x%010x\n", SEG0_BLKADDR(sbi));
1649 	seq_printf(seq, " Checkpoint    : 0x%010x (%10d)\n",
1650 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2);
1651 	seq_printf(seq, " SIT           : 0x%010x (%10d)\n",
1652 			SIT_I(sbi)->sit_base_addr,
1653 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit));
1654 	seq_printf(seq, " NAT           : 0x%010x (%10d)\n",
1655 			NM_I(sbi)->nat_blkaddr,
1656 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat));
1657 	seq_printf(seq, " SSA           : 0x%010x (%10d)\n",
1658 			SM_I(sbi)->ssa_blkaddr,
1659 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
1660 	seq_printf(seq, " Main          : 0x%010x (%10d)\n",
1661 			SM_I(sbi)->main_blkaddr,
1662 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main));
1663 	seq_printf(seq, " # of Sections : %12d\n",
1664 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count));
1665 	seq_printf(seq, " Segs/Sections : %12d\n",
1666 			SEGS_PER_SEC(sbi));
1667 	seq_printf(seq, " Section size  : %12d MB\n",
1668 			SEGS_PER_SEC(sbi) << 1);
1669 
1670 	if (!f2fs_is_multi_device(sbi))
1671 		return 0;
1672 
1673 	seq_puts(seq, "\nDisk Map for multi devices:\n");
1674 	for (i = 0; i < sbi->s_ndevs; i++)
1675 		seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n",
1676 			i, bdev_is_zoned(FDEV(i).bdev),
1677 			FDEV(i).start_blk, FDEV(i).end_blk,
1678 			FDEV(i).path);
1679 	return 0;
1680 }
1681 
f2fs_init_sysfs(void)1682 int __init f2fs_init_sysfs(void)
1683 {
1684 	int ret;
1685 
1686 	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1687 	f2fs_kset.kobj.parent = fs_kobj;
1688 	ret = kset_register(&f2fs_kset);
1689 	if (ret)
1690 		return ret;
1691 
1692 	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1693 				   NULL, "features");
1694 	if (ret)
1695 		goto put_kobject;
1696 
1697 	ret = kobject_init_and_add(&f2fs_tune, &f2fs_tune_ktype,
1698 				   NULL, "tuning");
1699 	if (ret)
1700 		goto put_kobject;
1701 
1702 	f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1703 	if (!f2fs_proc_root) {
1704 		ret = -ENOMEM;
1705 		goto put_kobject;
1706 	}
1707 
1708 	return 0;
1709 
1710 put_kobject:
1711 	kobject_put(&f2fs_tune);
1712 	kobject_put(&f2fs_feat);
1713 	kset_unregister(&f2fs_kset);
1714 	return ret;
1715 }
1716 
f2fs_exit_sysfs(void)1717 void f2fs_exit_sysfs(void)
1718 {
1719 	kobject_put(&f2fs_tune);
1720 	kobject_put(&f2fs_feat);
1721 	kset_unregister(&f2fs_kset);
1722 	remove_proc_entry("fs/f2fs", NULL);
1723 	f2fs_proc_root = NULL;
1724 }
1725 
f2fs_register_sysfs(struct f2fs_sb_info * sbi)1726 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1727 {
1728 	struct super_block *sb = sbi->sb;
1729 	int err;
1730 
1731 	sbi->s_kobj.kset = &f2fs_kset;
1732 	init_completion(&sbi->s_kobj_unregister);
1733 	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1734 				"%s", sb->s_id);
1735 	if (err)
1736 		goto put_sb_kobj;
1737 
1738 	sbi->s_stat_kobj.kset = &f2fs_kset;
1739 	init_completion(&sbi->s_stat_kobj_unregister);
1740 	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1741 						&sbi->s_kobj, "stat");
1742 	if (err)
1743 		goto put_stat_kobj;
1744 
1745 	sbi->s_feature_list_kobj.kset = &f2fs_kset;
1746 	init_completion(&sbi->s_feature_list_kobj_unregister);
1747 	err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1748 					&f2fs_feature_list_ktype,
1749 					&sbi->s_kobj, "feature_list");
1750 	if (err)
1751 		goto put_feature_list_kobj;
1752 
1753 	sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1754 	if (!sbi->s_proc) {
1755 		err = -ENOMEM;
1756 		goto put_feature_list_kobj;
1757 	}
1758 
1759 	proc_create_single_data("segment_info", 0444, sbi->s_proc,
1760 				segment_info_seq_show, sb);
1761 	proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1762 				segment_bits_seq_show, sb);
1763 #ifdef CONFIG_F2FS_IOSTAT
1764 	proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1765 				iostat_info_seq_show, sb);
1766 #endif
1767 	proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1768 				victim_bits_seq_show, sb);
1769 	proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
1770 				discard_plist_seq_show, sb);
1771 	proc_create_single_data("disk_map", 0444, sbi->s_proc,
1772 				disk_map_seq_show, sb);
1773 	return 0;
1774 put_feature_list_kobj:
1775 	kobject_put(&sbi->s_feature_list_kobj);
1776 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1777 put_stat_kobj:
1778 	kobject_put(&sbi->s_stat_kobj);
1779 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1780 put_sb_kobj:
1781 	kobject_put(&sbi->s_kobj);
1782 	wait_for_completion(&sbi->s_kobj_unregister);
1783 	return err;
1784 }
1785 
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)1786 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1787 {
1788 	remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
1789 
1790 	kobject_put(&sbi->s_stat_kobj);
1791 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1792 	kobject_put(&sbi->s_feature_list_kobj);
1793 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1794 
1795 	kobject_put(&sbi->s_kobj);
1796 	wait_for_completion(&sbi->s_kobj_unregister);
1797 }
1798