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(idle_max_writeback_rate
);
138 rw_attribute(gc_after_writeback
);
141 static ssize_t
bch_snprint_string_list(char *buf
,
143 const char * const list
[],
149 for (i
= 0; list
[i
]; i
++)
150 out
+= snprintf(out
, buf
+ size
- out
,
151 i
== selected
? "[%s] " : "%s ", list
[i
]);
157 SHOW(__bch_cached_dev
)
159 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
161 char const *states
[] = { "no cache", "clean", "dirty", "inconsistent" };
162 int wb
= dc
->writeback_running
;
164 #define var(stat) (dc->stat)
166 if (attr
== &sysfs_cache_mode
)
167 return bch_snprint_string_list(buf
, PAGE_SIZE
,
169 BDEV_CACHE_MODE(&dc
->sb
));
171 if (attr
== &sysfs_stop_when_cache_set_failed
)
172 return bch_snprint_string_list(buf
, PAGE_SIZE
,
173 bch_stop_on_failure_modes
,
174 dc
->stop_when_cache_set_failed
);
177 sysfs_printf(data_csum
, "%i", dc
->disk
.data_csum
);
178 var_printf(verify
, "%i");
179 var_printf(bypass_torture_test
, "%i");
180 var_printf(writeback_metadata
, "%i");
181 var_printf(writeback_running
, "%i");
182 var_print(writeback_delay
);
183 var_print(writeback_percent
);
184 sysfs_hprint(writeback_rate
,
185 wb
? atomic_long_read(&dc
->writeback_rate
.rate
) << 9 : 0);
186 sysfs_printf(io_errors
, "%i", atomic_read(&dc
->io_errors
));
187 sysfs_printf(io_error_limit
, "%i", dc
->error_limit
);
188 sysfs_printf(io_disable
, "%i", dc
->io_disable
);
189 var_print(writeback_rate_update_seconds
);
190 var_print(writeback_rate_i_term_inverse
);
191 var_print(writeback_rate_p_term_inverse
);
192 var_print(writeback_rate_minimum
);
194 if (attr
== &sysfs_writeback_rate_debug
) {
198 char proportional
[20];
204 * Except for dirty and target, other values should
205 * be 0 if writeback is not running.
208 wb
? atomic_long_read(&dc
->writeback_rate
.rate
) << 9
210 bch_hprint(dirty
, bcache_dev_sectors_dirty(&dc
->disk
) << 9);
211 bch_hprint(target
, dc
->writeback_rate_target
<< 9);
212 bch_hprint(proportional
,
213 wb
? dc
->writeback_rate_proportional
<< 9 : 0);
215 wb
? dc
->writeback_rate_integral_scaled
<< 9 : 0);
216 bch_hprint(change
, wb
? dc
->writeback_rate_change
<< 9 : 0);
217 next_io
= wb
? div64_s64(dc
->writeback_rate
.next
-local_clock(),
224 "proportional:\t%s\n"
226 "change:\t\t%s/sec\n"
227 "next io:\t%llims\n",
228 rate
, dirty
, target
, proportional
,
229 integral
, change
, next_io
);
232 sysfs_hprint(dirty_data
,
233 bcache_dev_sectors_dirty(&dc
->disk
) << 9);
235 sysfs_hprint(stripe_size
, ((uint64_t)dc
->disk
.stripe_size
) << 9);
236 var_printf(partial_stripes_expensive
, "%u");
238 var_hprint(sequential_cutoff
);
239 var_hprint(readahead
);
241 sysfs_print(running
, atomic_read(&dc
->running
));
242 sysfs_print(state
, states
[BDEV_STATE(&dc
->sb
)]);
244 if (attr
== &sysfs_label
) {
245 memcpy(buf
, dc
->sb
.label
, SB_LABEL_SIZE
);
246 buf
[SB_LABEL_SIZE
+ 1] = '\0';
251 if (attr
== &sysfs_backing_dev_name
) {
252 snprintf(buf
, BDEVNAME_SIZE
+ 1, "%s", dc
->backing_dev_name
);
257 if (attr
== &sysfs_backing_dev_uuid
) {
258 /* convert binary uuid into 36-byte string plus '\0' */
259 snprintf(buf
, 36+1, "%pU", dc
->sb
.uuid
);
267 SHOW_LOCKED(bch_cached_dev
)
271 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
275 struct kobj_uevent_env
*env
;
277 /* no user space access if system is rebooting */
278 if (bcache_is_reboot
)
281 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
282 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
283 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
285 sysfs_strtoul(data_csum
, dc
->disk
.data_csum
);
287 sysfs_strtoul_bool(bypass_torture_test
, dc
->bypass_torture_test
);
288 sysfs_strtoul_bool(writeback_metadata
, dc
->writeback_metadata
);
289 sysfs_strtoul_bool(writeback_running
, dc
->writeback_running
);
290 sysfs_strtoul_clamp(writeback_delay
, dc
->writeback_delay
, 0, UINT_MAX
);
292 sysfs_strtoul_clamp(writeback_percent
, dc
->writeback_percent
,
293 0, bch_cutoff_writeback
);
295 if (attr
== &sysfs_writeback_rate
) {
297 long int v
= atomic_long_read(&dc
->writeback_rate
.rate
);
299 ret
= strtoul_safe_clamp(buf
, v
, 1, INT_MAX
);
302 atomic_long_set(&dc
->writeback_rate
.rate
, v
);
309 sysfs_strtoul_clamp(writeback_rate_update_seconds
,
310 dc
->writeback_rate_update_seconds
,
311 1, WRITEBACK_RATE_UPDATE_SECS_MAX
);
312 sysfs_strtoul_clamp(writeback_rate_i_term_inverse
,
313 dc
->writeback_rate_i_term_inverse
,
315 sysfs_strtoul_clamp(writeback_rate_p_term_inverse
,
316 dc
->writeback_rate_p_term_inverse
,
318 sysfs_strtoul_clamp(writeback_rate_minimum
,
319 dc
->writeback_rate_minimum
,
322 sysfs_strtoul_clamp(io_error_limit
, dc
->error_limit
, 0, INT_MAX
);
324 if (attr
== &sysfs_io_disable
) {
325 int v
= strtoul_or_return(buf
);
327 dc
->io_disable
= v
? 1 : 0;
330 sysfs_strtoul_clamp(sequential_cutoff
,
331 dc
->sequential_cutoff
,
333 d_strtoi_h(readahead
);
335 if (attr
== &sysfs_clear_stats
)
336 bch_cache_accounting_clear(&dc
->accounting
);
338 if (attr
== &sysfs_running
&&
339 strtoul_or_return(buf
)) {
340 v
= bch_cached_dev_run(dc
);
345 if (attr
== &sysfs_cache_mode
) {
346 v
= __sysfs_match_string(bch_cache_modes
, -1, buf
);
350 if ((unsigned int) v
!= BDEV_CACHE_MODE(&dc
->sb
)) {
351 SET_BDEV_CACHE_MODE(&dc
->sb
, v
);
352 bch_write_bdev_super(dc
, NULL
);
356 if (attr
== &sysfs_stop_when_cache_set_failed
) {
357 v
= __sysfs_match_string(bch_stop_on_failure_modes
, -1, buf
);
361 dc
->stop_when_cache_set_failed
= v
;
364 if (attr
== &sysfs_label
) {
365 if (size
> SB_LABEL_SIZE
)
367 memcpy(dc
->sb
.label
, buf
, size
);
368 if (size
< SB_LABEL_SIZE
)
369 dc
->sb
.label
[size
] = '\0';
370 if (size
&& dc
->sb
.label
[size
- 1] == '\n')
371 dc
->sb
.label
[size
- 1] = '\0';
372 bch_write_bdev_super(dc
, NULL
);
374 memcpy(dc
->disk
.c
->uuids
[dc
->disk
.id
].label
,
376 bch_uuid_write(dc
->disk
.c
);
378 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
381 add_uevent_var(env
, "DRIVER=bcache");
382 add_uevent_var(env
, "CACHED_UUID=%pU", dc
->sb
.uuid
),
383 add_uevent_var(env
, "CACHED_LABEL=%s", buf
);
384 kobject_uevent_env(&disk_to_dev(dc
->disk
.disk
)->kobj
,
390 if (attr
== &sysfs_attach
) {
391 uint8_t set_uuid
[16];
393 if (bch_parse_uuid(buf
, set_uuid
) < 16)
397 list_for_each_entry(c
, &bch_cache_sets
, list
) {
398 v
= bch_cached_dev_attach(dc
, c
, set_uuid
);
403 pr_err("Can't attach %s: cache set not found", buf
);
407 if (attr
== &sysfs_detach
&& dc
->disk
.c
)
408 bch_cached_dev_detach(dc
);
410 if (attr
== &sysfs_stop
)
411 bcache_device_stop(&dc
->disk
);
416 STORE(bch_cached_dev
)
418 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
421 /* no user space access if system is rebooting */
422 if (bcache_is_reboot
)
425 mutex_lock(&bch_register_lock
);
426 size
= __cached_dev_store(kobj
, attr
, buf
, size
);
428 if (attr
== &sysfs_writeback_running
) {
429 /* dc->writeback_running changed in __cached_dev_store() */
430 if (IS_ERR_OR_NULL(dc
->writeback_thread
)) {
432 * reject setting it to 1 via sysfs if writeback
433 * kthread is not created yet.
435 if (dc
->writeback_running
) {
436 dc
->writeback_running
= false;
437 pr_err("%s: failed to run non-existent writeback thread",
438 dc
->disk
.disk
->disk_name
);
442 * writeback kthread will check if dc->writeback_running
445 bch_writeback_queue(dc
);
449 * Only set BCACHE_DEV_WB_RUNNING when cached device attached to
450 * a cache set, otherwise it doesn't make sense.
452 if (attr
== &sysfs_writeback_percent
)
453 if ((dc
->disk
.c
!= NULL
) &&
454 (!test_and_set_bit(BCACHE_DEV_WB_RUNNING
, &dc
->disk
.flags
)))
455 schedule_delayed_work(&dc
->writeback_rate_update
,
456 dc
->writeback_rate_update_seconds
* HZ
);
458 mutex_unlock(&bch_register_lock
);
462 static struct attribute
*bch_cached_dev_files
[] = {
470 &sysfs_stop_when_cache_set_failed
,
471 &sysfs_writeback_metadata
,
472 &sysfs_writeback_running
,
473 &sysfs_writeback_delay
,
474 &sysfs_writeback_percent
,
475 &sysfs_writeback_rate
,
476 &sysfs_writeback_rate_update_seconds
,
477 &sysfs_writeback_rate_i_term_inverse
,
478 &sysfs_writeback_rate_p_term_inverse
,
479 &sysfs_writeback_rate_minimum
,
480 &sysfs_writeback_rate_debug
,
482 &sysfs_io_error_limit
,
486 &sysfs_partial_stripes_expensive
,
487 &sysfs_sequential_cutoff
,
493 #ifdef CONFIG_BCACHE_DEBUG
495 &sysfs_bypass_torture_test
,
497 &sysfs_backing_dev_name
,
498 &sysfs_backing_dev_uuid
,
501 KTYPE(bch_cached_dev
);
505 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
507 struct uuid_entry
*u
= &d
->c
->uuids
[d
->id
];
509 sysfs_printf(data_csum
, "%i", d
->data_csum
);
510 sysfs_hprint(size
, u
->sectors
<< 9);
512 if (attr
== &sysfs_label
) {
513 memcpy(buf
, u
->label
, SB_LABEL_SIZE
);
514 buf
[SB_LABEL_SIZE
+ 1] = '\0';
522 STORE(__bch_flash_dev
)
524 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
526 struct uuid_entry
*u
= &d
->c
->uuids
[d
->id
];
528 /* no user space access if system is rebooting */
529 if (bcache_is_reboot
)
532 sysfs_strtoul(data_csum
, d
->data_csum
);
534 if (attr
== &sysfs_size
) {
537 strtoi_h_or_return(buf
, v
);
540 bch_uuid_write(d
->c
);
541 set_capacity(d
->disk
, u
->sectors
);
544 if (attr
== &sysfs_label
) {
545 memcpy(u
->label
, buf
, SB_LABEL_SIZE
);
546 bch_uuid_write(d
->c
);
549 if (attr
== &sysfs_unregister
) {
550 set_bit(BCACHE_DEV_DETACHING
, &d
->flags
);
551 bcache_device_stop(d
);
556 STORE_LOCKED(bch_flash_dev
)
558 static struct attribute
*bch_flash_dev_files
[] = {
567 KTYPE(bch_flash_dev
);
569 struct bset_stats_op
{
572 struct bset_stats stats
;
575 static int bch_btree_bset_stats(struct btree_op
*b_op
, struct btree
*b
)
577 struct bset_stats_op
*op
= container_of(b_op
, struct bset_stats_op
, op
);
580 bch_btree_keys_stats(&b
->keys
, &op
->stats
);
585 static int bch_bset_print_stats(struct cache_set
*c
, char *buf
)
587 struct bset_stats_op op
;
590 memset(&op
, 0, sizeof(op
));
591 bch_btree_op_init(&op
.op
, -1);
593 ret
= bch_btree_map_nodes(&op
.op
, c
, &ZERO_KEY
, bch_btree_bset_stats
);
597 return snprintf(buf
, PAGE_SIZE
,
599 "written sets: %zu\n"
600 "unwritten sets: %zu\n"
601 "written key bytes: %zu\n"
602 "unwritten key bytes: %zu\n"
606 op
.stats
.sets_written
, op
.stats
.sets_unwritten
,
607 op
.stats
.bytes_written
, op
.stats
.bytes_unwritten
,
608 op
.stats
.floats
, op
.stats
.failed
);
611 static unsigned int bch_root_usage(struct cache_set
*c
)
613 unsigned int bytes
= 0;
616 struct btree_iter iter
;
624 rw_lock(false, b
, b
->level
);
625 } while (b
!= c
->root
);
627 for_each_key_filter(&b
->keys
, k
, &iter
, bch_ptr_bad
)
628 bytes
+= bkey_bytes(k
);
632 return (bytes
* 100) / btree_bytes(c
);
635 static size_t bch_cache_size(struct cache_set
*c
)
640 mutex_lock(&c
->bucket_lock
);
641 list_for_each_entry(b
, &c
->btree_cache
, list
)
642 ret
+= 1 << (b
->keys
.page_order
+ PAGE_SHIFT
);
644 mutex_unlock(&c
->bucket_lock
);
648 static unsigned int bch_cache_max_chain(struct cache_set
*c
)
650 unsigned int ret
= 0;
651 struct hlist_head
*h
;
653 mutex_lock(&c
->bucket_lock
);
655 for (h
= c
->bucket_hash
;
656 h
< c
->bucket_hash
+ (1 << BUCKET_HASH_BITS
);
659 struct hlist_node
*p
;
667 mutex_unlock(&c
->bucket_lock
);
671 static unsigned int bch_btree_used(struct cache_set
*c
)
673 return div64_u64(c
->gc_stats
.key_bytes
* 100,
674 (c
->gc_stats
.nodes
?: 1) * btree_bytes(c
));
677 static unsigned int bch_average_key_size(struct cache_set
*c
)
679 return c
->gc_stats
.nkeys
680 ? div64_u64(c
->gc_stats
.data
, c
->gc_stats
.nkeys
)
684 SHOW(__bch_cache_set
)
686 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
688 sysfs_print(synchronous
, CACHE_SYNC(&c
->sb
));
689 sysfs_print(journal_delay_ms
, c
->journal_delay_ms
);
690 sysfs_hprint(bucket_size
, bucket_bytes(c
));
691 sysfs_hprint(block_size
, block_bytes(c
));
692 sysfs_print(tree_depth
, c
->root
->level
);
693 sysfs_print(root_usage_percent
, bch_root_usage(c
));
695 sysfs_hprint(btree_cache_size
, bch_cache_size(c
));
696 sysfs_print(btree_cache_max_chain
, bch_cache_max_chain(c
));
697 sysfs_print(cache_available_percent
, 100 - c
->gc_stats
.in_use
);
699 sysfs_print_time_stats(&c
->btree_gc_time
, btree_gc
, sec
, ms
);
700 sysfs_print_time_stats(&c
->btree_split_time
, btree_split
, sec
, us
);
701 sysfs_print_time_stats(&c
->sort
.time
, btree_sort
, ms
, us
);
702 sysfs_print_time_stats(&c
->btree_read_time
, btree_read
, ms
, us
);
704 sysfs_print(btree_used_percent
, bch_btree_used(c
));
705 sysfs_print(btree_nodes
, c
->gc_stats
.nodes
);
706 sysfs_hprint(average_key_size
, bch_average_key_size(c
));
708 sysfs_print(cache_read_races
,
709 atomic_long_read(&c
->cache_read_races
));
712 atomic_long_read(&c
->reclaim
));
714 sysfs_print(reclaimed_journal_buckets
,
715 atomic_long_read(&c
->reclaimed_journal_buckets
));
717 sysfs_print(flush_write
,
718 atomic_long_read(&c
->flush_write
));
720 sysfs_print(writeback_keys_done
,
721 atomic_long_read(&c
->writeback_keys_done
));
722 sysfs_print(writeback_keys_failed
,
723 atomic_long_read(&c
->writeback_keys_failed
));
725 if (attr
== &sysfs_errors
)
726 return bch_snprint_string_list(buf
, PAGE_SIZE
, error_actions
,
729 /* See count_io_errors for why 88 */
730 sysfs_print(io_error_halflife
, c
->error_decay
* 88);
731 sysfs_print(io_error_limit
, c
->error_limit
);
733 sysfs_hprint(congested
,
734 ((uint64_t) bch_get_congested(c
)) << 9);
735 sysfs_print(congested_read_threshold_us
,
736 c
->congested_read_threshold_us
);
737 sysfs_print(congested_write_threshold_us
,
738 c
->congested_write_threshold_us
);
740 sysfs_print(cutoff_writeback
, bch_cutoff_writeback
);
741 sysfs_print(cutoff_writeback_sync
, bch_cutoff_writeback_sync
);
743 sysfs_print(active_journal_entries
, fifo_used(&c
->journal
.pin
));
744 sysfs_printf(verify
, "%i", c
->verify
);
745 sysfs_printf(key_merging_disabled
, "%i", c
->key_merging_disabled
);
746 sysfs_printf(expensive_debug_checks
,
747 "%i", c
->expensive_debug_checks
);
748 sysfs_printf(gc_always_rewrite
, "%i", c
->gc_always_rewrite
);
749 sysfs_printf(btree_shrinker_disabled
, "%i", c
->shrinker_disabled
);
750 sysfs_printf(copy_gc_enabled
, "%i", c
->copy_gc_enabled
);
751 sysfs_printf(idle_max_writeback_rate
, "%i",
752 c
->idle_max_writeback_rate_enabled
);
753 sysfs_printf(gc_after_writeback
, "%i", c
->gc_after_writeback
);
754 sysfs_printf(io_disable
, "%i",
755 test_bit(CACHE_SET_IO_DISABLE
, &c
->flags
));
757 if (attr
== &sysfs_bset_tree_stats
)
758 return bch_bset_print_stats(c
, buf
);
762 SHOW_LOCKED(bch_cache_set
)
764 STORE(__bch_cache_set
)
766 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
769 /* no user space access if system is rebooting */
770 if (bcache_is_reboot
)
773 if (attr
== &sysfs_unregister
)
774 bch_cache_set_unregister(c
);
776 if (attr
== &sysfs_stop
)
777 bch_cache_set_stop(c
);
779 if (attr
== &sysfs_synchronous
) {
780 bool sync
= strtoul_or_return(buf
);
782 if (sync
!= CACHE_SYNC(&c
->sb
)) {
783 SET_CACHE_SYNC(&c
->sb
, sync
);
784 bcache_write_super(c
);
788 if (attr
== &sysfs_flash_vol_create
) {
792 strtoi_h_or_return(buf
, v
);
794 r
= bch_flash_dev_create(c
, v
);
799 if (attr
== &sysfs_clear_stats
) {
800 atomic_long_set(&c
->writeback_keys_done
, 0);
801 atomic_long_set(&c
->writeback_keys_failed
, 0);
803 memset(&c
->gc_stats
, 0, sizeof(struct gc_stat
));
804 bch_cache_accounting_clear(&c
->accounting
);
807 if (attr
== &sysfs_trigger_gc
)
810 if (attr
== &sysfs_prune_cache
) {
811 struct shrink_control sc
;
813 sc
.gfp_mask
= GFP_KERNEL
;
814 sc
.nr_to_scan
= strtoul_or_return(buf
);
815 c
->shrink
.scan_objects(&c
->shrink
, &sc
);
818 sysfs_strtoul_clamp(congested_read_threshold_us
,
819 c
->congested_read_threshold_us
,
821 sysfs_strtoul_clamp(congested_write_threshold_us
,
822 c
->congested_write_threshold_us
,
825 if (attr
== &sysfs_errors
) {
826 v
= __sysfs_match_string(error_actions
, -1, buf
);
833 sysfs_strtoul_clamp(io_error_limit
, c
->error_limit
, 0, UINT_MAX
);
835 /* See count_io_errors() for why 88 */
836 if (attr
== &sysfs_io_error_halflife
) {
840 ret
= strtoul_safe_clamp(buf
, v
, 0, UINT_MAX
);
842 c
->error_decay
= v
/ 88;
848 if (attr
== &sysfs_io_disable
) {
849 v
= strtoul_or_return(buf
);
851 if (test_and_set_bit(CACHE_SET_IO_DISABLE
,
853 pr_warn("CACHE_SET_IO_DISABLE already set");
855 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE
,
857 pr_warn("CACHE_SET_IO_DISABLE already cleared");
861 sysfs_strtoul_clamp(journal_delay_ms
,
864 sysfs_strtoul_bool(verify
, c
->verify
);
865 sysfs_strtoul_bool(key_merging_disabled
, c
->key_merging_disabled
);
866 sysfs_strtoul(expensive_debug_checks
, c
->expensive_debug_checks
);
867 sysfs_strtoul_bool(gc_always_rewrite
, c
->gc_always_rewrite
);
868 sysfs_strtoul_bool(btree_shrinker_disabled
, c
->shrinker_disabled
);
869 sysfs_strtoul_bool(copy_gc_enabled
, c
->copy_gc_enabled
);
870 sysfs_strtoul_bool(idle_max_writeback_rate
,
871 c
->idle_max_writeback_rate_enabled
);
874 * write gc_after_writeback here may overwrite an already set
875 * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
876 * set in next chance.
878 sysfs_strtoul_clamp(gc_after_writeback
, c
->gc_after_writeback
, 0, 1);
882 STORE_LOCKED(bch_cache_set
)
884 SHOW(bch_cache_set_internal
)
886 struct cache_set
*c
= container_of(kobj
, struct cache_set
, internal
);
888 return bch_cache_set_show(&c
->kobj
, attr
, buf
);
891 STORE(bch_cache_set_internal
)
893 struct cache_set
*c
= container_of(kobj
, struct cache_set
, internal
);
895 /* no user space access if system is rebooting */
896 if (bcache_is_reboot
)
899 return bch_cache_set_store(&c
->kobj
, attr
, buf
, size
);
902 static void bch_cache_set_internal_release(struct kobject
*k
)
906 static struct attribute
*bch_cache_set_files
[] = {
910 &sysfs_journal_delay_ms
,
911 &sysfs_flash_vol_create
,
916 &sysfs_root_usage_percent
,
917 &sysfs_btree_cache_size
,
918 &sysfs_cache_available_percent
,
920 &sysfs_average_key_size
,
923 &sysfs_io_error_limit
,
924 &sysfs_io_error_halflife
,
926 &sysfs_congested_read_threshold_us
,
927 &sysfs_congested_write_threshold_us
,
931 KTYPE(bch_cache_set
);
933 static struct attribute
*bch_cache_set_internal_files
[] = {
934 &sysfs_active_journal_entries
,
936 sysfs_time_stats_attribute_list(btree_gc
, sec
, ms
)
937 sysfs_time_stats_attribute_list(btree_split
, sec
, us
)
938 sysfs_time_stats_attribute_list(btree_sort
, ms
, us
)
939 sysfs_time_stats_attribute_list(btree_read
, ms
, us
)
942 &sysfs_btree_used_percent
,
943 &sysfs_btree_cache_max_chain
,
945 &sysfs_bset_tree_stats
,
946 &sysfs_cache_read_races
,
948 &sysfs_reclaimed_journal_buckets
,
950 &sysfs_writeback_keys_done
,
951 &sysfs_writeback_keys_failed
,
955 #ifdef CONFIG_BCACHE_DEBUG
957 &sysfs_key_merging_disabled
,
958 &sysfs_expensive_debug_checks
,
960 &sysfs_gc_always_rewrite
,
961 &sysfs_btree_shrinker_disabled
,
962 &sysfs_copy_gc_enabled
,
963 &sysfs_idle_max_writeback_rate
,
964 &sysfs_gc_after_writeback
,
966 &sysfs_cutoff_writeback
,
967 &sysfs_cutoff_writeback_sync
,
970 KTYPE(bch_cache_set_internal
);
972 static int __bch_cache_cmp(const void *l
, const void *r
)
975 return *((uint16_t *)r
) - *((uint16_t *)l
);
980 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
982 sysfs_hprint(bucket_size
, bucket_bytes(ca
));
983 sysfs_hprint(block_size
, block_bytes(ca
));
984 sysfs_print(nbuckets
, ca
->sb
.nbuckets
);
985 sysfs_print(discard
, ca
->discard
);
986 sysfs_hprint(written
, atomic_long_read(&ca
->sectors_written
) << 9);
987 sysfs_hprint(btree_written
,
988 atomic_long_read(&ca
->btree_sectors_written
) << 9);
989 sysfs_hprint(metadata_written
,
990 (atomic_long_read(&ca
->meta_sectors_written
) +
991 atomic_long_read(&ca
->btree_sectors_written
)) << 9);
993 sysfs_print(io_errors
,
994 atomic_read(&ca
->io_errors
) >> IO_ERROR_SHIFT
);
996 if (attr
== &sysfs_cache_replacement_policy
)
997 return bch_snprint_string_list(buf
, PAGE_SIZE
,
998 cache_replacement_policies
,
999 CACHE_REPLACEMENT(&ca
->sb
));
1001 if (attr
== &sysfs_priority_stats
) {
1003 size_t n
= ca
->sb
.nbuckets
, i
;
1004 size_t unused
= 0, available
= 0, dirty
= 0, meta
= 0;
1006 /* Compute 31 quantiles */
1007 uint16_t q
[31], *p
, *cached
;
1010 cached
= p
= vmalloc(array_size(sizeof(uint16_t),
1015 mutex_lock(&ca
->set
->bucket_lock
);
1016 for_each_bucket(b
, ca
) {
1017 if (!GC_SECTORS_USED(b
))
1019 if (GC_MARK(b
) == GC_MARK_RECLAIMABLE
)
1021 if (GC_MARK(b
) == GC_MARK_DIRTY
)
1023 if (GC_MARK(b
) == GC_MARK_METADATA
)
1027 for (i
= ca
->sb
.first_bucket
; i
< n
; i
++)
1028 p
[i
] = ca
->buckets
[i
].prio
;
1029 mutex_unlock(&ca
->set
->bucket_lock
);
1031 sort(p
, n
, sizeof(uint16_t), __bch_cache_cmp
, NULL
);
1037 while (cached
< p
+ n
&&
1038 *cached
== BTREE_PRIO
)
1041 for (i
= 0; i
< n
; i
++)
1042 sum
+= INITIAL_PRIO
- cached
[i
];
1047 for (i
= 0; i
< ARRAY_SIZE(q
); i
++)
1048 q
[i
] = INITIAL_PRIO
- cached
[n
* (i
+ 1) /
1049 (ARRAY_SIZE(q
) + 1)];
1053 ret
= scnprintf(buf
, PAGE_SIZE
,
1059 "Sectors per Q: %zu\n"
1061 unused
* 100 / (size_t) ca
->sb
.nbuckets
,
1062 available
* 100 / (size_t) ca
->sb
.nbuckets
,
1063 dirty
* 100 / (size_t) ca
->sb
.nbuckets
,
1064 meta
* 100 / (size_t) ca
->sb
.nbuckets
, sum
,
1065 n
* ca
->sb
.bucket_size
/ (ARRAY_SIZE(q
) + 1));
1067 for (i
= 0; i
< ARRAY_SIZE(q
); i
++)
1068 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1072 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "]\n");
1079 SHOW_LOCKED(bch_cache
)
1083 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
1086 /* no user space access if system is rebooting */
1087 if (bcache_is_reboot
)
1090 if (attr
== &sysfs_discard
) {
1091 bool v
= strtoul_or_return(buf
);
1093 if (blk_queue_discard(bdev_get_queue(ca
->bdev
)))
1096 if (v
!= CACHE_DISCARD(&ca
->sb
)) {
1097 SET_CACHE_DISCARD(&ca
->sb
, v
);
1098 bcache_write_super(ca
->set
);
1102 if (attr
== &sysfs_cache_replacement_policy
) {
1103 v
= __sysfs_match_string(cache_replacement_policies
, -1, buf
);
1107 if ((unsigned int) v
!= CACHE_REPLACEMENT(&ca
->sb
)) {
1108 mutex_lock(&ca
->set
->bucket_lock
);
1109 SET_CACHE_REPLACEMENT(&ca
->sb
, v
);
1110 mutex_unlock(&ca
->set
->bucket_lock
);
1112 bcache_write_super(ca
->set
);
1116 if (attr
== &sysfs_clear_stats
) {
1117 atomic_long_set(&ca
->sectors_written
, 0);
1118 atomic_long_set(&ca
->btree_sectors_written
, 0);
1119 atomic_long_set(&ca
->meta_sectors_written
, 0);
1120 atomic_set(&ca
->io_count
, 0);
1121 atomic_set(&ca
->io_errors
, 0);
1126 STORE_LOCKED(bch_cache
)
1128 static struct attribute
*bch_cache_files
[] = {
1132 &sysfs_priority_stats
,
1135 &sysfs_btree_written
,
1136 &sysfs_metadata_written
,
1139 &sysfs_cache_replacement_policy
,