f2fs: fix kbytes written stat for multi-device case
[linux-2.6-microblaze.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 #include <trace/events/f2fs.h>
19
20 static struct proc_dir_entry *f2fs_proc_root;
21
22 /* Sysfs support for f2fs */
23 enum {
24         GC_THREAD,      /* struct f2fs_gc_thread */
25         SM_INFO,        /* struct f2fs_sm_info */
26         DCC_INFO,       /* struct discard_cmd_control */
27         NM_INFO,        /* struct f2fs_nm_info */
28         F2FS_SBI,       /* struct f2fs_sb_info */
29 #ifdef CONFIG_F2FS_STAT_FS
30         STAT_INFO,      /* struct f2fs_stat_info */
31 #endif
32 #ifdef CONFIG_F2FS_FAULT_INJECTION
33         FAULT_INFO_RATE,        /* struct f2fs_fault_info */
34         FAULT_INFO_TYPE,        /* struct f2fs_fault_info */
35 #endif
36         RESERVED_BLOCKS,        /* struct f2fs_sb_info */
37 };
38
39 struct f2fs_attr {
40         struct attribute attr;
41         ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
42         ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
43                          const char *, size_t);
44         int struct_type;
45         int offset;
46         int id;
47 };
48
49 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
50                              struct f2fs_sb_info *sbi, char *buf);
51
52 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
53 {
54         if (struct_type == GC_THREAD)
55                 return (unsigned char *)sbi->gc_thread;
56         else if (struct_type == SM_INFO)
57                 return (unsigned char *)SM_I(sbi);
58         else if (struct_type == DCC_INFO)
59                 return (unsigned char *)SM_I(sbi)->dcc_info;
60         else if (struct_type == NM_INFO)
61                 return (unsigned char *)NM_I(sbi);
62         else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
63                 return (unsigned char *)sbi;
64 #ifdef CONFIG_F2FS_FAULT_INJECTION
65         else if (struct_type == FAULT_INFO_RATE ||
66                                         struct_type == FAULT_INFO_TYPE)
67                 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
68 #endif
69 #ifdef CONFIG_F2FS_STAT_FS
70         else if (struct_type == STAT_INFO)
71                 return (unsigned char *)F2FS_STAT(sbi);
72 #endif
73         return NULL;
74 }
75
76 static ssize_t dirty_segments_show(struct f2fs_attr *a,
77                 struct f2fs_sb_info *sbi, char *buf)
78 {
79         return sprintf(buf, "%llu\n",
80                         (unsigned long long)(dirty_segments(sbi)));
81 }
82
83 static ssize_t free_segments_show(struct f2fs_attr *a,
84                 struct f2fs_sb_info *sbi, char *buf)
85 {
86         return sprintf(buf, "%llu\n",
87                         (unsigned long long)(free_segments(sbi)));
88 }
89
90 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
91                 struct f2fs_sb_info *sbi, char *buf)
92 {
93         struct super_block *sb = sbi->sb;
94
95         if (!sb->s_bdev->bd_part)
96                 return sprintf(buf, "0\n");
97
98         return sprintf(buf, "%llu\n",
99                         (unsigned long long)(sbi->kbytes_written +
100                         ((f2fs_get_sectors_written(sbi) -
101                                 sbi->sectors_written_start) >> 1)));
102 }
103
104 static ssize_t features_show(struct f2fs_attr *a,
105                 struct f2fs_sb_info *sbi, char *buf)
106 {
107         struct super_block *sb = sbi->sb;
108         int len = 0;
109
110         if (!sb->s_bdev->bd_part)
111                 return sprintf(buf, "0\n");
112
113         if (f2fs_sb_has_encrypt(sbi))
114                 len += scnprintf(buf, PAGE_SIZE - len, "%s",
115                                                 "encryption");
116         if (f2fs_sb_has_blkzoned(sbi))
117                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
118                                 len ? ", " : "", "blkzoned");
119         if (f2fs_sb_has_extra_attr(sbi))
120                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
121                                 len ? ", " : "", "extra_attr");
122         if (f2fs_sb_has_project_quota(sbi))
123                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
124                                 len ? ", " : "", "projquota");
125         if (f2fs_sb_has_inode_chksum(sbi))
126                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
127                                 len ? ", " : "", "inode_checksum");
128         if (f2fs_sb_has_flexible_inline_xattr(sbi))
129                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
130                                 len ? ", " : "", "flexible_inline_xattr");
131         if (f2fs_sb_has_quota_ino(sbi))
132                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
133                                 len ? ", " : "", "quota_ino");
134         if (f2fs_sb_has_inode_crtime(sbi))
135                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
136                                 len ? ", " : "", "inode_crtime");
137         if (f2fs_sb_has_lost_found(sbi))
138                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
139                                 len ? ", " : "", "lost_found");
140         if (f2fs_sb_has_verity(sbi))
141                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
142                                 len ? ", " : "", "verity");
143         if (f2fs_sb_has_sb_chksum(sbi))
144                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
145                                 len ? ", " : "", "sb_checksum");
146         if (f2fs_sb_has_casefold(sbi))
147                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
148                                 len ? ", " : "", "casefold");
149         if (f2fs_sb_has_compression(sbi))
150                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
151                                 len ? ", " : "", "compression");
152         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
153                                 len ? ", " : "", "pin_file");
154         len += scnprintf(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 sprintf(buf, "%u\n", sbi->current_reserved_blocks);
162 }
163
164 static ssize_t unusable_show(struct f2fs_attr *a,
165                 struct f2fs_sb_info *sbi, char *buf)
166 {
167         block_t unusable;
168
169         if (test_opt(sbi, DISABLE_CHECKPOINT))
170                 unusable = sbi->unusable_block_count;
171         else
172                 unusable = f2fs_get_unusable_blocks(sbi);
173         return sprintf(buf, "%llu\n", (unsigned long long)unusable);
174 }
175
176 static ssize_t encoding_show(struct f2fs_attr *a,
177                 struct f2fs_sb_info *sbi, char *buf)
178 {
179 #ifdef CONFIG_UNICODE
180         struct super_block *sb = sbi->sb;
181
182         if (f2fs_sb_has_casefold(sbi))
183                 return snprintf(buf, PAGE_SIZE, "%s (%d.%d.%d)\n",
184                         sb->s_encoding->charset,
185                         (sb->s_encoding->version >> 16) & 0xff,
186                         (sb->s_encoding->version >> 8) & 0xff,
187                         sb->s_encoding->version & 0xff);
188 #endif
189         return sprintf(buf, "(none)");
190 }
191
192 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
193                 struct f2fs_sb_info *sbi, char *buf)
194 {
195         return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
196 }
197
198 #ifdef CONFIG_F2FS_STAT_FS
199 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
200                                 struct f2fs_sb_info *sbi, char *buf)
201 {
202         struct f2fs_stat_info *si = F2FS_STAT(sbi);
203
204         return sprintf(buf, "%llu\n",
205                 (unsigned long long)(si->tot_blks -
206                         (si->bg_data_blks + si->bg_node_blks)));
207 }
208
209 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
210                                 struct f2fs_sb_info *sbi, char *buf)
211 {
212         struct f2fs_stat_info *si = F2FS_STAT(sbi);
213
214         return sprintf(buf, "%llu\n",
215                 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
216 }
217
218 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
219                 struct f2fs_sb_info *sbi, char *buf)
220 {
221         struct f2fs_stat_info *si = F2FS_STAT(sbi);
222
223         si->dirty_count = dirty_segments(sbi);
224         f2fs_update_sit_info(sbi);
225         return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
226 }
227 #endif
228
229 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
230                                 struct f2fs_sb_info *sbi, char *buf)
231 {
232         return snprintf(buf, PAGE_SIZE, "%llu\n",
233                         (unsigned long long)MAIN_BLKADDR(sbi));
234 }
235
236 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
237                         struct f2fs_sb_info *sbi, char *buf)
238 {
239         unsigned char *ptr = NULL;
240         unsigned int *ui;
241
242         ptr = __struct_ptr(sbi, a->struct_type);
243         if (!ptr)
244                 return -EINVAL;
245
246         if (!strcmp(a->attr.name, "extension_list")) {
247                 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
248                                         sbi->raw_super->extension_list;
249                 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
250                 int hot_count = sbi->raw_super->hot_ext_count;
251                 int len = 0, i;
252
253                 len += scnprintf(buf + len, PAGE_SIZE - len,
254                                                 "cold file extension:\n");
255                 for (i = 0; i < cold_count; i++)
256                         len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
257                                                                 extlist[i]);
258
259                 len += scnprintf(buf + len, PAGE_SIZE - len,
260                                                 "hot file extension:\n");
261                 for (i = cold_count; i < cold_count + hot_count; i++)
262                         len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
263                                                                 extlist[i]);
264                 return len;
265         }
266
267         ui = (unsigned int *)(ptr + a->offset);
268
269         return sprintf(buf, "%u\n", *ui);
270 }
271
272 static ssize_t __sbi_store(struct f2fs_attr *a,
273                         struct f2fs_sb_info *sbi,
274                         const char *buf, size_t count)
275 {
276         unsigned char *ptr;
277         unsigned long t;
278         unsigned int *ui;
279         ssize_t ret;
280
281         ptr = __struct_ptr(sbi, a->struct_type);
282         if (!ptr)
283                 return -EINVAL;
284
285         if (!strcmp(a->attr.name, "extension_list")) {
286                 const char *name = strim((char *)buf);
287                 bool set = true, hot;
288
289                 if (!strncmp(name, "[h]", 3))
290                         hot = true;
291                 else if (!strncmp(name, "[c]", 3))
292                         hot = false;
293                 else
294                         return -EINVAL;
295
296                 name += 3;
297
298                 if (*name == '!') {
299                         name++;
300                         set = false;
301                 }
302
303                 if (strlen(name) >= F2FS_EXTENSION_LEN)
304                         return -EINVAL;
305
306                 down_write(&sbi->sb_lock);
307
308                 ret = f2fs_update_extension_list(sbi, name, hot, set);
309                 if (ret)
310                         goto out;
311
312                 ret = f2fs_commit_super(sbi, false);
313                 if (ret)
314                         f2fs_update_extension_list(sbi, name, hot, !set);
315 out:
316                 up_write(&sbi->sb_lock);
317                 return ret ? ret : count;
318         }
319
320         ui = (unsigned int *)(ptr + a->offset);
321
322         ret = kstrtoul(skip_spaces(buf), 0, &t);
323         if (ret < 0)
324                 return ret;
325 #ifdef CONFIG_F2FS_FAULT_INJECTION
326         if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
327                 return -EINVAL;
328         if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
329                 return -EINVAL;
330 #endif
331         if (a->struct_type == RESERVED_BLOCKS) {
332                 spin_lock(&sbi->stat_lock);
333                 if (t > (unsigned long)(sbi->user_block_count -
334                                 F2FS_OPTION(sbi).root_reserved_blocks)) {
335                         spin_unlock(&sbi->stat_lock);
336                         return -EINVAL;
337                 }
338                 *ui = t;
339                 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
340                                 sbi->user_block_count - valid_user_blocks(sbi));
341                 spin_unlock(&sbi->stat_lock);
342                 return count;
343         }
344
345         if (!strcmp(a->attr.name, "discard_granularity")) {
346                 if (t == 0 || t > MAX_PLIST_NUM)
347                         return -EINVAL;
348                 if (t == *ui)
349                         return count;
350                 *ui = t;
351                 return count;
352         }
353
354         if (!strcmp(a->attr.name, "migration_granularity")) {
355                 if (t == 0 || t > sbi->segs_per_sec)
356                         return -EINVAL;
357         }
358
359         if (!strcmp(a->attr.name, "trim_sections"))
360                 return -EINVAL;
361
362         if (!strcmp(a->attr.name, "gc_urgent")) {
363                 if (t == 0) {
364                         sbi->gc_mode = GC_NORMAL;
365                 } else if (t == 1) {
366                         sbi->gc_mode = GC_URGENT_HIGH;
367                         if (sbi->gc_thread) {
368                                 sbi->gc_thread->gc_wake = 1;
369                                 wake_up_interruptible_all(
370                                         &sbi->gc_thread->gc_wait_queue_head);
371                                 wake_up_discard_thread(sbi, true);
372                         }
373                 } else if (t == 2) {
374                         sbi->gc_mode = GC_URGENT_LOW;
375                 } else {
376                         return -EINVAL;
377                 }
378                 return count;
379         }
380         if (!strcmp(a->attr.name, "gc_idle")) {
381                 if (t == GC_IDLE_CB) {
382                         sbi->gc_mode = GC_IDLE_CB;
383                 } else if (t == GC_IDLE_GREEDY) {
384                         sbi->gc_mode = GC_IDLE_GREEDY;
385                 } else if (t == GC_IDLE_AT) {
386                         if (!sbi->am.atgc_enabled)
387                                 return -EINVAL;
388                         sbi->gc_mode = GC_AT;
389                 } else {
390                         sbi->gc_mode = GC_NORMAL;
391                 }
392                 return count;
393         }
394
395         if (!strcmp(a->attr.name, "iostat_enable")) {
396                 sbi->iostat_enable = !!t;
397                 if (!sbi->iostat_enable)
398                         f2fs_reset_iostat(sbi);
399                 return count;
400         }
401
402         if (!strcmp(a->attr.name, "iostat_period_ms")) {
403                 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
404                         return -EINVAL;
405                 spin_lock(&sbi->iostat_lock);
406                 sbi->iostat_period_ms = (unsigned int)t;
407                 spin_unlock(&sbi->iostat_lock);
408                 return count;
409         }
410
411         *ui = (unsigned int)t;
412
413         return count;
414 }
415
416 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
417                         struct f2fs_sb_info *sbi,
418                         const char *buf, size_t count)
419 {
420         ssize_t ret;
421         bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
422                                         a->struct_type == GC_THREAD);
423
424         if (gc_entry) {
425                 if (!down_read_trylock(&sbi->sb->s_umount))
426                         return -EAGAIN;
427         }
428         ret = __sbi_store(a, sbi, buf, count);
429         if (gc_entry)
430                 up_read(&sbi->sb->s_umount);
431
432         return ret;
433 }
434
435 static ssize_t f2fs_attr_show(struct kobject *kobj,
436                                 struct attribute *attr, char *buf)
437 {
438         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
439                                                                 s_kobj);
440         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
441
442         return a->show ? a->show(a, sbi, buf) : 0;
443 }
444
445 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
446                                                 const char *buf, size_t len)
447 {
448         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
449                                                                         s_kobj);
450         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
451
452         return a->store ? a->store(a, sbi, buf, len) : 0;
453 }
454
455 static void f2fs_sb_release(struct kobject *kobj)
456 {
457         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
458                                                                 s_kobj);
459         complete(&sbi->s_kobj_unregister);
460 }
461
462 enum feat_id {
463         FEAT_CRYPTO = 0,
464         FEAT_BLKZONED,
465         FEAT_ATOMIC_WRITE,
466         FEAT_EXTRA_ATTR,
467         FEAT_PROJECT_QUOTA,
468         FEAT_INODE_CHECKSUM,
469         FEAT_FLEXIBLE_INLINE_XATTR,
470         FEAT_QUOTA_INO,
471         FEAT_INODE_CRTIME,
472         FEAT_LOST_FOUND,
473         FEAT_VERITY,
474         FEAT_SB_CHECKSUM,
475         FEAT_CASEFOLD,
476         FEAT_COMPRESSION,
477         FEAT_TEST_DUMMY_ENCRYPTION_V2,
478 };
479
480 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
481                 struct f2fs_sb_info *sbi, char *buf)
482 {
483         switch (a->id) {
484         case FEAT_CRYPTO:
485         case FEAT_BLKZONED:
486         case FEAT_ATOMIC_WRITE:
487         case FEAT_EXTRA_ATTR:
488         case FEAT_PROJECT_QUOTA:
489         case FEAT_INODE_CHECKSUM:
490         case FEAT_FLEXIBLE_INLINE_XATTR:
491         case FEAT_QUOTA_INO:
492         case FEAT_INODE_CRTIME:
493         case FEAT_LOST_FOUND:
494         case FEAT_VERITY:
495         case FEAT_SB_CHECKSUM:
496         case FEAT_CASEFOLD:
497         case FEAT_COMPRESSION:
498         case FEAT_TEST_DUMMY_ENCRYPTION_V2:
499                 return sprintf(buf, "supported\n");
500         }
501         return 0;
502 }
503
504 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
505 static struct f2fs_attr f2fs_attr_##_name = {                   \
506         .attr = {.name = __stringify(_name), .mode = _mode },   \
507         .show   = _show,                                        \
508         .store  = _store,                                       \
509         .struct_type = _struct_type,                            \
510         .offset = _offset                                       \
511 }
512
513 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)    \
514         F2FS_ATTR_OFFSET(struct_type, name, 0644,               \
515                 f2fs_sbi_show, f2fs_sbi_store,                  \
516                 offsetof(struct struct_name, elname))
517
518 #define F2FS_GENERAL_RO_ATTR(name) \
519 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
520
521 #define F2FS_FEATURE_RO_ATTR(_name, _id)                        \
522 static struct f2fs_attr f2fs_attr_##_name = {                   \
523         .attr = {.name = __stringify(_name), .mode = 0444 },    \
524         .show   = f2fs_feature_show,                            \
525         .id     = _id,                                          \
526 }
527
528 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname)      \
529 static struct f2fs_attr f2fs_attr_##_name = {                   \
530         .attr = {.name = __stringify(_name), .mode = 0444 },    \
531         .show = f2fs_sbi_show,                                  \
532         .struct_type = _struct_type,                            \
533         .offset = offsetof(struct _struct_name, _elname),       \
534 }
535
536 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
537                                                         urgent_sleep_time);
538 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
539 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
540 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
541 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
542 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
543 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
544 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
545 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
546 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
547 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
548 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
549 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
550 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
551 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
552 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
553 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
554 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
555 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
556 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
557 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
558 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
559 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
560 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
561 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
562 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
563                                         interval_time[DISCARD_TIME]);
564 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
565 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
566                 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
567 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
568 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
569 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
570 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
571 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
572 #ifdef CONFIG_F2FS_FAULT_INJECTION
573 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
574 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
575 #endif
576 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
577 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
578 F2FS_GENERAL_RO_ATTR(dirty_segments);
579 F2FS_GENERAL_RO_ATTR(free_segments);
580 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
581 F2FS_GENERAL_RO_ATTR(features);
582 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
583 F2FS_GENERAL_RO_ATTR(unusable);
584 F2FS_GENERAL_RO_ATTR(encoding);
585 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
586 F2FS_GENERAL_RO_ATTR(main_blkaddr);
587 #ifdef CONFIG_F2FS_STAT_FS
588 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
589 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
590 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
591 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
592 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
593 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
594 F2FS_GENERAL_RO_ATTR(avg_vblocks);
595 #endif
596
597 #ifdef CONFIG_FS_ENCRYPTION
598 F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
599 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2, FEAT_TEST_DUMMY_ENCRYPTION_V2);
600 #endif
601 #ifdef CONFIG_BLK_DEV_ZONED
602 F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
603 #endif
604 F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
605 F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
606 F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
607 F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
608 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
609 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
610 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
611 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
612 #ifdef CONFIG_FS_VERITY
613 F2FS_FEATURE_RO_ATTR(verity, FEAT_VERITY);
614 #endif
615 F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
616 F2FS_FEATURE_RO_ATTR(casefold, FEAT_CASEFOLD);
617 #ifdef CONFIG_F2FS_FS_COMPRESSION
618 F2FS_FEATURE_RO_ATTR(compression, FEAT_COMPRESSION);
619 #endif
620
621 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
622 static struct attribute *f2fs_attrs[] = {
623         ATTR_LIST(gc_urgent_sleep_time),
624         ATTR_LIST(gc_min_sleep_time),
625         ATTR_LIST(gc_max_sleep_time),
626         ATTR_LIST(gc_no_gc_sleep_time),
627         ATTR_LIST(gc_idle),
628         ATTR_LIST(gc_urgent),
629         ATTR_LIST(reclaim_segments),
630         ATTR_LIST(main_blkaddr),
631         ATTR_LIST(max_small_discards),
632         ATTR_LIST(discard_granularity),
633         ATTR_LIST(batched_trim_sections),
634         ATTR_LIST(ipu_policy),
635         ATTR_LIST(min_ipu_util),
636         ATTR_LIST(min_fsync_blocks),
637         ATTR_LIST(min_seq_blocks),
638         ATTR_LIST(min_hot_blocks),
639         ATTR_LIST(min_ssr_sections),
640         ATTR_LIST(max_victim_search),
641         ATTR_LIST(migration_granularity),
642         ATTR_LIST(dir_level),
643         ATTR_LIST(ram_thresh),
644         ATTR_LIST(ra_nid_pages),
645         ATTR_LIST(dirty_nats_ratio),
646         ATTR_LIST(cp_interval),
647         ATTR_LIST(idle_interval),
648         ATTR_LIST(discard_idle_interval),
649         ATTR_LIST(gc_idle_interval),
650         ATTR_LIST(umount_discard_timeout),
651         ATTR_LIST(iostat_enable),
652         ATTR_LIST(iostat_period_ms),
653         ATTR_LIST(readdir_ra),
654         ATTR_LIST(gc_pin_file_thresh),
655         ATTR_LIST(extension_list),
656 #ifdef CONFIG_F2FS_FAULT_INJECTION
657         ATTR_LIST(inject_rate),
658         ATTR_LIST(inject_type),
659 #endif
660         ATTR_LIST(data_io_flag),
661         ATTR_LIST(node_io_flag),
662         ATTR_LIST(dirty_segments),
663         ATTR_LIST(free_segments),
664         ATTR_LIST(unusable),
665         ATTR_LIST(lifetime_write_kbytes),
666         ATTR_LIST(features),
667         ATTR_LIST(reserved_blocks),
668         ATTR_LIST(current_reserved_blocks),
669         ATTR_LIST(encoding),
670         ATTR_LIST(mounted_time_sec),
671 #ifdef CONFIG_F2FS_STAT_FS
672         ATTR_LIST(cp_foreground_calls),
673         ATTR_LIST(cp_background_calls),
674         ATTR_LIST(gc_foreground_calls),
675         ATTR_LIST(gc_background_calls),
676         ATTR_LIST(moved_blocks_foreground),
677         ATTR_LIST(moved_blocks_background),
678         ATTR_LIST(avg_vblocks),
679 #endif
680         NULL,
681 };
682 ATTRIBUTE_GROUPS(f2fs);
683
684 static struct attribute *f2fs_feat_attrs[] = {
685 #ifdef CONFIG_FS_ENCRYPTION
686         ATTR_LIST(encryption),
687         ATTR_LIST(test_dummy_encryption_v2),
688 #endif
689 #ifdef CONFIG_BLK_DEV_ZONED
690         ATTR_LIST(block_zoned),
691 #endif
692         ATTR_LIST(atomic_write),
693         ATTR_LIST(extra_attr),
694         ATTR_LIST(project_quota),
695         ATTR_LIST(inode_checksum),
696         ATTR_LIST(flexible_inline_xattr),
697         ATTR_LIST(quota_ino),
698         ATTR_LIST(inode_crtime),
699         ATTR_LIST(lost_found),
700 #ifdef CONFIG_FS_VERITY
701         ATTR_LIST(verity),
702 #endif
703         ATTR_LIST(sb_checksum),
704         ATTR_LIST(casefold),
705 #ifdef CONFIG_F2FS_FS_COMPRESSION
706         ATTR_LIST(compression),
707 #endif
708         NULL,
709 };
710 ATTRIBUTE_GROUPS(f2fs_feat);
711
712 static const struct sysfs_ops f2fs_attr_ops = {
713         .show   = f2fs_attr_show,
714         .store  = f2fs_attr_store,
715 };
716
717 static struct kobj_type f2fs_sb_ktype = {
718         .default_groups = f2fs_groups,
719         .sysfs_ops      = &f2fs_attr_ops,
720         .release        = f2fs_sb_release,
721 };
722
723 static struct kobj_type f2fs_ktype = {
724         .sysfs_ops      = &f2fs_attr_ops,
725 };
726
727 static struct kset f2fs_kset = {
728         .kobj   = {.ktype = &f2fs_ktype},
729 };
730
731 static struct kobj_type f2fs_feat_ktype = {
732         .default_groups = f2fs_feat_groups,
733         .sysfs_ops      = &f2fs_attr_ops,
734 };
735
736 static struct kobject f2fs_feat = {
737         .kset   = &f2fs_kset,
738 };
739
740 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
741                                                 void *offset)
742 {
743         struct super_block *sb = seq->private;
744         struct f2fs_sb_info *sbi = F2FS_SB(sb);
745         unsigned int total_segs =
746                         le32_to_cpu(sbi->raw_super->segment_count_main);
747         int i;
748
749         seq_puts(seq, "format: segment_type|valid_blocks\n"
750                 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
751
752         for (i = 0; i < total_segs; i++) {
753                 struct seg_entry *se = get_seg_entry(sbi, i);
754
755                 if ((i % 10) == 0)
756                         seq_printf(seq, "%-10d", i);
757                 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
758                 if ((i % 10) == 9 || i == (total_segs - 1))
759                         seq_putc(seq, '\n');
760                 else
761                         seq_putc(seq, ' ');
762         }
763
764         return 0;
765 }
766
767 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
768                                                 void *offset)
769 {
770         struct super_block *sb = seq->private;
771         struct f2fs_sb_info *sbi = F2FS_SB(sb);
772         unsigned int total_segs =
773                         le32_to_cpu(sbi->raw_super->segment_count_main);
774         int i, j;
775
776         seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
777                 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
778
779         for (i = 0; i < total_segs; i++) {
780                 struct seg_entry *se = get_seg_entry(sbi, i);
781
782                 seq_printf(seq, "%-10d", i);
783                 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
784                 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
785                         seq_printf(seq, " %.2x", se->cur_valid_map[j]);
786                 seq_putc(seq, '\n');
787         }
788         return 0;
789 }
790
791 void f2fs_record_iostat(struct f2fs_sb_info *sbi)
792 {
793         unsigned long long iostat_diff[NR_IO_TYPE];
794         int i;
795
796         if (time_is_after_jiffies(sbi->iostat_next_period))
797                 return;
798
799         /* Need double check under the lock */
800         spin_lock(&sbi->iostat_lock);
801         if (time_is_after_jiffies(sbi->iostat_next_period)) {
802                 spin_unlock(&sbi->iostat_lock);
803                 return;
804         }
805         sbi->iostat_next_period = jiffies +
806                                 msecs_to_jiffies(sbi->iostat_period_ms);
807
808         for (i = 0; i < NR_IO_TYPE; i++) {
809                 iostat_diff[i] = sbi->rw_iostat[i] -
810                                 sbi->prev_rw_iostat[i];
811                 sbi->prev_rw_iostat[i] = sbi->rw_iostat[i];
812         }
813         spin_unlock(&sbi->iostat_lock);
814
815         trace_f2fs_iostat(sbi, iostat_diff);
816 }
817
818 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
819                                                void *offset)
820 {
821         struct super_block *sb = seq->private;
822         struct f2fs_sb_info *sbi = F2FS_SB(sb);
823         time64_t now = ktime_get_real_seconds();
824
825         if (!sbi->iostat_enable)
826                 return 0;
827
828         seq_printf(seq, "time:          %-16llu\n", now);
829
830         /* print app write IOs */
831         seq_puts(seq, "[WRITE]\n");
832         seq_printf(seq, "app buffered:  %-16llu\n",
833                                 sbi->rw_iostat[APP_BUFFERED_IO]);
834         seq_printf(seq, "app direct:    %-16llu\n",
835                                 sbi->rw_iostat[APP_DIRECT_IO]);
836         seq_printf(seq, "app mapped:    %-16llu\n",
837                                 sbi->rw_iostat[APP_MAPPED_IO]);
838
839         /* print fs write IOs */
840         seq_printf(seq, "fs data:       %-16llu\n",
841                                 sbi->rw_iostat[FS_DATA_IO]);
842         seq_printf(seq, "fs node:       %-16llu\n",
843                                 sbi->rw_iostat[FS_NODE_IO]);
844         seq_printf(seq, "fs meta:       %-16llu\n",
845                                 sbi->rw_iostat[FS_META_IO]);
846         seq_printf(seq, "fs gc data:    %-16llu\n",
847                                 sbi->rw_iostat[FS_GC_DATA_IO]);
848         seq_printf(seq, "fs gc node:    %-16llu\n",
849                                 sbi->rw_iostat[FS_GC_NODE_IO]);
850         seq_printf(seq, "fs cp data:    %-16llu\n",
851                                 sbi->rw_iostat[FS_CP_DATA_IO]);
852         seq_printf(seq, "fs cp node:    %-16llu\n",
853                                 sbi->rw_iostat[FS_CP_NODE_IO]);
854         seq_printf(seq, "fs cp meta:    %-16llu\n",
855                                 sbi->rw_iostat[FS_CP_META_IO]);
856
857         /* print app read IOs */
858         seq_puts(seq, "[READ]\n");
859         seq_printf(seq, "app buffered:  %-16llu\n",
860                                 sbi->rw_iostat[APP_BUFFERED_READ_IO]);
861         seq_printf(seq, "app direct:    %-16llu\n",
862                                 sbi->rw_iostat[APP_DIRECT_READ_IO]);
863         seq_printf(seq, "app mapped:    %-16llu\n",
864                                 sbi->rw_iostat[APP_MAPPED_READ_IO]);
865
866         /* print fs read IOs */
867         seq_printf(seq, "fs data:       %-16llu\n",
868                                 sbi->rw_iostat[FS_DATA_READ_IO]);
869         seq_printf(seq, "fs gc data:    %-16llu\n",
870                                 sbi->rw_iostat[FS_GDATA_READ_IO]);
871         seq_printf(seq, "fs compr_data: %-16llu\n",
872                                 sbi->rw_iostat[FS_CDATA_READ_IO]);
873         seq_printf(seq, "fs node:       %-16llu\n",
874                                 sbi->rw_iostat[FS_NODE_READ_IO]);
875         seq_printf(seq, "fs meta:       %-16llu\n",
876                                 sbi->rw_iostat[FS_META_READ_IO]);
877
878         /* print other IOs */
879         seq_puts(seq, "[OTHER]\n");
880         seq_printf(seq, "fs discard:    %-16llu\n",
881                                 sbi->rw_iostat[FS_DISCARD]);
882
883         return 0;
884 }
885
886 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
887                                                 void *offset)
888 {
889         struct super_block *sb = seq->private;
890         struct f2fs_sb_info *sbi = F2FS_SB(sb);
891         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
892         int i;
893
894         seq_puts(seq, "format: victim_secmap bitmaps\n");
895
896         for (i = 0; i < MAIN_SECS(sbi); i++) {
897                 if ((i % 10) == 0)
898                         seq_printf(seq, "%-10d", i);
899                 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
900                 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
901                         seq_putc(seq, '\n');
902                 else
903                         seq_putc(seq, ' ');
904         }
905         return 0;
906 }
907
908 int __init f2fs_init_sysfs(void)
909 {
910         int ret;
911
912         kobject_set_name(&f2fs_kset.kobj, "f2fs");
913         f2fs_kset.kobj.parent = fs_kobj;
914         ret = kset_register(&f2fs_kset);
915         if (ret)
916                 return ret;
917
918         ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
919                                    NULL, "features");
920         if (ret) {
921                 kobject_put(&f2fs_feat);
922                 kset_unregister(&f2fs_kset);
923         } else {
924                 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
925         }
926         return ret;
927 }
928
929 void f2fs_exit_sysfs(void)
930 {
931         kobject_put(&f2fs_feat);
932         kset_unregister(&f2fs_kset);
933         remove_proc_entry("fs/f2fs", NULL);
934         f2fs_proc_root = NULL;
935 }
936
937 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
938 {
939         struct super_block *sb = sbi->sb;
940         int err;
941
942         sbi->s_kobj.kset = &f2fs_kset;
943         init_completion(&sbi->s_kobj_unregister);
944         err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
945                                 "%s", sb->s_id);
946         if (err) {
947                 kobject_put(&sbi->s_kobj);
948                 wait_for_completion(&sbi->s_kobj_unregister);
949                 return err;
950         }
951
952         if (f2fs_proc_root)
953                 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
954
955         if (sbi->s_proc) {
956                 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
957                                 segment_info_seq_show, sb);
958                 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
959                                 segment_bits_seq_show, sb);
960                 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
961                                 iostat_info_seq_show, sb);
962                 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
963                                 victim_bits_seq_show, sb);
964         }
965         return 0;
966 }
967
968 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
969 {
970         if (sbi->s_proc) {
971                 remove_proc_entry("iostat_info", sbi->s_proc);
972                 remove_proc_entry("segment_info", sbi->s_proc);
973                 remove_proc_entry("segment_bits", sbi->s_proc);
974                 remove_proc_entry("victim_bits", sbi->s_proc);
975                 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
976         }
977         kobject_del(&sbi->s_kobj);
978         kobject_put(&sbi->s_kobj);
979         wait_for_completion(&sbi->s_kobj_unregister);
980 }