Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * f2fs sysfs interface
0004  *
0005  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
0006  *             http://www.samsung.com/
0007  * Copyright (c) 2017 Chao Yu <chao@kernel.org>
0008  */
0009 #include <linux/compiler.h>
0010 #include <linux/proc_fs.h>
0011 #include <linux/f2fs_fs.h>
0012 #include <linux/seq_file.h>
0013 #include <linux/unicode.h>
0014 #include <linux/ioprio.h>
0015 #include <linux/sysfs.h>
0016 
0017 #include "f2fs.h"
0018 #include "segment.h"
0019 #include "gc.h"
0020 #include "iostat.h"
0021 #include <trace/events/f2fs.h>
0022 
0023 static struct proc_dir_entry *f2fs_proc_root;
0024 
0025 /* Sysfs support for f2fs */
0026 enum {
0027     GC_THREAD,  /* struct f2fs_gc_thread */
0028     SM_INFO,    /* struct f2fs_sm_info */
0029     DCC_INFO,   /* struct discard_cmd_control */
0030     NM_INFO,    /* struct f2fs_nm_info */
0031     F2FS_SBI,   /* struct f2fs_sb_info */
0032 #ifdef CONFIG_F2FS_STAT_FS
0033     STAT_INFO,  /* struct f2fs_stat_info */
0034 #endif
0035 #ifdef CONFIG_F2FS_FAULT_INJECTION
0036     FAULT_INFO_RATE,    /* struct f2fs_fault_info */
0037     FAULT_INFO_TYPE,    /* struct f2fs_fault_info */
0038 #endif
0039     RESERVED_BLOCKS,    /* struct f2fs_sb_info */
0040     CPRC_INFO,  /* struct ckpt_req_control */
0041     ATGC_INFO,  /* struct atgc_management */
0042 };
0043 
0044 static const char *gc_mode_names[MAX_GC_MODE] = {
0045     "GC_NORMAL",
0046     "GC_IDLE_CB",
0047     "GC_IDLE_GREEDY",
0048     "GC_IDLE_AT",
0049     "GC_URGENT_HIGH",
0050     "GC_URGENT_LOW",
0051     "GC_URGENT_MID"
0052 };
0053 
0054 struct f2fs_attr {
0055     struct attribute attr;
0056     ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
0057     ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
0058              const char *, size_t);
0059     int struct_type;
0060     int offset;
0061     int id;
0062 };
0063 
0064 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
0065                  struct f2fs_sb_info *sbi, char *buf);
0066 
0067 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
0068 {
0069     if (struct_type == GC_THREAD)
0070         return (unsigned char *)sbi->gc_thread;
0071     else if (struct_type == SM_INFO)
0072         return (unsigned char *)SM_I(sbi);
0073     else if (struct_type == DCC_INFO)
0074         return (unsigned char *)SM_I(sbi)->dcc_info;
0075     else if (struct_type == NM_INFO)
0076         return (unsigned char *)NM_I(sbi);
0077     else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
0078         return (unsigned char *)sbi;
0079 #ifdef CONFIG_F2FS_FAULT_INJECTION
0080     else if (struct_type == FAULT_INFO_RATE ||
0081                     struct_type == FAULT_INFO_TYPE)
0082         return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
0083 #endif
0084 #ifdef CONFIG_F2FS_STAT_FS
0085     else if (struct_type == STAT_INFO)
0086         return (unsigned char *)F2FS_STAT(sbi);
0087 #endif
0088     else if (struct_type == CPRC_INFO)
0089         return (unsigned char *)&sbi->cprc_info;
0090     else if (struct_type == ATGC_INFO)
0091         return (unsigned char *)&sbi->am;
0092     return NULL;
0093 }
0094 
0095 static ssize_t dirty_segments_show(struct f2fs_attr *a,
0096         struct f2fs_sb_info *sbi, char *buf)
0097 {
0098     return sprintf(buf, "%llu\n",
0099             (unsigned long long)(dirty_segments(sbi)));
0100 }
0101 
0102 static ssize_t free_segments_show(struct f2fs_attr *a,
0103         struct f2fs_sb_info *sbi, char *buf)
0104 {
0105     return sprintf(buf, "%llu\n",
0106             (unsigned long long)(free_segments(sbi)));
0107 }
0108 
0109 static ssize_t ovp_segments_show(struct f2fs_attr *a,
0110         struct f2fs_sb_info *sbi, char *buf)
0111 {
0112     return sprintf(buf, "%llu\n",
0113             (unsigned long long)(overprovision_segments(sbi)));
0114 }
0115 
0116 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
0117         struct f2fs_sb_info *sbi, char *buf)
0118 {
0119     return sprintf(buf, "%llu\n",
0120             (unsigned long long)(sbi->kbytes_written +
0121             ((f2fs_get_sectors_written(sbi) -
0122                 sbi->sectors_written_start) >> 1)));
0123 }
0124 
0125 static ssize_t sb_status_show(struct f2fs_attr *a,
0126         struct f2fs_sb_info *sbi, char *buf)
0127 {
0128     return sprintf(buf, "%lx\n", sbi->s_flag);
0129 }
0130 
0131 static ssize_t pending_discard_show(struct f2fs_attr *a,
0132         struct f2fs_sb_info *sbi, char *buf)
0133 {
0134     if (!SM_I(sbi)->dcc_info)
0135         return -EINVAL;
0136     return sprintf(buf, "%llu\n", (unsigned long long)atomic_read(
0137                 &SM_I(sbi)->dcc_info->discard_cmd_cnt));
0138 }
0139 
0140 static ssize_t features_show(struct f2fs_attr *a,
0141         struct f2fs_sb_info *sbi, char *buf)
0142 {
0143     int len = 0;
0144 
0145     if (f2fs_sb_has_encrypt(sbi))
0146         len += scnprintf(buf, PAGE_SIZE - len, "%s",
0147                         "encryption");
0148     if (f2fs_sb_has_blkzoned(sbi))
0149         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0150                 len ? ", " : "", "blkzoned");
0151     if (f2fs_sb_has_extra_attr(sbi))
0152         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0153                 len ? ", " : "", "extra_attr");
0154     if (f2fs_sb_has_project_quota(sbi))
0155         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0156                 len ? ", " : "", "projquota");
0157     if (f2fs_sb_has_inode_chksum(sbi))
0158         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0159                 len ? ", " : "", "inode_checksum");
0160     if (f2fs_sb_has_flexible_inline_xattr(sbi))
0161         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0162                 len ? ", " : "", "flexible_inline_xattr");
0163     if (f2fs_sb_has_quota_ino(sbi))
0164         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0165                 len ? ", " : "", "quota_ino");
0166     if (f2fs_sb_has_inode_crtime(sbi))
0167         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0168                 len ? ", " : "", "inode_crtime");
0169     if (f2fs_sb_has_lost_found(sbi))
0170         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0171                 len ? ", " : "", "lost_found");
0172     if (f2fs_sb_has_verity(sbi))
0173         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0174                 len ? ", " : "", "verity");
0175     if (f2fs_sb_has_sb_chksum(sbi))
0176         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0177                 len ? ", " : "", "sb_checksum");
0178     if (f2fs_sb_has_casefold(sbi))
0179         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0180                 len ? ", " : "", "casefold");
0181     if (f2fs_sb_has_readonly(sbi))
0182         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0183                 len ? ", " : "", "readonly");
0184     if (f2fs_sb_has_compression(sbi))
0185         len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0186                 len ? ", " : "", "compression");
0187     len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
0188                 len ? ", " : "", "pin_file");
0189     len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
0190     return len;
0191 }
0192 
0193 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
0194                     struct f2fs_sb_info *sbi, char *buf)
0195 {
0196     return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
0197 }
0198 
0199 static ssize_t unusable_show(struct f2fs_attr *a,
0200         struct f2fs_sb_info *sbi, char *buf)
0201 {
0202     block_t unusable;
0203 
0204     if (test_opt(sbi, DISABLE_CHECKPOINT))
0205         unusable = sbi->unusable_block_count;
0206     else
0207         unusable = f2fs_get_unusable_blocks(sbi);
0208     return sprintf(buf, "%llu\n", (unsigned long long)unusable);
0209 }
0210 
0211 static ssize_t encoding_show(struct f2fs_attr *a,
0212         struct f2fs_sb_info *sbi, char *buf)
0213 {
0214 #if IS_ENABLED(CONFIG_UNICODE)
0215     struct super_block *sb = sbi->sb;
0216 
0217     if (f2fs_sb_has_casefold(sbi))
0218         return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
0219             (sb->s_encoding->version >> 16) & 0xff,
0220             (sb->s_encoding->version >> 8) & 0xff,
0221             sb->s_encoding->version & 0xff);
0222 #endif
0223     return sprintf(buf, "(none)");
0224 }
0225 
0226 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
0227         struct f2fs_sb_info *sbi, char *buf)
0228 {
0229     return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
0230 }
0231 
0232 #ifdef CONFIG_F2FS_STAT_FS
0233 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
0234                 struct f2fs_sb_info *sbi, char *buf)
0235 {
0236     struct f2fs_stat_info *si = F2FS_STAT(sbi);
0237 
0238     return sprintf(buf, "%llu\n",
0239         (unsigned long long)(si->tot_blks -
0240             (si->bg_data_blks + si->bg_node_blks)));
0241 }
0242 
0243 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
0244                 struct f2fs_sb_info *sbi, char *buf)
0245 {
0246     struct f2fs_stat_info *si = F2FS_STAT(sbi);
0247 
0248     return sprintf(buf, "%llu\n",
0249         (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
0250 }
0251 
0252 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
0253         struct f2fs_sb_info *sbi, char *buf)
0254 {
0255     struct f2fs_stat_info *si = F2FS_STAT(sbi);
0256 
0257     si->dirty_count = dirty_segments(sbi);
0258     f2fs_update_sit_info(sbi);
0259     return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
0260 }
0261 #endif
0262 
0263 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
0264                 struct f2fs_sb_info *sbi, char *buf)
0265 {
0266     return sysfs_emit(buf, "%llu\n",
0267             (unsigned long long)MAIN_BLKADDR(sbi));
0268 }
0269 
0270 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
0271             struct f2fs_sb_info *sbi, char *buf)
0272 {
0273     unsigned char *ptr = NULL;
0274     unsigned int *ui;
0275 
0276     ptr = __struct_ptr(sbi, a->struct_type);
0277     if (!ptr)
0278         return -EINVAL;
0279 
0280     if (!strcmp(a->attr.name, "extension_list")) {
0281         __u8 (*extlist)[F2FS_EXTENSION_LEN] =
0282                     sbi->raw_super->extension_list;
0283         int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
0284         int hot_count = sbi->raw_super->hot_ext_count;
0285         int len = 0, i;
0286 
0287         len += scnprintf(buf + len, PAGE_SIZE - len,
0288                         "cold file extension:\n");
0289         for (i = 0; i < cold_count; i++)
0290             len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
0291                                 extlist[i]);
0292 
0293         len += scnprintf(buf + len, PAGE_SIZE - len,
0294                         "hot file extension:\n");
0295         for (i = cold_count; i < cold_count + hot_count; i++)
0296             len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
0297                                 extlist[i]);
0298         return len;
0299     }
0300 
0301     if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
0302         struct ckpt_req_control *cprc = &sbi->cprc_info;
0303         int len = 0;
0304         int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
0305         int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
0306 
0307         if (class == IOPRIO_CLASS_RT)
0308             len += scnprintf(buf + len, PAGE_SIZE - len, "rt,");
0309         else if (class == IOPRIO_CLASS_BE)
0310             len += scnprintf(buf + len, PAGE_SIZE - len, "be,");
0311         else
0312             return -EINVAL;
0313 
0314         len += scnprintf(buf + len, PAGE_SIZE - len, "%d\n", data);
0315         return len;
0316     }
0317 
0318 #ifdef CONFIG_F2FS_FS_COMPRESSION
0319     if (!strcmp(a->attr.name, "compr_written_block"))
0320         return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
0321 
0322     if (!strcmp(a->attr.name, "compr_saved_block"))
0323         return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
0324 
0325     if (!strcmp(a->attr.name, "compr_new_inode"))
0326         return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
0327 #endif
0328 
0329     if (!strcmp(a->attr.name, "gc_urgent"))
0330         return sysfs_emit(buf, "%s\n",
0331                 gc_mode_names[sbi->gc_mode]);
0332 
0333     if (!strcmp(a->attr.name, "gc_segment_mode"))
0334         return sysfs_emit(buf, "%s\n",
0335                 gc_mode_names[sbi->gc_segment_mode]);
0336 
0337     if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
0338         return sysfs_emit(buf, "%u\n",
0339             sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
0340     }
0341 
0342     if (!strcmp(a->attr.name, "current_atomic_write")) {
0343         s64 current_write = atomic64_read(&sbi->current_atomic_write);
0344 
0345         return sysfs_emit(buf, "%lld\n", current_write);
0346     }
0347 
0348     if (!strcmp(a->attr.name, "peak_atomic_write"))
0349         return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write);
0350 
0351     if (!strcmp(a->attr.name, "committed_atomic_block"))
0352         return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block);
0353 
0354     if (!strcmp(a->attr.name, "revoked_atomic_block"))
0355         return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block);
0356 
0357     ui = (unsigned int *)(ptr + a->offset);
0358 
0359     return sprintf(buf, "%u\n", *ui);
0360 }
0361 
0362 static ssize_t __sbi_store(struct f2fs_attr *a,
0363             struct f2fs_sb_info *sbi,
0364             const char *buf, size_t count)
0365 {
0366     unsigned char *ptr;
0367     unsigned long t;
0368     unsigned int *ui;
0369     ssize_t ret;
0370 
0371     ptr = __struct_ptr(sbi, a->struct_type);
0372     if (!ptr)
0373         return -EINVAL;
0374 
0375     if (!strcmp(a->attr.name, "extension_list")) {
0376         const char *name = strim((char *)buf);
0377         bool set = true, hot;
0378 
0379         if (!strncmp(name, "[h]", 3))
0380             hot = true;
0381         else if (!strncmp(name, "[c]", 3))
0382             hot = false;
0383         else
0384             return -EINVAL;
0385 
0386         name += 3;
0387 
0388         if (*name == '!') {
0389             name++;
0390             set = false;
0391         }
0392 
0393         if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
0394             return -EINVAL;
0395 
0396         f2fs_down_write(&sbi->sb_lock);
0397 
0398         ret = f2fs_update_extension_list(sbi, name, hot, set);
0399         if (ret)
0400             goto out;
0401 
0402         ret = f2fs_commit_super(sbi, false);
0403         if (ret)
0404             f2fs_update_extension_list(sbi, name, hot, !set);
0405 out:
0406         f2fs_up_write(&sbi->sb_lock);
0407         return ret ? ret : count;
0408     }
0409 
0410     if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
0411         const char *name = strim((char *)buf);
0412         struct ckpt_req_control *cprc = &sbi->cprc_info;
0413         int class;
0414         long data;
0415         int ret;
0416 
0417         if (!strncmp(name, "rt,", 3))
0418             class = IOPRIO_CLASS_RT;
0419         else if (!strncmp(name, "be,", 3))
0420             class = IOPRIO_CLASS_BE;
0421         else
0422             return -EINVAL;
0423 
0424         name += 3;
0425         ret = kstrtol(name, 10, &data);
0426         if (ret)
0427             return ret;
0428         if (data >= IOPRIO_NR_LEVELS || data < 0)
0429             return -EINVAL;
0430 
0431         cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
0432         if (test_opt(sbi, MERGE_CHECKPOINT)) {
0433             ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
0434                     cprc->ckpt_thread_ioprio);
0435             if (ret)
0436                 return ret;
0437         }
0438 
0439         return count;
0440     }
0441 
0442     ui = (unsigned int *)(ptr + a->offset);
0443 
0444     ret = kstrtoul(skip_spaces(buf), 0, &t);
0445     if (ret < 0)
0446         return ret;
0447 #ifdef CONFIG_F2FS_FAULT_INJECTION
0448     if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
0449         return -EINVAL;
0450     if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
0451         return -EINVAL;
0452 #endif
0453     if (a->struct_type == RESERVED_BLOCKS) {
0454         spin_lock(&sbi->stat_lock);
0455         if (t > (unsigned long)(sbi->user_block_count -
0456                 F2FS_OPTION(sbi).root_reserved_blocks -
0457                 sbi->blocks_per_seg *
0458                 SM_I(sbi)->additional_reserved_segments)) {
0459             spin_unlock(&sbi->stat_lock);
0460             return -EINVAL;
0461         }
0462         *ui = t;
0463         sbi->current_reserved_blocks = min(sbi->reserved_blocks,
0464                 sbi->user_block_count - valid_user_blocks(sbi));
0465         spin_unlock(&sbi->stat_lock);
0466         return count;
0467     }
0468 
0469     if (!strcmp(a->attr.name, "discard_granularity")) {
0470         if (t == 0 || t > MAX_PLIST_NUM)
0471             return -EINVAL;
0472         if (!f2fs_block_unit_discard(sbi))
0473             return -EINVAL;
0474         if (t == *ui)
0475             return count;
0476         *ui = t;
0477         return count;
0478     }
0479 
0480     if (!strcmp(a->attr.name, "migration_granularity")) {
0481         if (t == 0 || t > sbi->segs_per_sec)
0482             return -EINVAL;
0483     }
0484 
0485     if (!strcmp(a->attr.name, "trim_sections"))
0486         return -EINVAL;
0487 
0488     if (!strcmp(a->attr.name, "gc_urgent")) {
0489         if (t == 0) {
0490             sbi->gc_mode = GC_NORMAL;
0491         } else if (t == 1) {
0492             sbi->gc_mode = GC_URGENT_HIGH;
0493             if (sbi->gc_thread) {
0494                 sbi->gc_thread->gc_wake = 1;
0495                 wake_up_interruptible_all(
0496                     &sbi->gc_thread->gc_wait_queue_head);
0497                 wake_up_discard_thread(sbi, true);
0498             }
0499         } else if (t == 2) {
0500             sbi->gc_mode = GC_URGENT_LOW;
0501         } else if (t == 3) {
0502             sbi->gc_mode = GC_URGENT_MID;
0503             if (sbi->gc_thread) {
0504                 sbi->gc_thread->gc_wake = 1;
0505                 wake_up_interruptible_all(
0506                     &sbi->gc_thread->gc_wait_queue_head);
0507             }
0508         } else {
0509             return -EINVAL;
0510         }
0511         return count;
0512     }
0513     if (!strcmp(a->attr.name, "gc_idle")) {
0514         if (t == GC_IDLE_CB) {
0515             sbi->gc_mode = GC_IDLE_CB;
0516         } else if (t == GC_IDLE_GREEDY) {
0517             sbi->gc_mode = GC_IDLE_GREEDY;
0518         } else if (t == GC_IDLE_AT) {
0519             if (!sbi->am.atgc_enabled)
0520                 return -EINVAL;
0521             sbi->gc_mode = GC_IDLE_AT;
0522         } else {
0523             sbi->gc_mode = GC_NORMAL;
0524         }
0525         return count;
0526     }
0527 
0528     if (!strcmp(a->attr.name, "gc_urgent_high_remaining")) {
0529         spin_lock(&sbi->gc_urgent_high_lock);
0530         sbi->gc_urgent_high_limited = t != 0;
0531         sbi->gc_urgent_high_remaining = t;
0532         spin_unlock(&sbi->gc_urgent_high_lock);
0533 
0534         return count;
0535     }
0536 
0537 #ifdef CONFIG_F2FS_IOSTAT
0538     if (!strcmp(a->attr.name, "iostat_enable")) {
0539         sbi->iostat_enable = !!t;
0540         if (!sbi->iostat_enable)
0541             f2fs_reset_iostat(sbi);
0542         return count;
0543     }
0544 
0545     if (!strcmp(a->attr.name, "iostat_period_ms")) {
0546         if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
0547             return -EINVAL;
0548         spin_lock(&sbi->iostat_lock);
0549         sbi->iostat_period_ms = (unsigned int)t;
0550         spin_unlock(&sbi->iostat_lock);
0551         return count;
0552     }
0553 #endif
0554 
0555 #ifdef CONFIG_F2FS_FS_COMPRESSION
0556     if (!strcmp(a->attr.name, "compr_written_block") ||
0557         !strcmp(a->attr.name, "compr_saved_block")) {
0558         if (t != 0)
0559             return -EINVAL;
0560         sbi->compr_written_block = 0;
0561         sbi->compr_saved_block = 0;
0562         return count;
0563     }
0564 
0565     if (!strcmp(a->attr.name, "compr_new_inode")) {
0566         if (t != 0)
0567             return -EINVAL;
0568         sbi->compr_new_inode = 0;
0569         return count;
0570     }
0571 #endif
0572 
0573     if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
0574         if (t > 100)
0575             return -EINVAL;
0576         sbi->am.candidate_ratio = t;
0577         return count;
0578     }
0579 
0580     if (!strcmp(a->attr.name, "atgc_age_weight")) {
0581         if (t > 100)
0582             return -EINVAL;
0583         sbi->am.age_weight = t;
0584         return count;
0585     }
0586 
0587     if (!strcmp(a->attr.name, "gc_segment_mode")) {
0588         if (t < MAX_GC_MODE)
0589             sbi->gc_segment_mode = t;
0590         else
0591             return -EINVAL;
0592         return count;
0593     }
0594 
0595     if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
0596         if (t != 0)
0597             return -EINVAL;
0598         sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
0599         return count;
0600     }
0601 
0602     if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
0603         if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
0604             sbi->seq_file_ra_mul = t;
0605         else
0606             return -EINVAL;
0607         return count;
0608     }
0609 
0610     if (!strcmp(a->attr.name, "max_fragment_chunk")) {
0611         if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
0612             sbi->max_fragment_chunk = t;
0613         else
0614             return -EINVAL;
0615         return count;
0616     }
0617 
0618     if (!strcmp(a->attr.name, "max_fragment_hole")) {
0619         if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
0620             sbi->max_fragment_hole = t;
0621         else
0622             return -EINVAL;
0623         return count;
0624     }
0625 
0626     if (!strcmp(a->attr.name, "peak_atomic_write")) {
0627         if (t != 0)
0628             return -EINVAL;
0629         sbi->peak_atomic_write = 0;
0630         return count;
0631     }
0632 
0633     if (!strcmp(a->attr.name, "committed_atomic_block")) {
0634         if (t != 0)
0635             return -EINVAL;
0636         sbi->committed_atomic_block = 0;
0637         return count;
0638     }
0639 
0640     if (!strcmp(a->attr.name, "revoked_atomic_block")) {
0641         if (t != 0)
0642             return -EINVAL;
0643         sbi->revoked_atomic_block = 0;
0644         return count;
0645     }
0646 
0647     *ui = (unsigned int)t;
0648 
0649     return count;
0650 }
0651 
0652 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
0653             struct f2fs_sb_info *sbi,
0654             const char *buf, size_t count)
0655 {
0656     ssize_t ret;
0657     bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
0658                     a->struct_type == GC_THREAD);
0659 
0660     if (gc_entry) {
0661         if (!down_read_trylock(&sbi->sb->s_umount))
0662             return -EAGAIN;
0663     }
0664     ret = __sbi_store(a, sbi, buf, count);
0665     if (gc_entry)
0666         up_read(&sbi->sb->s_umount);
0667 
0668     return ret;
0669 }
0670 
0671 static ssize_t f2fs_attr_show(struct kobject *kobj,
0672                 struct attribute *attr, char *buf)
0673 {
0674     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
0675                                 s_kobj);
0676     struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
0677 
0678     return a->show ? a->show(a, sbi, buf) : 0;
0679 }
0680 
0681 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
0682                         const char *buf, size_t len)
0683 {
0684     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
0685                                     s_kobj);
0686     struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
0687 
0688     return a->store ? a->store(a, sbi, buf, len) : 0;
0689 }
0690 
0691 static void f2fs_sb_release(struct kobject *kobj)
0692 {
0693     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
0694                                 s_kobj);
0695     complete(&sbi->s_kobj_unregister);
0696 }
0697 
0698 /*
0699  * Note that there are three feature list entries:
0700  * 1) /sys/fs/f2fs/features
0701  *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
0702  *     - ref. F2FS_FEATURE_RO_ATTR()
0703  *
0704  * 2) /sys/fs/f2fs/$s_id/features <deprecated>
0705  *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
0706  *     won't add new feature anymore, and thus, users should check entries in 3)
0707  *     instead of this 2).
0708  *
0709  * 3) /sys/fs/f2fs/$s_id/feature_list
0710  *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
0711  *     sysfs entry rule where each entry should expose single value.
0712  *     This list covers old feature list provided by 2) and beyond. Therefore,
0713  *     please add new on-disk feature in this list only.
0714  *     - ref. F2FS_SB_FEATURE_RO_ATTR()
0715  */
0716 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
0717         struct f2fs_sb_info *sbi, char *buf)
0718 {
0719     return sprintf(buf, "supported\n");
0720 }
0721 
0722 #define F2FS_FEATURE_RO_ATTR(_name)             \
0723 static struct f2fs_attr f2fs_attr_##_name = {           \
0724     .attr = {.name = __stringify(_name), .mode = 0444 },    \
0725     .show   = f2fs_feature_show,                \
0726 }
0727 
0728 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
0729         struct f2fs_sb_info *sbi, char *buf)
0730 {
0731     if (F2FS_HAS_FEATURE(sbi, a->id))
0732         return sprintf(buf, "supported\n");
0733     return sprintf(buf, "unsupported\n");
0734 }
0735 
0736 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)           \
0737 static struct f2fs_attr f2fs_attr_sb_##_name = {        \
0738     .attr = {.name = __stringify(_name), .mode = 0444 },    \
0739     .show   = f2fs_sb_feature_show,             \
0740     .id = F2FS_FEATURE_##_feat,             \
0741 }
0742 
0743 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
0744 static struct f2fs_attr f2fs_attr_##_name = {           \
0745     .attr = {.name = __stringify(_name), .mode = _mode },   \
0746     .show   = _show,                    \
0747     .store  = _store,                   \
0748     .struct_type = _struct_type,                \
0749     .offset = _offset                   \
0750 }
0751 
0752 #define F2FS_RO_ATTR(struct_type, struct_name, name, elname)    \
0753     F2FS_ATTR_OFFSET(struct_type, name, 0444,       \
0754         f2fs_sbi_show, NULL,                \
0755         offsetof(struct struct_name, elname))
0756 
0757 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)    \
0758     F2FS_ATTR_OFFSET(struct_type, name, 0644,       \
0759         f2fs_sbi_show, f2fs_sbi_store,          \
0760         offsetof(struct struct_name, elname))
0761 
0762 #define F2FS_GENERAL_RO_ATTR(name) \
0763 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
0764 
0765 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname)  \
0766 static struct f2fs_attr f2fs_attr_##_name = {           \
0767     .attr = {.name = __stringify(_name), .mode = 0444 },    \
0768     .show = f2fs_sbi_show,                  \
0769     .struct_type = _struct_type,                \
0770     .offset = offsetof(struct _struct_name, _elname),       \
0771 }
0772 
0773 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
0774                             urgent_sleep_time);
0775 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
0776 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
0777 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
0778 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
0779 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
0780 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
0781 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
0782 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_discard_request, max_discard_request);
0783 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, min_discard_issue_time, min_discard_issue_time);
0784 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, mid_discard_issue_time, mid_discard_issue_time);
0785 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_discard_issue_time, max_discard_issue_time);
0786 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
0787 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
0788 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
0789 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
0790 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
0791 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
0792 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
0793 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
0794 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
0795 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
0796 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
0797 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
0798 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, max_roll_forward_node_blocks, max_rf_node_blocks);
0799 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
0800 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
0801 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
0802 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
0803 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
0804 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
0805                     interval_time[DISCARD_TIME]);
0806 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
0807 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
0808         umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
0809 #ifdef CONFIG_F2FS_IOSTAT
0810 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
0811 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
0812 #endif
0813 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
0814 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
0815 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
0816 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
0817 #ifdef CONFIG_F2FS_FAULT_INJECTION
0818 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
0819 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
0820 #endif
0821 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
0822 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
0823 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent_high_remaining, gc_urgent_high_remaining);
0824 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, ckpt_thread_ioprio, ckpt_thread_ioprio);
0825 F2FS_GENERAL_RO_ATTR(dirty_segments);
0826 F2FS_GENERAL_RO_ATTR(free_segments);
0827 F2FS_GENERAL_RO_ATTR(ovp_segments);
0828 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
0829 F2FS_GENERAL_RO_ATTR(features);
0830 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
0831 F2FS_GENERAL_RO_ATTR(unusable);
0832 F2FS_GENERAL_RO_ATTR(encoding);
0833 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
0834 F2FS_GENERAL_RO_ATTR(main_blkaddr);
0835 F2FS_GENERAL_RO_ATTR(pending_discard);
0836 #ifdef CONFIG_F2FS_STAT_FS
0837 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
0838 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
0839 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
0840 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
0841 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
0842 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
0843 F2FS_GENERAL_RO_ATTR(avg_vblocks);
0844 #endif
0845 
0846 #ifdef CONFIG_FS_ENCRYPTION
0847 F2FS_FEATURE_RO_ATTR(encryption);
0848 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
0849 #if IS_ENABLED(CONFIG_UNICODE)
0850 F2FS_FEATURE_RO_ATTR(encrypted_casefold);
0851 #endif
0852 #endif /* CONFIG_FS_ENCRYPTION */
0853 #ifdef CONFIG_BLK_DEV_ZONED
0854 F2FS_FEATURE_RO_ATTR(block_zoned);
0855 F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, unusable_blocks_per_sec,
0856                     unusable_blocks_per_sec);
0857 #endif
0858 F2FS_FEATURE_RO_ATTR(atomic_write);
0859 F2FS_FEATURE_RO_ATTR(extra_attr);
0860 F2FS_FEATURE_RO_ATTR(project_quota);
0861 F2FS_FEATURE_RO_ATTR(inode_checksum);
0862 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
0863 F2FS_FEATURE_RO_ATTR(quota_ino);
0864 F2FS_FEATURE_RO_ATTR(inode_crtime);
0865 F2FS_FEATURE_RO_ATTR(lost_found);
0866 #ifdef CONFIG_FS_VERITY
0867 F2FS_FEATURE_RO_ATTR(verity);
0868 #endif
0869 F2FS_FEATURE_RO_ATTR(sb_checksum);
0870 #if IS_ENABLED(CONFIG_UNICODE)
0871 F2FS_FEATURE_RO_ATTR(casefold);
0872 #endif
0873 F2FS_FEATURE_RO_ATTR(readonly);
0874 #ifdef CONFIG_F2FS_FS_COMPRESSION
0875 F2FS_FEATURE_RO_ATTR(compression);
0876 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_written_block, compr_written_block);
0877 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_saved_block, compr_saved_block);
0878 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, compr_new_inode, compr_new_inode);
0879 #endif
0880 F2FS_FEATURE_RO_ATTR(pin_file);
0881 
0882 /* For ATGC */
0883 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_ratio, candidate_ratio);
0884 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_candidate_count, max_candidate_count);
0885 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_weight, age_weight);
0886 F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
0887 
0888 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, seq_file_ra_mul, seq_file_ra_mul);
0889 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
0890 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
0891 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_chunk, max_fragment_chunk);
0892 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_hole, max_fragment_hole);
0893 
0894 /* For atomic write */
0895 F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, current_atomic_write, current_atomic_write);
0896 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, peak_atomic_write, peak_atomic_write);
0897 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, committed_atomic_block, committed_atomic_block);
0898 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, revoked_atomic_block, revoked_atomic_block);
0899 
0900 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
0901 static struct attribute *f2fs_attrs[] = {
0902     ATTR_LIST(gc_urgent_sleep_time),
0903     ATTR_LIST(gc_min_sleep_time),
0904     ATTR_LIST(gc_max_sleep_time),
0905     ATTR_LIST(gc_no_gc_sleep_time),
0906     ATTR_LIST(gc_idle),
0907     ATTR_LIST(gc_urgent),
0908     ATTR_LIST(reclaim_segments),
0909     ATTR_LIST(main_blkaddr),
0910     ATTR_LIST(max_small_discards),
0911     ATTR_LIST(max_discard_request),
0912     ATTR_LIST(min_discard_issue_time),
0913     ATTR_LIST(mid_discard_issue_time),
0914     ATTR_LIST(max_discard_issue_time),
0915     ATTR_LIST(discard_granularity),
0916     ATTR_LIST(pending_discard),
0917     ATTR_LIST(batched_trim_sections),
0918     ATTR_LIST(ipu_policy),
0919     ATTR_LIST(min_ipu_util),
0920     ATTR_LIST(min_fsync_blocks),
0921     ATTR_LIST(min_seq_blocks),
0922     ATTR_LIST(min_hot_blocks),
0923     ATTR_LIST(min_ssr_sections),
0924     ATTR_LIST(max_victim_search),
0925     ATTR_LIST(migration_granularity),
0926     ATTR_LIST(dir_level),
0927     ATTR_LIST(ram_thresh),
0928     ATTR_LIST(ra_nid_pages),
0929     ATTR_LIST(dirty_nats_ratio),
0930     ATTR_LIST(max_roll_forward_node_blocks),
0931     ATTR_LIST(cp_interval),
0932     ATTR_LIST(idle_interval),
0933     ATTR_LIST(discard_idle_interval),
0934     ATTR_LIST(gc_idle_interval),
0935     ATTR_LIST(umount_discard_timeout),
0936 #ifdef CONFIG_F2FS_IOSTAT
0937     ATTR_LIST(iostat_enable),
0938     ATTR_LIST(iostat_period_ms),
0939 #endif
0940     ATTR_LIST(readdir_ra),
0941     ATTR_LIST(max_io_bytes),
0942     ATTR_LIST(gc_pin_file_thresh),
0943     ATTR_LIST(extension_list),
0944 #ifdef CONFIG_F2FS_FAULT_INJECTION
0945     ATTR_LIST(inject_rate),
0946     ATTR_LIST(inject_type),
0947 #endif
0948     ATTR_LIST(data_io_flag),
0949     ATTR_LIST(node_io_flag),
0950     ATTR_LIST(gc_urgent_high_remaining),
0951     ATTR_LIST(ckpt_thread_ioprio),
0952     ATTR_LIST(dirty_segments),
0953     ATTR_LIST(free_segments),
0954     ATTR_LIST(ovp_segments),
0955     ATTR_LIST(unusable),
0956     ATTR_LIST(lifetime_write_kbytes),
0957     ATTR_LIST(features),
0958     ATTR_LIST(reserved_blocks),
0959     ATTR_LIST(current_reserved_blocks),
0960     ATTR_LIST(encoding),
0961     ATTR_LIST(mounted_time_sec),
0962 #ifdef CONFIG_F2FS_STAT_FS
0963     ATTR_LIST(cp_foreground_calls),
0964     ATTR_LIST(cp_background_calls),
0965     ATTR_LIST(gc_foreground_calls),
0966     ATTR_LIST(gc_background_calls),
0967     ATTR_LIST(moved_blocks_foreground),
0968     ATTR_LIST(moved_blocks_background),
0969     ATTR_LIST(avg_vblocks),
0970 #endif
0971 #ifdef CONFIG_BLK_DEV_ZONED
0972     ATTR_LIST(unusable_blocks_per_sec),
0973 #endif
0974 #ifdef CONFIG_F2FS_FS_COMPRESSION
0975     ATTR_LIST(compr_written_block),
0976     ATTR_LIST(compr_saved_block),
0977     ATTR_LIST(compr_new_inode),
0978 #endif
0979     /* For ATGC */
0980     ATTR_LIST(atgc_candidate_ratio),
0981     ATTR_LIST(atgc_candidate_count),
0982     ATTR_LIST(atgc_age_weight),
0983     ATTR_LIST(atgc_age_threshold),
0984     ATTR_LIST(seq_file_ra_mul),
0985     ATTR_LIST(gc_segment_mode),
0986     ATTR_LIST(gc_reclaimed_segments),
0987     ATTR_LIST(max_fragment_chunk),
0988     ATTR_LIST(max_fragment_hole),
0989     ATTR_LIST(current_atomic_write),
0990     ATTR_LIST(peak_atomic_write),
0991     ATTR_LIST(committed_atomic_block),
0992     ATTR_LIST(revoked_atomic_block),
0993     NULL,
0994 };
0995 ATTRIBUTE_GROUPS(f2fs);
0996 
0997 static struct attribute *f2fs_feat_attrs[] = {
0998 #ifdef CONFIG_FS_ENCRYPTION
0999     ATTR_LIST(encryption),
1000     ATTR_LIST(test_dummy_encryption_v2),
1001 #if IS_ENABLED(CONFIG_UNICODE)
1002     ATTR_LIST(encrypted_casefold),
1003 #endif
1004 #endif /* CONFIG_FS_ENCRYPTION */
1005 #ifdef CONFIG_BLK_DEV_ZONED
1006     ATTR_LIST(block_zoned),
1007 #endif
1008     ATTR_LIST(atomic_write),
1009     ATTR_LIST(extra_attr),
1010     ATTR_LIST(project_quota),
1011     ATTR_LIST(inode_checksum),
1012     ATTR_LIST(flexible_inline_xattr),
1013     ATTR_LIST(quota_ino),
1014     ATTR_LIST(inode_crtime),
1015     ATTR_LIST(lost_found),
1016 #ifdef CONFIG_FS_VERITY
1017     ATTR_LIST(verity),
1018 #endif
1019     ATTR_LIST(sb_checksum),
1020 #if IS_ENABLED(CONFIG_UNICODE)
1021     ATTR_LIST(casefold),
1022 #endif
1023     ATTR_LIST(readonly),
1024 #ifdef CONFIG_F2FS_FS_COMPRESSION
1025     ATTR_LIST(compression),
1026 #endif
1027     ATTR_LIST(pin_file),
1028     NULL,
1029 };
1030 ATTRIBUTE_GROUPS(f2fs_feat);
1031 
1032 F2FS_GENERAL_RO_ATTR(sb_status);
1033 static struct attribute *f2fs_stat_attrs[] = {
1034     ATTR_LIST(sb_status),
1035     NULL,
1036 };
1037 ATTRIBUTE_GROUPS(f2fs_stat);
1038 
1039 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1040 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1041 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1042 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1043 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1044 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1045 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1046 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1047 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1048 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1049 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1050 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1051 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1052 F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1053 
1054 static struct attribute *f2fs_sb_feat_attrs[] = {
1055     ATTR_LIST(sb_encryption),
1056     ATTR_LIST(sb_block_zoned),
1057     ATTR_LIST(sb_extra_attr),
1058     ATTR_LIST(sb_project_quota),
1059     ATTR_LIST(sb_inode_checksum),
1060     ATTR_LIST(sb_flexible_inline_xattr),
1061     ATTR_LIST(sb_quota_ino),
1062     ATTR_LIST(sb_inode_crtime),
1063     ATTR_LIST(sb_lost_found),
1064     ATTR_LIST(sb_verity),
1065     ATTR_LIST(sb_sb_checksum),
1066     ATTR_LIST(sb_casefold),
1067     ATTR_LIST(sb_compression),
1068     ATTR_LIST(sb_readonly),
1069     NULL,
1070 };
1071 ATTRIBUTE_GROUPS(f2fs_sb_feat);
1072 
1073 static const struct sysfs_ops f2fs_attr_ops = {
1074     .show   = f2fs_attr_show,
1075     .store  = f2fs_attr_store,
1076 };
1077 
1078 static struct kobj_type f2fs_sb_ktype = {
1079     .default_groups = f2fs_groups,
1080     .sysfs_ops  = &f2fs_attr_ops,
1081     .release    = f2fs_sb_release,
1082 };
1083 
1084 static struct kobj_type f2fs_ktype = {
1085     .sysfs_ops  = &f2fs_attr_ops,
1086 };
1087 
1088 static struct kset f2fs_kset = {
1089     .kobj   = {.ktype = &f2fs_ktype},
1090 };
1091 
1092 static struct kobj_type f2fs_feat_ktype = {
1093     .default_groups = f2fs_feat_groups,
1094     .sysfs_ops  = &f2fs_attr_ops,
1095 };
1096 
1097 static struct kobject f2fs_feat = {
1098     .kset   = &f2fs_kset,
1099 };
1100 
1101 static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1102                 struct attribute *attr, char *buf)
1103 {
1104     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1105                                 s_stat_kobj);
1106     struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1107 
1108     return a->show ? a->show(a, sbi, buf) : 0;
1109 }
1110 
1111 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1112                         const char *buf, size_t len)
1113 {
1114     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1115                                 s_stat_kobj);
1116     struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1117 
1118     return a->store ? a->store(a, sbi, buf, len) : 0;
1119 }
1120 
1121 static void f2fs_stat_kobj_release(struct kobject *kobj)
1122 {
1123     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1124                                 s_stat_kobj);
1125     complete(&sbi->s_stat_kobj_unregister);
1126 }
1127 
1128 static const struct sysfs_ops f2fs_stat_attr_ops = {
1129     .show   = f2fs_stat_attr_show,
1130     .store  = f2fs_stat_attr_store,
1131 };
1132 
1133 static struct kobj_type f2fs_stat_ktype = {
1134     .default_groups = f2fs_stat_groups,
1135     .sysfs_ops  = &f2fs_stat_attr_ops,
1136     .release    = f2fs_stat_kobj_release,
1137 };
1138 
1139 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1140                 struct attribute *attr, char *buf)
1141 {
1142     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1143                             s_feature_list_kobj);
1144     struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1145 
1146     return a->show ? a->show(a, sbi, buf) : 0;
1147 }
1148 
1149 static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1150 {
1151     struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1152                             s_feature_list_kobj);
1153     complete(&sbi->s_feature_list_kobj_unregister);
1154 }
1155 
1156 static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1157     .show   = f2fs_sb_feat_attr_show,
1158 };
1159 
1160 static struct kobj_type f2fs_feature_list_ktype = {
1161     .default_groups = f2fs_sb_feat_groups,
1162     .sysfs_ops  = &f2fs_feature_list_attr_ops,
1163     .release    = f2fs_feature_list_kobj_release,
1164 };
1165 
1166 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1167                         void *offset)
1168 {
1169     struct super_block *sb = seq->private;
1170     struct f2fs_sb_info *sbi = F2FS_SB(sb);
1171     unsigned int total_segs =
1172             le32_to_cpu(sbi->raw_super->segment_count_main);
1173     int i;
1174 
1175     seq_puts(seq, "format: segment_type|valid_blocks\n"
1176         "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1177 
1178     for (i = 0; i < total_segs; i++) {
1179         struct seg_entry *se = get_seg_entry(sbi, i);
1180 
1181         if ((i % 10) == 0)
1182             seq_printf(seq, "%-10d", i);
1183         seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1184         if ((i % 10) == 9 || i == (total_segs - 1))
1185             seq_putc(seq, '\n');
1186         else
1187             seq_putc(seq, ' ');
1188     }
1189 
1190     return 0;
1191 }
1192 
1193 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1194                         void *offset)
1195 {
1196     struct super_block *sb = seq->private;
1197     struct f2fs_sb_info *sbi = F2FS_SB(sb);
1198     unsigned int total_segs =
1199             le32_to_cpu(sbi->raw_super->segment_count_main);
1200     int i, j;
1201 
1202     seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1203         "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1204 
1205     for (i = 0; i < total_segs; i++) {
1206         struct seg_entry *se = get_seg_entry(sbi, i);
1207 
1208         seq_printf(seq, "%-10d", i);
1209         seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1210         for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1211             seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1212         seq_putc(seq, '\n');
1213     }
1214     return 0;
1215 }
1216 
1217 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1218                         void *offset)
1219 {
1220     struct super_block *sb = seq->private;
1221     struct f2fs_sb_info *sbi = F2FS_SB(sb);
1222     struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1223     int i;
1224 
1225     seq_puts(seq, "format: victim_secmap bitmaps\n");
1226 
1227     for (i = 0; i < MAIN_SECS(sbi); i++) {
1228         if ((i % 10) == 0)
1229             seq_printf(seq, "%-10d", i);
1230         seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1231         if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1232             seq_putc(seq, '\n');
1233         else
1234             seq_putc(seq, ' ');
1235     }
1236     return 0;
1237 }
1238 
1239 int __init f2fs_init_sysfs(void)
1240 {
1241     int ret;
1242 
1243     kobject_set_name(&f2fs_kset.kobj, "f2fs");
1244     f2fs_kset.kobj.parent = fs_kobj;
1245     ret = kset_register(&f2fs_kset);
1246     if (ret)
1247         return ret;
1248 
1249     ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1250                    NULL, "features");
1251     if (ret) {
1252         kobject_put(&f2fs_feat);
1253         kset_unregister(&f2fs_kset);
1254     } else {
1255         f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1256     }
1257     return ret;
1258 }
1259 
1260 void f2fs_exit_sysfs(void)
1261 {
1262     kobject_put(&f2fs_feat);
1263     kset_unregister(&f2fs_kset);
1264     remove_proc_entry("fs/f2fs", NULL);
1265     f2fs_proc_root = NULL;
1266 }
1267 
1268 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1269 {
1270     struct super_block *sb = sbi->sb;
1271     int err;
1272 
1273     sbi->s_kobj.kset = &f2fs_kset;
1274     init_completion(&sbi->s_kobj_unregister);
1275     err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1276                 "%s", sb->s_id);
1277     if (err)
1278         goto put_sb_kobj;
1279 
1280     sbi->s_stat_kobj.kset = &f2fs_kset;
1281     init_completion(&sbi->s_stat_kobj_unregister);
1282     err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1283                         &sbi->s_kobj, "stat");
1284     if (err)
1285         goto put_stat_kobj;
1286 
1287     sbi->s_feature_list_kobj.kset = &f2fs_kset;
1288     init_completion(&sbi->s_feature_list_kobj_unregister);
1289     err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1290                     &f2fs_feature_list_ktype,
1291                     &sbi->s_kobj, "feature_list");
1292     if (err)
1293         goto put_feature_list_kobj;
1294 
1295     if (f2fs_proc_root)
1296         sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1297 
1298     if (sbi->s_proc) {
1299         proc_create_single_data("segment_info", 0444, sbi->s_proc,
1300                 segment_info_seq_show, sb);
1301         proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1302                 segment_bits_seq_show, sb);
1303 #ifdef CONFIG_F2FS_IOSTAT
1304         proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1305                 iostat_info_seq_show, sb);
1306 #endif
1307         proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1308                 victim_bits_seq_show, sb);
1309     }
1310     return 0;
1311 put_feature_list_kobj:
1312     kobject_put(&sbi->s_feature_list_kobj);
1313     wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1314 put_stat_kobj:
1315     kobject_put(&sbi->s_stat_kobj);
1316     wait_for_completion(&sbi->s_stat_kobj_unregister);
1317 put_sb_kobj:
1318     kobject_put(&sbi->s_kobj);
1319     wait_for_completion(&sbi->s_kobj_unregister);
1320     return err;
1321 }
1322 
1323 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1324 {
1325     if (sbi->s_proc) {
1326 #ifdef CONFIG_F2FS_IOSTAT
1327         remove_proc_entry("iostat_info", sbi->s_proc);
1328 #endif
1329         remove_proc_entry("segment_info", sbi->s_proc);
1330         remove_proc_entry("segment_bits", sbi->s_proc);
1331         remove_proc_entry("victim_bits", sbi->s_proc);
1332         remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1333     }
1334 
1335     kobject_del(&sbi->s_stat_kobj);
1336     kobject_put(&sbi->s_stat_kobj);
1337     wait_for_completion(&sbi->s_stat_kobj_unregister);
1338     kobject_del(&sbi->s_feature_list_kobj);
1339     kobject_put(&sbi->s_feature_list_kobj);
1340     wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1341 
1342     kobject_del(&sbi->s_kobj);
1343     kobject_put(&sbi->s_kobj);
1344     wait_for_completion(&sbi->s_kobj_unregister);
1345 }