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