1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * f2fs sysfs interface 4 * 5 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com/ 7 * Copyright (c) 2017 Chao Yu <chao@kernel.org> 8 */ 9 #include <linux/compiler.h> 10 #include <linux/proc_fs.h> 11 #include <linux/f2fs_fs.h> 12 #include <linux/seq_file.h> 13 #include <linux/unicode.h> 14 #include <linux/ioprio.h> 15 #include <linux/sysfs.h> 16 17 #include "f2fs.h" 18 #include "segment.h" 19 #include "gc.h" 20 #include "iostat.h" 21 #include <trace/events/f2fs.h> 22 23 static struct proc_dir_entry *f2fs_proc_root; 24 25 /* Sysfs support for f2fs */ 26 enum { 27 GC_THREAD, /* struct f2fs_gc_thread */ 28 SM_INFO, /* struct f2fs_sm_info */ 29 DCC_INFO, /* struct discard_cmd_control */ 30 NM_INFO, /* struct f2fs_nm_info */ 31 F2FS_SBI, /* struct f2fs_sb_info */ 32 #ifdef CONFIG_F2FS_STAT_FS 33 STAT_INFO, /* struct f2fs_stat_info */ 34 #endif 35 #ifdef CONFIG_F2FS_FAULT_INJECTION 36 FAULT_INFO_RATE, /* struct f2fs_fault_info */ 37 FAULT_INFO_TYPE, /* struct f2fs_fault_info */ 38 FAULT_INFO_TIMEOUT, /* struct f2fs_fault_info */ 39 #endif 40 RESERVED_BLOCKS, /* struct f2fs_sb_info */ 41 CPRC_INFO, /* struct ckpt_req_control */ 42 ATGC_INFO, /* struct atgc_management */ 43 }; 44 45 static const char *gc_mode_names[MAX_GC_MODE] = { 46 "GC_NORMAL", 47 "GC_IDLE_CB", 48 "GC_IDLE_GREEDY", 49 "GC_IDLE_AT", 50 "GC_URGENT_HIGH", 51 "GC_URGENT_LOW", 52 "GC_URGENT_MID" 53 }; 54 55 struct f2fs_attr { 56 struct attribute attr; 57 ssize_t (*show)(struct f2fs_attr *a, struct f2fs_sb_info *sbi, char *buf); 58 ssize_t (*store)(struct f2fs_attr *a, struct f2fs_sb_info *sbi, 59 const char *buf, size_t len); 60 int struct_type; 61 int offset; 62 int size; 63 int id; 64 }; 65 66 struct f2fs_base_attr { 67 struct attribute attr; 68 ssize_t (*show)(struct f2fs_base_attr *a, char *buf); 69 ssize_t (*store)(struct f2fs_base_attr *a, const char *buf, size_t len); 70 }; 71 72 static ssize_t f2fs_sbi_show(struct f2fs_attr *a, 73 struct f2fs_sb_info *sbi, char *buf); 74 75 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type) 76 { 77 if (struct_type == GC_THREAD) 78 return (unsigned char *)sbi->gc_thread; 79 else if (struct_type == SM_INFO) 80 return (unsigned char *)SM_I(sbi); 81 else if (struct_type == DCC_INFO) 82 return (unsigned char *)SM_I(sbi)->dcc_info; 83 else if (struct_type == NM_INFO) 84 return (unsigned char *)NM_I(sbi); 85 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS) 86 return (unsigned char *)sbi; 87 #ifdef CONFIG_F2FS_FAULT_INJECTION 88 else if (struct_type == FAULT_INFO_RATE || 89 struct_type == FAULT_INFO_TYPE || 90 struct_type == FAULT_INFO_TIMEOUT) 91 return (unsigned char *)&F2FS_OPTION(sbi).fault_info; 92 #endif 93 #ifdef CONFIG_F2FS_STAT_FS 94 else if (struct_type == STAT_INFO) 95 return (unsigned char *)F2FS_STAT(sbi); 96 #endif 97 else if (struct_type == CPRC_INFO) 98 return (unsigned char *)&sbi->cprc_info; 99 else if (struct_type == ATGC_INFO) 100 return (unsigned char *)&sbi->am; 101 return NULL; 102 } 103 104 static ssize_t dirty_segments_show(struct f2fs_attr *a, 105 struct f2fs_sb_info *sbi, char *buf) 106 { 107 return sysfs_emit(buf, "%llu\n", 108 (unsigned long long)(dirty_segments(sbi))); 109 } 110 111 static ssize_t free_segments_show(struct f2fs_attr *a, 112 struct f2fs_sb_info *sbi, char *buf) 113 { 114 return sysfs_emit(buf, "%llu\n", 115 (unsigned long long)(free_segments(sbi))); 116 } 117 118 static ssize_t ovp_segments_show(struct f2fs_attr *a, 119 struct f2fs_sb_info *sbi, char *buf) 120 { 121 return sysfs_emit(buf, "%llu\n", 122 (unsigned long long)(overprovision_segments(sbi))); 123 } 124 125 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a, 126 struct f2fs_sb_info *sbi, char *buf) 127 { 128 return sysfs_emit(buf, "%llu\n", 129 (unsigned long long)(sbi->kbytes_written + 130 ((f2fs_get_sectors_written(sbi) - 131 sbi->sectors_written_start) >> 1))); 132 } 133 134 static ssize_t sb_status_show(struct f2fs_attr *a, 135 struct f2fs_sb_info *sbi, char *buf) 136 { 137 return sysfs_emit(buf, "%lx\n", sbi->s_flag); 138 } 139 140 static ssize_t cp_status_show(struct f2fs_attr *a, 141 struct f2fs_sb_info *sbi, char *buf) 142 { 143 return sysfs_emit(buf, "%x\n", le32_to_cpu(F2FS_CKPT(sbi)->ckpt_flags)); 144 } 145 146 static ssize_t pending_discard_show(struct f2fs_attr *a, 147 struct f2fs_sb_info *sbi, char *buf) 148 { 149 if (!SM_I(sbi)->dcc_info) 150 return -EINVAL; 151 return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read( 152 &SM_I(sbi)->dcc_info->discard_cmd_cnt)); 153 } 154 155 static ssize_t issued_discard_show(struct f2fs_attr *a, 156 struct f2fs_sb_info *sbi, char *buf) 157 { 158 if (!SM_I(sbi)->dcc_info) 159 return -EINVAL; 160 return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read( 161 &SM_I(sbi)->dcc_info->issued_discard)); 162 } 163 164 static ssize_t queued_discard_show(struct f2fs_attr *a, 165 struct f2fs_sb_info *sbi, char *buf) 166 { 167 if (!SM_I(sbi)->dcc_info) 168 return -EINVAL; 169 return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read( 170 &SM_I(sbi)->dcc_info->queued_discard)); 171 } 172 173 static ssize_t undiscard_blks_show(struct f2fs_attr *a, 174 struct f2fs_sb_info *sbi, char *buf) 175 { 176 if (!SM_I(sbi)->dcc_info) 177 return -EINVAL; 178 return sysfs_emit(buf, "%u\n", 179 SM_I(sbi)->dcc_info->undiscard_blks); 180 } 181 182 static ssize_t atgc_enabled_show(struct f2fs_attr *a, 183 struct f2fs_sb_info *sbi, char *buf) 184 { 185 return sysfs_emit(buf, "%d\n", sbi->am.atgc_enabled ? 1 : 0); 186 } 187 188 static ssize_t gc_mode_show(struct f2fs_attr *a, 189 struct f2fs_sb_info *sbi, char *buf) 190 { 191 return sysfs_emit(buf, "%s\n", gc_mode_names[sbi->gc_mode]); 192 } 193 194 static ssize_t features_show(struct f2fs_attr *a, 195 struct f2fs_sb_info *sbi, char *buf) 196 { 197 int len = 0; 198 199 if (f2fs_sb_has_encrypt(sbi)) 200 len += sysfs_emit_at(buf, len, "%s", 201 "encryption"); 202 if (f2fs_sb_has_blkzoned(sbi)) 203 len += sysfs_emit_at(buf, len, "%s%s", 204 len ? ", " : "", "blkzoned"); 205 if (f2fs_sb_has_extra_attr(sbi)) 206 len += sysfs_emit_at(buf, len, "%s%s", 207 len ? ", " : "", "extra_attr"); 208 if (f2fs_sb_has_project_quota(sbi)) 209 len += sysfs_emit_at(buf, len, "%s%s", 210 len ? ", " : "", "projquota"); 211 if (f2fs_sb_has_inode_chksum(sbi)) 212 len += sysfs_emit_at(buf, len, "%s%s", 213 len ? ", " : "", "inode_checksum"); 214 if (f2fs_sb_has_flexible_inline_xattr(sbi)) 215 len += sysfs_emit_at(buf, len, "%s%s", 216 len ? ", " : "", "flexible_inline_xattr"); 217 if (f2fs_sb_has_quota_ino(sbi)) 218 len += sysfs_emit_at(buf, len, "%s%s", 219 len ? ", " : "", "quota_ino"); 220 if (f2fs_sb_has_inode_crtime(sbi)) 221 len += sysfs_emit_at(buf, len, "%s%s", 222 len ? ", " : "", "inode_crtime"); 223 if (f2fs_sb_has_lost_found(sbi)) 224 len += sysfs_emit_at(buf, len, "%s%s", 225 len ? ", " : "", "lost_found"); 226 if (f2fs_sb_has_verity(sbi)) 227 len += sysfs_emit_at(buf, len, "%s%s", 228 len ? ", " : "", "verity"); 229 if (f2fs_sb_has_sb_chksum(sbi)) 230 len += sysfs_emit_at(buf, len, "%s%s", 231 len ? ", " : "", "sb_checksum"); 232 if (f2fs_sb_has_casefold(sbi)) 233 len += sysfs_emit_at(buf, len, "%s%s", 234 len ? ", " : "", "casefold"); 235 if (f2fs_sb_has_readonly(sbi)) 236 len += sysfs_emit_at(buf, len, "%s%s", 237 len ? ", " : "", "readonly"); 238 if (f2fs_sb_has_compression(sbi)) 239 len += sysfs_emit_at(buf, len, "%s%s", 240 len ? ", " : "", "compression"); 241 if (f2fs_sb_has_packed_ssa(sbi)) 242 len += sysfs_emit_at(buf, len, "%s%s", 243 len ? ", " : "", "packed_ssa"); 244 len += sysfs_emit_at(buf, len, "%s%s", 245 len ? ", " : "", "pin_file"); 246 len += sysfs_emit_at(buf, len, "\n"); 247 return len; 248 } 249 250 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a, 251 struct f2fs_sb_info *sbi, char *buf) 252 { 253 return sysfs_emit(buf, "%u\n", sbi->current_reserved_blocks); 254 } 255 256 static ssize_t unusable_show(struct f2fs_attr *a, 257 struct f2fs_sb_info *sbi, char *buf) 258 { 259 block_t unusable; 260 261 if (test_opt(sbi, DISABLE_CHECKPOINT)) 262 unusable = sbi->unusable_block_count; 263 else 264 unusable = f2fs_get_unusable_blocks(sbi); 265 return sysfs_emit(buf, "%llu\n", (unsigned long long)unusable); 266 } 267 268 static ssize_t encoding_show(struct f2fs_attr *a, 269 struct f2fs_sb_info *sbi, char *buf) 270 { 271 #if IS_ENABLED(CONFIG_UNICODE) 272 struct super_block *sb = sbi->sb; 273 274 if (f2fs_sb_has_casefold(sbi)) 275 return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n", 276 (sb->s_encoding->version >> 16) & 0xff, 277 (sb->s_encoding->version >> 8) & 0xff, 278 sb->s_encoding->version & 0xff); 279 #endif 280 return sysfs_emit(buf, "(none)\n"); 281 } 282 283 static ssize_t encoding_flags_show(struct f2fs_attr *a, 284 struct f2fs_sb_info *sbi, char *buf) 285 { 286 return sysfs_emit(buf, "%x\n", 287 le16_to_cpu(F2FS_RAW_SUPER(sbi)->s_encoding_flags)); 288 } 289 290 static ssize_t effective_lookup_mode_show(struct f2fs_attr *a, 291 struct f2fs_sb_info *sbi, char *buf) 292 { 293 switch (F2FS_OPTION(sbi).lookup_mode) { 294 case LOOKUP_PERF: 295 return sysfs_emit(buf, "perf\n"); 296 case LOOKUP_COMPAT: 297 return sysfs_emit(buf, "compat\n"); 298 case LOOKUP_AUTO: 299 if (sb_no_casefold_compat_fallback(sbi->sb)) 300 return sysfs_emit(buf, "auto:perf\n"); 301 return sysfs_emit(buf, "auto:compat\n"); 302 } 303 return 0; 304 } 305 306 static ssize_t mounted_time_sec_show(struct f2fs_attr *a, 307 struct f2fs_sb_info *sbi, char *buf) 308 { 309 return sysfs_emit(buf, "%llu\n", SIT_I(sbi)->mounted_time); 310 } 311 312 #ifdef CONFIG_F2FS_STAT_FS 313 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a, 314 struct f2fs_sb_info *sbi, char *buf) 315 { 316 struct f2fs_stat_info *si = F2FS_STAT(sbi); 317 318 return sysfs_emit(buf, "%llu\n", 319 (unsigned long long)(si->tot_blks - 320 (si->bg_data_blks + si->bg_node_blks))); 321 } 322 323 static ssize_t moved_blocks_background_show(struct f2fs_attr *a, 324 struct f2fs_sb_info *sbi, char *buf) 325 { 326 struct f2fs_stat_info *si = F2FS_STAT(sbi); 327 328 return sysfs_emit(buf, "%llu\n", 329 (unsigned long long)(si->bg_data_blks + si->bg_node_blks)); 330 } 331 332 static ssize_t avg_vblocks_show(struct f2fs_attr *a, 333 struct f2fs_sb_info *sbi, char *buf) 334 { 335 struct f2fs_stat_info *si = F2FS_STAT(sbi); 336 337 si->dirty_count = dirty_segments(sbi); 338 f2fs_update_sit_info(sbi); 339 return sysfs_emit(buf, "%llu\n", (unsigned long long)(si->avg_vblocks)); 340 } 341 #endif 342 343 static ssize_t main_blkaddr_show(struct f2fs_attr *a, 344 struct f2fs_sb_info *sbi, char *buf) 345 { 346 return sysfs_emit(buf, "%llu\n", 347 (unsigned long long)MAIN_BLKADDR(sbi)); 348 } 349 350 static ssize_t __sbi_show_value(struct f2fs_attr *a, 351 struct f2fs_sb_info *sbi, char *buf, 352 unsigned char *value) 353 { 354 switch (a->size) { 355 case 1: 356 return sysfs_emit(buf, "%u\n", *(u8 *)value); 357 case 2: 358 return sysfs_emit(buf, "%u\n", *(u16 *)value); 359 case 4: 360 return sysfs_emit(buf, "%u\n", *(u32 *)value); 361 case 8: 362 return sysfs_emit(buf, "%llu\n", *(u64 *)value); 363 default: 364 f2fs_bug_on(sbi, 1); 365 return sysfs_emit(buf, 366 "show sysfs node value with wrong type\n"); 367 } 368 } 369 370 static ssize_t f2fs_sbi_show(struct f2fs_attr *a, 371 struct f2fs_sb_info *sbi, char *buf) 372 { 373 unsigned char *ptr = NULL; 374 375 ptr = __struct_ptr(sbi, a->struct_type); 376 if (!ptr) 377 return -EINVAL; 378 379 if (!strcmp(a->attr.name, "extension_list")) { 380 __u8 (*extlist)[F2FS_EXTENSION_LEN] = 381 sbi->raw_super->extension_list; 382 int cold_count = le32_to_cpu(sbi->raw_super->extension_count); 383 int hot_count = sbi->raw_super->hot_ext_count; 384 int len = 0, i; 385 386 len += sysfs_emit_at(buf, len, "cold file extension:\n"); 387 for (i = 0; i < cold_count; i++) 388 len += sysfs_emit_at(buf, len, "%s\n", extlist[i]); 389 390 len += sysfs_emit_at(buf, len, "hot file extension:\n"); 391 for (i = cold_count; i < cold_count + hot_count; i++) 392 len += sysfs_emit_at(buf, len, "%s\n", extlist[i]); 393 394 return len; 395 } 396 397 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) { 398 struct ckpt_req_control *cprc = &sbi->cprc_info; 399 int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio); 400 int level = IOPRIO_PRIO_LEVEL(cprc->ckpt_thread_ioprio); 401 402 if (class != IOPRIO_CLASS_RT && class != IOPRIO_CLASS_BE) 403 return -EINVAL; 404 405 return sysfs_emit(buf, "%s,%d\n", 406 class == IOPRIO_CLASS_RT ? "rt" : "be", level); 407 } 408 409 #ifdef CONFIG_F2FS_FS_COMPRESSION 410 if (!strcmp(a->attr.name, "compr_written_block")) 411 return sysfs_emit(buf, "%llu\n", sbi->compr_written_block); 412 413 if (!strcmp(a->attr.name, "compr_saved_block")) 414 return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block); 415 416 if (!strcmp(a->attr.name, "compr_new_inode")) 417 return sysfs_emit(buf, "%u\n", sbi->compr_new_inode); 418 #endif 419 420 if (!strcmp(a->attr.name, "gc_segment_mode")) 421 return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode); 422 423 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) { 424 return sysfs_emit(buf, "%u\n", 425 sbi->gc_reclaimed_segs[sbi->gc_segment_mode]); 426 } 427 428 if (!strcmp(a->attr.name, "current_atomic_write")) { 429 s64 current_write = atomic64_read(&sbi->current_atomic_write); 430 431 return sysfs_emit(buf, "%lld\n", current_write); 432 } 433 434 if (!strcmp(a->attr.name, "peak_atomic_write")) 435 return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write); 436 437 if (!strcmp(a->attr.name, "committed_atomic_block")) 438 return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block); 439 440 if (!strcmp(a->attr.name, "revoked_atomic_block")) 441 return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block); 442 443 #ifdef CONFIG_F2FS_STAT_FS 444 if (!strcmp(a->attr.name, "cp_foreground_calls")) 445 return sysfs_emit(buf, "%d\n", 446 atomic_read(&sbi->cp_call_count[TOTAL_CALL]) - 447 atomic_read(&sbi->cp_call_count[BACKGROUND])); 448 if (!strcmp(a->attr.name, "cp_background_calls")) 449 return sysfs_emit(buf, "%d\n", 450 atomic_read(&sbi->cp_call_count[BACKGROUND])); 451 #endif 452 453 return __sbi_show_value(a, sbi, buf, ptr + a->offset); 454 } 455 456 static void __sbi_store_value(struct f2fs_attr *a, 457 struct f2fs_sb_info *sbi, 458 unsigned char *ui, unsigned long value) 459 { 460 switch (a->size) { 461 case 1: 462 *(u8 *)ui = value; 463 break; 464 case 2: 465 *(u16 *)ui = value; 466 break; 467 case 4: 468 *(u32 *)ui = value; 469 break; 470 case 8: 471 *(u64 *)ui = value; 472 break; 473 default: 474 f2fs_bug_on(sbi, 1); 475 f2fs_err(sbi, "store sysfs node value with wrong type"); 476 } 477 } 478 479 static ssize_t __sbi_store(struct f2fs_attr *a, 480 struct f2fs_sb_info *sbi, 481 const char *buf, size_t count) 482 { 483 unsigned char *ptr; 484 unsigned long t; 485 unsigned int *ui; 486 ssize_t ret; 487 488 ptr = __struct_ptr(sbi, a->struct_type); 489 if (!ptr) 490 return -EINVAL; 491 492 if (!strcmp(a->attr.name, "extension_list")) { 493 const char *name = strim((char *)buf); 494 bool set = true, hot; 495 496 if (!strncmp(name, "[h]", 3)) 497 hot = true; 498 else if (!strncmp(name, "[c]", 3)) 499 hot = false; 500 else 501 return -EINVAL; 502 503 name += 3; 504 505 if (*name == '!') { 506 name++; 507 set = false; 508 } 509 510 if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN) 511 return -EINVAL; 512 513 f2fs_down_write(&sbi->sb_lock); 514 515 ret = f2fs_update_extension_list(sbi, name, hot, set); 516 if (ret) 517 goto out; 518 519 ret = f2fs_commit_super(sbi, false); 520 if (ret) 521 f2fs_update_extension_list(sbi, name, hot, !set); 522 out: 523 f2fs_up_write(&sbi->sb_lock); 524 return ret ? ret : count; 525 } 526 527 if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) { 528 const char *name = strim((char *)buf); 529 struct ckpt_req_control *cprc = &sbi->cprc_info; 530 int class; 531 long level; 532 int ret; 533 534 if (!strncmp(name, "rt,", 3)) 535 class = IOPRIO_CLASS_RT; 536 else if (!strncmp(name, "be,", 3)) 537 class = IOPRIO_CLASS_BE; 538 else 539 return -EINVAL; 540 541 name += 3; 542 ret = kstrtol(name, 10, &level); 543 if (ret) 544 return ret; 545 if (level >= IOPRIO_NR_LEVELS || level < 0) 546 return -EINVAL; 547 548 cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, level); 549 if (test_opt(sbi, MERGE_CHECKPOINT)) { 550 ret = set_task_ioprio(cprc->f2fs_issue_ckpt, 551 cprc->ckpt_thread_ioprio); 552 if (ret) 553 return ret; 554 } 555 556 return count; 557 } 558 559 ui = (unsigned int *)(ptr + a->offset); 560 561 ret = kstrtoul(skip_spaces(buf), 0, &t); 562 if (ret < 0) 563 return ret; 564 #ifdef CONFIG_F2FS_FAULT_INJECTION 565 if (a->struct_type == FAULT_INFO_TYPE) { 566 if (f2fs_build_fault_attr(sbi, 0, t, FAULT_TYPE)) 567 return -EINVAL; 568 return count; 569 } 570 if (a->struct_type == FAULT_INFO_RATE) { 571 if (f2fs_build_fault_attr(sbi, t, 0, FAULT_RATE)) 572 return -EINVAL; 573 return count; 574 } 575 if (a->struct_type == FAULT_INFO_TIMEOUT) { 576 if (f2fs_build_fault_attr(sbi, 0, t, FAULT_TIMEOUT)) 577 return -EINVAL; 578 f2fs_simulate_lock_timeout(sbi); 579 return count; 580 } 581 #endif 582 if (a->struct_type == RESERVED_BLOCKS) { 583 spin_lock(&sbi->stat_lock); 584 if (t > (unsigned long)(sbi->user_block_count - 585 F2FS_OPTION(sbi).root_reserved_blocks)) { 586 spin_unlock(&sbi->stat_lock); 587 return -EINVAL; 588 } 589 *ui = t; 590 sbi->current_reserved_blocks = min(sbi->reserved_blocks, 591 sbi->user_block_count - valid_user_blocks(sbi)); 592 spin_unlock(&sbi->stat_lock); 593 return count; 594 } 595 596 if (!strcmp(a->attr.name, "discard_io_aware_gran")) { 597 if (t > MAX_PLIST_NUM) 598 return -EINVAL; 599 if (!f2fs_block_unit_discard(sbi)) 600 return -EINVAL; 601 if (t == *ui) 602 return count; 603 *ui = t; 604 return count; 605 } 606 607 if (!strcmp(a->attr.name, "discard_granularity")) { 608 if (t == 0 || t > MAX_PLIST_NUM) 609 return -EINVAL; 610 if (!f2fs_block_unit_discard(sbi)) 611 return -EINVAL; 612 if (t == *ui) 613 return count; 614 *ui = t; 615 return count; 616 } 617 618 if (!strcmp(a->attr.name, "max_ordered_discard")) { 619 if (t == 0 || t > MAX_PLIST_NUM) 620 return -EINVAL; 621 if (!f2fs_block_unit_discard(sbi)) 622 return -EINVAL; 623 *ui = t; 624 return count; 625 } 626 627 if (!strcmp(a->attr.name, "discard_urgent_util")) { 628 if (t > 100) 629 return -EINVAL; 630 *ui = t; 631 return count; 632 } 633 634 if (!strcmp(a->attr.name, "discard_io_aware")) { 635 if (t >= DPOLICY_IO_AWARE_MAX) 636 return -EINVAL; 637 *ui = t; 638 return count; 639 } 640 641 if (!strcmp(a->attr.name, "migration_granularity")) { 642 if (t == 0 || t > SEGS_PER_SEC(sbi)) 643 return -EINVAL; 644 } 645 646 if (!strcmp(a->attr.name, "migration_window_granularity")) { 647 if (t == 0 || t > SEGS_PER_SEC(sbi)) 648 return -EINVAL; 649 } 650 651 if (!strcmp(a->attr.name, "gc_urgent")) { 652 if (t == 0) { 653 sbi->gc_mode = GC_NORMAL; 654 } else if (t == 1) { 655 sbi->gc_mode = GC_URGENT_HIGH; 656 if (sbi->gc_thread) { 657 sbi->gc_thread->gc_wake = true; 658 wake_up_interruptible_all( 659 &sbi->gc_thread->gc_wait_queue_head); 660 wake_up_discard_thread(sbi, true); 661 } 662 } else if (t == 2) { 663 sbi->gc_mode = GC_URGENT_LOW; 664 } else if (t == 3) { 665 sbi->gc_mode = GC_URGENT_MID; 666 if (sbi->gc_thread) { 667 sbi->gc_thread->gc_wake = true; 668 wake_up_interruptible_all( 669 &sbi->gc_thread->gc_wait_queue_head); 670 } 671 } else { 672 return -EINVAL; 673 } 674 return count; 675 } 676 if (!strcmp(a->attr.name, "gc_idle")) { 677 if (t == GC_IDLE_CB) { 678 sbi->gc_mode = GC_IDLE_CB; 679 } else if (t == GC_IDLE_GREEDY) { 680 sbi->gc_mode = GC_IDLE_GREEDY; 681 } else if (t == GC_IDLE_AT) { 682 if (!sbi->am.atgc_enabled) 683 return -EINVAL; 684 sbi->gc_mode = GC_IDLE_AT; 685 } else { 686 sbi->gc_mode = GC_NORMAL; 687 } 688 return count; 689 } 690 691 if (!strcmp(a->attr.name, "gc_remaining_trials")) { 692 spin_lock(&sbi->gc_remaining_trials_lock); 693 sbi->gc_remaining_trials = t; 694 spin_unlock(&sbi->gc_remaining_trials_lock); 695 696 return count; 697 } 698 699 if (!strcmp(a->attr.name, "gc_no_zoned_gc_percent")) { 700 if (t > 100) 701 return -EINVAL; 702 *ui = (unsigned int)t; 703 return count; 704 } 705 706 if (!strcmp(a->attr.name, "gc_boost_zoned_gc_percent")) { 707 if (t > 100) 708 return -EINVAL; 709 *ui = (unsigned int)t; 710 return count; 711 } 712 713 if (!strcmp(a->attr.name, "gc_valid_thresh_ratio")) { 714 if (t > 100) 715 return -EINVAL; 716 *ui = (unsigned int)t; 717 return count; 718 } 719 720 #ifdef CONFIG_F2FS_IOSTAT 721 if (!strcmp(a->attr.name, "iostat_enable")) { 722 sbi->iostat_enable = !!t; 723 if (!sbi->iostat_enable) 724 f2fs_reset_iostat(sbi); 725 return count; 726 } 727 728 if (!strcmp(a->attr.name, "iostat_period_ms")) { 729 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS) 730 return -EINVAL; 731 spin_lock_irq(&sbi->iostat_lock); 732 sbi->iostat_period_ms = (unsigned int)t; 733 spin_unlock_irq(&sbi->iostat_lock); 734 return count; 735 } 736 #endif 737 738 #ifdef CONFIG_BLK_DEV_ZONED 739 if (!strcmp(a->attr.name, "blkzone_alloc_policy")) { 740 if (t < BLKZONE_ALLOC_PRIOR_SEQ || t > BLKZONE_ALLOC_PRIOR_CONV) 741 return -EINVAL; 742 sbi->blkzone_alloc_policy = t; 743 return count; 744 } 745 #endif 746 747 #ifdef CONFIG_F2FS_FS_COMPRESSION 748 if (!strcmp(a->attr.name, "compr_written_block") || 749 !strcmp(a->attr.name, "compr_saved_block")) { 750 if (t != 0) 751 return -EINVAL; 752 sbi->compr_written_block = 0; 753 sbi->compr_saved_block = 0; 754 return count; 755 } 756 757 if (!strcmp(a->attr.name, "compr_new_inode")) { 758 if (t != 0) 759 return -EINVAL; 760 sbi->compr_new_inode = 0; 761 return count; 762 } 763 764 if (!strcmp(a->attr.name, "compress_percent")) { 765 if (t == 0 || t > 100) 766 return -EINVAL; 767 *ui = t; 768 return count; 769 } 770 771 if (!strcmp(a->attr.name, "compress_watermark")) { 772 if (t == 0 || t > 100) 773 return -EINVAL; 774 *ui = t; 775 return count; 776 } 777 #endif 778 779 if (!strcmp(a->attr.name, "atgc_candidate_ratio")) { 780 if (t > 100) 781 return -EINVAL; 782 sbi->am.candidate_ratio = t; 783 return count; 784 } 785 786 if (!strcmp(a->attr.name, "atgc_age_weight")) { 787 if (t > 100) 788 return -EINVAL; 789 sbi->am.age_weight = t; 790 return count; 791 } 792 793 if (!strcmp(a->attr.name, "gc_segment_mode")) { 794 if (t < MAX_GC_MODE) 795 sbi->gc_segment_mode = t; 796 else 797 return -EINVAL; 798 return count; 799 } 800 801 if (!strcmp(a->attr.name, "gc_pin_file_thresh")) { 802 if (t > MAX_GC_FAILED_PINNED_FILES) 803 return -EINVAL; 804 sbi->gc_pin_file_threshold = t; 805 return count; 806 } 807 808 if (!strcmp(a->attr.name, "gc_reclaimed_segments")) { 809 if (t != 0) 810 return -EINVAL; 811 sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0; 812 return count; 813 } 814 815 if (!strcmp(a->attr.name, "seq_file_ra_mul")) { 816 if (t >= MIN_RA_MUL && t <= MAX_RA_MUL) 817 sbi->seq_file_ra_mul = t; 818 else 819 return -EINVAL; 820 return count; 821 } 822 823 if (!strcmp(a->attr.name, "max_fragment_chunk")) { 824 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE) 825 sbi->max_fragment_chunk = t; 826 else 827 return -EINVAL; 828 return count; 829 } 830 831 if (!strcmp(a->attr.name, "max_fragment_hole")) { 832 if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE) 833 sbi->max_fragment_hole = t; 834 else 835 return -EINVAL; 836 return count; 837 } 838 839 if (!strcmp(a->attr.name, "peak_atomic_write")) { 840 if (t != 0) 841 return -EINVAL; 842 sbi->peak_atomic_write = 0; 843 return count; 844 } 845 846 if (!strcmp(a->attr.name, "committed_atomic_block")) { 847 if (t != 0) 848 return -EINVAL; 849 sbi->committed_atomic_block = 0; 850 return count; 851 } 852 853 if (!strcmp(a->attr.name, "revoked_atomic_block")) { 854 if (t != 0) 855 return -EINVAL; 856 sbi->revoked_atomic_block = 0; 857 return count; 858 } 859 860 if (!strcmp(a->attr.name, "readdir_ra")) { 861 sbi->readdir_ra = !!t; 862 return count; 863 } 864 865 if (!strcmp(a->attr.name, "hot_data_age_threshold")) { 866 if (t == 0 || t >= sbi->warm_data_age_threshold) 867 return -EINVAL; 868 if (t == *ui) 869 return count; 870 *ui = (unsigned int)t; 871 return count; 872 } 873 874 if (!strcmp(a->attr.name, "warm_data_age_threshold")) { 875 if (t <= sbi->hot_data_age_threshold) 876 return -EINVAL; 877 if (t == *ui) 878 return count; 879 *ui = (unsigned int)t; 880 return count; 881 } 882 883 if (!strcmp(a->attr.name, "last_age_weight")) { 884 if (t > 100) 885 return -EINVAL; 886 if (t == *ui) 887 return count; 888 *ui = (unsigned int)t; 889 return count; 890 } 891 892 if (!strcmp(a->attr.name, "max_read_extent_count")) { 893 if (t > UINT_MAX) 894 return -EINVAL; 895 *ui = (unsigned int)t; 896 return count; 897 } 898 899 if (!strcmp(a->attr.name, "ipu_policy")) { 900 if (t >= BIT(F2FS_IPU_MAX)) 901 return -EINVAL; 902 /* allow F2FS_IPU_NOCACHE only for IPU in the pinned file */ 903 if (f2fs_lfs_mode(sbi) && (t & ~BIT(F2FS_IPU_NOCACHE))) 904 return -EINVAL; 905 SM_I(sbi)->ipu_policy = (unsigned int)t; 906 return count; 907 } 908 909 if (!strcmp(a->attr.name, "dir_level")) { 910 if (t > MAX_DIR_HASH_DEPTH) 911 return -EINVAL; 912 sbi->dir_level = t; 913 return count; 914 } 915 916 if (!strcmp(a->attr.name, "reserved_pin_section")) { 917 if (t > GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi))) 918 return -EINVAL; 919 *ui = (unsigned int)t; 920 return count; 921 } 922 923 if (!strcmp(a->attr.name, "gc_boost_gc_multiple")) { 924 if (t < 1 || t > SEGS_PER_SEC(sbi)) 925 return -EINVAL; 926 sbi->gc_thread->boost_gc_multiple = (unsigned int)t; 927 return count; 928 } 929 930 if (!strcmp(a->attr.name, "gc_boost_gc_greedy")) { 931 if (t > GC_GREEDY) 932 return -EINVAL; 933 sbi->gc_thread->boost_gc_greedy = (unsigned int)t; 934 return count; 935 } 936 937 if (!strcmp(a->attr.name, "bggc_io_aware")) { 938 if (t < AWARE_ALL_IO || t > AWARE_NONE) 939 return -EINVAL; 940 sbi->bggc_io_aware = t; 941 return count; 942 } 943 944 if (!strcmp(a->attr.name, "allocate_section_hint")) { 945 if (t < 0 || t > MAIN_SECS(sbi)) 946 return -EINVAL; 947 sbi->allocate_section_hint = t; 948 return count; 949 } 950 951 if (!strcmp(a->attr.name, "allocate_section_policy")) { 952 if (t < ALLOCATE_FORWARD_NOHINT || t > ALLOCATE_FORWARD_FROM_HINT) 953 return -EINVAL; 954 sbi->allocate_section_policy = t; 955 return count; 956 } 957 958 if (!strcmp(a->attr.name, "adjust_lock_priority")) { 959 if (t >= BIT(LOCK_NAME_MAX - 1)) 960 return -EINVAL; 961 sbi->adjust_lock_priority = t; 962 return count; 963 } 964 965 if (!strcmp(a->attr.name, "lock_duration_priority")) { 966 if (t < NICE_TO_PRIO(MIN_NICE) || t > NICE_TO_PRIO(MAX_NICE)) 967 return -EINVAL; 968 sbi->lock_duration_priority = t; 969 return count; 970 } 971 972 if (!strcmp(a->attr.name, "critical_task_priority")) { 973 if (t < NICE_TO_PRIO(MIN_NICE) || t > NICE_TO_PRIO(MAX_NICE)) 974 return -EINVAL; 975 if (!capable(CAP_SYS_NICE)) 976 return -EPERM; 977 sbi->critical_task_priority = t; 978 if (sbi->cprc_info.f2fs_issue_ckpt) 979 set_user_nice(sbi->cprc_info.f2fs_issue_ckpt, 980 PRIO_TO_NICE(sbi->critical_task_priority)); 981 if (sbi->gc_thread && sbi->gc_thread->f2fs_gc_task) 982 set_user_nice(sbi->gc_thread->f2fs_gc_task, 983 PRIO_TO_NICE(sbi->critical_task_priority)); 984 return count; 985 } 986 987 __sbi_store_value(a, sbi, ptr + a->offset, t); 988 989 return count; 990 } 991 992 static ssize_t f2fs_sbi_store(struct f2fs_attr *a, 993 struct f2fs_sb_info *sbi, 994 const char *buf, size_t count) 995 { 996 ssize_t ret; 997 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") || 998 a->struct_type == GC_THREAD); 999 1000 if (gc_entry) { 1001 if (!down_read_trylock(&sbi->sb->s_umount)) 1002 return -EAGAIN; 1003 } 1004 ret = __sbi_store(a, sbi, buf, count); 1005 if (gc_entry) 1006 up_read(&sbi->sb->s_umount); 1007 1008 return ret; 1009 } 1010 1011 static ssize_t f2fs_attr_show(struct kobject *kobj, 1012 struct attribute *attr, char *buf) 1013 { 1014 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1015 s_kobj); 1016 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr); 1017 1018 return a->show ? a->show(a, sbi, buf) : 0; 1019 } 1020 1021 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr, 1022 const char *buf, size_t len) 1023 { 1024 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1025 s_kobj); 1026 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr); 1027 1028 return a->store ? a->store(a, sbi, buf, len) : 0; 1029 } 1030 1031 static void f2fs_sb_release(struct kobject *kobj) 1032 { 1033 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1034 s_kobj); 1035 complete(&sbi->s_kobj_unregister); 1036 } 1037 1038 static ssize_t f2fs_base_attr_show(struct kobject *kobj, 1039 struct attribute *attr, char *buf) 1040 { 1041 struct f2fs_base_attr *a = container_of(attr, 1042 struct f2fs_base_attr, attr); 1043 1044 return a->show ? a->show(a, buf) : 0; 1045 } 1046 1047 static ssize_t f2fs_base_attr_store(struct kobject *kobj, 1048 struct attribute *attr, 1049 const char *buf, size_t len) 1050 { 1051 struct f2fs_base_attr *a = container_of(attr, 1052 struct f2fs_base_attr, attr); 1053 1054 return a->store ? a->store(a, buf, len) : 0; 1055 } 1056 1057 /* 1058 * Note that there are three feature list entries: 1059 * 1) /sys/fs/f2fs/features 1060 * : shows runtime features supported by in-kernel f2fs along with Kconfig. 1061 * - ref. F2FS_FEATURE_RO_ATTR() 1062 * 1063 * 2) /sys/fs/f2fs/$s_id/features <deprecated> 1064 * : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This 1065 * won't add new feature anymore, and thus, users should check entries in 3) 1066 * instead of this 2). 1067 * 1068 * 3) /sys/fs/f2fs/$s_id/feature_list 1069 * : shows on-disk features enabled by mkfs.f2fs per instance, which follows 1070 * sysfs entry rule where each entry should expose single value. 1071 * This list covers old feature list provided by 2) and beyond. Therefore, 1072 * please add new on-disk feature in this list only. 1073 * - ref. F2FS_SB_FEATURE_RO_ATTR() 1074 */ 1075 static ssize_t f2fs_feature_show(struct f2fs_base_attr *a, char *buf) 1076 { 1077 return sysfs_emit(buf, "supported\n"); 1078 } 1079 1080 #define F2FS_FEATURE_RO_ATTR(_name) \ 1081 static struct f2fs_base_attr f2fs_base_attr_##_name = { \ 1082 .attr = {.name = __stringify(_name), .mode = 0444 }, \ 1083 .show = f2fs_feature_show, \ 1084 } 1085 1086 static ssize_t f2fs_tune_show(struct f2fs_base_attr *a, char *buf) 1087 { 1088 unsigned int res = 0; 1089 1090 if (!strcmp(a->attr.name, "reclaim_caches_kb")) 1091 res = f2fs_donate_files(); 1092 1093 return sysfs_emit(buf, "%u\n", res); 1094 } 1095 1096 static ssize_t f2fs_tune_store(struct f2fs_base_attr *a, 1097 const char *buf, size_t count) 1098 { 1099 unsigned long t; 1100 int ret; 1101 1102 ret = kstrtoul(skip_spaces(buf), 0, &t); 1103 if (ret) 1104 return ret; 1105 1106 if (!strcmp(a->attr.name, "reclaim_caches_kb")) 1107 f2fs_reclaim_caches(t); 1108 1109 return count; 1110 } 1111 1112 #define F2FS_TUNE_RW_ATTR(_name) \ 1113 static struct f2fs_base_attr f2fs_base_attr_##_name = { \ 1114 .attr = {.name = __stringify(_name), .mode = 0644 }, \ 1115 .show = f2fs_tune_show, \ 1116 .store = f2fs_tune_store, \ 1117 } 1118 1119 static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a, 1120 struct f2fs_sb_info *sbi, char *buf) 1121 { 1122 if (F2FS_HAS_FEATURE(sbi, a->id)) 1123 return sysfs_emit(buf, "supported\n"); 1124 return sysfs_emit(buf, "unsupported\n"); 1125 } 1126 1127 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat) \ 1128 static struct f2fs_attr f2fs_attr_sb_##_name = { \ 1129 .attr = {.name = __stringify(_name), .mode = 0444 }, \ 1130 .show = f2fs_sb_feature_show, \ 1131 .id = F2FS_FEATURE_##_feat, \ 1132 } 1133 1134 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset, _size) \ 1135 static struct f2fs_attr f2fs_attr_##_name = { \ 1136 .attr = {.name = __stringify(_name), .mode = _mode }, \ 1137 .show = _show, \ 1138 .store = _store, \ 1139 .struct_type = _struct_type, \ 1140 .offset = _offset, \ 1141 .size = _size \ 1142 } 1143 1144 #define F2FS_RO_ATTR(struct_type, struct_name, name, elname) \ 1145 F2FS_ATTR_OFFSET(struct_type, name, 0444, \ 1146 f2fs_sbi_show, NULL, \ 1147 offsetof(struct struct_name, elname), \ 1148 sizeof_field(struct struct_name, elname)) 1149 1150 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \ 1151 F2FS_ATTR_OFFSET(struct_type, name, 0644, \ 1152 f2fs_sbi_show, f2fs_sbi_store, \ 1153 offsetof(struct struct_name, elname), \ 1154 sizeof_field(struct struct_name, elname)) 1155 1156 #define F2FS_GENERAL_RO_ATTR(name) \ 1157 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL) 1158 1159 #ifdef CONFIG_F2FS_STAT_FS 1160 #define STAT_INFO_RO_ATTR(name, elname) \ 1161 F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname) 1162 #endif 1163 1164 #define GC_THREAD_RW_ATTR(name, elname) \ 1165 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname) 1166 1167 #define SM_INFO_RW_ATTR(name, elname) \ 1168 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname) 1169 1170 #define SM_INFO_GENERAL_RW_ATTR(elname) \ 1171 SM_INFO_RW_ATTR(elname, elname) 1172 1173 #define DCC_INFO_RW_ATTR(name, elname) \ 1174 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname) 1175 1176 #define DCC_INFO_GENERAL_RW_ATTR(elname) \ 1177 DCC_INFO_RW_ATTR(elname, elname) 1178 1179 #define NM_INFO_RW_ATTR(name, elname) \ 1180 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname) 1181 1182 #define NM_INFO_GENERAL_RW_ATTR(elname) \ 1183 NM_INFO_RW_ATTR(elname, elname) 1184 1185 #define F2FS_SBI_RW_ATTR(name, elname) \ 1186 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname) 1187 1188 #define F2FS_SBI_GENERAL_RW_ATTR(elname) \ 1189 F2FS_SBI_RW_ATTR(elname, elname) 1190 1191 #define F2FS_SBI_GENERAL_RO_ATTR(elname) \ 1192 F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname) 1193 1194 #ifdef CONFIG_F2FS_FAULT_INJECTION 1195 #define FAULT_INFO_GENERAL_RW_ATTR(type, elname) \ 1196 F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname) 1197 #endif 1198 1199 #define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname) \ 1200 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname) 1201 1202 #define CPRC_INFO_GENERAL_RW_ATTR(elname) \ 1203 F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname) 1204 1205 #define ATGC_INFO_RW_ATTR(name, elname) \ 1206 F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname) 1207 1208 /* GC_THREAD ATTR */ 1209 GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time); 1210 GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time); 1211 GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time); 1212 GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time); 1213 GC_THREAD_RW_ATTR(gc_no_zoned_gc_percent, no_zoned_gc_percent); 1214 GC_THREAD_RW_ATTR(gc_boost_zoned_gc_percent, boost_zoned_gc_percent); 1215 GC_THREAD_RW_ATTR(gc_valid_thresh_ratio, valid_thresh_ratio); 1216 GC_THREAD_RW_ATTR(gc_boost_gc_multiple, boost_gc_multiple); 1217 GC_THREAD_RW_ATTR(gc_boost_gc_greedy, boost_gc_greedy); 1218 1219 /* SM_INFO ATTR */ 1220 SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments); 1221 SM_INFO_GENERAL_RW_ATTR(ipu_policy); 1222 SM_INFO_GENERAL_RW_ATTR(min_ipu_util); 1223 SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks); 1224 SM_INFO_GENERAL_RW_ATTR(min_seq_blocks); 1225 SM_INFO_GENERAL_RW_ATTR(min_hot_blocks); 1226 SM_INFO_GENERAL_RW_ATTR(min_ssr_sections); 1227 SM_INFO_GENERAL_RW_ATTR(reserved_segments); 1228 1229 /* DCC_INFO ATTR */ 1230 DCC_INFO_RW_ATTR(max_small_discards, max_discards); 1231 DCC_INFO_GENERAL_RW_ATTR(max_discard_request); 1232 DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time); 1233 DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time); 1234 DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time); 1235 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran); 1236 DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util); 1237 DCC_INFO_GENERAL_RW_ATTR(discard_granularity); 1238 DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard); 1239 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware); 1240 1241 /* NM_INFO ATTR */ 1242 NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks); 1243 NM_INFO_GENERAL_RW_ATTR(ram_thresh); 1244 NM_INFO_GENERAL_RW_ATTR(ra_nid_pages); 1245 NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio); 1246 1247 /* F2FS_SBI ATTR */ 1248 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list); 1249 F2FS_SBI_RW_ATTR(gc_idle, gc_mode); 1250 F2FS_SBI_RW_ATTR(gc_urgent, gc_mode); 1251 F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]); 1252 F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]); 1253 F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]); 1254 F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]); 1255 F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]); 1256 F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold); 1257 F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs); 1258 F2FS_SBI_GENERAL_RW_ATTR(max_victim_search); 1259 F2FS_SBI_GENERAL_RW_ATTR(migration_granularity); 1260 F2FS_SBI_GENERAL_RW_ATTR(migration_window_granularity); 1261 F2FS_SBI_GENERAL_RW_ATTR(dir_level); 1262 F2FS_SBI_GENERAL_RW_ATTR(allocate_section_hint); 1263 F2FS_SBI_GENERAL_RW_ATTR(allocate_section_policy); 1264 #ifdef CONFIG_F2FS_IOSTAT 1265 F2FS_SBI_GENERAL_RW_ATTR(iostat_enable); 1266 F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms); 1267 #endif 1268 F2FS_SBI_GENERAL_RW_ATTR(readdir_ra); 1269 F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes); 1270 F2FS_SBI_GENERAL_RW_ATTR(data_io_flag); 1271 F2FS_SBI_GENERAL_RW_ATTR(node_io_flag); 1272 F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials); 1273 F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul); 1274 F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode); 1275 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk); 1276 F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole); 1277 #ifdef CONFIG_F2FS_FS_COMPRESSION 1278 F2FS_SBI_GENERAL_RW_ATTR(compr_written_block); 1279 F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block); 1280 F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode); 1281 F2FS_SBI_GENERAL_RW_ATTR(compress_percent); 1282 F2FS_SBI_GENERAL_RW_ATTR(compress_watermark); 1283 #endif 1284 /* atomic write */ 1285 F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write); 1286 F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write); 1287 F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block); 1288 F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block); 1289 /* block age extent cache */ 1290 F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold); 1291 F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold); 1292 F2FS_SBI_GENERAL_RW_ATTR(last_age_weight); 1293 /* read extent cache */ 1294 F2FS_SBI_GENERAL_RW_ATTR(max_read_extent_count); 1295 #ifdef CONFIG_BLK_DEV_ZONED 1296 F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec); 1297 F2FS_SBI_GENERAL_RO_ATTR(max_open_zones); 1298 F2FS_SBI_GENERAL_RW_ATTR(blkzone_alloc_policy); 1299 #endif 1300 F2FS_SBI_GENERAL_RW_ATTR(carve_out); 1301 F2FS_SBI_GENERAL_RW_ATTR(reserved_pin_section); 1302 F2FS_SBI_GENERAL_RW_ATTR(bggc_io_aware); 1303 F2FS_SBI_GENERAL_RW_ATTR(max_lock_elapsed_time); 1304 F2FS_SBI_GENERAL_RW_ATTR(lock_duration_priority); 1305 F2FS_SBI_GENERAL_RW_ATTR(adjust_lock_priority); 1306 F2FS_SBI_GENERAL_RW_ATTR(critical_task_priority); 1307 1308 /* STAT_INFO ATTR */ 1309 #ifdef CONFIG_F2FS_STAT_FS 1310 STAT_INFO_RO_ATTR(cp_foreground_calls, cp_call_count[FOREGROUND]); 1311 STAT_INFO_RO_ATTR(cp_background_calls, cp_call_count[BACKGROUND]); 1312 STAT_INFO_RO_ATTR(gc_foreground_calls, gc_call_count[FOREGROUND]); 1313 STAT_INFO_RO_ATTR(gc_background_calls, gc_call_count[BACKGROUND]); 1314 #endif 1315 1316 /* FAULT_INFO ATTR */ 1317 #ifdef CONFIG_F2FS_FAULT_INJECTION 1318 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate); 1319 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type); 1320 FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TIMEOUT, inject_lock_timeout); 1321 #endif 1322 1323 /* RESERVED_BLOCKS ATTR */ 1324 RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks); 1325 1326 /* CPRC_INFO ATTR */ 1327 CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio); 1328 1329 /* ATGC_INFO ATTR */ 1330 ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio); 1331 ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count); 1332 ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight); 1333 ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold); 1334 1335 F2FS_GENERAL_RO_ATTR(dirty_segments); 1336 F2FS_GENERAL_RO_ATTR(free_segments); 1337 F2FS_GENERAL_RO_ATTR(ovp_segments); 1338 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes); 1339 F2FS_GENERAL_RO_ATTR(features); 1340 F2FS_GENERAL_RO_ATTR(current_reserved_blocks); 1341 F2FS_GENERAL_RO_ATTR(unusable); 1342 F2FS_GENERAL_RO_ATTR(encoding); 1343 F2FS_GENERAL_RO_ATTR(encoding_flags); 1344 F2FS_GENERAL_RO_ATTR(effective_lookup_mode); 1345 F2FS_GENERAL_RO_ATTR(mounted_time_sec); 1346 F2FS_GENERAL_RO_ATTR(main_blkaddr); 1347 F2FS_GENERAL_RO_ATTR(pending_discard); 1348 F2FS_GENERAL_RO_ATTR(atgc_enabled); 1349 F2FS_GENERAL_RO_ATTR(gc_mode); 1350 #ifdef CONFIG_F2FS_STAT_FS 1351 F2FS_GENERAL_RO_ATTR(moved_blocks_background); 1352 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground); 1353 F2FS_GENERAL_RO_ATTR(avg_vblocks); 1354 #endif 1355 1356 #ifdef CONFIG_FS_ENCRYPTION 1357 F2FS_FEATURE_RO_ATTR(encryption); 1358 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2); 1359 #if IS_ENABLED(CONFIG_UNICODE) 1360 F2FS_FEATURE_RO_ATTR(encrypted_casefold); 1361 #endif 1362 #endif /* CONFIG_FS_ENCRYPTION */ 1363 #ifdef CONFIG_BLK_DEV_ZONED 1364 F2FS_FEATURE_RO_ATTR(block_zoned); 1365 #endif 1366 F2FS_FEATURE_RO_ATTR(atomic_write); 1367 F2FS_FEATURE_RO_ATTR(extra_attr); 1368 F2FS_FEATURE_RO_ATTR(project_quota); 1369 F2FS_FEATURE_RO_ATTR(inode_checksum); 1370 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr); 1371 F2FS_FEATURE_RO_ATTR(quota_ino); 1372 F2FS_FEATURE_RO_ATTR(inode_crtime); 1373 F2FS_FEATURE_RO_ATTR(lost_found); 1374 #ifdef CONFIG_FS_VERITY 1375 F2FS_FEATURE_RO_ATTR(verity); 1376 #endif 1377 F2FS_FEATURE_RO_ATTR(sb_checksum); 1378 #if IS_ENABLED(CONFIG_UNICODE) 1379 F2FS_FEATURE_RO_ATTR(casefold); 1380 #endif 1381 F2FS_FEATURE_RO_ATTR(readonly); 1382 #ifdef CONFIG_F2FS_FS_COMPRESSION 1383 F2FS_FEATURE_RO_ATTR(compression); 1384 #endif 1385 F2FS_FEATURE_RO_ATTR(pin_file); 1386 #ifdef CONFIG_UNICODE 1387 F2FS_FEATURE_RO_ATTR(linear_lookup); 1388 #endif 1389 F2FS_FEATURE_RO_ATTR(packed_ssa); 1390 1391 #define ATTR_LIST(name) (&f2fs_attr_##name.attr) 1392 static struct attribute *f2fs_attrs[] = { 1393 ATTR_LIST(gc_urgent_sleep_time), 1394 ATTR_LIST(gc_min_sleep_time), 1395 ATTR_LIST(gc_max_sleep_time), 1396 ATTR_LIST(gc_no_gc_sleep_time), 1397 ATTR_LIST(gc_no_zoned_gc_percent), 1398 ATTR_LIST(gc_boost_zoned_gc_percent), 1399 ATTR_LIST(gc_valid_thresh_ratio), 1400 ATTR_LIST(gc_boost_gc_multiple), 1401 ATTR_LIST(gc_boost_gc_greedy), 1402 ATTR_LIST(gc_idle), 1403 ATTR_LIST(gc_urgent), 1404 ATTR_LIST(reclaim_segments), 1405 ATTR_LIST(main_blkaddr), 1406 ATTR_LIST(max_small_discards), 1407 ATTR_LIST(max_discard_request), 1408 ATTR_LIST(min_discard_issue_time), 1409 ATTR_LIST(mid_discard_issue_time), 1410 ATTR_LIST(max_discard_issue_time), 1411 ATTR_LIST(discard_io_aware_gran), 1412 ATTR_LIST(discard_urgent_util), 1413 ATTR_LIST(discard_granularity), 1414 ATTR_LIST(max_ordered_discard), 1415 ATTR_LIST(discard_io_aware), 1416 ATTR_LIST(pending_discard), 1417 ATTR_LIST(gc_mode), 1418 ATTR_LIST(ipu_policy), 1419 ATTR_LIST(min_ipu_util), 1420 ATTR_LIST(min_fsync_blocks), 1421 ATTR_LIST(min_seq_blocks), 1422 ATTR_LIST(min_hot_blocks), 1423 ATTR_LIST(min_ssr_sections), 1424 ATTR_LIST(reserved_segments), 1425 ATTR_LIST(max_victim_search), 1426 ATTR_LIST(migration_granularity), 1427 ATTR_LIST(migration_window_granularity), 1428 ATTR_LIST(dir_level), 1429 ATTR_LIST(ram_thresh), 1430 ATTR_LIST(ra_nid_pages), 1431 ATTR_LIST(dirty_nats_ratio), 1432 ATTR_LIST(max_roll_forward_node_blocks), 1433 ATTR_LIST(cp_interval), 1434 ATTR_LIST(idle_interval), 1435 ATTR_LIST(discard_idle_interval), 1436 ATTR_LIST(gc_idle_interval), 1437 ATTR_LIST(umount_discard_timeout), 1438 ATTR_LIST(bggc_io_aware), 1439 #ifdef CONFIG_F2FS_IOSTAT 1440 ATTR_LIST(iostat_enable), 1441 ATTR_LIST(iostat_period_ms), 1442 #endif 1443 ATTR_LIST(readdir_ra), 1444 ATTR_LIST(max_io_bytes), 1445 ATTR_LIST(gc_pin_file_thresh), 1446 ATTR_LIST(extension_list), 1447 #ifdef CONFIG_F2FS_FAULT_INJECTION 1448 ATTR_LIST(inject_rate), 1449 ATTR_LIST(inject_type), 1450 ATTR_LIST(inject_lock_timeout), 1451 #endif 1452 ATTR_LIST(data_io_flag), 1453 ATTR_LIST(node_io_flag), 1454 ATTR_LIST(gc_remaining_trials), 1455 ATTR_LIST(ckpt_thread_ioprio), 1456 ATTR_LIST(dirty_segments), 1457 ATTR_LIST(free_segments), 1458 ATTR_LIST(ovp_segments), 1459 ATTR_LIST(unusable), 1460 ATTR_LIST(lifetime_write_kbytes), 1461 ATTR_LIST(features), 1462 ATTR_LIST(reserved_blocks), 1463 ATTR_LIST(current_reserved_blocks), 1464 ATTR_LIST(encoding), 1465 ATTR_LIST(encoding_flags), 1466 ATTR_LIST(effective_lookup_mode), 1467 ATTR_LIST(mounted_time_sec), 1468 #ifdef CONFIG_F2FS_STAT_FS 1469 ATTR_LIST(cp_foreground_calls), 1470 ATTR_LIST(cp_background_calls), 1471 ATTR_LIST(gc_foreground_calls), 1472 ATTR_LIST(gc_background_calls), 1473 ATTR_LIST(moved_blocks_foreground), 1474 ATTR_LIST(moved_blocks_background), 1475 ATTR_LIST(avg_vblocks), 1476 #endif 1477 #ifdef CONFIG_BLK_DEV_ZONED 1478 ATTR_LIST(unusable_blocks_per_sec), 1479 ATTR_LIST(max_open_zones), 1480 ATTR_LIST(blkzone_alloc_policy), 1481 #endif 1482 #ifdef CONFIG_F2FS_FS_COMPRESSION 1483 ATTR_LIST(compr_written_block), 1484 ATTR_LIST(compr_saved_block), 1485 ATTR_LIST(compr_new_inode), 1486 ATTR_LIST(compress_percent), 1487 ATTR_LIST(compress_watermark), 1488 #endif 1489 /* For ATGC */ 1490 ATTR_LIST(atgc_candidate_ratio), 1491 ATTR_LIST(atgc_candidate_count), 1492 ATTR_LIST(atgc_age_weight), 1493 ATTR_LIST(atgc_age_threshold), 1494 ATTR_LIST(atgc_enabled), 1495 ATTR_LIST(seq_file_ra_mul), 1496 ATTR_LIST(gc_segment_mode), 1497 ATTR_LIST(gc_reclaimed_segments), 1498 ATTR_LIST(max_fragment_chunk), 1499 ATTR_LIST(max_fragment_hole), 1500 ATTR_LIST(current_atomic_write), 1501 ATTR_LIST(peak_atomic_write), 1502 ATTR_LIST(committed_atomic_block), 1503 ATTR_LIST(revoked_atomic_block), 1504 ATTR_LIST(hot_data_age_threshold), 1505 ATTR_LIST(warm_data_age_threshold), 1506 ATTR_LIST(last_age_weight), 1507 ATTR_LIST(max_read_extent_count), 1508 ATTR_LIST(carve_out), 1509 ATTR_LIST(reserved_pin_section), 1510 ATTR_LIST(allocate_section_hint), 1511 ATTR_LIST(allocate_section_policy), 1512 ATTR_LIST(max_lock_elapsed_time), 1513 ATTR_LIST(lock_duration_priority), 1514 ATTR_LIST(adjust_lock_priority), 1515 ATTR_LIST(critical_task_priority), 1516 NULL, 1517 }; 1518 ATTRIBUTE_GROUPS(f2fs); 1519 1520 #define BASE_ATTR_LIST(name) (&f2fs_base_attr_##name.attr) 1521 static struct attribute *f2fs_feat_attrs[] = { 1522 #ifdef CONFIG_FS_ENCRYPTION 1523 BASE_ATTR_LIST(encryption), 1524 BASE_ATTR_LIST(test_dummy_encryption_v2), 1525 #if IS_ENABLED(CONFIG_UNICODE) 1526 BASE_ATTR_LIST(encrypted_casefold), 1527 #endif 1528 #endif /* CONFIG_FS_ENCRYPTION */ 1529 #ifdef CONFIG_BLK_DEV_ZONED 1530 BASE_ATTR_LIST(block_zoned), 1531 #endif 1532 BASE_ATTR_LIST(atomic_write), 1533 BASE_ATTR_LIST(extra_attr), 1534 BASE_ATTR_LIST(project_quota), 1535 BASE_ATTR_LIST(inode_checksum), 1536 BASE_ATTR_LIST(flexible_inline_xattr), 1537 BASE_ATTR_LIST(quota_ino), 1538 BASE_ATTR_LIST(inode_crtime), 1539 BASE_ATTR_LIST(lost_found), 1540 #ifdef CONFIG_FS_VERITY 1541 BASE_ATTR_LIST(verity), 1542 #endif 1543 BASE_ATTR_LIST(sb_checksum), 1544 #if IS_ENABLED(CONFIG_UNICODE) 1545 BASE_ATTR_LIST(casefold), 1546 #endif 1547 BASE_ATTR_LIST(readonly), 1548 #ifdef CONFIG_F2FS_FS_COMPRESSION 1549 BASE_ATTR_LIST(compression), 1550 #endif 1551 BASE_ATTR_LIST(pin_file), 1552 #ifdef CONFIG_UNICODE 1553 BASE_ATTR_LIST(linear_lookup), 1554 #endif 1555 BASE_ATTR_LIST(packed_ssa), 1556 NULL, 1557 }; 1558 ATTRIBUTE_GROUPS(f2fs_feat); 1559 1560 F2FS_GENERAL_RO_ATTR(sb_status); 1561 F2FS_GENERAL_RO_ATTR(cp_status); 1562 F2FS_GENERAL_RO_ATTR(issued_discard); 1563 F2FS_GENERAL_RO_ATTR(queued_discard); 1564 F2FS_GENERAL_RO_ATTR(undiscard_blks); 1565 1566 static struct attribute *f2fs_stat_attrs[] = { 1567 ATTR_LIST(sb_status), 1568 ATTR_LIST(cp_status), 1569 ATTR_LIST(issued_discard), 1570 ATTR_LIST(queued_discard), 1571 ATTR_LIST(undiscard_blks), 1572 NULL, 1573 }; 1574 ATTRIBUTE_GROUPS(f2fs_stat); 1575 1576 F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT); 1577 F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED); 1578 F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR); 1579 F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA); 1580 F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM); 1581 F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR); 1582 F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO); 1583 F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME); 1584 F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND); 1585 F2FS_SB_FEATURE_RO_ATTR(verity, VERITY); 1586 F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM); 1587 F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD); 1588 F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION); 1589 F2FS_SB_FEATURE_RO_ATTR(readonly, RO); 1590 F2FS_SB_FEATURE_RO_ATTR(device_alias, DEVICE_ALIAS); 1591 F2FS_SB_FEATURE_RO_ATTR(packed_ssa, PACKED_SSA); 1592 1593 static struct attribute *f2fs_sb_feat_attrs[] = { 1594 ATTR_LIST(sb_encryption), 1595 ATTR_LIST(sb_block_zoned), 1596 ATTR_LIST(sb_extra_attr), 1597 ATTR_LIST(sb_project_quota), 1598 ATTR_LIST(sb_inode_checksum), 1599 ATTR_LIST(sb_flexible_inline_xattr), 1600 ATTR_LIST(sb_quota_ino), 1601 ATTR_LIST(sb_inode_crtime), 1602 ATTR_LIST(sb_lost_found), 1603 ATTR_LIST(sb_verity), 1604 ATTR_LIST(sb_sb_checksum), 1605 ATTR_LIST(sb_casefold), 1606 ATTR_LIST(sb_compression), 1607 ATTR_LIST(sb_readonly), 1608 ATTR_LIST(sb_device_alias), 1609 ATTR_LIST(sb_packed_ssa), 1610 NULL, 1611 }; 1612 ATTRIBUTE_GROUPS(f2fs_sb_feat); 1613 1614 F2FS_TUNE_RW_ATTR(reclaim_caches_kb); 1615 1616 static struct attribute *f2fs_tune_attrs[] = { 1617 BASE_ATTR_LIST(reclaim_caches_kb), 1618 NULL, 1619 }; 1620 ATTRIBUTE_GROUPS(f2fs_tune); 1621 1622 static const struct sysfs_ops f2fs_attr_ops = { 1623 .show = f2fs_attr_show, 1624 .store = f2fs_attr_store, 1625 }; 1626 1627 static const struct kobj_type f2fs_sb_ktype = { 1628 .default_groups = f2fs_groups, 1629 .sysfs_ops = &f2fs_attr_ops, 1630 .release = f2fs_sb_release, 1631 }; 1632 1633 static const struct kobj_type f2fs_ktype = { 1634 .sysfs_ops = &f2fs_attr_ops, 1635 }; 1636 1637 static struct kset f2fs_kset = { 1638 .kobj = {.ktype = &f2fs_ktype}, 1639 }; 1640 1641 static const struct sysfs_ops f2fs_feat_attr_ops = { 1642 .show = f2fs_base_attr_show, 1643 .store = f2fs_base_attr_store, 1644 }; 1645 1646 static const struct kobj_type f2fs_feat_ktype = { 1647 .default_groups = f2fs_feat_groups, 1648 .sysfs_ops = &f2fs_feat_attr_ops, 1649 }; 1650 1651 static struct kobject f2fs_feat = { 1652 .kset = &f2fs_kset, 1653 }; 1654 1655 static const struct sysfs_ops f2fs_tune_attr_ops = { 1656 .show = f2fs_base_attr_show, 1657 .store = f2fs_base_attr_store, 1658 }; 1659 1660 static const struct kobj_type f2fs_tune_ktype = { 1661 .default_groups = f2fs_tune_groups, 1662 .sysfs_ops = &f2fs_tune_attr_ops, 1663 }; 1664 1665 static struct kobject f2fs_tune = { 1666 .kset = &f2fs_kset, 1667 }; 1668 1669 static ssize_t f2fs_stat_attr_show(struct kobject *kobj, 1670 struct attribute *attr, char *buf) 1671 { 1672 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1673 s_stat_kobj); 1674 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr); 1675 1676 return a->show ? a->show(a, sbi, buf) : 0; 1677 } 1678 1679 static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr, 1680 const char *buf, size_t len) 1681 { 1682 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1683 s_stat_kobj); 1684 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr); 1685 1686 return a->store ? a->store(a, sbi, buf, len) : 0; 1687 } 1688 1689 static void f2fs_stat_kobj_release(struct kobject *kobj) 1690 { 1691 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1692 s_stat_kobj); 1693 complete(&sbi->s_stat_kobj_unregister); 1694 } 1695 1696 static const struct sysfs_ops f2fs_stat_attr_ops = { 1697 .show = f2fs_stat_attr_show, 1698 .store = f2fs_stat_attr_store, 1699 }; 1700 1701 static const struct kobj_type f2fs_stat_ktype = { 1702 .default_groups = f2fs_stat_groups, 1703 .sysfs_ops = &f2fs_stat_attr_ops, 1704 .release = f2fs_stat_kobj_release, 1705 }; 1706 1707 static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj, 1708 struct attribute *attr, char *buf) 1709 { 1710 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1711 s_feature_list_kobj); 1712 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr); 1713 1714 return a->show ? a->show(a, sbi, buf) : 0; 1715 } 1716 1717 static void f2fs_feature_list_kobj_release(struct kobject *kobj) 1718 { 1719 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, 1720 s_feature_list_kobj); 1721 complete(&sbi->s_feature_list_kobj_unregister); 1722 } 1723 1724 static const struct sysfs_ops f2fs_feature_list_attr_ops = { 1725 .show = f2fs_sb_feat_attr_show, 1726 }; 1727 1728 static const struct kobj_type f2fs_feature_list_ktype = { 1729 .default_groups = f2fs_sb_feat_groups, 1730 .sysfs_ops = &f2fs_feature_list_attr_ops, 1731 .release = f2fs_feature_list_kobj_release, 1732 }; 1733 1734 static int __maybe_unused segment_info_seq_show(struct seq_file *seq, 1735 void *offset) 1736 { 1737 struct super_block *sb = seq->private; 1738 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1739 unsigned int total_segs = 1740 le32_to_cpu(sbi->raw_super->segment_count_main); 1741 int i; 1742 1743 seq_puts(seq, "format: segment_type|valid_blocks\n" 1744 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n"); 1745 1746 for (i = 0; i < total_segs; i++) { 1747 struct seg_entry *se = get_seg_entry(sbi, i); 1748 1749 if ((i % 10) == 0) 1750 seq_printf(seq, "%-10d", i); 1751 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks); 1752 if ((i % 10) == 9 || i == (total_segs - 1)) 1753 seq_putc(seq, '\n'); 1754 else 1755 seq_putc(seq, ' '); 1756 } 1757 1758 return 0; 1759 } 1760 1761 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq, 1762 void *offset) 1763 { 1764 struct super_block *sb = seq->private; 1765 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1766 unsigned int total_segs = 1767 le32_to_cpu(sbi->raw_super->segment_count_main); 1768 int i, j; 1769 1770 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps|mtime\n" 1771 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n"); 1772 1773 for (i = 0; i < total_segs; i++) { 1774 struct seg_entry *se = get_seg_entry(sbi, i); 1775 1776 seq_printf(seq, "%-10d", i); 1777 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks); 1778 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++) 1779 seq_printf(seq, " %.2x", se->cur_valid_map[j]); 1780 seq_printf(seq, "| %llx", se->mtime); 1781 seq_putc(seq, '\n'); 1782 } 1783 return 0; 1784 } 1785 1786 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq, 1787 void *offset) 1788 { 1789 struct super_block *sb = seq->private; 1790 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1791 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 1792 int i; 1793 1794 seq_puts(seq, "format: victim_secmap bitmaps\n"); 1795 1796 for (i = 0; i < MAIN_SECS(sbi); i++) { 1797 if ((i % 10) == 0) 1798 seq_printf(seq, "%-10d", i); 1799 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0); 1800 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1)) 1801 seq_putc(seq, '\n'); 1802 else 1803 seq_putc(seq, ' '); 1804 } 1805 return 0; 1806 } 1807 1808 static int __maybe_unused discard_plist_seq_show(struct seq_file *seq, 1809 void *offset) 1810 { 1811 struct super_block *sb = seq->private; 1812 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1813 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1814 int i, count; 1815 1816 seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n"); 1817 if (!f2fs_realtime_discard_enable(sbi)) 1818 return 0; 1819 1820 if (dcc) { 1821 mutex_lock(&dcc->cmd_lock); 1822 for (i = 0; i < MAX_PLIST_NUM; i++) { 1823 struct list_head *pend_list; 1824 struct discard_cmd *dc, *tmp; 1825 1826 if (i % 8 == 0) 1827 seq_printf(seq, " %-3d", i); 1828 count = 0; 1829 pend_list = &dcc->pend_list[i]; 1830 list_for_each_entry_safe(dc, tmp, pend_list, list) 1831 count++; 1832 if (count) 1833 seq_printf(seq, " %7d", count); 1834 else 1835 seq_puts(seq, " ."); 1836 if (i % 8 == 7) 1837 seq_putc(seq, '\n'); 1838 } 1839 seq_putc(seq, '\n'); 1840 mutex_unlock(&dcc->cmd_lock); 1841 } 1842 1843 return 0; 1844 } 1845 1846 static int __maybe_unused disk_map_seq_show(struct seq_file *seq, 1847 void *offset) 1848 { 1849 struct super_block *sb = seq->private; 1850 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1851 int i; 1852 1853 seq_printf(seq, "Address Layout : %5luB Block address (# of Segments)\n", 1854 F2FS_BLKSIZE); 1855 seq_printf(seq, " SB : %12s\n", "0/1024B"); 1856 seq_printf(seq, " seg0_blkaddr : 0x%010x\n", SEG0_BLKADDR(sbi)); 1857 seq_printf(seq, " Checkpoint : 0x%010x (%10d)\n", 1858 le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2); 1859 seq_printf(seq, " SIT : 0x%010x (%10d)\n", 1860 SIT_I(sbi)->sit_base_addr, 1861 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit)); 1862 seq_printf(seq, " NAT : 0x%010x (%10d)\n", 1863 NM_I(sbi)->nat_blkaddr, 1864 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat)); 1865 seq_printf(seq, " SSA : 0x%010x (%10d)\n", 1866 SM_I(sbi)->ssa_blkaddr, 1867 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa)); 1868 seq_printf(seq, " Main : 0x%010x (%10d)\n", 1869 SM_I(sbi)->main_blkaddr, 1870 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main)); 1871 seq_printf(seq, " Block size : %12lu KB\n", F2FS_BLKSIZE >> 10); 1872 seq_printf(seq, " Segment size : %12d MB\n", 1873 (BLKS_PER_SEG(sbi) << (F2FS_BLKSIZE_BITS - 10)) >> 10); 1874 seq_printf(seq, " Segs/Sections : %12d\n", 1875 SEGS_PER_SEC(sbi)); 1876 seq_printf(seq, " Section size : %12d MB\n", 1877 (BLKS_PER_SEC(sbi) << (F2FS_BLKSIZE_BITS - 10)) >> 10); 1878 seq_printf(seq, " # of Sections : %12d\n", 1879 le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count)); 1880 1881 if (!f2fs_is_multi_device(sbi)) 1882 return 0; 1883 1884 seq_puts(seq, "\nDisk Map for multi devices:\n"); 1885 for (i = 0; i < sbi->s_ndevs; i++) 1886 seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n", 1887 i, bdev_is_zoned(FDEV(i).bdev), 1888 FDEV(i).start_blk, FDEV(i).end_blk, 1889 FDEV(i).path); 1890 return 0; 1891 } 1892 1893 static int __maybe_unused donation_list_seq_show(struct seq_file *seq, 1894 void *offset) 1895 { 1896 struct super_block *sb = seq->private; 1897 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1898 struct inode *inode; 1899 struct f2fs_inode_info *fi; 1900 struct dentry *dentry; 1901 char *buf, *path; 1902 int i; 1903 1904 buf = f2fs_getname(sbi); 1905 if (!buf) 1906 return 0; 1907 1908 seq_printf(seq, "Donation List\n"); 1909 seq_printf(seq, " # of files : %u\n", sbi->donate_files); 1910 seq_printf(seq, " %-50s %10s %20s %20s %22s\n", 1911 "File path", "Status", "Donation offset (kb)", 1912 "Donation size (kb)", "File cached size (kb)"); 1913 seq_printf(seq, "---\n"); 1914 1915 for (i = 0; i < sbi->donate_files; i++) { 1916 spin_lock(&sbi->inode_lock[DONATE_INODE]); 1917 if (list_empty(&sbi->inode_list[DONATE_INODE])) { 1918 spin_unlock(&sbi->inode_lock[DONATE_INODE]); 1919 break; 1920 } 1921 fi = list_first_entry(&sbi->inode_list[DONATE_INODE], 1922 struct f2fs_inode_info, gdonate_list); 1923 list_move_tail(&fi->gdonate_list, &sbi->inode_list[DONATE_INODE]); 1924 inode = igrab(&fi->vfs_inode); 1925 spin_unlock(&sbi->inode_lock[DONATE_INODE]); 1926 1927 if (!inode) 1928 continue; 1929 1930 inode_lock_shared(inode); 1931 1932 dentry = d_find_alias(inode); 1933 if (!dentry) { 1934 path = NULL; 1935 } else { 1936 path = dentry_path_raw(dentry, buf, PATH_MAX); 1937 if (IS_ERR(path)) 1938 goto next; 1939 } 1940 seq_printf(seq, " %-50s %10s %20llu %20llu %22llu\n", 1941 path ? path : "<unlinked>", 1942 is_inode_flag_set(inode, FI_DONATE_FINISHED) ? 1943 "Evicted" : "Donated", 1944 (loff_t)fi->donate_start << (PAGE_SHIFT - 10), 1945 (loff_t)(fi->donate_end + 1) << (PAGE_SHIFT - 10), 1946 (loff_t)inode->i_mapping->nrpages << (PAGE_SHIFT - 10)); 1947 next: 1948 dput(dentry); 1949 inode_unlock_shared(inode); 1950 iput(inode); 1951 } 1952 f2fs_putname(buf); 1953 return 0; 1954 } 1955 1956 #ifdef CONFIG_F2FS_FAULT_INJECTION 1957 static int __maybe_unused inject_stats_seq_show(struct seq_file *seq, 1958 void *offset) 1959 { 1960 struct super_block *sb = seq->private; 1961 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1962 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info; 1963 int i; 1964 1965 seq_puts(seq, "fault_type injected_count\n"); 1966 1967 for (i = 0; i < FAULT_MAX; i++) 1968 seq_printf(seq, "%-24s%-10u\n", f2fs_fault_name[i], 1969 ffi->inject_count[i]); 1970 return 0; 1971 } 1972 #endif 1973 1974 int __init f2fs_init_sysfs(void) 1975 { 1976 int ret; 1977 1978 kobject_set_name(&f2fs_kset.kobj, "f2fs"); 1979 f2fs_kset.kobj.parent = fs_kobj; 1980 ret = kset_register(&f2fs_kset); 1981 if (ret) 1982 return ret; 1983 1984 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype, 1985 NULL, "features"); 1986 if (ret) 1987 goto put_kobject; 1988 1989 ret = kobject_init_and_add(&f2fs_tune, &f2fs_tune_ktype, 1990 NULL, "tuning"); 1991 if (ret) 1992 goto put_kobject; 1993 1994 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL); 1995 if (!f2fs_proc_root) { 1996 ret = -ENOMEM; 1997 goto put_kobject; 1998 } 1999 2000 return 0; 2001 2002 put_kobject: 2003 kobject_put(&f2fs_tune); 2004 kobject_put(&f2fs_feat); 2005 kset_unregister(&f2fs_kset); 2006 return ret; 2007 } 2008 2009 void f2fs_exit_sysfs(void) 2010 { 2011 kobject_put(&f2fs_tune); 2012 kobject_put(&f2fs_feat); 2013 kset_unregister(&f2fs_kset); 2014 remove_proc_entry("fs/f2fs", NULL); 2015 f2fs_proc_root = NULL; 2016 } 2017 2018 int f2fs_register_sysfs(struct f2fs_sb_info *sbi) 2019 { 2020 struct super_block *sb = sbi->sb; 2021 int err; 2022 2023 sbi->s_kobj.kset = &f2fs_kset; 2024 init_completion(&sbi->s_kobj_unregister); 2025 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL, 2026 "%s", sb->s_id); 2027 if (err) 2028 goto put_sb_kobj; 2029 2030 sbi->s_stat_kobj.kset = &f2fs_kset; 2031 init_completion(&sbi->s_stat_kobj_unregister); 2032 err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype, 2033 &sbi->s_kobj, "stat"); 2034 if (err) 2035 goto put_stat_kobj; 2036 2037 sbi->s_feature_list_kobj.kset = &f2fs_kset; 2038 init_completion(&sbi->s_feature_list_kobj_unregister); 2039 err = kobject_init_and_add(&sbi->s_feature_list_kobj, 2040 &f2fs_feature_list_ktype, 2041 &sbi->s_kobj, "feature_list"); 2042 if (err) 2043 goto put_feature_list_kobj; 2044 2045 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root); 2046 if (!sbi->s_proc) { 2047 err = -ENOMEM; 2048 goto put_feature_list_kobj; 2049 } 2050 2051 proc_create_single_data("segment_info", 0444, sbi->s_proc, 2052 segment_info_seq_show, sb); 2053 proc_create_single_data("segment_bits", 0444, sbi->s_proc, 2054 segment_bits_seq_show, sb); 2055 #ifdef CONFIG_F2FS_IOSTAT 2056 proc_create_single_data("iostat_info", 0444, sbi->s_proc, 2057 iostat_info_seq_show, sb); 2058 #endif 2059 proc_create_single_data("victim_bits", 0444, sbi->s_proc, 2060 victim_bits_seq_show, sb); 2061 proc_create_single_data("discard_plist_info", 0444, sbi->s_proc, 2062 discard_plist_seq_show, sb); 2063 proc_create_single_data("disk_map", 0444, sbi->s_proc, 2064 disk_map_seq_show, sb); 2065 proc_create_single_data("donation_list", 0444, sbi->s_proc, 2066 donation_list_seq_show, sb); 2067 #ifdef CONFIG_F2FS_FAULT_INJECTION 2068 proc_create_single_data("inject_stats", 0444, sbi->s_proc, 2069 inject_stats_seq_show, sb); 2070 #endif 2071 return 0; 2072 put_feature_list_kobj: 2073 kobject_put(&sbi->s_feature_list_kobj); 2074 wait_for_completion(&sbi->s_feature_list_kobj_unregister); 2075 put_stat_kobj: 2076 kobject_put(&sbi->s_stat_kobj); 2077 wait_for_completion(&sbi->s_stat_kobj_unregister); 2078 put_sb_kobj: 2079 kobject_put(&sbi->s_kobj); 2080 wait_for_completion(&sbi->s_kobj_unregister); 2081 return err; 2082 } 2083 2084 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi) 2085 { 2086 remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root); 2087 2088 kobject_put(&sbi->s_stat_kobj); 2089 wait_for_completion(&sbi->s_stat_kobj_unregister); 2090 kobject_put(&sbi->s_feature_list_kobj); 2091 wait_for_completion(&sbi->s_feature_list_kobj_unregister); 2092 2093 kobject_put(&sbi->s_kobj); 2094 wait_for_completion(&sbi->s_kobj_unregister); 2095 } 2096