Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * bcache sysfs interfaces |
| 3 | * |
| 4 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> |
| 5 | * Copyright 2012 Google, Inc. |
| 6 | */ |
| 7 | |
| 8 | #include "bcache.h" |
| 9 | #include "sysfs.h" |
| 10 | #include "btree.h" |
| 11 | #include "request.h" |
| 12 | |
| 13 | #include <linux/sort.h> |
| 14 | |
| 15 | static const char * const cache_replacement_policies[] = { |
| 16 | "lru", |
| 17 | "fifo", |
| 18 | "random", |
| 19 | NULL |
| 20 | }; |
| 21 | |
| 22 | write_attribute(attach); |
| 23 | write_attribute(detach); |
| 24 | write_attribute(unregister); |
| 25 | write_attribute(stop); |
| 26 | write_attribute(clear_stats); |
| 27 | write_attribute(trigger_gc); |
| 28 | write_attribute(prune_cache); |
| 29 | write_attribute(flash_vol_create); |
| 30 | |
| 31 | read_attribute(bucket_size); |
| 32 | read_attribute(block_size); |
| 33 | read_attribute(nbuckets); |
| 34 | read_attribute(tree_depth); |
| 35 | read_attribute(root_usage_percent); |
| 36 | read_attribute(priority_stats); |
| 37 | read_attribute(btree_cache_size); |
| 38 | read_attribute(btree_cache_max_chain); |
| 39 | read_attribute(cache_available_percent); |
| 40 | read_attribute(written); |
| 41 | read_attribute(btree_written); |
| 42 | read_attribute(metadata_written); |
| 43 | read_attribute(active_journal_entries); |
| 44 | |
| 45 | sysfs_time_stats_attribute(btree_gc, sec, ms); |
| 46 | sysfs_time_stats_attribute(btree_split, sec, us); |
| 47 | sysfs_time_stats_attribute(btree_sort, ms, us); |
| 48 | sysfs_time_stats_attribute(btree_read, ms, us); |
| 49 | sysfs_time_stats_attribute(try_harder, ms, us); |
| 50 | |
| 51 | read_attribute(btree_nodes); |
| 52 | read_attribute(btree_used_percent); |
| 53 | read_attribute(average_key_size); |
| 54 | read_attribute(dirty_data); |
| 55 | read_attribute(bset_tree_stats); |
| 56 | |
| 57 | read_attribute(state); |
| 58 | read_attribute(cache_read_races); |
| 59 | read_attribute(writeback_keys_done); |
| 60 | read_attribute(writeback_keys_failed); |
| 61 | read_attribute(io_errors); |
| 62 | read_attribute(congested); |
| 63 | rw_attribute(congested_read_threshold_us); |
| 64 | rw_attribute(congested_write_threshold_us); |
| 65 | |
| 66 | rw_attribute(sequential_cutoff); |
| 67 | rw_attribute(sequential_merge); |
| 68 | rw_attribute(data_csum); |
| 69 | rw_attribute(cache_mode); |
| 70 | rw_attribute(writeback_metadata); |
| 71 | rw_attribute(writeback_running); |
| 72 | rw_attribute(writeback_percent); |
| 73 | rw_attribute(writeback_delay); |
| 74 | rw_attribute(writeback_rate); |
| 75 | |
| 76 | rw_attribute(writeback_rate_update_seconds); |
| 77 | rw_attribute(writeback_rate_d_term); |
| 78 | rw_attribute(writeback_rate_p_term_inverse); |
| 79 | rw_attribute(writeback_rate_d_smooth); |
| 80 | read_attribute(writeback_rate_debug); |
| 81 | |
| 82 | rw_attribute(synchronous); |
| 83 | rw_attribute(journal_delay_ms); |
| 84 | rw_attribute(discard); |
| 85 | rw_attribute(running); |
| 86 | rw_attribute(label); |
| 87 | rw_attribute(readahead); |
| 88 | rw_attribute(io_error_limit); |
| 89 | rw_attribute(io_error_halflife); |
| 90 | rw_attribute(verify); |
| 91 | rw_attribute(key_merging_disabled); |
| 92 | rw_attribute(gc_always_rewrite); |
| 93 | rw_attribute(freelist_percent); |
| 94 | rw_attribute(cache_replacement_policy); |
| 95 | rw_attribute(btree_shrinker_disabled); |
| 96 | rw_attribute(copy_gc_enabled); |
| 97 | rw_attribute(size); |
| 98 | |
| 99 | SHOW(__bch_cached_dev) |
| 100 | { |
| 101 | struct cached_dev *dc = container_of(kobj, struct cached_dev, |
| 102 | disk.kobj); |
| 103 | const char *states[] = { "no cache", "clean", "dirty", "inconsistent" }; |
| 104 | |
| 105 | #define var(stat) (dc->stat) |
| 106 | |
| 107 | if (attr == &sysfs_cache_mode) |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 108 | return bch_snprint_string_list(buf, PAGE_SIZE, |
| 109 | bch_cache_modes + 1, |
| 110 | BDEV_CACHE_MODE(&dc->sb)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 111 | |
| 112 | sysfs_printf(data_csum, "%i", dc->disk.data_csum); |
| 113 | var_printf(verify, "%i"); |
| 114 | var_printf(writeback_metadata, "%i"); |
| 115 | var_printf(writeback_running, "%i"); |
| 116 | var_print(writeback_delay); |
| 117 | var_print(writeback_percent); |
| 118 | sysfs_print(writeback_rate, dc->writeback_rate.rate); |
| 119 | |
| 120 | var_print(writeback_rate_update_seconds); |
| 121 | var_print(writeback_rate_d_term); |
| 122 | var_print(writeback_rate_p_term_inverse); |
| 123 | var_print(writeback_rate_d_smooth); |
| 124 | |
| 125 | if (attr == &sysfs_writeback_rate_debug) { |
| 126 | char dirty[20]; |
| 127 | char derivative[20]; |
| 128 | char target[20]; |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 129 | bch_hprint(dirty, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 130 | atomic_long_read(&dc->disk.sectors_dirty) << 9); |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 131 | bch_hprint(derivative, dc->writeback_rate_derivative << 9); |
| 132 | bch_hprint(target, dc->writeback_rate_target << 9); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 133 | |
| 134 | return sprintf(buf, |
| 135 | "rate:\t\t%u\n" |
| 136 | "change:\t\t%i\n" |
| 137 | "dirty:\t\t%s\n" |
| 138 | "derivative:\t%s\n" |
| 139 | "target:\t\t%s\n", |
| 140 | dc->writeback_rate.rate, |
| 141 | dc->writeback_rate_change, |
| 142 | dirty, derivative, target); |
| 143 | } |
| 144 | |
| 145 | sysfs_hprint(dirty_data, |
| 146 | atomic_long_read(&dc->disk.sectors_dirty) << 9); |
| 147 | |
| 148 | var_printf(sequential_merge, "%i"); |
| 149 | var_hprint(sequential_cutoff); |
| 150 | var_hprint(readahead); |
| 151 | |
| 152 | sysfs_print(running, atomic_read(&dc->running)); |
| 153 | sysfs_print(state, states[BDEV_STATE(&dc->sb)]); |
| 154 | |
| 155 | if (attr == &sysfs_label) { |
| 156 | memcpy(buf, dc->sb.label, SB_LABEL_SIZE); |
| 157 | buf[SB_LABEL_SIZE + 1] = '\0'; |
| 158 | strcat(buf, "\n"); |
| 159 | return strlen(buf); |
| 160 | } |
| 161 | |
| 162 | #undef var |
| 163 | return 0; |
| 164 | } |
| 165 | SHOW_LOCKED(bch_cached_dev) |
| 166 | |
| 167 | STORE(__cached_dev) |
| 168 | { |
| 169 | struct cached_dev *dc = container_of(kobj, struct cached_dev, |
| 170 | disk.kobj); |
| 171 | unsigned v = size; |
| 172 | struct cache_set *c; |
| 173 | |
| 174 | #define d_strtoul(var) sysfs_strtoul(var, dc->var) |
| 175 | #define d_strtoi_h(var) sysfs_hatoi(var, dc->var) |
| 176 | |
| 177 | sysfs_strtoul(data_csum, dc->disk.data_csum); |
| 178 | d_strtoul(verify); |
| 179 | d_strtoul(writeback_metadata); |
| 180 | d_strtoul(writeback_running); |
| 181 | d_strtoul(writeback_delay); |
| 182 | sysfs_strtoul_clamp(writeback_rate, |
| 183 | dc->writeback_rate.rate, 1, 1000000); |
| 184 | sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40); |
| 185 | |
| 186 | d_strtoul(writeback_rate_update_seconds); |
| 187 | d_strtoul(writeback_rate_d_term); |
| 188 | d_strtoul(writeback_rate_p_term_inverse); |
| 189 | sysfs_strtoul_clamp(writeback_rate_p_term_inverse, |
| 190 | dc->writeback_rate_p_term_inverse, 1, INT_MAX); |
| 191 | d_strtoul(writeback_rate_d_smooth); |
| 192 | |
| 193 | d_strtoul(sequential_merge); |
| 194 | d_strtoi_h(sequential_cutoff); |
| 195 | d_strtoi_h(readahead); |
| 196 | |
| 197 | if (attr == &sysfs_clear_stats) |
| 198 | bch_cache_accounting_clear(&dc->accounting); |
| 199 | |
| 200 | if (attr == &sysfs_running && |
| 201 | strtoul_or_return(buf)) |
| 202 | bch_cached_dev_run(dc); |
| 203 | |
| 204 | if (attr == &sysfs_cache_mode) { |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 205 | ssize_t v = bch_read_string_list(buf, bch_cache_modes + 1); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 206 | |
| 207 | if (v < 0) |
| 208 | return v; |
| 209 | |
| 210 | if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) { |
| 211 | SET_BDEV_CACHE_MODE(&dc->sb, v); |
| 212 | bch_write_bdev_super(dc, NULL); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (attr == &sysfs_label) { |
| 217 | memcpy(dc->sb.label, buf, SB_LABEL_SIZE); |
| 218 | bch_write_bdev_super(dc, NULL); |
| 219 | if (dc->disk.c) { |
| 220 | memcpy(dc->disk.c->uuids[dc->disk.id].label, |
| 221 | buf, SB_LABEL_SIZE); |
| 222 | bch_uuid_write(dc->disk.c); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (attr == &sysfs_attach) { |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 227 | if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 228 | return -EINVAL; |
| 229 | |
| 230 | list_for_each_entry(c, &bch_cache_sets, list) { |
| 231 | v = bch_cached_dev_attach(dc, c); |
| 232 | if (!v) |
| 233 | return size; |
| 234 | } |
| 235 | |
| 236 | pr_err("Can't attach %s: cache set not found", buf); |
| 237 | size = v; |
| 238 | } |
| 239 | |
| 240 | if (attr == &sysfs_detach && dc->disk.c) |
| 241 | bch_cached_dev_detach(dc); |
| 242 | |
| 243 | if (attr == &sysfs_stop) |
| 244 | bcache_device_stop(&dc->disk); |
| 245 | |
| 246 | return size; |
| 247 | } |
| 248 | |
| 249 | STORE(bch_cached_dev) |
| 250 | { |
| 251 | struct cached_dev *dc = container_of(kobj, struct cached_dev, |
| 252 | disk.kobj); |
| 253 | |
| 254 | mutex_lock(&bch_register_lock); |
| 255 | size = __cached_dev_store(kobj, attr, buf, size); |
| 256 | |
| 257 | if (attr == &sysfs_writeback_running) |
| 258 | bch_writeback_queue(dc); |
| 259 | |
| 260 | if (attr == &sysfs_writeback_percent) |
| 261 | schedule_delayed_work(&dc->writeback_rate_update, |
| 262 | dc->writeback_rate_update_seconds * HZ); |
| 263 | |
| 264 | mutex_unlock(&bch_register_lock); |
| 265 | return size; |
| 266 | } |
| 267 | |
| 268 | static struct attribute *bch_cached_dev_files[] = { |
| 269 | &sysfs_attach, |
| 270 | &sysfs_detach, |
| 271 | &sysfs_stop, |
| 272 | #if 0 |
| 273 | &sysfs_data_csum, |
| 274 | #endif |
| 275 | &sysfs_cache_mode, |
| 276 | &sysfs_writeback_metadata, |
| 277 | &sysfs_writeback_running, |
| 278 | &sysfs_writeback_delay, |
| 279 | &sysfs_writeback_percent, |
| 280 | &sysfs_writeback_rate, |
| 281 | &sysfs_writeback_rate_update_seconds, |
| 282 | &sysfs_writeback_rate_d_term, |
| 283 | &sysfs_writeback_rate_p_term_inverse, |
| 284 | &sysfs_writeback_rate_d_smooth, |
| 285 | &sysfs_writeback_rate_debug, |
| 286 | &sysfs_dirty_data, |
| 287 | &sysfs_sequential_cutoff, |
| 288 | &sysfs_sequential_merge, |
| 289 | &sysfs_clear_stats, |
| 290 | &sysfs_running, |
| 291 | &sysfs_state, |
| 292 | &sysfs_label, |
| 293 | &sysfs_readahead, |
| 294 | #ifdef CONFIG_BCACHE_DEBUG |
| 295 | &sysfs_verify, |
| 296 | #endif |
| 297 | NULL |
| 298 | }; |
| 299 | KTYPE(bch_cached_dev); |
| 300 | |
| 301 | SHOW(bch_flash_dev) |
| 302 | { |
| 303 | struct bcache_device *d = container_of(kobj, struct bcache_device, |
| 304 | kobj); |
| 305 | struct uuid_entry *u = &d->c->uuids[d->id]; |
| 306 | |
| 307 | sysfs_printf(data_csum, "%i", d->data_csum); |
| 308 | sysfs_hprint(size, u->sectors << 9); |
| 309 | |
| 310 | if (attr == &sysfs_label) { |
| 311 | memcpy(buf, u->label, SB_LABEL_SIZE); |
| 312 | buf[SB_LABEL_SIZE + 1] = '\0'; |
| 313 | strcat(buf, "\n"); |
| 314 | return strlen(buf); |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | STORE(__bch_flash_dev) |
| 321 | { |
| 322 | struct bcache_device *d = container_of(kobj, struct bcache_device, |
| 323 | kobj); |
| 324 | struct uuid_entry *u = &d->c->uuids[d->id]; |
| 325 | |
| 326 | sysfs_strtoul(data_csum, d->data_csum); |
| 327 | |
| 328 | if (attr == &sysfs_size) { |
| 329 | uint64_t v; |
| 330 | strtoi_h_or_return(buf, v); |
| 331 | |
| 332 | u->sectors = v >> 9; |
| 333 | bch_uuid_write(d->c); |
| 334 | set_capacity(d->disk, u->sectors); |
| 335 | } |
| 336 | |
| 337 | if (attr == &sysfs_label) { |
| 338 | memcpy(u->label, buf, SB_LABEL_SIZE); |
| 339 | bch_uuid_write(d->c); |
| 340 | } |
| 341 | |
| 342 | if (attr == &sysfs_unregister) { |
| 343 | atomic_set(&d->detaching, 1); |
| 344 | bcache_device_stop(d); |
| 345 | } |
| 346 | |
| 347 | return size; |
| 348 | } |
| 349 | STORE_LOCKED(bch_flash_dev) |
| 350 | |
| 351 | static struct attribute *bch_flash_dev_files[] = { |
| 352 | &sysfs_unregister, |
| 353 | #if 0 |
| 354 | &sysfs_data_csum, |
| 355 | #endif |
| 356 | &sysfs_label, |
| 357 | &sysfs_size, |
| 358 | NULL |
| 359 | }; |
| 360 | KTYPE(bch_flash_dev); |
| 361 | |
| 362 | SHOW(__bch_cache_set) |
| 363 | { |
| 364 | unsigned root_usage(struct cache_set *c) |
| 365 | { |
| 366 | unsigned bytes = 0; |
| 367 | struct bkey *k; |
| 368 | struct btree *b; |
| 369 | struct btree_iter iter; |
| 370 | |
| 371 | goto lock_root; |
| 372 | |
| 373 | do { |
| 374 | rw_unlock(false, b); |
| 375 | lock_root: |
| 376 | b = c->root; |
| 377 | rw_lock(false, b, b->level); |
| 378 | } while (b != c->root); |
| 379 | |
| 380 | for_each_key_filter(b, k, &iter, bch_ptr_bad) |
| 381 | bytes += bkey_bytes(k); |
| 382 | |
| 383 | rw_unlock(false, b); |
| 384 | |
| 385 | return (bytes * 100) / btree_bytes(c); |
| 386 | } |
| 387 | |
| 388 | size_t cache_size(struct cache_set *c) |
| 389 | { |
| 390 | size_t ret = 0; |
| 391 | struct btree *b; |
| 392 | |
| 393 | mutex_lock(&c->bucket_lock); |
| 394 | list_for_each_entry(b, &c->btree_cache, list) |
| 395 | ret += 1 << (b->page_order + PAGE_SHIFT); |
| 396 | |
| 397 | mutex_unlock(&c->bucket_lock); |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | unsigned cache_max_chain(struct cache_set *c) |
| 402 | { |
| 403 | unsigned ret = 0; |
| 404 | struct hlist_head *h; |
| 405 | |
| 406 | mutex_lock(&c->bucket_lock); |
| 407 | |
| 408 | for (h = c->bucket_hash; |
| 409 | h < c->bucket_hash + (1 << BUCKET_HASH_BITS); |
| 410 | h++) { |
| 411 | unsigned i = 0; |
| 412 | struct hlist_node *p; |
| 413 | |
| 414 | hlist_for_each(p, h) |
| 415 | i++; |
| 416 | |
| 417 | ret = max(ret, i); |
| 418 | } |
| 419 | |
| 420 | mutex_unlock(&c->bucket_lock); |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | unsigned btree_used(struct cache_set *c) |
| 425 | { |
| 426 | return div64_u64(c->gc_stats.key_bytes * 100, |
| 427 | (c->gc_stats.nodes ?: 1) * btree_bytes(c)); |
| 428 | } |
| 429 | |
| 430 | unsigned average_key_size(struct cache_set *c) |
| 431 | { |
| 432 | return c->gc_stats.nkeys |
| 433 | ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys) |
| 434 | : 0; |
| 435 | } |
| 436 | |
| 437 | struct cache_set *c = container_of(kobj, struct cache_set, kobj); |
| 438 | |
| 439 | sysfs_print(synchronous, CACHE_SYNC(&c->sb)); |
| 440 | sysfs_print(journal_delay_ms, c->journal_delay_ms); |
| 441 | sysfs_hprint(bucket_size, bucket_bytes(c)); |
| 442 | sysfs_hprint(block_size, block_bytes(c)); |
| 443 | sysfs_print(tree_depth, c->root->level); |
| 444 | sysfs_print(root_usage_percent, root_usage(c)); |
| 445 | |
| 446 | sysfs_hprint(btree_cache_size, cache_size(c)); |
| 447 | sysfs_print(btree_cache_max_chain, cache_max_chain(c)); |
| 448 | sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use); |
| 449 | |
| 450 | sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms); |
| 451 | sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us); |
| 452 | sysfs_print_time_stats(&c->sort_time, btree_sort, ms, us); |
| 453 | sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us); |
| 454 | sysfs_print_time_stats(&c->try_harder_time, try_harder, ms, us); |
| 455 | |
| 456 | sysfs_print(btree_used_percent, btree_used(c)); |
| 457 | sysfs_print(btree_nodes, c->gc_stats.nodes); |
| 458 | sysfs_hprint(dirty_data, c->gc_stats.dirty); |
| 459 | sysfs_hprint(average_key_size, average_key_size(c)); |
| 460 | |
| 461 | sysfs_print(cache_read_races, |
| 462 | atomic_long_read(&c->cache_read_races)); |
| 463 | |
| 464 | sysfs_print(writeback_keys_done, |
| 465 | atomic_long_read(&c->writeback_keys_done)); |
| 466 | sysfs_print(writeback_keys_failed, |
| 467 | atomic_long_read(&c->writeback_keys_failed)); |
| 468 | |
| 469 | /* See count_io_errors for why 88 */ |
| 470 | sysfs_print(io_error_halflife, c->error_decay * 88); |
| 471 | sysfs_print(io_error_limit, c->error_limit >> IO_ERROR_SHIFT); |
| 472 | |
| 473 | sysfs_hprint(congested, |
| 474 | ((uint64_t) bch_get_congested(c)) << 9); |
| 475 | sysfs_print(congested_read_threshold_us, |
| 476 | c->congested_read_threshold_us); |
| 477 | sysfs_print(congested_write_threshold_us, |
| 478 | c->congested_write_threshold_us); |
| 479 | |
| 480 | sysfs_print(active_journal_entries, fifo_used(&c->journal.pin)); |
| 481 | sysfs_printf(verify, "%i", c->verify); |
| 482 | sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled); |
| 483 | sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite); |
| 484 | sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled); |
| 485 | sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled); |
| 486 | |
| 487 | if (attr == &sysfs_bset_tree_stats) |
| 488 | return bch_bset_print_stats(c, buf); |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | SHOW_LOCKED(bch_cache_set) |
| 493 | |
| 494 | STORE(__bch_cache_set) |
| 495 | { |
| 496 | struct cache_set *c = container_of(kobj, struct cache_set, kobj); |
| 497 | |
| 498 | if (attr == &sysfs_unregister) |
| 499 | bch_cache_set_unregister(c); |
| 500 | |
| 501 | if (attr == &sysfs_stop) |
| 502 | bch_cache_set_stop(c); |
| 503 | |
| 504 | if (attr == &sysfs_synchronous) { |
| 505 | bool sync = strtoul_or_return(buf); |
| 506 | |
| 507 | if (sync != CACHE_SYNC(&c->sb)) { |
| 508 | SET_CACHE_SYNC(&c->sb, sync); |
| 509 | bcache_write_super(c); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (attr == &sysfs_flash_vol_create) { |
| 514 | int r; |
| 515 | uint64_t v; |
| 516 | strtoi_h_or_return(buf, v); |
| 517 | |
| 518 | r = bch_flash_dev_create(c, v); |
| 519 | if (r) |
| 520 | return r; |
| 521 | } |
| 522 | |
| 523 | if (attr == &sysfs_clear_stats) { |
| 524 | atomic_long_set(&c->writeback_keys_done, 0); |
| 525 | atomic_long_set(&c->writeback_keys_failed, 0); |
| 526 | |
| 527 | memset(&c->gc_stats, 0, sizeof(struct gc_stat)); |
| 528 | bch_cache_accounting_clear(&c->accounting); |
| 529 | } |
| 530 | |
| 531 | if (attr == &sysfs_trigger_gc) |
| 532 | bch_queue_gc(c); |
| 533 | |
| 534 | if (attr == &sysfs_prune_cache) { |
| 535 | struct shrink_control sc; |
| 536 | sc.gfp_mask = GFP_KERNEL; |
| 537 | sc.nr_to_scan = strtoul_or_return(buf); |
| 538 | c->shrink.shrink(&c->shrink, &sc); |
| 539 | } |
| 540 | |
| 541 | sysfs_strtoul(congested_read_threshold_us, |
| 542 | c->congested_read_threshold_us); |
| 543 | sysfs_strtoul(congested_write_threshold_us, |
| 544 | c->congested_write_threshold_us); |
| 545 | |
| 546 | if (attr == &sysfs_io_error_limit) |
| 547 | c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT; |
| 548 | |
| 549 | /* See count_io_errors() for why 88 */ |
| 550 | if (attr == &sysfs_io_error_halflife) |
| 551 | c->error_decay = strtoul_or_return(buf) / 88; |
| 552 | |
| 553 | sysfs_strtoul(journal_delay_ms, c->journal_delay_ms); |
| 554 | sysfs_strtoul(verify, c->verify); |
| 555 | sysfs_strtoul(key_merging_disabled, c->key_merging_disabled); |
| 556 | sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite); |
| 557 | sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled); |
| 558 | sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled); |
| 559 | |
| 560 | return size; |
| 561 | } |
| 562 | STORE_LOCKED(bch_cache_set) |
| 563 | |
| 564 | SHOW(bch_cache_set_internal) |
| 565 | { |
| 566 | struct cache_set *c = container_of(kobj, struct cache_set, internal); |
| 567 | return bch_cache_set_show(&c->kobj, attr, buf); |
| 568 | } |
| 569 | |
| 570 | STORE(bch_cache_set_internal) |
| 571 | { |
| 572 | struct cache_set *c = container_of(kobj, struct cache_set, internal); |
| 573 | return bch_cache_set_store(&c->kobj, attr, buf, size); |
| 574 | } |
| 575 | |
| 576 | static void bch_cache_set_internal_release(struct kobject *k) |
| 577 | { |
| 578 | } |
| 579 | |
| 580 | static struct attribute *bch_cache_set_files[] = { |
| 581 | &sysfs_unregister, |
| 582 | &sysfs_stop, |
| 583 | &sysfs_synchronous, |
| 584 | &sysfs_journal_delay_ms, |
| 585 | &sysfs_flash_vol_create, |
| 586 | |
| 587 | &sysfs_bucket_size, |
| 588 | &sysfs_block_size, |
| 589 | &sysfs_tree_depth, |
| 590 | &sysfs_root_usage_percent, |
| 591 | &sysfs_btree_cache_size, |
| 592 | &sysfs_cache_available_percent, |
| 593 | |
| 594 | &sysfs_average_key_size, |
| 595 | &sysfs_dirty_data, |
| 596 | |
| 597 | &sysfs_io_error_limit, |
| 598 | &sysfs_io_error_halflife, |
| 599 | &sysfs_congested, |
| 600 | &sysfs_congested_read_threshold_us, |
| 601 | &sysfs_congested_write_threshold_us, |
| 602 | &sysfs_clear_stats, |
| 603 | NULL |
| 604 | }; |
| 605 | KTYPE(bch_cache_set); |
| 606 | |
| 607 | static struct attribute *bch_cache_set_internal_files[] = { |
| 608 | &sysfs_active_journal_entries, |
| 609 | |
| 610 | sysfs_time_stats_attribute_list(btree_gc, sec, ms) |
| 611 | sysfs_time_stats_attribute_list(btree_split, sec, us) |
| 612 | sysfs_time_stats_attribute_list(btree_sort, ms, us) |
| 613 | sysfs_time_stats_attribute_list(btree_read, ms, us) |
| 614 | sysfs_time_stats_attribute_list(try_harder, ms, us) |
| 615 | |
| 616 | &sysfs_btree_nodes, |
| 617 | &sysfs_btree_used_percent, |
| 618 | &sysfs_btree_cache_max_chain, |
| 619 | |
| 620 | &sysfs_bset_tree_stats, |
| 621 | &sysfs_cache_read_races, |
| 622 | &sysfs_writeback_keys_done, |
| 623 | &sysfs_writeback_keys_failed, |
| 624 | |
| 625 | &sysfs_trigger_gc, |
| 626 | &sysfs_prune_cache, |
| 627 | #ifdef CONFIG_BCACHE_DEBUG |
| 628 | &sysfs_verify, |
| 629 | &sysfs_key_merging_disabled, |
| 630 | #endif |
| 631 | &sysfs_gc_always_rewrite, |
| 632 | &sysfs_btree_shrinker_disabled, |
| 633 | &sysfs_copy_gc_enabled, |
| 634 | NULL |
| 635 | }; |
| 636 | KTYPE(bch_cache_set_internal); |
| 637 | |
| 638 | SHOW(__bch_cache) |
| 639 | { |
| 640 | struct cache *ca = container_of(kobj, struct cache, kobj); |
| 641 | |
| 642 | sysfs_hprint(bucket_size, bucket_bytes(ca)); |
| 643 | sysfs_hprint(block_size, block_bytes(ca)); |
| 644 | sysfs_print(nbuckets, ca->sb.nbuckets); |
| 645 | sysfs_print(discard, ca->discard); |
| 646 | sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9); |
| 647 | sysfs_hprint(btree_written, |
| 648 | atomic_long_read(&ca->btree_sectors_written) << 9); |
| 649 | sysfs_hprint(metadata_written, |
| 650 | (atomic_long_read(&ca->meta_sectors_written) + |
| 651 | atomic_long_read(&ca->btree_sectors_written)) << 9); |
| 652 | |
| 653 | sysfs_print(io_errors, |
| 654 | atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT); |
| 655 | |
| 656 | sysfs_print(freelist_percent, ca->free.size * 100 / |
| 657 | ((size_t) ca->sb.nbuckets)); |
| 658 | |
| 659 | if (attr == &sysfs_cache_replacement_policy) |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 660 | return bch_snprint_string_list(buf, PAGE_SIZE, |
| 661 | cache_replacement_policies, |
| 662 | CACHE_REPLACEMENT(&ca->sb)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 663 | |
| 664 | if (attr == &sysfs_priority_stats) { |
| 665 | int cmp(const void *l, const void *r) |
| 666 | { return *((uint16_t *) r) - *((uint16_t *) l); } |
| 667 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 668 | size_t n = ca->sb.nbuckets, i, unused, btree; |
| 669 | uint64_t sum = 0; |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame^] | 670 | /* Compute 31 quantiles */ |
| 671 | uint16_t q[31], *p, *cached; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 672 | ssize_t ret; |
| 673 | |
| 674 | cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t)); |
| 675 | if (!p) |
| 676 | return -ENOMEM; |
| 677 | |
| 678 | mutex_lock(&ca->set->bucket_lock); |
| 679 | for (i = ca->sb.first_bucket; i < n; i++) |
| 680 | p[i] = ca->buckets[i].prio; |
| 681 | mutex_unlock(&ca->set->bucket_lock); |
| 682 | |
| 683 | sort(p, n, sizeof(uint16_t), cmp, NULL); |
| 684 | |
| 685 | while (n && |
| 686 | !cached[n - 1]) |
| 687 | --n; |
| 688 | |
| 689 | unused = ca->sb.nbuckets - n; |
| 690 | |
| 691 | while (cached < p + n && |
| 692 | *cached == BTREE_PRIO) |
| 693 | cached++; |
| 694 | |
| 695 | btree = cached - p; |
| 696 | n -= btree; |
| 697 | |
| 698 | for (i = 0; i < n; i++) |
| 699 | sum += INITIAL_PRIO - cached[i]; |
| 700 | |
| 701 | if (n) |
| 702 | do_div(sum, n); |
| 703 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame^] | 704 | for (i = 0; i < ARRAY_SIZE(q); i++) |
| 705 | q[i] = INITIAL_PRIO - cached[n * (i + 1) / |
| 706 | (ARRAY_SIZE(q) + 1)]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 707 | |
| 708 | vfree(p); |
| 709 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame^] | 710 | ret = scnprintf(buf, PAGE_SIZE, |
| 711 | "Unused: %zu%%\n" |
| 712 | "Metadata: %zu%%\n" |
| 713 | "Average: %llu\n" |
| 714 | "Sectors per Q: %zu\n" |
| 715 | "Quantiles: [", |
| 716 | unused * 100 / (size_t) ca->sb.nbuckets, |
| 717 | btree * 100 / (size_t) ca->sb.nbuckets, sum, |
| 718 | n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 719 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame^] | 720 | for (i = 0; i < ARRAY_SIZE(q); i++) |
| 721 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, |
| 722 | "%u ", q[i]); |
| 723 | ret--; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 724 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame^] | 725 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n"); |
| 726 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | SHOW_LOCKED(bch_cache) |
| 733 | |
| 734 | STORE(__bch_cache) |
| 735 | { |
| 736 | struct cache *ca = container_of(kobj, struct cache, kobj); |
| 737 | |
| 738 | if (attr == &sysfs_discard) { |
| 739 | bool v = strtoul_or_return(buf); |
| 740 | |
| 741 | if (blk_queue_discard(bdev_get_queue(ca->bdev))) |
| 742 | ca->discard = v; |
| 743 | |
| 744 | if (v != CACHE_DISCARD(&ca->sb)) { |
| 745 | SET_CACHE_DISCARD(&ca->sb, v); |
| 746 | bcache_write_super(ca->set); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (attr == &sysfs_cache_replacement_policy) { |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 751 | ssize_t v = bch_read_string_list(buf, cache_replacement_policies); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 752 | |
| 753 | if (v < 0) |
| 754 | return v; |
| 755 | |
| 756 | if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) { |
| 757 | mutex_lock(&ca->set->bucket_lock); |
| 758 | SET_CACHE_REPLACEMENT(&ca->sb, v); |
| 759 | mutex_unlock(&ca->set->bucket_lock); |
| 760 | |
| 761 | bcache_write_super(ca->set); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if (attr == &sysfs_freelist_percent) { |
| 766 | DECLARE_FIFO(long, free); |
| 767 | long i; |
| 768 | size_t p = strtoul_or_return(buf); |
| 769 | |
| 770 | p = clamp_t(size_t, |
| 771 | ((size_t) ca->sb.nbuckets * p) / 100, |
| 772 | roundup_pow_of_two(ca->sb.nbuckets) >> 9, |
| 773 | ca->sb.nbuckets / 2); |
| 774 | |
| 775 | if (!init_fifo_exact(&free, p, GFP_KERNEL)) |
| 776 | return -ENOMEM; |
| 777 | |
| 778 | mutex_lock(&ca->set->bucket_lock); |
| 779 | |
| 780 | fifo_move(&free, &ca->free); |
| 781 | fifo_swap(&free, &ca->free); |
| 782 | |
| 783 | mutex_unlock(&ca->set->bucket_lock); |
| 784 | |
| 785 | while (fifo_pop(&free, i)) |
| 786 | atomic_dec(&ca->buckets[i].pin); |
| 787 | |
| 788 | free_fifo(&free); |
| 789 | } |
| 790 | |
| 791 | if (attr == &sysfs_clear_stats) { |
| 792 | atomic_long_set(&ca->sectors_written, 0); |
| 793 | atomic_long_set(&ca->btree_sectors_written, 0); |
| 794 | atomic_long_set(&ca->meta_sectors_written, 0); |
| 795 | atomic_set(&ca->io_count, 0); |
| 796 | atomic_set(&ca->io_errors, 0); |
| 797 | } |
| 798 | |
| 799 | return size; |
| 800 | } |
| 801 | STORE_LOCKED(bch_cache) |
| 802 | |
| 803 | static struct attribute *bch_cache_files[] = { |
| 804 | &sysfs_bucket_size, |
| 805 | &sysfs_block_size, |
| 806 | &sysfs_nbuckets, |
| 807 | &sysfs_priority_stats, |
| 808 | &sysfs_discard, |
| 809 | &sysfs_written, |
| 810 | &sysfs_btree_written, |
| 811 | &sysfs_metadata_written, |
| 812 | &sysfs_io_errors, |
| 813 | &sysfs_clear_stats, |
| 814 | &sysfs_freelist_percent, |
| 815 | &sysfs_cache_replacement_policy, |
| 816 | NULL |
| 817 | }; |
| 818 | KTYPE(bch_cache); |