bcache: Add struct btree_keys
[linux-block.git] / drivers / md / bcache / sysfs.c
CommitLineData
cafe5635
KO
1/*
2 * bcache sysfs interfaces
3 *
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
6 */
7
8#include "bcache.h"
9#include "sysfs.h"
10#include "btree.h"
11#include "request.h"
279afbad 12#include "writeback.h"
cafe5635 13
c37511b8 14#include <linux/blkdev.h>
cafe5635
KO
15#include <linux/sort.h>
16
17static const char * const cache_replacement_policies[] = {
18 "lru",
19 "fifo",
20 "random",
21 NULL
22};
23
77c320eb
KO
24static const char * const error_actions[] = {
25 "unregister",
26 "panic",
27 NULL
28};
29
cafe5635
KO
30write_attribute(attach);
31write_attribute(detach);
32write_attribute(unregister);
33write_attribute(stop);
34write_attribute(clear_stats);
35write_attribute(trigger_gc);
36write_attribute(prune_cache);
37write_attribute(flash_vol_create);
38
39read_attribute(bucket_size);
40read_attribute(block_size);
41read_attribute(nbuckets);
42read_attribute(tree_depth);
43read_attribute(root_usage_percent);
44read_attribute(priority_stats);
45read_attribute(btree_cache_size);
46read_attribute(btree_cache_max_chain);
47read_attribute(cache_available_percent);
48read_attribute(written);
49read_attribute(btree_written);
50read_attribute(metadata_written);
51read_attribute(active_journal_entries);
52
53sysfs_time_stats_attribute(btree_gc, sec, ms);
54sysfs_time_stats_attribute(btree_split, sec, us);
55sysfs_time_stats_attribute(btree_sort, ms, us);
56sysfs_time_stats_attribute(btree_read, ms, us);
57sysfs_time_stats_attribute(try_harder, ms, us);
58
59read_attribute(btree_nodes);
60read_attribute(btree_used_percent);
61read_attribute(average_key_size);
62read_attribute(dirty_data);
63read_attribute(bset_tree_stats);
64
65read_attribute(state);
66read_attribute(cache_read_races);
67read_attribute(writeback_keys_done);
68read_attribute(writeback_keys_failed);
69read_attribute(io_errors);
70read_attribute(congested);
71rw_attribute(congested_read_threshold_us);
72rw_attribute(congested_write_threshold_us);
73
74rw_attribute(sequential_cutoff);
cafe5635
KO
75rw_attribute(data_csum);
76rw_attribute(cache_mode);
77rw_attribute(writeback_metadata);
78rw_attribute(writeback_running);
79rw_attribute(writeback_percent);
80rw_attribute(writeback_delay);
81rw_attribute(writeback_rate);
82
83rw_attribute(writeback_rate_update_seconds);
84rw_attribute(writeback_rate_d_term);
85rw_attribute(writeback_rate_p_term_inverse);
cafe5635
KO
86read_attribute(writeback_rate_debug);
87
72c27061
KO
88read_attribute(stripe_size);
89read_attribute(partial_stripes_expensive);
90
cafe5635
KO
91rw_attribute(synchronous);
92rw_attribute(journal_delay_ms);
93rw_attribute(discard);
94rw_attribute(running);
95rw_attribute(label);
96rw_attribute(readahead);
77c320eb 97rw_attribute(errors);
cafe5635
KO
98rw_attribute(io_error_limit);
99rw_attribute(io_error_halflife);
100rw_attribute(verify);
5ceaaad7 101rw_attribute(bypass_torture_test);
cafe5635
KO
102rw_attribute(key_merging_disabled);
103rw_attribute(gc_always_rewrite);
280481d0 104rw_attribute(expensive_debug_checks);
cafe5635
KO
105rw_attribute(cache_replacement_policy);
106rw_attribute(btree_shrinker_disabled);
107rw_attribute(copy_gc_enabled);
108rw_attribute(size);
109
110SHOW(__bch_cached_dev)
111{
112 struct cached_dev *dc = container_of(kobj, struct cached_dev,
113 disk.kobj);
114 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
115
116#define var(stat) (dc->stat)
117
118 if (attr == &sysfs_cache_mode)
169ef1cf
KO
119 return bch_snprint_string_list(buf, PAGE_SIZE,
120 bch_cache_modes + 1,
121 BDEV_CACHE_MODE(&dc->sb));
cafe5635
KO
122
123 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
124 var_printf(verify, "%i");
5ceaaad7 125 var_printf(bypass_torture_test, "%i");
cafe5635
KO
126 var_printf(writeback_metadata, "%i");
127 var_printf(writeback_running, "%i");
128 var_print(writeback_delay);
129 var_print(writeback_percent);
16749c23 130 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
cafe5635
KO
131
132 var_print(writeback_rate_update_seconds);
133 var_print(writeback_rate_d_term);
134 var_print(writeback_rate_p_term_inverse);
cafe5635
KO
135
136 if (attr == &sysfs_writeback_rate_debug) {
16749c23 137 char rate[20];
cafe5635 138 char dirty[20];
cafe5635 139 char target[20];
16749c23
KO
140 char proportional[20];
141 char derivative[20];
142 char change[20];
143 s64 next_io;
144
145 bch_hprint(rate, dc->writeback_rate.rate << 9);
146 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
169ef1cf 147 bch_hprint(target, dc->writeback_rate_target << 9);
16749c23
KO
148 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
149 bch_hprint(derivative, dc->writeback_rate_derivative << 9);
150 bch_hprint(change, dc->writeback_rate_change << 9);
151
152 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
153 NSEC_PER_MSEC);
cafe5635
KO
154
155 return sprintf(buf,
16749c23 156 "rate:\t\t%s/sec\n"
cafe5635 157 "dirty:\t\t%s\n"
16749c23
KO
158 "target:\t\t%s\n"
159 "proportional:\t%s\n"
cafe5635 160 "derivative:\t%s\n"
16749c23
KO
161 "change:\t\t%s/sec\n"
162 "next io:\t%llims\n",
163 rate, dirty, target, proportional,
164 derivative, change, next_io);
cafe5635
KO
165 }
166
167 sysfs_hprint(dirty_data,
279afbad 168 bcache_dev_sectors_dirty(&dc->disk) << 9);
cafe5635 169
2d679fc7 170 sysfs_hprint(stripe_size, dc->disk.stripe_size << 9);
72c27061
KO
171 var_printf(partial_stripes_expensive, "%u");
172
cafe5635
KO
173 var_hprint(sequential_cutoff);
174 var_hprint(readahead);
175
176 sysfs_print(running, atomic_read(&dc->running));
177 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
178
179 if (attr == &sysfs_label) {
180 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
181 buf[SB_LABEL_SIZE + 1] = '\0';
182 strcat(buf, "\n");
183 return strlen(buf);
184 }
185
186#undef var
187 return 0;
188}
189SHOW_LOCKED(bch_cached_dev)
190
191STORE(__cached_dev)
192{
193 struct cached_dev *dc = container_of(kobj, struct cached_dev,
194 disk.kobj);
195 unsigned v = size;
196 struct cache_set *c;
ab9e1400 197 struct kobj_uevent_env *env;
cafe5635
KO
198
199#define d_strtoul(var) sysfs_strtoul(var, dc->var)
16749c23 200#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
cafe5635
KO
201#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
202
203 sysfs_strtoul(data_csum, dc->disk.data_csum);
204 d_strtoul(verify);
5ceaaad7 205 d_strtoul(bypass_torture_test);
cafe5635
KO
206 d_strtoul(writeback_metadata);
207 d_strtoul(writeback_running);
208 d_strtoul(writeback_delay);
16749c23 209
cafe5635
KO
210 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
211
16749c23
KO
212 sysfs_strtoul_clamp(writeback_rate,
213 dc->writeback_rate.rate, 1, INT_MAX);
214
215 d_strtoul_nonzero(writeback_rate_update_seconds);
cafe5635 216 d_strtoul(writeback_rate_d_term);
16749c23 217 d_strtoul_nonzero(writeback_rate_p_term_inverse);
cafe5635 218
cafe5635
KO
219 d_strtoi_h(sequential_cutoff);
220 d_strtoi_h(readahead);
221
222 if (attr == &sysfs_clear_stats)
223 bch_cache_accounting_clear(&dc->accounting);
224
225 if (attr == &sysfs_running &&
226 strtoul_or_return(buf))
227 bch_cached_dev_run(dc);
228
229 if (attr == &sysfs_cache_mode) {
169ef1cf 230 ssize_t v = bch_read_string_list(buf, bch_cache_modes + 1);
cafe5635
KO
231
232 if (v < 0)
233 return v;
234
235 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
236 SET_BDEV_CACHE_MODE(&dc->sb, v);
237 bch_write_bdev_super(dc, NULL);
238 }
239 }
240
241 if (attr == &sysfs_label) {
aee6f1cf
GP
242 if (size > SB_LABEL_SIZE)
243 return -EINVAL;
244 memcpy(dc->sb.label, buf, size);
245 if (size < SB_LABEL_SIZE)
246 dc->sb.label[size] = '\0';
247 if (size && dc->sb.label[size - 1] == '\n')
248 dc->sb.label[size - 1] = '\0';
cafe5635
KO
249 bch_write_bdev_super(dc, NULL);
250 if (dc->disk.c) {
251 memcpy(dc->disk.c->uuids[dc->disk.id].label,
252 buf, SB_LABEL_SIZE);
253 bch_uuid_write(dc->disk.c);
254 }
ab9e1400 255 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
d2a65ce2
DC
256 if (!env)
257 return -ENOMEM;
ab9e1400
GP
258 add_uevent_var(env, "DRIVER=bcache");
259 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
260 add_uevent_var(env, "CACHED_LABEL=%s", buf);
261 kobject_uevent_env(
262 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
263 kfree(env);
cafe5635
KO
264 }
265
266 if (attr == &sysfs_attach) {
169ef1cf 267 if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
cafe5635
KO
268 return -EINVAL;
269
270 list_for_each_entry(c, &bch_cache_sets, list) {
271 v = bch_cached_dev_attach(dc, c);
272 if (!v)
273 return size;
274 }
275
276 pr_err("Can't attach %s: cache set not found", buf);
277 size = v;
278 }
279
280 if (attr == &sysfs_detach && dc->disk.c)
281 bch_cached_dev_detach(dc);
282
283 if (attr == &sysfs_stop)
284 bcache_device_stop(&dc->disk);
285
286 return size;
287}
288
289STORE(bch_cached_dev)
290{
291 struct cached_dev *dc = container_of(kobj, struct cached_dev,
292 disk.kobj);
293
294 mutex_lock(&bch_register_lock);
295 size = __cached_dev_store(kobj, attr, buf, size);
296
297 if (attr == &sysfs_writeback_running)
298 bch_writeback_queue(dc);
299
300 if (attr == &sysfs_writeback_percent)
301 schedule_delayed_work(&dc->writeback_rate_update,
302 dc->writeback_rate_update_seconds * HZ);
303
304 mutex_unlock(&bch_register_lock);
305 return size;
306}
307
308static struct attribute *bch_cached_dev_files[] = {
309 &sysfs_attach,
310 &sysfs_detach,
311 &sysfs_stop,
312#if 0
313 &sysfs_data_csum,
314#endif
315 &sysfs_cache_mode,
316 &sysfs_writeback_metadata,
317 &sysfs_writeback_running,
318 &sysfs_writeback_delay,
319 &sysfs_writeback_percent,
320 &sysfs_writeback_rate,
321 &sysfs_writeback_rate_update_seconds,
322 &sysfs_writeback_rate_d_term,
323 &sysfs_writeback_rate_p_term_inverse,
cafe5635
KO
324 &sysfs_writeback_rate_debug,
325 &sysfs_dirty_data,
72c27061
KO
326 &sysfs_stripe_size,
327 &sysfs_partial_stripes_expensive,
cafe5635 328 &sysfs_sequential_cutoff,
cafe5635
KO
329 &sysfs_clear_stats,
330 &sysfs_running,
331 &sysfs_state,
332 &sysfs_label,
333 &sysfs_readahead,
334#ifdef CONFIG_BCACHE_DEBUG
335 &sysfs_verify,
5ceaaad7 336 &sysfs_bypass_torture_test,
cafe5635
KO
337#endif
338 NULL
339};
340KTYPE(bch_cached_dev);
341
342SHOW(bch_flash_dev)
343{
344 struct bcache_device *d = container_of(kobj, struct bcache_device,
345 kobj);
346 struct uuid_entry *u = &d->c->uuids[d->id];
347
348 sysfs_printf(data_csum, "%i", d->data_csum);
349 sysfs_hprint(size, u->sectors << 9);
350
351 if (attr == &sysfs_label) {
352 memcpy(buf, u->label, SB_LABEL_SIZE);
353 buf[SB_LABEL_SIZE + 1] = '\0';
354 strcat(buf, "\n");
355 return strlen(buf);
356 }
357
358 return 0;
359}
360
361STORE(__bch_flash_dev)
362{
363 struct bcache_device *d = container_of(kobj, struct bcache_device,
364 kobj);
365 struct uuid_entry *u = &d->c->uuids[d->id];
366
367 sysfs_strtoul(data_csum, d->data_csum);
368
369 if (attr == &sysfs_size) {
370 uint64_t v;
371 strtoi_h_or_return(buf, v);
372
373 u->sectors = v >> 9;
374 bch_uuid_write(d->c);
375 set_capacity(d->disk, u->sectors);
376 }
377
378 if (attr == &sysfs_label) {
379 memcpy(u->label, buf, SB_LABEL_SIZE);
380 bch_uuid_write(d->c);
381 }
382
383 if (attr == &sysfs_unregister) {
c4d951dd 384 set_bit(BCACHE_DEV_DETACHING, &d->flags);
cafe5635
KO
385 bcache_device_stop(d);
386 }
387
388 return size;
389}
390STORE_LOCKED(bch_flash_dev)
391
392static struct attribute *bch_flash_dev_files[] = {
393 &sysfs_unregister,
394#if 0
395 &sysfs_data_csum,
396#endif
397 &sysfs_label,
398 &sysfs_size,
399 NULL
400};
401KTYPE(bch_flash_dev);
402
403SHOW(__bch_cache_set)
404{
405 unsigned root_usage(struct cache_set *c)
406 {
407 unsigned bytes = 0;
408 struct bkey *k;
409 struct btree *b;
410 struct btree_iter iter;
411
412 goto lock_root;
413
414 do {
415 rw_unlock(false, b);
416lock_root:
417 b = c->root;
418 rw_lock(false, b, b->level);
419 } while (b != c->root);
420
421 for_each_key_filter(b, k, &iter, bch_ptr_bad)
422 bytes += bkey_bytes(k);
423
424 rw_unlock(false, b);
425
426 return (bytes * 100) / btree_bytes(c);
427 }
428
429 size_t cache_size(struct cache_set *c)
430 {
431 size_t ret = 0;
432 struct btree *b;
433
434 mutex_lock(&c->bucket_lock);
435 list_for_each_entry(b, &c->btree_cache, list)
a85e968e 436 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
cafe5635
KO
437
438 mutex_unlock(&c->bucket_lock);
439 return ret;
440 }
441
442 unsigned cache_max_chain(struct cache_set *c)
443 {
444 unsigned ret = 0;
445 struct hlist_head *h;
446
447 mutex_lock(&c->bucket_lock);
448
449 for (h = c->bucket_hash;
450 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
451 h++) {
452 unsigned i = 0;
453 struct hlist_node *p;
454
455 hlist_for_each(p, h)
456 i++;
457
458 ret = max(ret, i);
459 }
460
461 mutex_unlock(&c->bucket_lock);
462 return ret;
463 }
464
465 unsigned btree_used(struct cache_set *c)
466 {
467 return div64_u64(c->gc_stats.key_bytes * 100,
468 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
469 }
470
471 unsigned average_key_size(struct cache_set *c)
472 {
473 return c->gc_stats.nkeys
474 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
475 : 0;
476 }
477
478 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
479
480 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
481 sysfs_print(journal_delay_ms, c->journal_delay_ms);
482 sysfs_hprint(bucket_size, bucket_bytes(c));
483 sysfs_hprint(block_size, block_bytes(c));
484 sysfs_print(tree_depth, c->root->level);
485 sysfs_print(root_usage_percent, root_usage(c));
486
487 sysfs_hprint(btree_cache_size, cache_size(c));
488 sysfs_print(btree_cache_max_chain, cache_max_chain(c));
489 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
490
491 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
492 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
67539e85 493 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
cafe5635
KO
494 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
495 sysfs_print_time_stats(&c->try_harder_time, try_harder, ms, us);
496
497 sysfs_print(btree_used_percent, btree_used(c));
498 sysfs_print(btree_nodes, c->gc_stats.nodes);
cafe5635
KO
499 sysfs_hprint(average_key_size, average_key_size(c));
500
501 sysfs_print(cache_read_races,
502 atomic_long_read(&c->cache_read_races));
503
504 sysfs_print(writeback_keys_done,
505 atomic_long_read(&c->writeback_keys_done));
506 sysfs_print(writeback_keys_failed,
507 atomic_long_read(&c->writeback_keys_failed));
508
77c320eb
KO
509 if (attr == &sysfs_errors)
510 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
511 c->on_error);
512
cafe5635
KO
513 /* See count_io_errors for why 88 */
514 sysfs_print(io_error_halflife, c->error_decay * 88);
515 sysfs_print(io_error_limit, c->error_limit >> IO_ERROR_SHIFT);
516
517 sysfs_hprint(congested,
518 ((uint64_t) bch_get_congested(c)) << 9);
519 sysfs_print(congested_read_threshold_us,
520 c->congested_read_threshold_us);
521 sysfs_print(congested_write_threshold_us,
522 c->congested_write_threshold_us);
523
524 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
525 sysfs_printf(verify, "%i", c->verify);
526 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
280481d0
KO
527 sysfs_printf(expensive_debug_checks,
528 "%i", c->expensive_debug_checks);
cafe5635
KO
529 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
530 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
531 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
532
533 if (attr == &sysfs_bset_tree_stats)
534 return bch_bset_print_stats(c, buf);
535
536 return 0;
537}
538SHOW_LOCKED(bch_cache_set)
539
540STORE(__bch_cache_set)
541{
542 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
543
544 if (attr == &sysfs_unregister)
545 bch_cache_set_unregister(c);
546
547 if (attr == &sysfs_stop)
548 bch_cache_set_stop(c);
549
550 if (attr == &sysfs_synchronous) {
551 bool sync = strtoul_or_return(buf);
552
553 if (sync != CACHE_SYNC(&c->sb)) {
554 SET_CACHE_SYNC(&c->sb, sync);
555 bcache_write_super(c);
556 }
557 }
558
559 if (attr == &sysfs_flash_vol_create) {
560 int r;
561 uint64_t v;
562 strtoi_h_or_return(buf, v);
563
564 r = bch_flash_dev_create(c, v);
565 if (r)
566 return r;
567 }
568
569 if (attr == &sysfs_clear_stats) {
570 atomic_long_set(&c->writeback_keys_done, 0);
571 atomic_long_set(&c->writeback_keys_failed, 0);
572
573 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
574 bch_cache_accounting_clear(&c->accounting);
575 }
576
577 if (attr == &sysfs_trigger_gc)
72a44517 578 wake_up_gc(c);
cafe5635
KO
579
580 if (attr == &sysfs_prune_cache) {
581 struct shrink_control sc;
582 sc.gfp_mask = GFP_KERNEL;
583 sc.nr_to_scan = strtoul_or_return(buf);
7dc19d5a 584 c->shrink.scan_objects(&c->shrink, &sc);
cafe5635
KO
585 }
586
587 sysfs_strtoul(congested_read_threshold_us,
588 c->congested_read_threshold_us);
589 sysfs_strtoul(congested_write_threshold_us,
590 c->congested_write_threshold_us);
591
77c320eb
KO
592 if (attr == &sysfs_errors) {
593 ssize_t v = bch_read_string_list(buf, error_actions);
594
595 if (v < 0)
596 return v;
597
598 c->on_error = v;
599 }
600
cafe5635
KO
601 if (attr == &sysfs_io_error_limit)
602 c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
603
604 /* See count_io_errors() for why 88 */
605 if (attr == &sysfs_io_error_halflife)
606 c->error_decay = strtoul_or_return(buf) / 88;
607
608 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
609 sysfs_strtoul(verify, c->verify);
610 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
280481d0 611 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
cafe5635
KO
612 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
613 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
614 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
615
616 return size;
617}
618STORE_LOCKED(bch_cache_set)
619
620SHOW(bch_cache_set_internal)
621{
622 struct cache_set *c = container_of(kobj, struct cache_set, internal);
623 return bch_cache_set_show(&c->kobj, attr, buf);
624}
625
626STORE(bch_cache_set_internal)
627{
628 struct cache_set *c = container_of(kobj, struct cache_set, internal);
629 return bch_cache_set_store(&c->kobj, attr, buf, size);
630}
631
632static void bch_cache_set_internal_release(struct kobject *k)
633{
634}
635
636static struct attribute *bch_cache_set_files[] = {
637 &sysfs_unregister,
638 &sysfs_stop,
639 &sysfs_synchronous,
640 &sysfs_journal_delay_ms,
641 &sysfs_flash_vol_create,
642
643 &sysfs_bucket_size,
644 &sysfs_block_size,
645 &sysfs_tree_depth,
646 &sysfs_root_usage_percent,
647 &sysfs_btree_cache_size,
648 &sysfs_cache_available_percent,
649
650 &sysfs_average_key_size,
cafe5635 651
77c320eb 652 &sysfs_errors,
cafe5635
KO
653 &sysfs_io_error_limit,
654 &sysfs_io_error_halflife,
655 &sysfs_congested,
656 &sysfs_congested_read_threshold_us,
657 &sysfs_congested_write_threshold_us,
658 &sysfs_clear_stats,
659 NULL
660};
661KTYPE(bch_cache_set);
662
663static struct attribute *bch_cache_set_internal_files[] = {
664 &sysfs_active_journal_entries,
665
666 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
667 sysfs_time_stats_attribute_list(btree_split, sec, us)
668 sysfs_time_stats_attribute_list(btree_sort, ms, us)
669 sysfs_time_stats_attribute_list(btree_read, ms, us)
670 sysfs_time_stats_attribute_list(try_harder, ms, us)
671
672 &sysfs_btree_nodes,
673 &sysfs_btree_used_percent,
674 &sysfs_btree_cache_max_chain,
675
676 &sysfs_bset_tree_stats,
677 &sysfs_cache_read_races,
678 &sysfs_writeback_keys_done,
679 &sysfs_writeback_keys_failed,
680
681 &sysfs_trigger_gc,
682 &sysfs_prune_cache,
683#ifdef CONFIG_BCACHE_DEBUG
684 &sysfs_verify,
685 &sysfs_key_merging_disabled,
280481d0 686 &sysfs_expensive_debug_checks,
cafe5635
KO
687#endif
688 &sysfs_gc_always_rewrite,
689 &sysfs_btree_shrinker_disabled,
690 &sysfs_copy_gc_enabled,
691 NULL
692};
693KTYPE(bch_cache_set_internal);
694
695SHOW(__bch_cache)
696{
697 struct cache *ca = container_of(kobj, struct cache, kobj);
698
699 sysfs_hprint(bucket_size, bucket_bytes(ca));
700 sysfs_hprint(block_size, block_bytes(ca));
701 sysfs_print(nbuckets, ca->sb.nbuckets);
702 sysfs_print(discard, ca->discard);
703 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
704 sysfs_hprint(btree_written,
705 atomic_long_read(&ca->btree_sectors_written) << 9);
706 sysfs_hprint(metadata_written,
707 (atomic_long_read(&ca->meta_sectors_written) +
708 atomic_long_read(&ca->btree_sectors_written)) << 9);
709
710 sysfs_print(io_errors,
711 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
712
cafe5635 713 if (attr == &sysfs_cache_replacement_policy)
169ef1cf
KO
714 return bch_snprint_string_list(buf, PAGE_SIZE,
715 cache_replacement_policies,
716 CACHE_REPLACEMENT(&ca->sb));
cafe5635
KO
717
718 if (attr == &sysfs_priority_stats) {
719 int cmp(const void *l, const void *r)
720 { return *((uint16_t *) r) - *((uint16_t *) l); }
721
cafe5635
KO
722 size_t n = ca->sb.nbuckets, i, unused, btree;
723 uint64_t sum = 0;
bbc77aa7
KO
724 /* Compute 31 quantiles */
725 uint16_t q[31], *p, *cached;
cafe5635
KO
726 ssize_t ret;
727
728 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
729 if (!p)
730 return -ENOMEM;
731
732 mutex_lock(&ca->set->bucket_lock);
733 for (i = ca->sb.first_bucket; i < n; i++)
734 p[i] = ca->buckets[i].prio;
735 mutex_unlock(&ca->set->bucket_lock);
736
737 sort(p, n, sizeof(uint16_t), cmp, NULL);
738
739 while (n &&
740 !cached[n - 1])
741 --n;
742
743 unused = ca->sb.nbuckets - n;
744
745 while (cached < p + n &&
746 *cached == BTREE_PRIO)
747 cached++;
748
749 btree = cached - p;
750 n -= btree;
751
752 for (i = 0; i < n; i++)
753 sum += INITIAL_PRIO - cached[i];
754
755 if (n)
756 do_div(sum, n);
757
bbc77aa7
KO
758 for (i = 0; i < ARRAY_SIZE(q); i++)
759 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
760 (ARRAY_SIZE(q) + 1)];
cafe5635
KO
761
762 vfree(p);
763
bbc77aa7
KO
764 ret = scnprintf(buf, PAGE_SIZE,
765 "Unused: %zu%%\n"
766 "Metadata: %zu%%\n"
767 "Average: %llu\n"
768 "Sectors per Q: %zu\n"
769 "Quantiles: [",
770 unused * 100 / (size_t) ca->sb.nbuckets,
771 btree * 100 / (size_t) ca->sb.nbuckets, sum,
772 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
773
774 for (i = 0; i < ARRAY_SIZE(q); i++)
775 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
776 "%u ", q[i]);
777 ret--;
778
779 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
780
cafe5635
KO
781 return ret;
782 }
783
784 return 0;
785}
786SHOW_LOCKED(bch_cache)
787
788STORE(__bch_cache)
789{
790 struct cache *ca = container_of(kobj, struct cache, kobj);
791
792 if (attr == &sysfs_discard) {
793 bool v = strtoul_or_return(buf);
794
795 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
796 ca->discard = v;
797
798 if (v != CACHE_DISCARD(&ca->sb)) {
799 SET_CACHE_DISCARD(&ca->sb, v);
800 bcache_write_super(ca->set);
801 }
802 }
803
804 if (attr == &sysfs_cache_replacement_policy) {
169ef1cf 805 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
cafe5635
KO
806
807 if (v < 0)
808 return v;
809
810 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
811 mutex_lock(&ca->set->bucket_lock);
812 SET_CACHE_REPLACEMENT(&ca->sb, v);
813 mutex_unlock(&ca->set->bucket_lock);
814
815 bcache_write_super(ca->set);
816 }
817 }
818
cafe5635
KO
819 if (attr == &sysfs_clear_stats) {
820 atomic_long_set(&ca->sectors_written, 0);
821 atomic_long_set(&ca->btree_sectors_written, 0);
822 atomic_long_set(&ca->meta_sectors_written, 0);
823 atomic_set(&ca->io_count, 0);
824 atomic_set(&ca->io_errors, 0);
825 }
826
827 return size;
828}
829STORE_LOCKED(bch_cache)
830
831static struct attribute *bch_cache_files[] = {
832 &sysfs_bucket_size,
833 &sysfs_block_size,
834 &sysfs_nbuckets,
835 &sysfs_priority_stats,
836 &sysfs_discard,
837 &sysfs_written,
838 &sysfs_btree_written,
839 &sysfs_metadata_written,
840 &sysfs_io_errors,
841 &sysfs_clear_stats,
cafe5635
KO
842 &sysfs_cache_replacement_policy,
843 NULL
844};
845KTYPE(bch_cache);