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 *, struct f2fs_sb_info *, char *);
57 	ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
58 			 const char *, size_t);
59 	int struct_type;
60 	int offset;
61 	int id;
62 };
63 
64 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
65 			     struct f2fs_sb_info *sbi, char *buf);
66 
__struct_ptr(struct f2fs_sb_info * sbi,int struct_type)67 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
68 {
69 	if (struct_type == GC_THREAD)
70 		return (unsigned char *)sbi->gc_thread;
71 	else if (struct_type == SM_INFO)
72 		return (unsigned char *)SM_I(sbi);
73 	else if (struct_type == DCC_INFO)
74 		return (unsigned char *)SM_I(sbi)->dcc_info;
75 	else if (struct_type == NM_INFO)
76 		return (unsigned char *)NM_I(sbi);
77 	else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
78 		return (unsigned char *)sbi;
79 #ifdef CONFIG_F2FS_FAULT_INJECTION
80 	else if (struct_type == FAULT_INFO_RATE ||
81 					struct_type == FAULT_INFO_TYPE)
82 		return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
83 #endif
84 #ifdef CONFIG_F2FS_STAT_FS
85 	else if (struct_type == STAT_INFO)
86 		return (unsigned char *)F2FS_STAT(sbi);
87 #endif
88 	else if (struct_type == CPRC_INFO)
89 		return (unsigned char *)&sbi->cprc_info;
90 	else if (struct_type == ATGC_INFO)
91 		return (unsigned char *)&sbi->am;
92 	return NULL;
93 }
94 
dirty_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)95 static ssize_t dirty_segments_show(struct f2fs_attr *a,
96 		struct f2fs_sb_info *sbi, char *buf)
97 {
98 	return sprintf(buf, "%llu\n",
99 			(unsigned long long)(dirty_segments(sbi)));
100 }
101 
free_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)102 static ssize_t free_segments_show(struct f2fs_attr *a,
103 		struct f2fs_sb_info *sbi, char *buf)
104 {
105 	return sprintf(buf, "%llu\n",
106 			(unsigned long long)(free_segments(sbi)));
107 }
108 
ovp_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)109 static ssize_t ovp_segments_show(struct f2fs_attr *a,
110 		struct f2fs_sb_info *sbi, char *buf)
111 {
112 	return sprintf(buf, "%llu\n",
113 			(unsigned long long)(overprovision_segments(sbi)));
114 }
115 
lifetime_write_kbytes_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)116 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
117 		struct f2fs_sb_info *sbi, char *buf)
118 {
119 	return sprintf(buf, "%llu\n",
120 			(unsigned long long)(sbi->kbytes_written +
121 			((f2fs_get_sectors_written(sbi) -
122 				sbi->sectors_written_start) >> 1)));
123 }
124 
sb_status_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)125 static ssize_t sb_status_show(struct f2fs_attr *a,
126 		struct f2fs_sb_info *sbi, char *buf)
127 {
128 	return sprintf(buf, "%lx\n", sbi->s_flag);
129 }
130 
cp_status_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)131 static ssize_t cp_status_show(struct f2fs_attr *a,
132 		struct f2fs_sb_info *sbi, char *buf)
133 {
134 	return sprintf(buf, "%x\n", le32_to_cpu(F2FS_CKPT(sbi)->ckpt_flags));
135 }
136 
pending_discard_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)137 static ssize_t pending_discard_show(struct f2fs_attr *a,
138 		struct f2fs_sb_info *sbi, char *buf)
139 {
140 	if (!SM_I(sbi)->dcc_info)
141 		return -EINVAL;
142 	return sprintf(buf, "%llu\n", (unsigned long long)atomic_read(
143 				&SM_I(sbi)->dcc_info->discard_cmd_cnt));
144 }
145 
features_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)146 static ssize_t features_show(struct f2fs_attr *a,
147 		struct f2fs_sb_info *sbi, char *buf)
148 {
149 	int len = 0;
150 
151 	if (f2fs_sb_has_encrypt(sbi))
152 		len += scnprintf(buf, PAGE_SIZE - len, "%s",
153 						"encryption");
154 	if (f2fs_sb_has_blkzoned(sbi))
155 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
156 				len ? ", " : "", "blkzoned");
157 	if (f2fs_sb_has_extra_attr(sbi))
158 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
159 				len ? ", " : "", "extra_attr");
160 	if (f2fs_sb_has_project_quota(sbi))
161 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
162 				len ? ", " : "", "projquota");
163 	if (f2fs_sb_has_inode_chksum(sbi))
164 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
165 				len ? ", " : "", "inode_checksum");
166 	if (f2fs_sb_has_flexible_inline_xattr(sbi))
167 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
168 				len ? ", " : "", "flexible_inline_xattr");
169 	if (f2fs_sb_has_quota_ino(sbi))
170 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
171 				len ? ", " : "", "quota_ino");
172 	if (f2fs_sb_has_inode_crtime(sbi))
173 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
174 				len ? ", " : "", "inode_crtime");
175 	if (f2fs_sb_has_lost_found(sbi))
176 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
177 				len ? ", " : "", "lost_found");
178 	if (f2fs_sb_has_verity(sbi))
179 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
180 				len ? ", " : "", "verity");
181 	if (f2fs_sb_has_sb_chksum(sbi))
182 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
183 				len ? ", " : "", "sb_checksum");
184 	if (f2fs_sb_has_casefold(sbi))
185 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
186 				len ? ", " : "", "casefold");
187 	if (f2fs_sb_has_readonly(sbi))
188 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
189 				len ? ", " : "", "readonly");
190 	if (f2fs_sb_has_compression(sbi))
191 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
192 				len ? ", " : "", "compression");
193 	len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
194 				len ? ", " : "", "pin_file");
195 	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
196 	return len;
197 }
198 
current_reserved_blocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)199 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
200 					struct f2fs_sb_info *sbi, char *buf)
201 {
202 	return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
203 }
204 
unusable_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)205 static ssize_t unusable_show(struct f2fs_attr *a,
206 		struct f2fs_sb_info *sbi, char *buf)
207 {
208 	block_t unusable;
209 
210 	if (test_opt(sbi, DISABLE_CHECKPOINT))
211 		unusable = sbi->unusable_block_count;
212 	else
213 		unusable = f2fs_get_unusable_blocks(sbi);
214 	return sprintf(buf, "%llu\n", (unsigned long long)unusable);
215 }
216 
encoding_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)217 static ssize_t encoding_show(struct f2fs_attr *a,
218 		struct f2fs_sb_info *sbi, char *buf)
219 {
220 #if IS_ENABLED(CONFIG_UNICODE)
221 	struct super_block *sb = sbi->sb;
222 
223 	if (f2fs_sb_has_casefold(sbi))
224 		return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
225 			(sb->s_encoding->version >> 16) & 0xff,
226 			(sb->s_encoding->version >> 8) & 0xff,
227 			sb->s_encoding->version & 0xff);
228 #endif
229 	return sprintf(buf, "(none)");
230 }
231 
mounted_time_sec_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)232 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
233 		struct f2fs_sb_info *sbi, char *buf)
234 {
235 	return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
236 }
237 
238 #ifdef CONFIG_F2FS_STAT_FS
moved_blocks_foreground_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)239 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
240 				struct f2fs_sb_info *sbi, char *buf)
241 {
242 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
243 
244 	return sprintf(buf, "%llu\n",
245 		(unsigned long long)(si->tot_blks -
246 			(si->bg_data_blks + si->bg_node_blks)));
247 }
248 
moved_blocks_background_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)249 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
250 				struct f2fs_sb_info *sbi, char *buf)
251 {
252 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
253 
254 	return sprintf(buf, "%llu\n",
255 		(unsigned long long)(si->bg_data_blks + si->bg_node_blks));
256 }
257 
avg_vblocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)258 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
259 		struct f2fs_sb_info *sbi, char *buf)
260 {
261 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
262 
263 	si->dirty_count = dirty_segments(sbi);
264 	f2fs_update_sit_info(sbi);
265 	return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
266 }
267 #endif
268 
main_blkaddr_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)269 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
270 				struct f2fs_sb_info *sbi, char *buf)
271 {
272 	return sysfs_emit(buf, "%llu\n",
273 			(unsigned long long)MAIN_BLKADDR(sbi));
274 }
275 
f2fs_sbi_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)276 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
277 			struct f2fs_sb_info *sbi, char *buf)
278 {
279 	unsigned char *ptr = NULL;
280 	unsigned int *ui;
281 
282 	ptr = __struct_ptr(sbi, a->struct_type);
283 	if (!ptr)
284 		return -EINVAL;
285 
286 	if (!strcmp(a->attr.name, "extension_list")) {
287 		__u8 (*extlist)[F2FS_EXTENSION_LEN] =
288 					sbi->raw_super->extension_list;
289 		int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
290 		int hot_count = sbi->raw_super->hot_ext_count;
291 		int len = 0, i;
292 
293 		len += scnprintf(buf + len, PAGE_SIZE - len,
294 						"cold file extension:\n");
295 		for (i = 0; i < cold_count; i++)
296 			len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
297 								extlist[i]);
298 
299 		len += scnprintf(buf + len, PAGE_SIZE - len,
300 						"hot file extension:\n");
301 		for (i = cold_count; i < cold_count + hot_count; i++)
302 			len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
303 								extlist[i]);
304 		return len;
305 	}
306 
307 	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
308 		struct ckpt_req_control *cprc = &sbi->cprc_info;
309 		int len = 0;
310 		int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
311 		int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
312 
313 		if (class == IOPRIO_CLASS_RT)
314 			len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
315 		else if (class == IOPRIO_CLASS_BE)
316 			len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
317 		else
318 			return -EINVAL;
319 
320 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
321 		return len;
322 	}
323 
324 #ifdef CONFIG_F2FS_FS_COMPRESSION
325 	if (!strcmp(a->attr.name, "compr_written_block"))
326 		return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
327 
328 	if (!strcmp(a->attr.name, "compr_saved_block"))
329 		return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
330 
331 	if (!strcmp(a->attr.name, "compr_new_inode"))
332 		return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
333 #endif
334 
335 	if (!strcmp(a->attr.name, "gc_urgent"))
336 		return sysfs_emit(buf, "%s\n",
337 				gc_mode_names[sbi->gc_mode]);
338 
339 	if (!strcmp(a->attr.name, "gc_segment_mode"))
340 		return sysfs_emit(buf, "%s\n",
341 				gc_mode_names[sbi->gc_segment_mode]);
342 
343 	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
344 		return sysfs_emit(buf, "%u\n",
345 			sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
346 	}
347 
348 	if (!strcmp(a->attr.name, "current_atomic_write")) {
349 		s64 current_write = atomic64_read(&sbi->current_atomic_write);
350 
351 		return sysfs_emit(buf, "%lld\n", current_write);
352 	}
353 
354 	if (!strcmp(a->attr.name, "peak_atomic_write"))
355 		return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write);
356 
357 	if (!strcmp(a->attr.name, "committed_atomic_block"))
358 		return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block);
359 
360 	if (!strcmp(a->attr.name, "revoked_atomic_block"))
361 		return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block);
362 
363 	ui = (unsigned int *)(ptr + a->offset);
364 
365 	return sprintf(buf, "%u\n", *ui);
366 }
367 
__sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)368 static ssize_t __sbi_store(struct f2fs_attr *a,
369 			struct f2fs_sb_info *sbi,
370 			const char *buf, size_t count)
371 {
372 	unsigned char *ptr;
373 	unsigned long t;
374 	unsigned int *ui;
375 	ssize_t ret;
376 
377 	ptr = __struct_ptr(sbi, a->struct_type);
378 	if (!ptr)
379 		return -EINVAL;
380 
381 	if (!strcmp(a->attr.name, "extension_list")) {
382 		const char *name = strim((char *)buf);
383 		bool set = true, hot;
384 
385 		if (!strncmp(name, "[h]", 3))
386 			hot = true;
387 		else if (!strncmp(name, "[c]", 3))
388 			hot = false;
389 		else
390 			return -EINVAL;
391 
392 		name += 3;
393 
394 		if (*name == '!') {
395 			name++;
396 			set = false;
397 		}
398 
399 		if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
400 			return -EINVAL;
401 
402 		f2fs_down_write(&sbi->sb_lock);
403 
404 		ret = f2fs_update_extension_list(sbi, name, hot, set);
405 		if (ret)
406 			goto out;
407 
408 		ret = f2fs_commit_super(sbi, false);
409 		if (ret)
410 			f2fs_update_extension_list(sbi, name, hot, !set);
411 out:
412 		f2fs_up_write(&sbi->sb_lock);
413 		return ret ? ret : count;
414 	}
415 
416 	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
417 		const char *name = strim((char *)buf);
418 		struct ckpt_req_control *cprc = &sbi->cprc_info;
419 		int class;
420 		long data;
421 		int ret;
422 
423 		if (!strncmp(name, "rt,", 3))
424 			class = IOPRIO_CLASS_RT;
425 		else if (!strncmp(name, "be,", 3))
426 			class = IOPRIO_CLASS_BE;
427 		else
428 			return -EINVAL;
429 
430 		name += 3;
431 		ret = kstrtol(name, 10, &data);
432 		if (ret)
433 			return ret;
434 		if (data >= IOPRIO_NR_LEVELS || data < 0)
435 			return -EINVAL;
436 
437 		cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
438 		if (test_opt(sbi, MERGE_CHECKPOINT)) {
439 			ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
440 					cprc->ckpt_thread_ioprio);
441 			if (ret)
442 				return ret;
443 		}
444 
445 		return count;
446 	}
447 
448 	ui = (unsigned int *)(ptr + a->offset);
449 
450 	ret = kstrtoul(skip_spaces(buf), 0, &t);
451 	if (ret < 0)
452 		return ret;
453 #ifdef CONFIG_F2FS_FAULT_INJECTION
454 	if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
455 		return -EINVAL;
456 	if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
457 		return -EINVAL;
458 #endif
459 	if (a->struct_type == RESERVED_BLOCKS) {
460 		spin_lock(&sbi->stat_lock);
461 		if (t > (unsigned long)(sbi->user_block_count -
462 				F2FS_OPTION(sbi).root_reserved_blocks -
463 				sbi->blocks_per_seg *
464 				SM_I(sbi)->additional_reserved_segments)) {
465 			spin_unlock(&sbi->stat_lock);
466 			return -EINVAL;
467 		}
468 		*ui = t;
469 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
470 				sbi->user_block_count - valid_user_blocks(sbi));
471 		spin_unlock(&sbi->stat_lock);
472 		return count;
473 	}
474 
475 	if (!strcmp(a->attr.name, "discard_granularity")) {
476 		if (t == 0 || t > MAX_PLIST_NUM)
477 			return -EINVAL;
478 		if (!f2fs_block_unit_discard(sbi))
479 			return -EINVAL;
480 		if (t == *ui)
481 			return count;
482 		*ui = t;
483 		return count;
484 	}
485 
486 	if (!strcmp(a->attr.name, "migration_granularity")) {
487 		if (t == 0 || t > sbi->segs_per_sec)
488 			return -EINVAL;
489 	}
490 
491 	if (!strcmp(a->attr.name, "trim_sections"))
492 		return -EINVAL;
493 
494 	if (!strcmp(a->attr.name, "gc_urgent")) {
495 		if (t == 0) {
496 			sbi->gc_mode = GC_NORMAL;
497 		} else if (t == 1) {
498 			sbi->gc_mode = GC_URGENT_HIGH;
499 			if (sbi->gc_thread) {
500 				sbi->gc_thread->gc_wake = 1;
501 				wake_up_interruptible_all(
502 					&sbi->gc_thread->gc_wait_queue_head);
503 				wake_up_discard_thread(sbi, true);
504 			}
505 		} else if (t == 2) {
506 			sbi->gc_mode = GC_URGENT_LOW;
507 		} else if (t == 3) {
508 			sbi->gc_mode = GC_URGENT_MID;
509 			if (sbi->gc_thread) {
510 				sbi->gc_thread->gc_wake = 1;
511 				wake_up_interruptible_all(
512 					&sbi->gc_thread->gc_wait_queue_head);
513 			}
514 		} else {
515 			return -EINVAL;
516 		}
517 		return count;
518 	}
519 	if (!strcmp(a->attr.name, "gc_idle")) {
520 		if (t == GC_IDLE_CB) {
521 			sbi->gc_mode = GC_IDLE_CB;
522 		} else if (t == GC_IDLE_GREEDY) {
523 			sbi->gc_mode = GC_IDLE_GREEDY;
524 		} else if (t == GC_IDLE_AT) {
525 			if (!sbi->am.atgc_enabled)
526 				return -EINVAL;
527 			sbi->gc_mode = GC_IDLE_AT;
528 		} else {
529 			sbi->gc_mode = GC_NORMAL;
530 		}
531 		return count;
532 	}
533 
534 	if (!strcmp(a->attr.name, "gc_urgent_high_remaining")) {
535 		spin_lock(&sbi->gc_urgent_high_lock);
536 		sbi->gc_urgent_high_remaining = t;
537 		spin_unlock(&sbi->gc_urgent_high_lock);
538 
539 		return count;
540 	}
541 
542 #ifdef CONFIG_F2FS_IOSTAT
543 	if (!strcmp(a->attr.name, "iostat_enable")) {
544 		sbi->iostat_enable = !!t;
545 		if (!sbi->iostat_enable)
546 			f2fs_reset_iostat(sbi);
547 		return count;
548 	}
549 
550 	if (!strcmp(a->attr.name, "iostat_period_ms")) {
551 		if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
552 			return -EINVAL;
553 		spin_lock(&sbi->iostat_lock);
554 		sbi->iostat_period_ms = (unsigned int)t;
555 		spin_unlock(&sbi->iostat_lock);
556 		return count;
557 	}
558 #endif
559 
560 #ifdef CONFIG_F2FS_FS_COMPRESSION
561 	if (!strcmp(a->attr.name, "compr_written_block") ||
562 		!strcmp(a->attr.name, "compr_saved_block")) {
563 		if (t != 0)
564 			return -EINVAL;
565 		sbi->compr_written_block = 0;
566 		sbi->compr_saved_block = 0;
567 		return count;
568 	}
569 
570 	if (!strcmp(a->attr.name, "compr_new_inode")) {
571 		if (t != 0)
572 			return -EINVAL;
573 		sbi->compr_new_inode = 0;
574 		return count;
575 	}
576 #endif
577 
578 	if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
579 		if (t > 100)
580 			return -EINVAL;
581 		sbi->am.candidate_ratio = t;
582 		return count;
583 	}
584 
585 	if (!strcmp(a->attr.name, "atgc_age_weight")) {
586 		if (t > 100)
587 			return -EINVAL;
588 		sbi->am.age_weight = t;
589 		return count;
590 	}
591 
592 	if (!strcmp(a->attr.name, "gc_segment_mode")) {
593 		if (t < MAX_GC_MODE)
594 			sbi->gc_segment_mode = t;
595 		else
596 			return -EINVAL;
597 		return count;
598 	}
599 
600 	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
601 		if (t != 0)
602 			return -EINVAL;
603 		sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
604 		return count;
605 	}
606 
607 	if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
608 		if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
609 			sbi->seq_file_ra_mul = t;
610 		else
611 			return -EINVAL;
612 		return count;
613 	}
614 
615 	if (!strcmp(a->attr.name, "max_fragment_chunk")) {
616 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
617 			sbi->max_fragment_chunk = t;
618 		else
619 			return -EINVAL;
620 		return count;
621 	}
622 
623 	if (!strcmp(a->attr.name, "max_fragment_hole")) {
624 		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
625 			sbi->max_fragment_hole = t;
626 		else
627 			return -EINVAL;
628 		return count;
629 	}
630 
631 	if (!strcmp(a->attr.name, "peak_atomic_write")) {
632 		if (t != 0)
633 			return -EINVAL;
634 		sbi->peak_atomic_write = 0;
635 		return count;
636 	}
637 
638 	if (!strcmp(a->attr.name, "committed_atomic_block")) {
639 		if (t != 0)
640 			return -EINVAL;
641 		sbi->committed_atomic_block = 0;
642 		return count;
643 	}
644 
645 	if (!strcmp(a->attr.name, "revoked_atomic_block")) {
646 		if (t != 0)
647 			return -EINVAL;
648 		sbi->revoked_atomic_block = 0;
649 		return count;
650 	}
651 
652 	*ui = (unsigned int)t;
653 
654 	return count;
655 }
656 
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)657 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
658 			struct f2fs_sb_info *sbi,
659 			const char *buf, size_t count)
660 {
661 	ssize_t ret;
662 	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
663 					a->struct_type == GC_THREAD);
664 
665 	if (gc_entry) {
666 		if (!down_read_trylock(&sbi->sb->s_umount))
667 			return -EAGAIN;
668 	}
669 	ret = __sbi_store(a, sbi, buf, count);
670 	if (gc_entry)
671 		up_read(&sbi->sb->s_umount);
672 
673 	return ret;
674 }
675 
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)676 static ssize_t f2fs_attr_show(struct kobject *kobj,
677 				struct attribute *attr, char *buf)
678 {
679 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
680 								s_kobj);
681 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
682 
683 	return a->show ? a->show(a, sbi, buf) : 0;
684 }
685 
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)686 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
687 						const char *buf, size_t len)
688 {
689 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
690 									s_kobj);
691 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
692 
693 	return a->store ? a->store(a, sbi, buf, len) : 0;
694 }
695 
f2fs_sb_release(struct kobject * kobj)696 static void f2fs_sb_release(struct kobject *kobj)
697 {
698 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
699 								s_kobj);
700 	complete(&sbi->s_kobj_unregister);
701 }
702 
703 /*
704  * Note that there are three feature list entries:
705  * 1) /sys/fs/f2fs/features
706  *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
707  *     - ref. F2FS_FEATURE_RO_ATTR()
708  *
709  * 2) /sys/fs/f2fs/$s_id/features <deprecated>
710  *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
711  *     won't add new feature anymore, and thus, users should check entries in 3)
712  *     instead of this 2).
713  *
714  * 3) /sys/fs/f2fs/$s_id/feature_list
715  *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
716  *     sysfs entry rule where each entry should expose single value.
717  *     This list covers old feature list provided by 2) and beyond. Therefore,
718  *     please add new on-disk feature in this list only.
719  *     - ref. F2FS_SB_FEATURE_RO_ATTR()
720  */
f2fs_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)721 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
722 		struct f2fs_sb_info *sbi, char *buf)
723 {
724 	return sprintf(buf, "supported\n");
725 }
726 
727 #define F2FS_FEATURE_RO_ATTR(_name)				\
728 static struct f2fs_attr f2fs_attr_##_name = {			\
729 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
730 	.show	= f2fs_feature_show,				\
731 }
732 
f2fs_sb_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)733 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
734 		struct f2fs_sb_info *sbi, char *buf)
735 {
736 	if (F2FS_HAS_FEATURE(sbi, a->id))
737 		return sprintf(buf, "supported\n");
738 	return sprintf(buf, "unsupported\n");
739 }
740 
741 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
742 static struct f2fs_attr f2fs_attr_sb_##_name = {		\
743 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
744 	.show	= f2fs_sb_feature_show,				\
745 	.id	= F2FS_FEATURE_##_feat,				\
746 }
747 
748 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
749 static struct f2fs_attr f2fs_attr_##_name = {			\
750 	.attr = {.name = __stringify(_name), .mode = _mode },	\
751 	.show	= _show,					\
752 	.store	= _store,					\
753 	.struct_type = _struct_type,				\
754 	.offset = _offset					\
755 }
756 
757 #define F2FS_RO_ATTR(struct_type, struct_name, name, elname)	\
758 	F2FS_ATTR_OFFSET(struct_type, name, 0444,		\
759 		f2fs_sbi_show, NULL,				\
760 		offsetof(struct struct_name, elname))
761 
762 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
763 	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
764 		f2fs_sbi_show, f2fs_sbi_store,			\
765 		offsetof(struct struct_name, elname))
766 
767 #define F2FS_GENERAL_RO_ATTR(name) \
768 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
769 
770 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname)	\
771 static struct f2fs_attr f2fs_attr_##_name = {			\
772 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
773 	.show = f2fs_sbi_show,					\
774 	.struct_type = _struct_type,				\
775 	.offset = offsetof(struct _struct_name, _elname),       \
776 }
777 
778 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
779 							urgent_sleep_time);
780 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
781 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
782 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
783 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
784 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
785 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
786 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
787 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_discard_request, max_discard_request);
788 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, min_discard_issue_time, min_discard_issue_time);
789 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, mid_discard_issue_time, mid_discard_issue_time);
790 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_discard_issue_time, max_discard_issue_time);
791 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
792 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
793 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
794 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
795 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
796 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
797 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
798 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
799 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
800 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
801 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
802 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
803 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, max_roll_forward_node_blocks, max_rf_node_blocks);
804 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
805 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
806 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
807 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
808 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
809 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
810 					interval_time[DISCARD_TIME]);
811 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
812 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
813 		umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
814 #ifdef CONFIG_F2FS_IOSTAT
815 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
816 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
817 #endif
818 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
819 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
820 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
821 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
822 #ifdef CONFIG_F2FS_FAULT_INJECTION
823 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
824 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
825 #endif
826 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
827 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
828 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent_high_remaining, gc_urgent_high_remaining);
829 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
830 F2FS_GENERAL_RO_ATTR(dirty_segments);
831 F2FS_GENERAL_RO_ATTR(free_segments);
832 F2FS_GENERAL_RO_ATTR(ovp_segments);
833 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
834 F2FS_GENERAL_RO_ATTR(features);
835 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
836 F2FS_GENERAL_RO_ATTR(unusable);
837 F2FS_GENERAL_RO_ATTR(encoding);
838 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
839 F2FS_GENERAL_RO_ATTR(main_blkaddr);
840 F2FS_GENERAL_RO_ATTR(pending_discard);
841 #ifdef CONFIG_F2FS_STAT_FS
842 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
843 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
844 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
845 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
846 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
847 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
848 F2FS_GENERAL_RO_ATTR(avg_vblocks);
849 #endif
850 
851 #ifdef CONFIG_FS_ENCRYPTION
852 F2FS_FEATURE_RO_ATTR(encryption);
853 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
854 #if IS_ENABLED(CONFIG_UNICODE)
855 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
856 #endif
857 #endif /* CONFIG_FS_ENCRYPTION */
858 #ifdef CONFIG_BLK_DEV_ZONED
859 F2FS_FEATURE_RO_ATTR(block_zoned);
860 F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, unusable_blocks_per_sec,
861 					unusable_blocks_per_sec);
862 #endif
863 F2FS_FEATURE_RO_ATTR(atomic_write);
864 F2FS_FEATURE_RO_ATTR(extra_attr);
865 F2FS_FEATURE_RO_ATTR(project_quota);
866 F2FS_FEATURE_RO_ATTR(inode_checksum);
867 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
868 F2FS_FEATURE_RO_ATTR(quota_ino);
869 F2FS_FEATURE_RO_ATTR(inode_crtime);
870 F2FS_FEATURE_RO_ATTR(lost_found);
871 #ifdef CONFIG_FS_VERITY
872 F2FS_FEATURE_RO_ATTR(verity);
873 #endif
874 F2FS_FEATURE_RO_ATTR(sb_checksum);
875 #if IS_ENABLED(CONFIG_UNICODE)
876 F2FS_FEATURE_RO_ATTR(casefold);
877 #endif
878 F2FS_FEATURE_RO_ATTR(readonly);
879 #ifdef CONFIG_F2FS_FS_COMPRESSION
880 F2FS_FEATURE_RO_ATTR(compression);
881 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_written_block, compr_written_block);
882 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_saved_block, compr_saved_block);
883 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_new_inode, compr_new_inode);
884 #endif
885 F2FS_FEATURE_RO_ATTR(pin_file);
886 
887 /* For ATGC */
888 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_ratio, candidate_ratio);
889 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_count, max_candidate_count);
890 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_weight, age_weight);
891 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
892 
893 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, seq_file_ra_mul, seq_file_ra_mul);
894 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
895 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
896 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_chunk, max_fragment_chunk);
897 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_hole, max_fragment_hole);
898 
899 /* For atomic write */
900 F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, current_atomic_write, current_atomic_write);
901 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, peak_atomic_write, peak_atomic_write);
902 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, committed_atomic_block, committed_atomic_block);
903 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, revoked_atomic_block, revoked_atomic_block);
904 
905 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
906 static struct attribute *f2fs_attrs[] = {
907 	ATTR_LIST(gc_urgent_sleep_time),
908 	ATTR_LIST(gc_min_sleep_time),
909 	ATTR_LIST(gc_max_sleep_time),
910 	ATTR_LIST(gc_no_gc_sleep_time),
911 	ATTR_LIST(gc_idle),
912 	ATTR_LIST(gc_urgent),
913 	ATTR_LIST(reclaim_segments),
914 	ATTR_LIST(main_blkaddr),
915 	ATTR_LIST(max_small_discards),
916 	ATTR_LIST(max_discard_request),
917 	ATTR_LIST(min_discard_issue_time),
918 	ATTR_LIST(mid_discard_issue_time),
919 	ATTR_LIST(max_discard_issue_time),
920 	ATTR_LIST(discard_granularity),
921 	ATTR_LIST(pending_discard),
922 	ATTR_LIST(batched_trim_sections),
923 	ATTR_LIST(ipu_policy),
924 	ATTR_LIST(min_ipu_util),
925 	ATTR_LIST(min_fsync_blocks),
926 	ATTR_LIST(min_seq_blocks),
927 	ATTR_LIST(min_hot_blocks),
928 	ATTR_LIST(min_ssr_sections),
929 	ATTR_LIST(max_victim_search),
930 	ATTR_LIST(migration_granularity),
931 	ATTR_LIST(dir_level),
932 	ATTR_LIST(ram_thresh),
933 	ATTR_LIST(ra_nid_pages),
934 	ATTR_LIST(dirty_nats_ratio),
935 	ATTR_LIST(max_roll_forward_node_blocks),
936 	ATTR_LIST(cp_interval),
937 	ATTR_LIST(idle_interval),
938 	ATTR_LIST(discard_idle_interval),
939 	ATTR_LIST(gc_idle_interval),
940 	ATTR_LIST(umount_discard_timeout),
941 #ifdef CONFIG_F2FS_IOSTAT
942 	ATTR_LIST(iostat_enable),
943 	ATTR_LIST(iostat_period_ms),
944 #endif
945 	ATTR_LIST(readdir_ra),
946 	ATTR_LIST(max_io_bytes),
947 	ATTR_LIST(gc_pin_file_thresh),
948 	ATTR_LIST(extension_list),
949 #ifdef CONFIG_F2FS_FAULT_INJECTION
950 	ATTR_LIST(inject_rate),
951 	ATTR_LIST(inject_type),
952 #endif
953 	ATTR_LIST(data_io_flag),
954 	ATTR_LIST(node_io_flag),
955 	ATTR_LIST(gc_urgent_high_remaining),
956 	ATTR_LIST(ckpt_thread_ioprio),
957 	ATTR_LIST(dirty_segments),
958 	ATTR_LIST(free_segments),
959 	ATTR_LIST(ovp_segments),
960 	ATTR_LIST(unusable),
961 	ATTR_LIST(lifetime_write_kbytes),
962 	ATTR_LIST(features),
963 	ATTR_LIST(reserved_blocks),
964 	ATTR_LIST(current_reserved_blocks),
965 	ATTR_LIST(encoding),
966 	ATTR_LIST(mounted_time_sec),
967 #ifdef CONFIG_F2FS_STAT_FS
968 	ATTR_LIST(cp_foreground_calls),
969 	ATTR_LIST(cp_background_calls),
970 	ATTR_LIST(gc_foreground_calls),
971 	ATTR_LIST(gc_background_calls),
972 	ATTR_LIST(moved_blocks_foreground),
973 	ATTR_LIST(moved_blocks_background),
974 	ATTR_LIST(avg_vblocks),
975 #endif
976 #ifdef CONFIG_BLK_DEV_ZONED
977 	ATTR_LIST(unusable_blocks_per_sec),
978 #endif
979 #ifdef CONFIG_F2FS_FS_COMPRESSION
980 	ATTR_LIST(compr_written_block),
981 	ATTR_LIST(compr_saved_block),
982 	ATTR_LIST(compr_new_inode),
983 #endif
984 	/* For ATGC */
985 	ATTR_LIST(atgc_candidate_ratio),
986 	ATTR_LIST(atgc_candidate_count),
987 	ATTR_LIST(atgc_age_weight),
988 	ATTR_LIST(atgc_age_threshold),
989 	ATTR_LIST(seq_file_ra_mul),
990 	ATTR_LIST(gc_segment_mode),
991 	ATTR_LIST(gc_reclaimed_segments),
992 	ATTR_LIST(max_fragment_chunk),
993 	ATTR_LIST(max_fragment_hole),
994 	ATTR_LIST(current_atomic_write),
995 	ATTR_LIST(peak_atomic_write),
996 	ATTR_LIST(committed_atomic_block),
997 	ATTR_LIST(revoked_atomic_block),
998 	NULL,
999 };
1000 ATTRIBUTE_GROUPS(f2fs);
1001 
1002 static struct attribute *f2fs_feat_attrs[] = {
1003 #ifdef CONFIG_FS_ENCRYPTION
1004 	ATTR_LIST(encryption),
1005 	ATTR_LIST(test_dummy_encryption_v2),
1006 #if IS_ENABLED(CONFIG_UNICODE)
1007 	ATTR_LIST(encrypted_casefold),
1008 #endif
1009 #endif /* CONFIG_FS_ENCRYPTION */
1010 #ifdef CONFIG_BLK_DEV_ZONED
1011 	ATTR_LIST(block_zoned),
1012 #endif
1013 	ATTR_LIST(atomic_write),
1014 	ATTR_LIST(extra_attr),
1015 	ATTR_LIST(project_quota),
1016 	ATTR_LIST(inode_checksum),
1017 	ATTR_LIST(flexible_inline_xattr),
1018 	ATTR_LIST(quota_ino),
1019 	ATTR_LIST(inode_crtime),
1020 	ATTR_LIST(lost_found),
1021 #ifdef CONFIG_FS_VERITY
1022 	ATTR_LIST(verity),
1023 #endif
1024 	ATTR_LIST(sb_checksum),
1025 #if IS_ENABLED(CONFIG_UNICODE)
1026 	ATTR_LIST(casefold),
1027 #endif
1028 	ATTR_LIST(readonly),
1029 #ifdef CONFIG_F2FS_FS_COMPRESSION
1030 	ATTR_LIST(compression),
1031 #endif
1032 	ATTR_LIST(pin_file),
1033 	NULL,
1034 };
1035 ATTRIBUTE_GROUPS(f2fs_feat);
1036 
1037 F2FS_GENERAL_RO_ATTR(sb_status);
1038 F2FS_GENERAL_RO_ATTR(cp_status);
1039 static struct attribute *f2fs_stat_attrs[] = {
1040 	ATTR_LIST(sb_status),
1041 	ATTR_LIST(cp_status),
1042 	NULL,
1043 };
1044 ATTRIBUTE_GROUPS(f2fs_stat);
1045 
1046 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1047 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1048 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1049 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1050 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1051 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1052 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1053 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1054 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1055 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1056 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1057 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1058 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1059 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1060 
1061 static struct attribute *f2fs_sb_feat_attrs[] = {
1062 	ATTR_LIST(sb_encryption),
1063 	ATTR_LIST(sb_block_zoned),
1064 	ATTR_LIST(sb_extra_attr),
1065 	ATTR_LIST(sb_project_quota),
1066 	ATTR_LIST(sb_inode_checksum),
1067 	ATTR_LIST(sb_flexible_inline_xattr),
1068 	ATTR_LIST(sb_quota_ino),
1069 	ATTR_LIST(sb_inode_crtime),
1070 	ATTR_LIST(sb_lost_found),
1071 	ATTR_LIST(sb_verity),
1072 	ATTR_LIST(sb_sb_checksum),
1073 	ATTR_LIST(sb_casefold),
1074 	ATTR_LIST(sb_compression),
1075 	ATTR_LIST(sb_readonly),
1076 	NULL,
1077 };
1078 ATTRIBUTE_GROUPS(f2fs_sb_feat);
1079 
1080 static const struct sysfs_ops f2fs_attr_ops = {
1081 	.show	= f2fs_attr_show,
1082 	.store	= f2fs_attr_store,
1083 };
1084 
1085 static struct kobj_type f2fs_sb_ktype = {
1086 	.default_groups = f2fs_groups,
1087 	.sysfs_ops	= &f2fs_attr_ops,
1088 	.release	= f2fs_sb_release,
1089 };
1090 
1091 static struct kobj_type f2fs_ktype = {
1092 	.sysfs_ops	= &f2fs_attr_ops,
1093 };
1094 
1095 static struct kset f2fs_kset = {
1096 	.kobj	= {.ktype = &f2fs_ktype},
1097 };
1098 
1099 static struct kobj_type f2fs_feat_ktype = {
1100 	.default_groups = f2fs_feat_groups,
1101 	.sysfs_ops	= &f2fs_attr_ops,
1102 };
1103 
1104 static struct kobject f2fs_feat = {
1105 	.kset	= &f2fs_kset,
1106 };
1107 
f2fs_stat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1108 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1109 				struct attribute *attr, char *buf)
1110 {
1111 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1112 								s_stat_kobj);
1113 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1114 
1115 	return a->show ? a->show(a, sbi, buf) : 0;
1116 }
1117 
f2fs_stat_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)1118 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1119 						const char *buf, size_t len)
1120 {
1121 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1122 								s_stat_kobj);
1123 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1124 
1125 	return a->store ? a->store(a, sbi, buf, len) : 0;
1126 }
1127 
f2fs_stat_kobj_release(struct kobject * kobj)1128 static void f2fs_stat_kobj_release(struct kobject *kobj)
1129 {
1130 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1131 								s_stat_kobj);
1132 	complete(&sbi->s_stat_kobj_unregister);
1133 }
1134 
1135 static const struct sysfs_ops f2fs_stat_attr_ops = {
1136 	.show	= f2fs_stat_attr_show,
1137 	.store	= f2fs_stat_attr_store,
1138 };
1139 
1140 static struct kobj_type f2fs_stat_ktype = {
1141 	.default_groups = f2fs_stat_groups,
1142 	.sysfs_ops	= &f2fs_stat_attr_ops,
1143 	.release	= f2fs_stat_kobj_release,
1144 };
1145 
f2fs_sb_feat_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1146 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1147 				struct attribute *attr, char *buf)
1148 {
1149 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1150 							s_feature_list_kobj);
1151 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1152 
1153 	return a->show ? a->show(a, sbi, buf) : 0;
1154 }
1155 
f2fs_feature_list_kobj_release(struct kobject * kobj)1156 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1157 {
1158 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1159 							s_feature_list_kobj);
1160 	complete(&sbi->s_feature_list_kobj_unregister);
1161 }
1162 
1163 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1164 	.show	= f2fs_sb_feat_attr_show,
1165 };
1166 
1167 static struct kobj_type f2fs_feature_list_ktype = {
1168 	.default_groups = f2fs_sb_feat_groups,
1169 	.sysfs_ops	= &f2fs_feature_list_attr_ops,
1170 	.release	= f2fs_feature_list_kobj_release,
1171 };
1172 
segment_info_seq_show(struct seq_file * seq,void * offset)1173 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1174 						void *offset)
1175 {
1176 	struct super_block *sb = seq->private;
1177 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1178 	unsigned int total_segs =
1179 			le32_to_cpu(sbi->raw_super->segment_count_main);
1180 	int i;
1181 
1182 	seq_puts(seq, "format: segment_type|valid_blocks\n"
1183 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1184 
1185 	for (i = 0; i < total_segs; i++) {
1186 		struct seg_entry *se = get_seg_entry(sbi, i);
1187 
1188 		if ((i % 10) == 0)
1189 			seq_printf(seq, "%-10d", i);
1190 		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1191 		if ((i % 10) == 9 || i == (total_segs - 1))
1192 			seq_putc(seq, '\n');
1193 		else
1194 			seq_putc(seq, ' ');
1195 	}
1196 
1197 	return 0;
1198 }
1199 
segment_bits_seq_show(struct seq_file * seq,void * offset)1200 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1201 						void *offset)
1202 {
1203 	struct super_block *sb = seq->private;
1204 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1205 	unsigned int total_segs =
1206 			le32_to_cpu(sbi->raw_super->segment_count_main);
1207 	int i, j;
1208 
1209 	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1210 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1211 
1212 	for (i = 0; i < total_segs; i++) {
1213 		struct seg_entry *se = get_seg_entry(sbi, i);
1214 
1215 		seq_printf(seq, "%-10d", i);
1216 		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1217 		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1218 			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1219 		seq_putc(seq, '\n');
1220 	}
1221 	return 0;
1222 }
1223 
victim_bits_seq_show(struct seq_file * seq,void * offset)1224 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1225 						void *offset)
1226 {
1227 	struct super_block *sb = seq->private;
1228 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1229 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1230 	int i;
1231 
1232 	seq_puts(seq, "format: victim_secmap bitmaps\n");
1233 
1234 	for (i = 0; i < MAIN_SECS(sbi); i++) {
1235 		if ((i % 10) == 0)
1236 			seq_printf(seq, "%-10d", i);
1237 		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1238 		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1239 			seq_putc(seq, '\n');
1240 		else
1241 			seq_putc(seq, ' ');
1242 	}
1243 	return 0;
1244 }
1245 
f2fs_init_sysfs(void)1246 int __init f2fs_init_sysfs(void)
1247 {
1248 	int ret;
1249 
1250 	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1251 	f2fs_kset.kobj.parent = fs_kobj;
1252 	ret = kset_register(&f2fs_kset);
1253 	if (ret)
1254 		return ret;
1255 
1256 	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1257 				   NULL, "features");
1258 	if (ret) {
1259 		kobject_put(&f2fs_feat);
1260 		kset_unregister(&f2fs_kset);
1261 	} else {
1262 		f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1263 	}
1264 	return ret;
1265 }
1266 
f2fs_exit_sysfs(void)1267 void f2fs_exit_sysfs(void)
1268 {
1269 	kobject_put(&f2fs_feat);
1270 	kset_unregister(&f2fs_kset);
1271 	remove_proc_entry("fs/f2fs", NULL);
1272 	f2fs_proc_root = NULL;
1273 }
1274 
f2fs_register_sysfs(struct f2fs_sb_info * sbi)1275 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1276 {
1277 	struct super_block *sb = sbi->sb;
1278 	int err;
1279 
1280 	sbi->s_kobj.kset = &f2fs_kset;
1281 	init_completion(&sbi->s_kobj_unregister);
1282 	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1283 				"%s", sb->s_id);
1284 	if (err)
1285 		goto put_sb_kobj;
1286 
1287 	sbi->s_stat_kobj.kset = &f2fs_kset;
1288 	init_completion(&sbi->s_stat_kobj_unregister);
1289 	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1290 						&sbi->s_kobj, "stat");
1291 	if (err)
1292 		goto put_stat_kobj;
1293 
1294 	sbi->s_feature_list_kobj.kset = &f2fs_kset;
1295 	init_completion(&sbi->s_feature_list_kobj_unregister);
1296 	err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1297 					&f2fs_feature_list_ktype,
1298 					&sbi->s_kobj, "feature_list");
1299 	if (err)
1300 		goto put_feature_list_kobj;
1301 
1302 	if (f2fs_proc_root)
1303 		sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1304 
1305 	if (sbi->s_proc) {
1306 		proc_create_single_data("segment_info", 0444, sbi->s_proc,
1307 				segment_info_seq_show, sb);
1308 		proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1309 				segment_bits_seq_show, sb);
1310 #ifdef CONFIG_F2FS_IOSTAT
1311 		proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1312 				iostat_info_seq_show, sb);
1313 #endif
1314 		proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1315 				victim_bits_seq_show, sb);
1316 	}
1317 	return 0;
1318 put_feature_list_kobj:
1319 	kobject_put(&sbi->s_feature_list_kobj);
1320 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1321 put_stat_kobj:
1322 	kobject_put(&sbi->s_stat_kobj);
1323 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1324 put_sb_kobj:
1325 	kobject_put(&sbi->s_kobj);
1326 	wait_for_completion(&sbi->s_kobj_unregister);
1327 	return err;
1328 }
1329 
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)1330 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1331 {
1332 	if (sbi->s_proc) {
1333 #ifdef CONFIG_F2FS_IOSTAT
1334 		remove_proc_entry("iostat_info", sbi->s_proc);
1335 #endif
1336 		remove_proc_entry("segment_info", sbi->s_proc);
1337 		remove_proc_entry("segment_bits", sbi->s_proc);
1338 		remove_proc_entry("victim_bits", sbi->s_proc);
1339 		remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1340 	}
1341 
1342 	kobject_del(&sbi->s_stat_kobj);
1343 	kobject_put(&sbi->s_stat_kobj);
1344 	wait_for_completion(&sbi->s_stat_kobj_unregister);
1345 	kobject_del(&sbi->s_feature_list_kobj);
1346 	kobject_put(&sbi->s_feature_list_kobj);
1347 	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1348 
1349 	kobject_del(&sbi->s_kobj);
1350 	kobject_put(&sbi->s_kobj);
1351 	wait_for_completion(&sbi->s_kobj_unregister);
1352 }
1353