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 static const char * const bch_reada_cache_policies[] = {
36 /* Default is 0 ("auto") */
37 static const char * const bch_stop_on_failure_modes[] = {
43 static const char * const cache_replacement_policies[] = {
50 static const char * const error_actions[] = {
56 write_attribute(attach);
57 write_attribute(detach);
58 write_attribute(unregister);
59 write_attribute(stop);
60 write_attribute(clear_stats);
61 write_attribute(trigger_gc);
62 write_attribute(prune_cache);
63 write_attribute(flash_vol_create);
65 read_attribute(bucket_size);
66 read_attribute(block_size);
67 read_attribute(nbuckets);
68 read_attribute(tree_depth);
69 read_attribute(root_usage_percent);
70 read_attribute(priority_stats);
71 read_attribute(btree_cache_size);
72 read_attribute(btree_cache_max_chain);
73 read_attribute(cache_available_percent);
74 read_attribute(written);
75 read_attribute(btree_written);
76 read_attribute(metadata_written);
77 read_attribute(active_journal_entries);
78 read_attribute(backing_dev_name);
79 read_attribute(backing_dev_uuid);
81 sysfs_time_stats_attribute(btree_gc, sec, ms);
82 sysfs_time_stats_attribute(btree_split, sec, us);
83 sysfs_time_stats_attribute(btree_sort, ms, us);
84 sysfs_time_stats_attribute(btree_read, ms, us);
86 read_attribute(btree_nodes);
87 read_attribute(btree_used_percent);
88 read_attribute(average_key_size);
89 read_attribute(dirty_data);
90 read_attribute(bset_tree_stats);
92 read_attribute(state);
93 read_attribute(cache_read_races);
94 read_attribute(reclaim);
95 read_attribute(reclaimed_journal_buckets);
96 read_attribute(flush_write);
97 read_attribute(writeback_keys_done);
98 read_attribute(writeback_keys_failed);
99 read_attribute(io_errors);
100 read_attribute(congested);
101 read_attribute(cutoff_writeback);
102 read_attribute(cutoff_writeback_sync);
103 rw_attribute(congested_read_threshold_us);
104 rw_attribute(congested_write_threshold_us);
106 rw_attribute(sequential_cutoff);
107 rw_attribute(data_csum);
108 rw_attribute(cache_mode);
109 rw_attribute(readahead_cache_policy);
110 rw_attribute(stop_when_cache_set_failed);
111 rw_attribute(writeback_metadata);
112 rw_attribute(writeback_running);
113 rw_attribute(writeback_percent);
114 rw_attribute(writeback_delay);
115 rw_attribute(writeback_rate);
117 rw_attribute(writeback_rate_update_seconds);
118 rw_attribute(writeback_rate_i_term_inverse);
119 rw_attribute(writeback_rate_p_term_inverse);
120 rw_attribute(writeback_rate_minimum);
121 read_attribute(writeback_rate_debug);
123 read_attribute(stripe_size);
124 read_attribute(partial_stripes_expensive);
126 rw_attribute(synchronous);
127 rw_attribute(journal_delay_ms);
128 rw_attribute(io_disable);
129 rw_attribute(discard);
130 rw_attribute(running);
132 rw_attribute(readahead);
133 rw_attribute(errors);
134 rw_attribute(io_error_limit);
135 rw_attribute(io_error_halflife);
136 rw_attribute(verify);
137 rw_attribute(bypass_torture_test);
138 rw_attribute(key_merging_disabled);
139 rw_attribute(gc_always_rewrite);
140 rw_attribute(expensive_debug_checks);
141 rw_attribute(cache_replacement_policy);
142 rw_attribute(btree_shrinker_disabled);
143 rw_attribute(copy_gc_enabled);
144 rw_attribute(idle_max_writeback_rate);
145 rw_attribute(gc_after_writeback);
148 static ssize_t bch_snprint_string_list(char *buf,
150 const char * const list[],
156 for (i = 0; list[i]; i++)
157 out += scnprintf(out, buf + size - out,
158 i == selected ? "[%s] " : "%s ", list[i]);
164 SHOW(__bch_cached_dev)
166 struct cached_dev *dc = container_of(kobj, struct cached_dev,
168 char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
169 int wb = dc->writeback_running;
171 #define var(stat) (dc->stat)
173 if (attr == &sysfs_cache_mode)
174 return bch_snprint_string_list(buf, PAGE_SIZE,
176 BDEV_CACHE_MODE(&dc->sb));
178 if (attr == &sysfs_readahead_cache_policy)
179 return bch_snprint_string_list(buf, PAGE_SIZE,
180 bch_reada_cache_policies,
181 dc->cache_readahead_policy);
183 if (attr == &sysfs_stop_when_cache_set_failed)
184 return bch_snprint_string_list(buf, PAGE_SIZE,
185 bch_stop_on_failure_modes,
186 dc->stop_when_cache_set_failed);
189 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
190 var_printf(verify, "%i");
191 var_printf(bypass_torture_test, "%i");
192 var_printf(writeback_metadata, "%i");
193 var_printf(writeback_running, "%i");
194 var_print(writeback_delay);
195 var_print(writeback_percent);
196 sysfs_hprint(writeback_rate,
197 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0);
198 sysfs_printf(io_errors, "%i", atomic_read(&dc->io_errors));
199 sysfs_printf(io_error_limit, "%i", dc->error_limit);
200 sysfs_printf(io_disable, "%i", dc->io_disable);
201 var_print(writeback_rate_update_seconds);
202 var_print(writeback_rate_i_term_inverse);
203 var_print(writeback_rate_p_term_inverse);
204 var_print(writeback_rate_minimum);
206 if (attr == &sysfs_writeback_rate_debug) {
210 char proportional[20];
216 * Except for dirty and target, other values should
217 * be 0 if writeback is not running.
220 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9
222 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
223 bch_hprint(target, dc->writeback_rate_target << 9);
224 bch_hprint(proportional,
225 wb ? dc->writeback_rate_proportional << 9 : 0);
227 wb ? dc->writeback_rate_integral_scaled << 9 : 0);
228 bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0);
229 next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(),
236 "proportional:\t%s\n"
238 "change:\t\t%s/sec\n"
239 "next io:\t%llims\n",
240 rate, dirty, target, proportional,
241 integral, change, next_io);
244 sysfs_hprint(dirty_data,
245 bcache_dev_sectors_dirty(&dc->disk) << 9);
247 sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9);
248 var_printf(partial_stripes_expensive, "%u");
250 var_hprint(sequential_cutoff);
251 var_hprint(readahead);
253 sysfs_print(running, atomic_read(&dc->running));
254 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
256 if (attr == &sysfs_label) {
257 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
258 buf[SB_LABEL_SIZE + 1] = '\0';
263 if (attr == &sysfs_backing_dev_name) {
264 snprintf(buf, BDEVNAME_SIZE + 1, "%s", dc->backing_dev_name);
269 if (attr == &sysfs_backing_dev_uuid) {
270 /* convert binary uuid into 36-byte string plus '\0' */
271 snprintf(buf, 36+1, "%pU", dc->sb.uuid);
279 SHOW_LOCKED(bch_cached_dev)
283 struct cached_dev *dc = container_of(kobj, struct cached_dev,
287 struct kobj_uevent_env *env;
289 /* no user space access if system is rebooting */
290 if (bcache_is_reboot)
293 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
294 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
295 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
297 sysfs_strtoul(data_csum, dc->disk.data_csum);
299 sysfs_strtoul_bool(bypass_torture_test, dc->bypass_torture_test);
300 sysfs_strtoul_bool(writeback_metadata, dc->writeback_metadata);
301 sysfs_strtoul_bool(writeback_running, dc->writeback_running);
302 sysfs_strtoul_clamp(writeback_delay, dc->writeback_delay, 0, UINT_MAX);
304 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent,
305 0, bch_cutoff_writeback);
307 if (attr == &sysfs_writeback_rate) {
309 long int v = atomic_long_read(&dc->writeback_rate.rate);
311 ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
314 atomic_long_set(&dc->writeback_rate.rate, v);
321 sysfs_strtoul_clamp(writeback_rate_update_seconds,
322 dc->writeback_rate_update_seconds,
323 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
324 sysfs_strtoul_clamp(writeback_rate_i_term_inverse,
325 dc->writeback_rate_i_term_inverse,
327 sysfs_strtoul_clamp(writeback_rate_p_term_inverse,
328 dc->writeback_rate_p_term_inverse,
330 sysfs_strtoul_clamp(writeback_rate_minimum,
331 dc->writeback_rate_minimum,
334 sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
336 if (attr == &sysfs_io_disable) {
337 int v = strtoul_or_return(buf);
339 dc->io_disable = v ? 1 : 0;
342 sysfs_strtoul_clamp(sequential_cutoff,
343 dc->sequential_cutoff,
345 d_strtoi_h(readahead);
347 if (attr == &sysfs_clear_stats)
348 bch_cache_accounting_clear(&dc->accounting);
350 if (attr == &sysfs_running &&
351 strtoul_or_return(buf)) {
352 v = bch_cached_dev_run(dc);
357 if (attr == &sysfs_cache_mode) {
358 v = __sysfs_match_string(bch_cache_modes, -1, buf);
362 if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) {
363 SET_BDEV_CACHE_MODE(&dc->sb, v);
364 bch_write_bdev_super(dc, NULL);
368 if (attr == &sysfs_readahead_cache_policy) {
369 v = __sysfs_match_string(bch_reada_cache_policies, -1, buf);
373 if ((unsigned int) v != dc->cache_readahead_policy)
374 dc->cache_readahead_policy = v;
377 if (attr == &sysfs_stop_when_cache_set_failed) {
378 v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
382 dc->stop_when_cache_set_failed = v;
385 if (attr == &sysfs_label) {
386 if (size > SB_LABEL_SIZE)
388 memcpy(dc->sb.label, buf, size);
389 if (size < SB_LABEL_SIZE)
390 dc->sb.label[size] = '\0';
391 if (size && dc->sb.label[size - 1] == '\n')
392 dc->sb.label[size - 1] = '\0';
393 bch_write_bdev_super(dc, NULL);
395 memcpy(dc->disk.c->uuids[dc->disk.id].label,
397 bch_uuid_write(dc->disk.c);
399 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
402 add_uevent_var(env, "DRIVER=bcache");
403 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
404 add_uevent_var(env, "CACHED_LABEL=%s", buf);
405 kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj,
411 if (attr == &sysfs_attach) {
412 uint8_t set_uuid[16];
414 if (bch_parse_uuid(buf, set_uuid) < 16)
418 list_for_each_entry(c, &bch_cache_sets, list) {
419 v = bch_cached_dev_attach(dc, c, set_uuid);
424 pr_err("Can't attach %s: cache set not found", buf);
428 if (attr == &sysfs_detach && dc->disk.c)
429 bch_cached_dev_detach(dc);
431 if (attr == &sysfs_stop)
432 bcache_device_stop(&dc->disk);
437 STORE(bch_cached_dev)
439 struct cached_dev *dc = container_of(kobj, struct cached_dev,
442 /* no user space access if system is rebooting */
443 if (bcache_is_reboot)
446 mutex_lock(&bch_register_lock);
447 size = __cached_dev_store(kobj, attr, buf, size);
449 if (attr == &sysfs_writeback_running) {
450 /* dc->writeback_running changed in __cached_dev_store() */
451 if (IS_ERR_OR_NULL(dc->writeback_thread)) {
453 * reject setting it to 1 via sysfs if writeback
454 * kthread is not created yet.
456 if (dc->writeback_running) {
457 dc->writeback_running = false;
458 pr_err("%s: failed to run non-existent writeback thread",
459 dc->disk.disk->disk_name);
463 * writeback kthread will check if dc->writeback_running
466 bch_writeback_queue(dc);
470 * Only set BCACHE_DEV_WB_RUNNING when cached device attached to
471 * a cache set, otherwise it doesn't make sense.
473 if (attr == &sysfs_writeback_percent)
474 if ((dc->disk.c != NULL) &&
475 (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)))
476 schedule_delayed_work(&dc->writeback_rate_update,
477 dc->writeback_rate_update_seconds * HZ);
479 mutex_unlock(&bch_register_lock);
483 static struct attribute *bch_cached_dev_files[] = {
491 &sysfs_readahead_cache_policy,
492 &sysfs_stop_when_cache_set_failed,
493 &sysfs_writeback_metadata,
494 &sysfs_writeback_running,
495 &sysfs_writeback_delay,
496 &sysfs_writeback_percent,
497 &sysfs_writeback_rate,
498 &sysfs_writeback_rate_update_seconds,
499 &sysfs_writeback_rate_i_term_inverse,
500 &sysfs_writeback_rate_p_term_inverse,
501 &sysfs_writeback_rate_minimum,
502 &sysfs_writeback_rate_debug,
504 &sysfs_io_error_limit,
508 &sysfs_partial_stripes_expensive,
509 &sysfs_sequential_cutoff,
515 #ifdef CONFIG_BCACHE_DEBUG
517 &sysfs_bypass_torture_test,
519 &sysfs_backing_dev_name,
520 &sysfs_backing_dev_uuid,
523 KTYPE(bch_cached_dev);
527 struct bcache_device *d = container_of(kobj, struct bcache_device,
529 struct uuid_entry *u = &d->c->uuids[d->id];
531 sysfs_printf(data_csum, "%i", d->data_csum);
532 sysfs_hprint(size, u->sectors << 9);
534 if (attr == &sysfs_label) {
535 memcpy(buf, u->label, SB_LABEL_SIZE);
536 buf[SB_LABEL_SIZE + 1] = '\0';
544 STORE(__bch_flash_dev)
546 struct bcache_device *d = container_of(kobj, struct bcache_device,
548 struct uuid_entry *u = &d->c->uuids[d->id];
550 /* no user space access if system is rebooting */
551 if (bcache_is_reboot)
554 sysfs_strtoul(data_csum, d->data_csum);
556 if (attr == &sysfs_size) {
559 strtoi_h_or_return(buf, v);
562 bch_uuid_write(d->c);
563 set_capacity(d->disk, u->sectors);
566 if (attr == &sysfs_label) {
567 memcpy(u->label, buf, SB_LABEL_SIZE);
568 bch_uuid_write(d->c);
571 if (attr == &sysfs_unregister) {
572 set_bit(BCACHE_DEV_DETACHING, &d->flags);
573 bcache_device_stop(d);
578 STORE_LOCKED(bch_flash_dev)
580 static struct attribute *bch_flash_dev_files[] = {
589 KTYPE(bch_flash_dev);
591 struct bset_stats_op {
594 struct bset_stats stats;
597 static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
599 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
602 bch_btree_keys_stats(&b->keys, &op->stats);
607 static int bch_bset_print_stats(struct cache_set *c, char *buf)
609 struct bset_stats_op op;
612 memset(&op, 0, sizeof(op));
613 bch_btree_op_init(&op.op, -1);
615 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
619 return snprintf(buf, PAGE_SIZE,
621 "written sets: %zu\n"
622 "unwritten sets: %zu\n"
623 "written key bytes: %zu\n"
624 "unwritten key bytes: %zu\n"
628 op.stats.sets_written, op.stats.sets_unwritten,
629 op.stats.bytes_written, op.stats.bytes_unwritten,
630 op.stats.floats, op.stats.failed);
633 static unsigned int bch_root_usage(struct cache_set *c)
635 unsigned int bytes = 0;
638 struct btree_iter iter;
646 rw_lock(false, b, b->level);
647 } while (b != c->root);
649 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
650 bytes += bkey_bytes(k);
654 return (bytes * 100) / btree_bytes(c);
657 static size_t bch_cache_size(struct cache_set *c)
662 mutex_lock(&c->bucket_lock);
663 list_for_each_entry(b, &c->btree_cache, list)
664 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
666 mutex_unlock(&c->bucket_lock);
670 static unsigned int bch_cache_max_chain(struct cache_set *c)
672 unsigned int ret = 0;
673 struct hlist_head *h;
675 mutex_lock(&c->bucket_lock);
677 for (h = c->bucket_hash;
678 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
681 struct hlist_node *p;
689 mutex_unlock(&c->bucket_lock);
693 static unsigned int bch_btree_used(struct cache_set *c)
695 return div64_u64(c->gc_stats.key_bytes * 100,
696 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
699 static unsigned int bch_average_key_size(struct cache_set *c)
701 return c->gc_stats.nkeys
702 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
706 SHOW(__bch_cache_set)
708 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
710 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
711 sysfs_print(journal_delay_ms, c->journal_delay_ms);
712 sysfs_hprint(bucket_size, bucket_bytes(c));
713 sysfs_hprint(block_size, block_bytes(c));
714 sysfs_print(tree_depth, c->root->level);
715 sysfs_print(root_usage_percent, bch_root_usage(c));
717 sysfs_hprint(btree_cache_size, bch_cache_size(c));
718 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
719 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
721 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
722 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
723 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
724 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
726 sysfs_print(btree_used_percent, bch_btree_used(c));
727 sysfs_print(btree_nodes, c->gc_stats.nodes);
728 sysfs_hprint(average_key_size, bch_average_key_size(c));
730 sysfs_print(cache_read_races,
731 atomic_long_read(&c->cache_read_races));
734 atomic_long_read(&c->reclaim));
736 sysfs_print(reclaimed_journal_buckets,
737 atomic_long_read(&c->reclaimed_journal_buckets));
739 sysfs_print(flush_write,
740 atomic_long_read(&c->flush_write));
742 sysfs_print(writeback_keys_done,
743 atomic_long_read(&c->writeback_keys_done));
744 sysfs_print(writeback_keys_failed,
745 atomic_long_read(&c->writeback_keys_failed));
747 if (attr == &sysfs_errors)
748 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
751 /* See count_io_errors for why 88 */
752 sysfs_print(io_error_halflife, c->error_decay * 88);
753 sysfs_print(io_error_limit, c->error_limit);
755 sysfs_hprint(congested,
756 ((uint64_t) bch_get_congested(c)) << 9);
757 sysfs_print(congested_read_threshold_us,
758 c->congested_read_threshold_us);
759 sysfs_print(congested_write_threshold_us,
760 c->congested_write_threshold_us);
762 sysfs_print(cutoff_writeback, bch_cutoff_writeback);
763 sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync);
765 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
766 sysfs_printf(verify, "%i", c->verify);
767 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
768 sysfs_printf(expensive_debug_checks,
769 "%i", c->expensive_debug_checks);
770 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
771 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
772 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
773 sysfs_printf(idle_max_writeback_rate, "%i",
774 c->idle_max_writeback_rate_enabled);
775 sysfs_printf(gc_after_writeback, "%i", c->gc_after_writeback);
776 sysfs_printf(io_disable, "%i",
777 test_bit(CACHE_SET_IO_DISABLE, &c->flags));
779 if (attr == &sysfs_bset_tree_stats)
780 return bch_bset_print_stats(c, buf);
784 SHOW_LOCKED(bch_cache_set)
786 STORE(__bch_cache_set)
788 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
791 /* no user space access if system is rebooting */
792 if (bcache_is_reboot)
795 if (attr == &sysfs_unregister)
796 bch_cache_set_unregister(c);
798 if (attr == &sysfs_stop)
799 bch_cache_set_stop(c);
801 if (attr == &sysfs_synchronous) {
802 bool sync = strtoul_or_return(buf);
804 if (sync != CACHE_SYNC(&c->sb)) {
805 SET_CACHE_SYNC(&c->sb, sync);
806 bcache_write_super(c);
810 if (attr == &sysfs_flash_vol_create) {
814 strtoi_h_or_return(buf, v);
816 r = bch_flash_dev_create(c, v);
821 if (attr == &sysfs_clear_stats) {
822 atomic_long_set(&c->writeback_keys_done, 0);
823 atomic_long_set(&c->writeback_keys_failed, 0);
825 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
826 bch_cache_accounting_clear(&c->accounting);
829 if (attr == &sysfs_trigger_gc)
832 if (attr == &sysfs_prune_cache) {
833 struct shrink_control sc;
835 sc.gfp_mask = GFP_KERNEL;
836 sc.nr_to_scan = strtoul_or_return(buf);
837 c->shrink.scan_objects(&c->shrink, &sc);
840 sysfs_strtoul_clamp(congested_read_threshold_us,
841 c->congested_read_threshold_us,
843 sysfs_strtoul_clamp(congested_write_threshold_us,
844 c->congested_write_threshold_us,
847 if (attr == &sysfs_errors) {
848 v = __sysfs_match_string(error_actions, -1, buf);
855 sysfs_strtoul_clamp(io_error_limit, c->error_limit, 0, UINT_MAX);
857 /* See count_io_errors() for why 88 */
858 if (attr == &sysfs_io_error_halflife) {
862 ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX);
864 c->error_decay = v / 88;
870 if (attr == &sysfs_io_disable) {
871 v = strtoul_or_return(buf);
873 if (test_and_set_bit(CACHE_SET_IO_DISABLE,
875 pr_warn("CACHE_SET_IO_DISABLE already set");
877 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
879 pr_warn("CACHE_SET_IO_DISABLE already cleared");
883 sysfs_strtoul_clamp(journal_delay_ms,
886 sysfs_strtoul_bool(verify, c->verify);
887 sysfs_strtoul_bool(key_merging_disabled, c->key_merging_disabled);
888 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
889 sysfs_strtoul_bool(gc_always_rewrite, c->gc_always_rewrite);
890 sysfs_strtoul_bool(btree_shrinker_disabled, c->shrinker_disabled);
891 sysfs_strtoul_bool(copy_gc_enabled, c->copy_gc_enabled);
892 sysfs_strtoul_bool(idle_max_writeback_rate,
893 c->idle_max_writeback_rate_enabled);
896 * write gc_after_writeback here may overwrite an already set
897 * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
898 * set in next chance.
900 sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
904 STORE_LOCKED(bch_cache_set)
906 SHOW(bch_cache_set_internal)
908 struct cache_set *c = container_of(kobj, struct cache_set, internal);
910 return bch_cache_set_show(&c->kobj, attr, buf);
913 STORE(bch_cache_set_internal)
915 struct cache_set *c = container_of(kobj, struct cache_set, internal);
917 /* no user space access if system is rebooting */
918 if (bcache_is_reboot)
921 return bch_cache_set_store(&c->kobj, attr, buf, size);
924 static void bch_cache_set_internal_release(struct kobject *k)
928 static struct attribute *bch_cache_set_files[] = {
932 &sysfs_journal_delay_ms,
933 &sysfs_flash_vol_create,
938 &sysfs_root_usage_percent,
939 &sysfs_btree_cache_size,
940 &sysfs_cache_available_percent,
942 &sysfs_average_key_size,
945 &sysfs_io_error_limit,
946 &sysfs_io_error_halflife,
948 &sysfs_congested_read_threshold_us,
949 &sysfs_congested_write_threshold_us,
953 KTYPE(bch_cache_set);
955 static struct attribute *bch_cache_set_internal_files[] = {
956 &sysfs_active_journal_entries,
958 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
959 sysfs_time_stats_attribute_list(btree_split, sec, us)
960 sysfs_time_stats_attribute_list(btree_sort, ms, us)
961 sysfs_time_stats_attribute_list(btree_read, ms, us)
964 &sysfs_btree_used_percent,
965 &sysfs_btree_cache_max_chain,
967 &sysfs_bset_tree_stats,
968 &sysfs_cache_read_races,
970 &sysfs_reclaimed_journal_buckets,
972 &sysfs_writeback_keys_done,
973 &sysfs_writeback_keys_failed,
977 #ifdef CONFIG_BCACHE_DEBUG
979 &sysfs_key_merging_disabled,
980 &sysfs_expensive_debug_checks,
982 &sysfs_gc_always_rewrite,
983 &sysfs_btree_shrinker_disabled,
984 &sysfs_copy_gc_enabled,
985 &sysfs_idle_max_writeback_rate,
986 &sysfs_gc_after_writeback,
988 &sysfs_cutoff_writeback,
989 &sysfs_cutoff_writeback_sync,
992 KTYPE(bch_cache_set_internal);
994 static int __bch_cache_cmp(const void *l, const void *r)
997 return *((uint16_t *)r) - *((uint16_t *)l);
1002 struct cache *ca = container_of(kobj, struct cache, kobj);
1004 sysfs_hprint(bucket_size, bucket_bytes(ca));
1005 sysfs_hprint(block_size, block_bytes(ca));
1006 sysfs_print(nbuckets, ca->sb.nbuckets);
1007 sysfs_print(discard, ca->discard);
1008 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
1009 sysfs_hprint(btree_written,
1010 atomic_long_read(&ca->btree_sectors_written) << 9);
1011 sysfs_hprint(metadata_written,
1012 (atomic_long_read(&ca->meta_sectors_written) +
1013 atomic_long_read(&ca->btree_sectors_written)) << 9);
1015 sysfs_print(io_errors,
1016 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
1018 if (attr == &sysfs_cache_replacement_policy)
1019 return bch_snprint_string_list(buf, PAGE_SIZE,
1020 cache_replacement_policies,
1021 CACHE_REPLACEMENT(&ca->sb));
1023 if (attr == &sysfs_priority_stats) {
1025 size_t n = ca->sb.nbuckets, i;
1026 size_t unused = 0, available = 0, dirty = 0, meta = 0;
1028 /* Compute 31 quantiles */
1029 uint16_t q[31], *p, *cached;
1032 cached = p = vmalloc(array_size(sizeof(uint16_t),
1037 mutex_lock(&ca->set->bucket_lock);
1038 for_each_bucket(b, ca) {
1039 if (!GC_SECTORS_USED(b))
1041 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
1043 if (GC_MARK(b) == GC_MARK_DIRTY)
1045 if (GC_MARK(b) == GC_MARK_METADATA)
1049 for (i = ca->sb.first_bucket; i < n; i++)
1050 p[i] = ca->buckets[i].prio;
1051 mutex_unlock(&ca->set->bucket_lock);
1053 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
1059 while (cached < p + n &&
1060 *cached == BTREE_PRIO)
1063 for (i = 0; i < n; i++)
1064 sum += INITIAL_PRIO - cached[i];
1069 for (i = 0; i < ARRAY_SIZE(q); i++)
1070 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
1071 (ARRAY_SIZE(q) + 1)];
1075 ret = scnprintf(buf, PAGE_SIZE,
1081 "Sectors per Q: %zu\n"
1083 unused * 100 / (size_t) ca->sb.nbuckets,
1084 available * 100 / (size_t) ca->sb.nbuckets,
1085 dirty * 100 / (size_t) ca->sb.nbuckets,
1086 meta * 100 / (size_t) ca->sb.nbuckets, sum,
1087 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
1089 for (i = 0; i < ARRAY_SIZE(q); i++)
1090 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
1094 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
1101 SHOW_LOCKED(bch_cache)
1105 struct cache *ca = container_of(kobj, struct cache, kobj);
1108 /* no user space access if system is rebooting */
1109 if (bcache_is_reboot)
1112 if (attr == &sysfs_discard) {
1113 bool v = strtoul_or_return(buf);
1115 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1118 if (v != CACHE_DISCARD(&ca->sb)) {
1119 SET_CACHE_DISCARD(&ca->sb, v);
1120 bcache_write_super(ca->set);
1124 if (attr == &sysfs_cache_replacement_policy) {
1125 v = __sysfs_match_string(cache_replacement_policies, -1, buf);
1129 if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) {
1130 mutex_lock(&ca->set->bucket_lock);
1131 SET_CACHE_REPLACEMENT(&ca->sb, v);
1132 mutex_unlock(&ca->set->bucket_lock);
1134 bcache_write_super(ca->set);
1138 if (attr == &sysfs_clear_stats) {
1139 atomic_long_set(&ca->sectors_written, 0);
1140 atomic_long_set(&ca->btree_sectors_written, 0);
1141 atomic_long_set(&ca->meta_sectors_written, 0);
1142 atomic_set(&ca->io_count, 0);
1143 atomic_set(&ca->io_errors, 0);
1148 STORE_LOCKED(bch_cache)
1150 static struct attribute *bch_cache_files[] = {
1154 &sysfs_priority_stats,
1157 &sysfs_btree_written,
1158 &sysfs_metadata_written,
1161 &sysfs_cache_replacement_policy,