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