1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * bcache sysfs interfaces 4 * 5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 6 * Copyright 2012 Google, Inc. 7 */ 8 9 #include "bcache.h" 10 #include "sysfs.h" 11 #include "btree.h" 12 #include "request.h" 13 #include "writeback.h" 14 15 #include <linux/blkdev.h> 16 #include <linux/sort.h> 17 #include <linux/sched/clock.h> 18 19 extern bool bcache_is_reboot; 20 21 /* Default is 0 ("writethrough") */ 22 static const char * const bch_cache_modes[] = { 23 "writethrough", 24 "writeback", 25 "writearound", 26 "none" 27 }; 28 29 /* Default is 0 ("auto") */ 30 static const char * const bch_stop_on_failure_modes[] = { 31 "auto", 32 "always" 33 }; 34 35 static const char * const cache_replacement_policies[] = { 36 "lru", 37 "fifo", 38 "random" 39 }; 40 41 static const char * const error_actions[] = { 42 "unregister", 43 "panic" 44 }; 45 46 write_attribute(attach); 47 write_attribute(detach); 48 write_attribute(unregister); 49 write_attribute(stop); 50 write_attribute(clear_stats); 51 write_attribute(trigger_gc); 52 write_attribute(prune_cache); 53 write_attribute(flash_vol_create); 54 55 read_attribute(bucket_size); 56 read_attribute(block_size); 57 read_attribute(nbuckets); 58 read_attribute(tree_depth); 59 read_attribute(root_usage_percent); 60 read_attribute(priority_stats); 61 read_attribute(btree_cache_size); 62 read_attribute(btree_cache_max_chain); 63 read_attribute(cache_available_percent); 64 read_attribute(written); 65 read_attribute(btree_written); 66 read_attribute(metadata_written); 67 read_attribute(active_journal_entries); 68 read_attribute(backing_dev_name); 69 read_attribute(backing_dev_uuid); 70 71 sysfs_time_stats_attribute(btree_gc, sec, ms); 72 sysfs_time_stats_attribute(btree_split, sec, us); 73 sysfs_time_stats_attribute(btree_sort, ms, us); 74 sysfs_time_stats_attribute(btree_read, ms, us); 75 76 read_attribute(btree_nodes); 77 read_attribute(btree_used_percent); 78 read_attribute(average_key_size); 79 read_attribute(dirty_data); 80 read_attribute(bset_tree_stats); 81 82 read_attribute(state); 83 read_attribute(cache_read_races); 84 read_attribute(reclaim); 85 read_attribute(reclaimed_journal_buckets); 86 read_attribute(flush_write); 87 read_attribute(writeback_keys_done); 88 read_attribute(writeback_keys_failed); 89 read_attribute(io_errors); 90 read_attribute(congested); 91 read_attribute(cutoff_writeback); 92 read_attribute(cutoff_writeback_sync); 93 rw_attribute(congested_read_threshold_us); 94 rw_attribute(congested_write_threshold_us); 95 96 rw_attribute(sequential_cutoff); 97 rw_attribute(data_csum); 98 rw_attribute(cache_mode); 99 rw_attribute(stop_when_cache_set_failed); 100 rw_attribute(writeback_metadata); 101 rw_attribute(writeback_running); 102 rw_attribute(writeback_percent); 103 rw_attribute(writeback_delay); 104 rw_attribute(writeback_rate); 105 106 rw_attribute(writeback_rate_update_seconds); 107 rw_attribute(writeback_rate_i_term_inverse); 108 rw_attribute(writeback_rate_p_term_inverse); 109 rw_attribute(writeback_rate_minimum); 110 read_attribute(writeback_rate_debug); 111 112 read_attribute(stripe_size); 113 read_attribute(partial_stripes_expensive); 114 115 rw_attribute(synchronous); 116 rw_attribute(journal_delay_ms); 117 rw_attribute(io_disable); 118 rw_attribute(discard); 119 rw_attribute(running); 120 rw_attribute(label); 121 rw_attribute(readahead); 122 rw_attribute(errors); 123 rw_attribute(io_error_limit); 124 rw_attribute(io_error_halflife); 125 rw_attribute(verify); 126 rw_attribute(bypass_torture_test); 127 rw_attribute(key_merging_disabled); 128 rw_attribute(gc_always_rewrite); 129 rw_attribute(expensive_debug_checks); 130 rw_attribute(cache_replacement_policy); 131 rw_attribute(btree_shrinker_disabled); 132 rw_attribute(copy_gc_enabled); 133 rw_attribute(gc_after_writeback); 134 rw_attribute(size); 135 136 static ssize_t bch_snprint_string_list(char *buf, 137 size_t size, 138 const char * const list[], 139 size_t selected) 140 { 141 char *out = buf; 142 size_t i; 143 144 for (i = 0; list[i]; i++) 145 out += snprintf(out, buf + size - out, 146 i == selected ? "[%s] " : "%s ", list[i]); 147 148 out[-1] = '\n'; 149 return out - buf; 150 } 151 152 SHOW(__bch_cached_dev) 153 { 154 struct cached_dev *dc = container_of(kobj, struct cached_dev, 155 disk.kobj); 156 char const *states[] = { "no cache", "clean", "dirty", "inconsistent" }; 157 int wb = dc->writeback_running; 158 159 #define var(stat) (dc->stat) 160 161 if (attr == &sysfs_cache_mode) 162 return bch_snprint_string_list(buf, PAGE_SIZE, 163 bch_cache_modes, 164 BDEV_CACHE_MODE(&dc->sb)); 165 166 if (attr == &sysfs_stop_when_cache_set_failed) 167 return bch_snprint_string_list(buf, PAGE_SIZE, 168 bch_stop_on_failure_modes, 169 dc->stop_when_cache_set_failed); 170 171 172 sysfs_printf(data_csum, "%i", dc->disk.data_csum); 173 var_printf(verify, "%i"); 174 var_printf(bypass_torture_test, "%i"); 175 var_printf(writeback_metadata, "%i"); 176 var_printf(writeback_running, "%i"); 177 var_print(writeback_delay); 178 var_print(writeback_percent); 179 sysfs_hprint(writeback_rate, 180 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0); 181 sysfs_printf(io_errors, "%i", atomic_read(&dc->io_errors)); 182 sysfs_printf(io_error_limit, "%i", dc->error_limit); 183 sysfs_printf(io_disable, "%i", dc->io_disable); 184 var_print(writeback_rate_update_seconds); 185 var_print(writeback_rate_i_term_inverse); 186 var_print(writeback_rate_p_term_inverse); 187 var_print(writeback_rate_minimum); 188 189 if (attr == &sysfs_writeback_rate_debug) { 190 char rate[20]; 191 char dirty[20]; 192 char target[20]; 193 char proportional[20]; 194 char integral[20]; 195 char change[20]; 196 s64 next_io; 197 198 /* 199 * Except for dirty and target, other values should 200 * be 0 if writeback is not running. 201 */ 202 bch_hprint(rate, 203 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 204 : 0); 205 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9); 206 bch_hprint(target, dc->writeback_rate_target << 9); 207 bch_hprint(proportional, 208 wb ? dc->writeback_rate_proportional << 9 : 0); 209 bch_hprint(integral, 210 wb ? dc->writeback_rate_integral_scaled << 9 : 0); 211 bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0); 212 next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(), 213 NSEC_PER_MSEC) : 0; 214 215 return sprintf(buf, 216 "rate:\t\t%s/sec\n" 217 "dirty:\t\t%s\n" 218 "target:\t\t%s\n" 219 "proportional:\t%s\n" 220 "integral:\t%s\n" 221 "change:\t\t%s/sec\n" 222 "next io:\t%llims\n", 223 rate, dirty, target, proportional, 224 integral, change, next_io); 225 } 226 227 sysfs_hprint(dirty_data, 228 bcache_dev_sectors_dirty(&dc->disk) << 9); 229 230 sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9); 231 var_printf(partial_stripes_expensive, "%u"); 232 233 var_hprint(sequential_cutoff); 234 var_hprint(readahead); 235 236 sysfs_print(running, atomic_read(&dc->running)); 237 sysfs_print(state, states[BDEV_STATE(&dc->sb)]); 238 239 if (attr == &sysfs_label) { 240 memcpy(buf, dc->sb.label, SB_LABEL_SIZE); 241 buf[SB_LABEL_SIZE + 1] = '\0'; 242 strcat(buf, "\n"); 243 return strlen(buf); 244 } 245 246 if (attr == &sysfs_backing_dev_name) { 247 snprintf(buf, BDEVNAME_SIZE + 1, "%s", dc->backing_dev_name); 248 strcat(buf, "\n"); 249 return strlen(buf); 250 } 251 252 if (attr == &sysfs_backing_dev_uuid) { 253 /* convert binary uuid into 36-byte string plus '\0' */ 254 snprintf(buf, 36+1, "%pU", dc->sb.uuid); 255 strcat(buf, "\n"); 256 return strlen(buf); 257 } 258 259 #undef var 260 return 0; 261 } 262 SHOW_LOCKED(bch_cached_dev) 263 264 STORE(__cached_dev) 265 { 266 struct cached_dev *dc = container_of(kobj, struct cached_dev, 267 disk.kobj); 268 ssize_t v; 269 struct cache_set *c; 270 struct kobj_uevent_env *env; 271 272 /* no user space access if system is rebooting */ 273 if (bcache_is_reboot) 274 return -EBUSY; 275 276 #define d_strtoul(var) sysfs_strtoul(var, dc->var) 277 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX) 278 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var) 279 280 sysfs_strtoul(data_csum, dc->disk.data_csum); 281 d_strtoul(verify); 282 sysfs_strtoul_bool(bypass_torture_test, dc->bypass_torture_test); 283 sysfs_strtoul_bool(writeback_metadata, dc->writeback_metadata); 284 sysfs_strtoul_bool(writeback_running, dc->writeback_running); 285 sysfs_strtoul_clamp(writeback_delay, dc->writeback_delay, 0, UINT_MAX); 286 287 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 288 0, bch_cutoff_writeback); 289 290 if (attr == &sysfs_writeback_rate) { 291 ssize_t ret; 292 long int v = atomic_long_read(&dc->writeback_rate.rate); 293 294 ret = strtoul_safe_clamp(buf, v, 1, INT_MAX); 295 296 if (!ret) { 297 atomic_long_set(&dc->writeback_rate.rate, v); 298 ret = size; 299 } 300 301 return ret; 302 } 303 304 sysfs_strtoul_clamp(writeback_rate_update_seconds, 305 dc->writeback_rate_update_seconds, 306 1, WRITEBACK_RATE_UPDATE_SECS_MAX); 307 sysfs_strtoul_clamp(writeback_rate_i_term_inverse, 308 dc->writeback_rate_i_term_inverse, 309 1, UINT_MAX); 310 sysfs_strtoul_clamp(writeback_rate_p_term_inverse, 311 dc->writeback_rate_p_term_inverse, 312 1, UINT_MAX); 313 sysfs_strtoul_clamp(writeback_rate_minimum, 314 dc->writeback_rate_minimum, 315 1, UINT_MAX); 316 317 sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX); 318 319 if (attr == &sysfs_io_disable) { 320 int v = strtoul_or_return(buf); 321 322 dc->io_disable = v ? 1 : 0; 323 } 324 325 sysfs_strtoul_clamp(sequential_cutoff, 326 dc->sequential_cutoff, 327 0, UINT_MAX); 328 d_strtoi_h(readahead); 329 330 if (attr == &sysfs_clear_stats) 331 bch_cache_accounting_clear(&dc->accounting); 332 333 if (attr == &sysfs_running && 334 strtoul_or_return(buf)) { 335 v = bch_cached_dev_run(dc); 336 if (v) 337 return v; 338 } 339 340 if (attr == &sysfs_cache_mode) { 341 v = sysfs_match_string(bch_cache_modes, buf); 342 if (v < 0) 343 return v; 344 345 if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) { 346 SET_BDEV_CACHE_MODE(&dc->sb, v); 347 bch_write_bdev_super(dc, NULL); 348 } 349 } 350 351 if (attr == &sysfs_stop_when_cache_set_failed) { 352 v = sysfs_match_string(bch_stop_on_failure_modes, buf); 353 if (v < 0) 354 return v; 355 356 dc->stop_when_cache_set_failed = v; 357 } 358 359 if (attr == &sysfs_label) { 360 if (size > SB_LABEL_SIZE) 361 return -EINVAL; 362 memcpy(dc->sb.label, buf, size); 363 if (size < SB_LABEL_SIZE) 364 dc->sb.label[size] = '\0'; 365 if (size && dc->sb.label[size - 1] == '\n') 366 dc->sb.label[size - 1] = '\0'; 367 bch_write_bdev_super(dc, NULL); 368 if (dc->disk.c) { 369 memcpy(dc->disk.c->uuids[dc->disk.id].label, 370 buf, SB_LABEL_SIZE); 371 bch_uuid_write(dc->disk.c); 372 } 373 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 374 if (!env) 375 return -ENOMEM; 376 add_uevent_var(env, "DRIVER=bcache"); 377 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid), 378 add_uevent_var(env, "CACHED_LABEL=%s", buf); 379 kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj, 380 KOBJ_CHANGE, 381 env->envp); 382 kfree(env); 383 } 384 385 if (attr == &sysfs_attach) { 386 uint8_t set_uuid[16]; 387 388 if (bch_parse_uuid(buf, set_uuid) < 16) 389 return -EINVAL; 390 391 v = -ENOENT; 392 list_for_each_entry(c, &bch_cache_sets, list) { 393 v = bch_cached_dev_attach(dc, c, set_uuid); 394 if (!v) 395 return size; 396 } 397 if (v == -ENOENT) 398 pr_err("Can't attach %s: cache set not found", buf); 399 return v; 400 } 401 402 if (attr == &sysfs_detach && dc->disk.c) 403 bch_cached_dev_detach(dc); 404 405 if (attr == &sysfs_stop) 406 bcache_device_stop(&dc->disk); 407 408 return size; 409 } 410 411 STORE(bch_cached_dev) 412 { 413 struct cached_dev *dc = container_of(kobj, struct cached_dev, 414 disk.kobj); 415 416 /* no user space access if system is rebooting */ 417 if (bcache_is_reboot) 418 return -EBUSY; 419 420 mutex_lock(&bch_register_lock); 421 size = __cached_dev_store(kobj, attr, buf, size); 422 423 if (attr == &sysfs_writeback_running) { 424 /* dc->writeback_running changed in __cached_dev_store() */ 425 if (IS_ERR_OR_NULL(dc->writeback_thread)) { 426 /* 427 * reject setting it to 1 via sysfs if writeback 428 * kthread is not created yet. 429 */ 430 if (dc->writeback_running) { 431 dc->writeback_running = false; 432 pr_err("%s: failed to run non-existent writeback thread", 433 dc->disk.disk->disk_name); 434 } 435 } else 436 /* 437 * writeback kthread will check if dc->writeback_running 438 * is true or false. 439 */ 440 bch_writeback_queue(dc); 441 } 442 443 /* 444 * Only set BCACHE_DEV_WB_RUNNING when cached device attached to 445 * a cache set, otherwise it doesn't make sense. 446 */ 447 if (attr == &sysfs_writeback_percent) 448 if ((dc->disk.c != NULL) && 449 (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))) 450 schedule_delayed_work(&dc->writeback_rate_update, 451 dc->writeback_rate_update_seconds * HZ); 452 453 mutex_unlock(&bch_register_lock); 454 return size; 455 } 456 457 static struct attribute *bch_cached_dev_files[] = { 458 &sysfs_attach, 459 &sysfs_detach, 460 &sysfs_stop, 461 #if 0 462 &sysfs_data_csum, 463 #endif 464 &sysfs_cache_mode, 465 &sysfs_stop_when_cache_set_failed, 466 &sysfs_writeback_metadata, 467 &sysfs_writeback_running, 468 &sysfs_writeback_delay, 469 &sysfs_writeback_percent, 470 &sysfs_writeback_rate, 471 &sysfs_writeback_rate_update_seconds, 472 &sysfs_writeback_rate_i_term_inverse, 473 &sysfs_writeback_rate_p_term_inverse, 474 &sysfs_writeback_rate_minimum, 475 &sysfs_writeback_rate_debug, 476 &sysfs_io_errors, 477 &sysfs_io_error_limit, 478 &sysfs_io_disable, 479 &sysfs_dirty_data, 480 &sysfs_stripe_size, 481 &sysfs_partial_stripes_expensive, 482 &sysfs_sequential_cutoff, 483 &sysfs_clear_stats, 484 &sysfs_running, 485 &sysfs_state, 486 &sysfs_label, 487 &sysfs_readahead, 488 #ifdef CONFIG_BCACHE_DEBUG 489 &sysfs_verify, 490 &sysfs_bypass_torture_test, 491 #endif 492 &sysfs_backing_dev_name, 493 &sysfs_backing_dev_uuid, 494 NULL 495 }; 496 KTYPE(bch_cached_dev); 497 498 SHOW(bch_flash_dev) 499 { 500 struct bcache_device *d = container_of(kobj, struct bcache_device, 501 kobj); 502 struct uuid_entry *u = &d->c->uuids[d->id]; 503 504 sysfs_printf(data_csum, "%i", d->data_csum); 505 sysfs_hprint(size, u->sectors << 9); 506 507 if (attr == &sysfs_label) { 508 memcpy(buf, u->label, SB_LABEL_SIZE); 509 buf[SB_LABEL_SIZE + 1] = '\0'; 510 strcat(buf, "\n"); 511 return strlen(buf); 512 } 513 514 return 0; 515 } 516 517 STORE(__bch_flash_dev) 518 { 519 struct bcache_device *d = container_of(kobj, struct bcache_device, 520 kobj); 521 struct uuid_entry *u = &d->c->uuids[d->id]; 522 523 /* no user space access if system is rebooting */ 524 if (bcache_is_reboot) 525 return -EBUSY; 526 527 sysfs_strtoul(data_csum, d->data_csum); 528 529 if (attr == &sysfs_size) { 530 uint64_t v; 531 532 strtoi_h_or_return(buf, v); 533 534 u->sectors = v >> 9; 535 bch_uuid_write(d->c); 536 set_capacity(d->disk, u->sectors); 537 } 538 539 if (attr == &sysfs_label) { 540 memcpy(u->label, buf, SB_LABEL_SIZE); 541 bch_uuid_write(d->c); 542 } 543 544 if (attr == &sysfs_unregister) { 545 set_bit(BCACHE_DEV_DETACHING, &d->flags); 546 bcache_device_stop(d); 547 } 548 549 return size; 550 } 551 STORE_LOCKED(bch_flash_dev) 552 553 static struct attribute *bch_flash_dev_files[] = { 554 &sysfs_unregister, 555 #if 0 556 &sysfs_data_csum, 557 #endif 558 &sysfs_label, 559 &sysfs_size, 560 NULL 561 }; 562 KTYPE(bch_flash_dev); 563 564 struct bset_stats_op { 565 struct btree_op op; 566 size_t nodes; 567 struct bset_stats stats; 568 }; 569 570 static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b) 571 { 572 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op); 573 574 op->nodes++; 575 bch_btree_keys_stats(&b->keys, &op->stats); 576 577 return MAP_CONTINUE; 578 } 579 580 static int bch_bset_print_stats(struct cache_set *c, char *buf) 581 { 582 struct bset_stats_op op; 583 int ret; 584 585 memset(&op, 0, sizeof(op)); 586 bch_btree_op_init(&op.op, -1); 587 588 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats); 589 if (ret < 0) 590 return ret; 591 592 return snprintf(buf, PAGE_SIZE, 593 "btree nodes: %zu\n" 594 "written sets: %zu\n" 595 "unwritten sets: %zu\n" 596 "written key bytes: %zu\n" 597 "unwritten key bytes: %zu\n" 598 "floats: %zu\n" 599 "failed: %zu\n", 600 op.nodes, 601 op.stats.sets_written, op.stats.sets_unwritten, 602 op.stats.bytes_written, op.stats.bytes_unwritten, 603 op.stats.floats, op.stats.failed); 604 } 605 606 static unsigned int bch_root_usage(struct cache_set *c) 607 { 608 unsigned int bytes = 0; 609 struct bkey *k; 610 struct btree *b; 611 struct btree_iter iter; 612 613 goto lock_root; 614 615 do { 616 rw_unlock(false, b); 617 lock_root: 618 b = c->root; 619 rw_lock(false, b, b->level); 620 } while (b != c->root); 621 622 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) 623 bytes += bkey_bytes(k); 624 625 rw_unlock(false, b); 626 627 return (bytes * 100) / btree_bytes(c); 628 } 629 630 static size_t bch_cache_size(struct cache_set *c) 631 { 632 size_t ret = 0; 633 struct btree *b; 634 635 mutex_lock(&c->bucket_lock); 636 list_for_each_entry(b, &c->btree_cache, list) 637 ret += 1 << (b->keys.page_order + PAGE_SHIFT); 638 639 mutex_unlock(&c->bucket_lock); 640 return ret; 641 } 642 643 static unsigned int bch_cache_max_chain(struct cache_set *c) 644 { 645 unsigned int ret = 0; 646 struct hlist_head *h; 647 648 mutex_lock(&c->bucket_lock); 649 650 for (h = c->bucket_hash; 651 h < c->bucket_hash + (1 << BUCKET_HASH_BITS); 652 h++) { 653 unsigned int i = 0; 654 struct hlist_node *p; 655 656 hlist_for_each(p, h) 657 i++; 658 659 ret = max(ret, i); 660 } 661 662 mutex_unlock(&c->bucket_lock); 663 return ret; 664 } 665 666 static unsigned int bch_btree_used(struct cache_set *c) 667 { 668 return div64_u64(c->gc_stats.key_bytes * 100, 669 (c->gc_stats.nodes ?: 1) * btree_bytes(c)); 670 } 671 672 static unsigned int bch_average_key_size(struct cache_set *c) 673 { 674 return c->gc_stats.nkeys 675 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys) 676 : 0; 677 } 678 679 SHOW(__bch_cache_set) 680 { 681 struct cache_set *c = container_of(kobj, struct cache_set, kobj); 682 683 sysfs_print(synchronous, CACHE_SYNC(&c->sb)); 684 sysfs_print(journal_delay_ms, c->journal_delay_ms); 685 sysfs_hprint(bucket_size, bucket_bytes(c)); 686 sysfs_hprint(block_size, block_bytes(c)); 687 sysfs_print(tree_depth, c->root->level); 688 sysfs_print(root_usage_percent, bch_root_usage(c)); 689 690 sysfs_hprint(btree_cache_size, bch_cache_size(c)); 691 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c)); 692 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use); 693 694 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms); 695 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us); 696 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us); 697 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us); 698 699 sysfs_print(btree_used_percent, bch_btree_used(c)); 700 sysfs_print(btree_nodes, c->gc_stats.nodes); 701 sysfs_hprint(average_key_size, bch_average_key_size(c)); 702 703 sysfs_print(cache_read_races, 704 atomic_long_read(&c->cache_read_races)); 705 706 sysfs_print(reclaim, 707 atomic_long_read(&c->reclaim)); 708 709 sysfs_print(reclaimed_journal_buckets, 710 atomic_long_read(&c->reclaimed_journal_buckets)); 711 712 sysfs_print(flush_write, 713 atomic_long_read(&c->flush_write)); 714 715 sysfs_print(writeback_keys_done, 716 atomic_long_read(&c->writeback_keys_done)); 717 sysfs_print(writeback_keys_failed, 718 atomic_long_read(&c->writeback_keys_failed)); 719 720 if (attr == &sysfs_errors) 721 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions, 722 c->on_error); 723 724 /* See count_io_errors for why 88 */ 725 sysfs_print(io_error_halflife, c->error_decay * 88); 726 sysfs_print(io_error_limit, c->error_limit); 727 728 sysfs_hprint(congested, 729 ((uint64_t) bch_get_congested(c)) << 9); 730 sysfs_print(congested_read_threshold_us, 731 c->congested_read_threshold_us); 732 sysfs_print(congested_write_threshold_us, 733 c->congested_write_threshold_us); 734 735 sysfs_print(cutoff_writeback, bch_cutoff_writeback); 736 sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync); 737 738 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin)); 739 sysfs_printf(verify, "%i", c->verify); 740 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled); 741 sysfs_printf(expensive_debug_checks, 742 "%i", c->expensive_debug_checks); 743 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite); 744 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled); 745 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled); 746 sysfs_printf(gc_after_writeback, "%i", c->gc_after_writeback); 747 sysfs_printf(io_disable, "%i", 748 test_bit(CACHE_SET_IO_DISABLE, &c->flags)); 749 750 if (attr == &sysfs_bset_tree_stats) 751 return bch_bset_print_stats(c, buf); 752 753 return 0; 754 } 755 SHOW_LOCKED(bch_cache_set) 756 757 STORE(__bch_cache_set) 758 { 759 struct cache_set *c = container_of(kobj, struct cache_set, kobj); 760 ssize_t v; 761 762 /* no user space access if system is rebooting */ 763 if (bcache_is_reboot) 764 return -EBUSY; 765 766 if (attr == &sysfs_unregister) 767 bch_cache_set_unregister(c); 768 769 if (attr == &sysfs_stop) 770 bch_cache_set_stop(c); 771 772 if (attr == &sysfs_synchronous) { 773 bool sync = strtoul_or_return(buf); 774 775 if (sync != CACHE_SYNC(&c->sb)) { 776 SET_CACHE_SYNC(&c->sb, sync); 777 bcache_write_super(c); 778 } 779 } 780 781 if (attr == &sysfs_flash_vol_create) { 782 int r; 783 uint64_t v; 784 785 strtoi_h_or_return(buf, v); 786 787 r = bch_flash_dev_create(c, v); 788 if (r) 789 return r; 790 } 791 792 if (attr == &sysfs_clear_stats) { 793 atomic_long_set(&c->writeback_keys_done, 0); 794 atomic_long_set(&c->writeback_keys_failed, 0); 795 796 memset(&c->gc_stats, 0, sizeof(struct gc_stat)); 797 bch_cache_accounting_clear(&c->accounting); 798 } 799 800 if (attr == &sysfs_trigger_gc) 801 force_wake_up_gc(c); 802 803 if (attr == &sysfs_prune_cache) { 804 struct shrink_control sc; 805 806 sc.gfp_mask = GFP_KERNEL; 807 sc.nr_to_scan = strtoul_or_return(buf); 808 c->shrink.scan_objects(&c->shrink, &sc); 809 } 810 811 sysfs_strtoul_clamp(congested_read_threshold_us, 812 c->congested_read_threshold_us, 813 0, UINT_MAX); 814 sysfs_strtoul_clamp(congested_write_threshold_us, 815 c->congested_write_threshold_us, 816 0, UINT_MAX); 817 818 if (attr == &sysfs_errors) { 819 v = sysfs_match_string(error_actions, buf); 820 if (v < 0) 821 return v; 822 823 c->on_error = v; 824 } 825 826 sysfs_strtoul_clamp(io_error_limit, c->error_limit, 0, UINT_MAX); 827 828 /* See count_io_errors() for why 88 */ 829 if (attr == &sysfs_io_error_halflife) { 830 unsigned long v = 0; 831 ssize_t ret; 832 833 ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX); 834 if (!ret) { 835 c->error_decay = v / 88; 836 return size; 837 } 838 return ret; 839 } 840 841 if (attr == &sysfs_io_disable) { 842 v = strtoul_or_return(buf); 843 if (v) { 844 if (test_and_set_bit(CACHE_SET_IO_DISABLE, 845 &c->flags)) 846 pr_warn("CACHE_SET_IO_DISABLE already set"); 847 } else { 848 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE, 849 &c->flags)) 850 pr_warn("CACHE_SET_IO_DISABLE already cleared"); 851 } 852 } 853 854 sysfs_strtoul_clamp(journal_delay_ms, 855 c->journal_delay_ms, 856 0, USHRT_MAX); 857 sysfs_strtoul_bool(verify, c->verify); 858 sysfs_strtoul_bool(key_merging_disabled, c->key_merging_disabled); 859 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks); 860 sysfs_strtoul_bool(gc_always_rewrite, c->gc_always_rewrite); 861 sysfs_strtoul_bool(btree_shrinker_disabled, c->shrinker_disabled); 862 sysfs_strtoul_bool(copy_gc_enabled, c->copy_gc_enabled); 863 /* 864 * write gc_after_writeback here may overwrite an already set 865 * BCH_DO_AUTO_GC, it doesn't matter because this flag will be 866 * set in next chance. 867 */ 868 sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1); 869 870 return size; 871 } 872 STORE_LOCKED(bch_cache_set) 873 874 SHOW(bch_cache_set_internal) 875 { 876 struct cache_set *c = container_of(kobj, struct cache_set, internal); 877 878 return bch_cache_set_show(&c->kobj, attr, buf); 879 } 880 881 STORE(bch_cache_set_internal) 882 { 883 struct cache_set *c = container_of(kobj, struct cache_set, internal); 884 885 /* no user space access if system is rebooting */ 886 if (bcache_is_reboot) 887 return -EBUSY; 888 889 return bch_cache_set_store(&c->kobj, attr, buf, size); 890 } 891 892 static void bch_cache_set_internal_release(struct kobject *k) 893 { 894 } 895 896 static struct attribute *bch_cache_set_files[] = { 897 &sysfs_unregister, 898 &sysfs_stop, 899 &sysfs_synchronous, 900 &sysfs_journal_delay_ms, 901 &sysfs_flash_vol_create, 902 903 &sysfs_bucket_size, 904 &sysfs_block_size, 905 &sysfs_tree_depth, 906 &sysfs_root_usage_percent, 907 &sysfs_btree_cache_size, 908 &sysfs_cache_available_percent, 909 910 &sysfs_average_key_size, 911 912 &sysfs_errors, 913 &sysfs_io_error_limit, 914 &sysfs_io_error_halflife, 915 &sysfs_congested, 916 &sysfs_congested_read_threshold_us, 917 &sysfs_congested_write_threshold_us, 918 &sysfs_clear_stats, 919 NULL 920 }; 921 KTYPE(bch_cache_set); 922 923 static struct attribute *bch_cache_set_internal_files[] = { 924 &sysfs_active_journal_entries, 925 926 sysfs_time_stats_attribute_list(btree_gc, sec, ms) 927 sysfs_time_stats_attribute_list(btree_split, sec, us) 928 sysfs_time_stats_attribute_list(btree_sort, ms, us) 929 sysfs_time_stats_attribute_list(btree_read, ms, us) 930 931 &sysfs_btree_nodes, 932 &sysfs_btree_used_percent, 933 &sysfs_btree_cache_max_chain, 934 935 &sysfs_bset_tree_stats, 936 &sysfs_cache_read_races, 937 &sysfs_reclaim, 938 &sysfs_reclaimed_journal_buckets, 939 &sysfs_flush_write, 940 &sysfs_writeback_keys_done, 941 &sysfs_writeback_keys_failed, 942 943 &sysfs_trigger_gc, 944 &sysfs_prune_cache, 945 #ifdef CONFIG_BCACHE_DEBUG 946 &sysfs_verify, 947 &sysfs_key_merging_disabled, 948 &sysfs_expensive_debug_checks, 949 #endif 950 &sysfs_gc_always_rewrite, 951 &sysfs_btree_shrinker_disabled, 952 &sysfs_copy_gc_enabled, 953 &sysfs_gc_after_writeback, 954 &sysfs_io_disable, 955 &sysfs_cutoff_writeback, 956 &sysfs_cutoff_writeback_sync, 957 NULL 958 }; 959 KTYPE(bch_cache_set_internal); 960 961 static int __bch_cache_cmp(const void *l, const void *r) 962 { 963 return *((uint16_t *)r) - *((uint16_t *)l); 964 } 965 966 SHOW(__bch_cache) 967 { 968 struct cache *ca = container_of(kobj, struct cache, kobj); 969 970 sysfs_hprint(bucket_size, bucket_bytes(ca)); 971 sysfs_hprint(block_size, block_bytes(ca)); 972 sysfs_print(nbuckets, ca->sb.nbuckets); 973 sysfs_print(discard, ca->discard); 974 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9); 975 sysfs_hprint(btree_written, 976 atomic_long_read(&ca->btree_sectors_written) << 9); 977 sysfs_hprint(metadata_written, 978 (atomic_long_read(&ca->meta_sectors_written) + 979 atomic_long_read(&ca->btree_sectors_written)) << 9); 980 981 sysfs_print(io_errors, 982 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT); 983 984 if (attr == &sysfs_cache_replacement_policy) 985 return bch_snprint_string_list(buf, PAGE_SIZE, 986 cache_replacement_policies, 987 CACHE_REPLACEMENT(&ca->sb)); 988 989 if (attr == &sysfs_priority_stats) { 990 struct bucket *b; 991 size_t n = ca->sb.nbuckets, i; 992 size_t unused = 0, available = 0, dirty = 0, meta = 0; 993 uint64_t sum = 0; 994 /* Compute 31 quantiles */ 995 uint16_t q[31], *p, *cached; 996 ssize_t ret; 997 998 cached = p = vmalloc(array_size(sizeof(uint16_t), 999 ca->sb.nbuckets)); 1000 if (!p) 1001 return -ENOMEM; 1002 1003 mutex_lock(&ca->set->bucket_lock); 1004 for_each_bucket(b, ca) { 1005 if (!GC_SECTORS_USED(b)) 1006 unused++; 1007 if (GC_MARK(b) == GC_MARK_RECLAIMABLE) 1008 available++; 1009 if (GC_MARK(b) == GC_MARK_DIRTY) 1010 dirty++; 1011 if (GC_MARK(b) == GC_MARK_METADATA) 1012 meta++; 1013 } 1014 1015 for (i = ca->sb.first_bucket; i < n; i++) 1016 p[i] = ca->buckets[i].prio; 1017 mutex_unlock(&ca->set->bucket_lock); 1018 1019 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL); 1020 1021 while (n && 1022 !cached[n - 1]) 1023 --n; 1024 1025 while (cached < p + n && 1026 *cached == BTREE_PRIO) 1027 cached++, n--; 1028 1029 for (i = 0; i < n; i++) 1030 sum += INITIAL_PRIO - cached[i]; 1031 1032 if (n) 1033 do_div(sum, n); 1034 1035 for (i = 0; i < ARRAY_SIZE(q); i++) 1036 q[i] = INITIAL_PRIO - cached[n * (i + 1) / 1037 (ARRAY_SIZE(q) + 1)]; 1038 1039 vfree(p); 1040 1041 ret = scnprintf(buf, PAGE_SIZE, 1042 "Unused: %zu%%\n" 1043 "Clean: %zu%%\n" 1044 "Dirty: %zu%%\n" 1045 "Metadata: %zu%%\n" 1046 "Average: %llu\n" 1047 "Sectors per Q: %zu\n" 1048 "Quantiles: [", 1049 unused * 100 / (size_t) ca->sb.nbuckets, 1050 available * 100 / (size_t) ca->sb.nbuckets, 1051 dirty * 100 / (size_t) ca->sb.nbuckets, 1052 meta * 100 / (size_t) ca->sb.nbuckets, sum, 1053 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1)); 1054 1055 for (i = 0; i < ARRAY_SIZE(q); i++) 1056 ret += scnprintf(buf + ret, PAGE_SIZE - ret, 1057 "%u ", q[i]); 1058 ret--; 1059 1060 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n"); 1061 1062 return ret; 1063 } 1064 1065 return 0; 1066 } 1067 SHOW_LOCKED(bch_cache) 1068 1069 STORE(__bch_cache) 1070 { 1071 struct cache *ca = container_of(kobj, struct cache, kobj); 1072 ssize_t v; 1073 1074 /* no user space access if system is rebooting */ 1075 if (bcache_is_reboot) 1076 return -EBUSY; 1077 1078 if (attr == &sysfs_discard) { 1079 bool v = strtoul_or_return(buf); 1080 1081 if (blk_queue_discard(bdev_get_queue(ca->bdev))) 1082 ca->discard = v; 1083 1084 if (v != CACHE_DISCARD(&ca->sb)) { 1085 SET_CACHE_DISCARD(&ca->sb, v); 1086 bcache_write_super(ca->set); 1087 } 1088 } 1089 1090 if (attr == &sysfs_cache_replacement_policy) { 1091 v = sysfs_match_string(cache_replacement_policies, buf); 1092 if (v < 0) 1093 return v; 1094 1095 if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) { 1096 mutex_lock(&ca->set->bucket_lock); 1097 SET_CACHE_REPLACEMENT(&ca->sb, v); 1098 mutex_unlock(&ca->set->bucket_lock); 1099 1100 bcache_write_super(ca->set); 1101 } 1102 } 1103 1104 if (attr == &sysfs_clear_stats) { 1105 atomic_long_set(&ca->sectors_written, 0); 1106 atomic_long_set(&ca->btree_sectors_written, 0); 1107 atomic_long_set(&ca->meta_sectors_written, 0); 1108 atomic_set(&ca->io_count, 0); 1109 atomic_set(&ca->io_errors, 0); 1110 } 1111 1112 return size; 1113 } 1114 STORE_LOCKED(bch_cache) 1115 1116 static struct attribute *bch_cache_files[] = { 1117 &sysfs_bucket_size, 1118 &sysfs_block_size, 1119 &sysfs_nbuckets, 1120 &sysfs_priority_stats, 1121 &sysfs_discard, 1122 &sysfs_written, 1123 &sysfs_btree_written, 1124 &sysfs_metadata_written, 1125 &sysfs_io_errors, 1126 &sysfs_clear_stats, 1127 &sysfs_cache_replacement_policy, 1128 NULL 1129 }; 1130 KTYPE(bch_cache); 1131