Merge tag 'unicode-for-next-5.17' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 #include <linux/ioprio.h>
15 #include <linux/sysfs.h>
16
17 #include "f2fs.h"
18 #include "segment.h"
19 #include "gc.h"
20 #include "iostat.h"
21 #include <trace/events/f2fs.h>
22
23 static struct proc_dir_entry *f2fs_proc_root;
24
25 /* Sysfs support for f2fs */
26 enum {
27         GC_THREAD,      /* struct f2fs_gc_thread */
28         SM_INFO,        /* struct f2fs_sm_info */
29         DCC_INFO,       /* struct discard_cmd_control */
30         NM_INFO,        /* struct f2fs_nm_info */
31         F2FS_SBI,       /* struct f2fs_sb_info */
32 #ifdef CONFIG_F2FS_STAT_FS
33         STAT_INFO,      /* struct f2fs_stat_info */
34 #endif
35 #ifdef CONFIG_F2FS_FAULT_INJECTION
36         FAULT_INFO_RATE,        /* struct f2fs_fault_info */
37         FAULT_INFO_TYPE,        /* struct f2fs_fault_info */
38 #endif
39         RESERVED_BLOCKS,        /* struct f2fs_sb_info */
40         CPRC_INFO,      /* struct ckpt_req_control */
41         ATGC_INFO,      /* struct atgc_management */
42 };
43
44 struct f2fs_attr {
45         struct attribute attr;
46         ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
47         ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
48                          const char *, size_t);
49         int struct_type;
50         int offset;
51         int id;
52 };
53
54 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
55                              struct f2fs_sb_info *sbi, char *buf);
56
57 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
58 {
59         if (struct_type == GC_THREAD)
60                 return (unsigned char *)sbi->gc_thread;
61         else if (struct_type == SM_INFO)
62                 return (unsigned char *)SM_I(sbi);
63         else if (struct_type == DCC_INFO)
64                 return (unsigned char *)SM_I(sbi)->dcc_info;
65         else if (struct_type == NM_INFO)
66                 return (unsigned char *)NM_I(sbi);
67         else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
68                 return (unsigned char *)sbi;
69 #ifdef CONFIG_F2FS_FAULT_INJECTION
70         else if (struct_type == FAULT_INFO_RATE ||
71                                         struct_type == FAULT_INFO_TYPE)
72                 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
73 #endif
74 #ifdef CONFIG_F2FS_STAT_FS
75         else if (struct_type == STAT_INFO)
76                 return (unsigned char *)F2FS_STAT(sbi);
77 #endif
78         else if (struct_type == CPRC_INFO)
79                 return (unsigned char *)&sbi->cprc_info;
80         else if (struct_type == ATGC_INFO)
81                 return (unsigned char *)&sbi->am;
82         return NULL;
83 }
84
85 static ssize_t dirty_segments_show(struct f2fs_attr *a,
86                 struct f2fs_sb_info *sbi, char *buf)
87 {
88         return sprintf(buf, "%llu\n",
89                         (unsigned long long)(dirty_segments(sbi)));
90 }
91
92 static ssize_t free_segments_show(struct f2fs_attr *a,
93                 struct f2fs_sb_info *sbi, char *buf)
94 {
95         return sprintf(buf, "%llu\n",
96                         (unsigned long long)(free_segments(sbi)));
97 }
98
99 static ssize_t ovp_segments_show(struct f2fs_attr *a,
100                 struct f2fs_sb_info *sbi, char *buf)
101 {
102         return sprintf(buf, "%llu\n",
103                         (unsigned long long)(overprovision_segments(sbi)));
104 }
105
106 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
107                 struct f2fs_sb_info *sbi, char *buf)
108 {
109         return sprintf(buf, "%llu\n",
110                         (unsigned long long)(sbi->kbytes_written +
111                         ((f2fs_get_sectors_written(sbi) -
112                                 sbi->sectors_written_start) >> 1)));
113 }
114
115 static ssize_t sb_status_show(struct f2fs_attr *a,
116                 struct f2fs_sb_info *sbi, char *buf)
117 {
118         return sprintf(buf, "%lx\n", sbi->s_flag);
119 }
120
121 static ssize_t features_show(struct f2fs_attr *a,
122                 struct f2fs_sb_info *sbi, char *buf)
123 {
124         int len = 0;
125
126         if (f2fs_sb_has_encrypt(sbi))
127                 len += scnprintf(buf, PAGE_SIZE - len, "%s",
128                                                 "encryption");
129         if (f2fs_sb_has_blkzoned(sbi))
130                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
131                                 len ? ", " : "", "blkzoned");
132         if (f2fs_sb_has_extra_attr(sbi))
133                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
134                                 len ? ", " : "", "extra_attr");
135         if (f2fs_sb_has_project_quota(sbi))
136                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
137                                 len ? ", " : "", "projquota");
138         if (f2fs_sb_has_inode_chksum(sbi))
139                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
140                                 len ? ", " : "", "inode_checksum");
141         if (f2fs_sb_has_flexible_inline_xattr(sbi))
142                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
143                                 len ? ", " : "", "flexible_inline_xattr");
144         if (f2fs_sb_has_quota_ino(sbi))
145                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
146                                 len ? ", " : "", "quota_ino");
147         if (f2fs_sb_has_inode_crtime(sbi))
148                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
149                                 len ? ", " : "", "inode_crtime");
150         if (f2fs_sb_has_lost_found(sbi))
151                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
152                                 len ? ", " : "", "lost_found");
153         if (f2fs_sb_has_verity(sbi))
154                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
155                                 len ? ", " : "", "verity");
156         if (f2fs_sb_has_sb_chksum(sbi))
157                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
158                                 len ? ", " : "", "sb_checksum");
159         if (f2fs_sb_has_casefold(sbi))
160                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
161                                 len ? ", " : "", "casefold");
162         if (f2fs_sb_has_readonly(sbi))
163                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
164                                 len ? ", " : "", "readonly");
165         if (f2fs_sb_has_compression(sbi))
166                 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
167                                 len ? ", " : "", "compression");
168         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
169                                 len ? ", " : "", "pin_file");
170         len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
171         return len;
172 }
173
174 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
175                                         struct f2fs_sb_info *sbi, char *buf)
176 {
177         return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
178 }
179
180 static ssize_t unusable_show(struct f2fs_attr *a,
181                 struct f2fs_sb_info *sbi, char *buf)
182 {
183         block_t unusable;
184
185         if (test_opt(sbi, DISABLE_CHECKPOINT))
186                 unusable = sbi->unusable_block_count;
187         else
188                 unusable = f2fs_get_unusable_blocks(sbi);
189         return sprintf(buf, "%llu\n", (unsigned long long)unusable);
190 }
191
192 static ssize_t encoding_show(struct f2fs_attr *a,
193                 struct f2fs_sb_info *sbi, char *buf)
194 {
195 #ifdef CONFIG_UNICODE
196         struct super_block *sb = sbi->sb;
197
198         if (f2fs_sb_has_casefold(sbi))
199                 return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
200                         (sb->s_encoding->version >> 16) & 0xff,
201                         (sb->s_encoding->version >> 8) & 0xff,
202                         sb->s_encoding->version & 0xff);
203 #endif
204         return sprintf(buf, "(none)");
205 }
206
207 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
208                 struct f2fs_sb_info *sbi, char *buf)
209 {
210         return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
211 }
212
213 #ifdef CONFIG_F2FS_STAT_FS
214 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
215                                 struct f2fs_sb_info *sbi, char *buf)
216 {
217         struct f2fs_stat_info *si = F2FS_STAT(sbi);
218
219         return sprintf(buf, "%llu\n",
220                 (unsigned long long)(si->tot_blks -
221                         (si->bg_data_blks + si->bg_node_blks)));
222 }
223
224 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
225                                 struct f2fs_sb_info *sbi, char *buf)
226 {
227         struct f2fs_stat_info *si = F2FS_STAT(sbi);
228
229         return sprintf(buf, "%llu\n",
230                 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
231 }
232
233 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
234                 struct f2fs_sb_info *sbi, char *buf)
235 {
236         struct f2fs_stat_info *si = F2FS_STAT(sbi);
237
238         si->dirty_count = dirty_segments(sbi);
239         f2fs_update_sit_info(sbi);
240         return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
241 }
242 #endif
243
244 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
245                                 struct f2fs_sb_info *sbi, char *buf)
246 {
247         return sysfs_emit(buf, "%llu\n",
248                         (unsigned long long)MAIN_BLKADDR(sbi));
249 }
250
251 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
252                         struct f2fs_sb_info *sbi, char *buf)
253 {
254         unsigned char *ptr = NULL;
255         unsigned int *ui;
256
257         ptr = __struct_ptr(sbi, a->struct_type);
258         if (!ptr)
259                 return -EINVAL;
260
261         if (!strcmp(a->attr.name, "extension_list")) {
262                 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
263                                         sbi->raw_super->extension_list;
264                 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
265                 int hot_count = sbi->raw_super->hot_ext_count;
266                 int len = 0, i;
267
268                 len += scnprintf(buf + len, PAGE_SIZE - len,
269                                                 "cold file extension:\n");
270                 for (i = 0; i < cold_count; i++)
271                         len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
272                                                                 extlist[i]);
273
274                 len += scnprintf(buf + len, PAGE_SIZE - len,
275                                                 "hot file extension:\n");
276                 for (i = cold_count; i < cold_count + hot_count; i++)
277                         len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
278                                                                 extlist[i]);
279                 return len;
280         }
281
282         if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
283                 struct ckpt_req_control *cprc = &sbi->cprc_info;
284                 int len = 0;
285                 int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
286                 int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
287
288                 if (class == IOPRIO_CLASS_RT)
289                         len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
290                 else if (class == IOPRIO_CLASS_BE)
291                         len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
292                 else
293                         return -EINVAL;
294
295                 len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
296                 return len;
297         }
298
299 #ifdef CONFIG_F2FS_FS_COMPRESSION
300         if (!strcmp(a->attr.name, "compr_written_block"))
301                 return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
302
303         if (!strcmp(a->attr.name, "compr_saved_block"))
304                 return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
305
306         if (!strcmp(a->attr.name, "compr_new_inode"))
307                 return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
308 #endif
309
310         if (!strcmp(a->attr.name, "gc_segment_mode"))
311                 return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
312
313         if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
314                 return sysfs_emit(buf, "%u\n",
315                         sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
316         }
317
318         ui = (unsigned int *)(ptr + a->offset);
319
320         return sprintf(buf, "%u\n", *ui);
321 }
322
323 static ssize_t __sbi_store(struct f2fs_attr *a,
324                         struct f2fs_sb_info *sbi,
325                         const char *buf, size_t count)
326 {
327         unsigned char *ptr;
328         unsigned long t;
329         unsigned int *ui;
330         ssize_t ret;
331
332         ptr = __struct_ptr(sbi, a->struct_type);
333         if (!ptr)
334                 return -EINVAL;
335
336         if (!strcmp(a->attr.name, "extension_list")) {
337                 const char *name = strim((char *)buf);
338                 bool set = true, hot;
339
340                 if (!strncmp(name, "[h]", 3))
341                         hot = true;
342                 else if (!strncmp(name, "[c]", 3))
343                         hot = false;
344                 else
345                         return -EINVAL;
346
347                 name += 3;
348
349                 if (*name == '!') {
350                         name++;
351                         set = false;
352                 }
353
354                 if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
355                         return -EINVAL;
356
357                 down_write(&sbi->sb_lock);
358
359                 ret = f2fs_update_extension_list(sbi, name, hot, set);
360                 if (ret)
361                         goto out;
362
363                 ret = f2fs_commit_super(sbi, false);
364                 if (ret)
365                         f2fs_update_extension_list(sbi, name, hot, !set);
366 out:
367                 up_write(&sbi->sb_lock);
368                 return ret ? ret : count;
369         }
370
371         if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
372                 const char *name = strim((char *)buf);
373                 struct ckpt_req_control *cprc = &sbi->cprc_info;
374                 int class;
375                 long data;
376                 int ret;
377
378                 if (!strncmp(name, "rt,", 3))
379                         class = IOPRIO_CLASS_RT;
380                 else if (!strncmp(name, "be,", 3))
381                         class = IOPRIO_CLASS_BE;
382                 else
383                         return -EINVAL;
384
385                 name += 3;
386                 ret = kstrtol(name, 10, &data);
387                 if (ret)
388                         return ret;
389                 if (data >= IOPRIO_NR_LEVELS || data < 0)
390                         return -EINVAL;
391
392                 cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
393                 if (test_opt(sbi, MERGE_CHECKPOINT)) {
394                         ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
395                                         cprc->ckpt_thread_ioprio);
396                         if (ret)
397                                 return ret;
398                 }
399
400                 return count;
401         }
402
403         ui = (unsigned int *)(ptr + a->offset);
404
405         ret = kstrtoul(skip_spaces(buf), 0, &t);
406         if (ret < 0)
407                 return ret;
408 #ifdef CONFIG_F2FS_FAULT_INJECTION
409         if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
410                 return -EINVAL;
411         if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
412                 return -EINVAL;
413 #endif
414         if (a->struct_type == RESERVED_BLOCKS) {
415                 spin_lock(&sbi->stat_lock);
416                 if (t > (unsigned long)(sbi->user_block_count -
417                                 F2FS_OPTION(sbi).root_reserved_blocks)) {
418                         spin_unlock(&sbi->stat_lock);
419                         return -EINVAL;
420                 }
421                 *ui = t;
422                 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
423                                 sbi->user_block_count - valid_user_blocks(sbi));
424                 spin_unlock(&sbi->stat_lock);
425                 return count;
426         }
427
428         if (!strcmp(a->attr.name, "discard_granularity")) {
429                 if (t == 0 || t > MAX_PLIST_NUM)
430                         return -EINVAL;
431                 if (!f2fs_block_unit_discard(sbi))
432                         return -EINVAL;
433                 if (t == *ui)
434                         return count;
435                 *ui = t;
436                 return count;
437         }
438
439         if (!strcmp(a->attr.name, "migration_granularity")) {
440                 if (t == 0 || t > sbi->segs_per_sec)
441                         return -EINVAL;
442         }
443
444         if (!strcmp(a->attr.name, "trim_sections"))
445                 return -EINVAL;
446
447         if (!strcmp(a->attr.name, "gc_urgent")) {
448                 if (t == 0) {
449                         sbi->gc_mode = GC_NORMAL;
450                 } else if (t == 1) {
451                         sbi->gc_mode = GC_URGENT_HIGH;
452                         if (sbi->gc_thread) {
453                                 sbi->gc_thread->gc_wake = 1;
454                                 wake_up_interruptible_all(
455                                         &sbi->gc_thread->gc_wait_queue_head);
456                                 wake_up_discard_thread(sbi, true);
457                         }
458                 } else if (t == 2) {
459                         sbi->gc_mode = GC_URGENT_LOW;
460                 } else {
461                         return -EINVAL;
462                 }
463                 return count;
464         }
465         if (!strcmp(a->attr.name, "gc_idle")) {
466                 if (t == GC_IDLE_CB) {
467                         sbi->gc_mode = GC_IDLE_CB;
468                 } else if (t == GC_IDLE_GREEDY) {
469                         sbi->gc_mode = GC_IDLE_GREEDY;
470                 } else if (t == GC_IDLE_AT) {
471                         if (!sbi->am.atgc_enabled)
472                                 return -EINVAL;
473                         sbi->gc_mode = GC_AT;
474                 } else {
475                         sbi->gc_mode = GC_NORMAL;
476                 }
477                 return count;
478         }
479
480 #ifdef CONFIG_F2FS_IOSTAT
481         if (!strcmp(a->attr.name, "iostat_enable")) {
482                 sbi->iostat_enable = !!t;
483                 if (!sbi->iostat_enable)
484                         f2fs_reset_iostat(sbi);
485                 return count;
486         }
487
488         if (!strcmp(a->attr.name, "iostat_period_ms")) {
489                 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
490                         return -EINVAL;
491                 spin_lock(&sbi->iostat_lock);
492                 sbi->iostat_period_ms = (unsigned int)t;
493                 spin_unlock(&sbi->iostat_lock);
494                 return count;
495         }
496 #endif
497
498 #ifdef CONFIG_F2FS_FS_COMPRESSION
499         if (!strcmp(a->attr.name, "compr_written_block") ||
500                 !strcmp(a->attr.name, "compr_saved_block")) {
501                 if (t != 0)
502                         return -EINVAL;
503                 sbi->compr_written_block = 0;
504                 sbi->compr_saved_block = 0;
505                 return count;
506         }
507
508         if (!strcmp(a->attr.name, "compr_new_inode")) {
509                 if (t != 0)
510                         return -EINVAL;
511                 sbi->compr_new_inode = 0;
512                 return count;
513         }
514 #endif
515
516         if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
517                 if (t > 100)
518                         return -EINVAL;
519                 sbi->am.candidate_ratio = t;
520                 return count;
521         }
522
523         if (!strcmp(a->attr.name, "atgc_age_weight")) {
524                 if (t > 100)
525                         return -EINVAL;
526                 sbi->am.age_weight = t;
527                 return count;
528         }
529
530         if (!strcmp(a->attr.name, "gc_segment_mode")) {
531                 if (t < MAX_GC_MODE)
532                         sbi->gc_segment_mode = t;
533                 else
534                         return -EINVAL;
535                 return count;
536         }
537
538         if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
539                 if (t != 0)
540                         return -EINVAL;
541                 sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
542                 return count;
543         }
544
545         if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
546                 if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
547                         sbi->seq_file_ra_mul = t;
548                 else
549                         return -EINVAL;
550                 return count;
551         }
552
553         if (!strcmp(a->attr.name, "max_fragment_chunk")) {
554                 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
555                         sbi->max_fragment_chunk = t;
556                 else
557                         return -EINVAL;
558                 return count;
559         }
560
561         if (!strcmp(a->attr.name, "max_fragment_hole")) {
562                 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
563                         sbi->max_fragment_hole = t;
564                 else
565                         return -EINVAL;
566                 return count;
567         }
568
569         *ui = (unsigned int)t;
570
571         return count;
572 }
573
574 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
575                         struct f2fs_sb_info *sbi,
576                         const char *buf, size_t count)
577 {
578         ssize_t ret;
579         bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
580                                         a->struct_type == GC_THREAD);
581
582         if (gc_entry) {
583                 if (!down_read_trylock(&sbi->sb->s_umount))
584                         return -EAGAIN;
585         }
586         ret = __sbi_store(a, sbi, buf, count);
587         if (gc_entry)
588                 up_read(&sbi->sb->s_umount);
589
590         return ret;
591 }
592
593 static ssize_t f2fs_attr_show(struct kobject *kobj,
594                                 struct attribute *attr, char *buf)
595 {
596         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
597                                                                 s_kobj);
598         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
599
600         return a->show ? a->show(a, sbi, buf) : 0;
601 }
602
603 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
604                                                 const char *buf, size_t len)
605 {
606         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
607                                                                         s_kobj);
608         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
609
610         return a->store ? a->store(a, sbi, buf, len) : 0;
611 }
612
613 static void f2fs_sb_release(struct kobject *kobj)
614 {
615         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
616                                                                 s_kobj);
617         complete(&sbi->s_kobj_unregister);
618 }
619
620 /*
621  * Note that there are three feature list entries:
622  * 1) /sys/fs/f2fs/features
623  *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
624  *     - ref. F2FS_FEATURE_RO_ATTR()
625  *
626  * 2) /sys/fs/f2fs/$s_id/features <deprecated>
627  *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
628  *     won't add new feature anymore, and thus, users should check entries in 3)
629  *     instead of this 2).
630  *
631  * 3) /sys/fs/f2fs/$s_id/feature_list
632  *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
633  *     sysfs entry rule where each entry should expose single value.
634  *     This list covers old feature list provided by 2) and beyond. Therefore,
635  *     please add new on-disk feature in this list only.
636  *     - ref. F2FS_SB_FEATURE_RO_ATTR()
637  */
638 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
639                 struct f2fs_sb_info *sbi, char *buf)
640 {
641         return sprintf(buf, "supported\n");
642 }
643
644 #define F2FS_FEATURE_RO_ATTR(_name)                             \
645 static struct f2fs_attr f2fs_attr_##_name = {                   \
646         .attr = {.name = __stringify(_name), .mode = 0444 },    \
647         .show   = f2fs_feature_show,                            \
648 }
649
650 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
651                 struct f2fs_sb_info *sbi, char *buf)
652 {
653         if (F2FS_HAS_FEATURE(sbi, a->id))
654                 return sprintf(buf, "supported\n");
655         return sprintf(buf, "unsupported\n");
656 }
657
658 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)                   \
659 static struct f2fs_attr f2fs_attr_sb_##_name = {                \
660         .attr = {.name = __stringify(_name), .mode = 0444 },    \
661         .show   = f2fs_sb_feature_show,                         \
662         .id     = F2FS_FEATURE_##_feat,                         \
663 }
664
665 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
666 static struct f2fs_attr f2fs_attr_##_name = {                   \
667         .attr = {.name = __stringify(_name), .mode = _mode },   \
668         .show   = _show,                                        \
669         .store  = _store,                                       \
670         .struct_type = _struct_type,                            \
671         .offset = _offset                                       \
672 }
673
674 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)    \
675         F2FS_ATTR_OFFSET(struct_type, name, 0644,               \
676                 f2fs_sbi_show, f2fs_sbi_store,                  \
677                 offsetof(struct struct_name, elname))
678
679 #define F2FS_GENERAL_RO_ATTR(name) \
680 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
681
682 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname)      \
683 static struct f2fs_attr f2fs_attr_##_name = {                   \
684         .attr = {.name = __stringify(_name), .mode = 0444 },    \
685         .show = f2fs_sbi_show,                                  \
686         .struct_type = _struct_type,                            \
687         .offset = offsetof(struct _struct_name, _elname),       \
688 }
689
690 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
691                                                         urgent_sleep_time);
692 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
693 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
694 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
695 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
696 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
697 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
698 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
699 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
700 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
701 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
702 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
703 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
704 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
705 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
706 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
707 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
708 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
709 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
710 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
711 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
712 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
713 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
714 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
715 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
716 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
717                                         interval_time[DISCARD_TIME]);
718 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
719 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
720                 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
721 #ifdef CONFIG_F2FS_IOSTAT
722 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
723 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
724 #endif
725 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
726 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
727 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
728 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
729 #ifdef CONFIG_F2FS_FAULT_INJECTION
730 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
731 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
732 #endif
733 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
734 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
735 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
736 F2FS_GENERAL_RO_ATTR(dirty_segments);
737 F2FS_GENERAL_RO_ATTR(free_segments);
738 F2FS_GENERAL_RO_ATTR(ovp_segments);
739 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
740 F2FS_GENERAL_RO_ATTR(features);
741 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
742 F2FS_GENERAL_RO_ATTR(unusable);
743 F2FS_GENERAL_RO_ATTR(encoding);
744 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
745 F2FS_GENERAL_RO_ATTR(main_blkaddr);
746 #ifdef CONFIG_F2FS_STAT_FS
747 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
748 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
749 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
750 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
751 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
752 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
753 F2FS_GENERAL_RO_ATTR(avg_vblocks);
754 #endif
755
756 #ifdef CONFIG_FS_ENCRYPTION
757 F2FS_FEATURE_RO_ATTR(encryption);
758 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
759 #ifdef CONFIG_UNICODE
760 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
761 #endif
762 #endif /* CONFIG_FS_ENCRYPTION */
763 #ifdef CONFIG_BLK_DEV_ZONED
764 F2FS_FEATURE_RO_ATTR(block_zoned);
765 #endif
766 F2FS_FEATURE_RO_ATTR(atomic_write);
767 F2FS_FEATURE_RO_ATTR(extra_attr);
768 F2FS_FEATURE_RO_ATTR(project_quota);
769 F2FS_FEATURE_RO_ATTR(inode_checksum);
770 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
771 F2FS_FEATURE_RO_ATTR(quota_ino);
772 F2FS_FEATURE_RO_ATTR(inode_crtime);
773 F2FS_FEATURE_RO_ATTR(lost_found);
774 #ifdef CONFIG_FS_VERITY
775 F2FS_FEATURE_RO_ATTR(verity);
776 #endif
777 F2FS_FEATURE_RO_ATTR(sb_checksum);
778 #ifdef CONFIG_UNICODE
779 F2FS_FEATURE_RO_ATTR(casefold);
780 #endif
781 F2FS_FEATURE_RO_ATTR(readonly);
782 #ifdef CONFIG_F2FS_FS_COMPRESSION
783 F2FS_FEATURE_RO_ATTR(compression);
784 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_written_block, compr_written_block);
785 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_saved_block, compr_saved_block);
786 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_new_inode, compr_new_inode);
787 #endif
788 F2FS_FEATURE_RO_ATTR(pin_file);
789
790 /* For ATGC */
791 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_ratio, candidate_ratio);
792 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_count, max_candidate_count);
793 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_weight, age_weight);
794 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
795
796 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, seq_file_ra_mul, seq_file_ra_mul);
797 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
798 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
799 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_chunk, max_fragment_chunk);
800 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_hole, max_fragment_hole);
801
802 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
803 static struct attribute *f2fs_attrs[] = {
804         ATTR_LIST(gc_urgent_sleep_time),
805         ATTR_LIST(gc_min_sleep_time),
806         ATTR_LIST(gc_max_sleep_time),
807         ATTR_LIST(gc_no_gc_sleep_time),
808         ATTR_LIST(gc_idle),
809         ATTR_LIST(gc_urgent),
810         ATTR_LIST(reclaim_segments),
811         ATTR_LIST(main_blkaddr),
812         ATTR_LIST(max_small_discards),
813         ATTR_LIST(discard_granularity),
814         ATTR_LIST(batched_trim_sections),
815         ATTR_LIST(ipu_policy),
816         ATTR_LIST(min_ipu_util),
817         ATTR_LIST(min_fsync_blocks),
818         ATTR_LIST(min_seq_blocks),
819         ATTR_LIST(min_hot_blocks),
820         ATTR_LIST(min_ssr_sections),
821         ATTR_LIST(max_victim_search),
822         ATTR_LIST(migration_granularity),
823         ATTR_LIST(dir_level),
824         ATTR_LIST(ram_thresh),
825         ATTR_LIST(ra_nid_pages),
826         ATTR_LIST(dirty_nats_ratio),
827         ATTR_LIST(cp_interval),
828         ATTR_LIST(idle_interval),
829         ATTR_LIST(discard_idle_interval),
830         ATTR_LIST(gc_idle_interval),
831         ATTR_LIST(umount_discard_timeout),
832 #ifdef CONFIG_F2FS_IOSTAT
833         ATTR_LIST(iostat_enable),
834         ATTR_LIST(iostat_period_ms),
835 #endif
836         ATTR_LIST(readdir_ra),
837         ATTR_LIST(max_io_bytes),
838         ATTR_LIST(gc_pin_file_thresh),
839         ATTR_LIST(extension_list),
840 #ifdef CONFIG_F2FS_FAULT_INJECTION
841         ATTR_LIST(inject_rate),
842         ATTR_LIST(inject_type),
843 #endif
844         ATTR_LIST(data_io_flag),
845         ATTR_LIST(node_io_flag),
846         ATTR_LIST(ckpt_thread_ioprio),
847         ATTR_LIST(dirty_segments),
848         ATTR_LIST(free_segments),
849         ATTR_LIST(ovp_segments),
850         ATTR_LIST(unusable),
851         ATTR_LIST(lifetime_write_kbytes),
852         ATTR_LIST(features),
853         ATTR_LIST(reserved_blocks),
854         ATTR_LIST(current_reserved_blocks),
855         ATTR_LIST(encoding),
856         ATTR_LIST(mounted_time_sec),
857 #ifdef CONFIG_F2FS_STAT_FS
858         ATTR_LIST(cp_foreground_calls),
859         ATTR_LIST(cp_background_calls),
860         ATTR_LIST(gc_foreground_calls),
861         ATTR_LIST(gc_background_calls),
862         ATTR_LIST(moved_blocks_foreground),
863         ATTR_LIST(moved_blocks_background),
864         ATTR_LIST(avg_vblocks),
865 #endif
866 #ifdef CONFIG_F2FS_FS_COMPRESSION
867         ATTR_LIST(compr_written_block),
868         ATTR_LIST(compr_saved_block),
869         ATTR_LIST(compr_new_inode),
870 #endif
871         /* For ATGC */
872         ATTR_LIST(atgc_candidate_ratio),
873         ATTR_LIST(atgc_candidate_count),
874         ATTR_LIST(atgc_age_weight),
875         ATTR_LIST(atgc_age_threshold),
876         ATTR_LIST(seq_file_ra_mul),
877         ATTR_LIST(gc_segment_mode),
878         ATTR_LIST(gc_reclaimed_segments),
879         ATTR_LIST(max_fragment_chunk),
880         ATTR_LIST(max_fragment_hole),
881         NULL,
882 };
883 ATTRIBUTE_GROUPS(f2fs);
884
885 static struct attribute *f2fs_feat_attrs[] = {
886 #ifdef CONFIG_FS_ENCRYPTION
887         ATTR_LIST(encryption),
888         ATTR_LIST(test_dummy_encryption_v2),
889 #ifdef CONFIG_UNICODE
890         ATTR_LIST(encrypted_casefold),
891 #endif
892 #endif /* CONFIG_FS_ENCRYPTION */
893 #ifdef CONFIG_BLK_DEV_ZONED
894         ATTR_LIST(block_zoned),
895 #endif
896         ATTR_LIST(atomic_write),
897         ATTR_LIST(extra_attr),
898         ATTR_LIST(project_quota),
899         ATTR_LIST(inode_checksum),
900         ATTR_LIST(flexible_inline_xattr),
901         ATTR_LIST(quota_ino),
902         ATTR_LIST(inode_crtime),
903         ATTR_LIST(lost_found),
904 #ifdef CONFIG_FS_VERITY
905         ATTR_LIST(verity),
906 #endif
907         ATTR_LIST(sb_checksum),
908 #ifdef CONFIG_UNICODE
909         ATTR_LIST(casefold),
910 #endif
911         ATTR_LIST(readonly),
912 #ifdef CONFIG_F2FS_FS_COMPRESSION
913         ATTR_LIST(compression),
914 #endif
915         ATTR_LIST(pin_file),
916         NULL,
917 };
918 ATTRIBUTE_GROUPS(f2fs_feat);
919
920 F2FS_GENERAL_RO_ATTR(sb_status);
921 static struct attribute *f2fs_stat_attrs[] = {
922         ATTR_LIST(sb_status),
923         NULL,
924 };
925 ATTRIBUTE_GROUPS(f2fs_stat);
926
927 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
928 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
929 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
930 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
931 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
932 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
933 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
934 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
935 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
936 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
937 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
938 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
939 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
940 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
941
942 static struct attribute *f2fs_sb_feat_attrs[] = {
943         ATTR_LIST(sb_encryption),
944         ATTR_LIST(sb_block_zoned),
945         ATTR_LIST(sb_extra_attr),
946         ATTR_LIST(sb_project_quota),
947         ATTR_LIST(sb_inode_checksum),
948         ATTR_LIST(sb_flexible_inline_xattr),
949         ATTR_LIST(sb_quota_ino),
950         ATTR_LIST(sb_inode_crtime),
951         ATTR_LIST(sb_lost_found),
952         ATTR_LIST(sb_verity),
953         ATTR_LIST(sb_sb_checksum),
954         ATTR_LIST(sb_casefold),
955         ATTR_LIST(sb_compression),
956         ATTR_LIST(sb_readonly),
957         NULL,
958 };
959 ATTRIBUTE_GROUPS(f2fs_sb_feat);
960
961 static const struct sysfs_ops f2fs_attr_ops = {
962         .show   = f2fs_attr_show,
963         .store  = f2fs_attr_store,
964 };
965
966 static struct kobj_type f2fs_sb_ktype = {
967         .default_groups = f2fs_groups,
968         .sysfs_ops      = &f2fs_attr_ops,
969         .release        = f2fs_sb_release,
970 };
971
972 static struct kobj_type f2fs_ktype = {
973         .sysfs_ops      = &f2fs_attr_ops,
974 };
975
976 static struct kset f2fs_kset = {
977         .kobj   = {.ktype = &f2fs_ktype},
978 };
979
980 static struct kobj_type f2fs_feat_ktype = {
981         .default_groups = f2fs_feat_groups,
982         .sysfs_ops      = &f2fs_attr_ops,
983 };
984
985 static struct kobject f2fs_feat = {
986         .kset   = &f2fs_kset,
987 };
988
989 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
990                                 struct attribute *attr, char *buf)
991 {
992         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
993                                                                 s_stat_kobj);
994         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
995
996         return a->show ? a->show(a, sbi, buf) : 0;
997 }
998
999 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1000                                                 const char *buf, size_t len)
1001 {
1002         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1003                                                                 s_stat_kobj);
1004         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1005
1006         return a->store ? a->store(a, sbi, buf, len) : 0;
1007 }
1008
1009 static void f2fs_stat_kobj_release(struct kobject *kobj)
1010 {
1011         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1012                                                                 s_stat_kobj);
1013         complete(&sbi->s_stat_kobj_unregister);
1014 }
1015
1016 static const struct sysfs_ops f2fs_stat_attr_ops = {
1017         .show   = f2fs_stat_attr_show,
1018         .store  = f2fs_stat_attr_store,
1019 };
1020
1021 static struct kobj_type f2fs_stat_ktype = {
1022         .default_groups = f2fs_stat_groups,
1023         .sysfs_ops      = &f2fs_stat_attr_ops,
1024         .release        = f2fs_stat_kobj_release,
1025 };
1026
1027 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1028                                 struct attribute *attr, char *buf)
1029 {
1030         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1031                                                         s_feature_list_kobj);
1032         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1033
1034         return a->show ? a->show(a, sbi, buf) : 0;
1035 }
1036
1037 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1038 {
1039         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1040                                                         s_feature_list_kobj);
1041         complete(&sbi->s_feature_list_kobj_unregister);
1042 }
1043
1044 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1045         .show   = f2fs_sb_feat_attr_show,
1046 };
1047
1048 static struct kobj_type f2fs_feature_list_ktype = {
1049         .default_groups = f2fs_sb_feat_groups,
1050         .sysfs_ops      = &f2fs_feature_list_attr_ops,
1051         .release        = f2fs_feature_list_kobj_release,
1052 };
1053
1054 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1055                                                 void *offset)
1056 {
1057         struct super_block *sb = seq->private;
1058         struct f2fs_sb_info *sbi = F2FS_SB(sb);
1059         unsigned int total_segs =
1060                         le32_to_cpu(sbi->raw_super->segment_count_main);
1061         int i;
1062
1063         seq_puts(seq, "format: segment_type|valid_blocks\n"
1064                 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1065
1066         for (i = 0; i < total_segs; i++) {
1067                 struct seg_entry *se = get_seg_entry(sbi, i);
1068
1069                 if ((i % 10) == 0)
1070                         seq_printf(seq, "%-10d", i);
1071                 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1072                 if ((i % 10) == 9 || i == (total_segs - 1))
1073                         seq_putc(seq, '\n');
1074                 else
1075                         seq_putc(seq, ' ');
1076         }
1077
1078         return 0;
1079 }
1080
1081 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1082                                                 void *offset)
1083 {
1084         struct super_block *sb = seq->private;
1085         struct f2fs_sb_info *sbi = F2FS_SB(sb);
1086         unsigned int total_segs =
1087                         le32_to_cpu(sbi->raw_super->segment_count_main);
1088         int i, j;
1089
1090         seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1091                 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1092
1093         for (i = 0; i < total_segs; i++) {
1094                 struct seg_entry *se = get_seg_entry(sbi, i);
1095
1096                 seq_printf(seq, "%-10d", i);
1097                 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1098                 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1099                         seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1100                 seq_putc(seq, '\n');
1101         }
1102         return 0;
1103 }
1104
1105 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1106                                                 void *offset)
1107 {
1108         struct super_block *sb = seq->private;
1109         struct f2fs_sb_info *sbi = F2FS_SB(sb);
1110         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1111         int i;
1112
1113         seq_puts(seq, "format: victim_secmap bitmaps\n");
1114
1115         for (i = 0; i < MAIN_SECS(sbi); i++) {
1116                 if ((i % 10) == 0)
1117                         seq_printf(seq, "%-10d", i);
1118                 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1119                 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1120                         seq_putc(seq, '\n');
1121                 else
1122                         seq_putc(seq, ' ');
1123         }
1124         return 0;
1125 }
1126
1127 int __init f2fs_init_sysfs(void)
1128 {
1129         int ret;
1130
1131         kobject_set_name(&f2fs_kset.kobj, "f2fs");
1132         f2fs_kset.kobj.parent = fs_kobj;
1133         ret = kset_register(&f2fs_kset);
1134         if (ret)
1135                 return ret;
1136
1137         ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1138                                    NULL, "features");
1139         if (ret) {
1140                 kobject_put(&f2fs_feat);
1141                 kset_unregister(&f2fs_kset);
1142         } else {
1143                 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1144         }
1145         return ret;
1146 }
1147
1148 void f2fs_exit_sysfs(void)
1149 {
1150         kobject_put(&f2fs_feat);
1151         kset_unregister(&f2fs_kset);
1152         remove_proc_entry("fs/f2fs", NULL);
1153         f2fs_proc_root = NULL;
1154 }
1155
1156 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1157 {
1158         struct super_block *sb = sbi->sb;
1159         int err;
1160
1161         sbi->s_kobj.kset = &f2fs_kset;
1162         init_completion(&sbi->s_kobj_unregister);
1163         err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1164                                 "%s", sb->s_id);
1165         if (err)
1166                 goto put_sb_kobj;
1167
1168         sbi->s_stat_kobj.kset = &f2fs_kset;
1169         init_completion(&sbi->s_stat_kobj_unregister);
1170         err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1171                                                 &sbi->s_kobj, "stat");
1172         if (err)
1173                 goto put_stat_kobj;
1174
1175         sbi->s_feature_list_kobj.kset = &f2fs_kset;
1176         init_completion(&sbi->s_feature_list_kobj_unregister);
1177         err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1178                                         &f2fs_feature_list_ktype,
1179                                         &sbi->s_kobj, "feature_list");
1180         if (err)
1181                 goto put_feature_list_kobj;
1182
1183         if (f2fs_proc_root)
1184                 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1185
1186         if (sbi->s_proc) {
1187                 proc_create_single_data("segment_info", 0444, sbi->s_proc,
1188                                 segment_info_seq_show, sb);
1189                 proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1190                                 segment_bits_seq_show, sb);
1191 #ifdef CONFIG_F2FS_IOSTAT
1192                 proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1193                                 iostat_info_seq_show, sb);
1194 #endif
1195                 proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1196                                 victim_bits_seq_show, sb);
1197         }
1198         return 0;
1199 put_feature_list_kobj:
1200         kobject_put(&sbi->s_feature_list_kobj);
1201         wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1202 put_stat_kobj:
1203         kobject_put(&sbi->s_stat_kobj);
1204         wait_for_completion(&sbi->s_stat_kobj_unregister);
1205 put_sb_kobj:
1206         kobject_put(&sbi->s_kobj);
1207         wait_for_completion(&sbi->s_kobj_unregister);
1208         return err;
1209 }
1210
1211 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1212 {
1213         if (sbi->s_proc) {
1214 #ifdef CONFIG_F2FS_IOSTAT
1215                 remove_proc_entry("iostat_info", sbi->s_proc);
1216 #endif
1217                 remove_proc_entry("segment_info", sbi->s_proc);
1218                 remove_proc_entry("segment_bits", sbi->s_proc);
1219                 remove_proc_entry("victim_bits", sbi->s_proc);
1220                 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1221         }
1222
1223         kobject_del(&sbi->s_stat_kobj);
1224         kobject_put(&sbi->s_stat_kobj);
1225         wait_for_completion(&sbi->s_stat_kobj_unregister);
1226         kobject_del(&sbi->s_feature_list_kobj);
1227         kobject_put(&sbi->s_feature_list_kobj);
1228         wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1229
1230         kobject_del(&sbi->s_kobj);
1231         kobject_put(&sbi->s_kobj);
1232         wait_for_completion(&sbi->s_kobj_unregister);
1233 }