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