1 // SPDX-License-Identifier: GPL-2.0
3 * bcache sysfs interfaces
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
13 #include "writeback.h"
15 #include <linux/blkdev.h>
16 #include <linux/sort.h>
17 #include <linux/sched/clock.h>
19 /* Default is 0 ("writethrough") */
20 static const char * const bch_cache_modes[] = {
27 /* Default is 0 ("auto") */
28 static const char * const bch_stop_on_failure_modes[] = {
33 static const char * const cache_replacement_policies[] = {
39 static const char * const error_actions[] = {
44 write_attribute(attach);
45 write_attribute(detach);
46 write_attribute(unregister);
47 write_attribute(stop);
48 write_attribute(clear_stats);
49 write_attribute(trigger_gc);
50 write_attribute(prune_cache);
51 write_attribute(flash_vol_create);
53 read_attribute(bucket_size);
54 read_attribute(block_size);
55 read_attribute(nbuckets);
56 read_attribute(tree_depth);
57 read_attribute(root_usage_percent);
58 read_attribute(priority_stats);
59 read_attribute(btree_cache_size);
60 read_attribute(btree_cache_max_chain);
61 read_attribute(cache_available_percent);
62 read_attribute(written);
63 read_attribute(btree_written);
64 read_attribute(metadata_written);
65 read_attribute(active_journal_entries);
66 read_attribute(backing_dev_name);
67 read_attribute(backing_dev_uuid);
69 sysfs_time_stats_attribute(btree_gc, sec, ms);
70 sysfs_time_stats_attribute(btree_split, sec, us);
71 sysfs_time_stats_attribute(btree_sort, ms, us);
72 sysfs_time_stats_attribute(btree_read, ms, us);
74 read_attribute(btree_nodes);
75 read_attribute(btree_used_percent);
76 read_attribute(average_key_size);
77 read_attribute(dirty_data);
78 read_attribute(bset_tree_stats);
80 read_attribute(state);
81 read_attribute(cache_read_races);
82 read_attribute(reclaim);
83 read_attribute(flush_write);
84 read_attribute(retry_flush_write);
85 read_attribute(writeback_keys_done);
86 read_attribute(writeback_keys_failed);
87 read_attribute(io_errors);
88 read_attribute(congested);
89 read_attribute(cutoff_writeback);
90 read_attribute(cutoff_writeback_sync);
91 rw_attribute(congested_read_threshold_us);
92 rw_attribute(congested_write_threshold_us);
94 rw_attribute(sequential_cutoff);
95 rw_attribute(data_csum);
96 rw_attribute(cache_mode);
97 rw_attribute(stop_when_cache_set_failed);
98 rw_attribute(writeback_metadata);
99 rw_attribute(writeback_running);
100 rw_attribute(writeback_percent);
101 rw_attribute(writeback_delay);
102 rw_attribute(writeback_rate);
104 rw_attribute(writeback_rate_update_seconds);
105 rw_attribute(writeback_rate_i_term_inverse);
106 rw_attribute(writeback_rate_p_term_inverse);
107 rw_attribute(writeback_rate_minimum);
108 read_attribute(writeback_rate_debug);
110 read_attribute(stripe_size);
111 read_attribute(partial_stripes_expensive);
113 rw_attribute(synchronous);
114 rw_attribute(journal_delay_ms);
115 rw_attribute(io_disable);
116 rw_attribute(discard);
117 rw_attribute(running);
119 rw_attribute(readahead);
120 rw_attribute(errors);
121 rw_attribute(io_error_limit);
122 rw_attribute(io_error_halflife);
123 rw_attribute(verify);
124 rw_attribute(bypass_torture_test);
125 rw_attribute(key_merging_disabled);
126 rw_attribute(gc_always_rewrite);
127 rw_attribute(expensive_debug_checks);
128 rw_attribute(cache_replacement_policy);
129 rw_attribute(btree_shrinker_disabled);
130 rw_attribute(copy_gc_enabled);
131 rw_attribute(gc_after_writeback);
134 static ssize_t bch_snprint_string_list(char *buf,
136 const char * const list[],
142 for (i = 0; list[i]; i++)
143 out += snprintf(out, buf + size - out,
144 i == selected ? "[%s] " : "%s ", list[i]);
150 SHOW(__bch_cached_dev)
152 struct cached_dev *dc = container_of(kobj, struct cached_dev,
154 char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
155 int wb = dc->writeback_running;
157 #define var(stat) (dc->stat)
159 if (attr == &sysfs_cache_mode)
160 return bch_snprint_string_list(buf, PAGE_SIZE,
162 BDEV_CACHE_MODE(&dc->sb));
164 if (attr == &sysfs_stop_when_cache_set_failed)
165 return bch_snprint_string_list(buf, PAGE_SIZE,
166 bch_stop_on_failure_modes,
167 dc->stop_when_cache_set_failed);
170 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
171 var_printf(verify, "%i");
172 var_printf(bypass_torture_test, "%i");
173 var_printf(writeback_metadata, "%i");
174 var_printf(writeback_running, "%i");
175 var_print(writeback_delay);
176 var_print(writeback_percent);
177 sysfs_hprint(writeback_rate,
178 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0);
179 sysfs_hprint(io_errors, atomic_read(&dc->io_errors));
180 sysfs_printf(io_error_limit, "%i", dc->error_limit);
181 sysfs_printf(io_disable, "%i", dc->io_disable);
182 var_print(writeback_rate_update_seconds);
183 var_print(writeback_rate_i_term_inverse);
184 var_print(writeback_rate_p_term_inverse);
185 var_print(writeback_rate_minimum);
187 if (attr == &sysfs_writeback_rate_debug) {
191 char proportional[20];
197 * Except for dirty and target, other values should
198 * be 0 if writeback is not running.
201 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9
203 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
204 bch_hprint(target, dc->writeback_rate_target << 9);
205 bch_hprint(proportional,
206 wb ? dc->writeback_rate_proportional << 9 : 0);
208 wb ? dc->writeback_rate_integral_scaled << 9 : 0);
209 bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0);
210 next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(),
217 "proportional:\t%s\n"
219 "change:\t\t%s/sec\n"
220 "next io:\t%llims\n",
221 rate, dirty, target, proportional,
222 integral, change, next_io);
225 sysfs_hprint(dirty_data,
226 bcache_dev_sectors_dirty(&dc->disk) << 9);
228 sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9);
229 var_printf(partial_stripes_expensive, "%u");
231 var_hprint(sequential_cutoff);
232 var_hprint(readahead);
234 sysfs_print(running, atomic_read(&dc->running));
235 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
237 if (attr == &sysfs_label) {
238 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
239 buf[SB_LABEL_SIZE + 1] = '\0';
244 if (attr == &sysfs_backing_dev_name) {
245 snprintf(buf, BDEVNAME_SIZE + 1, "%s", dc->backing_dev_name);
250 if (attr == &sysfs_backing_dev_uuid) {
251 /* convert binary uuid into 36-byte string plus '\0' */
252 snprintf(buf, 36+1, "%pU", dc->sb.uuid);
260 SHOW_LOCKED(bch_cached_dev)
264 struct cached_dev *dc = container_of(kobj, struct cached_dev,
268 struct kobj_uevent_env *env;
270 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
271 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
272 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
274 sysfs_strtoul(data_csum, dc->disk.data_csum);
276 sysfs_strtoul_bool(bypass_torture_test, dc->bypass_torture_test);
277 sysfs_strtoul_bool(writeback_metadata, dc->writeback_metadata);
278 sysfs_strtoul_bool(writeback_running, dc->writeback_running);
279 sysfs_strtoul_clamp(writeback_delay, dc->writeback_delay, 0, UINT_MAX);
281 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent,
282 0, bch_cutoff_writeback);
284 if (attr == &sysfs_writeback_rate) {
286 long int v = atomic_long_read(&dc->writeback_rate.rate);
288 ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
291 atomic_long_set(&dc->writeback_rate.rate, v);
298 sysfs_strtoul_clamp(writeback_rate_update_seconds,
299 dc->writeback_rate_update_seconds,
300 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
301 sysfs_strtoul_clamp(writeback_rate_i_term_inverse,
302 dc->writeback_rate_i_term_inverse,
304 sysfs_strtoul_clamp(writeback_rate_p_term_inverse,
305 dc->writeback_rate_p_term_inverse,
307 sysfs_strtoul_clamp(writeback_rate_minimum,
308 dc->writeback_rate_minimum,
311 sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
313 if (attr == &sysfs_io_disable) {
314 int v = strtoul_or_return(buf);
316 dc->io_disable = v ? 1 : 0;
319 sysfs_strtoul_clamp(sequential_cutoff,
320 dc->sequential_cutoff,
322 d_strtoi_h(readahead);
324 if (attr == &sysfs_clear_stats)
325 bch_cache_accounting_clear(&dc->accounting);
327 if (attr == &sysfs_running &&
328 strtoul_or_return(buf)) {
329 v = bch_cached_dev_run(dc);
334 if (attr == &sysfs_cache_mode) {
335 v = sysfs_match_string(bch_cache_modes, buf);
339 if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) {
340 SET_BDEV_CACHE_MODE(&dc->sb, v);
341 bch_write_bdev_super(dc, NULL);
345 if (attr == &sysfs_stop_when_cache_set_failed) {
346 v = sysfs_match_string(bch_stop_on_failure_modes, buf);
350 dc->stop_when_cache_set_failed = v;
353 if (attr == &sysfs_label) {
354 if (size > SB_LABEL_SIZE)
356 memcpy(dc->sb.label, buf, size);
357 if (size < SB_LABEL_SIZE)
358 dc->sb.label[size] = '\0';
359 if (size && dc->sb.label[size - 1] == '\n')
360 dc->sb.label[size - 1] = '\0';
361 bch_write_bdev_super(dc, NULL);
363 memcpy(dc->disk.c->uuids[dc->disk.id].label,
365 bch_uuid_write(dc->disk.c);
367 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
370 add_uevent_var(env, "DRIVER=bcache");
371 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
372 add_uevent_var(env, "CACHED_LABEL=%s", buf);
373 kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj,
379 if (attr == &sysfs_attach) {
380 uint8_t set_uuid[16];
382 if (bch_parse_uuid(buf, set_uuid) < 16)
386 list_for_each_entry(c, &bch_cache_sets, list) {
387 v = bch_cached_dev_attach(dc, c, set_uuid);
392 pr_err("Can't attach %s: cache set not found", buf);
396 if (attr == &sysfs_detach && dc->disk.c)
397 bch_cached_dev_detach(dc);
399 if (attr == &sysfs_stop)
400 bcache_device_stop(&dc->disk);
405 STORE(bch_cached_dev)
407 struct cached_dev *dc = container_of(kobj, struct cached_dev,
410 mutex_lock(&bch_register_lock);
411 size = __cached_dev_store(kobj, attr, buf, size);
413 if (attr == &sysfs_writeback_running) {
414 /* dc->writeback_running changed in __cached_dev_store() */
415 if (IS_ERR_OR_NULL(dc->writeback_thread)) {
417 * reject setting it to 1 via sysfs if writeback
418 * kthread is not created yet.
420 if (dc->writeback_running) {
421 dc->writeback_running = false;
422 pr_err("%s: failed to run non-existent writeback thread",
423 dc->disk.disk->disk_name);
427 * writeback kthread will check if dc->writeback_running
430 bch_writeback_queue(dc);
434 * Only set BCACHE_DEV_WB_RUNNING when cached device attached to
435 * a cache set, otherwise it doesn't make sense.
437 if (attr == &sysfs_writeback_percent)
438 if ((dc->disk.c != NULL) &&
439 (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)))
440 schedule_delayed_work(&dc->writeback_rate_update,
441 dc->writeback_rate_update_seconds * HZ);
443 mutex_unlock(&bch_register_lock);
447 static struct attribute *bch_cached_dev_files[] = {
455 &sysfs_stop_when_cache_set_failed,
456 &sysfs_writeback_metadata,
457 &sysfs_writeback_running,
458 &sysfs_writeback_delay,
459 &sysfs_writeback_percent,
460 &sysfs_writeback_rate,
461 &sysfs_writeback_rate_update_seconds,
462 &sysfs_writeback_rate_i_term_inverse,
463 &sysfs_writeback_rate_p_term_inverse,
464 &sysfs_writeback_rate_minimum,
465 &sysfs_writeback_rate_debug,
467 &sysfs_io_error_limit,
471 &sysfs_partial_stripes_expensive,
472 &sysfs_sequential_cutoff,
478 #ifdef CONFIG_BCACHE_DEBUG
480 &sysfs_bypass_torture_test,
482 &sysfs_backing_dev_name,
483 &sysfs_backing_dev_uuid,
486 KTYPE(bch_cached_dev);
490 struct bcache_device *d = container_of(kobj, struct bcache_device,
492 struct uuid_entry *u = &d->c->uuids[d->id];
494 sysfs_printf(data_csum, "%i", d->data_csum);
495 sysfs_hprint(size, u->sectors << 9);
497 if (attr == &sysfs_label) {
498 memcpy(buf, u->label, SB_LABEL_SIZE);
499 buf[SB_LABEL_SIZE + 1] = '\0';
507 STORE(__bch_flash_dev)
509 struct bcache_device *d = container_of(kobj, struct bcache_device,
511 struct uuid_entry *u = &d->c->uuids[d->id];
513 sysfs_strtoul(data_csum, d->data_csum);
515 if (attr == &sysfs_size) {
518 strtoi_h_or_return(buf, v);
521 bch_uuid_write(d->c);
522 set_capacity(d->disk, u->sectors);
525 if (attr == &sysfs_label) {
526 memcpy(u->label, buf, SB_LABEL_SIZE);
527 bch_uuid_write(d->c);
530 if (attr == &sysfs_unregister) {
531 set_bit(BCACHE_DEV_DETACHING, &d->flags);
532 bcache_device_stop(d);
537 STORE_LOCKED(bch_flash_dev)
539 static struct attribute *bch_flash_dev_files[] = {
548 KTYPE(bch_flash_dev);
550 struct bset_stats_op {
553 struct bset_stats stats;
556 static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
558 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
561 bch_btree_keys_stats(&b->keys, &op->stats);
566 static int bch_bset_print_stats(struct cache_set *c, char *buf)
568 struct bset_stats_op op;
571 memset(&op, 0, sizeof(op));
572 bch_btree_op_init(&op.op, -1);
574 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
578 return snprintf(buf, PAGE_SIZE,
580 "written sets: %zu\n"
581 "unwritten sets: %zu\n"
582 "written key bytes: %zu\n"
583 "unwritten key bytes: %zu\n"
587 op.stats.sets_written, op.stats.sets_unwritten,
588 op.stats.bytes_written, op.stats.bytes_unwritten,
589 op.stats.floats, op.stats.failed);
592 static unsigned int bch_root_usage(struct cache_set *c)
594 unsigned int bytes = 0;
597 struct btree_iter iter;
605 rw_lock(false, b, b->level);
606 } while (b != c->root);
608 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
609 bytes += bkey_bytes(k);
613 return (bytes * 100) / btree_bytes(c);
616 static size_t bch_cache_size(struct cache_set *c)
621 mutex_lock(&c->bucket_lock);
622 list_for_each_entry(b, &c->btree_cache, list)
623 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
625 mutex_unlock(&c->bucket_lock);
629 static unsigned int bch_cache_max_chain(struct cache_set *c)
631 unsigned int ret = 0;
632 struct hlist_head *h;
634 mutex_lock(&c->bucket_lock);
636 for (h = c->bucket_hash;
637 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
640 struct hlist_node *p;
648 mutex_unlock(&c->bucket_lock);
652 static unsigned int bch_btree_used(struct cache_set *c)
654 return div64_u64(c->gc_stats.key_bytes * 100,
655 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
658 static unsigned int bch_average_key_size(struct cache_set *c)
660 return c->gc_stats.nkeys
661 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
665 SHOW(__bch_cache_set)
667 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
669 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
670 sysfs_print(journal_delay_ms, c->journal_delay_ms);
671 sysfs_hprint(bucket_size, bucket_bytes(c));
672 sysfs_hprint(block_size, block_bytes(c));
673 sysfs_print(tree_depth, c->root->level);
674 sysfs_print(root_usage_percent, bch_root_usage(c));
676 sysfs_hprint(btree_cache_size, bch_cache_size(c));
677 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
678 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
680 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
681 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
682 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
683 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
685 sysfs_print(btree_used_percent, bch_btree_used(c));
686 sysfs_print(btree_nodes, c->gc_stats.nodes);
687 sysfs_hprint(average_key_size, bch_average_key_size(c));
689 sysfs_print(cache_read_races,
690 atomic_long_read(&c->cache_read_races));
693 atomic_long_read(&c->reclaim));
695 sysfs_print(flush_write,
696 atomic_long_read(&c->flush_write));
698 sysfs_print(retry_flush_write,
699 atomic_long_read(&c->retry_flush_write));
701 sysfs_print(writeback_keys_done,
702 atomic_long_read(&c->writeback_keys_done));
703 sysfs_print(writeback_keys_failed,
704 atomic_long_read(&c->writeback_keys_failed));
706 if (attr == &sysfs_errors)
707 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
710 /* See count_io_errors for why 88 */
711 sysfs_print(io_error_halflife, c->error_decay * 88);
712 sysfs_print(io_error_limit, c->error_limit);
714 sysfs_hprint(congested,
715 ((uint64_t) bch_get_congested(c)) << 9);
716 sysfs_print(congested_read_threshold_us,
717 c->congested_read_threshold_us);
718 sysfs_print(congested_write_threshold_us,
719 c->congested_write_threshold_us);
721 sysfs_print(cutoff_writeback, bch_cutoff_writeback);
722 sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync);
724 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
725 sysfs_printf(verify, "%i", c->verify);
726 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
727 sysfs_printf(expensive_debug_checks,
728 "%i", c->expensive_debug_checks);
729 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
730 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
731 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
732 sysfs_printf(gc_after_writeback, "%i", c->gc_after_writeback);
733 sysfs_printf(io_disable, "%i",
734 test_bit(CACHE_SET_IO_DISABLE, &c->flags));
736 if (attr == &sysfs_bset_tree_stats)
737 return bch_bset_print_stats(c, buf);
741 SHOW_LOCKED(bch_cache_set)
743 STORE(__bch_cache_set)
745 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
748 if (attr == &sysfs_unregister)
749 bch_cache_set_unregister(c);
751 if (attr == &sysfs_stop)
752 bch_cache_set_stop(c);
754 if (attr == &sysfs_synchronous) {
755 bool sync = strtoul_or_return(buf);
757 if (sync != CACHE_SYNC(&c->sb)) {
758 SET_CACHE_SYNC(&c->sb, sync);
759 bcache_write_super(c);
763 if (attr == &sysfs_flash_vol_create) {
767 strtoi_h_or_return(buf, v);
769 r = bch_flash_dev_create(c, v);
774 if (attr == &sysfs_clear_stats) {
775 atomic_long_set(&c->writeback_keys_done, 0);
776 atomic_long_set(&c->writeback_keys_failed, 0);
778 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
779 bch_cache_accounting_clear(&c->accounting);
782 if (attr == &sysfs_trigger_gc)
785 if (attr == &sysfs_prune_cache) {
786 struct shrink_control sc;
788 sc.gfp_mask = GFP_KERNEL;
789 sc.nr_to_scan = strtoul_or_return(buf);
790 c->shrink.scan_objects(&c->shrink, &sc);
793 sysfs_strtoul_clamp(congested_read_threshold_us,
794 c->congested_read_threshold_us,
796 sysfs_strtoul_clamp(congested_write_threshold_us,
797 c->congested_write_threshold_us,
800 if (attr == &sysfs_errors) {
801 v = sysfs_match_string(error_actions, buf);
808 sysfs_strtoul_clamp(io_error_limit, c->error_limit, 0, UINT_MAX);
810 /* See count_io_errors() for why 88 */
811 if (attr == &sysfs_io_error_halflife) {
815 ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX);
817 c->error_decay = v / 88;
823 if (attr == &sysfs_io_disable) {
824 v = strtoul_or_return(buf);
826 if (test_and_set_bit(CACHE_SET_IO_DISABLE,
828 pr_warn("CACHE_SET_IO_DISABLE already set");
830 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
832 pr_warn("CACHE_SET_IO_DISABLE already cleared");
836 sysfs_strtoul_clamp(journal_delay_ms,
839 sysfs_strtoul_bool(verify, c->verify);
840 sysfs_strtoul_bool(key_merging_disabled, c->key_merging_disabled);
841 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
842 sysfs_strtoul_bool(gc_always_rewrite, c->gc_always_rewrite);
843 sysfs_strtoul_bool(btree_shrinker_disabled, c->shrinker_disabled);
844 sysfs_strtoul_bool(copy_gc_enabled, c->copy_gc_enabled);
846 * write gc_after_writeback here may overwrite an already set
847 * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
848 * set in next chance.
850 sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
854 STORE_LOCKED(bch_cache_set)
856 SHOW(bch_cache_set_internal)
858 struct cache_set *c = container_of(kobj, struct cache_set, internal);
860 return bch_cache_set_show(&c->kobj, attr, buf);
863 STORE(bch_cache_set_internal)
865 struct cache_set *c = container_of(kobj, struct cache_set, internal);
867 return bch_cache_set_store(&c->kobj, attr, buf, size);
870 static void bch_cache_set_internal_release(struct kobject *k)
874 static struct attribute *bch_cache_set_files[] = {
878 &sysfs_journal_delay_ms,
879 &sysfs_flash_vol_create,
884 &sysfs_root_usage_percent,
885 &sysfs_btree_cache_size,
886 &sysfs_cache_available_percent,
888 &sysfs_average_key_size,
891 &sysfs_io_error_limit,
892 &sysfs_io_error_halflife,
894 &sysfs_congested_read_threshold_us,
895 &sysfs_congested_write_threshold_us,
899 KTYPE(bch_cache_set);
901 static struct attribute *bch_cache_set_internal_files[] = {
902 &sysfs_active_journal_entries,
904 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
905 sysfs_time_stats_attribute_list(btree_split, sec, us)
906 sysfs_time_stats_attribute_list(btree_sort, ms, us)
907 sysfs_time_stats_attribute_list(btree_read, ms, us)
910 &sysfs_btree_used_percent,
911 &sysfs_btree_cache_max_chain,
913 &sysfs_bset_tree_stats,
914 &sysfs_cache_read_races,
917 &sysfs_retry_flush_write,
918 &sysfs_writeback_keys_done,
919 &sysfs_writeback_keys_failed,
923 #ifdef CONFIG_BCACHE_DEBUG
925 &sysfs_key_merging_disabled,
926 &sysfs_expensive_debug_checks,
928 &sysfs_gc_always_rewrite,
929 &sysfs_btree_shrinker_disabled,
930 &sysfs_copy_gc_enabled,
931 &sysfs_gc_after_writeback,
933 &sysfs_cutoff_writeback,
934 &sysfs_cutoff_writeback_sync,
937 KTYPE(bch_cache_set_internal);
939 static int __bch_cache_cmp(const void *l, const void *r)
941 return *((uint16_t *)r) - *((uint16_t *)l);
946 struct cache *ca = container_of(kobj, struct cache, kobj);
948 sysfs_hprint(bucket_size, bucket_bytes(ca));
949 sysfs_hprint(block_size, block_bytes(ca));
950 sysfs_print(nbuckets, ca->sb.nbuckets);
951 sysfs_print(discard, ca->discard);
952 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
953 sysfs_hprint(btree_written,
954 atomic_long_read(&ca->btree_sectors_written) << 9);
955 sysfs_hprint(metadata_written,
956 (atomic_long_read(&ca->meta_sectors_written) +
957 atomic_long_read(&ca->btree_sectors_written)) << 9);
959 sysfs_print(io_errors,
960 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
962 if (attr == &sysfs_cache_replacement_policy)
963 return bch_snprint_string_list(buf, PAGE_SIZE,
964 cache_replacement_policies,
965 CACHE_REPLACEMENT(&ca->sb));
967 if (attr == &sysfs_priority_stats) {
969 size_t n = ca->sb.nbuckets, i;
970 size_t unused = 0, available = 0, dirty = 0, meta = 0;
972 /* Compute 31 quantiles */
973 uint16_t q[31], *p, *cached;
976 cached = p = vmalloc(array_size(sizeof(uint16_t),
981 mutex_lock(&ca->set->bucket_lock);
982 for_each_bucket(b, ca) {
983 if (!GC_SECTORS_USED(b))
985 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
987 if (GC_MARK(b) == GC_MARK_DIRTY)
989 if (GC_MARK(b) == GC_MARK_METADATA)
993 for (i = ca->sb.first_bucket; i < n; i++)
994 p[i] = ca->buckets[i].prio;
995 mutex_unlock(&ca->set->bucket_lock);
997 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
1003 while (cached < p + n &&
1004 *cached == BTREE_PRIO)
1007 for (i = 0; i < n; i++)
1008 sum += INITIAL_PRIO - cached[i];
1013 for (i = 0; i < ARRAY_SIZE(q); i++)
1014 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
1015 (ARRAY_SIZE(q) + 1)];
1019 ret = scnprintf(buf, PAGE_SIZE,
1025 "Sectors per Q: %zu\n"
1027 unused * 100 / (size_t) ca->sb.nbuckets,
1028 available * 100 / (size_t) ca->sb.nbuckets,
1029 dirty * 100 / (size_t) ca->sb.nbuckets,
1030 meta * 100 / (size_t) ca->sb.nbuckets, sum,
1031 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
1033 for (i = 0; i < ARRAY_SIZE(q); i++)
1034 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
1038 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
1045 SHOW_LOCKED(bch_cache)
1049 struct cache *ca = container_of(kobj, struct cache, kobj);
1052 if (attr == &sysfs_discard) {
1053 bool v = strtoul_or_return(buf);
1055 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1058 if (v != CACHE_DISCARD(&ca->sb)) {
1059 SET_CACHE_DISCARD(&ca->sb, v);
1060 bcache_write_super(ca->set);
1064 if (attr == &sysfs_cache_replacement_policy) {
1065 v = sysfs_match_string(cache_replacement_policies, buf);
1069 if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) {
1070 mutex_lock(&ca->set->bucket_lock);
1071 SET_CACHE_REPLACEMENT(&ca->sb, v);
1072 mutex_unlock(&ca->set->bucket_lock);
1074 bcache_write_super(ca->set);
1078 if (attr == &sysfs_clear_stats) {
1079 atomic_long_set(&ca->sectors_written, 0);
1080 atomic_long_set(&ca->btree_sectors_written, 0);
1081 atomic_long_set(&ca->meta_sectors_written, 0);
1082 atomic_set(&ca->io_count, 0);
1083 atomic_set(&ca->io_errors, 0);
1088 STORE_LOCKED(bch_cache)
1090 static struct attribute *bch_cache_files[] = {
1094 &sysfs_priority_stats,
1097 &sysfs_btree_written,
1098 &sysfs_metadata_written,
1101 &sysfs_cache_replacement_policy,