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 #ifndef NO_BCACHEFS_SYSFS 10 11 #include "bcachefs.h" 12 #include "alloc_background.h" 13 #include "alloc_foreground.h" 14 #include "sysfs.h" 15 #include "btree_cache.h" 16 #include "btree_io.h" 17 #include "btree_iter.h" 18 #include "btree_key_cache.h" 19 #include "btree_update.h" 20 #include "btree_update_interior.h" 21 #include "btree_gc.h" 22 #include "buckets.h" 23 #include "clock.h" 24 #include "compress.h" 25 #include "disk_accounting.h" 26 #include "disk_groups.h" 27 #include "ec.h" 28 #include "inode.h" 29 #include "journal.h" 30 #include "journal_reclaim.h" 31 #include "keylist.h" 32 #include "move.h" 33 #include "movinggc.h" 34 #include "nocow_locking.h" 35 #include "opts.h" 36 #include "rebalance.h" 37 #include "replicas.h" 38 #include "super-io.h" 39 #include "tests.h" 40 41 #include <linux/blkdev.h> 42 #include <linux/sort.h> 43 #include <linux/sched/clock.h> 44 45 #include "util.h" 46 47 #define SYSFS_OPS(type) \ 48 const struct sysfs_ops type ## _sysfs_ops = { \ 49 .show = type ## _show, \ 50 .store = type ## _store \ 51 } 52 53 #define SHOW(fn) \ 54 static ssize_t fn ## _to_text(struct printbuf *, \ 55 struct kobject *, struct attribute *); \ 56 \ 57 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\ 58 char *buf) \ 59 { \ 60 struct printbuf out = PRINTBUF; \ 61 ssize_t ret = fn ## _to_text(&out, kobj, attr); \ 62 \ 63 if (out.pos && out.buf[out.pos - 1] != '\n') \ 64 prt_newline(&out); \ 65 \ 66 if (!ret && out.allocation_failure) \ 67 ret = -ENOMEM; \ 68 \ 69 if (!ret) { \ 70 ret = min_t(size_t, out.pos, PAGE_SIZE - 1); \ 71 memcpy(buf, out.buf, ret); \ 72 } \ 73 printbuf_exit(&out); \ 74 return bch2_err_class(ret); \ 75 } \ 76 \ 77 static ssize_t fn ## _to_text(struct printbuf *out, struct kobject *kobj,\ 78 struct attribute *attr) 79 80 #define STORE(fn) \ 81 static ssize_t fn ## _store_inner(struct kobject *, struct attribute *,\ 82 const char *, size_t); \ 83 \ 84 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\ 85 const char *buf, size_t size) \ 86 { \ 87 return bch2_err_class(fn##_store_inner(kobj, attr, buf, size)); \ 88 } \ 89 \ 90 static ssize_t fn ## _store_inner(struct kobject *kobj, struct attribute *attr,\ 91 const char *buf, size_t size) 92 93 #define __sysfs_attribute(_name, _mode) \ 94 static struct attribute sysfs_##_name = \ 95 { .name = #_name, .mode = _mode } 96 97 #define write_attribute(n) __sysfs_attribute(n, 0200) 98 #define read_attribute(n) __sysfs_attribute(n, 0444) 99 #define rw_attribute(n) __sysfs_attribute(n, 0644) 100 101 #define sysfs_printf(file, fmt, ...) \ 102 do { \ 103 if (attr == &sysfs_ ## file) \ 104 prt_printf(out, fmt "\n", __VA_ARGS__); \ 105 } while (0) 106 107 #define sysfs_print(file, var) \ 108 do { \ 109 if (attr == &sysfs_ ## file) \ 110 snprint(out, var); \ 111 } while (0) 112 113 #define sysfs_hprint(file, val) \ 114 do { \ 115 if (attr == &sysfs_ ## file) \ 116 prt_human_readable_s64(out, val); \ 117 } while (0) 118 119 #define sysfs_strtoul(file, var) \ 120 do { \ 121 if (attr == &sysfs_ ## file) \ 122 return strtoul_safe(buf, var) ?: (ssize_t) size; \ 123 } while (0) 124 125 #define sysfs_strtoul_clamp(file, var, min, max) \ 126 do { \ 127 if (attr == &sysfs_ ## file) \ 128 return strtoul_safe_clamp(buf, var, min, max) \ 129 ?: (ssize_t) size; \ 130 } while (0) 131 132 #define strtoul_or_return(cp) \ 133 ({ \ 134 unsigned long _v; \ 135 int _r = kstrtoul(cp, 10, &_v); \ 136 if (_r) \ 137 return _r; \ 138 _v; \ 139 }) 140 141 write_attribute(trigger_gc); 142 write_attribute(trigger_discards); 143 write_attribute(trigger_invalidates); 144 write_attribute(trigger_journal_flush); 145 write_attribute(trigger_journal_writes); 146 write_attribute(trigger_btree_cache_shrink); 147 write_attribute(trigger_btree_key_cache_shrink); 148 write_attribute(trigger_freelist_wakeup); 149 rw_attribute(gc_gens_pos); 150 151 read_attribute(uuid); 152 read_attribute(minor); 153 read_attribute(flags); 154 read_attribute(bucket_size); 155 read_attribute(first_bucket); 156 read_attribute(nbuckets); 157 rw_attribute(durability); 158 read_attribute(io_done); 159 read_attribute(io_errors); 160 write_attribute(io_errors_reset); 161 162 read_attribute(io_latency_read); 163 read_attribute(io_latency_write); 164 read_attribute(io_latency_stats_read); 165 read_attribute(io_latency_stats_write); 166 read_attribute(congested); 167 168 read_attribute(btree_write_stats); 169 170 read_attribute(btree_cache_size); 171 read_attribute(compression_stats); 172 read_attribute(journal_debug); 173 read_attribute(btree_cache); 174 read_attribute(btree_key_cache); 175 read_attribute(btree_reserve_cache); 176 read_attribute(stripes_heap); 177 read_attribute(open_buckets); 178 read_attribute(open_buckets_partial); 179 read_attribute(write_points); 180 read_attribute(nocow_lock_table); 181 182 #ifdef BCH_WRITE_REF_DEBUG 183 read_attribute(write_refs); 184 185 static const char * const bch2_write_refs[] = { 186 #define x(n) #n, 187 BCH_WRITE_REFS() 188 #undef x 189 NULL 190 }; 191 192 static void bch2_write_refs_to_text(struct printbuf *out, struct bch_fs *c) 193 { 194 bch2_printbuf_tabstop_push(out, 24); 195 196 for (unsigned i = 0; i < ARRAY_SIZE(c->writes); i++) 197 prt_printf(out, "%s\t%li\n", bch2_write_refs[i], atomic_long_read(&c->writes[i])); 198 } 199 #endif 200 201 read_attribute(internal_uuid); 202 read_attribute(disk_groups); 203 204 read_attribute(has_data); 205 read_attribute(alloc_debug); 206 read_attribute(accounting); 207 read_attribute(usage_base); 208 209 #define x(t, n, ...) read_attribute(t); 210 BCH_PERSISTENT_COUNTERS() 211 #undef x 212 213 rw_attribute(discard); 214 rw_attribute(label); 215 216 rw_attribute(copy_gc_enabled); 217 read_attribute(copy_gc_wait); 218 219 rw_attribute(rebalance_enabled); 220 sysfs_pd_controller_attribute(rebalance); 221 read_attribute(rebalance_status); 222 rw_attribute(promote_whole_extents); 223 224 read_attribute(new_stripes); 225 226 read_attribute(io_timers_read); 227 read_attribute(io_timers_write); 228 229 read_attribute(moving_ctxts); 230 231 #ifdef CONFIG_BCACHEFS_TESTS 232 write_attribute(perf_test); 233 #endif /* CONFIG_BCACHEFS_TESTS */ 234 235 #define x(_name) \ 236 static struct attribute sysfs_time_stat_##_name = \ 237 { .name = #_name, .mode = 0444 }; 238 BCH_TIME_STATS() 239 #undef x 240 241 static struct attribute sysfs_state_rw = { 242 .name = "state", 243 .mode = 0444, 244 }; 245 246 static size_t bch2_btree_cache_size(struct bch_fs *c) 247 { 248 size_t ret = 0; 249 struct btree *b; 250 251 mutex_lock(&c->btree_cache.lock); 252 list_for_each_entry(b, &c->btree_cache.live, list) 253 ret += btree_buf_bytes(b); 254 255 mutex_unlock(&c->btree_cache.lock); 256 return ret; 257 } 258 259 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c) 260 { 261 prt_str(out, "type"); 262 printbuf_tabstop_push(out, 12); 263 printbuf_tabstop_push(out, 16); 264 printbuf_tabstop_push(out, 16); 265 printbuf_tabstop_push(out, 24); 266 prt_printf(out, "type\tcompressed\runcompressed\raverage extent size\r\n"); 267 268 for (unsigned i = 1; i < BCH_COMPRESSION_TYPE_NR; i++) { 269 struct disk_accounting_pos a = { 270 .type = BCH_DISK_ACCOUNTING_compression, 271 .compression.type = i, 272 }; 273 struct bpos p = disk_accounting_pos_to_bpos(&a); 274 u64 v[3]; 275 bch2_accounting_mem_read(c, p, v, ARRAY_SIZE(v)); 276 277 u64 nr_extents = v[0]; 278 u64 sectors_uncompressed = v[1]; 279 u64 sectors_compressed = v[2]; 280 281 bch2_prt_compression_type(out, i); 282 prt_tab(out); 283 284 prt_human_readable_u64(out, sectors_compressed << 9); 285 prt_tab_rjust(out); 286 287 prt_human_readable_u64(out, sectors_uncompressed << 9); 288 prt_tab_rjust(out); 289 290 prt_human_readable_u64(out, nr_extents 291 ? div_u64(sectors_uncompressed << 9, nr_extents) 292 : 0); 293 prt_tab_rjust(out); 294 prt_newline(out); 295 } 296 297 return 0; 298 } 299 300 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c) 301 { 302 prt_printf(out, "%s: ", bch2_btree_id_str(c->gc_gens_btree)); 303 bch2_bpos_to_text(out, c->gc_gens_pos); 304 prt_printf(out, "\n"); 305 } 306 307 static void bch2_fs_usage_base_to_text(struct printbuf *out, struct bch_fs *c) 308 { 309 struct bch_fs_usage_base b = {}; 310 311 acc_u64s_percpu(&b.hidden, &c->usage->hidden, sizeof(b) / sizeof(u64)); 312 313 prt_printf(out, "hidden:\t\t%llu\n", b.hidden); 314 prt_printf(out, "btree:\t\t%llu\n", b.btree); 315 prt_printf(out, "data:\t\t%llu\n", b.data); 316 prt_printf(out, "cached:\t%llu\n", b.cached); 317 prt_printf(out, "reserved:\t\t%llu\n", b.reserved); 318 prt_printf(out, "nr_inodes:\t%llu\n", b.nr_inodes); 319 } 320 321 SHOW(bch2_fs) 322 { 323 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 324 325 sysfs_print(minor, c->minor); 326 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b); 327 328 if (attr == &sysfs_flags) 329 prt_bitflags(out, bch2_fs_flag_strs, c->flags); 330 331 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c)); 332 333 if (attr == &sysfs_btree_write_stats) 334 bch2_btree_write_stats_to_text(out, c); 335 336 if (attr == &sysfs_gc_gens_pos) 337 bch2_gc_gens_pos_to_text(out, c); 338 339 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled); 340 341 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled); 342 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */ 343 344 if (attr == &sysfs_copy_gc_wait) 345 bch2_copygc_wait_to_text(out, c); 346 347 if (attr == &sysfs_rebalance_status) 348 bch2_rebalance_status_to_text(out, c); 349 350 sysfs_print(promote_whole_extents, c->promote_whole_extents); 351 352 /* Debugging: */ 353 354 if (attr == &sysfs_journal_debug) 355 bch2_journal_debug_to_text(out, &c->journal); 356 357 if (attr == &sysfs_btree_cache) 358 bch2_btree_cache_to_text(out, &c->btree_cache); 359 360 if (attr == &sysfs_btree_key_cache) 361 bch2_btree_key_cache_to_text(out, &c->btree_key_cache); 362 363 if (attr == &sysfs_btree_reserve_cache) 364 bch2_btree_reserve_cache_to_text(out, c); 365 366 if (attr == &sysfs_stripes_heap) 367 bch2_stripes_heap_to_text(out, c); 368 369 if (attr == &sysfs_open_buckets) 370 bch2_open_buckets_to_text(out, c, NULL); 371 372 if (attr == &sysfs_open_buckets_partial) 373 bch2_open_buckets_partial_to_text(out, c); 374 375 if (attr == &sysfs_write_points) 376 bch2_write_points_to_text(out, c); 377 378 if (attr == &sysfs_compression_stats) 379 bch2_compression_stats_to_text(out, c); 380 381 if (attr == &sysfs_new_stripes) 382 bch2_new_stripes_to_text(out, c); 383 384 if (attr == &sysfs_io_timers_read) 385 bch2_io_timers_to_text(out, &c->io_clock[READ]); 386 387 if (attr == &sysfs_io_timers_write) 388 bch2_io_timers_to_text(out, &c->io_clock[WRITE]); 389 390 if (attr == &sysfs_moving_ctxts) 391 bch2_fs_moving_ctxts_to_text(out, c); 392 393 #ifdef BCH_WRITE_REF_DEBUG 394 if (attr == &sysfs_write_refs) 395 bch2_write_refs_to_text(out, c); 396 #endif 397 398 if (attr == &sysfs_nocow_lock_table) 399 bch2_nocow_locks_to_text(out, &c->nocow_locks); 400 401 if (attr == &sysfs_disk_groups) 402 bch2_disk_groups_to_text(out, c); 403 404 if (attr == &sysfs_alloc_debug) 405 bch2_fs_alloc_debug_to_text(out, c); 406 407 if (attr == &sysfs_accounting) 408 bch2_fs_accounting_to_text(out, c); 409 410 if (attr == &sysfs_usage_base) 411 bch2_fs_usage_base_to_text(out, c); 412 413 return 0; 414 } 415 416 STORE(bch2_fs) 417 { 418 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 419 420 if (attr == &sysfs_copy_gc_enabled) { 421 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled) 422 ?: (ssize_t) size; 423 424 if (c->copygc_thread) 425 wake_up_process(c->copygc_thread); 426 return ret; 427 } 428 429 if (attr == &sysfs_rebalance_enabled) { 430 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled) 431 ?: (ssize_t) size; 432 433 rebalance_wakeup(c); 434 return ret; 435 } 436 437 sysfs_pd_controller_store(rebalance, &c->rebalance.pd); 438 439 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents); 440 441 /* Debugging: */ 442 443 if (!test_bit(BCH_FS_started, &c->flags)) 444 return -EPERM; 445 446 /* Debugging: */ 447 448 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs)) 449 return -EROFS; 450 451 if (attr == &sysfs_trigger_btree_cache_shrink) { 452 struct shrink_control sc; 453 454 sc.gfp_mask = GFP_KERNEL; 455 sc.nr_to_scan = strtoul_or_return(buf); 456 c->btree_cache.shrink->scan_objects(c->btree_cache.shrink, &sc); 457 } 458 459 if (attr == &sysfs_trigger_btree_key_cache_shrink) { 460 struct shrink_control sc; 461 462 sc.gfp_mask = GFP_KERNEL; 463 sc.nr_to_scan = strtoul_or_return(buf); 464 c->btree_key_cache.shrink->scan_objects(c->btree_cache.shrink, &sc); 465 } 466 467 if (attr == &sysfs_trigger_gc) 468 bch2_gc_gens(c); 469 470 if (attr == &sysfs_trigger_discards) 471 bch2_do_discards(c); 472 473 if (attr == &sysfs_trigger_invalidates) 474 bch2_do_invalidates(c); 475 476 if (attr == &sysfs_trigger_journal_flush) { 477 bch2_journal_flush_all_pins(&c->journal); 478 bch2_journal_meta(&c->journal); 479 } 480 481 if (attr == &sysfs_trigger_journal_writes) 482 bch2_journal_do_writes(&c->journal); 483 484 if (attr == &sysfs_trigger_freelist_wakeup) 485 closure_wake_up(&c->freelist_wait); 486 487 #ifdef CONFIG_BCACHEFS_TESTS 488 if (attr == &sysfs_perf_test) { 489 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp; 490 char *test = strsep(&p, " \t\n"); 491 char *nr_str = strsep(&p, " \t\n"); 492 char *threads_str = strsep(&p, " \t\n"); 493 unsigned threads; 494 u64 nr; 495 int ret = -EINVAL; 496 497 if (threads_str && 498 !(ret = kstrtouint(threads_str, 10, &threads)) && 499 !(ret = bch2_strtoull_h(nr_str, &nr))) 500 ret = bch2_btree_perf_test(c, test, nr, threads); 501 kfree(tmp); 502 503 if (ret) 504 size = ret; 505 } 506 #endif 507 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs); 508 return size; 509 } 510 SYSFS_OPS(bch2_fs); 511 512 struct attribute *bch2_fs_files[] = { 513 &sysfs_minor, 514 &sysfs_btree_cache_size, 515 &sysfs_btree_write_stats, 516 517 &sysfs_promote_whole_extents, 518 519 &sysfs_compression_stats, 520 521 #ifdef CONFIG_BCACHEFS_TESTS 522 &sysfs_perf_test, 523 #endif 524 NULL 525 }; 526 527 /* counters dir */ 528 529 SHOW(bch2_fs_counters) 530 { 531 struct bch_fs *c = container_of(kobj, struct bch_fs, counters_kobj); 532 u64 counter = 0; 533 u64 counter_since_mount = 0; 534 535 printbuf_tabstop_push(out, 32); 536 537 #define x(t, ...) \ 538 if (attr == &sysfs_##t) { \ 539 counter = percpu_u64_get(&c->counters[BCH_COUNTER_##t]);\ 540 counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\ 541 prt_printf(out, "since mount:\t"); \ 542 prt_human_readable_u64(out, counter_since_mount); \ 543 prt_newline(out); \ 544 \ 545 prt_printf(out, "since filesystem creation:\t"); \ 546 prt_human_readable_u64(out, counter); \ 547 prt_newline(out); \ 548 } 549 BCH_PERSISTENT_COUNTERS() 550 #undef x 551 return 0; 552 } 553 554 STORE(bch2_fs_counters) { 555 return 0; 556 } 557 558 SYSFS_OPS(bch2_fs_counters); 559 560 struct attribute *bch2_fs_counters_files[] = { 561 #define x(t, ...) \ 562 &sysfs_##t, 563 BCH_PERSISTENT_COUNTERS() 564 #undef x 565 NULL 566 }; 567 /* internal dir - just a wrapper */ 568 569 SHOW(bch2_fs_internal) 570 { 571 struct bch_fs *c = container_of(kobj, struct bch_fs, internal); 572 573 return bch2_fs_to_text(out, &c->kobj, attr); 574 } 575 576 STORE(bch2_fs_internal) 577 { 578 struct bch_fs *c = container_of(kobj, struct bch_fs, internal); 579 580 return bch2_fs_store(&c->kobj, attr, buf, size); 581 } 582 SYSFS_OPS(bch2_fs_internal); 583 584 struct attribute *bch2_fs_internal_files[] = { 585 &sysfs_flags, 586 &sysfs_journal_debug, 587 &sysfs_btree_cache, 588 &sysfs_btree_key_cache, 589 &sysfs_btree_reserve_cache, 590 &sysfs_new_stripes, 591 &sysfs_stripes_heap, 592 &sysfs_open_buckets, 593 &sysfs_open_buckets_partial, 594 &sysfs_write_points, 595 #ifdef BCH_WRITE_REF_DEBUG 596 &sysfs_write_refs, 597 #endif 598 &sysfs_nocow_lock_table, 599 &sysfs_io_timers_read, 600 &sysfs_io_timers_write, 601 602 &sysfs_trigger_gc, 603 &sysfs_trigger_discards, 604 &sysfs_trigger_invalidates, 605 &sysfs_trigger_journal_flush, 606 &sysfs_trigger_journal_writes, 607 &sysfs_trigger_btree_cache_shrink, 608 &sysfs_trigger_btree_key_cache_shrink, 609 &sysfs_trigger_freelist_wakeup, 610 611 &sysfs_gc_gens_pos, 612 613 &sysfs_copy_gc_enabled, 614 &sysfs_copy_gc_wait, 615 616 &sysfs_rebalance_enabled, 617 &sysfs_rebalance_status, 618 sysfs_pd_controller_files(rebalance), 619 620 &sysfs_moving_ctxts, 621 622 &sysfs_internal_uuid, 623 624 &sysfs_disk_groups, 625 &sysfs_alloc_debug, 626 &sysfs_accounting, 627 &sysfs_usage_base, 628 NULL 629 }; 630 631 /* options */ 632 633 SHOW(bch2_fs_opts_dir) 634 { 635 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir); 636 const struct bch_option *opt = container_of(attr, struct bch_option, attr); 637 int id = opt - bch2_opt_table; 638 u64 v = bch2_opt_get_by_id(&c->opts, id); 639 640 bch2_opt_to_text(out, c, c->disk_sb.sb, opt, v, OPT_SHOW_FULL_LIST); 641 prt_char(out, '\n'); 642 643 return 0; 644 } 645 646 STORE(bch2_fs_opts_dir) 647 { 648 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir); 649 const struct bch_option *opt = container_of(attr, struct bch_option, attr); 650 int ret, id = opt - bch2_opt_table; 651 char *tmp; 652 u64 v; 653 654 /* 655 * We don't need to take c->writes for correctness, but it eliminates an 656 * unsightly error message in the dmesg log when we're RO: 657 */ 658 if (unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs))) 659 return -EROFS; 660 661 tmp = kstrdup(buf, GFP_KERNEL); 662 if (!tmp) { 663 ret = -ENOMEM; 664 goto err; 665 } 666 667 ret = bch2_opt_parse(c, opt, strim(tmp), &v, NULL); 668 kfree(tmp); 669 670 if (ret < 0) 671 goto err; 672 673 ret = bch2_opt_check_may_set(c, id, v); 674 if (ret < 0) 675 goto err; 676 677 bch2_opt_set_sb(c, opt, v); 678 bch2_opt_set_by_id(&c->opts, id, v); 679 680 if (v && 681 (id == Opt_background_target || 682 id == Opt_background_compression || 683 (id == Opt_compression && !c->opts.background_compression))) 684 bch2_set_rebalance_needs_scan(c, 0); 685 686 ret = size; 687 err: 688 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs); 689 return ret; 690 } 691 SYSFS_OPS(bch2_fs_opts_dir); 692 693 struct attribute *bch2_fs_opts_dir_files[] = { NULL }; 694 695 int bch2_opts_create_sysfs_files(struct kobject *kobj) 696 { 697 const struct bch_option *i; 698 int ret; 699 700 for (i = bch2_opt_table; 701 i < bch2_opt_table + bch2_opts_nr; 702 i++) { 703 if (!(i->flags & OPT_FS)) 704 continue; 705 706 ret = sysfs_create_file(kobj, &i->attr); 707 if (ret) 708 return ret; 709 } 710 711 return 0; 712 } 713 714 /* time stats */ 715 716 SHOW(bch2_fs_time_stats) 717 { 718 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats); 719 720 #define x(name) \ 721 if (attr == &sysfs_time_stat_##name) \ 722 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]); 723 BCH_TIME_STATS() 724 #undef x 725 726 return 0; 727 } 728 729 STORE(bch2_fs_time_stats) 730 { 731 return size; 732 } 733 SYSFS_OPS(bch2_fs_time_stats); 734 735 struct attribute *bch2_fs_time_stats_files[] = { 736 #define x(name) \ 737 &sysfs_time_stat_##name, 738 BCH_TIME_STATS() 739 #undef x 740 NULL 741 }; 742 743 static const char * const bch2_rw[] = { 744 "read", 745 "write", 746 NULL 747 }; 748 749 static void dev_io_done_to_text(struct printbuf *out, struct bch_dev *ca) 750 { 751 int rw, i; 752 753 for (rw = 0; rw < 2; rw++) { 754 prt_printf(out, "%s:\n", bch2_rw[rw]); 755 756 for (i = 1; i < BCH_DATA_NR; i++) 757 prt_printf(out, "%-12s:%12llu\n", 758 bch2_data_type_str(i), 759 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9); 760 } 761 } 762 763 SHOW(bch2_dev) 764 { 765 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 766 struct bch_fs *c = ca->fs; 767 768 sysfs_printf(uuid, "%pU\n", ca->uuid.b); 769 770 sysfs_print(bucket_size, bucket_bytes(ca)); 771 sysfs_print(first_bucket, ca->mi.first_bucket); 772 sysfs_print(nbuckets, ca->mi.nbuckets); 773 sysfs_print(durability, ca->mi.durability); 774 sysfs_print(discard, ca->mi.discard); 775 776 if (attr == &sysfs_label) { 777 if (ca->mi.group) 778 bch2_disk_path_to_text(out, c, ca->mi.group - 1); 779 prt_char(out, '\n'); 780 } 781 782 if (attr == &sysfs_has_data) { 783 prt_bitflags(out, __bch2_data_types, bch2_dev_has_data(c, ca)); 784 prt_char(out, '\n'); 785 } 786 787 if (attr == &sysfs_state_rw) { 788 prt_string_option(out, bch2_member_states, ca->mi.state); 789 prt_char(out, '\n'); 790 } 791 792 if (attr == &sysfs_io_done) 793 dev_io_done_to_text(out, ca); 794 795 if (attr == &sysfs_io_errors) 796 bch2_dev_io_errors_to_text(out, ca); 797 798 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ])); 799 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE])); 800 801 if (attr == &sysfs_io_latency_stats_read) 802 bch2_time_stats_to_text(out, &ca->io_latency[READ].stats); 803 804 if (attr == &sysfs_io_latency_stats_write) 805 bch2_time_stats_to_text(out, &ca->io_latency[WRITE].stats); 806 807 sysfs_printf(congested, "%u%%", 808 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX) 809 * 100 / CONGESTED_MAX); 810 811 if (attr == &sysfs_alloc_debug) 812 bch2_dev_alloc_debug_to_text(out, ca); 813 814 if (attr == &sysfs_open_buckets) 815 bch2_open_buckets_to_text(out, c, ca); 816 817 return 0; 818 } 819 820 STORE(bch2_dev) 821 { 822 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 823 struct bch_fs *c = ca->fs; 824 struct bch_member *mi; 825 826 if (attr == &sysfs_discard) { 827 bool v = strtoul_or_return(buf); 828 829 mutex_lock(&c->sb_lock); 830 mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 831 832 if (v != BCH_MEMBER_DISCARD(mi)) { 833 SET_BCH_MEMBER_DISCARD(mi, v); 834 bch2_write_super(c); 835 } 836 mutex_unlock(&c->sb_lock); 837 } 838 839 if (attr == &sysfs_durability) { 840 u64 v = strtoul_or_return(buf); 841 842 mutex_lock(&c->sb_lock); 843 mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 844 845 if (v + 1 != BCH_MEMBER_DURABILITY(mi)) { 846 SET_BCH_MEMBER_DURABILITY(mi, v + 1); 847 bch2_write_super(c); 848 } 849 mutex_unlock(&c->sb_lock); 850 } 851 852 if (attr == &sysfs_label) { 853 char *tmp; 854 int ret; 855 856 tmp = kstrdup(buf, GFP_KERNEL); 857 if (!tmp) 858 return -ENOMEM; 859 860 ret = bch2_dev_group_set(c, ca, strim(tmp)); 861 kfree(tmp); 862 if (ret) 863 return ret; 864 } 865 866 if (attr == &sysfs_io_errors_reset) 867 bch2_dev_errors_reset(ca); 868 869 return size; 870 } 871 SYSFS_OPS(bch2_dev); 872 873 struct attribute *bch2_dev_files[] = { 874 &sysfs_uuid, 875 &sysfs_bucket_size, 876 &sysfs_first_bucket, 877 &sysfs_nbuckets, 878 &sysfs_durability, 879 880 /* settings: */ 881 &sysfs_discard, 882 &sysfs_state_rw, 883 &sysfs_label, 884 885 &sysfs_has_data, 886 &sysfs_io_done, 887 &sysfs_io_errors, 888 &sysfs_io_errors_reset, 889 890 &sysfs_io_latency_read, 891 &sysfs_io_latency_write, 892 &sysfs_io_latency_stats_read, 893 &sysfs_io_latency_stats_write, 894 &sysfs_congested, 895 896 /* debug: */ 897 &sysfs_alloc_debug, 898 &sysfs_open_buckets, 899 NULL 900 }; 901 902 #endif /* _BCACHEFS_SYSFS_H_ */ 903