2 * bcache sysfs interfaces
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
12 #include "writeback.h"
14 #include <linux/blkdev.h>
15 #include <linux/sort.h>
17 static const char * const cache_replacement_policies
[] = {
24 static const char * const error_actions
[] = {
30 write_attribute(attach
);
31 write_attribute(detach
);
32 write_attribute(unregister
);
33 write_attribute(stop
);
34 write_attribute(clear_stats
);
35 write_attribute(trigger_gc
);
36 write_attribute(prune_cache
);
37 write_attribute(flash_vol_create
);
39 read_attribute(bucket_size
);
40 read_attribute(block_size
);
41 read_attribute(nbuckets
);
42 read_attribute(tree_depth
);
43 read_attribute(root_usage_percent
);
44 read_attribute(priority_stats
);
45 read_attribute(btree_cache_size
);
46 read_attribute(btree_cache_max_chain
);
47 read_attribute(cache_available_percent
);
48 read_attribute(written
);
49 read_attribute(btree_written
);
50 read_attribute(metadata_written
);
51 read_attribute(active_journal_entries
);
53 sysfs_time_stats_attribute(btree_gc
, sec
, ms
);
54 sysfs_time_stats_attribute(btree_split
, sec
, us
);
55 sysfs_time_stats_attribute(btree_sort
, ms
, us
);
56 sysfs_time_stats_attribute(btree_read
, ms
, us
);
58 read_attribute(btree_nodes
);
59 read_attribute(btree_used_percent
);
60 read_attribute(average_key_size
);
61 read_attribute(dirty_data
);
62 read_attribute(bset_tree_stats
);
64 read_attribute(state
);
65 read_attribute(cache_read_races
);
66 read_attribute(writeback_keys_done
);
67 read_attribute(writeback_keys_failed
);
68 read_attribute(io_errors
);
69 read_attribute(congested
);
70 rw_attribute(congested_read_threshold_us
);
71 rw_attribute(congested_write_threshold_us
);
73 rw_attribute(sequential_cutoff
);
74 rw_attribute(data_csum
);
75 rw_attribute(cache_mode
);
76 rw_attribute(writeback_metadata
);
77 rw_attribute(writeback_running
);
78 rw_attribute(writeback_percent
);
79 rw_attribute(writeback_delay
);
80 rw_attribute(writeback_rate
);
82 rw_attribute(writeback_rate_update_seconds
);
83 rw_attribute(writeback_rate_d_term
);
84 rw_attribute(writeback_rate_p_term_inverse
);
85 read_attribute(writeback_rate_debug
);
87 read_attribute(stripe_size
);
88 read_attribute(partial_stripes_expensive
);
90 rw_attribute(synchronous
);
91 rw_attribute(journal_delay_ms
);
92 rw_attribute(discard
);
93 rw_attribute(running
);
95 rw_attribute(readahead
);
97 rw_attribute(io_error_limit
);
98 rw_attribute(io_error_halflife
);
100 rw_attribute(bypass_torture_test
);
101 rw_attribute(key_merging_disabled
);
102 rw_attribute(gc_always_rewrite
);
103 rw_attribute(expensive_debug_checks
);
104 rw_attribute(cache_replacement_policy
);
105 rw_attribute(btree_shrinker_disabled
);
106 rw_attribute(copy_gc_enabled
);
109 SHOW(__bch_cached_dev
)
111 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
113 const char *states
[] = { "no cache", "clean", "dirty", "inconsistent" };
115 #define var(stat) (dc->stat)
117 if (attr
== &sysfs_cache_mode
)
118 return bch_snprint_string_list(buf
, PAGE_SIZE
,
120 BDEV_CACHE_MODE(&dc
->sb
));
122 sysfs_printf(data_csum
, "%i", dc
->disk
.data_csum
);
123 var_printf(verify
, "%i");
124 var_printf(bypass_torture_test
, "%i");
125 var_printf(writeback_metadata
, "%i");
126 var_printf(writeback_running
, "%i");
127 var_print(writeback_delay
);
128 var_print(writeback_percent
);
129 sysfs_hprint(writeback_rate
, dc
->writeback_rate
.rate
<< 9);
131 var_print(writeback_rate_update_seconds
);
132 var_print(writeback_rate_d_term
);
133 var_print(writeback_rate_p_term_inverse
);
135 if (attr
== &sysfs_writeback_rate_debug
) {
139 char proportional
[20];
144 bch_hprint(rate
, dc
->writeback_rate
.rate
<< 9);
145 bch_hprint(dirty
, bcache_dev_sectors_dirty(&dc
->disk
) << 9);
146 bch_hprint(target
, dc
->writeback_rate_target
<< 9);
147 bch_hprint(proportional
,dc
->writeback_rate_proportional
<< 9);
148 bch_hprint(derivative
, dc
->writeback_rate_derivative
<< 9);
149 bch_hprint(change
, dc
->writeback_rate_change
<< 9);
151 next_io
= div64_s64(dc
->writeback_rate
.next
- local_clock(),
158 "proportional:\t%s\n"
160 "change:\t\t%s/sec\n"
161 "next io:\t%llims\n",
162 rate
, dirty
, target
, proportional
,
163 derivative
, change
, next_io
);
166 sysfs_hprint(dirty_data
,
167 bcache_dev_sectors_dirty(&dc
->disk
) << 9);
169 sysfs_hprint(stripe_size
, dc
->disk
.stripe_size
<< 9);
170 var_printf(partial_stripes_expensive
, "%u");
172 var_hprint(sequential_cutoff
);
173 var_hprint(readahead
);
175 sysfs_print(running
, atomic_read(&dc
->running
));
176 sysfs_print(state
, states
[BDEV_STATE(&dc
->sb
)]);
178 if (attr
== &sysfs_label
) {
179 memcpy(buf
, dc
->sb
.label
, SB_LABEL_SIZE
);
180 buf
[SB_LABEL_SIZE
+ 1] = '\0';
188 SHOW_LOCKED(bch_cached_dev
)
192 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
196 struct kobj_uevent_env
*env
;
198 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
199 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
200 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
202 sysfs_strtoul(data_csum
, dc
->disk
.data_csum
);
204 d_strtoul(bypass_torture_test
);
205 d_strtoul(writeback_metadata
);
206 d_strtoul(writeback_running
);
207 d_strtoul(writeback_delay
);
209 sysfs_strtoul_clamp(writeback_percent
, dc
->writeback_percent
, 0, 40);
211 sysfs_strtoul_clamp(writeback_rate
,
212 dc
->writeback_rate
.rate
, 1, INT_MAX
);
214 d_strtoul_nonzero(writeback_rate_update_seconds
);
215 d_strtoul(writeback_rate_d_term
);
216 d_strtoul_nonzero(writeback_rate_p_term_inverse
);
218 d_strtoi_h(sequential_cutoff
);
219 d_strtoi_h(readahead
);
221 if (attr
== &sysfs_clear_stats
)
222 bch_cache_accounting_clear(&dc
->accounting
);
224 if (attr
== &sysfs_running
&&
225 strtoul_or_return(buf
))
226 bch_cached_dev_run(dc
);
228 if (attr
== &sysfs_cache_mode
) {
229 v
= bch_read_string_list(buf
, bch_cache_modes
+ 1);
234 if ((unsigned) v
!= BDEV_CACHE_MODE(&dc
->sb
)) {
235 SET_BDEV_CACHE_MODE(&dc
->sb
, v
);
236 bch_write_bdev_super(dc
, NULL
);
240 if (attr
== &sysfs_label
) {
241 if (size
> SB_LABEL_SIZE
)
243 memcpy(dc
->sb
.label
, buf
, size
);
244 if (size
< SB_LABEL_SIZE
)
245 dc
->sb
.label
[size
] = '\0';
246 if (size
&& dc
->sb
.label
[size
- 1] == '\n')
247 dc
->sb
.label
[size
- 1] = '\0';
248 bch_write_bdev_super(dc
, NULL
);
250 memcpy(dc
->disk
.c
->uuids
[dc
->disk
.id
].label
,
252 bch_uuid_write(dc
->disk
.c
);
254 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
257 add_uevent_var(env
, "DRIVER=bcache");
258 add_uevent_var(env
, "CACHED_UUID=%pU", dc
->sb
.uuid
),
259 add_uevent_var(env
, "CACHED_LABEL=%s", buf
);
261 &disk_to_dev(dc
->disk
.disk
)->kobj
, KOBJ_CHANGE
, env
->envp
);
265 if (attr
== &sysfs_attach
) {
266 uint8_t set_uuid
[16];
268 if (bch_parse_uuid(buf
, set_uuid
) < 16)
272 list_for_each_entry(c
, &bch_cache_sets
, list
) {
273 v
= bch_cached_dev_attach(dc
, c
, set_uuid
);
278 pr_err("Can't attach %s: cache set not found", buf
);
282 if (attr
== &sysfs_detach
&& dc
->disk
.c
)
283 bch_cached_dev_detach(dc
);
285 if (attr
== &sysfs_stop
)
286 bcache_device_stop(&dc
->disk
);
291 STORE(bch_cached_dev
)
293 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
296 mutex_lock(&bch_register_lock
);
297 size
= __cached_dev_store(kobj
, attr
, buf
, size
);
299 if (attr
== &sysfs_writeback_running
)
300 bch_writeback_queue(dc
);
302 if (attr
== &sysfs_writeback_percent
)
303 schedule_delayed_work(&dc
->writeback_rate_update
,
304 dc
->writeback_rate_update_seconds
* HZ
);
306 mutex_unlock(&bch_register_lock
);
310 static struct attribute
*bch_cached_dev_files
[] = {
318 &sysfs_writeback_metadata
,
319 &sysfs_writeback_running
,
320 &sysfs_writeback_delay
,
321 &sysfs_writeback_percent
,
322 &sysfs_writeback_rate
,
323 &sysfs_writeback_rate_update_seconds
,
324 &sysfs_writeback_rate_d_term
,
325 &sysfs_writeback_rate_p_term_inverse
,
326 &sysfs_writeback_rate_debug
,
329 &sysfs_partial_stripes_expensive
,
330 &sysfs_sequential_cutoff
,
336 #ifdef CONFIG_BCACHE_DEBUG
338 &sysfs_bypass_torture_test
,
342 KTYPE(bch_cached_dev
);
346 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
348 struct uuid_entry
*u
= &d
->c
->uuids
[d
->id
];
350 sysfs_printf(data_csum
, "%i", d
->data_csum
);
351 sysfs_hprint(size
, u
->sectors
<< 9);
353 if (attr
== &sysfs_label
) {
354 memcpy(buf
, u
->label
, SB_LABEL_SIZE
);
355 buf
[SB_LABEL_SIZE
+ 1] = '\0';
363 STORE(__bch_flash_dev
)
365 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
367 struct uuid_entry
*u
= &d
->c
->uuids
[d
->id
];
369 sysfs_strtoul(data_csum
, d
->data_csum
);
371 if (attr
== &sysfs_size
) {
373 strtoi_h_or_return(buf
, v
);
376 bch_uuid_write(d
->c
);
377 set_capacity(d
->disk
, u
->sectors
);
380 if (attr
== &sysfs_label
) {
381 memcpy(u
->label
, buf
, SB_LABEL_SIZE
);
382 bch_uuid_write(d
->c
);
385 if (attr
== &sysfs_unregister
) {
386 set_bit(BCACHE_DEV_DETACHING
, &d
->flags
);
387 bcache_device_stop(d
);
392 STORE_LOCKED(bch_flash_dev
)
394 static struct attribute
*bch_flash_dev_files
[] = {
403 KTYPE(bch_flash_dev
);
405 struct bset_stats_op
{
408 struct bset_stats stats
;
411 static int bch_btree_bset_stats(struct btree_op
*b_op
, struct btree
*b
)
413 struct bset_stats_op
*op
= container_of(b_op
, struct bset_stats_op
, op
);
416 bch_btree_keys_stats(&b
->keys
, &op
->stats
);
421 static int bch_bset_print_stats(struct cache_set
*c
, char *buf
)
423 struct bset_stats_op op
;
426 memset(&op
, 0, sizeof(op
));
427 bch_btree_op_init(&op
.op
, -1);
429 ret
= bch_btree_map_nodes(&op
.op
, c
, &ZERO_KEY
, bch_btree_bset_stats
);
433 return snprintf(buf
, PAGE_SIZE
,
435 "written sets: %zu\n"
436 "unwritten sets: %zu\n"
437 "written key bytes: %zu\n"
438 "unwritten key bytes: %zu\n"
442 op
.stats
.sets_written
, op
.stats
.sets_unwritten
,
443 op
.stats
.bytes_written
, op
.stats
.bytes_unwritten
,
444 op
.stats
.floats
, op
.stats
.failed
);
447 static unsigned bch_root_usage(struct cache_set
*c
)
452 struct btree_iter iter
;
460 rw_lock(false, b
, b
->level
);
461 } while (b
!= c
->root
);
463 for_each_key_filter(&b
->keys
, k
, &iter
, bch_ptr_bad
)
464 bytes
+= bkey_bytes(k
);
468 return (bytes
* 100) / btree_bytes(c
);
471 static size_t bch_cache_size(struct cache_set
*c
)
476 mutex_lock(&c
->bucket_lock
);
477 list_for_each_entry(b
, &c
->btree_cache
, list
)
478 ret
+= 1 << (b
->keys
.page_order
+ PAGE_SHIFT
);
480 mutex_unlock(&c
->bucket_lock
);
484 static unsigned bch_cache_max_chain(struct cache_set
*c
)
487 struct hlist_head
*h
;
489 mutex_lock(&c
->bucket_lock
);
491 for (h
= c
->bucket_hash
;
492 h
< c
->bucket_hash
+ (1 << BUCKET_HASH_BITS
);
495 struct hlist_node
*p
;
503 mutex_unlock(&c
->bucket_lock
);
507 static unsigned bch_btree_used(struct cache_set
*c
)
509 return div64_u64(c
->gc_stats
.key_bytes
* 100,
510 (c
->gc_stats
.nodes
?: 1) * btree_bytes(c
));
513 static unsigned bch_average_key_size(struct cache_set
*c
)
515 return c
->gc_stats
.nkeys
516 ? div64_u64(c
->gc_stats
.data
, c
->gc_stats
.nkeys
)
520 SHOW(__bch_cache_set
)
522 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
524 sysfs_print(synchronous
, CACHE_SYNC(&c
->sb
));
525 sysfs_print(journal_delay_ms
, c
->journal_delay_ms
);
526 sysfs_hprint(bucket_size
, bucket_bytes(c
));
527 sysfs_hprint(block_size
, block_bytes(c
));
528 sysfs_print(tree_depth
, c
->root
->level
);
529 sysfs_print(root_usage_percent
, bch_root_usage(c
));
531 sysfs_hprint(btree_cache_size
, bch_cache_size(c
));
532 sysfs_print(btree_cache_max_chain
, bch_cache_max_chain(c
));
533 sysfs_print(cache_available_percent
, 100 - c
->gc_stats
.in_use
);
535 sysfs_print_time_stats(&c
->btree_gc_time
, btree_gc
, sec
, ms
);
536 sysfs_print_time_stats(&c
->btree_split_time
, btree_split
, sec
, us
);
537 sysfs_print_time_stats(&c
->sort
.time
, btree_sort
, ms
, us
);
538 sysfs_print_time_stats(&c
->btree_read_time
, btree_read
, ms
, us
);
540 sysfs_print(btree_used_percent
, bch_btree_used(c
));
541 sysfs_print(btree_nodes
, c
->gc_stats
.nodes
);
542 sysfs_hprint(average_key_size
, bch_average_key_size(c
));
544 sysfs_print(cache_read_races
,
545 atomic_long_read(&c
->cache_read_races
));
547 sysfs_print(writeback_keys_done
,
548 atomic_long_read(&c
->writeback_keys_done
));
549 sysfs_print(writeback_keys_failed
,
550 atomic_long_read(&c
->writeback_keys_failed
));
552 if (attr
== &sysfs_errors
)
553 return bch_snprint_string_list(buf
, PAGE_SIZE
, error_actions
,
556 /* See count_io_errors for why 88 */
557 sysfs_print(io_error_halflife
, c
->error_decay
* 88);
558 sysfs_print(io_error_limit
, c
->error_limit
>> IO_ERROR_SHIFT
);
560 sysfs_hprint(congested
,
561 ((uint64_t) bch_get_congested(c
)) << 9);
562 sysfs_print(congested_read_threshold_us
,
563 c
->congested_read_threshold_us
);
564 sysfs_print(congested_write_threshold_us
,
565 c
->congested_write_threshold_us
);
567 sysfs_print(active_journal_entries
, fifo_used(&c
->journal
.pin
));
568 sysfs_printf(verify
, "%i", c
->verify
);
569 sysfs_printf(key_merging_disabled
, "%i", c
->key_merging_disabled
);
570 sysfs_printf(expensive_debug_checks
,
571 "%i", c
->expensive_debug_checks
);
572 sysfs_printf(gc_always_rewrite
, "%i", c
->gc_always_rewrite
);
573 sysfs_printf(btree_shrinker_disabled
, "%i", c
->shrinker_disabled
);
574 sysfs_printf(copy_gc_enabled
, "%i", c
->copy_gc_enabled
);
576 if (attr
== &sysfs_bset_tree_stats
)
577 return bch_bset_print_stats(c
, buf
);
581 SHOW_LOCKED(bch_cache_set
)
583 STORE(__bch_cache_set
)
585 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
587 if (attr
== &sysfs_unregister
)
588 bch_cache_set_unregister(c
);
590 if (attr
== &sysfs_stop
)
591 bch_cache_set_stop(c
);
593 if (attr
== &sysfs_synchronous
) {
594 bool sync
= strtoul_or_return(buf
);
596 if (sync
!= CACHE_SYNC(&c
->sb
)) {
597 SET_CACHE_SYNC(&c
->sb
, sync
);
598 bcache_write_super(c
);
602 if (attr
== &sysfs_flash_vol_create
) {
605 strtoi_h_or_return(buf
, v
);
607 r
= bch_flash_dev_create(c
, v
);
612 if (attr
== &sysfs_clear_stats
) {
613 atomic_long_set(&c
->writeback_keys_done
, 0);
614 atomic_long_set(&c
->writeback_keys_failed
, 0);
616 memset(&c
->gc_stats
, 0, sizeof(struct gc_stat
));
617 bch_cache_accounting_clear(&c
->accounting
);
620 if (attr
== &sysfs_trigger_gc
)
623 if (attr
== &sysfs_prune_cache
) {
624 struct shrink_control sc
;
625 sc
.gfp_mask
= GFP_KERNEL
;
626 sc
.nr_to_scan
= strtoul_or_return(buf
);
627 c
->shrink
.scan_objects(&c
->shrink
, &sc
);
630 sysfs_strtoul(congested_read_threshold_us
,
631 c
->congested_read_threshold_us
);
632 sysfs_strtoul(congested_write_threshold_us
,
633 c
->congested_write_threshold_us
);
635 if (attr
== &sysfs_errors
) {
636 ssize_t v
= bch_read_string_list(buf
, error_actions
);
644 if (attr
== &sysfs_io_error_limit
)
645 c
->error_limit
= strtoul_or_return(buf
) << IO_ERROR_SHIFT
;
647 /* See count_io_errors() for why 88 */
648 if (attr
== &sysfs_io_error_halflife
)
649 c
->error_decay
= strtoul_or_return(buf
) / 88;
651 sysfs_strtoul(journal_delay_ms
, c
->journal_delay_ms
);
652 sysfs_strtoul(verify
, c
->verify
);
653 sysfs_strtoul(key_merging_disabled
, c
->key_merging_disabled
);
654 sysfs_strtoul(expensive_debug_checks
, c
->expensive_debug_checks
);
655 sysfs_strtoul(gc_always_rewrite
, c
->gc_always_rewrite
);
656 sysfs_strtoul(btree_shrinker_disabled
, c
->shrinker_disabled
);
657 sysfs_strtoul(copy_gc_enabled
, c
->copy_gc_enabled
);
661 STORE_LOCKED(bch_cache_set
)
663 SHOW(bch_cache_set_internal
)
665 struct cache_set
*c
= container_of(kobj
, struct cache_set
, internal
);
666 return bch_cache_set_show(&c
->kobj
, attr
, buf
);
669 STORE(bch_cache_set_internal
)
671 struct cache_set
*c
= container_of(kobj
, struct cache_set
, internal
);
672 return bch_cache_set_store(&c
->kobj
, attr
, buf
, size
);
675 static void bch_cache_set_internal_release(struct kobject
*k
)
679 static struct attribute
*bch_cache_set_files
[] = {
683 &sysfs_journal_delay_ms
,
684 &sysfs_flash_vol_create
,
689 &sysfs_root_usage_percent
,
690 &sysfs_btree_cache_size
,
691 &sysfs_cache_available_percent
,
693 &sysfs_average_key_size
,
696 &sysfs_io_error_limit
,
697 &sysfs_io_error_halflife
,
699 &sysfs_congested_read_threshold_us
,
700 &sysfs_congested_write_threshold_us
,
704 KTYPE(bch_cache_set
);
706 static struct attribute
*bch_cache_set_internal_files
[] = {
707 &sysfs_active_journal_entries
,
709 sysfs_time_stats_attribute_list(btree_gc
, sec
, ms
)
710 sysfs_time_stats_attribute_list(btree_split
, sec
, us
)
711 sysfs_time_stats_attribute_list(btree_sort
, ms
, us
)
712 sysfs_time_stats_attribute_list(btree_read
, ms
, us
)
715 &sysfs_btree_used_percent
,
716 &sysfs_btree_cache_max_chain
,
718 &sysfs_bset_tree_stats
,
719 &sysfs_cache_read_races
,
720 &sysfs_writeback_keys_done
,
721 &sysfs_writeback_keys_failed
,
725 #ifdef CONFIG_BCACHE_DEBUG
727 &sysfs_key_merging_disabled
,
728 &sysfs_expensive_debug_checks
,
730 &sysfs_gc_always_rewrite
,
731 &sysfs_btree_shrinker_disabled
,
732 &sysfs_copy_gc_enabled
,
735 KTYPE(bch_cache_set_internal
);
739 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
741 sysfs_hprint(bucket_size
, bucket_bytes(ca
));
742 sysfs_hprint(block_size
, block_bytes(ca
));
743 sysfs_print(nbuckets
, ca
->sb
.nbuckets
);
744 sysfs_print(discard
, ca
->discard
);
745 sysfs_hprint(written
, atomic_long_read(&ca
->sectors_written
) << 9);
746 sysfs_hprint(btree_written
,
747 atomic_long_read(&ca
->btree_sectors_written
) << 9);
748 sysfs_hprint(metadata_written
,
749 (atomic_long_read(&ca
->meta_sectors_written
) +
750 atomic_long_read(&ca
->btree_sectors_written
)) << 9);
752 sysfs_print(io_errors
,
753 atomic_read(&ca
->io_errors
) >> IO_ERROR_SHIFT
);
755 if (attr
== &sysfs_cache_replacement_policy
)
756 return bch_snprint_string_list(buf
, PAGE_SIZE
,
757 cache_replacement_policies
,
758 CACHE_REPLACEMENT(&ca
->sb
));
760 if (attr
== &sysfs_priority_stats
) {
761 int cmp(const void *l
, const void *r
)
762 { return *((uint16_t *) r
) - *((uint16_t *) l
); }
765 size_t n
= ca
->sb
.nbuckets
, i
;
766 size_t unused
= 0, available
= 0, dirty
= 0, meta
= 0;
768 /* Compute 31 quantiles */
769 uint16_t q
[31], *p
, *cached
;
772 cached
= p
= vmalloc(ca
->sb
.nbuckets
* sizeof(uint16_t));
776 mutex_lock(&ca
->set
->bucket_lock
);
777 for_each_bucket(b
, ca
) {
778 if (!GC_SECTORS_USED(b
))
780 if (GC_MARK(b
) == GC_MARK_RECLAIMABLE
)
782 if (GC_MARK(b
) == GC_MARK_DIRTY
)
784 if (GC_MARK(b
) == GC_MARK_METADATA
)
788 for (i
= ca
->sb
.first_bucket
; i
< n
; i
++)
789 p
[i
] = ca
->buckets
[i
].prio
;
790 mutex_unlock(&ca
->set
->bucket_lock
);
792 sort(p
, n
, sizeof(uint16_t), cmp
, NULL
);
798 unused
= ca
->sb
.nbuckets
- n
;
800 while (cached
< p
+ n
&&
801 *cached
== BTREE_PRIO
)
804 for (i
= 0; i
< n
; i
++)
805 sum
+= INITIAL_PRIO
- cached
[i
];
810 for (i
= 0; i
< ARRAY_SIZE(q
); i
++)
811 q
[i
] = INITIAL_PRIO
- cached
[n
* (i
+ 1) /
812 (ARRAY_SIZE(q
) + 1)];
816 ret
= scnprintf(buf
, PAGE_SIZE
,
822 "Sectors per Q: %zu\n"
824 unused
* 100 / (size_t) ca
->sb
.nbuckets
,
825 available
* 100 / (size_t) ca
->sb
.nbuckets
,
826 dirty
* 100 / (size_t) ca
->sb
.nbuckets
,
827 meta
* 100 / (size_t) ca
->sb
.nbuckets
, sum
,
828 n
* ca
->sb
.bucket_size
/ (ARRAY_SIZE(q
) + 1));
830 for (i
= 0; i
< ARRAY_SIZE(q
); i
++)
831 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
,
835 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "]\n");
842 SHOW_LOCKED(bch_cache
)
846 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
848 if (attr
== &sysfs_discard
) {
849 bool v
= strtoul_or_return(buf
);
851 if (blk_queue_discard(bdev_get_queue(ca
->bdev
)))
854 if (v
!= CACHE_DISCARD(&ca
->sb
)) {
855 SET_CACHE_DISCARD(&ca
->sb
, v
);
856 bcache_write_super(ca
->set
);
860 if (attr
== &sysfs_cache_replacement_policy
) {
861 ssize_t v
= bch_read_string_list(buf
, cache_replacement_policies
);
866 if ((unsigned) v
!= CACHE_REPLACEMENT(&ca
->sb
)) {
867 mutex_lock(&ca
->set
->bucket_lock
);
868 SET_CACHE_REPLACEMENT(&ca
->sb
, v
);
869 mutex_unlock(&ca
->set
->bucket_lock
);
871 bcache_write_super(ca
->set
);
875 if (attr
== &sysfs_clear_stats
) {
876 atomic_long_set(&ca
->sectors_written
, 0);
877 atomic_long_set(&ca
->btree_sectors_written
, 0);
878 atomic_long_set(&ca
->meta_sectors_written
, 0);
879 atomic_set(&ca
->io_count
, 0);
880 atomic_set(&ca
->io_errors
, 0);
885 STORE_LOCKED(bch_cache
)
887 static struct attribute
*bch_cache_files
[] = {
891 &sysfs_priority_stats
,
894 &sysfs_btree_written
,
895 &sysfs_metadata_written
,
898 &sysfs_cache_replacement_policy
,