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