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