blob: 7d289249cd7ebd7917ed1a5d178665446ffca918 [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 Kimbf9e6972017-07-21 17:14:09 -0700121static ssize_t features_show(struct f2fs_attr *a,
122 struct f2fs_sb_info *sbi, char *buf)
123{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700124 int len = 0;
125
Chao Yu7beb01f2018-10-24 18:34:26 +0800126 if (f2fs_sb_has_encrypt(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100127 len += scnprintf(buf, PAGE_SIZE - len, "%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700128 "encryption");
Chao Yu7beb01f2018-10-24 18:34:26 +0800129 if (f2fs_sb_has_blkzoned(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100130 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700131 len ? ", " : "", "blkzoned");
Chao Yu7beb01f2018-10-24 18:34:26 +0800132 if (f2fs_sb_has_extra_attr(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100133 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700134 len ? ", " : "", "extra_attr");
Chao Yu7beb01f2018-10-24 18:34:26 +0800135 if (f2fs_sb_has_project_quota(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100136 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700137 len ? ", " : "", "projquota");
Chao Yu7beb01f2018-10-24 18:34:26 +0800138 if (f2fs_sb_has_inode_chksum(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 ? ", " : "", "inode_checksum");
Chao Yu7beb01f2018-10-24 18:34:26 +0800141 if (f2fs_sb_has_flexible_inline_xattr(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100142 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Chao Yu6afc6622017-09-06 21:59:50 +0800143 len ? ", " : "", "flexible_inline_xattr");
Chao Yu7beb01f2018-10-24 18:34:26 +0800144 if (f2fs_sb_has_quota_ino(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100145 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700146 len ? ", " : "", "quota_ino");
Chao Yu7beb01f2018-10-24 18:34:26 +0800147 if (f2fs_sb_has_inode_crtime(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100148 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Chao Yu1c1d35d2018-01-25 14:54:42 +0800149 len ? ", " : "", "inode_crtime");
Chao Yu7beb01f2018-10-24 18:34:26 +0800150 if (f2fs_sb_has_lost_found(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100151 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Sheng Yongb7c409d2018-03-15 18:51:41 +0800152 len ? ", " : "", "lost_found");
Eric Biggers95ae2512019-07-22 09:26:24 -0700153 if (f2fs_sb_has_verity(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100154 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Eric Biggers95ae2512019-07-22 09:26:24 -0700155 len ? ", " : "", "verity");
Chao Yu7beb01f2018-10-24 18:34:26 +0800156 if (f2fs_sb_has_sb_chksum(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100157 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Junling Zhengd440c522018-09-28 20:25:56 +0800158 len ? ", " : "", "sb_checksum");
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700159 if (f2fs_sb_has_casefold(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100160 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700161 len ? ", " : "", "casefold");
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -0700162 if (f2fs_sb_has_readonly(sbi))
163 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
164 len ? ", " : "", "readonly");
Chao Yu4c8ff702019-11-01 18:07:14 +0800165 if (f2fs_sb_has_compression(sbi))
Takashi Iwaic6d57892020-03-11 10:33:53 +0100166 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Chao Yu4c8ff702019-11-01 18:07:14 +0800167 len ? ", " : "", "compression");
Takashi Iwaic6d57892020-03-11 10:33:53 +0100168 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -0700169 len ? ", " : "", "pin_file");
Takashi Iwaic6d57892020-03-11 10:33:53 +0100170 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700171 return len;
172}
173
Yunlong Song80d42142017-10-27 20:45:05 +0800174static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
175 struct f2fs_sb_info *sbi, char *buf)
176{
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800177 return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
Yunlong Song80d42142017-10-27 20:45:05 +0800178}
179
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800180static ssize_t unusable_show(struct f2fs_attr *a,
181 struct f2fs_sb_info *sbi, char *buf)
182{
183 block_t unusable;
184
185 if (test_opt(sbi, DISABLE_CHECKPOINT))
186 unusable = sbi->unusable_block_count;
187 else
188 unusable = f2fs_get_unusable_blocks(sbi);
189 return sprintf(buf, "%llu\n", (unsigned long long)unusable);
190}
191
192static ssize_t encoding_show(struct f2fs_attr *a,
193 struct f2fs_sb_info *sbi, char *buf)
194{
195#ifdef CONFIG_UNICODE
Daniel Rosenbergeca48732020-07-08 02:12:36 -0700196 struct super_block *sb = sbi->sb;
197
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800198 if (f2fs_sb_has_casefold(sbi))
Qing Wang84eab2a2021-10-12 20:29:04 -0700199 return sysfs_emit(buf, "%s (%d.%d.%d)\n",
Daniel Rosenbergeca48732020-07-08 02:12:36 -0700200 sb->s_encoding->charset,
201 (sb->s_encoding->version >> 16) & 0xff,
202 (sb->s_encoding->version >> 8) & 0xff,
203 sb->s_encoding->version & 0xff);
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800204#endif
205 return sprintf(buf, "(none)");
206}
207
Jaegeuk Kima7e679b2020-02-25 19:08:16 -0800208static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
209 struct f2fs_sb_info *sbi, char *buf)
210{
211 return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
212}
213
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800214#ifdef CONFIG_F2FS_STAT_FS
215static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
216 struct f2fs_sb_info *sbi, char *buf)
217{
218 struct f2fs_stat_info *si = F2FS_STAT(sbi);
219
220 return sprintf(buf, "%llu\n",
221 (unsigned long long)(si->tot_blks -
222 (si->bg_data_blks + si->bg_node_blks)));
223}
224
225static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
226 struct f2fs_sb_info *sbi, char *buf)
227{
228 struct f2fs_stat_info *si = F2FS_STAT(sbi);
229
230 return sprintf(buf, "%llu\n",
231 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
232}
233
234static ssize_t avg_vblocks_show(struct f2fs_attr *a,
235 struct f2fs_sb_info *sbi, char *buf)
236{
237 struct f2fs_stat_info *si = F2FS_STAT(sbi);
238
239 si->dirty_count = dirty_segments(sbi);
240 f2fs_update_sit_info(sbi);
241 return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
242}
243#endif
244
Dehe Guffcde4b2020-07-03 17:51:29 +0800245static ssize_t main_blkaddr_show(struct f2fs_attr *a,
246 struct f2fs_sb_info *sbi, char *buf)
247{
Qing Wang84eab2a2021-10-12 20:29:04 -0700248 return sysfs_emit(buf, "%llu\n",
Dehe Guffcde4b2020-07-03 17:51:29 +0800249 (unsigned long long)MAIN_BLKADDR(sbi));
250}
251
Chao Yu8ceffcb2017-06-14 17:39:47 +0800252static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
253 struct f2fs_sb_info *sbi, char *buf)
254{
255 unsigned char *ptr = NULL;
256 unsigned int *ui;
257
258 ptr = __struct_ptr(sbi, a->struct_type);
259 if (!ptr)
260 return -EINVAL;
261
Chao Yu846ae672018-02-26 22:04:13 +0800262 if (!strcmp(a->attr.name, "extension_list")) {
263 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
264 sbi->raw_super->extension_list;
Chao Yub6a06cb2018-02-28 17:07:27 +0800265 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
266 int hot_count = sbi->raw_super->hot_ext_count;
Chao Yu846ae672018-02-26 22:04:13 +0800267 int len = 0, i;
268
Takashi Iwaic6d57892020-03-11 10:33:53 +0100269 len += scnprintf(buf + len, PAGE_SIZE - len,
Colin Ian King45800382018-04-30 16:27:44 +0100270 "cold file extension:\n");
Chao Yub6a06cb2018-02-28 17:07:27 +0800271 for (i = 0; i < cold_count; i++)
Takashi Iwaic6d57892020-03-11 10:33:53 +0100272 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
Chao Yub6a06cb2018-02-28 17:07:27 +0800273 extlist[i]);
274
Takashi Iwaic6d57892020-03-11 10:33:53 +0100275 len += scnprintf(buf + len, PAGE_SIZE - len,
Colin Ian King45800382018-04-30 16:27:44 +0100276 "hot file extension:\n");
Chao Yub6a06cb2018-02-28 17:07:27 +0800277 for (i = cold_count; i < cold_count + hot_count; i++)
Takashi Iwaic6d57892020-03-11 10:33:53 +0100278 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
Chao Yu846ae672018-02-26 22:04:13 +0800279 extlist[i]);
280 return len;
281 }
282
Daeho Jeonge6592062021-01-21 22:45:29 +0900283 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
284 struct ckpt_req_control *cprc = &sbi->cprc_info;
285 int len = 0;
286 int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
287 int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
288
289 if (class == IOPRIO_CLASS_RT)
290 len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
291 else if (class == IOPRIO_CLASS_BE)
292 len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
293 else
294 return -EINVAL;
295
296 len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
297 return len;
298 }
299
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900300#ifdef CONFIG_F2FS_FS_COMPRESSION
301 if (!strcmp(a->attr.name, "compr_written_block"))
302 return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
303
304 if (!strcmp(a->attr.name, "compr_saved_block"))
305 return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
306
307 if (!strcmp(a->attr.name, "compr_new_inode"))
308 return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
309#endif
310
Daeho Jeong07c6b592021-07-09 22:53:57 -0700311 if (!strcmp(a->attr.name, "gc_segment_mode"))
312 return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
313
314 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
315 return sysfs_emit(buf, "%u\n",
316 sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
317 }
318
Chao Yu8ceffcb2017-06-14 17:39:47 +0800319 ui = (unsigned int *)(ptr + a->offset);
320
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800321 return sprintf(buf, "%u\n", *ui);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800322}
323
Chao Yu4d57b862018-05-30 00:20:41 +0800324static ssize_t __sbi_store(struct f2fs_attr *a,
Chao Yu8ceffcb2017-06-14 17:39:47 +0800325 struct f2fs_sb_info *sbi,
326 const char *buf, size_t count)
327{
328 unsigned char *ptr;
329 unsigned long t;
330 unsigned int *ui;
331 ssize_t ret;
332
333 ptr = __struct_ptr(sbi, a->struct_type);
334 if (!ptr)
335 return -EINVAL;
336
Chao Yu846ae672018-02-26 22:04:13 +0800337 if (!strcmp(a->attr.name, "extension_list")) {
338 const char *name = strim((char *)buf);
Chao Yub6a06cb2018-02-28 17:07:27 +0800339 bool set = true, hot;
Chao Yu846ae672018-02-26 22:04:13 +0800340
Chao Yub6a06cb2018-02-28 17:07:27 +0800341 if (!strncmp(name, "[h]", 3))
342 hot = true;
343 else if (!strncmp(name, "[c]", 3))
344 hot = false;
345 else
346 return -EINVAL;
347
348 name += 3;
349
350 if (*name == '!') {
Chao Yu846ae672018-02-26 22:04:13 +0800351 name++;
352 set = false;
353 }
354
Wang Xiaojun5417c982021-07-09 16:34:53 +0800355 if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
Chao Yu846ae672018-02-26 22:04:13 +0800356 return -EINVAL;
357
358 down_write(&sbi->sb_lock);
359
Chao Yu4d57b862018-05-30 00:20:41 +0800360 ret = f2fs_update_extension_list(sbi, name, hot, set);
Chao Yu846ae672018-02-26 22:04:13 +0800361 if (ret)
362 goto out;
363
364 ret = f2fs_commit_super(sbi, false);
365 if (ret)
Chao Yu4d57b862018-05-30 00:20:41 +0800366 f2fs_update_extension_list(sbi, name, hot, !set);
Chao Yu846ae672018-02-26 22:04:13 +0800367out:
368 up_write(&sbi->sb_lock);
369 return ret ? ret : count;
370 }
371
Daeho Jeonge6592062021-01-21 22:45:29 +0900372 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
373 const char *name = strim((char *)buf);
374 struct ckpt_req_control *cprc = &sbi->cprc_info;
375 int class;
376 long data;
377 int ret;
378
379 if (!strncmp(name, "rt,", 3))
380 class = IOPRIO_CLASS_RT;
381 else if (!strncmp(name, "be,", 3))
382 class = IOPRIO_CLASS_BE;
383 else
384 return -EINVAL;
385
386 name += 3;
387 ret = kstrtol(name, 10, &data);
388 if (ret)
389 return ret;
Damien Le Moal202bc942021-08-11 12:37:01 +0900390 if (data >= IOPRIO_NR_LEVELS || data < 0)
Daeho Jeonge6592062021-01-21 22:45:29 +0900391 return -EINVAL;
392
393 cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
394 if (test_opt(sbi, MERGE_CHECKPOINT)) {
395 ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
396 cprc->ckpt_thread_ioprio);
397 if (ret)
398 return ret;
399 }
400
401 return count;
402 }
403
Chao Yu8ceffcb2017-06-14 17:39:47 +0800404 ui = (unsigned int *)(ptr + a->offset);
405
406 ret = kstrtoul(skip_spaces(buf), 0, &t);
407 if (ret < 0)
408 return ret;
409#ifdef CONFIG_F2FS_FAULT_INJECTION
410 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
411 return -EINVAL;
Chao Yu36c57332019-01-04 17:39:53 +0800412 if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
413 return -EINVAL;
Chao Yu8ceffcb2017-06-14 17:39:47 +0800414#endif
Chao Yudaeb4332017-06-26 16:24:41 +0800415 if (a->struct_type == RESERVED_BLOCKS) {
416 spin_lock(&sbi->stat_lock);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800417 if (t > (unsigned long)(sbi->user_block_count -
Chao Yu63189b72018-03-08 14:22:56 +0800418 F2FS_OPTION(sbi).root_reserved_blocks)) {
Chao Yudaeb4332017-06-26 16:24:41 +0800419 spin_unlock(&sbi->stat_lock);
420 return -EINVAL;
421 }
422 *ui = t;
Yunlong Song80d42142017-10-27 20:45:05 +0800423 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
424 sbi->user_block_count - valid_user_blocks(sbi));
Chao Yudaeb4332017-06-26 16:24:41 +0800425 spin_unlock(&sbi->stat_lock);
426 return count;
427 }
Chao Yu969d1b12017-08-07 23:09:56 +0800428
429 if (!strcmp(a->attr.name, "discard_granularity")) {
Chao Yu969d1b12017-08-07 23:09:56 +0800430 if (t == 0 || t > MAX_PLIST_NUM)
431 return -EINVAL;
Chao Yu4f993262021-08-03 08:15:43 +0800432 if (!f2fs_block_unit_discard(sbi))
433 return -EINVAL;
Chao Yu969d1b12017-08-07 23:09:56 +0800434 if (t == *ui)
435 return count;
Chao Yu80647e52017-09-12 14:25:35 +0800436 *ui = t;
Chao Yu969d1b12017-08-07 23:09:56 +0800437 return count;
438 }
439
Chao Yu34655572018-10-25 16:19:28 +0800440 if (!strcmp(a->attr.name, "migration_granularity")) {
441 if (t == 0 || t > sbi->segs_per_sec)
442 return -EINVAL;
443 }
444
Chao Yu377224c2018-04-09 10:25:23 +0800445 if (!strcmp(a->attr.name, "trim_sections"))
446 return -EINVAL;
447
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700448 if (!strcmp(a->attr.name, "gc_urgent")) {
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900449 if (t == 0) {
450 sbi->gc_mode = GC_NORMAL;
451 } else if (t == 1) {
452 sbi->gc_mode = GC_URGENT_HIGH;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700453 if (sbi->gc_thread) {
Sheng Yonga690eff2018-08-05 12:45:35 +0800454 sbi->gc_thread->gc_wake = 1;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700455 wake_up_interruptible_all(
456 &sbi->gc_thread->gc_wait_queue_head);
457 wake_up_discard_thread(sbi, true);
458 }
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900459 } else if (t == 2) {
460 sbi->gc_mode = GC_URGENT_LOW;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700461 } else {
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900462 return -EINVAL;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700463 }
464 return count;
465 }
466 if (!strcmp(a->attr.name, "gc_idle")) {
Chao Yu093749e2020-08-04 21:14:49 +0800467 if (t == GC_IDLE_CB) {
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700468 sbi->gc_mode = GC_IDLE_CB;
Chao Yu093749e2020-08-04 21:14:49 +0800469 } else if (t == GC_IDLE_GREEDY) {
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700470 sbi->gc_mode = GC_IDLE_GREEDY;
Chao Yu093749e2020-08-04 21:14:49 +0800471 } else if (t == GC_IDLE_AT) {
472 if (!sbi->am.atgc_enabled)
473 return -EINVAL;
474 sbi->gc_mode = GC_AT;
475 } else {
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700476 sbi->gc_mode = GC_NORMAL;
Chao Yu093749e2020-08-04 21:14:49 +0800477 }
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700478 return count;
479 }
480
Daeho Jeong52118742021-08-19 20:52:28 -0700481#ifdef CONFIG_F2FS_IOSTAT
Sheng Yongac929852019-01-15 20:02:15 +0000482 if (!strcmp(a->attr.name, "iostat_enable")) {
483 sbi->iostat_enable = !!t;
484 if (!sbi->iostat_enable)
485 f2fs_reset_iostat(sbi);
486 return count;
487 }
488
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000489 if (!strcmp(a->attr.name, "iostat_period_ms")) {
490 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
491 return -EINVAL;
492 spin_lock(&sbi->iostat_lock);
493 sbi->iostat_period_ms = (unsigned int)t;
494 spin_unlock(&sbi->iostat_lock);
495 return count;
496 }
Daeho Jeong52118742021-08-19 20:52:28 -0700497#endif
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000498
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900499#ifdef CONFIG_F2FS_FS_COMPRESSION
500 if (!strcmp(a->attr.name, "compr_written_block") ||
501 !strcmp(a->attr.name, "compr_saved_block")) {
502 if (t != 0)
503 return -EINVAL;
504 sbi->compr_written_block = 0;
505 sbi->compr_saved_block = 0;
506 return count;
507 }
508
509 if (!strcmp(a->attr.name, "compr_new_inode")) {
510 if (t != 0)
511 return -EINVAL;
512 sbi->compr_new_inode = 0;
513 return count;
514 }
515#endif
516
Chao Yu8939a8482021-05-12 10:07:19 +0800517 if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
518 if (t > 100)
519 return -EINVAL;
520 sbi->am.candidate_ratio = t;
521 return count;
522 }
523
524 if (!strcmp(a->attr.name, "atgc_age_weight")) {
525 if (t > 100)
526 return -EINVAL;
527 sbi->am.age_weight = t;
528 return count;
529 }
530
Daeho Jeong07c6b592021-07-09 22:53:57 -0700531 if (!strcmp(a->attr.name, "gc_segment_mode")) {
532 if (t < MAX_GC_MODE)
533 sbi->gc_segment_mode = t;
534 else
535 return -EINVAL;
536 return count;
537 }
538
539 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
540 if (t != 0)
541 return -EINVAL;
542 sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
543 return count;
544 }
545
Daeho Jeong0f6b56e2021-08-02 21:22:45 -0700546 if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
547 if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
548 sbi->seq_file_ra_mul = t;
549 else
550 return -EINVAL;
551 return count;
552 }
553
Daeho Jeong6691d942021-09-29 11:12:03 -0700554 if (!strcmp(a->attr.name, "max_fragment_chunk")) {
555 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
556 sbi->max_fragment_chunk = t;
557 else
558 return -EINVAL;
559 return count;
560 }
561
562 if (!strcmp(a->attr.name, "max_fragment_hole")) {
563 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
564 sbi->max_fragment_hole = t;
565 else
566 return -EINVAL;
567 return count;
568 }
569
Sheng Yongac929852019-01-15 20:02:15 +0000570 *ui = (unsigned int)t;
571
Chao Yu8ceffcb2017-06-14 17:39:47 +0800572 return count;
573}
574
Chao Yu250dbf52018-05-28 16:57:32 +0800575static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
576 struct f2fs_sb_info *sbi,
577 const char *buf, size_t count)
578{
579 ssize_t ret;
580 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
581 a->struct_type == GC_THREAD);
582
Jaegeuk Kima1933c02018-07-15 09:58:08 +0900583 if (gc_entry) {
584 if (!down_read_trylock(&sbi->sb->s_umount))
585 return -EAGAIN;
586 }
Chao Yu4d57b862018-05-30 00:20:41 +0800587 ret = __sbi_store(a, sbi, buf, count);
Chao Yu250dbf52018-05-28 16:57:32 +0800588 if (gc_entry)
589 up_read(&sbi->sb->s_umount);
590
591 return ret;
592}
593
Chao Yu8ceffcb2017-06-14 17:39:47 +0800594static ssize_t f2fs_attr_show(struct kobject *kobj,
595 struct attribute *attr, char *buf)
596{
597 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
598 s_kobj);
599 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
600
601 return a->show ? a->show(a, sbi, buf) : 0;
602}
603
604static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
605 const char *buf, size_t len)
606{
607 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
608 s_kobj);
609 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
610
611 return a->store ? a->store(a, sbi, buf, len) : 0;
612}
613
614static void f2fs_sb_release(struct kobject *kobj)
615{
616 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
617 s_kobj);
618 complete(&sbi->s_kobj_unregister);
619}
620
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700621/*
622 * Note that there are three feature list entries:
623 * 1) /sys/fs/f2fs/features
624 * : shows runtime features supported by in-kernel f2fs along with Kconfig.
625 * - ref. F2FS_FEATURE_RO_ATTR()
626 *
627 * 2) /sys/fs/f2fs/$s_id/features <deprecated>
628 * : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
629 * won't add new feature anymore, and thus, users should check entries in 3)
630 * instead of this 2).
631 *
632 * 3) /sys/fs/f2fs/$s_id/feature_list
633 * : shows on-disk features enabled by mkfs.f2fs per instance, which follows
634 * sysfs entry rule where each entry should expose single value.
635 * This list covers old feature list provided by 2) and beyond. Therefore,
636 * please add new on-disk feature in this list only.
637 * - ref. F2FS_SB_FEATURE_RO_ATTR()
638 */
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700639static ssize_t f2fs_feature_show(struct f2fs_attr *a,
640 struct f2fs_sb_info *sbi, char *buf)
641{
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700642 return sprintf(buf, "supported\n");
643}
644
645#define F2FS_FEATURE_RO_ATTR(_name) \
646static struct f2fs_attr f2fs_attr_##_name = { \
647 .attr = {.name = __stringify(_name), .mode = 0444 }, \
648 .show = f2fs_feature_show, \
649}
650
651static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
652 struct f2fs_sb_info *sbi, char *buf)
653{
654 if (F2FS_HAS_FEATURE(sbi, a->id))
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800655 return sprintf(buf, "supported\n");
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700656 return sprintf(buf, "unsupported\n");
657}
658
659#define F2FS_SB_FEATURE_RO_ATTR(_name, _feat) \
660static struct f2fs_attr f2fs_attr_sb_##_name = { \
661 .attr = {.name = __stringify(_name), .mode = 0444 }, \
662 .show = f2fs_sb_feature_show, \
663 .id = F2FS_FEATURE_##_feat, \
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700664}
665
Chao Yu8ceffcb2017-06-14 17:39:47 +0800666#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
667static struct f2fs_attr f2fs_attr_##_name = { \
668 .attr = {.name = __stringify(_name), .mode = _mode }, \
669 .show = _show, \
670 .store = _store, \
671 .struct_type = _struct_type, \
672 .offset = _offset \
673}
674
675#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
676 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
677 f2fs_sbi_show, f2fs_sbi_store, \
678 offsetof(struct struct_name, elname))
679
680#define F2FS_GENERAL_RO_ATTR(name) \
681static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
682
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800683#define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname) \
684static struct f2fs_attr f2fs_attr_##_name = { \
685 .attr = {.name = __stringify(_name), .mode = 0444 }, \
686 .show = f2fs_sbi_show, \
687 .struct_type = _struct_type, \
688 .offset = offsetof(struct _struct_name, _elname), \
689}
690
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700691F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
692 urgent_sleep_time);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800693F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
694F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
695F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700696F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
697F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800698F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
699F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
Chao Yu969d1b12017-08-07 23:09:56 +0800700F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
Chao Yudaeb4332017-06-26 16:24:41 +0800701F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800702F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
703F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
704F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
705F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
Jaegeuk Kim853137c2018-08-09 17:53:34 -0700706F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800707F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
Chao Yua2a12b62017-10-28 16:52:33 +0800708F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800709F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
710F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
711F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
712F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
Chao Yu34655572018-10-25 16:19:28 +0800713F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800714F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
715F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
716F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +0530717F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
718 interval_time[DISCARD_TIME]);
719F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
Jaegeuk Kim03f2c022019-01-14 10:42:11 -0800720F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
721 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
Daeho Jeong52118742021-08-19 20:52:28 -0700722#ifdef CONFIG_F2FS_IOSTAT
Chao Yub0af6d42017-08-02 23:21:48 +0800723F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000724F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
Daeho Jeong52118742021-08-19 20:52:28 -0700725#endif
Sheng Yongf6df8f22017-11-22 18:23:38 +0800726F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
Jaegeuk Kim10208562020-12-03 09:52:45 -0800727F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -0800728F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
Chao Yu846ae672018-02-26 22:04:13 +0800729F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800730#ifdef CONFIG_F2FS_FAULT_INJECTION
731F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
732F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
733#endif
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700734F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700735F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
Daeho Jeonge6592062021-01-21 22:45:29 +0900736F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +0200737F2FS_GENERAL_RO_ATTR(dirty_segments);
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800738F2FS_GENERAL_RO_ATTR(free_segments);
Jaegeuk Kim08234272021-03-01 17:28:16 -0800739F2FS_GENERAL_RO_ATTR(ovp_segments);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800740F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700741F2FS_GENERAL_RO_ATTR(features);
Yunlong Song80d42142017-10-27 20:45:05 +0800742F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700743F2FS_GENERAL_RO_ATTR(unusable);
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700744F2FS_GENERAL_RO_ATTR(encoding);
Jaegeuk Kima7e679b2020-02-25 19:08:16 -0800745F2FS_GENERAL_RO_ATTR(mounted_time_sec);
Dehe Guffcde4b2020-07-03 17:51:29 +0800746F2FS_GENERAL_RO_ATTR(main_blkaddr);
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800747#ifdef CONFIG_F2FS_STAT_FS
748F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
749F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
750F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
751F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
752F2FS_GENERAL_RO_ATTR(moved_blocks_background);
753F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
754F2FS_GENERAL_RO_ATTR(avg_vblocks);
755#endif
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700756
Chandan Rajendra643fa962018-12-12 15:20:12 +0530757#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700758F2FS_FEATURE_RO_ATTR(encryption);
759F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000760#ifdef CONFIG_UNICODE
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700761F2FS_FEATURE_RO_ATTR(encrypted_casefold);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700762#endif
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000763#endif /* CONFIG_FS_ENCRYPTION */
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700764#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700765F2FS_FEATURE_RO_ATTR(block_zoned);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700766#endif
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700767F2FS_FEATURE_RO_ATTR(atomic_write);
768F2FS_FEATURE_RO_ATTR(extra_attr);
769F2FS_FEATURE_RO_ATTR(project_quota);
770F2FS_FEATURE_RO_ATTR(inode_checksum);
771F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
772F2FS_FEATURE_RO_ATTR(quota_ino);
773F2FS_FEATURE_RO_ATTR(inode_crtime);
774F2FS_FEATURE_RO_ATTR(lost_found);
Eric Biggers95ae2512019-07-22 09:26:24 -0700775#ifdef CONFIG_FS_VERITY
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700776F2FS_FEATURE_RO_ATTR(verity);
Eric Biggers95ae2512019-07-22 09:26:24 -0700777#endif
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700778F2FS_FEATURE_RO_ATTR(sb_checksum);
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000779#ifdef CONFIG_UNICODE
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700780F2FS_FEATURE_RO_ATTR(casefold);
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000781#endif
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700782F2FS_FEATURE_RO_ATTR(readonly);
Chao Yu9b6ed142020-03-27 18:29:00 +0800783#ifdef CONFIG_F2FS_FS_COMPRESSION
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700784F2FS_FEATURE_RO_ATTR(compression);
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900785F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_written_block, compr_written_block);
786F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_saved_block, compr_saved_block);
787F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_new_inode, compr_new_inode);
Chao Yu9b6ed142020-03-27 18:29:00 +0800788#endif
Jaegeuk Kim4a196df2021-06-03 22:30:09 -0700789F2FS_FEATURE_RO_ATTR(pin_file);
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -0700790
Chao Yu8939a8482021-05-12 10:07:19 +0800791/* For ATGC */
792F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_ratio, candidate_ratio);
793F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_count, max_candidate_count);
794F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_weight, age_weight);
795F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800796
Daeho Jeong0f6b56e2021-08-02 21:22:45 -0700797F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, seq_file_ra_mul, seq_file_ra_mul);
Daeho Jeong07c6b592021-07-09 22:53:57 -0700798F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
799F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
Daeho Jeong6691d942021-09-29 11:12:03 -0700800F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_chunk, max_fragment_chunk);
801F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_hole, max_fragment_hole);
Daeho Jeong07c6b592021-07-09 22:53:57 -0700802
Chao Yu8ceffcb2017-06-14 17:39:47 +0800803#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
804static struct attribute *f2fs_attrs[] = {
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700805 ATTR_LIST(gc_urgent_sleep_time),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800806 ATTR_LIST(gc_min_sleep_time),
807 ATTR_LIST(gc_max_sleep_time),
808 ATTR_LIST(gc_no_gc_sleep_time),
809 ATTR_LIST(gc_idle),
Jaegeuk Kimd9872a62017-08-06 22:09:00 -0700810 ATTR_LIST(gc_urgent),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800811 ATTR_LIST(reclaim_segments),
Jaegeuk Kima4db59a2019-11-22 11:53:10 -0800812 ATTR_LIST(main_blkaddr),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800813 ATTR_LIST(max_small_discards),
Chao Yu969d1b12017-08-07 23:09:56 +0800814 ATTR_LIST(discard_granularity),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800815 ATTR_LIST(batched_trim_sections),
816 ATTR_LIST(ipu_policy),
817 ATTR_LIST(min_ipu_util),
818 ATTR_LIST(min_fsync_blocks),
Jaegeuk Kim853137c2018-08-09 17:53:34 -0700819 ATTR_LIST(min_seq_blocks),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800820 ATTR_LIST(min_hot_blocks),
Chao Yua2a12b62017-10-28 16:52:33 +0800821 ATTR_LIST(min_ssr_sections),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800822 ATTR_LIST(max_victim_search),
Chao Yu34655572018-10-25 16:19:28 +0800823 ATTR_LIST(migration_granularity),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800824 ATTR_LIST(dir_level),
825 ATTR_LIST(ram_thresh),
826 ATTR_LIST(ra_nid_pages),
827 ATTR_LIST(dirty_nats_ratio),
828 ATTR_LIST(cp_interval),
829 ATTR_LIST(idle_interval),
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +0530830 ATTR_LIST(discard_idle_interval),
831 ATTR_LIST(gc_idle_interval),
Jaegeuk Kim03f2c022019-01-14 10:42:11 -0800832 ATTR_LIST(umount_discard_timeout),
Daeho Jeong52118742021-08-19 20:52:28 -0700833#ifdef CONFIG_F2FS_IOSTAT
Chao Yub0af6d42017-08-02 23:21:48 +0800834 ATTR_LIST(iostat_enable),
Daeho Jeong2bc4bea2020-03-30 03:30:59 +0000835 ATTR_LIST(iostat_period_ms),
Daeho Jeong52118742021-08-19 20:52:28 -0700836#endif
Sheng Yongf6df8f22017-11-22 18:23:38 +0800837 ATTR_LIST(readdir_ra),
Jaegeuk Kim10208562020-12-03 09:52:45 -0800838 ATTR_LIST(max_io_bytes),
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -0800839 ATTR_LIST(gc_pin_file_thresh),
Chao Yu846ae672018-02-26 22:04:13 +0800840 ATTR_LIST(extension_list),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800841#ifdef CONFIG_F2FS_FAULT_INJECTION
842 ATTR_LIST(inject_rate),
843 ATTR_LIST(inject_type),
844#endif
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700845 ATTR_LIST(data_io_flag),
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700846 ATTR_LIST(node_io_flag),
Daeho Jeonge6592062021-01-21 22:45:29 +0900847 ATTR_LIST(ckpt_thread_ioprio),
Jaegeuk Kim8f1572f2017-10-24 09:46:54 +0200848 ATTR_LIST(dirty_segments),
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800849 ATTR_LIST(free_segments),
Jaegeuk Kim08234272021-03-01 17:28:16 -0800850 ATTR_LIST(ovp_segments),
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700851 ATTR_LIST(unusable),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800852 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700853 ATTR_LIST(features),
Chao Yudaeb4332017-06-26 16:24:41 +0800854 ATTR_LIST(reserved_blocks),
Yunlong Song80d42142017-10-27 20:45:05 +0800855 ATTR_LIST(current_reserved_blocks),
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700856 ATTR_LIST(encoding),
Jaegeuk Kima7e679b2020-02-25 19:08:16 -0800857 ATTR_LIST(mounted_time_sec),
Hridya Valsarajufc7100e2020-01-22 10:51:16 -0800858#ifdef CONFIG_F2FS_STAT_FS
859 ATTR_LIST(cp_foreground_calls),
860 ATTR_LIST(cp_background_calls),
861 ATTR_LIST(gc_foreground_calls),
862 ATTR_LIST(gc_background_calls),
863 ATTR_LIST(moved_blocks_foreground),
864 ATTR_LIST(moved_blocks_background),
865 ATTR_LIST(avg_vblocks),
866#endif
Daeho Jeong5ac443e2021-03-15 17:12:33 +0900867#ifdef CONFIG_F2FS_FS_COMPRESSION
868 ATTR_LIST(compr_written_block),
869 ATTR_LIST(compr_saved_block),
870 ATTR_LIST(compr_new_inode),
871#endif
Chao Yu8939a8482021-05-12 10:07:19 +0800872 /* For ATGC */
873 ATTR_LIST(atgc_candidate_ratio),
874 ATTR_LIST(atgc_candidate_count),
875 ATTR_LIST(atgc_age_weight),
876 ATTR_LIST(atgc_age_threshold),
Daeho Jeong0f6b56e2021-08-02 21:22:45 -0700877 ATTR_LIST(seq_file_ra_mul),
Daeho Jeong07c6b592021-07-09 22:53:57 -0700878 ATTR_LIST(gc_segment_mode),
879 ATTR_LIST(gc_reclaimed_segments),
Daeho Jeong6691d942021-09-29 11:12:03 -0700880 ATTR_LIST(max_fragment_chunk),
881 ATTR_LIST(max_fragment_hole),
Chao Yu8ceffcb2017-06-14 17:39:47 +0800882 NULL,
883};
Kimberly Brown176ef3c2019-06-07 13:40:41 -0400884ATTRIBUTE_GROUPS(f2fs);
Chao Yu8ceffcb2017-06-14 17:39:47 +0800885
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700886static struct attribute *f2fs_feat_attrs[] = {
Chandan Rajendra643fa962018-12-12 15:20:12 +0530887#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700888 ATTR_LIST(encryption),
Eric Biggersed318a62020-05-12 16:32:50 -0700889 ATTR_LIST(test_dummy_encryption_v2),
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000890#ifdef CONFIG_UNICODE
891 ATTR_LIST(encrypted_casefold),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700892#endif
Daniel Rosenberg4c039d52021-06-03 09:50:38 +0000893#endif /* CONFIG_FS_ENCRYPTION */
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700894#ifdef CONFIG_BLK_DEV_ZONED
895 ATTR_LIST(block_zoned),
896#endif
897 ATTR_LIST(atomic_write),
898 ATTR_LIST(extra_attr),
899 ATTR_LIST(project_quota),
900 ATTR_LIST(inode_checksum),
Chao Yu6afc6622017-09-06 21:59:50 +0800901 ATTR_LIST(flexible_inline_xattr),
Jaegeuk Kim234a9682017-10-05 21:03:06 -0700902 ATTR_LIST(quota_ino),
Chao Yu1c1d35d2018-01-25 14:54:42 +0800903 ATTR_LIST(inode_crtime),
Sheng Yongb7c409d2018-03-15 18:51:41 +0800904 ATTR_LIST(lost_found),
Eric Biggers95ae2512019-07-22 09:26:24 -0700905#ifdef CONFIG_FS_VERITY
906 ATTR_LIST(verity),
907#endif
Junling Zhengd440c522018-09-28 20:25:56 +0800908 ATTR_LIST(sb_checksum),
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000909#ifdef CONFIG_UNICODE
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700910 ATTR_LIST(casefold),
Daniel Rosenberg39307f82021-06-03 09:50:37 +0000911#endif
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -0700912 ATTR_LIST(readonly),
Chao Yu9b6ed142020-03-27 18:29:00 +0800913#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800914 ATTR_LIST(compression),
Chao Yu9b6ed142020-03-27 18:29:00 +0800915#endif
Jaegeuk Kim4a196df2021-06-03 22:30:09 -0700916 ATTR_LIST(pin_file),
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700917 NULL,
918};
Kimberly Brown176ef3c2019-06-07 13:40:41 -0400919ATTRIBUTE_GROUPS(f2fs_feat);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700920
Chao Yu0bfe9f72021-01-14 09:41:27 +0800921F2FS_GENERAL_RO_ATTR(sb_status);
Chao Yu5d4daa52020-12-09 16:43:27 +0800922static struct attribute *f2fs_stat_attrs[] = {
Chao Yu0bfe9f72021-01-14 09:41:27 +0800923 ATTR_LIST(sb_status),
Chao Yu5d4daa52020-12-09 16:43:27 +0800924 NULL,
925};
926ATTRIBUTE_GROUPS(f2fs_stat);
927
Jaegeuk Kim4c89b532021-06-03 12:31:08 -0700928F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
929F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
930F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
931F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
932F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
933F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
934F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
935F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
936F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
937F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
938F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
939F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
940F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
941F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
942
943static struct attribute *f2fs_sb_feat_attrs[] = {
944 ATTR_LIST(sb_encryption),
945 ATTR_LIST(sb_block_zoned),
946 ATTR_LIST(sb_extra_attr),
947 ATTR_LIST(sb_project_quota),
948 ATTR_LIST(sb_inode_checksum),
949 ATTR_LIST(sb_flexible_inline_xattr),
950 ATTR_LIST(sb_quota_ino),
951 ATTR_LIST(sb_inode_crtime),
952 ATTR_LIST(sb_lost_found),
953 ATTR_LIST(sb_verity),
954 ATTR_LIST(sb_sb_checksum),
955 ATTR_LIST(sb_casefold),
956 ATTR_LIST(sb_compression),
957 ATTR_LIST(sb_readonly),
958 NULL,
959};
960ATTRIBUTE_GROUPS(f2fs_sb_feat);
961
Chao Yu8ceffcb2017-06-14 17:39:47 +0800962static const struct sysfs_ops f2fs_attr_ops = {
963 .show = f2fs_attr_show,
964 .store = f2fs_attr_store,
965};
966
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700967static struct kobj_type f2fs_sb_ktype = {
Kimberly Brown176ef3c2019-06-07 13:40:41 -0400968 .default_groups = f2fs_groups,
Chao Yu8ceffcb2017-06-14 17:39:47 +0800969 .sysfs_ops = &f2fs_attr_ops,
970 .release = f2fs_sb_release,
971};
972
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700973static struct kobj_type f2fs_ktype = {
974 .sysfs_ops = &f2fs_attr_ops,
975};
976
977static struct kset f2fs_kset = {
Jack Qiua87aff12020-07-24 16:55:28 +0800978 .kobj = {.ktype = &f2fs_ktype},
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700979};
980
981static struct kobj_type f2fs_feat_ktype = {
Kimberly Brown176ef3c2019-06-07 13:40:41 -0400982 .default_groups = f2fs_feat_groups,
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -0700983 .sysfs_ops = &f2fs_attr_ops,
984};
985
986static struct kobject f2fs_feat = {
987 .kset = &f2fs_kset,
988};
989
Chao Yu5d4daa52020-12-09 16:43:27 +0800990static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
991 struct attribute *attr, char *buf)
992{
993 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
994 s_stat_kobj);
995 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
996
997 return a->show ? a->show(a, sbi, buf) : 0;
998}
999
1000static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1001 const char *buf, size_t len)
1002{
1003 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1004 s_stat_kobj);
1005 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1006
1007 return a->store ? a->store(a, sbi, buf, len) : 0;
1008}
1009
1010static void f2fs_stat_kobj_release(struct kobject *kobj)
1011{
1012 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1013 s_stat_kobj);
1014 complete(&sbi->s_stat_kobj_unregister);
1015}
1016
1017static const struct sysfs_ops f2fs_stat_attr_ops = {
1018 .show = f2fs_stat_attr_show,
1019 .store = f2fs_stat_attr_store,
1020};
1021
1022static struct kobj_type f2fs_stat_ktype = {
1023 .default_groups = f2fs_stat_groups,
1024 .sysfs_ops = &f2fs_stat_attr_ops,
1025 .release = f2fs_stat_kobj_release,
1026};
1027
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001028static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1029 struct attribute *attr, char *buf)
1030{
1031 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1032 s_feature_list_kobj);
1033 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1034
1035 return a->show ? a->show(a, sbi, buf) : 0;
1036}
1037
1038static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1039{
1040 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1041 s_feature_list_kobj);
1042 complete(&sbi->s_feature_list_kobj_unregister);
1043}
1044
1045static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1046 .show = f2fs_sb_feat_attr_show,
1047};
1048
1049static struct kobj_type f2fs_feature_list_ktype = {
1050 .default_groups = f2fs_sb_feat_groups,
1051 .sysfs_ops = &f2fs_feature_list_attr_ops,
1052 .release = f2fs_feature_list_kobj_release,
1053};
1054
Randy Dunlapcb15d1e2018-07-06 20:50:57 -07001055static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1056 void *offset)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001057{
1058 struct super_block *sb = seq->private;
1059 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1060 unsigned int total_segs =
1061 le32_to_cpu(sbi->raw_super->segment_count_main);
1062 int i;
1063
1064 seq_puts(seq, "format: segment_type|valid_blocks\n"
1065 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1066
1067 for (i = 0; i < total_segs; i++) {
1068 struct seg_entry *se = get_seg_entry(sbi, i);
1069
1070 if ((i % 10) == 0)
1071 seq_printf(seq, "%-10d", i);
Chao Yu8740edc2019-06-18 18:00:09 +08001072 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001073 if ((i % 10) == 9 || i == (total_segs - 1))
1074 seq_putc(seq, '\n');
1075 else
1076 seq_putc(seq, ' ');
1077 }
1078
1079 return 0;
1080}
1081
Randy Dunlapcb15d1e2018-07-06 20:50:57 -07001082static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1083 void *offset)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001084{
1085 struct super_block *sb = seq->private;
1086 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1087 unsigned int total_segs =
1088 le32_to_cpu(sbi->raw_super->segment_count_main);
1089 int i, j;
1090
1091 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1092 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1093
1094 for (i = 0; i < total_segs; i++) {
1095 struct seg_entry *se = get_seg_entry(sbi, i);
1096
1097 seq_printf(seq, "%-10d", i);
Chao Yu8740edc2019-06-18 18:00:09 +08001098 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001099 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1100 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1101 seq_putc(seq, '\n');
1102 }
1103 return 0;
1104}
1105
Yunlong Song970e3482018-07-23 22:10:22 +08001106static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1107 void *offset)
1108{
1109 struct super_block *sb = seq->private;
1110 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1111 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1112 int i;
1113
1114 seq_puts(seq, "format: victim_secmap bitmaps\n");
1115
1116 for (i = 0; i < MAIN_SECS(sbi); i++) {
1117 if ((i % 10) == 0)
1118 seq_printf(seq, "%-10d", i);
1119 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1120 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1121 seq_putc(seq, '\n');
1122 else
1123 seq_putc(seq, ' ');
1124 }
1125 return 0;
1126}
1127
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001128int __init f2fs_init_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001129{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001130 int ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001131
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001132 kobject_set_name(&f2fs_kset.kobj, "f2fs");
1133 f2fs_kset.kobj.parent = fs_kobj;
1134 ret = kset_register(&f2fs_kset);
1135 if (ret)
1136 return ret;
1137
1138 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1139 NULL, "features");
Chao Yufe396ad2019-12-30 17:41:41 +08001140 if (ret) {
1141 kobject_put(&f2fs_feat);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001142 kset_unregister(&f2fs_kset);
Chao Yufe396ad2019-12-30 17:41:41 +08001143 } else {
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001144 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
Chao Yufe396ad2019-12-30 17:41:41 +08001145 }
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001146 return ret;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001147}
1148
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001149void f2fs_exit_sysfs(void)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001150{
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001151 kobject_put(&f2fs_feat);
1152 kset_unregister(&f2fs_kset);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001153 remove_proc_entry("fs/f2fs", NULL);
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001154 f2fs_proc_root = NULL;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001155}
1156
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001157int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001158{
1159 struct super_block *sb = sbi->sb;
1160 int err;
1161
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001162 sbi->s_kobj.kset = &f2fs_kset;
1163 init_completion(&sbi->s_kobj_unregister);
1164 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1165 "%s", sb->s_id);
Chao Yu5d4daa52020-12-09 16:43:27 +08001166 if (err)
1167 goto put_sb_kobj;
1168
1169 sbi->s_stat_kobj.kset = &f2fs_kset;
1170 init_completion(&sbi->s_stat_kobj_unregister);
1171 err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1172 &sbi->s_kobj, "stat");
1173 if (err)
1174 goto put_stat_kobj;
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001175
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001176 sbi->s_feature_list_kobj.kset = &f2fs_kset;
1177 init_completion(&sbi->s_feature_list_kobj_unregister);
1178 err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1179 &f2fs_feature_list_ktype,
1180 &sbi->s_kobj, "feature_list");
1181 if (err)
1182 goto put_feature_list_kobj;
1183
Chao Yu8ceffcb2017-06-14 17:39:47 +08001184 if (f2fs_proc_root)
1185 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1186
1187 if (sbi->s_proc) {
Yangtao Li491f7f72021-08-14 18:37:01 +08001188 proc_create_single_data("segment_info", 0444, sbi->s_proc,
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001189 segment_info_seq_show, sb);
Yangtao Li491f7f72021-08-14 18:37:01 +08001190 proc_create_single_data("segment_bits", 0444, sbi->s_proc,
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001191 segment_bits_seq_show, sb);
Daeho Jeong52118742021-08-19 20:52:28 -07001192#ifdef CONFIG_F2FS_IOSTAT
Yangtao Li491f7f72021-08-14 18:37:01 +08001193 proc_create_single_data("iostat_info", 0444, sbi->s_proc,
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001194 iostat_info_seq_show, sb);
Daeho Jeong52118742021-08-19 20:52:28 -07001195#endif
Yangtao Li491f7f72021-08-14 18:37:01 +08001196 proc_create_single_data("victim_bits", 0444, sbi->s_proc,
Yunlong Song970e3482018-07-23 22:10:22 +08001197 victim_bits_seq_show, sb);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001198 }
Chao Yu8ceffcb2017-06-14 17:39:47 +08001199 return 0;
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001200put_feature_list_kobj:
1201 kobject_put(&sbi->s_feature_list_kobj);
1202 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
Chao Yu5d4daa52020-12-09 16:43:27 +08001203put_stat_kobj:
1204 kobject_put(&sbi->s_stat_kobj);
1205 wait_for_completion(&sbi->s_stat_kobj_unregister);
1206put_sb_kobj:
1207 kobject_put(&sbi->s_kobj);
1208 wait_for_completion(&sbi->s_kobj_unregister);
1209 return err;
Chao Yu8ceffcb2017-06-14 17:39:47 +08001210}
1211
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001212void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
Chao Yu8ceffcb2017-06-14 17:39:47 +08001213{
Chao Yu8ceffcb2017-06-14 17:39:47 +08001214 if (sbi->s_proc) {
Daeho Jeong52118742021-08-19 20:52:28 -07001215#ifdef CONFIG_F2FS_IOSTAT
Chao Yub0af6d42017-08-02 23:21:48 +08001216 remove_proc_entry("iostat_info", sbi->s_proc);
Daeho Jeong52118742021-08-19 20:52:28 -07001217#endif
Chao Yu8ceffcb2017-06-14 17:39:47 +08001218 remove_proc_entry("segment_info", sbi->s_proc);
1219 remove_proc_entry("segment_bits", sbi->s_proc);
Yunlong Song970e3482018-07-23 22:10:22 +08001220 remove_proc_entry("victim_bits", sbi->s_proc);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001221 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1222 }
Chao Yu5d4daa52020-12-09 16:43:27 +08001223
1224 kobject_del(&sbi->s_stat_kobj);
1225 kobject_put(&sbi->s_stat_kobj);
1226 wait_for_completion(&sbi->s_stat_kobj_unregister);
Jaegeuk Kim4c89b532021-06-03 12:31:08 -07001227 kobject_del(&sbi->s_feature_list_kobj);
1228 kobject_put(&sbi->s_feature_list_kobj);
1229 wait_for_completion(&sbi->s_feature_list_kobj_unregister);
Chao Yu5d4daa52020-12-09 16:43:27 +08001230
Jaegeuk Kimbf9e6972017-07-21 17:14:09 -07001231 kobject_del(&sbi->s_kobj);
Jaegeuk Kim820d3662019-12-13 18:32:16 -08001232 kobject_put(&sbi->s_kobj);
Jamie Ilesae284d82020-10-12 14:09:48 +01001233 wait_for_completion(&sbi->s_kobj_unregister);
Chao Yu8ceffcb2017-06-14 17:39:47 +08001234}