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