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