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