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