xref: /linux/fs/f2fs/sysfs.c (revision d8441523f21375b11a4593a2d89942b407bcb44f)
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 #ifdef CONFIG_F2FS_IOSTAT
632 	if (!strcmp(a->attr.name, "iostat_enable")) {
633 		sbi->iostat_enable = !!t;
634 		if (!sbi->iostat_enable)
635 			f2fs_reset_iostat(sbi);
636 		return count;
637 	}
638 
639 	if (!strcmp(a->attr.name, "iostat_period_ms")) {
640 		if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
641 			return -EINVAL;
642 		spin_lock_irq(&sbi->iostat_lock);
643 		sbi->iostat_period_ms = (unsigned int)t;
644 		spin_unlock_irq(&sbi->iostat_lock);
645 		return count;
646 	}
647 #endif
648 
649 #ifdef CONFIG_BLK_DEV_ZONED
650 	if (!strcmp(a->attr.name, "blkzone_alloc_policy")) {
651 		if (t < BLKZONE_ALLOC_PRIOR_SEQ || t > BLKZONE_ALLOC_PRIOR_CONV)
652 			return -EINVAL;
653 		sbi->blkzone_alloc_policy = t;
654 		return count;
655 	}
656 #endif
657 
658 #ifdef CONFIG_F2FS_FS_COMPRESSION
659 	if (!strcmp(a->attr.name, "compr_written_block") ||
660 		!strcmp(a->attr.name, "compr_saved_block")) {
661 		if (t != 0)
662 			return -EINVAL;
663 		sbi->compr_written_block = 0;
664 		sbi->compr_saved_block = 0;
665 		return count;
666 	}
667 
668 	if (!strcmp(a->attr.name, "compr_new_inode")) {
669 		if (t != 0)
670 			return -EINVAL;
671 		sbi->compr_new_inode = 0;
672 		return count;
673 	}
674 
675 	if (!strcmp(a->attr.name, "compress_percent")) {
676 		if (t == 0 || t > 100)
677 			return -EINVAL;
678 		*ui = t;
679 		return count;
680 	}
681 
682 	if (!strcmp(a->attr.name, "compress_watermark")) {
683 		if (t == 0 || t > 100)
684 			return -EINVAL;
685 		*ui = t;
686 		return count;
687 	}
688 #endif
689 
690 	if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
691 		if (t > 100)
692 			return -EINVAL;
693 		sbi->am.candidate_ratio = t;
694 		return count;
695 	}
696 
697 	if (!strcmp(a->attr.name, "atgc_age_weight")) {
698 		if (t > 100)
699 			return -EINVAL;
700 		sbi->am.age_weight = t;
701 		return count;
702 	}
703 
704 	if (!strcmp(a->attr.name, "gc_segment_mode")) {
705 		if (t < MAX_GC_MODE)
706 			sbi->gc_segment_mode = t;
707 		else
708 			return -EINVAL;
709 		return count;
710 	}
711 
712 	if (!strcmp(a->attr.name, "gc_pin_file_threshold")) {
713 		if (t > MAX_GC_FAILED_PINNED_FILES)
714 			return -EINVAL;
715 		sbi->gc_pin_file_threshold = t;
716 		return count;
717 	}
718 
719 	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
720 		if (t != 0)
721 			return -EINVAL;
722 		sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
723 		return count;
724 	}
725 
726 	if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
727 		if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
728 			sbi->seq_file_ra_mul = t;
729 		else
730 			return -EINVAL;
731 		return count;
732 	}
733 
734 	if (!strcmp(a->attr.name, "max_fragment_chunk")) {
735 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
736 			sbi->max_fragment_chunk = t;
737 		else
738 			return -EINVAL;
739 		return count;
740 	}
741 
742 	if (!strcmp(a->attr.name, "max_fragment_hole")) {
743 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
744 			sbi->max_fragment_hole = t;
745 		else
746 			return -EINVAL;
747 		return count;
748 	}
749 
750 	if (!strcmp(a->attr.name, "peak_atomic_write")) {
751 		if (t != 0)
752 			return -EINVAL;
753 		sbi->peak_atomic_write = 0;
754 		return count;
755 	}
756 
757 	if (!strcmp(a->attr.name, "committed_atomic_block")) {
758 		if (t != 0)
759 			return -EINVAL;
760 		sbi->committed_atomic_block = 0;
761 		return count;
762 	}
763 
764 	if (!strcmp(a->attr.name, "revoked_atomic_block")) {
765 		if (t != 0)
766 			return -EINVAL;
767 		sbi->revoked_atomic_block = 0;
768 		return count;
769 	}
770 
771 	if (!strcmp(a->attr.name, "readdir_ra")) {
772 		sbi->readdir_ra = !!t;
773 		return count;
774 	}
775 
776 	if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
777 		if (t == 0 || t >= sbi->warm_data_age_threshold)
778 			return -EINVAL;
779 		if (t == *ui)
780 			return count;
781 		*ui = (unsigned int)t;
782 		return count;
783 	}
784 
785 	if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
786 		if (t <= sbi->hot_data_age_threshold)
787 			return -EINVAL;
788 		if (t == *ui)
789 			return count;
790 		*ui = (unsigned int)t;
791 		return count;
792 	}
793 
794 	if (!strcmp(a->attr.name, "last_age_weight")) {
795 		if (t > 100)
796 			return -EINVAL;
797 		if (t == *ui)
798 			return count;
799 		*ui = (unsigned int)t;
800 		return count;
801 	}
802 
803 	if (!strcmp(a->attr.name, "max_read_extent_count")) {
804 		if (t > UINT_MAX)
805 			return -EINVAL;
806 		*ui = (unsigned int)t;
807 		return count;
808 	}
809 
810 	if (!strcmp(a->attr.name, "ipu_policy")) {
811 		if (t >= BIT(F2FS_IPU_MAX))
812 			return -EINVAL;
813 		/* allow F2FS_IPU_NOCACHE only for IPU in the pinned file */
814 		if (f2fs_lfs_mode(sbi) && (t & ~BIT(F2FS_IPU_NOCACHE)))
815 			return -EINVAL;
816 		SM_I(sbi)->ipu_policy = (unsigned int)t;
817 		return count;
818 	}
819 
820 	if (!strcmp(a->attr.name, "dir_level")) {
821 		if (t > MAX_DIR_HASH_DEPTH)
822 			return -EINVAL;
823 		sbi->dir_level = t;
824 		return count;
825 	}
826 
827 	*ui = (unsigned int)t;
828 
829 	return count;
830 }
831 
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)832 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
833 			struct f2fs_sb_info *sbi,
834 			const char *buf, size_t count)
835 {
836 	ssize_t ret;
837 	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
838 					a->struct_type == GC_THREAD);
839 
840 	if (gc_entry) {
841 		if (!down_read_trylock(&sbi->sb->s_umount))
842 			return -EAGAIN;
843 	}
844 	ret = __sbi_store(a, sbi, buf, count);
845 	if (gc_entry)
846 		up_read(&sbi->sb->s_umount);
847 
848 	return ret;
849 }
850 
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)851 static ssize_t f2fs_attr_show(struct kobject *kobj,
852 				struct attribute *attr, char *buf)
853 {
854 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
855 								s_kobj);
856 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
857 
858 	return a->show ? a->show(a, sbi, buf) : 0;
859 }
860 
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)861 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
862 						const char *buf, size_t len)
863 {
864 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
865 									s_kobj);
866 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
867 
868 	return a->store ? a->store(a, sbi, buf, len) : 0;
869 }
870 
f2fs_sb_release(struct kobject * kobj)871 static void f2fs_sb_release(struct kobject *kobj)
872 {
873 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
874 								s_kobj);
875 	complete(&sbi->s_kobj_unregister);
876 }
877 
f2fs_base_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)878 static ssize_t f2fs_base_attr_show(struct kobject *kobj,
879 				struct attribute *attr, char *buf)
880 {
881 	struct f2fs_base_attr *a = container_of(attr,
882 				struct f2fs_base_attr, attr);
883 
884 	return a->show ? a->show(a, buf) : 0;
885 }
886 
f2fs_base_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)887 static ssize_t f2fs_base_attr_store(struct kobject *kobj,
888 				struct attribute *attr,
889 				const char *buf, size_t len)
890 {
891 	struct f2fs_base_attr *a = container_of(attr,
892 				struct f2fs_base_attr, attr);
893 
894 	return a->store ? a->store(a, buf, len) : 0;
895 }
896 
897 /*
898  * Note that there are three feature list entries:
899  * 1) /sys/fs/f2fs/features
900  *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
901  *     - ref. F2FS_FEATURE_RO_ATTR()
902  *
903  * 2) /sys/fs/f2fs/$s_id/features <deprecated>
904  *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
905  *     won't add new feature anymore, and thus, users should check entries in 3)
906  *     instead of this 2).
907  *
908  * 3) /sys/fs/f2fs/$s_id/feature_list
909  *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
910  *     sysfs entry rule where each entry should expose single value.
911  *     This list covers old feature list provided by 2) and beyond. Therefore,
912  *     please add new on-disk feature in this list only.
913  *     - ref. F2FS_SB_FEATURE_RO_ATTR()
914  */
f2fs_feature_show(struct f2fs_base_attr * a,char * buf)915 static ssize_t f2fs_feature_show(struct f2fs_base_attr *a, char *buf)
916 {
917 	return sysfs_emit(buf, "supported\n");
918 }
919 
920 #define F2FS_FEATURE_RO_ATTR(_name)				\
921 static struct f2fs_base_attr f2fs_base_attr_##_name = {		\
922 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
923 	.show	= f2fs_feature_show,				\
924 }
925 
f2fs_tune_show(struct f2fs_base_attr * a,char * buf)926 static ssize_t f2fs_tune_show(struct f2fs_base_attr *a, char *buf)
927 {
928 	unsigned int res = 0;
929 
930 	if (!strcmp(a->attr.name, "reclaim_caches_kb"))
931 		res = f2fs_donate_files();
932 
933 	return sysfs_emit(buf, "%u\n", res);
934 }
935 
f2fs_tune_store(struct f2fs_base_attr * a,const char * buf,size_t count)936 static ssize_t f2fs_tune_store(struct f2fs_base_attr *a,
937 			const char *buf, size_t count)
938 {
939 	unsigned long t;
940 	int ret;
941 
942 	ret = kstrtoul(skip_spaces(buf), 0, &t);
943 	if (ret)
944 		return ret;
945 
946 	if (!strcmp(a->attr.name, "reclaim_caches_kb"))
947 		f2fs_reclaim_caches(t);
948 
949 	return count;
950 }
951 
952 #define F2FS_TUNE_RW_ATTR(_name)				\
953 static struct f2fs_base_attr f2fs_base_attr_##_name = {		\
954 	.attr = {.name = __stringify(_name), .mode = 0644 },	\
955 	.show	= f2fs_tune_show,				\
956 	.store	= f2fs_tune_store,				\
957 }
958 
f2fs_sb_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)959 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
960 		struct f2fs_sb_info *sbi, char *buf)
961 {
962 	if (F2FS_HAS_FEATURE(sbi, a->id))
963 		return sysfs_emit(buf, "supported\n");
964 	return sysfs_emit(buf, "unsupported\n");
965 }
966 
967 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
968 static struct f2fs_attr f2fs_attr_sb_##_name = {		\
969 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
970 	.show	= f2fs_sb_feature_show,				\
971 	.id	= F2FS_FEATURE_##_feat,				\
972 }
973 
974 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
975 static struct f2fs_attr f2fs_attr_##_name = {			\
976 	.attr = {.name = __stringify(_name), .mode = _mode },	\
977 	.show	= _show,					\
978 	.store	= _store,					\
979 	.struct_type = _struct_type,				\
980 	.offset = _offset					\
981 }
982 
983 #define F2FS_RO_ATTR(struct_type, struct_name, name, elname)	\
984 	F2FS_ATTR_OFFSET(struct_type, name, 0444,		\
985 		f2fs_sbi_show, NULL,				\
986 		offsetof(struct struct_name, elname))
987 
988 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
989 	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
990 		f2fs_sbi_show, f2fs_sbi_store,			\
991 		offsetof(struct struct_name, elname))
992 
993 #define F2FS_GENERAL_RO_ATTR(name) \
994 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
995 
996 #ifdef CONFIG_F2FS_STAT_FS
997 #define STAT_INFO_RO_ATTR(name, elname)				\
998 	F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname)
999 #endif
1000 
1001 #define GC_THREAD_RW_ATTR(name, elname)				\
1002 	F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname)
1003 
1004 #define SM_INFO_RW_ATTR(name, elname)				\
1005 	F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname)
1006 
1007 #define SM_INFO_GENERAL_RW_ATTR(elname)				\
1008 	SM_INFO_RW_ATTR(elname, elname)
1009 
1010 #define DCC_INFO_RW_ATTR(name, elname)				\
1011 	F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname)
1012 
1013 #define DCC_INFO_GENERAL_RW_ATTR(elname)			\
1014 	DCC_INFO_RW_ATTR(elname, elname)
1015 
1016 #define NM_INFO_RW_ATTR(name, elname)				\
1017 	F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname)
1018 
1019 #define NM_INFO_GENERAL_RW_ATTR(elname)				\
1020 	NM_INFO_RW_ATTR(elname, elname)
1021 
1022 #define F2FS_SBI_RW_ATTR(name, elname)				\
1023 	F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname)
1024 
1025 #define F2FS_SBI_GENERAL_RW_ATTR(elname)			\
1026 	F2FS_SBI_RW_ATTR(elname, elname)
1027 
1028 #define F2FS_SBI_GENERAL_RO_ATTR(elname)			\
1029 	F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname)
1030 
1031 #ifdef CONFIG_F2FS_FAULT_INJECTION
1032 #define FAULT_INFO_GENERAL_RW_ATTR(type, elname)		\
1033 	F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname)
1034 #endif
1035 
1036 #define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname)			\
1037 	F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname)
1038 
1039 #define CPRC_INFO_GENERAL_RW_ATTR(elname)			\
1040 	F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname)
1041 
1042 #define ATGC_INFO_RW_ATTR(name, elname)				\
1043 	F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
1044 
1045 /* GC_THREAD ATTR */
1046 GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
1047 GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
1048 GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time);
1049 GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
1050 GC_THREAD_RW_ATTR(gc_no_zoned_gc_percent, no_zoned_gc_percent);
1051 GC_THREAD_RW_ATTR(gc_boost_zoned_gc_percent, boost_zoned_gc_percent);
1052 GC_THREAD_RW_ATTR(gc_valid_thresh_ratio, valid_thresh_ratio);
1053 
1054 /* SM_INFO ATTR */
1055 SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments);
1056 SM_INFO_GENERAL_RW_ATTR(ipu_policy);
1057 SM_INFO_GENERAL_RW_ATTR(min_ipu_util);
1058 SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks);
1059 SM_INFO_GENERAL_RW_ATTR(min_seq_blocks);
1060 SM_INFO_GENERAL_RW_ATTR(min_hot_blocks);
1061 SM_INFO_GENERAL_RW_ATTR(min_ssr_sections);
1062 SM_INFO_GENERAL_RW_ATTR(reserved_segments);
1063 
1064 /* DCC_INFO ATTR */
1065 DCC_INFO_RW_ATTR(max_small_discards, max_discards);
1066 DCC_INFO_GENERAL_RW_ATTR(max_discard_request);
1067 DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time);
1068 DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
1069 DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
1070 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
1071 DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
1072 DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
1073 DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
1074 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware);
1075 
1076 /* NM_INFO ATTR */
1077 NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
1078 NM_INFO_GENERAL_RW_ATTR(ram_thresh);
1079 NM_INFO_GENERAL_RW_ATTR(ra_nid_pages);
1080 NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio);
1081 
1082 /* F2FS_SBI ATTR */
1083 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
1084 F2FS_SBI_RW_ATTR(gc_idle, gc_mode);
1085 F2FS_SBI_RW_ATTR(gc_urgent, gc_mode);
1086 F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]);
1087 F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]);
1088 F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]);
1089 F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]);
1090 F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
1091 F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold);
1092 F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs);
1093 F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
1094 F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
1095 F2FS_SBI_GENERAL_RW_ATTR(migration_window_granularity);
1096 F2FS_SBI_GENERAL_RW_ATTR(dir_level);
1097 #ifdef CONFIG_F2FS_IOSTAT
1098 F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
1099 F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
1100 #endif
1101 F2FS_SBI_GENERAL_RW_ATTR(readdir_ra);
1102 F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes);
1103 F2FS_SBI_GENERAL_RW_ATTR(data_io_flag);
1104 F2FS_SBI_GENERAL_RW_ATTR(node_io_flag);
1105 F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials);
1106 F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul);
1107 F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode);
1108 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk);
1109 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole);
1110 #ifdef CONFIG_F2FS_FS_COMPRESSION
1111 F2FS_SBI_GENERAL_RW_ATTR(compr_written_block);
1112 F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block);
1113 F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode);
1114 F2FS_SBI_GENERAL_RW_ATTR(compress_percent);
1115 F2FS_SBI_GENERAL_RW_ATTR(compress_watermark);
1116 #endif
1117 /* atomic write */
1118 F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write);
1119 F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write);
1120 F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block);
1121 F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block);
1122 /* block age extent cache */
1123 F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold);
1124 F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold);
1125 F2FS_SBI_GENERAL_RW_ATTR(last_age_weight);
1126 /* read extent cache */
1127 F2FS_SBI_GENERAL_RW_ATTR(max_read_extent_count);
1128 #ifdef CONFIG_BLK_DEV_ZONED
1129 F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec);
1130 F2FS_SBI_GENERAL_RW_ATTR(blkzone_alloc_policy);
1131 #endif
1132 F2FS_SBI_GENERAL_RW_ATTR(carve_out);
1133 
1134 /* STAT_INFO ATTR */
1135 #ifdef CONFIG_F2FS_STAT_FS
1136 STAT_INFO_RO_ATTR(cp_foreground_calls, cp_call_count[FOREGROUND]);
1137 STAT_INFO_RO_ATTR(cp_background_calls, cp_call_count[BACKGROUND]);
1138 STAT_INFO_RO_ATTR(gc_foreground_calls, gc_call_count[FOREGROUND]);
1139 STAT_INFO_RO_ATTR(gc_background_calls, gc_call_count[BACKGROUND]);
1140 #endif
1141 
1142 /* FAULT_INFO ATTR */
1143 #ifdef CONFIG_F2FS_FAULT_INJECTION
1144 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate);
1145 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type);
1146 #endif
1147 
1148 /* RESERVED_BLOCKS ATTR */
1149 RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks);
1150 
1151 /* CPRC_INFO ATTR */
1152 CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio);
1153 
1154 /* ATGC_INFO ATTR */
1155 ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio);
1156 ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
1157 ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
1158 ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
1159 
1160 F2FS_GENERAL_RO_ATTR(dirty_segments);
1161 F2FS_GENERAL_RO_ATTR(free_segments);
1162 F2FS_GENERAL_RO_ATTR(ovp_segments);
1163 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
1164 F2FS_GENERAL_RO_ATTR(features);
1165 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
1166 F2FS_GENERAL_RO_ATTR(unusable);
1167 F2FS_GENERAL_RO_ATTR(encoding);
1168 F2FS_GENERAL_RO_ATTR(encoding_flags);
1169 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
1170 F2FS_GENERAL_RO_ATTR(main_blkaddr);
1171 F2FS_GENERAL_RO_ATTR(pending_discard);
1172 F2FS_GENERAL_RO_ATTR(atgc_enabled);
1173 F2FS_GENERAL_RO_ATTR(gc_mode);
1174 #ifdef CONFIG_F2FS_STAT_FS
1175 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
1176 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
1177 F2FS_GENERAL_RO_ATTR(avg_vblocks);
1178 #endif
1179 
1180 #ifdef CONFIG_FS_ENCRYPTION
1181 F2FS_FEATURE_RO_ATTR(encryption);
1182 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
1183 #if IS_ENABLED(CONFIG_UNICODE)
1184 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
1185 #endif
1186 #endif /* CONFIG_FS_ENCRYPTION */
1187 #ifdef CONFIG_BLK_DEV_ZONED
1188 F2FS_FEATURE_RO_ATTR(block_zoned);
1189 #endif
1190 F2FS_FEATURE_RO_ATTR(atomic_write);
1191 F2FS_FEATURE_RO_ATTR(extra_attr);
1192 F2FS_FEATURE_RO_ATTR(project_quota);
1193 F2FS_FEATURE_RO_ATTR(inode_checksum);
1194 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
1195 F2FS_FEATURE_RO_ATTR(quota_ino);
1196 F2FS_FEATURE_RO_ATTR(inode_crtime);
1197 F2FS_FEATURE_RO_ATTR(lost_found);
1198 #ifdef CONFIG_FS_VERITY
1199 F2FS_FEATURE_RO_ATTR(verity);
1200 #endif
1201 F2FS_FEATURE_RO_ATTR(sb_checksum);
1202 #if IS_ENABLED(CONFIG_UNICODE)
1203 F2FS_FEATURE_RO_ATTR(casefold);
1204 #endif
1205 F2FS_FEATURE_RO_ATTR(readonly);
1206 #ifdef CONFIG_F2FS_FS_COMPRESSION
1207 F2FS_FEATURE_RO_ATTR(compression);
1208 #endif
1209 F2FS_FEATURE_RO_ATTR(pin_file);
1210 #ifdef CONFIG_UNICODE
1211 F2FS_FEATURE_RO_ATTR(linear_lookup);
1212 #endif
1213 
1214 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
1215 static struct attribute *f2fs_attrs[] = {
1216 	ATTR_LIST(gc_urgent_sleep_time),
1217 	ATTR_LIST(gc_min_sleep_time),
1218 	ATTR_LIST(gc_max_sleep_time),
1219 	ATTR_LIST(gc_no_gc_sleep_time),
1220 	ATTR_LIST(gc_no_zoned_gc_percent),
1221 	ATTR_LIST(gc_boost_zoned_gc_percent),
1222 	ATTR_LIST(gc_valid_thresh_ratio),
1223 	ATTR_LIST(gc_idle),
1224 	ATTR_LIST(gc_urgent),
1225 	ATTR_LIST(reclaim_segments),
1226 	ATTR_LIST(main_blkaddr),
1227 	ATTR_LIST(max_small_discards),
1228 	ATTR_LIST(max_discard_request),
1229 	ATTR_LIST(min_discard_issue_time),
1230 	ATTR_LIST(mid_discard_issue_time),
1231 	ATTR_LIST(max_discard_issue_time),
1232 	ATTR_LIST(discard_io_aware_gran),
1233 	ATTR_LIST(discard_urgent_util),
1234 	ATTR_LIST(discard_granularity),
1235 	ATTR_LIST(max_ordered_discard),
1236 	ATTR_LIST(discard_io_aware),
1237 	ATTR_LIST(pending_discard),
1238 	ATTR_LIST(gc_mode),
1239 	ATTR_LIST(ipu_policy),
1240 	ATTR_LIST(min_ipu_util),
1241 	ATTR_LIST(min_fsync_blocks),
1242 	ATTR_LIST(min_seq_blocks),
1243 	ATTR_LIST(min_hot_blocks),
1244 	ATTR_LIST(min_ssr_sections),
1245 	ATTR_LIST(reserved_segments),
1246 	ATTR_LIST(max_victim_search),
1247 	ATTR_LIST(migration_granularity),
1248 	ATTR_LIST(migration_window_granularity),
1249 	ATTR_LIST(dir_level),
1250 	ATTR_LIST(ram_thresh),
1251 	ATTR_LIST(ra_nid_pages),
1252 	ATTR_LIST(dirty_nats_ratio),
1253 	ATTR_LIST(max_roll_forward_node_blocks),
1254 	ATTR_LIST(cp_interval),
1255 	ATTR_LIST(idle_interval),
1256 	ATTR_LIST(discard_idle_interval),
1257 	ATTR_LIST(gc_idle_interval),
1258 	ATTR_LIST(umount_discard_timeout),
1259 #ifdef CONFIG_F2FS_IOSTAT
1260 	ATTR_LIST(iostat_enable),
1261 	ATTR_LIST(iostat_period_ms),
1262 #endif
1263 	ATTR_LIST(readdir_ra),
1264 	ATTR_LIST(max_io_bytes),
1265 	ATTR_LIST(gc_pin_file_thresh),
1266 	ATTR_LIST(extension_list),
1267 #ifdef CONFIG_F2FS_FAULT_INJECTION
1268 	ATTR_LIST(inject_rate),
1269 	ATTR_LIST(inject_type),
1270 #endif
1271 	ATTR_LIST(data_io_flag),
1272 	ATTR_LIST(node_io_flag),
1273 	ATTR_LIST(gc_remaining_trials),
1274 	ATTR_LIST(ckpt_thread_ioprio),
1275 	ATTR_LIST(dirty_segments),
1276 	ATTR_LIST(free_segments),
1277 	ATTR_LIST(ovp_segments),
1278 	ATTR_LIST(unusable),
1279 	ATTR_LIST(lifetime_write_kbytes),
1280 	ATTR_LIST(features),
1281 	ATTR_LIST(reserved_blocks),
1282 	ATTR_LIST(current_reserved_blocks),
1283 	ATTR_LIST(encoding),
1284 	ATTR_LIST(encoding_flags),
1285 	ATTR_LIST(mounted_time_sec),
1286 #ifdef CONFIG_F2FS_STAT_FS
1287 	ATTR_LIST(cp_foreground_calls),
1288 	ATTR_LIST(cp_background_calls),
1289 	ATTR_LIST(gc_foreground_calls),
1290 	ATTR_LIST(gc_background_calls),
1291 	ATTR_LIST(moved_blocks_foreground),
1292 	ATTR_LIST(moved_blocks_background),
1293 	ATTR_LIST(avg_vblocks),
1294 #endif
1295 #ifdef CONFIG_BLK_DEV_ZONED
1296 	ATTR_LIST(unusable_blocks_per_sec),
1297 	ATTR_LIST(blkzone_alloc_policy),
1298 #endif
1299 #ifdef CONFIG_F2FS_FS_COMPRESSION
1300 	ATTR_LIST(compr_written_block),
1301 	ATTR_LIST(compr_saved_block),
1302 	ATTR_LIST(compr_new_inode),
1303 	ATTR_LIST(compress_percent),
1304 	ATTR_LIST(compress_watermark),
1305 #endif
1306 	/* For ATGC */
1307 	ATTR_LIST(atgc_candidate_ratio),
1308 	ATTR_LIST(atgc_candidate_count),
1309 	ATTR_LIST(atgc_age_weight),
1310 	ATTR_LIST(atgc_age_threshold),
1311 	ATTR_LIST(atgc_enabled),
1312 	ATTR_LIST(seq_file_ra_mul),
1313 	ATTR_LIST(gc_segment_mode),
1314 	ATTR_LIST(gc_reclaimed_segments),
1315 	ATTR_LIST(max_fragment_chunk),
1316 	ATTR_LIST(max_fragment_hole),
1317 	ATTR_LIST(current_atomic_write),
1318 	ATTR_LIST(peak_atomic_write),
1319 	ATTR_LIST(committed_atomic_block),
1320 	ATTR_LIST(revoked_atomic_block),
1321 	ATTR_LIST(hot_data_age_threshold),
1322 	ATTR_LIST(warm_data_age_threshold),
1323 	ATTR_LIST(last_age_weight),
1324 	ATTR_LIST(max_read_extent_count),
1325 	ATTR_LIST(carve_out),
1326 	NULL,
1327 };
1328 ATTRIBUTE_GROUPS(f2fs);
1329 
1330 #define BASE_ATTR_LIST(name) (&f2fs_base_attr_##name.attr)
1331 static struct attribute *f2fs_feat_attrs[] = {
1332 #ifdef CONFIG_FS_ENCRYPTION
1333 	BASE_ATTR_LIST(encryption),
1334 	BASE_ATTR_LIST(test_dummy_encryption_v2),
1335 #if IS_ENABLED(CONFIG_UNICODE)
1336 	BASE_ATTR_LIST(encrypted_casefold),
1337 #endif
1338 #endif /* CONFIG_FS_ENCRYPTION */
1339 #ifdef CONFIG_BLK_DEV_ZONED
1340 	BASE_ATTR_LIST(block_zoned),
1341 #endif
1342 	BASE_ATTR_LIST(atomic_write),
1343 	BASE_ATTR_LIST(extra_attr),
1344 	BASE_ATTR_LIST(project_quota),
1345 	BASE_ATTR_LIST(inode_checksum),
1346 	BASE_ATTR_LIST(flexible_inline_xattr),
1347 	BASE_ATTR_LIST(quota_ino),
1348 	BASE_ATTR_LIST(inode_crtime),
1349 	BASE_ATTR_LIST(lost_found),
1350 #ifdef CONFIG_FS_VERITY
1351 	BASE_ATTR_LIST(verity),
1352 #endif
1353 	BASE_ATTR_LIST(sb_checksum),
1354 #if IS_ENABLED(CONFIG_UNICODE)
1355 	BASE_ATTR_LIST(casefold),
1356 #endif
1357 	BASE_ATTR_LIST(readonly),
1358 #ifdef CONFIG_F2FS_FS_COMPRESSION
1359 	BASE_ATTR_LIST(compression),
1360 #endif
1361 	BASE_ATTR_LIST(pin_file),
1362 #ifdef CONFIG_UNICODE
1363 	BASE_ATTR_LIST(linear_lookup),
1364 #endif
1365 	NULL,
1366 };
1367 ATTRIBUTE_GROUPS(f2fs_feat);
1368 
1369 F2FS_GENERAL_RO_ATTR(sb_status);
1370 F2FS_GENERAL_RO_ATTR(cp_status);
1371 F2FS_GENERAL_RO_ATTR(issued_discard);
1372 F2FS_GENERAL_RO_ATTR(queued_discard);
1373 F2FS_GENERAL_RO_ATTR(undiscard_blks);
1374 
1375 static struct attribute *f2fs_stat_attrs[] = {
1376 	ATTR_LIST(sb_status),
1377 	ATTR_LIST(cp_status),
1378 	ATTR_LIST(issued_discard),
1379 	ATTR_LIST(queued_discard),
1380 	ATTR_LIST(undiscard_blks),
1381 	NULL,
1382 };
1383 ATTRIBUTE_GROUPS(f2fs_stat);
1384 
1385 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1386 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1387 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1388 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1389 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1390 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1391 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1392 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1393 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1394 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1395 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1396 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1397 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1398 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1399 F2FS_SB_FEATURE_RO_ATTR(device_alias, DEVICE_ALIAS);
1400 
1401 static struct attribute *f2fs_sb_feat_attrs[] = {
1402 	ATTR_LIST(sb_encryption),
1403 	ATTR_LIST(sb_block_zoned),
1404 	ATTR_LIST(sb_extra_attr),
1405 	ATTR_LIST(sb_project_quota),
1406 	ATTR_LIST(sb_inode_checksum),
1407 	ATTR_LIST(sb_flexible_inline_xattr),
1408 	ATTR_LIST(sb_quota_ino),
1409 	ATTR_LIST(sb_inode_crtime),
1410 	ATTR_LIST(sb_lost_found),
1411 	ATTR_LIST(sb_verity),
1412 	ATTR_LIST(sb_sb_checksum),
1413 	ATTR_LIST(sb_casefold),
1414 	ATTR_LIST(sb_compression),
1415 	ATTR_LIST(sb_readonly),
1416 	ATTR_LIST(sb_device_alias),
1417 	NULL,
1418 };
1419 ATTRIBUTE_GROUPS(f2fs_sb_feat);
1420 
1421 F2FS_TUNE_RW_ATTR(reclaim_caches_kb);
1422 
1423 static struct attribute *f2fs_tune_attrs[] = {
1424 	BASE_ATTR_LIST(reclaim_caches_kb),
1425 	NULL,
1426 };
1427 ATTRIBUTE_GROUPS(f2fs_tune);
1428 
1429 static const struct sysfs_ops f2fs_attr_ops = {
1430 	.show	= f2fs_attr_show,
1431 	.store	= f2fs_attr_store,
1432 };
1433 
1434 static const struct kobj_type f2fs_sb_ktype = {
1435 	.default_groups = f2fs_groups,
1436 	.sysfs_ops	= &f2fs_attr_ops,
1437 	.release	= f2fs_sb_release,
1438 };
1439 
1440 static const struct kobj_type f2fs_ktype = {
1441 	.sysfs_ops	= &f2fs_attr_ops,
1442 };
1443 
1444 static struct kset f2fs_kset = {
1445 	.kobj	= {.ktype = &f2fs_ktype},
1446 };
1447 
1448 static const struct sysfs_ops f2fs_feat_attr_ops = {
1449 	.show	= f2fs_base_attr_show,
1450 	.store	= f2fs_base_attr_store,
1451 };
1452 
1453 static const struct kobj_type f2fs_feat_ktype = {
1454 	.default_groups = f2fs_feat_groups,
1455 	.sysfs_ops	= &f2fs_feat_attr_ops,
1456 };
1457 
1458 static struct kobject f2fs_feat = {
1459 	.kset	= &f2fs_kset,
1460 };
1461 
1462 static const struct sysfs_ops f2fs_tune_attr_ops = {
1463 	.show	= f2fs_base_attr_show,
1464 	.store	= f2fs_base_attr_store,
1465 };
1466 
1467 static const struct kobj_type f2fs_tune_ktype = {
1468 	.default_groups = f2fs_tune_groups,
1469 	.sysfs_ops	= &f2fs_tune_attr_ops,
1470 };
1471 
1472 static struct kobject f2fs_tune = {
1473 	.kset	= &f2fs_kset,
1474 };
1475 
f2fs_stat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1476 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1477 				struct attribute *attr, char *buf)
1478 {
1479 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1480 								s_stat_kobj);
1481 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1482 
1483 	return a->show ? a->show(a, sbi, buf) : 0;
1484 }
1485 
f2fs_stat_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)1486 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1487 						const char *buf, size_t len)
1488 {
1489 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1490 								s_stat_kobj);
1491 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1492 
1493 	return a->store ? a->store(a, sbi, buf, len) : 0;
1494 }
1495 
f2fs_stat_kobj_release(struct kobject * kobj)1496 static void f2fs_stat_kobj_release(struct kobject *kobj)
1497 {
1498 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1499 								s_stat_kobj);
1500 	complete(&sbi->s_stat_kobj_unregister);
1501 }
1502 
1503 static const struct sysfs_ops f2fs_stat_attr_ops = {
1504 	.show	= f2fs_stat_attr_show,
1505 	.store	= f2fs_stat_attr_store,
1506 };
1507 
1508 static const struct kobj_type f2fs_stat_ktype = {
1509 	.default_groups = f2fs_stat_groups,
1510 	.sysfs_ops	= &f2fs_stat_attr_ops,
1511 	.release	= f2fs_stat_kobj_release,
1512 };
1513 
f2fs_sb_feat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1514 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1515 				struct attribute *attr, char *buf)
1516 {
1517 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1518 							s_feature_list_kobj);
1519 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1520 
1521 	return a->show ? a->show(a, sbi, buf) : 0;
1522 }
1523 
f2fs_feature_list_kobj_release(struct kobject * kobj)1524 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1525 {
1526 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1527 							s_feature_list_kobj);
1528 	complete(&sbi->s_feature_list_kobj_unregister);
1529 }
1530 
1531 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1532 	.show	= f2fs_sb_feat_attr_show,
1533 };
1534 
1535 static const struct kobj_type f2fs_feature_list_ktype = {
1536 	.default_groups = f2fs_sb_feat_groups,
1537 	.sysfs_ops	= &f2fs_feature_list_attr_ops,
1538 	.release	= f2fs_feature_list_kobj_release,
1539 };
1540 
segment_info_seq_show(struct seq_file * seq,void * offset)1541 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1542 						void *offset)
1543 {
1544 	struct super_block *sb = seq->private;
1545 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1546 	unsigned int total_segs =
1547 			le32_to_cpu(sbi->raw_super->segment_count_main);
1548 	int i;
1549 
1550 	seq_puts(seq, "format: segment_type|valid_blocks\n"
1551 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1552 
1553 	for (i = 0; i < total_segs; i++) {
1554 		struct seg_entry *se = get_seg_entry(sbi, i);
1555 
1556 		if ((i % 10) == 0)
1557 			seq_printf(seq, "%-10d", i);
1558 		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1559 		if ((i % 10) == 9 || i == (total_segs - 1))
1560 			seq_putc(seq, '\n');
1561 		else
1562 			seq_putc(seq, ' ');
1563 	}
1564 
1565 	return 0;
1566 }
1567 
segment_bits_seq_show(struct seq_file * seq,void * offset)1568 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1569 						void *offset)
1570 {
1571 	struct super_block *sb = seq->private;
1572 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1573 	unsigned int total_segs =
1574 			le32_to_cpu(sbi->raw_super->segment_count_main);
1575 	int i, j;
1576 
1577 	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps|mtime\n"
1578 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1579 
1580 	for (i = 0; i < total_segs; i++) {
1581 		struct seg_entry *se = get_seg_entry(sbi, i);
1582 
1583 		seq_printf(seq, "%-10d", i);
1584 		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1585 		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1586 			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1587 		seq_printf(seq, "| %llx", se->mtime);
1588 		seq_putc(seq, '\n');
1589 	}
1590 	return 0;
1591 }
1592 
victim_bits_seq_show(struct seq_file * seq,void * offset)1593 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1594 						void *offset)
1595 {
1596 	struct super_block *sb = seq->private;
1597 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1598 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1599 	int i;
1600 
1601 	seq_puts(seq, "format: victim_secmap bitmaps\n");
1602 
1603 	for (i = 0; i < MAIN_SECS(sbi); i++) {
1604 		if ((i % 10) == 0)
1605 			seq_printf(seq, "%-10d", i);
1606 		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1607 		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1608 			seq_putc(seq, '\n');
1609 		else
1610 			seq_putc(seq, ' ');
1611 	}
1612 	return 0;
1613 }
1614 
discard_plist_seq_show(struct seq_file * seq,void * offset)1615 static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
1616 						void *offset)
1617 {
1618 	struct super_block *sb = seq->private;
1619 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1620 	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1621 	int i, count;
1622 
1623 	seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
1624 	if (!f2fs_realtime_discard_enable(sbi))
1625 		return 0;
1626 
1627 	if (dcc) {
1628 		mutex_lock(&dcc->cmd_lock);
1629 		for (i = 0; i < MAX_PLIST_NUM; i++) {
1630 			struct list_head *pend_list;
1631 			struct discard_cmd *dc, *tmp;
1632 
1633 			if (i % 8 == 0)
1634 				seq_printf(seq, "  %-3d", i);
1635 			count = 0;
1636 			pend_list = &dcc->pend_list[i];
1637 			list_for_each_entry_safe(dc, tmp, pend_list, list)
1638 				count++;
1639 			if (count)
1640 				seq_printf(seq, " %7d", count);
1641 			else
1642 				seq_puts(seq, "       .");
1643 			if (i % 8 == 7)
1644 				seq_putc(seq, '\n');
1645 		}
1646 		seq_putc(seq, '\n');
1647 		mutex_unlock(&dcc->cmd_lock);
1648 	}
1649 
1650 	return 0;
1651 }
1652 
disk_map_seq_show(struct seq_file * seq,void * offset)1653 static int __maybe_unused disk_map_seq_show(struct seq_file *seq,
1654 						void *offset)
1655 {
1656 	struct super_block *sb = seq->private;
1657 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1658 	int i;
1659 
1660 	seq_printf(seq, "Address Layout   : %5luB Block address (# of Segments)\n",
1661 					F2FS_BLKSIZE);
1662 	seq_printf(seq, " SB            : %12s\n", "0/1024B");
1663 	seq_printf(seq, " seg0_blkaddr  : 0x%010x\n", SEG0_BLKADDR(sbi));
1664 	seq_printf(seq, " Checkpoint    : 0x%010x (%10d)\n",
1665 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2);
1666 	seq_printf(seq, " SIT           : 0x%010x (%10d)\n",
1667 			SIT_I(sbi)->sit_base_addr,
1668 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit));
1669 	seq_printf(seq, " NAT           : 0x%010x (%10d)\n",
1670 			NM_I(sbi)->nat_blkaddr,
1671 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat));
1672 	seq_printf(seq, " SSA           : 0x%010x (%10d)\n",
1673 			SM_I(sbi)->ssa_blkaddr,
1674 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
1675 	seq_printf(seq, " Main          : 0x%010x (%10d)\n",
1676 			SM_I(sbi)->main_blkaddr,
1677 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main));
1678 	seq_printf(seq, " # of Sections : %12d\n",
1679 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count));
1680 	seq_printf(seq, " Segs/Sections : %12d\n",
1681 			SEGS_PER_SEC(sbi));
1682 	seq_printf(seq, " Section size  : %12d MB\n",
1683 			SEGS_PER_SEC(sbi) << 1);
1684 
1685 	if (!f2fs_is_multi_device(sbi))
1686 		return 0;
1687 
1688 	seq_puts(seq, "\nDisk Map for multi devices:\n");
1689 	for (i = 0; i < sbi->s_ndevs; i++)
1690 		seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n",
1691 			i, bdev_is_zoned(FDEV(i).bdev),
1692 			FDEV(i).start_blk, FDEV(i).end_blk,
1693 			FDEV(i).path);
1694 	return 0;
1695 }
1696 
1697 #ifdef CONFIG_F2FS_FAULT_INJECTION
inject_stats_seq_show(struct seq_file * seq,void * offset)1698 static int __maybe_unused inject_stats_seq_show(struct seq_file *seq,
1699 						void *offset)
1700 {
1701 	struct super_block *sb = seq->private;
1702 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1703 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1704 	int i;
1705 
1706 	seq_puts(seq, "fault_type		injected_count\n");
1707 
1708 	for (i = 0; i < FAULT_MAX; i++)
1709 		seq_printf(seq, "%-24s%-10u\n", f2fs_fault_name[i],
1710 						ffi->inject_count[i]);
1711 	return 0;
1712 }
1713 #endif
1714 
f2fs_init_sysfs(void)1715 int __init f2fs_init_sysfs(void)
1716 {
1717 	int ret;
1718 
1719 	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1720 	f2fs_kset.kobj.parent = fs_kobj;
1721 	ret = kset_register(&f2fs_kset);
1722 	if (ret)
1723 		return ret;
1724 
1725 	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1726 				   NULL, "features");
1727 	if (ret)
1728 		goto put_kobject;
1729 
1730 	ret = kobject_init_and_add(&f2fs_tune, &f2fs_tune_ktype,
1731 				   NULL, "tuning");
1732 	if (ret)
1733 		goto put_kobject;
1734 
1735 	f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1736 	if (!f2fs_proc_root) {
1737 		ret = -ENOMEM;
1738 		goto put_kobject;
1739 	}
1740 
1741 	return 0;
1742 
1743 put_kobject:
1744 	kobject_put(&f2fs_tune);
1745 	kobject_put(&f2fs_feat);
1746 	kset_unregister(&f2fs_kset);
1747 	return ret;
1748 }
1749 
f2fs_exit_sysfs(void)1750 void f2fs_exit_sysfs(void)
1751 {
1752 	kobject_put(&f2fs_tune);
1753 	kobject_put(&f2fs_feat);
1754 	kset_unregister(&f2fs_kset);
1755 	remove_proc_entry("fs/f2fs", NULL);
1756 	f2fs_proc_root = NULL;
1757 }
1758 
f2fs_register_sysfs(struct f2fs_sb_info * sbi)1759 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1760 {
1761 	struct super_block *sb = sbi->sb;
1762 	int err;
1763 
1764 	sbi->s_kobj.kset = &f2fs_kset;
1765 	init_completion(&sbi->s_kobj_unregister);
1766 	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1767 				"%s", sb->s_id);
1768 	if (err)
1769 		goto put_sb_kobj;
1770 
1771 	sbi->s_stat_kobj.kset = &f2fs_kset;
1772 	init_completion(&sbi->s_stat_kobj_unregister);
1773 	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1774 						&sbi->s_kobj, "stat");
1775 	if (err)
1776 		goto put_stat_kobj;
1777 
1778 	sbi->s_feature_list_kobj.kset = &f2fs_kset;
1779 	init_completion(&sbi->s_feature_list_kobj_unregister);
1780 	err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1781 					&f2fs_feature_list_ktype,
1782 					&sbi->s_kobj, "feature_list");
1783 	if (err)
1784 		goto put_feature_list_kobj;
1785 
1786 	sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1787 	if (!sbi->s_proc) {
1788 		err = -ENOMEM;
1789 		goto put_feature_list_kobj;
1790 	}
1791 
1792 	proc_create_single_data("segment_info", 0444, sbi->s_proc,
1793 				segment_info_seq_show, sb);
1794 	proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1795 				segment_bits_seq_show, sb);
1796 #ifdef CONFIG_F2FS_IOSTAT
1797 	proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1798 				iostat_info_seq_show, sb);
1799 #endif
1800 	proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1801 				victim_bits_seq_show, sb);
1802 	proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
1803 				discard_plist_seq_show, sb);
1804 	proc_create_single_data("disk_map", 0444, sbi->s_proc,
1805 				disk_map_seq_show, sb);
1806 #ifdef CONFIG_F2FS_FAULT_INJECTION
1807 	proc_create_single_data("inject_stats", 0444, sbi->s_proc,
1808 				inject_stats_seq_show, sb);
1809 #endif
1810 	return 0;
1811 put_feature_list_kobj:
1812 	kobject_put(&sbi->s_feature_list_kobj);
1813 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1814 put_stat_kobj:
1815 	kobject_put(&sbi->s_stat_kobj);
1816 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1817 put_sb_kobj:
1818 	kobject_put(&sbi->s_kobj);
1819 	wait_for_completion(&sbi->s_kobj_unregister);
1820 	return err;
1821 }
1822 
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)1823 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1824 {
1825 	remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
1826 
1827 	kobject_put(&sbi->s_stat_kobj);
1828 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1829 	kobject_put(&sbi->s_feature_list_kobj);
1830 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1831 
1832 	kobject_put(&sbi->s_kobj);
1833 	wait_for_completion(&sbi->s_kobj_unregister);
1834 }
1835