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 extern bool bcache_is_reboot;
21 /* Default is 0 ("writethrough") */
22 static const char * const bch_cache_modes[] = {
30 /* Default is 0 ("auto") */
31 static const char * const bch_stop_on_failure_modes[] = {
37 static const char * const cache_replacement_policies[] = {
44 static const char * const error_actions[] = {
50 write_attribute(attach);
51 write_attribute(detach);
52 write_attribute(unregister);
53 write_attribute(stop);
54 write_attribute(clear_stats);
55 write_attribute(trigger_gc);
56 write_attribute(prune_cache);
57 write_attribute(flash_vol_create);
59 read_attribute(bucket_size);
60 read_attribute(block_size);
61 read_attribute(nbuckets);
62 read_attribute(tree_depth);
63 read_attribute(root_usage_percent);
64 read_attribute(priority_stats);
65 read_attribute(btree_cache_size);
66 read_attribute(btree_cache_max_chain);
67 read_attribute(cache_available_percent);
68 read_attribute(written);
69 read_attribute(btree_written);
70 read_attribute(metadata_written);
71 read_attribute(active_journal_entries);
72 read_attribute(backing_dev_name);
73 read_attribute(backing_dev_uuid);
75 sysfs_time_stats_attribute(btree_gc, sec, ms);
76 sysfs_time_stats_attribute(btree_split, sec, us);
77 sysfs_time_stats_attribute(btree_sort, ms, us);
78 sysfs_time_stats_attribute(btree_read, ms, us);
80 read_attribute(btree_nodes);
81 read_attribute(btree_used_percent);
82 read_attribute(average_key_size);
83 read_attribute(dirty_data);
84 read_attribute(bset_tree_stats);
86 read_attribute(state);
87 read_attribute(cache_read_races);
88 read_attribute(reclaim);
89 read_attribute(reclaimed_journal_buckets);
90 read_attribute(flush_write);
91 read_attribute(writeback_keys_done);
92 read_attribute(writeback_keys_failed);
93 read_attribute(io_errors);
94 read_attribute(congested);
95 read_attribute(cutoff_writeback);
96 read_attribute(cutoff_writeback_sync);
97 rw_attribute(congested_read_threshold_us);
98 rw_attribute(congested_write_threshold_us);
100 rw_attribute(sequential_cutoff);
101 rw_attribute(data_csum);
102 rw_attribute(cache_mode);
103 rw_attribute(stop_when_cache_set_failed);
104 rw_attribute(writeback_metadata);
105 rw_attribute(writeback_running);
106 rw_attribute(writeback_percent);
107 rw_attribute(writeback_delay);
108 rw_attribute(writeback_rate);
110 rw_attribute(writeback_rate_update_seconds);
111 rw_attribute(writeback_rate_i_term_inverse);
112 rw_attribute(writeback_rate_p_term_inverse);
113 rw_attribute(writeback_rate_minimum);
114 read_attribute(writeback_rate_debug);
116 read_attribute(stripe_size);
117 read_attribute(partial_stripes_expensive);
119 rw_attribute(synchronous);
120 rw_attribute(journal_delay_ms);
121 rw_attribute(io_disable);
122 rw_attribute(discard);
123 rw_attribute(running);
125 rw_attribute(readahead);
126 rw_attribute(errors);
127 rw_attribute(io_error_limit);
128 rw_attribute(io_error_halflife);
129 rw_attribute(verify);
130 rw_attribute(bypass_torture_test);
131 rw_attribute(key_merging_disabled);
132 rw_attribute(gc_always_rewrite);
133 rw_attribute(expensive_debug_checks);
134 rw_attribute(cache_replacement_policy);
135 rw_attribute(btree_shrinker_disabled);
136 rw_attribute(copy_gc_enabled);
137 rw_attribute(gc_after_writeback);
140 static ssize_t bch_snprint_string_list(char *buf,
142 const char * const list[],
148 for (i = 0; list[i]; i++)
149 out += snprintf(out, buf + size - out,
150 i == selected ? "[%s] " : "%s ", list[i]);
156 SHOW(__bch_cached_dev)
158 struct cached_dev *dc = container_of(kobj, struct cached_dev,
160 char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
161 int wb = dc->writeback_running;
163 #define var(stat) (dc->stat)
165 if (attr == &sysfs_cache_mode)
166 return bch_snprint_string_list(buf, PAGE_SIZE,
168 BDEV_CACHE_MODE(&dc->sb));
170 if (attr == &sysfs_stop_when_cache_set_failed)
171 return bch_snprint_string_list(buf, PAGE_SIZE,
172 bch_stop_on_failure_modes,
173 dc->stop_when_cache_set_failed);
176 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
177 var_printf(verify, "%i");
178 var_printf(bypass_torture_test, "%i");
179 var_printf(writeback_metadata, "%i");
180 var_printf(writeback_running, "%i");
181 var_print(writeback_delay);
182 var_print(writeback_percent);
183 sysfs_hprint(writeback_rate,
184 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0);
185 sysfs_printf(io_errors, "%i", atomic_read(&dc->io_errors));
186 sysfs_printf(io_error_limit, "%i", dc->error_limit);
187 sysfs_printf(io_disable, "%i", dc->io_disable);
188 var_print(writeback_rate_update_seconds);
189 var_print(writeback_rate_i_term_inverse);
190 var_print(writeback_rate_p_term_inverse);
191 var_print(writeback_rate_minimum);
193 if (attr == &sysfs_writeback_rate_debug) {
197 char proportional[20];
203 * Except for dirty and target, other values should
204 * be 0 if writeback is not running.
207 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9
209 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
210 bch_hprint(target, dc->writeback_rate_target << 9);
211 bch_hprint(proportional,
212 wb ? dc->writeback_rate_proportional << 9 : 0);
214 wb ? dc->writeback_rate_integral_scaled << 9 : 0);
215 bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0);
216 next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(),
223 "proportional:\t%s\n"
225 "change:\t\t%s/sec\n"
226 "next io:\t%llims\n",
227 rate, dirty, target, proportional,
228 integral, change, next_io);
231 sysfs_hprint(dirty_data,
232 bcache_dev_sectors_dirty(&dc->disk) << 9);
234 sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9);
235 var_printf(partial_stripes_expensive, "%u");
237 var_hprint(sequential_cutoff);
238 var_hprint(readahead);
240 sysfs_print(running, atomic_read(&dc->running));
241 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
243 if (attr == &sysfs_label) {
244 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
245 buf[SB_LABEL_SIZE + 1] = '\0';
250 if (attr == &sysfs_backing_dev_name) {
251 snprintf(buf, BDEVNAME_SIZE + 1, "%s", dc->backing_dev_name);
256 if (attr == &sysfs_backing_dev_uuid) {
257 /* convert binary uuid into 36-byte string plus '\0' */
258 snprintf(buf, 36+1, "%pU", dc->sb.uuid);
266 SHOW_LOCKED(bch_cached_dev)
270 struct cached_dev *dc = container_of(kobj, struct cached_dev,
274 struct kobj_uevent_env *env;
276 /* no user space access if system is rebooting */
277 if (bcache_is_reboot)
280 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
281 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
282 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
284 sysfs_strtoul(data_csum, dc->disk.data_csum);
286 sysfs_strtoul_bool(bypass_torture_test, dc->bypass_torture_test);
287 sysfs_strtoul_bool(writeback_metadata, dc->writeback_metadata);
288 sysfs_strtoul_bool(writeback_running, dc->writeback_running);
289 sysfs_strtoul_clamp(writeback_delay, dc->writeback_delay, 0, UINT_MAX);
291 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent,
292 0, bch_cutoff_writeback);
294 if (attr == &sysfs_writeback_rate) {
296 long int v = atomic_long_read(&dc->writeback_rate.rate);
298 ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
301 atomic_long_set(&dc->writeback_rate.rate, v);
308 sysfs_strtoul_clamp(writeback_rate_update_seconds,
309 dc->writeback_rate_update_seconds,
310 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
311 sysfs_strtoul_clamp(writeback_rate_i_term_inverse,
312 dc->writeback_rate_i_term_inverse,
314 sysfs_strtoul_clamp(writeback_rate_p_term_inverse,
315 dc->writeback_rate_p_term_inverse,
317 sysfs_strtoul_clamp(writeback_rate_minimum,
318 dc->writeback_rate_minimum,
321 sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
323 if (attr == &sysfs_io_disable) {
324 int v = strtoul_or_return(buf);
326 dc->io_disable = v ? 1 : 0;
329 sysfs_strtoul_clamp(sequential_cutoff,
330 dc->sequential_cutoff,
332 d_strtoi_h(readahead);
334 if (attr == &sysfs_clear_stats)
335 bch_cache_accounting_clear(&dc->accounting);
337 if (attr == &sysfs_running &&
338 strtoul_or_return(buf)) {
339 v = bch_cached_dev_run(dc);
344 if (attr == &sysfs_cache_mode) {
345 v = __sysfs_match_string(bch_cache_modes, -1, buf);
349 if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) {
350 SET_BDEV_CACHE_MODE(&dc->sb, v);
351 bch_write_bdev_super(dc, NULL);
355 if (attr == &sysfs_stop_when_cache_set_failed) {
356 v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
360 dc->stop_when_cache_set_failed = v;
363 if (attr == &sysfs_label) {
364 if (size > SB_LABEL_SIZE)
366 memcpy(dc->sb.label, buf, size);
367 if (size < SB_LABEL_SIZE)
368 dc->sb.label[size] = '\0';
369 if (size && dc->sb.label[size - 1] == '\n')
370 dc->sb.label[size - 1] = '\0';
371 bch_write_bdev_super(dc, NULL);
373 memcpy(dc->disk.c->uuids[dc->disk.id].label,
375 bch_uuid_write(dc->disk.c);
377 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
380 add_uevent_var(env, "DRIVER=bcache");
381 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
382 add_uevent_var(env, "CACHED_LABEL=%s", buf);
383 kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj,
389 if (attr == &sysfs_attach) {
390 uint8_t set_uuid[16];
392 if (bch_parse_uuid(buf, set_uuid) < 16)
396 list_for_each_entry(c, &bch_cache_sets, list) {
397 v = bch_cached_dev_attach(dc, c, set_uuid);
402 pr_err("Can't attach %s: cache set not found", buf);
406 if (attr == &sysfs_detach && dc->disk.c)
407 bch_cached_dev_detach(dc);
409 if (attr == &sysfs_stop)
410 bcache_device_stop(&dc->disk);
415 STORE(bch_cached_dev)
417 struct cached_dev *dc = container_of(kobj, struct cached_dev,
420 /* no user space access if system is rebooting */
421 if (bcache_is_reboot)
424 mutex_lock(&bch_register_lock);
425 size = __cached_dev_store(kobj, attr, buf, size);
427 if (attr == &sysfs_writeback_running) {
428 /* dc->writeback_running changed in __cached_dev_store() */
429 if (IS_ERR_OR_NULL(dc->writeback_thread)) {
431 * reject setting it to 1 via sysfs if writeback
432 * kthread is not created yet.
434 if (dc->writeback_running) {
435 dc->writeback_running = false;
436 pr_err("%s: failed to run non-existent writeback thread",
437 dc->disk.disk->disk_name);
441 * writeback kthread will check if dc->writeback_running
444 bch_writeback_queue(dc);
448 * Only set BCACHE_DEV_WB_RUNNING when cached device attached to
449 * a cache set, otherwise it doesn't make sense.
451 if (attr == &sysfs_writeback_percent)
452 if ((dc->disk.c != NULL) &&
453 (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)))
454 schedule_delayed_work(&dc->writeback_rate_update,
455 dc->writeback_rate_update_seconds * HZ);
457 mutex_unlock(&bch_register_lock);
461 static struct attribute *bch_cached_dev_files[] = {
469 &sysfs_stop_when_cache_set_failed,
470 &sysfs_writeback_metadata,
471 &sysfs_writeback_running,
472 &sysfs_writeback_delay,
473 &sysfs_writeback_percent,
474 &sysfs_writeback_rate,
475 &sysfs_writeback_rate_update_seconds,
476 &sysfs_writeback_rate_i_term_inverse,
477 &sysfs_writeback_rate_p_term_inverse,
478 &sysfs_writeback_rate_minimum,
479 &sysfs_writeback_rate_debug,
481 &sysfs_io_error_limit,
485 &sysfs_partial_stripes_expensive,
486 &sysfs_sequential_cutoff,
492 #ifdef CONFIG_BCACHE_DEBUG
494 &sysfs_bypass_torture_test,
496 &sysfs_backing_dev_name,
497 &sysfs_backing_dev_uuid,
500 KTYPE(bch_cached_dev);
504 struct bcache_device *d = container_of(kobj, struct bcache_device,
506 struct uuid_entry *u = &d->c->uuids[d->id];
508 sysfs_printf(data_csum, "%i", d->data_csum);
509 sysfs_hprint(size, u->sectors << 9);
511 if (attr == &sysfs_label) {
512 memcpy(buf, u->label, SB_LABEL_SIZE);
513 buf[SB_LABEL_SIZE + 1] = '\0';
521 STORE(__bch_flash_dev)
523 struct bcache_device *d = container_of(kobj, struct bcache_device,
525 struct uuid_entry *u = &d->c->uuids[d->id];
527 /* no user space access if system is rebooting */
528 if (bcache_is_reboot)
531 sysfs_strtoul(data_csum, d->data_csum);
533 if (attr == &sysfs_size) {
536 strtoi_h_or_return(buf, v);
539 bch_uuid_write(d->c);
540 set_capacity(d->disk, u->sectors);
543 if (attr == &sysfs_label) {
544 memcpy(u->label, buf, SB_LABEL_SIZE);
545 bch_uuid_write(d->c);
548 if (attr == &sysfs_unregister) {
549 set_bit(BCACHE_DEV_DETACHING, &d->flags);
550 bcache_device_stop(d);
555 STORE_LOCKED(bch_flash_dev)
557 static struct attribute *bch_flash_dev_files[] = {
566 KTYPE(bch_flash_dev);
568 struct bset_stats_op {
571 struct bset_stats stats;
574 static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
576 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
579 bch_btree_keys_stats(&b->keys, &op->stats);
584 static int bch_bset_print_stats(struct cache_set *c, char *buf)
586 struct bset_stats_op op;
589 memset(&op, 0, sizeof(op));
590 bch_btree_op_init(&op.op, -1);
592 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
596 return snprintf(buf, PAGE_SIZE,
598 "written sets: %zu\n"
599 "unwritten sets: %zu\n"
600 "written key bytes: %zu\n"
601 "unwritten key bytes: %zu\n"
605 op.stats.sets_written, op.stats.sets_unwritten,
606 op.stats.bytes_written, op.stats.bytes_unwritten,
607 op.stats.floats, op.stats.failed);
610 static unsigned int bch_root_usage(struct cache_set *c)
612 unsigned int bytes = 0;
615 struct btree_iter iter;
623 rw_lock(false, b, b->level);
624 } while (b != c->root);
626 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
627 bytes += bkey_bytes(k);
631 return (bytes * 100) / btree_bytes(c);
634 static size_t bch_cache_size(struct cache_set *c)
639 mutex_lock(&c->bucket_lock);
640 list_for_each_entry(b, &c->btree_cache, list)
641 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
643 mutex_unlock(&c->bucket_lock);
647 static unsigned int bch_cache_max_chain(struct cache_set *c)
649 unsigned int ret = 0;
650 struct hlist_head *h;
652 mutex_lock(&c->bucket_lock);
654 for (h = c->bucket_hash;
655 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
658 struct hlist_node *p;
666 mutex_unlock(&c->bucket_lock);
670 static unsigned int bch_btree_used(struct cache_set *c)
672 return div64_u64(c->gc_stats.key_bytes * 100,
673 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
676 static unsigned int bch_average_key_size(struct cache_set *c)
678 return c->gc_stats.nkeys
679 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
683 SHOW(__bch_cache_set)
685 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
687 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
688 sysfs_print(journal_delay_ms, c->journal_delay_ms);
689 sysfs_hprint(bucket_size, bucket_bytes(c));
690 sysfs_hprint(block_size, block_bytes(c));
691 sysfs_print(tree_depth, c->root->level);
692 sysfs_print(root_usage_percent, bch_root_usage(c));
694 sysfs_hprint(btree_cache_size, bch_cache_size(c));
695 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
696 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
698 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
699 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
700 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
701 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
703 sysfs_print(btree_used_percent, bch_btree_used(c));
704 sysfs_print(btree_nodes, c->gc_stats.nodes);
705 sysfs_hprint(average_key_size, bch_average_key_size(c));
707 sysfs_print(cache_read_races,
708 atomic_long_read(&c->cache_read_races));
711 atomic_long_read(&c->reclaim));
713 sysfs_print(reclaimed_journal_buckets,
714 atomic_long_read(&c->reclaimed_journal_buckets));
716 sysfs_print(flush_write,
717 atomic_long_read(&c->flush_write));
719 sysfs_print(writeback_keys_done,
720 atomic_long_read(&c->writeback_keys_done));
721 sysfs_print(writeback_keys_failed,
722 atomic_long_read(&c->writeback_keys_failed));
724 if (attr == &sysfs_errors)
725 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
728 /* See count_io_errors for why 88 */
729 sysfs_print(io_error_halflife, c->error_decay * 88);
730 sysfs_print(io_error_limit, c->error_limit);
732 sysfs_hprint(congested,
733 ((uint64_t) bch_get_congested(c)) << 9);
734 sysfs_print(congested_read_threshold_us,
735 c->congested_read_threshold_us);
736 sysfs_print(congested_write_threshold_us,
737 c->congested_write_threshold_us);
739 sysfs_print(cutoff_writeback, bch_cutoff_writeback);
740 sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync);
742 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
743 sysfs_printf(verify, "%i", c->verify);
744 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
745 sysfs_printf(expensive_debug_checks,
746 "%i", c->expensive_debug_checks);
747 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
748 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
749 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
750 sysfs_printf(gc_after_writeback, "%i", c->gc_after_writeback);
751 sysfs_printf(io_disable, "%i",
752 test_bit(CACHE_SET_IO_DISABLE, &c->flags));
754 if (attr == &sysfs_bset_tree_stats)
755 return bch_bset_print_stats(c, buf);
759 SHOW_LOCKED(bch_cache_set)
761 STORE(__bch_cache_set)
763 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
766 /* no user space access if system is rebooting */
767 if (bcache_is_reboot)
770 if (attr == &sysfs_unregister)
771 bch_cache_set_unregister(c);
773 if (attr == &sysfs_stop)
774 bch_cache_set_stop(c);
776 if (attr == &sysfs_synchronous) {
777 bool sync = strtoul_or_return(buf);
779 if (sync != CACHE_SYNC(&c->sb)) {
780 SET_CACHE_SYNC(&c->sb, sync);
781 bcache_write_super(c);
785 if (attr == &sysfs_flash_vol_create) {
789 strtoi_h_or_return(buf, v);
791 r = bch_flash_dev_create(c, v);
796 if (attr == &sysfs_clear_stats) {
797 atomic_long_set(&c->writeback_keys_done, 0);
798 atomic_long_set(&c->writeback_keys_failed, 0);
800 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
801 bch_cache_accounting_clear(&c->accounting);
804 if (attr == &sysfs_trigger_gc)
807 if (attr == &sysfs_prune_cache) {
808 struct shrink_control sc;
810 sc.gfp_mask = GFP_KERNEL;
811 sc.nr_to_scan = strtoul_or_return(buf);
812 c->shrink.scan_objects(&c->shrink, &sc);
815 sysfs_strtoul_clamp(congested_read_threshold_us,
816 c->congested_read_threshold_us,
818 sysfs_strtoul_clamp(congested_write_threshold_us,
819 c->congested_write_threshold_us,
822 if (attr == &sysfs_errors) {
823 v = __sysfs_match_string(error_actions, -1, buf);
830 sysfs_strtoul_clamp(io_error_limit, c->error_limit, 0, UINT_MAX);
832 /* See count_io_errors() for why 88 */
833 if (attr == &sysfs_io_error_halflife) {
837 ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX);
839 c->error_decay = v / 88;
845 if (attr == &sysfs_io_disable) {
846 v = strtoul_or_return(buf);
848 if (test_and_set_bit(CACHE_SET_IO_DISABLE,
850 pr_warn("CACHE_SET_IO_DISABLE already set");
852 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
854 pr_warn("CACHE_SET_IO_DISABLE already cleared");
858 sysfs_strtoul_clamp(journal_delay_ms,
861 sysfs_strtoul_bool(verify, c->verify);
862 sysfs_strtoul_bool(key_merging_disabled, c->key_merging_disabled);
863 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
864 sysfs_strtoul_bool(gc_always_rewrite, c->gc_always_rewrite);
865 sysfs_strtoul_bool(btree_shrinker_disabled, c->shrinker_disabled);
866 sysfs_strtoul_bool(copy_gc_enabled, c->copy_gc_enabled);
868 * write gc_after_writeback here may overwrite an already set
869 * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
870 * set in next chance.
872 sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
876 STORE_LOCKED(bch_cache_set)
878 SHOW(bch_cache_set_internal)
880 struct cache_set *c = container_of(kobj, struct cache_set, internal);
882 return bch_cache_set_show(&c->kobj, attr, buf);
885 STORE(bch_cache_set_internal)
887 struct cache_set *c = container_of(kobj, struct cache_set, internal);
889 /* no user space access if system is rebooting */
890 if (bcache_is_reboot)
893 return bch_cache_set_store(&c->kobj, attr, buf, size);
896 static void bch_cache_set_internal_release(struct kobject *k)
900 static struct attribute *bch_cache_set_files[] = {
904 &sysfs_journal_delay_ms,
905 &sysfs_flash_vol_create,
910 &sysfs_root_usage_percent,
911 &sysfs_btree_cache_size,
912 &sysfs_cache_available_percent,
914 &sysfs_average_key_size,
917 &sysfs_io_error_limit,
918 &sysfs_io_error_halflife,
920 &sysfs_congested_read_threshold_us,
921 &sysfs_congested_write_threshold_us,
925 KTYPE(bch_cache_set);
927 static struct attribute *bch_cache_set_internal_files[] = {
928 &sysfs_active_journal_entries,
930 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
931 sysfs_time_stats_attribute_list(btree_split, sec, us)
932 sysfs_time_stats_attribute_list(btree_sort, ms, us)
933 sysfs_time_stats_attribute_list(btree_read, ms, us)
936 &sysfs_btree_used_percent,
937 &sysfs_btree_cache_max_chain,
939 &sysfs_bset_tree_stats,
940 &sysfs_cache_read_races,
942 &sysfs_reclaimed_journal_buckets,
944 &sysfs_writeback_keys_done,
945 &sysfs_writeback_keys_failed,
949 #ifdef CONFIG_BCACHE_DEBUG
951 &sysfs_key_merging_disabled,
952 &sysfs_expensive_debug_checks,
954 &sysfs_gc_always_rewrite,
955 &sysfs_btree_shrinker_disabled,
956 &sysfs_copy_gc_enabled,
957 &sysfs_gc_after_writeback,
959 &sysfs_cutoff_writeback,
960 &sysfs_cutoff_writeback_sync,
963 KTYPE(bch_cache_set_internal);
965 static int __bch_cache_cmp(const void *l, const void *r)
967 return *((uint16_t *)r) - *((uint16_t *)l);
972 struct cache *ca = container_of(kobj, struct cache, kobj);
974 sysfs_hprint(bucket_size, bucket_bytes(ca));
975 sysfs_hprint(block_size, block_bytes(ca));
976 sysfs_print(nbuckets, ca->sb.nbuckets);
977 sysfs_print(discard, ca->discard);
978 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
979 sysfs_hprint(btree_written,
980 atomic_long_read(&ca->btree_sectors_written) << 9);
981 sysfs_hprint(metadata_written,
982 (atomic_long_read(&ca->meta_sectors_written) +
983 atomic_long_read(&ca->btree_sectors_written)) << 9);
985 sysfs_print(io_errors,
986 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
988 if (attr == &sysfs_cache_replacement_policy)
989 return bch_snprint_string_list(buf, PAGE_SIZE,
990 cache_replacement_policies,
991 CACHE_REPLACEMENT(&ca->sb));
993 if (attr == &sysfs_priority_stats) {
995 size_t n = ca->sb.nbuckets, i;
996 size_t unused = 0, available = 0, dirty = 0, meta = 0;
998 /* Compute 31 quantiles */
999 uint16_t q[31], *p, *cached;
1002 cached = p = vmalloc(array_size(sizeof(uint16_t),
1007 mutex_lock(&ca->set->bucket_lock);
1008 for_each_bucket(b, ca) {
1009 if (!GC_SECTORS_USED(b))
1011 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
1013 if (GC_MARK(b) == GC_MARK_DIRTY)
1015 if (GC_MARK(b) == GC_MARK_METADATA)
1019 for (i = ca->sb.first_bucket; i < n; i++)
1020 p[i] = ca->buckets[i].prio;
1021 mutex_unlock(&ca->set->bucket_lock);
1023 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
1029 while (cached < p + n &&
1030 *cached == BTREE_PRIO)
1033 for (i = 0; i < n; i++)
1034 sum += INITIAL_PRIO - cached[i];
1039 for (i = 0; i < ARRAY_SIZE(q); i++)
1040 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
1041 (ARRAY_SIZE(q) + 1)];
1045 ret = scnprintf(buf, PAGE_SIZE,
1051 "Sectors per Q: %zu\n"
1053 unused * 100 / (size_t) ca->sb.nbuckets,
1054 available * 100 / (size_t) ca->sb.nbuckets,
1055 dirty * 100 / (size_t) ca->sb.nbuckets,
1056 meta * 100 / (size_t) ca->sb.nbuckets, sum,
1057 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
1059 for (i = 0; i < ARRAY_SIZE(q); i++)
1060 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
1064 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
1071 SHOW_LOCKED(bch_cache)
1075 struct cache *ca = container_of(kobj, struct cache, kobj);
1078 /* no user space access if system is rebooting */
1079 if (bcache_is_reboot)
1082 if (attr == &sysfs_discard) {
1083 bool v = strtoul_or_return(buf);
1085 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1088 if (v != CACHE_DISCARD(&ca->sb)) {
1089 SET_CACHE_DISCARD(&ca->sb, v);
1090 bcache_write_super(ca->set);
1094 if (attr == &sysfs_cache_replacement_policy) {
1095 v = __sysfs_match_string(cache_replacement_policies, -1, buf);
1099 if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) {
1100 mutex_lock(&ca->set->bucket_lock);
1101 SET_CACHE_REPLACEMENT(&ca->sb, v);
1102 mutex_unlock(&ca->set->bucket_lock);
1104 bcache_write_super(ca->set);
1108 if (attr == &sysfs_clear_stats) {
1109 atomic_long_set(&ca->sectors_written, 0);
1110 atomic_long_set(&ca->btree_sectors_written, 0);
1111 atomic_long_set(&ca->meta_sectors_written, 0);
1112 atomic_set(&ca->io_count, 0);
1113 atomic_set(&ca->io_errors, 0);
1118 STORE_LOCKED(bch_cache)
1120 static struct attribute *bch_cache_files[] = {
1124 &sysfs_priority_stats,
1127 &sysfs_btree_written,
1128 &sysfs_metadata_written,
1131 &sysfs_cache_replacement_policy,