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