2 * bcache sysfs interfaces
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
13 #include <linux/sort.h>
15 static const char * const cache_replacement_policies[] = {
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);
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);
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);
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);
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);
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);
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);
82 rw_attribute(synchronous);
83 rw_attribute(journal_delay_ms);
84 rw_attribute(discard);
85 rw_attribute(running);
87 rw_attribute(readahead);
88 rw_attribute(io_error_limit);
89 rw_attribute(io_error_halflife);
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);
99 SHOW(__bch_cached_dev)
101 struct cached_dev *dc = container_of(kobj, struct cached_dev,
103 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
105 #define var(stat) (dc->stat)
107 if (attr == &sysfs_cache_mode)
108 return bch_snprint_string_list(buf, PAGE_SIZE,
110 BDEV_CACHE_MODE(&dc->sb));
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);
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);
125 if (attr == &sysfs_writeback_rate_debug) {
130 atomic_long_read(&dc->disk.sectors_dirty) << 9);
131 bch_hprint(derivative, dc->writeback_rate_derivative << 9);
132 bch_hprint(target, dc->writeback_rate_target << 9);
140 dc->writeback_rate.rate,
141 dc->writeback_rate_change,
142 dirty, derivative, target);
145 sysfs_hprint(dirty_data,
146 atomic_long_read(&dc->disk.sectors_dirty) << 9);
148 var_printf(sequential_merge, "%i");
149 var_hprint(sequential_cutoff);
150 var_hprint(readahead);
152 sysfs_print(running, atomic_read(&dc->running));
153 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
155 if (attr == &sysfs_label) {
156 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
157 buf[SB_LABEL_SIZE + 1] = '\0';
165 SHOW_LOCKED(bch_cached_dev)
169 struct cached_dev *dc = container_of(kobj, struct cached_dev,
174 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
175 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
177 sysfs_strtoul(data_csum, dc->disk.data_csum);
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);
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);
193 d_strtoul(sequential_merge);
194 d_strtoi_h(sequential_cutoff);
195 d_strtoi_h(readahead);
197 if (attr == &sysfs_clear_stats)
198 bch_cache_accounting_clear(&dc->accounting);
200 if (attr == &sysfs_running &&
201 strtoul_or_return(buf))
202 bch_cached_dev_run(dc);
204 if (attr == &sysfs_cache_mode) {
205 ssize_t v = bch_read_string_list(buf, bch_cache_modes + 1);
210 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
211 SET_BDEV_CACHE_MODE(&dc->sb, v);
212 bch_write_bdev_super(dc, NULL);
216 if (attr == &sysfs_label) {
217 memcpy(dc->sb.label, buf, SB_LABEL_SIZE);
218 bch_write_bdev_super(dc, NULL);
220 memcpy(dc->disk.c->uuids[dc->disk.id].label,
222 bch_uuid_write(dc->disk.c);
226 if (attr == &sysfs_attach) {
227 if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
230 list_for_each_entry(c, &bch_cache_sets, list) {
231 v = bch_cached_dev_attach(dc, c);
236 pr_err("Can't attach %s: cache set not found", buf);
240 if (attr == &sysfs_detach && dc->disk.c)
241 bch_cached_dev_detach(dc);
243 if (attr == &sysfs_stop)
244 bcache_device_stop(&dc->disk);
249 STORE(bch_cached_dev)
251 struct cached_dev *dc = container_of(kobj, struct cached_dev,
254 mutex_lock(&bch_register_lock);
255 size = __cached_dev_store(kobj, attr, buf, size);
257 if (attr == &sysfs_writeback_running)
258 bch_writeback_queue(dc);
260 if (attr == &sysfs_writeback_percent)
261 schedule_delayed_work(&dc->writeback_rate_update,
262 dc->writeback_rate_update_seconds * HZ);
264 mutex_unlock(&bch_register_lock);
268 static struct attribute *bch_cached_dev_files[] = {
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,
287 &sysfs_sequential_cutoff,
288 &sysfs_sequential_merge,
294 #ifdef CONFIG_BCACHE_DEBUG
299 KTYPE(bch_cached_dev);
303 struct bcache_device *d = container_of(kobj, struct bcache_device,
305 struct uuid_entry *u = &d->c->uuids[d->id];
307 sysfs_printf(data_csum, "%i", d->data_csum);
308 sysfs_hprint(size, u->sectors << 9);
310 if (attr == &sysfs_label) {
311 memcpy(buf, u->label, SB_LABEL_SIZE);
312 buf[SB_LABEL_SIZE + 1] = '\0';
320 STORE(__bch_flash_dev)
322 struct bcache_device *d = container_of(kobj, struct bcache_device,
324 struct uuid_entry *u = &d->c->uuids[d->id];
326 sysfs_strtoul(data_csum, d->data_csum);
328 if (attr == &sysfs_size) {
330 strtoi_h_or_return(buf, v);
333 bch_uuid_write(d->c);
334 set_capacity(d->disk, u->sectors);
337 if (attr == &sysfs_label) {
338 memcpy(u->label, buf, SB_LABEL_SIZE);
339 bch_uuid_write(d->c);
342 if (attr == &sysfs_unregister) {
343 atomic_set(&d->detaching, 1);
344 bcache_device_stop(d);
349 STORE_LOCKED(bch_flash_dev)
351 static struct attribute *bch_flash_dev_files[] = {
360 KTYPE(bch_flash_dev);
362 SHOW(__bch_cache_set)
364 unsigned root_usage(struct cache_set *c)
369 struct btree_iter iter;
377 rw_lock(false, b, b->level);
378 } while (b != c->root);
380 for_each_key_filter(b, k, &iter, bch_ptr_bad)
381 bytes += bkey_bytes(k);
385 return (bytes * 100) / btree_bytes(c);
388 size_t cache_size(struct cache_set *c)
393 mutex_lock(&c->bucket_lock);
394 list_for_each_entry(b, &c->btree_cache, list)
395 ret += 1 << (b->page_order + PAGE_SHIFT);
397 mutex_unlock(&c->bucket_lock);
401 unsigned cache_max_chain(struct cache_set *c)
404 struct hlist_head *h;
406 mutex_lock(&c->bucket_lock);
408 for (h = c->bucket_hash;
409 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
412 struct hlist_node *p;
420 mutex_unlock(&c->bucket_lock);
424 unsigned btree_used(struct cache_set *c)
426 return div64_u64(c->gc_stats.key_bytes * 100,
427 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
430 unsigned average_key_size(struct cache_set *c)
432 return c->gc_stats.nkeys
433 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
437 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
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));
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);
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);
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));
461 sysfs_print(cache_read_races,
462 atomic_long_read(&c->cache_read_races));
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));
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);
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);
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);
487 if (attr == &sysfs_bset_tree_stats)
488 return bch_bset_print_stats(c, buf);
492 SHOW_LOCKED(bch_cache_set)
494 STORE(__bch_cache_set)
496 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
498 if (attr == &sysfs_unregister)
499 bch_cache_set_unregister(c);
501 if (attr == &sysfs_stop)
502 bch_cache_set_stop(c);
504 if (attr == &sysfs_synchronous) {
505 bool sync = strtoul_or_return(buf);
507 if (sync != CACHE_SYNC(&c->sb)) {
508 SET_CACHE_SYNC(&c->sb, sync);
509 bcache_write_super(c);
513 if (attr == &sysfs_flash_vol_create) {
516 strtoi_h_or_return(buf, v);
518 r = bch_flash_dev_create(c, v);
523 if (attr == &sysfs_clear_stats) {
524 atomic_long_set(&c->writeback_keys_done, 0);
525 atomic_long_set(&c->writeback_keys_failed, 0);
527 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
528 bch_cache_accounting_clear(&c->accounting);
531 if (attr == &sysfs_trigger_gc)
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);
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);
546 if (attr == &sysfs_io_error_limit)
547 c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
549 /* See count_io_errors() for why 88 */
550 if (attr == &sysfs_io_error_halflife)
551 c->error_decay = strtoul_or_return(buf) / 88;
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);
562 STORE_LOCKED(bch_cache_set)
564 SHOW(bch_cache_set_internal)
566 struct cache_set *c = container_of(kobj, struct cache_set, internal);
567 return bch_cache_set_show(&c->kobj, attr, buf);
570 STORE(bch_cache_set_internal)
572 struct cache_set *c = container_of(kobj, struct cache_set, internal);
573 return bch_cache_set_store(&c->kobj, attr, buf, size);
576 static void bch_cache_set_internal_release(struct kobject *k)
580 static struct attribute *bch_cache_set_files[] = {
584 &sysfs_journal_delay_ms,
585 &sysfs_flash_vol_create,
590 &sysfs_root_usage_percent,
591 &sysfs_btree_cache_size,
592 &sysfs_cache_available_percent,
594 &sysfs_average_key_size,
597 &sysfs_io_error_limit,
598 &sysfs_io_error_halflife,
600 &sysfs_congested_read_threshold_us,
601 &sysfs_congested_write_threshold_us,
605 KTYPE(bch_cache_set);
607 static struct attribute *bch_cache_set_internal_files[] = {
608 &sysfs_active_journal_entries,
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)
617 &sysfs_btree_used_percent,
618 &sysfs_btree_cache_max_chain,
620 &sysfs_bset_tree_stats,
621 &sysfs_cache_read_races,
622 &sysfs_writeback_keys_done,
623 &sysfs_writeback_keys_failed,
627 #ifdef CONFIG_BCACHE_DEBUG
629 &sysfs_key_merging_disabled,
631 &sysfs_gc_always_rewrite,
632 &sysfs_btree_shrinker_disabled,
633 &sysfs_copy_gc_enabled,
636 KTYPE(bch_cache_set_internal);
640 struct cache *ca = container_of(kobj, struct cache, kobj);
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);
653 sysfs_print(io_errors,
654 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
656 sysfs_print(freelist_percent, ca->free.size * 100 /
657 ((size_t) ca->sb.nbuckets));
659 if (attr == &sysfs_cache_replacement_policy)
660 return bch_snprint_string_list(buf, PAGE_SIZE,
661 cache_replacement_policies,
662 CACHE_REPLACEMENT(&ca->sb));
664 if (attr == &sysfs_priority_stats) {
665 int cmp(const void *l, const void *r)
666 { return *((uint16_t *) r) - *((uint16_t *) l); }
668 /* Number of quantiles we compute */
669 const unsigned nq = 31;
671 size_t n = ca->sb.nbuckets, i, unused, btree;
673 uint16_t q[nq], *p, *cached;
676 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
680 mutex_lock(&ca->set->bucket_lock);
681 for (i = ca->sb.first_bucket; i < n; i++)
682 p[i] = ca->buckets[i].prio;
683 mutex_unlock(&ca->set->bucket_lock);
685 sort(p, n, sizeof(uint16_t), cmp, NULL);
691 unused = ca->sb.nbuckets - n;
693 while (cached < p + n &&
694 *cached == BTREE_PRIO)
700 for (i = 0; i < n; i++)
701 sum += INITIAL_PRIO - cached[i];
706 for (i = 0; i < nq; i++)
707 q[i] = INITIAL_PRIO - cached[n * (i + 1) / (nq + 1)];
711 ret = snprintf(buf, PAGE_SIZE,
715 "Sectors per Q: %zu\n"
717 unused * 100 / (size_t) ca->sb.nbuckets,
718 btree * 100 / (size_t) ca->sb.nbuckets, sum,
719 n * ca->sb.bucket_size / (nq + 1));
721 for (i = 0; i < nq && ret < (ssize_t) PAGE_SIZE; i++)
722 ret += snprintf(buf + ret, PAGE_SIZE - ret,
723 i < nq - 1 ? "%u " : "%u]\n", q[i]);
725 buf[PAGE_SIZE - 1] = '\0';
731 SHOW_LOCKED(bch_cache)
735 struct cache *ca = container_of(kobj, struct cache, kobj);
737 if (attr == &sysfs_discard) {
738 bool v = strtoul_or_return(buf);
740 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
743 if (v != CACHE_DISCARD(&ca->sb)) {
744 SET_CACHE_DISCARD(&ca->sb, v);
745 bcache_write_super(ca->set);
749 if (attr == &sysfs_cache_replacement_policy) {
750 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
755 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
756 mutex_lock(&ca->set->bucket_lock);
757 SET_CACHE_REPLACEMENT(&ca->sb, v);
758 mutex_unlock(&ca->set->bucket_lock);
760 bcache_write_super(ca->set);
764 if (attr == &sysfs_freelist_percent) {
765 DECLARE_FIFO(long, free);
767 size_t p = strtoul_or_return(buf);
770 ((size_t) ca->sb.nbuckets * p) / 100,
771 roundup_pow_of_two(ca->sb.nbuckets) >> 9,
772 ca->sb.nbuckets / 2);
774 if (!init_fifo_exact(&free, p, GFP_KERNEL))
777 mutex_lock(&ca->set->bucket_lock);
779 fifo_move(&free, &ca->free);
780 fifo_swap(&free, &ca->free);
782 mutex_unlock(&ca->set->bucket_lock);
784 while (fifo_pop(&free, i))
785 atomic_dec(&ca->buckets[i].pin);
790 if (attr == &sysfs_clear_stats) {
791 atomic_long_set(&ca->sectors_written, 0);
792 atomic_long_set(&ca->btree_sectors_written, 0);
793 atomic_long_set(&ca->meta_sectors_written, 0);
794 atomic_set(&ca->io_count, 0);
795 atomic_set(&ca->io_errors, 0);
800 STORE_LOCKED(bch_cache)
802 static struct attribute *bch_cache_files[] = {
806 &sysfs_priority_stats,
809 &sysfs_btree_written,
810 &sysfs_metadata_written,
813 &sysfs_freelist_percent,
814 &sysfs_cache_replacement_policy,