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 static const char * const cache_replacement_policies[] = {
26 static const char * const error_actions[] = {
32 write_attribute(attach);
33 write_attribute(detach);
34 write_attribute(unregister);
35 write_attribute(stop);
36 write_attribute(clear_stats);
37 write_attribute(trigger_gc);
38 write_attribute(prune_cache);
39 write_attribute(flash_vol_create);
41 read_attribute(bucket_size);
42 read_attribute(block_size);
43 read_attribute(nbuckets);
44 read_attribute(tree_depth);
45 read_attribute(root_usage_percent);
46 read_attribute(priority_stats);
47 read_attribute(btree_cache_size);
48 read_attribute(btree_cache_max_chain);
49 read_attribute(cache_available_percent);
50 read_attribute(written);
51 read_attribute(btree_written);
52 read_attribute(metadata_written);
53 read_attribute(active_journal_entries);
55 sysfs_time_stats_attribute(btree_gc, sec, ms);
56 sysfs_time_stats_attribute(btree_split, sec, us);
57 sysfs_time_stats_attribute(btree_sort, ms, us);
58 sysfs_time_stats_attribute(btree_read, ms, us);
60 read_attribute(btree_nodes);
61 read_attribute(btree_used_percent);
62 read_attribute(average_key_size);
63 read_attribute(dirty_data);
64 read_attribute(bset_tree_stats);
66 read_attribute(state);
67 read_attribute(cache_read_races);
68 read_attribute(reclaim);
69 read_attribute(flush_write);
70 read_attribute(retry_flush_write);
71 read_attribute(writeback_keys_done);
72 read_attribute(writeback_keys_failed);
73 read_attribute(io_errors);
74 read_attribute(congested);
75 rw_attribute(congested_read_threshold_us);
76 rw_attribute(congested_write_threshold_us);
78 rw_attribute(sequential_cutoff);
79 rw_attribute(data_csum);
80 rw_attribute(cache_mode);
81 rw_attribute(writeback_metadata);
82 rw_attribute(writeback_running);
83 rw_attribute(writeback_percent);
84 rw_attribute(writeback_delay);
85 rw_attribute(writeback_rate);
87 rw_attribute(writeback_rate_update_seconds);
88 rw_attribute(writeback_rate_i_term_inverse);
89 rw_attribute(writeback_rate_p_term_inverse);
90 rw_attribute(writeback_rate_minimum);
91 read_attribute(writeback_rate_debug);
93 read_attribute(stripe_size);
94 read_attribute(partial_stripes_expensive);
96 rw_attribute(synchronous);
97 rw_attribute(journal_delay_ms);
98 rw_attribute(discard);
99 rw_attribute(running);
101 rw_attribute(readahead);
102 rw_attribute(errors);
103 rw_attribute(io_error_limit);
104 rw_attribute(io_error_halflife);
105 rw_attribute(verify);
106 rw_attribute(bypass_torture_test);
107 rw_attribute(key_merging_disabled);
108 rw_attribute(gc_always_rewrite);
109 rw_attribute(expensive_debug_checks);
110 rw_attribute(cache_replacement_policy);
111 rw_attribute(btree_shrinker_disabled);
112 rw_attribute(copy_gc_enabled);
115 SHOW(__bch_cached_dev)
117 struct cached_dev *dc = container_of(kobj, struct cached_dev,
119 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
121 #define var(stat) (dc->stat)
123 if (attr == &sysfs_cache_mode)
124 return bch_snprint_string_list(buf, PAGE_SIZE,
126 BDEV_CACHE_MODE(&dc->sb));
128 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
129 var_printf(verify, "%i");
130 var_printf(bypass_torture_test, "%i");
131 var_printf(writeback_metadata, "%i");
132 var_printf(writeback_running, "%i");
133 var_print(writeback_delay);
134 var_print(writeback_percent);
135 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
137 var_print(writeback_rate_update_seconds);
138 var_print(writeback_rate_i_term_inverse);
139 var_print(writeback_rate_p_term_inverse);
140 var_print(writeback_rate_minimum);
142 if (attr == &sysfs_writeback_rate_debug) {
146 char proportional[20];
151 bch_hprint(rate, dc->writeback_rate.rate << 9);
152 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
153 bch_hprint(target, dc->writeback_rate_target << 9);
154 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
155 bch_hprint(integral, dc->writeback_rate_integral_scaled << 9);
156 bch_hprint(change, dc->writeback_rate_change << 9);
158 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
165 "proportional:\t%s\n"
167 "change:\t\t%s/sec\n"
168 "next io:\t%llims\n",
169 rate, dirty, target, proportional,
170 integral, change, next_io);
173 sysfs_hprint(dirty_data,
174 bcache_dev_sectors_dirty(&dc->disk) << 9);
176 sysfs_hprint(stripe_size, dc->disk.stripe_size << 9);
177 var_printf(partial_stripes_expensive, "%u");
179 var_hprint(sequential_cutoff);
180 var_hprint(readahead);
182 sysfs_print(running, atomic_read(&dc->running));
183 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
185 if (attr == &sysfs_label) {
186 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
187 buf[SB_LABEL_SIZE + 1] = '\0';
195 SHOW_LOCKED(bch_cached_dev)
199 struct cached_dev *dc = container_of(kobj, struct cached_dev,
203 struct kobj_uevent_env *env;
205 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
206 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
207 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
209 sysfs_strtoul(data_csum, dc->disk.data_csum);
211 d_strtoul(bypass_torture_test);
212 d_strtoul(writeback_metadata);
213 d_strtoul(writeback_running);
214 d_strtoul(writeback_delay);
216 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
218 sysfs_strtoul_clamp(writeback_rate,
219 dc->writeback_rate.rate, 1, INT_MAX);
221 d_strtoul_nonzero(writeback_rate_update_seconds);
222 d_strtoul(writeback_rate_i_term_inverse);
223 d_strtoul_nonzero(writeback_rate_p_term_inverse);
225 d_strtoi_h(sequential_cutoff);
226 d_strtoi_h(readahead);
228 if (attr == &sysfs_clear_stats)
229 bch_cache_accounting_clear(&dc->accounting);
231 if (attr == &sysfs_running &&
232 strtoul_or_return(buf))
233 bch_cached_dev_run(dc);
235 if (attr == &sysfs_cache_mode) {
236 v = bch_read_string_list(buf, bch_cache_modes + 1);
241 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
242 SET_BDEV_CACHE_MODE(&dc->sb, v);
243 bch_write_bdev_super(dc, NULL);
247 if (attr == &sysfs_label) {
248 if (size > SB_LABEL_SIZE)
250 memcpy(dc->sb.label, buf, size);
251 if (size < SB_LABEL_SIZE)
252 dc->sb.label[size] = '\0';
253 if (size && dc->sb.label[size - 1] == '\n')
254 dc->sb.label[size - 1] = '\0';
255 bch_write_bdev_super(dc, NULL);
257 memcpy(dc->disk.c->uuids[dc->disk.id].label,
259 bch_uuid_write(dc->disk.c);
261 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
264 add_uevent_var(env, "DRIVER=bcache");
265 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
266 add_uevent_var(env, "CACHED_LABEL=%s", buf);
268 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
272 if (attr == &sysfs_attach) {
273 if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
276 list_for_each_entry(c, &bch_cache_sets, list) {
277 v = bch_cached_dev_attach(dc, c);
282 pr_err("Can't attach %s: cache set not found", buf);
286 if (attr == &sysfs_detach && dc->disk.c)
287 bch_cached_dev_detach(dc);
289 if (attr == &sysfs_stop)
290 bcache_device_stop(&dc->disk);
295 STORE(bch_cached_dev)
297 struct cached_dev *dc = container_of(kobj, struct cached_dev,
300 mutex_lock(&bch_register_lock);
301 size = __cached_dev_store(kobj, attr, buf, size);
303 if (attr == &sysfs_writeback_running)
304 bch_writeback_queue(dc);
306 if (attr == &sysfs_writeback_percent)
307 schedule_delayed_work(&dc->writeback_rate_update,
308 dc->writeback_rate_update_seconds * HZ);
310 mutex_unlock(&bch_register_lock);
314 static struct attribute *bch_cached_dev_files[] = {
322 &sysfs_writeback_metadata,
323 &sysfs_writeback_running,
324 &sysfs_writeback_delay,
325 &sysfs_writeback_percent,
326 &sysfs_writeback_rate,
327 &sysfs_writeback_rate_update_seconds,
328 &sysfs_writeback_rate_i_term_inverse,
329 &sysfs_writeback_rate_p_term_inverse,
330 &sysfs_writeback_rate_debug,
333 &sysfs_partial_stripes_expensive,
334 &sysfs_sequential_cutoff,
340 #ifdef CONFIG_BCACHE_DEBUG
342 &sysfs_bypass_torture_test,
346 KTYPE(bch_cached_dev);
350 struct bcache_device *d = container_of(kobj, struct bcache_device,
352 struct uuid_entry *u = &d->c->uuids[d->id];
354 sysfs_printf(data_csum, "%i", d->data_csum);
355 sysfs_hprint(size, u->sectors << 9);
357 if (attr == &sysfs_label) {
358 memcpy(buf, u->label, SB_LABEL_SIZE);
359 buf[SB_LABEL_SIZE + 1] = '\0';
367 STORE(__bch_flash_dev)
369 struct bcache_device *d = container_of(kobj, struct bcache_device,
371 struct uuid_entry *u = &d->c->uuids[d->id];
373 sysfs_strtoul(data_csum, d->data_csum);
375 if (attr == &sysfs_size) {
377 strtoi_h_or_return(buf, v);
380 bch_uuid_write(d->c);
381 set_capacity(d->disk, u->sectors);
384 if (attr == &sysfs_label) {
385 memcpy(u->label, buf, SB_LABEL_SIZE);
386 bch_uuid_write(d->c);
389 if (attr == &sysfs_unregister) {
390 set_bit(BCACHE_DEV_DETACHING, &d->flags);
391 bcache_device_stop(d);
396 STORE_LOCKED(bch_flash_dev)
398 static struct attribute *bch_flash_dev_files[] = {
407 KTYPE(bch_flash_dev);
409 struct bset_stats_op {
412 struct bset_stats stats;
415 static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
417 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
420 bch_btree_keys_stats(&b->keys, &op->stats);
425 static int bch_bset_print_stats(struct cache_set *c, char *buf)
427 struct bset_stats_op op;
430 memset(&op, 0, sizeof(op));
431 bch_btree_op_init(&op.op, -1);
433 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
437 return snprintf(buf, PAGE_SIZE,
439 "written sets: %zu\n"
440 "unwritten sets: %zu\n"
441 "written key bytes: %zu\n"
442 "unwritten key bytes: %zu\n"
446 op.stats.sets_written, op.stats.sets_unwritten,
447 op.stats.bytes_written, op.stats.bytes_unwritten,
448 op.stats.floats, op.stats.failed);
451 static unsigned bch_root_usage(struct cache_set *c)
456 struct btree_iter iter;
464 rw_lock(false, b, b->level);
465 } while (b != c->root);
467 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
468 bytes += bkey_bytes(k);
472 return (bytes * 100) / btree_bytes(c);
475 static size_t bch_cache_size(struct cache_set *c)
480 mutex_lock(&c->bucket_lock);
481 list_for_each_entry(b, &c->btree_cache, list)
482 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
484 mutex_unlock(&c->bucket_lock);
488 static unsigned bch_cache_max_chain(struct cache_set *c)
491 struct hlist_head *h;
493 mutex_lock(&c->bucket_lock);
495 for (h = c->bucket_hash;
496 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
499 struct hlist_node *p;
507 mutex_unlock(&c->bucket_lock);
511 static unsigned bch_btree_used(struct cache_set *c)
513 return div64_u64(c->gc_stats.key_bytes * 100,
514 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
517 static unsigned bch_average_key_size(struct cache_set *c)
519 return c->gc_stats.nkeys
520 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
524 SHOW(__bch_cache_set)
526 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
528 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
529 sysfs_print(journal_delay_ms, c->journal_delay_ms);
530 sysfs_hprint(bucket_size, bucket_bytes(c));
531 sysfs_hprint(block_size, block_bytes(c));
532 sysfs_print(tree_depth, c->root->level);
533 sysfs_print(root_usage_percent, bch_root_usage(c));
535 sysfs_hprint(btree_cache_size, bch_cache_size(c));
536 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
537 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
539 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
540 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
541 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
542 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
544 sysfs_print(btree_used_percent, bch_btree_used(c));
545 sysfs_print(btree_nodes, c->gc_stats.nodes);
546 sysfs_hprint(average_key_size, bch_average_key_size(c));
548 sysfs_print(cache_read_races,
549 atomic_long_read(&c->cache_read_races));
552 atomic_long_read(&c->reclaim));
554 sysfs_print(flush_write,
555 atomic_long_read(&c->flush_write));
557 sysfs_print(retry_flush_write,
558 atomic_long_read(&c->retry_flush_write));
560 sysfs_print(writeback_keys_done,
561 atomic_long_read(&c->writeback_keys_done));
562 sysfs_print(writeback_keys_failed,
563 atomic_long_read(&c->writeback_keys_failed));
565 if (attr == &sysfs_errors)
566 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
569 /* See count_io_errors for why 88 */
570 sysfs_print(io_error_halflife, c->error_decay * 88);
571 sysfs_print(io_error_limit, c->error_limit >> IO_ERROR_SHIFT);
573 sysfs_hprint(congested,
574 ((uint64_t) bch_get_congested(c)) << 9);
575 sysfs_print(congested_read_threshold_us,
576 c->congested_read_threshold_us);
577 sysfs_print(congested_write_threshold_us,
578 c->congested_write_threshold_us);
580 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
581 sysfs_printf(verify, "%i", c->verify);
582 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
583 sysfs_printf(expensive_debug_checks,
584 "%i", c->expensive_debug_checks);
585 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
586 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
587 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
589 if (attr == &sysfs_bset_tree_stats)
590 return bch_bset_print_stats(c, buf);
594 SHOW_LOCKED(bch_cache_set)
596 STORE(__bch_cache_set)
598 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
600 if (attr == &sysfs_unregister)
601 bch_cache_set_unregister(c);
603 if (attr == &sysfs_stop)
604 bch_cache_set_stop(c);
606 if (attr == &sysfs_synchronous) {
607 bool sync = strtoul_or_return(buf);
609 if (sync != CACHE_SYNC(&c->sb)) {
610 SET_CACHE_SYNC(&c->sb, sync);
611 bcache_write_super(c);
615 if (attr == &sysfs_flash_vol_create) {
618 strtoi_h_or_return(buf, v);
620 r = bch_flash_dev_create(c, v);
625 if (attr == &sysfs_clear_stats) {
626 atomic_long_set(&c->writeback_keys_done, 0);
627 atomic_long_set(&c->writeback_keys_failed, 0);
629 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
630 bch_cache_accounting_clear(&c->accounting);
633 if (attr == &sysfs_trigger_gc) {
635 * Garbage collection thread only works when sectors_to_gc < 0,
636 * when users write to sysfs entry trigger_gc, most of time
637 * they want to forcibly triger gargage collection. Here -1 is
638 * set to c->sectors_to_gc, to make gc_should_run() give a
639 * chance to permit gc thread to run. "give a chance" means
640 * before going into gc_should_run(), there is still chance
641 * that c->sectors_to_gc being set to other positive value. So
642 * writing sysfs entry trigger_gc won't always make sure gc
643 * thread takes effect.
645 atomic_set(&c->sectors_to_gc, -1);
649 if (attr == &sysfs_prune_cache) {
650 struct shrink_control sc;
651 sc.gfp_mask = GFP_KERNEL;
652 sc.nr_to_scan = strtoul_or_return(buf);
653 c->shrink.scan_objects(&c->shrink, &sc);
656 sysfs_strtoul(congested_read_threshold_us,
657 c->congested_read_threshold_us);
658 sysfs_strtoul(congested_write_threshold_us,
659 c->congested_write_threshold_us);
661 if (attr == &sysfs_errors) {
662 ssize_t v = bch_read_string_list(buf, error_actions);
670 if (attr == &sysfs_io_error_limit)
671 c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
673 /* See count_io_errors() for why 88 */
674 if (attr == &sysfs_io_error_halflife)
675 c->error_decay = strtoul_or_return(buf) / 88;
677 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
678 sysfs_strtoul(verify, c->verify);
679 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
680 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
681 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
682 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
683 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
687 STORE_LOCKED(bch_cache_set)
689 SHOW(bch_cache_set_internal)
691 struct cache_set *c = container_of(kobj, struct cache_set, internal);
692 return bch_cache_set_show(&c->kobj, attr, buf);
695 STORE(bch_cache_set_internal)
697 struct cache_set *c = container_of(kobj, struct cache_set, internal);
698 return bch_cache_set_store(&c->kobj, attr, buf, size);
701 static void bch_cache_set_internal_release(struct kobject *k)
705 static struct attribute *bch_cache_set_files[] = {
709 &sysfs_journal_delay_ms,
710 &sysfs_flash_vol_create,
715 &sysfs_root_usage_percent,
716 &sysfs_btree_cache_size,
717 &sysfs_cache_available_percent,
719 &sysfs_average_key_size,
722 &sysfs_io_error_limit,
723 &sysfs_io_error_halflife,
725 &sysfs_congested_read_threshold_us,
726 &sysfs_congested_write_threshold_us,
730 KTYPE(bch_cache_set);
732 static struct attribute *bch_cache_set_internal_files[] = {
733 &sysfs_active_journal_entries,
735 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
736 sysfs_time_stats_attribute_list(btree_split, sec, us)
737 sysfs_time_stats_attribute_list(btree_sort, ms, us)
738 sysfs_time_stats_attribute_list(btree_read, ms, us)
741 &sysfs_btree_used_percent,
742 &sysfs_btree_cache_max_chain,
744 &sysfs_bset_tree_stats,
745 &sysfs_cache_read_races,
748 &sysfs_retry_flush_write,
749 &sysfs_writeback_keys_done,
750 &sysfs_writeback_keys_failed,
754 #ifdef CONFIG_BCACHE_DEBUG
756 &sysfs_key_merging_disabled,
757 &sysfs_expensive_debug_checks,
759 &sysfs_gc_always_rewrite,
760 &sysfs_btree_shrinker_disabled,
761 &sysfs_copy_gc_enabled,
764 KTYPE(bch_cache_set_internal);
766 static int __bch_cache_cmp(const void *l, const void *r)
768 return *((uint16_t *)r) - *((uint16_t *)l);
773 struct cache *ca = container_of(kobj, struct cache, kobj);
775 sysfs_hprint(bucket_size, bucket_bytes(ca));
776 sysfs_hprint(block_size, block_bytes(ca));
777 sysfs_print(nbuckets, ca->sb.nbuckets);
778 sysfs_print(discard, ca->discard);
779 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
780 sysfs_hprint(btree_written,
781 atomic_long_read(&ca->btree_sectors_written) << 9);
782 sysfs_hprint(metadata_written,
783 (atomic_long_read(&ca->meta_sectors_written) +
784 atomic_long_read(&ca->btree_sectors_written)) << 9);
786 sysfs_print(io_errors,
787 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
789 if (attr == &sysfs_cache_replacement_policy)
790 return bch_snprint_string_list(buf, PAGE_SIZE,
791 cache_replacement_policies,
792 CACHE_REPLACEMENT(&ca->sb));
794 if (attr == &sysfs_priority_stats) {
796 size_t n = ca->sb.nbuckets, i;
797 size_t unused = 0, available = 0, dirty = 0, meta = 0;
799 /* Compute 31 quantiles */
800 uint16_t q[31], *p, *cached;
803 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
807 mutex_lock(&ca->set->bucket_lock);
808 for_each_bucket(b, ca) {
809 if (!GC_SECTORS_USED(b))
811 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
813 if (GC_MARK(b) == GC_MARK_DIRTY)
815 if (GC_MARK(b) == GC_MARK_METADATA)
819 for (i = ca->sb.first_bucket; i < n; i++)
820 p[i] = ca->buckets[i].prio;
821 mutex_unlock(&ca->set->bucket_lock);
823 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
829 unused = ca->sb.nbuckets - n;
831 while (cached < p + n &&
832 *cached == BTREE_PRIO)
835 for (i = 0; i < n; i++)
836 sum += INITIAL_PRIO - cached[i];
841 for (i = 0; i < ARRAY_SIZE(q); i++)
842 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
843 (ARRAY_SIZE(q) + 1)];
847 ret = scnprintf(buf, PAGE_SIZE,
853 "Sectors per Q: %zu\n"
855 unused * 100 / (size_t) ca->sb.nbuckets,
856 available * 100 / (size_t) ca->sb.nbuckets,
857 dirty * 100 / (size_t) ca->sb.nbuckets,
858 meta * 100 / (size_t) ca->sb.nbuckets, sum,
859 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
861 for (i = 0; i < ARRAY_SIZE(q); i++)
862 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
866 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
873 SHOW_LOCKED(bch_cache)
877 struct cache *ca = container_of(kobj, struct cache, kobj);
879 if (attr == &sysfs_discard) {
880 bool v = strtoul_or_return(buf);
882 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
885 if (v != CACHE_DISCARD(&ca->sb)) {
886 SET_CACHE_DISCARD(&ca->sb, v);
887 bcache_write_super(ca->set);
891 if (attr == &sysfs_cache_replacement_policy) {
892 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
897 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
898 mutex_lock(&ca->set->bucket_lock);
899 SET_CACHE_REPLACEMENT(&ca->sb, v);
900 mutex_unlock(&ca->set->bucket_lock);
902 bcache_write_super(ca->set);
906 if (attr == &sysfs_clear_stats) {
907 atomic_long_set(&ca->sectors_written, 0);
908 atomic_long_set(&ca->btree_sectors_written, 0);
909 atomic_long_set(&ca->meta_sectors_written, 0);
910 atomic_set(&ca->io_count, 0);
911 atomic_set(&ca->io_errors, 0);
916 STORE_LOCKED(bch_cache)
918 static struct attribute *bch_cache_files[] = {
922 &sysfs_priority_stats,
925 &sysfs_btree_written,
926 &sysfs_metadata_written,
929 &sysfs_cache_replacement_policy,