blob: df406c16b2ebdecb902257628e4298e68092ffe5 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Chao Yu8ceffcb2017-06-14 17:39:47 +08002/*
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>
Chao Yu8ceffcb2017-06-14 17:39:47 +08008 */
Randy Dunlapcb15d1e2018-07-06 20:50:57 -07009#include <linux/compiler.h>
Chao Yu8ceffcb2017-06-14 17:39:47 +080010#include <linux/proc_fs.h>
11#include <linux/f2fs_fs.h>
Jaegeuk Kim299aa412017-07-13 17:45:21 -070012#include <linux/seq_file.h>
Daniel Rosenberg5aba5432019-07-23 16:05:28 -070013#include <linux/unicode.h>
Daeho Jeonge6592062021-01-21 22:45:29 +090014#include <linux/ioprio.h>
Daeho Jeong5ac443e2021-03-15 17:12:33 +090015#include <linux/sysfs.h>
Chao Yu8ceffcb2017-06-14 17:39:47 +080016
17#include "f2fs.h"
18#include "segment.h"
19#include "gc.h"
Daeho Jeong52118742021-08-19 20:52:28 -070020#include "iostat.h"
Daeho Jeong2bc4bea2020-03-30 03:30:59 +000021#include <trace/events/f2fs.h>
Chao Yu8ceffcb2017-06-14 17:39:47 +080022
23static struct proc_dir_entry *f2fs_proc_root;
Chao Yu8ceffcb2017-06-14 17:39:47 +080024
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 */
Hridya Valsarajufc7100e2020-01-22 10:51:16 -080032#ifdef CONFIG_F2FS_STAT_FS
Jack Qiua87aff12020-07-24 16:55:28 +080033 STAT_INFO, /* struct f2fs_stat_info */
Hridya Valsarajufc7100e2020-01-22 10:51:16 -080034#endif
Chao Yu8ceffcb2017-06-14 17:39:47 +080035#ifdef CONFIG_F2FS_FAULT_INJECTION
36 FAULT_INFO_RATE, /* struct f2fs_fault_info */
37 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
38#endif
Yunlong Song80d42142017-10-27 20:45:05 +080039 RESERVED_BLOCKS, /* struct f2fs_sb_info */
Daeho Jeonge6592062021-01-21 22:45:29 +090040 CPRC_INFO, /* struct ckpt_req_control */
Chao Yu8939a8482021-05-12 10:07:19 +080041 ATGC_INFO, /* struct atgc_management */
Chao Yu8ceffcb2017-06-14 17:39:47 +080042};
43
44struct f2fs_attr {
45 struct attribute attr;
46 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
47 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
48 const char *, size_t);
49 int struct_type;
50 int offset;
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -070051 int id;
Chao Yu8ceffcb2017-06-14 17:39:47 +080052};
53
Hridya Valsarajufc7100e2020-01-22 10:51:16 -080054static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
55 struct f2fs_sb_info *sbi, char *buf);
56
Chao Yu8ceffcb2017-06-14 17:39:47 +080057static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
58{
59 if (struct_type == GC_THREAD)
60 return (unsigned char *)sbi->gc_thread;
61 else if (struct_type == SM_INFO)
62 return (unsigned char *)SM_I(sbi);
63 else if (struct_type == DCC_INFO)
64 return (unsigned char *)SM_I(sbi)->dcc_info;
65 else if (struct_type == NM_INFO)
66 return (unsigned char *)NM_I(sbi);
Chao Yudaeb4332017-06-26 16:24:41 +080067 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
Chao Yu8ceffcb2017-06-14 17:39:47 +080068 return (unsigned char *)sbi;
69#ifdef CONFIG_F2FS_FAULT_INJECTION
70 else if (struct_type == FAULT_INFO_RATE ||
71 struct_type == FAULT_INFO_TYPE)
Chao Yu63189b72018-03-08 14:22:56 +080072 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
Chao Yu8ceffcb2017-06-14 17:39:47 +080073#endif
Hridya Valsarajufc7100e2020-01-22 10:51:16 -080074#ifdef CONFIG_F2FS_STAT_FS
75 else if (struct_type == STAT_INFO)
76 return (unsigned char *)F2FS_STAT(sbi);
77#endif
Daeho Jeonge6592062021-01-21 22:45:29 +090078 else if (struct_type == CPRC_INFO)
79 return (unsigned char *)&sbi->cprc_info;
Chao Yu8939a8482021-05-12 10:07:19 +080080 else if (struct_type == ATGC_INFO)
81 return (unsigned char *)&sbi->am;
Chao Yu8ceffcb2017-06-14 17:39:47 +080082 return NULL;
83}
84
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +020085static ssize_t dirty_segments_show(struct f2fs_attr *a,
86 struct f2fs_sb_info *sbi, char *buf)
87{
Hridya Valsarajufc7100e2020-01-22 10:51:16 -080088 return sprintf(buf, "%llu\n",
89 (unsigned long long)(dirty_segments(sbi)));
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +020090}
91
Hridya Valsarajufc7100e2020-01-22 10:51:16 -080092static ssize_t free_segments_show(struct f2fs_attr *a,
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -070093 struct f2fs_sb_info *sbi, char *buf)
94{
Hridya Valsarajufc7100e2020-01-22 10:51:16 -080095 return sprintf(buf, "%llu\n",
96 (unsigned long long)(free_segments(sbi)));
Daniel Rosenberg5aba5432019-07-23 16:05:28 -070097}
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -070098
Jaegeuk Kim08234272021-03-01 17:28:16 -080099static ssize_t ovp_segments_show(struct f2fs_attr *a,
100 struct f2fs_sb_info *sbi, char *buf)
101{
102 return sprintf(buf, "%llu\n",
103 (unsigned long long)(overprovision_segments(sbi)));
104}
105
Chao Yu8ceffcb2017-06-14 17:39:47 +0800106static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
107 struct f2fs_sb_info *sbi, char *buf)
108{
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800109 return sprintf(buf, "%llu\n",
110 (unsigned long long)(sbi->kbytes_written +
Chao Yu3a0a9cb2020-11-27 21:20:06 +0800111 ((f2fs_get_sectors_written(sbi) -
112 sbi->sectors_written_start) >> 1)));
Chao Yu8ceffcb2017-06-14 17:39:47 +0800113}
114
Chao Yu0bfe9f72021-01-14 09:41:27 +0800115static ssize_t sb_status_show(struct f2fs_attr *a,
116 struct f2fs_sb_info *sbi, char *buf)
117{
118 return sprintf(buf, "%lx\n", sbi->s_flag);
119}
120
Jaegeuk Kimae2e2802021-11-29 10:36:12 -0800121static ssize_t pending_discard_show(struct f2fs_attr *a,
122 struct f2fs_sb_info *sbi, char *buf)
123{
124 if (!SM_I(sbi)->dcc_info)
125 return -EINVAL;
126 return sprintf(buf, "%llu\n", (unsigned long long)atomic_read(
127 &SM_I(sbi)->dcc_info->discard_cmd_cnt));
128}
129
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700130static ssize_t features_show(struct f2fs_attr *a,
131 struct f2fs_sb_info *sbi, char *buf)
132{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700133 int len = 0;
134
Chao Yu7beb01f2018-10-24 18:34:26 +0800135 if (f2fs_sb_has_encrypt(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100136 len += scnprintf(buf, PAGE_SIZE - len, "%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700137 "encryption");
Chao Yu7beb01f2018-10-24 18:34:26 +0800138 if (f2fs_sb_has_blkzoned(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100139 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700140 len ? ", " : "", "blkzoned");
Chao Yu7beb01f2018-10-24 18:34:26 +0800141 if (f2fs_sb_has_extra_attr(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100142 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700143 len ? ", " : "", "extra_attr");
Chao Yu7beb01f2018-10-24 18:34:26 +0800144 if (f2fs_sb_has_project_quota(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100145 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700146 len ? ", " : "", "projquota");
Chao Yu7beb01f2018-10-24 18:34:26 +0800147 if (f2fs_sb_has_inode_chksum(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100148 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700149 len ? ", " : "", "inode_checksum");
Chao Yu7beb01f2018-10-24 18:34:26 +0800150 if (f2fs_sb_has_flexible_inline_xattr(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100151 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Chao Yu6afc6622017-09-06 21:59:50 +0800152 len ? ", " : "", "flexible_inline_xattr");
Chao Yu7beb01f2018-10-24 18:34:26 +0800153 if (f2fs_sb_has_quota_ino(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100154 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700155 len ? ", " : "", "quota_ino");
Chao Yu7beb01f2018-10-24 18:34:26 +0800156 if (f2fs_sb_has_inode_crtime(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100157 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Chao Yu1c1d35d2018-01-25 14:54:42 +0800158 len ? ", " : "", "inode_crtime");
Chao Yu7beb01f2018-10-24 18:34:26 +0800159 if (f2fs_sb_has_lost_found(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100160 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Sheng Yongb7c409d2018-03-15 18:51:41 +0800161 len ? ", " : "", "lost_found");
Eric Biggers95ae2512019-07-22 09:26:24 -0700162 if (f2fs_sb_has_verity(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100163 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Eric Biggers95ae2512019-07-22 09:26:24 -0700164 len ? ", " : "", "verity");
Chao Yu7beb01f2018-10-24 18:34:26 +0800165 if (f2fs_sb_has_sb_chksum(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100166 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Junling Zhengd440c522018-09-28 20:25:56 +0800167 len ? ", " : "", "sb_checksum");
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700168 if (f2fs_sb_has_casefold(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100169 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700170 len ? ", " : "", "casefold");
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -0700171 if (f2fs_sb_has_readonly(sbi))
172 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
173 len ? ", " : "", "readonly");
Chao Yu4c8ff702019-11-01 18:07:14 +0800174 if (f2fs_sb_has_compression(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100175 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Chao Yu4c8ff702019-11-01 18:07:14 +0800176 len ? ", " : "", "compression");
Takashi Iwaic6d57892020-03-11 10:33:53 +0100177 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -0700178 len ? ", " : "", "pin_file");
Takashi Iwaic6d57892020-03-11 10:33:53 +0100179 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700180 return len;
181}
182
Yunlong Song80d42142017-10-27 20:45:05 +0800183static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
184 struct f2fs_sb_info *sbi, char *buf)
185{
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800186 return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
Yunlong Song80d42142017-10-27 20:45:05 +0800187}
188
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800189static ssize_t unusable_show(struct f2fs_attr *a,
190 struct f2fs_sb_info *sbi, char *buf)
191{
192 block_t unusable;
193
194 if (test_opt(sbi, DISABLE_CHECKPOINT))
195 unusable = sbi->unusable_block_count;
196 else
197 unusable = f2fs_get_unusable_blocks(sbi);
198 return sprintf(buf, "%llu\n", (unsigned long long)unusable);
199}
200
201static ssize_t encoding_show(struct f2fs_attr *a,
202 struct f2fs_sb_info *sbi, char *buf)
203{
204#ifdef CONFIG_UNICODE
Daniel Rosenbergeca48732020-07-08 02:12:36 -0700205 struct super_block *sb = sbi->sb;
206
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800207 if (f2fs_sb_has_casefold(sbi))
Linus Torvalds66612242022-01-17 05:40:02 +0200208 return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
Daniel Rosenbergeca48732020-07-08 02:12:36 -0700209 (sb->s_encoding->version >> 16) & 0xff,
210 (sb->s_encoding->version >> 8) & 0xff,
211 sb->s_encoding->version & 0xff);
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800212#endif
213 return sprintf(buf, "(none)");
214}
215
Jaegeuk Kima7e679b2020-02-25 19:08:16 -0800216static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
217 struct f2fs_sb_info *sbi, char *buf)
218{
219 return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
220}
221
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800222#ifdef CONFIG_F2FS_STAT_FS
223static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
224 struct f2fs_sb_info *sbi, char *buf)
225{
226 struct f2fs_stat_info *si = F2FS_STAT(sbi);
227
228 return sprintf(buf, "%llu\n",
229 (unsigned long long)(si->tot_blks -
230 (si->bg_data_blks + si->bg_node_blks)));
231}
232
233static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
234 struct f2fs_sb_info *sbi, char *buf)
235{
236 struct f2fs_stat_info *si = F2FS_STAT(sbi);
237
238 return sprintf(buf, "%llu\n",
239 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
240}
241
242static ssize_t avg_vblocks_show(struct f2fs_attr *a,
243 struct f2fs_sb_info *sbi, char *buf)
244{
245 struct f2fs_stat_info *si = F2FS_STAT(sbi);
246
247 si->dirty_count = dirty_segments(sbi);
248 f2fs_update_sit_info(sbi);
249 return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
250}
251#endif
252
Dehe Guffcde4b2020-07-03 17:51:29 +0800253static ssize_t main_blkaddr_show(struct f2fs_attr *a,
254 struct f2fs_sb_info *sbi, char *buf)
255{
Qing Wang84eab2a2021-10-12 20:29:04 -0700256 return sysfs_emit(buf, "%llu\n",
Dehe Guffcde4b2020-07-03 17:51:29 +0800257 (unsigned long long)MAIN_BLKADDR(sbi));
258}
259
Chao Yu8ceffcb2017-06-14 17:39:47 +0800260static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
261 struct f2fs_sb_info *sbi, char *buf)
262{
263 unsigned char *ptr = NULL;
264 unsigned int *ui;
265
266 ptr = __struct_ptr(sbi, a->struct_type);
267 if (!ptr)
268 return -EINVAL;
269
Chao Yu846ae672018-02-26 22:04:13 +0800270 if (!strcmp(a->attr.name, "extension_list")) {
271 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
272 sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800273 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
274 int hot_count = sbi->raw_super->hot_ext_count;
Chao Yu846ae672018-02-26 22:04:13 +0800275 int len = 0, i;
276
Takashi Iwaic6d57892020-03-11 10:33:53 +0100277 len += scnprintf(buf + len, PAGE_SIZE - len,
Colin Ian King45800382018-04-30 16:27:44 +0100278 "cold file extension:\n");
Chao Yub6a06cb2018-02-28 17:07:27 +0800279 for (i = 0; i < cold_count; i++)
Takashi Iwaic6d57892020-03-11 10:33:53 +0100280 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
Chao Yub6a06cb2018-02-28 17:07:27 +0800281 extlist[i]);
282
Takashi Iwaic6d57892020-03-11 10:33:53 +0100283 len += scnprintf(buf + len, PAGE_SIZE - len,
Colin Ian King45800382018-04-30 16:27:44 +0100284 "hot file extension:\n");
Chao Yub6a06cb2018-02-28 17:07:27 +0800285 for (i = cold_count; i < cold_count + hot_count; i++)
Takashi Iwaic6d57892020-03-11 10:33:53 +0100286 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
Chao Yu846ae672018-02-26 22:04:13 +0800287 extlist[i]);
288 return len;
289 }
290
Daeho Jeonge6592062021-01-21 22:45:29 +0900291 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
292 struct ckpt_req_control *cprc = &sbi->cprc_info;
293 int len = 0;
294 int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
295 int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
296
297 if (class == IOPRIO_CLASS_RT)
298 len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
299 else if (class == IOPRIO_CLASS_BE)
300 len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
301 else
302 return -EINVAL;
303
304 len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
305 return len;
306 }
307
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900308#ifdef CONFIG_F2FS_FS_COMPRESSION
309 if (!strcmp(a->attr.name, "compr_written_block"))
310 return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
311
312 if (!strcmp(a->attr.name, "compr_saved_block"))
313 return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
314
315 if (!strcmp(a->attr.name, "compr_new_inode"))
316 return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
317#endif
318
Daeho Jeong07c6b592021-07-09 22:53:57 -0700319 if (!strcmp(a->attr.name, "gc_segment_mode"))
320 return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
321
322 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
323 return sysfs_emit(buf, "%u\n",
324 sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
325 }
326
Chao Yu8ceffcb2017-06-14 17:39:47 +0800327 ui = (unsigned int *)(ptr + a->offset);
328
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800329 return sprintf(buf, "%u\n", *ui);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800330}
331
Chao Yu4d57b862018-05-30 00:20:41 +0800332static ssize_t __sbi_store(struct f2fs_attr *a,
Chao Yu8ceffcb2017-06-14 17:39:47 +0800333 struct f2fs_sb_info *sbi,
334 const char *buf, size_t count)
335{
336 unsigned char *ptr;
337 unsigned long t;
338 unsigned int *ui;
339 ssize_t ret;
340
341 ptr = __struct_ptr(sbi, a->struct_type);
342 if (!ptr)
343 return -EINVAL;
344
Chao Yu846ae672018-02-26 22:04:13 +0800345 if (!strcmp(a->attr.name, "extension_list")) {
346 const char *name = strim((char *)buf);
Chao Yub6a06cb2018-02-28 17:07:27 +0800347 bool set = true, hot;
Chao Yu846ae672018-02-26 22:04:13 +0800348
Chao Yub6a06cb2018-02-28 17:07:27 +0800349 if (!strncmp(name, "[h]", 3))
350 hot = true;
351 else if (!strncmp(name, "[c]", 3))
352 hot = false;
353 else
354 return -EINVAL;
355
356 name += 3;
357
358 if (*name == '!') {
Chao Yu846ae672018-02-26 22:04:13 +0800359 name++;
360 set = false;
361 }
362
Wang Xiaojun5417c982021-07-09 16:34:53 +0800363 if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
Chao Yu846ae672018-02-26 22:04:13 +0800364 return -EINVAL;
365
366 down_write(&sbi->sb_lock);
367
Chao Yu4d57b862018-05-30 00:20:41 +0800368 ret = f2fs_update_extension_list(sbi, name, hot, set);
Chao Yu846ae672018-02-26 22:04:13 +0800369 if (ret)
370 goto out;
371
372 ret = f2fs_commit_super(sbi, false);
373 if (ret)
Chao Yu4d57b862018-05-30 00:20:41 +0800374 f2fs_update_extension_list(sbi, name, hot, !set);
Chao Yu846ae672018-02-26 22:04:13 +0800375out:
376 up_write(&sbi->sb_lock);
377 return ret ? ret : count;
378 }
379
Daeho Jeonge6592062021-01-21 22:45:29 +0900380 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
381 const char *name = strim((char *)buf);
382 struct ckpt_req_control *cprc = &sbi->cprc_info;
383 int class;
384 long data;
385 int ret;
386
387 if (!strncmp(name, "rt,", 3))
388 class = IOPRIO_CLASS_RT;
389 else if (!strncmp(name, "be,", 3))
390 class = IOPRIO_CLASS_BE;
391 else
392 return -EINVAL;
393
394 name += 3;
395 ret = kstrtol(name, 10, &data);
396 if (ret)
397 return ret;
Damien Le Moal202bc942021-08-11 12:37:01 +0900398 if (data >= IOPRIO_NR_LEVELS || data < 0)
Daeho Jeonge6592062021-01-21 22:45:29 +0900399 return -EINVAL;
400
401 cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
402 if (test_opt(sbi, MERGE_CHECKPOINT)) {
403 ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
404 cprc->ckpt_thread_ioprio);
405 if (ret)
406 return ret;
407 }
408
409 return count;
410 }
411
Chao Yu8ceffcb2017-06-14 17:39:47 +0800412 ui = (unsigned int *)(ptr + a->offset);
413
414 ret = kstrtoul(skip_spaces(buf), 0, &t);
415 if (ret < 0)
416 return ret;
417#ifdef CONFIG_F2FS_FAULT_INJECTION
418 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
419 return -EINVAL;
Chao Yu36c57332019-01-04 17:39:53 +0800420 if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
421 return -EINVAL;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800422#endif
Chao Yudaeb4332017-06-26 16:24:41 +0800423 if (a->struct_type == RESERVED_BLOCKS) {
424 spin_lock(&sbi->stat_lock);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800425 if (t > (unsigned long)(sbi->user_block_count -
Chao Yu300a84292021-12-11 21:27:36 +0800426 F2FS_OPTION(sbi).root_reserved_blocks -
427 sbi->blocks_per_seg *
428 SM_I(sbi)->additional_reserved_segments)) {
Chao Yudaeb4332017-06-26 16:24:41 +0800429 spin_unlock(&sbi->stat_lock);
430 return -EINVAL;
431 }
432 *ui = t;
Yunlong Song80d42142017-10-27 20:45:05 +0800433 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
434 sbi->user_block_count - valid_user_blocks(sbi));
Chao Yudaeb4332017-06-26 16:24:41 +0800435 spin_unlock(&sbi->stat_lock);
436 return count;
437 }
Chao Yu969d1b12017-08-07 23:09:56 +0800438
439 if (!strcmp(a->attr.name, "discard_granularity")) {
Chao Yu969d1b12017-08-07 23:09:56 +0800440 if (t == 0 || t > MAX_PLIST_NUM)
441 return -EINVAL;
Chao Yu4f993262021-08-03 08:15:43 +0800442 if (!f2fs_block_unit_discard(sbi))
443 return -EINVAL;
Chao Yu969d1b12017-08-07 23:09:56 +0800444 if (t == *ui)
445 return count;
Chao Yu80647e52017-09-12 14:25:35 +0800446 *ui = t;
Chao Yu969d1b12017-08-07 23:09:56 +0800447 return count;
448 }
449
Chao Yu34655572018-10-25 16:19:28 +0800450 if (!strcmp(a->attr.name, "migration_granularity")) {
451 if (t == 0 || t > sbi->segs_per_sec)
452 return -EINVAL;
453 }
454
Chao Yu377224c2018-04-09 10:25:23 +0800455 if (!strcmp(a->attr.name, "trim_sections"))
456 return -EINVAL;
457
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700458 if (!strcmp(a->attr.name, "gc_urgent")) {
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900459 if (t == 0) {
460 sbi->gc_mode = GC_NORMAL;
461 } else if (t == 1) {
462 sbi->gc_mode = GC_URGENT_HIGH;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700463 if (sbi->gc_thread) {
Sheng Yonga690eff2018-08-05 12:45:35 +0800464 sbi->gc_thread->gc_wake = 1;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700465 wake_up_interruptible_all(
466 &sbi->gc_thread->gc_wait_queue_head);
467 wake_up_discard_thread(sbi, true);
468 }
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900469 } else if (t == 2) {
470 sbi->gc_mode = GC_URGENT_LOW;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700471 } else {
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900472 return -EINVAL;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700473 }
474 return count;
475 }
476 if (!strcmp(a->attr.name, "gc_idle")) {
Chao Yu093749e2020-08-04 21:14:49 +0800477 if (t == GC_IDLE_CB) {
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700478 sbi->gc_mode = GC_IDLE_CB;
Chao Yu093749e2020-08-04 21:14:49 +0800479 } else if (t == GC_IDLE_GREEDY) {
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700480 sbi->gc_mode = GC_IDLE_GREEDY;
Chao Yu093749e2020-08-04 21:14:49 +0800481 } else if (t == GC_IDLE_AT) {
482 if (!sbi->am.atgc_enabled)
483 return -EINVAL;
484 sbi->gc_mode = GC_AT;
485 } else {
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700486 sbi->gc_mode = GC_NORMAL;
Chao Yu093749e2020-08-04 21:14:49 +0800487 }
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700488 return count;
489 }
490
Daeho Jeong325163e2021-12-08 16:41:51 -0800491 if (!strcmp(a->attr.name, "gc_urgent_high_remaining")) {
492 spin_lock(&sbi->gc_urgent_high_lock);
Yang Lid361b692021-12-15 10:38:58 +0800493 sbi->gc_urgent_high_limited = t != 0;
Daeho Jeong325163e2021-12-08 16:41:51 -0800494 sbi->gc_urgent_high_remaining = t;
495 spin_unlock(&sbi->gc_urgent_high_lock);
496
497 return count;
498 }
499
Daeho Jeong52118742021-08-19 20:52:28 -0700500#ifdef CONFIG_F2FS_IOSTAT
Sheng Yongac929852019-01-15 20:02:15 +0000501 if (!strcmp(a->attr.name, "iostat_enable")) {
502 sbi->iostat_enable = !!t;
503 if (!sbi->iostat_enable)
504 f2fs_reset_iostat(sbi);
505 return count;
506 }
507
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000508 if (!strcmp(a->attr.name, "iostat_period_ms")) {
509 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
510 return -EINVAL;
511 spin_lock(&sbi->iostat_lock);
512 sbi->iostat_period_ms = (unsigned int)t;
513 spin_unlock(&sbi->iostat_lock);
514 return count;
515 }
Daeho Jeong52118742021-08-19 20:52:28 -0700516#endif
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000517
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900518#ifdef CONFIG_F2FS_FS_COMPRESSION
519 if (!strcmp(a->attr.name, "compr_written_block") ||
520 !strcmp(a->attr.name, "compr_saved_block")) {
521 if (t != 0)
522 return -EINVAL;
523 sbi->compr_written_block = 0;
524 sbi->compr_saved_block = 0;
525 return count;
526 }
527
528 if (!strcmp(a->attr.name, "compr_new_inode")) {
529 if (t != 0)
530 return -EINVAL;
531 sbi->compr_new_inode = 0;
532 return count;
533 }
534#endif
535
Chao Yu8939a8482021-05-12 10:07:19 +0800536 if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
537 if (t > 100)
538 return -EINVAL;
539 sbi->am.candidate_ratio = t;
540 return count;
541 }
542
543 if (!strcmp(a->attr.name, "atgc_age_weight")) {
544 if (t > 100)
545 return -EINVAL;
546 sbi->am.age_weight = t;
547 return count;
548 }
549
Daeho Jeong07c6b592021-07-09 22:53:57 -0700550 if (!strcmp(a->attr.name, "gc_segment_mode")) {
551 if (t < MAX_GC_MODE)
552 sbi->gc_segment_mode = t;
553 else
554 return -EINVAL;
555 return count;
556 }
557
558 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
559 if (t != 0)
560 return -EINVAL;
561 sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
562 return count;
563 }
564
Daeho Jeong0f6b56e2021-08-02 21:22:45 -0700565 if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
566 if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
567 sbi->seq_file_ra_mul = t;
568 else
569 return -EINVAL;
570 return count;
571 }
572
Daeho Jeong6691d942021-09-29 11:12:03 -0700573 if (!strcmp(a->attr.name, "max_fragment_chunk")) {
574 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
575 sbi->max_fragment_chunk = t;
576 else
577 return -EINVAL;
578 return count;
579 }
580
581 if (!strcmp(a->attr.name, "max_fragment_hole")) {
582 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
583 sbi->max_fragment_hole = t;
584 else
585 return -EINVAL;
586 return count;
587 }
588
Sheng Yongac929852019-01-15 20:02:15 +0000589 *ui = (unsigned int)t;
590
Chao Yu8ceffcb2017-06-14 17:39:47 +0800591 return count;
592}
593
Chao Yu250dbf52018-05-28 16:57:32 +0800594static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
595 struct f2fs_sb_info *sbi,
596 const char *buf, size_t count)
597{
598 ssize_t ret;
599 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
600 a->struct_type == GC_THREAD);
601
Jaegeuk Kima1933c02018-07-15 09:58:08 +0900602 if (gc_entry) {
603 if (!down_read_trylock(&sbi->sb->s_umount))
604 return -EAGAIN;
605 }
Chao Yu4d57b862018-05-30 00:20:41 +0800606 ret = __sbi_store(a, sbi, buf, count);
Chao Yu250dbf52018-05-28 16:57:32 +0800607 if (gc_entry)
608 up_read(&sbi->sb->s_umount);
609
610 return ret;
611}
612
Chao Yu8ceffcb2017-06-14 17:39:47 +0800613static ssize_t f2fs_attr_show(struct kobject *kobj,
614 struct attribute *attr, char *buf)
615{
616 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
617 s_kobj);
618 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
619
620 return a->show ? a->show(a, sbi, buf) : 0;
621}
622
623static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
624 const char *buf, size_t len)
625{
626 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
627 s_kobj);
628 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
629
630 return a->store ? a->store(a, sbi, buf, len) : 0;
631}
632
633static void f2fs_sb_release(struct kobject *kobj)
634{
635 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
636 s_kobj);
637 complete(&sbi->s_kobj_unregister);
638}
639
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700640/*
641 * Note that there are three feature list entries:
642 * 1) /sys/fs/f2fs/features
643 * : shows runtime features supported by in-kernel f2fs along with Kconfig.
644 * - ref. F2FS_FEATURE_RO_ATTR()
645 *
646 * 2) /sys/fs/f2fs/$s_id/features <deprecated>
647 * : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
648 * won't add new feature anymore, and thus, users should check entries in 3)
649 * instead of this 2).
650 *
651 * 3) /sys/fs/f2fs/$s_id/feature_list
652 * : shows on-disk features enabled by mkfs.f2fs per instance, which follows
653 * sysfs entry rule where each entry should expose single value.
654 * This list covers old feature list provided by 2) and beyond. Therefore,
655 * please add new on-disk feature in this list only.
656 * - ref. F2FS_SB_FEATURE_RO_ATTR()
657 */
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700658static ssize_t f2fs_feature_show(struct f2fs_attr *a,
659 struct f2fs_sb_info *sbi, char *buf)
660{
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700661 return sprintf(buf, "supported\n");
662}
663
664#define F2FS_FEATURE_RO_ATTR(_name) \
665static struct f2fs_attr f2fs_attr_##_name = { \
666 .attr = {.name = __stringify(_name), .mode = 0444 }, \
667 .show = f2fs_feature_show, \
668}
669
670static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
671 struct f2fs_sb_info *sbi, char *buf)
672{
673 if (F2FS_HAS_FEATURE(sbi, a->id))
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800674 return sprintf(buf, "supported\n");
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700675 return sprintf(buf, "unsupported\n");
676}
677
678#define F2FS_SB_FEATURE_RO_ATTR(_name, _feat) \
679static struct f2fs_attr f2fs_attr_sb_##_name = { \
680 .attr = {.name = __stringify(_name), .mode = 0444 }, \
681 .show = f2fs_sb_feature_show, \
682 .id = F2FS_FEATURE_##_feat, \
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700683}
684
Chao Yu8ceffcb2017-06-14 17:39:47 +0800685#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
686static struct f2fs_attr f2fs_attr_##_name = { \
687 .attr = {.name = __stringify(_name), .mode = _mode }, \
688 .show = _show, \
689 .store = _store, \
690 .struct_type = _struct_type, \
691 .offset = _offset \
692}
693
694#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
695 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
696 f2fs_sbi_show, f2fs_sbi_store, \
697 offsetof(struct struct_name, elname))
698
699#define F2FS_GENERAL_RO_ATTR(name) \
700static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
701
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800702#define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname) \
703static struct f2fs_attr f2fs_attr_##_name = { \
704 .attr = {.name = __stringify(_name), .mode = 0444 }, \
705 .show = f2fs_sbi_show, \
706 .struct_type = _struct_type, \
707 .offset = offsetof(struct _struct_name, _elname), \
708}
709
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700710F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
711 urgent_sleep_time);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800712F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
713F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
714F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700715F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
716F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800717F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
718F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
Chao Yu969d1b12017-08-07 23:09:56 +0800719F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
Chao Yudaeb4332017-06-26 16:24:41 +0800720F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800721F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
722F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
723F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
724F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
Jaegeuk Kim853137c2018-08-09 17:53:34 -0700725F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800726F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
Chao Yua2a12b62017-10-28 16:52:33 +0800727F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800728F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
729F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
730F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
731F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
Chao Yu34655572018-10-25 16:19:28 +0800732F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800733F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
734F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
735F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +0530736F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
737 interval_time[DISCARD_TIME]);
738F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
Jaegeuk Kim03f2c022019-01-14 10:42:11 -0800739F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
740 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
Daeho Jeong52118742021-08-19 20:52:28 -0700741#ifdef CONFIG_F2FS_IOSTAT
Chao Yub0af6d42017-08-02 23:21:48 +0800742F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000743F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
Daeho Jeong52118742021-08-19 20:52:28 -0700744#endif
Sheng Yongf6df8f22017-11-22 18:23:38 +0800745F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
Jaegeuk Kim10208562020-12-03 09:52:45 -0800746F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -0800747F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
Chao Yu846ae672018-02-26 22:04:13 +0800748F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800749#ifdef CONFIG_F2FS_FAULT_INJECTION
750F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
751F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
752#endif
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700753F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700754F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
Daeho Jeong325163e2021-12-08 16:41:51 -0800755F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent_high_remaining, gc_urgent_high_remaining);
Daeho Jeonge6592062021-01-21 22:45:29 +0900756F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +0200757F2FS_GENERAL_RO_ATTR(dirty_segments);
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800758F2FS_GENERAL_RO_ATTR(free_segments);
Jaegeuk Kim08234272021-03-01 17:28:16 -0800759F2FS_GENERAL_RO_ATTR(ovp_segments);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800760F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700761F2FS_GENERAL_RO_ATTR(features);
Yunlong Song80d42142017-10-27 20:45:05 +0800762F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700763F2FS_GENERAL_RO_ATTR(unusable);
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700764F2FS_GENERAL_RO_ATTR(encoding);
Jaegeuk Kima7e679b2020-02-25 19:08:16 -0800765F2FS_GENERAL_RO_ATTR(mounted_time_sec);
Dehe Guffcde4b2020-07-03 17:51:29 +0800766F2FS_GENERAL_RO_ATTR(main_blkaddr);
Jaegeuk Kimae2e2802021-11-29 10:36:12 -0800767F2FS_GENERAL_RO_ATTR(pending_discard);
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800768#ifdef CONFIG_F2FS_STAT_FS
769F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
770F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
771F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
772F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
773F2FS_GENERAL_RO_ATTR(moved_blocks_background);
774F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
775F2FS_GENERAL_RO_ATTR(avg_vblocks);
776#endif
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700777
Chandan Rajendra643fa962018-12-12 15:20:12 +0530778#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700779F2FS_FEATURE_RO_ATTR(encryption);
780F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000781#ifdef CONFIG_UNICODE
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700782F2FS_FEATURE_RO_ATTR(encrypted_casefold);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700783#endif
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000784#endif /* CONFIG_FS_ENCRYPTION */
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700785#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700786F2FS_FEATURE_RO_ATTR(block_zoned);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700787#endif
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700788F2FS_FEATURE_RO_ATTR(atomic_write);
789F2FS_FEATURE_RO_ATTR(extra_attr);
790F2FS_FEATURE_RO_ATTR(project_quota);
791F2FS_FEATURE_RO_ATTR(inode_checksum);
792F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
793F2FS_FEATURE_RO_ATTR(quota_ino);
794F2FS_FEATURE_RO_ATTR(inode_crtime);
795F2FS_FEATURE_RO_ATTR(lost_found);
Eric Biggers95ae2512019-07-22 09:26:24 -0700796#ifdef CONFIG_FS_VERITY
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700797F2FS_FEATURE_RO_ATTR(verity);
Eric Biggers95ae2512019-07-22 09:26:24 -0700798#endif
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700799F2FS_FEATURE_RO_ATTR(sb_checksum);
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000800#ifdef CONFIG_UNICODE
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700801F2FS_FEATURE_RO_ATTR(casefold);
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000802#endif
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700803F2FS_FEATURE_RO_ATTR(readonly);
Chao Yu9b6ed142020-03-27 18:29:00 +0800804#ifdef CONFIG_F2FS_FS_COMPRESSION
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700805F2FS_FEATURE_RO_ATTR(compression);
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900806F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_written_block, compr_written_block);
807F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_saved_block, compr_saved_block);
808F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_new_inode, compr_new_inode);
Chao Yu9b6ed142020-03-27 18:29:00 +0800809#endif
Jaegeuk Kim4a196df2021-06-03 22:30:09 -0700810F2FS_FEATURE_RO_ATTR(pin_file);
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -0700811
Chao Yu8939a8482021-05-12 10:07:19 +0800812/* For ATGC */
813F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_ratio, candidate_ratio);
814F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_count, max_candidate_count);
815F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_weight, age_weight);
816F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800817
Daeho Jeong0f6b56e2021-08-02 21:22:45 -0700818F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, seq_file_ra_mul, seq_file_ra_mul);
Daeho Jeong07c6b592021-07-09 22:53:57 -0700819F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
820F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
Daeho Jeong6691d942021-09-29 11:12:03 -0700821F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_chunk, max_fragment_chunk);
822F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_hole, max_fragment_hole);
Daeho Jeong07c6b592021-07-09 22:53:57 -0700823
Chao Yu8ceffcb2017-06-14 17:39:47 +0800824#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
825static struct attribute *f2fs_attrs[] = {
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700826 ATTR_LIST(gc_urgent_sleep_time),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800827 ATTR_LIST(gc_min_sleep_time),
828 ATTR_LIST(gc_max_sleep_time),
829 ATTR_LIST(gc_no_gc_sleep_time),
830 ATTR_LIST(gc_idle),
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700831 ATTR_LIST(gc_urgent),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800832 ATTR_LIST(reclaim_segments),
Jaegeuk Kima4db59a2019-11-22 11:53:10 -0800833 ATTR_LIST(main_blkaddr),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800834 ATTR_LIST(max_small_discards),
Chao Yu969d1b12017-08-07 23:09:56 +0800835 ATTR_LIST(discard_granularity),
Jaegeuk Kimae2e2802021-11-29 10:36:12 -0800836 ATTR_LIST(pending_discard),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800837 ATTR_LIST(batched_trim_sections),
838 ATTR_LIST(ipu_policy),
839 ATTR_LIST(min_ipu_util),
840 ATTR_LIST(min_fsync_blocks),
Jaegeuk Kim853137c2018-08-09 17:53:34 -0700841 ATTR_LIST(min_seq_blocks),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800842 ATTR_LIST(min_hot_blocks),
Chao Yua2a12b62017-10-28 16:52:33 +0800843 ATTR_LIST(min_ssr_sections),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800844 ATTR_LIST(max_victim_search),
Chao Yu34655572018-10-25 16:19:28 +0800845 ATTR_LIST(migration_granularity),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800846 ATTR_LIST(dir_level),
847 ATTR_LIST(ram_thresh),
848 ATTR_LIST(ra_nid_pages),
849 ATTR_LIST(dirty_nats_ratio),
850 ATTR_LIST(cp_interval),
851 ATTR_LIST(idle_interval),
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +0530852 ATTR_LIST(discard_idle_interval),
853 ATTR_LIST(gc_idle_interval),
Jaegeuk Kim03f2c022019-01-14 10:42:11 -0800854 ATTR_LIST(umount_discard_timeout),
Daeho Jeong52118742021-08-19 20:52:28 -0700855#ifdef CONFIG_F2FS_IOSTAT
Chao Yub0af6d42017-08-02 23:21:48 +0800856 ATTR_LIST(iostat_enable),
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000857 ATTR_LIST(iostat_period_ms),
Daeho Jeong52118742021-08-19 20:52:28 -0700858#endif
Sheng Yongf6df8f22017-11-22 18:23:38 +0800859 ATTR_LIST(readdir_ra),
Jaegeuk Kim10208562020-12-03 09:52:45 -0800860 ATTR_LIST(max_io_bytes),
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -0800861 ATTR_LIST(gc_pin_file_thresh),
Chao Yu846ae672018-02-26 22:04:13 +0800862 ATTR_LIST(extension_list),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800863#ifdef CONFIG_F2FS_FAULT_INJECTION
864 ATTR_LIST(inject_rate),
865 ATTR_LIST(inject_type),
866#endif
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700867 ATTR_LIST(data_io_flag),
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700868 ATTR_LIST(node_io_flag),
Daeho Jeong325163e2021-12-08 16:41:51 -0800869 ATTR_LIST(gc_urgent_high_remaining),
Daeho Jeonge6592062021-01-21 22:45:29 +0900870 ATTR_LIST(ckpt_thread_ioprio),
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +0200871 ATTR_LIST(dirty_segments),
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800872 ATTR_LIST(free_segments),
Jaegeuk Kim08234272021-03-01 17:28:16 -0800873 ATTR_LIST(ovp_segments),
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700874 ATTR_LIST(unusable),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800875 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700876 ATTR_LIST(features),
Chao Yudaeb4332017-06-26 16:24:41 +0800877 ATTR_LIST(reserved_blocks),
Yunlong Song80d42142017-10-27 20:45:05 +0800878 ATTR_LIST(current_reserved_blocks),
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700879 ATTR_LIST(encoding),
Jaegeuk Kima7e679b2020-02-25 19:08:16 -0800880 ATTR_LIST(mounted_time_sec),
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800881#ifdef CONFIG_F2FS_STAT_FS
882 ATTR_LIST(cp_foreground_calls),
883 ATTR_LIST(cp_background_calls),
884 ATTR_LIST(gc_foreground_calls),
885 ATTR_LIST(gc_background_calls),
886 ATTR_LIST(moved_blocks_foreground),
887 ATTR_LIST(moved_blocks_background),
888 ATTR_LIST(avg_vblocks),
889#endif
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900890#ifdef CONFIG_F2FS_FS_COMPRESSION
891 ATTR_LIST(compr_written_block),
892 ATTR_LIST(compr_saved_block),
893 ATTR_LIST(compr_new_inode),
894#endif
Chao Yu8939a8482021-05-12 10:07:19 +0800895 /* For ATGC */
896 ATTR_LIST(atgc_candidate_ratio),
897 ATTR_LIST(atgc_candidate_count),
898 ATTR_LIST(atgc_age_weight),
899 ATTR_LIST(atgc_age_threshold),
Daeho Jeong0f6b56e2021-08-02 21:22:45 -0700900 ATTR_LIST(seq_file_ra_mul),
Daeho Jeong07c6b592021-07-09 22:53:57 -0700901 ATTR_LIST(gc_segment_mode),
902 ATTR_LIST(gc_reclaimed_segments),
Daeho Jeong6691d942021-09-29 11:12:03 -0700903 ATTR_LIST(max_fragment_chunk),
904 ATTR_LIST(max_fragment_hole),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800905 NULL,
906};
Kimberly Brown176ef3c2019-06-07 13:40:41 -0400907ATTRIBUTE_GROUPS(f2fs);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800908
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700909static struct attribute *f2fs_feat_attrs[] = {
Chandan Rajendra643fa962018-12-12 15:20:12 +0530910#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700911 ATTR_LIST(encryption),
Eric Biggersed318a62020-05-12 16:32:50 -0700912 ATTR_LIST(test_dummy_encryption_v2),
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000913#ifdef CONFIG_UNICODE
914 ATTR_LIST(encrypted_casefold),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700915#endif
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000916#endif /* CONFIG_FS_ENCRYPTION */
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700917#ifdef CONFIG_BLK_DEV_ZONED
918 ATTR_LIST(block_zoned),
919#endif
920 ATTR_LIST(atomic_write),
921 ATTR_LIST(extra_attr),
922 ATTR_LIST(project_quota),
923 ATTR_LIST(inode_checksum),
Chao Yu6afc6622017-09-06 21:59:50 +0800924 ATTR_LIST(flexible_inline_xattr),
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700925 ATTR_LIST(quota_ino),
Chao Yu1c1d35d2018-01-25 14:54:42 +0800926 ATTR_LIST(inode_crtime),
Sheng Yongb7c409d2018-03-15 18:51:41 +0800927 ATTR_LIST(lost_found),
Eric Biggers95ae2512019-07-22 09:26:24 -0700928#ifdef CONFIG_FS_VERITY
929 ATTR_LIST(verity),
930#endif
Junling Zhengd440c522018-09-28 20:25:56 +0800931 ATTR_LIST(sb_checksum),
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000932#ifdef CONFIG_UNICODE
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700933 ATTR_LIST(casefold),
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000934#endif
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -0700935 ATTR_LIST(readonly),
Chao Yu9b6ed142020-03-27 18:29:00 +0800936#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800937 ATTR_LIST(compression),
Chao Yu9b6ed142020-03-27 18:29:00 +0800938#endif
Jaegeuk Kim4a196df2021-06-03 22:30:09 -0700939 ATTR_LIST(pin_file),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700940 NULL,
941};
Kimberly Brown176ef3c2019-06-07 13:40:41 -0400942ATTRIBUTE_GROUPS(f2fs_feat);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700943
Chao Yu0bfe9f72021-01-14 09:41:27 +0800944F2FS_GENERAL_RO_ATTR(sb_status);
Chao Yu5d4daa52020-12-09 16:43:27 +0800945static struct attribute *f2fs_stat_attrs[] = {
Chao Yu0bfe9f72021-01-14 09:41:27 +0800946 ATTR_LIST(sb_status),
Chao Yu5d4daa52020-12-09 16:43:27 +0800947 NULL,
948};
949ATTRIBUTE_GROUPS(f2fs_stat);
950
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700951F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
952F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
953F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
954F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
955F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
956F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
957F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
958F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
959F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
960F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
961F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
962F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
963F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
964F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
965
966static struct attribute *f2fs_sb_feat_attrs[] = {
967 ATTR_LIST(sb_encryption),
968 ATTR_LIST(sb_block_zoned),
969 ATTR_LIST(sb_extra_attr),
970 ATTR_LIST(sb_project_quota),
971 ATTR_LIST(sb_inode_checksum),
972 ATTR_LIST(sb_flexible_inline_xattr),
973 ATTR_LIST(sb_quota_ino),
974 ATTR_LIST(sb_inode_crtime),
975 ATTR_LIST(sb_lost_found),
976 ATTR_LIST(sb_verity),
977 ATTR_LIST(sb_sb_checksum),
978 ATTR_LIST(sb_casefold),
979 ATTR_LIST(sb_compression),
980 ATTR_LIST(sb_readonly),
981 NULL,
982};
983ATTRIBUTE_GROUPS(f2fs_sb_feat);
984
Chao Yu8ceffcb2017-06-14 17:39:47 +0800985static const struct sysfs_ops f2fs_attr_ops = {
986 .show = f2fs_attr_show,
987 .store = f2fs_attr_store,
988};
989
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700990static struct kobj_type f2fs_sb_ktype = {
Kimberly Brown176ef3c2019-06-07 13:40:41 -0400991 .default_groups = f2fs_groups,
Chao Yu8ceffcb2017-06-14 17:39:47 +0800992 .sysfs_ops = &f2fs_attr_ops,
993 .release = f2fs_sb_release,
994};
995
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700996static struct kobj_type f2fs_ktype = {
997 .sysfs_ops = &f2fs_attr_ops,
998};
999
1000static struct kset f2fs_kset = {
Jack Qiua87aff12020-07-24 16:55:28 +08001001 .kobj = {.ktype = &f2fs_ktype},
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001002};
1003
1004static struct kobj_type f2fs_feat_ktype = {
Kimberly Brown176ef3c2019-06-07 13:40:41 -04001005 .default_groups = f2fs_feat_groups,
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001006 .sysfs_ops = &f2fs_attr_ops,
1007};
1008
1009static struct kobject f2fs_feat = {
1010 .kset = &f2fs_kset,
1011};
1012
Chao Yu5d4daa52020-12-09 16:43:27 +08001013static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1014 struct attribute *attr, char *buf)
1015{
1016 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1017 s_stat_kobj);
1018 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1019
1020 return a->show ? a->show(a, sbi, buf) : 0;
1021}
1022
1023static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1024 const char *buf, size_t len)
1025{
1026 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1027 s_stat_kobj);
1028 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1029
1030 return a->store ? a->store(a, sbi, buf, len) : 0;
1031}
1032
1033static void f2fs_stat_kobj_release(struct kobject *kobj)
1034{
1035 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1036 s_stat_kobj);
1037 complete(&sbi->s_stat_kobj_unregister);
1038}
1039
1040static const struct sysfs_ops f2fs_stat_attr_ops = {
1041 .show = f2fs_stat_attr_show,
1042 .store = f2fs_stat_attr_store,
1043};
1044
1045static struct kobj_type f2fs_stat_ktype = {
1046 .default_groups = f2fs_stat_groups,
1047 .sysfs_ops = &f2fs_stat_attr_ops,
1048 .release = f2fs_stat_kobj_release,
1049};
1050
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001051static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1052 struct attribute *attr, char *buf)
1053{
1054 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1055 s_feature_list_kobj);
1056 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1057
1058 return a->show ? a->show(a, sbi, buf) : 0;
1059}
1060
1061static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1062{
1063 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1064 s_feature_list_kobj);
1065 complete(&sbi->s_feature_list_kobj_unregister);
1066}
1067
1068static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1069 .show = f2fs_sb_feat_attr_show,
1070};
1071
1072static struct kobj_type f2fs_feature_list_ktype = {
1073 .default_groups = f2fs_sb_feat_groups,
1074 .sysfs_ops = &f2fs_feature_list_attr_ops,
1075 .release = f2fs_feature_list_kobj_release,
1076};
1077
Randy Dunlapcb15d1e2018-07-06 20:50:57 -07001078static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1079 void *offset)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001080{
1081 struct super_block *sb = seq->private;
1082 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1083 unsigned int total_segs =
1084 le32_to_cpu(sbi->raw_super->segment_count_main);
1085 int i;
1086
1087 seq_puts(seq, "format: segment_type|valid_blocks\n"
1088 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1089
1090 for (i = 0; i < total_segs; i++) {
1091 struct seg_entry *se = get_seg_entry(sbi, i);
1092
1093 if ((i % 10) == 0)
1094 seq_printf(seq, "%-10d", i);
Chao Yu8740edc2019-06-18 18:00:09 +08001095 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001096 if ((i % 10) == 9 || i == (total_segs - 1))
1097 seq_putc(seq, '\n');
1098 else
1099 seq_putc(seq, ' ');
1100 }
1101
1102 return 0;
1103}
1104
Randy Dunlapcb15d1e2018-07-06 20:50:57 -07001105static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1106 void *offset)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001107{
1108 struct super_block *sb = seq->private;
1109 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1110 unsigned int total_segs =
1111 le32_to_cpu(sbi->raw_super->segment_count_main);
1112 int i, j;
1113
1114 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1115 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1116
1117 for (i = 0; i < total_segs; i++) {
1118 struct seg_entry *se = get_seg_entry(sbi, i);
1119
1120 seq_printf(seq, "%-10d", i);
Chao Yu8740edc2019-06-18 18:00:09 +08001121 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001122 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1123 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1124 seq_putc(seq, '\n');
1125 }
1126 return 0;
1127}
1128
Yunlong Song970e3482018-07-23 22:10:22 +08001129static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1130 void *offset)
1131{
1132 struct super_block *sb = seq->private;
1133 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1134 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1135 int i;
1136
1137 seq_puts(seq, "format: victim_secmap bitmaps\n");
1138
1139 for (i = 0; i < MAIN_SECS(sbi); i++) {
1140 if ((i % 10) == 0)
1141 seq_printf(seq, "%-10d", i);
1142 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1143 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1144 seq_putc(seq, '\n');
1145 else
1146 seq_putc(seq, ' ');
1147 }
1148 return 0;
1149}
1150
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001151int __init f2fs_init_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001152{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001153 int ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001154
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001155 kobject_set_name(&f2fs_kset.kobj, "f2fs");
1156 f2fs_kset.kobj.parent = fs_kobj;
1157 ret = kset_register(&f2fs_kset);
1158 if (ret)
1159 return ret;
1160
1161 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1162 NULL, "features");
Chao Yufe396ad2019-12-30 17:41:41 +08001163 if (ret) {
1164 kobject_put(&f2fs_feat);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001165 kset_unregister(&f2fs_kset);
Chao Yufe396ad2019-12-30 17:41:41 +08001166 } else {
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001167 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
Chao Yufe396ad2019-12-30 17:41:41 +08001168 }
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001169 return ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001170}
1171
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001172void f2fs_exit_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001173{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001174 kobject_put(&f2fs_feat);
1175 kset_unregister(&f2fs_kset);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001176 remove_proc_entry("fs/f2fs", NULL);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001177 f2fs_proc_root = NULL;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001178}
1179
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001180int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001181{
1182 struct super_block *sb = sbi->sb;
1183 int err;
1184
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001185 sbi->s_kobj.kset = &f2fs_kset;
1186 init_completion(&sbi->s_kobj_unregister);
1187 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1188 "%s", sb->s_id);
Chao Yu5d4daa52020-12-09 16:43:27 +08001189 if (err)
1190 goto put_sb_kobj;
1191
1192 sbi->s_stat_kobj.kset = &f2fs_kset;
1193 init_completion(&sbi->s_stat_kobj_unregister);
1194 err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1195 &sbi->s_kobj, "stat");
1196 if (err)
1197 goto put_stat_kobj;
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001198
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001199 sbi->s_feature_list_kobj.kset = &f2fs_kset;
1200 init_completion(&sbi->s_feature_list_kobj_unregister);
1201 err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1202 &f2fs_feature_list_ktype,
1203 &sbi->s_kobj, "feature_list");
1204 if (err)
1205 goto put_feature_list_kobj;
1206
Chao Yu8ceffcb2017-06-14 17:39:47 +08001207 if (f2fs_proc_root)
1208 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1209
1210 if (sbi->s_proc) {
Yangtao Li491f7f72021-08-14 18:37:01 +08001211 proc_create_single_data("segment_info", 0444, sbi->s_proc,
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001212 segment_info_seq_show, sb);
Yangtao Li491f7f72021-08-14 18:37:01 +08001213 proc_create_single_data("segment_bits", 0444, sbi->s_proc,
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001214 segment_bits_seq_show, sb);
Daeho Jeong52118742021-08-19 20:52:28 -07001215#ifdef CONFIG_F2FS_IOSTAT
Yangtao Li491f7f72021-08-14 18:37:01 +08001216 proc_create_single_data("iostat_info", 0444, sbi->s_proc,
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001217 iostat_info_seq_show, sb);
Daeho Jeong52118742021-08-19 20:52:28 -07001218#endif
Yangtao Li491f7f72021-08-14 18:37:01 +08001219 proc_create_single_data("victim_bits", 0444, sbi->s_proc,
Yunlong Song970e3482018-07-23 22:10:22 +08001220 victim_bits_seq_show, sb);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001221 }
Chao Yu8ceffcb2017-06-14 17:39:47 +08001222 return 0;
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001223put_feature_list_kobj:
1224 kobject_put(&sbi->s_feature_list_kobj);
1225 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
Chao Yu5d4daa52020-12-09 16:43:27 +08001226put_stat_kobj:
1227 kobject_put(&sbi->s_stat_kobj);
1228 wait_for_completion(&sbi->s_stat_kobj_unregister);
1229put_sb_kobj:
1230 kobject_put(&sbi->s_kobj);
1231 wait_for_completion(&sbi->s_kobj_unregister);
1232 return err;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001233}
1234
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001235void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001236{
Chao Yu8ceffcb2017-06-14 17:39:47 +08001237 if (sbi->s_proc) {
Daeho Jeong52118742021-08-19 20:52:28 -07001238#ifdef CONFIG_F2FS_IOSTAT
Chao Yub0af6d42017-08-02 23:21:48 +08001239 remove_proc_entry("iostat_info", sbi->s_proc);
Daeho Jeong52118742021-08-19 20:52:28 -07001240#endif
Chao Yu8ceffcb2017-06-14 17:39:47 +08001241 remove_proc_entry("segment_info", sbi->s_proc);
1242 remove_proc_entry("segment_bits", sbi->s_proc);
Yunlong Song970e3482018-07-23 22:10:22 +08001243 remove_proc_entry("victim_bits", sbi->s_proc);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001244 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1245 }
Chao Yu5d4daa52020-12-09 16:43:27 +08001246
1247 kobject_del(&sbi->s_stat_kobj);
1248 kobject_put(&sbi->s_stat_kobj);
1249 wait_for_completion(&sbi->s_stat_kobj_unregister);
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001250 kobject_del(&sbi->s_feature_list_kobj);
1251 kobject_put(&sbi->s_feature_list_kobj);
1252 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
Chao Yu5d4daa52020-12-09 16:43:27 +08001253
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001254 kobject_del(&sbi->s_kobj);
Jaegeuk Kim820d3662019-12-13 18:32:16 -08001255 kobject_put(&sbi->s_kobj);
Jamie Ilesae284d82020-10-12 14:09:48 +01001256 wait_for_completion(&sbi->s_kobj_unregister);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001257}