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