]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/f2fs/sysfs.c
Merge tag 'kbuild-v5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[linux.git] / fs / f2fs / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * f2fs sysfs interface
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  * Copyright (c) 2017 Chao Yu <chao@kernel.org>
8  */
9 #include <linux/compiler.h>
10 #include <linux/proc_fs.h>
11 #include <linux/f2fs_fs.h>
12 #include <linux/seq_file.h>
13
14 #include "f2fs.h"
15 #include "segment.h"
16 #include "gc.h"
17
18 static struct proc_dir_entry *f2fs_proc_root;
19
20 /* Sysfs support for f2fs */
21 enum {
22         GC_THREAD,      /* struct f2fs_gc_thread */
23         SM_INFO,        /* struct f2fs_sm_info */
24         DCC_INFO,       /* struct discard_cmd_control */
25         NM_INFO,        /* struct f2fs_nm_info */
26         F2FS_SBI,       /* struct f2fs_sb_info */
27 #ifdef CONFIG_F2FS_FAULT_INJECTION
28         FAULT_INFO_RATE,        /* struct f2fs_fault_info */
29         FAULT_INFO_TYPE,        /* struct f2fs_fault_info */
30 #endif
31         RESERVED_BLOCKS,        /* struct f2fs_sb_info */
32 };
33
34 struct f2fs_attr {
35         struct attribute attr;
36         ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
37         ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
38                          const char *, size_t);
39         int struct_type;
40         int offset;
41         int id;
42 };
43
44 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
45 {
46         if (struct_type == GC_THREAD)
47                 return (unsigned char *)sbi->gc_thread;
48         else if (struct_type == SM_INFO)
49                 return (unsigned char *)SM_I(sbi);
50         else if (struct_type == DCC_INFO)
51                 return (unsigned char *)SM_I(sbi)->dcc_info;
52         else if (struct_type == NM_INFO)
53                 return (unsigned char *)NM_I(sbi);
54         else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
55                 return (unsigned char *)sbi;
56 #ifdef CONFIG_F2FS_FAULT_INJECTION
57         else if (struct_type == FAULT_INFO_RATE ||
58                                         struct_type == FAULT_INFO_TYPE)
59                 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
60 #endif
61         return NULL;
62 }
63
64 static ssize_t dirty_segments_show(struct f2fs_attr *a,
65                 struct f2fs_sb_info *sbi, char *buf)
66 {
67         return snprintf(buf, PAGE_SIZE, "%llu\n",
68                 (unsigned long long)(dirty_segments(sbi)));
69 }
70
71 static ssize_t unusable_show(struct f2fs_attr *a,
72                 struct f2fs_sb_info *sbi, char *buf)
73 {
74         block_t unusable;
75
76         if (test_opt(sbi, DISABLE_CHECKPOINT))
77                 unusable = sbi->unusable_block_count;
78         else
79                 unusable = f2fs_get_unusable_blocks(sbi);
80         return snprintf(buf, PAGE_SIZE, "%llu\n",
81                 (unsigned long long)unusable);
82 }
83
84
85 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
86                 struct f2fs_sb_info *sbi, char *buf)
87 {
88         struct super_block *sb = sbi->sb;
89
90         if (!sb->s_bdev->bd_part)
91                 return snprintf(buf, PAGE_SIZE, "0\n");
92
93         return snprintf(buf, PAGE_SIZE, "%llu\n",
94                 (unsigned long long)(sbi->kbytes_written +
95                         BD_PART_WRITTEN(sbi)));
96 }
97
98 static ssize_t features_show(struct f2fs_attr *a,
99                 struct f2fs_sb_info *sbi, char *buf)
100 {
101         struct super_block *sb = sbi->sb;
102         int len = 0;
103
104         if (!sb->s_bdev->bd_part)
105                 return snprintf(buf, PAGE_SIZE, "0\n");
106
107         if (f2fs_sb_has_encrypt(sbi))
108                 len += snprintf(buf, PAGE_SIZE - len, "%s",
109                                                 "encryption");
110         if (f2fs_sb_has_blkzoned(sbi))
111                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
112                                 len ? ", " : "", "blkzoned");
113         if (f2fs_sb_has_extra_attr(sbi))
114                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
115                                 len ? ", " : "", "extra_attr");
116         if (f2fs_sb_has_project_quota(sbi))
117                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
118                                 len ? ", " : "", "projquota");
119         if (f2fs_sb_has_inode_chksum(sbi))
120                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
121                                 len ? ", " : "", "inode_checksum");
122         if (f2fs_sb_has_flexible_inline_xattr(sbi))
123                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
124                                 len ? ", " : "", "flexible_inline_xattr");
125         if (f2fs_sb_has_quota_ino(sbi))
126                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
127                                 len ? ", " : "", "quota_ino");
128         if (f2fs_sb_has_inode_crtime(sbi))
129                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
130                                 len ? ", " : "", "inode_crtime");
131         if (f2fs_sb_has_lost_found(sbi))
132                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
133                                 len ? ", " : "", "lost_found");
134         if (f2fs_sb_has_verity(sbi))
135                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
136                                 len ? ", " : "", "verity");
137         if (f2fs_sb_has_sb_chksum(sbi))
138                 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
139                                 len ? ", " : "", "sb_checksum");
140         len += snprintf(buf + len, PAGE_SIZE - len, "\n");
141         return len;
142 }
143
144 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
145                                         struct f2fs_sb_info *sbi, char *buf)
146 {
147         return snprintf(buf, PAGE_SIZE, "%u\n", sbi->current_reserved_blocks);
148 }
149
150 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
151                         struct f2fs_sb_info *sbi, char *buf)
152 {
153         unsigned char *ptr = NULL;
154         unsigned int *ui;
155
156         ptr = __struct_ptr(sbi, a->struct_type);
157         if (!ptr)
158                 return -EINVAL;
159
160         if (!strcmp(a->attr.name, "extension_list")) {
161                 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
162                                         sbi->raw_super->extension_list;
163                 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
164                 int hot_count = sbi->raw_super->hot_ext_count;
165                 int len = 0, i;
166
167                 len += snprintf(buf + len, PAGE_SIZE - len,
168                                                 "cold file extension:\n");
169                 for (i = 0; i < cold_count; i++)
170                         len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
171                                                                 extlist[i]);
172
173                 len += snprintf(buf + len, PAGE_SIZE - len,
174                                                 "hot file extension:\n");
175                 for (i = cold_count; i < cold_count + hot_count; i++)
176                         len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
177                                                                 extlist[i]);
178                 return len;
179         }
180
181         ui = (unsigned int *)(ptr + a->offset);
182
183         return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
184 }
185
186 static ssize_t __sbi_store(struct f2fs_attr *a,
187                         struct f2fs_sb_info *sbi,
188                         const char *buf, size_t count)
189 {
190         unsigned char *ptr;
191         unsigned long t;
192         unsigned int *ui;
193         ssize_t ret;
194
195         ptr = __struct_ptr(sbi, a->struct_type);
196         if (!ptr)
197                 return -EINVAL;
198
199         if (!strcmp(a->attr.name, "extension_list")) {
200                 const char *name = strim((char *)buf);
201                 bool set = true, hot;
202
203                 if (!strncmp(name, "[h]", 3))
204                         hot = true;
205                 else if (!strncmp(name, "[c]", 3))
206                         hot = false;
207                 else
208                         return -EINVAL;
209
210                 name += 3;
211
212                 if (*name == '!') {
213                         name++;
214                         set = false;
215                 }
216
217                 if (strlen(name) >= F2FS_EXTENSION_LEN)
218                         return -EINVAL;
219
220                 down_write(&sbi->sb_lock);
221
222                 ret = f2fs_update_extension_list(sbi, name, hot, set);
223                 if (ret)
224                         goto out;
225
226                 ret = f2fs_commit_super(sbi, false);
227                 if (ret)
228                         f2fs_update_extension_list(sbi, name, hot, !set);
229 out:
230                 up_write(&sbi->sb_lock);
231                 return ret ? ret : count;
232         }
233
234         ui = (unsigned int *)(ptr + a->offset);
235
236         ret = kstrtoul(skip_spaces(buf), 0, &t);
237         if (ret < 0)
238                 return ret;
239 #ifdef CONFIG_F2FS_FAULT_INJECTION
240         if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
241                 return -EINVAL;
242         if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
243                 return -EINVAL;
244 #endif
245         if (a->struct_type == RESERVED_BLOCKS) {
246                 spin_lock(&sbi->stat_lock);
247                 if (t > (unsigned long)(sbi->user_block_count -
248                                 F2FS_OPTION(sbi).root_reserved_blocks)) {
249                         spin_unlock(&sbi->stat_lock);
250                         return -EINVAL;
251                 }
252                 *ui = t;
253                 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
254                                 sbi->user_block_count - valid_user_blocks(sbi));
255                 spin_unlock(&sbi->stat_lock);
256                 return count;
257         }
258
259         if (!strcmp(a->attr.name, "discard_granularity")) {
260                 if (t == 0 || t > MAX_PLIST_NUM)
261                         return -EINVAL;
262                 if (t == *ui)
263                         return count;
264                 *ui = t;
265                 return count;
266         }
267
268         if (!strcmp(a->attr.name, "migration_granularity")) {
269                 if (t == 0 || t > sbi->segs_per_sec)
270                         return -EINVAL;
271         }
272
273         if (!strcmp(a->attr.name, "trim_sections"))
274                 return -EINVAL;
275
276         if (!strcmp(a->attr.name, "gc_urgent")) {
277                 if (t >= 1) {
278                         sbi->gc_mode = GC_URGENT;
279                         if (sbi->gc_thread) {
280                                 sbi->gc_thread->gc_wake = 1;
281                                 wake_up_interruptible_all(
282                                         &sbi->gc_thread->gc_wait_queue_head);
283                                 wake_up_discard_thread(sbi, true);
284                         }
285                 } else {
286                         sbi->gc_mode = GC_NORMAL;
287                 }
288                 return count;
289         }
290         if (!strcmp(a->attr.name, "gc_idle")) {
291                 if (t == GC_IDLE_CB)
292                         sbi->gc_mode = GC_IDLE_CB;
293                 else if (t == GC_IDLE_GREEDY)
294                         sbi->gc_mode = GC_IDLE_GREEDY;
295                 else
296                         sbi->gc_mode = GC_NORMAL;
297                 return count;
298         }
299
300
301         if (!strcmp(a->attr.name, "iostat_enable")) {
302                 sbi->iostat_enable = !!t;
303                 if (!sbi->iostat_enable)
304                         f2fs_reset_iostat(sbi);
305                 return count;
306         }
307
308         *ui = (unsigned int)t;
309
310         return count;
311 }
312
313 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
314                         struct f2fs_sb_info *sbi,
315                         const char *buf, size_t count)
316 {
317         ssize_t ret;
318         bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
319                                         a->struct_type == GC_THREAD);
320
321         if (gc_entry) {
322                 if (!down_read_trylock(&sbi->sb->s_umount))
323                         return -EAGAIN;
324         }
325         ret = __sbi_store(a, sbi, buf, count);
326         if (gc_entry)
327                 up_read(&sbi->sb->s_umount);
328
329         return ret;
330 }
331
332 static ssize_t f2fs_attr_show(struct kobject *kobj,
333                                 struct attribute *attr, char *buf)
334 {
335         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
336                                                                 s_kobj);
337         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
338
339         return a->show ? a->show(a, sbi, buf) : 0;
340 }
341
342 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
343                                                 const char *buf, size_t len)
344 {
345         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
346                                                                         s_kobj);
347         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
348
349         return a->store ? a->store(a, sbi, buf, len) : 0;
350 }
351
352 static void f2fs_sb_release(struct kobject *kobj)
353 {
354         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
355                                                                 s_kobj);
356         complete(&sbi->s_kobj_unregister);
357 }
358
359 enum feat_id {
360         FEAT_CRYPTO = 0,
361         FEAT_BLKZONED,
362         FEAT_ATOMIC_WRITE,
363         FEAT_EXTRA_ATTR,
364         FEAT_PROJECT_QUOTA,
365         FEAT_INODE_CHECKSUM,
366         FEAT_FLEXIBLE_INLINE_XATTR,
367         FEAT_QUOTA_INO,
368         FEAT_INODE_CRTIME,
369         FEAT_LOST_FOUND,
370         FEAT_VERITY,
371         FEAT_SB_CHECKSUM,
372 };
373
374 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
375                 struct f2fs_sb_info *sbi, char *buf)
376 {
377         switch (a->id) {
378         case FEAT_CRYPTO:
379         case FEAT_BLKZONED:
380         case FEAT_ATOMIC_WRITE:
381         case FEAT_EXTRA_ATTR:
382         case FEAT_PROJECT_QUOTA:
383         case FEAT_INODE_CHECKSUM:
384         case FEAT_FLEXIBLE_INLINE_XATTR:
385         case FEAT_QUOTA_INO:
386         case FEAT_INODE_CRTIME:
387         case FEAT_LOST_FOUND:
388         case FEAT_VERITY:
389         case FEAT_SB_CHECKSUM:
390                 return snprintf(buf, PAGE_SIZE, "supported\n");
391         }
392         return 0;
393 }
394
395 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
396 static struct f2fs_attr f2fs_attr_##_name = {                   \
397         .attr = {.name = __stringify(_name), .mode = _mode },   \
398         .show   = _show,                                        \
399         .store  = _store,                                       \
400         .struct_type = _struct_type,                            \
401         .offset = _offset                                       \
402 }
403
404 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)    \
405         F2FS_ATTR_OFFSET(struct_type, name, 0644,               \
406                 f2fs_sbi_show, f2fs_sbi_store,                  \
407                 offsetof(struct struct_name, elname))
408
409 #define F2FS_GENERAL_RO_ATTR(name) \
410 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
411
412 #define F2FS_FEATURE_RO_ATTR(_name, _id)                        \
413 static struct f2fs_attr f2fs_attr_##_name = {                   \
414         .attr = {.name = __stringify(_name), .mode = 0444 },    \
415         .show   = f2fs_feature_show,                            \
416         .id     = _id,                                          \
417 }
418
419 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
420                                                         urgent_sleep_time);
421 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
422 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
423 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
424 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
425 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
426 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
427 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
428 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
429 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
430 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
431 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
432 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
433 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
434 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
435 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
436 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
437 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
438 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
439 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
440 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
441 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
442 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
443 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
444 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
445 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
446                                         interval_time[DISCARD_TIME]);
447 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
448 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
449                 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
450 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
451 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
452 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
453 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
454 #ifdef CONFIG_F2FS_FAULT_INJECTION
455 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
456 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
457 #endif
458 F2FS_GENERAL_RO_ATTR(dirty_segments);
459 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
460 F2FS_GENERAL_RO_ATTR(features);
461 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
462 F2FS_GENERAL_RO_ATTR(unusable);
463
464 #ifdef CONFIG_FS_ENCRYPTION
465 F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
466 #endif
467 #ifdef CONFIG_BLK_DEV_ZONED
468 F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
469 #endif
470 F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
471 F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
472 F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
473 F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
474 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
475 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
476 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
477 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
478 #ifdef CONFIG_FS_VERITY
479 F2FS_FEATURE_RO_ATTR(verity, FEAT_VERITY);
480 #endif
481 F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
482
483 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
484 static struct attribute *f2fs_attrs[] = {
485         ATTR_LIST(gc_urgent_sleep_time),
486         ATTR_LIST(gc_min_sleep_time),
487         ATTR_LIST(gc_max_sleep_time),
488         ATTR_LIST(gc_no_gc_sleep_time),
489         ATTR_LIST(gc_idle),
490         ATTR_LIST(gc_urgent),
491         ATTR_LIST(reclaim_segments),
492         ATTR_LIST(max_small_discards),
493         ATTR_LIST(discard_granularity),
494         ATTR_LIST(batched_trim_sections),
495         ATTR_LIST(ipu_policy),
496         ATTR_LIST(min_ipu_util),
497         ATTR_LIST(min_fsync_blocks),
498         ATTR_LIST(min_seq_blocks),
499         ATTR_LIST(min_hot_blocks),
500         ATTR_LIST(min_ssr_sections),
501         ATTR_LIST(max_victim_search),
502         ATTR_LIST(migration_granularity),
503         ATTR_LIST(dir_level),
504         ATTR_LIST(ram_thresh),
505         ATTR_LIST(ra_nid_pages),
506         ATTR_LIST(dirty_nats_ratio),
507         ATTR_LIST(cp_interval),
508         ATTR_LIST(idle_interval),
509         ATTR_LIST(discard_idle_interval),
510         ATTR_LIST(gc_idle_interval),
511         ATTR_LIST(umount_discard_timeout),
512         ATTR_LIST(iostat_enable),
513         ATTR_LIST(readdir_ra),
514         ATTR_LIST(gc_pin_file_thresh),
515         ATTR_LIST(extension_list),
516 #ifdef CONFIG_F2FS_FAULT_INJECTION
517         ATTR_LIST(inject_rate),
518         ATTR_LIST(inject_type),
519 #endif
520         ATTR_LIST(dirty_segments),
521         ATTR_LIST(unusable),
522         ATTR_LIST(lifetime_write_kbytes),
523         ATTR_LIST(features),
524         ATTR_LIST(reserved_blocks),
525         ATTR_LIST(current_reserved_blocks),
526         NULL,
527 };
528 ATTRIBUTE_GROUPS(f2fs);
529
530 static struct attribute *f2fs_feat_attrs[] = {
531 #ifdef CONFIG_FS_ENCRYPTION
532         ATTR_LIST(encryption),
533 #endif
534 #ifdef CONFIG_BLK_DEV_ZONED
535         ATTR_LIST(block_zoned),
536 #endif
537         ATTR_LIST(atomic_write),
538         ATTR_LIST(extra_attr),
539         ATTR_LIST(project_quota),
540         ATTR_LIST(inode_checksum),
541         ATTR_LIST(flexible_inline_xattr),
542         ATTR_LIST(quota_ino),
543         ATTR_LIST(inode_crtime),
544         ATTR_LIST(lost_found),
545 #ifdef CONFIG_FS_VERITY
546         ATTR_LIST(verity),
547 #endif
548         ATTR_LIST(sb_checksum),
549         NULL,
550 };
551 ATTRIBUTE_GROUPS(f2fs_feat);
552
553 static const struct sysfs_ops f2fs_attr_ops = {
554         .show   = f2fs_attr_show,
555         .store  = f2fs_attr_store,
556 };
557
558 static struct kobj_type f2fs_sb_ktype = {
559         .default_groups = f2fs_groups,
560         .sysfs_ops      = &f2fs_attr_ops,
561         .release        = f2fs_sb_release,
562 };
563
564 static struct kobj_type f2fs_ktype = {
565         .sysfs_ops      = &f2fs_attr_ops,
566 };
567
568 static struct kset f2fs_kset = {
569         .kobj   = {.ktype = &f2fs_ktype},
570 };
571
572 static struct kobj_type f2fs_feat_ktype = {
573         .default_groups = f2fs_feat_groups,
574         .sysfs_ops      = &f2fs_attr_ops,
575 };
576
577 static struct kobject f2fs_feat = {
578         .kset   = &f2fs_kset,
579 };
580
581 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
582                                                 void *offset)
583 {
584         struct super_block *sb = seq->private;
585         struct f2fs_sb_info *sbi = F2FS_SB(sb);
586         unsigned int total_segs =
587                         le32_to_cpu(sbi->raw_super->segment_count_main);
588         int i;
589
590         seq_puts(seq, "format: segment_type|valid_blocks\n"
591                 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
592
593         for (i = 0; i < total_segs; i++) {
594                 struct seg_entry *se = get_seg_entry(sbi, i);
595
596                 if ((i % 10) == 0)
597                         seq_printf(seq, "%-10d", i);
598                 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
599                 if ((i % 10) == 9 || i == (total_segs - 1))
600                         seq_putc(seq, '\n');
601                 else
602                         seq_putc(seq, ' ');
603         }
604
605         return 0;
606 }
607
608 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
609                                                 void *offset)
610 {
611         struct super_block *sb = seq->private;
612         struct f2fs_sb_info *sbi = F2FS_SB(sb);
613         unsigned int total_segs =
614                         le32_to_cpu(sbi->raw_super->segment_count_main);
615         int i, j;
616
617         seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
618                 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
619
620         for (i = 0; i < total_segs; i++) {
621                 struct seg_entry *se = get_seg_entry(sbi, i);
622
623                 seq_printf(seq, "%-10d", i);
624                 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
625                 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
626                         seq_printf(seq, " %.2x", se->cur_valid_map[j]);
627                 seq_putc(seq, '\n');
628         }
629         return 0;
630 }
631
632 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
633                                                void *offset)
634 {
635         struct super_block *sb = seq->private;
636         struct f2fs_sb_info *sbi = F2FS_SB(sb);
637         time64_t now = ktime_get_real_seconds();
638
639         if (!sbi->iostat_enable)
640                 return 0;
641
642         seq_printf(seq, "time:          %-16llu\n", now);
643
644         /* print app IOs */
645         seq_printf(seq, "app buffered:  %-16llu\n",
646                                 sbi->write_iostat[APP_BUFFERED_IO]);
647         seq_printf(seq, "app direct:    %-16llu\n",
648                                 sbi->write_iostat[APP_DIRECT_IO]);
649         seq_printf(seq, "app mapped:    %-16llu\n",
650                                 sbi->write_iostat[APP_MAPPED_IO]);
651
652         /* print fs IOs */
653         seq_printf(seq, "fs data:       %-16llu\n",
654                                 sbi->write_iostat[FS_DATA_IO]);
655         seq_printf(seq, "fs node:       %-16llu\n",
656                                 sbi->write_iostat[FS_NODE_IO]);
657         seq_printf(seq, "fs meta:       %-16llu\n",
658                                 sbi->write_iostat[FS_META_IO]);
659         seq_printf(seq, "fs gc data:    %-16llu\n",
660                                 sbi->write_iostat[FS_GC_DATA_IO]);
661         seq_printf(seq, "fs gc node:    %-16llu\n",
662                                 sbi->write_iostat[FS_GC_NODE_IO]);
663         seq_printf(seq, "fs cp data:    %-16llu\n",
664                                 sbi->write_iostat[FS_CP_DATA_IO]);
665         seq_printf(seq, "fs cp node:    %-16llu\n",
666                                 sbi->write_iostat[FS_CP_NODE_IO]);
667         seq_printf(seq, "fs cp meta:    %-16llu\n",
668                                 sbi->write_iostat[FS_CP_META_IO]);
669         seq_printf(seq, "fs discard:    %-16llu\n",
670                                 sbi->write_iostat[FS_DISCARD]);
671
672         return 0;
673 }
674
675 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
676                                                 void *offset)
677 {
678         struct super_block *sb = seq->private;
679         struct f2fs_sb_info *sbi = F2FS_SB(sb);
680         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
681         int i;
682
683         seq_puts(seq, "format: victim_secmap bitmaps\n");
684
685         for (i = 0; i < MAIN_SECS(sbi); i++) {
686                 if ((i % 10) == 0)
687                         seq_printf(seq, "%-10d", i);
688                 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
689                 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
690                         seq_putc(seq, '\n');
691                 else
692                         seq_putc(seq, ' ');
693         }
694         return 0;
695 }
696
697 int __init f2fs_init_sysfs(void)
698 {
699         int ret;
700
701         kobject_set_name(&f2fs_kset.kobj, "f2fs");
702         f2fs_kset.kobj.parent = fs_kobj;
703         ret = kset_register(&f2fs_kset);
704         if (ret)
705                 return ret;
706
707         ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
708                                    NULL, "features");
709         if (ret)
710                 kset_unregister(&f2fs_kset);
711         else
712                 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
713         return ret;
714 }
715
716 void f2fs_exit_sysfs(void)
717 {
718         kobject_put(&f2fs_feat);
719         kset_unregister(&f2fs_kset);
720         remove_proc_entry("fs/f2fs", NULL);
721         f2fs_proc_root = NULL;
722 }
723
724 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
725 {
726         struct super_block *sb = sbi->sb;
727         int err;
728
729         sbi->s_kobj.kset = &f2fs_kset;
730         init_completion(&sbi->s_kobj_unregister);
731         err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
732                                 "%s", sb->s_id);
733         if (err)
734                 return err;
735
736         if (f2fs_proc_root)
737                 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
738
739         if (sbi->s_proc) {
740                 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
741                                 segment_info_seq_show, sb);
742                 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
743                                 segment_bits_seq_show, sb);
744                 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
745                                 iostat_info_seq_show, sb);
746                 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
747                                 victim_bits_seq_show, sb);
748         }
749         return 0;
750 }
751
752 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
753 {
754         if (sbi->s_proc) {
755                 remove_proc_entry("iostat_info", sbi->s_proc);
756                 remove_proc_entry("segment_info", sbi->s_proc);
757                 remove_proc_entry("segment_bits", sbi->s_proc);
758                 remove_proc_entry("victim_bits", sbi->s_proc);
759                 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
760         }
761         kobject_del(&sbi->s_kobj);
762 }